Schim
sheetscene.h
Go to the documentation of this file.
1 #ifndef SHEETSCENE_H
3 #define SHEETSCENE_H
4 
5 #include "model/sheet.h"
6 #include "model/component.h"
7 #include "model/terminal.h"
8 
9 #include "ui/vim.h"
10 
11 #include <QGraphicsScene>
12 #include <QGraphicsItem>
13 #include <QUndoStack>
14 #include <QList>
15 #include <QPair>
16 
17 class GObject;
18 class GHeader;
19 class GComponent;
20 class GTerminal;
22 class SceneOperation;
23 
27 class SheetScene : public QGraphicsScene
28 {
29  Q_OBJECT
30 
31 public:
32  // CONSTRUCTORS
38  explicit SheetScene(Sheet *sheet);
39 
40  // GETTERS
41  Sheet *getSheet();
42  const Sheet *getSheet() const;
46  QPointF getRawCursorPos() const;
55  QPointF getCursorPos() const;
56  QPointF getSnappedCursorPos() const;
57  bool getSnapCursorGuides() const;
58  bool isGridEnabled() const;
59  bool isSnapEnabled() const;
60  QSizeF getGridSize() const;
67  QPointF snap(const QPointF &pt) const;
75  QPointF forcedSnap(const QPointF &pt) const;
76 
77  // SETTERS
78  void setSheet(Sheet *sheet);
84  void setHeader(Header *hdr, bool destroy = true);
85  void setSnapCursorGuides(bool snap);
86  void setGridSize(float x, float y = -1);
87  void setGridSize(QSizeF size);
88  void setGridEnabled(bool enabled);
89  void setSnapEnabled(bool snap);
90 
91  // USER ACTIONS
92  void undo(Vim::N n = 0);
93  void redo(Vim::N n = 0);
94  void command(QUndoCommand *command);
95  void cursorLeft(Vim::N n = 0);
96  void cursorDown(Vim::N n = 0);
97  void cursorUp(Vim::N n = 0);
98  void cursorRight(Vim::N n = 0);
99  void gridIncrease(Vim::N n = 0);
100  void gridDecrease(Vim::N n = 0);
107  void startOperation(SceneOperation *op);
108  void selectTexts();
109  void selectPrimitive();
110  void insertLine();
111  void insertRect();
112  void insertText();
113  void suggestConnections(GComponent *component);
114  void showGuides(bool show);
126  void tryChangeHeader(Header *hdr, bool *changed = nullptr,
127  bool *confirmed = nullptr);
128 
129  // OVERRIDDE QGraphicsScene
130  GObject *itemAt(const QPointF &pt, const QTransform &deviceTransform);
131 
132  // OTHER METHODS
133  void reload();
134 
135 signals:
136  void itemMoveRequested();
137 
138 private slots:
139  void onSelectionChanged();
140 
141 private:
142  // HELPERS
143  QPointF constrainToContentArea(QPointF pt) const;
144  void applyCursorMovement(const QPointF &pt);
149  void insertComponentOrHeader(const Object &obj);
150  bool askHeaderChangeConfirmation() const;
151  QList<QPair<Terminal::Prong, Terminal::Prong>>
152  getConnectionSuggestions(GComponent *component);
153  void registerConnectionSuggestion(Terminal::Prong a, Terminal::Prong b);
154 
155  // EVENTS
156  void keyPressEvent(QKeyEvent *event) override;
157  void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
158  void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
159  void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
160  void dragMoveEvent(QGraphicsSceneDragDropEvent *event) override;
161  void dragEnterEvent(QGraphicsSceneDragDropEvent *event) override;
162  void dropEvent(QGraphicsSceneDragDropEvent *event) override;
163 
164  bool processVimAction(const Vim::Action &action);
165 
166 private slots:
167  // SLOTS
168  void onOperationFinished();
169 
170  friend class SheetView;
171 
172 private:
173  // ATTRIBUTES
174  Sheet *sheet{};
175  QUndoStack undoStack{this};
176  GHeader *headerItem{};
178  QGraphicsRectItem *pageBackgroundItem;
179  QList<GConnectionSuggester*> _suggesters;
181  SceneOperation *operation{};
183  QPointF rawCursorPos;
185  bool snapGuides = false,
186  showCursorGuides = true;
187  GObject *hoveredItem{};
189  float gridX = 5, gridY = 5;
190  bool gridEnabled = true, snapEnabled = true;
191 
192  // FRIENDS
193  friend class GObject;
194 };
195 
196 #endif // SHEETSCENE_H
The abstract base class for all objects in a sheet.
Definition: object.h:21
void startOperation(SceneOperation *op)
Start a scene operation.
QPointF snap(const QPointF &pt) const
Return the point on the grid that is closest to pt.
QPointF getCursorPos() const
Return the processed cursor pos.
Graphical object that wraps a Component.
Definition: gcomponent.h:12
QPointF getRawCursorPos() const
Return the raw cursor position, before applying snap.
Count n()
The vim-count from the current key sequence.
A specialized QGraphicsScene containing a sheet of paper.
Definition: sheetscene.h:27
An action in vim-mode.
Definition: vim.h:81
void setHeader(Header *hdr, bool destroy=true)
Set the header in the scene and in the model.
Class representing a header for a sheet.
Definition: header.h:15
Definition: gterminal.h:27
bool enabled()
Is vim-mode enabled?
The base class of all objects represented in a graphical scene.
Definition: gobject.h:24
Definition: gterminal.h:8
QPointF forcedSnap(const QPointF &pt) const
Return the point on the grid that is closest to pt.
Definition: terminal.h:45
SheetScene(Sheet *sheet)
Construct a scene based on the specified sheet.
The base class of all scene operations.
Definition: operations.h:22
Graphical object that wraps a Header.
Definition: gheader.h:13
This class models a sheet of paper.
Definition: sheet.h:16
A specialized QGraphicsView that displays a sheet of paper.
Definition: sheetview.h:16
Representation of a vim-count.
Definition: vim.h:48
void tryChangeHeader(Header *hdr, bool *changed=nullptr, bool *confirmed=nullptr)
Try to set the sheet header to hdr and update the scene.