fw4spl
t/src/fwGuiQt/dialog/LoggerDialog.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwGuiQt/dialog/LoggerDialog.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwGui/registry/macros.hpp>
12 
13 #include <fwRuntime/operations.hpp>
14 
15 #include <boost/foreach.hpp>
16 
17 #include <QApplication>
18 #include <QCheckBox>
19 #include <QHBoxLayout>
20 #include <QHeaderView>
21 #include <QLabel>
22 #include <QPushButton>
23 #include <QTableWidgetItem>
24 #include <QVBoxLayout>
25 #include <QWidget>
26 
28 
29 namespace fwGuiQt
30 {
31 namespace dialog
32 {
33 
34 //------------------------------------------------------------------------------
35 
37 {
38 }
39 
40 //------------------------------------------------------------------------------
41 
43 {
44 }
45 
46 //------------------------------------------------------------------------------
47 
48 void LoggerDialog::setTitle(const std::string& title)
49 {
50  m_title = title;
51 }
52 
53 //------------------------------------------------------------------------------
54 
55 void LoggerDialog::setMessage(const std::string& message)
56 {
57  m_message = message;
58 }
59 
60 //------------------------------------------------------------------------------
61 
62 void LoggerDialog::setLogger(const ::fwLog::Logger::sptr& logger)
63 {
64  m_logger = logger;
65 }
66 
67 //------------------------------------------------------------------------------
68 
70 {
71  QWidget* parent = qApp->activeWindow();
72 
73  // Size policy
74  QSizePolicy policy(QSizePolicy::Maximum, QSizePolicy::Preferred);
75 
76  // Create dialog
77  m_dialog = new QDialog(parent);
78  m_dialog->resize(500, 50);
79  m_dialog->setWindowTitle(QString::fromStdString(m_title));
80  QVBoxLayout* mainLayout = new QVBoxLayout();
81  mainLayout->setAlignment(Qt::AlignTop);
82  m_dialog->setLayout(mainLayout);
83 
84  // Disable close button
85  m_dialog->setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint| Qt::WindowSystemMenuHint);
86 
87  // Create icon and message widget
88  QHBoxLayout* messageLayout = new QHBoxLayout();
89  QWidget* messageWidget = new QWidget();
90  messageWidget->setSizePolicy(policy);
91  messageWidget->setLayout(messageLayout);
92  messageLayout->setAlignment(Qt::AlignTop);
93 
94  // Create icon
95  QLabel* iconLabel = new QLabel();
96  if(m_logger->count(::fwLog::Log::CRITICAL) > 0)
97  {
98  const auto path = ::fwRuntime::getLibraryResourceFilePath("fwGuiQt-0.1/critical.png");
99  iconLabel->setPixmap(QIcon(QString::fromStdString(path.string())).pixmap(48, 48));
100  }
101  else if(m_logger->count(::fwLog::Log::WARNING) > 0)
102  {
103  const auto path = ::fwRuntime::getLibraryResourceFilePath("fwGuiQt-0.1/warning.png");
104  iconLabel->setPixmap(QIcon(QString::fromStdString(path.string())).pixmap(48, 48));
105  }
106  else
107  {
108  const auto path = ::fwRuntime::getLibraryResourceFilePath("fwGuiQt-0.1/information.png");
109  iconLabel->setPixmap(QIcon(QString::fromStdString(path.string())).pixmap(48, 48));
110  }
111  messageLayout->addWidget(iconLabel);
112 
113  // Create message
114  std::stringstream ss;
115  ss << m_message <<
116  "<br><br>" << "<b>Log report :</b> " << m_logger->count(::fwLog::Log::CRITICAL) << " critical, " <<
117  m_logger->count(::fwLog::Log::WARNING) << " warning and " <<
118  m_logger->count(::fwLog::Log::INFORMATION) << " information messages.";
119 
120  QLabel* messageLabel = new QLabel(ss.str().c_str());
121  messageLayout->addWidget(messageLabel);
122 
123  // Create button widget
124  QHBoxLayout* buttonLayout = new QHBoxLayout();
125  QWidget* buttonWidget = new QWidget();
126  buttonWidget->setSizePolicy(policy);
127  buttonWidget->setLayout(buttonLayout);
128 
129  // Create OK button
130  QPushButton* okButton = new QPushButton(tr("Ok"));
131  okButton->setSizePolicy(policy);
132  buttonLayout->addWidget(okButton);
133 
134  // Create CANCEL button
135  QPushButton* cancelButton = new QPushButton(tr("Cancel"));
136  cancelButton->setSizePolicy(policy);
137  buttonLayout->addWidget(cancelButton);
138 
139  // Create a checkbox to display the logs
140  QCheckBox* checkbox = new QCheckBox("Details");
141  const auto detailshidden = ::fwRuntime::getLibraryResourceFilePath("fwGuiQt-0.1/details-hidden.png").string();
142  const auto detailsshown = ::fwRuntime::getLibraryResourceFilePath("fwGuiQt-0.1/details-shown.png").string();
143  std::string styleSheet;
144  styleSheet += "QCheckBox::indicator:unchecked { image: url(" + detailshidden + "); }";
145  styleSheet += "QCheckBox::indicator:checked { image: url(" + detailsshown + "); }";
146  checkbox->setStyleSheet(QString::fromStdString(styleSheet));
147 
148  // Create log table
149  m_logTableWidget = new QTableWidget(static_cast<int>(m_logger->count()), 2);
150  m_logTableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("Level"));
151  m_logTableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("Message"));
152  m_logTableWidget->setColumnWidth(0, 120);
153  m_logTableWidget->horizontalHeader()->setStretchLastSection(true);
154 
155  // Fill log table
156  ::fwLog::Logger::ConstIteratorType it = m_logger->begin();
157  int row = 0;
158  for(; it != m_logger->end(); ++it, ++row)
159  {
160  std::string levelString = "Unknown";
161  QIcon levelIcon;
162  ::fwLog::Log::LevelType level = it->getLevel();
163  if (level == ::fwLog::Log::INFORMATION)
164  {
165  levelString = "Information";
166  const auto path = ::fwRuntime::getLibraryResourceFilePath("fwGuiQt-0.1/information.png");
167  levelIcon = QIcon(QString::fromStdString(path.string()));
168  }
169  else if (level == ::fwLog::Log::WARNING)
170  {
171  levelString = "Warning";
172  const auto path = ::fwRuntime::getLibraryResourceFilePath("fwGuiQt-0.1/warning.png");
173  levelIcon = QIcon(QString::fromStdString(path.string()));
174  }
175  else if (level == ::fwLog::Log::CRITICAL)
176  {
177  levelString = "Critical";
178  const auto path = ::fwRuntime::getLibraryResourceFilePath("fwGuiQt-0.1/critical.png");
179  levelIcon = QIcon(QString::fromStdString(path.string()));
180  }
181 
182  QTableWidgetItem* levelItem = new QTableWidgetItem(levelIcon, levelString.c_str());
183  levelItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
184  m_logTableWidget->setItem(row, 0, levelItem);
185 
186  QTableWidgetItem* messageItem = new QTableWidgetItem(it->getMessage().c_str());
187  messageItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
188  m_logTableWidget->setItem(row, 1, messageItem);
189  }
190 
191  // Add widget to dialog
192  mainLayout->addWidget(messageWidget, 0, Qt::AlignLeft);
193  mainLayout->addWidget(checkbox);
194  mainLayout->addWidget(m_logTableWidget);
195  mainLayout->addWidget(buttonWidget, 0, Qt::AlignRight);
196 
197  // Hide log table
198  m_logTableWidget->hide();
199 
200  // Connect buttons
201  QObject::connect(okButton, SIGNAL(clicked()), m_dialog, SLOT(accept()));
202  QObject::connect(cancelButton, SIGNAL(clicked()), m_dialog, SLOT(reject()));
203  QObject::connect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(displayLogs(int)));
204 
205  // Show dialog and return result
206  bool result = m_dialog->exec();
207 
208  // Disconnect buttons
209  QObject::disconnect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(displayLogs(int)));
210 
211  return result;
212 }
213 
214 //------------------------------------------------------------------------------
215 
217 {
218  int width = m_dialog->size().width();
219 
220  if(state)
221  {
222  m_logTableWidget->show();
223  }
224  else
225  {
226  m_logTableWidget->hide();
227  }
228 
229  m_dialog->adjustSize();
230  m_dialog->resize(width, m_dialog->baseSize().height());
231 }
232 
233 //------------------------------------------------------------------------------
234 
235 } // namespace dialog
236 } // namespace fwGuiQt
237 
FWGUIQT_API LoggerDialog(::fwGui::GuiBaseObject::Key key)
Constructor.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
virtual FWGUIQT_API bool show() override
Show the dialog and return whether the user has selected the Ok or Cancel button. ...
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...
Definition: WindowLevel.hpp:32
void displayLogs(int state)
Slot called when the user wants to display the logs.
virtual FWGUIQT_API ~LoggerDialog()
Destructor.
LoggerDialog allowing the choice of an element among severals (_selections)
static FWGUI_API const FactoryRegistryKeyType REGISTRY_KEY
This unique key should be used for all factory for specific Selector(qt,wx,...)
virtual FWGUIQT_API void setMessage(const std::string &message) override
Set the dialog message.
virtual FWGUIQT_API void setTitle(const std::string &title) override
Set the dialog title.
virtual FWGUIQT_API void setLogger(const ::fwLog::Logger::sptr &logger) override
Set the dialog logger.