source: trunk/src/gui/graphicsview/qgraphicswidget_p.cpp@ 396

Last change on this file since 396 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 25.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qglobal.h"
43
44#ifndef QT_NO_GRAPHICSVIEW
45
46#include <QtCore/qdebug.h>
47#include "qgraphicswidget_p.h"
48#include "qgraphicslayout.h"
49#include "qgraphicsscene_p.h"
50#include <QtGui/qapplication.h>
51#include <QtGui/qgraphicsscene.h>
52#include <QtGui/qstyleoption.h>
53#include <QtGui/QStyleOptionTitleBar>
54#include <QtGui/QGraphicsSceneMouseEvent>
55#if defined(Q_WS_MAC) && !defined(QT_NO_STYLE_MAC)
56# include <QMacStyle>
57#endif
58
59QT_BEGIN_NAMESPACE
60
61void QGraphicsWidgetPrivate::init(QGraphicsItem *parentItem, Qt::WindowFlags wFlags)
62{
63 Q_Q(QGraphicsWidget);
64
65 attributes = 0;
66 isWidget = 1; // QGraphicsItem::isWidget() returns true.
67 focusNext = focusPrev = q;
68 focusPolicy = Qt::NoFocus;
69 q->setParentItem(parentItem);
70 q->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred, QSizePolicy::DefaultType));
71 q->setGraphicsItem(q);
72
73 resolveLayoutDirection();
74
75 if (!parentItem)
76 adjustWindowFlags(&wFlags);
77 windowFlags = wFlags;
78 q->unsetWindowFrameMargins();
79}
80qreal QGraphicsWidgetPrivate::titleBarHeight(const QStyleOptionTitleBar &options) const
81{
82 Q_Q(const QGraphicsWidget);
83 int height = q->style()->pixelMetric(QStyle::PM_TitleBarHeight, &options);
84#if defined(Q_WS_MAC) && !defined(QT_NO_STYLE_MAC)
85 if (qobject_cast<QMacStyle*>(q->style())) {
86 height -=4;
87 }
88#endif
89 return (qreal)height;
90}
91
92void QGraphicsWidgetPrivate::getLayoutItemMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const
93{
94 if (left)
95 *left = leftLayoutItemMargin;
96 if (top)
97 *top = topLayoutItemMargin;
98 if (right)
99 *right = rightLayoutItemMargin;
100 if (bottom)
101 *bottom = bottomLayoutItemMargin;
102}
103
104void QGraphicsWidgetPrivate::setLayoutItemMargins(qreal left, qreal top, qreal right, qreal bottom)
105{
106 if (leftLayoutItemMargin == left
107 && topLayoutItemMargin == top
108 && rightLayoutItemMargin == right
109 && bottomLayoutItemMargin == bottom)
110 return;
111
112 Q_Q(QGraphicsWidget);
113 leftLayoutItemMargin = left;
114 topLayoutItemMargin = top;
115 rightLayoutItemMargin = right;
116 bottomLayoutItemMargin = bottom;
117 q->updateGeometry();
118}
119
120void QGraphicsWidgetPrivate::setLayoutItemMargins(QStyle::SubElement element, const QStyleOption *opt)
121{
122 Q_Q(QGraphicsWidget);
123 QStyleOption myOpt;
124 if (!opt) {
125 q->initStyleOption(&myOpt);
126 myOpt.rect.setRect(0, 0, 32768, 32768); // arbitrary
127 opt = &myOpt;
128 }
129
130 QRect liRect = q->style()->subElementRect(element, opt, /* q */ 0);
131 if (liRect.isValid()) {
132 leftLayoutItemMargin = (opt->rect.left() - liRect.left());
133 topLayoutItemMargin = (opt->rect.top() - liRect.top());
134 rightLayoutItemMargin = (liRect.right() - opt->rect.right());
135 bottomLayoutItemMargin = (liRect.bottom() - opt->rect.bottom());
136 } else {
137 leftLayoutItemMargin = 0;
138 topLayoutItemMargin = 0;
139 rightLayoutItemMargin = 0;
140 bottomLayoutItemMargin = 0;
141 }
142}
143
144void QGraphicsWidgetPrivate::setPalette_helper(const QPalette &palette)
145{
146 if (this->palette == palette && this->palette.resolve() == palette.resolve())
147 return;
148 updatePalette(palette);
149}
150
151void QGraphicsWidgetPrivate::resolvePalette(uint inheritedMask)
152{
153 inheritedPaletteResolveMask = inheritedMask;
154 QPalette naturalPalette = naturalWidgetPalette();
155 QPalette resolvedPalette = palette.resolve(naturalPalette);
156 updatePalette(resolvedPalette);
157}
158
159void QGraphicsWidgetPrivate::updatePalette(const QPalette &palette)
160{
161 Q_Q(QGraphicsWidget);
162 // Update local palette setting.
163 this->palette = palette;
164
165 // Calculate new mask.
166 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation))
167 inheritedPaletteResolveMask = 0;
168 int mask = palette.resolve() | inheritedPaletteResolveMask;
169
170 // Propagate to children.
171 for (int i = 0; i < children.size(); ++i) {
172 QGraphicsItem *item = children.at(i);
173 if (item->isWidget()) {
174 QGraphicsWidget *w = static_cast<QGraphicsWidget *>(item);
175 if (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))
176 w->d_func()->resolvePalette(mask);
177 } else {
178 item->d_ptr->resolvePalette(mask);
179 }
180 }
181
182 // Notify change.
183 QEvent event(QEvent::PaletteChange);
184 QApplication::sendEvent(q, &event);
185}
186
187void QGraphicsWidgetPrivate::setLayoutDirection_helper(Qt::LayoutDirection direction)
188{
189 Q_Q(QGraphicsWidget);
190 if ((direction == Qt::RightToLeft) == (testAttribute(Qt::WA_RightToLeft)))
191 return;
192 q->setAttribute(Qt::WA_RightToLeft, (direction == Qt::RightToLeft));
193
194 // Propagate this change to all children.
195 for (int i = 0; i < children.size(); ++i) {
196 QGraphicsItem *item = children.at(i);
197 if (item->isWidget()) {
198 QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item);
199 if (widget->parentWidget() && !widget->testAttribute(Qt::WA_SetLayoutDirection))
200 widget->d_func()->setLayoutDirection_helper(direction);
201 }
202 }
203
204 // Send the notification event to this widget item.
205 QEvent e(QEvent::LayoutDirectionChange);
206 QApplication::sendEvent(q, &e);
207}
208
209void QGraphicsWidgetPrivate::resolveLayoutDirection()
210{
211 Q_Q(QGraphicsWidget);
212 if (q->testAttribute(Qt::WA_SetLayoutDirection)) {
213 return;
214 }
215 if (QGraphicsWidget *parentWidget = q->parentWidget()) {
216 setLayoutDirection_helper(parentWidget->layoutDirection());
217 } else if (scene) {
218 // ### shouldn't the scene have a layoutdirection really? how does
219 // ### QGraphicsWidget get changes from QApplication::layoutDirection?
220 setLayoutDirection_helper(QApplication::layoutDirection());
221 } else {
222 setLayoutDirection_helper(QApplication::layoutDirection());
223 }
224}
225
226QPalette QGraphicsWidgetPrivate::naturalWidgetPalette() const
227{
228 Q_Q(const QGraphicsWidget);
229 QPalette palette;
230 if (QGraphicsWidget *parent = q->parentWidget()) {
231 palette = parent->palette();
232 } else if (scene) {
233 palette = scene->palette();
234 }
235 palette.resolve(0);
236 return palette;
237}
238
239void QGraphicsWidgetPrivate::setFont_helper(const QFont &font)
240{
241 if (this->font == font && this->font.resolve() == font.resolve())
242 return;
243 updateFont(font);
244}
245
246void QGraphicsWidgetPrivate::resolveFont(uint inheritedMask)
247{
248 inheritedFontResolveMask = inheritedMask;
249 QFont naturalFont = naturalWidgetFont();
250 QFont resolvedFont = font.resolve(naturalFont);
251 updateFont(resolvedFont);
252}
253
254void QGraphicsWidgetPrivate::updateFont(const QFont &font)
255{
256 Q_Q(QGraphicsWidget);
257 // Update the local font setting.
258 this->font = font;
259
260 // Calculate new mask.
261 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation))
262 inheritedFontResolveMask = 0;
263 int mask = font.resolve() | inheritedFontResolveMask;
264
265 // Propagate to children.
266 for (int i = 0; i < children.size(); ++i) {
267 QGraphicsItem *item = children.at(i);
268 if (item->isWidget()) {
269 QGraphicsWidget *w = static_cast<QGraphicsWidget *>(item);
270 if (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))
271 w->d_func()->resolveFont(mask);
272 } else {
273 item->d_ptr->resolveFont(mask);
274 }
275 }
276
277 if (!polished)
278 return;
279 // Notify change.
280 QEvent event(QEvent::FontChange);
281 QApplication::sendEvent(q, &event);
282}
283
284QFont QGraphicsWidgetPrivate::naturalWidgetFont() const
285{
286 Q_Q(const QGraphicsWidget);
287 QFont naturalFont; // ### no application font support
288 if (QGraphicsWidget *parent = q->parentWidget()) {
289 naturalFont = parent->font();
290 } else if (scene) {
291 naturalFont = scene->font();
292 }
293 naturalFont.resolve(0);
294 return naturalFont;
295}
296
297void QGraphicsWidgetPrivate::initStyleOptionTitleBar(QStyleOptionTitleBar *option)
298{
299 Q_Q(QGraphicsWidget);
300 q->initStyleOption(option);
301 option->rect.setHeight(titleBarHeight(*option));
302 option->titleBarFlags = windowFlags;