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

« back to all changes in this revision

Viewing changes to examples/tools/qtdemo/displaywidget.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) 2005-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes 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 <QtGui>
 
30
 
 
31
#include "displayshape.h"
 
32
#include "displaywidget.h"
 
33
 
 
34
DisplayWidget::DisplayWidget(QWidget *parent)
 
35
    : QWidget(parent)
 
36
{
 
37
    empty = true;
 
38
    emptying = false;
 
39
 
 
40
    timer = new QTimer(this);
 
41
    connect(timer, SIGNAL(timeout()), this, SLOT(updateShapes()));
 
42
    timer->setSingleShot(false);
 
43
    enableUpdates();
 
44
 
 
45
    setBackgroundRole(QPalette::Base);
 
46
    setMouseTracking(true);
 
47
}
 
48
 
 
49
void DisplayWidget::appendShape(DisplayShape *shape)
 
50
{
 
51
    shapes.append(shape);
 
52
    empty = false;
 
53
    enableUpdates();
 
54
}
 
55
 
 
56
void DisplayWidget::insertShape(int position, DisplayShape *shape)
 
57
{
 
58
    shapes.insert(position, shape);
 
59
    empty = false;
 
60
    enableUpdates();
 
61
}
 
62
 
 
63
QSize DisplayWidget::minimumSizeHint() const
 
64
{
 
65
    return QSize(800, 600);
 
66
}
 
67
 
 
68
void DisplayWidget::mouseMoveEvent(QMouseEvent *event)
 
69
{
 
70
    if (emptying)
 
71
        return;
 
72
 
 
73
    bool updated = false;
 
74
 
 
75
    foreach (DisplayShape *shape, shapes) {
 
76
        if (shape->isInteractive() && shape->rect().contains(event->pos())
 
77
            && !shape->contains("fade")) {
 
78
            shape->setMetaData("highlight", true);
 
79
            updated = true;
 
80
        } else if (shape->isInteractive() && shape->contains("highlight")) {
 
81
            shape->setMetaData("highlight", false);
 
82
            updated = true;
 
83
        }
 
84
    }
 
85
 
 
86
    if (updated)
 
87
        enableUpdates();
 
88
}
 
89
 
 
90
void DisplayWidget::mousePressEvent(QMouseEvent *event)
 
91
{
 
92
    if (event->button() != Qt::LeftButton)
 
93
        return;
 
94
 
 
95
    if (emptying)
 
96
        return;
 
97
 
 
98
    foreach (DisplayShape *shape, shapes) {
 
99
        if (shape->rect().contains(event->pos()) && !shape->contains("fade")) {
 
100
            if (shape->contains("action"))
 
101
                emit actionRequested(shape->metaData("action").toString());
 
102
            else if (shape->contains("category"))
 
103
                emit categoryRequested(shape->metaData("category").toString());
 
104
            else if (shape->contains("example"))
 
105
                emit exampleRequested(shape->metaData("example").toString());
 
106
            else if (shape->contains("documentation")) {
 
107
                emit documentationRequested(
 
108
                    shape->metaData("documentation").toString());
 
109
                shape->setMetaData("highlight", false);
 
110
                enableUpdates();
 
111
            } else if (shape->contains("launch")) {
 
112
                emit launchRequested(shape->metaData("launch").toString());
 
113
                shape->setMetaData("fade", -5);
 
114
                enableUpdates();
 
115
            }
 
116
        }
 
117
    }
 
118
}
 
119
 
 
120
void DisplayWidget::paintEvent(QPaintEvent *event)
 
121
{
 
122
    QPainter painter;
 
123
    painter.begin(this);
 
124
    painter.fillRect(event->rect(), Qt::white);
 
125
    foreach (DisplayShape *shape, shapes)
 
126
        shape->paint(&painter);
 
127
    painter.end();
 
128
}
 
129
 
 
130
void DisplayWidget::reset()
 
131
{
 
132
    if (emptying)
 
133
        return;
 
134
 
 
135
    if (shapes.size() == 0) {
 
136
        empty = true;
 
137
        timer->stop();
 
138
        emit displayEmpty();    // Note: synchronous signal
 
139
    } else {
 
140
        enableUpdates();
 
141
        emptying = true;
 
142
        empty = false;
 
143
        foreach (DisplayShape *shape, shapes) {
 
144
            shape->setMetaData("fade", -15);
 
145
            shape->setMetaData("fade minimum", 0);
 
146
        }
 
147
    }
 
148
}
 
149
 
 
150
DisplayShape *DisplayWidget::shape(int index) const
 
151
{
 
152
    return shapes.value(index);
 
153
}
 
154
 
 
155
int DisplayWidget::shapesCount() const
 
156
{
 
157
    return shapes.size();
 
158
}
 
159
 
 
160
void DisplayWidget::enableUpdates()
 
161
{
 
162
    if (!timer->isActive())
 
163
        timer->start(50);
 
164
}
 
165
 
 
166
void DisplayWidget::updateShapes()
 
167
{
 
168
    QVector<DisplayShape*> discard;
 
169
 
 
170
    int updated = 0;
 
171
 
 
172
    foreach (DisplayShape *shape, shapes) {
 
173
        QRect oldRect = shape->rect().toRect().adjusted(-1,-1,1,1);
 
174
        if (shape->animate()) {
 
175
 
 
176
            update(oldRect);
 
177
            QRect newRect = shape->rect().toRect().adjusted(-1,-1,1,1);
 
178
            ++updated;
 
179
 
 
180
            if (shape->contains("destroy")) {
 
181
                discard.append(shape);
 
182
            } else {
 
183
                update(newRect);
 
184
            }
 
185
        }
 
186
    }
 
187
 
 
188
    if (updated == 0)
 
189
        timer->stop();
 
190
 
 
191
    foreach (DisplayShape *shape, discard) {
 
192
        shapes.removeAll(shape);
 
193
        delete shape;
 
194
    }
 
195
 
 
196
    if (shapes.size() == 0 && !empty) {
 
197
        empty = true;
 
198
        emptying = false;
 
199
        timer->stop();
 
200
        emit displayEmpty();    // Note: synchronous signal
 
201
    }
 
202
}