172 lines
4.2 KiB
C++
172 lines
4.2 KiB
C++
#include "ApplicationController.h"
|
|
#include "Document.h"
|
|
#include "SketchFeature.h"
|
|
#include "SketchLine.h"
|
|
#include "SketchRectangle.h"
|
|
#include "SketchCircle.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;
|
|
}
|
|
|
|
ApplicationController::ToolType ApplicationController::activeTool() const
|
|
{
|
|
return m_activeTool;
|
|
}
|
|
|
|
void ApplicationController::setActiveTool(ToolType tool)
|
|
{
|
|
if (m_activeTool == tool) {
|
|
return;
|
|
}
|
|
|
|
m_activeTool = tool;
|
|
emit activeToolChanged(m_activeTool);
|
|
}
|
|
|
|
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()
|
|
{
|
|
if (m_activeSketch) {
|
|
return;
|
|
}
|
|
emit planeSelectionModeStarted();
|
|
}
|
|
|
|
void ApplicationController::onPlaneSelected(ViewportWidget::SketchPlane plane)
|
|
{
|
|
auto feature = new SketchFeature("Sketch");
|
|
m_activeSketch = feature;
|
|
|
|
switch (plane) {
|
|
case ViewportWidget::SketchPlane::XY:
|
|
feature->setPlane(SketchFeature::SketchPlane::XY);
|
|
break;
|
|
case ViewportWidget::SketchPlane::XZ:
|
|
feature->setPlane(SketchFeature::SketchPlane::XZ);
|
|
break;
|
|
case ViewportWidget::SketchPlane::YZ:
|
|
feature->setPlane(SketchFeature::SketchPlane::YZ);
|
|
break;
|
|
case ViewportWidget::SketchPlane::NONE:
|
|
delete feature;
|
|
m_activeSketch = nullptr;
|
|
return;
|
|
}
|
|
|
|
m_document->addFeature(feature);
|
|
emit sketchModeStarted(plane);
|
|
}
|
|
|
|
void ApplicationController::addLine(const gp_Pnt& start, const gp_Pnt& end)
|
|
{
|
|
if (m_activeSketch) {
|
|
m_activeSketch->addObject(new SketchLine(start, end));
|
|
}
|
|
}
|
|
|
|
void ApplicationController::addRectangle(const gp_Pnt& corner1, const gp_Pnt& corner2)
|
|
{
|
|
if (m_activeSketch) {
|
|
m_activeSketch->addObject(new SketchRectangle(corner1, corner2));
|
|
}
|
|
}
|
|
|
|
void ApplicationController::addCircle(const gp_Pnt& center, double radius)
|
|
{
|
|
if (m_activeSketch) {
|
|
m_activeSketch->addObject(new SketchCircle(center, radius));
|
|
}
|
|
}
|
|
|
|
void ApplicationController::endSketch()
|
|
{
|
|
m_activeSketch = nullptr;
|
|
setActiveTool(ToolType::None);
|
|
emit sketchModeEnded();
|
|
}
|
|
|
|
void ApplicationController::setCurrentFile(const QString& fileName)
|
|
{
|
|
m_currentFile = fileName;
|
|
m_document->setFileName(fileName);
|
|
emit currentFileChanged(m_currentFile);
|
|
}
|