src/main.cpp

changeset 202
b05af0bab735
parent 201
5d201ee4a9c3
child 203
1909a0123c72
equal deleted inserted replaced
201:5d201ee4a9c3 202:b05af0bab735
1 #include <QApplication> 1 #include <QApplication>
2 #include <QFileDialog> 2 #include <QFileDialog>
3 #include <QMessageBox> 3 #include <QMessageBox>
4 #include <QMdiSubWindow>
4 #include "mainwindow.h" 5 #include "mainwindow.h"
5 #include "ui_mainwindow.h" 6 #include "ui_mainwindow.h"
6 #include "version.h" 7 #include "version.h"
7 #include "document.h" 8 #include "document.h"
8 #include "settingseditor/settingseditor.h" 9 #include "settingseditor/settingseditor.h"
124 } 125 }
125 */ 126 */
126 127
127 static EditorTabWidget* currentTabWidget(Ui_MainWindow* ui) 128 static EditorTabWidget* currentTabWidget(Ui_MainWindow* ui)
128 { 129 {
129 return qobject_cast<EditorTabWidget*>(ui->tabs->currentWidget()); 130 QMdiSubWindow* activeSubWindow = ui->mdiArea->activeSubWindow();
131 if (activeSubWindow == nullptr) {
132 return nullptr;
133 }
134 else {
135 return qobject_cast<EditorTabWidget*>(activeSubWindow->widget());
136 }
130 }; 137 };
131 138
132 139
133 static void closeDocument(DocumentManager* documents, EditorTabWidget *document) 140 static void closeDocument(DocumentManager* documents, EditorTabWidget *document)
134 { 141 {
139 } 146 }
140 } 147 }
141 148
142 static void handleTabCloseButton(Ui_MainWindow* ui, DocumentManager* documents, int tabIndex) 149 static void handleTabCloseButton(Ui_MainWindow* ui, DocumentManager* documents, int tabIndex)
143 { 150 {
151 /*
144 if (tabIndex >= 0 and tabIndex < ui->tabs->count()) { 152 if (tabIndex >= 0 and tabIndex < ui->tabs->count()) {
145 EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(ui->tabs->widget(tabIndex)); 153 EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(ui->tabs->widget(tabIndex));
146 if (tab != nullptr) { 154 if (tab != nullptr) {
147 closeDocument(documents, tab); 155 closeDocument(documents, tab);
148 } 156 }
149 } 157 }
158 */
150 } 159 }
151 160
152 static std::optional<ModelId> findCurrentModelId(Ui_MainWindow* ui, DocumentManager* documents) 161 static std::optional<ModelId> findCurrentModelId(Ui_MainWindow* ui, DocumentManager* documents)
153 { 162 {
154 const EditorTabWidget* tab = currentTabWidget(ui); 163 const EditorTabWidget* tab = currentTabWidget(ui);
199 208
200 static void updateRenderPreferences( 209 static void updateRenderPreferences(
201 Ui_MainWindow* ui, 210 Ui_MainWindow* ui,
202 const gl::RenderPreferences* renderPreferences) 211 const gl::RenderPreferences* renderPreferences)
203 { 212 {
204 for (int i = 0; i < ui->tabs->count(); i += 1) { 213 for (QMdiSubWindow* subWindow : ui->mdiArea->subWindowList()) {
205 EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(ui->tabs->widget(i)); 214 EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(subWindow->widget());
206 if (tab != nullptr) { 215 if (tab != nullptr) {
207 tab->canvas->setRenderPreferences(*renderPreferences); 216 tab->canvas->setRenderPreferences(*renderPreferences);
208 } 217 }
209 } 218 }
210 for (auto data : ::renderStyleButtons) { 219 for (auto data : ::renderStyleButtons) {
273 changeLanguage(settings.locale(), &translator); 282 changeLanguage(settings.locale(), &translator);
274 libraries.restoreFromSettings(&settings); 283 libraries.restoreFromSettings(&settings);
275 updateRecentlyOpenedDocumentsMenu(); 284 updateRecentlyOpenedDocumentsMenu();
276 colorTable = loadColors(&libraries); 285 colorTable = loadColors(&libraries);
277 updateRenderPreferences(&ui, &renderPreferences); 286 updateRenderPreferences(&ui, &renderPreferences);
287 ui.mdiArea->setViewMode(static_cast<QMdiArea::ViewMode>(settings.viewMode()));
278 ui.retranslateUi(&mainWindow); 288 ui.retranslateUi(&mainWindow);
279 }; 289 };
280 const auto addRecentlyOpenedFile = [&](const QString& path){ 290 const auto addRecentlyOpenedFile = [&](const QString& path){
281 constexpr int maxRecentlyOpenedFiles = 10; 291 constexpr int maxRecentlyOpenedFiles = 10;
282 recentlyOpenedFiles.removeAll(path); 292 recentlyOpenedFiles.removeAll(path);
300 &EditorTabWidget::newStatusText, 310 &EditorTabWidget::newStatusText,
301 [&](const QString& newStatusText) { 311 [&](const QString& newStatusText) {
302 mainWindow.statusBar()->showMessage(newStatusText); 312 mainWindow.statusBar()->showMessage(newStatusText);
303 }); 313 });
304 const QFileInfo fileInfo{*documents.modelPath(modelId)}; 314 const QFileInfo fileInfo{*documents.modelPath(modelId)};
305 ui.tabs->addTab(document, tabName(fileInfo)); 315 QMdiSubWindow* subWindow = ui.mdiArea->addSubWindow(document);
306 ui.tabs->setCurrentWidget(document); 316 subWindow->setWindowTitle(tabName(fileInfo));
317 subWindow->show();
307 document->restoreSplitterState(documentSplitterState); 318 document->restoreSplitterState(documentSplitterState);
308 }; 319 };
309 const auto newModel = [&openModelForEditing](DocumentManager* documents){ 320 QObject::connect(ui.actionNew, &QAction::triggered, [&]{
310 openModelForEditing(documents->newModel()); 321 openModelForEditing(documents.newModel());
311 };
312 QObject::connect(ui.actionNew, &QAction::triggered, [&newModel, &documents]{
313 newModel(&documents);
314 }); 322 });
315 QObject::connect(ui.actionOpen, &QAction::triggered, [&]{ 323 QObject::connect(ui.actionOpen, &QAction::triggered, [&]{
316 const QString path = getOpenModelPath(&mainWindow); 324 const QString path = getOpenModelPath(&mainWindow);
317 if (not path.isEmpty()) 325 if (not path.isEmpty())
318 { 326 {
376 ); 384 );
377 if (not newPath.isEmpty()) { 385 if (not newPath.isEmpty()) {
378 QString error; 386 QString error;
379 QTextStream errorStream{&error}; 387 QTextStream errorStream{&error};
380 documents.setModelPath(*modelId, newPath, libraries, errorStream); 388 documents.setModelPath(*modelId, newPath, libraries, errorStream);
381 ui.tabs->setTabText(ui.tabs->currentIndex(), QFileInfo{newPath}.fileName()); 389 QMdiSubWindow* const subWindow = ui.mdiArea->currentSubWindow();
390 if (subWindow != nullptr) {
391 subWindow->setWindowTitle(tabName(QFileInfo{newPath}));
392 }
382 save(*modelId); 393 save(*modelId);
383 } 394 }
384 } 395 }
385 }; 396 };
386 QObject::connect(ui.actionSaveAs, &QAction::triggered, actionSaveAs); 397 QObject::connect(ui.actionSaveAs, &QAction::triggered, actionSaveAs);
396 save(*modelId); 407 save(*modelId);
397 } 408 }
398 } 409 }
399 } 410 }
400 }); 411 });
401 QObject::connect(ui.tabs, &QTabWidget::tabCloseRequested, [&](int index){ 412 /*
413 QObject::connect(ui.mdiArea, &QTabWidget::tabCloseRequested, [&](int index){
402 handleTabCloseButton(&ui, &documents, index); 414 handleTabCloseButton(&ui, &documents, index);
403 }); 415 });
416 */
404 QObject::connect(ui.actionDrawAxes, &QAction::triggered, [&](bool drawAxes){ 417 QObject::connect(ui.actionDrawAxes, &QAction::triggered, [&](bool drawAxes){
405 renderPreferences.drawAxes = drawAxes; 418 renderPreferences.drawAxes = drawAxes;
406 saveSettings(); 419 saveSettings();
407 updateRenderPreferences(&ui, &renderPreferences); 420 updateRenderPreferences(&ui, &renderPreferences);
408 }); 421 });
416 } 429 }
417 mainWindow.setWindowTitle(title()); 430 mainWindow.setWindowTitle(title());
418 mainWindow.restoreGeometry(settings.mainWindowGeometry()); 431 mainWindow.restoreGeometry(settings.mainWindowGeometry());
419 restoreSettings(); 432 restoreSettings();
420 updateRenderPreferences(&ui, &renderPreferences); 433 updateRenderPreferences(&ui, &renderPreferences);
421 newModel(&documents);
422 mainWindow.show(); 434 mainWindow.show();
423 const int result = app.exec(); 435 const int result = app.exec();
424 saveSettings(); 436 saveSettings();
425 return result; 437 return result;
426 } 438 }

mercurial