Debian Codegraph @

codeeditor.h

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation ([email protected])
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial Usage
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
35 **
36 ** If you have questions regarding the use of this file, please contact
37 ** Nokia at [email protected].
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef CODEEDITOR_H
43 #define CODEEDITOR_H
44
45 #include <QPlainTextEdit>
46 #include <QObject>
47 #include <QGroupBox>
48 #include <QLineEdit>
49 #include <QToolButton>
50 #include <QToolButton>
51 #include <QCheckBox>
52 #include <QGridLayout>
53 #include <QComboBox>
54 #include <QGraphicsScene>
55
56 #include "highlighter.h"
57 #include "multitextcursor.h"
58
59 #if defined Q_WS_WIN || defined Q_OS_WIN
60 #define FONTFAM "Lucida Console"
61 #elif defined Q_WS_MAC || defined Q_OS_MAC
62 #define FONTFAM "Monaco"
63 #else
64 #define FONTFAM "Droid Sans Mono"
65 #endif
66
67 QT_BEGIN_NAMESPACE
68 class QPaintEvent;
69 class QResizeEvent;
70 class QSize;
71 class QCompleter;
72 class QWidget;
73 class ChGL;
74 class PlayOpenGL;
75 QT_END_NAMESPACE
76
77 class LineNumberArea;
78 class AfixHighlightArea;
79
80 static inline bool isPrintableText(const QString &text) {
81 return !text.isEmpty() && (text.at(0).isPrint() || text.at(0) == QLatin1Char('\t'));
82 }
83
84
85 /*! \brief CodeEditor is the editor of shelXle featuring syntax higlighter, code completer, LineNumberArea, AfixHighlightArea and Part Higlighter.
86 *
87 *
88 */
89 class CodeEditor : public QPlainTextEdit {
90 Q_OBJECT
91
92 public:
93 CodeEditor(QWidget *parent = 0);
94 ~CodeEditor();
95 QList<int> sfac;//!<List of Scattering factors.
96 QStringList sfacsmbl;//!<List of Scattering factors pte symbols (needed as D has same atomic number as H)
97 QStringList easf;//!Electron Atom Scattering Factors (SFAC)
98 QStringList xasf;//!X-Ray Atom Scattering Factors (SFAC)
99 int sortierWeise;
100 int midCursorpos;
101 bool ListFile;
102 bool uCanSeeMe;
103 bool dark;
104 double smin;
105 double smax;
106 QString unitAlt, unitNeu;
107 //QSyntaxHighlighter *highlighter;//!< Highlighter is the syntax higlighter.
108 Highlighter *highlighter;//!< Highlighter is the syntax higlighter.
109 void setCompleter(QCompleter *c);//!< setCompleter sets the QCompleter for the editor.
110 QCompleter *completer() const;//!< completer is the QCompleter
111 QStringList blocks; //!< blocks is a QStringList containing the lines of the editor.
112 QList<int> errorInLine; //!< A list of line numbers with syntax errors.
113
114 void lineNumberAreaPaintEvent(QPaintEvent *event);//!< The paint event of LineNumberArea.
115 void afixHighlightAreaPaintEvent(QPaintEvent *event);//!< The paint event of AfixHighlightArea.
116 void hoverLineNumber(QMouseEvent *event);
117 int lineNumberAreaWidth();//!< calulates the width of the LineNumberArea.
118 void diffTo(QString org);//!< A simple line diff used for the SaveHistoryWidget. Changes back ground color of differering lines into orange. @param org The original file content.
119 QList<int> parenthesis;//!< List of lines. If the line in an afix environment the value is greater than zero.
120 QList<int> parenthesis2;//!< List of lines. The value is the current part number.
121 // QList<bool> parenthesis3;//!< List of lines. If it is OK to have nested afixes like in afix 66 it is true.
122 QList<bool> comment;//!< List of all lines true if they are comments
123 QList<double> fv;//!< List of 'Free Variables' in the SHELX res or ins file.
124 int fvMinLine;//!< fvMinLine specifies the line number of the FVAR istruction.
125 QGroupBox *suchbox;
126 QLineEdit *searchLE, *replace;
127 QToolButton *hidesearch;
128 QToolButton *prev,*next,*replaceButton,*replacePButton,*replaceAButton;
129 QCheckBox *wholeLine;
130 QGridLayout *slt;
131 QComboBox *resiFinder;
132 QString dispFromWave(int an);
133 QString sortLabelList(CEnvironment &asymm, MyAtom &tatze);
134 void searchchanged(bool current,bool back);
135 //void searchchanged2(bool current,bool back);
136 MultiTextCursor multiTextCursor() const;
137 void setMultiTextCursor(const MultiTextCursor &cursor);
138 QRect cursorUpdateRect(const MultiTextCursor &cursor);
139 void doSetTextCursor1(const QTextCursor &cursor);
140 void doSetTextCursor(const QTextCursor &cursor, bool keepMultiSelection);
141 //void setExtraSelections(Utils::Id kind, const QList<QTextEdit::ExtraSelection> &selections);
142 // QHash<Utils::Id, QList<QTextEdit::ExtraSelection>> m_extraSelections;
143
144 void startCursorFlashTimer();
145 void resetCursorFlashTimer();
146 QBasicTimer m_cursorFlashTimer;
147 bool m_cursorVisible;
148 //bool m_moveLineUndoHack = false;
149 void updateCursorSelections();
150 void moveCursor(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor);
151 bool cursorMoveKeyEvent(QKeyEvent *e);
152 void handleHomeKey(bool anchor, bool block);
153 void handleBackspaceKey();
154 struct BlockSelection{
155 int blockNumber;
156 int column;
157 int anchorBlockNumber;
158 int anchorColumn;
159 };
160 QList<BlockSelection> m_blockSelections;
161 QList<QTextCursor> generateCursorsForBlockSelection(const BlockSelection &blockSelection);
162 void initBlockSelection();
163 void clearBlockSelection();
164 void handleMoveBlockSelection(QTextCursor::MoveOperation op);
165 struct CursorData
166 {
167 QTextLayout *layout;
168 QPointF offset;
169 int pos;
170 QPen pen;
171 };
172
173 struct PaintEventData{
174 PaintEventData(CodeEditor *editor, QPaintEvent *event, QPointF offset)
175 : offset(offset)
176 , viewportRect(editor->viewport()->rect())
177 , eventRect(event->rect())
178 , doc(editor->document())
179 , documentLayout(qobject_cast<QPlainTextDocumentLayout *>(doc->documentLayout()))
180 , documentWidth(int(doc->size().width()))
181 , textCursor(editor->textCursor())
182 , textCursorBlock(textCursor.block())
183 , isEditable(!editor->isReadOnly())
184 { }
185 QPointF offset;
186 const QRect viewportRect;
187 const QRect eventRect;
188 qreal rightMargin;
189 const QTextDocument *doc;
190 QPlainTextDocumentLayout *documentLayout;
191 const int documentWidth;
192 const QTextCursor textCursor;
193 const QTextBlock textCursorBlock;
194 const bool isEditable;
195 const QTextCharFormat searchScopeFormat;
196 const QTextCharFormat searchResultFormat;
197 const QTextCharFormat visualWhitespaceFormat;
198 const QTextCharFormat ifdefedOutFormat;
199 QAbstractTextDocumentLayout::PaintContext context;
200 QTextBlock visibleCollapsedBlock;
201 QPointF visibleCollapsedBlockOffset;
202 QTextBlock block;
203 QList<CursorData> cursors;
204 };//PaintEventData
205 struct PaintEventBlockData{
206 QRectF boundingRect;
207 QVector<QTextLayout::FormatRange> selections;
208 QTextLayout *layout;
209 int position;
210 int length;
211 };
212 void setupBlockLayout(const PaintEventData &data, QPainter &painter, PaintEventBlockData &blockData) const;
213 void setupSelections(const PaintEventData &data, PaintEventBlockData &blockData) const;
214 void addCursorsPosition(PaintEventData &data, QPainter &painter, const PaintEventBlockData &blockData) const;
215 void paintCursor(const PaintEventData &data, QPainter &painter) const;
216 static bool blockContainsCursor(const PaintEventBlockData &blockData, const QTextCursor &cursor);
217 static CursorData generateCursorData(const int cursorPos,
218 const PaintEventData &data,
219 const PaintEventBlockData &blockData,
220 QPainter &painter);
221
222 signals:
223 void findInStructure(const QString &);//!< findInStructure is emitted by a context menu to select and center an atom in the OpenGL representation of the structure.
224 void openIncludeFile(const QString &);
225 void electInStructure(const QString &);//!< electInStructure is emitted by a context menu to select atoms found in the editor text selection in the OpenGL representation.
226 void deleteSelected();//!< deleteSelected is emitted by a context menu to select and delete atoms found in the editor text selection in the OpenGL representation.
227 void saveMe(bool spell, bool loadafter);
228 void updateLabel();//!< the rename mode is updated
229 protected:
230 void resizeEvent(QResizeEvent *event);
231 bool viewportEvent(QEvent *event);
232 void keyPressEvent(QKeyEvent *e);
233 void mouseMoveEvent(QMouseEvent *);
234 void mousePressEvent(QMouseEvent *);
235 void timerEvent(QTimerEvent *);
236 void focusInEvent(QFocusEvent *e);
237 void contextMenuEvent(QContextMenuEvent *event);
238 void focusOutEvent(QFocusEvent *e);
239 void paintEvent(QPaintEvent *);
240
241 public slots:
242 void testCpd();
243 void mpaste();
244 void mcut();
245 void plotSFAC();
246 void slotCursorPositionChanged();
247 void updateCursorPosition();
248 void handleCBstates(int state);
249 void vis(bool b){ uCanSeeMe=b; }
250 void fitWidth();
251 void restoreMinimumWidth();
252 void updateLineNumberAreaWidth(int newBlockCount);//!< updateLineNumberAreaWidth sets the new width of LineNumberArea for the given amount of lines newBlockCount. @param newBlockCount number of lines in the editor.
253 void insertAnis();//!< inserts ANIS instruction into the file.
254 void insertActa();//!< inserts ACTA instruction into the file.
255 void insertCONF();//!< inserts CONF instruction into the file.
256 void insertList4();//!< inserts a LIST 4 instruction into the file.
257 void insertList6();//!< inserts a LIST 6 instruction into the file.
258 void insertBind();//!< inserts a BIND instruction into the file.
259 void insertEXTI();//!< inserts a EXTI instruction into the file.
260 void insertFree();//!< inserts a FREE instruction into the file.
261 void insertElectronSFACs();
262 void insertXRaySFACs();
263 void findResi(int);//!<Searches a Residue via a string like "RESI 22 ALA" or "RESI ALA 22" in the editor and selects all atoms with the same residue number (eg: 22).
264 void findText();//!<Find functionality of search and replace in the editor.
265 void hideSearch();//!< Hides the search and replace widget for the editor.
266 void replaceAll();//!<replace all functionality of search and replace in the editor.
267 void replaceNext();//!<replace next functionality of search and replace in the editor.
268 void replacePrev();//!<replace previous functionality of search and replace in the editor.
269 void showSearch();//!< shows the search and repleace for the editor.
270 void changeSortierWeise(QAction* action);//!<Changes the sorting option to action->data. @param action the calling QAction.
271 void sortAtoms();//!< sorts atoms in file acording to the sort option.
272 void sortAtoms(CEnvironment &as);//!< sorts atoms in file acording to the sort option.
273 void sortSelectedRegion();//!< sorts all atoms found in selected text in the structure @params info seleted text.
274 void expandGreaterThan();//!< some SHELXL re- or constrainst use > to specify a range of atoms this is a problem for sorting atoms
275 void findNext();//!<Find next (F3) functionality of search and replace in the editor.
276 void findPrev();//!<Find previous functionality of search and replace in the editor.
277 void weedEmptySfacs();//!< weeds out unused scattering factors
278 void updateUnit();//!< the unit instruction gets the true amount of atoms per type
279 void dispFromWave();//!<calculates f', f" and mu for the given wavelength and inserts DISP lines for each SFAC entry.
280 void resiFinderDestroyed();
281 void decreaseEdtiorFont();//!<Decreases the editor font by one point.
282 void increaseEdtiorFont();//!< Increases the editor font by one point.
283 void jumpToError();//!< the cursor points to the first syntax error in the file.a
284 void jumpToAtom(int index);//!< if rename mode is visible the atom at index gets renamed if not the cursor of the editor is pointed to the specified atom.
285 void omitsome(const QString &s);//!< insetrts a 'OMIT h k l\n' string in the res file.
286 void insertDFIX(double value, double esd, QList<MyAtom> selected, QString resiSpec=""); //!< Insert DFIX restraint into ins file
287 void insertDANG(double value, double esd, QList<MyAtom> selected, QString resiSpec=""); //!< Insert DANG restraint into ins file
288 void insertFLAT(double esd, QList<MyAtom> selected, QString resiSpec=""); //!< Insert FLAT restraint into ins file
289 void insertEXYZ(QList<MyAtom> selected, QString resiSpec=""); //!< Insert EXYZ restraint into ins file
290 void insertEADP(QList<MyAtom> selected, QString resiSpec=""); //!< Insert EADP restraint into ins file
291 void insertDELU(double esd1, double esd2, QList<MyAtom> selected, QString resiSpec=""); //!< Insert DELU restraint into ins file
292 void insertSIMU(double esd1, double esd2, double dmax, QList<MyAtom> selected, QString resiSpec=""); //!< Insert SIMU restraint into ins file
293 void insertISOR(double esd1, double esd2, QList<MyAtom> selected, QString resiSpec=""); //!< Insert ISOR restraint into ins file
294 void insertRIGU(double esd1, double esd2, QList<MyAtom> selected, QString resiSpec=""); //!< Insert RIGU restraint into ins file
295 void insertCHIV(double vol, double esd1, QList<MyAtom> selected, QString resiSpec=""); //!< Insert CHIV restraint into ins file
296 void insertANIS(QList<MyAtom> selected); //!< Insert ANIS for selected atoms
297 void updateLineNumberArea(const QRect &, int);//!< updateLineNumberArea in the curent viewport.
298 void updateAfix();//!< updateAfix updates the parenthesis list for the AfixHighlightArea.
299 void updateWght();//!< the WGHT instruction is updated from the back of the res file
300 void insertCompletion(const QString &completion);//!< insertCompletion inserts the text selected in the completer. @param completion selected text of the completer.
301 void searchInStructure();//!< Select and center an atom in the OpenGL representation of the structure.
302 void openAnIncludeFile();
303 void selectInStructure();//!< Select atoms found in the editor text selection in the OpenGL representation.
304 void lineNumberToggled(QMouseEvent *event);//!< when the LineNumberArea is clicked the comment state of the secified line is toggled.
305 void remark(int line);//!< toggles the comment state of the secified line. @param line The line number in file
306 void toggleRemarks();//!< toggleRemarks toggles the comment state of the selected lines.
307 /*
308 void setgl(ChGL *g){
309 chgl=g;
310 }//!< setgl passes the ChGL widget to the CodeEditor
311 // */
312 //*
313 void setgl(PlayOpenGL *g){
314 chgl=g;
315 }
316 // */
317
318 private:
319 QString selectedRestraintsAtoms(QString buffer, QList<MyAtom> selected, QString resiSpec, bool exclude_H=false);
320 QString textUnderCursor() const;
321 QWidget *lineNumberArea;
322 QCompleter *c;
323 QWidget *afixHighlightArea;
324 //ChGL *chgl;
325 PlayOpenGL *chgl;
326 bool cbhandled=true;
327 MultiTextCursor m_cursors;
328 bool startMouseMoveCursor1;
329 double cromerMann(double sintl, double a1, double b1, double a2, double b2, double a3, double b3, double a4, double b4, double c);
330 const QRegularExpression spaces = QRegularExpression("\\s+");
331 const QRegularExpression comments = QRegularExpression("^REM",QRegularExpression::CaseInsensitiveOption);
332 const QRegularExpression onlydigits = QRegularExpression("^[0-9]+$");
333 const QRegularExpression nodigits = QRegularExpression("\\D");
334 const QRegularExpression digits = QRegularExpression("\\d+");
335 const QRegularExpression plusfloat = QRegularExpression("[.0-9]+");
336 const QRegularExpression grossbuch = QRegularExpression("^[A-Z]");
337 const QRegularExpression onlysmall = QRegularExpression("^[a-z]$");
338 const QRegularExpression linecontinue = QRegularExpression("^[0-9]+");
339 const QRegularExpression listinst = QRegularExpression("\nLIST",QRegularExpression::CaseInsensitiveOption);
340 const QRegularExpression unitinst = QRegularExpression("^UNIT",QRegularExpression::CaseInsensitiveOption);
341 const QRegularExpression actainst = QRegularExpression("^ACTA",QRegularExpression::CaseInsensitiveOption);
342 const QRegularExpression sfacinst = QRegularExpression("^SFAC",QRegularExpression::CaseInsensitiveOption);
343 const QRegularExpression hklfinst = QRegularExpression("^HKLF",QRegularExpression::CaseInsensitiveOption);
344 const QRegularExpression extiinst = QRegularExpression("^EXTI",QRegularExpression::CaseInsensitiveOption);
345 const QRegularExpression confinst = QRegularExpression("^CONF",QRegularExpression::CaseInsensitiveOption);
346 const QRegularExpression omithklinst = QRegularExpression("^OMIT\\s+\\d+\\s+\\d+\\s+\\d+");
347 const QRegularExpression afix0inst = QRegularExpression("^AFIX\\s+0",QRegularExpression::CaseInsensitiveOption);
348 const QRegularExpression afixinst = QRegularExpression("^AFIX\\s+\\d+",QRegularExpression::CaseInsensitiveOption);
349 const QRegularExpression part0inst = QRegularExpression("^PART\\s+0",QRegularExpression::CaseInsensitiveOption);
350 const QRegularExpression partinst = QRegularExpression("^PART\\s+[-0-9]{1,}",QRegularExpression::CaseInsensitiveOption);
351 const QRegularExpression startsmall = QRegularExpression("^[a-z]+");
352 };
353
354 /////////////////////////////////////////////////
355 /*! \brief AfixHighlightArea is a colored bar right of the LineNumberArea to indicate if the line is in a AFIX environment.
356 *
357 * This is just a dummy class to reimplement the paintEvent which is impemented in CodeEditor.
358 *
359 */
360 class AfixHighlightArea : public QWidget{
361 public:
362 AfixHighlightArea(CodeEditor *editor) : QWidget(editor){
363 codeEditor = editor;
364 }
365 QSize sizeHint() const {
366 return QSize(5,0);
367 }
368 protected:
369 void paintEvent(QPaintEvent *event){
370 codeEditor->afixHighlightAreaPaintEvent(event);
371 }//!< calls afixHighlightAreaPaintEvent().
372 void mouseMoveEvent(QMouseEvent *event){
373 codeEditor->hoverLineNumber(event);
374 }
375 private:
376 CodeEditor *codeEditor;
377 };
378 /////////////////////////////////////////////////
379 /*! \brief LineNumberArea is a area with line numbers left of the CodeEditor.
380 *
381 * This is just a dummy class to reimplement the paintEvent and mousePressEvent which are impemented in CodeEditor.
382 */
383 class LineNumberArea : public QWidget{
384 public:
385 LineNumberArea(CodeEditor *editor) : QWidget(editor) {
386 codeEditor = editor;
387 }
388
389 QSize sizeHint() const {
390 return QSize(codeEditor->lineNumberAreaWidth(), 0);
391 }
392
393 protected:
394 void paintEvent(QPaintEvent *event) {
395 codeEditor->lineNumberAreaPaintEvent(event);
396 }//!< calls lineNumberAreaPaintEvent().
397 void mousePressEvent ( QMouseEvent * event ){
398 codeEditor->lineNumberToggled(event);
399 }//!< calls lineNumberToggled().
400 void mouseMoveEvent(QMouseEvent *event){
401 codeEditor->hoverLineNumber(event);
402 }
403
404 private:
405 CodeEditor *codeEditor;
406 };
407
408 class CheckBox : public QCheckBox{
409 public:
410 CheckBox(QGraphicsScene * scene, const QString & text, QWidget * parent = 0);
411 protected:
412 void hoverEnter(QHoverEvent *event);
413 void hoverLeave(QHoverEvent *event);
414 void hoverMove(QHoverEvent *event);
415 bool event(QEvent *event){
416 switch (event->type()){
417 case QEvent::HoverEnter:
418 hoverEnter(static_cast<QHoverEvent*>(event));
419 return true;
420 break;
421 case QEvent::HoverLeave:
422 hoverLeave(static_cast<QHoverEvent*>(event));
423 return true;
424 break;
425 case QEvent::HoverMove:
426 hoverMove(static_cast<QHoverEvent*>(event));
427 return true;
428 break;
429 default:
430 break;
431 }
432 return QWidget::event(event);
433 }
434 private:
435 QGraphicsScene *scene;
436 };
437
438 #endif