refactor: Introduce ApplicationController to centralize logic
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "Document.h"
|
||||
#include "SketchFeature.h"
|
||||
#include "Feature.h"
|
||||
#include "ApplicationController.h"
|
||||
|
||||
#include <QMenuBar>
|
||||
#include <QMenu>
|
||||
@@ -116,107 +117,74 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
m_viewport = new ViewportWidget;
|
||||
setCentralWidget(m_viewport);
|
||||
|
||||
m_document = new Document(this);
|
||||
m_viewport->setDocument(m_document);
|
||||
connect(m_document, &Document::featureAdded, m_viewport, QOverload<>::of(&QWidget::update));
|
||||
connect(m_document, &Document::cleared, m_viewport, QOverload<>::of(&QWidget::update));
|
||||
ApplicationController::instance()->setMainWindow(this);
|
||||
Document* document = ApplicationController::instance()->document();
|
||||
|
||||
setCurrentFile(QString());
|
||||
m_viewport->setDocument(document);
|
||||
connect(document, &Document::featureAdded, m_viewport, QOverload<>::of(&QWidget::update));
|
||||
connect(document, &Document::cleared, m_viewport, QOverload<>::of(&QWidget::update));
|
||||
|
||||
connect(ApplicationController::instance(), &ApplicationController::sketchModeStarted, this, &MainWindow::enterSketchMode);
|
||||
connect(ApplicationController::instance(), &ApplicationController::sketchModeEnded, this, &MainWindow::exitSketchMode);
|
||||
connect(ApplicationController::instance(), &ApplicationController::sketchModeStarted, m_viewport, &ViewportWidget::onSketchModeStarted);
|
||||
connect(ApplicationController::instance(), &ApplicationController::sketchModeEnded, m_viewport, &ViewportWidget::onSketchModeEnded);
|
||||
connect(ApplicationController::instance(), &ApplicationController::currentFileChanged, this, &MainWindow::updateWindowTitle);
|
||||
|
||||
updateWindowTitle(QString());
|
||||
}
|
||||
|
||||
void MainWindow::createSketch()
|
||||
{
|
||||
QStringList items;
|
||||
items << "XY-Plane" << "XZ-Plane" << "YZ-Plane";
|
||||
|
||||
bool ok;
|
||||
QString item = QInputDialog::getItem(this, "Select Sketch Plane",
|
||||
"Plane:", items, 0, false, &ok);
|
||||
if (ok && !item.isEmpty()) {
|
||||
auto feature = new SketchFeature("Sketch");
|
||||
if (item == "XY-Plane") {
|
||||
m_viewport->startSketch(ViewportWidget::SketchPlane::XY);
|
||||
feature->setPlane(SketchFeature::SketchPlane::XY);
|
||||
} else if (item == "XZ-Plane") {
|
||||
m_viewport->startSketch(ViewportWidget::SketchPlane::XZ);
|
||||
feature->setPlane(SketchFeature::SketchPlane::XZ);
|
||||
} else if (item == "YZ-Plane") {
|
||||
m_viewport->startSketch(ViewportWidget::SketchPlane::YZ);
|
||||
feature->setPlane(SketchFeature::SketchPlane::YZ);
|
||||
}
|
||||
m_document->addFeature(feature);
|
||||
|
||||
m_tabWidget->removeTab(m_tabWidget->indexOf(m_toolsTab));
|
||||
m_tabWidget->removeTab(m_tabWidget->indexOf(m_surfaceTab));
|
||||
m_tabWidget->removeTab(m_tabWidget->indexOf(m_solidTab));
|
||||
m_tabWidget->addTab(m_sketchTab, "SKETCH");
|
||||
}
|
||||
ApplicationController::instance()->beginSketchCreation();
|
||||
}
|
||||
|
||||
void MainWindow::saveSketch()
|
||||
{
|
||||
m_viewport->saveSketch();
|
||||
ApplicationController::instance()->endSketch();
|
||||
}
|
||||
|
||||
void MainWindow::newFile()
|
||||
{
|
||||
ApplicationController::instance()->newDocument();
|
||||
}
|
||||
|
||||
void MainWindow::open()
|
||||
{
|
||||
ApplicationController::instance()->openDocument();
|
||||
}
|
||||
|
||||
bool MainWindow::save()
|
||||
{
|
||||
return ApplicationController::instance()->saveDocument();
|
||||
}
|
||||
|
||||
bool MainWindow::saveAs()
|
||||
{
|
||||
return ApplicationController::instance()->saveDocumentAs();
|
||||
}
|
||||
|
||||
void MainWindow::enterSketchMode()
|
||||
{
|
||||
m_tabWidget->removeTab(m_tabWidget->indexOf(m_toolsTab));
|
||||
m_tabWidget->removeTab(m_tabWidget->indexOf(m_surfaceTab));
|
||||
m_tabWidget->removeTab(m_tabWidget->indexOf(m_solidTab));
|
||||
m_tabWidget->addTab(m_sketchTab, "SKETCH");
|
||||
}
|
||||
|
||||
void MainWindow::exitSketchMode()
|
||||
{
|
||||
m_tabWidget->removeTab(m_tabWidget->indexOf(m_sketchTab));
|
||||
m_tabWidget->addTab(m_solidTab, "SOLID");
|
||||
m_tabWidget->addTab(m_surfaceTab, "SURFACE");
|
||||
m_tabWidget->addTab(m_toolsTab, "TOOLS");
|
||||
}
|
||||
|
||||
void MainWindow::newFile()
|
||||
void MainWindow::updateWindowTitle(const QString& filePath)
|
||||
{
|
||||
m_document->clear();
|
||||
setCurrentFile(QString());
|
||||
}
|
||||
setWindowFilePath(filePath);
|
||||
|
||||
void MainWindow::open()
|
||||
{
|
||||
const QString fileName = QFileDialog::getOpenFileName(this);
|
||||
if (!fileName.isEmpty()) {
|
||||
if (!m_document->load(fileName)) {
|
||||
QMessageBox::warning(this, tr("OpenCAD"),
|
||||
tr("Cannot read file %1").arg(QDir::toNativeSeparators(fileName)));
|
||||
return;
|
||||
}
|
||||
setCurrentFile(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
bool MainWindow::save()
|
||||
{
|
||||
if (m_currentFile.isEmpty()) {
|
||||
return saveAs();
|
||||
} else {
|
||||
if (!m_document->save(m_currentFile)) {
|
||||
QMessageBox::warning(this, tr("OpenCAD"),
|
||||
tr("Cannot write file %1").arg(QDir::toNativeSeparators(m_currentFile)));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool MainWindow::saveAs()
|
||||
{
|
||||
QFileDialog dialog(this);
|
||||
dialog.setWindowModality(Qt::WindowModal);
|
||||
dialog.setAcceptMode(QFileDialog::AcceptSave);
|
||||
if (dialog.exec() != QDialog::Accepted)
|
||||
return false;
|
||||
|
||||
const QString fileName = dialog.selectedFiles().first();
|
||||
setCurrentFile(fileName);
|
||||
return save();
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::setCurrentFile(const QString &fileName)
|
||||
{
|
||||
m_currentFile = fileName;
|
||||
m_document->setFileName(fileName);
|
||||
setWindowFilePath(m_currentFile);
|
||||
|
||||
QString shownName = m_currentFile;
|
||||
if (m_currentFile.isEmpty())
|
||||
QString shownName = filePath;
|
||||
if (filePath.isEmpty())
|
||||
shownName = "Untitled";
|
||||
setWindowTitle(tr("%1[*] - %2").arg(QFileInfo(shownName).fileName(), tr("OpenCAD")));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user