-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.cpp
257 lines (221 loc) · 8.08 KB
/
view.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/****************************************************************************
**
** Copyright (C) 2009 TECHNOGERMA Systems France and/or its subsidiary(-ies).
** Contact: Technogerma Systems France Information ([email protected])
**
** This file is part of the GICS library.
**
** Commercial Usage
** Licensees holding valid GICS Commercial licenses may use this file in
** accordance with the GICS Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and TECHNOGERMA Systems France.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL3.txt included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at [email protected].
**
****************************************************************************/
/****************************************************************************
**
** Copyright (C) 2009 caiwenfeng at suda dot edu dot cn.
**
****************************************************************************/
#include "view.hpp"
#include <gics/picture.hpp>
#include <gics/instrument.hpp>
#include <gics/math.hpp>
#include <QGraphicsView>
#include <QGraphicsLayout>
#include <QMouseEvent>
#include <QGLWidget>
#include <QTransform>
#include <QTimeLine>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
//-------------------------------------------------------------------------------------------------
View::View(QWidget* parent)
: QGraphicsView(parent)
, m_front(0)
, m_back(0)
{
// View setup
setStyleSheet("background: transparent");
setFrameShape(QFrame::NoFrame);
setRenderHint(QPainter::Antialiasing);
setRenderHint(QPainter::TextAntialiasing);
setRenderHint(QPainter::HighQualityAntialiasing);
setCacheMode(CacheBackground);
setInteractive(true);
// Scene setup
setScene(&m_scene);
// Create the capture items
m_frontCapture = new QGraphicsPixmapItem;
m_backCapture = new QGraphicsPixmapItem;
// Timeline setup
m_timeline = new QTimeLine(1000, this);
m_timeline->setUpdateInterval(10);
// Connect signals
connect(m_timeline, SIGNAL(valueChanged(qreal)), this, SLOT(handleTimelineUpdated(qreal)));
connect(m_timeline, SIGNAL(finished()), this, SLOT(handleTimelineFinished()));
}
//-------------------------------------------------------------------------------------------------
void View::setFrontInstrument(gics::Instrument* instrument)
{
if (m_front)
delete m_front;
m_front = instrument;
if (m_front)
{
if (m_timeline->state() != QTimeLine::Running)
{
m_scene.addItem(m_front);
m_front->setVisible(m_timeline->direction() == QTimeLine::Forward);
}
}
}
//-------------------------------------------------------------------------------------------------
gics::Instrument* View::frontInstrument() const
{
return m_front;
}
//-------------------------------------------------------------------------------------------------
void View::setBackInstrument(gics::Instrument* instrument)
{
if (m_back)
delete m_back;
m_back = instrument;
if (m_back)
{
if (m_timeline->state() != QTimeLine::Running)
{
m_scene.addItem(m_back);
m_back->setVisible(m_timeline->direction() == QTimeLine::Backward);
}
}
}
//-------------------------------------------------------------------------------------------------
gics::Instrument* View::backInstrument() const
{
return m_back;
}
//-------------------------------------------------------------------------------------------------
void View::flip()
{
if (m_front && m_back && (m_timeline->state() != QTimeLine::Running))
{
// Take a capture of the front and back instruments
m_frontCapture->setPixmap(capture(m_front));
m_backCapture->setPixmap(capture(m_back));
// Replace the front and back instruments with their capture
m_scene.removeItem(m_front);
m_scene.removeItem(m_back);
m_scene.addItem(m_frontCapture);
m_scene.addItem(m_backCapture);
// Start the timeline
m_timeline->start();
}
}
//-------------------------------------------------------------------------------------------------
void View::useOpenGL(bool use)
{
if (use)
{
setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
setStyleSheet("background: black");
}
else
{
setViewport(new QWidget);
setStyleSheet("background: transparent");
}
}
//-------------------------------------------------------------------------------------------------
void View::resizeEvent(QResizeEvent* event)
{
// Resize the view
QGraphicsView::resizeEvent(event);
// Resize the widgets
if (m_front) m_front->resize(size());
if (m_back) m_back->resize(size());
// Resize the scene
m_scene.setSceneRect(0, 0, size().width(), size().height());
}
//-------------------------------------------------------------------------------------------------
void View::mouseMoveEvent(QMouseEvent* event)
{
QGraphicsView::mouseMoveEvent(event);
// This is to workaround Qt not forwarding properly the "accepted" state of the mouse event sent to the scene
if (!m_scene.mouseGrabberItem())
event->ignore();
}
//-------------------------------------------------------------------------------------------------
QPixmap View::capture(gics::Instrument* instrument)
{
// Show only the item in the scene
m_front->setVisible(false);
m_back->setVisible(false);
instrument->setVisible(true);
// Prepare the target pixmap
QPixmap pixmap(size());
pixmap.fill(Qt::transparent);
// Render the item
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::TextAntialiasing);
m_scene.render(&painter);
painter.end();
return pixmap;
}
//-------------------------------------------------------------------------------------------------
void View::handleTimelineUpdated(qreal value)
{
// Compute the transform to apply
QPointF center(width() / 2.f, height() / 2.f);
qreal angle = value < 0.5 ? (180 * value) : (180 * value - 180);
qreal sizeFactor = 1 - sin(GICS_PI * value) * 0.4;
QTransform transform = QTransform()
.translate(center.x(), center.y())
.rotate(angle, Qt::XAxis)
.scale(sizeFactor, sizeFactor)
.translate(-center.x(), -center.y());
// Update the instruments
if (value < 0.5)
{
m_backCapture->setVisible(false);
m_frontCapture->setVisible(true);
m_frontCapture->setTransform(transform);
}
else
{
m_frontCapture->setVisible(false);
m_backCapture->setVisible(true);
m_backCapture->setTransform(transform);
}
// Update the mask of the parent window to match the transformed view :)
QRegion region = transform.map(QRegion(rect()));
if (region.boundingRect().isValid())
parentWidget()->setMask(region);
}
//-------------------------------------------------------------------------------------------------
void View::handleTimelineFinished()
{
// Reset the parent window's mask
parentWidget()->setMask(QRegion());
// Flip the animation's direction
m_timeline->toggleDirection();
// Put the real instruments back into the scene
m_scene.removeItem(m_frontCapture);
m_scene.removeItem(m_backCapture);
m_scene.addItem(m_front);
m_scene.addItem(m_back);
m_front->setVisible(m_timeline->direction() == QTimeLine::Forward);
m_back->setVisible(m_timeline->direction() == QTimeLine::Backward);
}