1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/***************************************************************************
* *
* This file is part of the Fotowall project, *
* http://www.enricoros.com/opensource/fotowall *
* *
* Copyright (C) 2009 by Enrico Ros <enrico.ros@gmail.com> *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "ButtonsDialog.h"
#include <QAbstractButton>
#include <QCheckBox>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QSettings>
#include <QVBoxLayout>
ButtonsDialog::ButtonsDialog(const QString & dialogId, const QString & title,
const QString & message, QDialogButtonBox::StandardButtons buttons,
bool buttonsCentered, bool memorize)
: m_dialogId(dialogId)
, m_memorize(false)
, m_iconLabel(0)
, m_messageLabel(0)
, m_memorizeCheckbox(0)
, m_extraLayout(0)
, m_buttonBox(0)
, m_defaultButton(QDialogButtonBox::NoButton)
, m_pressedButton(QDialogButtonBox::NoButton)
{
// create contents
QGridLayout * mainLay = new QGridLayout(this);
m_iconLabel = new QLabel(this);
m_iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
mainLay->addWidget(m_iconLabel, 0, 0, 3, 1);
m_messageLabel = new QLabel(this);
m_messageLabel->setContentsMargins(2, 0, 0, 0);
m_messageLabel->setTextFormat(Qt::RichText);
m_messageLabel->setTextInteractionFlags(Qt::NoTextInteraction);
//m_messageLabel->setWordWrap(true);
mainLay->addWidget(m_messageLabel, 0, 1, 1, 1, Qt::AlignVCenter);
m_extraLayout = new QVBoxLayout();
mainLay->addLayout(m_extraLayout, 1, 1, 1, 1);
m_memorizeCheckbox = new QCheckBox(tr("ask again next time"), this);
m_memorizeCheckbox->setChecked(true);
m_memorizeCheckbox->setVisible(m_memorize);
mainLay->addWidget(m_memorizeCheckbox, 2, 1, 1, 1);
m_buttonBox = new QDialogButtonBox(this);
connect(m_buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(slotButtonClicked(QAbstractButton *)));
m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
m_buttonBox->setFocus();
mainLay->addWidget(m_buttonBox, 3, 0, 1, 2);
// apply initial values
if (!title.isEmpty())
setTitle(title);
if (!message.isEmpty())
setMessage(message);
if (buttons != QDialogButtonBox::NoButton)
setButtons(buttons);
if (buttonsCentered)
setButtonsCentered(true);
if (memorize)
setMemorizeEnabled(true);
#if 0
// useful if the message label word-wraps, so we have to set a minimum size
// by hand
setModal(true);
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
QSize screenSize = QApplication::desktop()->availableGeometry(QCursor::pos()).size();
setMinimumWidth(qMin(screenSize.width() - 480, screenSize.width()/2));
setMinimumHeight(190);
mainLay->activate();
adjustSize();
resize(minimumSize());
#endif
}
QDialogButtonBox::StandardButton ButtonsDialog::execute()
{
// handle a previous answer if memorize is on
QString configKey = "ButtonsDialogSkip/" + m_dialogId;
if (m_memorize) {
// return the previous answer if such answer exists
QSettings s;
if (s.contains(configKey))
return (QDialogButtonBox::StandardButton)s.value(configKey).toInt();
}
// pop up the dialog
m_pressedButton = QDialogButtonBox::NoButton;
int retCode = exec();
if (retCode == QDialog::Rejected || m_pressedButton == QDialogButtonBox::NoButton)
return QDialogButtonBox::NoButton;
// save the answer if requested
if (m_memorize && !m_memorizeCheckbox->isChecked() && m_pressedButton != QDialogButtonBox::Cancel)
QSettings().setValue(configKey, (int)m_pressedButton);
// return the choosen key
return m_pressedButton;
}
void ButtonsDialog::setButtons(QDialogButtonBox::StandardButtons buttons)
{
m_buttonBox->setStandardButtons(buttons);
setDefaultButton(m_defaultButton);
foreach (QDialogButtonBox::StandardButton sb, m_buttonNames.keys())
setButtonText(sb, m_buttonNames[sb]);
}
QDialogButtonBox::StandardButtons ButtonsDialog::buttons() const
{
return m_buttonBox->standardButtons();
}
void ButtonsDialog::setButtonText(QDialogButtonBox::StandardButton sb, const QString & text)
{
m_buttonNames[sb] = text;
if (QPushButton * pButton = m_buttonBox->button(sb))
pButton->setText(text);
}
QString ButtonsDialog::buttonText(QDialogButtonBox::StandardButton sb) const
{
QPushButton * pButton = m_buttonBox->button(sb);
return pButton ? pButton->text() : QString();
}
void ButtonsDialog::setDefaultButton(QDialogButtonBox::StandardButton sb)
{
m_defaultButton = sb;
foreach (QAbstractButton * aButton, m_buttonBox->buttons())
if (QPushButton * pButton = dynamic_cast<QPushButton *>(aButton))
pButton->setDefault(m_buttonBox->standardButton(aButton) == m_defaultButton);
}
QDialogButtonBox::StandardButton ButtonsDialog::defaultButton() const
{
return m_defaultButton;
}
void ButtonsDialog::setButtonsCentered(bool centered)
{
m_buttonBox->setCenterButtons(centered);
}
bool ButtonsDialog::buttonsCentered() const
{
return m_buttonBox->centerButtons();
}
void ButtonsDialog::setMessage(const QString & message)
{
m_messageLabel->setText(message);
}
QString ButtonsDialog::message() const
{
return m_messageLabel->text();
}
void ButtonsDialog::setTitle(const QString & title)
{
setWindowTitle(title);
}
QString ButtonsDialog::title() const
{
return windowTitle();
}
void ButtonsDialog::setIcon(const QIcon & icon)
{
m_icon = icon;
setWindowIcon(m_icon);
m_iconLabel->setPixmap(m_icon.pixmap(32, 32));
}
void ButtonsDialog::setIcon(QStyle::StandardPixmap standardIcon)
{
setIcon(style()->standardIcon(standardIcon));
}
QIcon ButtonsDialog::icon() const
{
return m_icon;
}
void ButtonsDialog::addExtraWidget(QWidget * widget)
{
if (widget) {
m_extraLayout->addWidget(widget);
m_extraWidgets.append(widget);
connect(widget, SIGNAL(destroyed()), this, SLOT(slotExtraWidgetDeleted(QObject*)));
}
}
QList<QWidget *> ButtonsDialog::extraWidgets() const
{
return m_extraWidgets;
}
void ButtonsDialog::setMemorizeEnabled(bool enabled)
{
m_memorize = enabled;
m_memorizeCheckbox->setVisible(m_memorize);
}
bool ButtonsDialog::memorizeEnabled() const
{
return m_memorize;
}
void ButtonsDialog::slotButtonClicked(QAbstractButton * button)
{
// save the pressed button and return from the 'exec()'
m_pressedButton = m_buttonBox->standardButton(button);
accept();
}
void ButtonsDialog::slotExtraWidgetDeleted(QObject * object)
{
QWidget * widget = static_cast<QWidget *>(object);
m_extraWidgets.removeAll(widget);
}
|