From c3fcd3b116b1d990fb30e855287d007977a62073 Mon Sep 17 00:00:00 2001 From: marian Date: Mon, 6 Feb 2012 13:40:47 +0000 Subject: [PATCH] Optionally load application stylesheet from file. git-svn-id: http://yate.null.ro/svn/yate/trunk@4837 acf43c95-373e-0410-b603-e72c3f656dc1 --- clients/qt4/qt4client.cpp | 86 +++++++++++++++++++++++++++--------- clients/qt4/qt4client.h | 8 ++++ conf.d/yate-qt4.conf.default | 1 + 3 files changed, 74 insertions(+), 21 deletions(-) diff --git a/clients/qt4/qt4client.cpp b/clients/qt4/qt4client.cpp index 764fc735..636771a4 100644 --- a/clients/qt4/qt4client.cpp +++ b/clients/qt4/qt4client.cpp @@ -641,6 +641,33 @@ static bool createProperty(QObject* obj, const char* name, QVariant::Type t, return true; } +// Replace file path in URLs in a character array +static void addFilePathUrl(QByteArray& a, const String& file) +{ + if (!file) + return; + QString path = QDir::fromNativeSeparators(QtClient::setUtf8(file)); + // Truncate after last path separator (lastIndexOf() returns -1 if not found) + path.truncate(path.lastIndexOf(QString("/")) + 1); + if (!path.size()) + return; + int start = 0; + int end = -1; + while ((start = a.indexOf("url(",end + 1)) > 0) { + start += 4; + end = a.indexOf(")",start); + if (end <= start) + break; + // Add + int len = end - start; + QByteArray tmp = a.mid(start,len); + if (tmp.indexOf('/') != -1) + continue; + tmp.insert(0,path); + a.replace(start,len,tmp); + } +} + /** * Qt4ClientFactory @@ -813,28 +840,8 @@ UIBuffer* UIBuffer::build(const String& name) delete qArray; return 0; } - // Add URLs path when missing - QString path = QDir::fromNativeSeparators(QtClient::setUtf8(name)); - // Truncate after last path separator (lastIndexOf() returns -1 if not found) - path.truncate(path.lastIndexOf(QString("/")) + 1); - if (path.size()) { - int start = 0; - int end = -1; - while ((start = qArray->indexOf("url(",end + 1)) > 0) { - start += 4; - end = qArray->indexOf(")",start); - if (end <= start) - break; - // Add - int len = end - start; - QByteArray tmp = qArray->mid(start,len); - if (tmp.indexOf('/') != -1) - continue; - tmp.insert(0,path); - qArray->replace(start,len,tmp); - } - } + addFilePathUrl(*qArray,name); return new UIBuffer(name,qArray); } @@ -3220,6 +3227,7 @@ void QtClient::run() char* argv = 0; m_app = new QApplication(argc,&argv); m_app->setQuitOnLastWindowClosed(false); + setAppStyleSheet(Engine::config().getValue("client","stylesheet_file","stylesheet")); String imgRead; QList imgs = QImageReader::supportedImageFormats(); for (int i = 0; i < imgs.size(); i++) @@ -4077,6 +4085,42 @@ bool QtClient::getPixmapFromCache(QPixmap& pixmap, const QString& file) return true; } +// Set application style sheet from file. +bool QtClient::setAppStyleSheet(const String& file) +{ + if (!qApp) { + Debug(ClientDriver::self(),DebugNote,"Set application stylesheet called without app"); + return false; + } + String shf; + QString sh; + const char* oper = 0; + if (file) { + shf = file; + if (shf.find('/') < 0 && shf.find('\\') < 0) + shf = Client::s_skinPath + shf; + QFile f(setUtf8(shf)); + if (f.open(QIODevice::ReadOnly)) { + QByteArray a = f.readAll(); + if (a.size()) { + addFilePathUrl(a,shf); + sh = QString::fromUtf8(a.constData()); + } + else if (f.error() != QFile::NoError) + oper = "read"; + } + else + oper = "open"; + if (oper) + Debug(ClientDriver::self(),DebugWarn, + "Failed to %s stylesheet file '%s': %d '%s'", + oper,shf.c_str(),f.error(),f.errorString().toUtf8().constData()); + } + if (!oper) + qApp->setStyleSheet(sh); + return oper == 0; +} + /** * QtDriver diff --git a/clients/qt4/qt4client.h b/clients/qt4/qt4client.h index 5a372eb5..a527bb81 100644 --- a/clients/qt4/qt4client.h +++ b/clients/qt4/qt4client.h @@ -523,6 +523,14 @@ public: */ static bool getPixmapFromCache(QPixmap& pixmap, const QString& file); + /** + * Set application style sheet from file. + * Use skin path if there is no path separator in file + * @param file File containing application style sheet (UTF8 text) + * @return True on success + */ + static bool setAppStyleSheet(const String& file); + protected: virtual void loadWindows(const char* file = 0); private: diff --git a/conf.d/yate-qt4.conf.default b/conf.d/yate-qt4.conf.default index 98f4c810..6dd7cb56 100644 --- a/conf.d/yate-qt4.conf.default +++ b/conf.d/yate-qt4.conf.default @@ -7,5 +7,6 @@ h323chan.yate=yes [client] ;skin=default ;style= +;stylesheet_file= ;device= ;greeting=Yate ${version} - ${release}