Skip to content

Commit 61e5286

Browse files
committed
Add variable support to themes
- Variables loaded from variables.qss file - @varname: value e.g @mycolor: red - Copy base themes to users home
1 parent 37fc244 commit 61e5286

File tree

6 files changed

+141
-30
lines changed

6 files changed

+141
-30
lines changed
990 Bytes
Loading

resources/themes/Night Mapping/style.qss

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ QToolTip
22
{
33
border: 1px solid #222;
44
background-color: #333;
5-
color: #aaa;
5+
color: @text;
66
}
77

88
QWidget
99
{
10-
color: #aaa;
11-
background-color: #323232;
10+
color: @text;
11+
background-color: @background;
1212
}
1313

1414

1515
QWidget:item:hover
1616
{
1717
background-color: #507098;
18-
color: #aaa;
18+
color: @text;
1919
}
2020

2121
QWidget:item:selected
@@ -24,7 +24,7 @@ QWidget:item:selected
2424
}
2525

2626
QMenuBar {
27-
background-color: #323232;
27+
background-color: @background;
2828
}
2929

3030
QMenuBar::item
@@ -87,7 +87,7 @@ QMenu
8787
QMenu::item
8888
{
8989
background: transparent;
90-
padding: 2px 20px 2px 20px;
90+
padding: 5px 20px 5px 20px;
9191
}
9292

9393
QMenu::item:disabled
@@ -101,13 +101,13 @@ QMenu::item:disabled
101101
QMenu::item:selected
102102
{
103103
background-color: #507098;
104-
color: #aaa;
104+
color: @text;
105105
}
106106

107107
QWidget:disabled
108108
{
109109
color: #404040;
110-
background-color: #323232;
110+
background-color: @background;
111111
}
112112

113113
QLineEdit
@@ -171,7 +171,7 @@ QComboBox:on {
171171
}
172172

173173
QComboBox:editable {
174-
background: #323232;
174+
background: @background;
175175
}
176176

177177
QComboBox QAbstractItemView {
@@ -279,7 +279,7 @@ QDockWidget::title
279279
{
280280
text-align: center;
281281
spacing: 3px; /* spacing between items in the tool bar */
282-
background-color: #323232;
282+
background-color: @background;
283283
font-weight: bold;
284284
}
285285

@@ -319,8 +319,8 @@ QMainWindow::separator:hover
319319
}
320320

321321
QToolBar {
322-
background: #323232;
323-
border: 1px solid #323232;
322+
background: @background;
323+
border: 1px solid @background;
324324
font-weight: bold;
325325
}
326326

@@ -373,7 +373,7 @@ QTabBar::tab {
373373
color: #b1b1b1;
374374
border: 1px solid #444;
375375
border-bottom-style: none;
376-
background-color: #323232;
376+
background-color: @background;
377377
padding-left: 10px;
378378
padding-right: 10px;
379379
padding-top: 3px;
@@ -396,12 +396,13 @@ QTabBar::tab:first:!selected
396396
margin-left: 0px; /* the last selected tab has nothing to overlap with on the right */
397397
}
398398

399+
QTabBar::tab:bottom {
400+
border-bottom: 1px solid rgb(190, 190, 190);
401+
}
402+
399403
QTabBar::tab:!selected
400404
{
401-
color: #b1b1b1;
402-
border-bottom-style: solid;
403-
margin-top: 3px;
404-
/*background-color: QLinearGradient(x1:0, y1:0, x2:0, y2:1, stop:1 #212121, stop:.4 #343434);*/
405+
margin-top: 5px;
405406
}
406407

407408
QTabBar::tab:selected
@@ -423,23 +424,32 @@ QTabBar::tab:!selected:hover
423424
QGroupBox::indicator:unchecked,
424425
QGroupBox::indicator:checked,
425426
QCheckBox::indicator:checked,
426-
QCheckBox::indicator:unchecked,
427+
QCheckBox::indicator:unchecked
427428
QRadioButton::indicator:checked,
428429
QRadioButton::indicator:unchecked{
429430
color: #b1b1b1;
430-
background-color: #323232;
431+
background-color: @background;
431432
border: 1px solid #b1b1b1;
432433
}
433434

434435
QGroupBox::indicator:checked,
435-
QCheckBox::indicator:checked,
436+
QCheckBox::indicator:checked
437+
{
438+
background-color: qradialgradient(
439+
cx: 0.5, cy: 0.5,
440+
fx: 0.5, fy: 0.5,
441+
radius: 1.0,
442+
stop: 0.25 #ffaa00,
443+
stop: 0.3 @background
444+
);
445+
}
436446
QRadioButton::indicator:checked {
437447
background-color: qradialgradient(
438448
cx: 0.5, cy: 0.5,
439449
fx: 0.5, fy: 0.5,
440450
radius: 1.0,
441451
stop: 0.25 #ffaa00,
442-
stop: 0.3 #323232
452+
stop: 0.3 @background
443453
);
444454
}
445455

@@ -461,7 +471,7 @@ QRadioButton::indicator:hover
461471
QAbstractItemView
462472
{
463473
background-color: #222;
464-
alternate-background-color: #323232;
474+
alternate-background-color: @background;
465475
color: silver;
466476
border: none;
467477
border-radius: 3px;
@@ -513,20 +523,28 @@ QgsLayerTreeView
513523
}
514524

515525
QgsLayerTreeView::item,
516-
QTreeView#viewCategories::item
526+
QTreeView#viewGraduated::item,
527+
QTreeView#viewCategories::item,
528+
QTreeView#viewRules::item
517529
{
518530
border-top: 0.5px solid rgba(108,108,108,50);
519531
border-bottom: 0.5px solid rgba(108,108,108,50);
520532
padding: 3px;
521533
}
522534

523535
QgsLayerTreeView::indicator:unchecked,
524-
QTreeView#viewCategories::indicator:unchecked {
536+
QTreeView#viewGraduated::indicator:unchecked,
537+
QTreeView#viewCategories::indicator:unchecked,
538+
QTreeView#viewRules::indicator:unchecked
539+
{
525540
image: url(icons/eye-blocked.svg);
526541
}
527542

528543
QgsLayerTreeView::indicator:checked,
529-
QTreeView#viewCategories::indicator:checked {
544+
QTreeView#viewGraduated::indicator:checked,
545+
QTreeView#viewCategories::indicator:checked,
546+
QTreeView#viewRules::indicator:checked
547+
{
530548
image: url(icons/eye.svg);
531549
}
532550

@@ -539,7 +557,7 @@ QHeaderView {
539557

540558
QHeaderView::section {
541559
background: transparent;
542-
background-color: #323232;
560+
background-color: @background;
543561
color: #777;
544562
border-right: 0px solid #777;
545563
border-top: 0px solid #777;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@background: #323232
2+
@text: #aaa;

src/app/qgisapp.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,9 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
552552
QMessageBox::critical( this, tr( "Private qgis.db" ), dbError );
553553
}
554554

555+
// Create the themes folder for the user
556+
QgsApplication::createThemeFolder();
557+
555558
mSplash->showMessage( tr( "Reading settings" ), Qt::AlignHCenter | Qt::AlignBottom );
556559
qApp->processEvents();
557560

src/core/qgsapplication.cpp

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,18 +437,55 @@ void QgsApplication::setUITheme( const QString &themeName )
437437
// Loop all style sheets, find matching name, load it.
438438
QHash<QString, QString> themes = QgsApplication::uiThemes();
439439
QString path = themes[themeName];
440+
QString stylesheetname = path + "/style.qss";
441+
QString autostylesheet = stylesheetname + ".auto";
442+
443+
QFile file( stylesheetname );
444+
QFile variablesfile( path + "/variables.qss" );
445+
QFile fileout( autostylesheet );
446+
447+
QFileInfo variableInfo( variablesfile );
448+
449+
if ( variableInfo.exists() && variablesfile.open( QIODevice::ReadOnly ) )
450+
{
451+
if ( !file.open( QIODevice::ReadOnly ) || !fileout.open( QIODevice::WriteOnly | QIODevice::Text ) )
452+
{
453+
return;
454+
}
455+
456+
QHash<QString, QString> variables;
457+
QString styledata = file.readAll();
458+
QTextStream in( &variablesfile );
459+
while ( !in.atEnd() )
460+
{
461+
QString line = in.readLine();
462+
// This is is a variable
463+
if ( line.startsWith( "@" ) )
464+
{
465+
int index = line.indexOf( ":" );
466+
QString name = line.mid( 0, index );
467+
QString value = line.mid( index + 1, line.length() );
468+
styledata.replace( name, value );
469+
}
470+
}
471+
variablesfile.close();
472+
QTextStream out( &fileout );
473+
out << styledata;
474+
fileout.close();
475+
file.close();
476+
stylesheetname = autostylesheet;
477+
}
478+
440479
QString styleSheet = QLatin1String( "file:///" );
441-
styleSheet.append( path + "/style.qss" );
480+
styleSheet.append( stylesheetname );
442481
qApp->setStyleSheet( styleSheet );
443482
QSettings settings;
444483
return settings.setValue( "UI/UITheme", themeName );
445484
}
446485

447486
QHash<QString, QString> QgsApplication::uiThemes()
448487
{
449-
QString themepath = ABISYM( mPkgDataPath ) + QString( "/resources/themes" );
450-
QString userthemes = qgisSettingsDirPath() + QString( "/themes" );
451-
QStringList paths = QStringList() << themepath << userthemes;
488+
QStringList paths = QStringList() << userThemesFolder();
452489
QHash<QString, QString> mapping;
453490
mapping.insert( "default", "" );
454491
foreach ( const QString& path, paths )
@@ -643,11 +680,21 @@ QString QgsApplication::userStyleV2Path()
643680
return qgisSettingsDirPath() + QString( "symbology-ng-style.db" );
644681
}
645682

683+
QString QgsApplication::userThemesFolder()
684+
{
685+
return qgisSettingsDirPath() + QString( "/themes" );
686+
}
687+
646688
QString QgsApplication::defaultStyleV2Path()
647689
{
648690
return ABISYM( mPkgDataPath ) + QString( "/resources/symbology-ng-style.db" );
649691
}
650692

693+
QString QgsApplication::defaultThemesFolder()
694+
{
695+
return ABISYM( mPkgDataPath ) + QString( "/resources/themes" );
696+
}
697+
651698
QString QgsApplication::libraryPath()
652699
{
653700
return ABISYM( mLibraryPath );
@@ -971,6 +1018,37 @@ void QgsApplication::applyGdalSkippedDrivers()
9711018
GDALAllRegister(); //to update driver list and skip missing ones
9721019
}
9731020

1021+
bool QgsApplication::createThemeFolder()
1022+
{
1023+
QString folder = userThemesFolder();
1024+
QDir myDir( folder );
1025+
if ( !myDir.exists() )
1026+
{
1027+
myDir.mkpath( folder );
1028+
}
1029+
1030+
copyPath( defaultThemesFolder(), userThemesFolder() );
1031+
}
1032+
1033+
void QgsApplication::copyPath( QString src, QString dst )
1034+
{
1035+
QDir dir( src );
1036+
if ( ! dir.exists() )
1037+
return;
1038+
1039+
foreach ( QString d, dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot ) )
1040+
{
1041+
QString dst_path = dst + QDir::separator() + d;
1042+
dir.mkpath( dst_path );
1043+
copyPath( src + QDir::separator() + d, dst_path );
1044+
}
1045+
1046+
foreach ( QString f, dir.entryList( QDir::Files ) )
1047+
{
1048+
QFile::copy( src + QDir::separator() + f, dst + QDir::separator() + f );
1049+
}
1050+
}
1051+
9741052
bool QgsApplication::createDB( QString *errorMessage )
9751053
{
9761054
// set a working directory up for gdal to write .aux.xml files into

src/core/qgsapplication.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,15 @@ class CORE_EXPORT QgsApplication : public QApplication
186186
//! Returns the path to user's style.
187187
static QString userStyleV2Path();
188188

189+
//! Returns the path to user's themes folder
190+
static QString userThemesFolder();
191+
189192
//! Returns the path to default style (works as a starting point).
190193
static QString defaultStyleV2Path();
191194

195+
//! Returns the path to default themes folder from install (works as a starting point).
196+
static QString defaultThemesFolder();
197+
192198
//! Returns the path containing qgis_core, qgis_gui, qgispython (and other) libraries
193199
static QString libraryPath();
194200

@@ -213,6 +219,9 @@ class CORE_EXPORT QgsApplication : public QApplication
213219
//! initialise qgis.db
214220
static bool createDB( QString* errorMessage = 0 );
215221

222+
//! Create the users theme folder
223+
static bool createThemeFolder( );
224+
216225
//! deletes provider registry and map layer registry
217226
static void exitQgis();
218227

@@ -320,6 +329,7 @@ class CORE_EXPORT QgsApplication : public QApplication
320329
void preNotify( QObject * receiver, QEvent * event, bool * done );
321330

322331
private:
332+
static void copyPath( QString src, QString dst );
323333
static QObject* ABISYM( mFileOpenEventReceiver );
324334
static QStringList ABISYM( mFileOpenEventList );
325335

0 commit comments

Comments
 (0)