| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qbitmap.h"
|
|---|
| 43 | #include "qevent.h"
|
|---|
| 44 | #include "qstylepainter.h"
|
|---|
| 45 | #include "qrubberband.h"
|
|---|
| 46 | #include "qtimer.h"
|
|---|
| 47 |
|
|---|
| 48 | #ifndef QT_NO_RUBBERBAND
|
|---|
| 49 |
|
|---|
| 50 | #include "qstyle.h"
|
|---|
| 51 | #include "qstyleoption.h"
|
|---|
| 52 | #ifdef Q_WS_MAC
|
|---|
| 53 | # include <private/qt_mac_p.h>
|
|---|
| 54 | # include <private/qt_cocoa_helpers_mac_p.h>
|
|---|
| 55 | #endif
|
|---|
| 56 |
|
|---|
| 57 | #include <qdebug.h>
|
|---|
| 58 |
|
|---|
| 59 | #include <private/qwidget_p.h>
|
|---|
| 60 |
|
|---|
| 61 | QT_BEGIN_NAMESPACE
|
|---|
| 62 |
|
|---|
| 63 | //### a rubberband window type would be a more elegant solution
|
|---|
| 64 | #define RUBBERBAND_WINDOW_TYPE Qt::ToolTip
|
|---|
| 65 |
|
|---|
| 66 | class QRubberBandPrivate : public QWidgetPrivate
|
|---|
| 67 | {
|
|---|
| 68 | Q_DECLARE_PUBLIC(QRubberBand)
|
|---|
| 69 | public:
|
|---|
| 70 | QRect rect;
|
|---|
| 71 | QRubberBand::Shape shape;
|
|---|
| 72 | QRegion clipping;
|
|---|
| 73 | void updateMask();
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | /*!
|
|---|
| 77 | Initialize \a option with the values from this QRubberBand. This method
|
|---|
| 78 | is useful for subclasses when they need a QStyleOptionRubberBand, but don't want
|
|---|
| 79 | to fill in all the information themselves.
|
|---|
| 80 |
|
|---|
| 81 | \sa QStyleOption::initFrom()
|
|---|
| 82 | */
|
|---|
| 83 | void QRubberBand::initStyleOption(QStyleOptionRubberBand *option) const
|
|---|
| 84 | {
|
|---|
| 85 | if (!option)
|
|---|
| 86 | return;
|
|---|
| 87 | option->initFrom(this);
|
|---|
| 88 | option->shape = d_func()->shape;
|
|---|
| 89 | #ifndef Q_WS_MAC
|
|---|
| 90 | option->opaque = true;
|
|---|
| 91 | #else
|
|---|
| 92 | option->opaque = windowFlags() & RUBBERBAND_WINDOW_TYPE;
|
|---|
| 93 | #endif
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /*!
|
|---|
| 97 | \class QRubberBand
|
|---|
| 98 | \brief The QRubberBand class provides a rectangle or line that can
|
|---|
| 99 | indicate a selection or a boundary.
|
|---|
| 100 |
|
|---|
| 101 | A rubber band is often used to show a new bounding area (as in a
|
|---|
| 102 | QSplitter or a QDockWidget that is undocking). Historically this has
|
|---|
| 103 | been implemented using a QPainter and XOR, but this approach
|
|---|
| 104 | doesn't always work properly since rendering can happen in the
|
|---|
| 105 | window below the rubber band, but before the rubber band has been
|
|---|
| 106 | "erased".
|
|---|
| 107 |
|
|---|
| 108 | You can create a QRubberBand whenever you need to render a rubber band
|
|---|
| 109 | around a given area (or to represent a single line), then call
|
|---|
| 110 | setGeometry(), move() or resize() to position and size it. A common
|
|---|
| 111 | pattern is to do this in conjunction with mouse events. For example:
|
|---|
| 112 |
|
|---|
| 113 | \snippet doc/src/snippets/code/src_gui_widgets_qrubberband.cpp 0
|
|---|
| 114 |
|
|---|
| 115 | If you pass a parent to QRubberBand's constructor, the rubber band will
|
|---|
| 116 | display only inside its parent, but stays on top of other child widgets.
|
|---|
| 117 | If no parent is passed, QRubberBand will act as a top-level widget.
|
|---|
| 118 |
|
|---|
| 119 | Call show() to make the rubber band visible; also when the
|
|---|
| 120 | rubber band is not a top-level. Hiding or destroying
|
|---|
| 121 | the widget will make the rubber band disappear. The rubber band
|
|---|
| 122 | can be a \l Rectangle or a \l Line (vertical or horizontal),
|
|---|
| 123 | depending on the shape() it was given when constructed.
|
|---|
| 124 | */
|
|---|
| 125 |
|
|---|
| 126 | // ### DOC: How about some nice convenience constructors?
|
|---|
| 127 | //QRubberBand::QRubberBand(QRubberBand::Type t, const QRect &rect, QWidget *p)
|
|---|
| 128 | //QRubberBand::QRubberBand(QRubberBand::Type t, int x, int y, int w, int h, QWidget *p)
|
|---|
| 129 |
|
|---|
| 130 | /*!
|
|---|
| 131 | Constructs a rubber band of shape \a s, with parent \a p.
|
|---|
| 132 |
|
|---|
| 133 | By default a rectangular rubber band (\a s is \c Rectangle) will
|
|---|
| 134 | use a mask, so that a small border of the rectangle is all
|
|---|
| 135 | that is visible. Some styles (e.g., native Mac OS X) will
|
|---|
| 136 | change this and call QWidget::setWindowOpacity() to make a
|
|---|
| 137 | semi-transparent filled selection rectangle.
|
|---|
| 138 | */
|
|---|
| 139 | QRubberBand::QRubberBand(Shape s, QWidget *p)
|
|---|
| 140 | : QWidget(*new QRubberBandPrivate, p, (p && p->windowType() != Qt::Desktop) ? Qt::Widget : RUBBERBAND_WINDOW_TYPE)
|
|---|
| 141 | {
|
|---|
| 142 | Q_D(QRubberBand);
|
|---|
| 143 | d->shape = s;
|
|---|
| 144 | setAttribute(Qt::WA_TransparentForMouseEvents);
|
|---|
| 145 | #ifndef Q_WS_WIN
|
|---|
| 146 | setAttribute(Qt::WA_NoSystemBackground);
|
|---|
| 147 | #endif //Q_WS_WIN
|
|---|
| 148 | setAttribute(Qt::WA_WState_ExplicitShowHide);
|
|---|
| 149 | setVisible(false);
|
|---|
|
|---|