diff options
| author | Mikko Syrjä <mikko@3d-system.fi> | 2019-02-20 01:29:10 +0200 |
|---|---|---|
| committer | Mikko Syrjä <mikko@3d-system.fi> | 2019-02-20 01:29:10 +0200 |
| commit | 20908c295805d66ede49372ba6ff0c57b8e769d8 (patch) | |
| tree | a6850429eaaf94a87d6667924ffd3d739e9f91f1 | |
| parent | 778312fa2063400b15d85de4bb7d9d350996c579 (diff) | |
| download | symedit-20908c295805d66ede49372ba6ff0c57b8e769d8.tar.gz symedit-20908c295805d66ede49372ba6ff0c57b8e769d8.zip | |
Initial editing features.
| -rw-r--r-- | Editor.qml | 26 | ||||
| -rw-r--r-- | main.qml | 12 | ||||
| -rw-r--r-- | symbol.cpp | 46 | ||||
| -rw-r--r-- | symbol.h | 2 | ||||
| -rw-r--r-- | symedit.cpp | 17 | ||||
| -rw-r--r-- | symedit.h | 3 |
6 files changed, 91 insertions, 15 deletions
@@ -24,13 +24,13 @@ Rectangle Radius = 82 // R } - property int margin: 20 property int units: 100 property int max: units / 2 property int grid: 10 property int offset: 10 property int total: units + offset * 2 + property int margin: 20 property bool horizontal: (height < width) property real scalexy: (horizontal ? (height - margin * 2) / total : (width - margin * 2) / total) @@ -84,12 +84,19 @@ Rectangle { if ( tool === Editor.Tool.Select ) { - var index = manager.selectItem(Qt.point(mousex, mousey)) + manager.selectItem(Qt.point(mousex, mousey)) + canvas.requestPaint() } else { endx = mousex endy = mousey + if ( tool === Editor.Tool.LineSingle ) + { + manager.addItem(Editor.Operation.Move, Qt.point(startx, starty), false) + manager.addItem(Editor.Operation.Line, Qt.point(mousex, mousey), false) + symbol = manager.getSymbol() + } } down = false } @@ -155,9 +162,19 @@ Rectangle context.lineWidth = linewidth var previous = Qt.point(0, 0) + var active = manager.getActiveIndex(); var index, count = manager.getItemCount() for ( index = 0; index < count; index++ ) { + if ( index === active ) + { + context.strokeStyle = "red" + } + else + { + context.strokeStyle = "black" + } + var operation = manager.getItemOperation(index) var position = manager.getItemPoint(index) var fill = manager.getItemFill(index) @@ -254,4 +271,9 @@ Rectangle } } } + + function update() + { + canvas.requestPaint() + } } @@ -13,6 +13,8 @@ ApplicationWindow property int snapgrid: 1 property int tool: 0 + property string symbol + id: window visible: false title: qsTr("Symbol editor") //%% @@ -159,7 +161,7 @@ ApplicationWindow { id: widthlist model: [ 1, 2, 3, 4, 5 ] - onCurrentIndexChanged: { linewidth = currentIndex + 1 } + onCurrentIndexChanged: { linewidth = currentIndex + 1; editor.update() } function setWidth() { currentIndex = linewidth - 1 } } BarSeparator { } @@ -185,7 +187,7 @@ ApplicationWindow Label { Layout.minimumWidth: 40; text: " X: " + mousex } Label { Layout.minimumWidth: 40; text: " Y: " + mousey } Item { Layout.fillWidth: true } - Label { Layout.minimumWidth: 40; text: manager.getSymbol() } + Label { Layout.minimumWidth: 40; text: symbol } } } @@ -217,6 +219,8 @@ ApplicationWindow widthlist.setWidth() alignlist.setAlign() + symbol = manager.getSymbol() + visible = true manager.setInitialized() } @@ -258,7 +262,9 @@ ApplicationWindow function remove() { - + manager.removeItem() + symbol = manager.getSymbol() + editor.update() } function help() @@ -1,5 +1,7 @@ #include <QStringList> +#include <math.h> + #include "symbol.h" // @@ -46,6 +48,7 @@ void SymEditSymbol::Load(const QString& buffer) Item item(operation, QPoint(x, y)); Items.push_back(item); } + ActiveIndex = static_cast<int>(Items.size()) - 1; } //! Save symbol to string. @@ -87,6 +90,7 @@ SymEditSymbol::Item& SymEditSymbol::AddItem(int operation, QPoint point, bool fi { Item item(operation, point); item.Fill = fill; + ActiveIndex = static_cast<int>(Items.size()); Items.push_back(item); return Items.back(); } @@ -104,6 +108,7 @@ SymEditSymbol::Item& SymEditSymbol::AddItem(int operation, QPoint point, QString Item item(operation, point); item.Text = text; item.Align = align; + ActiveIndex = static_cast<int>(Items.size()); Items.push_back(item); return Items.back(); } @@ -115,14 +120,51 @@ SymEditSymbol::Item& SymEditSymbol::AddItem(int operation, QPoint point, QString void SymEditSymbol::RemoveItem(int index) { if ( static_cast<size_t>(index) < Items.size() ) + { Items.erase(Items.begin() + index); + ActiveIndex = index - 1; + } } // int SymEditSymbol::SelectItem(QPoint point) const { - ActiveIndex = -1; //## - return ActiveIndex; + ActiveIndex = -1; + int index = 0; + QPoint previous(0, 0); + double minimum = 100.0; + for ( const auto& item : Items ) + { + switch ( item.Operation ) + { + case 'D': // line + { + //## + break; + } + case 'R': // circle + { + double dx = point.x() - previous.x(); + double dy = point.y() - previous.y(); + double distance = sqrt(dx * dx + dy * dy); + distance = abs(distance - item.Point.x()); + if ( ActiveIndex < 0 || distance < minimum ) + { + ActiveIndex = index; + minimum = distance; + } + break; + } + } + ++index; + } + return static_cast<int>(ActiveIndex); +} + +// +int SymEditSymbol::GetActiveIndex() const +{ + return static_cast<int>(ActiveIndex); } //! Get item count. @@ -30,7 +30,9 @@ public: Item& AddItem(int operation, QPoint point, bool fill); Item& AddItem(int operation, QPoint point, QString text, int align); void RemoveItem(int index); + int SelectItem(QPoint point) const; + int GetActiveIndex() const; int GetItemCount() const; const Item& GetItem(int index) const; diff --git a/symedit.cpp b/symedit.cpp index 763f6bf..1f60f0a 100644 --- a/symedit.cpp +++ b/symedit.cpp @@ -158,13 +158,10 @@ void SymEditManager::addText(int operation, QPoint point, QString text, int alig Symbol.AddItem(operation, point, text, align); } -//! Remove item. -/*! - \param index Item index. -*/ -void SymEditManager::removeItem(int index) +//! Remove active item. +void SymEditManager::removeItem() { - Symbol.RemoveItem(index); + Symbol.RemoveItem(Symbol.GetActiveIndex()); } //! Get item count. @@ -219,7 +216,13 @@ bool SymEditManager::getItemFill(int index) const // int SymEditManager::selectItem(QPoint point) const { - return -1; //## + return Symbol.SelectItem(point); +} + +// +int SymEditManager::getActiveIndex() const +{ + return Symbol.GetActiveIndex(); } //! Read symbol from clipboard. @@ -63,7 +63,7 @@ public: Q_INVOKABLE void addItem(int operation, QPoint point, bool fill); Q_INVOKABLE void addText(int operation, QPoint point, QString text, int align); - Q_INVOKABLE void removeItem(int index); + Q_INVOKABLE void removeItem(); Q_INVOKABLE int getItemCount() const; Q_INVOKABLE int getItemOperation(int index) const; @@ -72,6 +72,7 @@ public: Q_INVOKABLE bool getItemFill(int index) const; Q_INVOKABLE int selectItem(QPoint point) const; + Q_INVOKABLE int getActiveIndex() const; Q_INVOKABLE void readClipboard(); Q_INVOKABLE void writeClipboard() const; |
