diff options
author | Lars Uebernickel <lars.uebernickel@canonical.com> | 2013-06-25 15:39:11 -0400 |
---|---|---|
committer | Lars Uebernickel <lars.uebernickel@canonical.com> | 2013-06-25 15:39:11 -0400 |
commit | 6aaafd55328c1860fd1b734fa29ff77673538a2b (patch) | |
tree | b2c54591354672c6c15b959160a5d013345329b7 | |
parent | 050958c58924934b10cf3a6272cff3ebe867f1dd (diff) | |
download | qmenumodel-6aaafd55328c1860fd1b734fa29ff77673538a2b.tar.gz qmenumodel-6aaafd55328c1860fd1b734fa29ff77673538a2b.tar.bz2 qmenumodel-6aaafd55328c1860fd1b734fa29ff77673538a2b.zip |
unitymenumodel: add support for icons
For now, this includes themed icons, file icons, and icons send as raw data.
-rw-r--r-- | libqmenumodel/QMenuModel/CMakeLists.txt | 2 | ||||
-rw-r--r-- | libqmenumodel/QMenuModel/plugin.cpp | 6 | ||||
-rw-r--r-- | libqmenumodel/QMenuModel/plugin.h | 1 | ||||
-rw-r--r-- | libqmenumodel/src/CMakeLists.txt | 5 | ||||
-rw-r--r-- | libqmenumodel/src/unitymenumodel.cpp | 56 | ||||
-rw-r--r-- | libqmenumodel/src/unitythemediconprovider.cpp | 35 | ||||
-rw-r--r-- | libqmenumodel/src/unitythemediconprovider.h | 31 |
7 files changed, 132 insertions, 4 deletions
diff --git a/libqmenumodel/QMenuModel/CMakeLists.txt b/libqmenumodel/QMenuModel/CMakeLists.txt index 7367e18..78e062b 100644 --- a/libqmenumodel/QMenuModel/CMakeLists.txt +++ b/libqmenumodel/QMenuModel/CMakeLists.txt @@ -19,7 +19,7 @@ target_link_libraries(qmenumodel-qml ${GIO_LDFLAGS} ) -qt5_use_modules(qmenumodel-qml Qml Widgets) +qt5_use_modules(qmenumodel-qml Qml Quick) execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/qmldir" "${CMAKE_CURRENT_BINARY_DIR}/qmldir") diff --git a/libqmenumodel/QMenuModel/plugin.cpp b/libqmenumodel/QMenuModel/plugin.cpp index f529c0a..0629099 100644 --- a/libqmenumodel/QMenuModel/plugin.cpp +++ b/libqmenumodel/QMenuModel/plugin.cpp @@ -23,9 +23,15 @@ #include "qdbusactiongroup.h" #include "qstateaction.h" #include "unitymenumodel.h" +#include "unitythemediconprovider.h" #include <QtQml> +void QMenuModelQmlPlugin::initializeEngine(QQmlEngine *engine, const char *uri) +{ + engine->addImageProvider("theme", new UnityThemedIconProvider); +} + void QMenuModelQmlPlugin::registerTypes(const char *uri) { qmlRegisterUncreatableType<QMenuModel>(uri, 0, 1, "QMenuModel", diff --git a/libqmenumodel/QMenuModel/plugin.h b/libqmenumodel/QMenuModel/plugin.h index fc732d2..3474139 100644 --- a/libqmenumodel/QMenuModel/plugin.h +++ b/libqmenumodel/QMenuModel/plugin.h @@ -28,6 +28,7 @@ class QMenuModelQmlPlugin : public QQmlExtensionPlugin Q_PLUGIN_METADATA(IID "com.canonical.qmenumodel") public: + void initializeEngine(QQmlEngine *engine, const char *uri); void registerTypes(const char *uri); }; diff --git a/libqmenumodel/src/CMakeLists.txt b/libqmenumodel/src/CMakeLists.txt index d74f2cc..24b65d2 100644 --- a/libqmenumodel/src/CMakeLists.txt +++ b/libqmenumodel/src/CMakeLists.txt @@ -10,7 +10,7 @@ set(QMENUMODEL_SRC qdbusactiongroup.cpp qstateaction.cpp unitymenumodel.cpp - unitymenumodel.h + unitythemediconprovider.cpp gtk/gtkactionmuxer.c gtk/gtkactionmuxer.h gtk/gtkactionobservable.c @@ -44,7 +44,7 @@ target_link_libraries(${SHAREDLIBNAME} ${GIO_LDFLAGS} ) -qt5_use_modules(${SHAREDLIBNAME} Core Qml) +qt5_use_modules(${SHAREDLIBNAME} Core Qml Quick) install(TARGETS ${SHAREDLIBNAME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) @@ -56,6 +56,7 @@ set(QMENUMODEL_HEADERS qmenumodel.h qstateaction.h unitymenumodel.h + unitythemediconprovider.h ) set(INCLUDEDIR qmenumodel) diff --git a/libqmenumodel/src/unitymenumodel.cpp b/libqmenumodel/src/unitymenumodel.cpp index 81f5121..db70936 100644 --- a/libqmenumodel/src/unitymenumodel.cpp +++ b/libqmenumodel/src/unitymenumodel.cpp @@ -31,7 +31,8 @@ enum MenuRoles { ActionRole = Qt::DisplayRole + 1, LabelRole, SensitiveRole, - IsSeparatorRole + IsSeparatorRole, + IconRole }; class UnityMenuModelPrivate @@ -279,6 +280,47 @@ int UnityMenuModel::columnCount(const QModelIndex &parent) const return 1; } +static QString iconUri(GIcon *icon) +{ + QString uri; + + if (G_IS_THEMED_ICON (icon)) { + const gchar * const *names; + + names = g_themed_icon_get_names (G_THEMED_ICON (icon)); + if (names && names[0] && *names[0]) + uri = QString("image://theme/") + names[0]; + } + else if (G_IS_FILE_ICON (icon)) { + GFile *file; + + file = g_file_icon_get_file (G_FILE_ICON (icon)); + if (g_file_is_native (file)) { + gchar *fileuri; + + fileuri = g_file_get_path (file); + uri = QString(fileuri); + + g_free (fileuri); + } + } + else if (G_IS_BYTES_ICON (icon)) { + gsize size; + gconstpointer data; + gchar *base64; + + data = g_bytes_get_data (g_bytes_icon_get_bytes (G_BYTES_ICON (icon)), &size); + base64 = g_base64_encode ((const guchar *) data, size); + + uri = QString("data://"); + uri.append (base64); + + g_free (base64); + } + + return uri; +} + QVariant UnityMenuModel::data(const QModelIndex &index, int role) const { GtkMenuTrackerItem *item; @@ -295,6 +337,17 @@ QVariant UnityMenuModel::data(const QModelIndex &index, int role) const case IsSeparatorRole: return gtk_menu_tracker_item_get_is_separator (item); + case IconRole: { + GIcon *icon = gtk_menu_tracker_item_get_icon (item); + if (icon) { + QString uri = iconUri(icon); + g_object_unref (icon); + return uri; + } + else + return QString(); + } + default: return QVariant(); } @@ -318,6 +371,7 @@ QHash<int, QByteArray> UnityMenuModel::roleNames() const names[ActionRole] = "action"; names[SensitiveRole] = "sensitive"; names[IsSeparatorRole] = "isSeparator"; + names[IconRole] = "icon"; return names; } diff --git a/libqmenumodel/src/unitythemediconprovider.cpp b/libqmenumodel/src/unitythemediconprovider.cpp new file mode 100644 index 0000000..69afd76 --- /dev/null +++ b/libqmenumodel/src/unitythemediconprovider.cpp @@ -0,0 +1,35 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; version 3. + * + * 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: Lars Uebernickel <lars.uebernickel@canonical.com> + */ + +#include "unitythemediconprovider.h" + +#include <QIcon> + +UnityThemedIconProvider::UnityThemedIconProvider(): + QQuickImageProvider(QQuickImageProvider::Pixmap) +{ +} + +#include <QtDebug> + +QPixmap UnityThemedIconProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) +{ + QPixmap pixmap = QIcon::fromTheme(id).pixmap(requestedSize.isValid() ? requestedSize : QSize(32, 32)); + *size = pixmap.size(); + return pixmap; +} diff --git a/libqmenumodel/src/unitythemediconprovider.h b/libqmenumodel/src/unitythemediconprovider.h new file mode 100644 index 0000000..7e71ea8 --- /dev/null +++ b/libqmenumodel/src/unitythemediconprovider.h @@ -0,0 +1,31 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; version 3. + * + * 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: Lars Uebernickel <lars.uebernickel@canonical.com> + */ + +#ifndef UNITY_THEMED_ICON_PROVIDER_H +#define UNITY_THEMED_ICON_PROVIDER_H + +#include <QQuickImageProvider> + +class UnityThemedIconProvider: public QQuickImageProvider +{ +public: + UnityThemedIconProvider(); + QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize); +}; + +#endif |