-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathQtGnuplotWindow.cpp
More file actions
354 lines (318 loc) · 12.7 KB
/
QtGnuplotWindow.cpp
File metadata and controls
354 lines (318 loc) · 12.7 KB
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
/* GNUPLOT - QtGnuplotWindow.cpp */
/*[
* Copyright 2009 Jérôme Lodewyck
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose with or without fee is hereby granted,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
*
* Permission to modify the software is granted, but not the right to
* distribute the complete modified source code. Modifications are to
* be distributed as patches to the released version. Permission to
* distribute binaries produced by compiling modified sources is granted,
* provided you
* 1. distribute the corresponding source modifications from the
* released version in the form of a patch file along with the binaries,
* 2. add special version identification to distinguish your version
* in addition to the base release version number,
* 3. provide your name and address as the primary contact for the
* support of your modified version, and
* 4. retain our contact information in regard to use of the base
* software.
* Permission to distribute the released version of the source code along
* with corresponding source modifications in the form of a patch file is
* granted with same provisions 2 through 4 for binary distributions.
*
* This software is provided "as is" without express or implied warranty
* to the extent permitted by applicable law.
*
*
* Alternatively, the contents of this file may be used under the terms of the
* GNU General Public License Version 2 or later (the "GPL"), in which case the
* provisions of GPL are applicable instead of those above. If you wish to allow
* use of your version of this file only under the terms of the GPL and not
* to allow others to use your version of this file under the above gnuplot
* license, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the GPL. If you do not
* delete the provisions above, a recipient may use your version of this file
* under either the GPL or the gnuplot license.
]*/
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
#include "QtGnuplotWindow.h"
#include "QtGnuplotWidget.h"
#include "QtGnuplotEvent.h"
extern "C" {
#include "../mousecmn.h"
}
#include <QtGui>
QtGnuplotWindow::QtGnuplotWindow(int id, QtGnuplotEventHandler* eventHandler, QWidget* parent)
: QMainWindow(parent)
{
// m_ctrl = false;
m_eventHandler = eventHandler;
m_id = id;
m_pid = 0;
setWindowIcon(QIcon(":/images/gnuplot"));
// This attribute is required in order for QtGnuplotApplication::windowDestroyed()
// to be called when a plot window is closed externally. Without it, gnuplot_qt
// may be left as a zombie. On the other hand, manually closing the last open
// window may cause warning messages (linux) or a program hang (Windows).
// We now work around this by instead checking for visible/invisible windows in
// QtGnuplotApplication::enterPersistMode() before exiting.
// History: Bugs #1554 #1753 #2188
// setAttribute(Qt::WA_DeleteOnClose);
// Register as the main event receiver if not already created
if (m_eventHandler == 0)
m_eventHandler = new QtGnuplotEventHandler(this,
"qtgnuplot" + QString::number(QCoreApplication::applicationPid()));
// Central widget
m_widget = new QtGnuplotWidget(m_id, m_eventHandler, this);
connect(m_widget, SIGNAL(statusTextChanged(const QString&)), this, SLOT(on_setStatusText(const QString&)));
setCentralWidget(m_widget);
// Bars
m_toolBar = addToolBar("Main tool bar");
m_mouseToolBar = addToolBar("Mouse tool bar");
m_mouseToolBarLabel = new QLabel();
m_mouseToolBar->addWidget(m_mouseToolBarLabel);
m_statusBar = statusBar();
// Actions
QAction* copyToClipboardAction = new QAction(QIcon(":/images/clipboard" ), tr("Copy to clipboard"), this);
QAction* printAction = new QAction(QIcon(":/images/print" ), tr("Print" ), this);
QAction* exportAction = new QAction(QIcon(":/images/export" ), tr("Export" ), this);
QAction* exportPdfAction = new QAction(QIcon(":/images/exportPDF" ), tr("Export to PDF" ), this);
QAction* exportEpsAction = new QAction(QIcon(":/images/exportVector"), tr("Export to EPS" ), this);
QAction* exportSvgAction = new QAction(QIcon(":/images/exportVector"), tr("Export to SVG" ), this);
QAction* exportPngAction = new QAction(QIcon(":/images/exportRaster"), tr("Export to image" ), this);
QAction* settingsAction = new QAction(QIcon(":/images/settings" ), tr("Settings" ), this);
connect(copyToClipboardAction, SIGNAL(triggered()), m_widget, SLOT(copyToClipboard()));
connect(printAction, SIGNAL(triggered()), this, SLOT(print()));
connect(exportPdfAction, SIGNAL(triggered()), this, SLOT(exportToPdf()));
connect(exportEpsAction, SIGNAL(triggered()), m_widget, SLOT(exportToEps()));
connect(exportSvgAction, SIGNAL(triggered()), this, SLOT(exportToSvg()));
connect(exportPngAction, SIGNAL(triggered()), this, SLOT(exportToImage()));
connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettingsDialog()));
QMenu* exportMenu = new QMenu(this);
exportMenu->addAction(copyToClipboardAction);
exportMenu->addAction(printAction);
exportMenu->addAction(exportPdfAction);
// exportMenu->addAction(exportEpsAction);
exportMenu->addAction(exportSvgAction);
exportMenu->addAction(exportPngAction);
exportAction->setMenu(exportMenu);
m_toolBar->addAction(exportAction);
QWidget* exportWidget = m_toolBar->widgetForAction(exportAction);
QToolButton* exportButton = qobject_cast<QToolButton*>(exportWidget);
if (exportButton) {
connect(exportAction, SIGNAL(triggered(bool)), exportButton, SLOT(showMenu()));
}
createAction(tr("Replot") , 'e', ":/images/replot");
createAction(tr("Show grid") , 'g', ":/images/grid");
createAction(tr("Previous zoom"), 'p', ":/images/zoomPrevious");
createAction(tr("Next zoom") , 'n', ":/images/zoomNext");
createAction(tr("Autoscale") , 'a', ":/images/autoscale");
m_toolBar->addAction(settingsAction);
loadSettings();
}
QtGnuplotWindow::~QtGnuplotWindow()
{
saveSettings();
}
void QtGnuplotWindow::createAction(const QString& name, int key, const QString& icon)
{
QAction* action = new QAction(QIcon(icon), name, this);
connect(action, SIGNAL(triggered()), this, SLOT(on_keyAction()));
action->setData(key);
m_toolBar->addAction(action);
}
void QtGnuplotWindow::on_setStatusText(const QString& status)
{
if (m_mouseToolBar->toggleViewAction()->isChecked())
m_mouseToolBarLabel->setText(status);
if (m_statusBar->isVisible())
m_statusBar->showMessage(status);
}
void QtGnuplotWindow::on_keyAction()
{
QAction* action = qobject_cast<QAction *>(sender());
m_eventHandler->postTermEvent(GE_keypress, 0, 0, action->data().toInt(), 0, m_widget);
}
void QtGnuplotWindow::print()
{
QPrinter printer;
printer.setDocName(tr("gnuplot-qt graph"));
QPrintDialog dialog(&printer, this);
dialog.setOption(QAbstractPrintDialog::PrintPageRange, false);
if (dialog.exec() == QDialog::Accepted)
m_widget->print(printer);
}
void QtGnuplotWindow::exportToPdf()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Export to PDF"), "", tr("PDF files (*.pdf)"));
if (fileName.isEmpty())
return;
if (!fileName.endsWith(".pdf", Qt::CaseInsensitive))
fileName += ".pdf";
m_widget->exportToPdf(fileName);
}
void QtGnuplotWindow::exportToImage()
{
/// @todo other image formats supported by Qt
QString fileName = QFileDialog::getSaveFileName(this, tr("Export to Image"), "",
tr("Image files (*.png *.bmp)"));
if (fileName.isEmpty())
return;
if (!fileName.endsWith(".png", Qt::CaseInsensitive) &&
!fileName.endsWith(".bmp", Qt::CaseInsensitive))
fileName += ".png";
m_widget->exportToImage(fileName);
}
void QtGnuplotWindow::exportToSvg()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Export to SVG"), "", tr("SVG files (*.svg)"));
if (fileName.isEmpty())
return;
if (!fileName.endsWith(".svg", Qt::CaseInsensitive))
fileName += ".svg";
m_widget->exportToSvg(fileName);
}
#include "ui_QtGnuplotSettings.h"
void QtGnuplotWindow::loadSettings()
{
QSettings settings("gnuplot", "qtterminal");
settings.beginGroup("view");
m_widget->loadSettings(settings);
m_statusBarActive = settings.value("statusBarActive", true).toBool();
m_statusBar->setVisible(m_statusBarActive);
bool mouseToolBarActive = settings.value("mouseToolBarActive", false).toBool();
m_mouseToolBar->toggleViewAction()->setChecked(mouseToolBarActive);
m_mouseToolBar->setVisible(mouseToolBarActive);
}
void QtGnuplotWindow::saveSettings() const
{
QSettings settings("gnuplot", "qtterminal");
settings.beginGroup("view");
m_widget->saveSettings(settings);
settings.setValue("statusBarActive", m_statusBarActive);
settings.setValue("mouseToolBarActive", m_mouseToolBar->toggleViewAction()->isChecked());
}
void QtGnuplotWindow::showSettingsDialog()
{
QDialog* settingsDialog = new QDialog(this);
m_ui = new Ui_settingsDialog();
m_ui->setupUi(settingsDialog);
m_ui->antialiasCheckBox->setCheckState(m_widget->antialias() ? Qt::Checked : Qt::Unchecked);
m_ui->roundedCheckBox->setCheckState(m_widget->rounded() ? Qt::Checked : Qt::Unchecked);
m_ui->ctrlQCheckBox->setCheckState(m_widget->ctrlQ() ? Qt::Checked : Qt::Unchecked);
m_ui->replotOnResizeCheckBox->setCheckState(m_widget->replotOnResize() ? Qt::Checked : Qt::Unchecked);
if (m_statusBar->isVisible())
m_ui->mouseLabelComboBox->setCurrentIndex(0);
else if (m_mouseToolBar->toggleViewAction()->isChecked())
m_ui->mouseLabelComboBox->setCurrentIndex(1);
else if (m_widget->statusLabelActive())
m_ui->mouseLabelComboBox->setCurrentIndex(2);
else
m_ui->mouseLabelComboBox->setCurrentIndex(3);
QPixmap samplePixmap(m_ui->sampleColorLabel->size());
samplePixmap.fill(m_widget->backgroundColor());
m_ui->sampleColorLabel->setPixmap(samplePixmap);
m_chosenBackgroundColor = m_widget->backgroundColor();
connect(m_ui->backgroundButton, SIGNAL(clicked()), this, SLOT(settingsSelectBackgroundColor()));
settingsDialog->exec();
if (settingsDialog->result() == QDialog::Accepted)
{
m_widget->setBackgroundColor(m_chosenBackgroundColor);
m_widget->setAntialias(m_ui->antialiasCheckBox->checkState() == Qt::Checked);
m_widget->setRounded(m_ui->roundedCheckBox->checkState() == Qt::Checked);
m_widget->setCtrlQ(m_ui->ctrlQCheckBox->checkState() == Qt::Checked);
m_widget->setReplotOnResize(m_ui->replotOnResizeCheckBox->checkState() == Qt::Checked);
int statusIndex = m_ui->mouseLabelComboBox->currentIndex();
m_statusBarActive = (statusIndex == 0);
m_statusBar->setVisible(m_statusBarActive);
m_mouseToolBar->toggleViewAction()->setChecked(statusIndex == 1);
m_mouseToolBar->setVisible(statusIndex == 1);
m_widget->setStatusLabelActive(statusIndex == 2);
saveSettings();
}
}
void QtGnuplotWindow::settingsSelectBackgroundColor()
{
m_chosenBackgroundColor = QColorDialog::getColor(m_chosenBackgroundColor, this);
QPixmap samplePixmap(m_ui->sampleColorLabel->size());
samplePixmap.fill(m_chosenBackgroundColor);
m_ui->sampleColorLabel->setPixmap(samplePixmap);
}
void QtGnuplotWindow::closeEvent(QCloseEvent *event)
{
m_eventHandler->postTermEvent(GE_reset, 0, 0, 0, 0, m_widget);
event->accept();
}
void QtGnuplotWindow::processEvent(QtGnuplotEventType type, QDataStream& in)
{
if (type == GETitle)
{
QString title;
in >> title;
if (title.isEmpty())
title = tr("Gnuplot window ") + QString::number(m_id);
setWindowTitle(title);
}
else if (type == GERaise)
{
#ifdef _WIN32
SetForegroundWindow((HWND) winId());
if (isMinimized())
showNormal();
#endif
raise();
}
else if (type == GESetCtrl)
{
in >> m_ctrl;
m_widget->setCtrlQ(m_ctrl);
}
else if (type == GESetRounded)
{
in >> m_rounded;
m_widget->setRounded(m_rounded);
}
else if (type == GESetAntiAlias)
{
in >> m_antialias;
m_widget->setAntialias(m_antialias);
}
else if (type == GESetReplotOnResize)
{
in >> m_replotOnResize;
m_widget->setReplotOnResize(m_replotOnResize);
}
else if (type == GESetPosition)
{
QPoint pos;
in >> pos;
move(pos);
}
else if (type == GEPID)
in >> m_pid;
else
m_widget->processEvent(type, in);
}
void QtGnuplotWindow::keyPressEvent(QKeyEvent* event)
{
if ((event->key() == 'Q') && ( !m_widget->ctrlQ() || (QApplication::keyboardModifiers() & Qt::ControlModifier) ))
close();
#ifdef _WIN32
#if !defined(DISABLE_SPACE_RAISES_CONSOLE)
if ((event->key() == Qt::Key_Space) && ( !m_ctrl || (QApplication::keyboardModifiers() & Qt::ControlModifier) ))
{
AllowSetForegroundWindow(m_pid);
m_eventHandler->postTermEvent(GE_raise, 0, 0, 0, 0, m_widget);
}
#endif
#endif
QMainWindow::keyPressEvent(event);
}