From ce93c7316978a1c9da3ec8cbf3e26ed8786cebc0 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sat, 30 Jun 2012 21:47:04 +0100
Subject: Trying some changes to label handling


diff --git a/Swift/QtUI/QtChatWindow.cpp b/Swift/QtUI/QtChatWindow.cpp
index d3abaa6..5a39b34 100644
--- a/Swift/QtUI/QtChatWindow.cpp
+++ b/Swift/QtUI/QtChatWindow.cpp
@@ -5,7 +5,6 @@
  */
 
 #include "QtChatWindow.h"
-#include "QtSwiftUtil.h"
 #include "Swift/Controllers/Roster/Roster.h"
 #include "Swift/Controllers/Roster/RosterItem.h"
 #include "Swift/Controllers/Roster/ContactRosterItem.h"
@@ -32,6 +31,7 @@
 #include <boost/lexical_cast.hpp>
 
 #include <QLabel>
+#include <qdebug.h>
 #include <QMessageBox>
 #include <QInputDialog>
 #include <QApplication>
@@ -60,6 +60,7 @@ const QString QtChatWindow::ButtonFileTransferSendRequest = QString("filetransfe
 const QString QtChatWindow::ButtonFileTransferAcceptRequest = QString("filetransfer-acceptrequest");
 const QString QtChatWindow::ButtonMUCInvite = QString("mucinvite");
 
+
 QtChatWindow::QtChatWindow(const QString &contact, QtChatTheme* theme, UIEventStream* eventStream, SettingsProvider* settings, QMap<QString, QString> emoticons) : QtTabbable(), contact_(contact), previousMessageWasSelf_(false), previousMessageKind_(PreviosuMessageWasNone), eventStream_(eventStream), emoticons_(emoticons) {
 	settings_ = settings;
 	unreadCount_ = 0;
@@ -69,6 +70,7 @@ QtChatWindow::QtChatWindow(const QString &contact, QtChatTheme* theme, UIEventSt
 	affiliationEditor_ = NULL;
 	theme_ = theme;
 	isCorrection_ = false;
+	labelModel_ = NULL;
 	correctionEnabled_ = Maybe;
 	showEmoticons_ = true;
 	updateTitleWithUnreadCount();
@@ -127,24 +129,27 @@ QtChatWindow::QtChatWindow(const QString &contact, QtChatTheme* theme, UIEventSt
 	connect(logRosterSplitter_, SIGNAL(splitterMoved(int, int)), this, SLOT(handleSplitterMoved(int, int)));
 
 	QWidget* midBar = new QWidget(this);
-	layout->addWidget(midBar);
+	//layout->addWidget(midBar);
 	midBar->setAutoFillBackground(true);
 	QHBoxLayout *midBarLayout = new QHBoxLayout(midBar);
 	midBarLayout->setContentsMargins(0,0,0,0);
 	midBarLayout->setSpacing(2);
-	midBarLayout->addStretch();
+	//midBarLayout->addStretch();
 
 	labelsWidget_ = new QComboBox(this);
 	labelsWidget_->setFocusPolicy(Qt::NoFocus);
 	labelsWidget_->hide();
 	labelsWidget_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
 	midBarLayout->addWidget(labelsWidget_,0);
+	connect(labelsWidget_, SIGNAL(currentIndexChanged(int)), this, SLOT(handleCurrentLabelChanged(int)));
+	defaultLabelsPalette_ = labelsWidget_->palette();
 
 	QHBoxLayout* inputBarLayout = new QHBoxLayout();
 	inputBarLayout->setContentsMargins(0,0,0,0);
 	inputBarLayout->setSpacing(2);
 	input_ = new QtTextEdit(this);
 	input_->setAcceptRichText(false);
+	inputBarLayout->addWidget(midBar);
 	inputBarLayout->addWidget(input_);
 	correctingLabel_ = new QLabel(tr("Correcting"), this);
 	inputBarLayout->addWidget(correctingLabel_);
@@ -332,23 +337,42 @@ void QtChatWindow::setRosterModel(Roster* roster) {
 }
 
 void QtChatWindow::setAvailableSecurityLabels(const std::vector<SecurityLabelsCatalog::Item>& labels) {
-	availableLabels_ = labels;
-	labelsWidget_->clear();
+	delete labelModel_;
+	labelModel_ = new LabelModel();
+	labelModel_->availableLabels_ = labels;
 	int i = 0;
 	int defaultIndex = 0;
+	labelsWidget_->setModel(labelModel_);
 	foreach (SecurityLabelsCatalog::Item label, labels) {
-		std::string selector = label.getSelector();
-		std::string displayMarking = label.getLabel() ? label.getLabel()->getDisplayMarking() : "";
-		QString labelName = selector.empty() ? displayMarking.c_str() : selector.c_str();
-		labelsWidget_->addItem(labelName, QVariant(i));
 		if (label.getIsDefault()) {
 			defaultIndex = i;
+			break;
 		}
 		i++;
 	}
 	labelsWidget_->setCurrentIndex(defaultIndex);
 }
 
+void QtChatWindow::handleCurrentLabelChanged(int index) {
+	if (static_cast<size_t>(index) >= labelModel_->availableLabels_.size()) {
+		qDebug() << "User selected a label that doesn't exist";
+		return;
+	}
+	const SecurityLabelsCatalog::Item& label = labelModel_->availableLabels_[index];
+	if (label.getLabel()) {
+		qDebug() << "Displaying label colours";
+		QPalette palette = labelsWidget_->palette();
+		//palette.setColor(QPalette::Base, P2QSTRING(label.getLabel()->getBackgroundColor()));
+		palette.setColor(labelsWidget_->backgroundRole(), P2QSTRING(label.getLabel()->getBackgroundColor()));
+		palette.setColor(labelsWidget_->foregroundRole(), P2QSTRING(label.getLabel()->getForegroundColor()));
+		labelsWidget_->setPalette(palette);
+		labelsWidget_->setAutoFillBackground(true);
+	}
+	else {
+		labelsWidget_->setAutoFillBackground(false);
+		labelsWidget_->setPalette(defaultLabelsPalette_);
+	}
+}
 
 void QtChatWindow::setSecurityLabelsError() {
 	labelsWidget_->setEnabled(false);
@@ -369,8 +393,8 @@ void QtChatWindow::setCorrectionEnabled(Tristate enabled) {
 
 SecurityLabelsCatalog::Item QtChatWindow::getSelectedSecurityLabel() {
 	assert(labelsWidget_->isEnabled());
-	assert(labelsWidget_->currentIndex() >= 0 && static_cast<size_t>(labelsWidget_->currentIndex()) < availableLabels_.size());
-	return availableLabels_[labelsWidget_->currentIndex()];
+	assert(labelsWidget_->currentIndex() >= 0 && static_cast<size_t>(labelsWidget_->currentIndex()) < labelModel_->availableLabels_.size());
+	return labelModel_->availableLabels_[labelsWidget_->currentIndex()];
 }
 
 void QtChatWindow::closeEvent(QCloseEvent* event) {
diff --git a/Swift/QtUI/QtChatWindow.h b/Swift/QtUI/QtChatWindow.h
index 5081681..3e58f8c 100644
--- a/Swift/QtUI/QtChatWindow.h
+++ b/Swift/QtUI/QtChatWindow.h
@@ -9,6 +9,7 @@
 #include <Swift/Controllers/UIInterfaces/ChatWindow.h>
 #include <Swift/QtUI/QtMUCConfigurationWindow.h>
 #include <Swift/QtUI/QtAffiliationEditor.h>
+#include <Swift/QtUI/QtSwiftUtil.h>
 
 #include <QtTabbable.h>
 
@@ -36,6 +37,38 @@ namespace Swift {
 	class QtChatWindowJSBridge;
 	class SettingsProvider;
 
+	class LabelModel : public QAbstractListModel {
+		Q_OBJECT
+		public:
+			LabelModel(QObject* parent = NULL) : QAbstractListModel(parent) {}
+
+			virtual int rowCount(const QModelIndex& /*index*/) const {
+				return static_cast<int>(availableLabels_.size());
+			}
+
+			virtual QVariant data(const QModelIndex& index, int role) const {
+				if (!index.isValid()) {
+					return QVariant();
+				}
+				SecurityLabel::ref label = availableLabels_[index.row()].getLabel();
+				if (label && role == Qt::TextColorRole) {
+					return P2QSTRING(label->getForegroundColor());
+				}
+				if (label && role == Qt::TextColorRole) {
+					return P2QSTRING(label->getBackgroundColor());
+				}
+				if (role == Qt::DisplayRole) {
+					std::string selector = availableLabels_[index.row()].getSelector();
+					std::string displayMarking = label ? label->getDisplayMarking() : "";
+					QString labelName = selector.empty() ? displayMarking.c_str() : selector.c_str();
+					return labelName;
+				}
+				return QVariant();
+			}
+
+			std::vector<SecurityLabelsCatalog::Item> availableLabels_;
+	};
+
 	class QtChatWindow : public QtTabbable, public ChatWindow {
 		Q_OBJECT
 
@@ -132,6 +165,7 @@ namespace Swift {
 
 			void handleHTMLButtonClicked(QString id, QString arg1, QString arg2, QString arg3);
 			void handleAffiliationEditorAccepted();
+			void handleCurrentLabelChanged(int);
 
 		private:
 			enum PreviousMessageKind {
@@ -172,7 +206,6 @@ namespace Swift {
 			TabComplete* completer_;
 			QLineEdit* subject_;
 			QPushButton* actionButton_;
-			std::vector<SecurityLabelsCatalog::Item> availableLabels_;
 			bool isCorrection_;
 			bool previousMessageWasSelf_;
 			PreviousMessageKind previousMessageKind_;
@@ -193,5 +226,7 @@ namespace Swift {
 			std::vector<ChatWindow::RoomAction> availableRoomActions_;
 			QMap<QString, QString> emoticons_;
 			bool showEmoticons_;
+			QPalette defaultLabelsPalette_;
+			LabelModel* labelModel_;
 	};
 }
-- 
cgit v0.10.2-6-g49f6