NCL Composer  0.1.5
 All Classes Functions Variables Pages
NCLTextEditor.h
1 /*
2  * Copyright 2011 TeleMidia/PUC-Rio.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18 #ifndef NCLTEXTEDITOR_H
19 #define NCLTEXTEDITOR_H
20 
21 #include <QColor>
22 #include <QShortcut>
23 #include <QWheelEvent>
24 #include <QMessageBox>
25 #include <QDomDocument>
26 #include <Qsci/qsciscintilla.h>
27 
28 #include "MyLexer.h"
29 
30 #include "QsciLexerNCL.h"
31 #include "QsciNCLAPIs.h"
32 
33 #define PREF_FONT_FAMILY "Courier"
34 #define PREF_FONT_SIZE 10
35 
36 #define LIGTHSTYLE 1
37 
38 #ifdef LIGTHSTYLE
39 /* Light Style */
40  #define PREF_CARET_LINE_BG_COLOR QColor("#B9D3EE")
41  #define PREF_FOLD_MARGIN_FORE_COLOR QColor("#B9D3EE")
42  #define PREF_FOLD_MARGIN_BACK_COLOR QColor("#FFFFFF")
43  #define MARGINS_BACKGROUND_COLOR QColor("#ffffff")
44 #else
45  /* Dark Style */
46  #define PREF_CARET_LINE_BG_COLOR QColor("#B9D3EE")
47  #define PREF_FOLD_MARGIN_FORE_COLOR QColor("#FFFFFF")
48  #define PREF_FOLD_MARGIN_BACK_COLOR QColor("#AAAAAA")
49  #define MARGINS_BACKGROUND_COLOR QColor("#BBBBBB")
50 #endif
51 
53 
60 class NCLTextEditor : public QsciScintilla
61 {
62  Q_OBJECT
63 
64 public:
65  typedef enum
66  {
67  TAB_BEHAVIOR_DEFAULT = 1,
68  TAB_BEHAVIOR_NEXT_ATTR_AFTER_AUTOCOMPLETE,
69  TAB_BEHAVIOR_NEXT_ATTR
70  } TAB_BEHAVIOR;
71 
72  explicit NCLTextEditor(QWidget *parent = 0);
73  virtual ~NCLTextEditor();
74 
75  void setTabBehavior(TAB_BEHAVIOR tabBehavior);
76  void userFillingNextAttribute(int pos);
77  void userFillingPreviousAttribute(int pos);
78  void updateVisualFillingAttributeField( int line,
79  int index,
80  int &begin,
81  int &end);
82 
83  void keepFocused();
84  QString textWithoutUserInteraction();
85  void setTextWithoutUserInteraction(QString text);
86 
87  void setDocumentUrl(QString docURL);
88  QString getDocumentUrl();
89  bool parseDocument(bool recursive = true);
90  void updateElementsIDWithAlias(QDomDocument doc, QString alias);
91 
92  QDomElement elementById(const QDomDocument &domDoc, QString id);
93  QDomElement elementById(QString id, bool recursive = true);
94  QList <QDomElement> elementsByTagname(QString tagname);
95  QList <QDomElement> elementsByTagname(const QDomDocument &domDoc,
96  QString tagname);
97  QList <QDomElement> elementsByTagname(QString tagname, QString parentId);
98 
99 private:
100  enum INTERACTION_STATE
101  {
102  DEFAULT_STATE = 1,
103  FILLING_ATTRIBUTES_STATE
104  };
105 
106  INTERACTION_STATE interaction_state;
107 
108  QsciLexerNCL *nclexer;
109  QsciNCLAPIs *apis;
110  QDomDocument domDoc;
111  QString docURL;
112  QMap <QString, QDomDocument> domDocs;
113 
114  int error_indicator;
115  int error_marker;
116  int filling_attribute_indicator;
117 
118  TAB_BEHAVIOR tabBehavior;
119  bool focusInIgnoringCurrentText;
120  QString textWithoutUserInter;
121 
122  void initParameters();
123 
124  /* events */
125  void wheelEvent( QWheelEvent * event );
126  void keyPressEvent(QKeyEvent *event);
127  void keyReleaseEvent(QKeyEvent *event);
128  void mousePressEvent(QMouseEvent *e);
129 
130  bool parseImportedDocuments( QString currentFileURI,
131  QDomDocument &doc, bool recursive = true);
132 
133 protected:
134  void AutoCompleteCompleted();
135  void focusInEvent(QFocusEvent *e);
136  void focusOutEvent(QFocusEvent *e);
137  /*bool canInsertFromMimeData(const QMimeData *source) const;
138  QByteArray fromMimeData(const QMimeData *source, bool &rectangular) const;
139  void dragEnterEvent(QDragEnterEvent *event);
140  void dropEvent(QDropEvent *event);*/
141 
142 public slots:
143  void Increasefont();
144  void Decreasefont();
145  void clearErrorIndicators();
146  void clearFillingAttributeIndicator();
147  void markError(QString description, QString file, int line, int column = 0,
148  int severity = 0);
149  void MarkLine(int, int, Qt::KeyboardModifiers);
150  void formatText();
151 
152 signals:
153  void focusLosted(QFocusEvent *event);
154 };
155 
156 #endif // NCLTEXTEDITOR_H