src/mainwindow.cpp

changeset 160
536efae7137e
parent 148
e1ced2523cad
child 169
6da096930534
equal deleted inserted replaced
159:1a04364d20b5 160:536efae7137e
74 }); 74 });
75 connect(this->ui->actionSave, &QAction::triggered, 75 connect(this->ui->actionSave, &QAction::triggered,
76 this, &MainWindow::actionSave); 76 this, &MainWindow::actionSave);
77 connect(this->ui->actionSaveAs, &QAction::triggered, 77 connect(this->ui->actionSaveAs, &QAction::triggered,
78 this, &MainWindow::actionSaveAs); 78 this, &MainWindow::actionSaveAs);
79 connect(this->ui->actionClose, &QAction::triggered, this, &MainWindow::actionClose);
80 connect(this->ui->tabs, &QTabWidget::tabCloseRequested, this, &MainWindow::handleTabCloseButton);
79 for (auto data : ::renderStyleButtons) 81 for (auto data : ::renderStyleButtons)
80 { 82 {
81 QAction* action = data.memberInstance(this->ui.get()); 83 QAction* action = data.memberInstance(this->ui.get());
82 connect(action, &QAction::triggered, [this, data]() 84 connect(action, &QAction::triggered, [this, data]()
83 { 85 {
211 213
212 Document* MainWindow::currentDocument() 214 Document* MainWindow::currentDocument()
213 { 215 {
214 return qobject_cast<Document*>(this->ui->tabs->currentWidget()); 216 return qobject_cast<Document*>(this->ui->tabs->currentWidget());
215 } 217 }
216
217 /*
218 void MainWindow::closeDocument(Document *document)
219 {
220 const int tabIndex = this->ui->tabs->indexOf(document);
221 if (tabIndex != -1)
222 {
223 this->ui->tabs->removeTab(tabIndex);
224 const QString closedDocumentName = pathToName(document->modelPath());
225 delete document;
226 }
227 QSet<QString> openedDocumentNames;
228 for (int i = 0; i < this->ui->tabs->count(); i += 1)
229 {
230 Document* document = qobject_cast<Document*>(this->ui->tabs->widget(i));
231 if (document != nullptr)
232 {
233 openedDocumentNames.insert(pathToName(document->modelPath()));
234 }
235 }
236 }
237 */
238 218
239 void MainWindow::handleDocumentSplitterChange() 219 void MainWindow::handleDocumentSplitterChange()
240 { 220 {
241 Document* currentDocument = this->currentDocument(); 221 Document* currentDocument = this->currentDocument();
242 if (currentDocument != nullptr) 222 if (currentDocument != nullptr)
335 QTextStream errorStream{&error}; 315 QTextStream errorStream{&error};
336 this->documents.setModelPath(modelId, newPath, this->libraries, errorStream); 316 this->documents.setModelPath(modelId, newPath, this->libraries, errorStream);
337 this->ui->tabs->setTabText(this->ui->tabs->currentIndex(), QFileInfo{newPath}.fileName()); 317 this->ui->tabs->setTabText(this->ui->tabs->currentIndex(), QFileInfo{newPath}.fileName());
338 this->actionSave(); 318 this->actionSave();
339 } 319 }
320 }
321 }
322
323 /**
324 * @brief Handles the "Close" (Ctrl+W) action
325 */
326 void MainWindow::actionClose()
327 {
328 if (this->currentDocument() != nullptr)
329 {
330 this->closeDocument(this->currentDocument());
331 }
332 }
333
334 /**
335 * @brief Removes the document at the specified tab index
336 * @param index
337 */
338 void MainWindow::handleTabCloseButton(int tabIndex)
339 {
340 if (tabIndex >= 0 and tabIndex < this->ui->tabs->count())
341 {
342 Document* document = qobject_cast<Document*>(this->ui->tabs->widget(tabIndex));
343 if (document != nullptr)
344 {
345 this->closeDocument(document);
346 }
347 }
348 }
349
350 /**
351 * @brief Closes the specified document
352 * @param document
353 */
354 void MainWindow::closeDocument(Document *document)
355 {
356 std::optional<ModelId> modelId = this->documents.findIdForModel(&document->getModel());
357 if (modelId.has_value())
358 {
359 this->documents.closeDocument(modelId.value());
360 delete document;
340 } 361 }
341 } 362 }
342 363
343 void MainWindow::changeEvent(QEvent* event) 364 void MainWindow::changeEvent(QEvent* event)
344 { 365 {

mercurial