refactor: Introduce ApplicationController to centralize logic
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
122
src/ApplicationController.cpp
Normal file
122
src/ApplicationController.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
#include "ApplicationController.h"
|
||||
#include "Document.h"
|
||||
#include "SketchFeature.h"
|
||||
#include "MainWindow.h"
|
||||
|
||||
#include <QInputDialog>
|
||||
#include <QFileDialog>
|
||||
#include <QMessageBox>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
|
||||
ApplicationController* ApplicationController::instance()
|
||||
{
|
||||
static ApplicationController instance;
|
||||
return &instance;
|
||||
}
|
||||
|
||||
ApplicationController::ApplicationController(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
m_document = new Document(this);
|
||||
}
|
||||
|
||||
ApplicationController::~ApplicationController()
|
||||
{
|
||||
}
|
||||
|
||||
void ApplicationController::setMainWindow(MainWindow* mainWindow)
|
||||
{
|
||||
m_mainWindow = mainWindow;
|
||||
}
|
||||
|
||||
Document* ApplicationController::document() const
|
||||
{
|
||||
return m_document;
|
||||
}
|
||||
|
||||
void ApplicationController::newDocument()
|
||||
{
|
||||
m_document->clear();
|
||||
setCurrentFile(QString());
|
||||
}
|
||||
|
||||
bool ApplicationController::openDocument()
|
||||
{
|
||||
const QString fileName = QFileDialog::getOpenFileName(m_mainWindow);
|
||||
if (!fileName.isEmpty()) {
|
||||
if (!m_document->load(fileName)) {
|
||||
QMessageBox::warning(m_mainWindow, tr("OpenCAD"),
|
||||
tr("Cannot read file %1").arg(QDir::toNativeSeparators(fileName)));
|
||||
return false;
|
||||
}
|
||||
setCurrentFile(fileName);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ApplicationController::saveDocument()
|
||||
{
|
||||
if (m_currentFile.isEmpty()) {
|
||||
return saveDocumentAs();
|
||||
} else {
|
||||
if (!m_document->save(m_currentFile)) {
|
||||
QMessageBox::warning(m_mainWindow, tr("OpenCAD"),
|
||||
tr("Cannot write file %1").arg(QDir::toNativeSeparators(m_currentFile)));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool ApplicationController::saveDocumentAs()
|
||||
{
|
||||
QFileDialog dialog(m_mainWindow);
|
||||
dialog.setWindowModality(Qt::WindowModal);
|
||||
dialog.setAcceptMode(QFileDialog::AcceptSave);
|
||||
if (dialog.exec() != QDialog::Accepted)
|
||||
return false;
|
||||
|
||||
const QString fileName = dialog.selectedFiles().first();
|
||||
setCurrentFile(fileName);
|
||||
return saveDocument();
|
||||
}
|
||||
|
||||
void ApplicationController::beginSketchCreation()
|
||||
{
|
||||
QStringList items;
|
||||
items << "XY-Plane" << "XZ-Plane" << "YZ-Plane";
|
||||
|
||||
bool ok;
|
||||
QString item = QInputDialog::getItem(m_mainWindow, "Select Sketch Plane",
|
||||
"Plane:", items, 0, false, &ok);
|
||||
if (ok && !item.isEmpty()) {
|
||||
auto feature = new SketchFeature("Sketch");
|
||||
ViewportWidget::SketchPlane plane;
|
||||
if (item == "XY-Plane") {
|
||||
plane = ViewportWidget::SketchPlane::XY;
|
||||
feature->setPlane(SketchFeature::SketchPlane::XY);
|
||||
} else if (item == "XZ-Plane") {
|
||||
plane = ViewportWidget::SketchPlane::XZ;
|
||||
feature->setPlane(SketchFeature::SketchPlane::XZ);
|
||||
} else { // "YZ-Plane"
|
||||
plane = ViewportWidget::SketchPlane::YZ;
|
||||
feature->setPlane(SketchFeature::SketchPlane::YZ);
|
||||
}
|
||||
m_document->addFeature(feature);
|
||||
emit sketchModeStarted(plane);
|
||||
}
|
||||
}
|
||||
|
||||
void ApplicationController::endSketch()
|
||||
{
|
||||
emit sketchModeEnded();
|
||||
}
|
||||
|
||||
void ApplicationController::setCurrentFile(const QString& fileName)
|
||||
{
|
||||
m_currentFile = fileName;
|
||||
m_document->setFileName(fileName);
|
||||
emit currentFileChanged(m_currentFile);
|
||||
}
|
||||
Reference in New Issue
Block a user