/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtWidgets>#include "dialog.h"#define MESSAGE \
Dialog::tr("<p>Message boxes have a caption, a text, " \
"and any number of buttons, each with standard or custom texts." \
"<p>Click a button to close the message box. Pressing the Esc button " \
"will activate the detected escape button (if any).")#define MESSAGE_DETAILS \
Dialog::tr("If a message box has detailed text, the user can reveal it " \
"by pressing the Show Details... button.")class DialogOptionsWidget : publicQGroupBox
{
public:
explicit DialogOptionsWidget(QWidget*parent =0);
void addCheckBox(constQString&text,int value);
void addSpacer();
int value() const;
private:
typedefQPair<QCheckBox*,int> CheckBoxEntry;
QVBoxLayout*layout;
QList<CheckBoxEntry> checkBoxEntries;
};
DialogOptionsWidget::DialogOptionsWidget(QWidget*parent) :
QGroupBox(parent) , layout(newQVBoxLayout)
{
setTitle(Dialog::tr("Options"));
setLayout(layout);
}
void DialogOptionsWidget::addCheckBox(constQString&text,int value)
{
QCheckBox*checkBox =newQCheckBox(text);
layout->addWidget(checkBox);
checkBoxEntries.append(CheckBoxEntry(checkBox, value));
}
void DialogOptionsWidget::addSpacer()
{
layout->addItem(newQSpacerItem(0,0,QSizePolicy::Ignored,QSizePolicy::MinimumExpanding));
}
int DialogOptionsWidget::value() const
{
int result =0;
foreach (const CheckBoxEntry &checkboxEntry, checkBoxEntries)
if (checkboxEntry.first->isChecked())
result |= checkboxEntry.second;
return result;
}
Dialog::Dialog(QWidget*parent)
: QWidget(parent)
{
QVBoxLayout*verticalLayout;
if (QGuiApplication::styleHints()->showIsFullScreen() ||QGuiApplication::styleHints()->showIsMaximized()) {
QHBoxLayout*horizontalLayout =newQHBoxLayout(this);
QGroupBox*groupBox =newQGroupBox(QGuiApplication::applicationDisplayName(),this);
horizontalLayout->addWidget(groupBox);
verticalLayout =newQVBoxLayout(groupBox);
} else {
verticalLayout =newQVBoxLayout(this);
}
QToolBox*toolbox =newQToolBox;
verticalLayout->addWidget(toolbox);
errorMessageDialog =newQErrorMessage(this);
int frameStyle =QFrame::Sunken |QFrame::Panel;
integerLabel =newQLabel;
integerLabel->setFrameStyle(frameStyle);
QPushButton*integerButton =newQPushButton(tr("QInputDialog::get&Int()"));
doubleLabel =newQLabel;
doubleLabel->setFrameStyle(frameStyle);
QPushButton*doubleButton =newQPushButton(tr("QInputDialog::get&Double()"));
itemLabel =newQLabel;
itemLabel->setFrameStyle(frameStyle);
QPushButton*itemButton =newQPushButton(tr("QInputDialog::getIte&m()"));
textLabel =newQLabel;
textLabel->setFrameStyle(frameStyle);
QPushButton*textButton =newQPushButton(tr("QInputDialog::get&Text()"));
multiLineTextLabel =newQLabel;
multiLineTextLabel->setFrameStyle(frameStyle);
QPushButton*multiLineTextButton =newQPushButton(tr("QInputDialog::get&MultiLineText()"));
colorLabel =newQLabel;
colorLabel->setFrameStyle(frameStyle);
QPushButton*colorButton =newQPushButton(tr("QColorDialog::get&Color()"));
fontLabel =newQLabel;
fontLabel->setFrameStyle(frameStyle);
QPushButton*fontButton =newQPushButton(tr("QFontDialog::get&Font()"));
directoryLabel =newQLabel;
directoryLabel->setFrameStyle(frameStyle);
QPushButton*directoryButton =newQPushButton(tr("QFileDialog::getE&xistingDirectory()"));
openFileNameLabel =newQLabel;
openFileNameLabel->setFrameStyle(frameStyle);
QPushButton*openFileNameButton =newQPushButton(tr("QFileDialog::get&OpenFileName()"));
openFileNamesLabel =newQLabel;
openFileNamesLabel->setFrameStyle(frameStyle);
QPushButton*openFileNamesButton =newQPushButton(tr("QFileDialog::&getOpenFileNames()"));
saveFileNameLabel =newQLabel;
saveFileNameLabel->setFrameStyle(frameStyle);
QPushButton*saveFileNameButton =newQPushButton(tr("QFileDialog::get&SaveFileName()"));
criticalLabel =newQLabel;
criticalLabel->setFrameStyle(frameStyle);
QPushButton*criticalButton =newQPushButton(tr("QMessageBox::critica&l()"));
informationLabel =newQLabel;
informationLabel->setFrameStyle(frameStyle);
QPushButton*informationButton =newQPushButton(tr("QMessageBox::i&nformation()"));
questionLabel =newQLabel;
questionLabel->setFrameStyle(frameStyle);
QPushButton*questionButton =newQPushButton(tr("QMessageBox::&question()"));
warningLabel =newQLabel;
warningLabel->setFrameStyle(frameStyle);
QPushButton*warningButton =newQPushButton(tr("QMessageBox::&warning()"));
errorLabel =newQLabel;
errorLabel->setFrameStyle(frameStyle);
QPushButton*errorButton =newQPushButton(tr("QErrorMessage::showM&essage()"));
connect(integerButton,&QAbstractButton::clicked,this,&Dialog::setInteger);
connect(doubleButton,&QAbstractButton::clicked,this,&Dialog::setDouble);
connect(itemButton,&QAbstractButton::clicked,this,&Dialog::setItem);
connect(textButton,&QAbstractButton::clicked,this,&Dialog::setText);
connect(multiLineTextButton,&QAbstractButton::clicked,this,&Dialog::setMultiLineText);
connect(colorButton,&QAbstractButton::clicked,this,&Dialog::setColor);
connect(fontButton,&QAbstractButton::clicked,this,&Dialog::setFont);
connect(directoryButton,&QAbstractButton::clicked,this,&Dialog::setExistingDirectory);
connect(openFileNameButton,&QAbstractButton::clicked,this,&Dialog::setOpenFileName);
connect(openFileNamesButton,&QAbstractButton::clicked,this,&Dialog::setOpenFileNames);
connect(saveFileNameButton,&QAbstractButton::clicked,this,&Dialog::setSaveFileName);
connect(criticalButton,&QAbstractButton::clicked,this,&Dialog::criticalMessage);
connect(informationButton,&QAbstractButton::clicked,this,&Dialog::informationMessage);
connect(questionButton,&QAbstractButton::clicked,this,&Dialog::questionMessage);
connect(warningButton,&QAbstractButton::clicked,this,&Dialog::warningMessage);
connect(errorButton,&QAbstractButton::clicked,this,&Dialog::errorMessage);
QWidget*page =newQWidget;
QGridLayout*layout =newQGridLayout(page);
layout->setColumnStretch(1,1);
layout->setColumnMinimumWidth(1,250);
layout->addWidget(integerButton,0,0);
layout->addWidget(integerLabel,0,1);
layout->addWidget(doubleButton,1,0);
layout->addWidget(doubleLabel,1,1);
layout->addWidget(itemButton,2,0);
layout->addWidget(itemLabel,2,1);
layout->addWidget(textButton,3,0);
layout->addWidget(textLabel,3,1);
layout->addWidget(multiLineTextButton,4,0);
layout->addWidget(multiLineTextLabel,4,1);
layout->addItem(newQSpacerItem(0,0,QSizePolicy::Ignored,QSizePolicy::MinimumExpanding),5,0);
toolbox->addItem(page, tr("Input Dialogs"));
constQString doNotUseNativeDialog = tr("Do not use native dialog");
page =newQWidget;
layout =newQGridLayout(page);
layout->setColumnStretch(1,1);
layout->addWidget(colorButton,0,0);
layout->addWidget(colorLabel,0,1);
colorDialogOptionsWidget =new DialogOptionsWidget;
colorDialogOptionsWidget->addCheckBox(doNotUseNativeDialog,QColorDialog::DontUseNativeDialog);
colorDialogOptionsWidget->addCheckBox(tr("Show alpha channel") ,QColorDialog::ShowAlphaChannel);
colorDialogOptionsWidget->addCheckBox(tr("No buttons") ,QColorDialog::NoButtons);
layout->addItem(newQSpacerItem(0,0,QSizePolicy::Ignored,QSizePolicy::MinimumExpanding),1,0);
layout->addWidget(colorDialogOptionsWidget,2,0,1,2);
toolbox->addItem(page, tr("Color Dialog"));
page =newQWidget;
layout =newQGridLayout(page);
layout->setColumnStretch(1,1);
layout->addWidget(fontButton,0,0);
layout->addWidget(fontLabel,0,1);
fontDialogOptionsWidget =new DialogOptionsWidget;
fontDialogOptionsWidget->addCheckBox(doNotUseNativeDialog,QFontDialog::DontUseNativeDialog);
fontDialogOptionsWidget->addCheckBox(tr("Show scalable fonts"),QFontDialog::ScalableFonts);
fontDialogOptionsWidget->addCheckBox(tr("Show non scalable fonts"),QFontDialog::NonScalableFonts);
fontDialogOptionsWidget->addCheckBox(tr("Show monospaced fonts"),QFontDialog::MonospacedFonts);
fontDialogOptionsWidget->addCheckBox(tr("Show proportional fonts"),QFontDialog::ProportionalFonts);
fontDialogOptionsWidget->addCheckBox(tr("No buttons") ,QFontDialog::NoButtons);
layout->addItem(newQSpacerItem(0,0,QSizePolicy::Ignored,QSizePolicy::MinimumExpanding),1,0);
layout->addWidget(fontDialogOptionsWidget,2,0,1,2);
toolbox->addItem(page, tr("Font Dialog"));
page =newQWidget;
layout =newQGridLayout(page);
layout->setColumnStretch(1,1);
layout->addWidget(directoryButton,0,0);
layout->addWidget(directoryLabel,0,1);
layout->addWidget(openFileNameButton,1,0);
layout->addWidget(openFileNameLabel,1,1);
layout->addWidget(openFileNamesButton,2,0);
layout->addWidget(openFileNamesLabel,2,1);
layout->addWidget(saveFileNameButton,3,0);
layout->addWidget(saveFileNameLabel,3,1);
fileDialogOptionsWidget =new DialogOptionsWidget;
fileDialogOptionsWidget->addCheckBox(doNotUseNativeDialog,QFileDialog::DontUseNativeDialog);
fileDialogOptionsWidget->addCheckBox(tr("Show directories only"),QFileDialog::ShowDirsOnly);
fileDialogOptionsWidget->addCheckBox(tr("Do not resolve symlinks"),QFileDialog::DontResolveSymlinks);
fileDialogOptionsWidget->addCheckBox(tr("Do not confirm overwrite"),QFileDialog::DontConfirmOverwrite);
fileDialogOptionsWidget->addCheckBox(tr("Do not use sheet"),QFileDialog::DontUseSheet);
fileDialogOptionsWidget->addCheckBox(tr("Readonly"),QFileDialog::ReadOnly);
fileDialogOptionsWidget->addCheckBox(tr("Hide name filter details"),QFileDialog::HideNameFilterDetails);
fileDialogOptionsWidget->addCheckBox(tr("Do not use custom directory icons (Windows)"),QFileDialog::DontUseCustomDirectoryIcons);
layout->addItem(newQSpacerItem(0,0,QSizePolicy::Ignored,QSizePolicy::MinimumExpanding),4,0);
layout->addWidget(fileDialogOptionsWidget,5,0,1,2);
toolbox->addItem(page, tr("File Dialogs"));
page =newQWidget;
layout =newQGridLayout(page);
layout->setColumnStretch(1,1);
layout->addWidget(criticalButton,0,0);
layout->addWidget(criticalLabel,0,1);
layout->addWidget(informationButton,1,0);
layout->addWidget(informationLabel,1,1);
layout->addWidget(questionButton,2,0);
layout->addWidget(questionLabel,2,1);
layout->addWidget(warningButton,3,0);
layout->addWidget(warningLabel,3,1);
layout->addWidget(errorButton,4,0);
layout->addWidget(errorLabel,4,1);
layout->addItem(newQSpacerItem(0,0,QSizePolicy::Ignored,QSizePolicy::MinimumExpanding),5,0);
toolbox->addItem(page, tr("Message Boxes"));
setWindowTitle(QGuiApplication::applicationDisplayName());
}
void Dialog::setInteger()
{
bool ok;
int i =QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
tr("Percentage:"),25,0,100,1,&ok);
if (ok)
integerLabel->setText(tr("%1%").arg(i));
}
void Dialog::setDouble()
{
bool ok;
double d =QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"),
tr("Amount:"),37.56,-10000,10000,2,&ok);
if (ok)
doubleLabel->setText(QString("$%1").arg(d));
}
void Dialog::setItem()
{
QStringList items;
items << tr("Spring") << tr("Summer") << tr("Fall") << tr("Winter");
bool ok;
QString item =QInputDialog::getItem(this, tr("QInputDialog::getItem()"),
tr("Season:"), items,0,false,&ok);
if (ok &&!item.isEmpty())
itemLabel->setText(item);
}
void Dialog::setText()
{
bool ok;
QString text =QInputDialog::getText(this, tr("QInputDialog::getText()"),
tr("User name:"),QLineEdit::Normal,QDir::home().dirName(),&ok);
if (ok &&!text.isEmpty())
textLabel->setText(text);
}
void Dialog::setMultiLineText()
{
bool ok;
QString text =QInputDialog::getMultiLineText(this, tr("QInputDialog::getMultiLineText()"),
tr("Address:"),"John Doe\nFreedom Street",&ok);
if (ok &&!text.isEmpty())
multiLineTextLabel->setText(text);
}
void Dialog::setColor()
{
constQColorDialog::ColorDialogOptions options =QFlag(colorDialogOptionsWidget->value());
constQColor color =QColorDialog::getColor(Qt::green,this,"Select Color", options);
if (color.isValid()) {
colorLabel->setText(color.name());
colorLabel->setPalette(QPalette(color));
colorLabel->setAutoFillBackground(true);
}
}
void Dialog::setFont()
{
constQFontDialog::FontDialogOptions options =QFlag(fontDialogOptionsWidget->value());
bool ok;
QFont font =QFontDialog::getFont(&ok,QFont(fontLabel->text()),this,"Select Font", options);
if (ok) {
fontLabel->setText(font.key());
fontLabel->setFont(font);
}
}
void Dialog::setExistingDirectory()
{
QFileDialog::Options options =QFlag(fileDialogOptionsWidget->value());
options |=QFileDialog::DontResolveSymlinks |QFileDialog::ShowDirsOnly;
QString directory =QFileDialog::getExistingDirectory(this,
tr("QFileDialog::getExistingDirectory()"),
directoryLabel->text(),
options);
if (!directory.isEmpty())
directoryLabel->setText(directory);
}
void Dialog::setOpenFileName()
{
constQFileDialog::Options options =QFlag(fileDialogOptionsWidget->value());
QString selectedFilter;
QString fileName =QFileDialog::getOpenFileName(this,
tr("QFileDialog::getOpenFileName()"),
openFileNameLabel->text(),
tr("All Files (*);;Text Files (*.txt)"),&selectedFilter,
options);
if (!fileName.isEmpty())
openFileNameLabel->setText(fileName);
}
void Dialog::setOpenFileNames()
{
constQFileDialog::Options options =QFlag(fileDialogOptionsWidget->value());
QString selectedFilter;
QStringList files =QFileDialog::getOpenFileNames(
this, tr("QFileDialog::getOpenFileNames()"),
openFilesPath,
tr("All Files (*);;Text Files (*.txt)"),&selectedFilter,
options);
if (files.count()) {
openFilesPath = files[0];
openFileNamesLabel->setText(QString("[%1]").arg(files.join(", ")));
}
}
void Dialog::setSaveFileName()
{
constQFileDialog::Options options =QFlag(fileDialogOptionsWidget->value());
QString selectedFilter;
QString fileName =QFileDialog::getSaveFileName(this,
tr("QFileDialog::getSaveFileName()"),
saveFileNameLabel->text(),
tr("All Files (*);;Text Files (*.txt)"),&selectedFilter,
options);
if (!fileName.isEmpty())
saveFileNameLabel->setText(fileName);
}
void Dialog::criticalMessage()
{
QMessageBox::StandardButton reply;
reply =QMessageBox::critical(this, tr("QMessageBox::critical()"),
MESSAGE,QMessageBox::Abort |QMessageBox::Retry |QMessageBox::Ignore);
if (reply ==QMessageBox::Abort)
criticalLabel->setText(tr("Abort"));
elseif (reply ==QMessageBox::Retry)
criticalLabel->setText(tr("Retry"));
else
criticalLabel->setText(tr("Ignore"));
}
void Dialog::informationMessage()
{
QMessageBox::StandardButton reply;
reply =QMessageBox::information(this, tr("QMessageBox::information()"), MESSAGE);
if (reply ==QMessageBox::Ok)
informationLabel->setText(tr("OK"));
else
informationLabel->setText(tr("Escape"));
}
void Dialog::questionMessage()
{
QMessageBox::StandardButton reply;
reply =QMessageBox::question(this, tr("QMessageBox::question()"),
MESSAGE,QMessageBox::Yes |QMessageBox::No |QMessageBox::Cancel);
if (reply ==QMessageBox::Yes)
questionLabel->setText(tr("Yes"));
elseif (reply ==QMessageBox::No)
questionLabel->setText(tr("No"));
else
questionLabel->setText(tr("Cancel"));
}
void Dialog::warningMessage()
{
QMessageBox msgBox(QMessageBox::Warning, tr("QMessageBox::warning()"),
MESSAGE,0,this);
msgBox.setDetailedText(MESSAGE_DETAILS);
msgBox.addButton(tr("Save &Again"),QMessageBox::AcceptRole);
msgBox.addButton(tr("&Continue"),QMessageBox::RejectRole);
if (msgBox.exec() ==QMessageBox::AcceptRole)
warningLabel->setText(tr("Save Again"));
else
warningLabel->setText(tr("Continue"));
}
void Dialog::errorMessage()
{
errorMessageDialog->showMessage(
tr("This dialog shows and remembers error messages. ""If the checkbox is checked (as it is by default), ""the shown message will be shown again, ""but if the user unchecks the box the message ""will not appear again if QErrorMessage::showMessage() ""is called with the same message."));
errorLabel->setText(tr("If the box is unchecked, the message ""won't appear again."));
}