# HG changeset patch # User Teemu Piippo # Date 1522314654 -10800 # Node ID b725b7fb63a55df941e228be8194b1d8fd8e4b73 # Parent 48374309f3d19f943ad10f7e80c7d08dae49445b started work on the pattern editor diff -r 48374309f3d1 -r b725b7fb63a5 CMakeLists.txt --- 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 diff -r 48374309f3d1 -r b725b7fb63a5 data/pattern-background.png Binary file data/pattern-background.png has changed diff -r 48374309f3d1 -r b725b7fb63a5 data/transparent-background.png Binary file data/transparent-background.png has changed diff -r 48374309f3d1 -r b725b7fb63a5 ldforge.qrc --- 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 @@ ./icons/visibility-toggle.png ./icons/wireframe.png ./icons/ytruder.png + data/pattern-background.png data/primitive-categories.cfg + data/transparent-background.png LICENSE LICENSE.icons diff -r 48374309f3d1 -r b725b7fb63a5 src/main.cpp --- 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"); 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(); diff -r 48374309f3d1 -r b725b7fb63a5 src/types/pattern.cpp --- /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} +{ + +} diff -r 48374309f3d1 -r b725b7fb63a5 src/types/pattern.h --- /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 +#include "../main.h" + +struct ColoredPolygon +{ + QPolygonF geometry; + LDColor color; + + ColoredPolygon(const QPolygonF& polygon, LDColor color); +}; + +struct Pattern +{ + Pattern(const QSizeF& size); + + QVector polygons; + QSizeF canvasSize; +}; diff -r 48374309f3d1 -r b725b7fb63a5 src/widgets/patterneditor.cpp --- /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; +} diff -r 48374309f3d1 -r b725b7fb63a5 src/widgets/patterneditor.h --- /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 +#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; +}; diff -r 48374309f3d1 -r b725b7fb63a5 src/widgets/patterneditor.ui --- /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 @@ + + + PatternEditor + + + + 0 + 0 + 800 + 600 + + + + Pattern editor + + + + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + + + + + + diff -r 48374309f3d1 -r b725b7fb63a5 src/widgets/patternviewer.cpp --- /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 +#include +#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(); +} diff -r 48374309f3d1 -r b725b7fb63a5 src/widgets/patternviewer.h --- /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 +#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 = {}; +};