| author | eckhart.koppen@nokia.com |
| Wed, 31 Mar 2010 11:06:36 +0300 | |
| changeset 7 | f7bc934e204c |
| parent 0 | 1918ee327afb |
| permissions | -rw-r--r-- |
| 0 | 1 |
/**************************************************************************** |
2 |
** |
|
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
| 0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the Qt Designer of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <QtCore> |
|
43 |
#include <QtGui> |
|
44 |
#include <QtOpenGL> |
|
45 |
||
46 |
#include "abstractformeditor.h" |
|
47 |
#include "abstractmetadatabase.h" |
|
48 |
#include "abstractformwindow.h" |
|
49 |
#include "view3d.h" |
|
50 |
||
51 |
#define SELECTION_BUFSIZE 512 |
|
52 |
||
53 |
/******************************************************************************* |
|
54 |
** QView3DWidget |
|
55 |
*/ |
|
56 |
||
57 |
class QView3DWidget : public QGLWidget |
|
58 |
{
|
|
59 |
Q_OBJECT |
|
60 |
public: |
|
61 |
QView3DWidget(QWidget *parent); |
|
62 |
virtual void initializeGL(); |
|
63 |
virtual void resizeGL(int w, int h); |
|
64 |
virtual void paintGL(); |
|
65 |
void clear(); |
|
66 |
||
67 |
void addTexture(QWidget *w, const QPixmap &pm); |
|
68 |
||
69 |
void beginAddingWidgets(QWidget *form); |
|
70 |
void addWidget(int depth, QWidget *w); |
|
71 |
void endAddingWidgets(); |
|
72 |
||
73 |
QWidget *widgetAt(const QPoint &pos); |
|
74 |
||
75 |
signals: |
|
76 |
void updateForm(); |
|
77 |
||
78 |
protected: |
|
79 |
void mousePressEvent(QMouseEvent *); |
|
80 |
void mouseReleaseEvent(QMouseEvent *); |
|
81 |
void mouseMoveEvent(QMouseEvent *); |
|
82 |
void wheelEvent(QWheelEvent *); |
|
83 |
void keyReleaseEvent(QKeyEvent *); |
|
84 |
||
85 |
void contextMenuEvent(QContextMenuEvent *); |
|
86 |
||
87 |
private: |
|
88 |
QWidget *m_form; |
|
89 |
QPoint m_old_pos; |
|
90 |
bool m_layer_coloring; |
|
91 |
bool m_use_mipmaps; |
|
92 |
GLuint m_form_list_id; |
|
93 |
||
94 |
typedef QMap<QWidget*, GLuint> TextureMap; |
|
95 |
TextureMap m_texture_map; |
|
96 |
||
97 |
typedef QMap<GLuint, QWidget*> WidgetNameMap; |
|
98 |
GLuint m_next_widget_name; |
|
99 |
WidgetNameMap m_widget_name_map; |
|
100 |
}; |
|
101 |
||
102 |
QView3DWidget::QView3DWidget(QWidget *parent) |
|
103 |
: QGLWidget(parent) |
|
104 |
, m_layer_coloring(true) |
|
105 |
, m_form_list_id(0) |
|
106 |
, m_next_widget_name(0) |
|
107 |
{
|
|
108 |
setFocusPolicy(Qt::StrongFocus); |
|
109 |
} |
|
110 |
||
111 |
static int nearestSize(int v) |
|
112 |
{
|
|
113 |
int n = 0, last = 0; |
|
114 |
for (int s = 0; s < 32; ++s) {
|
|
115 |
if (((v>>s) & 1) == 1) {
|
|
116 |
++n; |
|
117 |
last = s; |
|
118 |
} |
|
119 |
} |
|
120 |
if (n > 1) |
|
121 |
return 1 << (last+1); |
|
122 |
return 1 << last; |
|
123 |
} |
|
124 |
||
125 |
// static int pm_cnt = 0; |
|
126 |
||
127 |
void QView3DWidget::addTexture(QWidget *w, const QPixmap &pm) |
|
128 |
{
|
|
129 |
int tx_w = nearestSize(pm.width()); |
|
130 |
int tx_h = nearestSize(pm.height()); |
|
131 |
||
132 |
QPixmap tmp(tx_w, tx_h); |
|
133 |
tmp.fill(QColor(0,0,0)); |
|
134 |
QPainter p(&tmp); |
|
135 |
p.drawPixmap(0, tx_h - pm.height(), pm); |
|
136 |
p.end(); |
|
137 |
QImage tex = tmp.toImage(); |
|
138 |
||
139 |
// QString file_name = QString("pixmapDump%1.png").arg(pm_cnt++);
|
|
140 |
// qDebug() << "grabWidget():" << file_name << tex.save(file_name, "PNG"); |
|
141 |
||
142 |
tex = QGLWidget::convertToGLFormat(tex); |
|
143 |
||
144 |
GLuint tx_id; |
|
145 |
glGenTextures(1, &tx_id); |
|
146 |
glBindTexture(GL_TEXTURE_2D, tx_id); |
|
147 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
148 |
if (m_use_mipmaps) {
|
|
149 |
//glHint(GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST); |
|
150 |
//glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE); |
|
151 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
|
152 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.f); |
|
153 |
} else {
|
|
154 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
155 |
} |
|
156 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, tex.width(), tex.height(), 0, GL_RGBA, |
|
157 |
GL_UNSIGNED_BYTE, tex.bits()); |
|
158 |
m_texture_map[w] = tx_id; |
|
159 |
} |
|
160 |
||
161 |
void QView3DWidget::addWidget(int depth, QWidget *widget) |
|
162 |
{
|
|
163 |
TextureMap::const_iterator it = m_texture_map.find(widget); |
|
164 |
Q_ASSERT(it != m_texture_map.end()); |
|
165 |
||
166 |
makeCurrent(); |
|
167 |
||
168 |
int w = m_form->size().width(); |
|
169 |
int h = m_form->size().height(); |
|
170 |
int max = qMax(w, h); |
|
171 |
QRect r = widget->rect(); |
|
172 |
QPoint pos = widget->mapToGlobal(QPoint(0, 0)); |
|
173 |
r.moveTopLeft(m_form->mapFromGlobal(pos)); |
|
174 |
||
175 |
float s = r.width()/float(nearestSize(r.width())); |
|
176 |
float t = r.height()/float(nearestSize(r.height())); |
|
177 |
||
178 |
if (m_layer_coloring) |
|
179 |
glColor4f(1.0 - depth/10.0, 1.0 - depth/10.0, 1.0, 1.0 - depth/20.0); |
|
180 |
else |
|
181 |
glColor4f(1.0, 1.0, 1.0, 1.0); |
|
182 |
||
183 |
glBindTexture(GL_TEXTURE_2D, *it); |
|
184 |
glBegin(GL_QUADS); |
|
185 |
glLoadName(m_next_widget_name); |
|
186 |
glTexCoord2f(0.0, 0.0); glVertex3f(r.left() - w/2, r.bottom() - h/2, depth*max/8); |
|
187 |
glTexCoord2f(s, 0.0); glVertex3f(r.right() - w/2, r.bottom()- h/2, depth*max/8); |
|
188 |
glTexCoord2f(s, t); glVertex3f(r.right() - w/2, r.top() - h/2, depth*max/8); |
|
189 |
glTexCoord2f(0.0, t); glVertex3f(r.left() - w/2, r.top() - h/2, depth*max/8); |
|
190 |
glEnd(); |
|
191 |
||
192 |
m_widget_name_map[m_next_widget_name++] = widget; |
|
193 |
} |
|
194 |
||
195 |
void QView3DWidget::clear() |
|
196 |
{
|
|
197 |
makeCurrent(); |
|
198 |
glDeleteLists(m_form_list_id, 1); |
|
199 |
foreach (GLuint id, m_texture_map) |
|
200 |
glDeleteTextures(1, &id); |
|
201 |
m_texture_map.clear(); |
|
202 |
m_widget_name_map.clear(); |
|
203 |
m_next_widget_name = 0; |
|
204 |
} |
|
205 |
||
206 |
void QView3DWidget::beginAddingWidgets(QWidget *form) |
|
207 |
{
|
|
208 |
makeCurrent(); |
|
209 |
m_form = form; |
|
210 |
m_form_list_id = glGenLists(1); |
|
211 |
glNewList(m_form_list_id, GL_COMPILE); |
|
212 |
glInitNames(); |
|
213 |
glPushName(-1); |
|
214 |
m_next_widget_name = 0; |
|
215 |
} |
|
216 |
||
217 |
void QView3DWidget::endAddingWidgets() |
|
218 |
{
|
|
219 |
makeCurrent(); |
|
220 |
glEndList(); |
|
221 |
} |
|
222 |
||
223 |
void QView3DWidget::initializeGL() |
|
224 |
{
|
|
225 |
glMatrixMode(GL_MODELVIEW); |
|
226 |
glLoadIdentity(); |
|
227 |
||
228 |
qglClearColor(palette().color(QPalette::Window).dark()); |
|
229 |
glColor3f (1.0, 1.0, 1.0); |
|
230 |
glEnable(GL_DEPTH_TEST); |
|
231 |
glDepthFunc(GL_LEQUAL); |
|
232 |
||
233 |
glShadeModel(GL_FLAT); |
|
234 |
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); |
|
235 |
QString extensions(reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS))); |
|
236 |
m_use_mipmaps = false;// extensions.contains("GL_SGIS_generate_mipmap");
|
|
237 |
} |
|
238 |
||
239 |
void QView3DWidget::resizeGL(int width, int height) |
|
240 |
{
|
|
241 |
glViewport(0, 0, width, height); |
|
242 |
glMatrixMode(GL_PROJECTION); |
|
243 |
glLoadIdentity(); |
|
244 |
glOrtho(-width/2, width/2, height/2, -height/2, -999999, 999999); |
|
245 |
} |
|
246 |
||
247 |
void QView3DWidget::paintGL() |
|
248 |
{
|
|
249 |
glColor4f(1.0, 1.0, 1.0, 1.0); |
|
250 |
glEnable(GL_TEXTURE_2D); |
|
251 |
glEnable(GL_BLEND); |
|
252 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
253 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
254 |
glCallList(m_form_list_id); |
|
255 |
||
256 |
glPushAttrib(GL_ENABLE_BIT); |
|
257 |
glDisable(GL_DEPTH_TEST); |
|
258 |
glDisable(GL_LIGHTING); |
|
259 |
glDisable(GL_TEXTURE_2D); |
|
260 |
glMatrixMode(GL_MODELVIEW); |
|
261 |
glPushMatrix(); |
|
262 |
glLoadIdentity(); |
|
263 |
glTranslatef(-width()/2, -height()/2, 0.0); |
|
264 |
||
265 |
QFontMetrics fm(font()); |
|
266 |
glColor4f(0.4, 0.4, 0.4, 0.7); |
|
267 |
glRecti(0, height() - fm.lineSpacing()*2.5, width(), height()); |
|
268 |
||
269 |
glColor3f(1.0, 1.0, 1.0); |
|
270 |
renderText(10, height() - fm.lineSpacing()*1.5, |
|
271 |
"Press and hold left/right mouse button = tilt the view."); |
|
272 |
renderText(10, height() - fm.lineSpacing()*0.5, |
|
273 |
"Mouse wheel = zoom. 't' = toggle layer coloring. 'r' = reset transform."); |
|
274 |
glPopMatrix(); |
|
275 |
glPopAttrib(); |
|
276 |
} |
|
277 |
||
278 |
QWidget *QView3DWidget::widgetAt(const QPoint &pos) |
|
279 |
{
|
|
280 |
makeCurrent(); |
|
281 |
GLuint selectBuf[SELECTION_BUFSIZE]; |
|
282 |
glSelectBuffer(SELECTION_BUFSIZE, selectBuf); |
|
283 |
glRenderMode (GL_SELECT); |
|
284 |
||
285 |
glMatrixMode(GL_PROJECTION); |
|
286 |
glPushMatrix(); |
|
287 |
glLoadIdentity(); |
|
288 |
||
289 |
glCallList(m_form_list_id); |
|
290 |
return 0; |
|
291 |
} |
|
292 |
||
293 |
void QView3DWidget::keyReleaseEvent(QKeyEvent *e) |
|
294 |
{
|
|
295 |
if (e->key() == Qt::Key_T) {
|
|
296 |
m_layer_coloring = !m_layer_coloring; |
|
297 |
emit updateForm(); |
|
298 |
} else if (e->key() == Qt::Key_R) {
|
|
299 |
makeCurrent(); |
|
300 |
glMatrixMode(GL_MODELVIEW); |
|
301 |
glLoadIdentity(); |
|
302 |
} |
|
303 |
||
304 |
updateGL(); |
|
305 |
} |
|
306 |
||
307 |
void QView3DWidget::mousePressEvent(QMouseEvent *e) |
|
308 |
{
|
|
309 |
m_old_pos = e->pos(); |
|
310 |
} |
|
311 |
||
312 |
void QView3DWidget::mouseReleaseEvent(QMouseEvent *e) |
|
313 |
{
|
|
314 |
m_old_pos = e->pos(); |
|
315 |
} |
|
316 |
||
317 |
void QView3DWidget::mouseMoveEvent(QMouseEvent *e) |
|
318 |
{
|
|
319 |
if (e->buttons() & (Qt::LeftButton | Qt::RightButton)) {
|
|
320 |
GLfloat rx = (GLfloat) (e->x() - m_old_pos.x()) / width(); |
|
321 |
GLfloat ry = (GLfloat) (e->y() - m_old_pos.y()) / height(); |
|
322 |
||
323 |
makeCurrent(); |
|
324 |
glMatrixMode(GL_MODELVIEW); |
|
325 |
if (e->buttons() & Qt::LeftButton) {
|
|
326 |
// Left button down - rotate around X and Y axes |
|
327 |
glRotatef(-180*ry, 1, 0, 0); |
|
328 |
glRotatef(180*rx, 0, 1, 0); |
|
329 |
} else if (e->buttons() & Qt::RightButton) {
|
|
330 |
// Right button down - rotate around X and Z axes |
|
331 |
glRotatef(-180*ry, 1, 0, 0); |
|
332 |
glRotatef(-180*rx, 0, 0, 1); |
|
333 |
} |
|
334 |
updateGL(); |
|
335 |
m_old_pos = e->pos(); |
|
336 |
} else {
|
|
337 |
||
338 |
} |
|
339 |
} |
|
340 |
||
341 |
void QView3DWidget::wheelEvent(QWheelEvent *e) |
|
342 |
{
|
|
343 |
makeCurrent(); |
|
344 |
glMatrixMode(GL_MODELVIEW); |
|
345 |
if (e->delta() < 0) |
|
346 |
glScalef(0.9, 0.9, 0.9); |
|
347 |
else |
|
348 |
glScalef(1.1, 1.1, 1.1); |
|
349 |
updateGL(); |
|
350 |
} |
|
351 |
||
352 |
void QView3DWidget::contextMenuEvent(QContextMenuEvent *e) |
|
353 |
{
|
|
354 |
e->accept(); |
|
355 |
} |
|
356 |
||
357 |
/******************************************************************************* |
|
358 |
** Misc tools |
|
359 |
*/ |
|
360 |
||
361 |
class WalkWidgetTreeFunction |
|
362 |
{
|
|
363 |
public: |
|
364 |
virtual void operator () (int depth, QWidget *widget) const = 0; |
|
365 |
}; |
|
366 |
||
367 |
static bool skipWidget(QDesignerFormEditorInterface *core, QWidget *widget) |
|
368 |
{
|
|
369 |
QDesignerMetaDataBaseItemInterface *item = core->metaDataBase()->item(widget); |
|
370 |
if (item == 0) |
|
371 |
return true; |
|
372 |
QString name = widget->metaObject()->className(); |
|
373 |
if (name == "QLayoutWidget") |
|
374 |
return true; |
|
375 |
||
376 |
return false; |
|
377 |
} |
|
378 |
||
379 |
static void walkWidgetTree(QDesignerFormEditorInterface *core, int depth, QWidget *widget, const WalkWidgetTreeFunction &func) |
|
380 |
{
|
|
381 |
if (widget == 0) |
|
382 |
return; |
|
383 |
if (!widget->isVisible()) |
|
384 |
return; |
|
385 |
||
386 |
if (!skipWidget(core, widget)) |
|
387 |
func(depth++, widget); |
|
388 |
||
389 |
QObjectList child_obj_list = widget->children(); |
|
390 |
foreach (QObject *child_obj, child_obj_list) {
|
|
391 |
QWidget *child = qobject_cast<QWidget*>(child_obj); |
|
392 |
if (child != 0) |
|
393 |
walkWidgetTree(core, depth, child, func); |
|
394 |
} |
|
395 |
} |
|
396 |
||
397 |
static void grabWidget_helper(QWidget *widget, QPixmap &res, QPixmap &buf, |
|
398 |
const QRect &r, const QPoint &offset, QDesignerFormEditorInterface *core) |
|
399 |
{
|
|
400 |
buf.fill(widget, r.topLeft()); |
|
401 |
QPainter::setRedirected(widget, &buf, r.topLeft()); |
|
402 |
QPaintEvent e(r & widget->rect()); |
|
403 |
QApplication::sendEvent(widget, &e); |
|
404 |
QPainter::restoreRedirected(widget); |
|
405 |
{
|
|
406 |
QPainter pt(&res); |
|
407 |
pt.drawPixmap(offset.x(), offset.y(), buf, 0, 0, r.width(), r.height()); |
|
408 |
} |
|
409 |
||
410 |
const QObjectList children = widget->children(); |
|
411 |
for (int i = 0; i < children.size(); ++i) {
|
|
412 |
QWidget *child = qobject_cast<QWidget*>(children.at(i)); |
|
413 |
if (child == 0 || child->isWindow()) |
|
414 |
continue; |
|
415 |
if (child->isHidden() || !child->geometry().intersects(r)) |
|
416 |
continue; |
|
417 |
if (core->metaDataBase()->item(child) != 0) |
|
418 |
continue; |
|
419 |
QRect cr = r & child->geometry(); |
|
420 |
cr.translate(-child->pos()); |
|
421 |
grabWidget_helper(child, res, buf, cr, offset + child->pos(), core); |
|
422 |
} |
|
423 |
} |
|
424 |
||
425 |
static QPixmap grabWidget(QWidget * widget, QDesignerFormEditorInterface *core) |
|
426 |
{
|
|
427 |
if (!widget) |
|
428 |
return QPixmap(); |
|
429 |
||
430 |
QRect r = widget->rect(); |
|
431 |
QSize s = widget->size(); |
|
432 |
||
433 |
QPixmap res(s), buf(s); |
|
434 |
||
435 |
grabWidget_helper(widget, res, buf, r, QPoint(), core); |
|
436 |
||
437 |
return res; |
|
438 |
} |
|
439 |
||
440 |
/******************************************************************************* |
|
441 |
** QView3D |
|
442 |
*/ |
|
443 |
||
444 |
class AddTexture : public WalkWidgetTreeFunction |
|
445 |
{
|
|
446 |
public: |
|
447 |
inline AddTexture(QDesignerFormEditorInterface *core, QView3DWidget *w) |
|
448 |
: m_core(core), m_3d_widget(w) {}
|
|
449 |
inline virtual void operator ()(int, QWidget *w) const |
|
450 |
{ m_3d_widget->addTexture(w, ::grabWidget(w, m_core)); }
|
|
451 |
QDesignerFormEditorInterface *m_core; |
|
452 |
QView3DWidget *m_3d_widget; |
|
453 |
}; |
|
454 |
||
455 |
class AddWidget : public WalkWidgetTreeFunction |
|
456 |
{
|
|
457 |
public: |
|
458 |
inline AddWidget(QView3DWidget *w) : m_3d_widget(w) {}
|
|
459 |
inline virtual void operator ()(int depth, QWidget *w) const |
|
460 |
{ m_3d_widget->addWidget(depth, w); }
|
|
461 |
QView3DWidget *m_3d_widget; |
|
462 |
}; |
|
463 |
||
464 |
QView3D::QView3D(QDesignerFormWindowInterface *form_window, QWidget *parent) |
|
465 |
: QWidget(parent) |
|
466 |
{
|
|
467 |
m_form_window = form_window; |
|
468 |
m_3d_widget = new QView3DWidget(this); |
|
469 |
connect(m_3d_widget, SIGNAL(updateForm()), this, SLOT(updateForm())); |
|
470 |
||
471 |
QGridLayout *layout = new QGridLayout(this); |
|
472 |
layout->setMargin(0); |
|
473 |
layout->addWidget(m_3d_widget, 0, 0, 1, 1); |
|
474 |
||
475 |
updateForm(); |
|
476 |
} |
|
477 |
||
478 |
void QView3D::updateForm() |
|
479 |
{
|
|
480 |
QWidget *w = m_form_window->mainContainer(); |
|
481 |
if (w == 0) |
|
482 |
return; |
|
483 |
||
484 |
m_3d_widget->clear(); |
|
485 |
||
486 |
walkWidgetTree(m_form_window->core(), 0, w, AddTexture(m_form_window->core(), m_3d_widget)); |
|
487 |
m_3d_widget->beginAddingWidgets(w); |
|
488 |
walkWidgetTree(m_form_window->core(), 0, w, AddWidget(m_3d_widget)); |
|
489 |
m_3d_widget->endAddingWidgets(); |
|
490 |
} |
|
491 |
||
492 |
#include "view3d.moc" |