Manual test for QFontDialog

Change-Id: I2bcdd30990e88af45c8d4b2756fcdd48fc2a47d7
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
This commit is contained in:
J-P Nurmi 2013-02-04 14:51:15 +01:00 committed by The Qt Project
parent 6c6960015e
commit 5809471ac7
4 changed files with 299 additions and 3 deletions

View File

@ -4,5 +4,5 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = dialogs
TEMPLATE = app
SOURCES += main.cpp filedialogpanel.cpp colordialogpanel.cpp
HEADERS += filedialogpanel.h colordialogpanel.h
SOURCES += main.cpp filedialogpanel.cpp colordialogpanel.cpp fontdialogpanel.cpp
HEADERS += filedialogpanel.h colordialogpanel.h fontdialogpanel.h

View File

@ -0,0 +1,208 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "fontdialogpanel.h"
#include <QGroupBox>
#include <QCheckBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QFontComboBox>
#include <QDoubleSpinBox>
#include <QTimer>
#include <QDebug>
static inline 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;
}
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")))
{
// Options
QGroupBox *optionsGroupBox = new QGroupBox(tr("Options"), this);
QVBoxLayout *optionsLayout = new QVBoxLayout(optionsGroupBox);
optionsLayout->addWidget(m_noButtons);
optionsLayout->addWidget(m_dontUseNativeDialog);
// 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 modal"), buttonsLayout, this, SLOT(showModal()));
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()
{
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();
}
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());
QFont font = m_fontFamilyBox->currentFont();
font.setPointSizeF(m_fontSizeBox->value());
d->setCurrentFont(font);
}

View File

@ -0,0 +1,87 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef FONTDIALOGPANEL_H
#define FONTDIALOGPANEL_H
#include <QPointer>
#include <QFontDialog>
class QCheckBox;
class QPushButton;
class QFontComboBox;
class QDoubleSpinBox;
class FontDialogPanel : public QWidget
{
Q_OBJECT
public:
explicit FontDialogPanel(QWidget *parent = 0);
public slots:
void execModal();
void showModal();
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;
QPushButton *m_deleteNonModalDialogButton;
QPushButton *m_deleteModalDialogButton;
QString m_result;
QPointer<QFontDialog> m_modalDialog;
QPointer<QFontDialog> m_nonModalDialog;
};
#endif // FONTDIALOGPANEL_H

View File

@ -41,6 +41,7 @@
#include "filedialogpanel.h"
#include "colordialogpanel.h"
#include "fontdialogpanel.h"
#include <QMainWindow>
#include <QApplication>
@ -51,7 +52,6 @@
#include <QKeySequence>
// Test for dialogs, allowing to play with all dialog options for implementing native dialogs.
// Currently, only QFileDialog and QColorDialog are implemented.
// Compiles with Qt 4.8 and Qt 5.
class MainWindow : public QMainWindow {
@ -70,6 +70,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
QTabWidget *tabWidget = new QTabWidget;
tabWidget->addTab(new FileDialogPanel, tr("QFileDialog"));
tabWidget->addTab(new ColorDialogPanel, tr("QColorDialog"));
tabWidget->addTab(new FontDialogPanel, tr("QFontDialog"));
setCentralWidget(tabWidget);
}