From 6aaafd55328c1860fd1b734fa29ff77673538a2b Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Tue, 25 Jun 2013 15:39:11 -0400 Subject: unitymenumodel: add support for icons For now, this includes themed icons, file icons, and icons send as raw data. --- libqmenumodel/src/unitymenumodel.cpp | 56 +++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'libqmenumodel/src/unitymenumodel.cpp') 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 UnityMenuModel::roleNames() const names[ActionRole] = "action"; names[SensitiveRole] = "sensitive"; names[IsSeparatorRole] = "isSeparator"; + names[IconRole] = "icon"; return names; } -- cgit v1.2.3