Skip to content

Commit

Permalink
Add adjustable Qt stylesheet class, QgisAppStyleSheet
Browse files Browse the repository at this point in the history
- [API] Remove individual stylesheet option functions (e.g. setFontSize) in favor of direct access to QgisAppStyleSheet functions
  • Loading branch information
dakcarto committed Jan 21, 2013
1 parent 3f024e1 commit 65ae7d8
Show file tree
Hide file tree
Showing 13 changed files with 443 additions and 155 deletions.
32 changes: 21 additions & 11 deletions python/gui/qgisinterface.sip
Expand Up @@ -33,17 +33,6 @@ class QgisInterface : QObject
public slots: // TODO: do these functions really need to be slots?

/* Exposed functions */
/** Set the app font size
* @param fontSize point size of font
* @note added in 2.0
*/
virtual void setFontSize( int fontSize ) = 0;

/** Set the app font family
* @param fontFamily family of font (not including any style)
* @note added in 2.0
*/
virtual void setFontFamily( QString fontFamily ) = 0;

//! Zoom to full extent of map layers
virtual void zoomFull() = 0;
Expand Down Expand Up @@ -132,6 +121,27 @@ class QgisInterface : QObject
/**Return mainwindows / composer views of running composer instances (currently only one)*/
virtual QList<QgsComposerView*> activeComposers() = 0;

/** Return changeable options built from settings and/or defaults
* @note (added in 1.9)
*/
virtual QMap<QString, QVariant> defaultStyleSheetOptions() = 0;

/** Generate stylesheet
* @param opts generated default option values, or a changed copy of them
* @note added in 1.9
*/
virtual void buildStyleSheet( const QMap<QString, QVariant>& opts ) = 0;

/** Save changed default option keys/values to user settings
* @note added in 1.9
*/
virtual void saveStyleSheetOptions( const QMap<QString, QVariant>& opts ) = 0;

/** Get reference font for initial qApp (may not be same as QgisApp)
* @note added in 1.9
*/
virtual QFont defaultStyleSheetFont() = 0;

/** Add action to the plugins menu */
virtual void addPluginToMenu( QString name, QAction* action ) = 0;

Expand Down
2 changes: 2 additions & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -2,6 +2,7 @@ SET(QGIS_APP_SRCS
main.cpp
qgisapp.cpp
qgisappinterface.cpp
qgisappstylesheet.cpp
qgsabout.cpp
qgssponsors.cpp
qgsaddattrdialog.cpp
Expand Down Expand Up @@ -167,6 +168,7 @@ ENDIF (ANDROID)
SET (QGIS_APP_MOC_HDRS
qgisapp.h
qgisappinterface.h
qgisappstylesheet.h
qgsabout.h
qgsaddattrdialog.h
qgsaddjoindialog.h
Expand Down
18 changes: 3 additions & 15 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -88,7 +88,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
connect( mButtonBox, SIGNAL( rejected() ), this, SLOT( close() ) );

QSettings settings;
setAppStyleSheet();
setStyleSheet( QgisApp::instance()->styleSheet() );

int size = settings.value( "/IconSize", QGIS_ICON_SIZE ).toInt();
setIconSize( QSize( size, size ) );
Expand Down Expand Up @@ -394,18 +394,6 @@ void QgsComposer::setIconSizes( int size )
}
}

void QgsComposer::setFontSize( int fontSize )
{
//Convenience method for backwards compatibility
//Should set directly for QgisApp instead
QgisApp::instance()->setFontSize( fontSize );
}

void QgsComposer::setAppStyleSheet()
{
setStyleSheet( QgisApp::instance()->styleSheet() );
}

void QgsComposer::connectSlots()
{
connect( mView, SIGNAL( selectedItemChanged( QgsComposerItem* ) ), this, SLOT( showItemOptions( QgsComposerItem* ) ) );
Expand Down Expand Up @@ -705,8 +693,8 @@ void QgsComposer::on_mActionExportAsPDF_triggered()
{
outputFileName = QDir( outputDir ).filePath( atlasMap->currentFilename() ) + ".pdf";
mComposition->beginPrintAsPDF( printer, outputFileName );
// set the correct resolution
mComposition->beginPrint( printer );
// set the correct resolution
mComposition->beginPrint( printer );
painter.begin( &printer );
mComposition->doPrint( printer, painter );
painter.end();
Expand Down
4 changes: 0 additions & 4 deletions src/app/composer/qgscomposer.h
Expand Up @@ -60,10 +60,6 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
void setupTheme();

void setIconSizes( int size );
void setFontSize( int size );
//! Set app stylesheet from main app
//! @note added in 2.0
void setAppStyleSheet();

//! Open and show, set defaults if first time
void open();
Expand Down
54 changes: 15 additions & 39 deletions src/app/qgisapp.cpp
Expand Up @@ -97,6 +97,7 @@

#include "qgisapp.h"
#include "qgisappinterface.h"
#include "qgisappstylesheet.h"
#include "qgis.h"
#include "qgisplugin.h"
#include "qgsabout.h"
Expand Down Expand Up @@ -464,7 +465,12 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
qApp->processEvents();

QSettings settings;
setAppStyleSheet();

// set up stylesheet builder and apply saved or default style options
mStyleSheetBuilder = new QgisAppStyleSheet( this );
connect( mStyleSheetBuilder, SIGNAL( appStyleSheetChanged( const QString& ) ),
this, SLOT( setAppStyleSheet( const QString& ) ) );
mStyleSheetBuilder->buildStyleSheet( mStyleSheetBuilder->defaultOptions() );

QWidget *centralWidget = this->centralWidget();
QGridLayout *centralLayout = new QGridLayout( centralWidget );
Expand Down Expand Up @@ -717,6 +723,7 @@ QgisApp::~QgisApp()
{
delete mInternalClipboard;
delete mQgisInterface;
delete mStyleSheetBuilder;

delete mMapTools.mZoomIn;
delete mMapTools.mZoomOut;
Expand Down Expand Up @@ -838,6 +845,11 @@ bool QgisApp::event( QEvent * event )
return done;
}

QgisAppStyleSheet* QgisApp::styleSheetBuilder()
{
Q_ASSERT( mStyleSheetBuilder );
return mStyleSheetBuilder;
}

// restore any application settings stored in QSettings
void QgisApp::readSettings()
Expand Down Expand Up @@ -1175,50 +1187,14 @@ void QgisApp::createActionGroups()
mMapToolGroup->addAction( mActionChangeLabelProperties );
}

void QgisApp::setFontSize( int fontSize )
{
if ( fontSize < 4 || 99 < fontSize ) // defaults for Options spinbox
{
return;
}
QSettings settings;
settings.setValue( "/fontPointSize", fontSize );
setAppStyleSheet();
}

void QgisApp::setFontFamily( const QString& fontFamily )
{
QSettings settings;
settings.setValue( "/fontFamily", fontFamily );
setAppStyleSheet();
}

void QgisApp::setAppStyleSheet()
void QgisApp::setAppStyleSheet( const QString& stylesheet )
{
QSettings settings;
int fontSize = settings.value( "/fontPointSize", QGIS_DEFAULT_FONTSIZE ).toInt();

QString fontFamily = settings.value( "/fontFamily", QVariant( "QtDefault" ) ).toString();

QString family = QString( "" ); // use default Qt font family
if ( fontFamily != "QtDefault" )
{
QFont *tempFont = new QFont( fontFamily );
// is exact family match returned from system?
if ( tempFont->family() == fontFamily )
{
family = QString( " \"%1\";" ).arg( fontFamily );
}
delete tempFont;
}

QString stylesheet = QString( "font: %1pt%2\n" ).arg( fontSize ).arg( family );
setStyleSheet( stylesheet );

// cascade styles to any current project composers
foreach ( QgsComposer *c, mPrintComposers )
{
c->setAppStyleSheet();
c->setStyleSheet( stylesheet );
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/app/qgisapp.h
Expand Up @@ -37,6 +37,7 @@ class QTcpSocket;
class QValidator;

class QgisAppInterface;
class QgisAppStyleSheet;
class QgsAnnotationItem;
class QgsClipboard;
class QgsComposer;
Expand Down Expand Up @@ -175,13 +176,10 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
void setTheme( QString themeName = "default" );

void setIconSizes( int size );
void setFontSize( int fontSize );
//! Set app font family
//! @note added in 2.0
void setFontFamily( const QString& fontFamily );
//! Set app stylesheet from settings
//! @note added in 2.0
void setAppStyleSheet();

//! Get stylesheet builder object for app and print composers
//! @note added in 1.9
QgisAppStyleSheet* styleSheetBuilder();

//! Setup the toolbar popup menus for a given theme
void setupToolbarPopups( QString themeName );
Expand Down Expand Up @@ -536,6 +534,10 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
//! project was read
void readProject( const QDomDocument & );

//! Set app stylesheet from settings
//! @note added in 1.9
void setAppStyleSheet( const QString& stylesheet );

//! request credentials for network manager
void namAuthenticationRequired( QNetworkReply *reply, QAuthenticator *auth );
void namProxyAuthenticationRequired( const QNetworkProxy &proxy, QAuthenticator *auth );
Expand Down Expand Up @@ -1106,6 +1108,8 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
/**Do histogram stretch for singleband gray / multiband color rasters*/
void histogramStretch( bool visibleAreaOnly = false, QgsRasterLayer::ContrastEnhancementLimits theLimits = QgsRasterLayer::ContrastEnhancementMinMax );

QgisAppStyleSheet* mStyleSheetBuilder;

// actions for menus and toolbars -----------------

#ifdef Q_WS_MAC
Expand Down Expand Up @@ -1353,11 +1357,8 @@ class QgisApp : public QMainWindow, private Ui::MainWindow

#ifdef ANDROID
#define QGIS_ICON_SIZE 32
//TODO find a better default fontsize maybe using DPI detection or so
#define QGIS_DEFAULT_FONTSIZE 8
#else
#define QGIS_ICON_SIZE 24
#define QGIS_DEFAULT_FONTSIZE qApp->font().pointSize()
#endif

#endif
31 changes: 21 additions & 10 deletions src/app/qgisappinterface.cpp
Expand Up @@ -23,6 +23,7 @@
#include <QAbstractButton>

#include "qgisappinterface.h"
#include "qgisappstylesheet.h"
#include "qgisapp.h"
#include "qgscomposer.h"
#include "qgsmaplayer.h"
Expand Down Expand Up @@ -67,16 +68,6 @@ QgsLegendInterface* QgisAppInterface::legendInterface()
return &legendIface;
}

void QgisAppInterface::setFontSize( int fontSize )
{
qgis->setFontSize( fontSize );
}

void QgisAppInterface::setFontFamily( QString fontFamily )
{
qgis->setFontFamily( fontFamily );
}

void QgisAppInterface::zoomFull()
{
qgis->zoomFull();
Expand Down Expand Up @@ -299,6 +290,26 @@ QList<QgsComposerView*> QgisAppInterface::activeComposers()
return composerViewList;
}

QMap<QString, QVariant> QgisAppInterface::defaultStyleSheetOptions()
{
return qgis->styleSheetBuilder()->defaultOptions();
}

void QgisAppInterface::buildStyleSheet( const QMap<QString, QVariant>& opts )
{
qgis->styleSheetBuilder()->buildStyleSheet( opts );
}

void QgisAppInterface::saveStyleSheetOptions( const QMap<QString, QVariant>& opts )
{
qgis->styleSheetBuilder()->saveToSettings( opts );
}

QFont QgisAppInterface::defaultStyleSheetFont()
{
return qgis->styleSheetBuilder()->defaultFont();
}

void QgisAppInterface::addDockWidget( Qt::DockWidgetArea area, QDockWidget * dockwidget )
{
qgis->addDockWidget( area, dockwidget );
Expand Down
32 changes: 21 additions & 11 deletions src/app/qgisappinterface.h
Expand Up @@ -50,17 +50,6 @@ class QgisAppInterface : public QgisInterface
QgsLegendInterface* legendInterface();

/* Exposed functions */
/** Set the app font size
* @param fontSize point size of font
* @note added in 2.0
*/
void setFontSize( int fontSize );

/** Set the app font family
* @param fontFamily family of font (not including any style)
* @note added in 2.0
*/
void setFontFamily( QString fontFamily );

//! Zoom map to full extent
void zoomFull();
Expand Down Expand Up @@ -136,6 +125,27 @@ class QgisAppInterface : public QgisInterface

QList<QgsComposerView*> activeComposers();

/** Return changeable options built from settings and/or defaults
* @note (added in 1.9)
*/
QMap<QString, QVariant> defaultStyleSheetOptions();

/** Generate stylesheet
* @param opts generated default option values, or a changed copy of them
* @note added in 1.9
*/
void buildStyleSheet( const QMap<QString, QVariant>& opts );

/** Save changed default option keys/values to user settings
* @note added in 1.9
*/
void saveStyleSheetOptions( const QMap<QString, QVariant>& opts );

/** Get reference font for initial qApp (may not be same as QgisApp)
* @note added in 1.9
*/
QFont defaultStyleSheetFont();

/** Add action to the plugins menu */
void addPluginToMenu( QString name, QAction* action );
/** Remove action from the plugins menu */
Expand Down

0 comments on commit 65ae7d8

Please sign in to comment.