-
Notifications
You must be signed in to change notification settings - Fork 0
/
OGLContextWidget.cpp
118 lines (95 loc) · 3.36 KB
/
OGLContextWidget.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
#include "OGLContextWidget.h"
#include <QFileDialog>
#include <QMessageBox>
#ifdef _DEBUG
#include <QDebug>
#endif
OGLContextWidget::OGLContextWidget(QWidget *parent) :
QWidget(parent)
{
createGLWidgets();
createMenuAndActions();
createLayout();
settingsWidget();
}
void OGLContextWidget::createGLWidgets()
{
glWidget = new OGLContext;
glWidgetArea = new QScrollArea;
glWidgetArea->setWidget(glWidget);
glWidgetArea->setWidgetResizable(true);
glWidgetArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
glWidgetArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
glWidgetArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
/* For KeyEvent! */
glWidget->setFocusPolicy(Qt::StrongFocus);
}
void OGLContextWidget::createMenuAndActions()
{
fileMenu = new QMenu(this);
fileMenu->setTitle(tr("&File"));
fileMenu->addAction(QIcon("://icons/chart_icons/export_chart_icon.png"), tr("&Grab to \"*.png\""), this, SLOT(grabFrameBuffer()), Qt::Key_F6);
fileMenu->addSeparator();
fileMenu->addAction(QIcon("://icons/chart_icons/close_chart_icon.png"), tr("&Close window"), this, SLOT(close()), Qt::Key_F10);
glMenuBar = new QMenuBar(this);
glMenuBar->setStyleSheet("QMenuBar {"
"background-color: #0099CC "
"}"
"QMenuBar::item {"
"color: #FFFFFF;"
"background-color: #0099CC"
"}"
"QMenuBar::item:selected {"
"color: #000000;"
"background: orange"
"}"
"QMenuBar::item:pressed {"
"color: #000000;"
"background: #FFCC33"
"}");
glMenuBar->addMenu(fileMenu);
}
void OGLContextWidget::createLayout()
{
centralLayout = new QGridLayout(this);
centralLayout->setMenuBar(glMenuBar);
centralLayout->addWidget(glWidgetArea, 0, 0);
setLayout(centralLayout);
}
void OGLContextWidget::settingsWidget()
{
setAttribute(Qt::WA_DeleteOnClose);
setAttribute(Qt::WA_ShowModal);
move(128, 100);
setMinimumSize(320, 320);
resize(520, 520);
setWindowTitle(tr("3D OpenGL Context"));
}
void OGLContextWidget::grabFrameBuffer()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save *.png file"), "",
tr("Image File (*.png);;All Files (*)"));
#ifdef _DEBUG
qDebug() << "Grabbing...";
#endif
QImage image = glWidget->grabFrameBuffer();
QPixmap::fromImage(image);
if (image.save(fileName, "PNG"))
{
#ifdef _DEBUG
qDebug() << "Grabbed!";
#endif
}
else
{
QMessageBox::critical(0, tr("Error!"), QString(tr("Can't write PNG image!\n"
"Please check RW permission or correct select the file.\n"
"And try again!")));
return;
}
}
OGLContextWidget::~OGLContextWidget()
{
/* Empty Destructor */
}