feat: Implement feature graph and JSON document save/load

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2026-02-09 17:16:32 -07:00
parent bc851585f7
commit d2d49726d6
9 changed files with 340 additions and 0 deletions

View File

@@ -1,6 +1,12 @@
#include "MainWindow.h"
#include "ViewportWidget.h"
#include "Document.h"
#include "SketchFeature.h"
#include <QMenuBar>
#include <QMenu>
#include <QFileDialog>
#include <QMessageBox>
#include <QToolBar>
#include <QTabWidget>
#include <QWidget>
@@ -16,6 +22,20 @@ MainWindow::MainWindow(QWidget *parent)
setWindowTitle("OpenCAD");
resize(1280, 720);
QMenu *fileMenu = menuBar()->addMenu("&File");
QAction *newAction = fileMenu->addAction("&New");
connect(newAction, &QAction::triggered, this, &MainWindow::newFile);
QAction *openAction = fileMenu->addAction("&Open...");
connect(openAction, &QAction::triggered, this, &MainWindow::open);
QAction *saveAction = fileMenu->addAction("&Save");
connect(saveAction, &QAction::triggered, this, &MainWindow::save);
QAction *saveAsAction = fileMenu->addAction("Save &As...");
connect(saveAsAction, &QAction::triggered, this, &MainWindow::saveAs);
QToolBar* mainToolBar = addToolBar("Main Toolbar");
mainToolBar->setMovable(false);
mainToolBar->setFloatable(false);
@@ -58,6 +78,9 @@ MainWindow::MainWindow(QWidget *parent)
m_viewport = new ViewportWidget;
setCentralWidget(m_viewport);
m_document = new Document(this);
setCurrentFile(QString());
}
void MainWindow::createSketch()
@@ -69,12 +92,75 @@ void MainWindow::createSketch()
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);
}
}
void MainWindow::newFile()
{
m_document->clear();
setCurrentFile(QString());
}
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;
setWindowFilePath(m_currentFile);
QString shownName = m_currentFile;
if (m_currentFile.isEmpty())
shownName = "untitled.json";
setWindowTitle(tr("%1[*] - %2").arg(QFileInfo(shownName).fileName(), tr("OpenCAD")));
}