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