[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$ */
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
40
41
42// ------------------------------------------------
43
44
45struct KLFLatexEditPrivate;
46
51class 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
58public:
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
86signals:
91 void insertContextMenuActions(const QPoint& pos, QList<QAction*> *actionList);
92
93public 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
117protected:
118 virtual void contextMenuEvent(QContextMenuEvent *event);
119 virtual bool canInsertFromMimeData(const QMimeData *source) const;
120 virtual void insertFromMimeData(const QMimeData *source);
121
122private:
124};
125
126
127struct KLFLatexParenSpecsPrivate;
128
130{
131public:
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
179private:
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
200public:
201 KLFLatexSyntaxHighlighter(QTextEdit *textedit, QObject *parent);
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
253signals:
254 void newSymbolTyped(const QString& symbolName);
255
256public 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
273private:
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
An abstract handler for when data is dropped.
Definition klfguiutil.h:527
A text edit field that edits latex code.
void insertContextMenuActions(const QPoint &pos, QList< QAction * > *actionList)
QTextCharFormat fmtLonelyParen() const
bool highlightLonelyParens() const
QTextCharFormat fmtParenMatch() const
void newSymbolTyped(const QString &symbolName)
bool highlightParensOnly() const
QList< ParsedBlock > parsedContent() const
QTextCharFormat fmtParenMismatch() const
QTextCharFormat fmtComment() const
QTextCharFormat fmtKeyword() const
const char * keyword
const char * format
const char * type
Base declarations for klatexformula and some utilities.
#define KLF_DECLARE_PRIVATE(ClassName)
Definition klfdefs.h:74
#define KLF_EXPORT
Definition klfdefs.h:41
KLF_EXPORT QDebug operator<<(QDebug str, const KLFLatexSyntaxHighlighter::ParsedBlock &p)
ParenModifierSpec(const QString &o, const QString &c)
ParenSpec(const QString &o, const QString &c, uint f=0x00)
static KLFLatexParenSpecs parenSpecs
ParsedBlock(Type t=Normal, int a=-1, int l=-1)

Generated by doxygen 1.12.0