Linux multi-monitor fullscreen support
[ryzomcore.git] / studio / src / plugins / translation_manager / editor_phrase.cpp
blob510255828d9556481790121a6d186f8431372c8d
1 // Translation Manager Plugin - OVQT Plugin <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2011 Emanuel COSTEA <cemycc@gmail.com>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // Project includes
18 #include "editor_phrase.h"
19 #include "translation_manager_constants.h"
21 // Nel includes
22 #include "nel/misc/path.h"
23 #include "nel/misc/diff_tool.h"
25 // Qt includes
26 #include <QtCore/QFileInfo>
27 #include <QtCore/QByteArray>
28 #include <QtCore/QTextCodec>
29 #include <QtCore/QTextStream>
30 #include <QtGui/QTextCursor>
31 #include <QtGui/QErrorMessage>
32 #include <QtGui/QMessageBox>
33 #include <QtGui/QCloseEvent>
35 using namespace std;
37 namespace TranslationManager
40 void CEditorPhrase::open(QString filename)
42 std::vector<STRING_MANAGER::TPhrase> phrases;
43 if(readPhraseFile(filename.toUtf8().constData(), phrases, false))
45 text_edit = new CTextEdit(this);
46 text_edit->setUndoStack(current_stack);
47 SyntaxHighlighter *highlighter = new SyntaxHighlighter(text_edit);
48 text_edit->setUndoRedoEnabled(true);
49 text_edit->document()->setUndoRedoEnabled(true);
50 setWidget(text_edit);
51 // read the file content
52 QFile file(filename);
53 file.open(QIODevice::ReadOnly | QIODevice::Text);
54 QTextStream in(&file);
55 // set the file content to the text edit
56 QString data = in.readAll();
57 text_edit->append(data);
58 // window settings
59 setCurrentFile(filename);
60 setAttribute(Qt::WA_DeleteOnClose);
61 editor_type = Constants::ED_PHRASE;
62 current_file = filename;
63 connect(text_edit->document(), SIGNAL(contentsChanged()), this, SLOT(docContentsChanged()));
64 connect(text_edit->document(), SIGNAL(undoCommandAdded()), this, SLOT(newUndoCommandAdded()));
66 else
68 QErrorMessage error;
69 error.showMessage("This file is not a phrase file.");
70 error.exec();
74 void CEditorPhrase::newUndoCommandAdded()
76 current_stack->push(new CUndoPhraseNewCommand(text_edit));
79 void CEditorPhrase::docContentsChanged()
81 setWindowModified(true);
84 void CEditorPhrase::activateWindow()
86 showMaximized();
89 void CEditorPhrase::save()
91 saveAs(current_file);
94 void CEditorPhrase::saveAs(QString filename)
96 QFile file(filename);
97 file.open(QIODevice::WriteOnly | QIODevice::Text);
98 QTextStream out(&file);
99 out.setCodec("UTF-8");
100 out.setGenerateByteOrderMark(true);
101 out << text_edit->toPlainText();
102 current_file = filename;
103 setCurrentFile(current_file);
106 void CEditorPhrase::closeEvent(QCloseEvent *event)
108 if(isWindowModified())
110 QMessageBox msgBox;
111 msgBox.setIcon(QMessageBox::Question);
112 msgBox.setText(tr("The document has been modified."));
113 msgBox.setInformativeText(tr("Do you want to save your changes?"));
114 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
115 msgBox.setDefaultButton(QMessageBox::Save);
117 int ret = msgBox.exec();
118 switch (ret)
120 case QMessageBox::Save:
121 save();
122 break;
123 case QMessageBox::Discard:
124 break;
125 case QMessageBox::Cancel:
126 event->ignore();
127 return;
130 event->accept();
131 close();