Manual for OpenFLUID 2.1.10

WareSrcFileEditor.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30  */
31 
32 /**
33  @file WareSrcFileEditor.hpp
34  @brief Header of ...
35 
36  @author Aline LIBRES <aline.libres@gmail.com>
37  */
38 
39 #ifndef __OPENFLUID_UIWARESDEV_WARESRCFILEEDITOR_HPP__
40 #define __OPENFLUID_UIWARESDEV_WARESRCFILEEDITOR_HPP__
41 
42 
43 #include <QPlainTextEdit>
44 #include <QCompleter>
45 #include <QSignalMapper>
46 #include <QMenu>
47 
48 #include <openfluid/dllexport.hpp>
52 #include <openfluid/ui/config.hpp>
53 
54 
55 namespace openfluid { namespace ui { namespace waresdev {
56 
57 
58 class WareSrcSyntaxHighlighter;
59 
60 
61 class OPENFLUID_API WareSrcFileEditor: public QPlainTextEdit, public WareFileEditor
62 {
63  Q_OBJECT
64 
65 
66  private slots:
67 
68  void updateLineNumberAreaWidth();
69 
70  void highlightCurrentLine();
71 
72  void updateLineNumberArea(const QRect& Rect, int);
73 
74  void onChanged(bool Changed);
75 
76  void onInsertRequested(const QString& Str);
77 
78  void insertCompletion();
79 
80  void onCompletionPopupCurrentRowChanged(const QModelIndex &Current, const QModelIndex& Previous);
81 
82 
83  private:
84 
85  class LineMarker
86  {
87  private:
88 
90  QStringList m_ContentList;
91 
92  QColor Red = QColor(openfluid::ui::config::LINEMARKER_ERRCOLOR);
93  QColor Orange = QColor(openfluid::ui::config::LINEMARKER_WARNCOLOR);
94 
95  public:
96 
97  QRect m_Rect;
98 
99  LineMarker(openfluid::waresdev::WareSrcMsgParser::WareSrcMsg::MessageType MsgType, const QString& Content) :
100  m_MajorMarkerType(MsgType)
101  {
102  QString Cleared = Content.trimmed();
103  if (!Cleared.isEmpty())
104  {
105  m_ContentList.append(Cleared);
106  }
107  }
108 
109 
110  // =====================================================================
111  // =====================================================================
112 
113 
114  QColor getColor() const
115  {
117  {
118  return Red;
119  }
120  return Orange;
121  }
122 
123 
124  // =====================================================================
125  // =====================================================================
126 
127 
128  QString getContent() const
129  {
130  if (m_ContentList.isEmpty())
131  {
132  return "";
133  }
134 
135  if (m_ContentList.size() == 1)
136  {
137  return QString("<html>%1</html>").arg(m_ContentList[0]);
138  }
139 
140  return QString("&bull;&nbsp;%1").arg(m_ContentList.join("<br/>&bull;&nbsp;"));
141  }
142 
143 
144  // =====================================================================
145  // =====================================================================
146 
147 
148  void update(openfluid::waresdev::WareSrcMsgParser::WareSrcMsg::MessageType MsgType, const QString& Content)
149  {
151  {
152  m_MajorMarkerType = MsgType;
153  }
154 
155  m_ContentList.append(Content.trimmed());
156  }
157  };
158 
159  WareSrcSyntaxHighlighter* mp_SyntaxHighlighter = nullptr;
160 
161  QRegExp m_SelectionTagsRegExp;
162  QRegExp m_AllTagsRegExp;
163  QRegExp m_WordPartRegExp;
164 
165  QWidget* mp_LineNumberArea;
166 
167  QColor m_LineColor;
168  QColor m_LineNbAreaColor = QColor("#F8F8FF");
169  QColor m_LineNbTextColor = QColor("#999999");
170 
171  QString m_IndentString;
172 
173  int m_SpaceCharWidth;
174 
176 
177  QSignalMapper* mp_SignalMapper;
178 
179  QMap<QString, QMenu*> m_InsertionMenus;
180 
181  QCompleter* mp_Completer;
182 
183  QMap<QTextBlock, LineMarker> m_LineMarkersByBlock;
184 
185  bool m_ShowLineMarkers = true;
186 
187  void writeString(const QString& Str, int InitialIndentInSpaceNb);
188 
189  void insertNewLine();
190 
191  bool findString(const QString& StringToFind, QTextDocument::FindFlags Options);
192 
193  bool replaceString(const QString& StringToFind, const QString& StringForReplace, Qt::CaseSensitivity Cs);
194 
195 
196  protected:
197 
198  void resizeEvent(QResizeEvent* Event);
199 
200  void contextMenuEvent(QContextMenuEvent* Event);
201 
202  void keyPressEvent(QKeyEvent* Event);
203 
204 
205  signals :
206 
207  void editorTxtChanged(WareFileEditor* Editor, bool Changed);
208 
209  void editorSaved();
210 
211 
212  public slots :
213 
214  void setShowLineMarkers(bool ShowMarkers);
215 
216 
217  public:
218 
219  WareSrcFileEditor(const QString& FilePath, QWidget* Parent = nullptr);
220 
221  virtual ~WareSrcFileEditor();
222 
223  void lineNumberAreaPaintEvent(QPaintEvent* Event);
224 
225  int lineNumberAreaWidth();
226 
227  void saveContent();
228 
229  void saveContentToPath(const QString& Path);
230 
231  void updateContent();
232 
233  bool findReplace(FindReplaceDialog::FindReplaceAction Action, const QString& StringToFind,
234  const QString& StringForReplace, QTextDocument::FindFlags Options, QString& Message);
235 
236  QString getSelectedText();
237 
238  void clearLineMessages();
239 
240  void addLineMessage(openfluid::waresdev::WareSrcMsgParser::WareSrcMsg Message);
241 
242  void updateLineNumberArea();
243 
244  void tooltipEvent(const QPoint& Position);
245 
246  void selectLine(int LineNumber);
247 
248  bool getShowLineMarkers();
249 
250  void goToLine();
251 
252  void updateSettings();
253 
254  bool isModified();
255 
256  QWidget* getWidget();
257 
258  void copy();
259 
260  void cut();
261 
262  void paste();
263 
264  void setFocus();
265 
266 };
267 
268 
269 // =====================================================================
270 // =====================================================================
271 
272 
273 class LineNumberArea: public QWidget
274 {
275  Q_OBJECT
276 
277  private:
278 
279  WareSrcFileEditor* mp_Editor;
280 
281  QAction* mp_ShowMarkersAction = nullptr;
282 
283 
284  protected:
285 
286  void paintEvent(QPaintEvent* Event)
287  {
288  mp_Editor->lineNumberAreaPaintEvent(Event);
289  }
290 
291 
292  // =====================================================================
293  // =====================================================================
294 
295 
296  bool event(QEvent* Event)
297  {
298  if (Event->type() == QEvent::ToolTip)
299  {
300  QHelpEvent* HelpEvent = static_cast<QHelpEvent*>(Event);
301  mp_Editor->tooltipEvent(HelpEvent->pos());
302  }
303  return QWidget::event(Event);
304  }
305 
306 
307  // =====================================================================
308  // =====================================================================
309 
310 
311  void contextMenuEvent(QContextMenuEvent* Event)
312  {
313  mp_ShowMarkersAction->setChecked(mp_Editor->getShowLineMarkers());
314 
315  QMenu* Menu = new QMenu();
316  Menu->addAction(mp_ShowMarkersAction);
317  Menu->exec(Event->globalPos());
318  delete Menu;
319  }
320 
321 
322  public:
323 
325  QWidget(Editor)
326  {
327  setStyleSheet("QToolTip {min-width:300px;}");
328 
329  mp_Editor = Editor;
330 
331  mp_ShowMarkersAction = new QAction("Show markers", this);
332  mp_ShowMarkersAction->setCheckable(true);
333 
334  connect(mp_ShowMarkersAction, SIGNAL(triggered(bool)), mp_Editor, SLOT(setShowLineMarkers(bool)));
335  }
336 
337 
338  // =====================================================================
339  // =====================================================================
340 
341 
342  QSize sizeHint() const
343  {
344  return QSize(mp_Editor->lineNumberAreaWidth(), 0);
345  }
346 };
347 
348 
349 } } } // namespaces
350 
351 
352 #endif /* __OPENFLUID_UIWARESDEV_WARESRCFILEEDITOR_HPP__ */
QVector< Rule > Rules
Definition: CompletionProvider.hpp:108
Definition: WareSrcFileEditor.hpp:61
Definition: ApplicationException.hpp:47
bool event(QEvent *Event)
Definition: WareSrcFileEditor.hpp:296
Definition: WareSrcFileEditor.hpp:273
Definition: WareSrcSyntaxHighlighter.hpp:54
void lineNumberAreaPaintEvent(QPaintEvent *Event)
Definition: WareSrcMsgParser.hpp:58
Definition: WareFileEditor.hpp:54
Header of ...
void paintEvent(QPaintEvent *Event)
Definition: WareSrcFileEditor.hpp:286
MessageType
Definition: WareSrcMsgParser.hpp:62
FindReplaceAction
Definition: FindReplaceDialog.hpp:67
LineNumberArea(WareSrcFileEditor *Editor)
Definition: WareSrcFileEditor.hpp:324
void tooltipEvent(const QPoint &Position)
#define OPENFLUID_API
Definition: dllexport.hpp:86
void contextMenuEvent(QContextMenuEvent *Event)
Definition: WareSrcFileEditor.hpp:311
QSize sizeHint() const
Definition: WareSrcFileEditor.hpp:342