containments: Optimization

This commit is contained in:
Jonah Brüchert 2020-03-20 01:08:59 +01:00 committed by Nicolas Fella
parent c43777d1be
commit d2b461e17b
8 changed files with 55 additions and 52 deletions

View file

@ -34,6 +34,7 @@
#include <KIOWidgets/KRun> #include <KIOWidgets/KRun>
#include <QDebug> #include <QDebug>
constexpr int MAX_FAVOURITES = 5;
ApplicationListModel::ApplicationListModel(HomeScreen *parent) ApplicationListModel::ApplicationListModel(HomeScreen *parent)
: QAbstractListModel(parent), : QAbstractListModel(parent),
@ -45,8 +46,7 @@ ApplicationListModel::ApplicationListModel(HomeScreen *parent)
loadSettings(); loadSettings();
} }
ApplicationListModel::~ApplicationListModel() ApplicationListModel::~ApplicationListModel() = default;
= default;
void ApplicationListModel::loadSettings() void ApplicationListModel::loadSettings()
{ {
@ -58,7 +58,7 @@ void ApplicationListModel::loadSettings()
m_desktopItems = m_homeScreen->config().readEntry("DesktopItems", QStringList()).toSet(); m_desktopItems = m_homeScreen->config().readEntry("DesktopItems", QStringList()).toSet();
#endif #endif
m_appOrder = m_homeScreen->config().readEntry("AppOrder", QStringList()); 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; int i = 0;
for (const QString &app : qAsConst(m_appOrder)) { for (const QString &app : qAsConst(m_appOrder)) {
@ -84,7 +84,7 @@ QHash<int, QByteArray> ApplicationListModel::roleNames() const
void ApplicationListModel::sycocaDbChanged(const QStringList &changes) 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; return;
} }
@ -100,7 +100,7 @@ bool appNameLessThan(const ApplicationData &a1, const ApplicationData &a2)
void ApplicationListModel::loadApplications() void ApplicationListModel::loadApplications()
{ {
auto cfg = KSharedConfig::openConfig("applications-blacklistrc"); auto cfg = KSharedConfig::openConfig(QStringLiteral("applications-blacklistrc"));
auto blgroup = KConfigGroup(cfg, QStringLiteral("Applications")); auto blgroup = KConfigGroup(cfg, QStringLiteral("Applications"));
// This is only temporary to get a clue what those apps' desktop files are called // 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(); subGroupList.pop_front();
if (groupEntry->isType(KST_KServiceGroup)) { if (groupEntry->isType(KST_KServiceGroup)) {
KServiceGroup::Ptr serviceGroup(static_cast<KServiceGroup* >(groupEntry.data())); KServiceGroup::Ptr serviceGroup(dynamic_cast<KServiceGroup* >(groupEntry.data()));
if (!serviceGroup->noDisplay()) { if (!serviceGroup->noDisplay()) {
KServiceGroup::List entryGroupList = serviceGroup->entries(true); KServiceGroup::List entryGroupList = serviceGroup->entries(true);
@ -136,16 +136,16 @@ void ApplicationListModel::loadApplications()
KSycocaEntry::Ptr entry = (*it); KSycocaEntry::Ptr entry = (*it);
if (entry->isType(KST_KServiceGroup)) { if (entry->isType(KST_KServiceGroup)) {
KServiceGroup::Ptr serviceGroup(static_cast<KServiceGroup* >(entry.data())); KServiceGroup::Ptr serviceGroup(dynamic_cast<KServiceGroup* >(entry.data()));
subGroupList << serviceGroup; subGroupList << serviceGroup;
} else if (entry->property("Exec").isValid()) { } else if (entry->property(QStringLiteral("Exec")).isValid()) {
KService::Ptr service(static_cast<KService* >(entry.data())); KService::Ptr service(dynamic_cast<KService* >(entry.data()));
if (service->isApplication() && if (service->isApplication() &&
!blacklist.contains(service->desktopEntryName()) && !blacklist.contains(service->desktopEntryName()) &&
service->showOnCurrentPlatform() && service->showOnCurrentPlatform() &&
!service->property("Terminal", QVariant::Bool).toBool()) { !service->property(QStringLiteral("Terminal"), QVariant::Bool).toBool()) {
bl << service->desktopEntryName(); bl << service->desktopEntryName();
@ -154,12 +154,12 @@ void ApplicationListModel::loadApplications()
data.icon = service->icon(); data.icon = service->icon();
data.storageId = service->storageId(); data.storageId = service->storageId();
data.entryPath = service->exec(); data.entryPath = service->exec();
data.startupNotify = service->property("StartupNotify").toBool(); data.startupNotify = service->property(QStringLiteral("StartupNotify")).toBool();
if (m_favorites.contains(data.storageId)) { if (m_favorites.contains(data.storageId)) {
data.location = Favorites; data.location = ApplicationData::Favorites;
} else if (m_desktopItems.contains(data.storageId)) { } else if (m_desktopItems.contains(data.storageId)) {
data.location = Desktop; data.location = ApplicationData::Desktop;
} }
auto it = m_appPositions.constFind(service->storageId()); 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 Qt::ItemFlags ApplicationListModel::flags(const QModelIndex &index) const
{ {
if (!index.isValid()) if (!index.isValid())
return nullptr; return {};
return Qt::ItemIsDragEnabled|QAbstractListModel::flags(index); return Qt::ItemIsDragEnabled|QAbstractListModel::flags(index);
} }
@ -236,18 +236,19 @@ void ApplicationListModel::moveRow(const QModelIndex& /* sourceParent */, int so
moveItem(sourceRow, destinationChild); 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()) { if (row < 0 || row >= m_applicationList.length()) {
return; return;
} }
ApplicationData &data = m_applicationList[row]; ApplicationData data = m_applicationList.at(row);
if (data.location == location) { if (data.location == location) {
return; return;
} }
if (location == Favorites) {qWarning()<<"favoriting"<<row<<data.name; if (location == ApplicationData::Favorites) {
qWarning() << "favoriting" << row << data.name;
// Deny favorites when full // Deny favorites when full
if (row >= m_maxFavoriteCount || m_favorites.count() >= m_maxFavoriteCount) { if (row >= m_maxFavoriteCount || m_favorites.count() >= m_maxFavoriteCount) {
return; return;
@ -259,21 +260,21 @@ void ApplicationListModel::setLocation(int row, LauncherLocation location)
emit favoriteCountChanged(); emit favoriteCountChanged();
// Out of favorites // Out of favorites
} else if (data.location == Favorites) { } else if (data.location == ApplicationData::Favorites) {
m_favorites.removeAll(data.storageId); m_favorites.removeAll(data.storageId);
m_homeScreen->config().writeEntry("Favorites", m_favorites); m_homeScreen->config().writeEntry("Favorites", m_favorites);
emit favoriteCountChanged(); emit favoriteCountChanged();
} }
// In Desktop // In Desktop
if (location == Desktop) { if (location == ApplicationData::Desktop) {
m_desktopItems.insert(data.storageId); m_desktopItems.insert(data.storageId);
m_homeScreen->config().writeEntry("DesktopItems", m_desktopItems.values()); m_homeScreen->config().writeEntry("DesktopItems", m_desktopItems.values());
// Out of Desktop // Out of Desktop
} else if (data.location == Desktop) { } else if (data.location == ApplicationData::Desktop) {
m_desktopItems.remove(data.storageId); 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; data.location = location;
@ -347,8 +348,8 @@ void ApplicationListModel::setMaxFavoriteCount(int count)
int i = 0; int i = 0;
for (auto &app : m_applicationList) { for (auto &app : m_applicationList) {
if (i >= count && app.location == Favorites) { if (i >= count && app.location == ApplicationData::Favorites) {
app.location = Grid; app.location = ApplicationData::Grid;
emit dataChanged(index(i, 0), index(i, 0)); emit dataChanged(index(i, 0), index(i, 0));
} }
++i; ++i;

View file

@ -33,11 +33,16 @@ class QString;
class ApplicationListModel; class ApplicationListModel;
struct ApplicationData { struct ApplicationData {
enum LauncherLocation {
Grid = 0,
Favorites,
Desktop
};
QString name; QString name;
QString icon; QString icon;
QString storageId; QString storageId;
QString entryPath; QString entryPath;
int location = 0; //FIXME LauncherLocation location = Grid;
bool startupNotify = true; bool startupNotify = true;
}; };
@ -50,9 +55,9 @@ class ApplicationListModel : public QAbstractListModel {
public: public:
enum LauncherLocation { enum LauncherLocation {
Grid = 0, Grid = ApplicationData::Grid,
Favorites, Favorites = ApplicationData::Favorites,
Desktop Desktop = ApplicationData::Desktop
}; };
Q_ENUM(LauncherLocation) Q_ENUM(LauncherLocation)
@ -87,9 +92,9 @@ public:
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE; QHash<int, QByteArray> 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); Q_INVOKABLE void runApplication(const QString &storageId);

View file

@ -21,13 +21,14 @@
#include <QColor> #include <QColor>
#include <QImage> #include <QImage>
#include <QDebug> #include <QDebug>
#include <signal.h>
#include "colouraverage.h" #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 r = 0;
int g = 0; int g = 0;
int b = 0; int b = 0;

View file

@ -25,7 +25,8 @@
class ColourAverage : public QObject class ColourAverage : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ColourAverage(QObject *parent = nullptr); explicit ColourAverage(QObject *parent = nullptr);
Q_INVOKABLE QColor averageColour(QImage img); Q_INVOKABLE QColor averageColour(const QImage &img);
}; };

View file

@ -29,19 +29,14 @@ HomeScreen::HomeScreen(QObject *parent, const QVariantList &args)
: Plasma::Containment(parent, args) : Plasma::Containment(parent, args)
{ {
qmlRegisterUncreatableType<ApplicationListModel>("org.kde.phone.homescreen", 1, 0, "ApplicationListModel", QStringLiteral("Cannot create item of type ApplicationListModel")); qmlRegisterUncreatableType<ApplicationListModel>("org.kde.phone.homescreen", 1, 0, "ApplicationListModel", QStringLiteral("Cannot create item of type ApplicationListModel"));
qmlRegisterSingletonType<ColourAverage>("org.kde.phone.homescreen", 1, 0, "ColourAverage", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * { qmlRegisterSingletonType<ColourAverage>("org.kde.phone.homescreen", 1, 0, "ColourAverage", [](QQmlEngine *, QJSEngine *) -> QObject * {
Q_UNUSED(engine); return new ColourAverage();
Q_UNUSED(scriptEngine);
ColourAverage *obj = new ColourAverage();
return obj;
}); });
setHasConfigurationInterface(true); setHasConfigurationInterface(true);
} }
HomeScreen::~HomeScreen() HomeScreen::~HomeScreen() = default;
= default;
void HomeScreen::configChanged() void HomeScreen::configChanged()
{ {

View file

@ -31,14 +31,15 @@
#include "screenshotinterface.h" #include "screenshotinterface.h"
constexpr int SCREENSHOT_DELAY = 200;
PhonePanel::PhonePanel(QObject *parent, const QVariantList &args) PhonePanel::PhonePanel(QObject *parent, const QVariantList &args)
: Plasma::Containment(parent, args) : Plasma::Containment(parent, args)
{ {
//setHasConfigurationInterface(true); //setHasConfigurationInterface(true);
} }
PhonePanel::~PhonePanel() PhonePanel::~PhonePanel() = default;
= default;
void PhonePanel::executeCommand(const QString &command) void PhonePanel::executeCommand(const QString &command)
{ {
@ -82,14 +83,14 @@ void PhonePanel::toggleTorch()
void PhonePanel::takeScreenshot() void PhonePanel::takeScreenshot()
{ {
// wait ~200 ms to wait for rest of animations // wait ~200 ms to wait for rest of animations
QTimer::singleShot(200, [=]() { QTimer::singleShot(SCREENSHOT_DELAY, [=]() {
auto *interface = new org::kde::kwin::Screenshot(QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), QDBusConnection::sessionBus(), this); auto *interface = new org::kde::kwin::Screenshot(QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), QDBusConnection::sessionBus(), this);
// screenshot fullscreen currently doesn't work on all devices -> we need to use screenshot area // screenshot fullscreen currently doesn't work on all devices -> we need to use screenshot area
// this won't work with multiple screens // this won't work with multiple screens
QSize screenSize = QGuiApplication::primaryScreen()->size(); QSize screenSize = QGuiApplication::primaryScreen()->size();
QDBusPendingReply<QString> reply = interface->screenshotArea(0, 0, screenSize.width(), screenSize.height()); QDBusPendingReply<QString> 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) { connect(watcher, &QDBusPendingCallWatcher::finished, this, [=](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<QString> reply = *watcher; QDBusPendingReply<QString> reply = *watcher;

View file

@ -34,6 +34,7 @@
#include <KWayland/Client/surface.h> #include <KWayland/Client/surface.h>
static const QString s_kwinService = QStringLiteral("org.kde.KWin"); static const QString s_kwinService = QStringLiteral("org.kde.KWin");
constexpr int ACTIVE_WINDOW_UPDATE_INVERVAL = 250;
TaskPanel::TaskPanel(QObject *parent, const QVariantList &args) TaskPanel::TaskPanel(QObject *parent, const QVariantList &args)
: Plasma::Containment(parent, args) : Plasma::Containment(parent, args)
@ -43,14 +44,12 @@ TaskPanel::TaskPanel(QObject *parent, const QVariantList &args)
setHasConfigurationInterface(true); setHasConfigurationInterface(true);
m_activeTimer = new QTimer(this); m_activeTimer = new QTimer(this);
m_activeTimer->setSingleShot(true); m_activeTimer->setSingleShot(true);
m_activeTimer->setInterval(250); m_activeTimer->setInterval(ACTIVE_WINDOW_UPDATE_INVERVAL);
connect(m_activeTimer, &QTimer::timeout, this, &TaskPanel::updateActiveWindow); connect(m_activeTimer, &QTimer::timeout, this, &TaskPanel::updateActiveWindow);
initWayland(); initWayland();
} }
TaskPanel::~TaskPanel() TaskPanel::~TaskPanel() = default;
{
}
void TaskPanel::requestShowingDesktop(bool showingDesktop) void TaskPanel::requestShowingDesktop(bool showingDesktop)
{ {