diff options
author | Jan Dalheimer <jan@dalheimer.de> | 2014-03-10 17:38:27 +0100 |
---|---|---|
committer | Jan Dalheimer <jan@dalheimer.de> | 2014-03-10 17:38:27 +0100 |
commit | 73fc9c79cff979e9023df0b1a77848c67b590681 (patch) | |
tree | 7c8be387b504eccf35e4e3422b39a78d47354e7d /gui/groupview/InstanceDelegate.cpp | |
parent | 737169d1d3ac62a7db99d51e892289aee8c1d3a3 (diff) | |
download | MultiMC-73fc9c79cff979e9023df0b1a77848c67b590681.tar MultiMC-73fc9c79cff979e9023df0b1a77848c67b590681.tar.gz MultiMC-73fc9c79cff979e9023df0b1a77848c67b590681.tar.lz MultiMC-73fc9c79cff979e9023df0b1a77848c67b590681.tar.xz MultiMC-73fc9c79cff979e9023df0b1a77848c67b590681.zip |
Instance badges. Some easter eggs and one for broken so far.
Diffstat (limited to 'gui/groupview/InstanceDelegate.cpp')
-rw-r--r-- | gui/groupview/InstanceDelegate.cpp | 85 |
1 files changed, 80 insertions, 5 deletions
diff --git a/gui/groupview/InstanceDelegate.cpp b/gui/groupview/InstanceDelegate.cpp index 8a273758..7ca4d7b4 100644 --- a/gui/groupview/InstanceDelegate.cpp +++ b/gui/groupview/InstanceDelegate.cpp @@ -18,9 +18,13 @@ #include <QTextOption> #include <QTextLayout> #include <QApplication> -#include <QtCore/qmath.h> +#include <QtMath> #include "GroupView.h" +#include "logic/BaseInstance.h" +#include "logic/lists/InstanceList.h" + +QCache<QString, QPixmap> ListViewDelegate::m_pixmapCache; // Origin: Qt static void viewItemTextLayout(QTextLayout &textLayout, int lineWidth, qreal &height, @@ -45,8 +49,6 @@ static void viewItemTextLayout(QTextLayout &textLayout, int lineWidth, qreal &he textLayout.endLayout(); } -#define QFIXED_MAX (INT_MAX / 256) - ListViewDelegate::ListViewDelegate(QObject *parent) : QStyledItemDelegate(parent) { } @@ -108,6 +110,64 @@ void drawProgressOverlay(QPainter *painter, const QStyleOptionViewItemV4 &option painter->restore(); } +void drawBadges(QPainter *painter, const QStyleOptionViewItemV4 &option, BaseInstance *instance) +{ + QList<QString> pixmaps; + for (auto flag : instance->flags()) + { + switch (flag) + { + case BaseInstance::VersionBrokenFlag: + pixmaps.append("broken"); + break; + } + } + + // 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"); + } + // end easter eggs + + static const int itemSide = 24; + static const int spacing = 1; + const int itemsPerRow = qMax(1, qFloor(double(option.rect.width() + spacing) / double(itemSide + spacing))); + const int rows = qCeil((double)pixmaps.size() / (double)itemsPerRow); + QListIterator<QString> it(pixmaps); + painter->translate(option.rect.topLeft()); + for (int y = 0; y < rows; ++y) + { + for (int x = 0; x < itemsPerRow; ++x) + { + if (!it.hasNext()) + { + return; + } + const QPixmap pixmap = ListViewDelegate::requestPixmap(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); + } + } + painter->translate(-option.rect.topLeft()); +} + static QSize viewItemTextSize(const QStyleOptionViewItemV4 *option) { QStyle *style = option->widget ? option->widget->style() : QApplication::style(); @@ -257,8 +317,14 @@ void ListViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti line.draw(painter, position); } - drawProgressOverlay(painter, opt, - index.data(GroupViewRoles::ProgressValueRole).toInt(), + auto instance = (BaseInstance*)index.data(InstanceList::InstancePointerRole) + .value<void *>(); + if (instance) + { + drawBadges(painter, opt, instance); + } + + drawProgressOverlay(painter, opt, index.data(GroupViewRoles::ProgressValueRole).toInt(), index.data(GroupViewRoles::ProgressMaximumRole).toInt()); painter->restore(); @@ -284,3 +350,12 @@ QSize ListViewDelegate::sizeHint(const QStyleOptionViewItem &option, QSize sz(100, height); return sz; } + +QPixmap ListViewDelegate::requestPixmap(const QString &key) +{ + if (!m_pixmapCache.contains(key)) + { + m_pixmapCache.insert(key, new QPixmap(":/icons/badges/" + key + ".png")); + } + return *m_pixmapCache.object(key); +} |