use a simple model for the application list

This commit is contained in:
Marco Martin 2014-10-28 13:27:54 +01:00
parent 2832ca129d
commit dbda61e030
11 changed files with 335 additions and 23 deletions

View file

@ -38,3 +38,5 @@ plasma_install_package(look-and-feel org.kde.satellite.phone look-and-feel)
plasma_install_package(shell org.kde.satellite.phone shells) plasma_install_package(shell org.kde.satellite.phone shells)
install(DIRECTORY wallpaper/ DESTINATION "${WALLPAPER_INSTALL_DIR}/org.kde.satellite.lockers") install(DIRECTORY wallpaper/ DESTINATION "${WALLPAPER_INSTALL_DIR}/org.kde.satellite.lockers")
add_subdirectory(qmlcomponents)

View file

@ -0,0 +1,16 @@
project(satellitecomponents)
set(satellitecomponents_SRCS
satellitecomponentsplugin.cpp
applicationlistmodel.cpp
)
add_library(satellitecomponentsplugin SHARED ${satellitecomponents_SRCS})
target_link_libraries(satellitecomponentsplugin Qt5::Core Qt5::Widgets Qt5::Qml Qt5::Quick KF5::Declarative KF5::I18n KF5::KIOCore KF5::KIOWidgets)
install(TARGETS satellitecomponentsplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/satellite/components)
install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kde/satellite/components)

View file

@ -0,0 +1,4 @@
#! /usr/bin/env bash
$XGETTEXT `find . -name \*.qml` -L Java -o $podir/libsatellitecomponentsplugin.pot
rm -f rc.cpp

View file

@ -0,0 +1,139 @@
/*
* Copyright (C) 2014 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* or (at your option) any later version, as published by the Free
* Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// Self
#include "applicationlistmodel.h"
// Qt
#include <QByteArray>
#include <QModelIndex>
// KDE
#include <KPluginInfo>
#include <KRun>
#include <KService>
#include <KServiceGroup>
#include <KServiceTypeTrader>
#include <KSycocaEntry>
#include <QDebug>
ApplicationListModel::ApplicationListModel(QObject *parent)
: QAbstractListModel(parent)
{
loadApplications();
}
ApplicationListModel::~ApplicationListModel()
{
}
QHash<int, QByteArray> ApplicationListModel::roleNames() const
{
QHash<int, QByteArray> roleNames;
roleNames[ApplicationNameRole] = "ApplicationNameRole";
roleNames[ApplicationIconRole] = "ApplicationIconRole";
roleNames[ApplicationStorageIdRole] = "ApplicationStorageIdRole";
roleNames[ApplicationEntryPathRole] = "ApplicationEntryPathRole";
return roleNames;
}
void ApplicationListModel::loadApplications()
{
beginResetModel();
KServiceGroup::Ptr group = KServiceGroup::root();
if (!group || !group->isValid()) return;
KServiceGroup::List subGroupList = group->entries(true);
// Iterate over all entries in the group
for(KServiceGroup::List::ConstIterator it = subGroupList.begin();it != subGroupList.end(); it++) {
KSycocaEntry::Ptr groupEntry = (*it);
if (groupEntry->isType(KST_KServiceGroup) && groupEntry->name() != "System") {
KServiceGroup::Ptr serviceGroup = static_cast<KServiceGroup::Ptr >(groupEntry);
if (!serviceGroup->noDisplay()) {
KServiceGroup::List entryGroupList = serviceGroup->entries(true);
for(KServiceGroup::List::ConstIterator it = entryGroupList.begin(); it != entryGroupList.end(); it++) {
KSycocaEntry::Ptr entry = (*it);
ApplicationData data;
if (entry->isType(KST_KService)) {
KService::Ptr service = static_cast<KService::Ptr >(entry);
if (service->isApplication()) {
KPluginInfo plugin(service);
data.name = plugin.name();
data.icon = plugin.icon();
data.storageId = service->storageId();
data.entryPath = plugin.entryPath();
m_applicationList << data;
}
}
}
}
}
}
endResetModel();
emit countChanged();
}
QVariant ApplicationListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
switch (role) {
case Qt::DisplayRole:
case ApplicationNameRole:
return m_applicationList.at(index.row()).name;
case ApplicationIconRole:
return m_applicationList.at(index.row()).icon;
case ApplicationStorageIdRole:
return m_applicationList.at(index.row()).storageId;
case ApplicationEntryPathRole:
return m_applicationList.at(index.row()).entryPath;
default:
return QVariant();
}
}
int ApplicationListModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return m_applicationList.count();
}
void ApplicationListModel::runApplication(const QString &storageId) {
if (storageId.isEmpty()) {
return;
}
KService::Ptr service = KService::serviceByStorageId(storageId);
KRun::run(*service, QList<QUrl>(), 0);
}
#include "applicationlistmodel.moc"

View file

@ -0,0 +1,71 @@
/*
* Copyright (C) 2014 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* or (at your option) any later version, as published by the Free
* Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef APPLICATIONLISTMODEL_H
#define APPLICATIONLISTMODEL_H
// Qt
#include <QObject>
#include <QAbstractListModel>
#include <QList>
class QString;
struct ApplicationData {
QString name;
QString icon;
QString storageId;
QString entryPath;
};
class ApplicationListModel : public QAbstractListModel {
Q_OBJECT
Q_PROPERTY(int count READ count NOTIFY countChanged)
public:
ApplicationListModel(QObject *parent = 0);
virtual ~ApplicationListModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
int count() { return m_applicationList.count(); }
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
enum Roles {
ApplicationNameRole = Qt::UserRole + 1,
ApplicationIconRole = Qt::UserRole + 2,
ApplicationStorageIdRole = Qt::UserRole + 3,
ApplicationEntryPathRole = Qt::UserRole + 4
};
Q_INVOKABLE void runApplication(const QString &storageId);
Q_SIGNALS:
void countChanged();
private:
QList<ApplicationData> m_applicationList;
void loadApplications();
};
#endif // APPLICATIONLISTMODEL_H

2
qmlcomponents/qmldir Normal file
View file

@ -0,0 +1,2 @@
module org.kde.satellite.components
plugin satellitecomponentsplugin

View file

@ -0,0 +1,52 @@
/*
* Copyright 2009 by Alan Alpert <alan.alpert@nokia.com>
* Copyright 2010 by Ménard Alexis <menard@kde.org>
* Copyright 2010 by Marco Martin <mart@kde.org>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "satellitecomponentsplugin.h"
#include <QtQml>
#include <QQmlExtensionPlugin>
#include <QQmlEngine>
#include <QQmlContext>
#include <kdeclarative/kdeclarative.h>
#include "applicationlistmodel.h"
void SatelliteComponentsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
{
QQmlExtensionPlugin::initializeEngine(engine, uri);
if (!engine->rootContext()->contextObject()) {
KDeclarative::KDeclarative kdeclarative;
kdeclarative.setDeclarativeEngine(engine);
kdeclarative.setupBindings();
}
}
void SatelliteComponentsPlugin::registerTypes(const char *uri)
{
Q_ASSERT(uri == QLatin1String("org.kde.satellite.components"));
qmlRegisterType<ApplicationListModel>(uri, 0, 1, "ApplicationListModel");
}
#include "satellitecomponentsplugin.moc"

View file

@ -0,0 +1,39 @@
/*
* Copyright 2009 by Alan Alpert <alan.alpert@nokia.com>
* Copyright 2010 by Ménard Alexis <menard@kde.org>
* Copyright 2010 by Marco Martin <mart@kde.org>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef SATELLITECOMPONENTSPLUGIN_H
#define SATELLITECOMPONENTSPLUGIN_H
#include <QQmlEngine>
#include <QQmlExtensionPlugin>
class SatelliteComponentsPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public:
void initializeEngine(QQmlEngine *engine, const char *uri);
void registerTypes(const char *uri);
};
#endif

View file

@ -7,19 +7,16 @@ MouseArea {
width: applications.cellWidth width: applications.cellWidth
height: width height: width
onClicked: { onClicked: {
console.log("Clicked: " + model.entryPath) console.log("Clicked: " + model.ApplicationStorageIdRole)
krun.openUrl(model.entryPath) appListModel.runApplication(model.ApplicationStorageIdRole)
} }
Kio.KRun {
id: krun
}
PlasmaCore.IconItem { PlasmaCore.IconItem {
id: icon id: icon
anchors.centerIn: parent anchors.centerIn: parent
width: units.iconSizes.large width: units.iconSizes.large
height: width height: width
source: iconName source: model.ApplicationIconRole
} }
Text { Text {
@ -35,7 +32,8 @@ MouseArea {
horizontalAlignment: Qt.AlignHCenter horizontalAlignment: Qt.AlignHCenter
verticalAlignment: Qt.AlignVCenter verticalAlignment: Qt.AlignVCenter
text: name text: model.ApplicationNameRole
font.pixelSize: theme.smallestFont.pixelSize
color: "white" color: "white"
} }
} }

View file

@ -2,7 +2,7 @@
LookAndFeelPackage=org.kde.satellite.phone LookAndFeelPackage=org.kde.satellite.phone
[Desktop] [Desktop]
Containment=null Containment=org.kde.desktopcontainment
ToolBox= ToolBox=
[Desktop][ContainmentActions] [Desktop][ContainmentActions]

View file

@ -22,6 +22,7 @@ import QtQuick 2.0
import QtGraphicalEffects 1.0 import QtGraphicalEffects 1.0
import org.kde.plasma.core 2.0 as PlasmaCore import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.shell 2.0 as Shell import org.kde.plasma.shell 2.0 as Shell
import org.kde.satellite.components 0.1 as SatelliteComponents
import "../components" import "../components"
Item { Item {
@ -330,20 +331,8 @@ Item {
} }
} }
PlasmaCore.DataSource { SatelliteComponents.ApplicationListModel {
id: applicationsSource id: appListModel
engine: "apps"
interval: 0
connectedSources: sources
onSourceAdded: {
connectSource(source);
}
onSourceRemoved: {
disconnectSource(source);
}
} }
GridView { GridView {
@ -358,7 +347,7 @@ Item {
z: 1 z: 1
cellWidth: stripe.height * 2 cellWidth: stripe.height * 2
cellHeight: cellWidth cellHeight: cellWidth
model: PlasmaCore.DataModel { dataSource: applicationsSource } model: appListModel
snapMode: GridView.SnapToRow snapMode: GridView.SnapToRow
clip: true clip: true
delegate: HomeLauncher {} delegate: HomeLauncher {}