rssguard/src/gui/messagebox.cpp

130 lines
4.2 KiB
C++
Raw Normal View History

2014-02-26 07:41:40 +01:00
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2014 by Martin Rotter <rotter.martinos@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
2014-01-20 13:59:08 +01:00
#include "gui/messagebox.h"
2014-03-28 09:04:36 +01:00
#include "miscellaneous/iconfactory.h"
2014-01-22 16:59:39 +01:00
2014-01-20 13:59:08 +01:00
#include <QDialogButtonBox>
#include <QtGlobal>
#include <QPushButton>
2014-01-22 16:59:39 +01:00
#include <QDialogButtonBox>
#include <QStyle>
#include <QApplication>
2014-01-20 13:59:08 +01:00
2014-01-22 17:12:57 +01:00
MessageBox::MessageBox(QWidget *parent) : QMessageBox(parent) {
}
MessageBox::~MessageBox() {
}
void MessageBox::setIcon(QMessageBox::Icon icon) {
// Determine correct status icon size.
int icon_size = qApp->style()->pixelMetric(QStyle::PM_MessageBoxIconSize,
0,
this);
// Setup status icon.
setIconPixmap(iconForStatus(icon).pixmap(icon_size,
icon_size));
2014-01-20 13:59:08 +01:00
}
2014-01-22 16:59:39 +01:00
void MessageBox::iconify(QDialogButtonBox *button_box) {
foreach (QAbstractButton *button, button_box->buttons()) {
button->setIcon(iconForRole(button_box->standardButton(button)));
}
}
QIcon MessageBox::iconForRole(QDialogButtonBox::StandardButton button) {
switch (button) {
case QMessageBox::Ok:
2014-03-28 09:04:36 +01:00
return IconFactory::instance()->fromTheme("dialog-ok");
2014-01-22 16:59:39 +01:00
2014-01-22 17:32:00 +01:00
case QMessageBox::Cancel:
2014-01-22 20:29:05 +01:00
case QMessageBox::Close:
2014-03-28 09:04:36 +01:00
return IconFactory::instance()->fromTheme("dialog-cancel");
2014-01-22 17:32:00 +01:00
2014-01-22 16:59:39 +01:00
case QMessageBox::Yes:
case QMessageBox::YesToAll:
2014-03-28 09:04:36 +01:00
return IconFactory::instance()->fromTheme("dialog-yes");
2014-01-22 16:59:39 +01:00
case QMessageBox::No:
case QMessageBox::NoToAll:
2014-03-28 09:04:36 +01:00
return IconFactory::instance()->fromTheme("dialog-no");
2014-01-22 16:59:39 +01:00
2014-01-31 09:41:19 +01:00
case QMessageBox::Help:
2014-03-28 09:04:36 +01:00
return IconFactory::instance()->fromTheme("dialog-question");
2014-01-31 09:41:19 +01:00
2014-01-22 16:59:39 +01:00
default:
return QIcon();
}
}
QIcon MessageBox::iconForStatus(QMessageBox::Icon status) {
switch (status) {
case QMessageBox::Information:
2014-03-28 09:04:36 +01:00
return IconFactory::instance()->fromTheme("dialog-information");
2014-01-22 16:59:39 +01:00
case QMessageBox::Warning:
2014-03-28 09:04:36 +01:00
return IconFactory::instance()->fromTheme("dialog-warning");
2014-01-22 16:59:39 +01:00
case QMessageBox::Critical:
2014-03-28 09:04:36 +01:00
return IconFactory::instance()->fromTheme("dialog-error");
2014-01-22 16:59:39 +01:00
case QMessageBox::Question:
2014-03-28 09:04:36 +01:00
return IconFactory::instance()->fromTheme("dialog-question");
2014-01-22 16:59:39 +01:00
case QMessageBox::NoIcon:
default:
return QIcon();
2014-01-20 13:59:08 +01:00
}
2014-01-22 16:59:39 +01:00
}
QMessageBox::StandardButton MessageBox::show(QWidget *parent,
QMessageBox::Icon icon,
const QString &title,
const QString &text,
2014-01-22 17:32:00 +01:00
const QString &informative_text,
2014-01-22 20:29:05 +01:00
const QString &detailed_text,
2014-01-22 16:59:39 +01:00
QMessageBox::StandardButtons buttons,
2014-01-22 17:32:00 +01:00
QMessageBox::StandardButton default_button) {
2014-01-22 16:59:39 +01:00
// Create and find needed components.
2014-01-22 17:12:57 +01:00
MessageBox msg_box(parent);
2014-01-22 16:59:39 +01:00
// Initialize message box properties.
msg_box.setWindowTitle(title);
msg_box.setText(text);
2014-01-22 17:32:00 +01:00
msg_box.setInformativeText(informative_text);
2014-01-22 20:29:05 +01:00
msg_box.setDetailedText(detailed_text);
2014-01-22 17:12:57 +01:00
msg_box.setIcon(icon);
2014-01-22 16:59:39 +01:00
msg_box.setStandardButtons(buttons);
2014-01-22 17:32:00 +01:00
msg_box.setDefaultButton(default_button);
2014-01-22 16:59:39 +01:00
2014-01-22 17:32:00 +01:00
// Setup button box icons.
2014-01-22 20:29:05 +01:00
#if !defined(Q_OS_WIN)
QDialogButtonBox *button_box = msg_box.findChild<QDialogButtonBox*>();
2014-01-22 16:59:39 +01:00
iconify(button_box);
2014-01-22 20:29:05 +01:00
#endif
2014-01-22 16:59:39 +01:00
// Display it.
if (msg_box.exec() == -1) {
2014-01-20 13:59:08 +01:00
return QMessageBox::Cancel;
2014-01-22 16:59:39 +01:00
}
else {
return msg_box.standardButton(msg_box.clickedButton());
}
2014-01-20 13:59:08 +01:00
}