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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Designer of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** 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.GPL 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.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtCore>
#include <QtGui>
#include <QtOpenGL>
#include "abstractformeditor.h"
#include "abstractmetadatabase.h"
#include "abstractformwindow.h"
#include "view3d.h"
#define SELECTION_BUFSIZE 512
#ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT
# define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
#endif
/*******************************************************************************
** QView3DWidget
*/
class QView3DWidget : public QGLWidget
{
Q_OBJECT
public:
QView3DWidget(QWidget *parent);
virtual void initializeGL();
virtual void resizeGL(int w, int h);
virtual void paintGL();
void clear();
void addTexture(QWidget *w, const QPixmap &pm);
void beginAddingWidgets(QWidget *form);
void addWidget(int depth, QWidget *w);
void endAddingWidgets();
QWidget *widgetAt(const QPoint &pos);
signals:
void updateForm();
protected:
void mousePressEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void wheelEvent(QWheelEvent *);
void keyReleaseEvent(QKeyEvent *);
void contextMenuEvent(QContextMenuEvent *);
private:
QWidget *m_form;
QPoint m_old_pos;
bool m_layer_coloring;
bool m_use_mipmaps;
GLuint m_form_list_id;
typedef QMap<QWidget*, GLuint> TextureMap;
TextureMap m_texture_map;
typedef QMap<GLuint, QWidget*> WidgetNameMap;
GLuint m_next_widget_name;
WidgetNameMap m_widget_name_map;
};
QView3DWidget::QView3DWidget(QWidget *parent)
: QGLWidget(parent)
, m_layer_coloring(true)
, m_form_list_id(0)
, m_next_widget_name(0)
{
setFocusPolicy(Qt::StrongFocus);
}
static int nearestSize(int v)
{
int n = 0, last = 0;
for (int s = 0; s < 32; ++s) {
if (((v>>s) & 1) == 1) {
++n;
last = s;
}
}
if (n > 1)
return 1 << (last+1);
return 1 << last;
}
// static int pm_cnt = 0;
void QView3DWidget::addTexture(QWidget *w, const QPixmap &pm)
{
int tx_w = nearestSize(pm.width());
int tx_h = nearestSize(pm.height());
QPixmap tmp(tx_w, tx_h);
tmp.fill(QColor(0,0,0));
QPainter p(&tmp);
p.drawPixmap(0, tx_h - pm.height(), pm);
p.end();
QImage tex = tmp.toImage();
// QString file_name = QString("pixmapDump%1.png").arg(pm_cnt++);
// qDebug() << "grabWidget():" << file_name << tex.save(file_name, "PNG");
tex = QGLWidget::convertToGLFormat(tex);
GLuint tx_id;
glGenTextures(1, &tx_id);
glBindTexture(GL_TEXTURE_2D, tx_id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (m_use_mipmaps) {
//glHint(GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
//glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.f);
} else {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, tex.width(), tex.height(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, tex.bits());
m_texture_map[w] = tx_id;
}
void QView3DWidget::addWidget(int depth, QWidget *widget)
{
TextureMap::const_iterator it = m_texture_map.find(widget);
Q_ASSERT(it != m_texture_map.end());
makeCurrent();
int w = m_form->size().width();
int h = m_form->size().height();
int max = qMax(w, h);
QRect r = widget->rect();
QPoint pos = widget->mapToGlobal(QPoint(0, 0));
r.moveTopLeft(m_form->mapFromGlobal(pos));
float s = r.width()/float(nearestSize(r.width()));
float t = r.height()/float(nearestSize(r.height()));
if (m_layer_coloring)
glColor4f(1.0 - depth/10.0, 1.0 - depth/10.0, 1.0, 1.0 - depth/20.0);
else
glColor4f(1.0, 1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, *it);
glBegin(GL_QUADS);
glLoadName(m_next_widget_name);
glTexCoord2f(0.0, 0.0); glVertex3f(r.left() - w/2, r.bottom() - h/2, depth*max/8);
glTexCoord2f(s, 0.0); glVertex3f(r.right() - w/2, r.bottom()- h/2, depth*max/8);
glTexCoord2f(s, t); glVertex3f(r.right() - w/2, r.top() - h/2, depth*max/8);
glTexCoord2f(0.0, t); glVertex3f(r.left() - w/2, r.top() - h/2, depth*max/8);
glEnd();
m_widget_name_map[m_next_widget_name++] = widget;
}
void QView3DWidget::clear()
{
makeCurrent();
glDeleteLists(m_form_list_id, 1);
foreach (GLuint id, m_texture_map)
glDeleteTextures(1, &id);
m_texture_map.clear();
m_widget_name_map.clear();
m_next_widget_name = 0;
}
void QView3DWidget::beginAddingWidgets(QWidget *form)
{
makeCurrent();
m_form = form;
m_form_list_id = glGenLists(1);
glNewList(m_form_list_id, GL_COMPILE);
glInitNames();
glPushName(-1);
m_next_widget_name = 0;
}
void QView3DWidget::endAddingWidgets()
{
makeCurrent();
glEndList();
}
void QView3DWidget::initializeGL()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
qglClearColor(palette().color(QPalette::Window).dark());
glColor3f (1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_FLAT);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
QString extensions(reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)));
m_use_mipmaps = false;// extensions.contains("GL_SGIS_generate_mipmap");
}
void QView3DWidget::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-width/2, width/2, height/2, -height/2, -999999, 999999);
}
void QView3DWidget::paintGL()
{
glColor4f(1.0, 1.0, 1.0, 1.0);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glCallList(m_form_list_id);
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(-width()/2, -height()/2, 0.0);
QFontMetrics fm(font());
glColor4f(0.4, 0.4, 0.4, 0.7);
glRecti(0, height() - fm.lineSpacing()*2.5, width(), height());
glColor3f(1.0, 1.0, 1.0);
renderText(10, height() - fm.lineSpacing()*1.5,
"Press and hold left/right mouse button = tilt the view.");
renderText(10, height() - fm.lineSpacing()*0.5,
"Mouse wheel = zoom. 't' = toggle layer coloring. 'r' = reset transform.");
glPopMatrix();
glPopAttrib();
}
QWidget *QView3DWidget::widgetAt(const QPoint &pos)
{
makeCurrent();
GLuint selectBuf[SELECTION_BUFSIZE];
glSelectBuffer(SELECTION_BUFSIZE, selectBuf);
glRenderMode (GL_SELECT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glCallList(m_form_list_id);
return 0;
}
void QView3DWidget::keyReleaseEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_T) {
m_layer_coloring = !m_layer_coloring;
emit updateForm();
} else if (e->key() == Qt::Key_R) {
makeCurrent();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
updateGL();
}
void QView3DWidget::mousePressEvent(QMouseEvent *e)
{
m_old_pos = e->pos();
}
void QView3DWidget::mouseReleaseEvent(QMouseEvent *e)
{
m_old_pos = e->pos();
}
void QView3DWidget::mouseMoveEvent(QMouseEvent *e)
{
if (e->buttons() & (Qt::LeftButton | Qt::RightButton)) {
GLfloat rx = (GLfloat) (e->x() - m_old_pos.x()) / width();
GLfloat ry = (GLfloat) (e->y() - m_old_pos.y()) / height();
makeCurrent();
glMatrixMode(GL_MODELVIEW);
if (e->buttons() & Qt::LeftButton) {
// Left button down - rotate around X and Y axes
glRotatef(-180*ry, 1, 0, 0);
glRotatef(180*rx, 0, 1, 0);
} else if (e->buttons() & Qt::RightButton) {
// Right button down - rotate around X and Z axes
glRotatef(-180*ry, 1, 0, 0);
glRotatef(-180*rx, 0, 0, 1);
}
updateGL();
m_old_pos = e->pos();
} else {
}
}
void QView3DWidget::wheelEvent(QWheelEvent *e)
{
makeCurrent();
glMatrixMode(GL_MODELVIEW);
if (e->delta() < 0)
glScalef(0.9, 0.9, 0.9);
else
glScalef(1.1, 1.1, 1.1);
updateGL();
}
void QView3DWidget::contextMenuEvent(QContextMenuEvent *e)
{
e->accept();
}
/*******************************************************************************
** Misc tools
*/
class WalkWidgetTreeFunction
{
public:
virtual void operator () (int depth, QWidget *widget) const = 0;
};
static bool skipWidget(QDesignerFormEditorInterface *core, QWidget *widget)
{
QDesignerMetaDataBaseItemInterface *item = core->metaDataBase()->item(widget);
if (item == 0)
return true;
QString name = widget->metaObject()->className();
if (name == "QLayoutWidget")
return true;
return false;
}
static void walkWidgetTree(QDesignerFormEditorInterface *core, int depth, QWidget *widget, const WalkWidgetTreeFunction &func)
{
if (widget == 0)
return;
if (!widget->isVisible())
return;
if (!skipWidget(core, widget))
func(depth++, widget);
QObjectList child_obj_list = widget->children();
foreach (QObject *child_obj, child_obj_list) {
QWidget *child = qobject_cast<QWidget*>(child_obj);
if (child != 0)
walkWidgetTree(core, depth, child, func);
}
}
static void grabWidget_helper(QWidget *widget, QPixmap &res, QPixmap &buf,
const QRect &r, const QPoint &offset, QDesignerFormEditorInterface *core)
{
buf.fill(widget, r.topLeft());
QPainter::setRedirected(widget, &buf, r.topLeft());
QPaintEvent e(r & widget->rect());
QApplication::sendEvent(widget, &e);
QPainter::restoreRedirected(widget);
{
QPainter pt(&res);
pt.drawPixmap(offset.x(), offset.y(), buf, 0, 0, r.width(), r.height());
}
const QObjectList children = widget->children();
for (int i = 0; i < children.size(); ++i) {
QWidget *child = qobject_cast<QWidget*>(children.at(i));
if (child == 0 || child->isWindow())
continue;
if (child->isHidden() || !child->geometry().intersects(r))
continue;
if (core->metaDataBase()->item(child) != 0)
continue;
QRect cr = r & child->geometry();
cr.translate(-child->pos());
grabWidget_helper(child, res, buf, cr, offset + child->pos(), core);
}
}
static QPixmap grabWidget(QWidget * widget, QDesignerFormEditorInterface *core)
{
if (!widget)
return QPixmap();
QRect r = widget->rect();
QSize s = widget->size();
QPixmap res(s), buf(s);
grabWidget_helper(widget, res, buf, r, QPoint(), core);
return res;
}
/*******************************************************************************
** QView3D
*/
class AddTexture : public WalkWidgetTreeFunction
{
public:
inline AddTexture(QDesignerFormEditorInterface *core, QView3DWidget *w)
: m_core(core), m_3d_widget(w) {}
inline virtual void operator ()(int, QWidget *w) const
{ m_3d_widget->addTexture(w, ::grabWidget(w, m_core)); }
QDesignerFormEditorInterface *m_core;
QView3DWidget *m_3d_widget;
};
class AddWidget : public WalkWidgetTreeFunction
{
public:
inline AddWidget(QView3DWidget *w) : m_3d_widget(w) {}
inline virtual void operator ()(int depth, QWidget *w) const
{ m_3d_widget->addWidget(depth, w); }
QView3DWidget *m_3d_widget;
};
QView3D::QView3D(QDesignerFormWindowInterface *form_window, QWidget *parent)
: QWidget(parent)
{
m_form_window = form_window;
m_3d_widget = new QView3DWidget(this);
connect(m_3d_widget, SIGNAL(updateForm()), this, SLOT(updateForm()));
QGridLayout *layout = new QGridLayout(this);
layout->setMargin(0);
layout->addWidget(m_3d_widget, 0, 0, 1, 1);
updateForm();
}
void QView3D::updateForm()
{
QWidget *w = m_form_window->mainContainer();
if (w == 0)
return;
m_3d_widget->clear();
walkWidgetTree(m_form_window->core(), 0, w, AddTexture(m_form_window->core(), m_3d_widget));
m_3d_widget->beginAddingWidgets(w);
walkWidgetTree(m_form_window->core(), 0, w, AddWidget(m_3d_widget));
m_3d_widget->endAddingWidgets();
}
#include "view3d.moc"
|