[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
klflatexedit.h
Go to the documentation of this file.
1 /***************************************************************************
2  * file klflatexedit.h
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist at bluewin.ch
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 /* $Id: klflatexedit.h 953 2016-12-27 00:13:10Z phfaist $ */
23 
24 #ifndef KLFLATEXEDIT_H
25 #define KLFLATEXEDIT_H
26 
27 #include <klfdefs.h>
28 
29 #include <QObject>
30 #include <QTextEdit>
31 #include <QEvent>
32 #include <QContextMenuEvent>
33 #include <QMimeData>
34 #include <QSyntaxHighlighter>
35 #include <QTextCharFormat>
36 
37 
39 class KLFDropDataHandler;
40 
41 
42 // ------------------------------------------------
43 
44 
45 struct KLFLatexEditPrivate;
46 
51 class KLF_EXPORT KLFLatexEdit : public QTextEdit
52 {
53  Q_OBJECT
54 
55  Q_PROPERTY(int heightHintLines READ heightHintLines WRITE setHeightHintLines) ;
56  Q_PROPERTY(bool wrapLines READ wrapLines WRITE setWrapLines) ;
57 
58 public:
59  KLFLatexEdit(QWidget *parent);
60  virtual ~KLFLatexEdit();
61 
62  KLFLatexSyntaxHighlighter *syntaxHighlighter();
63 
70  void setDropDataHandler(KLFDropDataHandler *handler);
71 
74  int heightHintLines() const;
75 
80  virtual QSize sizeHint() const;
81 
82  QString latex() const;
83 
84  bool wrapLines() const;
85 
86 signals:
91  void insertContextMenuActions(const QPoint& pos, QList<QAction*> *actionList);
92 
93 public slots:
99  void setLatex(const QString& latex);
100  void clearLatex();
101 
105  void setWrapLines(bool wrap);
106 
108  void setHeightHintLines(int lines);
109 
112  void insertDelimiter(const QString& delim, int charsBack = 1);
113 
115  void setPalette(const QPalette& palette);
116 
117 protected:
118  virtual void contextMenuEvent(QContextMenuEvent *event);
119  virtual bool canInsertFromMimeData(const QMimeData *source) const;
120  virtual void insertFromMimeData(const QMimeData *source);
121 
122 private:
124 };
125 
126 
127 struct KLFLatexParenSpecsPrivate;
128 
130 {
131 public:
132  struct ParenSpec {
133  enum Flag { None = 0x00, IsLaTeXBrace = 0x01, AllowAlone = 0x02 };
134  ParenSpec(const QString& o, const QString& c, uint f = 0x00) : open(o), close(c), flags(f) { }
137  uint flags;
138  };
140  ParenModifierSpec(const QString& o, const QString& c) : openmod(o), closemod(c) { }
143  };
144 
145  // loads the default paren & paren modifier specs
147  // loads the given paren & paren modifier spec list
148  KLFLatexParenSpecs(const QList<ParenSpec>& parens, const QList<ParenModifierSpec>& modifiers);
149  // copy constructor
151  virtual ~KLFLatexParenSpecs();
152 
153  QList<ParenSpec> parenSpecList() const;
154  QList<ParenModifierSpec> parenModifierSpecList() const;
155 
156  QStringList openParenList() const;
157  QStringList closeParenList() const;
158  QStringList openParenModifiers() const;
159  QStringList closeParenModifiers() const;
160 
161  enum {
162  IdentifyFlagOpen = 0x01,
163  IdentifyFlagClose = 0x02,
164  IdentifyFlagOpenClose = IdentifyFlagOpen|IdentifyFlagClose
165  };
166 
171  int identifyParen(const QString& parenstr, uint identflags);
172 
177  int identifyModifier(const QString& modstr, uint identflags);
178 
179 private:
181 };
182 
183 
184 // ----------------------------------------------
185 
186 
188 {
189  Q_OBJECT
190 
191  Q_PROPERTY(bool highlightEnabled READ highlightEnabled WRITE setHighlightEnabled) ;
192  Q_PROPERTY(bool highlightParensOnly READ highlightParensOnly WRITE setHighlightParensOnly) ;
193  Q_PROPERTY(bool highlightLonelyParens READ highlightLonelyParens WRITE setHighlightLonelyParens) ;
194  Q_PROPERTY(QTextFormat fmtKeyword READ fmtKeyword WRITE setFmtKeyword) ;
195  Q_PROPERTY(QTextFormat fmtComment READ fmtComment WRITE setFmtComment) ;
196  Q_PROPERTY(QTextFormat fmtParenMatch READ fmtParenMatch WRITE setFmtParenMatch) ;
197  Q_PROPERTY(QTextFormat fmtParenMismatch READ fmtParenMismatch WRITE setFmtParenMismatch) ;
198  Q_PROPERTY(QTextFormat fmtLonelyParen READ fmtLonelyParen WRITE setFmtLonelyParen) ;
199 
200 public:
201  KLFLatexSyntaxHighlighter(QTextEdit *textedit, QObject *parent);
202  virtual ~KLFLatexSyntaxHighlighter();
203 
205  enum Type { Normal = 0, Keyword, Comment, Paren };
206  enum TypeMask { NoMask = 0,
207  KeywordMask = 1 << Keyword,
208  CommentMask = 1 << Comment,
209  ParenMask = 1 << Paren };
210  enum ParenMatch { None = 0, Matched, Mismatched, Lonely };
211 
212  ParsedBlock(Type t = Normal, int a = -1, int l = -1)
213  : type(t), pos(a), len(l), keyword(), parenmatch(None), parenisopening(false),
214  parenmodifier(), parenstr(), parenotherpos(-1)
215  { }
216 
218 
219  int pos;
220  int len;
221 
223 
230  bool parenIsLatexBrace() const;
231 
234  };
235 
236  QList<ParsedBlock> parsedContent() const { return pParsedBlocks; }
240  QList<ParsedBlock> parsedBlocksForPos(int pos, unsigned int filter_type = 0xffffffff) const;
241 
242  virtual void highlightBlock(const QString& text);
243 
244  bool highlightEnabled() const { return pConf.enabled; }
245  bool highlightParensOnly() const { return pConf.highlightParensOnly; }
246  bool highlightLonelyParens() const { return pConf.highlightLonelyParens; }
247  QTextCharFormat fmtKeyword() const { return pConf.fmtKeyword; }
248  QTextCharFormat fmtComment() const { return pConf.fmtComment; }
249  QTextCharFormat fmtParenMatch() const { return pConf.fmtParenMatch; }
250  QTextCharFormat fmtParenMismatch() const { return pConf.fmtParenMismatch; }
251  QTextCharFormat fmtLonelyParen() const { return pConf.fmtLonelyParen; }
252 
253 signals:
254  void newSymbolTyped(const QString& symbolName);
255 
256 public slots:
257  void setCaretPos(int position);
258 
259  void refreshAll();
260 
262  void resetEditing();
263 
264  void setHighlightEnabled(bool on);
265  void setHighlightParensOnly(bool on);
266  void setHighlightLonelyParens(bool on);
267  void setFmtKeyword(const QTextFormat& f);
268  void setFmtComment(const QTextFormat& f);
269  void setFmtParenMatch(const QTextFormat& f);
270  void setFmtParenMismatch(const QTextFormat& f);
271  void setFmtLonelyParen(const QTextFormat& f);
272 
273 private:
274 
275  QTextEdit *_textedit;
276 
277  int _caretpos;
278 
279  enum Format { FNormal = 0, FKeyWord, FComment, FParenMatch, FParenMismatch, FLonelyParen };
280 
281  struct FormatRule {
282  FormatRule(int ps = -1, int l = 0, Format f = FNormal, bool needsfocus = false)
283  : pos(ps), len(l), format(f), onlyIfFocus(needsfocus)
284  {
285  if (len < 0) {
286  len = -len; // len is now positive
287  pos -= len; // pos is now at beginning of the region
288  }
289  }
290  int pos;
291  int len;
292  int end() const { return pos + len; }
293  Format format;
294  bool onlyIfFocus;
295  };
296 
297  QList<FormatRule> _rulestoapply;
298 
299  QList<ParsedBlock> pParsedBlocks;
300 
301  void parseEverything();
302 
303  QTextCharFormat charfmtForFormat(Format f);
304 
307  QStringList pTypedSymbols;
308 
309  struct Conf {
310  bool enabled;
311  bool highlightParensOnly;
312  bool highlightLonelyParens;
313  QTextCharFormat fmtKeyword;
314  QTextCharFormat fmtComment;
315  QTextCharFormat fmtParenMatch;
316  QTextCharFormat fmtParenMismatch;
317  QTextCharFormat fmtLonelyParen;
318  };
319  Conf pConf;
320 };
321 
322 
324 
325 
326 
327 
328 #endif
QTextCharFormat fmtParenMatch() const
Definition: klflatexedit.h:249
static KLFLatexParenSpecs parenSpecs
Definition: klflatexedit.h:233
Base declarations for klatexformula and some utilities.
const char * type
Definition: klfdatautil.cpp:96
KLF_EXPORT QDebug operator<<(QDebug str, const KLFLatexSyntaxHighlighter::ParsedBlock &p)
bool highlightLonelyParens() const
Definition: klflatexedit.h:246
#define KLF_EXPORT
Definition: klfdefs.h:41
ParenSpec(const QString &o, const QString &c, uint f=0x00)
Definition: klflatexedit.h:134
bool highlightEnabled() const
Definition: klflatexedit.h:244
An abstract handler for when data is dropped.
Definition: klfguiutil.h:526
const char * format
Definition: klfdatautil.cpp:81
QTextCharFormat fmtLonelyParen() const
Definition: klflatexedit.h:251
#define KLF_DECLARE_PRIVATE(ClassName)
Definition: klfdefs.h:74
QTextCharFormat fmtParenMismatch() const
Definition: klflatexedit.h:250
A text edit field that edits latex code.
Definition: klflatexedit.h:51
QTextCharFormat fmtKeyword() const
Definition: klflatexedit.h:247
QTextCharFormat fmtComment() const
Definition: klflatexedit.h:248
QList< ParsedBlock > parsedContent() const
Definition: klflatexedit.h:236
ParenModifierSpec(const QString &o, const QString &c)
Definition: klflatexedit.h:140
ParsedBlock(Type t=Normal, int a=-1, int l=-1)
Definition: klflatexedit.h:212
bool highlightParensOnly() const
Definition: klflatexedit.h:245
const char * keyword

Generated by doxygen 1.8.13