-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
180 lines (129 loc) · 5.06 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <iostream>
#include <fstream>
using namespace std;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
QMessageBox::information(this, tr("Notice"), tr("This is a Work in Progress. Some features may not work properly."));
// Dialogs
mSettingsDialog = new SettingsDialog(this);
mSettingsDialog->setReferenceTextEdit(ui->codeEdit);
// Menu bar actions
connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newFile);
connect(ui->actionLoad, &QAction::triggered, this, &MainWindow::loadFile);
connect(ui->actionSave, &QAction::triggered, this, &MainWindow::saveFile);
connect(ui->actionSave_as, &QAction::triggered, this, &MainWindow::saveFileAs);
connect(ui->actionSettings, &QAction::triggered, this, &MainWindow::openSettingsWindow);
// Button actions
connect(ui->BtnNewfile, &QPushButton::clicked, this, &MainWindow::newFile);
connect(ui->BtnLoadfile, &QPushButton::clicked, this, &MainWindow::loadFile);
connect(ui->BtnSave, &QPushButton::clicked, this, &MainWindow::saveFile);
connect(ui->BtnSaveAs, &QPushButton::clicked, this, &MainWindow::saveFileAs);
connect(ui->BtnSettings, &QPushButton::clicked, this, &MainWindow::openSettingsWindow);
// Anchor widgets
mVFullAnchorWidgets.push_back(ui->codeEdit);
mVWESAnchorWidgets.push_back(ui->Label);
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::resizeEvent(QResizeEvent* e) {
QMainWindow::resizeEvent(e);
static QSize previous_size;
// Do not do anything on first resize call.
static bool first_resize = true;
if (first_resize) {
first_resize = false;
previous_size = size(); // But do save the fresh size of the main window for next resize.
return;
}
// Anchoring
// Calculate difference in main window resize and anchor all widgets assigned for anchoring.
QSize diff_size = size() - previous_size;
for (QWidget*& awidget : mVFullAnchorWidgets)
awidget->resize(awidget->size() + diff_size);
for (QWidget*& awidget : mVWESAnchorWidgets) {
QSize newsize = awidget->size();
newsize = QSize(newsize.width() + diff_size.width(), newsize.height());
awidget->move(awidget->x(), awidget->y() + diff_size.height());
awidget->resize(newsize);
}
previous_size = size();
}
void MainWindow::newFile() {
mFileName = "";
ui->Label->setText("No file");
ui->codeEdit->clear();
}
void MainWindow::loadFile() {
QString filename = QFileDialog::getOpenFileName(this,
tr("Open file"), "",
tr("All Files (*)"));
if (filename.isEmpty())
return;
else {
ifstream file(filename.toLocal8Bit().constData());
if (!file) {
QMessageBox::information(this, tr("Error"), tr("File not found."));
return;
}
string read = string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
ui->codeEdit->clear();
ui->codeEdit->document()->setPlainText(QString::fromLocal8Bit(read.c_str()));
mFileName = filename.toLocal8Bit().constData();
ui->Label->setText(QString::fromLocal8Bit(mFileName.c_str()));
}
}
void MainWindow::saveFile() {
string code = ui->codeEdit->toPlainText().toLocal8Bit().constData();
if (!mFileName.empty()) {
ofstream file(mFileName, fstream::out | fstream::trunc);
if (!file) {
QMessageBox::information(this, tr("Error"), tr("Unable to save file."));
QApplication::quit();
}
file << code;
}
else {
QString filename = QFileDialog::getSaveFileName(this,
tr("Save to file"), "",
tr("All Files (*)"));
if (filename.isEmpty())
return;
else {
fstream file(filename.toLocal8Bit().constData(), fstream::out);
if (!file) {
QMessageBox::information(this, tr("Error"), tr("Unable to save file."));
return;
}
file << code;
mFileName = filename.toLocal8Bit().constData();
ui->Label->setText(QString::fromLocal8Bit(mFileName.c_str()));
}
}
}
void MainWindow::saveFileAs() {
string code = ui->codeEdit->toPlainText().toLocal8Bit().constData();
QString filename = QFileDialog::getSaveFileName(this,
tr("Save to file"), "",
tr("All Files (*)"));
if (filename.isEmpty())
return;
else {
fstream file(filename.toLocal8Bit().constData(), fstream::out);
if (!file) {
QMessageBox::information(this, tr("Error"), tr("Unable to save file."));
return;
}
file << code;
mFileName = filename.toLocal8Bit().constData();
ui->Label->setText(QString::fromLocal8Bit(mFileName.c_str()));
}
}
void MainWindow::openSettingsWindow() {
mSettingsDialog->setEditorTextFont(ui->codeEdit->font());
mSettingsDialog->show();
}