⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qfontdialog.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.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 GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qwindowdefs.h"#ifndef QT_NO_FONTDIALOG#include "qfontdialog.h"#include <qapplication.h>#include <qcheckbox.h>#include <qcombobox.h>#include <qevent.h>#include <qfontdatabase.h>#include <qgroupbox.h>#include <qlabel.h>#include <qlayout.h>#include <qlineedit.h>#include <qpushbutton.h>#include <qstyle.h>#include <qdialogbuttonbox.h>#include <qheaderview.h>#include <qlistview.h>#include <qstringlistmodel.h>#include <qvalidator.h>#include <private/qdialog_p.h>#include <private/qfont_p.h>class QFontListView : public QListView{    Q_OBJECTpublic:    QFontListView(QWidget *parent);    inline QStringListModel *model() const {        return static_cast<QStringListModel *>(QListView::model());    }    inline void setCurrentItem(int item) {        QListView::setCurrentIndex(static_cast<QAbstractListModel*>(model())->index(item));    }    inline int currentItem() const {        return QListView::currentIndex().row();    }    inline int count() const {        return model()->rowCount();    }    inline QString currentText() const {        int row = QListView::currentIndex().row();        return row < 0 ? QString() : model()->stringList().at(row);    }    void currentChanged(const QModelIndex &current, const QModelIndex &previous) {        QListView::currentChanged(current, previous);        if (current.isValid())            emit highlighted(current.row());    }    QString text(int i) const {        return model()->stringList().at(i);    }signals:    void highlighted(int);};QFontListView::QFontListView(QWidget *parent)    : QListView(parent){    setModel(new QStringListModel(parent));    setEditTriggers(NoEditTriggers);}/*!  \class QFontDialog  \ingroup dialogs  \ingroup text  \mainclass  \brief The QFontDialog class provides a dialog widget for selecting a font.  The usual way to use this class is to call one of the static convenience  functions, e.g. getFont().  Examples:  \code    bool ok;    QFont font = QFontDialog::getFont(                    &ok, QFont("Helvetica [Cronyx]", 10), this);    if (ok) {        // the user clicked OK and font is set to the font the user selected    } else {        // the user canceled the dialog; font is set to the initial        // value, in this case Helvetica [Cronyx], 10    }  \endcode    The dialog can also be used to set a widget's font directly:  \code    myWidget.setFont(QFontDialog::getFont(0, myWidget.font()));  \endcode  If the user clicks OK the font they chose will be used for myWidget,  and if they click Cancel the original font is used.  \image plastique-fontdialog.png A font dialog in the Plastique widget style.  \sa QFont, QFontInfo, QFontMetrics, QColorDialog, QFileDialog, QPrintDialog,      {Standard Dialogs Example}*/class QFontDialogPrivate : public QDialogPrivate{    Q_DECLARE_PUBLIC(QFontDialog)public:    inline QFontDialogPrivate()        : writingSystem(QFontDatabase::Any)    { }    static QFont getFont(bool *ok, const QFont *def, QWidget* parent, const QString &caption = QString());    void _q_sizeChanged(const QString &);    void _q_familyHighlighted(int);    void _q_writingSystemHighlighted(int);    void _q_styleHighlighted(int);    void _q_sizeHighlighted(int);    void _q_updateSample();    void retranslateStrings();    QLabel * familyAccel;    QLineEdit * familyEdit;    QFontListView * familyList;    QLabel * styleAccel;    QLineEdit * styleEdit;    QFontListView * styleList;    QLabel * sizeAccel;    QLineEdit * sizeEdit;    QFontListView * sizeList;    QGroupBox * effects;    QCheckBox * strikeout;    QCheckBox * underline;    QComboBox * color;    QGroupBox * sample;    QLineEdit * sampleEdit;    QLabel * writingSystemAccel;    QComboBox * writingSystemCombo;    QBoxLayout * buttonLayout;    QBoxLayout * effectsLayout;    QBoxLayout * sampleLayout;    QBoxLayout * sampleEditLayout;    QFontDatabase fdb;    QString       family;    QFontDatabase::WritingSystem writingSystem;    QString       style;    int           size;    bool smoothScalable;};/*!  \internal  Constructs a standard font dialog.  Use setFont() to set the initial font attributes.  The \a parent, \a name, \a modal and \a f parameters are passed to  the QDialog constructor.  \sa getFont()*/QFontDialog::QFontDialog(QWidget *parent, bool modal, Qt::WindowFlags f)    : QDialog(*new QFontDialogPrivate, parent, f){    Q_D(QFontDialog);    setModal(modal);    setSizeGripEnabled(true);    // grid    d->familyEdit = new QLineEdit(this);    d->familyEdit->setReadOnly(true);    d->familyList = new QFontListView(this);    d->familyEdit->setFocusProxy(d->familyList);    d->familyAccel = new QLabel(this);#ifndef QT_NO_SHORTCUT    d->familyAccel->setBuddy(d->familyList);#endif    d->familyAccel->setIndent(2);    d->styleEdit = new QLineEdit(this);    d->styleEdit->setReadOnly(true);    d->styleList = new QFontListView(this);    d->styleEdit->setFocusProxy(d->styleList);    d->styleAccel = new QLabel(this);#ifndef QT_NO_SHORTCUT    d->styleAccel->setBuddy(d->styleList);#endif    d->styleAccel->setIndent(2);    d->sizeEdit = new QLineEdit(this);    d->sizeEdit->setFocusPolicy(Qt::ClickFocus);    QIntValidator *validator = new QIntValidator(1, 512, this);    d->sizeEdit->setValidator(validator);    d->sizeList = new QFontListView(this);    d->sizeAccel = new QLabel(this);#ifndef QT_NO_SHORTCUT    d->sizeAccel->setBuddy(d->sizeEdit);#endif    d->sizeAccel->setIndent(2);    // effects box    d->effects = new QGroupBox(this);    QVBoxLayout *vbox = new QVBoxLayout(d->effects);    d->strikeout = new QCheckBox(d->effects);    vbox->addWidget(d->strikeout);    d->underline = new QCheckBox(d->effects);    vbox->addWidget(d->underline);    d->sample = new QGroupBox(this);    QHBoxLayout *hbox = new QHBoxLayout(d->sample);    d->sampleEdit = new QLineEdit(d->sample);    d->sampleEdit->setSizePolicy(QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored));    d->sampleEdit->setAlignment(Qt::AlignCenter);    // Note that the sample text is *not* translated with tr(), as the    // characters used depend on the charset encoding.    d->sampleEdit->setText(QLatin1String("AaBbYyZz"));    hbox->addWidget(d->sampleEdit);    d->writingSystemCombo = new QComboBox(this);    d->writingSystemAccel = new QLabel(this);#ifndef QT_NO_SHORTCUT    d->writingSystemAccel->setBuddy(d->writingSystemCombo);#endif    d->writingSystemAccel->setIndent(2);    d->size = 0;    d->smoothScalable = false;    connect(d->writingSystemCombo, SIGNAL(activated(int)),            SLOT(_q_writingSystemHighlighted(int)));    connect(d->familyList, SIGNAL(highlighted(int)),            SLOT(_q_familyHighlighted(int)));    connect(d->styleList, SIGNAL(highlighted(int)),            SLOT(_q_styleHighlighted(int)));    connect(d->sizeList, SIGNAL(highlighted(int)),            SLOT(_q_sizeHighlighted(int)));    connect(d->sizeEdit, SIGNAL(textChanged(QString)),            SLOT(_q_sizeChanged(QString)));    connect(d->strikeout, SIGNAL(clicked()),            SLOT(_q_updateSample()));    connect(d->underline, SIGNAL(clicked()),            SLOT(_q_updateSample()));    for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {        QFontDatabase::WritingSystem ws = QFontDatabase::WritingSystem(i);        QString writingSystemName = QFontDatabase::writingSystemName(ws);        if (writingSystemName.isEmpty())            break;        d->writingSystemCombo->addItem(writingSystemName);    }    updateFamilies();    if (d->familyList->count() != 0)        d->familyList->setCurrentItem(0);    // grid layout    QGridLayout *mainGrid = new QGridLayout(this);    int spacing = mainGrid->spacing();    if (spacing >= 0) {     // uniform spacing       mainGrid->setSpacing(0);       mainGrid->setColumnMinimumWidth(1, spacing);       mainGrid->setColumnMinimumWidth(3, spacing);       int margin = 0;       mainGrid->getContentsMargins(0, 0, 0, &margin);       mainGrid->setRowMinimumHeight(3, margin);       mainGrid->setRowMinimumHeight(6, 2);       mainGrid->setRowMinimumHeight(8, margin);    }    mainGrid->addWidget(d->familyAccel, 0, 0);    mainGrid->addWidget(d->familyEdit, 1, 0);    mainGrid->addWidget(d->familyList, 2, 0);    mainGrid->addWidget(d->styleAccel, 0, 2);    mainGrid->addWidget(d->styleEdit, 1, 2);    mainGrid->addWidget(d->styleList, 2, 2);    mainGrid->addWidget(d->sizeAccel, 0, 4);    mainGrid->addWidget(d->sizeEdit, 1, 4);    mainGrid->addWidget(d->sizeList, 2, 4);    mainGrid->setColumnStretch(0, 38);    mainGrid->setColumnStretch(2, 24);    mainGrid->setColumnStretch(4, 10);    mainGrid->addWidget(d->effects, 4, 0);    mainGrid->addWidget(d->sample, 4, 2, 4, 3);    mainGrid->addWidget(d->writingSystemAccel, 5, 0);    mainGrid->addWidget(d->writingSystemCombo, 7, 0);    QDialogButtonBox *buttonBox = new QDialogButtonBox(this);    mainGrid->addWidget(buttonBox, 9, 0, 1, 5);    QPushButton *button            = static_cast<QPushButton *>(buttonBox->addButton(modal ? QDialogButtonBox::Ok                                                                    : QDialogButtonBox::Apply));    if (modal)        connect(buttonBox, SIGNAL(accepted()), SLOT(accept()));    button->setDefault(true);    buttonBox->addButton(modal ? QDialogButtonBox::Cancel : QDialogButtonBox::Close);    connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));    resize(500, 360);    d->sizeEdit->installEventFilter(this);    d->familyList->installEventFilter(this);    d->styleList->installEventFilter(this);    d->sizeList->installEventFilter(this);    d->familyList->setFocus();    d->retranslateStrings();}/*!  \internal Destroys the font dialog and frees up its storage.*/QFontDialog::~QFontDialog(){}/*!  Executes a modal font dialog and returns a font.  If the user clicks \gui OK, the selected font is returned. If the user  clicks \gui Cancel, the \a initial font is returned.  The dialog is constructed with the given \a parent.  \a caption is  shown as the window title of the dialog and  \a initial is the  initially selected font. If the \a ok parameter is not-null, the  value it refers to is set to true if the user clicks \gui OK, and  set to false if the user clicks \gui Cancel.  This static function is less flexible than the full QFontDialog  object, but is convenient and easy to use.  Examples:  \code    bool ok;    QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this);    if (ok) {        // font is set to the font the user selected    } else {        // the user canceled the dialog; font is set to the initial        // value, in this case Times, 12.    }  \endcode    The dialog can also be used to set a widget's font directly:  \code    myWidget.setFont(QFontDialog::getFont(0, myWidget.font()));  \endcode  In this example, if the user clicks OK the font they chose will be  used, and if they click Cancel the original font is used.*/QFont QFontDialog::getFont(bool *ok, const QFont &initial, QWidget* parent, const QString &caption){    return QFontDialogPrivate::getFont(ok, &initial, parent, caption);}/*!  \overload*/QFont QFontDialog::getFont(bool *ok, const QFont &initial,                            QWidget *parent){    return QFontDialogPrivate::getFont(ok, &initial, parent);}/*!    \overload  Executes a modal font dialog and returns a font.  If the user clicks \gui OK, the selected font is returned. If the user  clicks \gui Cancel, the Qt default font is returned.  The dialog is constructed with the given \a parent.  If the \a ok parameter is not-null, the value it refers to is set  to true if the user clicks \gui OK, and false if the user clicks  \gui Cancel.  This static function is less functional than the full QFontDialog  object, but is convenient and easy to use.  Example:  \code    bool ok;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -