From c2833f8afa8db40cfebfe51ecf5bb42fd87d8dca Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Wed, 18 Sep 2013 23:47:37 +0200
Subject: VCard UI fixes.

Fix QtRemovableItemDelegate and QtCloseButton rendering on Windows.
Fix organization's unit field to allow creation of new units.
Fix QtResizableLineEdit's sizeHint to be relative to font metrics.

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

diff --git a/Swift/QtUI/QtBlockListEditorWindow.cpp b/Swift/QtUI/QtBlockListEditorWindow.cpp
index 63e8d1f..150b634 100644
--- a/Swift/QtUI/QtBlockListEditorWindow.cpp
+++ b/Swift/QtUI/QtBlockListEditorWindow.cpp
@@ -77,7 +77,7 @@ QtBlockListEditorWindow::QtBlockListEditorWindow() : QWidget(), ui(new Ui::QtBlo
 
 	ui->blockListTreeWidget->setColumnCount(2);
 	ui->blockListTreeWidget->header()->setStretchLastSection(false);
-	int closeIconWidth = ui->blockListTreeWidget->fontMetrics().height();
+	int closeIconWidth = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, 0);
 	ui->blockListTreeWidget->header()->resizeSection(1, closeIconWidth);
 
 #if QT_VERSION >= 0x050000
diff --git a/Swift/QtUI/QtVCardWidget/QtCloseButton.cpp b/Swift/QtUI/QtVCardWidget/QtCloseButton.cpp
index a6afe81..ebd62bc 100644
--- a/Swift/QtUI/QtVCardWidget/QtCloseButton.cpp
+++ b/Swift/QtUI/QtVCardWidget/QtCloseButton.cpp
@@ -14,25 +14,11 @@
 namespace Swift {
 
 QtCloseButton::QtCloseButton(QWidget *parent) : QAbstractButton(parent) {
-	lightPixmap = QPixmap(12,12);
-	lightPixmap.fill(QColor(0,0,0,0));
-	QStyleOption opt;
-	opt.init(this);
-	opt.state = QStyle::State(0);
-	opt.state |= QStyle::State_MouseOver;
-	QPainter lightPixmapPainter(&lightPixmap);
-	style()->drawPrimitive(QStyle::PE_IndicatorTabClose, &opt, &lightPixmapPainter);
 
-	darkPixmap = QPixmap(12,12);
-	darkPixmap.fill(QColor(0,0,0,0));
-	opt.init(this);
-	opt.state = QStyle::State(0);
-	QPainter darkPixmapPainter(&darkPixmap);
-	style()->drawPrimitive(QStyle::PE_IndicatorTabClose, &opt, &darkPixmapPainter);
 }
 
 QSize QtCloseButton::sizeHint() const {
-	return QSize(width(), height());
+	return QSize(style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, 0), style()->pixelMetric(QStyle::PM_TabCloseIndicatorHeight, 0, 0));
 }
 
 bool QtCloseButton::event(QEvent *e) {
@@ -45,11 +31,15 @@ bool QtCloseButton::event(QEvent *e) {
 void QtCloseButton::paintEvent(QPaintEvent *) {
 	QPainter painter(this);
 	painter.setRenderHint(QPainter::HighQualityAntialiasing);
-	if (underMouse()) {
-		painter.drawPixmap(0, 0, height(), height(), darkPixmap);
-	} else {
-		painter.drawPixmap(0, 0, height(), height(), lightPixmap);
+	QStyleOption opt;
+	opt.init(this);
+	opt.state |= QStyle::State_AutoRaise;
+	if (underMouse() && !isDown()) {
+		opt.state |= QStyle::State_Raised;
+	} else if (isDown()) {
+		opt.state |= QStyle::State_Sunken;
 	}
+	style()->drawPrimitive(QStyle::PE_IndicatorTabClose, &opt, &painter, this);
 }
 
 }
diff --git a/Swift/QtUI/QtVCardWidget/QtCloseButton.h b/Swift/QtUI/QtVCardWidget/QtCloseButton.h
index 6ce8d30..cb92e12 100644
--- a/Swift/QtUI/QtVCardWidget/QtCloseButton.h
+++ b/Swift/QtUI/QtVCardWidget/QtCloseButton.h
@@ -19,10 +19,6 @@ namespace Swift {
 		protected:
 			virtual bool event(QEvent *e);
 			virtual void paintEvent(QPaintEvent* );
-
-		private:
-			QPixmap lightPixmap;
-			QPixmap darkPixmap;
 	};
 
 }
diff --git a/Swift/QtUI/QtVCardWidget/QtRemovableItemDelegate.cpp b/Swift/QtUI/QtVCardWidget/QtRemovableItemDelegate.cpp
index 90520ad..e7e3175 100644
--- a/Swift/QtUI/QtVCardWidget/QtRemovableItemDelegate.cpp
+++ b/Swift/QtUI/QtVCardWidget/QtRemovableItemDelegate.cpp
@@ -5,7 +5,7 @@
  */
 
 #include "QtRemovableItemDelegate.h"
-
+#include <Swiften/Base/Platform.h>
 #include <QEvent>
 #include <QPainter>
 
@@ -15,13 +15,21 @@ QtRemovableItemDelegate::QtRemovableItemDelegate(const QStyle* style) : style(st
 
 }
 
-void QtRemovableItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
+void QtRemovableItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex&) const {
 	QStyleOption opt;
-	opt.state = QStyle::State(0);
-	opt.state |= QStyle::State_MouseOver;
+	opt.state = option.state;
+	opt.state |= QStyle::State_AutoRaise;
+	if (option.state.testFlag(QStyle::State_MouseOver)) {
+		opt.state |= QStyle::State_Raised;
+	}
+	opt.rect = option.rect;
 	painter->save();
-	drawBackground(painter, option, index);
-	painter->translate(option.rect.x(), option.rect.y()+(option.rect.height() - 12)/2);
+	painter->fillRect(option.rect, option.state & QStyle::State_Selected ? option.palette.highlight() : option.palette.base());
+#ifdef SWIFTEN_PLATFORM_MACOSX
+	// workaround for Qt not painting relative to the cell we're in, on OS X
+	int height = style->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, 0);
+	painter->translate(option.rect.x(), option.rect.y() + (option.rect.height() - height)/2);
+#endif
 	style->drawPrimitive(QStyle::PE_IndicatorTabClose, &opt, painter);
 	painter->restore();
 }
@@ -39,4 +47,9 @@ bool QtRemovableItemDelegate::editorEvent(QEvent* event, QAbstractItemModel* mod
 	}
 }
 
+QSize QtRemovableItemDelegate::sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const {
+	QSize size(style->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, 0), style->pixelMetric(QStyle::PM_TabCloseIndicatorHeight, 0, 0));
+	return size;
+}
+
 }
diff --git a/Swift/QtUI/QtVCardWidget/QtRemovableItemDelegate.h b/Swift/QtUI/QtVCardWidget/QtRemovableItemDelegate.h
index 3d99ad8..75137e1 100644
--- a/Swift/QtUI/QtVCardWidget/QtRemovableItemDelegate.h
+++ b/Swift/QtUI/QtVCardWidget/QtRemovableItemDelegate.h
@@ -16,6 +16,7 @@ class QtRemovableItemDelegate : public QItemDelegate {
 
 		virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex&) const;
 		virtual QWidget* createEditor(QWidget*, const QStyleOptionViewItem&, const QModelIndex&) const;
+		virtual QSize sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const;
 
 	protected:
 		virtual bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index);
diff --git a/Swift/QtUI/QtVCardWidget/QtResizableLineEdit.cpp b/Swift/QtUI/QtVCardWidget/QtResizableLineEdit.cpp
index efe04dc..4f1d3ab 100644
--- a/Swift/QtUI/QtVCardWidget/QtResizableLineEdit.cpp
+++ b/Swift/QtUI/QtVCardWidget/QtResizableLineEdit.cpp
@@ -11,9 +11,7 @@ namespace Swift {
 QtResizableLineEdit::QtResizableLineEdit(QWidget* parent) :
 	QLineEdit(parent) {
 	connect(this, SIGNAL(textChanged(QString)), SLOT(textChanged(QString)));
-	setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
-	int marginHeight = 6;
-	setMaximumHeight(fontMetrics().height() + marginHeight);
+	setMinimumWidth(30);
 }
 
 QtResizableLineEdit::~QtResizableLineEdit() {
@@ -34,12 +32,16 @@ void QtResizableLineEdit::setEditable(const bool editable) {
 
 QSize QtResizableLineEdit::sizeHint() const {
 	int horizontalMargin = 10;
+	int verticalMargin = 6;
+	QSize textDimensions;
 #if QT_VERSION >= 0x040700
-	int w = fontMetrics().boundingRect(text().isEmpty() ? placeholderText() : text()).width() + horizontalMargin;
+	textDimensions = fontMetrics().boundingRect(text().isEmpty() ? placeholderText() : text()).size();
 #else
-	int w = fontMetrics().boundingRect(text().isEmpty() ? QString("   ") : text()).width() + horizontalMargin;
+	textDimensions = fontMetrics().boundingRect(text().isEmpty() ? QString("   ") : text()).size();
 #endif
-	return QSize(w, height());
+	textDimensions.setWidth(textDimensions.width() + horizontalMargin);
+	textDimensions.setHeight(textDimensions.height() + verticalMargin);
+	return textDimensions;
 }
 
 void QtResizableLineEdit::textChanged(QString) {
diff --git a/Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.cpp b/Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.cpp
index e399885..9ecc8e0 100644
--- a/Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.cpp
+++ b/Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.cpp
@@ -39,7 +39,7 @@ void QtVCardOrganizationField::setupContentWidgets() {
 	unitsTreeWidget = new QTreeWidget(this);
 	unitsTreeWidget->setColumnCount(2);
 	unitsTreeWidget->header()->setStretchLastSection(false);
-	int closeIconWidth = unitsTreeWidget->fontMetrics().height();
+	int closeIconWidth = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, 0);
 	unitsTreeWidget->header()->resizeSection(1, closeIconWidth);
 
 #if QT_VERSION >= 0x050000
@@ -76,6 +76,10 @@ void QtVCardOrganizationField::setOrganization(const VCard::Organization& organi
 		item->setFlags(item->flags() | Qt::ItemIsEditable);
 		unitsTreeWidget->addTopLevelItem(item);
 	}
+
+	QTreeWidgetItem* item = new QTreeWidgetItem(QStringList("") << "");
+	item->setFlags(item->flags() | Qt::ItemIsEditable);
+	unitsTreeWidget->addTopLevelItem(item);
 }
 
 VCard::Organization QtVCardOrganizationField::getOrganization() const {
@@ -88,10 +92,6 @@ VCard::Organization QtVCardOrganizationField::getOrganization() const {
 		}
 	}
 
-	QTreeWidgetItem* item = new QTreeWidgetItem(QStringList("") << "");
-	item->setFlags(item->flags() | Qt::ItemIsEditable);
-	unitsTreeWidget->addTopLevelItem(item);
-
 	return organization;
 }
 
-- 
cgit v0.10.2-6-g49f6