LibrePCB Developers Documentation
editorwidgetbase.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_EDITOR_EDITORWIDGETBASE_H
21 #define LIBREPCB_EDITOR_EDITORWIDGETBASE_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../dialogs/graphicsexportdialog.h"
27 #include "../undostack.h"
28 #include "../widgets/rulechecklistwidget.h"
29 
31 #include <librepcb/core/qtcompat.h>
32 
33 #include <QtCore>
34 #include <QtWidgets>
35 
36 /*******************************************************************************
37  * Namespace / Forward Declarations
38  ******************************************************************************/
39 namespace librepcb {
40 
41 class Angle;
42 class Library;
43 class LibraryBaseElement;
44 class Point;
45 class Workspace;
46 
47 namespace editor {
48 
49 class ExclusiveActionGroup;
50 class IF_GraphicsLayerProvider;
51 class StatusBar;
52 class ToolBarProxy;
53 class UndoStackActionGroup;
54 
55 /*******************************************************************************
56  * Class EditorWidgetBase
57  ******************************************************************************/
58 
62 class EditorWidgetBase : public QWidget, protected IF_RuleCheckHandler {
63  Q_OBJECT
64 
65 public:
66  // Types
67 
68  struct Context {
72  bool readOnly;
73  const Library* library;
74  };
75 
76  enum Tool {
93  };
94 
95  enum class Feature {
96  // Handled by editor widgets (constant).
97  Close,
98  Filter,
100  OpenGlView,
101  ExportGraphics,
102  GenerateOutline,
103  GenerateCourtyard,
104 
105  // Handled by FSM states (dynamic).
106  SelectGraphics,
107  ImportGraphics,
108  Abort,
109  Cut,
110  Copy,
111  Paste,
112  Remove,
113  Move,
114  Rotate,
115  Mirror,
116  Flip,
117  SnapToGrid,
118  Properties,
119  };
120 
121  // Constructors / Destructor
122  EditorWidgetBase() = delete;
123  EditorWidgetBase(const EditorWidgetBase& other) = delete;
124  explicit EditorWidgetBase(const Context& context, const FilePath& fp,
125  QWidget* parent = nullptr);
126  virtual ~EditorWidgetBase() noexcept;
127 
128  // Getters
129  const FilePath& getFilePath() const noexcept { return mFilePath; }
130  bool isDirty() const noexcept {
131  return mManualModificationsMade || (!mUndoStack->isClean());
132  }
133  virtual QSet<Feature> getAvailableFeatures() const noexcept = 0;
134 
135  // Setters
136  virtual void connectEditor(UndoStackActionGroup& undoStackActionGroup,
137  ExclusiveActionGroup& toolsActionGroup,
138  QToolBar& commandToolBar,
139  StatusBar& statusBar) noexcept;
140  virtual void disconnectEditor() noexcept;
141 
142  // Operator Overloadings
143  EditorWidgetBase& operator=(const EditorWidgetBase& rhs) = delete;
144 
145 public slots:
146  virtual bool save() noexcept;
147  virtual bool selectAll() noexcept { return false; }
148  virtual bool cut() noexcept { return false; }
149  virtual bool copy() noexcept { return false; }
150  virtual bool paste() noexcept { return false; }
151  virtual bool move(Qt::ArrowType direction) noexcept {
152  Q_UNUSED(direction);
153  return false;
154  }
155  virtual bool rotate(const librepcb::Angle& rotation) noexcept {
156  Q_UNUSED(rotation);
157  return false;
158  }
159  virtual bool mirror(Qt::Orientation orientation) noexcept {
160  Q_UNUSED(orientation);
161  return false;
162  }
163  virtual bool flip(Qt::Orientation orientation) noexcept {
164  Q_UNUSED(orientation);
165  return false;
166  }
167  virtual bool snapToGrid() noexcept { return false; }
168  virtual bool remove() noexcept { return false; }
169  virtual bool editProperties() noexcept { return false; }
170  virtual bool zoomIn() noexcept { return false; }
171  virtual bool zoomOut() noexcept { return false; }
172  virtual bool zoomAll() noexcept { return false; }
173  virtual bool toggle3D() noexcept { return false; }
174  virtual bool abortCommand() noexcept { return false; }
175  virtual bool processGenerateOutline() noexcept { return false; }
176  virtual bool processGenerateCourtyard() noexcept { return false; }
177  virtual bool importDxf() noexcept { return false; }
178  virtual bool exportImage() noexcept;
179  virtual bool exportPdf() noexcept;
180  virtual bool print() noexcept;
181  virtual bool editGridProperties() noexcept { return false; }
182  virtual bool increaseGridInterval() noexcept { return false; }
183  virtual bool decreaseGridInterval() noexcept { return false; }
184 
185 protected: // Methods
186  void setupInterfaceBrokenWarningWidget(QWidget& widget) noexcept;
187  void setupErrorNotificationWidget(QWidget& widget) noexcept;
188  virtual bool isInterfaceBroken() const noexcept = 0;
189  virtual bool toolChangeRequested(Tool newTool,
190  const QVariant& mode) noexcept {
191  Q_UNUSED(newTool);
192  Q_UNUSED(mode);
193  return false;
194  }
195  virtual bool runChecks(RuleCheckMessageList& msgs) const = 0;
197  std::shared_ptr<const RuleCheckMessage> msg,
198  bool approve) noexcept;
200  const QString& settingsKey) noexcept {
201  Q_UNUSED(output);
202  Q_UNUSED(settingsKey);
203  return false;
204  }
205  void undoStackStateModified() noexcept;
206  void setStatusBarMessage(const QString& message, int timeoutMs = -1) noexcept;
207  const QStringList& getLibLocaleOrder() const noexcept;
208  QString getWorkspaceSettingsUserName() noexcept;
209 
210 private slots:
211  void updateCheckMessages() noexcept;
212 
213 private: // Methods
224  static bool askForRestoringBackup(const FilePath& dir);
225  void toolRequested(int tool, const QVariant& mode) noexcept;
226  void undoStackCleanChanged(bool clean) noexcept;
227  void scheduleLibraryElementChecks() noexcept;
228  virtual bool processRuleCheckMessage(
229  std::shared_ptr<const RuleCheckMessage> msg, bool applyFix) = 0;
231  std::shared_ptr<const RuleCheckMessage> msg) noexcept override;
233  std::shared_ptr<const RuleCheckMessage> msg) noexcept override;
235  std::shared_ptr<const RuleCheckMessage> msg) noexcept override;
237  std::shared_ptr<const RuleCheckMessage> msg) noexcept override;
239  std::shared_ptr<const RuleCheckMessage> msg) noexcept override;
240 
241 signals:
242  void dirtyChanged(bool dirty);
243  void elementEdited(const librepcb::FilePath& fp);
244  void interfaceBrokenChanged(bool broken);
245  void errorsAvailableChanged(bool hasErrors);
247  const QSet<librepcb::editor::EditorWidgetBase::Feature>& features);
248 
249 protected: // Data
252  std::shared_ptr<TransactionalFileSystem> mFileSystem;
253  QScopedPointer<UndoStack> mUndoStack;
257  QScopedPointer<ToolBarProxy> mCommandToolBarProxy;
261 
262  // Memorized message approvals
263  QSet<SExpression> mSupportedApprovals;
264  QSet<SExpression> mDisappearedApprovals;
265 };
266 
268  QtCompat::Hash seed = 0) noexcept {
269  return ::qHash(static_cast<int>(feature), seed);
270 }
271 
272 /*******************************************************************************
273  * End of File
274  ******************************************************************************/
275 
276 } // namespace editor
277 } // namespace librepcb
278 
279 #endif
void updateCheckMessages() noexcept
Definition: editorwidgetbase.cpp:287
void ruleCheckFixRequested(std::shared_ptr< const RuleCheckMessage > msg) noexcept override
Definition: editorwidgetbase.cpp:323
void undoStackStateModified() noexcept
Definition: editorwidgetbase.cpp:206
QSet< SExpression > mDisappearedApprovals
Definition: editorwidgetbase.h:264
The LibraryBaseElement class.
Definition: librarybaseelement.h:48
Definition: editorwidgetbase.h:88
QtCompat::Hash qHash(const EditorWidgetBase::Feature &feature, QtCompat::Hash seed=0) noexcept
Definition: editorwidgetbase.h:267
void setupErrorNotificationWidget(QWidget &widget) noexcept
Definition: editorwidgetbase.cpp:170
static bool askForRestoringBackup(const FilePath &dir)
Ask the user whether to restore a backup of a library element.
Definition: editorwidgetbase.cpp:250
The GraphicsView class.
Definition: graphicsview.h:51
ExclusiveActionGroup * mToolsActionGroup
Definition: editorwidgetbase.h:255
Definition: editorwidgetbase.h:85
Definition: editorwidgetbase.h:84
virtual void disconnectEditor() noexcept
Definition: editorwidgetbase.cpp:103
virtual bool zoomAll() noexcept
Definition: editorwidgetbase.h:172
virtual bool decreaseGridInterval() noexcept
Definition: editorwidgetbase.h:183
const QStringList & getLibLocaleOrder() const noexcept
Definition: editorwidgetbase.cpp:231
Definition: occmodel.cpp:77
QString getWorkspaceSettingsUserName() noexcept
Definition: editorwidgetbase.cpp:235
QSet< SExpression > mSupportedApprovals
Definition: editorwidgetbase.h:263
const IF_GraphicsLayerProvider & layerProvider
Definition: editorwidgetbase.h:70
virtual bool copy() noexcept
Definition: editorwidgetbase.h:149
virtual bool save() noexcept
Definition: editorwidgetbase.cpp:122
The UndoStackActionGroup class groups an undo-QAction and redo-QAction together and optionally connec...
Definition: undostackactiongroup.h:45
QVector< std::shared_ptr< const RuleCheckMessage > > RuleCheckMessageList
Definition: rulecheckmessage.h:104
The ExclusiveActionGroup class groups multiple QAction&#39;s together.
Definition: exclusiveactiongroup.h:54
Definition: editorwidgetbase.h:92
virtual bool rotate(const librepcb::Angle &rotation) noexcept
Definition: editorwidgetbase.h:155
UndoStackActionGroup * mUndoStackActionGroup
Definition: editorwidgetbase.h:254
Tool
Definition: editorwidgetbase.h:76
bool readOnly
Definition: editorwidgetbase.h:72
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:78
virtual bool editProperties() noexcept
Definition: editorwidgetbase.h:169
Definition: editorwidgetbase.h:82
virtual bool cut() noexcept
Definition: editorwidgetbase.h:148
Definition: rulechecklistwidget.h:44
virtual bool processRuleCheckMessage(std::shared_ptr< const RuleCheckMessage > msg, bool applyFix)=0
virtual bool abortCommand() noexcept
Definition: editorwidgetbase.h:174
EditorWidgetBase & operator=(const EditorWidgetBase &rhs)=delete
Feature
Definition: editorwidgetbase.h:95
virtual bool exportImage() noexcept
Definition: editorwidgetbase.cpp:132
const FilePath & getFilePath() const noexcept
Definition: editorwidgetbase.h:129
virtual bool move(Qt::ArrowType direction) noexcept
Definition: editorwidgetbase.h:151
Definition: editorwidgetbase.h:89
bool mManualModificationsMade
Modifications bypassing the undo stack.
Definition: editorwidgetbase.h:258
virtual bool flip(Qt::Orientation orientation) noexcept
Definition: editorwidgetbase.h:163
void undoStackCleanChanged(bool clean) noexcept
Definition: editorwidgetbase.cpp:272
void setupInterfaceBrokenWarningWidget(QWidget &widget) noexcept
Definition: editorwidgetbase.cpp:148
void setStatusBarMessage(const QString &message, int timeoutMs=-1) noexcept
Definition: editorwidgetbase.cpp:217
Definition: editorwidgetbase.h:78
virtual bool increaseGridInterval() noexcept
Definition: editorwidgetbase.h:182
virtual bool processGenerateCourtyard() noexcept
Definition: editorwidgetbase.h:176
Definition: editorwidgetbase.h:87
virtual bool isInterfaceBroken() const noexcept=0
virtual bool mirror(Qt::Orientation orientation) noexcept
Definition: editorwidgetbase.h:159
The StatusBar class extends QStatusBar for some commonly used fields.
Definition: statusbar.h:45
std::shared_ptr< TransactionalFileSystem > mFileSystem
Definition: editorwidgetbase.h:252
Workspace & workspace
Definition: editorwidgetbase.h:69
Definition: editorwidgetbase.h:80
virtual QSet< Feature > getAvailableFeatures() const noexcept=0
virtual bool zoomOut() noexcept
Definition: editorwidgetbase.h:171
bool elementIsNewlyCreated
Definition: editorwidgetbase.h:71
Context mContext
Definition: editorwidgetbase.h:250
The IF_GraphicsLayerProvider class defines an interface for classes which provide layers...
Definition: graphicslayer.h:111
Definition: editorwidgetbase.h:79
virtual bool toolChangeRequested(Tool newTool, const QVariant &mode) noexcept
Definition: editorwidgetbase.h:189
virtual bool print() noexcept
Definition: editorwidgetbase.cpp:140
virtual void connectEditor(UndoStackActionGroup &undoStackActionGroup, ExclusiveActionGroup &toolsActionGroup, QToolBar &commandToolBar, StatusBar &statusBar) noexcept
Definition: editorwidgetbase.cpp:85
virtual ~EditorWidgetBase() noexcept
Definition: editorwidgetbase.cpp:78
Definition: editorwidgetbase.h:83
FilePath mFilePath
Definition: editorwidgetbase.h:251
void toolRequested(int tool, const QVariant &mode) noexcept
Definition: editorwidgetbase.cpp:268
virtual bool runChecks(RuleCheckMessageList &msgs) const =0
virtual bool toggle3D() noexcept
Definition: editorwidgetbase.h:173
void ruleCheckMessageDoubleClicked(std::shared_ptr< const RuleCheckMessage > msg) noexcept override
Definition: editorwidgetbase.cpp:344
Definition: editorwidgetbase.h:81
virtual bool importDxf() noexcept
Definition: editorwidgetbase.h:177
bool isDirty() const noexcept
Definition: editorwidgetbase.h:130
virtual bool execGraphicsExportDialog(GraphicsExportDialog::Output output, const QString &settingsKey) noexcept
Definition: editorwidgetbase.h:199
void errorsAvailableChanged(bool hasErrors)
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:129
void ruleCheckMessageSelected(std::shared_ptr< const RuleCheckMessage > msg) noexcept override
Definition: editorwidgetbase.cpp:339
void ruleCheckDescriptionRequested(std::shared_ptr< const RuleCheckMessage > msg) noexcept override
Definition: editorwidgetbase.cpp:332
virtual bool exportPdf() noexcept
Definition: editorwidgetbase.cpp:136
virtual bool editGridProperties() noexcept
Definition: editorwidgetbase.h:181
uint Hash
Return type of Qt&#39;s qHash() function.
Definition: qtcompat.h:58
void interfaceBrokenChanged(bool broken)
virtual bool selectAll() noexcept
Definition: editorwidgetbase.h:147
QScopedPointer< ToolBarProxy > mCommandToolBarProxy
Definition: editorwidgetbase.h:257
Definition: editorwidgetbase.h:91
bool mIsInterfaceBroken
Definition: editorwidgetbase.h:259
The Library class represents a library directory.
Definition: library.h:46
Definition: editorwidgetbase.h:77
virtual bool paste() noexcept
Definition: editorwidgetbase.h:150
virtual bool snapToGrid() noexcept
Definition: editorwidgetbase.h:167
bool ruleCheckFixAvailable(std::shared_ptr< const RuleCheckMessage > msg) noexcept override
Definition: editorwidgetbase.cpp:313
virtual bool zoomIn() noexcept
Definition: editorwidgetbase.h:170
void availableFeaturesChanged(const QSet< librepcb::editor::EditorWidgetBase::Feature > &features)
The EditorWidgetBase class.
Definition: editorwidgetbase.h:62
Output
Definition: graphicsexportdialog.h:83
Definition: editorwidgetbase.h:90
OpenGL 3D viewer widget.
Definition: openglview.h:52
QString mStatusBarMessage
Definition: editorwidgetbase.h:260
virtual bool processGenerateOutline() noexcept
Definition: editorwidgetbase.h:175
StatusBar * mStatusBar
Definition: editorwidgetbase.h:256
const Library * library
Definition: editorwidgetbase.h:73
Definition: editorwidgetbase.h:68
void elementEdited(const librepcb::FilePath &fp)
void scheduleLibraryElementChecks() noexcept
Definition: editorwidgetbase.cpp:277
The Workspace class represents a workspace with all its data (library, projects, settings, ...)
Definition: workspace.h:54
Definition: editorwidgetbase.h:86
QScopedPointer< UndoStack > mUndoStack
Definition: editorwidgetbase.h:253
void setMessageApproved(LibraryBaseElement &element, std::shared_ptr< const RuleCheckMessage > msg, bool approve) noexcept
Definition: editorwidgetbase.cpp:190