Qt 4.8
qdecorationstyled_qws.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <qapplication.h>
43 #include <qwidget.h>
44 #include <qpainter.h>
45 #include <qdrawutil.h>
46 #include "qdecorationstyled_qws.h"
47 #include "qstyle.h"
48 #include "qstyleoption.h"
49 #include "qpaintengine.h"
50 
52 
53 #if !defined(QT_NO_QWS_DECORATION_STYLED) || defined(QT_PLUGIN)
54 
57 {
58 }
59 
61 {
62 }
63 
65 {
74  opt.titleBarFlags = widget->windowFlags();
76  opt.text = windowTitleFor(widget);
77  opt.icon = widget->windowIcon();
78  opt.rect = widget->rect();
79 
80  QStyle *style = QApplication::style();
81  if (!style)
82  return 18;
83 
84  return style->pixelMetric(QStyle::PM_TitleBarHeight, &opt, 0);
85 }
86 
87 bool QDecorationStyled::paint(QPainter *painter, const QWidget *widget, int decorationRegion,
88  DecorationState state)
89 {
90  if (decorationRegion == None)
91  return false;
92 
93  bool isActive = (widget == qApp->activeWindow());
94  QPalette pal = qApp->palette();
95  //ideally, the difference between Active and Inactive should be enough, so we shouldn't need to test this
96  if (!isActive) {
97  //pal.setCurrentColorGroup(QPalette::Disabled); //Can't do this either, because of palette limitations
98  //copied from Q3TitleBar:
105  }
106 
107  Qt::WindowFlags flags = widget->windowFlags();
108  bool hasBorder = !widget->isMaximized();
109  bool hasTitle = flags & Qt::WindowTitleHint;
110  bool hasSysMenu = flags & Qt::WindowSystemMenuHint;
111  bool hasContextHelp = flags & Qt::WindowContextHelpButtonHint;
112  bool hasMinimize = flags & Qt::WindowMinimizeButtonHint;
113  bool hasMaximize = flags & Qt::WindowMaximizeButtonHint;
114 
115  bool paintAll = (DecorationRegion(decorationRegion) == All);
116  bool handled = false;
117 
118  QStyle *style = QApplication::style();
119 
120  // In the case of a borderless title bar, the title bar must be expanded one
121  // borderWidth to the left, right and up.
122  bool noTitleBorder = style->styleHint(QStyle::SH_TitleBar_NoBorder, 0, widget);
123  int borderWidth = style->pixelMetric(QStyle::PM_MDIFrameWidth, 0, 0);
124  int titleHeight = titleBarHeight(widget) + (noTitleBorder ? borderWidth : 0);
125  int titleExtra = noTitleBorder ? borderWidth : 0;
126 
127  if ((paintAll || decorationRegion & Borders) && state == Normal && hasBorder) {
128  QRegion newClip = painter->clipRegion();
129  if (hasTitle) { // reduce flicker
130  QRect rect(widget->rect());
131  QRect r(rect.left() - titleExtra, rect.top() - titleHeight,
132  rect.width() + 2 * titleExtra, titleHeight);
133  newClip -= r;
134  }
135  if (!newClip.isEmpty()) {
136  QRect br = QDecoration::region(widget).boundingRect();
137  painter->save();
138  painter->setClipRegion(newClip);
139 
140  QStyleOptionFrame opt;
141  opt.palette = pal;
142  opt.rect = br;
143  opt.lineWidth = borderWidth;
144 
145  if (isActive)
147  bool porterDuff = painter->paintEngine()->hasFeature(QPaintEngine::PorterDuff);
148  if (porterDuff)
150  painter->fillRect(br, pal.window());
151  if (porterDuff)
153  style->drawPrimitive(QStyle::PE_FrameWindow, &opt, painter, 0);
154  painter->restore();
155 
156  decorationRegion &= (~Borders);
157  handled |= true;
158  }
159  }
160 
161  if (hasTitle) {
162  painter->save();
163 
165  opt.subControls = (decorationRegion & Title
167  | (decorationRegion & Menu
169  | (decorationRegion & Help
171  | (decorationRegion & Minimize
173  | (decorationRegion & Maximize
175  | (decorationRegion & (Minimize | Maximize)
177  | (decorationRegion & Close
179  opt.titleBarFlags = widget->windowFlags();
180  opt.titleBarState = widget->windowState();
181  if (isActive)
183  opt.text = windowTitleFor(widget);
184  opt.icon = widget->windowIcon();
185  opt.palette = pal;
186  opt.rect = QRect(widget->rect().x() - titleExtra, -titleHeight,
187  widget->rect().width() + 2 * titleExtra, titleHeight);
188 
189  if (paintAll) {
190  painter->setClipRegion(opt.rect);
191  } else {
192  const QRect widgetRect = widget->rect();
193  QRegion newClip = opt.rect;
194  if (!(decorationRegion & Menu) && hasSysMenu)
195  newClip -= region(widget, widgetRect, Menu);
196  if (!(decorationRegion & Title) && hasTitle)
197  newClip -= region(widget, widgetRect, Title);
198  if (!(decorationRegion & Help) && hasContextHelp)
199  newClip -= region(widget, widgetRect, Help);
200  if (!(decorationRegion & Minimize) && hasMinimize)
201  newClip -= region(widget, widgetRect, Minimize);
202  if (!(decorationRegion & Maximize) && hasMaximize)
203  newClip -= region(widget, widgetRect, Maximize);
204  if (!(decorationRegion & (Minimize | Maximize)) && (hasMaximize | hasMinimize))
205  newClip -= region(widget, widgetRect, Normal);
206  if (!(decorationRegion & Close))
207  newClip -= region(widget, widgetRect, Close);
208  painter->setClipRegion(newClip);
209  }
210 
211  if (state == Pressed)
212  opt.activeSubControls = opt.subControls;
213 
214  style->drawComplexControl(QStyle::CC_TitleBar, &opt, painter, 0);
215  painter->restore();
216 
217  decorationRegion &= ~(Title | Menu | Help | Normalize | Minimize | Maximize | Close);
218  handled |= true;
219  }
220 
221  return handled;
222 }
223 
224 QRegion QDecorationStyled::region(const QWidget *widget, const QRect &rect, int decorationRegion)
225 {
226  QStyle *style = QApplication::style();
227 
228  // In the case of a borderless title bar, the title bar must be expanded one
229  // borderWidth to the left, right and up.
230  bool noTitleBorder = style->styleHint(QStyle::SH_TitleBar_NoBorder, 0, widget);
231  int borderWidth = style->pixelMetric(QStyle::PM_MDIFrameWidth, 0, 0);
232  int titleHeight = titleBarHeight(widget) + (noTitleBorder ? borderWidth : 0);
233  int titleExtra = noTitleBorder ? borderWidth : 0;
234 
235  QRect inside = QRect(rect.x() - titleExtra, rect.top() - titleHeight,
236  rect.width() + 2 * titleExtra, titleHeight);
237 
238  Qt::WindowFlags flags = widget->windowFlags();
239  bool hasSysMenu = flags & Qt::WindowSystemMenuHint;
240  bool hasContextHelp = flags & Qt::WindowContextHelpButtonHint;
241  bool hasMinimize = flags & Qt::WindowMinimizeButtonHint;
242  bool hasMaximize = flags & Qt::WindowMaximizeButtonHint;
243 
251  opt.titleBarFlags = widget->windowFlags();
253  opt.text = windowTitleFor(widget);
254  opt.icon = widget->windowIcon();
255  opt.rect = inside;
256 
257  QRegion region;
258  switch (decorationRegion) {
259  case Title:
260  region = style->subControlRect(QStyle::CC_TitleBar, &opt,
262  break;
263  case Menu:
264  if (hasSysMenu)
265  region = style->subControlRect(QStyle::CC_TitleBar, &opt,
267  break;
268  case Help:
269  if (hasContextHelp)
270  region = style->subControlRect(QStyle::CC_TitleBar, &opt,
272  0);
273  break;
274  case Normalize:
275  if (hasMaximize | hasMinimize)
276  region = style->subControlRect(QStyle::CC_TitleBar, &opt,
278  0);
279  break;
280  case Minimize:
281  if (hasMinimize)
282  region = style->subControlRect(QStyle::CC_TitleBar, &opt,
284  0);
285  break;
286  case Maximize:
287  if (hasMaximize)
288  region = style->subControlRect(QStyle::CC_TitleBar, &opt,
290  0);
291  break;
292  case Close:
293  region = style->subControlRect(QStyle::CC_TitleBar, &opt,
295  break;
296 
297  default:
298  region = QDecorationDefault::region(widget, rect, decorationRegion);
299  }
300 
301  opt.rect = QRect(rect.x() - titleExtra, rect.top() - titleHeight,
302  rect.width() + 2 * titleExtra,
303  rect.height() + titleHeight + titleExtra);
304 
306  style->styleHint(QStyle::SH_WindowFrame_Mask, &opt, 0, &mask);
307 
308  return (mask.region.isEmpty() ? region : (region & mask.region));
309 }
310 
311 #endif // QT_NO_QWS_DECORATION_STYLED
312 
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
static Qt::LayoutDirection layoutDirection()
SubControl
This enum describes the available sub controls.
Definition: qstyle.h:402
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointer< QWidget > widget
Qt::WindowStates windowState() const
Returns the current window state.
Definition: qwidget.cpp:3086
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
virtual QRect subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget=0) const =0
Returns the rectangle containing the specified subControl of the given complex control (with the styl...
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
QIcon icon
the icon for the title bar
Definition: qstyleoption.h:823
virtual QRegion region(const QWidget *widget, const QRect &rect, int decorationRegion=All)
Returns the region specified by decorationRegion for the top-level widget.
The QStyleOptionTitleBar class is used to describe the parameters for drawing a title bar...
Definition: qstyleoption.h:816
void restore()
Restores the current painter state (pops a saved state off the stack).
Definition: qpainter.cpp:1620
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
virtual int styleHint(StyleHint stylehint, const QStyleOption *opt=0, const QWidget *widget=0, QStyleHintReturn *returnData=0) const =0
Returns an integer representing the specified style hint for the given widget described by the provid...
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
QString text
the text of the title bar
Definition: qstyleoption.h:822
QRect boundingRect() const
Returns the bounding rectangle of this region.
Definition: qregion.cpp:4363
static QStyle * style()
Returns the application&#39;s style object.
QStyle::SubControls activeSubControls
This variable holds a bitwise OR of the sub-controls that are active for the complex control...
Definition: qstyleoption.h:694
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
const QColor & color(ColorGroup cg, ColorRole cr) const
Returns the color in the specified color group, used for the given color role.
Definition: qpalette.h:107
QIcon windowIcon
the widget&#39;s icon
Definition: qwidget.h:199
int titleBarHeight(const QWidget *widget)
Returns the title bar height in pixels for the given widget.
void save()
Saves the current painter state (pushes the state onto a stack).
Definition: qpainter.cpp:1590
virtual QRegion region(const QWidget *w, const QRect &rect, int decorationRegion=All)=0
Implement this function to return the region specified by decorationRegion for the given top level wi...
int lineWidth
the line width for drawing the frame
Definition: qstyleoption.h:124
QRegion clipRegion() const
Returns the currently set clip region.
Definition: qpainter.cpp:2562
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isEmpty() const
Returns true if the region is empty; otherwise returns false.
Definition: qregion.cpp:4098
#define qApp
QRegion region(const QWidget *widget, const QRect &rect, int decorationRegion=All)
Returns the region specified by decorationRegion for the top-level widget.
virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w=0) const =0
Draws the given primitive element with the provided painter using the style options specified by opti...
The QStyleOptionFrame class is used to describe the parameters for drawing a frame.
Definition: qstyleoption.h:118
QPaintEngine * paintEngine() const
Returns the paint engine that the painter is currently operating on if the painter is active; otherwi...
Definition: qpainter.cpp:1991
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
virtual void drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget=0) const =0
Draws the given control using the provided painter with the style options specified by option...
bool isMaximized() const
Definition: qwidget.cpp:3074
QPalette palette
the palette that should be used when painting the control
Definition: qstyleoption.h:92
The QStyleHintReturnMask class provides style hints that return a QRegion.
Definition: qstyleoption.h:923
int top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:243
QString windowTitleFor(const QWidget *widget) const
void setClipRegion(const QRegion &, Qt::ClipOperation op=Qt::ReplaceClip)
Sets the clip region to the given region using the specified clip operation.
Definition: qpainter.cpp:2917
void setCompositionMode(CompositionMode mode)
Sets the composition mode to the given mode.
Definition: qpainter.cpp:2422
QRect rect
the internal geometry of the widget excluding any window frame
Definition: qwidget.h:168
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
void setColor(ColorGroup cg, ColorRole cr, const QColor &color)
Sets the color in the specified color group, used for the given color role, to the specified solid co...
Definition: qpalette.h:201
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
bool paint(QPainter *painter, const QWidget *widget, int decorationRegion=All, DecorationState state=Normal)
Paints the border and title decoration for the top-level widget using the painter provided and the de...
Qt::LayoutDirection direction
the text layout direction that should be used when drawing text in the control
Definition: qstyleoption.h:89
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
DecorationState
This enum describes the various states of a decoration region.
Qt::WindowFlags titleBarFlags
the widget flags for the title bar
Definition: qstyleoption.h:825
Qt::WindowFlags windowFlags() const
Window flags are a combination of a type (e.
Definition: qwidget.h:939
The QDecorationDefault class is a base class providing default window decorations.
int titleBarState
the state of the title bar
Definition: qstyleoption.h:824
const QBrush & window() const
Returns the window (general background) brush of the current color group.
Definition: qpalette.h:135
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90
QStyle::SubControls subControls
This variable holds a bitwise OR of the sub-controls to be drawn for the complex control.
Definition: qstyleoption.h:693
DecorationRegion
This enum describes the various regions of the window decoration.
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
Definition: qpainter.cpp:7420
bool hasFeature(PaintEngineFeatures feature) const
Returns true if the paint engine supports the specified feature; otherwise returns false...
Definition: qpaintengine.h:229
The QPalette class contains color groups for each widget state.
Definition: qpalette.h:61