[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project

src/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) 2010 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: klflatexedit.h 477 2010-09-08 16:30:49Z philippe $ */
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 KLFMainWin;
00040 
00041 
00042 // ------------------------------------------------
00043 
00044 
00045 
00046 
00051 class KLF_EXPORT KLFLatexEdit : public QTextEdit
00052 {
00053   Q_OBJECT
00054 
00055   Q_PROPERTY(int heightHintLines READ heightHintLines WRITE setHeightHintLines) ;
00056 public:
00057   KLFLatexEdit(QWidget *mainwin);
00058   virtual ~KLFLatexEdit();
00059 
00060   KLFLatexSyntaxHighlighter *syntaxHighlighter() { return mSyntaxHighlighter; }
00061 
00068   void setMainWinDataOpener(KLFMainWin *mainwin) { mMainWin = mainwin; }
00069 
00072   inline int heightHintLines() const { return pHeightHintLines; }
00073 
00078   virtual QSize sizeHint() const;
00079 
00080 
00081 signals:
00086   void insertContextMenuActions(const QPoint& pos, QList<QAction*> *actionList);
00087 
00088 public slots:
00094   void setLatex(const QString& latex);
00095   void clearLatex();
00096 
00098   void setHeightHintLines(int lines);
00099 
00102   void insertDelimiter(const QString& delim, int charsBack = 1);
00103 
00104 protected:
00105   virtual void contextMenuEvent(QContextMenuEvent *event);
00106   virtual bool canInsertFromMimeData(const QMimeData *source) const;
00107   virtual void insertFromMimeData(const QMimeData *source);
00108 
00109 private slots:
00110   void slotInsertFromActionSender();
00111 
00112 private:
00113   KLFLatexSyntaxHighlighter *mSyntaxHighlighter;
00114 
00116   KLFMainWin *mMainWin;
00117 
00118   int pHeightHintLines;
00119 };
00120 
00121 
00122 
00123 
00124 
00125 // ----------------------------------------------
00126 
00127 
00128 class KLF_EXPORT KLFLatexSyntaxHighlighter : public QSyntaxHighlighter
00129 {
00130   Q_OBJECT
00131 public:
00132   KLFLatexSyntaxHighlighter(QTextEdit *textedit, QObject *parent);
00133   virtual ~KLFLatexSyntaxHighlighter();
00134 
00135   void setCaretPos(int position);
00136 
00137   virtual void highlightBlock(const QString& text);
00138 
00139   enum { Enabled = 0x01,
00140          HighlightParensOnly = 0x02,
00141          HighlightLonelyParen = 0x04 };
00142 
00143 signals:
00144   void newSymbolTyped(const QString& symbolName);
00145 
00146 public slots:
00147   void refreshAll();
00148 
00150   void resetEditing();
00151 
00152 private:
00153 
00154   QTextEdit *_textedit;
00155 
00156   int _caretpos;
00157 
00158   enum Format { FNormal = 0, FKeyWord, FComment, FParenMatch, FParenMismatch, FLonelyParen };
00159 
00160   struct FormatRule {
00161     FormatRule(int ps = -1, int l = 0, Format f = FNormal, bool needsfocus = false)
00162       : pos(ps), len(l), format(f), onlyIfFocus(needsfocus) { }
00163     int pos;
00164     int len;
00165     int end() const { return pos + len; }
00166     Format format;
00167     bool onlyIfFocus;
00168   };
00169 
00170   struct ParenItem {
00171     ParenItem(int ps = -1, bool h = false, char c = 0, bool l = false)
00172       : pos(ps), highlight(h), ch(c), left(l) { }
00173     int pos;
00174     bool highlight;
00175     char ch;
00176     bool left; 
00177   };
00178 
00179   QList<FormatRule> _rulestoapply;
00180 
00181   void parseEverything();
00182 
00183   QTextCharFormat charfmtForFormat(Format f);
00184 
00187   QStringList pTypedSymbols;
00188 };
00189 
00190 
00191 
00192 
00193 #endif

Generated by doxygen 1.7.3