1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.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")))
, 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 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());
#if QT_VERSION >= 0x050000
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());
#endif // Qt 5
QFont font = m_fontFamilyBox->currentFont();
font.setPointSizeF(m_fontSizeBox->value());
d->setCurrentFont(font);
}
|