File indexing completed on 2024-05-12 04:55:02

0001 /**
0002  * \file messagedialog.cpp
0003  * Message dialog.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 18 Aug 2011
0008  *
0009  * Copyright (C) 2011-2018  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "messagedialog.h"
0028 #include <QLabel>
0029 #include <QTextEdit>
0030 #include <QDialogButtonBox>
0031 #include <QVBoxLayout>
0032 #include <QStyle>
0033 #include <QIcon>
0034 #include <QPushButton>
0035 
0036 /**
0037  * Constructor.
0038  *
0039  * @param parent parent widget
0040  */
0041 MessageDialog::MessageDialog(QWidget* parent)
0042   : QDialog(parent)
0043 {
0044   setObjectName(QLatin1String("MessageDialog"));
0045   auto vlayout = new QVBoxLayout(this);
0046 
0047   auto hlayout = new QHBoxLayout;
0048   m_iconLabel = new QLabel;
0049   m_iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0050   hlayout->addWidget(m_iconLabel);
0051   m_textLabel = new QLabel;
0052   m_textLabel->setWordWrap(true);
0053   m_textLabel->setMinimumSize(50, 50);
0054   hlayout->addWidget(m_textLabel);
0055   vlayout->addLayout(hlayout);
0056 
0057   m_textEdit = new QTextEdit;
0058   m_textEdit->setFocusPolicy(Qt::NoFocus);
0059   m_textEdit->setReadOnly(true);
0060   m_textEdit->hide();
0061   vlayout->addWidget(m_textEdit);
0062 
0063   m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
0064   m_buttonBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
0065   connect(m_buttonBox, &QDialogButtonBox::clicked,
0066           this, &MessageDialog::buttonClicked);
0067   vlayout->addWidget(m_buttonBox);
0068 }
0069 
0070 /**
0071  * Set the text to be displayed.
0072  *
0073  * @param text message text.
0074  */
0075 void MessageDialog::setText(const QString &text)
0076 {
0077   m_textLabel->setText(text);
0078 }
0079 
0080 /**
0081  * Set the informative text.
0082    * This text can be large and is displayed in a text edit.
0083  *
0084  * @param text message text.
0085  */
0086 void MessageDialog::setInformativeText(const QString& text)
0087 {
0088   m_textEdit->setText(text);
0089   const QStringList lines = text.split(QLatin1Char('\n'));
0090   QFontMetrics fm(m_textEdit->fontMetrics());
0091   int maxWidth = 0;
0092   for (const QString& line : lines) {
0093 #if QT_VERSION >= 0x050b00
0094     int lineWidth = fm.horizontalAdvance(line);
0095 #else
0096     int lineWidth = fm.width(line);
0097 #endif
0098     if (maxWidth < lineWidth) {
0099       maxWidth = lineWidth;
0100     }
0101   }
0102 #if QT_VERSION >= 0x050b00
0103   maxWidth += fm.horizontalAdvance(QLatin1String("WW")); // some space for the borders
0104 #else
0105   maxWidth += fm.width(QLatin1String("WW")); // some space for the borders
0106 #endif
0107 
0108   if (maxWidth <= 1000) {
0109     m_textEdit->setMinimumWidth(maxWidth);
0110     m_textEdit->setWordWrapMode(QTextOption::NoWrap);
0111   } else {
0112     m_textEdit->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
0113   }
0114 
0115   if (text.isEmpty()) {
0116     m_textEdit->hide();
0117   } else {
0118     m_textEdit->show();
0119   }
0120 }
0121 
0122 /**
0123  * Set the message box's icon.
0124  *
0125  * @param icon icon to be displayed
0126  */
0127 void MessageDialog::setIcon(QMessageBox::Icon icon)
0128 {
0129   QStyle::StandardPixmap sp = QStyle::SP_MessageBoxQuestion;
0130   bool hasIcon = true;
0131   switch (icon) {
0132   case QMessageBox::Question:
0133     sp = QStyle::SP_MessageBoxQuestion;
0134     break;
0135   case QMessageBox::Information:
0136     sp = QStyle::SP_MessageBoxInformation;
0137     break;
0138   case QMessageBox::Warning:
0139     sp = QStyle::SP_MessageBoxWarning;
0140     break;
0141   case QMessageBox::Critical:
0142     sp = QStyle::SP_MessageBoxCritical;
0143     break;
0144   case QMessageBox::NoIcon:
0145   default:
0146     hasIcon = false;
0147   }
0148   if (hasIcon) {
0149     QStyle* widgetStyle = style();
0150     int iconSize = widgetStyle->pixelMetric(
0151           QStyle::PM_MessageBoxIconSize, nullptr, this);
0152     m_iconLabel->setPixmap(widgetStyle->standardIcon(sp, nullptr, this)
0153                            .pixmap(iconSize, iconSize));
0154   } else {
0155     m_iconLabel->setPixmap(QPixmap());
0156   }
0157 }
0158 
0159 /**
0160  * Set buttons to be displayed.
0161  *
0162  * @param buttons buttons to be displayed
0163  */
0164 void MessageDialog::setStandardButtons(QMessageBox::StandardButtons buttons)
0165 {
0166   m_buttonBox->setStandardButtons(
0167         QDialogButtonBox::StandardButtons(static_cast<int>(buttons)));
0168 }
0169 
0170 /**
0171  * Set default button.
0172  *
0173  * @param button button which gets default focus
0174  */
0175 void MessageDialog::setDefaultButton(QMessageBox::StandardButton button)
0176 {
0177   m_buttonBox->button(static_cast<QDialogButtonBox::StandardButton>(button))->
0178       setDefault(true);
0179 }
0180 
0181 /**
0182  * Called when a button is clicked.
0183  *
0184  * @param button button which was clicked
0185  */
0186 void MessageDialog::buttonClicked(QAbstractButton* button)
0187 {
0188   done(m_buttonBox->standardButton(button));
0189 }
0190 
0191 /**
0192  * Display a modal dialog with a list of items.
0193  *
0194  * @param parent parent widget
0195  * @param title dialog title
0196  * @param text dialog text
0197  * @param list list of items
0198  * @param buttons buttons shown
0199  *
0200  * @return QMessageBox::StandardButton code of pressed button.
0201  */
0202 int MessageDialog::warningList(
0203   QWidget* parent, const QString& title,
0204   const QString& text, const QStringList& list,
0205   QMessageBox::StandardButtons buttons)
0206 {
0207   MessageDialog dialog(parent);
0208   dialog.setWindowTitle(title);
0209   dialog.setText(text);
0210   dialog.setInformativeText(list.join(QLatin1String("\n")));
0211   dialog.setIcon(QMessageBox::Warning);
0212   dialog.setStandardButtons(buttons);
0213   return dialog.exec();
0214 }