-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgribouillot.cpp
More file actions
executable file
·1320 lines (1051 loc) · 39.4 KB
/
gribouillot.cpp
File metadata and controls
executable file
·1320 lines (1051 loc) · 39.4 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
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
/**
* @brief The QMainWindow of Gribouillot
* @details Implements the GraphicsView which receives the drawing events,
* a TabWidget for layers pages,
* a DockWidget for the drawing tools
* @author Ludovic A.
* @date 2015/2016/2017/2018
* @bug No known bugs
* @copyright GNU Public License v3
*/
#include <thread>
//#include <ctime>
#include <QtMath>
#include <QtWidgets>
#include <QtDebug>
#include "ui_gribouillot.h"
#include "ui_maptabwidget.h"
#include "dlg_autosave.h"
#include "dlg_newgribproject.h"
#include "dlg_importlayer.h"
#include "dlg_changemap.h"
#include "gribouillot.h"
#include "main.h"
#include "zoomablegraphicsview.h"
//declaring global functions defined in main.c
extern QString getSupportedImageFormats();
extern QString getDefaultImageFilter(QString);
Gribouillot::Gribouillot(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Gribouillot),
scene(new GribouillotScene),
minimap(nullptr),
timer(nullptr),
currentProjectName(""),
mapName(""),
backgroundMap(new QGraphicsPixmapItem),
currentTabIndex(0),
drawingColor(Qt::black),
drawingWidth(5),
currentDrawing(NONE)
{
ui->setupUi(this);
//The spiral dialog is an extension of the interface which is shown/hidden if necessary
spiralDialog = new Dlg_spiral(this);
//Menu connections
connect (ui->actionAbout_Qt, &QAction::triggered, qApp, &QApplication::aboutQt);
//TabWidget
connect (ui->gribTabWidget, &SmartInsertTabWidget::currentChanged, this, &Gribouillot::switchToTabIndex);
//mapTabWidget is an extension of the mainWindow and even a "friendly" Class.
connect (ui->mapTabWidget->ui->mapTabNameTlBtt, &QToolButton::clicked, this, &Gribouillot::mapNameTlBttClicked);
connect (ui->mapTabWidget->ui->blackWhiteTlBtt, &QToolButton::toggled, this, &Gribouillot::blackWhiteTlBttClicked);
connect (ui->mapTabWidget->ui->scaleRulerTlBtt, &QToolButton::clicked, this, &Gribouillot::scaleRulerTlBttTriggered);
connect (ui->mapTabWidget->ui->gpsTlBtt, &QToolButton::clicked, this, &Gribouillot::gpsTlBttClicked);
//graphicsView and its drawing cursor
ui->zGraphicsView->setScene(scene);
drawingCursor = QCursor(QPixmap(":/Resources/Icons/cursor-drawing.png"));
//scene
connect (scene, &GribouillotScene::selectionChanged, this, &Gribouillot::sceneSelectionChanged);
connect (scene, &GribouillotScene::newMouseClickPreSelect, this, &Gribouillot::newSceneClickPreSelect);
connect (scene, &GribouillotScene::newMouseClickPostSelect, this, &Gribouillot::newSceneClickPostSelect);
connect (scene, &GribouillotScene::newMouseMove, this, &Gribouillot::newMoveOnScene);
connect (scene, &GribouillotScene::keyDeletePressed, this, &Gribouillot::keyDeleteFromScene);
connect (scene, &GribouillotScene::keySpacePressed, this, &Gribouillot::keySpaceFromScene);
connect (scene, &GribouillotScene::keyCPressed, this, &Gribouillot::on_actionChooseColor_triggered);
connect (scene, &GribouillotScene::keyTPressed, this, &Gribouillot::keyTFromScene);
connect (scene, &GribouillotScene::keyEscPressed, this, &Gribouillot::keyEscFromScene);
//Connect mapTabWidget to zGraphicsView in order to update the black & white scale bar
connect(ui->mapTabWidget, &MapTabWidget::newSystemScale, ui->zGraphicsView, &ZoomableGraphicsView::systemScaleChanged);
drawingGroup = new QActionGroup(this);
//Toolbar actions which are not affected by restrictToolbar()
drawingGroup->addAction(ui->actionCursorDrag);
drawingGroup->addAction(ui->actionCursorSelect);
drawingGroup->addAction(ui->actionMeasureDistance);
drawingGroup->addAction(ui->actionMeasureAngle);
restrictedActions = drawingGroup->actions().count();//Do not move this line!
//Toolbar actions affected by restrictToolbar()
drawingGroup->addAction(ui->actionPoint);
drawingGroup->addAction(ui->actionSegment);
drawingGroup->addAction(ui->actionLine);
drawingGroup->addAction(ui->actionHorizontalLine);
drawingGroup->addAction(ui->actionVerticalLine);
drawingGroup->addAction(ui->actionParallel);
drawingGroup->addAction(ui->actionPerpendicular);
drawingGroup->addAction(ui->actionBisection);
drawingGroup->addAction(ui->actionAngleLine);
drawingGroup->addAction(ui->actionCircleCenterPoint);
drawingGroup->addAction(ui->actionCircleSelectRadius);
drawingGroup->addAction(ui->actionCircleRadiusValue);
drawingGroup->addAction(ui->actionCircleDiameter);
drawingGroup->addAction(ui->actionCircleSelectDiameter);
drawingGroup->addAction(ui->actionCircleTriangle);
drawingGroup->addAction(ui->actionArcFromCircle);
drawingGroup->addAction(ui->actionArc);
drawingGroup->addAction(ui->actionSpiral);
drawingGroup->addAction(ui->actionLoadPicture);
//connect because mapTabWidget toolButtons can not be a part of the QActionGroup above
connect(drawingGroup, &QActionGroup::triggered, ui->mapTabWidget, &MapTabWidget::uncheckDrawingButtons);
//Disable everything before a first project is created
ui->toolBar->setEnabled(false);
ui->centralwidget->setEnabled(false);
//TODO: implement GPS
ui->mapTabWidget->ui->gpsTlBtt->setVisible(false);
show();
//load global settings
readSettings();
}
Gribouillot::~Gribouillot()
{
delete ui;
}
/************************* Protected functions ****************************/
/**
* @brief catch the MainWindow close event
*/
void Gribouillot::closeEvent(QCloseEvent *event)
{
/*
* Saving project must be done first, because of the 'recentProject'
* key used in writeSettings.
*/
if(!currentProjectName.isEmpty())
maybeSave();//maybe save current project
//Automatically save general settings (window position, etc)
writeSettings();
event->accept();
}
/************************* Private functions - UI ****************************/
/**
* @brief Do not allow the use of layer-specific actions.
* @details Hard coded limit depending on the ui design. See gribouillot.ui
*/
void Gribouillot::restrictToolbar()
{
QList<QAction*> actionsList = ui->toolBar->actions();
for (int i=restrictedActions; i < actionsList.size(); ++i)
{
actionsList.at(i)->setEnabled(false);
}
//currentLayer has no sense since no layer is selected
currentLayer = nullptr;
//Default to select cursor
setSelectionView();
}
/**
* @brief Enable full toolBar
*/
void Gribouillot::fullToolbar()
{
foreach(QAction *action, ui->toolBar->actions())
{
action->setEnabled(true);
//qDebug() << action;
}
}
/**
* @brief Typical readSettings function for global parameters
*/
void Gribouillot::readSettings()
{
QSettings settings("GAT", "Gribouillot");
settings.beginGroup("MainWindow");
resize(settings.value("size", QSize(970, 600)).toSize());
move(settings.value("pos", QPoint(100, 100)).toPoint());
settings.endGroup();
//Restore spiral dialog settings
settings.beginGroup("spiral");
spiralDialog->loadData( settings.value("constructAsOneItem", true).toBool(),
settings.value("baseDisplay", false).toBool(),
settings.value("showBaseCentersOnly", true).toBool());
settings.endGroup();
settings.beginGroup("preferences");
ui->actionCopy_pixmaps->setChecked( settings.value("copyPixmaps", true).toBool() );
autosaveTimeout = settings.value("autosave", 0).toInt();
newSaveTimeout();
settings.endGroup();
if (settings.contains("recentProject"))
openProject(settings.value("recentProject").toString());
//Restore minimap after openProject(), when the background map is loaded.
ui->actionMinimap->setChecked(settings.value("preferences/minimap").toBool());
}
/**
* @brief Typical writeSettings function
*/
void Gribouillot::writeSettings()
{
QSettings settings("GAT", "Gribouillot");
settings.beginGroup("MainWindow");
settings.setValue("size", size());
settings.setValue("pos", pos());
settings.endGroup();
settings.beginGroup("spiral");
settings.setValue("constructAsOneItem", spiralDialog->isOneItem());
settings.setValue("baseDisplay", spiralDialog->isBaseDisplayed());
settings.setValue("showBaseCentersOnly", spiralDialog->showBaseCentersOnly());
settings.endGroup();
settings.beginGroup("preferences");
settings.setValue("minimap", ui->actionMinimap->isChecked());
settings.setValue("copyPixmaps", ui->actionCopy_pixmaps->isChecked());
settings.setValue("autosave", autosaveTimeout);
settings.endGroup();
QString projectFile = QDir::currentPath()+"/"+currentProjectName+".grib";
/*
* The current project will be the 'recent project' of the next session,
* except if the User didn't want to save it for some reason. So we verify
* if the current project was actually written on disk during maybeSave().
*/
if ( QFileInfo(projectFile).exists() )
settings.setValue("recentProject", projectFile);
//else
//settings.value() remains the last project saved on disk, or an empty string.
}
/**
* @brief Create or load a layer (load if a path is given).
* @details Ideally, addNewLayer should only call the GribouillotLayer constructor. However
* the new layer interface needs to be integrated in the mainWindow interface when
* it comes to tab insertion, deletion or labelling.
*/
void Gribouillot::addNewLayer(QString path)
{
GribouillotLayer *newLayer;
if (path.isEmpty())
{
//A new layer is created with the +tab.
newLayer = new GribouillotLayer(this);
}
else
{
//A new layer is loaded from an external file.
newLayer = new GribouillotLayer(path, this);
}
//Insert the new layer after 'currentLayer' and make it the visible layer
currentTabIndex = ui->gribTabWidget->insertAndDisplayTab(currentTabIndex+1,
newLayer,
newLayer->getLabel());
currentLayer = newLayer;
setWorkingLayer(currentLayer);
}
/**
* @brief Items from the working layer are fully activated.
* @details Items from other layers can only be selected as reference
* items for drawing tools, but can't be deleted, moved, etc.
*/
void Gribouillot::setWorkingLayer(GribouillotLayer* layer)
{
if( layer != nullptr )
{
bool belongsToLayer;
foreach(QGraphicsItem* item, scene->items())
{
belongsToLayer = layer->contains(item) ? true : false;
//enable item
item->setEnabled(true);
//enable or not specifics of this item
if( item->type() == PIXMAP )
item->setFlag(QGraphicsItem::ItemIsMovable, belongsToLayer);
if ( item->type() == SPIRAL)
item->setEnabled(belongsToLayer);
//to complete if necessary
}
//As a working layer is defined, allow full toolBar
fullToolbar();
}
}
/**
* @brief Clear the GraphicsView from drawing helps
*/
void Gribouillot::clearView()
{
if(currentDrawing != NONE)
{
//Clear any temporary graphic item: drawing help, etc
foreach(QGraphicsItem* item, scene->items())
{
if(item->type() == ARC_DRAWER
|| item->type() == POINT_ONRAIL
|| item->type() == SPIRAL_DRAWER
|| item->type() == SCALERULER)
{
//qDebug() << "clear: " << item;
scene->removeItem(item);
delete item;
}
}
//Also reset vector used to store drawing positions
drawingCoords.clear();
}
}
/**
* @brief Set a specific T-cursor and a noDrag view for drawing on scene
* @details Active when drawing actions are selected. By definition
* drawingView is exclusive of cursorDrag and cursorSelect modes.
*/
void Gribouillot::setDrawingView()
{
clearView();
setCursor(Qt::ArrowCursor);//Main window cursor
//Hack because of bug using weighted points proxy widgets
ui->zGraphicsView->setDragMode(QGraphicsView::ScrollHandDrag);//hack
ui->zGraphicsView->setCursor(drawingCursor);//GraphicsView cursor
ui->zGraphicsView->setDragMode(QGraphicsView::NoDrag);
}
/**
* @brief Set a generic Arrow cursor for item selection, etc.
* @details By definition selectionView is exclusive of drawingView
*/
void Gribouillot::setSelectionView()
{
/*
* It is not possible to manually check an action of the drawingGroup
* (see QActionGroup doc), EXCEPT when not one action is checked yet;
* which is the case here thanks to uncheckDrawingGroup().
*/
uncheckDrawingGroup();
ui->actionCursorSelect->setChecked(true);//works fine
//Checking is not triggering: manually trigger the corresponding SLOT
on_actionCursorSelect_triggered();
}
/**
* @brief Uncheck any toolbar action.
* @details Useful when using mapTabWidget toolButtons which can not be
* a part of the exclusive 'drawingGroup' QActionGroup.
*/
void Gribouillot::uncheckDrawingGroup()
{
if (drawingGroup->checkedAction() != 0)
{
/**
* An 'exclusive' actionGroup does not allow for manual checking/
* unchecking, so we need to setExclusive(false) first.
*/
drawingGroup->setExclusive(false);
drawingGroup->checkedAction()->setChecked(false);
drawingGroup->setExclusive(true);
}
}
/**
* @brief Reset the user interface when changing project
* @details Does not reset color wheel
*/
void Gribouillot::resetUi()
{
//reset MapTabWidget
ui->mapTabWidget->resetUi();
ui->gribTabWidget->reset();
GribouillotLayer::resetLayerIndex();
//reset scene and delete all items including backgroundMap
scene->clear();
scene->setSceneRect(0, 0, 0, 0);
//new empty background map
backgroundMap = new QGraphicsPixmapItem();
mapName = "";
//reset GPS dialog
delete gpsDialog; gpsDialog = nullptr;
gpsEnabled = false;
}
/**
* @brief Create a new Gps Dialog each time a new project is open/created
* @details gpsDialog is an extension of the mainWindow in the same way than mapTabWidget is.
*/
void Gribouillot::initGpsDialog()
{
gpsEnabled = false;
gpsDialog = new Dlg_setupGps(scene, this);
connect(gpsDialog, &Dlg_setupGps::accepted, this, &Gribouillot::acceptGpsDialog);
connect(gpsDialog, &Dlg_setupGps::rejected, this, &Gribouillot::hideGpsDialog);
connect(gpsDialog, &Dlg_setupGps::disableGPS, this, &Gribouillot::disableGps);
}
/**
* @brief Set the icon showing the drawing color
*/
void Gribouillot::setColorIcon(QColor color)
{
QPixmap pixmap(18, 18);
pixmap.fill(color);
ui->actionChooseColor->setIcon(QIcon(pixmap));
}
/********************* Private functions - Project management **********************/
/**
* @brief Factorize code for creating or opening a project.
*/
void Gribouillot::initProject()
{
if(!currentProjectName.isEmpty())
{
maybeSave();
//Reset interface to default settings
resetUi();
}
else
{
//Enable interface which is disabled on start
ui->centralwidget->setEnabled(true);
ui->toolBar->setEnabled(true);
restrictToolbar();
}
}
/**
* @brief Load an existing Gribouillot project
*/
void Gribouillot::openProject(QString gribFile)
{
if ( !gribFile.isEmpty() )
{
QFileInfo gribFileInfo = QFileInfo(gribFile);
QString notFound ="";
initProject();
QDir::setCurrent(gribFileInfo.absolutePath());
currentProjectName = gribFileInfo.baseName();
setWindowTitle("Gribouillot - "+currentProjectName);
QSettings settings(gribFile, QSettings::IniFormat);
//Restore background map
mapName = settings.value("map/mapName", "").toString();
mapPath = settings.value("map/mapPath", "").toString();
QFileInfo mapFileInfo = QFileInfo(mapPath);
/*
* Verify that mapPath is valid, the project folder may have
* been moved around the file system!
*/
if ( !mapFileInfo.exists() )
{
//map may still exist in the current dir
QString possibleLocalFile = QDir::currentPath()+"/"+mapFileInfo.fileName();
if ( QFile::exists(possibleLocalFile) )
mapPath = possibleLocalFile;
else
{
//can't find map file in the current Dir, ask user
dlg_changeMap dialog(mapPath, this);
if (dialog.exec() == QDialog::Accepted)
mapPath = dialog.getMapPath();
else
mapPath = QString();//left empty by user
}
}
if ( !mapPath.isEmpty() )
loadBackgroundMap(mapPath);
else
//Load project without a background map
QMessageBox::information(this,
currentProjectName,
tr("Starting without background map."),
QMessageBox::Ok);
//Restore drawing color and drawing width
drawingColor = settings.value("drawing/color", QColor(Qt::black)).value<QColor>();
setColorIcon(drawingColor);
drawingWidth = settings.value("drawing/width", 5).toInt();
QString iconString = ":/Resources/Icons/draw-width-"
+QString::number(drawingWidth)
+".png";
ui->actionChooseWidth->setIcon(QIcon(QPixmap(iconString)));
//Restore custom colors settings
for (int i = 0; i < QColorDialog::customCount(); ++i)
{
QColorDialog::setCustomColor(i,
settings.value("customColor/color"+QString::number(i), QColor(Qt::white)).value<QColor>());
}
//Restore scale spinBoxes.
//NOTE: setting new SpinBoxes values automatically triggers a new scale computation.
ui->mapTabWidget->ui->pxSpinBox->setValue(settings.value("scale/pxSpinBx", 0).toDouble());
ui->mapTabWidget->ui->kmSpinBox->setValue(settings.value("scale/kmSpinBx", 0).toDouble());
ui->mapTabWidget->ui->mKmComboBox->setCurrentIndex(settings.value("scale/mkmUnit", 0).toInt());
/*
* Restore GPS settings. Since initProject() was called, resetUi() was also
* called and the gpsDialog is currently a null pointer.
*/
initGpsDialog();
if (settings.contains("GPS/gpsEnabled"))
{
if(settings.value("GPS/gpsEnabled").toBool())
{
gpsEnabled = true;
//change gps icon to ON
ui->mapTabWidget->ui->gpsTlBtt->setIcon(QIcon(QPixmap(":/Resources/Icons/gps-on.png")));
}
}
//Restore layers from XML files located in current project folder
settings.beginGroup("layersOrder");
QMap<int, QString> layers;
/*
* Retrieve the indexed order of the layers with a QMap because QSettings
* write settings values on file in alphabetical order.
*/
foreach (QString layerKey, settings.allKeys())
{
int key = layerKey.toInt();
layers[key] = settings.value(layerKey).toString()+".xml";
}
//qDebug() << layers;
//Open layers in the correct order
foreach (QString layerPath, layers)
{
if ( QFile::exists(layerPath) )
addNewLayer(layerPath);
else
notFound += layerPath+", ";
}
if (!notFound.isEmpty())
QMessageBox::warning(this,
currentProjectName,
tr("Layer(s) file(s) missing in ")
+QDir::currentPath()
+" : "+notFound,
QMessageBox::Ok);
settings.endGroup();
}
}
/**
* @brief Load a picture as background map
*/
void Gribouillot::loadBackgroundMap(QString path)
{
if(!path.isEmpty())
{
if (mapName.isEmpty())
mapName = QFileInfo(path).baseName();
backgroundMap->setPixmap(QPixmap(path));
ui->gribTabWidget->setMapTab(path, mapName);
/* Limit the scrollable view to the background map*/
QRectF viewBoundingRect = backgroundMap->boundingRect();
ui->zGraphicsView->setSceneRect(viewBoundingRect);
/*
* Being the first item added to the scene, backgroundMap has the lowest
* Z value and is always in the background.
*/
scene->addItem(backgroundMap);
//Refresh minimap view
if (minimap != nullptr)
minimap->fitInView(backgroundMap, Qt::KeepAspectRatio);
}
}
/**
* @brief Save (only) the order of the layers after a layer deletion. Layers are not written on disk!!
* @details A deleted layer has its corresponding file definitively removed from disk.
*/
void Gribouillot::saveLayersOrder()
{
QSettings settings(currentProjectName+".grib",
QSettings::IniFormat);
settings.beginGroup("layersOrder");
settings.remove("");
for (int i = 1; i < (ui->gribTabWidget->count() -1); ++i)
{
GribouillotLayer *layer = dynamic_cast<GribouillotLayer *>(ui->gribTabWidget->widget(i));
settings.setValue(QString::number(i), layer->getLabel());
}
settings.endGroup();
}
/**
* @brief typical maybeSave() implementation asking the user to confirm the changes
* to the current project. Ui changes are saved separately.
*/
void Gribouillot::maybeSave()
{
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this,
tr("Confirm action"),
tr("Do you want to save your changes to project \n\"")+currentProjectName+"\"?",
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (ret == QMessageBox::Save)
saveProject();
}
/*********************** private slots ** MainWindow Menu ****************************/
/**
* @brief Pop-up a dialog to create a new Grib project
* @details A grib project is a directory containing:
* - a grib config file (.grib) with map path, scales, etc
* - the layers grib files
*/
void Gribouillot::on_actionNewProject_triggered()
{
Dlg_newGribproject dialog(this);
if (dialog.exec() == QDialog::Accepted)
{
QString newProjectPath = dialog.getProjectDirPath();
initProject();
//Set project path from dialog input
QDir::setCurrent(newProjectPath);
currentProjectName = QDir(newProjectPath).dirName();
setWindowTitle("Gribouillot - "+currentProjectName);
//Set map path from dialog input and copy map image if necessary
mapPath = dialog.getMapImagePath();
if( !mapPath.isEmpty() )
{
if( dialog.copyMapImageChkBx() )
{
QFileInfo mapFileInfo(mapPath);
QString newPath = newProjectPath +"/"+ mapFileInfo.fileName();
QFile::copy(mapPath, newPath);//wont overwrite if already there
mapPath = newPath;
}
loadBackgroundMap(mapPath);
}
//Copy loaded external pictures to project folder?
if ( dialog.copyExternalPictures() )
ui->actionCopy_pixmaps->setChecked(true);
//New GPS dialog
initGpsDialog();
statusBar()->showMessage(" "+tr("Project created successfully!"));
}
}
/**
* @brief Open an existing Gribouillot project from the menuBar
*/
void Gribouillot::on_actionOpenProject_triggered()
{
/*
* Note: getOpenFileName will trigger a Gtk warning message on Gtk UI:
* "GtkDialog mapped without a transient parent. This is discouraged."
* This is because Qt source calls a native Gtk window and Gtk can't have
* a Qt class as parent... Can't be fixed, must be ignored.
*/
QString gribFile = QFileDialog::getOpenFileName(this,
tr("Open an existing project"),
(QDir::currentPath())+"/..",
tr("Gribouillot project (*.grib)"));
openProject(gribFile);
}
/**
* @brief Import an external layer file to current project
*/
void Gribouillot::on_actionImport_layer_triggered()
{
Dlg_importLayer dialog(currentProjectName, this);
if (dialog.exec() == QDialog::Accepted)
{
QString newLayer = dialog.getLayerPath();
if( !newLayer.isEmpty() )
addNewLayer(newLayer);
statusBar()->showMessage(" "+tr("Layer imported successfully!"));
}
}
/**
* @brief save a Gribouillot project
* @details An unsaved project will still exist as an empty directory because of
* the way a new project is created.
*/
int Gribouillot::saveProject()
{
if(!currentProjectName.isEmpty())
{
//NB: We already are in the project directory
QSettings settings(currentProjectName+".grib",
QSettings::IniFormat);
//Save background map path & name if any
settings.beginGroup("map");
settings.setValue("mapPath", mapPath);
settings.setValue("mapName", mapName);
settings.endGroup();
//Save current drawing color and width
settings.beginGroup("drawing");
settings.setValue("color", drawingColor);
settings.setValue("width", drawingWidth);
settings.endGroup();
//Save User's custom colors
settings.beginGroup("customColors");
for (int i = 0; i < QColorDialog::customCount(); ++i)
{
settings.setValue("color"+QString::number(i),
QColorDialog::customColor(i));
}
settings.endGroup();
//Save scale spinBoxes
settings.beginGroup("scale");
settings.setValue("pxSpinBx", ui->mapTabWidget->ui->pxSpinBox->value());
settings.setValue("kmSpinBx", ui->mapTabWidget->ui->kmSpinBox->value());
settings.setValue("mkmUnit", ui->mapTabWidget->ui->mKmComboBox->currentIndex());
settings.endGroup();
//Save GPS data
settings.beginGroup("GPS");
settings.setValue("gpsEnabled", gpsEnabled);
settings.endGroup();
//Save layers
settings.beginGroup("layersOrder");
settings.remove("");
QStringList savedLabels;
/*
* Ask each layer to write its own XML file but save the tab order in the grib file.
* Some explanations on the loop values:
* int i = 1 and not int i = 0 becoz the first tab is no layer (mapTab)
* count() -1 and not just count() becoz the last tab is no layer (plusTab)
*/
for (int i = 1; i < (ui->gribTabWidget->count() -1); ++i)
{
GribouillotLayer *layer = dynamic_cast<GribouillotLayer *>(ui->gribTabWidget->widget(i));
QString label = layer->getLabel();
if ( savedLabels.contains(label) )
{
//Two layers with the same label would write into the same file on disk!
ui->gribTabWidget->setTabIcon(i,QIcon(QPixmap(":/Resources/Icons/exclamation.png")));
QMessageBox::warning(this,
tr("Layer name conflict!"),
label+tr(": another layer was already saved under "
"this name, please change the name of the layer."),
QMessageBox::Ok);
label = layer->newLabel(true);//a modal dialog asks for new label
ui->gribTabWidget->setTabIcon(i, QIcon());
ui->gribTabWidget->setTabText(i, label);
//At this point the layer has a new label, we can move on
}
if(layer->writeXML())
{
settings.setValue(QString::number(i), label);
savedLabels << label;
}
}
settings.endGroup();
QSettings appSettings("GAT", "Gribouillot");
QString projectFile = QDir::currentPath()+"/"+currentProjectName+".grib";
appSettings.setValue("recentProject", projectFile);
//User feedback
statusBar()->showMessage(tr("Project saved (")+
QString::number(savedLabels.size())+
tr(" layers saved)."));
return savedLabels.size();
}
return -1;
}
/**
* @brief Wrapper around saveProject to inform user on autosave
*/
void Gribouillot::autoSaveProject()
{
int layersSaved = saveProject();
if ( layersSaved != -1 )
statusBar()->showMessage(tr("Autosave done (")
+QString::number(layersSaved)
+tr(" layers saved)."));
}
/**
* @brief Start autosave timer
*/
void Gribouillot::newSaveTimeout()
{
if( autosaveTimeout != 0)
{
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &Gribouillot::autoSaveProject);
connect(timer, &QTimer::timeout, this, &Gribouillot::writeSettings);
timer->start(autosaveTimeout*60000);
ui->actionAutosave->setChecked(true);
}
else
ui->actionAutosave->setChecked(false);
}
/**
* @brief Export the view as an image file
*/
void Gribouillot::on_actionExportAs_triggered()
{
//calls to global functions
QString supportedFormats = getSupportedImageFormats();
QString defaultFilter = getDefaultImageFilter(supportedFormats);
QString filename = QFileDialog::getSaveFileName(this,
tr("Export current view"),
QDir::currentPath(),
supportedFormats,
&defaultFilter);
if(!filename.isNull())
{
QPixmap pix = ui->zGraphicsView->grab();
pix.save(filename);
}
}
/**
* @brief Close Gribouillot
*/
void Gribouillot::on_actionQuit_triggered()
{
close(); //will call closeEvent() then maybeSave()
}
/**
* @brief Display a Minimap to navigate through large map
*/
void Gribouillot::on_actionMinimap_toggled(bool isChecked)
{
if (isChecked && minimap == nullptr)
{
minimap = new Minimap(scene, ui->zGraphicsView);
minimap->fitInView(backgroundMap, Qt::KeepAspectRatio);
}
else
{
delete minimap;
minimap = nullptr;
}
}
/**
* @brief Tell the current layer to compute the center of Mass of its points
*/
void Gribouillot::on_actionCenterOfMass_triggered()
{
Item_point *centerOfMass = currentLayer->computeCoM(drawingColor, drawingWidth);
if ( centerOfMass != nullptr )
ui->zGraphicsView->centerOn(centerOfMass);
}
/**