diff options
author | Lukáš Lalinský <lalinsky@gmail.com> | 2008-12-08 01:27:57 +0100 |
---|---|---|
committer | Lukáš Lalinský <lalinsky@gmail.com> | 2008-12-08 01:27:57 +0100 |
commit | f56606b58d991e84a4f92942618ff5541929e5a6 (patch) | |
tree | 4cb5cdeae56c1149423f7052b98ac65d7c7be820 | |
parent | e180ea6a919eced6a6a2bfa6dab7ece0f37f79f6 (diff) | |
download | dbmodel-f56606b58d991e84a4f92942618ff5541929e5a6.tar.gz dbmodel-f56606b58d991e84a4f92942618ff5541929e5a6.tar.bz2 |
Use standard GNOME icons if possible
-rw-r--r-- | src/mainwindow.cpp | 23 | ||||
-rw-r--r-- | src/src.pro | 1 | ||||
-rw-r--r-- | src/utils/iconprovider.cpp | 123 | ||||
-rw-r--r-- | src/utils/iconprovider.h | 32 | ||||
-rw-r--r-- | src/utils/utils.pri | 7 |
5 files changed, 176 insertions, 10 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 4ece140..2038f5d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -31,6 +31,8 @@ #include "mainwindow.h" #include "items/database/databasetable.h" #include "commands.h" +#include "utils/iconprovider.h" + using namespace std; @@ -148,26 +150,27 @@ MainWindow::setupActions() { m_actionNew = new QAction(this); m_actionNew->setText(tr("&New")); - m_actionNew->setIcon(QIcon(":/icons/16x16/document-new.png")); + + m_actionNew->setIcon(IconProvider::findIcon(16, "document-new.png")); m_actionNew->setShortcut(QKeySequence(tr("Ctrl+N"))); connect(m_actionNew, SIGNAL(triggered(bool)), SLOT(newModel())); m_actionOpen = new QAction(this); m_actionOpen->setText(tr("&Open...")); - m_actionOpen->setIcon(QIcon(":/icons/16x16/document-open.png")); + m_actionOpen->setIcon(IconProvider::findIcon(16, "document-open.png")); m_actionOpen->setShortcut(QKeySequence(tr("Ctrl+O"))); connect(m_actionOpen, SIGNAL(triggered(bool)), SLOT(open())); m_actionSave = new QAction(this); m_actionSave->setText(tr("&Save")); - m_actionSave->setIcon(QIcon(":/icons/16x16/document-save.png")); + m_actionSave->setIcon(IconProvider::findIcon(16, "document-save.png")); m_actionSave->setShortcut(QKeySequence(tr("Ctrl+S"))); //m_actionSave->setDisabled(true); connect(m_actionSave, SIGNAL(triggered(bool)), SLOT(save())); m_actionSaveAs = new QAction(this); m_actionSaveAs->setText(tr("Save &As...")); - m_actionSaveAs->setIcon(QIcon(":/icons/16x16/document-save-as.png")); + m_actionSaveAs->setIcon(IconProvider::findIcon(16, "document-save-as.png")); //m_actionSaveAs->setDisabled(true); connect(m_actionSaveAs, SIGNAL(triggered(bool)), SLOT(saveAs())); @@ -202,10 +205,10 @@ MainWindow::setupActions() m_actionUndo = m_undoGroup->createUndoAction(this, tr("&Undo")); m_actionUndo->setShortcut(QKeySequence(tr("Ctrl+Z"))); - m_actionUndo->setIcon(QIcon(":/icons/16x16/edit-undo.png")); + m_actionUndo->setIcon(IconProvider::findIcon(16, "edit-undo.png")); m_actionRedo = m_undoGroup->createRedoAction(this, tr("Re&do")); m_actionRedo->setShortcut(QKeySequence(tr("Ctrl+Shift+Z"))); - m_actionRedo->setIcon(QIcon(":/icons/16x16/edit-redo.png")); + m_actionRedo->setIcon(IconProvider::findIcon(16, "edit-redo.png")); connect(m_actionSwitchMode[0], SIGNAL(triggered(bool)), SLOT(switchModeSelect())); connect(m_actionSwitchMode[1], SIGNAL(triggered(bool)), SLOT(switchModeAddTable())); @@ -213,25 +216,25 @@ MainWindow::setupActions() m_actionCut = new QAction(this); m_actionCut->setText(tr("Cu&t")); - m_actionCut->setIcon(QIcon(":/icons/16x16/edit-cut.png")); + m_actionCut->setIcon(IconProvider::findIcon(16, "edit-cut.png")); m_actionCut->setShortcut(QKeySequence(tr("Ctrl+X"))); connect(m_actionCut, SIGNAL(triggered(bool)), SLOT(cut())); m_actionCopy = new QAction(this); m_actionCopy->setText(tr("&Copy")); - m_actionCopy->setIcon(QIcon(":/icons/16x16/edit-copy.png")); + m_actionCopy->setIcon(IconProvider::findIcon(16, "edit-copy.png")); m_actionCopy->setShortcut(QKeySequence(tr("Ctrl+C"))); connect(m_actionCopy, SIGNAL(triggered(bool)), SLOT(copy())); m_actionPaste = new QAction(this); m_actionPaste->setText(tr("&Paste")); - m_actionPaste->setIcon(QIcon(":/icons/16x16/edit-paste.png")); + m_actionPaste->setIcon(IconProvider::findIcon(16, "edit-paste.png")); m_actionPaste->setShortcut(QKeySequence(tr("Ctrl+V"))); connect(m_actionPaste, SIGNAL(triggered(bool)), SLOT(paste())); m_actionDelete = new QAction(this); m_actionDelete->setText(tr("&Delete")); - m_actionDelete->setIcon(QIcon(":/icons/16x16/edit-delete.png")); + m_actionDelete->setIcon(IconProvider::findIcon(16, "edit-delete.png")); m_actionDelete->setShortcut(QKeySequence(tr("Del"))); connect(m_actionDelete, SIGNAL(triggered(bool)), SLOT(deleteSelectedItems())); diff --git a/src/src.pro b/src/src.pro index edf06a1..38efe2d 100644 --- a/src/src.pro +++ b/src/src.pro @@ -25,6 +25,7 @@ HEADERS = \ RESOURCES = ../dbmodel.qrc include(items/items.pri) +include(utils/utils.pri) DESTDIR = ../ diff --git a/src/utils/iconprovider.cpp b/src/utils/iconprovider.cpp new file mode 100644 index 0000000..54e466c --- /dev/null +++ b/src/utils/iconprovider.cpp @@ -0,0 +1,123 @@ +// Copyright (C) 2008 Lukas Lalinsky +// Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, 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 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. + +#include <cstdlib> +#include <QDebug> +#include <QIcon> +#include <QDir> +#include <QVariant> +#include <QLibrary> +#include <QApplication> +#include "iconprovider.h" + +using namespace std; + +#ifdef Q_WS_X11 +extern "C" { + struct GConfClient; + struct GError; + typedef void (*Ptr_g_type_init)(); + typedef GConfClient* (*Ptr_gconf_client_get_default)(); + typedef char* (*Ptr_gconf_client_get_string)(GConfClient*, const char*, GError **); + typedef void (*Ptr_g_object_unref)(void *); + typedef void (*Ptr_g_error_free)(GError *); + typedef void (*Ptr_g_free)(void*); +} + +static Ptr_g_type_init p_g_type_init = 0; +static Ptr_gconf_client_get_default p_gconf_client_get_default = 0; +static Ptr_gconf_client_get_string p_gconf_client_get_string = 0; +static Ptr_g_object_unref p_g_object_unref = 0; +static Ptr_g_error_free p_g_error_free = 0; +static Ptr_g_free p_g_free = 0; +#endif + + +void IconProvider::gnomeLookupIconTheme() +{ +#ifdef Q_WS_X11 + if (themeName.isEmpty()) { + //resolve glib and gconf functions + p_g_type_init = (Ptr_g_type_init)QLibrary::resolve(QLatin1String("gobject-2.0"), 0, "g_type_init"); + p_gconf_client_get_default = (Ptr_gconf_client_get_default)QLibrary::resolve(QLatin1String("gconf-2"), 4, "gconf_client_get_default"); + p_gconf_client_get_string = (Ptr_gconf_client_get_string)QLibrary::resolve(QLatin1String("gconf-2"), 4, "gconf_client_get_string"); + p_g_object_unref = (Ptr_g_object_unref)QLibrary::resolve(QLatin1String("gobject-2.0"), 0, "g_object_unref"); + p_g_error_free = (Ptr_g_error_free)QLibrary::resolve(QLatin1String("glib-2.0"), 0, "g_error_free"); + p_g_free = (Ptr_g_free)QLibrary::resolve(QLatin1String("glib-2.0"), 0, "g_free"); + + if (p_g_type_init && + p_gconf_client_get_default && + p_gconf_client_get_string && + p_g_object_unref && + p_g_error_free && + p_g_free) { + + p_g_type_init(); + GConfClient* client = p_gconf_client_get_default(); + GError *err = 0; + char *str = p_gconf_client_get_string(client, "/desktop/gnome/interface/icon_theme", &err); + if (!err) { + themeName = QString::fromUtf8(str); + p_g_free(str); + } + p_g_object_unref(client); + if (err) + p_g_error_free (err); + } + if (themeName.isEmpty()) + themeName = QLatin1String("gnome"); + } +#endif +} + +void +IconProvider::lookupIconTheme() +{ +#ifdef Q_WS_X11 + if (getenv("GNOME_DESKTOP_SESSION_ID")) { + gnomeLookupIconTheme(); + } + else if (getenv("KDE_FULL_SESSION")) { + // FIXME + } + if (!themeName.isEmpty()) { + iconDirs << QDir::homePath() + "/.icons/"; + iconDirs << "/usr/share/icons/"; + } +#endif +} + +QIcon +IconProvider::findIcon(int size, const QString &name) +{ +#ifdef Q_WS_X11 + IconProvider *iconProvider = static_cast<IconProvider *>(qApp->property("__IconProvider_instance").value<void *>()); + if (iconProvider == NULL) { + iconProvider = new IconProvider(); + iconProvider->lookupIconTheme(); + qApp->setProperty("__IconProvider_instance", qVariantFromValue(static_cast<void *>(iconProvider))); + } + QString sizeString = QString("/%1x%2/").arg(size).arg(size); + foreach(QString path, iconProvider->iconDirs) { + QString fullPath = path + iconProvider->themeName + sizeString + "actions/" + name; + if (QFile::exists(fullPath)) { + return QIcon(fullPath); + } + } +#endif + return QIcon(":/icons" + sizeString + name); +} diff --git a/src/utils/iconprovider.h b/src/utils/iconprovider.h new file mode 100644 index 0000000..07a9d32 --- /dev/null +++ b/src/utils/iconprovider.h @@ -0,0 +1,32 @@ +// Copyright (C) 2008 Lukas Lalinsky +// Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, 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 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. + +#include <QString> + +class IconProvider +{ +public: + static QIcon findIcon(int size, const QString &name); + +protected: + QString themeName; + QStringList iconDirs; + +private: + void gnomeLookupIconTheme(); + void lookupIconTheme(); +}; diff --git a/src/utils/utils.pri b/src/utils/utils.pri new file mode 100644 index 0000000..f178afb --- /dev/null +++ b/src/utils/utils.pri @@ -0,0 +1,7 @@ +DEPENDPATH += $$PWD + +SOURCES += \ + iconprovider.cpp + +HEADERS += \ + iconprovider.h |