From 4ab20a452e0af56c7ee210f863aeae31450954cc Mon Sep 17 00:00:00 2001
From: Richard Maudsley <richard.maudsley@isode.com>
Date: Wed, 16 Jul 2014 14:47:27 +0100
Subject: Add ability to reorder highlight rules list.

Test-Information:

Add several highlight rules. Verify that the up and down buttons reorder the items in the list.

Change-Id: I6272799e2b2767ddfa01068c2ffcd1fb4651e11d

diff --git a/Swift/Controllers/HighlightManager.cpp b/Swift/Controllers/HighlightManager.cpp
index eac562f..ca0567e 100644
--- a/Swift/Controllers/HighlightManager.cpp
+++ b/Swift/Controllers/HighlightManager.cpp
@@ -103,6 +103,14 @@ void HighlightManager::removeRule(int index)
 	rules_->list_.erase(rules_->list_.begin() + index);
 }
 
+void HighlightManager::swapRules(const size_t first, const size_t second) {
+	assert(first < rules_->getSize());
+	assert(second < rules_->getSize());
+	const HighlightRule swap = rules_->getRule(first);
+	rules_->setRule(first, rules_->getRule(second));
+	rules_->setRule(second, swap);
+}
+
 void HighlightManager::storeSettings()
 {
 	storingSettings_ = true;	// don't reload settings while saving
diff --git a/Swift/Controllers/HighlightManager.h b/Swift/Controllers/HighlightManager.h
index 3da72eb..07a3fe3 100644
--- a/Swift/Controllers/HighlightManager.h
+++ b/Swift/Controllers/HighlightManager.h
@@ -31,10 +31,13 @@ namespace Swift {
 				friend class HighlightManager;
 				size_t getSize() const { return list_.size(); }
 				const HighlightRule& getRule(const size_t index) const { return list_[index]; }
-				void addRule(const HighlightRule &rule) { list_.push_back(rule); }
-				void combineRules(const HighlightRulesList &rhs) {
+				void addRule(const HighlightRule& rule) { list_.push_back(rule); }
+				void combineRules(const HighlightRulesList& rhs) {
 					list_.insert(list_.end(), rhs.list_.begin(), rhs.list_.end());
 				}
+				void setRule(const size_t index, const HighlightRule& rule) {
+					list_[index] = rule;
+				}
 			private:
 				std::vector<HighlightRule> list_;
 			};
@@ -49,6 +52,7 @@ namespace Swift {
 			void setRule(int index, const HighlightRule& rule);
 			void insertRule(int index, const HighlightRule& rule);
 			void removeRule(int index);
+			void swapRules(const size_t first, const size_t second);
 			void storeSettings();
 			void loadSettings();
 
diff --git a/Swift/QtUI/QtHighlightEditor.cpp b/Swift/QtUI/QtHighlightEditor.cpp
index 8488d7d..134155c 100644
--- a/Swift/QtUI/QtHighlightEditor.cpp
+++ b/Swift/QtUI/QtHighlightEditor.cpp
@@ -75,9 +75,9 @@ QtHighlightEditor::QtHighlightEditor(QtSettingsProvider* settings, QWidget* pare
 	/* allow selection of a custom sound file */
 	connect(ui_.soundFileButton, SIGNAL(clicked()), SLOT(selectSoundFile()));
 
-	/* if these are not needed, then they should be removed */
-	ui_.moveUpButton->setVisible(false);
-	ui_.moveDownButton->setVisible(false);
+	/* allowing reordering items */
+	connect(ui_.moveUpButton, SIGNAL(clicked()), this, SLOT(onUpButtonClicked()));
+	connect(ui_.moveDownButton, SIGNAL(clicked()), this, SLOT(onDownButtonClicked()));
 
 	setWindowTitle(tr("Highlight Rules"));
 }
@@ -195,6 +195,22 @@ void QtHighlightEditor::onDeleteButtonClicked()
 	}
 }
 
+void QtHighlightEditor::onUpButtonClicked() {
+	const size_t moveFrom = ui_.listWidget->currentRow();
+	const size_t moveTo = moveFrom - 1;
+	highlightManager_->swapRules(moveFrom, moveTo);
+	populateList();
+	selectRow(moveTo);
+}
+
+void QtHighlightEditor::onDownButtonClicked() {
+	const size_t moveFrom = ui_.listWidget->currentRow();
+	const size_t moveTo = moveFrom + 1;
+	highlightManager_->swapRules(moveFrom, moveTo);
+	populateList();
+	selectRow(moveTo);
+}
+
 void QtHighlightEditor::onCurrentRowChanged(int currentRow)
 {
 	ui_.deleteButton->setEnabled(currentRow != -1);
diff --git a/Swift/QtUI/QtHighlightEditor.h b/Swift/QtUI/QtHighlightEditor.h
index c7db464..e0595ad 100644
--- a/Swift/QtUI/QtHighlightEditor.h
+++ b/Swift/QtUI/QtHighlightEditor.h
@@ -40,6 +40,8 @@ namespace Swift {
 			void soundCustomSelect();
 			void onNewButtonClicked();
 			void onDeleteButtonClicked();
+			void onUpButtonClicked();
+			void onDownButtonClicked();
 			void onCurrentRowChanged(int currentRow);
 			void onApplyButtonClick();
 			void onCancelButtonClick();
@@ -53,7 +55,6 @@ namespace Swift {
 		private:
 			void handleOnUserSelected(const JID& jid);
 			void populateList();
-			void updateChatPreview();
 			void selectRow(int row);
 			int getSelectedRow() const;
 			HighlightRule ruleFromDialog();
-- 
cgit v0.10.2-6-g49f6