1 // Translation Manager Plugin - OVQT Plugin <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2011 Emanuel COSTEA <cemycc@gmail.com>
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Winch Gate Property Limited
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef EDITOR_PHRASE_H
21 #define EDITOR_PHRASE_H
24 #include "translation_manager_editor.h"
27 #include <QtCore/QObject>
28 #include <QtCore/QFile>
29 #include <QtCore/QTextStream>
30 #include <QtGui/QWidget>
31 #include <QtGui/QMdiArea>
32 #include <QtGui/QMdiSubWindow>
33 #include <QtGui/QUndoCommand>
34 #include <QtGui/QUndoStack>
35 #include <QtGui/QTextEdit>
36 #include <QtGui/QSyntaxHighlighter>
37 #include <QtGui/QErrorMessage>
40 namespace TranslationManager
43 class CTextEdit
: public QTextEdit
48 QUndoStack
*m_undoStack
;
51 CTextEdit(QWidget
*parent
= 0) : QTextEdit(parent
)
53 setUndoRedoEnabled(true);
55 //void keyPressEvent(QKeyEvent *event);
56 void setUndoStack(QUndoStack
*undoStack
)
58 m_undoStack
= undoStack
;
62 class CEditorPhrase
: public CEditor
67 CEditorPhrase(QMdiArea
*parent
) : CEditor(parent
) {}
68 CEditorPhrase() : CEditor() {}
69 void open(QString filename
);
71 void saveAs(QString filename
);
72 void activateWindow();
73 void closeEvent(QCloseEvent
*event
);
76 void docContentsChanged();
77 void newUndoCommandAdded();
83 class CUndoPhraseNewCommand
: public QUndoCommand
86 CUndoPhraseNewCommand(CTextEdit
*textEdit
, QUndoCommand
*parent
= 0)
87 : QUndoCommand("Inserting/Removing characters", parent
),
91 ~CUndoPhraseNewCommand() {}
104 CTextEdit
*m_textEdit
;
107 class SyntaxHighlighter
: public QSyntaxHighlighter
110 SyntaxHighlighter(QTextEdit
*parent
) : QSyntaxHighlighter(parent
)
112 HighlightingRule rule
;
114 translateStringFormat
.setFontWeight(QFont::Bold
);
115 translateStringFormat
.setForeground(Qt::darkMagenta
);
116 rule
.pattern
= QRegExp("\\[.+\\]");
117 rule
.format
= translateStringFormat
;
118 highlightingRules
.append(rule
);
120 singleLineCommentFormat
.setForeground(Qt::red
);
121 rule
.pattern
= QRegExp("//[^\n]*");
122 rule
.format
= singleLineCommentFormat
;
123 highlightingRules
.append(rule
);
125 multiLineCommentFormat
.setForeground(Qt::red
);
127 quotationFormat
.setForeground(Qt::darkGreen
);
128 rule
.pattern
= QRegExp("\".*\"");
129 rule
.format
= quotationFormat
;
130 highlightingRules
.append(rule
);
132 functionFormat
.setFontItalic(true);
133 functionFormat
.setForeground(Qt::blue
);
134 rule
.pattern
= QRegExp("\\(.+\\)");
135 rule
.format
= functionFormat
;
136 highlightingRules
.append(rule
);
138 commentStartExpression
= QRegExp("/\\*");
139 commentEndExpression
= QRegExp("\\*/");
142 void highlightBlock(const QString
&text
)
144 Q_FOREACH(const HighlightingRule
&rule
, highlightingRules
)
146 QRegExp
expression(rule
.pattern
);
147 int index
= expression
.indexIn(text
);
150 int length
= expression
.matchedLength();
151 setFormat(index
, length
, rule
.format
);
152 index
= expression
.indexIn(text
, index
+ length
);
155 setCurrentBlockState(0);
158 if (previousBlockState() != 1)
159 startIndex
= commentStartExpression
.indexIn(text
);
161 while (startIndex
>= 0)
163 int endIndex
= commentEndExpression
.indexIn(text
, startIndex
);
167 setCurrentBlockState(1);
168 commentLength
= text
.length() - startIndex
;
172 commentLength
= endIndex
- startIndex
173 + commentEndExpression
.matchedLength();
175 setFormat(startIndex
, commentLength
, multiLineCommentFormat
);
176 startIndex
= commentStartExpression
.indexIn(text
, startIndex
+ commentLength
);
181 struct HighlightingRule
184 QTextCharFormat format
;
186 QVector
<HighlightingRule
> highlightingRules
;
188 QRegExp commentStartExpression
;
189 QRegExp commentEndExpression
;
191 QTextCharFormat keywordFormat
;
192 QTextCharFormat classFormat
;
193 QTextCharFormat singleLineCommentFormat
;
194 QTextCharFormat multiLineCommentFormat
;
195 QTextCharFormat quotationFormat
;
196 QTextCharFormat functionFormat
;
197 QTextCharFormat translateStringFormat
;
202 #endif /* EDITOR_PHRASE_H */