From ad59d1dc5e2aeee1b20a46dfa7478d146c0785b3 Mon Sep 17 00:00:00 2001 From: Igor Date: Fri, 20 May 2016 16:55:25 +0300 Subject: [PATCH] Allow app to add custom color sheme locations --- lib/ColorScheme.cpp | 60 +++++++++++++++++++++++++-------------------- lib/ColorScheme.h | 8 ++++++ lib/qtermwidget.cpp | 5 ++++ lib/qtermwidget.h | 1 + lib/tools.cpp | 34 ++++++++++++++++++++----- lib/tools.h | 5 ++-- 6 files changed, 78 insertions(+), 35 deletions(-) diff --git a/lib/ColorScheme.cpp b/lib/ColorScheme.cpp index f9aa921..1633ece 100644 --- a/lib/ColorScheme.cpp +++ b/lib/ColorScheme.cpp @@ -558,17 +558,13 @@ ColorSchemeManager::~ColorSchemeManager() void ColorSchemeManager::loadAllColorSchemes() { qDebug() << "loadAllColorSchemes"; - int success = 0; int failed = 0; QList nativeColorSchemes = listColorSchemes(); - QListIterator nativeIter(nativeColorSchemes); while ( nativeIter.hasNext() ) { - if ( loadColorScheme( nativeIter.next() ) ) - success++; - else + if ( !loadColorScheme( nativeIter.next() ) ) failed++; } @@ -576,9 +572,7 @@ void ColorSchemeManager::loadAllColorSchemes() QListIterator kde3Iter(kde3ColorSchemes); while ( kde3Iter.hasNext() ) { - if ( loadKDE3ColorScheme( kde3Iter.next() ) ) - success++; - else + if ( !loadKDE3ColorScheme( kde3Iter.next() ) ) failed++; } @@ -650,6 +644,11 @@ bool ColorSchemeManager::loadCustomColorScheme(const QString& path) return false; } +void ColorSchemeManager::addCustomColorSchemeDir(const QString& custom_dir) +{ + add_custom_color_scheme_dir(custom_dir); +} + bool ColorSchemeManager::loadColorScheme(const QString& filePath) { if ( !filePath.endsWith(QLatin1String(".colorscheme")) || !QFile::exists(filePath) ) @@ -686,15 +685,18 @@ bool ColorSchemeManager::loadColorScheme(const QString& filePath) } QList ColorSchemeManager::listKDE3ColorSchemes() { - QString dname(get_color_schemes_dir()); - QDir dir(dname); - QStringList filters; - filters << "*.schema"; - dir.setNameFilters(filters); - QStringList list = dir.entryList(filters); QStringList ret; - foreach(QString i, list) - ret << dname + "/" + i; + foreach(const QString &scheme_dir, get_color_schemes_dirs()) + { + QString dname(scheme_dir); + QDir dir(dname); + QStringList filters; + filters << "*.schema"; + dir.setNameFilters(filters); + QStringList list = dir.entryList(filters); + foreach(QString i, list) + ret << dname + "/" + i; + } return ret; //return KGlobal::dirs()->findAllResources("data", // "konsole/*.schema", @@ -703,15 +705,18 @@ QList ColorSchemeManager::listKDE3ColorSchemes() } QList ColorSchemeManager::listColorSchemes() { - QString dname(get_color_schemes_dir()); - QDir dir(dname); - QStringList filters; - filters << "*.colorscheme"; - dir.setNameFilters(filters); - QStringList list = dir.entryList(filters); QStringList ret; - foreach(QString i, list) - ret << dname + "/" + i; + foreach(const QString &scheme_dir, get_color_schemes_dirs()) + { + QString dname(scheme_dir); + QDir dir(dname); + QStringList filters; + filters << "*.colorscheme"; + dir.setNameFilters(filters); + QStringList list = dir.entryList(filters); + foreach(QString i, list) + ret << dname + "/" + i; + } return ret; // return KGlobal::dirs()->findAllResources("data", // "konsole/*.colorscheme", @@ -742,12 +747,13 @@ bool ColorSchemeManager::deleteColorScheme(const QString& name) QString ColorSchemeManager::findColorSchemePath(const QString& name) const { // QString path = KStandardDirs::locate("data","konsole/"+name+".colorscheme"); - QString path(get_color_schemes_dir() + "/"+ name + ".colorscheme"); + const QString dir = get_color_schemes_dirs().first(); + QString path(dir + "/"+ name + ".colorscheme"); if ( !path.isEmpty() ) - return path; + return path; //path = KStandardDirs::locate("data","konsole/"+name+".schema"); - path = get_color_schemes_dir() + "/"+ name + ".schema"; + path = dir + "/"+ name + ".schema"; return path; } diff --git a/lib/ColorScheme.h b/lib/ColorScheme.h index e951b5e..f9e619b 100644 --- a/lib/ColorScheme.h +++ b/lib/ColorScheme.h @@ -327,6 +327,14 @@ public: * @return Whether the color scheme is loaded successfully. */ bool loadCustomColorScheme(const QString& path); + + /** + * @brief Allows to add a custom location of color schemes. + * + * @param[in] custom_dir Custom location of color schemes (must end with /). + */ + void addCustomColorSchemeDir(const QString& custom_dir); + private: // loads a color scheme from a KDE 4+ .colorscheme file bool loadColorScheme(const QString& path); diff --git a/lib/qtermwidget.cpp b/lib/qtermwidget.cpp index 8084644..cbdf968 100644 --- a/lib/qtermwidget.cpp +++ b/lib/qtermwidget.cpp @@ -433,6 +433,11 @@ QStringList QTermWidget::availableColorSchemes() return ret; } +void QTermWidget::addCustomColorSchemeDir(const QString& custom_dir) +{ + ColorSchemeManager::instance()->addCustomColorSchemeDir(custom_dir); +} + void QTermWidget::setSize(const QSize &size) { if (!m_impl->m_terminalDisplay) diff --git a/lib/qtermwidget.h b/lib/qtermwidget.h index 90a41b3..613175f 100644 --- a/lib/qtermwidget.h +++ b/lib/qtermwidget.h @@ -120,6 +120,7 @@ public: */ void setColorScheme(const QString & name); static QStringList availableColorSchemes(); + void addCustomColorSchemeDir(const QString& custom_dir); // History size for scrolling void setHistorySize(int lines); //infinite if lines < 0 diff --git a/lib/tools.cpp b/lib/tools.cpp index 487495d..0bec7f1 100644 --- a/lib/tools.cpp +++ b/lib/tools.cpp @@ -43,36 +43,58 @@ QString get_kb_layout_dir() #endif // BUNDLE_KEYBOARDLAYOUTS } -/*! Helper function to get possible location of layout files. +/*! Helper function to add custom location of color schemes. +*/ +QStringList custom_color_schemes_dirs; +void add_custom_color_scheme_dir(const QString& custom_dir) +{ + custom_color_schemes_dirs << custom_dir; +} + +/*! Helper function to get possible locations of color schemes. By default the COLORSCHEMES_DIR is used (linux/BSD/macports). But in some cases (apple bundle) there can be more locations). */ -QString get_color_schemes_dir() +QStringList get_color_schemes_dirs() { #ifdef BUNDLE_COLORSCHEMES return QLatin1String(":/"); #else // qDebug() << __FILE__ << __FUNCTION__; - QString rval = ""; + QStringList rval; QString k(COLORSCHEMES_DIR); QDir d(k); // qDebug() << "default COLORSCHEMES_DIR: " << k; if (d.exists()) - rval = k.append("/"); + rval << k.append("/"); // subdir in the app location d.setPath(QCoreApplication::applicationDirPath() + "/color-schemes/"); //qDebug() << d.path(); if (d.exists()) - rval = QCoreApplication::applicationDirPath() + "/color-schemes/"; + { + if (!rval.isEmpty()) + rval.clear(); + rval << (QCoreApplication::applicationDirPath() + "/color-schemes/"); + } #ifdef Q_WS_MAC d.setPath(QCoreApplication::applicationDirPath() + "/../Resources/color-schemes/"); if (d.exists()) - rval = QCoreApplication::applicationDirPath() + "/../Resources/color-schemes/"; + { + if (!rval.isEmpty()) + rval.clear(); + rval << (QCoreApplication::applicationDirPath() + "/../Resources/color-schemes/"); + } #endif + foreach (const QString& custom_dir, custom_color_schemes_dirs) + { + d.setPath(custom_dir); + if (d.exists()) + rval << custom_dir; + } #ifdef QT_DEBUG if(!rval.isEmpty()) { qDebug() << "Using color-schemes: " << rval; diff --git a/lib/tools.h b/lib/tools.h index b24d88f..849e3a8 100644 --- a/lib/tools.h +++ b/lib/tools.h @@ -2,9 +2,10 @@ #define TOOLS_H #include +#include QString get_kb_layout_dir(); -QString get_color_schemes_dir(); - +void add_custom_color_scheme_dir(const QString& custom_dir); +QStringList get_color_schemes_dirs(); #endif