jtdx/JTDXMessageBox.cpp

138 lines
6.4 KiB
C++
Raw Normal View History

2020-04-11 13:39:03 +03:00
//
// JTDXMessageBox - wrap the Qt QMessageBox class to give a more platform
// neutral and functional interface and translation of buttons
//
//Created by Arvo, ES1JA 2020
2020-04-08 08:42:11 +03:00
#include "JTDXMessageBox.hpp"
2020-04-07 20:32:57 +03:00
#include <QDialogButtonBox>
#include <QPushButton>
#include "revision_utils.hpp"
2020-04-08 08:42:11 +03:00
JTDXMessageBox::JTDXMessageBox (QWidget * parent)
2020-04-07 20:32:57 +03:00
: QMessageBox {parent}
{
setWindowTitle ("JTDX");
}
2020-04-08 08:42:11 +03:00
JTDXMessageBox::JTDXMessageBox (Icon icon, QString const& text, StandardButtons buttons
2020-04-07 20:32:57 +03:00
, QWidget * parent, Qt::WindowFlags flags)
: QMessageBox {icon, "JTDX", text, buttons, parent, flags}
{
}
2020-04-08 08:42:11 +03:00
void JTDXMessageBox::about_message (QWidget * parent, QString const& text)
2020-04-07 20:32:57 +03:00
{
QMessageBox::about (parent, "JTDX", text);
}
2020-04-08 08:42:11 +03:00
void JTDXMessageBox::about_Qt_message (QWidget * parent)
2020-04-07 20:32:57 +03:00
{
QMessageBox::aboutQt (parent, "JTDX");
}
2020-04-08 08:42:11 +03:00
void JTDXMessageBox::translate_buttons()
2020-04-07 20:32:57 +03:00
{
StandardButtons buttons = standardButtons();
2020-04-08 08:42:11 +03:00
if (buttons & JTDXMessageBox::Ok) button(JTDXMessageBox::Ok)->setText(tr("&OK"));
if (buttons & JTDXMessageBox::Save) button(JTDXMessageBox::Save)->setText(tr("Save"));
if (buttons & JTDXMessageBox::SaveAll) button(JTDXMessageBox::SaveAll)->setText(tr("Save All"));
if (buttons & JTDXMessageBox::Open) button(JTDXMessageBox::Open)->setText(tr("Open"));
if (buttons & JTDXMessageBox::Yes) button(JTDXMessageBox::Yes)->setText(tr("&Yes"));
if (buttons & JTDXMessageBox::YesToAll) button(JTDXMessageBox::YesToAll)->setText(tr("Yes to &All"));
if (buttons & JTDXMessageBox::No) button(JTDXMessageBox::No)->setText(tr("&No"));
if (buttons & JTDXMessageBox::NoToAll) button(JTDXMessageBox::NoToAll)->setText(tr("N&o to All"));
if (buttons & JTDXMessageBox::Abort) button(JTDXMessageBox::Abort)->setText(tr("Abort"));
if (buttons & JTDXMessageBox::Retry) button(JTDXMessageBox::Retry)->setText(tr("&Retry"));
if (buttons & JTDXMessageBox::Ignore) button(JTDXMessageBox::Ignore)->setText(tr("Ignore"));
if (buttons & JTDXMessageBox::Close) button(JTDXMessageBox::Close)->setText(tr("Close"));
if (buttons & JTDXMessageBox::Cancel) button(JTDXMessageBox::Cancel)->setText(tr("&Cancel"));
if (buttons & JTDXMessageBox::Discard) button(JTDXMessageBox::Discard)->setText(tr("Discard"));
if (buttons & JTDXMessageBox::Help) button(JTDXMessageBox::Help)->setText(tr("Help"));
if (buttons & JTDXMessageBox::Apply) button(JTDXMessageBox::Apply)->setText(tr("Apply"));
if (buttons & JTDXMessageBox::Reset) button(JTDXMessageBox::Reset)->setText(tr("Reset"));
if (buttons & JTDXMessageBox::RestoreDefaults) button(JTDXMessageBox::RestoreDefaults)->setText(tr("Restore Defaults"));
if (buttons & JTDXMessageBox::NoButton) button(JTDXMessageBox::NoButton)->setText("");
2020-04-07 20:32:57 +03:00
}
2020-04-08 08:42:11 +03:00
JTDXMessageBox::StandardButton JTDXMessageBox::show_it (QWidget * parent, JTDXMessageBox::Icon icon
2020-04-07 20:32:57 +03:00
, QString const& title
, QString const& text
, QString const& informative
, QString const& detail
2020-04-08 08:42:11 +03:00
, JTDXMessageBox::StandardButtons buttons
, JTDXMessageBox::StandardButton default_button)
2020-04-07 20:32:57 +03:00
{
2020-04-08 08:42:11 +03:00
JTDXMessageBox mb {icon, text, JTDXMessageBox::NoButton, parent};
2020-04-07 20:32:57 +03:00
if (!title.isEmpty()) mb.setWindowTitle(title);
QDialogButtonBox * button_box = mb.findChild<QDialogButtonBox *> ();
Q_ASSERT (button_box);
2020-04-08 08:42:11 +03:00
uint mask = JTDXMessageBox::FirstButton;
while (mask <= JTDXMessageBox::LastButton) {
2020-04-07 20:32:57 +03:00
uint sb = buttons & mask;
mask <<= 1;
if (!sb)
continue;
2020-04-08 08:42:11 +03:00
QPushButton * button = mb.addButton (static_cast<JTDXMessageBox::StandardButton> (sb));
2020-04-07 20:32:57 +03:00
// Choose the first accept role as the default
if (mb.defaultButton ())
continue;
2020-04-08 08:42:11 +03:00
if ((default_button == JTDXMessageBox::NoButton
2020-04-07 20:32:57 +03:00
&& button_box->buttonRole (button) == QDialogButtonBox::AcceptRole)
2020-04-08 08:42:11 +03:00
|| (default_button != JTDXMessageBox::NoButton
2020-04-07 20:32:57 +03:00
&& sb == static_cast<uint> (default_button)))
mb.setDefaultButton (button);
}
mb.setInformativeText (informative);
mb.setDetailedText (detail);
QMessageBox::tr("Show Details...");
QMessageBox::tr("Hide Details...");
2020-04-07 20:32:57 +03:00
mb.translate_buttons();
if (mb.exec() == -1)
2020-04-08 08:42:11 +03:00
return JTDXMessageBox::Cancel;
2020-04-07 20:32:57 +03:00
return mb.standardButton (mb.clickedButton ());
}
2020-04-08 08:42:11 +03:00
auto JTDXMessageBox::information_message (QWidget * parent, QString const& title
2020-04-07 20:32:57 +03:00
, QString const& text
, QString const& informative
, QString const& detail
, StandardButtons buttons
, StandardButton default_button) -> StandardButton
{
return show_it (parent, Information, title, text, informative, detail, buttons, default_button);
}
2020-04-08 08:42:11 +03:00
auto JTDXMessageBox::query_message (QWidget * parent, QString const& title
2020-04-07 20:32:57 +03:00
, QString const& text
, QString const& informative
, QString const& detail
, StandardButtons buttons
, StandardButton default_button) -> StandardButton
{
return show_it (parent, Question, title, text, informative, detail, buttons, default_button);
}
2020-04-08 08:42:11 +03:00
auto JTDXMessageBox::warning_message (QWidget * parent, QString const& title
2020-04-07 20:32:57 +03:00
, QString const& text
, QString const& informative
, QString const& detail
, StandardButtons buttons
, StandardButton default_button) -> StandardButton
{
return show_it (parent, Warning, title, text, informative, detail, buttons, default_button);
}
2020-04-08 08:42:11 +03:00
auto JTDXMessageBox::critical_message (QWidget * parent, QString const& title
2020-04-07 20:32:57 +03:00
, QString const& text
, QString const& informative
, QString const& detail
, StandardButtons buttons
, StandardButton default_button) -> StandardButton
{
return show_it (parent, Critical, title, text, informative, detail, buttons, default_button);
}