BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
custommessagebox.cpp
1 /*
2  Copyright (c) 2012, BuildmLearn Contributors listed at http://buildmlearn.org/people/
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 
11  * Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  * Neither the name of the BuildmLearn nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #include "gui/custommessagebox.h"
32 
33 #include "miscellaneous/iconfactory.h"
34 #include "miscellaneous/application.h"
35 
36 #include <QtGlobal>
37 #include <QDialogButtonBox>
38 #include <QPushButton>
39 #include <QDialogButtonBox>
40 #include <QStyle>
41 
42 
43 CustomMessageBox::CustomMessageBox(QWidget *parent) : QMessageBox(parent) {
44 }
45 
46 CustomMessageBox::~CustomMessageBox() {
47 }
48 
49 void CustomMessageBox::setIcon(QMessageBox::Icon icon) {
50  // Determine correct status icon size.
51  int icon_size = qApp->style()->pixelMetric(QStyle::PM_MessageBoxIconSize, 0, this);
52  // Setup status icon.
53  setIconPixmap(iconForStatus(icon).pixmap(icon_size, icon_size));
54 }
55 
56 void CustomMessageBox::iconify(QDialogButtonBox *button_box) {
57  foreach (QAbstractButton *button, button_box->buttons()) {
58  button->setIcon(iconForRole(button_box->standardButton(button)));
59  }
60 }
61 
62 QIcon CustomMessageBox::iconForRole(QDialogButtonBox::StandardButton button) {
63  switch (button) {
64  case QMessageBox::Ok:
65  return IconFactory::instance()->fromTheme("dialog-ok");
66 
67  case QMessageBox::Cancel:
68  case QMessageBox::Close:
69  return IconFactory::instance()->fromTheme("dialog-cancel");
70 
71  case QMessageBox::Yes:
72  case QMessageBox::YesToAll:
73  return IconFactory::instance()->fromTheme("dialog-yes");
74 
75  case QMessageBox::No:
76  case QMessageBox::NoToAll:
77  return IconFactory::instance()->fromTheme("dialog-no");
78 
79  case QMessageBox::Help:
80  return IconFactory::instance()->fromTheme("dialog-question");
81 
82  default:
83  return QIcon();
84  }
85 }
86 
87 QIcon CustomMessageBox::iconForStatus(QMessageBox::Icon status) {
88  switch (status) {
89  case QMessageBox::Information:
90  return IconFactory::instance()->fromTheme("dialog-information");
91 
92  case QMessageBox::Warning:
93  return IconFactory::instance()->fromTheme("dialog-warning");
94 
95  case QMessageBox::Critical:
96  return IconFactory::instance()->fromTheme("dialog-error");
97 
98  case QMessageBox::Question:
99  return IconFactory::instance()->fromTheme("dialog-question");
100 
101  case QMessageBox::NoIcon:
102  default:
103  return QIcon();
104  }
105 }
106 
107 QMessageBox::StandardButton CustomMessageBox::show(QWidget *parent,
108  QMessageBox::Icon icon,
109  const QString &title,
110  const QString &text,
111  const QString &informative_text,
112  const QString &detailed_text,
113  QMessageBox::StandardButtons buttons,
114  QMessageBox::StandardButton default_button) {
115  // Create and find needed components.
116  CustomMessageBox msg_box(parent);
117 
118  // Initialize message box properties.
119  msg_box.setWindowTitle(title);
120  msg_box.setText(text);
121  msg_box.setInformativeText(informative_text);
122  msg_box.setDetailedText(detailed_text);
123  msg_box.setIcon(icon);
124  msg_box.setStandardButtons(buttons);
125  msg_box.setDefaultButton(default_button);
126 
127  // Setup button box icons.
128 #if defined(Q_OS_OS2)
129  QDialogButtonBox *button_box = msg_box.findChild<QDialogButtonBox*>();
130  iconify(button_box);
131 #endif
132 
133  // Display it.
134  if (msg_box.exec() == -1) {
135  return QMessageBox::Cancel;
136  }
137  else {
138  return msg_box.standardButton(msg_box.clickedButton());
139  }
140 }
static void iconify(QDialogButtonBox *button_box)
Performs icon replacements for given button box.
Custom message boxes.
static QMessageBox::StandardButton show(QWidget *parent, QMessageBox::Icon icon, const QString &title, const QString &text, const QString &informative_text=QString(), const QString &detailed_text=QString(), QMessageBox::StandardButtons buttons=QMessageBox::Ok, QMessageBox::StandardButton default_button=QMessageBox::Ok)
Displays custom message box.
void setIcon(Icon icon)
Custom icon setting.
QIcon fromTheme(const QString &name)
Returns icon from active theme.
Definition: iconfactory.h:58
CustomMessageBox(QWidget *parent=0)
Constructor.
static IconFactory * instance()
Singleton getter.
Definition: iconfactory.cpp:48