Salome HOME
b1f6e1ec745c1845a2bb274122cc6f393260cd5c
[modules/shaper.git] / src / XGUI / XGUI_ErrorDialog.cpp
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <XGUI_ErrorDialog.h>
21
22 #include <ModuleBase_Tools.h>
23 #include <Config_Translator.h>
24
25 #include <QDialogButtonBox>
26 #include <QHBoxLayout>
27 #include <QLabel>
28 #include <QVBoxLayout>
29 #include <QTextEdit>
30 #include <QTextCodec>
31
32 XGUI_ErrorDialog::XGUI_ErrorDialog(QWidget* parent)
33     : QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowTitleHint |
34       Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
35 {
36   QVBoxLayout* aDlgLay = new QVBoxLayout(this);
37   setWindowTitle(tr("Application errors"));
38   myErrorLog = new QTextEdit(this);
39   myErrorLog->setReadOnly(true);
40   aDlgLay->addWidget(myErrorLog);
41   QDialogButtonBox* aButtonBox = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal,
42                                                       this);
43   aDlgLay->addWidget(aButtonBox);
44   aDlgLay->setContentsMargins(2, 2, 2, 2);
45   aDlgLay->setSpacing(2);
46   setLayout(aDlgLay);
47   resize(420, 240);
48
49   connect(aButtonBox, SIGNAL(accepted()), this, SLOT(clear()));
50   connect(aButtonBox, SIGNAL(rejected()), this, SLOT(clear()));
51 }
52
53 XGUI_ErrorDialog::~XGUI_ErrorDialog()
54 {
55 }
56
57 void XGUI_ErrorDialog::refresh()
58 {
59   myErrorLog->clear();
60   foreach(QString eachError, myErrors)
61   {
62     myErrorLog->append(eachError);
63   }
64 }
65
66 void XGUI_ErrorDialog::clear()
67 {
68   myErrorLog->clear();
69   myErrors.clear();
70   QDialog::reject();
71 }
72
73 void XGUI_ErrorDialog::addError(std::shared_ptr<Events_InfoMessage> theMsg)
74 {
75   std::string aError = Config_Translator::translate(*theMsg);
76   std::string aCodec = Config_Translator::codec(*theMsg);
77   QString aMsg = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aError.c_str());
78   myErrors.append(aMsg);
79   refresh();
80   if (!isVisible()) {
81     show();
82     ModuleBase_Tools::activateWindow(this, "XGUI_ErrorDialog::addError");
83   }
84 }
85
86 void XGUI_ErrorDialog::removeError(const QString& theError)
87 {
88   myErrors.removeAll(theError);
89   refresh();
90 }