~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/widgets/qfocusframe.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the widgets module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qfocusframe.h"
 
30
#include "qstyle.h"
 
31
#include "qbitmap.h"
 
32
#include "qstylepainter.h"
 
33
#include "qstyleoption.h"
 
34
#include <private/qwidget_p.h>
 
35
 
 
36
class QFocusFramePrivate : public QWidgetPrivate
 
37
{
 
38
    Q_DECLARE_PUBLIC(QFocusFrame)
 
39
    QWidget *widget;
 
40
 
 
41
public:
 
42
    QFocusFramePrivate() {
 
43
        widget = 0;
 
44
        sendChildEvents = false;
 
45
    }
 
46
    void updateSize();
 
47
    void update();
 
48
    QStyleOption getStyleOption() const;
 
49
};
 
50
 
 
51
void QFocusFramePrivate::update()
 
52
{
 
53
    Q_Q(QFocusFrame);
 
54
    q->setParent(widget->parentWidget());
 
55
    updateSize();
 
56
    if (q->parentWidget()->rect().contains(q->geometry())) {
 
57
        q->stackUnder(widget);
 
58
        q->show();
 
59
    } else {
 
60
        q->hide();
 
61
    }
 
62
}
 
63
 
 
64
void QFocusFramePrivate::updateSize()
 
65
{
 
66
    Q_Q(QFocusFrame);
 
67
    int vmargin = q->style()->pixelMetric(QStyle::PM_FocusFrameVMargin),
 
68
        hmargin = q->style()->pixelMetric(QStyle::PM_FocusFrameHMargin);
 
69
    QRect geom(widget->x()-hmargin, widget->y()-vmargin,
 
70
               widget->width()+(hmargin*2), widget->height()+(vmargin*2));
 
71
    if(q->geometry() == geom)
 
72
        return;
 
73
    q->setGeometry(geom);
 
74
    QStyleHintReturnMask mask;
 
75
    QStyleOption opt = getStyleOption();
 
76
    if (q->style()->styleHint(QStyle::SH_FocusFrame_Mask, &opt, q, &mask))
 
77
        q->setMask(mask.region);
 
78
}
 
79
 
 
80
QStyleOption QFocusFramePrivate::getStyleOption() const
 
81
{
 
82
    Q_Q(const QFocusFrame);
 
83
    QStyleOption opt;
 
84
    opt.init(q);
 
85
    opt.rect = q->rect();
 
86
    return opt;
 
87
}
 
88
 
 
89
/*!
 
90
    \class QFocusFrame
 
91
    \brief The QFocusFrame widget provides a focus frame which can be
 
92
    outside of a widget's normal paintable area.
 
93
 
 
94
    \ingroup basic
 
95
    \mainclass
 
96
 
 
97
    Normally an application will not need to create its own
 
98
    QFocusFrame as QStyle will handle this detail for
 
99
    you. A style writer can optionally use a QFocusFrame to have a
 
100
    focus area outside of the widget's paintable geometry. In this way
 
101
    space need not be reserved for the widget to have focus but only
 
102
    set on a QWidget with QFocusFrame::setWidget. It is, however,
 
103
    legal to create your own QFocusFrame on a custom widget and set
 
104
    its geometry manually via QWidget::setGeometry however you will
 
105
    not get auto-placement when the focused widget changes size or
 
106
    placement.
 
107
*/
 
108
 
 
109
/*!
 
110
    Constructs a QFocusFrame.
 
111
 
 
112
    The focus frame will not monitor \a parent for updates but rather
 
113
    can be placed manually or by using QFocusFrame::setWidget. A
 
114
    QFocusFrame sets Qt::WA_NoChildEventsForParent attribute; as a
 
115
    result the parent will not receive a QEvent::ChildInserted event,
 
116
    this will make it possible to manually set the geometry of the
 
117
    QFocusFrame inside of a QSplitter or other child event monitoring
 
118
    widget.
 
119
 
 
120
    \sa QFocusFrame::setWidget()
 
121
*/
 
122
 
 
123
QFocusFrame::QFocusFrame(QWidget *parent)
 
124
    : QWidget(*new QFocusFramePrivate, parent, 0)
 
125
{
 
126
    setAttribute(Qt::WA_TransparentForMouseEvents);
 
127
    setFocusPolicy(Qt::NoFocus);
 
128
    setAttribute(Qt::WA_NoSystemBackground, true);
 
129
    setAttribute(Qt::WA_NoChildEventsForParent, true);
 
130
}
 
131
 
 
132
/*!
 
133
    Destructor.
 
134
*/
 
135
 
 
136
QFocusFrame::~QFocusFrame()
 
137
{
 
138
}
 
139
 
 
140
/*!
 
141
  QFocusFrame will track changes to \a widget and resize itself automatically.
 
142
  If the monitored widget's parent changes, QFocusFrame will follow the widget
 
143
  and place itself around the widget automatically. If the monitored widget is deleted,
 
144
  QFocusFrame will set it to zero.
 
145
 
 
146
  \sa QFocusFrame::widget()
 
147
*/
 
148
 
 
149
void
 
150
QFocusFrame::setWidget(QWidget *widget)
 
151
{
 
152
    Q_D(QFocusFrame);
 
153
    if(widget == d->widget)
 
154
        return;
 
155
 
 
156
    if(d->widget)
 
157
        d->widget->removeEventFilter(this);
 
158
    if(widget && !widget->isWindow() && widget->parentWidget()->windowType() != Qt::SubWindow) {
 
159
        d->widget = widget;
 
160
        widget->installEventFilter(this);
 
161
        d->update();
 
162
    } else {
 
163
        d->widget = 0;
 
164
        hide();
 
165
    }
 
166
}
 
167
 
 
168
/*!
 
169
  Returns the currently monitored widget for automatically resize and
 
170
  update.
 
171
 
 
172
   \sa QFocusFrame::setWidget()
 
173
*/
 
174
 
 
175
QWidget *
 
176
QFocusFrame::widget() const
 
177
{
 
178
    Q_D(const QFocusFrame);
 
179
    return d->widget;
 
180
}
 
181
 
 
182
 
 
183
/*! \reimp */
 
184
void
 
185
QFocusFrame::paintEvent(QPaintEvent *)
 
186
{
 
187
    Q_D(QFocusFrame);
 
188
    QStylePainter p(this);
 
189
    p.drawControl(QStyle::CE_FocusFrame, d->getStyleOption());
 
190
}
 
191
 
 
192
 
 
193
/*! \reimp */
 
194
bool
 
195
QFocusFrame::eventFilter(QObject *o, QEvent *e)
 
196
{
 
197
    Q_D(QFocusFrame);
 
198
    if(o == d->widget) {
 
199
        switch(e->type()) {
 
200
        case QEvent::Move:
 
201
        case QEvent::Resize:
 
202
            d->updateSize();
 
203
            break;
 
204
        case QEvent::Hide:
 
205
        case QEvent::StyleChange:
 
206
            hide();
 
207
            break;
 
208
        case QEvent::ParentChange:
 
209
            d->update();
 
210
            break;
 
211
        case QEvent::Show:
 
212
            d->update();
 
213
            show();
 
214
            break;
 
215
        case QEvent::PaletteChange:
 
216
            setPalette(d->widget->palette());
 
217
            break;
 
218
        case QEvent::ZOrderChange:
 
219
            stackUnder(d->widget);
 
220
            break;
 
221
        case QEvent::Destroy:
 
222
            setWidget(0);
 
223
            break;
 
224
        default:
 
225
            break;
 
226
        }
 
227
    }
 
228
    return false;
 
229
}