QCodeEdit  2.2
qeditor.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
4 **
5 ** This file is part of the Edyuk project <http://edyuk.org>
6 **
7 ** This file may be used under the terms of the GNU General Public License
8 ** version 3 as published by the Free Software Foundation and appearing in the
9 ** file GPL.txt included in the packaging of this file.
10 **
11 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 **
14 ****************************************************************************/
15 
16 #ifndef _QEDITOR_H_
17 #define _QEDITOR_H_
18 
19 #include "qce-config.h"
20 
26 #include <QHash>
27 #include <QPointer>
28 #include <QScrollBar>
29 #include <QBasicTimer>
30 #include <QFontMetrics>
31 #include <QAbstractScrollArea>
32 
33 #include "qdocument.h"
34 #include "qdocumentcursor.h"
35 
36 #ifdef _QMDI_
37  #include "qmdiclient.h"
38 #endif
39 
40 class QMenu;
41 class QAction;
42 class QMimeData;
43 class QTextCodec;
44 class QActionGroup;
45 
46 class QReliableFileWatch;
47 
49 
52 
54 
56 #ifdef _QMDI_
57 , public qmdiClient
58 #endif
59 {
60  friend class QEditConfig;
61  friend class QEditorFactory;
62 
63  Q_OBJECT
64 
65  public:
67  {
68  NoUpdate = 0,
69  UpdateOld = 1,
70  UpdateDefault = 2,
71  UpdateCustom = 4,
72 
73  UpdateAll = 7
74  };
75 
76  enum EditFlag
77  {
78  None = 0,
79 
80  Overwrite = 0x001,
81  CursorOn = 0x002,
82  ReadOnly = 0x004,
83  MousePressed = 0x008,
84  MaybeDrag = 0x010,
85  Selection = 0x020,
86  EnsureVisible = 0x040,
87 
88  FoldedCursor = 0x100,
89 
90  Internal = 0x00000fff,
91 
92  LineWrap = 0x00001000,
93 
94  CtrlNavigation = 0x00010000,
95  CursorJumpPastWrap = 0x00020000,
96 
97  ReplaceTabs = 0x00100000,
98  RemoveTrailing = 0x00200000,
99  PreserveTrailingIndent = 0x00400000,
100  AdjustIndent = 0x00800000,
101 
102  AutoCloseChars = 0x01000000,
103  AutoIndent = 0x02000000,
104 
105  Accessible = 0xfffff000
106  };
107 
108  Q_DECLARE_FLAGS(State, EditFlag)
109 
110  struct PlaceHolder
111  {
112  class Affector
113  {
114  public:
115  virtual ~Affector() {}
116  virtual void affect(const QStringList& base, int ph, const QKeyEvent *e, int mirror, QString& after) const = 0;
117  };
118 
119  PlaceHolder() : length(0), autoRemove(false), affector(0) {}
120  PlaceHolder(const PlaceHolder& ph) : length(ph.length), autoRemove(ph.autoRemove), affector(ph.affector)
121  {
122  cursor = ph.cursor;
123  mirrors << ph.mirrors;
124  }
125 
126  int length;
127  bool autoRemove;
128  Affector *affector;
129  QDocumentCursor cursor;
130  QList<QDocumentCursor> mirrors;
131  };
132 
133  QEditor(QWidget *p = 0);
134  QEditor(bool actions, QWidget *p = 0);
135  QEditor(const QString& s, QWidget *p = 0);
136  QEditor(const QString& s, bool actions, QWidget *p = 0);
137  virtual ~QEditor();
138 
139  bool flag(EditFlag) const;
140 
141  bool canUndo() const;
142  bool canRedo() const;
143 
144  QString text() const;
145  QString text(int line) const;
146 
147  QTextCodec* codec() const;
148  QDocument* document() const;
149 
150  QList<QEditorInputBindingInterface*> inputBindings() const;
151 
152  bool isCursorVisible() const;
153  QDocumentCursor cursor() const;
154 
155  int cursorMirrorCount() const;
156  QDocumentCursor cursorMirror(int i) const;
157 
158  QLanguageDefinition* languageDefinition() const;
159  QCodeCompletionEngine* completionEngine() const;
160 
161  QAction* action(const QString& s);
162 
163  virtual QRect cursorRect() const;
164  virtual QRect selectionRect() const;
165  virtual QRect lineRect(int line) const;
166  virtual QRect lineRect(const QDocumentLine& l) const;
167  virtual QRect cursorRect(const QDocumentCursor& c) const;
168 
169  #ifndef _QMDI_
170  QString name() const;
171  QString fileName() const;
172 
173  bool isContentModified() const;
174  #endif
175 
176  bool isInConflict() const;
177 
178  int wrapWidth() const;
179 
180  inline int horizontalOffset() const
181  { return horizontalScrollBar()->isVisible() ? horizontalScrollBar()->value() : 0; }
182  inline int verticalOffset() const
183  { return verticalScrollBar()->isVisible() ? verticalScrollBar()->value() * m_doc->fontMetrics().lineSpacing() : 0; }
184 
185  inline QPoint mapToContents(const QPoint &point) const
186  {
187  return QPoint( point.x() + horizontalOffset(),
188  point.y() + verticalOffset() );
189  }
190 
191  inline QPoint mapFromContents(const QPoint &point) const
192  {
193  return QPoint( point.x() - horizontalOffset(),
194  point.y() - verticalOffset() );
195  }
196 
197  virtual bool protectedCursor(const QDocumentCursor& c) const;
198 
199  static int defaultFlags();
200  static void setDefaultFlags(int f);
201 
202  static QTextCodec* defaultCodec();
203  static void setDefaultCodec(int mib, int update);
204  static void setDefaultCodec(QTextCodec *c, int update);
205  static void setDefaultCodec(const char *name, int update);
206  static void setDefaultCodec(const QByteArray& name, int update);
207 
208  static QEditorInputBindingInterface* registeredInputBinding(const QString& n);
209  static QString defaultInputBindingId();
210  static QStringList registeredInputBindingIds();
211  static void registerInputBinding(QEditorInputBindingInterface *b);
212  static void unregisterInputBinding(QEditorInputBindingInterface *b);
213  static void setDefaultInputBinding(QEditorInputBindingInterface *b);
214  static void setDefaultInputBinding(const QString& b);
215 
216  static inline const QList<QEditor*>& editors() { return m_editors; }
217 
218  public slots:
219  void undo();
220  void redo();
221 
222  void cut();
223  void copy();
224  void paste();
225 
226  void selectAll();
227 
228  void find();
229  void findNext();
230  void replace();
231 
232  void gotoLine();
233 
234  void indentSelection();
235  void unindentSelection();
236 
237  void commentSelection();
238  void uncommentSelection();
239 
240  void setLineWrapping(bool on);
241 
242  virtual void save();
243  void save(const QString& filename);
244 
245  virtual void print();
246 
247  virtual void retranslate();
248 
249  virtual void write(const QString& s);
250 
251  void addAction(QAction *a, const QString& menu, const QString& toolbar = QString());
252  void removeAction(QAction *a, const QString& menu, const QString& toolbar = QString());
253 
254  void load(const QString& file);
255  void setText(const QString& s);
256 
257  void setCodec(int mib);
258  void setCodec(QTextCodec *c);
259  void setCodec(const char *name);
260  void setCodec(const QByteArray& name);
261 
262  void setDocument(QDocument *d);
263 
264  void addInputBinding(QEditorInputBindingInterface *b);
265  void removeInputBinding(QEditorInputBindingInterface *b);
266  void setInputBinding(QEditorInputBindingInterface *b);
267 
268  void setCursor(const QDocumentCursor& c);
269 
270  void setLanguageDefinition(QLanguageDefinition *d);
271 
272  void setCompletionEngine(QCodeCompletionEngine *e);
273 
274  void zoom(int n);
275 
276  void setPanelMargins(int l, int t, int r, int b);
277  void getPanelMargins(int *l, int *t, int *r, int *b) const;
278 
279  void setTitle(const QString& title);
280 
281  void highlight();
282 
283  void clearPlaceHolders();
284  void removePlaceHolder(int i);
285  void addPlaceHolder(const PlaceHolder& p, bool autoUpdate = true);
286 
287  int placeHolderCount() const;
288  int currentPlaceHolder() const;
289 
290  void nextPlaceHolder();
291  void previousPlaceHolder();
292  void setPlaceHolder(int i);
293 
294  virtual void setFileName(const QString& f);
295 
296  signals:
297  void loaded(QEditor *e, const QString& s);
298  void saved(QEditor *e, const QString& s);
299 
300  void contentModified(bool y);
301  void titleChanged(const QString& title);
302 
303  void textEdited(QKeyEvent *e);
304  void cursorPositionChanged();
305 
306  void copyAvailable(bool y);
307 
308  void undoAvailable(bool y);
309  void redoAvailable(bool y);
310 
311  void markChanged(const QString& f, QDocumentLineHandle *l, int mark, bool on);
312 
313  public slots:
314  void checkClipboard();
315  void reconnectWatcher();
316  void fileChanged(const QString& f);
317 
318  void setContentClean(bool y);
319 
320  void emitCursorPositionChanged();
321 
322  virtual void setContentModified(bool y);
323 
324  protected:
325  virtual bool event(QEvent *e);
326 
327  virtual void paintEvent(QPaintEvent *e);
328  virtual void timerEvent(QTimerEvent *e);
329 
330  virtual void keyPressEvent(QKeyEvent *e);
331 
332  virtual void inputMethodEvent(QInputMethodEvent* e);
333 
334  virtual void mouseMoveEvent(QMouseEvent *e);
335  virtual void mousePressEvent(QMouseEvent *e);
336  virtual void mouseReleaseEvent(QMouseEvent *e);
337  virtual void mouseDoubleClickEvent(QMouseEvent *e);
338 
339  virtual void dragEnterEvent(QDragEnterEvent *e);
340  virtual void dragLeaveEvent(QDragLeaveEvent *e);
341  virtual void dragMoveEvent(QDragMoveEvent *e);
342  virtual void dropEvent(QDropEvent *e);
343 
344  virtual void changeEvent(QEvent *e);
345  virtual void showEvent(QShowEvent *);
346  virtual void wheelEvent(QWheelEvent *e);
347  virtual void resizeEvent(QResizeEvent *e);
348  virtual void focusInEvent(QFocusEvent *e);
349  virtual void focusOutEvent(QFocusEvent *e);
350 
351  virtual void contextMenuEvent(QContextMenuEvent *e);
352 
353  virtual void closeEvent(QCloseEvent *e);
354 
355  virtual bool focusNextPrevChild(bool next);
356 
357  virtual bool moveKeyEvent(QDocumentCursor& c, QKeyEvent *e, bool *leave);
358  virtual bool isProcessingKeyEvent(QKeyEvent *e, int *offset = 0);
359  virtual bool processCursor(QDocumentCursor& c, QKeyEvent *e, bool& b);
360 
361  virtual void startDrag();
362  virtual QMimeData* createMimeDataFromSelection() const;
363  virtual void insertFromMimeData(const QMimeData *d);
364 
365  virtual void scrollContentsBy(int dx, int dy);
366 
367  // got to make it public for bindings
368  public:
369  void setFlag(EditFlag f, bool b);
370 
371  void pageUp(QDocumentCursor::MoveMode moveMode);
372  void pageDown(QDocumentCursor::MoveMode moveMode);
373 
374  void selectionChange(bool force = false);
375 
376  void repaintCursor();
377  void ensureCursorVisible();
378  void ensureVisible(int line);
379  void ensureVisible(const QRect &rect);
380 
381  void preInsert(QDocumentCursor& c, const QString& text);
382  void insertText(QDocumentCursor& c, const QString& text);
383 
384  QDocumentLine lineAtPosition(const QPoint& p) const;
385  QDocumentCursor cursorForPosition(const QPoint& p) const;
386 
387  void setClipboardSelection();
388  void setCursorPosition(const QPoint& p);
389 
390  void setCursorPosition(int line, int index);
391  void getCursorPosition(int &line, int &index);
392 
393  void clearCursorMirrors();
394  void addCursorMirror(const QDocumentCursor& c);
395 
396  protected slots:
397  void documentWidthChanged(int newWidth);
398  void documentHeightChanged(int newWidth);
399 
400  void repaintContent(int i, int n);
401  void updateContent (int i, int n);
402 
403  void markChanged(QDocumentLineHandle *l, int mark, bool on);
404 
405  void bindingSelected(QAction *a);
406 
407  void lineEndingSelected(QAction *a);
408  void lineEndingChanged(int lineEnding);
409 
410  protected:
411  enum SaveState
412  {
413  Undefined,
414  Saving,
415  Saved,
416  Conflict
417  };
418 
419  void init(bool actions = true);
420  void updateBindingsMenu();
421 
422  #ifndef _QMDI_
423  QString m_name, m_fileName;
424  #endif
425 
426  QMenu *pMenu;
427  QHash<QString, QAction*> m_actions;
428 
429  QMenu *m_lineEndingsMenu;
430  QActionGroup *m_lineEndingsActions;
431 
432  QMenu *m_bindingsMenu;
433  QAction *aDefaultBinding;
434  QActionGroup *m_bindingsActions;
435 
436  char m_saveState;
437  quint16 m_checksum;
438 
439  QDocument *m_doc;
440  QTextCodec *m_codec;
442 
443  QLanguageDefinition *m_definition;
444  QPointer<QCodeCompletionEngine> m_completionEngine;
445 
446  QDocumentCursor m_cursor, m_doubleClick, m_dragAndDrop;
447 
448  QList<QDocumentCursor> m_mirrors;
449 
450  int m_curPlaceHolder, m_cphOffset;
451  QList<PlaceHolder> m_placeHolders;
452 
453  int m_state;
454  bool m_selection;
455  QRect m_crect, m_margins;
456  QPoint m_clickPoint, m_dragPoint;
457  QBasicTimer m_blink, m_scroll, m_click, m_drag;
458 
459  static QReliableFileWatch* watcher();
460 
461  static int m_defaultFlags;
462  static QTextCodec *m_defaultCodec;
463 
464  static QList<QEditor*> m_editors;
465  static QEditorInputBindingInterface *m_defaultBinding;
466  static QHash<QString, QEditorInputBindingInterface*> m_registeredBindings;
467 };
468 
469 Q_DECLARE_OPERATORS_FOR_FLAGS(QEditor::State);
470 
471 #endif