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