-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
117 lines (103 loc) · 3.74 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "documentview.h"
#include <QActionGroup>
#include <QFileDialog>
#include <QDir>
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
// group view actions
QActionGroup* group = new QActionGroup(this);
group->addAction(ui->actionText);
group->addAction(ui->actionSingle_column);
group->addAction(ui->actionDouble_column);
// connect exit action
connect(ui->actionExit, &QAction::triggered, qApp, QApplication::quit);
}
MainWindow::~MainWindow(){
delete ui;
}
QString MainWindow::nextDocumentName(const QString& baseName) const{
// assemble list of names
QStringList names;
for (QMdiSubWindow* window : ui->mdiArea->subWindowList()){
DocumentView* documentView = static_cast<DocumentView*>(window->widget());
names << documentView->documentName();
}
// check is base name exists
if (!names.contains(baseName))
return baseName;
// try different indexes until suitable one is found
unsigned int index = 2;
while(true){
QString proposedName = baseName + " " + QString::number(index);
if (!names.contains(proposedName))
return proposedName;
index++;
}
}
void MainWindow::on_actionNew_triggered(){
//create
DocumentView * document = new DocumentView(nextDocumentName(tr("New document")), this);
connect(document, &DocumentView::windowTitleChanged, this, &MainWindow::activeWindowTitleChanged);
document->setAttribute(Qt::WA_DeleteOnClose);
ui->mdiArea->addSubWindow(document);
document->show();
}
void MainWindow::on_actionClose_triggered(){
ui->mdiArea->closeActiveSubWindow();
}
void MainWindow::on_actionOpen_triggered(){
QString filename = QFileDialog::getOpenFileName(this, tr("Open file"), QDir::homePath(), tr("Json files (*.json);;All files (*.*)"));
if (filename != ""){
QFile file(filename);
DocumentView* document = new DocumentView(file, this);
if (!document->isValid()){
delete document;
return;
}
connect(document, &DocumentView::windowTitleChanged, this, &MainWindow::activeWindowTitleChanged);
document->setAttribute(Qt::WA_DeleteOnClose);
ui->mdiArea->addSubWindow(document);
document->show();
}
}
void MainWindow::on_mdiArea_subWindowActivated(QMdiSubWindow *window){
bool enabled;
if (!window){
setWindowTitle("JSON edit");
enabled = false;
}
else {
DocumentView* documentView = static_cast<DocumentView*>(window->widget());
setWindowTitle("JSON edit - " + documentView->windowTitle());
enabled = true;
}
ui->actionSave->setEnabled(enabled);
ui->actionSave_as->setEnabled(enabled);
ui->actionClose->setEnabled(enabled);
ui->actionText->setEnabled(enabled);
ui->actionSingle_column->setEnabled(enabled);
ui->actionDouble_column->setEnabled(enabled);
}
void MainWindow::activeWindowTitleChanged(const QString &title){
setWindowTitle("JSON edit - " + title);
}
void MainWindow::on_actionSave_as_triggered(){
DocumentView* documentView = static_cast<DocumentView*>(ui->mdiArea->activeSubWindow()->widget());
documentView->saveAs();
}
void MainWindow::on_actionSave_triggered(){
DocumentView* documentView = static_cast<DocumentView*>(ui->mdiArea->activeSubWindow()->widget());
documentView->save();
}
void MainWindow::closeEvent(QCloseEvent *event){
for (QMdiSubWindow* window : ui->mdiArea->subWindowList()){
DocumentView* documentView = static_cast<DocumentView*>(window->widget());
if (!documentView->close()){
event->ignore();
return;
}
event->accept();
}
}