From 98d308dfcfcf366a884ddccea7a2c3e14fedfbd4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 2 Jun 2012 14:13:08 +0530 Subject: [PATCH] Infrastructure for dynamically loading Qt style plugins from python --- .../progress_indicator/QProgressIndicator.cpp | 23 +++++++++++++++++++ .../progress_indicator/QProgressIndicator.h | 9 ++++++++ .../progress_indicator/QProgressIndicator.sip | 7 ++++++ 3 files changed, 39 insertions(+) diff --git a/src/calibre/gui2/progress_indicator/QProgressIndicator.cpp b/src/calibre/gui2/progress_indicator/QProgressIndicator.cpp index 24d69bc164..64fd346674 100644 --- a/src/calibre/gui2/progress_indicator/QProgressIndicator.cpp +++ b/src/calibre/gui2/progress_indicator/QProgressIndicator.cpp @@ -1,6 +1,10 @@ #include "QProgressIndicator.h" #include +#include +#include +#include +#include QProgressIndicator::QProgressIndicator(QWidget* parent, int size) : QWidget(parent), @@ -122,3 +126,22 @@ void QProgressIndicator::paintEvent(QPaintEvent * /*event*/) p.restore(); } } + +int load_style(QString &path, QString &name) { + int ret = 0; + QStyle *s; + QPluginLoader pl(path); + QObject *o = pl.instance(); + if (o != 0) { + QStylePlugin *sp = qobject_cast(o); + if (sp != 0) { + s = sp->create(name); + if (s != 0) { + s->setObjectName(name); + QApplication::setStyle(s); + ret = 1; + } + } + } + return ret; +} diff --git a/src/calibre/gui2/progress_indicator/QProgressIndicator.h b/src/calibre/gui2/progress_indicator/QProgressIndicator.h index c2098ffe64..0fd82a99f5 100644 --- a/src/calibre/gui2/progress_indicator/QProgressIndicator.h +++ b/src/calibre/gui2/progress_indicator/QProgressIndicator.h @@ -91,3 +91,12 @@ private: QColor m_color; }; +/* Utility function that can be used to load a QStyle from a Qt plugin. This is + * here so that there is no need to create a separate PyQt plugin just for this + * simple functionality. + * \param path The full path to the DLL containing the plugin + * \param name The name of the style plugin to load + * \return 1 if succeeds 0 otherwise. The objectName of the loaded style is set to name + */ +int load_style(QString &path, QString &name); + diff --git a/src/calibre/gui2/progress_indicator/QProgressIndicator.sip b/src/calibre/gui2/progress_indicator/QProgressIndicator.sip index 3db47d668a..03c6dacdd6 100644 --- a/src/calibre/gui2/progress_indicator/QProgressIndicator.sip +++ b/src/calibre/gui2/progress_indicator/QProgressIndicator.sip @@ -6,6 +6,10 @@ %Import QtCore/QtCoremod.sip %Import QtGui/QtGuimod.sip +%ModuleHeaderCode +int load_style(QString &path, QString &name); +%End + class QProgressIndicator : QWidget { %TypeHeaderCode @@ -50,3 +54,6 @@ protected: virtual void paintEvent(QPaintEvent * event); }; + +int load_style(QString &path, QString &name); +