/
mainwindow.cpp
2045 lines (1716 loc) · 64.2 KB
/
mainwindow.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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* *
* copyright : (C) 2007 The University of Toronto *
* netterfield@astro.utoronto.ca *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "mainwindow.h"
#include "boxitem.h"
#include "datamanager.h"
#include "debugdialog.h"
#include "debugnotifier.h"
#include "document.h"
#include "ellipseitem.h"
#include "exportgraphicsdialog.h"
#include "exportvectorsdialog.h"
#include "logdialog.h"
#include "application.h"
#include "debug.h"
#include "labelitem.h"
#include "lineitem.h"
#include "circleitem.h"
#include "arrowitem.h"
#include "memorywidget.h"
#include "objectstore.h"
#include "pictureitem.h"
#include "plotitem.h"
#include "plotitemmanager.h"
#include "svgitem.h"
#include "tabwidget.h"
#include "sharedaxisboxitem.h"
#include "ui_aboutdialog.h"
#include "viewvectordialog.h"
#include "viewmatrixdialog.h"
#include "viewprimitivedialog.h"
#include "view.h"
#include "applicationsettings.h"
#include "updatemanager.h"
#include "datasourcepluginmanager.h"
#include "pluginmenuitemaction.h"
#include "applicationsettingsdialog.h"
#include "themedialog.h"
#include "differentiatecurvesdialog.h"
#include "choosecolordialog.h"
#include "changedatasampledialog.h"
#include "changefiledialog.h"
#include "bugreportwizard.h"
#include "datawizard.h"
#include "aboutdialog.h"
#include "datavector.h"
#include "commandlineparser.h"
#include "dialogdefaults.h"
#include "dialoglauncher.h"
#include "scriptserver.h"
#ifndef KST_NO_SVG
#include <QSvgGenerator>
#endif
#include <QUndoGroup>
#ifndef KST_NO_PRINTER
#include <QPrintDialog>
#endif
#include <QMenuBar>
#include <QStatusBar>
#include <QProgressBar>
#include <QMessageBox>
#include <QImageWriter>
#include <QToolBar>
#include <QDebug>
namespace Kst {
MainWindow::MainWindow() :
_dataManager(0),
_exportGraphics(0),
_exportVectors(0),
_logDialog(0),
_differentiateCurvesDialog(0),
_chooseColorDialog(0),
_changeDataSampleDialog(0),
_changeFileDialog(0),
_bugReportWizard(0),
_applicationSettingsDialog(0),
_themeDialog(0),
_aboutDialog(0),
_highlightPoint(false)
#if defined(__QNX__)
, _qnxToolbarsVisible(true)
#endif
{
_doc = new Document(this);
_scriptServer = new ScriptServer(_doc->objectStore());
_tabWidget = new TabWidget(this);
_undoGroup = new QUndoGroup(this);
_debugDialog = new DebugDialog(this);
Debug::self()->setHandler(_debugDialog);
setWindowTitle("Kst");
createActions();
createMenus();
createToolBars();
createStatusBar();
_tabWidget->createView();
setCentralWidget(_tabWidget);
_tabWidget->setAcceptDrops(false); // Force drops to be passed to parent
connect(_tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentViewChanged()));
connect(_tabWidget, SIGNAL(currentViewModeChanged()), this, SLOT(currentViewModeChanged()));
connect(PlotItemManager::self(), SIGNAL(tiedZoomRemoved()), this, SLOT(tiedZoomRemoved()));
connect(PlotItemManager::self(), SIGNAL(allPlotsTiedZoom()), this, SLOT(allPlotsTiedZoom()));
readSettings();
connect(UpdateManager::self(), SIGNAL(objectsUpdated(qint64)), this, SLOT(updateViewItems(qint64)));
QTimer::singleShot(0, this, SLOT(performHeavyStartupActions()));
updateRecentKstFiles();
setAcceptDrops(true);
#if defined(__QNX__)
// We want to be able to intercept bezel gestures, which show up in Qt as menu button keyPressEvents.
qApp->installEventFilter(this);
qnxToggleToolbarVisibility();
#endif
}
MainWindow::~MainWindow() {
delete _dataManager;
_dataManager = 0;
delete _doc;
_doc = 0;
delete _scriptServer;
_scriptServer = 0;
}
void MainWindow::performHeavyStartupActions() {
// Set the timer for the UpdateManager.
UpdateManager::self()->setMinimumUpdatePeriod(ApplicationSettings::self()->minimumUpdatePeriod());
DataObject::init();
DataSourcePluginManager::init();
}
void MainWindow::cleanup() {
if (document() && document()->objectStore()) {
document()->objectStore()->clear();
}
}
void MainWindow::setLayoutMode(bool layoutMode) {
View *v = tabWidget()->currentView();
Q_ASSERT(v);
if (layoutMode) {
v->setViewMode(View::Layout);
_highlightPointAct->setEnabled(false);
_tiedZoomAct->setEnabled(false);
} else {
v->setViewMode(View::Data);
_highlightPointAct->setEnabled(true);
_tiedZoomAct->setEnabled(true);
}
}
void MainWindow::setHighlightPoint(bool highlight) {
_highlightPoint = highlight;
}
void MainWindow::changeZoomOnlyMode(QAction* act) {
if (act == _layoutModeAct && act->isChecked()) {
setLayoutMode(true);
} else {
setLayoutMode(false);
}
tabWidget()->currentView()->setZoomOnly((View::ZoomOnlyMode)act->data().toInt());
}
void MainWindow::toggleTiedZoom() {
if (isTiedTabs()) {
PlotItemManager::self()->toggleAllTiedZoom(0);
/*
QList<View*> views = tabWidget()->views();
foreach (View* view, views) {
PlotItemManager::self()->toggleAllTiedZoom(view);
}
*/
} else {
PlotItemManager::self()->toggleAllTiedZoom(tabWidget()->currentView());
}
}
void MainWindow::tiedZoomRemoved() {
_tiedZoomAct->setChecked(false);
}
void MainWindow::allPlotsTiedZoom() {
_tiedZoomAct->setChecked(true);
}
bool MainWindow::promptSaveDone() {
if (! _doc->isChanged()) {
return true; // No need to ask if there is no unsaved change -> we're done
}
else { // Changes registered: ask the user
int rc = QMessageBox::warning(this, tr("Kst: Save Prompt"), tr("Your document has been modified.\nSave changes?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
if (rc == QMessageBox::Save) {
save();
} else if (rc == QMessageBox::Cancel) {
return false;
}
return true;
}
}
void MainWindow::closeEvent(QCloseEvent *e) {
if (!promptSaveDone()) {
e->ignore();
return;
}
//cleanup();
QMainWindow::closeEvent(e);
kstApp->closeAllWindows();
}
Document *MainWindow::document() const {
return _doc;
}
QUndoGroup *MainWindow::undoGroup() const {
return _undoGroup;
}
TabWidget *MainWindow::tabWidget() const {
return _tabWidget;
}
void MainWindow::save() {
if (_doc->isOpen()) {
_doc->save();
} else {
saveAs();
}
}
void MainWindow::saveAs() {
QString fn = QFileDialog::getSaveFileName(this, tr("Kst: Save File"), _doc->fileName(), tr("Kst Sessions (*.kst)"));
if (fn.isEmpty()) {
return;
}
QString restorePath = QDir::currentPath();
QString kstfiledir = fn.left(fn.lastIndexOf('/')) + '/';
QDir::setCurrent(kstfiledir);
QString currentP = QDir::currentPath();
_doc->save(fn);
QDir::setCurrent(restorePath);
setWindowTitle("Kst - " + fn);
updateRecentKstFiles(fn);
}
void MainWindow::newDoc(bool force) {
bool clearApproved = false;
if (force) {
clearApproved = true;
} else {
clearApproved = promptSaveDone();
}
if (clearApproved) {
delete _dataManager;
_dataManager = 0;
delete _doc;
_doc = new Document(this);
_scriptServer->setStore(_doc->objectStore());
tabWidget()->clear();
tabWidget()->createView();
return;
} else {
return;
}
}
void MainWindow::open() {
if (!promptSaveDone()) {
return;
}
QSettings settings("Kst2");
const QString lastKey = "lastOpenedKstFile";
QString fn = settings.value(lastKey).toString();
if (fn.isEmpty()) {
fn = _doc->fileName();
}
fn = QFileDialog::getOpenFileName(this, tr("Kst: Open File"), fn, tr("Kst Sessions (*.kst)"));
if (fn.isEmpty()) {
return;
}
settings.setValue(lastKey, fn);
openFile(fn);
}
QAction* MainWindow::createRecentFileAction(const QString& filename, int idx, const QString& name, const char* openslot)
{
QAction* action = new QAction(this);
QString text = tr("&%1 %2").arg(idx).arg(name);
action->setText(text);
action->setData(filename);
action->setStatusTip(filename);
action->setVisible(true);
connect(action, SIGNAL(triggered()), this, openslot);
return action;
}
void MainWindow::updateRecentKstFiles(const QString& filename)
{
updateRecentFiles("recentKstFileList", _fileMenu, _bottomRecentKstActions, _recentKstFilesMenu, filename, SLOT(openRecentKstFile()));
}
void MainWindow::updateRecentDataFiles(const QString& filename)
{
updateRecentFiles("recentDataFileList", _toolsMenu, _bottomRecentDataActions, _recentDataFilesMenu, filename, SLOT(openRecentDataFile()));
if (!filename.isEmpty()) {
_dialogDefaults->setValue("vector/datasource", filename);
}
}
void MainWindow::updateRecentFiles(const QString& key, QMenu* menu, QList<QAction*>& actions, QMenu* submenu, const QString& newfilename, const char* openslot)
{
// Always add absolute paths to the recent file lists, otherwise they are not very reusable
QString absoluteFilePath = newfilename;
if (!newfilename.isEmpty() && !QDir::isAbsolutePath(newfilename)) { // If it's not empty and not absolute either, add the dir
absoluteFilePath = QDir::currentPath() + "/" + newfilename;
}
foreach(QAction* it, actions) {
menu->removeAction(it);
delete it;
}
actions.clear();
QSettings settings("Kst2");
QStringList recentFiles = settings.value(key).toStringList();
if (recentFiles.removeDuplicates() > 0) {
settings.setValue(key, recentFiles);
}
if (!absoluteFilePath.isEmpty()) {
recentFiles.removeOne(absoluteFilePath);
recentFiles.push_front(absoluteFilePath);
recentFiles = recentFiles.mid(0, 30);
settings.setValue(key, recentFiles);
}
submenu->clear();
QAction* check = new QAction(this);
check->setText("&Cleanup Non-Existent Files");
check->setData(key);
check->setVisible(true);
connect(check, SIGNAL(triggered()), this, SLOT(checkRecentFilesOnExistence()));
submenu->addAction(check);
submenu->addSeparator();
int i = 0;
foreach(const QString& it, recentFiles) {
i++;
if (i <= 5) {
// don't make file menu too wide, show complete path in statusbar
QAction* action = createRecentFileAction(it, i, QFileInfo(it).fileName(), openslot);
actions << action;
menu->addAction(action);
}
submenu->addAction(createRecentFileAction(it, i, it, openslot));
}
}
void MainWindow::openRecentKstFile()
{
QAction *action = qobject_cast<QAction *>(sender());
if (action) {
openFile(action->data().toString());
}
}
void MainWindow::checkRecentFilesOnExistence()
{
QAction *action = qobject_cast<QAction *>(sender());
if (action) {
QSettings settings("Kst2");
QStringList recentFiles = settings.value(action->data().toString()).toStringList();
recentFiles.removeDuplicates();
foreach(const QString& it, recentFiles) {
if (!QFileInfo(it).exists()) {
recentFiles.removeOne(it);
}
}
settings.setValue(action->data().toString(), recentFiles);
updateRecentKstFiles();
updateRecentDataFiles();
}
}
bool MainWindow::initFromCommandLine() {
delete _doc;
_doc = new Document(this);
_scriptServer->setStore(_doc->objectStore());
CommandLineParser P(_doc, this);
bool ok = _doc->initFromCommandLine(&P);
if (!P.pngFile().isEmpty()) {
exportGraphicsFile(P.pngFile(), "png", 1280, 1024,0);
ok = false;
}
if (!P.printFile().isEmpty()) {
#ifndef KST_NO_PRINTER
printFromCommandLine(P.printFile());
#endif
ok = false;
}
if (!P.kstFileName().isEmpty()) {
setWindowTitle("Kst - " + P.kstFileName());
}
_doc->setChanged(false);
return ok;
}
void MainWindow::openFile(const QString &file) {
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
newDoc(true); // Does all the init stuff, but does not ask for override as it's supposed to be done elsewhere
bool ok = _doc->open(file);
QApplication::restoreOverrideCursor();
if (!ok) {
QString lastError = _doc->lastError();
if (lastError.isEmpty())
lastError = tr("For details see Help->Debug Dialog->Log.");
QMessageBox::critical(this, tr("Kst"),tr("Error opening document:\n '%1'\n%2\n").arg(file, lastError));
}
setWindowTitle("Kst - " + file);
updateRecentKstFiles(file);
}
void MainWindow::exportGraphicsFile(const QString &filename, const QString &format, int width, int height, int display) {
int viewCount = 0;
int n_views = _tabWidget->views().size();
for (int i_view = 0; i_view<n_views; i_view++) {
View *view = _tabWidget->views().at(i_view);
QSize size;
if (display == 0) { // Width set by user, maintain aspect ratio
QSize sizeWindow(view->geometry().size());
size.setWidth(width);
size.setHeight((int)((qreal)width * (qreal)sizeWindow.height() / (qreal)sizeWindow.width()));
} else if (display == 1) { // Height set by user, maintain aspect ratio
QSize sizeWindow(view->geometry().size());
size.setHeight(height);
size.setWidth((int)((qreal)height * (qreal)sizeWindow.width() / (qreal)sizeWindow.height()));
} else if (display == 2) { // Width and height set by user
size.setWidth(width);
size.setHeight(height);
} else { //if (display == 3) { // Square (width x width)
size.setWidth(width);
size.setHeight(width);
}
QString file = filename;
if (n_views != 1) {
QFileInfo QFI(filename);
file = QFI.dir().path() + '/' + QFI.completeBaseName() +
'_' +
_tabWidget->tabBar()->tabText(viewCount).replace(QString("&"),QString()) + '.' +
QFI.suffix();
}
if (format == QString("svg")) {
#ifndef KST_NO_SVG
QPainter painter;
QSvgGenerator generator;
QSize currentSize(view->size());
view->resize(size);
view->processResize(size);
view->setPrinting(true);
generator.setFileName(file);
generator.setResolution(300);
generator.setSize(view->size());
generator.setViewBox(view->rect());
painter.begin(&generator);
view->render(&painter);
painter.end();
view->setPrinting(false);
view->resize(currentSize);
view->processResize(currentSize);
#endif
} else if (format == QString("eps")) {
#ifndef KST_NO_PRINTER
QPrinter printer(QPrinter::ScreenResolution);
#ifdef QT5
printer.setOutputFormat(QPrinter::PdfFormat);
#else
printer.setOutputFormat(QPrinter::PostScriptFormat);
#endif
printer.setOutputFileName(file);
printer.setOrientation(QPrinter::Portrait);
printer.setPrintRange(QPrinter::PageRange);
printer.setFromTo(i_view+1, i_view+1);
printer.setPaperSize(size, QPrinter::DevicePixel);
printToPrinter(&printer);
} else if (format == QString("pdf")) {
QPrinter printer(QPrinter::ScreenResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(file);
//setPrinterDefaults(&printer);
printer.setOrientation(QPrinter::Portrait);
printer.setPrintRange(QPrinter::PageRange);
printer.setFromTo(i_view+1, i_view+1);
printer.setPaperSize(size, QPrinter::DevicePixel);
printToPrinter(&printer);
#endif
} else {
QPainter painter;
QImage image(size, QImage::Format_ARGB32);
painter.begin(&image);
QSize currentSize(view->size());
view->resize(size);
view->processResize(size);
view->setPrinting(true);
view->render(&painter);
view->setPrinting(false);
view->resize(currentSize);
view->processResize(currentSize);
painter.end();
QImageWriter imageWriter(file, format.toLatin1());
imageWriter.write(image);
}
viewCount++;
}
}
void MainWindow::exportLog(const QString &imagename, QString &msgfilename, const QString &format, int x_size, int y_size,
int size_option_index, const QString &message) {
View *view = _tabWidget->currentView();
QSize size;
if (size_option_index == 0) {
size.setWidth(x_size);
size.setHeight(y_size);
} else if (size_option_index == 1) {
size.setWidth(x_size);
size.setHeight(y_size);
} else if (size_option_index == 2) {
QSize sizeWindow(view->geometry().size());
size.setWidth(x_size);
size.setHeight((int)((qreal)x_size * (qreal)sizeWindow.height() / (qreal)sizeWindow.width()));
} else {
QSize sizeWindow(view->geometry().size());
size.setHeight(y_size);
size.setWidth((int)((qreal)y_size * (qreal)sizeWindow.width() / (qreal)sizeWindow.height()));
}
QImage image(size, QImage::Format_ARGB32);
QPainter painter(&image);
QSize currentSize(view->size());
view->resize(size);
view->processResize(size);
view->setPrinting(true);
view->render(&painter);
view->setPrinting(false);
view->resize(currentSize);
view->processResize(currentSize);
QImageWriter imageWriter(imagename, format.toLatin1());
imageWriter.write(image);
QFile file(msgfilename);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << message;
file.close();
}
}
#ifndef KST_NO_PRINTER
void MainWindow::printToPrinter(QPrinter *printer) {
QPainter painter(printer);
QList<View*> pages;
switch (printer->printRange()) {
case QPrinter::PageRange:
if (printer->fromPage()>0) {
for (int i_page = printer->fromPage(); i_page<=printer->toPage(); i_page++) {
pages.append(_tabWidget->views().at(i_page-1));
}
}
break;
case QPrinter::AllPages:
foreach (View *view, _tabWidget->views()) {
pages.append(view);
}
break;
case QPrinter::Selection:
default:
pages.append(_tabWidget->currentView());
break;
}
QSize printerPageSize = printer->pageRect().size();
for (int i = 0; i < printer->numCopies(); ++i) {
for (int i_page = 0; i_page<pages.count(); i_page++) {
View *view = pages.at(i_page);
QSize currentSize(view->size());
view->resize(printerPageSize);
view->processResize(printerPageSize);
view->setPrinting(true);
view->render(&painter);
view->setPrinting(false);
view->resize(currentSize);
view->processResize(currentSize);
if (i_page<pages.count()-1)
printer->newPage();
}
}
}
void MainWindow::printFromCommandLine(const QString &printFileName) {
QPrinter printer(QPrinter::ScreenResolution);
printer.setResolution(300);
printer.setOutputFileName(printFileName);
setPrinterDefaults(&printer);
printer.setPrintRange(QPrinter::AllPages);
printToPrinter(&printer);
}
void MainWindow::setPrinterDefaults(QPrinter *printer) {
if (_dialogDefaults->value("print/landscape",true).toBool()) {
printer->setOrientation(QPrinter::Landscape);
} else {
printer->setOrientation(QPrinter::Portrait);
}
printer->setPaperSize(QPrinter::PaperSize(_dialogDefaults->value("print/paperSize", QPrinter::Letter).toInt()));
QPointF topLeft =_dialogDefaults->value("print/topLeftMargin", QPointF(15.0,15.0)).toPointF();
QPointF bottomRight =_dialogDefaults->value("print/bottomRightMargin", QPointF(15.0,15.0)).toPointF();
printer->setPageMargins(topLeft.x(), topLeft.y(), bottomRight.x(), bottomRight.y(), QPrinter::Millimeter);
// Apparent Qt bug: setting the page margins here doesn't set the correspoding values in the print
// dialog->printer-options sub-dialog under linux. If you don't open the printer-options sub-dialog,
// the values here are honored.
}
void MainWindow::savePrinterDefaults(QPrinter *printer) {
_dialogDefaults->setValue("print/landscape", printer->orientation() == QPrinter::Landscape);
_dialogDefaults->setValue("print/paperSize", int(printer->paperSize()));
qreal left, top, right, bottom;
printer->getPageMargins(&left, &top, &right, &bottom, QPrinter::Millimeter);
_dialogDefaults->setValue("print/topLeftMargin", QPointF(left, top));
_dialogDefaults->setValue("print/bottomRightMargin", QPointF(right, bottom));
}
void MainWindow::print() {
QPrinter printer(QPrinter::ScreenResolution);
printer.setResolution(300);
setPrinterDefaults(&printer);
#ifndef Q_OS_WIN
// QPrintDialog: Cannot be used on non-native printers
printer.setOutputFileName(_dialogDefaults->value("print/path", "./print.pdf").toString());
#endif
QPointer<QPrintDialog> pd = new QPrintDialog(&printer, this);
#if QT_VERSION >= 0x040500
pd->setOption(QPrintDialog::PrintToFile);
pd->setOption(QPrintDialog::PrintPageRange, true);
pd->setOption(QAbstractPrintDialog::PrintShowPageSize,true);
#endif
if (pd->exec() == QDialog::Accepted) {
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
printToPrinter(&printer);
QApplication::restoreOverrideCursor();
savePrinterDefaults(&printer);
}
_dialogDefaults->setValue("print/path", printer.outputFileName());
delete pd;
}
#endif
void MainWindow::currentViewChanged() {
if(!_tabWidget->currentView())
return;
_undoGroup->setActiveStack(_tabWidget->currentView()->undoStack());
currentViewModeChanged();
}
void MainWindow::currentViewModeChanged() {
if(!_tabWidget->currentView())
return;
_layoutModeAct->setChecked(_tabWidget->currentView()->viewMode() == View::Layout);
}
void MainWindow::aboutToQuit() {
writeSettings();
}
void MainWindow::about() {
if (!_aboutDialog) {
_aboutDialog = new AboutDialog(this);
}
if (_aboutDialog->isVisible()) {
_aboutDialog->raise();
_aboutDialog->activateWindow();
}
_aboutDialog->show();
}
void MainWindow::clearDrawingMarker() {
_createBoxAct->setChecked(false);
_createSharedAxisBoxAct->setChecked(false);
_createCircleAct->setChecked(false);
_createEllipseAct->setChecked(false);
_createLabelAct->setChecked(false);
_createLineAct->setChecked(false);
_createArrowAct->setChecked(false);
_createPictureAct->setChecked(false);
_createPlotAct->setChecked(false);
#ifndef KST_NO_SVG
_createSvgAct->setChecked(false);
#endif
_tabWidget->currentView()->setMouseMode(View::Default);
}
void MainWindow::createBox() {
if (_createBoxAct->isChecked()) {
clearDrawingMarker();
_createBoxAct->setChecked(true);
CreateBoxCommand *cmd = new CreateBoxCommand;
cmd->createItem();
} else {
_tabWidget->currentView()->setMouseMode(View::Default);
}
}
void MainWindow::createSharedAxisBox() {
if (_createSharedAxisBoxAct->isChecked()) {
clearDrawingMarker();
_createSharedAxisBoxAct->setChecked(true);
CreateSharedAxisBoxCommand *cmd = new CreateSharedAxisBoxCommand;
cmd->createItem();
} else {
_tabWidget->currentView()->setMouseMode(View::Default);
}
}
void MainWindow::createVector() {
QString tmp;
DialogLauncher::self()->showVectorDialog(tmp);
}
void MainWindow::createMatrix() {
QString tmp;
DialogLauncher::self()->showMatrixDialog(tmp);
}
void MainWindow::createScalar() {
QString scalarName;
DialogLauncher::self()->showScalarDialog(scalarName);
}
void MainWindow::createString() {
QString stringName;
DialogLauncher::self()->showStringDialog(stringName);
}
void MainWindow::createCircle() {
if (_createCircleAct->isChecked()) {
clearDrawingMarker();
_createCircleAct->setChecked(true);
CreateCircleCommand *cmd = new CreateCircleCommand;
cmd->createItem();
} else {
_tabWidget->currentView()->setMouseMode(View::Default);
}
}
void MainWindow::createEllipse() {
if (_createEllipseAct->isChecked()) {
clearDrawingMarker();
_createEllipseAct->setChecked(true);
CreateEllipseCommand *cmd = new CreateEllipseCommand;
cmd->createItem();
} else {
_tabWidget->currentView()->setMouseMode(View::Default);
}
}
void MainWindow::createLabel() {
if (_createLabelAct->isChecked()) {
clearDrawingMarker();
_createLabelAct->setChecked(true);
CreateLabelCommand *cmd = new CreateLabelCommand;
cmd->createItem();
} else {
_tabWidget->currentView()->setMouseMode(View::Default);
}
}
void MainWindow::createLine() {
if (_createLineAct->isChecked()) {
clearDrawingMarker();
_createLineAct->setChecked(true);
CreateLineCommand *cmd = new CreateLineCommand;
cmd->createItem();
} else {
_tabWidget->currentView()->setMouseMode(View::Default);
}
}
void MainWindow::createArrow() {
if (_createArrowAct->isChecked()) {
clearDrawingMarker();
_createArrowAct->setChecked(true);
CreateArrowCommand *cmd = new CreateArrowCommand;
cmd->createItem();
} else {
_tabWidget->currentView()->setMouseMode(View::Default);
}
}
void MainWindow::createPicture() {
if (_createPictureAct->isChecked()) {
clearDrawingMarker();
_createPictureAct->setChecked(true);
CreatePictureCommand *cmd = new CreatePictureCommand;
cmd->createItem();
} else {
_tabWidget->currentView()->setMouseMode(View::Default);
}
}
void MainWindow::createPlot() {
if (_createPlotAct->isChecked()) {
clearDrawingMarker();
_createPlotAct->setChecked(true);
CreatePlotCommand *cmd = new CreatePlotCommand;
cmd->createItem();
} else {
_tabWidget->currentView()->setMouseMode(View::Default);
}
}
#ifndef KST_NO_SVG
void MainWindow::createSvg() {
if (_createSvgAct->isChecked()) {
clearDrawingMarker();
_createSvgAct->setChecked(true);
CreateSvgCommand *cmd = new CreateSvgCommand;
cmd->createItem();
} else {
_tabWidget->currentView()->setMouseMode(View::Default);
}
}
#endif
void MainWindow::createCurve() {
DialogLauncher::self()->showCurveDialog();
}
void MainWindow::createPSD() {
DialogLauncher::self()->showPowerSpectrumDialog();
}
void MainWindow::createEquation() {
DialogLauncher::self()->showEquationDialog();
}
void MainWindow::createHistogram() {
DialogLauncher::self()->showHistogramDialog();
}
void MainWindow::createImage() {
DialogLauncher::self()->showImageDialog();
}
void MainWindow::createSpectogram() {
DialogLauncher::self()->showCSDDialog();
}
void MainWindow::createEventMonitor() {
DialogLauncher::self()->showEventMonitorDialog();
}
void MainWindow::createActions() {
// ***************************** File actions ******************************** //
_openAct = new QAction(tr("&Open..."), this);
_openAct->setStatusTip(tr("Open a new session"));
_openAct->setShortcut(tr("Ctrl+O"));
_openAct->setIcon(QPixmap(":document-open.png"));
connect(_openAct, SIGNAL(triggered()), this, SLOT(open()));
_saveAct = new QAction(tr("&Save"), this);
_saveAct->setStatusTip(tr("Save the current session"));
_saveAct->setShortcut(tr("Ctrl+S"));
_saveAct->setIcon(QPixmap(":document-save.png"));
connect(_saveAct, SIGNAL(triggered()), this, SLOT(save()));
_saveAsAct = new QAction(tr("Save &as..."), this);
_saveAsAct->setStatusTip(tr("Save the current session"));
_saveAsAct->setIcon(QPixmap(":document-save-as.png"));
connect(_saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
_closeAct = new QAction(tr("Ne&w Session"), this);
_closeAct->setStatusTip(tr("Close current session and start a new, empty one"));
_closeAct->setIcon(QPixmap(":document-close.png"));
connect(_closeAct, SIGNAL(triggered()), this, SLOT(newDoc()));
_reloadAct = new QAction(tr("Reload All &Data Sources"), this);
_reloadAct->setStatusTip(tr("Reload all data sources"));
_reloadAct->setIcon(QPixmap(":kst_reload.png"));
connect(_reloadAct, SIGNAL(triggered()), this, SLOT(reload()));
_printAct = new QAction(tr("&Print..."), this);
_printAct->setStatusTip(tr("Print the current view"));
_printAct->setIcon(QPixmap(":document-print.png"));
connect(_printAct, SIGNAL(triggered()), this, SLOT(print()));
#ifdef KST_NO_PRINTER
_printAct->setEnabled(false);
#endif
_exportGraphicsAct = new QAction(tr("&Export as Image(s)..."), this);
_exportGraphicsAct->setStatusTip(tr("Export graphics to disk"));
_exportGraphicsAct->setIcon(QPixmap(":image-x-generic.png"));
connect(_exportGraphicsAct, SIGNAL(triggered()), this, SLOT(showExportGraphicsDialog()));
_exportVectorsAct = new QAction(tr("Save &Vectors to Disk..."), this);
_exportVectorsAct->setStatusTip(tr("Export vectors to ascii file"));
_exportVectorsAct->setIcon(QPixmap(":save-vectors.png"));