diff options
Diffstat (limited to 'gui')
-rw-r--r-- | gui/iconcache.cpp | 127 | ||||
-rw-r--r-- | gui/iconcache.h | 43 | ||||
-rw-r--r-- | gui/instancedelegate.cpp | 12 | ||||
-rw-r--r-- | gui/instancemodel.cpp | 43 | ||||
-rw-r--r-- | gui/instancemodel.h | 7 | ||||
-rw-r--r-- | gui/mainwindow.cpp | 65 | ||||
-rw-r--r-- | gui/mainwindow.h | 7 |
7 files changed, 287 insertions, 17 deletions
diff --git a/gui/iconcache.cpp b/gui/iconcache.cpp new file mode 100644 index 00000000..520a7839 --- /dev/null +++ b/gui/iconcache.cpp @@ -0,0 +1,127 @@ +#include "iconcache.h" +#include <QMap> +#include <QWebView> +#include <QWebFrame> +#include <QEventLoop> +#include <QWebElement> + +IconCache* IconCache::m_Instance = 0; +QMutex IconCache::mutex; +#define MAX_SIZE 1024 + +class Private : public QWebView +{ + Q_OBJECT + +public: + QString name; + QSize size; + QMap<QString, QIcon> icons; + +public: + Private() + { + connect(this, SIGNAL(loadFinished(bool)), this, SLOT(svgLoaded(bool))); + setFixedSize(MAX_SIZE, MAX_SIZE); + + QPalette pal = palette(); + pal.setColor(QPalette::Base, Qt::transparent); + setPalette(pal); + setAttribute(Qt::WA_OpaquePaintEvent, false); + size = QSize(128,128); + } + void renderSVGIcon(QString name); + +signals: + void svgRendered(); + +private slots: + void svgLoaded(bool ok); +}; + +void Private::svgLoaded(bool ok) +{ + if (!ok) + { + emit svgRendered(); + return; + } + // check for SVG root tag + QString root = page()->currentFrame()->documentElement().tagName(); + if (root.compare("svg", Qt::CaseInsensitive) != 0) + { + emit svgRendered(); + return; + } + + // get the size of the svg image, check if it's valid + auto elem = page()->currentFrame()->documentElement(); + double width = elem.attribute("width").toDouble(); + double height = elem.attribute("height").toDouble(); + if (width == 0.0 || height == 0.0 || width == MAX_SIZE || height == MAX_SIZE) + { + emit svgRendered(); + return; + } + + // create the target surface + QSize t = size.isValid() ? size : QSize(width, height); + QImage img(t, QImage::Format_ARGB32_Premultiplied); + img.fill(Qt::transparent); + + // prepare the painter, scale to required size + QPainter p(&img); + if(size.isValid()) + { + p.scale(size.width() / width, size.height() / height); + } + + // the best quality + p.setRenderHint(QPainter::Antialiasing); + p.setRenderHint(QPainter::TextAntialiasing); + p.setRenderHint(QPainter::SmoothPixmapTransform); + + page()->mainFrame()->render(&p,QWebFrame::ContentsLayer); + p.end(); + + icons[name] = QIcon(QPixmap::fromImage(img)); + emit svgRendered(); +} + +void Private::renderSVGIcon ( QString name ) +{ + // use event loop to wait for signal + QEventLoop loop; + this->name = name; + QString prefix = "qrc:/icons/instances/"; + QObject::connect(this, SIGNAL(svgRendered()), &loop, SLOT(quit())); + load(QUrl(prefix + name)); + loop.exec(); +} + +IconCache::IconCache():d(new Private()) +{ +} + +QIcon IconCache::getIcon ( QString name ) +{ + if(name == "default") + name = "infinity"; + { + auto iter = d->icons.find(name); + if(iter != d->icons.end()) + return *iter; + } + d->renderSVGIcon(name); + auto iter = d->icons.find(name); + if(iter != d->icons.end()) + return *iter; + + // Fallback for icons that don't exist. + QString path = ":/icons/instances/infinity"; + //path += name; + d->icons[name] = QIcon(path); + return d->icons[name]; +} + +#include "iconcache.moc"
\ No newline at end of file diff --git a/gui/iconcache.h b/gui/iconcache.h new file mode 100644 index 00000000..5c5e4142 --- /dev/null +++ b/gui/iconcache.h @@ -0,0 +1,43 @@ +#pragma once + +#include <QMutex> +#include <QtGui/QIcon> + +class Private; + +class IconCache +{ +public: + static IconCache* instance() + { + if (!m_Instance) + { + mutex.lock(); + if (!m_Instance) + m_Instance = new IconCache; + mutex.unlock(); + } + return m_Instance; + } + + static void drop() + { + mutex.lock(); + delete m_Instance; + m_Instance = 0; + mutex.unlock(); + } + + QIcon getIcon(QString name); + +private: + IconCache(); + // hide copy constructor + IconCache(const IconCache &); + // hide assign op + IconCache& operator=(const IconCache &); + static IconCache* m_Instance; + static QMutex mutex; + Private* d; +}; +
\ No newline at end of file diff --git a/gui/instancedelegate.cpp b/gui/instancedelegate.cpp index dac2dacb..86b7d399 100644 --- a/gui/instancedelegate.cpp +++ b/gui/instancedelegate.cpp @@ -33,9 +33,15 @@ ListViewDelegate::ListViewDelegate ( QObject* parent ) : QStyledItemDelegate ( p void drawSelectionRect(QPainter *painter, const QStyleOptionViewItemV4 &option, const QRect &rect) { - if (!(option.state & QStyle::State_Selected)) - return; - painter->fillRect ( rect, option.palette.brush ( QPalette::Highlight ) ); + if ((option.state & QStyle::State_Selected)) + painter->fillRect ( rect, option.palette.brush ( QPalette::Highlight ) ); + else + { + QColor backgroundColor = option.palette.color(QPalette::Background); + backgroundColor.setAlpha(160); + painter->fillRect ( rect, QBrush(backgroundColor) ); + } + } void drawFocusRect(QPainter *painter, const QStyleOptionViewItemV4 &option, const QRect &rect) diff --git a/gui/instancemodel.cpp b/gui/instancemodel.cpp index 73d0dbc1..8db985e8 100644 --- a/gui/instancemodel.cpp +++ b/gui/instancemodel.cpp @@ -1,13 +1,38 @@ #include "instancemodel.h" #include <instance.h> #include <QIcon> +#include "iconcache.h" InstanceModel::InstanceModel ( const InstanceList& instances, QObject *parent ) : QAbstractListModel ( parent ), m_instances ( &instances ) { - cachedIcon = QIcon(":/icons/multimc/scalable/apps/multimc.svg"); + currentInstancesNumber = m_instances->count(); + connect(m_instances,SIGNAL(instanceAdded(int)),this,SLOT(onInstanceAdded(int))); + connect(m_instances,SIGNAL(instanceChanged(int)),this,SLOT(onInstanceChanged(int))); + connect(m_instances,SIGNAL(invalidated()),this,SLOT(onInvalidated())); } +void InstanceModel::onInstanceAdded ( int index ) +{ + beginInsertRows(QModelIndex(), index, index); + currentInstancesNumber ++; + endInsertRows(); +} + +void InstanceModel::onInstanceChanged ( int index ) +{ + QModelIndex mx = InstanceModel::index(index); + dataChanged(mx,mx); +} + +void InstanceModel::onInvalidated() +{ + beginResetModel(); + currentInstancesNumber = m_instances->count(); + endResetModel(); +} + + int InstanceModel::rowCount ( const QModelIndex& parent ) const { Q_UNUSED ( parent ); @@ -17,7 +42,7 @@ int InstanceModel::rowCount ( const QModelIndex& parent ) const QModelIndex InstanceModel::index ( int row, int column, const QModelIndex& parent ) const { Q_UNUSED ( parent ); - if ( row < 0 || row >= m_instances->count() ) + if ( row < 0 || row >= currentInstancesNumber ) return QModelIndex(); return createIndex ( row, column, ( void* ) m_instances->at ( row ).data() ); } @@ -46,14 +71,22 @@ QVariant InstanceModel::data ( const QModelIndex& index, int role ) const } case Qt::DecorationRole: { - // FIXME: replace with an icon cache - return cachedIcon; + IconCache * ic = IconCache::instance(); + // FIXME: replace with an icon cache/renderer + /* + QString path = ":/icons/instances/"; + path += pdata->iconKey(); + QIcon icon(path); + */ + QString key = pdata->iconKey(); + return ic->getIcon(key); + //else return QIcon(":/icons/multimc/scalable/apps/multimc.svg"); } // for now. case KCategorizedSortFilterProxyModel::CategorySortRole: case KCategorizedSortFilterProxyModel::CategoryDisplayRole: { - return "IT'S A GROUP"; + return pdata->group(); } default: break; diff --git a/gui/instancemodel.h b/gui/instancemodel.h index 995c51ec..208ee68e 100644 --- a/gui/instancemodel.h +++ b/gui/instancemodel.h @@ -22,9 +22,14 @@ public: QVariant data ( const QModelIndex& index, int role ) const; Qt::ItemFlags flags ( const QModelIndex& index ) const; +public slots: + void onInstanceAdded(int index); + void onInstanceChanged(int index); + void onInvalidated(); + private: const InstanceList* m_instances; - QIcon cachedIcon; + int currentInstancesNumber; }; class InstanceProxyModel : public KCategorizedSortFilterProxyModel diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index e691c8c4..5a915e8c 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -64,11 +64,25 @@ MainWindow::MainWindow ( QWidget *parent ) : { ui->setupUi ( this ); // Create the widget - instList.loadList(); - view = new KCategorizedView ( ui->centralWidget ); drawer = new KCategoryDrawer ( view ); - + /* + QPalette pal = view->palette(); + pal.setBrush(QPalette::Base, QBrush(QPixmap(QString::fromUtf8(":/backgrounds/kitteh")))); + view->setPalette(pal); + */ + + view->setStyleSheet( + "QListView\ + {\ + background-image: url(:/backgrounds/kitteh);\ + background-attachment: fixed;\ + background-clip: padding;\ + background-position: top right;\ + background-repeat: none;\ + background-color:palette(base);\ + }"); + view->setSelectionMode ( QAbstractItemView::SingleSelection ); //view->setSpacing( KDialog::spacingHint() ); view->setCategoryDrawer ( drawer ); @@ -100,7 +114,14 @@ MainWindow::MainWindow ( QWidget *parent ) : view->setModel ( proxymodel ); connect(view, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(instanceActivated(const QModelIndex &))); - + + // Load the instances. + instList.loadList(); + // just a test + /* + instList.at(0)->setGroup("TEST GROUP"); + instList.at(0)->setName("TEST ITEM"); + */ } MainWindow::~MainWindow() @@ -125,6 +146,18 @@ void MainWindow::on_actionAddInstance_triggered() newInstDlg->exec(); } +void MainWindow::on_actionChangeInstGroup_triggered() +{ + Instance* inst = selectedInstance(); + if(inst) + { + QString name ( inst->group() ); + name = QInputDialog::getText ( this, tr ( "Group name" ), tr ( "Enter a new group name." ), QLineEdit::Normal, name ); + inst->setGroup(name); + } +} + + void MainWindow::on_actionViewInstanceFolder_triggered() { openInDefaultProgram ( globalSettings->get ( "InstanceDir" ).toString() ); @@ -195,13 +228,31 @@ void MainWindow::on_instanceView_customContextMenuRequested ( const QPoint &pos instContextMenu->exec ( view->mapToGlobal ( pos ) ); } +Instance* MainWindow::selectedInstance() +{ + QAbstractItemView * iv = view; + auto smodel = iv->selectionModel(); + QModelIndex mindex; + if(smodel->hasSelection()) + { + auto rows = smodel->selectedRows(); + mindex = rows.at(0); + } + + if(mindex.isValid()) + { + return (Instance *) mindex.data(InstanceModel::InstancePointerRole).value<void *>(); + } + else + return nullptr; +} + void MainWindow::on_actionLaunchInstance_triggered() { - QModelIndex index = view->currentIndex(); - if(index.isValid()) + Instance* inst = selectedInstance(); + if(inst) { - Instance * inst = (Instance *) index.data(InstanceModel::InstancePointerRole).value<void *>(); doLogin(inst->id()); } } diff --git a/gui/mainwindow.h b/gui/mainwindow.h index c2dedf74..5d990639 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -44,14 +44,19 @@ public: // Browser Dialog void openWebPage(QUrl url); + +private: + Instance *selectedInstance(); private slots: void on_actionAbout_triggered(); void on_actionAddInstance_triggered(); - void on_actionViewInstanceFolder_triggered(); + void on_actionChangeInstGroup_triggered(); + void on_actionViewInstanceFolder_triggered(); + void on_actionRefresh_triggered(); void on_actionViewCentralModsFolder_triggered(); |