From ef23be16e875d8dbc5aae58c191b09ada94776af Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Wed, 16 Oct 2013 12:33:29 +0200
Subject: VCard: Guarantee an empty row when editing organisation units, so the
 user can add new units.

Change-Id: Ibdc132467e69ff906c4b4bea2d12f8b5704cf74c
License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.

diff --git a/Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.cpp b/Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.cpp
index 7f6c23d..99e5f0b 100644
--- a/Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.cpp
+++ b/Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.cpp
@@ -4,12 +4,13 @@
  * See Documentation/Licenses/BSD-simplified.txt for more information.
  */
 
-#include "QtVCardOrganizationField.h"
+#include <Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.h>
+
+#include <boost/algorithm/string.hpp>
 
 #include <QGridLayout>
 #include <QHBoxLayout>
 #include <QHeaderView>
-#include <boost/algorithm/string.hpp>
 
 #include <Swift/QtUI/QtSwiftUtil.h>
 
@@ -37,6 +38,7 @@ void QtVCardOrganizationField::setupContentWidgets() {
 	itemDelegate = new QtRemovableItemDelegate(style());
 
 	unitsTreeWidget = new QTreeWidget(this);
+	connect(unitsTreeWidget->model(), SIGNAL(rowsRemoved(QModelIndex, int, int)), SLOT(handleRowsRemoved(QModelIndex,int,int)));
 	unitsTreeWidget->setColumnCount(2);
 	unitsTreeWidget->header()->setStretchLastSection(false);
 	int closeIconWidth = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, 0);
@@ -117,6 +119,14 @@ void QtVCardOrganizationField::handleEditibleChanged(bool isEditable) {
 }
 
 void QtVCardOrganizationField::handleItemChanged(QTreeWidgetItem *, int) {
+	guaranteeEmptyRow();
+}
+
+void QtVCardOrganizationField::handleRowsRemoved(const QModelIndex&, int, int) {
+	guaranteeEmptyRow();
+}
+
+void QtVCardOrganizationField::guaranteeEmptyRow() {
 	bool hasEmptyRow = false;
 	QList<QTreeWidgetItem*> rows = unitsTreeWidget->findItems("", Qt::MatchFixedString);
 	foreach(QTreeWidgetItem* row, rows) {
@@ -129,8 +139,8 @@ void QtVCardOrganizationField::handleItemChanged(QTreeWidgetItem *, int) {
 		QTreeWidgetItem* item = new QTreeWidgetItem(QStringList("") << "");
 		item->setFlags(item->flags() | Qt::ItemIsEditable);
 		unitsTreeWidget->addTopLevelItem(item);
+		unitsTreeWidget->setCurrentItem(item);
 	}
-	getTagComboBox()->hide();
 }
 
 }
diff --git a/Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.h b/Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.h
index 917e22a..47868a7 100644
--- a/Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.h
+++ b/Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.h
@@ -6,14 +6,14 @@
 
 #pragma once
 
-#include <Swiften/Elements/VCard.h>
-
 #include <QTreeWidget>
 
-#include "QtRemovableItemDelegate.h"
-#include "QtResizableLineEdit.h"
-#include "QtVCardFieldInfo.h"
-#include "QtVCardGeneralField.h"
+#include <Swiften/Elements/VCard.h>
+
+#include <Swift/QtUI/QtVCardWidget/QtRemovableItemDelegate.h>
+#include <Swift/QtUI/QtVCardWidget/QtResizableLineEdit.h>
+#include <Swift/QtUI/QtVCardWidget/QtVCardFieldInfo.h>
+#include <Swift/QtUI/QtVCardWidget/QtVCardGeneralField.h>
 
 namespace Swift {
 
@@ -39,6 +39,10 @@ class QtVCardOrganizationField : public QtVCardGeneralField {
 
 	private slots:
 		void handleItemChanged(QTreeWidgetItem*, int);
+		void handleRowsRemoved(const QModelIndex&, int, int);
+
+	private:
+		void guaranteeEmptyRow();
 
 	private:
 		QLabel* organizationLabel;
-- 
cgit v0.10.2-6-g49f6