aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/mainwindow.cpp
blob: 5776db7f2387e0f24208af37f4cf1c82aac335e8 (plain)
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
#include "debugtools.h"

#include "ui/metadatadialog.h"
#include "ui/mainwindow.h"
#include "ui_mainwindow.h"

#include <iostream>

#include <QMessageBox>
#include <QFileDialog>

MainWindow::MainWindow(samb::Structogram *structogram, QWidget *parent) :
    QMainWindow(parent),
    _ui(new Ui::MainWindow),
    _structogram(structogram)
{
    _ui->setupUi(this);

    toolButtonsEnabled((_structogram != nullptr));
}

MainWindow::~MainWindow()
{
    delete _ui;
}

/**** private slots ****/

void MainWindow::on_newButton_clicked()
{
    if (_structogram != nullptr)
        if (!askSaveDialog())
            return;

    delete _structogram;
    _structogram = nullptr;

    MetadataDialog *dialog = new MetadataDialog(this);
    if (dialog->exec() == QDialog::Accepted) {
        _structogram = new samb::Structogram(dialog->title(), dialog->author());
        toolButtonsEnabled(true);
    } else {
        toolButtonsEnabled(false);
    }

    delete dialog;
}

void MainWindow::on_openButton_clicked()
{
    if (_structogram != nullptr)
        if (!askSaveDialog())
            return;

    QString fileName = QFileDialog::getOpenFileName(this,
        tr("Load diagram"), "", "NS Diagram (*.nsdg);;All Files (*)");

    if (fileName.isEmpty())
        return;

    debug_err("load dialog is unimplemented");

    // toolButtonsEnabled(true);
}

void MainWindow::on_saveButton_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,
        tr("Save diagram"), "", tr("NS Diagram (*.nsdg);;All Files (*)"));

    if (fileName.isEmpty())
        return;

    debug_err("saved dialog is unimplemented");

    // toolButtonsEnabled(true);
}

void MainWindow::on_refreshButton_clicked()
{

}

void MainWindow::on_metadataButton_clicked()
{
    if (_structogram == nullptr)
        return;

    MetadataDialog *dialog = new MetadataDialog(this);
    dialog->setMetadata(_structogram->title(), _structogram->author());

    if (dialog->exec() == QDialog::Accepted) {
        _structogram->title(dialog->title());
        _structogram->author(dialog->author());
    }
}

void MainWindow::on_newStatementButton_clicked()
{

}

/**** private methods ****/

/* @brief open a save dialog
 * @return true if operation completed successfully
 */
bool MainWindow::askSaveDialog()
{
    // TODO: check if it has already been saved once
    auto reply = QMessageBox::question(this,
                                       "Save " + _structogram->title(),
                                       "'" + _structogram->title() + "' is not saved.\nDo you want to save it?",
                                       QMessageBox::Save, QMessageBox::Discard);

    if (reply == QMessageBox::Save) {
        on_saveButton_clicked();

        // TODO: check if file has been ACTUALLY saved
        return true;
    } else {
        return true;
    }

}

/* @brief control ui elements that are relate to _structogram member
 */
void MainWindow::toolButtonsEnabled(bool state)
{
    if (_structogram == nullptr && state == true) {
        debug_err("called enableToolButtons(true) but _structogram is nullptr");
        return;
    }

    // buttons
    _ui->refreshButton->setEnabled(state);
    _ui->metadataButton->setEnabled(state);
    _ui->newStatementButton->setEnabled(state);

    // actions
    _ui->actionSave->setEnabled(state);
    _ui->actionSaveAs->setEnabled(state);
    _ui->actionClose->setEnabled(state);
}