SymbolEditor
1.3.0
|
Manages the editor window allowing user interaction with the various tools. More...
#include <Editor.h>
Public Types | |
enum | ToolMode { MoveTo, LineTo, CubicTo, Rectangle, Ellipse, Character } |
Public Slots | |
void | selectTool (QAction *action) |
void | charSelected (const QChar &character) |
void | enableSnap (bool enabled) |
void | selectFilled (bool enabled) |
void | selectFillRule (QAction *action) |
void | selectCapStyle (QAction *action) |
void | selectJoinStyle (QAction *action) |
void | increaseLineWidth () |
void | decreaseLineWidth () |
void | rotateLeft () |
void | rotateRight () |
void | flipHorizontal () |
void | flipVertical () |
void | scalePreferred () |
void | readSettings () |
Signals | |
void | message (const QString &text) |
void | minLineWidth (bool reached) |
void | maxLineWidth (bool reached) |
Public Member Functions | |
Editor (QWidget *parent=0) | |
~Editor () | |
QPair< qint16, Symbol > | symbol () |
void | setSymbol (const QPair< qint16, Symbol > &pair) |
QPainterPath | moveTo (const QPointF &to) |
QPainterPath | lineTo (const QPointF &to) |
QPainterPath | cubicTo (const QPointF &control1, const QPointF &control2, const QPointF &to) |
QPainterPath | addRectangle (const QPointF &from, const QPointF &to) |
QPainterPath | addEllipse (const QPointF &from, const QPointF &to) |
void | removeLast (const QPainterPath &path) |
void | movePoint (int index, const QPointF &to) |
void | rotatePointsLeft () |
void | rotatePointsRight () |
void | flipPointsHorizontal () |
void | flipPointsVertical () |
void | setFilled (bool filled) |
void | setFillRule (Qt::FillRule rule) |
void | setCapStyle (Qt::PenCapStyle capStyle) |
void | setJoinStyle (Qt::PenJoinStyle joinStyle) |
void | setLineWidth (double width) |
QPainterPath | setPath (const QPainterPath &path) |
void | clear () |
QUndoStack * | undoStack () |
Protected Member Functions | |
void | mousePressEvent (QMouseEvent *event) |
void | mouseMoveEvent (QMouseEvent *event) |
void | mouseReleaseEvent (QMouseEvent *event) |
void | paintEvent (QPaintEvent *event) |
void | keyPressEvent (QKeyEvent *event) |
Private Member Functions | |
void | addPoint (const QPointF &point) |
QPointF | snapPoint (const QPoint &point) const |
QPair< bool, QPointF > | snapToGrid (const QPoint &point) const |
QPair< bool, QPointF > | snapToGuide (const QPointF &point) const |
QPointF | toSymbol (const QPoint &point) const |
QPoint | toScreen (const QPointF &point) const |
bool | node (const QPointF &point) const |
QPair< bool, int > | nodeUnderCursor (const QPointF &point) const |
void | deconstructPainterPath () |
void | constructPainterPath () |
bool | constructGuides (const QPointF &to) |
void | constructLineGuides (const QPointF &from, const QPointF &to) |
void | constructCircleGuides (const QPointF &from, const QPointF &to) |
QLineF | projected (const QLineF &line) const |
Private Attributes | |
int | m_size |
bool | m_snap |
bool | m_fill |
QUndoStack | m_undoStack |
ToolMode | m_toolMode |
QList< QPointF > | m_points |
QList< QPainterPath::ElementType > | m_elements |
QList< QPointF > | m_activePoints |
qint16 | m_index |
QPainterPath | m_painterPath |
Symbol | m_symbol |
bool | m_dragging |
QPointF | m_start |
QPointF | m_tracking |
QRectF | m_rubberBand |
QPair< bool, int > | m_dragPointIndex |
QVector< qreal > | m_angles |
QList< QLineF > | m_guideLines |
QList< qreal > | m_guideCircles |
QList< QPointF > | m_snapPoints |
QLineF | m_topEdge |
QLineF | m_bottomEdge |
QLineF | m_leftEdge |
QLineF | m_rightEdge |
KCharSelect * | m_charSelect |
int | m_gridElements |
int | m_elementSize |
int | m_elementGrouping |
int | m_pointSize |
double | m_snapThreshold |
int | m_borderSize |
QColor | m_preferredSizeColor |
QColor | m_guideLineColor |
Manages the editor window allowing user interaction with the various tools.
The Editor class is used to edit the new symbols or the symbols from the loaded library. It shows a grid of gridElements of elementSize grouped into elementGroup number of elements.
The path is a QPainterPath element and is constructed from the various elements created by the tools. The constructed QPainterPath is used to draw the path in the editor so it is an accurate representation of the final path. The QPainterPath is deconstructed into its individual elements and points for editing.
There are additional attributes used to draw the path, filled or unfilled, the fill rule, line width, end cap and join styles can be changed to alter the appearance of the path.
The user interacts with the editor using the various tools. For each tool a number of points need to be defined, simple tools like move to and line to only require one point whereas cubic to requires three to define the two control points and the end point.
Convenience tools are available for rectangles and ellipses but these are broken down into lines and curves in the constructed path.
Points that have been added can be moved to adjust the shape by dragging them around. The points will snap to the grid intersections if the snap option is enabled. Otherwise they can be positioned anywhere.
Tools are available to rotate or flip the symbol which adjusts the points positions and reconstructs the QPainterPath.
Guide lines and circles will be displayed if the current position of the mouse on the editor aligns with an existing point either horizontally, vertically or at a specified angle, or if the point lies on the same circle as the original where its origin is at the center of the grid.
enum Editor::ToolMode |
|
explicit |
Construct the Editor. The editor is a sub class of a QWidget and is added to a layout widget. The boundary edges of the symbol editor are defined in the range 0..1. These are used to intersect the guidelines which are shown at the m_angles relative to existing points
parent | a pointer to the parent widget, this is passed to the base class object |
Definition at line 169 of file Editor.cpp.
Editor::~Editor | ( | ) |
Destructor for the Editor. No additional destruction is required here.
Definition at line 192 of file Editor.cpp.
QPainterPath Editor::addEllipse | ( | const QPointF & | from, |
const QPointF & | to | ||
) |
Add an ellipse as a new sub path. The points are in symbol coordinates. Returns the current painterPath for later undo.
from | a const reference to a QPointF representing the first corner |
to | a const reference to a QPointF representing the second corner |
Definition at line 317 of file Editor.cpp.
|
private |
Add a point for the current tool. Each tool requires a number of points to be defined to fully describe the shape the tool represents. This function adds the points and keeps track of the number of points required. When the number of points required have been selected, the tool command can be implemented and added to the symbol undo stack. The points are in symbol coordinates in the range 0..1
point | a const reference to a QPointF |
Definition at line 873 of file Editor.cpp.
QPainterPath Editor::addRectangle | ( | const QPointF & | from, |
const QPointF & | to | ||
) |
Add a rectangle as a new sub path. The points are in symbol coordinates. Returns the current painterPath for later undo.
from | a const reference to a QPointF representing the first corner |
to | a const reference to a QPointF representing the second corner |
Definition at line 297 of file Editor.cpp.
|
slot |
Called when a character is selected from the KCharSelect widget. Create a QPainterPath object and make it available to the editor for insertion.
char | a QChar containing the character selected. |
Definition at line 582 of file Editor.cpp.
void Editor::clear | ( | ) |
Clear the editor to represent a blank symbol. Delete the existing data and update the display.
Definition at line 525 of file Editor.cpp.
|
private |
Construct circular guides for the given point. Calculate the possible intersection points where point p would be if it was mirrored across the vertical and horizontal center lines, this would be comparable to it being on a circle whose center is at the grid center. There are special cases where the point is on the center line. These mirrored points will not be included as they are the same as the new one.
from | a const reference to a QPointF representing the point to test, from m_points or m_activePoints |
to | a const reference to a QPointF representing the cursor position |
Definition at line 1395 of file Editor.cpp.
|
private |
Construct guides for the point relative to the other points. Iterate through points in m_points and m_active points passing them to the constructLineGuides and constructCircleGuides with the new point to create guide lines conforming with the requirements for each.
to | a const reference to a QPointF representing the new point position |
Definition at line 1347 of file Editor.cpp.
|
private |
Construct line guides for the given point. The angle formed by the line from-to is tested against the allowed angles. If the angle is acceptable a new QLineF is created projected to the edges of the grid.
from | a const reference to a QPointF representing the point to test, from m_points or m_activePoints |
to | a const reference to a QPointF representing the cursor position |
Definition at line 1372 of file Editor.cpp.
|
private |
Construct a QPainterPath from the commands and points. Initialise an empty QPainterPath. Iterate all elements stored in m_elements and add them to the path.
Definition at line 1309 of file Editor.cpp.
QPainterPath Editor::cubicTo | ( | const QPointF & | control1, |
const QPointF & | control2, | ||
const QPointF & | to | ||
) |
Add a cubic spline to the sub path. The points are in symbol coordinates. Returns the current painterPath for later undo.
control1 | a const reference to a QPointF representing the first control point |
control2 | a const reference to a QPointF representing the second control point |
to | a const reference to a QPointF representing the end point of the curve |
Definition at line 277 of file Editor.cpp.
|
private |
Convert a QPainterPath to commands and points. Clear the current lists of points. Iterate all the elements of the QPainterPath adding commands and points as necessary.
Definition at line 1271 of file Editor.cpp.
|
slot |
Decrease the thickness of the path.
Definition at line 673 of file Editor.cpp.
|
slot |
Switch the snap mode on or off.
enabled | true if on, false otherwise |
Definition at line 605 of file Editor.cpp.
|
slot |
Flip the symbol horizontally about the vertical center line.
Definition at line 700 of file Editor.cpp.
void Editor::flipPointsHorizontal | ( | ) |
Flip all the points about a vertical axis passing through the symbol center.
Definition at line 397 of file Editor.cpp.
void Editor::flipPointsVertical | ( | ) |
Flip all the points about a horizontal axis passing through the symbol center.
Definition at line 419 of file Editor.cpp.
|
slot |
Flip the symbol vertically about the horizontal center line.
Definition at line 709 of file Editor.cpp.
|
slot |
Increase the thickness of the path.
Definition at line 664 of file Editor.cpp.
|
protected |
Process key presses to check for Escape to clear the current points being entered.
event | a pointer to a QKeyEvent |
Definition at line 1076 of file Editor.cpp.
QPainterPath Editor::lineTo | ( | const QPointF & | to | ) |
Add a line to the sub path. The point is in symbol coordinates. Returns the current painterPath for later undo.
to | a const reference to a QPointF representing the end point of the line |
Definition at line 256 of file Editor.cpp.
|
signal |
|
signal |
|
signal |
|
protected |
Process the mouse move event. Effective for the left mouse button pressed. Clear any currently defined guides. If dragging a point, update the points position and update the view, otherwise if the toolmode is for a rectangle or an ellipse initialise the rubber band rectange and update the view. If the left button is not pressed, test if the cursor is over an existing point and if it is change the cursor to a SizeAllCursor to indicate it can be moved, otherwise reset it to an ArrowCursor. Guide lines are constructed for each of the existing points to show if the new position lies in line with or at a specific angle to or on a circular path to that point.
event | a pointer to a QMouseEvent |
Definition at line 788 of file Editor.cpp.
|
protected |
Process the mouse press event. Effective for the left mouse button pressed. Test if the cursor is over an existing point in which case dragging of the point is enabled. Otherwise a point is added to the active points.
event | a pointer to a QMouseEvent |
Definition at line 753 of file Editor.cpp.
|
protected |
Process the mouse release event. If dragging a point that is in the m_points list and it hasn't been dragged back to its start position create a MovePointCommand and push it onto the editor undo stack. Pushing commands onto the stack will execute it, although the point is already in its new position, this will have no adverse effects, but will record the move for possible undo/redo later. If not dragging a point, clear the rubber band rectangle and add a point if the new point is not the same as the starting point.
event | a pointer to a QMouseEvent |
Definition at line 841 of file Editor.cpp.
void Editor::movePoint | ( | int | index, |
const QPointF & | to | ||
) |
Move a point node to a new position.
index | the index of the point to move |
to | a const reference to a QPointF representing the new position |
Definition at line 346 of file Editor.cpp.
QPainterPath Editor::moveTo | ( | const QPointF & | to | ) |
Move the start of the sub path. The point is in symbol coordinates. Returns the current painterPath for later undo.
to | a const reference to a QPointF representing the new start position |
Definition at line 237 of file Editor.cpp.
|
private |
Test if a control point is at the point specified. This will test both the committed m_points and the uncommitted m_activePoints
point | a const reference to a QPointF representing the symbol coordinate |
Definition at line 1211 of file Editor.cpp.
|
private |
Find the index of the node that is at the point specified. This will test both the committed m_points and the uncommitted m_activePoints
point | a const reference to a QPointF representing the point to find |
Definition at line 1244 of file Editor.cpp.
|
protected |
Paint the contents of the editor. This will fill the background and draw a grid based on the elementSize and elementGroup. For each element of the current path the control points are drawn with suitable lines joining them, for example for a cubic curve, a curve is drawn, but the control points are joined with dashed lines. A complete path is constructed and painted in a light colour with transparency to show the current symbol shape. TODO scale the painter earlier to avoid the use of toScreen.
event | a pointer to a QPaintEvent |
Definition at line 919 of file Editor.cpp.
|
private |
Project the line to the edges of the grid. Calculate the points where the projected line would intersect the edges. Check which edges get intersected within the coordinate 0..1 which will determine which points are required to construct the projected line. A special case occurs when the line is a diagonal passing through the corners as it intersects all sides within the coordinate range 0..1
line | a const reference to a QLineF |
Definition at line 1430 of file Editor.cpp.
|
slot |
Read the settings from the configuration file and apply them. The widget is resized to accommodate a gridElements number of cells of width elementSize. An additional 1 is added to allow for a right and bottom edge to be drawn.
Definition at line 729 of file Editor.cpp.
void Editor::removeLast | ( | const QPainterPath & | path | ) |
Remove the last added element by restoring the path that existed before the last command.
path | a const reference to a QPainterPath being restored |
Definition at line 332 of file Editor.cpp.
|
slot |
Rotate the symbol left (counter clock wise)
Definition at line 682 of file Editor.cpp.
void Editor::rotatePointsLeft | ( | ) |
Rotate all the points around the center of the symbol 90 degrees counter clockwise.
Definition at line 357 of file Editor.cpp.
void Editor::rotatePointsRight | ( | ) |
Rotate all the points around the center of the symbol 90 degrees clockwise
Definition at line 377 of file Editor.cpp.
|
slot |
Rotate the symbol right (clock wise)
Definition at line 691 of file Editor.cpp.
|
slot |
Scale the symbol to fit within the preferred size.
Definition at line 718 of file Editor.cpp.
|
slot |
Select the pen cap style. The specified value is taken from the sending actions data.
action | a pointer to the action that triggered the selection |
Definition at line 641 of file Editor.cpp.
|
slot |
Switch the fill mode on or off.
filled | true if on, false otherwise |
Definition at line 616 of file Editor.cpp.
|
slot |
Select the fill rule to be used when filling is enabled. The specified rule is taken from the sending actions data.
action | a pointer to the action triggering the selection |
Definition at line 628 of file Editor.cpp.
|
slot |
Select the pen join style. The specified value is taken from the sending actions data.
action | a pointer to the action that triggered the selection |
Definition at line 654 of file Editor.cpp.
|
slot |
Select the next tool. Remove any points that may have been used in the last tool command but not yet committed. Set the tool mode from the sending actions data. If the character selector tool is selected, display a KCharSelect widget to get a character to be inserted otherwise hide the KCharSelect widget.
action | a pointer to the action triggering the selection |
Definition at line 558 of file Editor.cpp.
void Editor::setCapStyle | ( | Qt::PenCapStyle | capStyle | ) |
Set the pen cap style, see the QPen documentation for details on the styles.
capStyle | a Qt::PenCapStyle value |
Definition at line 467 of file Editor.cpp.
void Editor::setFilled | ( | bool | filled | ) |
Set the fill state of the symbols path.
filled | true if the path is to be filled, false for outline paths |
Definition at line 443 of file Editor.cpp.
void Editor::setFillRule | ( | Qt::FillRule | rule | ) |
Set the fill rule of the symbols path.
rule | a Qt::FillRule value, see the QPainterPath documentation for details |
Definition at line 455 of file Editor.cpp.
void Editor::setJoinStyle | ( | Qt::PenJoinStyle | joinStyle | ) |
Set the pen join style, see the QPen documentation for details on the styles.
joinStyle | a Qt::PenJoinStyle value |
Definition at line 479 of file Editor.cpp.
void Editor::setLineWidth | ( | double | width | ) |
Set the line width. The width parameter is rounded to 2 decimal places to avoid errors in the comparisons. Signals are emitted to enable or disable the relevant actions depending on the values.
width | the new line width |
Definition at line 493 of file Editor.cpp.
QPainterPath Editor::setPath | ( | const QPainterPath & | path | ) |
Set the painter path to the requested path updating the points lists and the view.
path | contains the new path |
Definition at line 510 of file Editor.cpp.
void Editor::setSymbol | ( | const QPair< qint16, Symbol > & | pair | ) |
Set the symbol and index to be edited. The undo stack will be cleared.
pair | a QPair consisting of the index and the Symbol |
Definition at line 217 of file Editor.cpp.
|
private |
Convert a point to a snap point.
The point comes from the mouse events pos() value and will be converted to a value in symbol coordinates. If snapping is enabled, the point will be snapped to a guide or to a grid intersection. If snapping is disabled, the point is returned converted to the symbol coordinates.
point | a const reference to a QPoint |
Definition at line 1121 of file Editor.cpp.
|
private |
Convert a point to a symbol snap point. The point will have come from the mouse events pos() value and will be converted to a value that represents an intersection of the grid lines. This will happen only if snapping is enabled.
point | a const reference to a QPoint |
Definition at line 1143 of file Editor.cpp.
|
private |
Convert a point to a guide snap point. The point will have come from the mouse events pos() value and will be converted to a value that is within the threshold of a calculated guide intersection. This will happen only if the snapping is enabled.
point | a const reference to a QPointF |
Definition at line 1168 of file Editor.cpp.
QPair< qint16, Symbol > Editor::symbol | ( | ) |
Get the index and Symbol. Update the Symbol with the path being edited.
Definition at line 204 of file Editor.cpp.
|
private |
Convert a symbol point to a screen point. The symbol point will be a QPointF in the range 0..1 and will be converted to a value representing the position in the editor.
point | a const reference to a QPointF |
Definition at line 1195 of file Editor.cpp.
|
private |
Convert a point to a symbol point.
The point will have come from the mouse events pos() value and will be converted to a value representing a point in the symbol, not necessarily at a snap position.
point | a const reference to a QPoint |
Definition at line 1101 of file Editor.cpp.
QUndoStack * Editor::undoStack | ( | ) |
Get a pointer to the undo stack.
Definition at line 543 of file Editor.cpp.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |