Commit 2a8cabcc authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧
Browse files

Make the snap source less of a weird thing

This way we can make the sources page less of a weird monster.
parent ef66f3d0
......@@ -33,170 +33,149 @@ DiscoverPage {
}
currentIndex: -1
Component {
id: sourceBackendDelegate
Kirigami.AbstractListItem {
id: backendItem
hoverEnabled: false
supportsMouseEvents: false
readonly property QtObject backend: sourcesBackend
readonly property bool isDefault: ResourcesModel.currentApplicationBackend == resourcesBackend
RowLayout {
Connections {
target: backendItem.backend
onPassiveMessage: window.showPassiveNotification(message)
}
section.property: "sourceName"
section.delegate: Kirigami.AbstractListItem {
id: backendItem
hoverEnabled: false
supportsMouseEvents: false
readonly property QtObject backend: SourcesModel.sourcesBackendByName(section)
readonly property QtObject resourcesBackend: backend.resourcesBackend
readonly property bool isDefault: ResourcesModel.currentApplicationBackend == resourcesBackend
RowLayout {
Connections {
target: backendItem.backend
onPassiveMessage: window.showPassiveNotification(message)
}
anchors {
right: parent.right
left: parent.left
rightMargin: parent.rightPadding
leftMargin: parent.leftPadding
}
Kirigami.Heading {
Layout.fillWidth: true
text: backendItem.isDefault ? i18n("%1 (Default)", resourcesBackend.displayName) : resourcesBackend.displayName
}
Button {
Layout.rightMargin: Kirigami.Units.smallSpacing
icon.name: "preferences-other"
visible: resourcesBackend && resourcesBackend.hasApplications
Component {
id: dialogComponent
AddSourceDialog {
source: backendItem.backend
onVisibleChanged: if (!visible) {
destroy()
}
anchors {
right: parent.right
left: parent.left
rightMargin: parent.rightPadding
leftMargin: parent.leftPadding
}
Kirigami.Heading {
Layout.fillWidth: true
text: backendItem.isDefault ? i18n("%1 (Default)", resourcesBackend.displayName) : resourcesBackend.displayName
}
Button {
Layout.rightMargin: Kirigami.Units.smallSpacing
icon.name: "preferences-other"
visible: resourcesBackend && resourcesBackend.hasApplications
Component {
id: dialogComponent
AddSourceDialog {
source: backendItem.backend
onVisibleChanged: if (!visible) {
destroy()
}
}
}
id: this
onClicked: settingsMenu.popup(this)
Menu {
id: settingsMenu
MenuItem {
enabled: !backendItem.isDefault
text: i18n("Make default")
onTriggered: ResourcesModel.currentApplicationBackend = backendItem.backend.resourcesBackend
}
id: this
onClicked: settingsMenu.popup(this)
Menu {
id: settingsMenu
MenuItem {
enabled: !backendItem.isDefault
text: i18n("Make default")
onTriggered: ResourcesModel.currentApplicationBackend = backendItem.backend.resourcesBackend
}
MenuItem {
text: i18n("Add Source...")
visible: backendItem.backend && backendItem.backend.supportsAdding
MenuItem {
text: i18n("Add Source...")
visible: backendItem.backend && backendItem.backend.supportsAdding
onTriggered: {
var addSourceDialog = dialogComponent.createObject(null, {displayName: backendItem.backend.resourcesBackend.displayName })
addSourceDialog.open()
}
onTriggered: {
var addSourceDialog = dialogComponent.createObject(null, {displayName: backendItem.backend.resourcesBackend.displayName })
addSourceDialog.open()
}
}
MenuSeparator {
visible: backendActionsInst.count>0
}
MenuSeparator {
visible: backendActionsInst.count>0
}
Instantiator {
id: backendActionsInst
model: ActionsModel {
actions: backendItem.backend ? backendItem.backend.actions : undefined
}
delegate: MenuItem {
action: ActionBridge {
action: modelData.action
}
}
onObjectAdded: {
settingsMenu.insertItem(index, object)
}
onObjectRemoved: {
object.destroy()
Instantiator {
id: backendActionsInst
model: ActionsModel {
actions: backendItem.backend ? backendItem.backend.actions : undefined
}
delegate: MenuItem {
action: ActionBridge {
action: modelData
}
}
onObjectAdded: {
settingsMenu.insertItem(index, object)
}
onObjectRemoved: {
object.destroy()
}
}
}
}
}
}
delegate: ConditionalLoader {
anchors {
right: parent.right
left: parent.left
}
readonly property variant resourcesBackend: model.resourcesBackend
readonly property variant sourcesBackend: model.sourcesBackend
readonly property variant display: model.display
readonly property variant checked: model.checked
readonly property variant statusTip: model.statusTip
readonly property variant toolTip: model.toolTip
readonly property variant sourceId: model.sourceId
readonly property variant modelIndex: sourcesView.model.index(index, 0)
condition: resourcesBackend != null
componentTrue: sourceBackendDelegate
componentFalse: sourceDelegate
}
Component {
id: sourceDelegate
Kirigami.SwipeListItem {
Layout.fillWidth: true
enabled: display.length>0
highlighted: ListView.isCurrentItem
onClicked: Navigation.openApplicationListSource(sourceId)
Keys.onReturnPressed: clicked()
actions: [
Kirigami.Action {
iconName: "go-up"
enabled: sourcesBackend.firstSourceId !== sourceId
visible: sourcesBackend.canMoveSources
onTriggered: {
var ret = sourcesBackend.moveSource(sourceId, -1)
if (!ret)
window.showPassiveNotification(i18n("Failed to increase '%1' preference", display))
}
},
Kirigami.Action {
iconName: "go-down"
enabled: sourcesBackend.lastSourceId !== sourceId
visible: sourcesBackend.canMoveSources
onTriggered: {
var ret = sourcesBackend.moveSource(sourceId, +1)
delegate: Kirigami.SwipeListItem {
Layout.fillWidth: true
enabled: display.length>0
highlighted: ListView.isCurrentItem
onClicked: Navigation.openApplicationListSource(sourceId)
Keys.onReturnPressed: clicked()
actions: [
Kirigami.Action {
iconName: "go-up"
enabled: sourcesBackend.firstSourceId !== sourceId
visible: sourcesBackend.canMoveSources
onTriggered: {
var ret = sourcesBackend.moveSource(sourceId, -1)
if (!ret)
window.showPassiveNotification(i18n("Failed to decrease '%1' preference", display))
}
},
Kirigami.Action {
iconName: "edit-delete"
tooltip: i18n("Delete the origin")
onTriggered: {
var backend = sourcesBackend
if (!backend.removeSource(sourceId)) {
window.showPassiveNotification(i18n("Failed to remove the source '%1'", display))
}
}
window.showPassiveNotification(i18n("Failed to increase '%1' preference", display))
}
]
RowLayout {
CheckBox {
id: enabledBox
readonly property variant modelChecked: sourcesView.model.data(modelIndex, Qt.CheckStateRole)
checked: modelChecked != Qt.Unchecked
enabled: modelChecked !== undefined
onClicked: {
sourcesView.model.setData(modelIndex, checkedState, Qt.CheckStateRole)
},
Kirigami.Action {
iconName: "go-down"
enabled: sourcesBackend.lastSourceId !== sourceId
visible: sourcesBackend.canMoveSources
onTriggered: {
var ret = sourcesBackend.moveSource(sourceId, +1)
if (!ret)
window.showPassiveNotification(i18n("Failed to decrease '%1' preference", display))
}
},
Kirigami.Action {
iconName: "edit-delete"
tooltip: i18n("Delete the origin")
visible: sourcesBackend.supportsAdding
onTriggered: {
var backend = sourcesBackend
if (!backend.removeSource(sourceId)) {
window.showPassiveNotification(i18n("Failed to remove the source '%1'", display))
}
}
Label {
text: display + (toolTip ? " - <i>" + toolTip + "</i>" : "")
elide: Text.ElideRight
Layout.fillWidth: true
}
]
RowLayout {
CheckBox {
id: enabledBox
readonly property variant idx: sourcesView.model.index(index, 0)
readonly property variant modelChecked: sourcesView.model.data(idx, Qt.CheckStateRole)
checked: modelChecked != Qt.Unchecked
enabled: modelChecked !== undefined
onClicked: {
sourcesView.model.setData(idx, checkedState, Qt.CheckStateRole)
}
}
Label {
text: display + (toolTip ? " - <i>" + toolTip + "</i>" : "")
elide: Text.ElideRight
Layout.fillWidth: true
}
}
}
......
......@@ -119,6 +119,13 @@ DiscoverPage
id: updatesView
currentIndex: -1
displaced: Transition {
YAnimator {
duration: Kirigami.Units.longDuration
easing.type: Easing.InOutQuad
}
}
footer: ColumnLayout {
anchors.right: parent.right
anchors.left: parent.left
......
......@@ -37,11 +37,32 @@
#include <QThread>
#include <QTimer>
#include <QAction>
#include <QStandardItemModel>
#include "utils.h"
DISCOVER_BACKEND_PLUGIN(SnapBackend)
class SnapSourcesBackend : public AbstractSourcesBackend
{
public:
explicit SnapSourcesBackend(AbstractResourcesBackend * parent) : AbstractSourcesBackend(parent), m_model(new QStandardItemModel) {
m_model->appendRow(new QStandardItem(i18n("Snap")));
}
QAbstractItemModel* sources() override { return m_model; }
bool addSource(const QString& /*id*/) override { return false; }
bool removeSource(const QString& /*id*/) override { return false;}
QString idDescription() override { return QStringLiteral("Snap"); }
QList<QAction*> actions() const override { return {}; }
bool supportsAdding() const override { return false; }
bool canMoveSources() const override { return false; }
private:
QStandardItemModel* const m_model;
};
SnapBackend::SnapBackend(QObject* parent)
: AbstractResourcesBackend(parent)
, m_updater(new StandardBackendUpdater(this))
......@@ -61,7 +82,7 @@ SnapBackend::SnapBackend(QObject* parent)
//make sure we populate the installed resources first
refreshStates();
SourcesModel::global()->addBackend(this);
SourcesModel::global()->addSourcesBackend(new SnapSourcesBackend(this));
}
int SnapBackend::updatesCount() const
......
......@@ -66,6 +66,7 @@ QHash<int, QByteArray> SourcesModel::roleNames() const
{
QHash<int, QByteArray> roles = KConcatenateRowsProxyModel::roleNames();
roles.insert(AbstractSourcesBackend::IdRole, "sourceId");
roles.insert(SourceNameRole, "sourceName");
roles.insert(SourcesBackend, "sourcesBackend");
roles.insert(ResourcesBackend, "resourcesBackend");
return roles;
......@@ -74,32 +75,13 @@ QHash<int, QByteArray> SourcesModel::roleNames() const
void SourcesModel::addSourcesBackend(AbstractSourcesBackend* sources)
{
auto backend = qobject_cast<AbstractResourcesBackend*>(sources->parent());
auto b = addBackend(backend);
if (!b)
return;
auto m = sources->sources();
m->setProperty(DisplayName, backend->displayName());
m->setProperty(SourcesBackendId, qVariantFromValue<QObject*>(sources));
b->setProperty(SourcesBackendId, qVariantFromValue<QObject*>(sources));
addSourceModel(m);
}
SourceBackendModel* SourcesModel::addBackend(AbstractResourcesBackend* backend)
{
Q_ASSERT(backend);
const auto inSourcesModel = "InSourcesModel";
if (backend->dynamicPropertyNames().contains(inSourcesModel))
return nullptr;
backend->setProperty(inSourcesModel, 1);
auto b = new SourceBackendModel(backend);
b->setProperty(DisplayName, backend->displayName());
addSourceModel(b);
return b;
}
const QAbstractItemModel * SourcesModel::modelAt(const QModelIndex& index) const
{
const auto sidx = mapToSource(index);
......@@ -119,4 +101,15 @@ QVariant SourcesModel::data(const QModelIndex& index, int role) const
}
}
AbstractSourcesBackend * SourcesModel::sourcesBackendByName(const QString& id) const
{
for(int i = 0, c = rowCount(); i < c; ++i) {
const auto idx = index(i, 0);
if (idx.data(SourceNameRole) == id) {
return qobject_cast<AbstractSourcesBackend *>(idx.data(SourcesBackend).value<QObject*>());
}
}
return nullptr;
}
#include "SourcesModel.moc"
......@@ -27,10 +27,6 @@
#include "discovercommon_export.h"
#include "AbstractSourcesBackend.h"
class QAction;
class AbstractResourcesBackend;
class SourceBackendModel;
class DISCOVERCOMMON_EXPORT SourcesModel : public KConcatenateRowsProxyModel
{
Q_OBJECT
......@@ -49,9 +45,10 @@ class DISCOVERCOMMON_EXPORT SourcesModel : public KConcatenateRowsProxyModel
QVariant data(const QModelIndex & index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
SourceBackendModel* addBackend(AbstractResourcesBackend* backend);
void addSourcesBackend(AbstractSourcesBackend* sources);
Q_SCRIPTABLE AbstractSourcesBackend* sourcesBackendByName(const QString &name) const;
private:
const QAbstractItemModel* modelAt(const QModelIndex& idx) const;
};
......
Markdown is supported
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