diff --git a/src/Document.cpp b/src/Document.cpp index cf1243d..f66df09 100644 --- a/src/Document.cpp +++ b/src/Document.cpp @@ -3,6 +3,7 @@ #include "SketchFeature.h" #include +#include #include #include #include @@ -26,9 +27,28 @@ void Document::clear() { qDeleteAll(m_features); m_features.clear(); + m_fileName.clear(); emit cleared(); } +const QList& Document::features() const +{ + return m_features; +} + +void Document::setFileName(const QString& fileName) +{ + m_fileName = fileName; +} + +QString Document::fileName() const +{ + if (m_fileName.isEmpty()) { + return "Untitled"; + } + return QFileInfo(m_fileName).fileName(); +} + bool Document::save(const QString& path) const { QJsonArray featuresArray; diff --git a/src/Document.h b/src/Document.h index 5a7fc72..9d9006b 100644 --- a/src/Document.h +++ b/src/Document.h @@ -19,12 +19,17 @@ public: bool save(const QString& path) const; bool load(const QString& path); + const QList& features() const; + void setFileName(const QString& fileName); + QString fileName() const; + signals: void featureAdded(Feature* feature); void cleared(); private: QList m_features; + QString m_fileName; }; #endif // DOCUMENT_H diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index cd79bb8..39b5e1c 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -8,9 +8,6 @@ #include #include #include -#include -#include -#include #include #include #include @@ -84,17 +81,9 @@ MainWindow::MainWindow(QWidget *parent) setCentralWidget(m_viewport); m_document = new Document(this); - connect(m_document, &Document::featureAdded, this, &MainWindow::onFeatureAdded); - connect(m_document, &Document::cleared, this, &MainWindow::onDocumentCleared); - - QDockWidget *dock = new QDockWidget("Browser", this); - addDockWidget(Qt::LeftDockWidgetArea, dock); - - m_featureTree = new QTreeWidget(dock); - m_featureTree->setHeaderHidden(true); - dock->setWidget(m_featureTree); - - m_rootItem = new QTreeWidgetItem(m_featureTree); + 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)); setCurrentFile(QString()); } @@ -173,26 +162,11 @@ bool MainWindow::saveAs() void MainWindow::setCurrentFile(const QString &fileName) { m_currentFile = fileName; + m_document->setFileName(fileName); setWindowFilePath(m_currentFile); QString shownName = m_currentFile; if (m_currentFile.isEmpty()) shownName = "Untitled"; setWindowTitle(tr("%1[*] - %2").arg(QFileInfo(shownName).fileName(), tr("OpenCAD"))); - - if (m_rootItem) { - m_rootItem->setText(0, QFileInfo(shownName).fileName()); - } -} - -void MainWindow::onFeatureAdded(Feature* feature) -{ - QTreeWidgetItem *item = new QTreeWidgetItem(m_rootItem); - item->setText(0, feature->name()); - m_rootItem->setExpanded(true); -} - -void MainWindow::onDocumentCleared() -{ - qDeleteAll(m_rootItem->takeChildren()); } diff --git a/src/MainWindow.h b/src/MainWindow.h index ef0997e..9dc5655 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -6,8 +6,6 @@ class ViewportWidget; class Document; class Feature; -class QTreeWidget; -class QTreeWidgetItem; class MainWindow : public QMainWindow { @@ -23,18 +21,12 @@ private slots: bool saveAs(); void createSketch(); - void onFeatureAdded(Feature* feature); - void onDocumentCleared(); - private: void setCurrentFile(const QString &fileName); ViewportWidget *m_viewport; Document *m_document; QString m_currentFile; - - QTreeWidget* m_featureTree; - QTreeWidgetItem* m_rootItem; }; #endif // MAINWINDOW_H diff --git a/src/ViewportWidget.cpp b/src/ViewportWidget.cpp index e4a91d0..b00ff0a 100644 --- a/src/ViewportWidget.cpp +++ b/src/ViewportWidget.cpp @@ -1,6 +1,9 @@ #include "ViewportWidget.h" #include "ViewCube.h" #include "SketchGrid.h" +#include "Document.h" +#include "Feature.h" +#include #include #include #include @@ -21,6 +24,11 @@ ViewportWidget::~ViewportWidget() delete m_sketchGrid; } +void ViewportWidget::setDocument(Document* document) +{ + m_document = document; +} + void ViewportWidget::initializeGL() { initializeOpenGLFunctions(); @@ -61,11 +69,12 @@ void ViewportWidget::paintGL() glViewport(0, 0, width(), height()); + QPainter painter(this); if (m_currentPlane != SketchPlane::NONE) { - QPainter painter(this); drawAxisLabels(painter, model, projection); - painter.end(); } + drawFeatureBrowser(painter); + painter.end(); } void ViewportWidget::resizeGL(int w, int h) @@ -181,3 +190,50 @@ void ViewportWidget::drawAxisLabels(QPainter& painter, const QMatrix4x4& modelVi drawLabelsForAxis(2); // Z } } + +void ViewportWidget::drawFeatureBrowser(QPainter& painter) +{ + if (!m_document) { + return; + } + + painter.setRenderHint(QPainter::Antialiasing); + painter.setFont(QFont("Arial", 10)); + + const int padding = 5; + const int margin = 10; + int x = margin; + int y = height() - margin; + int lineHeight = painter.fontMetrics().height(); + const QList& features = m_document->features(); + QString docName = m_document->fileName(); + + int maxWidth = painter.fontMetrics().horizontalAdvance(docName); + + for (const Feature* feature : features) { + maxWidth = std::max(maxWidth, painter.fontMetrics().horizontalAdvance(" " + feature->name())); + } + + int boxWidth = maxWidth + 2 * padding; + int boxHeight = (features.size() + 1) * lineHeight + 2 * padding; + + y -= boxHeight; // Adjust y to be the top of the box + + // Draw transparent background + painter.setPen(Qt::NoPen); + painter.setBrush(QColor(50, 50, 50, 150)); + painter.drawRoundedRect(x, y, boxWidth, boxHeight, 3, 3); + + // Draw text + painter.setPen(Qt::white); + int currentY = y + padding + lineHeight; + int textX = x + padding; + + painter.drawText(textX, currentY, docName); + currentY += lineHeight; + + for (const Feature* feature : features) { + painter.drawText(textX + 10, currentY, feature->name()); + currentY += lineHeight; + } +} diff --git a/src/ViewportWidget.h b/src/ViewportWidget.h index 9869e82..932596a 100644 --- a/src/ViewportWidget.h +++ b/src/ViewportWidget.h @@ -10,6 +10,8 @@ class ViewCube; class SketchGrid; +class Document; +class Feature; class ViewportWidget : public QOpenGLWidget, protected QOpenGLFunctions { @@ -27,6 +29,7 @@ public: ~ViewportWidget(); void startSketch(SketchPlane plane); + void setDocument(Document* document); protected: void initializeGL() override; @@ -40,10 +43,12 @@ protected: private: QVector3D project(const QVector3D& worldCoord, const QMatrix4x4& modelView, const QMatrix4x4& projection, const QRect& viewport); void drawAxisLabels(QPainter& painter, const QMatrix4x4& modelView, const QMatrix4x4& projection); + void drawFeatureBrowser(QPainter& painter); QMatrix4x4 projection; ViewCube* m_viewCube; SketchGrid* m_sketchGrid = nullptr; + Document* m_document = nullptr; SketchPlane m_currentPlane = SketchPlane::NONE; float xRot = 0;