| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 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 qt-sales@nokia.com.
|
|---|
| 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 |
|
|---|
| 59 | QT_BEGIN_NAMESPACE
|
|---|
| 60 |
|
|---|
| 61 | void 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 | }
|
|---|
| 80 | qreal 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 |
|
|---|
| 92 | void 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 |
|
|---|
| 104 | void 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 |
|
|---|
| 120 | void 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 |
|
|---|
| 144 | void QGraphicsWidgetPrivate::setPalette_helper(const QPalette &palette)
|
|---|
| 145 | {
|
|---|
| 146 | if (this->palette == palette && this->palette.resolve() == palette.resolve())
|
|---|
| 147 | return;
|
|---|
| 148 | updatePalette(palette);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | void QGraphicsWidgetPrivate::resolvePalette(uint inheritedMask)
|
|---|
| 152 | {
|
|---|
| 153 | inheritedPaletteResolveMask = inheritedMask;
|
|---|
| 154 | QPalette naturalPalette = naturalWidgetPalette();
|
|---|
| 155 | QPalette resolvedPalette = palette.resolve(naturalPalette);
|
|---|
| 156 | updatePalette(resolvedPalette);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | void 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 |
|
|---|
| 187 | void 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 |
|
|---|
| 209 | void 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 |
|
|---|
| 226 | QPalette 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 |
|
|---|
| 239 | void QGraphicsWidgetPrivate::setFont_helper(const QFont &font)
|
|---|
| 240 | {
|
|---|
| 241 | if (this->font == font && this->font.resolve() == font.resolve())
|
|---|
| 242 | return;
|
|---|
| 243 | updateFont(font);
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | void QGraphicsWidgetPrivate::resolveFont(uint inheritedMask)
|
|---|
| 247 | {
|
|---|
| 248 | inheritedFontResolveMask = inheritedMask;
|
|---|
| 249 | QFont naturalFont = naturalWidgetFont();
|
|---|
| 250 | QFont resolvedFont = font.resolve(naturalFont);
|
|---|
| 251 | updateFont(resolvedFont);
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | void 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 |
|
|---|
| 284 | QFont 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 |
|
|---|
| 297 | void QGraphicsWidgetPrivate::initStyleOptionTitleBar(QStyleOptionTitleBar *option)
|
|---|
| 298 | {
|
|---|
| 299 | Q_Q(QGraphicsWidget);
|
|---|
| 300 | q->initStyleOption(option);
|
|---|
| 301 | option->rect.setHeight(titleBarHeight(*option));
|
|---|
| 302 | option->titleBarFlags = windowFlags;
|
|---|
| 303 | option->subControls = QStyle::SC_TitleBarCloseButton | QStyle::SC_TitleBarLabel | QStyle::SC_TitleBarSysMenu;
|
|---|
| 304 | option->activeSubControls = hoveredSubControl;
|
|---|
| 305 | bool isActive = q->isActiveWindow();
|
|---|
| 306 | if (isActive) {
|
|---|
| 307 | option->state |= QStyle::State_Active;
|
|---|
| 308 | option->titleBarState = Qt::WindowActive;
|
|---|
| 309 | option->titleBarState |= QStyle::State_Active;
|
|---|
| 310 | } else {
|
|---|
| 311 | option->state &= ~QStyle::State_Active;
|
|---|
| 312 | option->titleBarState = Qt::WindowNoState;
|
|---|
| 313 | }
|
|---|
| 314 | QFont windowTitleFont = QApplication::font("QWorkspaceTitleBar");
|
|---|
| 315 | QRect textRect = q->style()->subControlRect(QStyle::CC_TitleBar, option, QStyle::SC_TitleBarLabel, 0);
|
|---|
| 316 | option->text = QFontMetrics(windowTitleFont).elidedText(windowTitle, Qt::ElideRight, textRect.width());
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | void QGraphicsWidgetPrivate::adjustWindowFlags(Qt::WindowFlags *flags)
|
|---|
| 320 | {
|
|---|
| 321 | bool customize = (*flags & (Qt::CustomizeWindowHint
|
|---|
| 322 | | Qt::FramelessWindowHint
|
|---|
| 323 | | Qt::WindowTitleHint
|
|---|
| 324 | | Qt::WindowSystemMenuHint
|
|---|
| 325 | | Qt::WindowMinimizeButtonHint
|
|---|
| 326 | | Qt::WindowMaximizeButtonHint
|
|---|
| 327 | | Qt::WindowContextHelpButtonHint));
|
|---|
| 328 |
|
|---|
| 329 | uint type = (*flags & Qt::WindowType_Mask);
|
|---|
| 330 | if (customize)
|
|---|
| 331 | ;
|
|---|
| 332 | else if (type == Qt::Dialog || type == Qt::Sheet)
|
|---|
| 333 | *flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowContextHelpButtonHint;
|
|---|
| 334 | else if (type == Qt::Tool)
|
|---|
| 335 | *flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint;
|
|---|
| 336 | else if (type == Qt::Window || type == Qt::SubWindow)
|
|---|
| 337 | *flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint
|
|---|
| 338 | | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | void QGraphicsWidgetPrivate::windowFrameMouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|---|
| 342 | {
|
|---|
| 343 | Q_Q(QGraphicsWidget);
|
|---|
| 344 | if (grabbedSection != Qt::NoSection) {
|
|---|
| 345 | if (grabbedSection == Qt::TitleBarArea) {
|
|---|
| 346 | buttonSunken = false;
|
|---|
| 347 | QStyleOptionTitleBar bar;
|
|---|
| 348 | initStyleOptionTitleBar(&bar);
|
|---|
| 349 | // make sure that the coordinates (rect and pos) we send to the style are positive.
|
|---|
| 350 | bar.rect = q->windowFrameRect().toRect();
|
|---|
| 351 | bar.rect.moveTo(0,0);
|
|---|
| 352 | bar.rect.setHeight(q->style()->pixelMetric(QStyle::PM_TitleBarHeight, &bar));
|
|---|
| 353 | QPointF pos = event->pos();
|
|---|
| 354 | pos.rx() += leftWindowFrameMargin;
|
|---|
| 355 | pos.ry() += topWindowFrameMargin;
|
|---|
| 356 | bar.subControls = QStyle::SC_TitleBarCloseButton;
|
|---|
| 357 | if (q->style()->subControlRect(QStyle::CC_TitleBar, &bar,
|
|---|
| 358 | QStyle::SC_TitleBarCloseButton,
|
|---|
| 359 | event->widget()).contains(pos.toPoint())) {
|
|---|
| 360 | q->close();
|
|---|
| 361 | }
|
|---|
| 362 | }
|
|---|
| 363 | if (!(static_cast<QGraphicsSceneMouseEvent *>(event)->buttons()))
|
|---|
| 364 | grabbedSection = Qt::NoSection;
|
|---|
| 365 | event->accept();
|
|---|
| 366 | }
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | void QGraphicsWidgetPrivate::windowFrameMousePressEvent(QGraphicsSceneMouseEvent *event)
|
|---|
| 370 | {
|
|---|
| 371 | Q_Q(QGraphicsWidget);
|
|---|
| 372 | if (event->button() != Qt::LeftButton)
|
|---|
| 373 | return;
|
|---|
| 374 |
|
|---|
| 375 | startGeometry = q->geometry();
|
|---|
| 376 | grabbedSection = q->windowFrameSectionAt(event->pos());
|
|---|
| 377 | switch (grabbedSection) {
|
|---|
| 378 | case Qt::LeftSection:
|
|---|
| 379 | case Qt::TopLeftSection:
|
|---|
| 380 | mouseDelta = event->pos() - q->rect().topLeft();
|
|---|
| 381 | break;
|
|---|
| 382 | case Qt::TopSection:
|
|---|
| 383 | case Qt::TopRightSection:
|
|---|
| 384 | mouseDelta = event->pos() - q->rect().topRight();
|
|---|
| 385 | break;
|
|---|
| 386 | case Qt::RightSection:
|
|---|
| 387 | case Qt::BottomRightSection:
|
|---|
| 388 | mouseDelta = event->pos() - q->rect().bottomRight();
|
|---|
| 389 | break;
|
|---|
| 390 | case Qt::BottomSection:
|
|---|
| 391 | case Qt::BottomLeftSection:
|
|---|
| 392 | mouseDelta = event->pos() - q->rect().bottomLeft();
|
|---|
| 393 | break;
|
|---|
| 394 | case Qt::TitleBarArea:
|
|---|
| 395 | if (hoveredSubControl == QStyle::SC_TitleBarCloseButton) {
|
|---|
| 396 | buttonSunken = true;
|
|---|
| 397 | q->update();
|
|---|
| 398 | }
|
|---|
| 399 | break;
|
|---|
| 400 | case Qt::NoSection:
|
|---|
| 401 | break;
|
|---|
| 402 | }
|
|---|
| 403 | event->setAccepted(grabbedSection != Qt::NoSection);
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | static void _q_boundGeometryToSizeConstraints(const QRectF &startGeometry,
|
|---|
| 407 | QRectF *rect, Qt::WindowFrameSection section,
|
|---|
| 408 | const QSizeF &min, const QSizeF &max)
|
|---|
| 409 | {
|
|---|
| 410 | int height;
|
|---|
| 411 | int width;
|
|---|
| 412 | switch (section) {
|
|---|
| 413 | case Qt::LeftSection:
|
|---|
| 414 | width = qRound(qBound(min.width(), rect->width(), max.width()));
|
|---|
| 415 | rect->setRect(startGeometry.right() - width, startGeometry.top(),
|
|---|
| 416 | width, startGeometry.height());
|
|---|
| 417 | break;
|
|---|
| 418 | case Qt::TopLeftSection:
|
|---|
| 419 | width = qRound(qBound(min.width(), rect->width(), max.width()));
|
|---|
| 420 | height = qRound(qBound(min.height(), rect->height(), max.height()));
|
|---|
| 421 | rect->setRect(startGeometry.right() - width, startGeometry.bottom() - height,
|
|---|
| 422 | width, height);
|
|---|
| 423 | break;
|
|---|
| 424 | case Qt::TopSection:
|
|---|
| 425 | height = qRound(qBound(min.height(), rect->height(), max.height()));
|
|---|
| 426 | rect->setRect(startGeometry.left(), startGeometry.bottom() - height,
|
|---|
| 427 | startGeometry.width(), height);
|
|---|
| 428 | break;
|
|---|
| 429 | case Qt::TopRightSection:
|
|---|
| 430 | height = qRound(qBound(min.height(), rect->height(), max.height()));
|
|---|
| 431 | rect->setTop(rect->bottom() - height);
|
|---|
| 432 | rect->setWidth(qBound(min.width(), rect->width(), max.width()));
|
|---|
| 433 | break;
|
|---|
| 434 | case Qt::RightSection:
|
|---|
| 435 | rect->setWidth(qBound(min.width(), rect->width(), max.width()));
|
|---|
| 436 | break;
|
|---|
| 437 | case Qt::BottomRightSection:
|
|---|
| 438 | rect->setWidth(qBound(min.width(), rect->width(), max.width()));
|
|---|
| 439 | rect->setHeight(qBound(min.height(), rect->height(), max.height()));
|
|---|
| 440 | break;
|
|---|
| 441 | case Qt::BottomSection:
|
|---|
| 442 | rect->setHeight(qBound(min.height(), rect->height(), max.height()));
|
|---|
| 443 | break;
|
|---|
| 444 | case Qt::BottomLeftSection:
|
|---|
| 445 | height = qRound(qBound(min.height(), rect->height(), max.height()));
|
|---|
| 446 | width = qRound(qBound(min.width(), rect->width(), max.width()));
|
|---|
| 447 | rect->setRect(startGeometry.right() - width, startGeometry.top(),
|
|---|
| 448 | width, height);
|
|---|
| 449 | break;
|
|---|
| 450 | default:
|
|---|
| 451 | break;
|
|---|
| 452 | }
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | void QGraphicsWidgetPrivate::windowFrameMouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|---|
| 456 | {
|
|---|
| 457 | Q_Q(QGraphicsWidget);
|
|---|
| 458 | if (!(event->buttons() & Qt::LeftButton) || hoveredSubControl != QStyle::SC_TitleBarLabel)
|
|---|
| 459 | return;
|
|---|
| 460 |
|
|---|
| 461 | QLineF delta(q->mapFromScene(event->buttonDownScenePos(Qt::LeftButton)), event->pos());
|
|---|
| 462 | QLineF parentDelta(q->mapToParent(delta.p1()), q->mapToParent(delta.p2()));
|
|---|
| 463 | QLineF parentXDelta(q->mapToParent(QPointF(delta.p1().x(), 0)), q->mapToParent(QPointF(delta.p2().x(), 0)));
|
|---|
| 464 | QLineF parentYDelta(q->mapToParent(QPointF(0, delta.p1().y())), q->mapToParent(QPointF(0, delta.p2().y())));
|
|---|
| 465 |
|
|---|
| 466 | QRectF newGeometry;
|
|---|
| 467 | switch (grabbedSection) {
|
|---|
| 468 | case Qt::LeftSection:
|
|---|
| 469 | newGeometry = QRectF(startGeometry.topLeft() + QPointF(parentXDelta.dx(), parentXDelta.dy()),
|
|---|
| 470 | startGeometry.size() - QSizeF(delta.dx(), delta.dy()));
|
|---|
| 471 | break;
|
|---|
| 472 | case Qt::TopLeftSection:
|
|---|
| 473 | newGeometry = QRectF(startGeometry.topLeft() + QPointF(parentDelta.dx(), parentDelta.dy()),
|
|---|
| 474 | startGeometry.size() - QSizeF(delta.dx(), delta.dy()));
|
|---|
| 475 | break;
|
|---|
| 476 | case Qt::TopSection:
|
|---|
| 477 | newGeometry = QRectF(startGeometry.topLeft() + QPointF(parentYDelta.dx(), parentYDelta.dy()),
|
|---|
| 478 | startGeometry.size() - QSizeF(0, delta.dy()));
|
|---|
| 479 | break;
|
|---|
| 480 | case Qt::TopRightSection:
|
|---|
| 481 | newGeometry = QRectF(startGeometry.topLeft() + QPointF(parentYDelta.dx(), parentYDelta.dy()),
|
|---|
| 482 | startGeometry.size() - QSizeF(-delta.dx(), delta.dy()));
|
|---|
| 483 | break;
|
|---|
| 484 | case Qt::RightSection:
|
|---|
| 485 | newGeometry = QRectF(startGeometry.topLeft(),
|
|---|
| 486 | startGeometry.size() + QSizeF(delta.dx(), 0));
|
|---|
| 487 | break;
|
|---|
| 488 | case Qt::BottomRightSection:
|
|---|
| 489 | newGeometry = QRectF(startGeometry.topLeft(),
|
|---|
| 490 | startGeometry.size() + QSizeF(delta.dx(), delta.dy()));
|
|---|
| 491 | break;
|
|---|
| 492 | case Qt::BottomSection:
|
|---|
| 493 | newGeometry = QRectF(startGeometry.topLeft(),
|
|---|
| 494 | startGeometry.size() + QSizeF(0, delta.dy()));
|
|---|
| 495 | break;
|
|---|
| 496 | case Qt::BottomLeftSection:
|
|---|
| 497 | newGeometry = QRectF(startGeometry.topLeft() + QPointF(parentXDelta.dx(), parentXDelta.dy()),
|
|---|
| 498 | startGeometry.size() - QSizeF(delta.dx(), -delta.dy()));
|
|---|
| 499 | break;
|
|---|
| 500 | case Qt::TitleBarArea:
|
|---|
| 501 | newGeometry = QRectF(startGeometry.topLeft() + QPointF(parentDelta.dx(), parentDelta.dy()),
|
|---|
| 502 | startGeometry.size());
|
|---|
| 503 | break;
|
|---|
| 504 | case Qt::NoSection:
|
|---|
| 505 | break;
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | if (grabbedSection != Qt::NoSection) {
|
|---|
| 509 | _q_boundGeometryToSizeConstraints(startGeometry, &newGeometry, grabbedSection,
|
|---|
| 510 | q->effectiveSizeHint(Qt::MinimumSize),
|
|---|
| 511 | q->effectiveSizeHint(Qt::MaximumSize));
|
|---|
| 512 | q->setGeometry(newGeometry);
|
|---|
| 513 | }
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | void QGraphicsWidgetPrivate::windowFrameHoverMoveEvent(QGraphicsSceneHoverEvent *event)
|
|---|
| 517 | {
|
|---|
| 518 | Q_Q(QGraphicsWidget);
|
|---|
| 519 | if (!hasDecoration())
|
|---|
| 520 | return;
|
|---|
| 521 |
|
|---|
| 522 | if (q->rect().contains(event->pos())) {
|
|---|
| 523 | if (buttonMouseOver || hoveredSubControl != QStyle::SC_None)
|
|---|
| 524 | windowFrameHoverLeaveEvent(event);
|
|---|
| 525 | return;
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | bool wasMouseOver = buttonMouseOver;
|
|---|
| 529 | QRect oldButtonRect = buttonRect;
|
|---|
| 530 | buttonRect = QRect();
|
|---|
| 531 | buttonMouseOver = false;
|
|---|
| 532 | QPointF pos = event->pos();
|
|---|
| 533 | QStyleOptionTitleBar bar;
|
|---|
| 534 | // make sure that the coordinates (rect and pos) we send to the style are positive.
|
|---|
| 535 | pos.rx() += leftWindowFrameMargin;
|
|---|
| 536 | pos.ry() += topWindowFrameMargin;
|
|---|
| 537 | initStyleOptionTitleBar(&bar);
|
|---|
| 538 | bar.rect = q->windowFrameRect().toRect();
|
|---|
| 539 | bar.rect.moveTo(0,0);
|
|---|
| 540 | bar.rect.setHeight(int(titleBarHeight(bar)));
|
|---|
| 541 |
|
|---|
| 542 | Qt::CursorShape cursorShape = Qt::ArrowCursor;
|
|---|
| 543 | bool needsSetCursorCall = true;
|
|---|
| 544 | switch (q->windowFrameSectionAt(event->pos())) {
|
|---|
| 545 | case Qt::TopLeftSection:
|
|---|
| 546 | case Qt::BottomRightSection:
|
|---|
| 547 | cursorShape = Qt::SizeFDiagCursor;
|
|---|
| 548 | break;
|
|---|
| 549 | case Qt::TopRightSection:
|
|---|
| 550 | case Qt::BottomLeftSection:
|
|---|
| 551 | cursorShape = Qt::SizeBDiagCursor;
|
|---|
| 552 | break;
|
|---|
| 553 | case Qt::LeftSection:
|
|---|
| 554 | case Qt::RightSection:
|
|---|
| 555 | cursorShape = Qt::SizeHorCursor;
|
|---|
| 556 | break;
|
|---|
| 557 | case Qt::TopSection:
|
|---|
| 558 | case Qt::BottomSection:
|
|---|
| 559 | cursorShape = Qt::SizeVerCursor;
|
|---|
| 560 | break;
|
|---|
| 561 | case Qt::TitleBarArea:
|
|---|
| 562 | buttonRect = q->style()->subControlRect(QStyle::CC_TitleBar, &bar, QStyle::SC_TitleBarCloseButton, 0);
|
|---|
| 563 | #ifdef Q_WS_MAC
|
|---|
| 564 | // On mac we should hover if we are in the 'area' of the buttons
|
|---|
| 565 | buttonRect |= q->style()->subControlRect(QStyle::CC_TitleBar, &bar, QStyle::SC_TitleBarMinButton, 0);
|
|---|
| 566 | buttonRect |= q->style()->subControlRect(QStyle::CC_TitleBar, &bar, QStyle::SC_TitleBarMaxButton, 0);
|
|---|
| 567 | #endif
|
|---|
| 568 | if (buttonRect.contains(pos.toPoint()))
|
|---|
| 569 | buttonMouseOver = true;
|
|---|
| 570 | event->ignore();
|
|---|
| 571 | break;
|
|---|
| 572 | default:
|
|---|
| 573 | needsSetCursorCall = false;
|
|---|
| 574 | event->ignore();
|
|---|
| 575 | }
|
|---|
| 576 | #ifndef QT_NO_CURSOR
|
|---|
| 577 | if (needsSetCursorCall)
|
|---|
| 578 | q->setCursor(cursorShape);
|
|---|
| 579 | #endif
|
|---|
| 580 | // update buttons if we hover over them
|
|---|
| 581 | hoveredSubControl = q->style()->hitTestComplexControl(QStyle::CC_TitleBar, &bar, pos.toPoint(), 0);
|
|---|
| 582 | if (hoveredSubControl != QStyle::SC_TitleBarCloseButton)
|
|---|
| 583 | hoveredSubControl = QStyle::SC_TitleBarLabel;
|
|---|
| 584 |
|
|---|
| 585 | if (buttonMouseOver != wasMouseOver) {
|
|---|
| 586 | if (!oldButtonRect.isNull())
|
|---|
| 587 | q->update(QRectF(oldButtonRect).translated(q->windowFrameRect().topLeft()));
|
|---|
| 588 | if (!buttonRect.isNull())
|
|---|
| 589 | q->update(QRectF(buttonRect).translated(q->windowFrameRect().topLeft()));
|
|---|
| 590 | }
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | void QGraphicsWidgetPrivate::windowFrameHoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|---|
| 594 | {
|
|---|
| 595 | Q_UNUSED(event);
|
|---|
| 596 | Q_Q(QGraphicsWidget);
|
|---|
| 597 | if (hasDecoration()) {
|
|---|
| 598 | // ### restore the cursor, don't override it
|
|---|
| 599 | #ifndef QT_NO_CURSOR
|
|---|
| 600 | q->unsetCursor();
|
|---|
| 601 | #endif
|
|---|
| 602 |
|
|---|
| 603 | bool needsUpdate = false;
|
|---|
| 604 | if (hoveredSubControl == QStyle::SC_TitleBarCloseButton || buttonMouseOver)
|
|---|
| 605 | needsUpdate = true;
|
|---|
| 606 |
|
|---|
| 607 | // update the hover state (of buttons etc...)
|
|---|
| 608 | hoveredSubControl = QStyle::SC_None;
|
|---|
| 609 | buttonMouseOver = false;
|
|---|
| 610 | buttonRect = QRect();
|
|---|
| 611 | if (needsUpdate)
|
|---|
| 612 | q->update(buttonRect);
|
|---|
| 613 | }
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | bool QGraphicsWidgetPrivate::hasDecoration() const
|
|---|
| 617 | {
|
|---|
| 618 | return (windowFlags & Qt::Window) && (windowFlags & Qt::WindowTitleHint);
|
|---|
| 619 | }
|
|---|
| 620 |
|
|---|
| 621 | /*!
|
|---|
| 622 | \internal
|
|---|
| 623 | */
|
|---|
| 624 | void QGraphicsWidgetPrivate::setFocusWidget()
|
|---|
| 625 | {
|
|---|
| 626 | // Update focus child chain.
|
|---|
| 627 | QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(q_ptr);
|
|---|
| 628 | QGraphicsWidget *parent = widget;
|
|---|
| 629 | bool hidden = !visible;
|
|---|
| 630 | do {
|
|---|
| 631 | parent->d_func()->focusChild = widget;
|
|---|
| 632 | } while (!parent->isWindow() && (parent = parent->parentWidget()) && (!hidden || !parent->d_func()->visible));
|
|---|
| 633 | }
|
|---|
| 634 |
|
|---|
| 635 | /*!
|
|---|
| 636 | \internal
|
|---|
| 637 | */
|
|---|
| 638 | void QGraphicsWidgetPrivate::clearFocusWidget()
|
|---|
| 639 | {
|
|---|
| 640 | // Reset focus child chain.
|
|---|
| 641 | QGraphicsWidget *parent = static_cast<QGraphicsWidget *>(q_ptr);
|
|---|
| 642 | do {
|
|---|
| 643 | if (parent->d_func()->focusChild != q_ptr)
|
|---|
| 644 | break;
|
|---|
| 645 | parent->d_func()->focusChild = 0;
|
|---|
| 646 | } while (!parent->isWindow() && (parent = parent->parentWidget()));
|
|---|
| 647 | }
|
|---|
| 648 |
|
|---|
| 649 | /**
|
|---|
| 650 | * is called after a reparent has taken place to fix up the focus chain(s)
|
|---|
| 651 | */
|
|---|
| 652 | void QGraphicsWidgetPrivate::fixFocusChainBeforeReparenting(QGraphicsWidget *newParent, QGraphicsScene *newScene)
|
|---|
| 653 | {
|
|---|
| 654 | Q_Q(QGraphicsWidget);
|
|---|
| 655 |
|
|---|
| 656 | Q_ASSERT(focusNext && focusPrev);
|
|---|
| 657 |
|
|---|
| 658 | QGraphicsWidget *n = q; //last one in 'new' list
|
|---|
| 659 | QGraphicsWidget *o = 0; //last one in 'old' list
|
|---|
| 660 |
|
|---|
| 661 | QGraphicsWidget *w = focusNext;
|
|---|
| 662 |
|
|---|
| 663 | QGraphicsWidget *firstOld = 0;
|
|---|
| 664 | bool wasPreviousNew = true;
|
|---|
| 665 |
|
|---|
| 666 | if (focusChild) {
|
|---|
| 667 | // Ensure that the current focus child doesn't leave pointers around
|
|---|
| 668 | // before reparenting.
|
|---|
| 669 | focusChild->clearFocus();
|
|---|
| 670 | }
|
|---|
| 671 |
|
|---|
| 672 | while (w != q) {
|
|---|
| 673 | bool isCurrentNew = q->isAncestorOf(w);
|
|---|
| 674 | if (isCurrentNew) {
|
|---|
| 675 | if (!wasPreviousNew) {
|
|---|
| 676 | n->d_func()->focusNext = w;
|
|---|
| 677 | w->d_func()->focusPrev = n;
|
|---|
| 678 | }
|
|---|
| 679 | n = w;
|
|---|
| 680 | } else /*if (!isCurrentNew)*/ {
|
|---|
| 681 | if (wasPreviousNew) {
|
|---|
| 682 | if (o) {
|
|---|
| 683 | o->d_func()->focusNext = w;
|
|---|
| 684 | w->d_func()->focusPrev = o;
|
|---|
| 685 | } else {
|
|---|
| 686 | firstOld = w;
|
|---|
| 687 | }
|
|---|
| 688 | }
|
|---|
| 689 | o = w;
|
|---|
| 690 | }
|
|---|
| 691 | w = w->d_func()->focusNext;
|
|---|
| 692 | wasPreviousNew = isCurrentNew;
|
|---|
| 693 | }
|
|---|
| 694 |
|
|---|
| 695 | // repair the 'old' chain
|
|---|
| 696 | if (firstOld) {
|
|---|
| 697 | o->d_func()->focusNext = firstOld;
|
|---|
| 698 | firstOld->d_func()->focusPrev = o;
|
|---|
| 699 | }
|
|---|
| 700 |
|
|---|
| 701 | // update tabFocusFirst for oldScene if the item is going to be removed from oldScene
|
|---|
| 702 | if (newParent)
|
|---|
| 703 | newScene = newParent->scene();
|
|---|
| 704 | QGraphicsScene *oldScene = q->scene();
|
|---|
| 705 | if (oldScene && newScene != oldScene)
|
|---|
| 706 | oldScene->d_func()->tabFocusFirst = firstOld;
|
|---|
| 707 |
|
|---|
| 708 | QGraphicsItem *topLevelItem = newParent ? newParent->topLevelItem() : 0;
|
|---|
| 709 | QGraphicsWidget *topLevel = 0;
|
|---|
| 710 | if (topLevelItem && topLevelItem->isWidget())
|
|---|
| 711 | topLevel = static_cast<QGraphicsWidget *>(topLevelItem);
|
|---|
| 712 |
|
|---|
| 713 | if (topLevel && newParent) {
|
|---|
| 714 | QGraphicsWidget *last = topLevel->d_func()->focusPrev;
|
|---|
| 715 | // link last with new chain
|
|---|
| 716 | last->d_func()->focusNext = q;
|
|---|
| 717 | focusPrev = last;
|
|---|
| 718 |
|
|---|
| 719 | // link last in chain with
|
|---|
| 720 | topLevel->d_func()->focusPrev = n;
|
|---|
| 721 | n->d_func()->focusNext = topLevel;
|
|---|
| 722 | } else {
|
|---|
| 723 | // q is the start of the focus chain
|
|---|
| 724 | n->d_func()->focusNext = q;
|
|---|
| 725 | focusPrev = n;
|
|---|
| 726 | }
|
|---|
| 727 |
|
|---|
| 728 | }
|
|---|
| 729 |
|
|---|
| 730 | void QGraphicsWidgetPrivate::setLayout_helper(QGraphicsLayout *l)
|
|---|
| 731 | {
|
|---|
| 732 | delete (this->layout);
|
|---|
| 733 | layout = l;
|
|---|
| 734 | if (!l) {
|
|---|
| 735 | Q_Q(QGraphicsWidget);
|
|---|
| 736 | q->updateGeometry();
|
|---|
| 737 | }
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|
| 740 | QT_END_NAMESPACE
|
|---|
| 741 |
|
|---|
| 742 | #endif //QT_NO_GRAPHICSVIEW
|
|---|