From aa5f787ac73c9e0db0b67c977dbf55a6fe3edf7c Mon Sep 17 00:00:00 2001 From: kateleet Date: Sun, 5 Dec 2021 15:33:19 +0800 Subject: [PATCH] Add send to desktop option --- qml/GridItemDelegate.qml | 24 ++++++++++++++ src/appitem.cpp | 2 ++ src/appitem.h | 2 ++ src/launchermodel.cpp | 70 +++++++++++++++++++++++++++++----------- src/launchermodel.h | 6 ++-- src/processprovider.cpp | 4 +++ translations/en_US.ts | 17 ++++++---- translations/zh_CN.ts | 23 +++++++------ 8 files changed, 112 insertions(+), 36 deletions(-) diff --git a/qml/GridItemDelegate.qml b/qml/GridItemDelegate.qml index 9817437..01ccb03 100755 --- a/qml/GridItemDelegate.qml +++ b/qml/GridItemDelegate.qml @@ -122,6 +122,12 @@ Item { onTriggered: launcherModel.sendToDock(model.appId) } + MenuItem { + id: sendToDesktop + text: qsTr("Send to desktop") + onTriggered: launcherModel.sendToDesktop(model.appId) + } + MenuItem { id: removeFromDock text: qsTr("Remove from dock") @@ -178,6 +184,24 @@ Item { text: label.text } + Rectangle { + id: newInstallPoint + + width: 6 + height: 6 + + visible: model.newInstalled + + anchors { + horizontalCenter: parent.horizontalCenter + bottom: label.top + bottomMargin: FishUI.Units.smallSpacing + } + + color: FishUI.Theme.highlightColor + radius: height / 2 + } + Label { id: label diff --git a/src/appitem.cpp b/src/appitem.cpp index e5234ee..dfabc33 100644 --- a/src/appitem.cpp +++ b/src/appitem.cpp @@ -20,6 +20,7 @@ #include "appitem.h" AppItem::AppItem() + : newInstalled(false) { } @@ -31,6 +32,7 @@ AppItem::AppItem(const AppItem &info) , comment(info.comment) , iconName(info.iconName) , args(info.args) + , newInstalled(false) { } diff --git a/src/appitem.h b/src/appitem.h index 255041c..f6779fe 100644 --- a/src/appitem.h +++ b/src/appitem.h @@ -41,6 +41,8 @@ public: QString comment; QString iconName; QStringList args; + + bool newInstalled; }; Q_DECLARE_METATYPE(AppItem) diff --git a/src/launchermodel.cpp b/src/launchermodel.cpp index 91c49d0..ae9a589 100755 --- a/src/launchermodel.cpp +++ b/src/launchermodel.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -47,7 +48,7 @@ LauncherModel::LauncherModel(QObject *parent) : QAbstractListModel(parent) , m_settings("cutefishos", "launcher-applist", this) , m_mode(NormalMode) - , m_needSort(false) + , m_firstLoad(false) { // Init datas. QByteArray listByteArray = m_settings.value("list").toByteArray(); @@ -55,7 +56,7 @@ LauncherModel::LauncherModel(QObject *parent) in >> m_appItems; if (m_appItems.isEmpty()) - m_needSort = true; + m_firstLoad = true; QtConcurrent::run(LauncherModel::refresh, this); @@ -66,6 +67,7 @@ LauncherModel::LauncherModel(QObject *parent) }); m_saveTimer.setInterval(1000); + m_saveTimer.setSingleShot(true); connect(&m_saveTimer, &QTimer::timeout, this, &LauncherModel::save); connect(this, &QAbstractItemModel::rowsInserted, this, &LauncherModel::countChanged); @@ -77,6 +79,7 @@ LauncherModel::LauncherModel(QObject *parent) LauncherModel::~LauncherModel() { + LauncherModel::save(); } int LauncherModel::count() const @@ -96,17 +99,22 @@ int LauncherModel::rowCount(const QModelIndex &parent) const QHash LauncherModel::roleNames() const { - QHash roles; - roles.insert(AppIdRole, "appId"); - roles.insert(ApplicationRole, "application"); - roles.insert(NameRole, "name"); - roles.insert(GenericNameRole, "genericName"); - roles.insert(CommentRole, "comment"); - roles.insert(IconNameRole, "iconName"); - roles.insert(CategoriesRole, "categories"); - roles.insert(FilterInfoRole, "filterInfo"); - roles.insert(PinnedRole, "pinned"); - roles.insert(PinnedIndexRole, "pinnedIndex"); + static QHash roles; + + if (roles.isEmpty()) { + roles.insert(AppIdRole, "appId"); + roles.insert(ApplicationRole, "application"); + roles.insert(NameRole, "name"); + roles.insert(GenericNameRole, "genericName"); + roles.insert(CommentRole, "comment"); + roles.insert(IconNameRole, "iconName"); + roles.insert(CategoriesRole, "categories"); + roles.insert(FilterInfoRole, "filterInfo"); + roles.insert(PinnedRole, "pinned"); + roles.insert(PinnedIndexRole, "pinnedIndex"); + roles.insert(NewInstalledRole, "newInstalled"); + } + return roles; } @@ -130,9 +138,11 @@ QVariant LauncherModel::data(const QModelIndex &index, int role) const + appItem.genericName + QStringLiteral(" ") + appItem.comment); - default: - return QVariant(); + case NewInstalledRole: + return appItem.newInstalled; } + + return QVariant(); } void LauncherModel::search(const QString &key) @@ -168,6 +178,20 @@ void LauncherModel::sendToDock(const QString &key) } } +void LauncherModel::sendToDesktop(const QString &key) +{ + int index = findById(key); + + if (index != -1) { + QFileInfo info(key); + + QString newFileName = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); + newFileName.append(QString("/%1").arg(info.fileName())); + + QFile::copy(key, newFileName); + } +} + void LauncherModel::removeFromDock(const QString &desktop) { int index = findById(desktop); @@ -264,10 +288,16 @@ bool LauncherModel::launch(const QString &path) int index = findById(path); if (index != -1) { - const AppItem &item = m_appItems.at(index); + AppItem &item = m_appItems[index]; QStringList args = item.args; QString cmd = args.takeFirst(); + if (item.newInstalled) { + item.newInstalled = false; + emit dataChanged(LauncherModel::index(index), LauncherModel::index(index)); + delaySave(); + } + // Because launcher has hidden animation, // cutefish-screenshot needs to be processed. if (cmd == "cutefish-screenshot") { @@ -286,10 +316,10 @@ bool LauncherModel::launch(const QString &path) void LauncherModel::onRefreshed() { - if (!m_needSort) + if (!m_firstLoad) return; - m_needSort = false; + m_firstLoad = false; beginResetModel(); std::sort(m_appItems.begin(), m_appItems.end(), [=] (AppItem &a, AppItem &b) { @@ -349,14 +379,16 @@ void LauncherModel::addApp(const QString &fileName) appItem.comment = desktop.value("Comment").toString(); appItem.iconName = desktop.value("Icon").toString(); appItem.args = appExec.split(" "); + appItem.newInstalled = true; beginInsertRows(QModelIndex(), m_appItems.count(), m_appItems.count()); m_appItems.append(appItem); qDebug() << "added: " << appItem.name; endInsertRows(); - if (!m_needSort) + if (!m_firstLoad) { delaySave(); + } } } diff --git a/src/launchermodel.h b/src/launchermodel.h index 5aad0ba..6cd9050 100755 --- a/src/launchermodel.h +++ b/src/launchermodel.h @@ -44,7 +44,8 @@ public: CategoriesRole, FilterInfoRole, PinnedRole, - PinnedIndexRole + PinnedIndexRole, + NewInstalledRole }; Q_ENUM(Roles) @@ -64,6 +65,7 @@ public: Q_INVOKABLE void search(const QString &key); Q_INVOKABLE void sendToDock(const QString &key); + Q_INVOKABLE void sendToDesktop(const QString &key); Q_INVOKABLE void removeFromDock(const QString &desktop); int findById(const QString &id); @@ -97,7 +99,7 @@ private: QSettings m_settings; Mode m_mode; - bool m_needSort; + bool m_firstLoad; }; #endif // LAUNCHERMODEL_H diff --git a/src/processprovider.cpp b/src/processprovider.cpp index 0daef9e..f805308 100644 --- a/src/processprovider.cpp +++ b/src/processprovider.cpp @@ -20,6 +20,7 @@ #include "processprovider.h" #include #include +#include ProcessProvider::ProcessProvider(QObject *parent) : QObject(parent) @@ -36,6 +37,9 @@ bool ProcessProvider::startDetached(const QString &exec, QStringList args) if (iface.isValid()) { iface.asyncCall("launch", exec, args).waitForFinished(); return true; + } else { + QProcess process; + process.startDetached(exec, args); } return false; diff --git a/translations/en_US.ts b/translations/en_US.ts index 51862fe..1116ef6 100644 --- a/translations/en_US.ts +++ b/translations/en_US.ts @@ -4,17 +4,22 @@ GridItemDelegate - + Open Open - + Send to dock Send to dock - + + Send to desktop + + + + Remove from dock Remove from dock @@ -22,7 +27,7 @@ Launcher - + Launcher Launcher @@ -30,12 +35,12 @@ main - + Search Search - + Not found Not found diff --git a/translations/zh_CN.ts b/translations/zh_CN.ts index 923e455..e27c8fa 100644 --- a/translations/zh_CN.ts +++ b/translations/zh_CN.ts @@ -4,25 +4,30 @@ GridItemDelegate - + Open - 打开 + 打开 - + Send to dock - 发送到程序坞 + 发送到程序坞 - + + Send to desktop + 发送到桌面 + + + Remove from dock - 在程序坞中移除 + 在程序坞中移除 Launcher - + Launcher 启动器 @@ -30,12 +35,12 @@ main - + Search 搜索 - + Not found 未找到