[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klflatexedit.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klflatexedit.h
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
00023 
00024 #ifndef KLFLATEXEDIT_H
00025 #define KLFLATEXEDIT_H
00026 
00027 #include <klfdefs.h>
00028 
00029 #include <QObject>
00030 #include <QTextEdit>
00031 #include <QEvent>
00032 #include <QContextMenuEvent>
00033 #include <QMimeData>
00034 #include <QSyntaxHighlighter>
00035 #include <QTextCharFormat>
00036 
00037 
00038 class KLFLatexSyntaxHighlighter;
00039 class KLFDropDataHandler;
00040 
00041 
00042 // ------------------------------------------------
00043 
00044 
00045 struct KLFLatexEditPrivate;
00046 
00051 class KLF_EXPORT KLFLatexEdit : public QTextEdit
00052 {
00053   Q_OBJECT
00054 
00055   Q_PROPERTY(int heightHintLines READ heightHintLines WRITE setHeightHintLines) ;
00056   Q_PROPERTY(bool wrapLines READ wrapLines WRITE setWrapLines) ;
00057 
00058 public:
00059   KLFLatexEdit(QWidget *parent);
00060   virtual ~KLFLatexEdit();
00061 
00062   KLFLatexSyntaxHighlighter *syntaxHighlighter();
00063 
00070   void setDropDataHandler(KLFDropDataHandler *handler);
00071 
00074   int heightHintLines() const;
00075 
00080   virtual QSize sizeHint() const;
00081 
00082   QString latex() const;
00083 
00084   bool wrapLines() const;
00085 
00086 signals:
00091   void insertContextMenuActions(const QPoint& pos, QList<QAction*> *actionList);
00092 
00093 public slots:
00099   void setLatex(const QString& latex);
00100   void clearLatex();
00101 
00105   void setWrapLines(bool wrap);
00106 
00108   void setHeightHintLines(int lines);
00109 
00112   void insertDelimiter(const QString& delim, int charsBack = 1);
00113 
00115   void setPalette(const QPalette& palette);
00116 
00117 protected:
00118   virtual void contextMenuEvent(QContextMenuEvent *event);
00119   virtual bool canInsertFromMimeData(const QMimeData *source) const;
00120   virtual void insertFromMimeData(const QMimeData *source);
00121 
00122 private:
00123   KLF_DECLARE_PRIVATE(KLFLatexEdit) ;
00124 };
00125 
00126 
00127 struct KLFLatexParenSpecsPrivate;
00128 
00129 class KLF_EXPORT KLFLatexParenSpecs
00130 {
00131 public:
00132   struct ParenSpec {
00133     enum Flag { None = 0x00, IsLaTeXBrace = 0x01, AllowAlone = 0x02 };
00134     ParenSpec(const QString& o, const QString& c, uint f = 0x00) : open(o), close(c), flags(f) { }
00135     QString open;
00136     QString close;
00137     uint flags;
00138   };
00139   struct ParenModifierSpec {
00140     ParenModifierSpec(const QString& o, const QString& c) : openmod(o), closemod(c) { }
00141     QString openmod;
00142     QString closemod;
00143   };
00144 
00145   // loads the default paren & paren modifier specs
00146   KLFLatexParenSpecs();
00147   // loads the given paren & paren modifier spec list
00148   KLFLatexParenSpecs(const QList<ParenSpec>& parens, const QList<ParenModifierSpec>& modifiers);
00149   // copy constructor
00150   KLFLatexParenSpecs(const KLFLatexParenSpecs& other);
00151   virtual ~KLFLatexParenSpecs();
00152 
00153   QList<ParenSpec> parenSpecList() const;
00154   QList<ParenModifierSpec> parenModifierSpecList() const;
00155 
00156   QStringList openParenList() const;
00157   QStringList closeParenList() const;
00158   QStringList openParenModifiers() const;
00159   QStringList closeParenModifiers() const;
00160 
00161   enum {
00162     IdentifyFlagOpen = 0x01, 
00163     IdentifyFlagClose = 0x02, 
00164     IdentifyFlagOpenClose = IdentifyFlagOpen|IdentifyFlagClose 
00165   };
00166 
00171   int identifyParen(const QString& parenstr, uint identflags);
00172 
00177   int identifyModifier(const QString& modstr, uint identflags);
00178 
00179 private:
00180   KLF_DECLARE_PRIVATE(KLFLatexParenSpecs) ;
00181 };
00182 
00183 
00184 // ----------------------------------------------
00185 
00186 
00187 class KLF_EXPORT KLFLatexSyntaxHighlighter : public QSyntaxHighlighter
00188 {
00189   Q_OBJECT
00190 
00191   Q_PROPERTY(bool highlightEnabled READ highlightEnabled WRITE setHighlightEnabled) ;
00192   Q_PROPERTY(bool highlightParensOnly READ highlightParensOnly WRITE setHighlightParensOnly) ;
00193   Q_PROPERTY(bool highlightLonelyParens READ highlightLonelyParens WRITE setHighlightLonelyParens) ;
00194   Q_PROPERTY(QTextFormat fmtKeyword READ fmtKeyword WRITE setFmtKeyword) ;
00195   Q_PROPERTY(QTextFormat fmtComment READ fmtComment WRITE setFmtComment) ;
00196   Q_PROPERTY(QTextFormat fmtParenMatch READ fmtParenMatch WRITE setFmtParenMatch) ;
00197   Q_PROPERTY(QTextFormat fmtParenMismatch READ fmtParenMismatch WRITE setFmtParenMismatch) ;
00198   Q_PROPERTY(QTextFormat fmtLonelyParen READ fmtLonelyParen WRITE setFmtLonelyParen) ;
00199 
00200 public:
00201   KLFLatexSyntaxHighlighter(QTextEdit *textedit, QObject *parent);
00202   virtual ~KLFLatexSyntaxHighlighter();
00203 
00204   struct KLF_EXPORT ParsedBlock {
00205     enum Type { Normal = 0, Keyword, Comment, Paren };
00206     enum TypeMask { NoMask = 0,
00207                     KeywordMask = 1 << Keyword,
00208                     CommentMask = 1 << Comment,
00209                     ParenMask = 1 << Paren };
00210     enum ParenMatch { None = 0, Matched, Mismatched, Lonely };
00211 
00212     ParsedBlock(Type t = Normal, int a = -1, int l = -1)
00213       :  type(t), pos(a), len(l), keyword(), parenmatch(None), parenisopening(false),
00214          parenmodifier(), parenstr(), parenotherpos(-1)
00215     { }
00216     
00217     Type type;
00218 
00219     int pos;
00220     int len;
00221 
00222     QString keyword;
00223 
00224     ParenMatch parenmatch;
00225     bool parenisopening;
00226     int parenSpecIndex;
00227     QString parenmodifier;
00228     QString parenstr;
00229     int parenotherpos;
00230     bool parenIsLatexBrace() const;
00231 
00233     static KLFLatexParenSpecs parenSpecs;
00234   };
00235 
00236   QList<ParsedBlock> parsedContent() const { return pParsedBlocks; }
00240   QList<ParsedBlock> parsedBlocksForPos(int pos, unsigned int filter_type = 0xffffffff) const;
00241 
00242   virtual void highlightBlock(const QString& text);
00243 
00244   bool highlightEnabled() const { return pConf.enabled; }
00245   bool highlightParensOnly() const { return pConf.highlightParensOnly; }
00246   bool highlightLonelyParens() const { return pConf.highlightLonelyParens; }
00247   QTextCharFormat fmtKeyword() const { return pConf.fmtKeyword; }
00248   QTextCharFormat fmtComment() const { return pConf.fmtComment; }
00249   QTextCharFormat fmtParenMatch() const { return pConf.fmtParenMatch; }
00250   QTextCharFormat fmtParenMismatch() const { return pConf.fmtParenMismatch; }
00251   QTextCharFormat fmtLonelyParen() const { return pConf.fmtLonelyParen; }
00252 
00253 signals:
00254   void newSymbolTyped(const QString& symbolName);
00255 
00256 public slots:
00257   void setCaretPos(int position);
00258 
00259   void refreshAll();
00260 
00262   void resetEditing();
00263 
00264   void setHighlightEnabled(bool on);
00265   void setHighlightParensOnly(bool on);
00266   void setHighlightLonelyParens(bool on);
00267   void setFmtKeyword(const QTextFormat& f);
00268   void setFmtComment(const QTextFormat& f);
00269   void setFmtParenMatch(const QTextFormat& f);
00270   void setFmtParenMismatch(const QTextFormat& f);
00271   void setFmtLonelyParen(const QTextFormat& f);
00272 
00273 private:
00274 
00275   QTextEdit *_textedit;
00276 
00277   int _caretpos;
00278 
00279   enum Format { FNormal = 0, FKeyWord, FComment, FParenMatch, FParenMismatch, FLonelyParen };
00280 
00281   struct FormatRule {
00282     FormatRule(int ps = -1, int l = 0, Format f = FNormal, bool needsfocus = false)
00283       : pos(ps), len(l), format(f), onlyIfFocus(needsfocus)
00284     {
00285       if (len < 0) {
00286         len = -len; // len is now positive
00287         pos -= len; // pos is now at beginning of the region
00288       }
00289     }
00290     int pos;
00291     int len;
00292     int end() const { return pos + len; }
00293     Format format;
00294     bool onlyIfFocus;
00295   };
00296 
00297   QList<FormatRule> _rulestoapply;
00298 
00299   QList<ParsedBlock> pParsedBlocks;
00300 
00301   void parseEverything();
00302 
00303   QTextCharFormat charfmtForFormat(Format f);
00304 
00307   QStringList pTypedSymbols;
00308 
00309   struct Conf {
00310     bool enabled;
00311     bool highlightParensOnly;
00312     bool highlightLonelyParens;
00313     QTextCharFormat fmtKeyword;
00314     QTextCharFormat fmtComment;
00315     QTextCharFormat fmtParenMatch;
00316     QTextCharFormat fmtParenMismatch;
00317     QTextCharFormat fmtLonelyParen;
00318   };
00319   Conf pConf;
00320 };
00321 
00322 
00323 KLF_EXPORT QDebug operator<<(QDebug str, const KLFLatexSyntaxHighlighter::ParsedBlock& p);
00324 
00325 
00326 
00327 
00328 #endif

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net