Salome HOME
Translate strings with corresponded codec
[modules/shaper.git] / src / XGUI / XGUI_ErrorDialog.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 /*
4  * XGUI_ErrorDialog.cpp
5  *
6  *  Created on: Apr 28, 2014
7  *      Author: sbh
8  */
9 #include <XGUI_ErrorDialog.h>
10
11 #include <ModuleBase_Tools.h>
12 #include <Config_Translator.h>
13
14 #include <QDialogButtonBox>
15 #include <QHBoxLayout>
16 #include <QLabel>
17 #include <QVBoxLayout>
18 #include <QTextEdit>
19 #include <QTextCodec>
20
21 XGUI_ErrorDialog::XGUI_ErrorDialog(QWidget* parent)
22     : QDialog(parent)
23 {
24   QVBoxLayout* aDlgLay = new QVBoxLayout(this);
25   setWindowTitle(tr("Application errors"));
26   myErrorLog = new QTextEdit(this);
27   myErrorLog->setReadOnly(true);
28   aDlgLay->addWidget(myErrorLog);
29   QDialogButtonBox* aButtonBox = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal,
30                                                       this);
31   aDlgLay->addWidget(aButtonBox);
32   aDlgLay->setContentsMargins(2, 2, 2, 2);
33   aDlgLay->setSpacing(2);
34   setLayout(aDlgLay);
35   resize(420, 240);
36
37   connect(aButtonBox, SIGNAL(accepted()), this, SLOT(clear()));
38   connect(aButtonBox, SIGNAL(rejected()), this, SLOT(clear()));
39 }
40
41 XGUI_ErrorDialog::~XGUI_ErrorDialog()
42 {
43 }
44
45 void XGUI_ErrorDialog::refresh()
46 {
47   myErrorLog->clear();
48   foreach(QString eachError, myErrors)
49   {
50     myErrorLog->append(eachError);
51   }
52 }
53
54 void XGUI_ErrorDialog::clear()
55 {
56   myErrorLog->clear();
57   myErrors.clear();
58   QDialog::reject();
59 }
60
61 void XGUI_ErrorDialog::addError(std::shared_ptr<Events_InfoMessage> theMsg)
62 {
63   std::string aError = Config_Translator::translate(theMsg);
64   std::string aCodec = Config_Translator::codec(theMsg->context());
65   QString aMsg = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aError.c_str());
66   myErrors.append(aMsg);
67   refresh();
68   if (!isVisible()) {
69     show();
70     raise();
71     ModuleBase_Tools::activateWindow(this, "XGUI_ErrorDialog::addError");
72   }
73 }
74
75 void XGUI_ErrorDialog::removeError(const QString& theError)
76 {
77   myErrors.removeAll(theError);
78   refresh();
79 }