Skip to content

Commit

Permalink
update appmenu
Browse files Browse the repository at this point in the history
  • Loading branch information
Protostars committed Oct 10, 2023
1 parent 88515b0 commit 536e875
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 18 deletions.
20 changes: 4 additions & 16 deletions Extension/App/kapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,10 @@ AppWidget *KApp::getWidget(const QString &name)
return mainWindow->findChild<AppWidget *>(name);
}

const QIcon &KApp::icon() const
const QPixmap &KApp::icon() const
{
static QIcon defaultIcon;
if (defaultIcon.isNull())
{
QPixmap iconPixmap(":/res/images/app.png");
iconPixmap = iconPixmap.scaled(256, 256, Qt::KeepAspectRatioByExpanding);
defaultIcon.addPixmap(iconPixmap, QIcon::Normal);
defaultIcon.addPixmap(iconPixmap, QIcon::Selected);
}
static QPixmap defaultIcon(":/res/images/app.png");

return appIcon.isNull() ? defaultIcon : appIcon;
}

Expand Down Expand Up @@ -471,13 +465,7 @@ QString KApp::loadAppInfo(const QString &path)
appInfo["time"] = QString::number(getLatestFileModifyTime());
if (appInfo.contains("icon"))
{
QPixmap appIconPixmap = QPixmap(appInfo["path"].toString() + "/" + appInfo["icon"].toString());
if (!appIconPixmap.isNull())
{
appIconPixmap = appIconPixmap.scaled(256, 256, Qt::KeepAspectRatioByExpanding);
appIcon.addPixmap(appIconPixmap, QIcon::Normal);
appIcon.addPixmap(appIconPixmap, QIcon::Selected);
}
appIcon = QPixmap(appInfo["path"].toString() + "/" + appInfo["icon"].toString());
}
if(appInfo.contains("min_kiko"))
{
Expand Down
4 changes: 2 additions & 2 deletions Extension/App/kapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class KApp : public QObject
QString name() const {return appInfo.value("name").toString();}
QString path() const { return appInfo.value("path").toString(); }
QString dataPath() const { return appInfo.value("dataPath").toString(); }
const QIcon &icon() const;
const QPixmap &icon() const;
qint64 time() const { return appInfo.value("time").toLongLong(); }
qint64 getLatestFileModifyTime() const;
bool isLoaded() const { return loaded; }
Expand Down Expand Up @@ -82,7 +82,7 @@ class KApp : public QObject
QVector<AppRes *> appResources;
QHash<QString, QVariant> appInfo;
QHash<QString, AppRes *> resHash;
QIcon appIcon;
QPixmap appIcon;

void setAppWindowEvent();
void loadFont();
Expand Down
57 changes: 57 additions & 0 deletions UI/appmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <QGridLayout>
#include <QPushButton>
#include <QListView>
#include <QPainter>
#include "globalobjects.h"
#include "Extension/App/appmanager.h"
#include "widgets/dialogtip.h"
Expand Down Expand Up @@ -30,6 +31,7 @@ AppMenu::AppMenu(QWidget *p, QWidget *parent)

const int appItemWidth = 64*logicalDpiX()/96;
const int appItemHeight = 64*logicalDpiY()/96 + appView->fontMetrics().height();
appView->setItemDelegate(new AppItemDelegate(appItemWidth, appItemHeight, appView));
appView->setGridSize(QSize(appItemWidth, appItemHeight));
appView->setMinimumWidth(appItemWidth * 4 + 8*logicalDpiX()/96);
appView->setMinimumHeight(appItemHeight * 2 + 4*logicalDpiX()/96);
Expand Down Expand Up @@ -65,3 +67,58 @@ void AppMenu::showMessage(const QString &content, int flag, const QVariant &)
{
dialogTip->showMessage(content, flag);
}

AppItemDelegate::AppItemDelegate(int w, int h, QObject *parent) : QStyledItemDelegate(parent), itemWidth(w), itemHeight(h)
{

}

void AppItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem viewOption(option);
initStyleOption(&viewOption,index);

if (viewOption.state.testFlag(QStyle::State_MouseOver))
{
painter->fillRect(option.rect, QColor(255, 255, 255, 60));
}
else
{
painter->fillRect(option.rect, Qt::transparent);
}

painter->save();
painter->translate(option.rect.x(), option.rect.y());

int textHeight = painter->fontMetrics().height();
const QPixmap icon = index.data(Qt::DecorationRole).value<QPixmap>();

const int imgRectW = option.rect.width() * 0.8;
const int imgRectH = imgRectW;

int imgW = imgRectW * 0.7, imgH = imgRectH * 0.7;
if (icon.width() >= icon.height())
{
imgH = (float)icon.height() / icon.width() * imgW;
}
else
{
imgW = (float)icon.width() / icon.height() * imgH;
}

const int imgX = (option.rect.width() - imgW) / 2;
const int imgY = (option.rect.height() - imgH - textHeight) / 2;

painter->drawPixmap(QRect(imgX, imgY, imgW, imgH), icon);

const int textY = (option.rect.height() - imgRectH - textHeight) / 2 + imgRectH;
const QString text = painter->fontMetrics().elidedText(index.data(Qt::DisplayRole).toString(), Qt::TextElideMode::ElideRight, option.rect.width());
painter->drawText(0, textY, option.rect.width(), textHeight, Qt::AlignCenter, text);

painter->restore();
}

QSize AppItemDelegate::sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const
{
return QSize(itemWidth, itemHeight);
}
16 changes: 16 additions & 0 deletions UI/appmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define APPMENU_H

#include <QMenu>
#include <QStyledItemDelegate>
#include "Common/notifier.h"
class DialogTip;
class AppMenu : public QMenu, public NotifyInterface
Expand All @@ -22,4 +23,19 @@ class AppMenu : public QMenu, public NotifyInterface
virtual void showMessage(const QString &content, int flag, const QVariant &);
};

class AppItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit AppItemDelegate(int w, int h, QObject *parent = nullptr);
// QAbstractItemDelegate interface
public:
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
//virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
virtual QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const;
signals:
void ItemClicked(const QModelIndex &index);
private:
int itemWidth, itemHeight;
};
#endif // APPMENU_H
1 change: 1 addition & 0 deletions res/mpvOptions
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
vf=lavfi="fps=fps=60:round=down"
hwdec=auto
demuxer-max-bytes=1024MiB
hr-seek-framedrop=yes

0 comments on commit 536e875

Please sign in to comment.