From 5db79919b3140673f66c9da5965b54671e8f4b6a Mon Sep 17 00:00:00 2001
From: "Javier S. Pedro" <maemo@javispedro.com>
Date: Mon, 17 Oct 2011 00:08:17 +0200
Subject: Simplify notification rendering code usign QTextOption

---
 metawatch/metawatchdigital.cpp | 44 ++++++++++++++++++------------------------
 1 file changed, 19 insertions(+), 25 deletions(-)

(limited to 'metawatch')

diff --git a/metawatch/metawatchdigital.cpp b/metawatch/metawatchdigital.cpp
index df7d015..8a81f97 100644
--- a/metawatch/metawatchdigital.cpp
+++ b/metawatch/metawatchdigital.cpp
@@ -260,7 +260,8 @@ void MetaWatchDigital::renderNotification(Notification *n)
 	const int iconY = margin;
 	const int titleY = margin*2 + iconH;
 	const int dateX = x + iconW + margin;
-	int textFlags;
+	QTextOption option;
+	QRect rect, titleRect;
 	QString text;
 
 	p.begin(this);
@@ -269,40 +270,33 @@ void MetaWatchDigital::renderNotification(Notification *n)
 	p.drawImage(x, iconY, icon);
 
 	p.setPen(Qt::black);
+	option.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
+	option.setAlignment(Qt::AlignRight | Qt::AlignVCenter);
 
+	// Render "N minutes ago"
 	p.setFont(sf);
-	textFlags = Qt::AlignRight | Qt::AlignVCenter | Qt::TextWordWrap;
+	rect.setRect(dateX, iconY, (screenWidth - dateX) - margin, iconH);
 	text = n->displayTime();
-	QRect dateRect(dateX, iconY, (screenWidth - dateX) - margin, iconH);
-	p.drawText(dateRect, textFlags, text);
+	p.drawText(rect, text, option);
 
+	option.setAlignment(Qt::AlignLeft | Qt::AlignTop);
+
+	// Render title
 	p.setFont(lf);
-	textFlags = Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap;
+	rect.setRect(x, titleY, screenWidth - x*2, screenHeight - titleY);
 	text = n->title();
+	titleRect = p.boundingRect(rect, text, option).toRect();
+	p.drawText(rect, text, option);
 
-	QRect titleMaxRect(x, titleY, screenWidth - x*2, screenHeight - titleY);
-	QRect titleRect = p.boundingRect(titleMaxRect, textFlags, text);
-	if (titleRect.width() > titleMaxRect.width()) {
-		textFlags = Qt::AlignLeft | Qt::AlignTop | Qt::TextWrapAnywhere;
-		titleRect = p.boundingRect(titleMaxRect, textFlags, text);
-	}
-
-	p.drawText(titleMaxRect, textFlags, text);
-
-	p.setFont(mf);
-	textFlags = Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap;
-	text = n->body();
-
+	// Do not try to draw body if title was large
 	int bodyY = titleRect.y() + titleRect.height();
 	if (bodyY >= screenHeight) return;
 
-	QRect bodyMaxRect(x, bodyY, titleMaxRect.width(), screenHeight - bodyY);
-	QRect bodyRect = p.boundingRect(bodyMaxRect, textFlags, text);
-	if (bodyRect.width() > bodyMaxRect.width()) {
-		textFlags = Qt::AlignLeft | Qt::AlignTop | Qt::TextWrapAnywhere;
-	}
-
-	p.drawText(bodyMaxRect, textFlags, text);
+	// Render body
+	p.setFont(mf);
+	rect.setRect(x, bodyY, screenWidth - x*2, screenHeight - bodyY);
+	text = n->body();
+	p.drawText(rect, text, option);
 
 	p.end();
 	_paintMode = _currentMode;
-- 
cgit v1.2.3