New lua versions
[ryzomcore.git] / studio / src / plugins / translation_manager / editor_phrase.h
blob404a17d5158e797eb30cc1a27e67f2ec63725324
1 // Translation Manager Plugin - OVQT Plugin <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2011 Emanuel COSTEA <cemycc@gmail.com>
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Winch Gate Property Limited
6 //
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
23 // Project includes
24 #include "translation_manager_editor.h"
26 // Qt includes
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>
38 #include <QKeyEvent>
40 namespace TranslationManager
43 class CTextEdit : public QTextEdit
45 Q_OBJECT
47 private:
48 QUndoStack *m_undoStack;
50 public:
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
64 Q_OBJECT
66 public:
67 CEditorPhrase(QMdiArea *parent) : CEditor(parent) {}
68 CEditorPhrase() : CEditor() {}
69 void open(QString filename);
70 void save();
71 void saveAs(QString filename);
72 void activateWindow();
73 void closeEvent(QCloseEvent *event);
75 public Q_SLOTS:
76 void docContentsChanged();
77 void newUndoCommandAdded();
79 private:
80 CTextEdit *text_edit;
83 class CUndoPhraseNewCommand : public QUndoCommand
85 public:
86 CUndoPhraseNewCommand(CTextEdit *textEdit, QUndoCommand *parent = 0)
87 : QUndoCommand("Inserting/Removing characters", parent),
88 m_textEdit(textEdit)
91 ~CUndoPhraseNewCommand() {}
93 void undo()
95 m_textEdit->undo();
98 void redo()
100 m_textEdit->redo();
103 private:
104 CTextEdit *m_textEdit;
107 class SyntaxHighlighter : public QSyntaxHighlighter
109 public:
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);
148 while (index >= 0)
150 int length = expression.matchedLength();
151 setFormat(index, length, rule.format);
152 index = expression.indexIn(text, index + length);
155 setCurrentBlockState(0);
157 int startIndex = 0;
158 if (previousBlockState() != 1)
159 startIndex = commentStartExpression.indexIn(text);
161 while (startIndex >= 0)
163 int endIndex = commentEndExpression.indexIn(text, startIndex);
164 int commentLength;
165 if (endIndex == -1)
167 setCurrentBlockState(1);
168 commentLength = text.length() - startIndex;
170 else
172 commentLength = endIndex - startIndex
173 + commentEndExpression.matchedLength();
175 setFormat(startIndex, commentLength, multiLineCommentFormat);
176 startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
180 private:
181 struct HighlightingRule
183 QRegExp pattern;
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 */