From d2b461e17b2f285c67aa111baa35b55dda3b94f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonah=20Br=C3=BCchert?= Date: Fri, 20 Mar 2020 01:08:59 +0100 Subject: [PATCH] containments: Optimization --- containments/homescreen/CMakeLists.txt | 2 +- .../homescreen/applicationlistmodel.cpp | 47 ++++++++++--------- .../homescreen/applicationlistmodel.h | 17 ++++--- containments/homescreen/colouraverage.cpp | 7 +-- containments/homescreen/colouraverage.h | 5 +- containments/homescreen/homescreen.cpp | 11 ++--- containments/panel/phonepanel.cpp | 11 +++-- containments/taskpanel/taskpanel.cpp | 7 ++- 8 files changed, 55 insertions(+), 52 deletions(-) diff --git a/containments/homescreen/CMakeLists.txt b/containments/homescreen/CMakeLists.txt index d9eedc67..4cb6f5ce 100644 --- a/containments/homescreen/CMakeLists.txt +++ b/containments/homescreen/CMakeLists.txt @@ -16,7 +16,7 @@ target_link_libraries(plasma_containment_phone_homescreen KF5::I18n KF5::Service KF5::KIOWidgets - ) +) install(TARGETS plasma_containment_phone_homescreen DESTINATION ${KDE_INSTALL_PLUGINDIR}/plasma/applets) diff --git a/containments/homescreen/applicationlistmodel.cpp b/containments/homescreen/applicationlistmodel.cpp index ab8638b3..05c56a09 100644 --- a/containments/homescreen/applicationlistmodel.cpp +++ b/containments/homescreen/applicationlistmodel.cpp @@ -34,6 +34,7 @@ #include #include +constexpr int MAX_FAVOURITES = 5; ApplicationListModel::ApplicationListModel(HomeScreen *parent) : QAbstractListModel(parent), @@ -45,8 +46,7 @@ ApplicationListModel::ApplicationListModel(HomeScreen *parent) loadSettings(); } -ApplicationListModel::~ApplicationListModel() -= default; +ApplicationListModel::~ApplicationListModel() = default; void ApplicationListModel::loadSettings() { @@ -58,7 +58,7 @@ void ApplicationListModel::loadSettings() m_desktopItems = m_homeScreen->config().readEntry("DesktopItems", QStringList()).toSet(); #endif m_appOrder = m_homeScreen->config().readEntry("AppOrder", QStringList()); - m_maxFavoriteCount = m_homeScreen->config().readEntry("MaxFavoriteCount", 5); + m_maxFavoriteCount = m_homeScreen->config().readEntry("MaxFavoriteCount", MAX_FAVOURITES); int i = 0; for (const QString &app : qAsConst(m_appOrder)) { @@ -84,7 +84,7 @@ QHash ApplicationListModel::roleNames() const void ApplicationListModel::sycocaDbChanged(const QStringList &changes) { - if (!changes.contains("apps") && !changes.contains("xdgdata-apps")) { + if (!changes.contains(QStringLiteral("apps")) && !changes.contains(QStringLiteral("xdgdata-apps"))) { return; } @@ -100,7 +100,7 @@ bool appNameLessThan(const ApplicationData &a1, const ApplicationData &a2) void ApplicationListModel::loadApplications() { - auto cfg = KSharedConfig::openConfig("applications-blacklistrc"); + auto cfg = KSharedConfig::openConfig(QStringLiteral("applications-blacklistrc")); auto blgroup = KConfigGroup(cfg, QStringLiteral("Applications")); // This is only temporary to get a clue what those apps' desktop files are called @@ -127,7 +127,7 @@ void ApplicationListModel::loadApplications() subGroupList.pop_front(); if (groupEntry->isType(KST_KServiceGroup)) { - KServiceGroup::Ptr serviceGroup(static_cast(groupEntry.data())); + KServiceGroup::Ptr serviceGroup(dynamic_cast(groupEntry.data())); if (!serviceGroup->noDisplay()) { KServiceGroup::List entryGroupList = serviceGroup->entries(true); @@ -136,16 +136,16 @@ void ApplicationListModel::loadApplications() KSycocaEntry::Ptr entry = (*it); if (entry->isType(KST_KServiceGroup)) { - KServiceGroup::Ptr serviceGroup(static_cast(entry.data())); + KServiceGroup::Ptr serviceGroup(dynamic_cast(entry.data())); subGroupList << serviceGroup; - } else if (entry->property("Exec").isValid()) { - KService::Ptr service(static_cast(entry.data())); + } else if (entry->property(QStringLiteral("Exec")).isValid()) { + KService::Ptr service(dynamic_cast(entry.data())); if (service->isApplication() && !blacklist.contains(service->desktopEntryName()) && service->showOnCurrentPlatform() && - !service->property("Terminal", QVariant::Bool).toBool()) { + !service->property(QStringLiteral("Terminal"), QVariant::Bool).toBool()) { bl << service->desktopEntryName(); @@ -154,12 +154,12 @@ void ApplicationListModel::loadApplications() data.icon = service->icon(); data.storageId = service->storageId(); data.entryPath = service->exec(); - data.startupNotify = service->property("StartupNotify").toBool(); + data.startupNotify = service->property(QStringLiteral("StartupNotify")).toBool(); if (m_favorites.contains(data.storageId)) { - data.location = Favorites; + data.location = ApplicationData::Favorites; } else if (m_desktopItems.contains(data.storageId)) { - data.location = Desktop; + data.location = ApplicationData::Desktop; } auto it = m_appPositions.constFind(service->storageId()); @@ -218,7 +218,7 @@ QVariant ApplicationListModel::data(const QModelIndex &index, int role) const Qt::ItemFlags ApplicationListModel::flags(const QModelIndex &index) const { if (!index.isValid()) - return nullptr; + return {}; return Qt::ItemIsDragEnabled|QAbstractListModel::flags(index); } @@ -236,18 +236,19 @@ void ApplicationListModel::moveRow(const QModelIndex& /* sourceParent */, int so moveItem(sourceRow, destinationChild); } -void ApplicationListModel::setLocation(int row, LauncherLocation location) +void ApplicationListModel::setLocation(int row, ApplicationData::LauncherLocation location) { if (row < 0 || row >= m_applicationList.length()) { return; } - ApplicationData &data = m_applicationList[row]; + ApplicationData data = m_applicationList.at(row); if (data.location == location) { return; } - if (location == Favorites) {qWarning()<<"favoriting"<= m_maxFavoriteCount || m_favorites.count() >= m_maxFavoriteCount) { return; @@ -259,21 +260,21 @@ void ApplicationListModel::setLocation(int row, LauncherLocation location) emit favoriteCountChanged(); // Out of favorites - } else if (data.location == Favorites) { + } else if (data.location == ApplicationData::Favorites) { m_favorites.removeAll(data.storageId); m_homeScreen->config().writeEntry("Favorites", m_favorites); emit favoriteCountChanged(); } // In Desktop - if (location == Desktop) { + if (location == ApplicationData::Desktop) { m_desktopItems.insert(data.storageId); m_homeScreen->config().writeEntry("DesktopItems", m_desktopItems.values()); // Out of Desktop - } else if (data.location == Desktop) { + } else if (data.location == ApplicationData::Desktop) { m_desktopItems.remove(data.storageId); - m_homeScreen->config().writeEntry("DesktopItems", m_desktopItems.values()); + m_homeScreen->config().writeEntry(QStringLiteral("DesktopItems"), m_desktopItems.values()); } data.location = location; @@ -347,8 +348,8 @@ void ApplicationListModel::setMaxFavoriteCount(int count) int i = 0; for (auto &app : m_applicationList) { - if (i >= count && app.location == Favorites) { - app.location = Grid; + if (i >= count && app.location == ApplicationData::Favorites) { + app.location = ApplicationData::Grid; emit dataChanged(index(i, 0), index(i, 0)); } ++i; diff --git a/containments/homescreen/applicationlistmodel.h b/containments/homescreen/applicationlistmodel.h index c8e913a4..b9eb1e8d 100644 --- a/containments/homescreen/applicationlistmodel.h +++ b/containments/homescreen/applicationlistmodel.h @@ -33,11 +33,16 @@ class QString; class ApplicationListModel; struct ApplicationData { + enum LauncherLocation { + Grid = 0, + Favorites, + Desktop + }; QString name; QString icon; QString storageId; QString entryPath; - int location = 0; //FIXME + LauncherLocation location = Grid; bool startupNotify = true; }; @@ -50,9 +55,9 @@ class ApplicationListModel : public QAbstractListModel { public: enum LauncherLocation { - Grid = 0, - Favorites, - Desktop + Grid = ApplicationData::Grid, + Favorites = ApplicationData::Favorites, + Desktop = ApplicationData::Desktop }; Q_ENUM(LauncherLocation) @@ -87,9 +92,9 @@ public: QHash roleNames() const Q_DECL_OVERRIDE; - Q_INVOKABLE void setLocation(int row, ApplicationListModel::LauncherLocation location); + Q_INVOKABLE void setLocation(int row, ApplicationData::LauncherLocation location); - Q_INVOKABLE void moveItem(int row, int order); + Q_INVOKABLE void moveItem(int row, int destination); Q_INVOKABLE void runApplication(const QString &storageId); diff --git a/containments/homescreen/colouraverage.cpp b/containments/homescreen/colouraverage.cpp index 81b43ad4..1e269ed0 100644 --- a/containments/homescreen/colouraverage.cpp +++ b/containments/homescreen/colouraverage.cpp @@ -21,13 +21,14 @@ #include #include #include -#include #include "colouraverage.h" -ColourAverage::ColourAverage(QObject* parent) : QObject(parent) {} +ColourAverage::ColourAverage(QObject *parent) : QObject(parent) +{ +} -QColor ColourAverage::averageColour(QImage img) { +QColor ColourAverage::averageColour(const QImage &img) { int r = 0; int g = 0; int b = 0; diff --git a/containments/homescreen/colouraverage.h b/containments/homescreen/colouraverage.h index 609f138e..0d15f53b 100644 --- a/containments/homescreen/colouraverage.h +++ b/containments/homescreen/colouraverage.h @@ -25,7 +25,8 @@ class ColourAverage : public QObject { Q_OBJECT + public: - explicit ColourAverage(QObject* parent = nullptr); - Q_INVOKABLE QColor averageColour(QImage img); + explicit ColourAverage(QObject *parent = nullptr); + Q_INVOKABLE QColor averageColour(const QImage &img); }; diff --git a/containments/homescreen/homescreen.cpp b/containments/homescreen/homescreen.cpp index c006de8e..318b0e8c 100644 --- a/containments/homescreen/homescreen.cpp +++ b/containments/homescreen/homescreen.cpp @@ -29,19 +29,14 @@ HomeScreen::HomeScreen(QObject *parent, const QVariantList &args) : Plasma::Containment(parent, args) { qmlRegisterUncreatableType("org.kde.phone.homescreen", 1, 0, "ApplicationListModel", QStringLiteral("Cannot create item of type ApplicationListModel")); - qmlRegisterSingletonType("org.kde.phone.homescreen", 1, 0, "ColourAverage", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * { - Q_UNUSED(engine); - Q_UNUSED(scriptEngine); - - ColourAverage *obj = new ColourAverage(); - return obj; + qmlRegisterSingletonType("org.kde.phone.homescreen", 1, 0, "ColourAverage", [](QQmlEngine *, QJSEngine *) -> QObject * { + return new ColourAverage(); }); setHasConfigurationInterface(true); } -HomeScreen::~HomeScreen() -= default; +HomeScreen::~HomeScreen() = default; void HomeScreen::configChanged() { diff --git a/containments/panel/phonepanel.cpp b/containments/panel/phonepanel.cpp index e61cf953..651a77f9 100644 --- a/containments/panel/phonepanel.cpp +++ b/containments/panel/phonepanel.cpp @@ -31,18 +31,19 @@ #include "screenshotinterface.h" +constexpr int SCREENSHOT_DELAY = 200; + PhonePanel::PhonePanel(QObject *parent, const QVariantList &args) : Plasma::Containment(parent, args) { //setHasConfigurationInterface(true); } -PhonePanel::~PhonePanel() -= default; +PhonePanel::~PhonePanel() = default; void PhonePanel::executeCommand(const QString &command) { - qWarning()<<"Executing"< we need to use screenshot area // this won't work with multiple screens QSize screenSize = QGuiApplication::primaryScreen()->size(); QDBusPendingReply reply = interface->screenshotArea(0, 0, screenSize.width(), screenSize.height()); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); + auto *watcher = new QDBusPendingCallWatcher(reply, this); connect(watcher, &QDBusPendingCallWatcher::finished, this, [=](QDBusPendingCallWatcher *watcher) { QDBusPendingReply reply = *watcher; diff --git a/containments/taskpanel/taskpanel.cpp b/containments/taskpanel/taskpanel.cpp index 174a9981..78bed317 100644 --- a/containments/taskpanel/taskpanel.cpp +++ b/containments/taskpanel/taskpanel.cpp @@ -34,6 +34,7 @@ #include static const QString s_kwinService = QStringLiteral("org.kde.KWin"); +constexpr int ACTIVE_WINDOW_UPDATE_INVERVAL = 250; TaskPanel::TaskPanel(QObject *parent, const QVariantList &args) : Plasma::Containment(parent, args) @@ -43,14 +44,12 @@ TaskPanel::TaskPanel(QObject *parent, const QVariantList &args) setHasConfigurationInterface(true); m_activeTimer = new QTimer(this); m_activeTimer->setSingleShot(true); - m_activeTimer->setInterval(250); + m_activeTimer->setInterval(ACTIVE_WINDOW_UPDATE_INVERVAL); connect(m_activeTimer, &QTimer::timeout, this, &TaskPanel::updateActiveWindow); initWayland(); } -TaskPanel::~TaskPanel() -{ -} +TaskPanel::~TaskPanel() = default; void TaskPanel::requestShowingDesktop(bool showingDesktop) {