From f9d94a45eebd5fb7b14d4bcc6ad77cb96ecdd771 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= <peterix@gmail.com>
Date: Thu, 10 Nov 2016 02:54:53 +0100
Subject: NOISSUE allow using icon themes for instances and instance badges

---
 application/groupview/InstanceDelegate.cpp | 76 ++++++++++--------------------
 application/groupview/InstanceDelegate.h   |  5 --
 2 files changed, 25 insertions(+), 56 deletions(-)

(limited to 'application/groupview')

diff --git a/application/groupview/InstanceDelegate.cpp b/application/groupview/InstanceDelegate.cpp
index b88f9a8a..532b9d5b 100644
--- a/application/groupview/InstanceDelegate.cpp
+++ b/application/groupview/InstanceDelegate.cpp
@@ -23,8 +23,7 @@
 #include "GroupView.h"
 #include "BaseInstance.h"
 #include "InstanceList.h"
-
-QCache<QString, QPixmap> ListViewDelegate::m_pixmapCache;
+#include <xdgicon.h>
 
 // Origin: Qt
 static void viewItemTextLayout(QTextLayout &textLayout, int lineWidth, qreal &height,
@@ -110,46 +109,21 @@ void drawProgressOverlay(QPainter *painter, const QStyleOptionViewItemV4 &option
 	painter->restore();
 }
 
-void drawBadges(QPainter *painter, const QStyleOptionViewItemV4 &option, BaseInstance *instance)
+void drawBadges(QPainter *painter, const QStyleOptionViewItemV4 &option, BaseInstance *instance, QIcon::Mode mode, QIcon::State state)
 {
 	QList<QString> pixmaps;
 	if (instance->isRunning())
 	{
 		pixmaps.append("status-running");
 	}
-	else if (instance->hasCrashed())
+	else if (instance->hasCrashed() || instance->hasVersionBroken())
 	{
 		pixmaps.append("status-bad");
 	}
-	if (instance->hasVersionBroken())
-	{
-		pixmaps.append("broken");
-	}
 	if (instance->hasUpdateAvailable())
 	{
-		pixmaps.append("updateavailable");
-	}
-
-	// begin easter eggs
-	if (instance->name().contains("btw", Qt::CaseInsensitive) ||
-		instance->name().contains("better then wolves", Qt::CaseInsensitive) ||
-		instance->name().contains("better than wolves", Qt::CaseInsensitive))
-	{
-		pixmaps.append("herobrine");
-	}
-	if (instance->name().contains("direwolf", Qt::CaseInsensitive))
-	{
-		pixmaps.append("enderman");
-	}
-	if (instance->name().contains("kitten", Qt::CaseInsensitive))
-	{
-		pixmaps.append("kitten");
-	}
-	if (instance->name().contains("derp", Qt::CaseInsensitive))
-	{
-		pixmaps.append("derp");
+		pixmaps.append("checkupdate");
 	}
-	// end easter eggs
 
 	static const int itemSide = 24;
 	static const int spacing = 1;
@@ -165,11 +139,18 @@ void drawBadges(QPainter *painter, const QStyleOptionViewItemV4 &option, BaseIns
 			{
 				return;
 			}
-			const QPixmap pixmap = ListViewDelegate::requestBadgePixmap(it.next()).scaled(
-				itemSide, itemSide, Qt::KeepAspectRatio, Qt::FastTransformation);
-			painter->drawPixmap(option.rect.width() - x * itemSide + qMax(x - 1, 0) * spacing - itemSide,
-								y * itemSide + qMax(y - 1, 0) * spacing, itemSide, itemSide,
-								pixmap);
+			// FIXME: inject this.
+			auto icon = XdgIcon::fromTheme(it.next());
+			// opt.icon.paint(painter, iconbox, Qt::AlignCenter, mode, state);
+			const QPixmap pixmap;
+			// itemSide
+			QRect badgeRect(
+				option.rect.width() - x * itemSide + qMax(x - 1, 0) * spacing - itemSide,
+				y * itemSide + qMax(y - 1, 0) * spacing,
+				itemSide,
+				itemSide
+			);
+			icon.paint(painter, badgeRect, Qt::AlignCenter, mode, state);
 		}
 	}
 	painter->translate(-option.rect.topLeft());
@@ -274,15 +255,16 @@ void ListViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
 		*/
 	}
 
+	// icon mode and state, also used for badges
+	QIcon::Mode mode = QIcon::Normal;
+	if (!(opt.state & QStyle::State_Enabled))
+		mode = QIcon::Disabled;
+	else if (opt.state & QStyle::State_Selected)
+		mode = QIcon::Selected;
+	QIcon::State state = opt.state & QStyle::State_Open ? QIcon::On : QIcon::Off;
+
 	// draw the icon
 	{
-		QIcon::Mode mode = QIcon::Normal;
-		if (!(opt.state & QStyle::State_Enabled))
-			mode = QIcon::Disabled;
-		else if (opt.state & QStyle::State_Selected)
-			mode = QIcon::Selected;
-		QIcon::State state = opt.state & QStyle::State_Open ? QIcon::On : QIcon::Off;
-
 		iconbox.setHeight(iconSize);
 		opt.icon.paint(painter, iconbox, Qt::AlignCenter, mode, state);
 	}
@@ -329,7 +311,7 @@ void ListViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
 			.value<void *>();
 	if (instance)
 	{
-		drawBadges(painter, opt, instance);
+		drawBadges(painter, opt, instance, mode, state);
 	}
 
 	drawProgressOverlay(painter, opt, index.data(GroupViewRoles::ProgressValueRole).toInt(),
@@ -359,11 +341,3 @@ QSize ListViewDelegate::sizeHint(const QStyleOptionViewItem &option,
 	return sz;
 }
 
-QPixmap ListViewDelegate::requestBadgePixmap(const QString &key)
-{
-	if (!m_pixmapCache.contains(key))
-	{
-		m_pixmapCache.insert(key, new QPixmap(":/icons/badges/" + key + ".png"));
-	}
-	return *m_pixmapCache.object(key);
-}
diff --git a/application/groupview/InstanceDelegate.h b/application/groupview/InstanceDelegate.h
index 2b5439cb..f62df99e 100644
--- a/application/groupview/InstanceDelegate.h
+++ b/application/groupview/InstanceDelegate.h
@@ -23,13 +23,8 @@ class ListViewDelegate : public QStyledItemDelegate
 public:
 	explicit ListViewDelegate(QObject *parent = 0);
 
-	static QPixmap requestBadgePixmap(const QString &key);
-
 protected:
 	void paint(QPainter *painter, const QStyleOptionViewItem &option,
 			   const QModelIndex &index) const;
 	QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
-
-private:
-	static QCache<QString, QPixmap> m_pixmapCache;
 };
-- 
cgit v1.2.3