From dbb6b58abede12cf9f2fad3086dd59a9d46b7ee7 Mon Sep 17 00:00:00 2001 From: John Layt Date: Sun, 23 Mar 2014 17:07:26 +0100 Subject: QPrintDialog - Convert manual test to .ui file Convert the manual print dialogs test to use a ui file as subsequent changes will add a lot more widgets. Change-Id: I06ac54b67532f0eea1e91a2d9aca4f587d2fa332 Reviewed-by: Friedemann Kleint --- tests/manual/dialogs/dialogs.pro | 1 + tests/manual/dialogs/printdialogpanel.cpp | 157 ++++++----------- tests/manual/dialogs/printdialogpanel.h | 18 +- tests/manual/dialogs/printdialogpanel.ui | 270 ++++++++++++++++++++++++++++++ tests/manual/dialogs/utils.cpp | 21 +++ tests/manual/dialogs/utils.h | 4 + 6 files changed, 357 insertions(+), 114 deletions(-) create mode 100644 tests/manual/dialogs/printdialogpanel.ui (limited to 'tests/manual/dialogs') diff --git a/tests/manual/dialogs/dialogs.pro b/tests/manual/dialogs/dialogs.pro index 71c41119fe..21ebf9b622 100644 --- a/tests/manual/dialogs/dialogs.pro +++ b/tests/manual/dialogs/dialogs.pro @@ -8,3 +8,4 @@ SOURCES += main.cpp filedialogpanel.cpp colordialogpanel.cpp fontdialogpanel.cpp wizardpanel.cpp messageboxpanel.cpp printdialogpanel.cpp utils.cpp HEADERS += filedialogpanel.h colordialogpanel.h fontdialogpanel.h \ wizardpanel.h messageboxpanel.h printdialogpanel.h utils.h +FORMS += printdialogpanel.ui diff --git a/tests/manual/dialogs/printdialogpanel.cpp b/tests/manual/dialogs/printdialogpanel.cpp index 5c89055e22..b012d34db0 100644 --- a/tests/manual/dialogs/printdialogpanel.cpp +++ b/tests/manual/dialogs/printdialogpanel.cpp @@ -65,7 +65,7 @@ #include #include -const FlagData modeComboData[] = +const FlagData printerModeComboData[] = { {"ScreenResolution", QPrinter::ScreenResolution}, {"PrinterResolution", QPrinter::PrinterResolution}, @@ -260,91 +260,30 @@ public slots: void slotPaintRequested(QPrinter *p) { print(p); } }; -class PageSizeControl : public QWidget { -public: - explicit PageSizeControl(QWidget *parent = 0); - QSizeF pageSize() const { return QSizeF(m_width->value(), m_height->value()); } - void setPageSize(const QSizeF &s) { m_width->setValue(s.width()); m_height->setValue(s.height()); } - -private: - QDoubleSpinBox *m_width; - QDoubleSpinBox *m_height; -}; - -PageSizeControl::PageSizeControl(QWidget *parent) - : QWidget(parent) - , m_width(new QDoubleSpinBox(this)) - , m_height(new QDoubleSpinBox(this)) -{ - m_width->setRange(1, 1000); - m_width->setSingleStep(10); - m_height->setRange(1, 1000); - m_height->setSingleStep(10); - QHBoxLayout *hBoxLayout = new QHBoxLayout(this); - hBoxLayout->addWidget(m_width); - hBoxLayout->addWidget(new QLabel("x", this)); - hBoxLayout->addWidget(m_height); - hBoxLayout->addWidget(new QLabel("mm", this)); -} - PrintDialogPanel::PrintDialogPanel(QWidget *parent) : QWidget(parent) - , m_creationGroupBox(new QGroupBox(tr("Create"), this)) - , m_settingsGroupBox(new QGroupBox(tr("Settings"), this)) - , m_dialogsGroupBox(new QGroupBox(tr("Dialogs"), this)) - , m_pageSizeCombo(new QComboBox) { - // Create with resolution - QHBoxLayout *hBoxLayout = new QHBoxLayout(m_creationGroupBox); - m_modeCombo = createCombo(m_creationGroupBox, modeComboData, sizeof(modeComboData)/sizeof(FlagData)); - hBoxLayout->addWidget(m_modeCombo); - m_createButton = new QPushButton(tr("Create"), m_creationGroupBox); - connect(m_createButton, SIGNAL(clicked()), this, SLOT(createPrinter())); - hBoxLayout->addWidget(m_createButton); - m_deleteButton = new QPushButton(tr("Delete"), m_creationGroupBox); - connect(m_deleteButton, SIGNAL(clicked()), this, SLOT(deletePrinter())); - hBoxLayout->addWidget(m_deleteButton); - hBoxLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored)); - - QFormLayout *formLayout = new QFormLayout(m_settingsGroupBox); - m_pageSizeCombo = createCombo(m_settingsGroupBox, pageSizeComboData, sizeof(pageSizeComboData)/sizeof(FlagData)); - connect(m_pageSizeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(enableCustomSizeControl())); - formLayout->addRow(tr("Paper #:"), m_pageSizeCombo); - m_customPageSizeControl = new PageSizeControl; - formLayout->addRow(tr("Custom size:"), m_customPageSizeControl); - m_orientationCombo = createCombo(m_settingsGroupBox, orientationComboData, sizeof(orientationComboData)/sizeof(FlagData)); - formLayout->addRow("Orientation:", m_orientationCombo); - m_fullPageCheckBox = new QCheckBox(tr("Full page"), m_settingsGroupBox); - formLayout->addRow(m_fullPageCheckBox); - - QVBoxLayout *vBoxLayout = new QVBoxLayout(m_dialogsGroupBox); - - m_printDialogOptionsControl = new OptionsControl(tr("Options"), printDialogOptions, sizeof(printDialogOptions) / sizeof(FlagData), m_dialogsGroupBox); - vBoxLayout->addWidget(m_printDialogOptionsControl); - m_printDialogRangeCombo = createCombo(m_dialogsGroupBox, printRangeOptions, sizeof(printRangeOptions) / sizeof(FlagData)); - vBoxLayout->addWidget(m_printDialogRangeCombo); - - { - QPrintDialog dialog; - m_printDialogOptionsControl->setValue(dialog.options()); - m_printDialogRangeCombo->setCurrentIndex(dialog.printRange()); - } - - QPushButton *button = new QPushButton(tr("Print..."), m_dialogsGroupBox); - connect(button, SIGNAL(clicked()), this, SLOT(showPrintDialog())); - vBoxLayout->addWidget(button); - button = new QPushButton(tr("Preview..."), m_dialogsGroupBox); - connect(button, SIGNAL(clicked()), this, SLOT(showPreviewDialog())); - vBoxLayout->addWidget(button); - button = new QPushButton(tr("Page Setup..."), m_dialogsGroupBox); - connect(button, SIGNAL(clicked()), this, SLOT(showPageSetupDialog())); - vBoxLayout->addWidget(button); - - QGridLayout *gridLayout = new QGridLayout(this); - gridLayout->addWidget(m_creationGroupBox, 0, 0); - gridLayout->addWidget(m_settingsGroupBox, 1, 0); - gridLayout->addWidget(m_dialogsGroupBox, 0, 1, 2, 1); - gridLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding), 2, 0, 1, 2); + 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 Settings box + populateCombo(m_panel.m_pageSizeCombo, pageSizeComboData, sizeof(pageSizeComboData)/sizeof(FlagData)); + connect(m_panel.m_pageSizeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(enableCustomSizeControl())); + populateCombo(m_panel.m_orientationCombo, orientationComboData, sizeof(orientationComboData)/sizeof(FlagData)); + + // Setup the Dialogs box + m_panel.m_dialogOptionsGroupBox->populateOptions(printDialogOptions, sizeof(printDialogOptions) / sizeof(FlagData)); + populateCombo(m_panel.m_printDialogRangeCombo, printRangeOptions, sizeof(printRangeOptions) / sizeof(FlagData)); + QPrintDialog dialog; + m_panel.m_dialogOptionsGroupBox->setValue(dialog.options()); + m_panel.m_printDialogRangeCombo->setCurrentIndex(dialog.printRange()); + 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())); enablePanels(); } @@ -356,16 +295,16 @@ PrintDialogPanel::~PrintDialogPanel() void PrintDialogPanel::enablePanels() { const bool exists = !m_printer.isNull(); - m_createButton->setEnabled(!exists); - m_modeCombo->setEnabled(!exists); - m_deleteButton->setEnabled(exists); - m_settingsGroupBox->setEnabled(exists); - m_dialogsGroupBox->setEnabled(exists); + m_panel.m_createButton->setEnabled(!exists); + m_panel.m_printerModeCombo->setEnabled(!exists); + m_panel.m_deleteButton->setEnabled(exists); + m_panel.m_settingsGroupBox->setEnabled(exists); + m_panel.m_dialogsGroupBox->setEnabled(exists); } void PrintDialogPanel::createPrinter() { - const QPrinter::PrinterMode mode = comboBoxValue(m_modeCombo); + const QPrinter::PrinterMode mode = comboBoxValue(m_panel.m_printerModeCombo); m_printer.reset(new QPrinter(mode)); // Can set only once. retrieveSettings(m_printer.data()); enablePanels(); @@ -378,36 +317,49 @@ void PrintDialogPanel::deletePrinter() enablePanels(); } +QSizeF PrintDialogPanel::pageSize() const +{ + return QSizeF(m_panel.m_pageWidth->value(), m_panel.m_pageHeight->value()); +} + +void PrintDialogPanel::setPageSize(const QSizeF &sizef) +{ + m_panel.m_pageWidth->setValue(sizef.width()); + m_panel.m_pageHeight->setValue(sizef.height()); +} + void PrintDialogPanel::applySettings(QPrinter *printer) const { - const QPrinter::PageSize pageSize = comboBoxValue(m_pageSizeCombo); - if (pageSize == QPrinter::Custom) - printer->setPaperSize(m_customPageSizeControl->pageSize(), QPrinter::Millimeter); + const QPrinter::PageSize pageSizeId = comboBoxValue(m_panel.m_pageSizeCombo); + if (pageSizeId == QPrinter::Custom) + printer->setPaperSize(pageSize(), QPrinter::Millimeter); else - printer->setPageSize(pageSize); - printer->setOrientation(comboBoxValue(m_orientationCombo)); - printer->setFullPage(m_fullPageCheckBox->isChecked()); + printer->setPageSize(pageSizeId); + printer->setOrientation(comboBoxValue(m_panel.m_orientationCombo)); + printer->setFullPage(m_panel.m_fullPageCheckBox->isChecked()); } void PrintDialogPanel::retrieveSettings(const QPrinter *printer) { - setComboBoxValue(m_pageSizeCombo, printer->pageSize()); - setComboBoxValue(m_orientationCombo, printer->orientation()); - m_fullPageCheckBox->setChecked(printer->fullPage()); - m_customPageSizeControl->setPageSize(m_printer->paperSize(QPrinter::Millimeter)); + setComboBoxValue(m_panel.m_pageSizeCombo, printer->pageSize()); + setComboBoxValue(m_panel.m_orientationCombo, printer->orientation()); + m_panel.m_fullPageCheckBox->setChecked(printer->fullPage()); + setPageSize(m_printer->paperSize(QPrinter::Millimeter)); } void PrintDialogPanel::enableCustomSizeControl() { - m_customPageSizeControl->setEnabled(m_pageSizeCombo->currentIndex() == QPrinter::Custom); + bool custom = (m_panel.m_pageSizeCombo->currentIndex() == QPrinter::Custom); + m_panel.m_pageWidth->setEnabled(custom); + m_panel.m_pageHeight->setEnabled(custom); } void PrintDialogPanel::showPrintDialog() { applySettings(m_printer.data()); QPrintDialog dialog(m_printer.data(), this); - dialog.setOptions(m_printDialogOptionsControl->value()); - dialog.setPrintRange(comboBoxValue(m_printDialogRangeCombo)); + dialog.setOptions(m_panel.m_dialogOptionsGroupBox->value()); + dialog.setPrintRange(comboBoxValue(m_panel.m_printDialogRangeCombo)); if (dialog.exec() == QDialog::Accepted) { retrieveSettings(m_printer.data()); print(m_printer.data()); @@ -431,6 +383,7 @@ void PrintDialogPanel::showPageSetupDialog() retrieveSettings(m_printer.data()); } +#include "moc_printdialogpanel.cpp" #include "printdialogpanel.moc" #endif // !QT_NO_PRINTER diff --git a/tests/manual/dialogs/printdialogpanel.h b/tests/manual/dialogs/printdialogpanel.h index c869782769..a7ae257cf8 100644 --- a/tests/manual/dialogs/printdialogpanel.h +++ b/tests/manual/dialogs/printdialogpanel.h @@ -44,6 +44,8 @@ #ifndef QT_NO_PRINTER +#include "ui_printdialogpanel.h" + #include QT_BEGIN_NAMESPACE @@ -73,22 +75,14 @@ private slots: void enableCustomSizeControl(); private: + QSizeF pageSize() const; + void setPageSize(const QSizeF &sizef); void applySettings(QPrinter *printer) const; void retrieveSettings(const QPrinter *printer); void enablePanels(); - QGroupBox *m_creationGroupBox; - QPushButton *m_createButton; - QPushButton *m_deleteButton; - QGroupBox *m_settingsGroupBox; - QCheckBox *m_fullPageCheckBox; - QGroupBox *m_dialogsGroupBox; - OptionsControl *m_printDialogOptionsControl; - QComboBox *m_printDialogRangeCombo; - QComboBox *m_modeCombo; - QComboBox *m_orientationCombo; - QComboBox *m_pageSizeCombo; - PageSizeControl *m_customPageSizeControl; + Ui::PrintDialogPanel m_panel; + QScopedPointer m_printer; }; diff --git a/tests/manual/dialogs/printdialogpanel.ui b/tests/manual/dialogs/printdialogpanel.ui new file mode 100644 index 0000000000..a185629fc1 --- /dev/null +++ b/tests/manual/dialogs/printdialogpanel.ui @@ -0,0 +1,270 @@ + + + PrintDialogPanel + + + + 0 + 0 + 507 + 291 + + + + + 0 + 0 + + + + Form + + + + + + + + + 0 + 0 + + + + Create + + + + + + Printer Mode: + + + + + + + + + + + + Create + + + + + + + Delete + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + 0 + 0 + + + + Settings + + + + + + Page Size: + + + + + + + + + + + + mm + + + 1000.000000000000000 + + + 10.000000000000000 + + + + + + + x + + + + + + + mm + + + 1000.000000000000000 + + + 10.000000000000000 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Orientation: + + + + + + + + + + Full Page Mode: + + + + + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 22 + + + + + + + + + + + + + 0 + 0 + + + + Dialogs + + + + + + Options + + + + + + + + + + Print... + + + + + + + Preview... + + + + + + + Page Setup... + + + + + + + + + + Qt::Vertical + + + + 20 + 80 + + + + + + + + + + + OptionsControl + QGroupBox +
utils.h
+ 1 +
+
+ + +
diff --git a/tests/manual/dialogs/utils.cpp b/tests/manual/dialogs/utils.cpp index 7e0067c7f3..2dc01359b1 100644 --- a/tests/manual/dialogs/utils.cpp +++ b/tests/manual/dialogs/utils.cpp @@ -52,11 +52,23 @@ QComboBox *createCombo(QWidget *parent, const FlagData *d, size_t size) 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) { @@ -68,6 +80,15 @@ OptionsControl::OptionsControl(const QString &title, const FlagData *data, size_ } } +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) diff --git a/tests/manual/dialogs/utils.h b/tests/manual/dialogs/utils.h index 634795627f..ac91754139 100644 --- a/tests/manual/dialogs/utils.h +++ b/tests/manual/dialogs/utils.h @@ -59,6 +59,7 @@ struct FlagData // 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 Enum comboBoxValue(const QComboBox *c) @@ -71,8 +72,11 @@ 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 Enum value() const { return static_cast(intValue()); } -- cgit v1.2.3