diff options
author | Holger Hans Peter Freyther <holger@moiji-mobile.com> | 2017-07-13 12:16:43 +0200 |
---|---|---|
committer | Holger Freyther <holger+qt@freyther.de> | 2017-09-12 06:14:39 +0000 |
commit | df9402f361b521877fdc2b1ec23a1e205699224c (patch) | |
tree | 92d9fb35db9aba34f668ccae2a9eaf3d6ec8e8de /wayland/democompositor/apps | |
parent | c09aa96a136dd841e75b459d78ea27ad2bc4474c (diff) |
democompositor: Introduce an AppListModel to expose apps
Turn the AppEntry into a gadget and store the filename of where the
entry came from. Update the existing test to deal with it.
Introduce AppListModel as a QAbstractItemModel and expose the raw
AppEntry and its fields as role. Have some slow O(n) code to check
if an entry has already been added. The goal is to use a directory
watcher to automatically update this list.
Store the AppEntry as a pointer so we don't have to remove the
const from the field.
The next steps are to fully populate the AppListModel by scanning
and watching a directory and then replacing the LaunchButton code
with a Repeater on the application model. After starting of apps
will be done by passing the AppEntry to a launcher. Application
state should be stored outside the AppEntry and maybe in a different
model as well.
Change-Id: Ia933a09942ea8bb88eae5c101d13a30564d8c639
Reviewed-by: Paul Olav Tvete <paul.tvete@qt.io>
Diffstat (limited to 'wayland/democompositor/apps')
-rw-r--r-- | wayland/democompositor/apps/appentry.cpp | 2 | ||||
-rw-r--r-- | wayland/democompositor/apps/appentry.h | 12 | ||||
-rw-r--r-- | wayland/democompositor/apps/applistmodel.cpp | 136 | ||||
-rw-r--r-- | wayland/democompositor/apps/applistmodel.h | 88 | ||||
-rw-r--r-- | wayland/democompositor/apps/appparser.cpp | 6 | ||||
-rw-r--r-- | wayland/democompositor/apps/appparser.h | 2 | ||||
-rw-r--r-- | wayland/democompositor/apps/apps.pri | 2 |
7 files changed, 242 insertions, 6 deletions
diff --git a/wayland/democompositor/apps/appentry.cpp b/wayland/democompositor/apps/appentry.cpp index 6cd813a..aaae167 100644 --- a/wayland/democompositor/apps/appentry.cpp +++ b/wayland/democompositor/apps/appentry.cpp @@ -53,5 +53,5 @@ AppEntry AppEntry::empty() { QString empty; - return AppEntry{empty, empty, empty, empty}; + return AppEntry{empty, empty, empty, empty, empty}; } diff --git a/wayland/democompositor/apps/appentry.h b/wayland/democompositor/apps/appentry.h index 6de410c..cb7a727 100644 --- a/wayland/democompositor/apps/appentry.h +++ b/wayland/democompositor/apps/appentry.h @@ -50,17 +50,27 @@ #pragma once +#include <QtCore/QObject> #include <QtCore/QString> /** * A const representatation of an application entry. Members include * executable name, path, icon and other information in the future. */ -struct AppEntry { +class AppEntry { + Q_GADGET + Q_PROPERTY(QString iconName MEMBER iconName CONSTANT) + Q_PROPERTY(QString appName MEMBER appName CONSTANT) + Q_PROPERTY(QString executableName MEMBER executableName CONSTANT) + Q_PROPERTY(QString executablePath MEMBER executablePath CONSTANT) +public: const QString iconName; const QString appName; const QString executableName; const QString executablePath; + const QString sourceFileName; static AppEntry empty(); }; + +Q_DECLARE_METATYPE(AppEntry) diff --git a/wayland/democompositor/apps/applistmodel.cpp b/wayland/democompositor/apps/applistmodel.cpp new file mode 100644 index 0000000..542f8a7 --- /dev/null +++ b/wayland/democompositor/apps/applistmodel.cpp @@ -0,0 +1,136 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of Qt for Device Creation. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "applistmodel.h" +#include "appparser.h" +#include "applog.h" + +static QHash<int, QByteArray> modelRoles() +{ + QHash<int, QByteArray> roles; + roles[AppListModel::App] = "appEntry"; + roles[AppListModel::IconName] = "iconName"; + roles[AppListModel::ApplicationName] = "applicationName"; + roles[AppListModel::ExeuctableName] = "executableName"; + roles[AppListModel::ExecutablePath] = "executablePath"; + roles[AppListModel::SourceFileName] = "sourceFileName"; + return roles; +} + +QHash<int, QByteArray> AppListModel::m_roles = modelRoles(); + +AppListModel::~AppListModel() +{ + qDeleteAll(m_rows); +} + +int AppListModel::rowCount(const QModelIndex& index) const +{ + if (index.isValid()) + return 0; + return m_rows.count(); +} + +QVariant AppListModel::data(const QModelIndex& index, int role) const +{ + if (!index.isValid() || index.parent().isValid()) + return QVariant(); + + auto entry = m_rows[index.row()]; + + switch (role) { + case App: + return QVariant::fromValue(*entry); + case IconName: + return entry->iconName; + case ApplicationName: + return entry->appName; + case ExeuctableName: + return entry->executableName; + case ExecutablePath: + return entry->executablePath; + case SourceFileName: + return entry->sourceFileName; + default: + qCWarning(apps) << "Unhandled role" << role; + return QVariant(); + } +} + +QHash<int, QByteArray> AppListModel::roleNames() const +{ + return m_roles; +} + +void AppListModel::addFile(const QString& fileName) +{ + beginResetModel(); + doAddFile(fileName); + endResetModel(); +} + +void AppListModel::doAddFile(const QString& fileName) +{ + bool ok; + auto newEntry = AppParser::parseFile(fileName, &ok); + if (!ok) + return; + + for (int i = 0; i < m_rows.count(); ++i) { + auto oldEntry = m_rows[i]; + if (oldEntry->sourceFileName == fileName) { + m_rows[i] = new AppEntry(newEntry); + delete oldEntry; + return; + } + } + + m_rows.push_back(new AppEntry(newEntry)); +} diff --git a/wayland/democompositor/apps/applistmodel.h b/wayland/democompositor/apps/applistmodel.h new file mode 100644 index 0000000..f8fc3e0 --- /dev/null +++ b/wayland/democompositor/apps/applistmodel.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of Qt for Device Creation. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#pragma once + +#include <QtCore/QAbstractListModel> +#include <QtCore/QVector> + +class AppEntry; + +/** + * A model that holds all available applications and + * exports them to a QML scene + */ +class AppListModel : public QAbstractListModel { + Q_OBJECT +public: + + ~AppListModel(); + + enum Roles { + App = Qt::UserRole, + IconName, + ApplicationName, + ExeuctableName, + ExecutablePath, + SourceFileName, + }; + + int rowCount(const QModelIndex& parent) const Q_DECL_OVERRIDE; + QVariant data(const QModelIndex& index, int role) const Q_DECL_OVERRIDE; + QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE; + + void addFile(const QString& fileName); + +private: + void doAddFile(const QString& fileName); + + QVector<AppEntry*> m_rows; + static QHash<int, QByteArray> m_roles; +}; diff --git a/wayland/democompositor/apps/appparser.cpp b/wayland/democompositor/apps/appparser.cpp index 18d2d22..2c373cc 100644 --- a/wayland/democompositor/apps/appparser.cpp +++ b/wayland/democompositor/apps/appparser.cpp @@ -94,7 +94,7 @@ static int readInt(const QJsonObject& object, const QString& key, bool *ok) return doReadInt(object.value(key), ok); } -AppEntry AppParser::parseData(const QByteArray& content, bool *ok) +AppEntry AppParser::parseData(const QByteArray& content, const QString& fileName, bool *ok) { *ok = true; QJsonParseError error; @@ -132,7 +132,7 @@ AppEntry AppParser::parseData(const QByteArray& content, bool *ok) if (!*ok) return AppEntry::empty(); - return AppEntry{iconName, appName, executableName, executablePath}; + return AppEntry{iconName, appName, executableName, executablePath, fileName}; } AppEntry AppParser::parseFile(const QString& fileName, bool *ok) @@ -146,7 +146,7 @@ AppEntry AppParser::parseFile(const QString& fileName, bool *ok) return AppEntry::empty(); } - auto entry = parseData(file.readAll(), ok); + auto entry = parseData(file.readAll(), fileName, ok); file.close(); if (!*ok) { qCWarning(apps) << "Failed to parse" << fileName; diff --git a/wayland/democompositor/apps/appparser.h b/wayland/democompositor/apps/appparser.h index da52c5a..a6711b4 100644 --- a/wayland/democompositor/apps/appparser.h +++ b/wayland/democompositor/apps/appparser.h @@ -57,6 +57,6 @@ */ class AppParser { public: - static AppEntry parseData(const QByteArray& content, bool *ok); + static AppEntry parseData(const QByteArray& content, const QString& fileName, bool *ok); static AppEntry parseFile(const QString& fileName, bool *ok); }; diff --git a/wayland/democompositor/apps/apps.pri b/wayland/democompositor/apps/apps.pri index dfcee61..9b19060 100644 --- a/wayland/democompositor/apps/apps.pri +++ b/wayland/democompositor/apps/apps.pri @@ -2,8 +2,10 @@ HEADERS += \ apps/appentry.h \ apps/applog.h \ + apps/applistmodel.h \ apps/appparser.h SOURCES += \ apps/appentry.cpp \ + apps/applistmodel.cpp \ apps/appparser.cpp |