1 #ifndef _SCRIPTEDITOR_H_
2 #define _SCRIPTEDITOR_H_
3 //=============================================================================
4 //
5 //   File : ScriptEditorImplementation.h
6 //   Creation date : Sun Mar 28 1999 16:11:48 CEST by Szymon Stefanek
7 //
8 //   This file is part of the KVIrc IRC client distribution
9 //   Copyright (C) 1999-2010 Szymon Stefanek <pragma at kvirc dot net>
10 //
11 //   This program is FREE software. You can redistribute it and/or
12 //   modify it under the terms of the GNU General Public License
13 //   as published by the Free Software Foundation; either version 2
14 //   of the License, or (at your option) any later version.
15 //
16 //   This program is distributed in the HOPE that it will be USEFUL,
17 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
18 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 //   See the GNU General Public License for more details.
20 //
21 //   You should have received a copy of the GNU General Public License
22 //   along with this program. If not, write to the Free Software Foundation,
23 //   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 //=============================================================================
26 
27 #include "KviScriptEditor.h"
28 #include "KviSelectors.h"
29 
30 #include <QCompleter>
31 #include <QLabel>
32 #include <QDialog>
33 #include <QEvent>
34 #include <QTextEdit>
35 #include <QListWidget>
36 #include <QCheckBox>
37 #include <QSyntaxHighlighter>
38 #include <QByteArray>
39 #include <QMenu>
40 #include <QTimer>
41 
42 #include <set>
43 #include <vector>
44 
45 class ScriptEditorSyntaxHighlighter;
46 class ScriptEditorWidget final : public QTextEdit
47 {
48 	Q_OBJECT
49 	Q_PROPERTY(bool contextSensitiveHelp READ contextSensitiveHelp)
50 public:
51 	ScriptEditorWidget(QWidget * pParent);
52 	virtual ~ScriptEditorWidget();
53 
54 public:
55 	QString m_szFind;
56 
57 protected:
58 	ScriptEditorSyntaxHighlighter * m_pSyntaxHighlighter;
59 	QCompleter * m_pCompleter;
60 	QStringList * m_pListModulesNames;
61 	QStringList * m_pListCompletition;
62 	QTimer * m_pStartTimer;
63 	QWidget * m_pParent;
64 	int iIndex, iModulesCount;
65 	QString m_szHelp;
66 
67 public:
syntaxHighlighter()68 	ScriptEditorSyntaxHighlighter * syntaxHighlighter() { return m_pSyntaxHighlighter; };
69 	void disableSyntaxHighlighter();
70 	void enableSyntaxHighlighter();
71 
72 	void createCompleter(QStringList & list);
73 
74 	void loadCompleterFromFile();
completer()75 	QCompleter * completer() const { return m_pCompleter; };
76 	QString textUnderCursor() const;
77 	void updateOptions();
78 	bool contextSensitiveHelp() const;
79 public slots:
80 	void checkReadyCompleter();
81 	void insertCompletion(const QString & szCompletion);
82 	void slotFind();
83 	void slotHelp();
84 	void slotReplace();
85 protected slots:
86 	void asyncCompleterCreation();
87 signals:
88 	void keyPressed();
89 
90 private:
91 	void contextMenuEvent(QContextMenuEvent * e) override;
92 	void keyPressEvent(QKeyEvent * e) override;
93 };
94 
95 class ScriptEditorWidgetColorOptions final : public QDialog
96 {
97 	Q_OBJECT
98 public:
99 	ScriptEditorWidgetColorOptions(QWidget * pParent);
100 
101 private:
102 	std::vector<KviSelectorInterface *> m_pSelectorInterfaceList;
103 	KviColorSelector * addColorSelector(QWidget * pParent, const QString & txt, QColor * pOption, bool bEnabled);
104 
105 protected slots:
106 	void okClicked();
107 };
108 
109 class ScriptEditorSyntaxHighlighter final : public QSyntaxHighlighter
110 {
111 public:
112 	ScriptEditorSyntaxHighlighter(ScriptEditorWidget * pWidget);
113 	virtual ~ScriptEditorSyntaxHighlighter();
114 
115 public:
116 	QTextEdit * m_pTextEdit;
117 
118 public:
textEdit()119 	QTextEdit * textEdit() { return m_pTextEdit; }
120 	void highlightBlock(const QString & szText);
121 	void updateSyntaxtTextFormat();
122 
123 private:
124 	struct KviScriptHighlightingRule
125 	{
126 		QRegExp pattern;
127 		QTextCharFormat format;
128 	};
129 
130 	QVector<KviScriptHighlightingRule> highlightingRules;
131 	QRegExp commentStartExpression;
132 	QRegExp commentEndExpression;
133 
134 	QTextCharFormat bracketFormat;
135 	QTextCharFormat punctuationFormat;
136 	QTextCharFormat keywordFormat;
137 	QTextCharFormat variableFormat;
138 	QTextCharFormat normaltextFormat;
139 	QTextCharFormat findFormat;
140 	QTextCharFormat functionFormat;
141 	QTextCharFormat commentFormat;
142 };
143 
144 class ScriptEditorImplementation final : public KviScriptEditor
145 {
146 	Q_OBJECT
147 public:
148 	ScriptEditorImplementation(QWidget * par);
149 	~ScriptEditorImplementation() override;
150 
151 public:
152 	QLineEdit * m_pFindLineEdit;
153 
154 protected:
155 	ScriptEditorWidgetColorOptions * m_pOptionsDialog;
156 	ScriptEditorWidget * m_pEditor;
157 	QLabel * m_pRowColLabel;
158 	QPushButton * m_pFindButton;
159 	int m_lastCursorPos;
160 
161 public:
162 	void setText(const char * txt) override;
163 	void setText(const QString & szText) override;
164 	void setText(const QByteArray & szText) override;
165 	void getText(QString & szText) override;
166 	void getText(QByteArray & szText) override;
167 	void setFindText(const QString & szText) override;
168 	virtual void setEnabled(bool bEnabled);
169 	void setReadOnly(bool bReadOnly) override;
170 	void setUnHighlightedText(const QString & szText) override;
171 
172 	virtual void setFocus();
173 	bool isModified() override;
174 	void setModified(bool) override;
175 
getCursor()176 	int getCursor() override { return m_pEditor->textCursor().position(); };
177 	void setCursorPosition(int iPos) override;
cursor()178 	int cursor() { return m_lastCursorPos; };
findLineEdit()179 	QLineEdit * findLineEdit() { return m_pFindLineEdit; };
180 private:
181 	void focusInEvent(QFocusEvent * e) override;
182 	void loadOptions();
183 	void saveOptions();
184 protected slots:
185 	void saveToFile();
186 	void loadFromFile();
187 	void configureColors();
188 	void updateRowColLabel();
189 	void slotFind();
190 	void slotReplaceAll(const QString & szToReplace, const QString & szReplaceWith);
191 	void slotInitFind();
192 	void slotNextFind(const QString &);
193 	void optionsDialogFinished(int iResult);
194 signals:
195 	void find(const QString &);
196 	void replaceAll(const QString & szToReplace, const QString & szReplaceWith);
197 	void initFind();
198 	void nextFind(const QString & szText);
199 };
200 
201 class ScriptEditorReplaceDialog final : public QDialog
202 {
203 	Q_OBJECT
204 public:
205 	ScriptEditorReplaceDialog(QWidget * parent = nullptr, const QString & szName = QString());
206 
207 public:
208 	QLineEdit * m_pFindLineEdit;
209 	QLineEdit * m_pReplaceLineEdit;
210 
211 private:
212 	QPushButton * m_pReplaceButton;
213 	QCheckBox * m_pCheckReplaceAll;
214 	QWidget * m_pParent;
215 private slots:
216 	void textChanged(const QString &);
217 	void slotReplace();
218 	void slotNextFind();
219 signals:
220 	void replaceAll(const QString &, const QString &);
221 	void initFind();
222 	void nextFind(const QString &);
223 };
224 
225 #endif // _SCRIPTEDITOR_H_
226