diff options
| author | Mikko Syrjä <mikko@3d-system.fi> | 2019-02-28 01:51:18 +0200 |
|---|---|---|
| committer | Mikko Syrjä <mikko@3d-system.fi> | 2019-02-28 01:51:18 +0200 |
| commit | ce5c4e3f8fdf2821ced67f673a6417f85fb27f19 (patch) | |
| tree | da6f3cabc38211ae48e94d0243a0412b1511199c | |
| parent | bc9e3fec14b0b17fb082f271f58f76c549704b59 (diff) | |
| download | symedit-ce5c4e3f8fdf2821ced67f673a6417f85fb27f19.tar.gz symedit-ce5c4e3f8fdf2821ced67f673a6417f85fb27f19.zip | |
Initial text support.
| -rw-r--r-- | Editor.qml | 72 | ||||
| -rw-r--r-- | main.qml | 7 | ||||
| -rw-r--r-- | symbol.cpp | 30 | ||||
| -rw-r--r-- | symedit.cpp | 34 | ||||
| -rw-r--r-- | symedit.h | 7 |
5 files changed, 118 insertions, 32 deletions
@@ -18,10 +18,10 @@ Rectangle enum Operation { - Fill = 70, // F Line = 68, // D Rectangle = 66, // B - Circle = 82 // R + Circle = 82, // R + Text = 33 // ! } property int units: 100 @@ -104,8 +104,8 @@ Rectangle startx -= (endx - startx) starty -= (endy - starty) } - manager.addPointItem(Editor.Operation.Rectangle, Qt.point(startx, starty), Qt.point(endx, endy), fillitem) - symbol = manager.getSymbol() + if ( manager.addPointItem(Editor.Operation.Rectangle, Qt.point(startx, starty), Qt.point(endx, endy), fillitem) ) + symbol = manager.getSymbol() } else if ( tool > 30 && tool < 40 ) // circle { @@ -130,8 +130,17 @@ Rectangle centerx = startx centery = starty } - manager.addValueItem(Editor.Operation.Circle, Qt.point(centerx, centery), radius, fillitem) - symbol = manager.getSymbol() + if ( manager.addValueItem(Editor.Operation.Circle, Qt.point(centerx, centery), radius, fillitem) ) + symbol = manager.getSymbol() + } + else if ( tool > 40 && tool < 50 ) // arc + { + + } + else if ( tool > 50 && tool < 60 ) // text + { + if ( manager.addTextItem(Editor.Operation.Text, Qt.point(endx, endy), textvalue, alignment) ) + symbol = manager.getSymbol() } } } @@ -159,7 +168,7 @@ Rectangle size.width * scalexy, size.height * scalexy) if ( fill ) { - context.fillStyle = (fill === 1 ? "black" : "white") + context.fillStyle = (fill === 2 ? "black" : "white") context.fill() } else @@ -172,13 +181,42 @@ Rectangle (max - center.y - radius + offset) * scalexy, radius * 2 * scalexy, radius * 2 * scalexy) if ( fill ) { - context.fillStyle = (fill === 1 ? "black" : "white") + context.fillStyle = (fill === 2 ? "black" : "white") context.fill() } else context.stroke() } + function setalignment(context, align) + { + switch ( align ) + { + case 1: context.textAlign = "right"; context.textBaseline = "top"; break + case 2: context.textAlign = "center"; context.textBaseline = "top"; break + case 3: context.textAlign = "left"; context.textBaseline = "top"; break + case 4: context.textAlign = "right"; context.textBaseline = "alphabetic"; break + case 5: context.textAlign = "center"; context.textBaseline = "alphabetic"; break + case 6: context.textAlign = "left"; context.textBaseline = "alphabetic"; break + case 7: context.textAlign = "right"; context.textBaseline = "bottom"; break + case 8: context.textAlign = "center"; context.textBaseline = "bottom"; break + case 9: context.textAlign = "left"; context.textBaseline = "bottom"; break + case 10: context.textAlign = "right"; context.textBaseline = "middle"; break + case 11: context.textAlign = "center"; context.textBaseline = "middle"; break + case 12: context.textAlign = "left"; context.textBaseline = "middle"; break + } + } + + function painttext(context, string, point) + { + var fontsize = 20 * textsize + context.font = fontsize.toString() + "px sans-serif" + var position = Qt.point((point.x + max + offset) * scalexy, (max - point.y + offset) * scalexy) + context.fillText(string, position.x, position.y) + if ( !preview ) + context.beginPath().ellipse(position.x - 5, position.y - 5, 10, 10).fill() + } + function paintgrid(context) { context.lineWidth = 0.2 @@ -238,6 +276,11 @@ Rectangle if ( !preview && fill && index === active ) paintcircle(context, position, radius, false) } + else if ( operation === Editor.Operation.Text ) + { + setalignment(context, manager.getItemAlign(index)) + painttext(context, manager.getItemText(index), position) + } } } @@ -248,7 +291,7 @@ Rectangle paintgrid(context) context.strokeStyle = "black" -// context.fillStyle = "black" + context.fillStyle = "black" paintsymbol(context) @@ -309,11 +352,16 @@ Rectangle paintcircle(context, center, radius, false) } } - else if ( tool > 40 && tool < 50 ) // text + else if ( tool > 40 && tool < 50 ) // arcs + { + + } + else if ( tool > 50 && tool < 60 ) // text { - if ( tool === Editor.Tool.Texts ) + if ( tool === Editor.Tool.Text ) { - //## + setalignment(context, alignment) + painttext(context, textvalue, Qt.point(mousex, mousey)) } } } @@ -174,7 +174,7 @@ ApplicationWindow { id: filllist implicitWidth: 50 - model: [ "0 No fill", "1 Area fill", "2 Backgroud" ] //%% + model: [ "0 No fill", "1 Backgroud", "2 Area fill" ] //%% onCurrentIndexChanged: { fillitem = currentIndex; editor.update() } function setFill() { currentIndex = fillitem } } @@ -214,7 +214,10 @@ ApplicationWindow "6 Base Left", //%% "7 Bottom Right", //%% "8 Bottom Center", //%% - "9 Bottom Left" //%% + "9 Bottom Left", //%% + "10 Middle Right", //%% + "11 Middle Center", //%% + "12 Middle Left" //%% ] onCurrentIndexChanged: { alignment = currentIndex + 1 } function setAlign() { currentIndex = alignment - 1 } @@ -53,7 +53,7 @@ SymEditSymbol::SymEditSymbol() : ActiveIndex(-1) void SymEditSymbol::Load(const QString& buffer) { Items.clear(); - int fill = 0; + int fill = 0, align = 9; QPoint position(0, 0); for ( const auto& string : buffer.split(';', QString::SkipEmptyParts) ) { @@ -69,6 +69,12 @@ void SymEditSymbol::Load(const QString& buffer) } else if ( operation == 'F' ) fill = string.mid(1).toInt(); + else if ( operation == 'J' ) + align = string.mid(1).toInt(); + else if ( operation == '!' ) + Items.push_back(Item(operation, position, string.mid(1), align)); + else if ( operation == '$' || operation == '#' ) + Items.push_back(Item(operation, position, string, align)); else // single parameter { int value = string.mid(1).toInt(); @@ -93,15 +99,15 @@ QString& SymEditSymbol::Save(QString& buffer) const buffer.append(',').append(value.setNum(point.y())); buffer.append(';'); }; - auto appendfill = [](QString& buffer, int fill) + auto appendoption = [](QChar option, QString& buffer, int index) { QString value; - buffer.append('F').append(value.setNum(fill)).append(';'); - return fill; + buffer.append(option).append(value.setNum(index)).append(';'); + return index; }; - int fill = 0; buffer.clear(); + int fill = 0, align = 9; QPoint position(0, 0); for ( const auto& item : Items ) switch ( item.Operation ) { @@ -109,7 +115,7 @@ QString& SymEditSymbol::Save(QString& buffer) const case 'B': // rectangle { if ( item.Fill != fill ) - fill = appendfill(buffer, item.Fill); + fill = appendoption('F', buffer, item.Fill); if ( item.Point != position ) appendvalue(buffer.append('U'), item.Point, 2); appendvalue(buffer.append(item.Operation), item.Value, 2); @@ -119,13 +125,23 @@ QString& SymEditSymbol::Save(QString& buffer) const case 'R': // circle { if ( item.Fill != fill ) - fill = appendfill(buffer, item.Fill); + fill = appendoption('F', buffer, item.Fill); if ( item.Point != position ) appendvalue(buffer.append('U'), item.Point, 2); appendvalue(buffer.append(item.Operation), item.Value, 1); position = item.Point; break; } + case '!': // text + { + if ( item.Align != align ) + align = appendoption('J', buffer, item.Align); + if ( item.Point != position ) + appendvalue(buffer.append('U'), item.Point, 2); + if ( item.Text.front() != '$' && item.Text.front() != '#' ) + buffer.append('!'); // constant text + buffer.append(item.Text).append(';'); + } } if ( !buffer.isEmpty() && buffer.back() == ';' ) buffer.chop(1); diff --git a/symedit.cpp b/symedit.cpp index 6e3b256..949fcf0 100644 --- a/symedit.cpp +++ b/symedit.cpp @@ -166,12 +166,13 @@ QString SymEditManager::getSymbol() const \param point Item position. \param value Item value. \param fill Item area fill. - \return Reference to item. + \return True for success. */ -void SymEditManager::addValueItem(int operation, QPoint point, int value, int fill) +bool SymEditManager::addValueItem(int operation, QPoint point, int value, int fill) { undosave(); Symbol.AddItem(operation, point, value, fill); + return true; } //! Add symbol item. @@ -180,12 +181,13 @@ void SymEditManager::addValueItem(int operation, QPoint point, int value, int fi \param point Item position. \param value Item value. \param fill Item area fill. - \return Reference to item. + \return True for success. */ -void SymEditManager::addPointItem(int operation, QPoint point, QPoint value, int fill) +bool SymEditManager::addPointItem(int operation, QPoint point, QPoint value, int fill) { undosave(); Symbol.AddItem(operation, point, value, fill); + return true; } //! Add symbol item. @@ -194,12 +196,17 @@ void SymEditManager::addPointItem(int operation, QPoint point, QPoint value, int \param point Item position. \param value Item text value. \param align Item text alignment. - \return Reference to item. + \return True for success. */ -void SymEditManager::addTextItem(int operation, QPoint point, QString value, int align) +bool SymEditManager::addTextItem(int operation, QPoint point, QString value, int align) { - undosave(); - Symbol.AddItem(operation, point, value, align); + if ( !value.isEmpty() ) + { + undosave(); + Symbol.AddItem(operation, point, value, align); + return true; + } + return false; } //! Remove active item. @@ -315,6 +322,17 @@ int SymEditManager::getItemFill(int index) const return 0; } +// +int SymEditManager::getItemAlign(int index) const +{ + if ( Symbol.GetItemCount() ) + { + const auto& item = Symbol.GetItem(index); + return item.Align; + } + return 0; +} + //! Select item nearest to point. /*! \param point Point coordinates. @@ -53,9 +53,9 @@ public: Q_INVOKABLE QString getSymbol() const; - Q_INVOKABLE void addValueItem(int operation, QPoint point, int value, int fill); - Q_INVOKABLE void addPointItem(int operation, QPoint point, QPoint value, int fill); - Q_INVOKABLE void addTextItem(int operation, QPoint point, QString value, int align); + Q_INVOKABLE bool addValueItem(int operation, QPoint point, int value, int fill); + Q_INVOKABLE bool addPointItem(int operation, QPoint point, QPoint value, int fill); + Q_INVOKABLE bool addTextItem(int operation, QPoint point, QString value, int align); Q_INVOKABLE bool removeItem(); Q_INVOKABLE int getItemCount() const; @@ -65,6 +65,7 @@ public: Q_INVOKABLE QPoint getItemPoint(int index) const; Q_INVOKABLE QString getItemText(int index ) const; Q_INVOKABLE int getItemFill(int index) const; + Q_INVOKABLE int getItemAlign(int index) const; Q_INVOKABLE int selectItem(QPoint point) const; Q_INVOKABLE void setActiveIndex(int index); |
