qt 6.5.1 original

This commit is contained in:
kleuter
2023-10-29 23:33:08 +01:00
parent 71d22ab6b0
commit 85d238dfda
21202 changed files with 5499099 additions and 0 deletions

View File

@ -0,0 +1,36 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## dialogs Binary:
#####################################################################
qt_internal_add_manual_test(dialogs
GUI
SOURCES
colordialogpanel.cpp colordialogpanel.h
filedialogpanel.cpp filedialogpanel.h
fontdialogpanel.cpp fontdialogpanel.h
main.cpp
messageboxpanel.cpp messageboxpanel.h
utils.cpp utils.h
wizardpanel.cpp wizardpanel.h
LIBRARIES
Qt::Gui
Qt::Widgets
)
## Scopes:
#####################################################################
qt_internal_extend_target(dialogs CONDITION TARGET Qt::PrintSupport
LIBRARIES
Qt::PrintSupport
)
qt_internal_extend_target(dialogs CONDITION NOT (QT_FEATURE_printer EQUAL FALSE)
SOURCES
printdialogpanel.cpp printdialogpanel.h printdialogpanel.ui
ENABLE_AUTOGEN_TOOLS
uic
)

View File

@ -0,0 +1,236 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "colordialogpanel.h"
#include "utils.h"
#include <QGroupBox>
#include <QCheckBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QSortFilterProxyModel>
#include <QComboBox>
#include <QTimer>
#include <QDebug>
// SVG color keyword names provided by the World Wide Web Consortium
static inline QStringList svgColorNames()
{
return QStringList()
<< "aliceblue" << "antiquewhite" << "aqua" << "aquamarine" << "azure" << "beige" << "bisque"
<< "black" << "blanchedalmond" << "blue" << "blueviolet" << "brown" << "burlywood" << "cadetblue"
<< "chartreuse" << "chocolate" << "coral" << "cornflowerblue" << "cornsilk" << "crimson" << "cyan"
<< "darkblue" << "darkcyan" << "darkgoldenrod" << "darkgray" << "darkgreen" << "darkgrey"
<< "darkkhaki" << "darkmagenta" << "darkolivegreen" << "darkorange" << "darkorchid" << "darkred"
<< "darksalmon" << "darkseagreen" << "darkslateblue" << "darkslategray" << "darkslategrey"
<< "darkturquoise" << "darkviolet" << "deeppink" << "deepskyblue" << "dimgray" << "dimgrey"
<< "dodgerblue" << "firebrick" << "floralwhite" << "forestgreen" << "fuchsia" << "gainsboro"
<< "ghostwhite" << "gold" << "goldenrod" << "gray" << "grey" << "green" << "greenyellow"
<< "honeydew" << "hotpink" << "indianred" << "indigo" << "ivory" << "khaki" << "lavender"
<< "lavenderblush" << "lawngreen" << "lemonchiffon" << "lightblue" << "lightcoral" << "lightcyan"
<< "lightgoldenrodyellow" << "lightgray" << "lightgreen" << "lightgrey" << "lightpink"
<< "lightsalmon" << "lightseagreen" << "lightskyblue" << "lightslategray" << "lightslategrey"
<< "lightsteelblue" << "lightyellow" << "lime" << "limegreen" << "linen" << "magenta"
<< "maroon" << "mediumaquamarine" << "mediumblue" << "mediumorchid" << "mediumpurple"
<< "mediumseagreen" << "mediumslateblue" << "mediumspringgreen" << "mediumturquoise"
<< "mediumvioletred" << "midnightblue" << "mintcream" << "mistyrose" << "moccasin"
<< "navajowhite" << "navy" << "oldlace" << "olive" << "olivedrab" << "orange" << "orangered"
<< "orchid" << "palegoldenrod" << "palegreen" << "paleturquoise" << "palevioletred"
<< "papayawhip" << "peachpuff" << "peru" << "pink" << "plum" << "powderblue" << "purple" << "red"
<< "rosybrown" << "royalblue" << "saddlebrown" << "salmon" << "sandybrown" << "seagreen"
<< "seashell" << "sienna" << "silver" << "skyblue" << "slateblue" << "slategray" << "slategrey"
<< "snow" << "springgreen" << "steelblue" << "tan" << "teal" << "thistle" << "tomato"
<< "turquoise" << "violet" << "wheat" << "white" << "whitesmoke" << "yellow" << "yellowgreen";
}
class ColorProxyModel : public QSortFilterProxyModel
{
public:
ColorProxyModel(QObject *parent = nullptr) : QSortFilterProxyModel(parent)
{
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
{
if (role == Qt::DisplayRole) {
QString name = data(index, Qt::EditRole).toString();
return tr("%1 (%2)").arg(name, QColor(name).name());
}
if (role == Qt::DecorationRole)
return QColor(data(index, Qt::EditRole).toString());
return QSortFilterProxyModel::data(index, role);
}
};
ColorDialogPanel::ColorDialogPanel(QWidget *parent)
: QWidget(parent)
, m_colorComboBox(new QComboBox)
, m_showAlphaChannel(new QCheckBox(tr("Show alpha channel")))
, m_noButtons(new QCheckBox(tr("Don't display OK/Cancel buttons")))
, m_dontUseNativeDialog(new QCheckBox(tr("Don't use native dialog")))
{
// Options
QGroupBox *optionsGroupBox = new QGroupBox(tr("Options"), this);
QVBoxLayout *optionsLayout = new QVBoxLayout(optionsGroupBox);
optionsLayout->addWidget(m_showAlphaChannel);
optionsLayout->addWidget(m_noButtons);
optionsLayout->addWidget(m_dontUseNativeDialog);
// Color
QGroupBox *colorGroupBox = new QGroupBox(tr("Color"), this);
QVBoxLayout *colorLayout = new QVBoxLayout(colorGroupBox);
colorLayout->addWidget(m_colorComboBox);
m_colorComboBox->addItems(svgColorNames());
m_colorComboBox->setEditable(true);
QAbstractItemModel *sourceModel = m_colorComboBox->model();
ColorProxyModel* proxyModel = new ColorProxyModel(m_colorComboBox);
proxyModel->setSourceModel(sourceModel);
sourceModel->setParent(proxyModel);
m_colorComboBox->setModel(proxyModel);
// Buttons
QGroupBox *buttonsGroupBox = new QGroupBox(tr("Show"));
QVBoxLayout *buttonsLayout = new QVBoxLayout(buttonsGroupBox);
addButton(tr("Exec modal"), buttonsLayout, this, SLOT(execModal()));
addButton(tr("Show application modal"), buttonsLayout,
[this]() { showModal(Qt::ApplicationModal); });
addButton(tr("Show window modal"), buttonsLayout, [this]() { showModal(Qt::WindowModal); });
m_deleteModalDialogButton =
addButton(tr("Delete modal"), buttonsLayout, this, SLOT(deleteModalDialog()));
addButton(tr("Show non-modal"), buttonsLayout, this, SLOT(showNonModal()));
m_deleteNonModalDialogButton =
addButton(tr("Delete non-modal"), buttonsLayout, this, SLOT(deleteNonModalDialog()));
addButton(tr("Restore defaults"), buttonsLayout, this, SLOT(restoreDefaults()));
buttonsLayout->addStretch();
// Main layout
QHBoxLayout *mainLayout = new QHBoxLayout(this);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addWidget(optionsGroupBox);
leftLayout->addWidget(colorGroupBox);
leftLayout->addStretch();
mainLayout->addLayout(leftLayout);
mainLayout->addWidget(buttonsGroupBox);
enableDeleteModalDialogButton();
enableDeleteNonModalDialogButton();
restoreDefaults();
}
void ColorDialogPanel::execModal()
{
QColorDialog dialog(this);
applySettings(&dialog);
connect(&dialog, SIGNAL(accepted()), this, SLOT(accepted()));
connect(&dialog, SIGNAL(rejected()), this, SLOT(rejected()));
connect(&dialog, SIGNAL(currentColorChanged(const QColor&)), this, SLOT(currentColorChanged(const QColor&)));
dialog.setWindowTitle(tr("Modal Color Dialog Qt %1").arg(QLatin1String(QT_VERSION_STR)));
dialog.exec();
}
void ColorDialogPanel::showModal(Qt::WindowModality modality)
{
if (m_modalDialog.isNull()) {
static int n = 0;
m_modalDialog = new QColorDialog(this);
m_modalDialog->setModal(true);
connect(m_modalDialog.data(), SIGNAL(accepted()), this, SLOT(accepted()));
connect(m_modalDialog.data(), SIGNAL(rejected()), this, SLOT(rejected()));
connect(m_modalDialog.data(), SIGNAL(currentColorChanged(const QColor&)), this, SLOT(currentColorChanged(const QColor&)));
m_modalDialog->setWindowTitle(tr("Modal Color Dialog #%1 Qt %2")
.arg(++n)
.arg(QLatin1String(QT_VERSION_STR)));
enableDeleteModalDialogButton();
}
m_modalDialog->setWindowModality(modality);
applySettings(m_modalDialog);
m_modalDialog->show();
}
void ColorDialogPanel::showNonModal()
{
if (m_nonModalDialog.isNull()) {
static int n = 0;
m_nonModalDialog = new QColorDialog(this);
connect(m_nonModalDialog.data(), SIGNAL(accepted()), this, SLOT(accepted()));
connect(m_nonModalDialog.data(), SIGNAL(rejected()), this, SLOT(rejected()));
connect(m_nonModalDialog.data(), SIGNAL(currentColorChanged(const QColor&)), this, SLOT(currentColorChanged(const QColor&)));
m_nonModalDialog->setWindowTitle(tr("Non-Modal Color Dialog #%1 Qt %2")
.arg(++n)
.arg(QLatin1String(QT_VERSION_STR)));
enableDeleteNonModalDialogButton();
}
applySettings(m_nonModalDialog);
m_nonModalDialog->show();
}
void ColorDialogPanel::deleteNonModalDialog()
{
if (!m_nonModalDialog.isNull())
delete m_nonModalDialog;
enableDeleteNonModalDialogButton();
}
void ColorDialogPanel::deleteModalDialog()
{
if (!m_modalDialog.isNull())
delete m_modalDialog;
enableDeleteModalDialogButton();
}
void ColorDialogPanel::accepted()
{
const QColorDialog *d = qobject_cast<const QColorDialog *>(sender());
Q_ASSERT(d);
m_result.clear();
qDebug() << "Current color: " << d->currentColor()
<< "Selected color: " << d->selectedColor();
QDebug(&m_result).nospace()
<< "Current color: " << d->currentColor()
<< "\nSelected color: " << d->selectedColor();
QTimer::singleShot(0, this, SLOT(showAcceptedResult())); // Avoid problems with the closing (modal) dialog as parent.
}
void ColorDialogPanel::rejected()
{
qDebug() << "rejected";
}
void ColorDialogPanel::currentColorChanged(const QColor &color)
{
qDebug() << color;
}
void ColorDialogPanel::showAcceptedResult()
{
QMessageBox::information(this, tr("Color Dialog Accepted"), m_result, QMessageBox::Ok);
}
void ColorDialogPanel::restoreDefaults()
{
QColorDialog d;
m_showAlphaChannel->setChecked(d.testOption(QColorDialog::ShowAlphaChannel));
m_noButtons->setChecked(d.testOption(QColorDialog::NoButtons));
m_dontUseNativeDialog->setChecked(d.testOption(QColorDialog::DontUseNativeDialog));
}
void ColorDialogPanel::enableDeleteNonModalDialogButton()
{
m_deleteNonModalDialogButton->setEnabled(!m_nonModalDialog.isNull());
}
void ColorDialogPanel::enableDeleteModalDialogButton()
{
m_deleteModalDialogButton->setEnabled(!m_modalDialog.isNull());
}
void ColorDialogPanel::applySettings(QColorDialog *d) const
{
d->setOption(QColorDialog::ShowAlphaChannel, m_showAlphaChannel->isChecked());
d->setOption(QColorDialog::NoButtons, m_noButtons->isChecked());
d->setOption(QColorDialog::DontUseNativeDialog, m_dontUseNativeDialog->isChecked());
d->setCurrentColor(QColor(m_colorComboBox->itemData(m_colorComboBox->currentIndex(), Qt::EditRole).toString()));
}

View File

@ -0,0 +1,52 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef COLORDIALOGPANEL_H
#define COLORDIALOGPANEL_H
#include <QPointer>
#include <QColorDialog>
QT_BEGIN_NAMESPACE
class QComboBox;
class QCheckBox;
class QPushButton;
QT_END_NAMESPACE
class ColorDialogPanel : public QWidget
{
Q_OBJECT
public:
explicit ColorDialogPanel(QWidget *parent = nullptr);
public slots:
void execModal();
void showModal(Qt::WindowModality modality);
void showNonModal();
void deleteNonModalDialog();
void deleteModalDialog();
void accepted();
void rejected();
void currentColorChanged(const QColor & color);
void showAcceptedResult();
void restoreDefaults();
private slots:
void enableDeleteNonModalDialogButton();
void enableDeleteModalDialogButton();
private:
void applySettings(QColorDialog *d) const;
QComboBox *m_colorComboBox;
QCheckBox *m_showAlphaChannel;
QCheckBox *m_noButtons;
QCheckBox *m_dontUseNativeDialog;
QPushButton *m_deleteNonModalDialogButton;
QPushButton *m_deleteModalDialogButton;
QString m_result;
QPointer<QColorDialog> m_modalDialog;
QPointer<QColorDialog> m_nonModalDialog;
};
#endif // COLORDIALOGPANEL_H

View File

@ -0,0 +1,16 @@
QT += core gui widgets
qtHaveModule(printsupport): QT += printsupport
TARGET = dialogs
TEMPLATE = app
SOURCES += main.cpp filedialogpanel.cpp colordialogpanel.cpp fontdialogpanel.cpp \
wizardpanel.cpp messageboxpanel.cpp utils.cpp
HEADERS += filedialogpanel.h colordialogpanel.h fontdialogpanel.h \
wizardpanel.h messageboxpanel.h utils.h
!contains(DEFINES, QT_NO_PRINTER) {
SOURCES += printdialogpanel.cpp
HEADERS += printdialogpanel.h
FORMS += printdialogpanel.ui
}

View File

@ -0,0 +1,481 @@
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "filedialogpanel.h"
#include "utils.h"
#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QFormLayout>
#include <QSpacerItem>
#include <QGroupBox>
#include <QPushButton>
#include <QCheckBox>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QLabel>
#include <QMessageBox>
#include <QApplication>
#include <QUrl>
#include <QTimer>
#include <QDebug>
const FlagData acceptModeComboData[] =
{
{"AcceptOpen", QFileDialog::AcceptOpen },
{"AcceptSave", QFileDialog::AcceptSave }
};
const FlagData viewModeComboData[] =
{
{"Detail", QFileDialog::Detail},
{"List", QFileDialog::List}
};
const FlagData fileModeComboData[] =
{
{"AnyFile", QFileDialog::AnyFile},
{"ExistingFile", QFileDialog::ExistingFile},
{"ExistingFiles", QFileDialog::ExistingFiles},
{"Directory", QFileDialog::Directory}
};
// A line edit for editing the label fields of the dialog, keeping track of whether it has
// been modified by the user to avoid applying Qt's default texts to native dialogs.
class LabelLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit LabelLineEdit(QFileDialog::DialogLabel label, QWidget *parent = nullptr) : QLineEdit(parent), m_label(label), m_dirty(false)
{
connect(this, SIGNAL(textEdited(QString)), this, SLOT(setDirty()));
}
void restoreDefault(const QFileDialog *d)
{
setText(d->labelText(m_label));
m_dirty = false;
}
void apply(QFileDialog *d) const
{
if (m_dirty)
d->setLabelText(m_label, text());
}
private slots:
void setDirty() { m_dirty = true; }
private:
const QFileDialog::DialogLabel m_label;
bool m_dirty;
};
FileDialogPanel::FileDialogPanel(QWidget *parent)
: QWidget(parent)
, m_showDirsOnly(new QCheckBox(tr("Show dirs only")))
, m_readOnly(new QCheckBox(tr("Read only")))
, m_confirmOverWrite(new QCheckBox(tr("Confirm overwrite")))
, m_nameFilterDetailsVisible(new QCheckBox(tr("Name filter details visible")))
, m_resolveSymLinks(new QCheckBox(tr("Resolve symlinks")))
, m_native(new QCheckBox(tr("Use native dialog")))
, m_customDirIcons(new QCheckBox(tr("Don't use custom directory icons")))
, m_acceptMode(createCombo(this, acceptModeComboData, sizeof(acceptModeComboData)/sizeof(FlagData)))
, m_fileMode(createCombo(this, fileModeComboData, sizeof(fileModeComboData)/sizeof(FlagData)))
, m_viewMode(createCombo(this, viewModeComboData, sizeof(viewModeComboData)/sizeof(FlagData)))
, m_allowedSchemes(new QLineEdit(this))
, m_defaultSuffix(new QLineEdit(this))
, m_directory(new QLineEdit(this))
, m_selectedFileName(new QLineEdit(this))
, m_useMimeTypeFilters(new QCheckBox(this))
, m_nameFilters(new QPlainTextEdit)
, m_selectedNameFilter(new QLineEdit(this))
, m_deleteNonModalDialogButton(0)
, m_deleteModalDialogButton(0)
{
// Options
QGroupBox *optionsGroupBox = new QGroupBox(tr("Options"));
QFormLayout *optionsLayout = new QFormLayout(optionsGroupBox);
optionsLayout->addRow(tr("AcceptMode:"), m_acceptMode);
optionsLayout->addRow(tr("FileMode:"), m_fileMode);
optionsLayout->addRow(tr("ViewMode:"), m_viewMode);
optionsLayout->addRow(tr("Allowed Schemes:"), m_allowedSchemes);
optionsLayout->addRow(m_showDirsOnly);
optionsLayout->addRow(m_native);
optionsLayout->addRow(m_confirmOverWrite);
optionsLayout->addRow(m_nameFilterDetailsVisible);
optionsLayout->addRow(m_resolveSymLinks);
optionsLayout->addRow(m_readOnly);
optionsLayout->addRow(m_customDirIcons);
// Files
QGroupBox *filesGroupBox = new QGroupBox(tr("Files / Filters"));
filesLayout = new QFormLayout(filesGroupBox);
filesLayout->addRow(tr("Default Suffix:"), m_defaultSuffix);
filesLayout->addRow(tr("Directory:"), m_directory);
filesLayout->addRow(tr("Selected file:"), m_selectedFileName);
filesLayout->addRow(tr("Use mime type filters:"), m_useMimeTypeFilters);
m_nameFilters->setMaximumHeight(80);
filesLayout->addRow(tr("Name filters:"), m_nameFilters);
filesLayout->addRow(tr("Selected name filter:"), m_selectedNameFilter);
// Optional labels
QGroupBox *labelsGroupBox = new QGroupBox(tr("Labels"));
QFormLayout *labelsLayout = new QFormLayout(labelsGroupBox);
m_labelLineEdits.push_back(new LabelLineEdit(QFileDialog::LookIn, this));
labelsLayout->addRow(tr("Look in label:"), m_labelLineEdits.back());
m_labelLineEdits.push_back(new LabelLineEdit(QFileDialog::FileName, this));
labelsLayout->addRow(tr("File name label:"), m_labelLineEdits.back());
m_labelLineEdits.push_back(new LabelLineEdit(QFileDialog::FileType, this));
labelsLayout->addRow(tr("File type label:"), m_labelLineEdits.back());
m_labelLineEdits.push_back(new LabelLineEdit(QFileDialog::Accept, this));
labelsLayout->addRow(tr("Accept label:"), m_labelLineEdits.back());
m_labelLineEdits.push_back(new LabelLineEdit(QFileDialog::Reject, this));
labelsLayout->addRow(tr("Reject label:"), m_labelLineEdits.back());
// Buttons
QGroupBox *buttonsGroupBox = new QGroupBox(tr("Show"));
QGridLayout *buttonLayout = new QGridLayout(buttonsGroupBox);
int row = 0;
int column = 0;
addButton(tr("Exec modal"), buttonLayout, row, column, this, SLOT(execModal()));
addButton(tr("Show application modal"), buttonLayout, row, column,
[this]() { showModal(Qt::ApplicationModal); });
addButton(tr("Show window modal"), buttonLayout, row, column,
[this]() { showModal(Qt::WindowModal); });
m_deleteModalDialogButton = addButton(tr("Delete modal"), buttonLayout, row, column, this,
SLOT(deleteModalDialog()));
addButton(tr("Show non-modal"), buttonLayout, row, column, this, SLOT(showNonModal()));
m_deleteNonModalDialogButton =
addButton(tr("Delete non-modal"), buttonLayout, row, column, this, SLOT(deleteNonModalDialog()));
row = 0;
column++;
addButton(tr("getOpenFileName"), buttonLayout, row, column, this, SLOT(getOpenFileName()));
addButton(tr("getOpenFileUrl"), buttonLayout, row, column, this, SLOT(getOpenFileUrl()));
addButton(tr("getOpenFileNames"), buttonLayout, row, column, this, SLOT(getOpenFileNames()));
addButton(tr("getOpenFileUrls"), buttonLayout, row, column, this, SLOT(getOpenFileUrls()));
addButton(tr("getSaveFileName"), buttonLayout, row, column, this, SLOT(getSaveFileName()));
addButton(tr("getSaveFileUrl"), buttonLayout, row, column, this, SLOT(getSaveFileUrl()));
addButton(tr("getExistingDirectory"), buttonLayout, row, column, this, SLOT(getExistingDirectory()));
addButton(tr("getExistingDirectoryUrl"), buttonLayout, row, column, this, SLOT(getExistingDirectoryUrl()));
addButton(tr("Restore defaults"), buttonLayout, row, column, this, SLOT(restoreDefaults()));
// Main layout
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->addWidget(optionsGroupBox, 0, 0);
gridLayout->addWidget(filesGroupBox, 0, 1);
gridLayout->addWidget(labelsGroupBox, 1, 0);
gridLayout->addWidget(buttonsGroupBox, 1, 1);
connect(m_useMimeTypeFilters, SIGNAL(toggled(bool)), this, SLOT(useMimeTypeFilters(bool)));
enableDeleteModalDialogButton();
enableDeleteNonModalDialogButton();
restoreDefaults();
}
void FileDialogPanel::execModal()
{
QFileDialog dialog(this);
applySettings(&dialog);
connect(&dialog, SIGNAL(accepted()), this, SLOT(accepted()));
dialog.setWindowTitle(tr("Modal File Dialog Qt %1").arg(QLatin1String(QT_VERSION_STR)));
dialog.exec();
}
void FileDialogPanel::showModal(Qt::WindowModality modality)
{
if (m_modalDialog.isNull()) {
static int n = 0;
m_modalDialog = new QFileDialog(this);
m_modalDialog->setModal(true);
connect(m_modalDialog.data(), SIGNAL(accepted()), this, SLOT(accepted()));
m_modalDialog->setWindowTitle(tr("Modal File Dialog #%1 Qt %2")
.arg(++n)
.arg(QLatin1String(QT_VERSION_STR)));
enableDeleteModalDialogButton();
}
m_modalDialog->setWindowModality(modality);
applySettings(m_modalDialog);
m_modalDialog->show();
}
void FileDialogPanel::showNonModal()
{
if (m_nonModalDialog.isNull()) {
static int n = 0;
m_nonModalDialog = new QFileDialog(this);
connect(m_nonModalDialog.data(), SIGNAL(accepted()), this, SLOT(accepted()));
m_nonModalDialog->setWindowTitle(tr("Non-Modal File Dialog #%1 Qt %2")
.arg(++n)
.arg(QLatin1String(QT_VERSION_STR)));
enableDeleteNonModalDialogButton();
}
applySettings(m_nonModalDialog);
m_nonModalDialog->show();
}
void FileDialogPanel::deleteNonModalDialog()
{
if (!m_nonModalDialog.isNull())
delete m_nonModalDialog;
enableDeleteNonModalDialogButton();
}
void FileDialogPanel::deleteModalDialog()
{
if (!m_modalDialog.isNull())
delete m_modalDialog;
enableDeleteModalDialogButton();
}
void FileDialogPanel::enableDeleteNonModalDialogButton()
{
m_deleteNonModalDialogButton->setEnabled(!m_nonModalDialog.isNull());
}
void FileDialogPanel::enableDeleteModalDialogButton()
{
m_deleteModalDialogButton->setEnabled(!m_modalDialog.isNull());
}
QString FileDialogPanel::filterString() const
{
return m_nameFilters->toPlainText().trimmed().replace(QLatin1String("\n"), QLatin1String(";;"));
}
QUrl FileDialogPanel::currentDirectoryUrl() const
{
return QUrl::fromUserInput(m_directory->text().trimmed());
}
QFileDialog::Options FileDialogPanel::options() const
{
QFileDialog::Options result;
if (m_showDirsOnly->isChecked())
result |= QFileDialog::ShowDirsOnly;
if (!m_nameFilterDetailsVisible->isChecked())
result |= QFileDialog::HideNameFilterDetails;
if (!m_resolveSymLinks->isChecked())
result |= QFileDialog::DontResolveSymlinks;
if (m_readOnly->isChecked())
result |= QFileDialog::ReadOnly;
if (!m_confirmOverWrite->isChecked())
result |= QFileDialog::DontConfirmOverwrite;
if (!m_native->isChecked())
result |= QFileDialog::DontUseNativeDialog;
if (m_customDirIcons->isChecked())
result |= QFileDialog::DontUseCustomDirectoryIcons;
return result;
}
QStringList FileDialogPanel::allowedSchemes() const
{
return m_allowedSchemes->text().simplified().split(' ', Qt::SkipEmptyParts);
}
void FileDialogPanel::getOpenFileNames()
{
QString selectedFilter = m_selectedNameFilter->text().trimmed();
const QStringList files =
QFileDialog::getOpenFileNames(this, tr("getOpenFileNames Qt %1").arg(QLatin1String(QT_VERSION_STR)),
m_directory->text(), filterString(), &selectedFilter, options());
if (!files.isEmpty()) {
QString result;
QDebug(&result).nospace()
<< "Files: " << files
<< "\nName filter: " << selectedFilter;
QMessageBox::information(this, tr("getOpenFileNames"), result, QMessageBox::Ok);
}
}
void FileDialogPanel::getOpenFileUrls()
{
QString selectedFilter = m_selectedNameFilter->text().trimmed();
const QList<QUrl> files =
QFileDialog::getOpenFileUrls(this, tr("getOpenFileNames Qt %1").arg(QLatin1String(QT_VERSION_STR)),
currentDirectoryUrl(), filterString(), &selectedFilter, options(),
allowedSchemes());
if (!files.isEmpty()) {
QString result;
QDebug(&result).nospace()
<< "Files: " << QUrl::toStringList(files)
<< "\nName filter: " << selectedFilter;
QMessageBox::information(this, tr("getOpenFileNames"), result, QMessageBox::Ok);
}
}
void FileDialogPanel::getOpenFileName()
{
QString selectedFilter = m_selectedNameFilter->text().trimmed();
const QString file =
QFileDialog::getOpenFileName(this, tr("getOpenFileName Qt %1").arg(QLatin1String(QT_VERSION_STR)),
m_directory->text(), filterString(), &selectedFilter, options());
if (!file.isEmpty()) {
QString result;
QDebug(&result).nospace()
<< "File: " << file
<< "\nName filter: " << selectedFilter;
QMessageBox::information(this, tr("getOpenFileName"), result, QMessageBox::Ok);
}
}
void FileDialogPanel::getOpenFileUrl()
{
QString selectedFilter = m_selectedNameFilter->text().trimmed();
const QUrl file =
QFileDialog::getOpenFileUrl(this, tr("getOpenFileUrl Qt %1").arg(QLatin1String(QT_VERSION_STR)),
currentDirectoryUrl(), filterString(), &selectedFilter, options(),
allowedSchemes());
if (file.isValid()) {
QString result;
QDebug(&result).nospace()
<< "File: " << file.toString()
<< "\nName filter: " << selectedFilter;
QMessageBox::information(this, tr("getOpenFileName"), result, QMessageBox::Ok);
}
}
void FileDialogPanel::getSaveFileName()
{
QString selectedFilter = m_selectedNameFilter->text().trimmed();
const QString file =
QFileDialog::getSaveFileName(this, tr("getSaveFileName Qt %1").arg(QLatin1String(QT_VERSION_STR)),
m_directory->text(), filterString(), &selectedFilter, options());
if (!file.isEmpty()) {
QString result;
QDebug(&result).nospace()
<< "File: " << file
<< "\nName filter: " << selectedFilter;
QMessageBox::information(this, tr("getSaveFileNames"), result, QMessageBox::Ok);
}
}
void FileDialogPanel::getSaveFileUrl()
{
QString selectedFilter = m_selectedNameFilter->text().trimmed();
const QUrl file =
QFileDialog::getSaveFileUrl(this, tr("getSaveFileName Qt %1").arg(QLatin1String(QT_VERSION_STR)),
currentDirectoryUrl(), filterString(), &selectedFilter, options(),
allowedSchemes());
if (file.isValid()) {
QString result;
QDebug(&result).nospace()
<< "File: " << file.toString()
<< "\nName filter: " << selectedFilter;
QMessageBox::information(this, tr("getSaveFileNames"), result, QMessageBox::Ok);
}
}
void FileDialogPanel::getExistingDirectory()
{
const QString dir =
QFileDialog::getExistingDirectory(this, tr("getExistingDirectory Qt %1").arg(QLatin1String(QT_VERSION_STR)),
m_directory->text(), options() | QFileDialog::ShowDirsOnly);
if (!dir.isEmpty())
QMessageBox::information(this, tr("getExistingDirectory"), QLatin1String("Directory: ") + dir, QMessageBox::Ok);
}
void FileDialogPanel::getExistingDirectoryUrl()
{
const QUrl dir =
QFileDialog::getExistingDirectoryUrl(this, tr("getExistingDirectory Qt %1").arg(QLatin1String(QT_VERSION_STR)),
currentDirectoryUrl(), options() | QFileDialog::ShowDirsOnly,
allowedSchemes());
if (!dir.isEmpty())
QMessageBox::information(this, tr("getExistingDirectory"), QLatin1String("Directory: ") + dir.toString(), QMessageBox::Ok);
}
void FileDialogPanel::restoreDefaults()
{
QFileDialog d;
setComboBoxValue(m_acceptMode, d.acceptMode());
setComboBoxValue(m_fileMode, d.fileMode());
setComboBoxValue(m_viewMode, d.viewMode());
m_showDirsOnly->setChecked(d.testOption(QFileDialog::ShowDirsOnly));
m_allowedSchemes->setText(QString());
m_confirmOverWrite->setChecked(!d.testOption(QFileDialog::DontConfirmOverwrite));
m_nameFilterDetailsVisible->setChecked(!d.testOption(QFileDialog::HideNameFilterDetails));
m_resolveSymLinks->setChecked(!d.testOption(QFileDialog::DontResolveSymlinks));
m_readOnly->setChecked(d.testOption(QFileDialog::ReadOnly));
m_native->setChecked(true);
m_customDirIcons->setChecked(d.testOption(QFileDialog::DontUseCustomDirectoryIcons));
m_directory->setText(QDir::homePath());
m_defaultSuffix->setText(QLatin1String("txt"));
m_useMimeTypeFilters->setChecked(false);
useMimeTypeFilters(false);
m_selectedFileName->setText(QString());
m_selectedNameFilter->setText(QString());
foreach (LabelLineEdit *l, m_labelLineEdits)
l->restoreDefault(&d);
}
void FileDialogPanel::applySettings(QFileDialog *d) const
{
d->setAcceptMode(comboBoxValue<QFileDialog::AcceptMode>(m_acceptMode));
d->setViewMode(comboBoxValue<QFileDialog::ViewMode>(m_viewMode));
d->setFileMode(comboBoxValue<QFileDialog::FileMode>(m_fileMode));
d->setOptions(options());
d->setDefaultSuffix(m_defaultSuffix->text().trimmed());
const QString directory = m_directory->text().trimmed();
if (!directory.isEmpty())
d->setDirectory(directory);
const QString file = m_selectedFileName->text().trimmed();
if (!file.isEmpty())
d->selectFile(file);
const QString filter = m_selectedNameFilter->text().trimmed();
const QStringList filters = m_nameFilters->toPlainText().trimmed().split(QLatin1Char('\n'), Qt::SkipEmptyParts);
if (!m_useMimeTypeFilters->isChecked()) {
d->setNameFilters(filters);
if (!filter.isEmpty())
d->selectNameFilter(filter);
} else {
d->setMimeTypeFilters(filters);
if (!filter.isEmpty())
d->selectMimeTypeFilter(filter);
}
foreach (LabelLineEdit *l, m_labelLineEdits)
l->apply(d);
}
void FileDialogPanel::useMimeTypeFilters(bool b)
{
QWidget *textEdit = filesLayout->labelForField(m_nameFilters);
if (QLabel *label = qobject_cast<QLabel *>(textEdit))
label->setText(b ? tr("Mime type filters:") : tr("Name filters:"));
QWidget *w = filesLayout->labelForField(m_selectedNameFilter);
if (QLabel *label = qobject_cast<QLabel *>(w))
label->setText(b ? tr("Selected mime type filter:") : tr("Selected name filter:"));
if (b)
m_nameFilters->setPlainText(QLatin1String("image/jpeg\nimage/png\ntext/plain\napplication/octet-stream"));
else
m_nameFilters->setPlainText(QLatin1String("Any files (*)\nImage files (*.png *.xpm *.jpg)\nText files (*.txt)"));
}
void FileDialogPanel::accepted()
{
const QFileDialog *d = qobject_cast<const QFileDialog *>(sender());
Q_ASSERT(d);
m_result.clear();
QDebug(&m_result).nospace()
<< "URLs: " << d->selectedUrls() << '\n'
<< "Files: " << d->selectedFiles()
<< "\nDirectory: "
<< d->directoryUrl() << ", "
<< d->directory().absolutePath()
<< "\nName filter: " << d->selectedNameFilter();
QTimer::singleShot(0, this, SLOT(showAcceptedResult())); // Avoid problems with the closing (modal) dialog as parent.
}
void FileDialogPanel::showAcceptedResult()
{
QMessageBox::information(this, tr("File Dialog Accepted"), m_result, QMessageBox::Ok);
}
#include "filedialogpanel.moc"

View File

@ -0,0 +1,83 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef FILEDIALOGPANEL_H
#define FILEDIALOGPANEL_H
#include <QGroupBox>
#include <QFileDialog>
#include <QPointer>
QT_BEGIN_NAMESPACE
class QPushButton;
class QCheckBox;
class QComboBox;
class QLineEdit;
class QPlainTextEdit;
class QFormLayout;
QT_END_NAMESPACE
class LabelLineEdit;
class FileDialogPanel : public QWidget
{
Q_OBJECT
public:
explicit FileDialogPanel(QWidget *parent = nullptr);
public slots:
void execModal();
void showModal(Qt::WindowModality modality);
void showNonModal();
void deleteNonModalDialog();
void deleteModalDialog();
void getOpenFileNames();
void getOpenFileUrls();
void getOpenFileName();
void getOpenFileUrl();
void getSaveFileName();
void getSaveFileUrl();
void getExistingDirectory();
void getExistingDirectoryUrl();
void accepted();
void showAcceptedResult();
void restoreDefaults();
private slots:
void enableDeleteNonModalDialogButton();
void enableDeleteModalDialogButton();
void useMimeTypeFilters(bool);
private:
QUrl currentDirectoryUrl() const;
QString filterString() const;
QFileDialog::Options options() const;
QStringList allowedSchemes() const;
void applySettings(QFileDialog *d) const;
QFormLayout *filesLayout;
QCheckBox *m_showDirsOnly;
QCheckBox *m_readOnly;
QCheckBox *m_confirmOverWrite;
QCheckBox *m_nameFilterDetailsVisible;
QCheckBox *m_resolveSymLinks;
QCheckBox *m_native;
QCheckBox *m_customDirIcons;
QComboBox *m_acceptMode;
QComboBox *m_fileMode;
QComboBox *m_viewMode;
QLineEdit *m_allowedSchemes;
QLineEdit *m_defaultSuffix;
QLineEdit *m_directory;
QLineEdit *m_selectedFileName;
QList<LabelLineEdit *> m_labelLineEdits;
QCheckBox *m_useMimeTypeFilters;
QPlainTextEdit *m_nameFilters;
QLineEdit *m_selectedNameFilter;
QPushButton *m_deleteNonModalDialogButton;
QPushButton *m_deleteModalDialogButton;
QString m_result;
QPointer<QFileDialog> m_modalDialog;
QPointer<QFileDialog> m_nonModalDialog;
};
#endif // FILEDIALOGPANEL_H

View File

@ -0,0 +1,177 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "fontdialogpanel.h"
#include "utils.h"
#include <QGroupBox>
#include <QCheckBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QFontComboBox>
#include <QDoubleSpinBox>
#include <QTimer>
#include <QDebug>
FontDialogPanel::FontDialogPanel(QWidget *parent)
: QWidget(parent)
, m_fontFamilyBox(new QFontComboBox)
, m_fontSizeBox(new QDoubleSpinBox)
, m_noButtons(new QCheckBox(tr("Don't display OK/Cancel buttons")))
, m_dontUseNativeDialog(new QCheckBox(tr("Don't use native dialog")))
, m_scalableFilter(new QCheckBox(tr("Filter scalable fonts")))
, m_nonScalableFilter(new QCheckBox(tr("Filter non scalable fonts")))
, m_monospacedFilter(new QCheckBox(tr("Filter monospaced fonts")))
, m_proportionalFilter(new QCheckBox(tr("Filter proportional fonts")))
{
// Options
QGroupBox *optionsGroupBox = new QGroupBox(tr("Options"), this);
QVBoxLayout *optionsLayout = new QVBoxLayout(optionsGroupBox);
optionsLayout->addWidget(m_noButtons);
optionsLayout->addWidget(m_dontUseNativeDialog);
optionsLayout->addWidget(m_scalableFilter);
optionsLayout->addWidget(m_nonScalableFilter);
optionsLayout->addWidget(m_monospacedFilter);
optionsLayout->addWidget(m_proportionalFilter);
// Font
QGroupBox *fontGroupBox = new QGroupBox(tr("Font"), this);
QHBoxLayout *fontLayout = new QHBoxLayout(fontGroupBox);
fontLayout->addWidget(m_fontFamilyBox);
fontLayout->addWidget(m_fontSizeBox);
m_fontSizeBox->setValue(QFont().pointSizeF());
// Buttons
QGroupBox *buttonsGroupBox = new QGroupBox(tr("Show"));
QVBoxLayout *buttonsLayout = new QVBoxLayout(buttonsGroupBox);
addButton(tr("Exec modal"), buttonsLayout, this, SLOT(execModal()));
addButton(tr("Show application modal"), buttonsLayout,
[this]() { showModal(Qt::ApplicationModal); });
addButton(tr("Show window modal"), buttonsLayout, [this]() { showModal(Qt::WindowModal); });
m_deleteModalDialogButton =
addButton(tr("Delete modal"), buttonsLayout, this, SLOT(deleteModalDialog()));
addButton(tr("Show non-modal"), buttonsLayout, this, SLOT(showNonModal()));
m_deleteNonModalDialogButton =
addButton(tr("Delete non-modal"), buttonsLayout, this, SLOT(deleteNonModalDialog()));
addButton(tr("Restore defaults"), buttonsLayout, this, SLOT(restoreDefaults()));
buttonsLayout->addStretch();
// Main layout
QHBoxLayout *mainLayout = new QHBoxLayout(this);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addWidget(optionsGroupBox);
leftLayout->addWidget(fontGroupBox);
leftLayout->addStretch();
mainLayout->addLayout(leftLayout);
mainLayout->addWidget(buttonsGroupBox);
enableDeleteModalDialogButton();
enableDeleteNonModalDialogButton();
restoreDefaults();
}
void FontDialogPanel::execModal()
{
QFontDialog dialog(this);
applySettings(&dialog);
connect(&dialog, SIGNAL(accepted()), this, SLOT(accepted()));
dialog.setWindowTitle(tr("Modal Font Dialog Qt %1").arg(QLatin1String(QT_VERSION_STR)));
dialog.exec();
}
void FontDialogPanel::showModal(Qt::WindowModality modality)
{
if (m_modalDialog.isNull()) {
static int n = 0;
m_modalDialog = new QFontDialog(this);
m_modalDialog->setModal(true);
connect(m_modalDialog.data(), SIGNAL(accepted()), this, SLOT(accepted()));
m_modalDialog->setWindowTitle(tr("Modal Font Dialog #%1 Qt %2")
.arg(++n)
.arg(QLatin1String(QT_VERSION_STR)));
enableDeleteModalDialogButton();
}
m_modalDialog->setWindowModality(modality);
applySettings(m_modalDialog);
m_modalDialog->show();
}
void FontDialogPanel::showNonModal()
{
if (m_nonModalDialog.isNull()) {
static int n = 0;
m_nonModalDialog = new QFontDialog(this);
connect(m_nonModalDialog.data(), SIGNAL(accepted()), this, SLOT(accepted()));
m_nonModalDialog->setWindowTitle(tr("Non-Modal Font Dialog #%1 Qt %2")
.arg(++n)
.arg(QLatin1String(QT_VERSION_STR)));
enableDeleteNonModalDialogButton();
}
applySettings(m_nonModalDialog);
m_nonModalDialog->show();
}
void FontDialogPanel::deleteNonModalDialog()
{
if (!m_nonModalDialog.isNull())
delete m_nonModalDialog;
enableDeleteNonModalDialogButton();
}
void FontDialogPanel::deleteModalDialog()
{
if (!m_modalDialog.isNull())
delete m_modalDialog;
enableDeleteModalDialogButton();
}
void FontDialogPanel::accepted()
{
const QFontDialog *d = qobject_cast<const QFontDialog *>(sender());
Q_ASSERT(d);
m_result.clear();
QDebug(&m_result).nospace()
<< "Current font: " << d->currentFont()
<< "\nSelected font: " << d->selectedFont();
QTimer::singleShot(0, this, SLOT(showAcceptedResult())); // Avoid problems with the closing (modal) dialog as parent.
}
void FontDialogPanel::showAcceptedResult()
{
QMessageBox::information(this, tr("Color Dialog Accepted"), m_result, QMessageBox::Ok);
}
void FontDialogPanel::restoreDefaults()
{
QFontDialog d;
m_noButtons->setChecked(d.testOption(QFontDialog::NoButtons));
m_dontUseNativeDialog->setChecked(d.testOption(QFontDialog::DontUseNativeDialog));
m_fontFamilyBox->setCurrentFont(QFont());
m_fontSizeBox->setValue(QFont().pointSizeF());
}
void FontDialogPanel::enableDeleteNonModalDialogButton()
{
m_deleteNonModalDialogButton->setEnabled(!m_nonModalDialog.isNull());
}
void FontDialogPanel::enableDeleteModalDialogButton()
{
m_deleteModalDialogButton->setEnabled(!m_modalDialog.isNull());
}
void FontDialogPanel::applySettings(QFontDialog *d) const
{
d->setOption(QFontDialog::NoButtons, m_noButtons->isChecked());
d->setOption(QFontDialog::DontUseNativeDialog, m_dontUseNativeDialog->isChecked());
d->setOption(QFontDialog::ScalableFonts, m_scalableFilter->isChecked());
d->setOption(QFontDialog::NonScalableFonts, m_nonScalableFilter->isChecked());
d->setOption(QFontDialog::MonospacedFonts, m_monospacedFilter->isChecked());
d->setOption(QFontDialog::ProportionalFonts, m_proportionalFilter->isChecked());
QFont font = m_fontFamilyBox->currentFont();
font.setPointSizeF(m_fontSizeBox->value());
d->setCurrentFont(font);
}

View File

@ -0,0 +1,55 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef FONTDIALOGPANEL_H
#define FONTDIALOGPANEL_H
#include <QPointer>
#include <QFontDialog>
QT_BEGIN_NAMESPACE
class QCheckBox;
class QPushButton;
class QFontComboBox;
class QDoubleSpinBox;
QT_END_NAMESPACE
class FontDialogPanel : public QWidget
{
Q_OBJECT
public:
explicit FontDialogPanel(QWidget *parent = nullptr);
public slots:
void execModal();
void showModal(Qt::WindowModality modality);
void showNonModal();
void deleteNonModalDialog();
void deleteModalDialog();
void accepted();
void showAcceptedResult();
void restoreDefaults();
private slots:
void enableDeleteNonModalDialogButton();
void enableDeleteModalDialogButton();
private:
void applySettings(QFontDialog *d) const;
QFontComboBox *m_fontFamilyBox;
QDoubleSpinBox *m_fontSizeBox;
QCheckBox *m_noButtons;
QCheckBox *m_dontUseNativeDialog;
QCheckBox *m_scalableFilter;
QCheckBox *m_nonScalableFilter;
QCheckBox *m_monospacedFilter;
QCheckBox *m_proportionalFilter;
QPushButton *m_deleteNonModalDialogButton;
QPushButton *m_deleteModalDialogButton;
QString m_result;
QPointer<QFontDialog> m_modalDialog;
QPointer<QFontDialog> m_nonModalDialog;
};
#endif // FONTDIALOGPANEL_H

View File

@ -0,0 +1,114 @@
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "filedialogpanel.h"
#include "colordialogpanel.h"
#include "fontdialogpanel.h"
#include "printdialogpanel.h"
#include "wizardpanel.h"
#include "messageboxpanel.h"
#include <QLibraryInfo>
#include <QDialogButtonBox>
#include <QMainWindow>
#include <QApplication>
#include <QMenuBar>
#include <QTabWidget>
#include <QFormLayout>
#include <QMenu>
#include <QAction>
#include <QKeySequence>
static bool optNoPrinter = false;
// Test for dialogs, allowing to play with all dialog options for implementing native dialogs.
// Compiles with Qt 4.8 and Qt 5.
class AboutDialog : public QDialog
{
public:
explicit AboutDialog(QWidget *parent = nullptr);
};
AboutDialog::AboutDialog(QWidget *parent) : QDialog(parent)
{
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
QFormLayout *mainLayout = new QFormLayout(this);
mainLayout->addRow(new QLabel(QLibraryInfo::build()));
mainLayout->addRow("Style:", new QLabel(qApp->style()->objectName()));
mainLayout->addRow("DPR:", new QLabel(QString::number(qApp->devicePixelRatio())));
const QString resolution = QString::number(logicalDpiX()) + QLatin1Char(',')
+ QString::number(logicalDpiY()) + QLatin1String("dpi");
mainLayout->addRow("Resolution:", new QLabel(resolution));
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal);
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
mainLayout->addRow(buttonBox);
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
public slots:
void aboutDialog();
};
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
setWindowTitle(tr("Dialogs Qt %1").arg(QLatin1String(QT_VERSION_STR)));
QMenu *fileMenu = menuBar()->addMenu(tr("File"));
QAction *quitAction = fileMenu->addAction(tr("Quit"));
quitAction->setShortcut(QKeySequence(QKeySequence::Quit));
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
QAction *action = editMenu->addAction(tr("Cut"));
action->setShortcut(QKeySequence(QKeySequence::Cut));
action = editMenu->addAction(tr("Copy"));
action->setShortcut(QKeySequence(QKeySequence::Copy));
action = editMenu->addAction(tr("Paste"));
action->setShortcut(QKeySequence(QKeySequence::Paste));
action = editMenu->addAction(tr("Select All"));
action->setShortcut(QKeySequence(QKeySequence::SelectAll));
QMenu *aboutMenu = menuBar()->addMenu(tr("&About"));
QAction *aboutAction = aboutMenu->addAction(tr("About..."), this, SLOT(aboutDialog()));
aboutAction->setShortcut(Qt::Key_F1);
QTabWidget *tabWidget = new QTabWidget;
tabWidget->addTab(new FileDialogPanel, tr("QFileDialog"));
tabWidget->addTab(new ColorDialogPanel, tr("QColorDialog"));
tabWidget->addTab(new FontDialogPanel, tr("QFontDialog"));
tabWidget->addTab(new WizardPanel, tr("QWizard"));
tabWidget->addTab(new MessageBoxPanel, tr("QMessageBox"));
#ifndef QT_NO_PRINTER
if (!optNoPrinter)
tabWidget->addTab(new PrintDialogPanel, tr("QPrintDialog"));
#endif
setCentralWidget(tabWidget);
}
void MainWindow::aboutDialog()
{
AboutDialog dialog(this);
dialog.setWindowTitle(tr("About Dialogs"));
dialog.exec();
}
int main(int argc, char *argv[])
{
for (int a = 1; a < argc; ++a) {
if (!qstrcmp(argv[a], "-n")) {
qDebug("AA_DontUseNativeDialogs");
QCoreApplication::setAttribute(Qt::AA_DontUseNativeDialogs);
} else if (!qstrcmp(argv[a], "-p")) {
optNoPrinter = true; // Avoid startup slowdown by printer code
}
}
QApplication a(argc, argv);
MainWindow w;
w.move(500, 200);
w.show();
return a.exec();
}
#include "main.moc"

View File

@ -0,0 +1,163 @@
// Copyright (C) 2013 Thorbjørn Lund Martsum - tmartsum[at]gmail.com
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "messageboxpanel.h"
#include <QGroupBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QPushButton>
#include <QDebug>
#include <QComboBox>
#include <QLineEdit>
#include <QLabel>
#include <QCheckBox>
#include <QRegularExpressionValidator>
#include <QRegularExpression>
MessageBoxPanel::MessageBoxPanel(QWidget *parent) : QWidget(parent)
,m_iconComboBox(new QComboBox)
,m_textInMsgBox(new QLineEdit)
,m_informativeText(new QLineEdit)
,m_detailedtext(new QLineEdit)
,m_buttonsMask(new QLineEdit)
,m_btnExec(new QPushButton)
,m_btnShowApply(new QPushButton)
,m_resultLabel(new QLabel)
,m_chkReallocMsgBox(new QCheckBox(QString::fromLatin1("Reallocate Message Box")))
,m_checkboxText(new QLineEdit)
,m_checkBoxResult(new QLabel)
,m_msgbox(new QMessageBox)
{
// --- Options ---
QGroupBox *optionsGroupBox = new QGroupBox(tr("Options"), this);
QVBoxLayout *optionsLayout = new QVBoxLayout(optionsGroupBox);
// text
optionsLayout->addWidget(new QLabel(QString::fromLatin1("Message box text")));
m_textInMsgBox->setText(QString::fromLatin1("This is a simple test with a text that is not long"));
optionsLayout->addWidget(m_textInMsgBox);
// informative text
optionsLayout->addWidget(new QLabel(QString::fromLatin1("Informative Text")));
optionsLayout->addWidget(m_informativeText);
// detailed text
optionsLayout->addWidget(new QLabel(QString::fromLatin1("detailed Text")));
optionsLayout->addWidget(m_detailedtext);
// icon
QStringList items;
items << "NoIcon" << "Information" << "Warning" << "Critical" << "Question";
m_iconComboBox->addItems(items);
optionsLayout->addWidget(new QLabel(QString::fromLatin1("Message box icon")));
optionsLayout->addWidget(m_iconComboBox);
// buttons mask
optionsLayout->addWidget(new QLabel(QString::fromLatin1("Message box button mask (in hex)")));
m_validator = new QRegularExpressionValidator(QRegularExpression("0[xX]?[0-9a-fA-F]+"), this);
m_buttonsMask->setMaxLength(10);
m_buttonsMask->setValidator(m_validator);
m_buttonsMask->setText(QString::fromLatin1("0x00300400"));
optionsLayout->addWidget(m_buttonsMask);
// check box check
optionsLayout->addWidget(new QLabel(QString::fromLatin1("Checkbox text ("" => no chkbox)")));
optionsLayout->addWidget(m_checkboxText);
// reallocate
optionsLayout->addWidget(m_chkReallocMsgBox);
optionsLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding));
// Exec/Show
QGroupBox *execGroupBox = new QGroupBox(tr("Exec"));
QVBoxLayout *execLayout = new QVBoxLayout(execGroupBox);
m_btnExec->setText(QString::fromLatin1("Exec message box"));
connect(m_btnExec, SIGNAL(clicked()), this, SLOT(doExec()));
execLayout->addWidget(m_btnExec);
m_btnShowApply->setText(QString::fromLatin1("Show / apply"));
connect(m_btnShowApply, SIGNAL(clicked()), this, SLOT(doShowApply()));
execLayout->addWidget(m_btnShowApply);
// result label
execLayout->addWidget(m_resultLabel);
execLayout->addWidget(m_checkBoxResult);
execLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding));
execGroupBox->setLayout(execLayout);
// Main layout
QHBoxLayout *mainLayout = new QHBoxLayout();
mainLayout->addWidget(optionsGroupBox);
mainLayout->addWidget(execGroupBox);
setLayout(mainLayout);
}
void MessageBoxPanel::setupMessageBox(QMessageBox &box)
{
m_resultLabel->setText(QString());
m_checkBoxResult->setText(QString());
box.setText(m_textInMsgBox->text());
box.setInformativeText(m_informativeText->text());
box.setDetailedText(m_detailedtext->text());
QString btnHexText = m_buttonsMask->text();
btnHexText = btnHexText.replace(QString::fromLatin1("0x"), QString(), Qt::CaseInsensitive);
bool ok;
QMessageBox::StandardButtons btns = (QMessageBox::StandardButtons) btnHexText.toUInt(&ok, 16);
box.setStandardButtons((QMessageBox::StandardButtons) btns);
if (box.standardButtons() == QMessageBox::StandardButtons())
box.setStandardButtons(QMessageBox::Ok); // just to have something.
box.setCheckBox(0);
if (m_checkboxText->text().length() > 0)
box.setCheckBox(new QCheckBox(m_checkboxText->text()));
box.setIcon((QMessageBox::Icon) m_iconComboBox->currentIndex());
}
MessageBoxPanel::~MessageBoxPanel()
{
if (m_msgbox)
m_msgbox->deleteLater();
}
void MessageBoxPanel::doExec()
{
if (!m_msgbox || m_chkReallocMsgBox->isChecked()) {
if (m_msgbox)
m_msgbox->deleteLater();
m_msgbox = new QMessageBox;
}
setupMessageBox(*m_msgbox);
m_msgbox->setWindowModality(Qt::NonModal);
int res = m_msgbox->exec();
QString sres;
sres.setNum(res, 16);
m_resultLabel->setText(QString::fromLatin1("Return value (hex): %1").arg(sres));
if (m_msgbox->checkBox()) {
if (m_msgbox->checkBox()->isChecked())
m_checkBoxResult->setText(QString::fromLatin1("Checkbox was checked"));
else
m_checkBoxResult->setText(QString::fromLatin1("Checkbox was not checked"));
}
}
void MessageBoxPanel::doShowApply()
{
if (!m_msgbox || m_chkReallocMsgBox->isChecked()) {
if (m_msgbox)
m_msgbox->deleteLater();
m_msgbox = new QMessageBox;
}
setupMessageBox(*m_msgbox);
if (!m_msgbox->isVisible()) {
m_msgbox->setWindowModality(Qt::NonModal);
m_msgbox->show();
}
}

View File

@ -0,0 +1,49 @@
// Copyright (C) 2013 2013 Thorbjørn Lund Martsum - tmartsum[at]gmail.com
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef MESSAGEBOXPANEL_H
#define MESSAGEBOXPANEL_H
#include <QWidget>
#include <QCheckBox>
QT_BEGIN_NAMESPACE
class QComboBox;
class QCheckBox;
class QPushButton;
class QLineEdit;
class QValidator;
class QLabel;
class QMessageBox;
class QCheckBox;
QT_END_NAMESPACE
class MessageBoxPanel : public QWidget
{
Q_OBJECT
public:
explicit MessageBoxPanel(QWidget *parent = nullptr);
~MessageBoxPanel();
public slots:
void doExec();
void doShowApply();
private:
QComboBox *m_iconComboBox;
QLineEdit *m_textInMsgBox;
QLineEdit *m_informativeText;
QLineEdit *m_detailedtext;
QLineEdit *m_buttonsMask;
QPushButton *m_btnExec;
QPushButton *m_btnShowApply;
QValidator *m_validator;
QLabel *m_resultLabel;
QCheckBox *m_chkReallocMsgBox;
QLineEdit *m_checkboxText;
QLabel *m_checkBoxResult;
QMessageBox *m_msgbox;
void setupMessageBox(QMessageBox &box);
};
#endif

View File

@ -0,0 +1,582 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef QT_NO_PRINTER
#include "printdialogpanel.h"
#include "utils.h"
#include <QPrinter>
#include <QPrinterInfo>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QPageSetupDialog>
#include <QApplication>
#include <QGroupBox>
#include <QCheckBox>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QFormLayout>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QDoubleSpinBox>
#include <QPainter>
#include <QFont>
#include <QFontMetrics>
#include <QDateTime>
#include <QDebug>
#include <QTextStream>
#include <QDir>
#include <QScreen>
const FlagData printerModeComboData[] =
{
{"ScreenResolution", QPrinter::ScreenResolution},
{"PrinterResolution", QPrinter::PrinterResolution},
{"HighResolution", QPrinter::HighResolution}
};
const FlagData printRangeComboData[] =
{
{"AllPages", QPrinter::AllPages},
{"Selection", QPrinter::Selection},
{"PageRange", QPrinter::PageRange},
{"CurrentPage", QPrinter::CurrentPage}
};
const FlagData pageOrderComboData[] =
{
{"FirstPageFirst", QPrinter::FirstPageFirst},
{"LastPageFirst", QPrinter::LastPageFirst},
};
const FlagData duplexModeComboData[] =
{
{"DuplexNone", QPrinter::DuplexNone},
{"DuplexAuto", QPrinter::DuplexAuto},
{"DuplexLongSide", QPrinter::DuplexLongSide},
{"DuplexShortSide", QPrinter::DuplexShortSide},
};
const FlagData paperSourceComboData[] =
{
{"OnlyOne", QPrinter::OnlyOne},
{"Lower", QPrinter::Lower},
{"Middle", QPrinter::Middle},
{"Manual", QPrinter::Manual},
{"Envelope", QPrinter::Envelope},
{"EnvelopeManual", QPrinter::EnvelopeManual},
{"Auto", QPrinter::Auto},
{"Tractor", QPrinter::Tractor},
{"SmallFormat", QPrinter::SmallFormat},
{"LargeFormat", QPrinter::LargeFormat},
{"LargeCapacity", QPrinter::LargeCapacity},
{"Cassette", QPrinter::Cassette},
{"FormSource", QPrinter::FormSource},
{"DuplexLongSide", QPrinter::DuplexLongSide},
{"DuplexShortSide", QPrinter::DuplexShortSide},
};
const FlagData colorModeComboData[] =
{
{"GrayScale", QPrinter::GrayScale},
{"Color", QPrinter::Color},
};
const FlagData unitsComboData[] =
{
{"Millimeter", QPageLayout::Millimeter},
{"Inch", QPageLayout::Inch},
{"Point", QPageLayout::Point},
{"Pica", QPageLayout::Pica},
{"Didot", QPageLayout::Didot},
{"Cicero", QPageLayout::Cicero},
};
const FlagData orientationComboData[] =
{
{"Portrait", QPageLayout::Portrait},
{"Landscape", QPageLayout::Landscape},
};
const FlagData layoutModeComboData[] =
{
{"StandardMode", QPageLayout::StandardMode},
{"FullPageMode", QPageLayout::FullPageMode},
};
const FlagData printDialogOptions[] =
{
{"PrintToFile", QPrintDialog::PrintToFile},
{"PrintSelection", QPrintDialog::PrintSelection},
{"PrintPageRange", QPrintDialog::PrintPageRange},
{"PrintShowPageSize", QPrintDialog::PrintShowPageSize},
{"PrintCollateCopies", QPrintDialog::PrintCollateCopies},
{"PrintCurrentPage", QPrintDialog::PrintCurrentPage}
};
QTextStream &operator<<(QTextStream &s, const QSizeF &size)
{
s << size.width() << 'x' << size.height();
return s;
}
QTextStream &operator<<(QTextStream &s, const QRectF &rect)
{
s << rect.width() << 'x' << rect.height() << Qt::forcesign << rect.x() << rect.y()
<< Qt::noforcesign;
return s;
}
QTextStream &operator<<(QTextStream &s, const QPrinter &printer)
{
const auto pageLayout = printer.pageLayout();
const auto pageSize = pageLayout.pageSize();
s << '"' << printer.printerName() << "\"\nPaper #" << pageSize.id()
<< " \"" << pageSize.name() << '"'
<< (pageLayout.orientation() == QPageLayout::Portrait ? ", Portrait" : ", Landscape");
if (printer.fullPage())
s << ", full page";
s << "\nPaper size: "
<< pageSize.sizePoints() << "pt "
<< pageSize.size(QPageSize::Millimeter) << "mm "
<< "\n " << pageSize.sizePixels(printer.resolution()) << " device pt "
<< pageSize.size(QPageSize::Inch) << "inch "
<< "\n " << pageSize.size(QPageSize::Millimeter) << "mm"
<< "\nLogical resolution : " << printer.logicalDpiX() << ',' << printer.logicalDpiY() << "DPI"
<< "\nPhysical resolution: " << printer.physicalDpiX() << ',' << printer.physicalDpiY() << "DPI"
<< "\nPaperRect: " << printer.paperRect(QPrinter::Point) << "pt "
<< printer.paperRect(QPrinter::Millimeter) << "mm "
<< "\n " << printer.paperRect(QPrinter::DevicePixel) << "device pt"
<< "\nPageRect: " << printer.pageRect(QPrinter::Point) << "pt "
<< printer.pageRect(QPrinter::Millimeter) << "mm "
<< "\n " << printer.pageRect(QPrinter::DevicePixel) << "device pt";
return s;
}
// Print a page with a rectangular frame, vertical / horizontal rulers in cm and printer info.
static void drawHorizCmRuler(QPainter &painter, int x1, int x2, int y)
{
painter.drawLine(x1, y, x2, y);
const int dpI = painter.device()->logicalDpiX();
const int dpCm = qRound(double(dpI) / 2.54);
const int h = dpCm / 2;
const QFontMetrics fm(painter.font());
for (int cm = 0, x = x1; x < x2; x += dpCm, ++cm) {
painter.drawLine(x, y, x, y - h);
if (cm) {
const QString n = QString::number(cm);
const QRect br = fm.boundingRect(n);
painter.drawText(x - br.width() / 2, y - h - 10, n);
}
}
}
static void drawVertCmRuler(QPainter &painter, int x, int y1, int y2)
{
painter.drawLine(x, y1, x, y2);
const int dpI = painter.device()->logicalDpiY();
const int dpCm = qRound(double(dpI) / 2.54);
const int h = dpCm / 2;
const QFontMetrics fm(painter.font());
for (int cm = 0, y = y1; y < y2; y += dpCm, ++cm) {
painter.drawLine(x, y, x + h, y);
if (cm) {
const QString n = QString::number(cm);
const QRect br = fm.boundingRect(n);
painter.drawText(x + h + 10, y + br.height() / 2, n);
}
}
}
static bool print(QPrinter *printer, QString *errorMessage)
{
QPainter painter;
if (!printer->isValid()) {
*errorMessage = QLatin1String("Invalid printer.");
return false;
}
if (printer->printerState() != QPrinter::Idle) {
*errorMessage = QLatin1String("Printer not idle (state ")
+ QString::number(printer->printerState())
+ QLatin1String(").");
return false;
}
if (!painter.begin(printer)) {
*errorMessage = QLatin1String("QPainter::begin() failed.");
return false;
}
const QRectF pageF = printer->pageRect(QPrinter::DevicePixel);
QFont font = painter.font();
font.setFamily("Courier");
font.setPointSize(10);
// Format message.
const int charHeight = QFontMetrics(font).boundingRect('X').height();
QString msg;
QTextStream str(&msg);
str << "Qt "<< QT_VERSION_STR;
str << ' ' << QGuiApplication::platformName();
str << ' ' << QDateTime::currentDateTime().toString()
<< "\nFont: " << font.family() << ' ' << font.pointSize() << '\n'
<< *printer;
if (!painter.device()->logicalDpiY() || !painter.device()->logicalDpiX()) {
*errorMessage = QLatin1String("Bailing out due to invalid DPI.");
return false;
}
painter.drawRect(pageF);
drawHorizCmRuler(painter, pageF.x(), pageF.right(), pageF.height() /2);
drawVertCmRuler(painter, pageF.x() + pageF.width() / 2, pageF.top(), pageF.bottom());
painter.setFont(font);
QPointF textPoint = pageF.topLeft() + QPoint(10, charHeight + 10);
foreach (const QString &line, msg.split('\n')) {
painter.drawText(textPoint, line);
textPoint.ry() += (15 * charHeight) / 10;
}
if (!painter.end()) {
*errorMessage = QLatin1String("QPainter::end() failed.");
return false;
}
return true;
}
static bool print(QPrinter *printer, QWidget *dialogParent)
{
QString errorMessage;
const bool result = print(printer, &errorMessage);
if (!result)
QMessageBox::warning(dialogParent, QLatin1String("Printing Failed"), errorMessage);
return result;
}
class PrintPreviewDialog : public QPrintPreviewDialog {
Q_OBJECT
public:
explicit PrintPreviewDialog(QPrinter *printer, QWidget *parent = nullptr) : QPrintPreviewDialog(printer, parent)
{
connect(this, SIGNAL(paintRequested(QPrinter*)), this, SLOT(slotPaintRequested(QPrinter*)));
}
public slots:
void slotPaintRequested(QPrinter *p) { print(p, this); }
};
PrintDialogPanel::PrintDialogPanel(QWidget *parent)
: QWidget(parent), m_blockSignals(true)
{
m_panel.setupUi(this);
// Setup the Create box
populateCombo(m_panel.m_printerModeCombo, printerModeComboData, sizeof(printerModeComboData)/sizeof(FlagData));
connect(m_panel.m_createButton, SIGNAL(clicked()), this, SLOT(createPrinter()));
connect(m_panel.m_deleteButton, SIGNAL(clicked()), this, SLOT(deletePrinter()));
// Setup the Page Layout box
populateCombo(m_panel.m_unitsCombo, unitsComboData, sizeof(unitsComboData)/sizeof(FlagData));
connect(m_panel.m_unitsCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(unitsChanged()));
for (int i = QPageSize::A4; i < QPageSize::LastPageSize; ++i) {
QPageSize::PageSizeId id = QPageSize::PageSizeId(i);
m_panel.m_pageSizeCombo->addItem(QPageSize::name(id), QVariant(id));
}
connect(m_panel.m_pageSizeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(pageSizeChanged()));
connect(m_panel.m_pageWidth, SIGNAL(valueChanged(double)), this, SLOT(pageDimensionsChanged()));
connect(m_panel.m_pageHeight, SIGNAL(valueChanged(double)), this, SLOT(pageDimensionsChanged()));
populateCombo(m_panel.m_orientationCombo, orientationComboData, sizeof(orientationComboData)/sizeof(FlagData));
connect(m_panel.m_orientationCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(orientationChanged()));
connect(m_panel.m_leftMargin, SIGNAL(valueChanged(double)), this, SLOT(marginsChanged()));
connect(m_panel.m_topMargin, SIGNAL(valueChanged(double)), this, SLOT(marginsChanged()));
connect(m_panel.m_rightMargin, SIGNAL(valueChanged(double)), this, SLOT(marginsChanged()));
connect(m_panel.m_bottomMargin, SIGNAL(valueChanged(double)), this, SLOT(marginsChanged()));
populateCombo(m_panel.m_layoutModeCombo, layoutModeComboData, sizeof(layoutModeComboData)/sizeof(FlagData));
connect(m_panel.m_layoutModeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(layoutModeChanged()));
// Setup the Print Job box
m_panel.m_printerCombo->addItem(tr("Print to PDF"), QVariant("PdfFormat"));
foreach (const QString &name, QPrinterInfo::availablePrinterNames())
m_panel.m_printerCombo->addItem(name, QVariant(name));
connect(m_panel.m_printerCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(printerChanged()));
populateCombo(m_panel.m_printRangeCombo, printRangeComboData, sizeof(printRangeComboData)/sizeof(FlagData));
populateCombo(m_panel.m_pageOrderCombo, pageOrderComboData, sizeof(pageOrderComboData)/sizeof(FlagData));
populateCombo(m_panel.m_duplexModeCombo, duplexModeComboData, sizeof(duplexModeComboData)/sizeof(FlagData));
populateCombo(m_panel.m_paperSourceCombo, paperSourceComboData, sizeof(paperSourceComboData)/sizeof(FlagData));
populateCombo(m_panel.m_colorModeCombo, colorModeComboData, sizeof(colorModeComboData)/sizeof(FlagData));
// Setup the Dialogs box
m_panel.m_dialogOptionsGroupBox->populateOptions(printDialogOptions, sizeof(printDialogOptions) / sizeof(FlagData));
QPrintDialog dialog;
m_panel.m_dialogOptionsGroupBox->setValue(dialog.options());
connect(m_panel.m_printButton, SIGNAL(clicked()), this, SLOT(showPrintDialog()));
connect(m_panel.m_printPreviewButton, SIGNAL(clicked()), this, SLOT(showPreviewDialog()));
connect(m_panel.m_pageSetupButton, SIGNAL(clicked()), this, SLOT(showPageSetupDialog()));
connect(m_panel.m_directPrintButton, SIGNAL(clicked()), this, SLOT(directPrint()));
enablePanels();
m_blockSignals = false;
}
PrintDialogPanel::~PrintDialogPanel()
{
}
void PrintDialogPanel::enablePanels()
{
const bool exists = !m_printer.isNull();
m_panel.m_createButton->setEnabled(!exists);
m_panel.m_printerModeCombo->setEnabled(!exists);
m_panel.m_deleteButton->setEnabled(exists);
m_panel.m_pageLayoutGroupBox->setEnabled(exists);
m_panel.m_printJobGroupBox->setEnabled(exists);
m_panel.m_dialogsGroupBox->setEnabled(exists);
}
void PrintDialogPanel::createPrinter()
{
const QPrinter::PrinterMode mode = comboBoxValue<QPrinter::PrinterMode>(m_panel.m_printerModeCombo);
m_printer.reset(new QPrinter(mode)); // Can set only once.
retrieveSettings(m_printer.data());
enablePanels();
}
void PrintDialogPanel::deletePrinter()
{
m_printer.reset();
enablePanels();
}
QSizeF PrintDialogPanel::customPageSize() const
{
return QSizeF(m_panel.m_pageWidth->value(), m_panel.m_pageHeight->value());
}
// Apply the settings to the QPrinter
void PrintDialogPanel::applySettings(QPrinter *printer) const
{
const int currentIndex = m_panel.m_printerCombo->currentIndex();
QString printerName = m_panel.m_printerCombo->itemData(currentIndex).toString();
if (printerName == QLatin1String("PdfFormat"))
printer->setOutputFileName(m_panel.m_fileName->text());
else
printer->setPrinterName(printerName);
printer->setPrintRange(comboBoxValue<QPrinter::PrintRange>(m_panel.m_printRangeCombo));
printer->setFromTo(m_panel.m_fromPage->value(), m_panel.m_toPage->value());
printer->setPageOrder(comboBoxValue<QPrinter::PageOrder>(m_panel.m_pageOrderCombo));
printer->setCopyCount(m_panel.m_copyCount->value());
printer->setCollateCopies(m_panel.m_collateCopies->isChecked());
printer->setDuplex(comboBoxValue<QPrinter::DuplexMode>(m_panel.m_duplexModeCombo));
printer->setPaperSource(comboBoxValue<QPrinter::PaperSource>(m_panel.m_paperSourceCombo));
printer->setColorMode(comboBoxValue<QPrinter::ColorMode>(m_panel.m_colorModeCombo));
printer->setResolution(m_panel.m_resolution->value());
printer->setPageLayout(m_pageLayout);
}
// Retrieve the settings from the QPrinter
void PrintDialogPanel::retrieveSettings(const QPrinter *printer)
{
if (printer->outputFormat() == QPrinter::NativeFormat) {
m_panel.m_printerCombo->setCurrentIndex(m_panel.m_printerCombo->findData(QVariant(printer->printerName())));
m_panel.m_fileName->setEnabled(false);
} else {
m_panel.m_printerCombo->setCurrentIndex(m_panel.m_printerCombo->findData(QVariant(QLatin1String("PdfFormat"))));
m_panel.m_fileName->setEnabled(true);
}
m_panel.m_fileName->setText(printer->outputFileName());
setComboBoxValue(m_panel.m_printRangeCombo, printer->printRange());
m_panel.m_fromPage->setValue(printer->fromPage());
m_panel.m_toPage->setValue(printer->toPage());
setComboBoxValue(m_panel.m_pageOrderCombo, printer->pageOrder());
m_panel.m_copyCount->setValue(printer->copyCount());
m_panel.m_collateCopies->setChecked(printer->collateCopies());
setComboBoxValue(m_panel.m_duplexModeCombo, printer->duplex());
setComboBoxValue(m_panel.m_paperSourceCombo, printer->paperSource());
setComboBoxValue(m_panel.m_colorModeCombo, printer->colorMode());
m_panel.m_resolution->setValue(printer->resolution());
#ifdef Q_OS_WIN
QString availPaperSources;
foreach (QPrinter::PaperSource ps, printer->supportedPaperSources())
availPaperSources += QString::number(int(ps)) + QLatin1Char(' ');
m_panel.availPaperSourceLabel->setText(availPaperSources);
#else
m_panel.availPaperSourceLabel->setText(QLatin1String("N/A"));
#endif
m_pageLayout = printer->pageLayout();
updatePageLayoutWidgets();
}
void PrintDialogPanel::updatePageLayoutWidgets()
{
m_blockSignals = true;
setComboBoxValue(m_panel.m_unitsCombo, m_pageLayout.units());
setComboBoxValue(m_panel.m_pageSizeCombo, m_pageLayout.pageSize().id());
QSizeF sizef = m_pageLayout.pageSize().size(QPageSize::Unit(m_pageLayout.units()));
bool custom = (m_pageLayout.pageSize().id() == QPageSize::Custom);
setComboBoxValue(m_panel.m_orientationCombo, m_pageLayout.orientation());
m_panel.m_leftMargin->setValue(m_pageLayout.margins().left());
m_panel.m_topMargin->setValue(m_pageLayout.margins().top());
m_panel.m_rightMargin->setValue(m_pageLayout.margins().right());
m_panel.m_bottomMargin->setValue(m_pageLayout.margins().bottom());
setComboBoxValue(m_panel.m_layoutModeCombo, m_pageLayout.mode());
QRectF rectf = m_pageLayout.paintRect();
m_panel.m_pageWidth->setValue(sizef.width());
m_panel.m_pageHeight->setValue(sizef.height());
m_panel.m_pageWidth->setEnabled(custom);
m_panel.m_pageHeight->setEnabled(custom);
m_panel.m_rectX->setValue(rectf.x());
m_panel.m_rectY->setValue(rectf.y());
m_panel.m_rectWidth->setValue(rectf.width());
m_panel.m_rectHeight->setValue(rectf.height());
QString suffix;
switch (comboBoxValue<QPageLayout::Unit>(m_panel.m_unitsCombo)) {
case QPageLayout::Millimeter:
suffix = tr(" mm");
break;
case QPageLayout::Point:
suffix = tr(" pt");
break;
case QPageLayout::Inch:
suffix = tr(" in");
break;
case QPageLayout::Pica:
suffix = tr(" pc");
break;
case QPageLayout::Didot:
suffix = tr(" DD");
break;
case QPageLayout::Cicero:
suffix = tr(" CC");
break;
}
m_panel.m_pageWidth->setSuffix(suffix);
m_panel.m_pageHeight->setSuffix(suffix);
m_panel.m_leftMargin->setSuffix(suffix);
m_panel.m_topMargin->setSuffix(suffix);
m_panel.m_rightMargin->setSuffix(suffix);
m_panel.m_bottomMargin->setSuffix(suffix);
m_panel.m_rectX->setSuffix(suffix);
m_panel.m_rectY->setSuffix(suffix);
m_panel.m_rectWidth->setSuffix(suffix);
m_panel.m_rectHeight->setSuffix(suffix);
m_blockSignals = false;
}
void PrintDialogPanel::unitsChanged()
{
if (m_blockSignals)
return;
m_pageLayout.setUnits(comboBoxValue<QPageLayout::Unit>(m_panel.m_unitsCombo));
updatePageLayoutWidgets();
}
void PrintDialogPanel::pageSizeChanged()
{
if (m_blockSignals)
return;
const QPageSize::PageSizeId pageSizeId = comboBoxValue<QPageSize::PageSizeId>(m_panel.m_pageSizeCombo);
QPageSize pageSize;
if (pageSizeId == QPageSize::Custom)
pageSize = QPageSize(QSizeF(200, 200), QPageSize::Unit(m_pageLayout.units()));
else
pageSize = QPageSize(pageSizeId);
m_pageLayout.setPageSize(pageSize);
updatePageLayoutWidgets();
}
void PrintDialogPanel::pageDimensionsChanged()
{
if (m_blockSignals)
return;
m_pageLayout.setPageSize(QPageSize(customPageSize(), QPageSize::Unit(m_pageLayout.units())));
updatePageLayoutWidgets();
}
void PrintDialogPanel::orientationChanged()
{
if (m_blockSignals)
return;
m_pageLayout.setOrientation(comboBoxValue<QPageLayout::Orientation>(m_panel.m_orientationCombo));
updatePageLayoutWidgets();
}
void PrintDialogPanel::marginsChanged()
{
if (m_blockSignals)
return;
m_pageLayout.setMargins(QMarginsF(m_panel.m_leftMargin->value(), m_panel.m_topMargin->value(),
m_panel.m_rightMargin->value(), m_panel.m_bottomMargin->value()));
updatePageLayoutWidgets();
}
void PrintDialogPanel::layoutModeChanged()
{
if (m_blockSignals)
return;
m_pageLayout.setMode(comboBoxValue<QPageLayout::Mode>(m_panel.m_layoutModeCombo));
updatePageLayoutWidgets();
}
void PrintDialogPanel::printerChanged()
{
const int currentIndex = m_panel.m_printerCombo->currentIndex();
const bool isPdf = (m_panel.m_printerCombo->itemData(currentIndex).toString() == QLatin1String("PdfFormat"));
m_panel.m_fileName->setEnabled(isPdf);
if (isPdf && m_panel.m_fileName->text().isEmpty())
m_panel.m_fileName->setText(QDir::homePath() + QDir::separator() + QLatin1String("print.pdf"));
}
void PrintDialogPanel::showPrintDialog()
{
applySettings(m_printer.data());
QPrintDialog dialog(m_printer.data(), this);
dialog.setOptions(m_panel.m_dialogOptionsGroupBox->value<QPrintDialog::PrintDialogOptions>());
if (dialog.exec() == QDialog::Accepted) {
retrieveSettings(m_printer.data());
print(m_printer.data(), this);
}
}
void PrintDialogPanel::showPreviewDialog()
{
applySettings(m_printer.data());
PrintPreviewDialog dialog(m_printer.data(), this);
const QSize availableSize = screen()->availableSize();
dialog.resize(availableSize * 4/ 5);
if (dialog.exec() == QDialog::Accepted)
retrieveSettings(m_printer.data());
}
void PrintDialogPanel::showPageSetupDialog()
{
applySettings(m_printer.data());
QPageSetupDialog dialog(m_printer.data(), this);
if (dialog.exec() == QDialog::Accepted)
retrieveSettings(m_printer.data());
}
void PrintDialogPanel::directPrint()
{
applySettings(m_printer.data());
print(m_printer.data(), this);
retrieveSettings(m_printer.data());
}
#include "moc_printdialogpanel.cpp"
#include "printdialogpanel.moc"
#endif // !QT_NO_PRINTER

View File

@ -0,0 +1,63 @@
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef PRINTDIALOGPANEL_H
#define PRINTDIALOGPANEL_H
#ifndef QT_NO_PRINTER
#include "ui_printdialogpanel.h"
#include <QPageLayout>
#include <QPrinter>
#include <QWidget>
QT_BEGIN_NAMESPACE
class QPrinter;
class QComboBox;
class QGroupBox;
class QPushButton;
class QCheckBox;
QT_END_NAMESPACE
class PageSizeControl;
class OptionsControl;
class PrintDialogPanel : public QWidget
{
Q_OBJECT
public:
explicit PrintDialogPanel(QWidget *parent = nullptr);
~PrintDialogPanel();
private slots:
void createPrinter();
void deletePrinter();
void showPrintDialog();
void showPreviewDialog();
void showPageSetupDialog();
void directPrint();
void unitsChanged();
void pageSizeChanged();
void pageDimensionsChanged();
void orientationChanged();
void marginsChanged();
void layoutModeChanged();
void printerChanged();
private:
QSizeF customPageSize() const;
void applySettings(QPrinter *printer) const;
void retrieveSettings(const QPrinter *printer);
void updatePageLayoutWidgets();
void enablePanels();
bool m_blockSignals;
Ui::PrintDialogPanel m_panel;
QPageLayout m_pageLayout;
QScopedPointer<QPrinter> m_printer;
};
#endif // !QT_NO_PRINTER
#endif // PRINTDIALOGPANEL_H

View File

@ -0,0 +1,694 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>PrintDialogPanel</class>
<widget class="QWidget" name="PrintDialogPanel">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>650</width>
<height>707</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="m_createGroupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Create</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Printer Mode:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="m_printerModeCombo"/>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="m_createButton">
<property name="text">
<string>Create</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_deleteButton">
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="m_pageLayoutGroupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Page Layout</string>
</property>
<layout class="QFormLayout" name="formLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Units:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="m_unitsCombo"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Page Size:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="m_pageSizeCombo"/>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QDoubleSpinBox" name="m_pageWidth">
<property name="suffix">
<string> mm</string>
</property>
<property name="maximum">
<double>1000.000000000000000</double>
</property>
<property name="singleStep">
<double>10.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>x</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="m_pageHeight">
<property name="suffix">
<string> mm</string>
</property>
<property name="maximum">
<double>1000.000000000000000</double>
</property>
<property name="singleStep">
<double>10.000000000000000</double>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Orientation:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="m_orientationCombo"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Margins:</string>
</property>
</widget>
</item>
<item row="4" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QDoubleSpinBox" name="m_leftMargin">
<property name="maximum">
<double>1000.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_8">
<property name="text">
<string>l</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="m_rightMargin">
<property name="maximum">
<double>1000.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_9">
<property name="text">
<string>t</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="m_topMargin">
<property name="maximum">
<double>1000.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_10">
<property name="text">
<string>r</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="m_bottomMargin">
<property name="maximum">
<double>1000.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_11">
<property name="text">
<string>b</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Layout Mode:</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QComboBox" name="m_layoutModeCombo"/>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_16">
<property name="text">
<string>Paint Rect:</string>
</property>
</widget>
</item>
<item row="6" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QDoubleSpinBox" name="m_rectX">
<property name="enabled">
<bool>false</bool>
</property>
<property name="buttonSymbols">
<enum>QAbstractSpinBox::NoButtons</enum>
</property>
<property name="suffix">
<string> mm</string>
</property>
<property name="maximum">
<double>1000.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_12">
<property name="text">
<string>x</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="m_rectY">
<property name="enabled">
<bool>false</bool>
</property>
<property name="buttonSymbols">
<enum>QAbstractSpinBox::NoButtons</enum>
</property>
<property name="maximum">
<double>1000.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_13">
<property name="text">
<string>y</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="m_rectWidth">
<property name="enabled">
<bool>false</bool>
</property>
<property name="buttonSymbols">
<enum>QAbstractSpinBox::NoButtons</enum>
</property>
<property name="maximum">
<double>1000.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_14">
<property name="text">
<string>w</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="m_rectHeight">
<property name="enabled">
<bool>false</bool>
</property>
<property name="buttonSymbols">
<enum>QAbstractSpinBox::NoButtons</enum>
</property>
<property name="maximum">
<double>1000.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_15">
<property name="text">
<string>h</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="m_printJobGroupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Print Job</string>
</property>
<layout class="QFormLayout" name="formLayout_3">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_17">
<property name="text">
<string>Printer:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="m_printerCombo"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_18">
<property name="text">
<string>File Name:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="m_fileName"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_19">
<property name="text">
<string>Print Range:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="m_printRangeCombo"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_20">
<property name="text">
<string>Page Range:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QSpinBox" name="m_fromPage">
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>100</number>
</property>
<property name="value">
<number>0</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_21">
<property name="text">
<string>to</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="m_toPage">
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>100</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_25">
<property name="text">
<string>Copies:</string>
</property>
</widget>
</item>
<item row="5" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QSpinBox" name="m_copyCount">
<property name="maximum">
<number>100</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_7">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="m_collateCopies">
<property name="text">
<string>Collate Copies</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_22">
<property name="text">
<string>Duplex Mode:</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QComboBox" name="m_duplexModeCombo"/>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_27">
<property name="text">
<string>Paper Source:</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QComboBox" name="m_paperSourceCombo"/>
</item>
<item row="9" column="0">
<widget class="QLabel" name="label_23">
<property name="text">
<string>Color Mode:</string>
</property>
</widget>
</item>
<item row="9" column="1">
<widget class="QComboBox" name="m_colorModeCombo"/>
</item>
<item row="10" column="0">
<widget class="QLabel" name="label_28">
<property name="text">
<string>Resolution:</string>
</property>
</widget>
</item>
<item row="10" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_7">
<item>
<widget class="QSpinBox" name="m_resolution">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>5000</number>
</property>
<property name="singleStep">
<number>100</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_6">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_24">
<property name="text">
<string>Page Order:</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QComboBox" name="m_pageOrderCombo"/>
</item>
<item row="8" column="0">
<widget class="QLabel" name="availPaperSourceDescLabel">
<property name="text">
<string>Available Paper Sources:</string>
</property>
</widget>
</item>
<item row="8" column="1">
<widget class="QLabel" name="availPaperSourceLabel"/>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>22</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="0" column="1">
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QGroupBox" name="m_dialogsGroupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Dialogs</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="OptionsControl" name="m_dialogOptionsGroupBox">
<property name="title">
<string>Options</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_printButton">
<property name="text">
<string>Print...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_printPreviewButton">
<property name="text">
<string>Preview...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_pageSetupButton">
<property name="text">
<string>Page Setup...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_directPrintButton">
<property name="text">
<string>Direct Print</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>80</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>OptionsControl</class>
<extends>QGroupBox</extends>
<header>utils.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -0,0 +1,105 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "utils.h"
#include <QCheckBox>
#include <QGridLayout>
#include <QPushButton>
#include <QVBoxLayout>
QComboBox *createCombo(QWidget *parent, const FlagData *d, size_t size)
{
QComboBox *c = new QComboBox(parent);
for (size_t i = 0; i < size; ++i)
c->addItem(QLatin1String(d[i].description), QVariant(d[i].value));
return c;
}
void populateCombo(QComboBox *combo, const FlagData *d, size_t size)
{
for (size_t i = 0; i < size; ++i)
combo->addItem(QLatin1String(d[i].description), QVariant(d[i].value));
}
void setComboBoxValue(QComboBox *c, int v)
{
c->setCurrentIndex(c->findData(QVariant(v)));
}
OptionsControl::OptionsControl(QWidget *parent)
: QGroupBox(parent)
{
setLayout(new QVBoxLayout(this));
}
OptionsControl::OptionsControl(const QString &title, const FlagData *data, size_t count, QWidget *parent)
: QGroupBox(title, parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
for (size_t i = 0; i < count; ++i) {
QCheckBox *box = new QCheckBox(QString::fromLatin1(data[i].description));
m_checkBoxes.push_back(CheckBoxFlagPair(box, data[i].value));
layout->addWidget(box);
}
}
void OptionsControl::populateOptions(const FlagData *data, size_t count)
{
for (size_t i = 0; i < count; ++i) {
QCheckBox *box = new QCheckBox(QString::fromLatin1(data[i].description));
m_checkBoxes.push_back(CheckBoxFlagPair(box, data[i].value));
layout()->addWidget(box);
}
}
void OptionsControl::setValue(int flags)
{
foreach (const CheckBoxFlagPair &cf, m_checkBoxes)
cf.first->setChecked(cf.second & flags);
}
int OptionsControl::intValue() const
{
int result = 0;
foreach (const CheckBoxFlagPair &cf, m_checkBoxes) {
if (cf.first->isChecked())
result |= cf.second;
}
return result;
}
QPushButton *addButton(const QString &description, QGridLayout *layout, int &row, int column,
QObject *receiver, const char *slotFunc)
{
QPushButton *button = new QPushButton(description);
QObject::connect(button, SIGNAL(clicked()), receiver, slotFunc);
layout->addWidget(button, row++, column);
return button;
}
QPushButton *addButton(const QString &description, QGridLayout *layout, int &row, int column,
std::function<void()> fn)
{
QPushButton *button = new QPushButton(description);
QObject::connect(button, &QPushButton::clicked, fn);
layout->addWidget(button, row++, column);
return button;
}
QPushButton *addButton(const QString &description, QVBoxLayout *layout, QObject *receiver,
const char *slotFunc)
{
QPushButton *button = new QPushButton(description);
QObject::connect(button, SIGNAL(clicked()), receiver, slotFunc);
layout->addWidget(button);
return button;
}
QPushButton *addButton(const QString &description, QVBoxLayout *layout, std::function<void()> fn)
{
QPushButton *button = new QPushButton(description);
QObject::connect(button, &QPushButton::clicked, fn);
layout->addWidget(button);
return button;
}

View File

@ -0,0 +1,70 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef UTILS_H
#define UTILS_H
#include <QComboBox>
#include <QGroupBox>
#include <QVariant>
#include <QPair>
#include <QList>
#include <functional>
QT_FORWARD_DECLARE_CLASS(QCheckBox)
QT_FORWARD_DECLARE_CLASS(QGridLayout)
QT_FORWARD_DECLARE_CLASS(QVBoxLayout)
QT_FORWARD_DECLARE_CLASS(QPushButton)
// Associate enum/flag value with a description.
struct FlagData
{
const char *description;
int value;
};
// Helpers for creating combo boxes representing enumeration values from flag data.
QComboBox *createCombo(QWidget *parent, const FlagData *d, size_t size);
void populateCombo(QComboBox *combo, const FlagData *d, size_t size);
template <class Enum>
Enum comboBoxValue(const QComboBox *c)
{
return static_cast<Enum>(c->itemData(c->currentIndex()).toInt());
}
void setComboBoxValue(QComboBox *c, int v);
// A group box with check boxes for option flags.
class OptionsControl : public QGroupBox {
public:
OptionsControl(QWidget *parent);
explicit OptionsControl(const QString &title, const FlagData *data, size_t count, QWidget *parent);
void populateOptions(const FlagData *data, size_t count);
void setValue(int flags);
template <class Enum>
Enum value() const { return static_cast<Enum>(intValue()); }
private:
typedef QPair<QCheckBox *, int> CheckBoxFlagPair;
int intValue() const;
QList<CheckBoxFlagPair> m_checkBoxes;
};
QPushButton *addButton(const QString &description, QGridLayout *layout, int &row, int column,
QObject *receiver, const char *slotFunc);
QPushButton *addButton(const QString &description, QGridLayout *layout, int &row, int column,
std::function<void()> fn);
QPushButton *addButton(const QString &description, QVBoxLayout *layout, QObject *receiver,
const char *slotFunc);
QPushButton *addButton(const QString &description, QVBoxLayout *layout, std::function<void()> fn);
#endif // UTILS_H

View File

@ -0,0 +1,339 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "wizardpanel.h"
#include <QWizard>
#include <QWizardPage>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QRadioButton>
#include <QPushButton>
#include <QCheckBox>
#include <QButtonGroup>
#include <QSpacerItem>
#include <QGroupBox>
#include <QLabel>
#include <QStyle>
#include <QIcon>
#include <QImage>
#include <QPainter>
#include <QFont>
#include <QFontMetrics>
#include <QHash>
static QIcon coloredIcon(const Qt::GlobalColor color)
{
QImage image(QSize(24, 24), QImage::Format_RGB32);
image.fill(color);
return QIcon(QPixmap::fromImage(image));
}
static QPixmap pixmapWithText(const QString &text, const QColor color)
{
QFont font;
QFontMetrics metric(font);
QRect rectangle = metric.boundingRect(text);
rectangle.setBottomRight(rectangle.bottomRight() + QPoint(20, 20));
QImage image(rectangle.size(), QImage::Format_RGB32);
image.fill(color);
QPainter painter(&image);
painter.setFont(font);
painter.drawText(rectangle, Qt::AlignHCenter | Qt::AlignVCenter, text);
return QPixmap::fromImage(image);
}
// A radio-group control for QWizard::WizardStyle.
class WizardStyleControl : public QGroupBox
{
Q_OBJECT
public:
WizardStyleControl(QWidget *parent = nullptr);
void setWizardStyle(int style);
QWizard::WizardStyle wizardStyle() const;
signals:
void wizardStyleChanged(int);
private:
QButtonGroup *m_group;
};
WizardStyleControl::WizardStyleControl(QWidget *parent)
: QGroupBox(tr("Style"), parent)
, m_group(new QButtonGroup(this))
{
m_group->setExclusive(true);
connect(m_group, &QButtonGroup::idClicked, this, &WizardStyleControl::wizardStyleChanged);
QVBoxLayout *vLayout = new QVBoxLayout(this);
QRadioButton *radioButton = new QRadioButton(tr("None/OS Default"), this);
m_group->addButton(radioButton, QWizard::NStyles);
vLayout->addWidget(radioButton);
radioButton = new QRadioButton(tr("ClassicStyle"), this);
m_group->addButton(radioButton, QWizard::ClassicStyle);
vLayout->addWidget(radioButton);
radioButton = new QRadioButton(tr("ModernStyle"), this);
m_group->addButton(radioButton, QWizard::ModernStyle);
vLayout->addWidget(radioButton);
radioButton = new QRadioButton(tr("MacStyle"), this);
m_group->addButton(radioButton, QWizard::MacStyle);
vLayout->addWidget(radioButton);
radioButton = new QRadioButton(tr("AeroStyle"), this);
m_group->addButton(radioButton, QWizard::AeroStyle);
vLayout->addWidget(radioButton);
vLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
setWizardStyle(style()->styleHint(QStyle::SH_WizardStyle));
}
QWizard::WizardStyle WizardStyleControl::wizardStyle() const
{
return static_cast<QWizard::WizardStyle>(m_group->checkedId());
}
void WizardStyleControl::setWizardStyle(int wizardStyle)
{
if (wizardStyle < 0 || wizardStyle > QWizard::NStyles)
wizardStyle = QWizard::NStyles;
QAbstractButton *button = m_group->button(wizardStyle);
Q_ASSERT(button);
const bool blocked = m_group->blockSignals(true);
button->setChecked(true);
m_group->blockSignals(blocked);
}
// A control with checkboxes for QWizard::WizardOption.
class WizardOptionsControl : public QGroupBox
{
public:
explicit WizardOptionsControl(QWidget *parent = nullptr);
QWizard::WizardOption wizardOptions() const;
void setWizardOptions(int options);
private:
typedef QHash<int, QCheckBox *> CheckBoxHash;
void addCheckBox(QVBoxLayout *layout, int flag, const QString &title);
CheckBoxHash m_checkBoxes;
};
WizardOptionsControl::WizardOptionsControl(QWidget *parent)
: QGroupBox(tr("Options"), parent)
{
QVBoxLayout *vLayout = new QVBoxLayout(this);
addCheckBox(vLayout, QWizard::IndependentPages, QLatin1String("IndependentPages"));
addCheckBox(vLayout, QWizard::IgnoreSubTitles, QLatin1String("IgnoreSubTitles"));
addCheckBox(vLayout, QWizard::ExtendedWatermarkPixmap, QLatin1String("ExtendedWatermarkPixmap"));
addCheckBox(vLayout, QWizard::NoDefaultButton, QLatin1String("NoDefaultButton"));
addCheckBox(vLayout, QWizard::NoBackButtonOnStartPage, QLatin1String("NoBackButtonOnStartPage"));
addCheckBox(vLayout, QWizard::NoBackButtonOnLastPage, QLatin1String("NoBackButtonOnLastPage"));
addCheckBox(vLayout, QWizard::DisabledBackButtonOnLastPage, QLatin1String("DisabledBackButtonOnLastPage"));
addCheckBox(vLayout, QWizard::HaveNextButtonOnLastPage, QLatin1String("HaveNextButtonOnLastPage"));
addCheckBox(vLayout, QWizard::HaveFinishButtonOnEarlyPages, QLatin1String("HaveFinishButtonOnEarlyPages"));
addCheckBox(vLayout, QWizard::NoCancelButton, QLatin1String("NoCancelButton"));
addCheckBox(vLayout, QWizard::CancelButtonOnLeft, QLatin1String("CancelButtonOnLeft"));
addCheckBox(vLayout, QWizard::HaveHelpButton, QLatin1String("HaveHelpButton"));
addCheckBox(vLayout, QWizard::HelpButtonOnRight, QLatin1String("HelpButtonOnRight"));
addCheckBox(vLayout, QWizard::HaveCustomButton1, QLatin1String("HaveCustomButton1"));
addCheckBox(vLayout, QWizard::HaveCustomButton2, QLatin1String("HaveCustomButton2"));
addCheckBox(vLayout, QWizard::HaveCustomButton3, QLatin1String("HaveCustomButton3"));
vLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
}
void WizardOptionsControl::addCheckBox(QVBoxLayout *layout, int flag, const QString &title)
{
QCheckBox *checkBox = new QCheckBox(title, this);
layout->addWidget(checkBox);
m_checkBoxes.insert(flag, checkBox);
}
QWizard::WizardOption WizardOptionsControl::wizardOptions() const
{
int result = 0;
typedef CheckBoxHash::const_iterator ConstIterator;
const ConstIterator cend = m_checkBoxes.constEnd();
for (ConstIterator it = m_checkBoxes.constBegin(); it != cend; ++it)
if (it.value()->isChecked())
result |= it.key();
return static_cast<QWizard::WizardOption>(result);
}
void WizardOptionsControl::setWizardOptions(int options)
{
typedef CheckBoxHash::iterator Iterator;
const Iterator end = m_checkBoxes.end();
for (Iterator it = m_checkBoxes.begin(); it != end; ++it)
it.value()->setChecked(options & it.key());
}
// A test wizard with a slot to change its style.
class Wizard : public QWizard {
Q_OBJECT
public:
explicit Wizard(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
public slots:
void changeWizardStyle(int newStyle);
};
void Wizard::changeWizardStyle(int newStyle)
{
if (newStyle >= 0 && newStyle < int(QWizard::NStyles))
setWizardStyle(static_cast<QWizard::WizardStyle>(newStyle));
}
// A test wizard page with a WizardStyleControl.
class WizardPage : public QWizardPage
{
public:
explicit WizardPage(const QString &title, QWidget *parent = nullptr);
void initializePage();
private:
WizardStyleControl *m_styleControl;
bool m_firstTimeShown;
};
WizardPage::WizardPage(const QString &title, QWidget *parent)
: QWizardPage(parent)
, m_styleControl(new WizardStyleControl(this))
, m_firstTimeShown(true)
{
setTitle(title);
setSubTitle(title + QLatin1String(" SubTitle"));
QVBoxLayout *vLayout = new QVBoxLayout(this);
vLayout->addWidget(m_styleControl);
}
void WizardPage::initializePage()
{
m_styleControl->setWizardStyle(wizard()->wizardStyle());
if (m_firstTimeShown) {
m_firstTimeShown = false;
connect(m_styleControl, SIGNAL(wizardStyleChanged(int)),
wizard(), SLOT(changeWizardStyle(int)));
}
}
Wizard::Wizard(QWidget *parent, Qt::WindowFlags flags)
: QWizard(parent, flags)
{
setWindowIcon(coloredIcon(Qt::red));
setWindowTitle(QLatin1String("Wizard ") + QLatin1String(QT_VERSION_STR));
addPage(new WizardPage(tr("Page 1"), this));
addPage(new WizardPage(tr("Page 2"), this));
addPage(new WizardPage(tr("Page 3"), this));
}
// A dialog using a Wizard as child widget (emulating Qt Designer).
class WizardEmbeddingDialog : public QDialog {
public:
explicit WizardEmbeddingDialog(QWidget *parent = nullptr);
Wizard *wizard() const { return m_wizard; }
private:
Wizard *m_wizard;
};
WizardEmbeddingDialog::WizardEmbeddingDialog(QWidget *parent)
: QDialog(parent)
, m_wizard(new Wizard)
{
setWindowTitle(QString::fromLatin1("Dialog Embedding QWizard %1").arg(QT_VERSION_STR));
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->addWidget(new QLabel(tr("Above wizard")), 0, 0, 1, 3);
gridLayout->addWidget(new QLabel(tr("Left of wizard")), 1, 0);
m_wizard->setObjectName(QLatin1String("EmbeddedWizard"));
m_wizard->setParent(this, Qt::Widget);
gridLayout->addWidget(m_wizard, 1, 1);
gridLayout->addWidget(new QLabel(tr("Right of wizard")), 1, 2);
gridLayout->addWidget(new QLabel(tr("Below wizard")), 2, 0, 1, 3);
}
WizardPanel::WizardPanel(QWidget *parent)
: QWidget(parent)
, m_styleControl(new WizardStyleControl(this))
, m_optionsControl(new WizardOptionsControl(this))
{
{
QWizard wizard;
m_optionsControl->setWizardOptions(wizard.options());
m_styleControl->setWizardStyle(wizard.wizardStyle());
}
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->addWidget(m_optionsControl, 0, 0, 2, 1);
gridLayout->addWidget(m_styleControl, 0, 1);
QGroupBox *buttonGroupBox = new QGroupBox(this);
QVBoxLayout *vLayout = new QVBoxLayout(buttonGroupBox);
QPushButton *button = new QPushButton(tr("Exec modal"), this);
connect(button, SIGNAL(clicked()), this, SLOT(execModal()));
vLayout->addWidget(button);
button = new QPushButton(tr("Show application modal"), this);
connect(button, &QPushButton::clicked, [this]() { showModal(Qt::ApplicationModal); });
vLayout->addWidget(button);
button = new QPushButton(tr("Show window modal"), this);
connect(button, &QPushButton::clicked, [this]() { showModal(Qt::WindowModal); });
vLayout->addWidget(button);
button = new QPushButton(tr("Show non-modal"), this);
connect(button, SIGNAL(clicked()), this, SLOT(showNonModal()));
vLayout->addWidget(button);
button = new QPushButton(tr("Show embedded"), this);
button->setToolTip(tr("Test QWizard's behavior when used as a widget child."));
connect(button, SIGNAL(clicked()), this, SLOT(showEmbedded()));
vLayout->addWidget(button);
vLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
gridLayout->addWidget(buttonGroupBox, 1, 1);
}
void WizardPanel::execModal()
{
Wizard wizard(this);
applyParameters(&wizard);
wizard.exec();
}
void WizardPanel::showModal(Qt::WindowModality modality)
{
Wizard *wizard = new Wizard(this);
applyParameters(wizard);
wizard->setModal(true);
wizard->setAttribute(Qt::WA_DeleteOnClose);
wizard->setWindowModality(modality);
wizard->show();
}
void WizardPanel::showNonModal()
{
Wizard *wizard = new Wizard(this);
applyParameters(wizard);
wizard->setModal(false);
wizard->setAttribute(Qt::WA_DeleteOnClose);
wizard->show();
}
void WizardPanel::showEmbedded()
{
WizardEmbeddingDialog *dialog = new WizardEmbeddingDialog(this);
applyParameters(dialog->wizard());
dialog->setModal(false);
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show();
}
void WizardPanel::applyParameters(QWizard *wizard) const
{
wizard->setWizardStyle(m_styleControl->wizardStyle());
wizard->setOptions(m_optionsControl->wizardOptions());
wizard->setPixmap(QWizard::WatermarkPixmap, pixmapWithText(QLatin1String("Watermark"), QColor(Qt::blue).lighter()));
wizard->setPixmap(QWizard::LogoPixmap, pixmapWithText(QLatin1String("Logo"), Qt::green));
wizard->setPixmap(QWizard::BannerPixmap, pixmapWithText(QLatin1String("Banner"), Qt::green));
wizard->setPixmap(QWizard::BackgroundPixmap, pixmapWithText(QLatin1String("Background"), QColor(Qt::red).lighter()));
}
#include "wizardpanel.moc"

View File

@ -0,0 +1,34 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef WIZARDPANEL_H
#define WIZARDPANEL_H
#include <QWidget>
class WizardStyleControl;
class WizardOptionsControl;
QT_BEGIN_NAMESPACE
class QWizard;
QT_END_NAMESPACE
class WizardPanel : public QWidget
{
Q_OBJECT
public:
explicit WizardPanel(QWidget *parent = nullptr);
public slots:
void execModal();
void showModal(Qt::WindowModality modality);
void showNonModal();
void showEmbedded();
private:
void applyParameters(QWizard *wizard) const;
WizardStyleControl *m_styleControl;
WizardOptionsControl *m_optionsControl;
};
#endif // WIZARDPANEL_H