Commit 4bf3f59a authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧 Committed by Aleix Pol Gonzalez
Browse files

Port away from QAction

It's big part of the reason why we use QtWidgets and it seems to add
some unnecessary overhead.
parent 38e10f60
......@@ -11,6 +11,7 @@
#include <Transaction/TransactionListener.h>
#include <Transaction/TransactionModel.h>
#include <Transaction/Transaction.h>
#include <resources/DiscoverAction.h>
#include <resources/ResourcesUpdatesModel.h>
#include <resources/AbstractResource.h>
#include <resources/ResourcesModel.h>
......@@ -24,7 +25,6 @@
#include <UpdateModel/UpdateModel.h>
#include <ScreenshotsModel.h>
#include <ApplicationAddonsModel.h>
#include <ActionsModel.h>
#include <qqml.h>
#include <QQmlEngine>
#include <QQmlContext>
......@@ -48,10 +48,10 @@ void DiscoverDeclarativePlugin::registerTypes(const char* /*uri*/)
qmlRegisterType<ReviewsModel>("org.kde.discover", 2, 0, "ReviewsModel");
qmlRegisterType<ApplicationAddonsModel>("org.kde.discover", 2, 0, "ApplicationAddonsModel");
qmlRegisterType<ScreenshotsModel>("org.kde.discover", 2, 0, "ScreenshotsModel");
qmlRegisterType<ActionsModel>("org.kde.discover", 2, 0, "ActionsModel");
qmlRegisterType<UpdateModel>("org.kde.discover", 2, 0, "UpdateModel");
qmlRegisterType<ReadFile>("org.kde.discover", 2, 0, "ReadFile");
qmlRegisterUncreatableType<DiscoverAction>("org.kde.discover", 2, 0, "DiscoverAction", QStringLiteral("Use QQC Action"));
qmlRegisterUncreatableType<QAction>("org.kde.discover", 2, 0, "QAction", QStringLiteral("Use QQC Action"));
qmlRegisterUncreatableType<AbstractResource>("org.kde.discover", 2, 0, "AbstractResource", QStringLiteral("should come from the ResourcesModel"));
qmlRegisterUncreatableType<AbstractSourcesBackend>("org.kde.discover", 2, 0, "AbstractSourcesBackend", QStringLiteral("should come from the SourcesModel"));
......
......@@ -4,7 +4,7 @@
* SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include <QApplication>
#include <QGuiApplication>
#include <QCommandLineParser>
#include <KLocalizedString>
#include <KAboutData>
......@@ -40,7 +40,7 @@ int main(int argc, char** argv)
exp.setExportPath(QUrl::fromUserInput(parser.positionalArguments().at(0), QString(), QUrl::AssumeLocalFile));
}
QObject::connect(&exp, &MuonExporter::exportDone, &app, &QApplication::quit);
QObject::connect(&exp, &MuonExporter::exportDone, &app, &QCoreApplication::quit);
return app.exec();
}
/*
* SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "ActionsModel.h"
#include "resources/ResourcesModel.h"
#include "utils.h"
#include <QAction>
#include "libdiscover_debug.h"
ActionsModel::ActionsModel(QObject* parent)
: QAbstractListModel(parent)
, m_priority(-1)
{
connect(ResourcesModel::global(), &ResourcesModel::backendsChanged, this, &ActionsModel::reload);
}
QHash< int, QByteArray > ActionsModel::roleNames() const
{
return { { Qt::UserRole, "action" }};
}
QVariant ActionsModel::data(const QModelIndex& index, int role) const
{
if(!index.isValid() || role!=Qt::UserRole)
return QVariant();
return QVariant::fromValue<QObject*>(m_filteredActions[index.row()]);
}
int ActionsModel::rowCount(const QModelIndex& parent) const
{
return parent.isValid() ? 0 : m_filteredActions.count();
}
void ActionsModel::setActions(const QVariant& actions)
{
if (m_actions == actions) {
return;
}
m_actions = actions;
reload();
Q_EMIT actionsChanged(m_actions);
}
void ActionsModel::reload()
{
QList<QAction*> actions = m_actions.value<QList<QAction*>>();
if (m_priority>=0) {
actions = kFilter<QList<QAction*>>(actions, [this](QAction* action){ return action->priority() == m_priority; });
}
actions = kFilter<QList<QAction*>>(actions, [](QAction* action){ return action->isVisible(); });
if (actions == m_filteredActions)
return;
beginResetModel();
m_filteredActions = actions;
endResetModel();
for(auto a : qAsConst(m_filteredActions)) {
connect(a, &QAction::changed, this, &ActionsModel::reload, Qt::UniqueConnection);
}
}
int ActionsModel::filterPriority() const
{
return m_priority;
}
void ActionsModel::setFilterPriority(int p)
{
if (m_priority != p) {
m_priority = p;
reload();
Q_EMIT filterPriorityChanged();
}
}
/*
* SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#ifndef ACTIONSMODEL_H
#define ACTIONSMODEL_H
#include <QAbstractListModel>
#include <QQmlParserStatus>
#include "discovercommon_export.h"
class QAction;
class DISCOVERCOMMON_EXPORT ActionsModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QVariant actions READ actions WRITE setActions NOTIFY actionsChanged)
Q_PROPERTY(int filterPriority READ filterPriority WRITE setFilterPriority NOTIFY filterPriorityChanged)
public:
explicit ActionsModel(QObject* parent = nullptr);
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex& index, int role) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
void setFilterPriority(int p);
int filterPriority() const;
void setActions(const QVariant& actions);
QVariant actions() const { return m_actions; }
Q_SIGNALS:
void actionsChanged(const QVariant& actions);
void filterPriorityChanged();
private:
void reload();
QVariant m_actions;
QList<QAction*> m_filteredActions;
int m_priority;
};
#endif
......@@ -20,6 +20,7 @@ set(discovercommon_SRCS
Transaction/TransactionModel.cpp
UpdateModel/UpdateItem.cpp
UpdateModel/UpdateModel.cpp
resources/DiscoverAction.cpp
resources/ResourcesModel.cpp
resources/ResourcesProxyModel.cpp
resources/PackageState.cpp
......@@ -31,7 +32,6 @@ set(discovercommon_SRCS
resources/AbstractBackendUpdater.cpp
resources/AbstractSourcesBackend.cpp
resources/StoredResultsStream.cpp
ActionsModel.cpp
DiscoverBackendsFactory.cpp
ScreenshotsModel.cpp
ApplicationAddonsModel.cpp
......@@ -66,7 +66,7 @@ target_link_libraries(DiscoverCommon
PUBLIC
Qt::Core
Qt::Qml
Qt::Widgets
Qt::Gui
KF5::I18n
KF5::ItemModels
PRIVATE
......
......@@ -21,7 +21,6 @@
#include <QDebug>
#include <QThread>
#include <QTimer>
#include <QAction>
DISCOVER_BACKEND_PLUGIN(DummyBackend)
......
......@@ -6,17 +6,17 @@
#include "DummySourcesBackend.h"
#include <QDebug>
#include <QAction>
#include "resources/DiscoverAction.h"
DummySourcesBackend::DummySourcesBackend(AbstractResourcesBackend * parent)
: AbstractSourcesBackend(parent)
, m_sources(new QStandardItemModel(this))
, m_testAction(new QAction(QIcon::fromTheme(QStringLiteral("kalgebra")), QStringLiteral("DummyAction"), this))
, m_testAction(new DiscoverAction(QIcon::fromTheme(QStringLiteral("kalgebra")), QStringLiteral("DummyAction"), this))
{
for (int i = 0; i<10; ++i)
addSource(QStringLiteral("DummySource%1").arg(i));
connect(m_testAction, &QAction::triggered, [](){ qDebug() << "action triggered!"; });
connect(m_testAction, &DiscoverAction::triggered, [](){ qDebug() << "action triggered!"; });
connect(m_sources, &QStandardItemModel::itemChanged, this, [](QStandardItem* item) { qDebug() << "DummySource changed" << item << item->checkState(); });
}
......
......@@ -10,6 +10,8 @@
#include <resources/AbstractSourcesBackend.h>
#include <QStandardItemModel>
class DiscoverAction;
class DummySourcesBackend : public AbstractSourcesBackend
{
public:
......@@ -29,7 +31,7 @@ private:
QStandardItem* sourceForId(const QString& id) const;
QStandardItemModel* m_sources;
QAction* m_testAction;
DiscoverAction* m_testAction;
};
#endif // DUMMYSOURCESBACKEND_H
......@@ -20,7 +20,6 @@
#include <QTest>
#include <QtTest>
#include <QAction>
QTEST_MAIN(DummyTest)
......
......@@ -18,7 +18,6 @@
#include <QTest>
#include <QtTest>
#include <QAction>
class UpdateDummyTest
: public QObject
......
......@@ -28,7 +28,6 @@
#include <KConfigGroup>
#include <KSharedConfig>
#include <QAction>
#include <QtConcurrentRun>
#include <QDebug>
#include <QDir>
......
......@@ -15,11 +15,11 @@
#include <QDebug>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QAction>
#include <glib.h>
#include <QTemporaryFile>
#include <QStandardPaths>
#include <resources/DiscoverAction.h>
#include <resources/StoredResultsStream.h>
class FlatpakSourceItem : public QStandardItem
......@@ -37,12 +37,12 @@ FlatpakSourcesBackend::FlatpakSourcesBackend(const QVector<FlatpakInstallation *
: AbstractSourcesBackend(parent)
, m_preferredInstallation(installations.constFirst())
, m_sources(new QStandardItemModel(this))
, m_flathubAction(new QAction(i18n("Add Flathub"), this))
, m_flathubAction(new DiscoverAction(i18n("Add Flathub"), this))
, m_noSourcesItem(new QStandardItem(QStringLiteral("-")))
{
m_flathubAction->setObjectName(QStringLiteral("flathub"));
m_flathubAction->setToolTip(i18n("Makes it possible to easily install the applications listed in https://flathub.org"));
connect(m_flathubAction, &QAction::triggered, this, [this](){
connect(m_flathubAction, &DiscoverAction::triggered, this, [this](){
addSource(QStringLiteral("https://flathub.org/repo/flathub.flatpakrepo"));
});
for (auto installation : installations) {
......@@ -302,7 +302,7 @@ void FlatpakSourcesBackend::addRemote(FlatpakRemote *remote, FlatpakInstallation
const auto theActions = actions();
for(const QVariant& act: theActions) {
QAction* action = qobject_cast<QAction*>(act.value<QObject*>());
DiscoverAction* action = qobject_cast<DiscoverAction*>(act.value<QObject*>());
if (action->objectName() == id) {
action->setEnabled(false);
action->setVisible(false);
......
......@@ -17,6 +17,7 @@ extern "C" {
#include <flatpak.h>
}
class DiscoverAction;
class FlatpakResource;
class FlatpakSourcesBackend : public AbstractSourcesBackend
{
......@@ -52,7 +53,7 @@ private:
FlatpakInstallation *m_preferredInstallation;
QStandardItemModel* m_sources;
QAction* const m_flathubAction;
DiscoverAction* const m_flathubAction;
QStandardItem* m_noSourcesItem;
QStack<std::function<void()>> m_proceedFunctions;
};
......
......@@ -8,6 +8,7 @@
#include <resources/ResourcesProxyModel.h>
#include <resources/AbstractBackendUpdater.h>
#include <resources/SourcesModel.h>
#include <resources/DiscoverAction.h>
#include <ApplicationAddonsModel.h>
#include <ReviewsBackend/ReviewsModel.h>
#include <Transaction/TransactionModel.h>
......@@ -15,7 +16,6 @@
#include <QTest>
#include <QtTest>
#include <QAction>
class FlatpakTest
: public QObject
......@@ -64,7 +64,7 @@ private Q_SLOTS:
QSignalSpy initializedSpy(m_appBackend, SIGNAL(initialized()));
if (m->rowCount() == 1) {
QSignalSpy spy(m, &SourcesModel::rowsInserted);
qobject_cast<QAction*>(bk->actions().constFirst().value<QObject*>())->trigger();
qobject_cast<DiscoverAction*>(bk->actions().constFirst().value<QObject*>())->trigger();
QVERIFY(spy.count() || spy.wait(20000));
}
QVERIFY(initializedSpy.count() || initializedSpy.wait(20000));
......
......@@ -14,7 +14,6 @@
#include <QDir>
#include <QDebug>
#include <QTimer>
#include <QAction>
#include <QMimeDatabase>
#include <QVariantList>
#include <QSet>
......@@ -30,7 +29,7 @@ extern "C" {
}
#include <glib-2.0/glib-object.h>
class QAction;
class DiscoverAction;
class StandardBackendUpdater;
class FwupdResource;
class FwupdBackend : public AbstractResourcesBackend
......
......@@ -7,11 +7,9 @@
#include "FwupdSourcesBackend.h"
#include <QAction>
#include <QString>
#include <KLocalizedString>
class FwupdSourcesModel : public QStandardItemModel
{
Q_OBJECT
......
......@@ -24,7 +24,6 @@
#include <QDebug>
#include <QStandardPaths>
#include <QFile>
#include <QAction>
#include <QMimeDatabase>
#include <QFileSystemWatcher>
#include <QFutureWatcher>
......
......@@ -9,10 +9,10 @@
#include <KLocalizedString>
#include <KDesktopFile>
#include <PackageKit/Daemon>
#include <QAction>
#include <QProcess>
#include <QDebug>
#include <QRegularExpression>
#include <resources/DiscoverAction.h>
#include <resources/SourcesModel.h>
#include <resources/AbstractResourcesBackend.h>
#include "PackageKitBackend.h"
......@@ -45,14 +45,14 @@ private:
PackageKitSourcesBackend* m_backend;
};
static QAction* createActionForService(const QString &servicePath, QObject* parent)
static DiscoverAction* createActionForService(const QString &servicePath, QObject* parent)
{
QAction* action = new QAction(parent);
DiscoverAction* action = new DiscoverAction(parent);
KDesktopFile parser(servicePath);
action->setIcon(QIcon::fromTheme(parser.readIcon()));
action->setText(parser.readName());
action->setToolTip(parser.readComment());
QObject::connect(action, &QAction::triggered, action, [servicePath](){
QObject::connect(action, &DiscoverAction::triggered, action, [servicePath](){
bool b = QProcess::startDetached(QStringLiteral(CMAKE_INSTALL_FULL_LIBEXECDIR_KF5 "/discover/runservice"), {servicePath});
if (!b)
qWarning() << "Could not start" << servicePath;
......
......@@ -9,7 +9,6 @@
#include <PackageKit/Daemon>
#include <PackageKit/Offline>
#include <QDebug>
#include <QAction>
#include <QSet>
#include <KLocalizedString>
......
add_subdirectory(libsnapclient)
add_library(snap-backend MODULE SnapResource.cpp SnapBackend.cpp SnapTransaction.cpp snapui.qrc)
target_link_libraries(snap-backend Qt::Core Qt::Concurrent KF5::CoreAddons KF5::ConfigCore Discover::Common Snapd::Core)
target_link_libraries(snap-backend Qt::Gui Qt::Core Qt::Concurrent KF5::CoreAddons KF5::ConfigCore Discover::Common Snapd::Core)
if ("${Snapd_VERSION}" VERSION_GREATER 1.40)
target_compile_definitions(snap-backend PRIVATE -DSNAP_COMMON_IDS -DSNAP_CHANNELS)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment