Thu, 29 Mar 2018 12:10:54 +0300
started work on the pattern editor
--- a/CMakeLists.txt Thu Mar 29 10:42:45 2018 +0300 +++ b/CMakeLists.txt Thu Mar 29 12:10:54 2018 +0300 @@ -93,7 +93,10 @@ src/toolsets/viewtoolset.cpp src/types/boundingbox.cpp src/types/matrix.cpp + src/types/pattern.cpp src/types/vertex.cpp + src/widgets/patterneditor.cpp + src/widgets/patternviewer.cpp src/widgets/headeredit.cpp src/widgets/vertexobjecteditor.cpp ) @@ -171,7 +174,10 @@ src/types/boundingbox.h src/types/library.h src/types/matrix.h + src/types/pattern.h src/types/vertex.h + src/widgets/patterneditor.h + src/widgets/patternviewer.h src/widgets/headeredit.h src/widgets/vertexobjecteditor.h ) @@ -200,6 +206,7 @@ src/partdownloader.ui src/widgets/vertexobjecteditor.ui src/widgets/headeredit.ui + src/widgets/patterneditor.ui ) set (LDFORGE_OTHER_FILES
--- a/ldforge.qrc Thu Mar 29 10:42:45 2018 +0300 +++ b/ldforge.qrc Thu Mar 29 12:10:54 2018 +0300 @@ -107,7 +107,9 @@ <file>./icons/visibility-toggle.png</file> <file>./icons/wireframe.png</file> <file>./icons/ytruder.png</file> + <file>data/pattern-background.png</file> <file>data/primitive-categories.cfg</file> + <file>data/transparent-background.png</file> <file>LICENSE</file> <file>LICENSE.icons</file> </qresource>
--- a/src/main.cpp Thu Mar 29 10:42:45 2018 +0300 +++ b/src/main.cpp Thu Mar 29 12:10:54 2018 +0300 @@ -21,6 +21,7 @@ #include "documentmanager.h" #include "mainwindow.h" #include "generics/reverse.h" +#include "widgets/patterneditor.h" int main (int argc, char* argv[]) { @@ -35,6 +36,13 @@ qRegisterMetaTypeStreamOperators<Vertex>("Vertex"); initializeCrashHandler(); LDColor::initColors(); + + Pattern pattern {{200, 100}}; + PatternEditor* editor = new PatternEditor {pattern, nullptr}; + editor->setMinimumSize({320, 200}); + editor->show(); + return app.exec(); + MainWindow* mainWindow = new MainWindow; mainWindow->show();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/types/pattern.cpp Thu Mar 29 12:10:54 2018 +0300 @@ -0,0 +1,11 @@ +#include "pattern.h" + +ColoredPolygon::ColoredPolygon(const QPolygonF& polygon, LDColor color) : + geometry {polygon}, + color {color} {} + +Pattern::Pattern(const QSizeF& size) : + canvasSize {size} +{ + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/types/pattern.h Thu Mar 29 12:10:54 2018 +0300 @@ -0,0 +1,19 @@ +#pragma once +#include <QPolygonF> +#include "../main.h" + +struct ColoredPolygon +{ + QPolygonF geometry; + LDColor color; + + ColoredPolygon(const QPolygonF& polygon, LDColor color); +}; + +struct Pattern +{ + Pattern(const QSizeF& size); + + QVector<ColoredPolygon> polygons; + QSizeF canvasSize; +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/patterneditor.cpp Thu Mar 29 12:10:54 2018 +0300 @@ -0,0 +1,18 @@ +#include "patterneditor.h" +#include "patternviewer.h" +#include "ui_patterneditor.h" + +PatternEditor::PatternEditor(Pattern& pattern, QWidget* parent) : + QMainWindow {parent}, + ui {*new Ui_PatternEditor}, + viewer {new PatternViewer {this}}, + pattern {pattern} +{ + ui.setupUi(this); + ui.patternFrame->layout()->addWidget(this->viewer); +} + +PatternEditor::~PatternEditor() +{ + delete &this->ui; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/patterneditor.h Thu Mar 29 12:10:54 2018 +0300 @@ -0,0 +1,25 @@ +#pragma once +#include <QMainWindow> +#include "../main.h" +#include "../types/pattern.h" + +class PatternEditor : public QMainWindow +{ + Q_OBJECT + +public: + PatternEditor(Pattern& pattern, QWidget *parent = nullptr); + ~PatternEditor(); + +signals: + +public slots: + +private: + friend class PatternViewer; + + class Ui_PatternEditor& ui; + class PatternViewer* viewer; + Pattern& pattern; + LDColor currentColor = MainColor; +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/patterneditor.ui Thu Mar 29 12:10:54 2018 +0300 @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>PatternEditor</class> + <widget class="QMainWindow" name="PatternEditor"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>800</width> + <height>600</height> + </rect> + </property> + <property name="windowTitle"> + <string>Pattern editor</string> + </property> + <widget class="QWidget" name="centralwidget"> + <layout class="QVBoxLayout" name="verticalLayout_2"> + <item> + <widget class="QFrame" name="patternFrame"> + <property name="frameShape"> + <enum>QFrame::StyledPanel</enum> + </property> + <property name="frameShadow"> + <enum>QFrame::Raised</enum> + </property> + <layout class="QVBoxLayout" name="verticalLayout"/> + </widget> + </item> + </layout> + </widget> + </widget> + <resources/> + <connections/> +</ui>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/patternviewer.cpp Thu Mar 29 12:10:54 2018 +0300 @@ -0,0 +1,49 @@ +#include <QPainter> +#include <QPaintEvent> +#include "patterneditor.h" +#include "patternviewer.h" + +/* + * Construct a pattern viewer for an existing pattern editor. + */ +PatternViewer::PatternViewer(PatternEditor* parent) : + QWidget {parent}, + editor {parent} +{ + this->transformation.scale(4, 4); +} + +/* + * Unfortunately, Qt doesn't provide an easy way to translate a floating point rectangle + * into a floating point polygon. So here's a manual implementation. + */ +static QPolygonF transformRect(const QRectF& rect, const QTransform& transform) +{ + QPolygonF transformed; + for (const QPointF& point : { + rect.topLeft(), + rect.topRight(), + rect.bottomRight(), + rect.bottomLeft() + }) { + transformed.append(transform.map(point)); + } + return transformed; +} + +/* + * Renders the pattern. + */ +void PatternViewer::paintEvent(QPaintEvent* event) +{ + const Pattern& pattern = this->editor->pattern; + static const QPixmap viewerBackground {":/data/pattern-background.png"}; + static const QPixmap canvasBackground {":/data/transparent-background.png"}; + QPainter painter {this}; + painter.drawTiledPixmap(this->rect(), viewerBackground); + painter.setBrush(canvasBackground); + QRectF canvasRect = QRectF {{0.0f, 0.0f}, pattern.canvasSize}; + painter.drawPolygon(::transformRect(canvasRect, this->transformation)); + painter.setTransform(this->transformation); + event->accept(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/patternviewer.h Thu Mar 29 12:10:54 2018 +0300 @@ -0,0 +1,20 @@ +#pragma once +#include <QWidget> +#include "patterneditor.h" + +class PatternViewer : public QWidget +{ + Q_OBJECT + +public: + PatternViewer(PatternEditor* parent); + +signals: +public slots: +protected: + void paintEvent(QPaintEvent* event) override; + +private: + class PatternEditor* const editor; + QTransform transformation = {}; +};