diff --git a/config.cc b/config.cc index 0dd18f01..48ca3103 100644 --- a/config.cc +++ b/config.cc @@ -219,6 +219,7 @@ Preferences::Preferences(): selectWordBySingleClick( false ), autoScrollToTargetArticle( true ), escKeyHidesMainWindow( false ), + darkMode( false ), alwaysOnTop ( false ), searchInDock ( false ), @@ -869,6 +870,9 @@ Class load() if ( !preferences.namedItem( "escKeyHidesMainWindow" ).isNull() ) c.preferences.escKeyHidesMainWindow = ( preferences.namedItem( "escKeyHidesMainWindow" ).toElement().text() == "1" ); + if ( !preferences.namedItem( "darkMode" ).isNull() ) + c.preferences.darkMode = ( preferences.namedItem( "darkMode" ).toElement().text() == "1" ); + if ( !preferences.namedItem( "zoomFactor" ).isNull() ) c.preferences.zoomFactor = preferences.namedItem( "zoomFactor" ).toElement().text().toDouble(); @@ -1711,6 +1715,10 @@ void save( Class const & c ) opt.appendChild( dd.createTextNode( c.preferences.escKeyHidesMainWindow ? "1":"0" ) ); preferences.appendChild( opt ); + opt = dd.createElement( "darkMode" ); + opt.appendChild( dd.createTextNode( c.preferences.darkMode ? "1":"0" ) ); + preferences.appendChild( opt ); + opt = dd.createElement( "zoomFactor" ); opt.appendChild( dd.createTextNode( QString::number( c.preferences.zoomFactor ) ) ); preferences.appendChild( opt ); diff --git a/config.hh b/config.hh index 402723c9..b2ce2d7c 100644 --- a/config.hh +++ b/config.hh @@ -307,6 +307,7 @@ struct Preferences bool selectWordBySingleClick; bool autoScrollToTargetArticle; bool escKeyHidesMainWindow; + bool darkMode; bool alwaysOnTop; /// An old UI mode when tranlateLine and wordList diff --git a/mainwindow.cc b/mainwindow.cc index 743af1a0..6511c672 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -834,7 +834,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ): translateLine->setFocus(); - applyQtStyleSheet( cfg.preferences.displayStyle, cfg.preferences.addonStyle ); + applyQtStyleSheet( cfg.preferences.displayStyle, cfg.preferences.addonStyle, cfg.preferences.darkMode ); makeScanPopup(); @@ -1175,8 +1175,45 @@ QPrinter & MainWindow::getPrinter() return *printer; } -void MainWindow::applyQtStyleSheet( QString const & displayStyle, QString const & addonStyle ) +void MainWindow::applyQtStyleSheet( QString const & displayStyle, QString const & addonStyle, bool const & darkMode ) { + if( darkMode ) + { + //https://forum.qt.io/topic/101391/windows-10-dark-theme + #ifdef Q_OS_WIN32 + qApp->setStyle( QStyleFactory::create( "Fusion" ) ); + #endif + QPalette darkPalette; + QColor darkColor = QColor( 45, 45, 45 ); + QColor disabledColor = QColor( 127, 127, 127 ); + darkPalette.setColor( QPalette::Window, darkColor ); + darkPalette.setColor( QPalette::WindowText, Qt::white ); + darkPalette.setColor( QPalette::Base, QColor( 18, 18, 18 ) ); + darkPalette.setColor( QPalette::AlternateBase, darkColor ); + darkPalette.setColor( QPalette::ToolTipBase, Qt::white ); + darkPalette.setColor( QPalette::ToolTipText, Qt::white ); + darkPalette.setColor( QPalette::Text, Qt::white ); + darkPalette.setColor( QPalette::Disabled, QPalette::Text, disabledColor ); + darkPalette.setColor( QPalette::Button, darkColor ); + darkPalette.setColor( QPalette::ButtonText, Qt::white ); + darkPalette.setColor( QPalette::Disabled, QPalette::ButtonText, disabledColor ); + darkPalette.setColor( QPalette::BrightText, Qt::red ); + darkPalette.setColor( QPalette::Link, QColor( 42, 130, 218 ) ); + + darkPalette.setColor( QPalette::Highlight, QColor( 42, 130, 218 ) ); + darkPalette.setColor( QPalette::HighlightedText, Qt::black ); + darkPalette.setColor( QPalette::Disabled, QPalette::HighlightedText, disabledColor ); + + qApp->setPalette( darkPalette ); + } + else + { + #ifdef Q_OS_WIN32 + qApp->setStyle( QStyleFactory::create( "Windows" ) ); + #endif + qApp->setPalette( QPalette() ); + } + QFile builtInCssFile( ":/qt-style.css" ); builtInCssFile.open( QFile::ReadOnly ); QByteArray css = builtInCssFile.readAll(); @@ -1210,6 +1247,10 @@ void MainWindow::applyQtStyleSheet( QString const & displayStyle, QString const css += addonCss.readAll(); } + if(darkMode){ + css += "QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }"; + } + setStyleSheet( css ); } @@ -2237,9 +2278,9 @@ void MainWindow::editPreferences() bool needReload = false; // See if we need to reapply stylesheets - if ( cfg.preferences.displayStyle != p.displayStyle || cfg.preferences.addonStyle != p.addonStyle ) + if ( cfg.preferences.displayStyle != p.displayStyle || cfg.preferences.addonStyle != p.addonStyle || cfg.preferences.darkMode != p.darkMode) { - applyQtStyleSheet( p.displayStyle, p.addonStyle ); + applyQtStyleSheet( p.displayStyle, p.addonStyle, p.darkMode ); articleMaker.setDisplayStyle( p.displayStyle, p.addonStyle ); needReload = true; } diff --git a/mainwindow.hh b/mainwindow.hh index b57c1ba1..346e0c5b 100644 --- a/mainwindow.hh +++ b/mainwindow.hh @@ -184,7 +184,7 @@ private: ResourceSchemeHandler * resourceSchemeHandler; /// Applies the qt's stylesheet, given the style's name. - void applyQtStyleSheet( QString const & displayStyle, QString const & addonStyle ); + void applyQtStyleSheet( QString const & displayStyle, QString const & addonStyle, bool const & darkMode ); /// Creates, destroys or otherwise updates tray icon, according to the /// current configuration and situation. diff --git a/preferences.cc b/preferences.cc index 96d693d7..f08380b5 100644 --- a/preferences.cc +++ b/preferences.cc @@ -181,6 +181,7 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ): ui.selectBySingleClick->setChecked( p.selectWordBySingleClick); ui.autoScrollToTargetArticle->setChecked( p.autoScrollToTargetArticle ); ui.escKeyHidesMainWindow->setChecked( p.escKeyHidesMainWindow ); + ui.darkMode->setChecked(p.darkMode); ui.enableMainWindowHotkey->setChecked( p.enableMainWindowHotkey ); ui.mainWindowHotkey->setHotKey( p.mainWindowHotkey ); @@ -400,6 +401,7 @@ Config::Preferences Preferences::getPreferences() p.autoScrollToTargetArticle = ui.autoScrollToTargetArticle->isChecked(); p.escKeyHidesMainWindow = ui.escKeyHidesMainWindow->isChecked(); + p.darkMode = ui.darkMode->isChecked(); p.enableMainWindowHotkey = ui.enableMainWindowHotkey->isChecked(); p.mainWindowHotkey = ui.mainWindowHotkey->getHotKey(); p.enableClipboardHotkey = ui.enableClipboardHotkey->isChecked(); diff --git a/preferences.ui b/preferences.ui index a9e9c159..f3c0403b 100644 --- a/preferences.ui +++ b/preferences.ui @@ -412,6 +412,13 @@ be the last ones. + + + + dark mode + + +