Documentation for OpenFLUID 2.2.0
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 
35  @author Aline LIBRES <aline.libres@gmail.com>
36  @author Armel THÖNI <armel.thoni@inrae.fr>
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>
53 #include <openfluid/ui/config.hpp>
54 
55 
56 namespace openfluid { namespace ui { namespace waresdev {
57 
58 
59 class WareSrcSyntaxHighlighter;
60 
61 
62 class OPENFLUID_API WareSrcFileEditor: public QPlainTextEdit, public WareFileEditor
63 {
64  Q_OBJECT
65 
66 
67  private slots:
68 
69  void updateLineNumberAreaWidth();
70 
71  void highlightCurrentLine();
72 
73  void updateLineNumberArea(const QRect& Rect, int);
74 
75  void onChanged(bool Changed);
76 
77  void onInsertRequested(const QString& Str);
78 
79  void insertCompletion();
80 
81  void onCompletionPopupCurrentRowChanged(const QModelIndex &Current, const QModelIndex& Previous);
82 
83 
84  private:
85 
86  class LineMarker
87  {
88  private:
89 
91  QStringList m_ContentList;
92 
93  QColor Red = QColor(openfluid::ui::config::LINEMARKER_ERRCOLOR);
94  QColor Orange = QColor(openfluid::ui::config::LINEMARKER_WARNCOLOR);
95 
96  public:
97 
98  QRect m_Rect;
99 
100  LineMarker(WareSrcMsgParser::WareSrcMsg::MessageType MsgType, const QString& Content) :
101  m_MajorMarkerType(MsgType)
102  {
103  QString Cleared = Content.trimmed();
104  if (!Cleared.isEmpty())
105  {
106  m_ContentList.append(Cleared);
107  }
108  }
109 
110 
111  // =====================================================================
112  // =====================================================================
113 
114 
115  QColor getColor() const
116  {
118  {
119  return Red;
120  }
121  return Orange;
122  }
123 
124 
125  // =====================================================================
126  // =====================================================================
127 
128 
129  QString getContent() const
130  {
131  if (m_ContentList.isEmpty())
132  {
133  return "";
134  }
135 
136  if (m_ContentList.size() == 1)
137  {
138  return QString("<html>%1</html>").arg(m_ContentList[0]);
139  }
140 
141  return QString("&bull;&nbsp;%1").arg(m_ContentList.join("<br/>&bull;&nbsp;"));
142  }
143 
144 
145  // =====================================================================
146  // =====================================================================
147 
148 
149  void update(WareSrcMsgParser::WareSrcMsg::MessageType MsgType, const QString& Content)
150  {
152  {
153  m_MajorMarkerType = MsgType;
154  }
155 
156  m_ContentList.append(Content.trimmed());
157  }
158  };
159 
160  WareSrcSyntaxHighlighter* mp_SyntaxHighlighter = nullptr;
161 
162 #if (QT_VERSION_MAJOR < 6)
163  QRegExp m_SelectionTagsRegExp;
164  QRegExp m_AllTagsRegExp;
165  QRegExp m_WordPartRegExp;
166 #else
167  QRegularExpression m_SelectionTagsRegExp;
168  QRegularExpression m_AllTagsRegExp;
169  QRegularExpression m_WordPartRegExp;
170 #endif
171 
172  QWidget* mp_LineNumberArea;
173 
174  QColor m_LineColor;
175  QColor m_LineNbAreaColor = QColor("#F8F8FF");
176  QColor m_LineNbTextColor = QColor("#999999");
177 
178  QString m_IndentString;
179 
180  int m_SpaceCharWidth;
181 
183 
184  QSignalMapper* mp_SignalMapper;
185 
186  QMap<QString, QMenu*> m_InsertionMenus;
187 
188  QCompleter* mp_Completer;
189 
190  QMap<QTextBlock, LineMarker> m_LineMarkersByBlock;
191 
192  bool m_ShowLineMarkers = true;
193 
194  void writeString(const QString& Str, int InitialIndentInSpaceNb);
195 
196  void insertNewLine();
197 
198  bool findString(const QString& StringToFind, QTextDocument::FindFlags Options);
199 
200  bool replaceString(const QString& StringToFind, const QString& StringForReplace, Qt::CaseSensitivity Cs);
201 
202 
203  protected:
204 
205  void resizeEvent(QResizeEvent* Event);
206 
207  void contextMenuEvent(QContextMenuEvent* Event);
208 
209  void keyPressEvent(QKeyEvent* Event);
210 
211 
212  signals :
213 
214  void editorTxtChanged(WareFileEditor* Editor, bool Changed);
215 
216  void editorSaved();
217 
218 
219  public slots :
220 
221  void setShowLineMarkers(bool ShowMarkers);
222 
223 
224  public:
225 
226  WareSrcFileEditor(const QString& FilePath, QWidget* Parent = nullptr);
227 
229 
230  void lineNumberAreaPaintEvent(QPaintEvent* Event);
231 
233 
234  void saveContent();
235 
236  void saveContentToPath(const QString& Path);
237 
239 
240  bool findReplace(FindReplaceDialog::FindReplaceAction Action, const QString& StringToFind,
241  const QString& StringForReplace, QTextDocument::FindFlags Options, QString& Message);
242 
243  QString getSelectedText();
244 
246 
248 
250 
251  void tooltipEvent(const QPoint& Position);
252 
253  void selectLine(int LineNumber);
254 
256 
257  void goToLine();
258 
260 
261  bool isModified();
262 
263  QWidget* getWidget();
264 
265  void copy();
266 
267  void cut();
268 
269  void paste();
270 
271  void setFocus();
272 
273 };
274 
275 
276 // =====================================================================
277 // =====================================================================
278 
279 
280 class LineNumberArea: public QWidget
281 {
282  Q_OBJECT
283 
284  private:
285 
286  WareSrcFileEditor* mp_Editor;
287 
288  QAction* mp_ShowMarkersAction = nullptr;
289 
290 
291  protected:
292 
293  void paintEvent(QPaintEvent* Event)
294  {
295  mp_Editor->lineNumberAreaPaintEvent(Event);
296  }
297 
298 
299  // =====================================================================
300  // =====================================================================
301 
302 
303  bool event(QEvent* Event)
304  {
305  if (Event->type() == QEvent::ToolTip)
306  {
307  QHelpEvent* HelpEvent = static_cast<QHelpEvent*>(Event);
308  mp_Editor->tooltipEvent(HelpEvent->pos());
309  }
310  return QWidget::event(Event);
311  }
312 
313 
314  // =====================================================================
315  // =====================================================================
316 
317 
318  void contextMenuEvent(QContextMenuEvent* Event)
319  {
320  mp_ShowMarkersAction->setChecked(mp_Editor->getShowLineMarkers());
321 
322  QMenu* Menu = new QMenu();
323  Menu->addAction(mp_ShowMarkersAction);
324  Menu->exec(Event->globalPos());
325  delete Menu;
326  }
327 
328 
329  public:
330 
332  QWidget(Editor)
333  {
334  setStyleSheet("QToolTip {min-width:300px;}");
335 
336  mp_Editor = Editor;
337 
338  mp_ShowMarkersAction = new openfluid::ui::common::DefaultAction("Show markers", this);
339  mp_ShowMarkersAction->setCheckable(true);
340 
341  connect(mp_ShowMarkersAction, SIGNAL(triggered(bool)), mp_Editor, SLOT(setShowLineMarkers(bool)));
342  }
343 
344 
345  // =====================================================================
346  // =====================================================================
347 
348 
349  QSize sizeHint() const
350  {
351  return QSize(mp_Editor->lineNumberAreaWidth(), 0);
352  }
353 };
354 
355 
356 } } } // namespaces
357 
358 
359 #endif /* __OPENFLUID_UIWARESDEV_WARESRCFILEEDITOR_HPP__ */
Header of ...
Definition: DefaultAction.hpp:54
QVector< Rule > Rules
Definition: CompletionProvider.hpp:108
FindReplaceAction
Definition: FindReplaceDialog.hpp:68
Definition: WareSrcFileEditor.hpp:281
LineNumberArea(WareSrcFileEditor *Editor)
Definition: WareSrcFileEditor.hpp:331
void paintEvent(QPaintEvent *Event)
Definition: WareSrcFileEditor.hpp:293
QSize sizeHint() const
Definition: WareSrcFileEditor.hpp:349
bool event(QEvent *Event)
Definition: WareSrcFileEditor.hpp:303
void contextMenuEvent(QContextMenuEvent *Event)
Definition: WareSrcFileEditor.hpp:318
Definition: WareFileEditor.hpp:55
Definition: WareSrcFileEditor.hpp:63
void saveContentToPath(const QString &Path)
void contextMenuEvent(QContextMenuEvent *Event)
void editorTxtChanged(WareFileEditor *Editor, bool Changed)
bool findReplace(FindReplaceDialog::FindReplaceAction Action, const QString &StringToFind, const QString &StringForReplace, QTextDocument::FindFlags Options, QString &Message)
void lineNumberAreaPaintEvent(QPaintEvent *Event)
void tooltipEvent(const QPoint &Position)
void resizeEvent(QResizeEvent *Event)
void addLineMessage(WareSrcMsgParser::WareSrcMsg Message)
WareSrcFileEditor(const QString &FilePath, QWidget *Parent=nullptr)
MessageType
Definition: WareSrcMsgParser.hpp:72
Definition: WareSrcSyntaxHighlighter.hpp:55
#define OPENFLUID_API
Definition: dllexport.hpp:86
FilesystemPath Path
Definition: FilesystemPath.hpp:308
Definition: ApplicationException.hpp:47