Files
unnamed-cad-software/src/ApplicationController.cpp
2026-02-13 17:33:17 -07:00

126 lines
3.3 KiB
C++

#include "ApplicationController.h"
#include "Document.h"
#include "SketchFeature.h"
#include "MainWindow.h"
#include <QInputDialog>
#include <QFileDialog>
#include <QMessageBox>
#include <QDir>
#include <QFileInfo>
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;
}
SketchFeature* ApplicationController::activeSketch() const
{
return m_activeSketch;
}
void ApplicationController::newDocument()
{
m_activeSketch = nullptr;
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);
m_activeSketch = nullptr;
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");
m_activeSketch = feature;
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()
{
m_activeSketch = nullptr;
emit sketchModeEnded();
}
void ApplicationController::setCurrentFile(const QString& fileName)
{
m_currentFile = fileName;
m_document->setFileName(fileName);
emit currentFileChanged(m_currentFile);
}