libyui-qt  2.47.1.1
QY2Styler.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: QY2Styler.h
20 
21  Author: Stefan Kulow <coolo@suse.de>
22 
23 /-*/
24 
25 
26 #ifndef QY2Styler_h
27 #define QY2Styler_h
28 
29 #include <QObject>
30 #include <QHash>
31 #include <QString>
32 #include <QImage>
33 #include <QMap>
34 
35 #define HIGH_CONTRAST_STYLE_SHEET "highcontrast.qss"
36 #define DEFAULT_STYLE_SHEET "style.qss"
37 
38 class QY2Styler : public QObject
39 {
40  Q_OBJECT
41 
42 protected:
43 
44  /**
45  * Constructor. Use the static styler() function instead to return the
46  * singleton for this class.
47  **/
48  QY2Styler( QObject * parent,
49  const QString & defaultStyleSheet = "",
50  const QString & alternateStyleSheet = "" );
51 
52 public:
53 
54  static QY2Styler * styler();
55 
56  /**
57  * Determines if an style sheet exists.
58  *
59  * \param file Filename. It should live in the themeDir() directory.
60  * \return true if the file was found; false otherwise.
61  */
62  bool styleSheetExists( const QString & file );
63 
64  /**
65  * Loads and apply a style sheet from a file.
66  *
67  * \param file Filename. It should live in the themeDir() directory.
68  * \return true if the file was found (and applied); false otherwise.
69  */
70  bool loadStyleSheet( const QString &file );
71 
72  /**
73  * Applies a style sheet from a string.
74  *
75  * \param text Style sheet content.
76  */
77  void setStyleSheet( const QString & text );
78 
79  /**
80  * Loads the default stylesheet.
81  *
82  * The default stylesheet is determined by the environment variable Y2STYLE.
83  * If this variable is not set, the DEFAULT_STYLE_SHEET style sheet will be used.
84  *
85  * \return true if the stylesheet was loaded; false otherwise.
86  */
87  bool loadDefaultStyleSheet();
88 
89  /**
90  * Loads the alternate stylesheet
91  *
92  * The alternate stylesheet is determined by the environment variable Y2ALTSTYLE.
93  * If this variable is not set, the HIGH_COLOR_STYLE_SHEET style sheet will be used.
94  *
95  * \return true if the stylesheet was loaded; false otherwise.
96  */
98 
99  /**
100  * Returns the path to the style sheets directory.
101  */
102  QString themeDir() const;
103 
104  /**
105  * Registers a widget and applies the style sheet
106  *
107  * \param widget Widget to register.
108  */
109  void registerWidget( QWidget *widget );
110 
111  /**
112  * Unregisters a widget.
113  *
114  * \param widget Widget to unregister.
115  */
116  void unregisterWidget( QWidget *widget );
117 
118 
119  /**
120  * Registers a child widget.
121  *
122  * \param parent Parent widget.
123  * \param widget Widget to register.
124  */
125  void registerChildWidget( QWidget *parent, QWidget *widget );
126 
127  QString textStyle() const { return _textStyle; }
128 
129  /**
130  * Set style sheet for the default theme
131  *
132  * If the style sheet does not exists, it won't be changed.
133  *
134  * \param styleSheet Style sheet file name
135  */
136  void setDefaultStyleSheet(const QString & styleSheet);
137 
138  /**
139  * Set style sheet for the alternate theme
140  *
141  * If the style sheet does not exists, it won't be changed.
142  *
143  * \param styleSheet Style sheet file name
144  */
145  void setAlternateStyleSheet(const QString & styleSheet);
146 
147  /**
148  * Toggle between default/alternate style sheets.
149  */
151 
152  /**
153  * Determines if the alternate style is being used.
154  */
155  bool usingAlternateStyleSheet() { return _usingAlternateStyleSheet; }
156 
157  bool updateRendering( QWidget *wid );
158 
159 protected:
160  void renderParent( QWidget *wid );
161  QImage getScaled( const QString name, const QSize & size );
162 
163  /**
164  * Search and replace some self-defined macros in the style sheet.
165  * Among other things, expands the file name inside url( filename.png ) in
166  * the style sheet with the full path.
167  **/
168  void processUrls( QString & text );
169 
170  /**
171  * Build a stylesheet from a string.
172  */
173  const QString buildStyleSheet(QString content);
174 
175  /**
176  * Build a stylesheet from a string.
177  *
178  * Receives a list of already imported files.
179  */
180  const QString buildStyleSheet(QString content, QStringList & alreadyImportedFilenames);
181 
182  /**
183  * Build a stylesheet from a file.
184  *
185  * Receives a list of already imported files.
186  */
187  const QString buildStyleSheetFromFile(const QString & filename, QStringList & alreadyImportedFilenames);
188 
189  /*
190  * Reimplemented from QObject.
191  **/
192  bool eventFilter( QObject * obj, QEvent * ev );
193 
194  QString _currentStyleSheet;
195  QString _defaultStyleSheet = DEFAULT_STYLE_SHEET;
196  QString _alternateStyleSheet = HIGH_CONTRAST_STYLE_SHEET;
197  bool _usingAlternateStyleSheet = false;
198 
199 private:
200 
201  struct BackgrInfo
202  {
203  QString filename;
204  QImage pix;
205  QImage scaled;
206  QSize lastscale;
207  bool full;
208  };
209 
210  QHash<QString,BackgrInfo> _backgrounds;
211  QMap<QWidget*, QList< QWidget* > > _children;
212  // remember all registered widgets to allow styling not only for
213  // the explicitly requested children widgets (stored in _children)
214  QList< QWidget* > _registered_widgets;
215  QString _style;
216  QString _textStyle;
217 };
218 
219 
220 #endif // QY2Styler_h
bool styleSheetExists(const QString &file)
Determines if an style sheet exists.
Definition: QY2Styler.cc:91
QY2Styler(QObject *parent, const QString &defaultStyleSheet="", const QString &alternateStyleSheet="")
Constructor.
Definition: QY2Styler.cc:55
const QString buildStyleSheet(QString content)
Build a stylesheet from a string.
Definition: QY2Styler.cc:146
void toggleAlternateStyleSheet()
Toggle between default/alternate style sheets.
Definition: QY2Styler.cc:193
const QString buildStyleSheetFromFile(const QString &filename, QStringList &alreadyImportedFilenames)
Build a stylesheet from a file.
Definition: QY2Styler.cc:165
void setStyleSheet(const QString &text)
Applies a style sheet from a string.
Definition: QY2Styler.cc:177
void registerWidget(QWidget *widget)
Registers a widget and applies the style sheet.
Definition: QY2Styler.cc:268
bool loadStyleSheet(const QString &file)
Loads and apply a style sheet from a file.
Definition: QY2Styler.cc:127
bool usingAlternateStyleSheet()
Determines if the alternate style is being used.
Definition: QY2Styler.h:155
void processUrls(QString &text)
Search and replace some self-defined macros in the style sheet.
Definition: QY2Styler.cc:201
bool loadDefaultStyleSheet()
Loads the default stylesheet.
Definition: QY2Styler.cc:113
void setDefaultStyleSheet(const QString &styleSheet)
Set style sheet for the default theme.
Definition: QY2Styler.cc:97
void setAlternateStyleSheet(const QString &styleSheet)
Set style sheet for the alternate theme.
Definition: QY2Styler.cc:105
bool loadAlternateStyleSheet()
Loads the alternate stylesheet.
Definition: QY2Styler.cc:120
QString themeDir() const
Returns the path to the style sheets directory.
Definition: QY2Styler.cc:262
void registerChildWidget(QWidget *parent, QWidget *widget)
Registers a child widget.
Definition: QY2Styler.cc:284
void unregisterWidget(QWidget *widget)
Unregisters a widget.
Definition: QY2Styler.cc:277