aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikko Syrjä <mikko@3d-system.fi>2019-03-01 21:52:01 +0200
committerMikko Syrjä <mikko@3d-system.fi>2019-03-01 21:52:01 +0200
commita00e1b93ae146d94124c1a865c026857147b4fe7 (patch)
treeb7dac51916a51a1e3c031dd90da795b844a993b8
parentce5c4e3f8fdf2821ced67f673a6417f85fb27f19 (diff)
downloadsymedit-a00e1b93ae146d94124c1a865c026857147b4fe7.tar.gz
symedit-a00e1b93ae146d94124c1a865c026857147b4fe7.zip
Text edit improvements.
-rw-r--r--Editor.qml52
-rw-r--r--main.cpp3
-rw-r--r--symbol.cpp84
-rw-r--r--symbol.h30
-rw-r--r--symedit.cpp12
5 files changed, 113 insertions, 68 deletions
diff --git a/Editor.qml b/Editor.qml
index 5b93bce..c33f89f 100644
--- a/Editor.qml
+++ b/Editor.qml
@@ -1,4 +1,5 @@
import QtQuick 2.9
+import Org.Syrja.Symbol.Operation 1.0
Rectangle
{
@@ -16,14 +17,6 @@ Rectangle
Text = 51
}
- enum Operation
- {
- Line = 68, // D
- Rectangle = 66, // B
- Circle = 82, // R
- Text = 33 // !
- }
-
property int units: 100
property int max: units / 2
property int grid: 10
@@ -81,20 +74,21 @@ Rectangle
onReleased:
{
+ down = false
+ endx = mousex
+ endy = mousey
if ( tool === Editor.Tool.Select )
{
- manager.selectItem(Qt.point(mousex, mousey))
+ manager.selectItem(Qt.point(endx, endy))
canvas.requestPaint()
}
else if ( !preview )
{
- endx = mousex
- endy = mousey
if ( startx != endx || starty != endy )
{
if ( tool === Editor.Tool.Line )
{
- manager.addPointItem(Editor.Operation.Line, Qt.point(startx, starty), Qt.point(endx, endy), false)
+ manager.addPointItem(Operation.Line, Qt.point(startx, starty), Qt.point(endx, endy), false)
symbol = manager.getSymbol()
}
else if ( tool === Editor.Tool.RectCenter || tool === Editor.Tool.RectCorner )
@@ -104,7 +98,7 @@ Rectangle
startx -= (endx - startx)
starty -= (endy - starty)
}
- if ( manager.addPointItem(Editor.Operation.Rectangle, Qt.point(startx, starty), Qt.point(endx, endy), fillitem) )
+ if ( manager.addPointItem(Operation.Rectangle, Qt.point(startx, starty), Qt.point(endx, endy), fillitem) )
symbol = manager.getSymbol()
}
else if ( tool > 30 && tool < 40 ) // circle
@@ -130,7 +124,7 @@ Rectangle
centerx = startx
centery = starty
}
- if ( manager.addValueItem(Editor.Operation.Circle, Qt.point(centerx, centery), radius, fillitem) )
+ if ( manager.addValueItem(Operation.Circle, Qt.point(centerx, centery), radius, fillitem) )
symbol = manager.getSymbol()
}
else if ( tool > 40 && tool < 50 ) // arc
@@ -139,12 +133,11 @@ Rectangle
}
else if ( tool > 50 && tool < 60 ) // text
{
- if ( manager.addTextItem(Editor.Operation.Text, Qt.point(endx, endy), textvalue, alignment) )
+ if ( manager.addTextItem(Operation.Text, Qt.point(endx, endy), textvalue, alignment) )
symbol = manager.getSymbol()
}
}
}
- down = false
}
}
@@ -207,14 +200,23 @@ Rectangle
}
}
- function painttext(context, string, point)
+ function painttext(context, string, point, active)
{
- var fontsize = 20 * textsize
+ context.lineWidth = textsize * zoomscale / 2
+ var fontsize = 20 * textsize * zoomscale
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)
+ context.strokeText(string, position.x, position.y)
if ( !preview )
+ {
+ if ( active )
+ context.fillStyle = "red"
context.beginPath().ellipse(position.x - 5, position.y - 5, 10, 10).fill()
+ if ( active )
+ context.fillStyle = "black"
+ }
+ context.lineWidth = linewidth
}
function paintgrid(context)
@@ -255,12 +257,12 @@ Rectangle
var operation = manager.getItemOperation(index)
var point, position = manager.getItemPosition(index)
var fill = manager.getItemFill(index)
- if ( operation === Editor.Operation.Line )
+ if ( operation === Operation.Line )
{
point = manager.getItemPoint(index)
paintline(context, position, point)
}
- else if ( operation === Editor.Operation.Rectangle )
+ else if ( operation === Operation.Rectangle )
{
point = manager.getItemPoint(index)
var deltax = point.x - position.x
@@ -269,17 +271,17 @@ Rectangle
if ( !preview && fill && index === active )
paintrect(context, position, Qt.size(deltax, deltay), false)
}
- else if ( operation === Editor.Operation.Circle )
+ else if ( operation === Operation.Circle )
{
var radius = manager.getItemValue(index)
paintcircle(context, position, radius, fill)
if ( !preview && fill && index === active )
paintcircle(context, position, radius, false)
}
- else if ( operation === Editor.Operation.Text )
+ else if ( operation === Operation.Text )
{
setalignment(context, manager.getItemAlign(index))
- painttext(context, manager.getItemText(index), position)
+ painttext(context, manager.getItemText(index), position, index === active)
}
}
}
@@ -298,7 +300,7 @@ Rectangle
if ( down )
{
context.strokeStyle = "red"
- context.fillStyle = "red"
+// context.fillStyle = "red"
var cornerx = (mousex < startx ? mousex : startx)
var cornery = (mousey > starty ? mousey : starty)
@@ -361,7 +363,7 @@ Rectangle
if ( tool === Editor.Tool.Text )
{
setalignment(context, alignment)
- painttext(context, textvalue, Qt.point(mousex, mousey))
+ painttext(context, textvalue, Qt.point(mousex, mousey), true)
}
}
}
diff --git a/main.cpp b/main.cpp
index 75481a6..26d215e 100644
--- a/main.cpp
+++ b/main.cpp
@@ -15,6 +15,9 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv);
+ qmlRegisterUncreatableMetaObject(Operation::staticMetaObject,
+ "Org.Syrja.Symbol.Operation", 1, 0, "Operation", "Error: only enums" );
+
SymEditManager manager;
QQmlApplicationEngine engine;
diff --git a/symbol.cpp b/symbol.cpp
index 19e2ddb..499a44c 100644
--- a/symbol.cpp
+++ b/symbol.cpp
@@ -5,9 +5,12 @@
#include "symbol.h"
const double ConstPi = 3.14159265358979323846; //!< Constant for PI.
-const double Const2Pi = 2.0 * ConstPi; //!< Constant for PI * 2.
const double ConstPi2 = ConstPi / 2.0; //!< Constant for PI / 2.
-const double ConstPi4 = ConstPi / 4.0; //!< Constant for PI / 4.
+
+namespace Operation
+{
+ Q_NAMESPACE
+}
//
// symbol item functions
@@ -20,17 +23,17 @@ const double ConstPi4 = ConstPi / 4.0; //!< Constant for PI / 4.
\param value Item value.
\param fill Item area fill.
*/
-SymEditSymbol::Item::Item(int operation, QPoint point, int value, int fill)
+SymEditSymbol::Item::Item(Operation::Type operation, QPoint point, int value, int fill)
: Operation(operation), Point(point), Value(value, value), Fill(fill), Align(9)
{
}
-SymEditSymbol::Item::Item(int operation, QPoint point, QPoint value, int fill)
+SymEditSymbol::Item::Item(Operation::Type operation, QPoint point, QPoint value, int fill)
: Operation(operation), Point(point), Value(value), Fill(fill), Align(0)
{
}
-SymEditSymbol::Item::Item(int operation, QPoint point, QString value, int align)
+SymEditSymbol::Item::Item(Operation::Type operation, QPoint point, QString value, int align)
: Operation(operation), Point(point), Text(value), Fill(0), Align(align)
{
@@ -57,28 +60,31 @@ void SymEditSymbol::Load(const QString& buffer)
QPoint position(0, 0);
for ( const auto& string : buffer.split(';', QString::SkipEmptyParts) )
{
- int operation = string.at(0).toLatin1();
+ int type = string.at(0).toLatin1();
int comma = string.indexOf(',');
if ( comma >= 0 )
{
int x = string.mid(1, comma - 1).toInt();
int y = string.mid(comma + 1).toInt();
- if ( operation != 'U' )
- Items.push_back(Item(operation, position, QPoint(x, y), fill));
+ if ( type == 'D' )
+ Items.push_back(Item(Operation::Line, position, QPoint(x, y), fill));
+ else if ( type == 'B' )
+ Items.push_back(Item(Operation::Rectangle, position, QPoint(x, y), fill));
position = QPoint(x, y);
}
- 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();
- Items.push_back(Item(operation, position, value, fill));
+ if ( type == 'F' )
+ fill = value;
+ else if ( type == 'J' )
+ align = value;
+ else if ( type == 'R' )
+ Items.push_back(Item(Operation::Circle, position, value, fill));
+ else if ( type == '!' )
+ Items.push_back(Item(Operation::Text, position, string.mid(1), align));
+ else if ( type == '$' || type == '#' )
+ Items.push_back(Item(Operation::Text, position, string, align));
}
}
ActiveIndex = static_cast<int>(Items.size()) - 1;
@@ -111,28 +117,28 @@ QString& SymEditSymbol::Save(QString& buffer) const
QPoint position(0, 0);
for ( const auto& item : Items ) switch ( item.Operation )
{
- case 'D': // line
- case 'B': // rectangle
+ case Operation::Line:
+ case Operation::Rectangle:
{
if ( item.Fill != 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);
+ appendvalue(buffer.append(item.Operation == Operation::Line ? 'D' : 'B'), item.Value, 2);
position = item.Value;
break;
}
- case 'R': // circle
+ case Operation::Circle:
{
if ( item.Fill != 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);
+ appendvalue(buffer.append('R'), item.Value, 1);
position = item.Point;
break;
}
- case '!': // text
+ case Operation::Text:
{
if ( item.Align != align )
align = appendoption('J', buffer, item.Align);
@@ -141,7 +147,14 @@ QString& SymEditSymbol::Save(QString& buffer) const
if ( item.Text.front() != '$' && item.Text.front() != '#' )
buffer.append('!'); // constant text
buffer.append(item.Text).append(';');
+ break;
+ }
+ case Operation::Arc:
+ {
+ //##
+ break;
}
+ default: assert(0);
}
if ( !buffer.isEmpty() && buffer.back() == ';' )
buffer.chop(1);
@@ -164,21 +177,21 @@ void SymEditSymbol::Clear()
\param fill Item area fill.
\return Reference to item.
*/
-SymEditSymbol::Item& SymEditSymbol::AddItem(int operation, QPoint point, int value, int fill)
+SymEditSymbol::Item& SymEditSymbol::AddItem(Operation::Type operation, QPoint point, int value, int fill)
{
Item item(operation, point, value, fill);
ActiveIndex = static_cast<int>(Items.size());
Items.push_back(item);
return Items.back();
}
-SymEditSymbol::Item& SymEditSymbol::AddItem(int operation, QPoint point, QPoint value, int fill)
+SymEditSymbol::Item& SymEditSymbol::AddItem(Operation::Type operation, QPoint point, QPoint value, int fill)
{
Item item(operation, point, value, fill);
ActiveIndex = static_cast<int>(Items.size());
Items.push_back(item);
return Items.back();
}
-SymEditSymbol::Item& SymEditSymbol::AddItem(int operation, QPoint point, QString value, int align)
+SymEditSymbol::Item& SymEditSymbol::AddItem(Operation::Type operation, QPoint point, QString value, int align)
{
Item item(operation, point, value, align);
ActiveIndex = static_cast<int>(Items.size());
@@ -246,12 +259,12 @@ int SymEditSymbol::SelectItem(QPoint point) const
{
switch ( item.Operation )
{
- case 'D': // line
+ case Operation::Line:
{
distance(point, item.Point, item.Value);
break;
}
- case 'B': // rectangle
+ case Operation::Rectangle:
{
distance(point, item.Point, QPoint(item.Point.x(), item.Value.y()));
distance(point, QPoint(item.Point.x(), item.Value.y()), item.Value);
@@ -259,11 +272,22 @@ int SymEditSymbol::SelectItem(QPoint point) const
distance(point, QPoint(item.Value.x(), item.Point.y()), item.Point);
break;
}
- case 'R': // circle
+ case Operation::Circle:
{
check(fabs(length(difference(point, item.Point)) - item.Value.x()));
break;
}
+ case Operation::Text:
+ {
+ check(fabs(length(difference(point, item.Point))));
+ break;
+ }
+ case Operation::Arc:
+ {
+ //##
+ break;
+ }
+ default: assert(0);
}
++index;
}
@@ -322,7 +346,7 @@ void SymEditSymbol::RotateSymbol(int dir)
for ( auto& item : Items )
{
rotate(item.Point, dir);
- if ( item.Operation != 'R' )
+ if ( item.Operation == Operation::Line || item.Operation == Operation::Rectangle )
rotate(item.Value, dir);
}
}
diff --git a/symbol.h b/symbol.h
index aa4f3d9..0ddd759 100644
--- a/symbol.h
+++ b/symbol.h
@@ -5,6 +5,22 @@
#include <QPoint>
#include <QString>
+#include <QObject>
+
+namespace Operation //!< Item operation namespace.
+{
+ Q_NAMESPACE
+ enum Type //!< Item operation type.
+ {
+ None = 0, //!< No operation.
+ Line = 1, //!< Straight line with two points.
+ Rectangle = 2, //!< Rectangle with two corners.
+ Circle = 3, //!< Circle with center and radius.
+ Text = 4, //!< Text with single position point.
+ Arc = 5 //!< Semicircle with two endpoints.
+ };
+ Q_ENUM_NS(Type)
+}
//! Symbol class.
class SymEditSymbol
@@ -15,11 +31,11 @@ public:
class Item //!< Symbol item.
{
public:
- Item(int operation, QPoint point, int value, int fill);
- Item(int operation, QPoint point, QPoint value, int fill);
- Item(int operation, QPoint point, QString value, int align);
+ Item(Operation::Type operation, QPoint point, int value, int fill);
+ Item(Operation::Type operation, QPoint point, QPoint value, int fill);
+ Item(Operation::Type operation, QPoint point, QString value, int align);
- int Operation; //!< Item operation.
+ Operation::Type Operation; //!< Item operation.
QPoint Point; //!< Item coordinates.
QPoint Value; //!< Item value.
QString Text; //!< Text string.
@@ -31,9 +47,9 @@ public:
QString& Save(QString& buffer) const;
void Clear();
- Item& AddItem(int operation, QPoint point, int value, int fill);
- Item& AddItem(int operation, QPoint point, QPoint end, int fill);
- Item& AddItem(int operation, QPoint point, QString text, int align);
+ Item& AddItem(Operation::Type operation, QPoint point, int value, int fill);
+ Item& AddItem(Operation::Type operation, QPoint point, QPoint end, int fill);
+ Item& AddItem(Operation::Type operation, QPoint point, QString text, int align);
bool RemoveItem(int index);
int SelectItem(QPoint point) const;
diff --git a/symedit.cpp b/symedit.cpp
index 949fcf0..06db2ce 100644
--- a/symedit.cpp
+++ b/symedit.cpp
@@ -171,7 +171,7 @@ QString SymEditManager::getSymbol() const
bool SymEditManager::addValueItem(int operation, QPoint point, int value, int fill)
{
undosave();
- Symbol.AddItem(operation, point, value, fill);
+ Symbol.AddItem(static_cast<Operation::Type>(operation), point, value, fill);
return true;
}
@@ -186,7 +186,7 @@ bool SymEditManager::addValueItem(int operation, QPoint point, int value, int fi
bool SymEditManager::addPointItem(int operation, QPoint point, QPoint value, int fill)
{
undosave();
- Symbol.AddItem(operation, point, value, fill);
+ Symbol.AddItem(static_cast<Operation::Type>(operation), point, value, fill);
return true;
}
@@ -203,7 +203,7 @@ bool SymEditManager::addTextItem(int operation, QPoint point, QString value, int
if ( !value.isEmpty() )
{
undosave();
- Symbol.AddItem(operation, point, value, align);
+ Symbol.AddItem(static_cast<Operation::Type>(operation), point, value, align);
return true;
}
return false;
@@ -244,7 +244,7 @@ int SymEditManager::getItemOperation(int index) const
const auto& item = Symbol.GetItem(index);
return item.Operation;
}
- return 0;
+ return Operation::None;
}
//! Get item position.
@@ -257,7 +257,7 @@ QPoint SymEditManager::getItemPosition(int index) const
if ( Symbol.GetItemCount() )
{
const auto& item = Symbol.GetItem(index);
- if ( item.Operation == 'B' ) // normalize to upper left
+ if ( item.Operation == Operation::Rectangle ) // normalize to upper left
return QPoint(std::min(item.Value.x(), item.Point.x()), std::max(item.Value.y(), item.Point.y()));
return item.Point;
}
@@ -289,7 +289,7 @@ QPoint SymEditManager::getItemPoint(int index) const
if ( Symbol.GetItemCount() )
{
const auto& item = Symbol.GetItem(index);
- if ( item.Operation == 'B' ) // normalize to lower right
+ if ( item.Operation == Operation::Rectangle ) // normalize to lower right
return QPoint(std::max(item.Value.x(), item.Point.x()), std::min(item.Value.y(), item.Point.y()));
return item.Value;
}