1
1
#include " MainWindow.h"
2
2
#include " Syntax.h"
3
3
#include " Tree.h"
4
+ #include " CodeEditor.h"
4
5
5
6
#include < QMenuBar>
6
7
#include < QFileDialog>
14
15
15
16
MainWindow::MainWindow (QWidget *parent)
16
17
: QMainWindow(parent),
17
- editor(new CodeEditor(this )),
18
- syntax(new Syntax(editor->document ())),
18
+ editor(std::make_unique< CodeEditor> (this )),
19
+ syntax(std::make_unique< Syntax> (editor->document ())),
19
20
tree(nullptr )
20
21
{
21
22
setWindowTitle (" CodeAstra ~ Code Editor" );
@@ -38,10 +39,9 @@ void MainWindow::initTree()
38
39
QSplitter *splitter = new QSplitter (Qt::Horizontal, this );
39
40
setCentralWidget (splitter);
40
41
41
- tree = new Tree (splitter, this );
42
-
43
- splitter->addWidget (editor);
42
+ tree = std::make_unique<Tree>(splitter, this );
44
43
44
+ splitter->addWidget (editor.get ());
45
45
splitter->setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding);
46
46
splitter->setHandleWidth (5 );
47
47
splitter->setSizes (QList<int >() << 150 << 800 );
@@ -158,27 +158,7 @@ void MainWindow::openFile()
158
158
" C++ Files (*.cpp *.h);;Text Files (*.txt);;All Files (*)" );
159
159
if (!fileName.isEmpty ())
160
160
{
161
- QFile file (fileName);
162
- if (!file.open (QFile::ReadOnly | QFile::Text))
163
- {
164
- QMessageBox::warning (this , " Error" , " Cannot open file: " + file.errorString ());
165
- return ;
166
- }
167
-
168
- QTextStream in (&file);
169
- if (editor)
170
- {
171
- editor->setPlainText (in.readAll ());
172
- }
173
- else
174
- {
175
- QMessageBox::critical (this , " Error" , " Editor is not initialized." );
176
- }
177
- file.close ();
178
-
179
- currentFileName = fileName;
180
-
181
- setWindowTitle (" CodeAstra ~ " + QFileInfo (fileName).fileName ());
161
+ loadFileInEditor (fileName);
182
162
}
183
163
}
184
164
@@ -202,6 +182,11 @@ void MainWindow::saveFile()
202
182
{
203
183
out << editor->toPlainText ();
204
184
}
185
+ else
186
+ {
187
+ QMessageBox::critical (this , " Error" , " Editor is not initialized." );
188
+ return ;
189
+ }
205
190
file.close ();
206
191
207
192
statusBar ()->showMessage (" File saved successfully." , 2000 );
@@ -229,7 +214,15 @@ void MainWindow::loadFileInEditor(const QString &filePath)
229
214
}
230
215
231
216
QTextStream in (&file);
232
- editor->setPlainText (in.readAll ());
217
+ if (editor)
218
+ {
219
+ editor->setPlainText (in.readAll ());
220
+ }
221
+ else
222
+ {
223
+ QMessageBox::critical (this , " Error" , " Editor is not initialized." );
224
+ return ;
225
+ }
233
226
file.close ();
234
227
235
228
currentFileName = filePath;
0 commit comments