1 /****************************************************************************
2  **
3  ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4  ** All rights reserved.
5  ** Contact: Nokia Corporation (qt-info@nokia.com)
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 qt-info@nokia.com.
38  ** $QT_END_LICENSE$
39  **
40  ****************************************************************************/
41 
42 #ifndef CODEEDITOR_H
43 #define CODEEDITOR_H
44 
45 #include <QPlainTextEdit>
46 #include <QObject>
47 #include "highlighter.h"
48 
49 #if defined Q_WS_WIN || defined Q_OS_WIN
50 #define FONTFAM "Lucida Console"
51 #elif defined Q_WS_MAC || defined Q_OS_MAC
52 #define FONTFAM "Monaco"
53 #else
54 #define FONTFAM "Droid Sans Mono"
55 #endif
56 
57 QT_BEGIN_NAMESPACE
58 class QPaintEvent;
59 class QResizeEvent;
60 class QSize;
61 class QCompleter;
62 class QWidget;
63 class ChGL;
64 QT_END_NAMESPACE
65 
66 class LineNumberArea;
67 class AfixHighlightArea;
68 
69 /*! \brief CodeEditor is the editor of shelXle featuring syntax higlighter, code completer, LineNumberArea, AfixHighlightArea and Part Higlighter.
70  *
71  *
72  */
73 class CodeEditor : public QPlainTextEdit {
74   Q_OBJECT
75 
76   public:
77     CodeEditor(QWidget *parent = 0);
78     ~CodeEditor();
79     QList<int> sfac;//!<List of Scattering factors.
80     int sortierWeise;
81     int midCursorpos;
82     bool ListFile;
83     bool uCanSeeMe;
84     bool dark;
85     QString unitAlt, unitNeu;
86     //QSyntaxHighlighter *highlighter;//!< Highlighter is the syntax higlighter.
87     Highlighter *highlighter;//!< Highlighter is the syntax higlighter.
88     void setCompleter(QCompleter *c);//!< setCompleter sets the QCompleter for the editor.
89     QCompleter *completer() const;//!< completer is the QCompleter
90     QStringList blocks; //!< blocks is a QStringList containing the lines of the editor.
91     QList<int> errorInLine; //!< A list of line numbers with syntax errors.
92 
93     void lineNumberAreaPaintEvent(QPaintEvent *event);//!< The paint event of LineNumberArea.
94     void afixHighlightAreaPaintEvent(QPaintEvent *event);//!< The paint event of AfixHighlightArea.
95     void hoverLineNumber(QMouseEvent *event);
96     int lineNumberAreaWidth();//!< calulates the width of the LineNumberArea.
97     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.
98     QList<int> parenthesis;//!< List of lines.  If the line in an afix environment the value is greater than zero.
99     QList<int> parenthesis2;//!< List of lines. The value is the current part number.
100     //    QList<bool> parenthesis3;//!< List of lines. If it is OK to have nested afixes like in afix 66 it is true.
101     QList<bool> comment;//!< List of all lines true if they are comments
102     QList<double> fv;//!< List of 'Free Variables' in the SHELX res or ins file.
103     int fvMinLine;//!< fvMinLine specifies the line number of the FVAR istruction.
104     QGroupBox  *suchbox;
105     QLineEdit *searchLE, *replace;
106     QToolButton *hidesearch;
107     QToolButton *prev,*next,*replaceButton,*replacePButton,*replaceAButton;
108     QCheckBox *wholeLine;
109     QGridLayout *slt;
110     QComboBox *resiFinder;
111     QString sortLabelList(CEnvironment &asymm, MyAtom &tatze);
112     void searchchanged(bool current,bool back);
113   //void searchchanged2(bool current,bool back);
114 signals:
115     void findInStructure(const QString &);//!< findInStructure is emitted by a context menu to select and center an atom in the OpenGL representation of the structure.
116     void openIncludeFile(const QString &);
117     void electInStructure(const QString &);//!< electInStructure is emitted by a context menu to select atoms found in the editor text selection in the OpenGL representation.
118     void deleteSelected();//!< deleteSelected is emitted by a context menu to select and delete atoms found in the editor text selection in the OpenGL representation.
119     void saveMe(bool spell, bool loadafter);
120     void updateLabel();//!< the rename mode is updated
121 protected:
122     void resizeEvent(QResizeEvent *event);
123     bool viewportEvent(QEvent *event);
124     void keyPressEvent(QKeyEvent *e);
125     void focusInEvent(QFocusEvent *e);
126     void contextMenuEvent(QContextMenuEvent *event);
127 public slots:
vis(bool b)128     void vis(bool b){ uCanSeeMe=b; }
129     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.
130     void insertAnis();//!< inserts ANIS instruction into the file.
131     void insertActa();//!< inserts ACTA instruction into the file.
132     void insertList4();//!< inserts a LIST 4 instruction into the file.
133     void insertList6();//!< inserts a LIST 6 instruction into the file.
134     void insertBind();//!< inserts a BIND instruction into the file.
135     void insertFree();//!< inserts a FREE instruction into the file.
136     void findResi(QString);//!<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).
137     void findText();//!<Find functionality of search and replace in the editor.
138     void hideSearch();//!< Hides the search and replace widget for the editor.
139     void replaceAll();//!<replace all functionality of search and replace in the editor.
140     void replaceNext();//!<replace next functionality of search and replace in the editor.
141     void replacePrev();//!<replace previous functionality of search and replace in the editor.
142     void showSearch();//!< shows the search and repleace for the editor.
143     void changeSortierWeise(QAction* action);//!<Changes the sorting option to action->data. @param action the calling QAction.
144     void sortAtoms();//!< sorts atoms in file acording to the sort option.
145     void sortAtoms(CEnvironment &as);//!< sorts atoms in file acording to the sort option.
146     void sortSelectedRegion();//!< sorts all atoms found in selected text in the structure @params info seleted text.
147     void expandGreaterThan();//!< some SHELXL re- or constrainst use > to specify a range of atoms this is a problem for sorting atoms
148     void findNext();//!<Find next (F3) functionality of search and replace in the editor.
149     void findPrev();//!<Find previous functionality of search and replace in the editor.
150     void weedEmptySfacs();//!< weeds out unused scattering factors
151     void updateUnit();//!< the unit instruction gets the true amount of atoms per type
152     void dispFromWave();//!<calculates f', f" and mu for the given wavelength and inserts DISP lines for each SFAC entry.
153     void resiFinderDestroyed();
154     void decreaseEdtiorFont();//!<Decreases the editor font by one point.
155     void increaseEdtiorFont();//!< Increases the editor font by one point.
156     void jumpToError();//!< the cursor points to the first syntax error in the file.a
157     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.
158     void omitsome(const QString &s);//!< insetrts a 'OMIT h k l\n' string in the res file.
159     void insertDFIX(double value, double esd, QList<MyAtom> selected, QString resiSpec=""); //!< Insert DFIX restraint into ins file
160     void insertDANG(double value, double esd, QList<MyAtom> selected, QString resiSpec=""); //!< Insert DANG restraint into ins file
161     void insertFLAT(double esd, QList<MyAtom> selected, QString resiSpec=""); //!< Insert FLAT restraint into ins file
162     void insertEXYZ(QList<MyAtom> selected, QString resiSpec=""); //!< Insert EXYZ restraint into ins file
163     void insertEADP(QList<MyAtom> selected, QString resiSpec=""); //!< Insert EADP restraint into ins file
164     void insertDELU(double esd1, double esd2, QList<MyAtom> selected, QString resiSpec=""); //!< Insert DELU restraint into ins file
165     void insertSIMU(double esd1, double esd2, double dmax, QList<MyAtom> selected, QString resiSpec=""); //!< Insert SIMU restraint into ins file
166     void insertISOR(double esd1, double esd2, QList<MyAtom> selected, QString resiSpec=""); //!< Insert ISOR restraint into ins file
167     void insertRIGU(double esd1, double esd2, QList<MyAtom> selected, QString resiSpec=""); //!< Insert RIGU restraint into ins file
168     void insertCHIV(double vol, double esd1, QList<MyAtom> selected, QString resiSpec=""); //!< Insert CHIV restraint into ins file
169     void insertANIS(QList<MyAtom> selected); //!< Insert ANIS for selected atoms
170     void updateLineNumberArea(const QRect &, int);//!< updateLineNumberArea in the curent viewport.
171     void updateAfix();//!< updateAfix updates the parenthesis list for the AfixHighlightArea.
172     void updateWght();//!< the WGHT instruction is updated from the back of the res file
173     void insertCompletion(const QString &completion);//!< insertCompletion inserts the text selected in the completer. @param completion selected text of the completer.
174     void searchInStructure();//!< Select and center an atom in the OpenGL representation of the structure.
175     void openAnIncludeFile();
176     void selectInStructure();//!< Select atoms found in the editor text selection in the OpenGL representation.
177     void lineNumberToggled(QMouseEvent *event);//!< when the LineNumberArea is clicked the comment state of the secified line is toggled.
178     void remark(int line);//!< toggles the comment state of the secified line. @param line The line number in file
179     void toggleRemarks();//!< toggleRemarks toggles the comment state of the selected lines.
setgl(ChGL * g)180     void setgl(ChGL *g){
181       chgl=g;
182     }//!< setgl passes the ChGL widget to the CodeEditor
183 
184 private:
185     QString selectedRestraintsAtoms(QString buffer, QList<MyAtom> selected, QString resiSpec, bool exclude_H=false);
186     QString textUnderCursor() const;
187     QWidget *lineNumberArea;
188     QCompleter *c;
189     QWidget *afixHighlightArea;
190     ChGL *chgl;
191 };
192 
193 /////////////////////////////////////////////////
194 /*! \brief AfixHighlightArea is a colored bar right of the LineNumberArea to indicate if the line is in a AFIX environment.
195  *
196  *  This is just a dummy class to reimplement the paintEvent which is impemented in CodeEditor.
197  *
198  */
199 class AfixHighlightArea : public QWidget{
200   public:
AfixHighlightArea(CodeEditor * editor)201     AfixHighlightArea(CodeEditor *editor) : QWidget(editor){
202       codeEditor = editor;
203     }
sizeHint()204     QSize sizeHint() const {
205       return QSize(5,0);
206     }
207   protected:
paintEvent(QPaintEvent * event)208     void paintEvent(QPaintEvent *event){
209       codeEditor->afixHighlightAreaPaintEvent(event);
210     }//!< calls afixHighlightAreaPaintEvent().
mouseMoveEvent(QMouseEvent * event)211     void mouseMoveEvent(QMouseEvent *event){
212         codeEditor->hoverLineNumber(event);
213     }
214   private:
215     CodeEditor *codeEditor;
216 };
217 /////////////////////////////////////////////////
218 /*! \brief LineNumberArea is a area with line numbers left of the CodeEditor.
219  *
220  * This is just a dummy class to reimplement the paintEvent and mousePressEvent which are impemented in CodeEditor.
221  */
222 class LineNumberArea : public QWidget{
223   public:
LineNumberArea(CodeEditor * editor)224     LineNumberArea(CodeEditor *editor) : QWidget(editor) {
225       codeEditor = editor;
226     }
227 
sizeHint()228     QSize sizeHint() const {
229       return QSize(codeEditor->lineNumberAreaWidth(), 0);
230     }
231 
232   protected:
paintEvent(QPaintEvent * event)233     void paintEvent(QPaintEvent *event) {
234       codeEditor->lineNumberAreaPaintEvent(event);
235     }//!< calls lineNumberAreaPaintEvent().
mousePressEvent(QMouseEvent * event)236     void mousePressEvent ( QMouseEvent * event ){
237       codeEditor->lineNumberToggled(event);
238     }//!< calls lineNumberToggled().
mouseMoveEvent(QMouseEvent * event)239     void mouseMoveEvent(QMouseEvent *event){
240         codeEditor->hoverLineNumber(event);
241     }
242 
243   private:
244     CodeEditor *codeEditor;
245 };
246 
247 #endif
248