Switch user styles on-the-fly

This commit is contained in:
Abs62 2012-12-10 18:14:13 +04:00
parent 5070b5b859
commit 8901dccbef
11 changed files with 220 additions and 61 deletions

View file

@ -21,17 +21,20 @@ using std::list;
ArticleMaker::ArticleMaker( vector< sptr< Dictionary::Class > > const & dictionaries_,
vector< Instances::Group > const & groups_,
QString const & displayStyle_ ):
QString const & displayStyle_,
QString const & addonStyle_):
dictionaries( dictionaries_ ),
groups( groups_ ),
displayStyle( displayStyle_ ),
addonStyle( addonStyle_ ),
needExpandOptionalParts( true )
{
}
void ArticleMaker::setDisplayStyle( QString const & st )
void ArticleMaker::setDisplayStyle( QString const & st, QString const & adst )
{
displayStyle = st;
addonStyle = adst;
}
std::string ArticleMaker::makeHtmlHeader( QString const & word,
@ -48,7 +51,7 @@ std::string ArticleMaker::makeHtmlHeader( QString const & word,
QFile builtInCssFile( ":/article-style.css" );
builtInCssFile.open( QFile::ReadOnly );
QByteArray css = builtInCssFile.readAll();
if ( displayStyle.size() )
{
// Load an additional stylesheet
@ -58,10 +61,19 @@ std::string ArticleMaker::makeHtmlHeader( QString const & word,
}
QFile cssFile( Config::getUserCssFileName() );
if ( cssFile.open( QFile::ReadOnly ) )
css += cssFile.readAll();
if( !addonStyle.isEmpty() )
{
QString name = Config::getStylesDir() + addonStyle
+ QDir::separator() + "article-style.css";
QFile addonCss( name );
if( addonCss.open( QFile::ReadOnly ) )
css += addonCss.readAll();
}
result += "<style type=\"text/css\" media=\"all\">\n";
result += css.data();
@ -73,7 +85,7 @@ std::string ArticleMaker::makeHtmlHeader( QString const & word,
}
// Add print-only css
{
QFile builtInCssFile( ":/article-style-print.css" );
builtInCssFile.open( QFile::ReadOnly );
@ -83,12 +95,21 @@ std::string ArticleMaker::makeHtmlHeader( QString const & word,
if ( cssFile.open( QFile::ReadOnly ) )
css += cssFile.readAll();
if( !addonStyle.isEmpty() )
{
QString name = Config::getStylesDir() + addonStyle
+ QDir::separator() + "article-style-print.css";
QFile addonCss( name );
if( addonCss.open( QFile::ReadOnly ) )
css += addonCss.readAll();
}
result += "<style type=\"text/css\" media=\"print\">\n";
result += css.data();
result += "</style>\n";
}
result += "<title>" + Html::escape( Utf8::encode( gd::toWString( word ) ) ) + "</title>";
// This doesn't seem to be much of influence right now, but we'll keep

View file

@ -20,7 +20,7 @@ class ArticleMaker: public QObject
std::vector< sptr< Dictionary::Class > > const & dictionaries;
std::vector< Instances::Group > const & groups;
QString displayStyle;
QString displayStyle, addonStyle;
bool needExpandOptionalParts;
@ -32,11 +32,12 @@ public:
/// of the inquiries, although those changes are perfectly legal.
ArticleMaker( std::vector< sptr< Dictionary::Class > > const & dictionaries,
std::vector< Instances::Group > const & groups,
QString const & displayStyle );
QString const & displayStyle,
QString const & addonStyle);
/// Sets the display style to use for any new requests. This affects the
/// choice of the stylesheet file.
void setDisplayStyle( QString const & );
void setDisplayStyle( QString const &, QString const & addonStyle );
/// Looks up the given word within the given group, and creates a full html
/// page text containing its definition.

View file

@ -371,6 +371,8 @@ Class load() throw( exError )
}
}
getStylesDir();
QFile configFile( configName );
if ( !configFile.open( QFile::ReadOnly ) )
@ -692,6 +694,9 @@ Class load() throw( exError )
if ( !preferences.namedItem( "alwaysExpandOptionalParts" ).isNull() )
c.preferences.alwaysExpandOptionalParts = preferences.namedItem( "alwaysExpandOptionalParts" ).toElement().text().toUInt() ;
if ( !preferences.namedItem( "addonStyle" ).isNull() )
c.preferences.addonStyle = preferences.namedItem( "addonStyle" ).toElement().text();
}
c.lastMainGroupId = root.namedItem( "lastMainGroupId" ).toElement().text().toUInt();
@ -1315,6 +1320,10 @@ void save( Class const & c ) throw( exError )
opt = dd.createElement( "alwaysExpandOptionalParts" );
opt.appendChild( dd.createTextNode( QString::number( c.preferences.alwaysExpandOptionalParts ) ) );
preferences.appendChild( opt );
opt = dd.createElement( "addonStyle" );
opt.appendChild( dd.createTextNode( c.preferences.addonStyle ) );
preferences.appendChild( opt );
}
{
@ -1489,4 +1498,16 @@ QString getPortableVersionMorphoDir() throw()
return QString();
}
QString getStylesDir() throw()
{
QDir result = getHomeDir();
result.mkpath( "styles" );
if ( !result.cd( "styles" ) )
return QString();
return result.path() + QDir::separator();
}
}

View file

@ -195,6 +195,8 @@ struct Preferences
unsigned storeHistory;
bool alwaysExpandOptionalParts;
QString addonStyle;
Preferences();
};
@ -501,6 +503,9 @@ QString getPortableVersionDictionaryDir() throw();
/// content/morphology in the application's directory.
QString getPortableVersionMorphoDir() throw();
/// Returns the add-on styles directory.
QString getStylesDir() throw();
}
#endif

View file

@ -205,7 +205,8 @@ HEADERS += folding.hh \
aard.hh \
mruqmenu.hh \
dictinfo.hh \
zipsounds.hh
zipsounds.hh \
stylescombobox.hh
FORMS += groups.ui \
dictgroupwidget.ui \
mainwindow.ui \
@ -301,7 +302,8 @@ SOURCES += folding.cc \
aard.cc \
mruqmenu.cc \
dictinfo.cc \
zipsounds.cc
zipsounds.cc \
stylescombobox.cc
win32 {
SOURCES += mouseover_win32/ThTypes.c \
wordbyauto.cc \

View file

@ -64,7 +64,8 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
cfg( cfg_ ),
history( History::Load(), cfg_.preferences.maxStringsInHistory ),
dictionaryBar( this, configEvents, cfg.editDictionaryCommandLine ),
articleMaker( dictionaries, groupInstances, cfg.preferences.displayStyle ),
articleMaker( dictionaries, groupInstances, cfg.preferences.displayStyle,
cfg.preferences.addonStyle ),
articleNetMgr( this, dictionaries, articleMaker,
cfg.preferences.disallowContentFromOtherSites ),
dictNetMgr( this ),
@ -72,7 +73,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
newReleaseCheckTimer( this ),
wordListSelChanged( false )
{
applyQtStyleSheet( cfg.preferences.displayStyle );
applyQtStyleSheet( cfg.preferences.displayStyle, cfg.preferences.addonStyle );
ui.setupUi( this );
@ -646,7 +647,7 @@ QPrinter & MainWindow::getPrinter()
return *printer;
}
void MainWindow::applyQtStyleSheet( QString const & displayStyle )
void MainWindow::applyQtStyleSheet( QString const & displayStyle, QString const & addonStyle )
{
QFile builtInCssFile( ":/qt-style.css" );
builtInCssFile.open( QFile::ReadOnly );
@ -666,6 +667,15 @@ void MainWindow::applyQtStyleSheet( QString const & displayStyle )
if ( cssFile.open( QFile::ReadOnly ) )
css += cssFile.readAll();
if( !addonStyle.isEmpty() )
{
QString name = Config::getStylesDir() + addonStyle
+ QDir::separator() + "qt-style.css";
QFile addonCss( name );
if( addonCss.open( QFile::ReadOnly ) )
css += addonCss.readAll();
}
setStyleSheet( css );
}
@ -1433,10 +1443,10 @@ void MainWindow::editPreferences()
bool needReload = false;
// See if we need to reapply stylesheets
if ( cfg.preferences.displayStyle != p.displayStyle )
if ( cfg.preferences.displayStyle != p.displayStyle || cfg.preferences.addonStyle != p.addonStyle )
{
applyQtStyleSheet( p.displayStyle );
articleMaker.setDisplayStyle( p.displayStyle );
applyQtStyleSheet( p.displayStyle, p.addonStyle );
articleMaker.setDisplayStyle( p.displayStyle, p.addonStyle );
needReload = true;
}

View file

@ -121,7 +121,7 @@ private:
QPrinter & getPrinter(); // Creates a printer if it's not there and returns it
/// Applies the qt's stylesheet, given the style's name.
void applyQtStyleSheet( QString const & displayStyle );
void applyQtStyleSheet( QString const & displayStyle, QString const & addonStyle );
/// Creates, destroys or otherwise updates tray icon, according to the
/// current configuration and situation.

View file

@ -198,6 +198,10 @@ Preferences::Preferences( QWidget * parent, Config::Preferences const & p ):
ui.checkForNewReleases->setChecked( p.checkForNewReleases );
ui.disallowContentFromOtherSites->setChecked( p.disallowContentFromOtherSites );
ui.enableWebPlugins->setChecked( p.enableWebPlugins );
// Add-on styles
ui.addonStylesLabel->setVisible( ui.addonStyles->count() > 1 );
ui.addonStyles->setCurrentStyle( p.addonStyle );
}
Config::Preferences Preferences::getPreferences()
@ -277,6 +281,8 @@ Config::Preferences Preferences::getPreferences()
p.disallowContentFromOtherSites = ui.disallowContentFromOtherSites->isChecked();
p.enableWebPlugins = ui.enableWebPlugins->isChecked();
p.addonStyle = ui.addonStyles->getCurrentStyle();
return p;
}

View file

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>572</width>
<height>381</height>
<height>394</height>
</rect>
</property>
<property name="windowTitle">
@ -41,19 +41,6 @@
<string>&amp;Interface</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0" colspan="2">
<spacer name="verticalSpacer_8">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox">
<property name="title">
@ -151,6 +138,16 @@ the application.</string>
</layout>
</widget>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="selectBySingleClick">
<property name="toolTip">
<string>Turn this option on if you want to select words by single mouse click</string>
</property>
<property name="text">
<string>Select word by single click</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
@ -170,7 +167,20 @@ the application.</string>
</layout>
</widget>
</item>
<item row="7" column="0" colspan="2">
<item row="0" column="0" colspan="2">
<spacer name="verticalSpacer_8">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="6" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QLabel" name="label_6">
@ -223,7 +233,7 @@ the application.</string>
</item>
</layout>
</item>
<item row="8" column="0" colspan="2">
<item row="10" column="0" colspan="2">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
@ -236,22 +246,6 @@ the application.</string>
</property>
</spacer>
</item>
<item row="6" column="0">
<spacer name="verticalSpacer_9">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>9</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="0">
<widget class="QCheckBox" name="doubleClickTranslates">
<property name="text">
@ -270,15 +264,32 @@ With this on however, it will hide the main window.</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="selectBySingleClick">
<property name="toolTip">
<string>Turn this option on if you want to select words by single mouse click</string>
</property>
<property name="text">
<string>Select word by single click</string>
</property>
</widget>
<item row="8" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="addonStylesLabel">
<property name="text">
<string>Add-on style:</string>
</property>
</widget>
</item>
<item>
<widget class="StylesComboBox" name="addonStyles"/>
</item>
<item>
<spacer name="horizontalSpacer_6">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
@ -1180,6 +1191,11 @@ It is not needed to select this option if you don't use such programs.</string>
<extends>QLineEdit</extends>
<header>hotkeyedit.hh</header>
</customwidget>
<customwidget>
<class>StylesComboBox</class>
<extends>QComboBox</extends>
<header>stylescombobox.hh</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>tabWidget</tabstop>

47
stylescombobox.cc Normal file
View file

@ -0,0 +1,47 @@
/* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "stylescombobox.hh"
#include "config.hh"
#include <QDir>
StylesComboBox::StylesComboBox( QWidget * parent ):
QComboBox( parent )
{
fill();
setVisible( count() > 1 );
}
void StylesComboBox::fill()
{
clear();
addItem( tr( "None" ) );
QString stylesDir = Config::getStylesDir();
if( !stylesDir.isEmpty() )
{
QDir dir( stylesDir );
QStringList styles = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot, QDir::LocaleAware );
addItems( styles );
}
}
void StylesComboBox::setCurrentStyle( QString const & style )
{
int nom = 0;
if( !style.isEmpty() )
{
for( int i = 1; i < count(); i++ )
if( style.compare( itemText( i ) ) == 0 )
{
nom = i;
break;
}
}
setCurrentIndex( nom );
}
QString StylesComboBox::getCurrentStyle() const
{
if( currentIndex() == 0 )
return QString();
return itemText( currentIndex() );
}

30
stylescombobox.hh Normal file
View file

@ -0,0 +1,30 @@
/* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#ifndef __STYLESCOMBOBOX_HH_INCLUDED__
#define __STYLESCOMBOBOX_HH_INCLUDED__
#include <QComboBox>
#include <QString>
/// This is a combo box which is for choosing the add-on styles
class StylesComboBox : public QComboBox
{
Q_OBJECT
public:
StylesComboBox( QWidget * parent );
/// Fills combo-box with the given groups
void fill();
/// Chooses the given style in the combobox. If there's no such style,
/// set to "None".
void setCurrentStyle( QString const & style );
/// Returns current style.
QString getCurrentStyle() const;
};
#endif // __STYLESCOMBOBOX_HH_INCLUDED__