-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
657 lines (564 loc) · 22 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->widgetManager->clear();
// Avvio la lettura delle impostazioni dal disco
settings.setFilePath(formatPathForOs(QDir::currentPath(), QStringList("settings/settings.stc")));
// Controllo se il file di impostazioni esiste
if(QFile::exists(settings.getFilePath())){
// Notifico che ho trovato le impostazioni
qInfo() << "File di impostazioni esistente, avvio il caricamento. " << endl;
// Avvio la lettura delle impostazioni
SettingsReader *settingsReader = new SettingsReader();
settingsReader->setSettingsObject(&settings);
settingsReader->start();
// Connetto i thread
QObject::connect(settingsReader, &SettingsReader::finished, this, &MainWindow::loadSearchPathFromFile);
}
else{
qInfo() << "File di impostazioni non trovato, avvio la scrittura sul disco. " << endl;
// Avvio la scrittura delle impostazioni
writeDefaultSettings(&settings);
}
// Imposto la barra di stato
ui->statusbar->addPermanentWidget(statusBar, 1);
// Aggiorno il titolo
updateTitleInfo();
// Mostro la scheda home
showHomePage();
}
void MainWindow::showHomePage(){
// Aggiungo il tab home
HomePage *home = new HomePage( nullptr);
ui->widgetManager->addTab(home, "Home");
tabs.append(home);
// Connetto gli eventi
QObject::connect(home, &HomePage::newPackageRequired, this, &MainWindow::onNewPackageRequired);
QObject::connect(home, &HomePage::openPackageRequired, this, &MainWindow::onOpenPackageRequired);
}
void MainWindow::onNewPackageRequired(){
newPackage();
}
void MainWindow::onOpenPackageRequired(){
openPackage();
}
void MainWindow::openPackage(){
// Cartella in cui aprire il file browser
QString file_browser_base_path = resolvePath(settings.getKeyValue("paths", "default-package-path"));
// Controllo se esiste un valore
if(file_browser_base_path == ""){
// Uso il percorso home del sistema operativo
file_browser_base_path = getHomePath();
}
qInfo() << "Apro browser " << file_browser_base_path << endl;
// Chiedo una directory in cui cercare il pacchetto partendo dal percorso giusto
QString dir = QFileDialog::getExistingDirectory(this, "Selezionare la cartella del pacchetto", file_browser_base_path);
// Controllo che l'utente abbia accettato
if(dir != ""){
// Avvio un thread di lettura delle informazioni sul pacchetto
PackageReader *reader = new PackageReader();
reader->setPath(dir);
reader->start();
// Attendo che finisca
while(reader->isRunning()){
// Attendo
}
// Pacchetto letto
Package* pack = reader->getPackage();
// Controllo che il pacchetto sia stato letto correttamente
if(pack != nullptr){
// Controllo che non esista già nella vista
if(!isOpenedPackage(pack)){
// Aggiungo il pacchetto
auto p = addPackageToView(pack);
// Controllo che sia stato aggiunto
if(p != nullptr){
tabs.append(p);
// Aggiungo il pacchetto alla lista dei pacchetti
packageList.append(pack);
qInfo() << "Pacchetto caricato con successo: " << pack->getName() << endl;
}
}
else{
// Mostro un warning
QMessageBox::warning(this, "Attenzione", "Il pacchetto che si è cercato di aprire è già aperto. ");
qInfo() << pack->getName() << " tentativo di aprirlo due volte. " << endl;
}
}
else{
// Mostro errori
QMessageBox::critical(this, "Errore", "Impossibile aprire il pacchetto. ");
qInfo() << "Errore nella apertura del pacchetto. " << endl;
}
}
}
void MainWindow::updateTitleInfo(){
// Titolo base
QString title = QString("ArduinoPacMan %1 ").arg(ARDUINO_PACMAN_VERSION.toString());
// Determino se il tab corrente è un pacchetto
if(currentTabIsPackage()){
// Scrivo il nome del pacchetto
title += "| ";
title += getCurrentPackage()->getName();
}
else{
title += "| Scheda home";
}
// Imposto il titolo alla finestra
setWindowTitle(title);
}
void MainWindow::updateStatusBar(){
// Scrivo il numero di pacchetti aperti
statusBar->setPackagesCount(packageList.count());
// Controllo se il tab corrente è un pacchetto
if(currentTabIsPackage()){
// Imposto il percorso del pacchetto corrente
statusBar->setCurrentPackagePath(getCurrentPackage()->getSavePath());
statusBar->setLibraryCount(getCurrentPackage()->getLibraryCount());
// Show all the controls
statusBar->showLibrariesCount();
statusBar->showPackagesCount();
// Enable actions for packages
ui->menuPacchetto->setEnabled(true);
ui->menuGestione->setEnabled(true);
ui->actionApri_nel_gestore_dei_file->setEnabled(true);
ui->actionApri_nel_terminale->setEnabled(true);
}
else{
statusBar->setCurrentPackagePath("Scheda home");
// Hide all the uncecessary controls
statusBar->hideLibrariesCount();
statusBar->hidePackagesCount();
// Disable actions for packages
ui->menuPacchetto->setEnabled(false);
ui->menuGestione->setEnabled(false);
ui->actionApri_nel_gestore_dei_file->setEnabled(false);
ui->actionApri_nel_terminale->setEnabled(false);
}
}
bool MainWindow::currentTabIsPackage(){
if(getCurrentPackage() != nullptr){
return true;
}
return false;
}
void MainWindow::newPackage(){
NewPackageDialog *dialog = new NewPackageDialog(&settings);
int res = dialog->exec();
if(res == QDialog::Accepted){
Package *package = new Package();
// Imposto le generalità
package->setName(dialog->getPackageName());
package->setSavePath(dialog->getPackagePath());
// Imposto i percorsi legati ai sorgenti del progetto
package->setSourcesPath(dialog->getSourcesPath());
package->setMainSourcePath(dialog->getMainFilePath());
// Imposto la descrizione del pacchetto
package->setDescription(dialog->getDescription());
// Imposto la data e l'ora di creazione del pacchetto
package->setCreationDate(QDate::currentDate());
package->setCreationTime(QTime::currentTime());
// Imposto l'orario di ultima modifica
package->setLastModifyDate(QDate::currentDate());
package->setLastModifyTime(QTime::currentTime());
qInfo() << " descrizione " << dialog->getDescription() << endl;
package->create();
// Aggiungo il pacchetto alla lista
packageList.append(package);
// Aggiungo il pacchetto al gestore
tabs.append(addPackageToView(package));
}
// Aggiorno il titolo
updateTitleInfo();
// Aggiorno la barra di stato
updateStatusBar();
}
bool MainWindow::isOpenedPackage(Package *package){
for(int x = 0; x < packageList.count(); x++){
if(package->equals(packageList[x])){
return true;
}
}
return false;
}
Tab* MainWindow::addPackageToView(Package *pack){
// Controllo che non sia un puntatore nullo
if(pack != nullptr){
// Controllo che abbia un nome e un percorso
if(pack->getName() != "" && pack->getCompletePath() != ""){
// Imposto l'indice del pacchetto
pack->addTag(QString::number(packageList.count()));
// Mostro il pacchetto
PackageTab *tab = new PackageTab(pack);
// Imposto i tag del tab
tab->setTags("<package>");
// Aggiungo
ui->widgetManager->addTab(tab, pack->getName());
// Percorso dei sorgenti
QString sources_path = pack->getSourcesPath();
// File sorgente principale
QString main_file_path = pack->getMainSourcePath();
// Controllo che il pacchetto abbia dei sorgenti
if(sources_path != "" && QDir(sources_path).exists()){
// Controllo che abbia un file principale
if(main_file_path != "" && QFile::exists(main_file_path)){
// Carico i suoi sorgenti
auto *loader = new SourcesLoader();
loader->setDestination(tab->getFileBrowser());
loader->setSearchPath(pack->getSourcesPath());
qInfo() << "Carico sorgenti per il percorso " << pack->getSourcesPath() << endl;
loader->setPackage(pack);
// Avvio il thread
loader->start(QThread::HighPriority);
// Controllo che il file impostato come sorgente principale sia effettivamente un sorgente
if(SourceInfo::fileIsSource(main_file_path)){
// Avvio il caricamento dei sorgenti per il pacchetto
auto *lister = new SrcDependencyLister();
// Imposto il pacchetto
lister->setPackage(pack);
// Imposto il widget
lister->setWidget(tab->getDependencyBrowser());
lister->start();
}
else{
// Non si tratta di un vero sorgente supportato
QMessageBox::critical(this, "Errore", "Il file impostato come sorgente principale ha come estensione \n" + getFileExtension(main_file_path) + " che non rappresenta un file sorgente supportato. \nNon e' possibile caricare le dipendenze da questo file. ");
}
}
else{
QMessageBox::warning(this, "Attenzione", "Il pacchetto che si è tentato di aprire non ha un file sorgente principale o, se è stato impostato, il file non esiste sul disco. ");
qInfo() << "Pacchetto " << pack->getName() << " senza file principale corretto. " << endl;
}
}
else{
QMessageBox::warning(this, "Attenzione", "Il pacchetto che si è tentato di aprire non ha dei sorgenti associati. Non verranno caricate dipendenze fino a quando non saranno impostati. ");
qInfo() << "Pacchetto " << pack->getName() << " senza sorgenti" << endl;
}
// Add the found libraries to the object view
for(Natural x = 0; x < mk(foundLibraries.count()); x++){
tab->addLibraryToList(foundLibraries[x]);
}
// Update indicators
updateStatusBar();
updateTitleInfo();
return tab;
}
else{
QMessageBox::critical(this, "Errore", "Errore durante l'apertura del pacchetto. Non è stato possibile leggerlo correttamente o non si trattava di un pacchetto formattato correttamente. ");
}
}
return nullptr;
}
void MainWindow::on_actionNuova_finestra_triggered()
{
// Creo una nuova finestra
auto *mainWin = new MainWindow();
mainWin->show();
}
void MainWindow::on_pushButton_clicked()
{
}
void MainWindow::on_actionNuovo_pacchetto_triggered()
{
newPackage();
}
void MainWindow::aggiungiPercorsoRicercaTriggered(QString path){
librariesSearchPath.addPath(path);
}
void MainWindow::on_packageManager_currentChanged(int index)
{
// Aggiorno il titolo
updateTitleInfo();
}
void MainWindow::rimuoviPercorsoRicerca(QString path){
// Rimuovo l'elemento con quel testo
librariesSearchPath.removeElemByPath(path);
}
void MainWindow::modificaPercorsoRicerca(Natural index, QString old_val, QString new_val){
librariesSearchPath.editElem(index, new_val);
}
void MainWindow::loadSearchPathFromFile(){
// Avvio il caricamento dei percorsi di ricerca dal percorso specificato nelle impostazioni
SearchPathListReader *reader = new SearchPathListReader();
// Formatto il percorso su cui leggere o scrivere le informazioni
QString use_path = resolvePath(settings.getKeyValue("paths", "libsearchpath"));
// Imposto il file da leggere al thread di lettura
reader->setFile(use_path);
// Imposto il file da leggere alla classe per la gestione della lista
librariesSearchPath.setSavePath(use_path);
// Imposto la lista a cui aggiungere i risultati
reader->setDestinationList(&librariesSearchPath);
// Avvio il thread
reader->start();
// Connect the events
QObject::connect(reader, &SearchPathListReader::finished, this, &MainWindow::loadLibraryList);
qInfo() << "Caricati percorsi ricerca librerie. " << endl;
}
void MainWindow::loadLibraryList(){
// Avvio i vari thread per il caricamento delle informazioni per le librerie
for(Natural x = 0; x < mk(librariesSearchPath.getListCount()); x++){
// Creo il thread
auto *loader = new LibrariesLoader();
loader->setLibrariesDestination(&foundLibraries);
loader->setSearchPath(librariesSearchPath.getPath(x));
loader->start();
}
}
void MainWindow::on_actionPercorsi_ricerca_librerie_triggered()
{
// Creo la finestra
SearchPathManager *man = new SearchPathManager();
man->setSettings(&settings);
// Connetto il segnale di aggiunta di una nuova entry el metodo di gestione
QObject::connect(man, &SearchPathManager::pathAdded, this, &MainWindow::aggiungiPercorsoRicercaTriggered);
// Connetto il segnale di rimozione degli elementi
QObject::connect(man, &SearchPathManager::pathRemoved, this, &MainWindow::rimuoviPercorsoRicerca);
// Connetto il segnale per la modifica degli elementi
QObject::connect(man, &SearchPathManager::pathEdited, this, &MainWindow::modificaPercorsoRicerca);
// Controllo se sono presenti dei percorsi già salvati
if(librariesSearchPath.getListCount() > 0){
man->loadFromList(librariesSearchPath.getEntryList());
}
int res = man->exec();
// Controllo se l'utente ha accettato
if(res == QDialog::Accepted){
// Avvio uno scrittore
SearchPathListWriter *writer = new SearchPathListWriter();
writer->setLibraryList(&librariesSearchPath);
// Avvio il thread di scrittura
writer->start();
}
}
void MainWindow::on_actionApri_pacchetto_triggered()
{
openPackage();
}
void MainWindow::on_actionScheda_home_triggered()
{
showHomePage();
}
void MainWindow::on_actionSchede_a_sinistra_triggered()
{
if(ui->actionSchede_a_sinistra->isChecked()){
ui->widgetManager->setTabPosition(QTabWidget::West);
}
else{
ui->widgetManager->setTabPosition(QTabWidget::North);
}
}
int MainWindow::getPackageIndex(Natural tabIndex){
// Tab corrente
Tab *curTab = nullptr;
// Numero di pagine home trovate
Natural homeFound = 0;
// Numero di pacchetti trovati
int packagesFound = -1;
// Scorro tutti i tab
for(Natural x = 0; x <= tabIndex; x++){
if(mk(tabs.count()) > x){
// Ottengo il tab con quell' indice
curTab = tabs[x];
if(curTab != nullptr){
if(curTab->getInfo() == "<home>"){
homeFound ++;
}
else{
packagesFound ++;
}
}
}
}
// Controllo che esista un elemento con quell' indice
if(mk(packageList.count()) > packagesFound && packagesFound >= 0){
return packagesFound;
}
return -1;
}
void MainWindow::on_widgetManager_currentChanged(int index)
{
// Aggiorno la barra di stato
updateStatusBar();
// Aggiorno il titolo
updateTitleInfo();
}
Package* MainWindow::getCurrentPackage(){
// Indice del pacchetto nella sua lista
int index = getPackageIndex(ui->widgetManager->currentIndex());
if(index >= 0 && packageList.count() > index){
return packageList[index];
}
return nullptr;
}
MainWindow::~MainWindow()
{
delete ui;
// Rimuovo tutti i pacchetti
for(Natural x = 0; x < mk(packageList.count()); x++){
delete packageList[x];
}
}
void MainWindow::on_actionPropriet_pacchetto_triggered()
{
// Ottengo il pacchetto corrente
auto cur = getCurrentPackage();
// Controllo se effettivamente esiste
if(cur != nullptr){
PackageInfo *info = new PackageInfo(cur);
info->show();
}
else{
QMessageBox::critical(this, "Errore", "Per visualizzare le proprietà di un pacchetto è necessario prima selezionarne uno nel controllo multi-tab o aprirne uno nuovo. ");
}
}
Package* MainWindow::getPackageFromTabIndex(Natural tabIndex){
// Ottengo il numero del pacchetto
int num = getPackageIndex(tabIndex);
if(num > 0){
// Controllo se esiste un pacchetto con quell' indice
if(packageList.count() > num){
return packageList[num];
}
qInfo() << "Errore: numero di pacchetto rintracciato a partire dall' indice tab " << tabIndex << " ma non corrisponde a nessun pacchetto in memoria. " << endl;
return nullptr;
}
return nullptr;
}
void MainWindow::on_widgetManager_tabCloseRequested(int index)
{
removeTab(index);
}
void MainWindow::removeTab(Natural tabIndex){
// Ottengo l'indice del pacchetto
int pack = getPackageIndex(tabIndex);
if(pack >= 0 && pack < packageList.count()){
// Tolgo il pacchetto dalla memoria
packageList.removeAt(pack);
}
// Rimuovo il tab
ui->widgetManager->removeTab(tabIndex);
}
void MainWindow::on_actionChiudi_tutte_le_schede_aperte_triggered()
{
int dim = ui->widgetManager->count();
// Number of removed items
int removed_items = 0;
for(int x = 0; x < dim; x++){
// Calculate the index of the package
int index = getPackageIndex(x);
// Check if the tab with this index is a package manager
if(index >= 0){
// Remove the package from the list
packageList.removeAt(index);
}
tabs.removeAt(x - removed_items);
removed_items ++;
}
// Clear all the items
ui->widgetManager->clear();
// Aggiorno il titolo
updateTitleInfo();
// Aggiorno la barra di stato
updateStatusBar();
}
void MainWindow::on_actionApri_nel_terminale_triggered()
{
// Current package
auto p = getCurrentPackage();
// Check if it's a valid package
if(p != nullptr){
// Ottengo la versione del sistema operativo corrente
auto os = QOperatingSystemVersion::currentType();
// Name of the key to search
QString key_name = "";
switch(os){
case QOperatingSystemVersion::Windows:
key_name = "open-in-terminal-windows";
break;
case QOperatingSystemVersion::MacOS:
key_name = "open-in-terminal-macos";
break;
default:
key_name = "open-in-terminal-linux";
break;
}
// Get the key
QString c = settings.getKeyValue("os-selective", key_name);
QString comm = c + "\"" + p->getCompletePath() + "\"";
system(comm.toLocal8Bit().data());
}
}
void MainWindow::on_actionApri_nel_gestore_dei_file_triggered()
{
// Current package
auto p = getCurrentPackage();
// Check if it's a valid package
if(p != nullptr){
// Ottengo la versione del sistema operativo corrente
auto os = QOperatingSystemVersion::currentType();
// Name of the key to search
QString key_name = "";
switch(os){
case QOperatingSystemVersion::Windows:
key_name = "open-in-files-windows";
break;
case QOperatingSystemVersion::MacOS:
key_name = "open-in-files-macos";
break;
default:
key_name = "open-in-files-linux";
break;
}
// Get the key
QString c = settings.getKeyValue("os-selective", key_name);
// Start the process
QProcess *process = new QProcess();
process->setProgram(c);
process->setArguments(QStringList(p->getCompletePath()));
process->start();
}
}
void MainWindow::on_actionEsci_triggered()
{
this->close();
}
void MainWindow::on_actionChiudi_tutte_le_altre_schede_triggered()
{
if(ui->widgetManager->currentIndex() >= 0){
// Get the current tab index
int curTab = mk(ui->widgetManager->currentIndex());
int tabCount = ui->widgetManager->count();
// Elaborate all the tabs
for(int x = 0; x < tabCount; x++){
if(x != curTab){
// Calculate the current tab package index
int index = getPackageIndex(x);
// Check if cur tab is package
if(index >= 0){
packageList.removeAt(index);
}
tabs.removeAt(x);
// Remove the tab
ui->widgetManager->removeTab(x);
}
}
}
}
void MainWindow::reloadPackageTab(Natural tabIndex){
if(tabIndex <= ui->widgetManager->count()){
// The index of the package
int package = getPackageIndex(tabIndex);
// Get the package with that index
if(package >= 0){
// Remove the tab and re-add it
removeTab(tabIndex);
addPackageToView(packageList[package]);
}
}
}
void MainWindow::on_actionReloadPackage_triggered()
{
}