From: Kevin Krammer Date: Fri, 2 Nov 2012 15:19:09 +0000 (+0100) Subject: Implement "static methods" for font dialog X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=49fdf1e22f38ec5bdee3fdd6d7681d8134e6704c;p=konrad%2FDeclarativeWidgets.git Implement "static methods" for font dialog --- diff --git a/examples/dialogs.qml b/examples/dialogs.qml index 85c372f..ff0aa15 100644 --- a/examples/dialogs.qml +++ b/examples/dialogs.qml @@ -139,5 +139,39 @@ Widget { } } + PushButton { + text: qsTr("Font Dialog::getFont") + onClicked: { + var font = FontDialog.getFont() + console.log("FontDialog.getFont " + (FontDialog.ok ? "returned " + font : "was cancelled")) + } + } + + PushButton { + text: qsTr("Font Dialog::getFont with initial") + onClicked: { + var font = FontDialog.getFont(Qt.fontFamilies()[0]) + console.log("FontDialog.getFont " + (FontDialog.ok ? "returned " + font : "was cancelled")) + } + } + + PushButton { + text: qsTr("Font Dialog::getFont with title") + onClicked: { + FontDialog.title = "Pick a font" + var font = FontDialog.getFont() + console.log("FontDialog.getFont " + (FontDialog.ok ? "returned " + font : "was cancelled")) + } + } + + PushButton { + text: qsTr("Font Dialog::getFont with options") + onClicked: { + FontDialog.options = FontDialog.NoButtons + var font = FontDialog.getFont() + console.log("FontDialog.getFont " + (FontDialog.ok ? "returned " + font : "was cancelled")) + } + } + } } diff --git a/lib/declarativefontdialog.cpp b/lib/declarativefontdialog.cpp index c7e433d..c6498ff 100644 --- a/lib/declarativefontdialog.cpp +++ b/lib/declarativefontdialog.cpp @@ -20,10 +20,104 @@ #include "declarativefontdialog_p.h" +class DeclarativeFontDialogAttached::Private +{ + public: + Private() : dialogAccepted(false), options(0) {} + + public: + QFont initial; + QString title; + bool dialogAccepted; + QFontDialog::FontDialogOptions options; +}; + + +DeclarativeFontDialogAttached::DeclarativeFontDialogAttached(QObject *parent) + : StaticDialogMethodAttached(parent), d(new Private) +{ +} + +DeclarativeFontDialogAttached::~DeclarativeFontDialogAttached() +{ + delete d; +} + +void DeclarativeFontDialogAttached::setTitle(const QString &title) +{ + if (title == d->title) + return; + + d->title = title; + emit titleChanged(title); +} + +QString DeclarativeFontDialogAttached::title() const +{ + return d->title; +} + +bool DeclarativeFontDialogAttached::dialogAccepted() const +{ + return d->dialogAccepted; +} + +void DeclarativeFontDialogAttached::setOptions(int options) +{ + if (options == d->options) + return; + + d->options = static_cast(options); + emit optionsChanged(options); +} + +int DeclarativeFontDialogAttached::options() const +{ + return d->options; +} + +QFont DeclarativeFontDialogAttached::getFont() +{ + QWidget *parent = bestParentWindow(); + bool ok = false; + + QFont retVal; + if (d->title.isEmpty() && d->options == 0) { + retVal = QFontDialog::getFont(&ok, d->initial, parent); + } else if (d->options != 0) { + retVal = QFontDialog::getFont(&ok, d->initial, parent, d->title, d->options); + } else { + retVal = QFontDialog::getFont(&ok, d->initial, parent, d->title); + } + + setDialogAccepted(ok); + return retVal; +} + +QFont DeclarativeFontDialogAttached::getFont(const QString &fontFamily) +{ + d->initial = QFont(fontFamily); + return getFont(); +} + +void DeclarativeFontDialogAttached::setDialogAccepted(bool accepted) +{ + if (accepted == d->dialogAccepted) + return; + + d->dialogAccepted = accepted; + emit dialogAcceptedChanged(accepted); +} + DeclarativeFontDialog::DeclarativeFontDialog(QObject *parent) : DeclarativeWidgetProxy(parent) { connectAllSignals(m_proxiedObject, this); } +DeclarativeFontDialogAttached *DeclarativeFontDialog::qmlAttachedProperties(QObject *parent) +{ + return new DeclarativeFontDialogAttached(parent); +} + CUSTOM_METAOBJECT(DeclarativeFontDialog, QFontDialog) diff --git a/lib/declarativefontdialog_p.h b/lib/declarativefontdialog_p.h index 354e6e1..6f55790 100644 --- a/lib/declarativefontdialog_p.h +++ b/lib/declarativefontdialog_p.h @@ -23,14 +23,54 @@ #include "declarativewidgetproxy_p.h" +#include "staticdialogmethodattached_p.h" + #include +class DeclarativeFontDialogAttached : public StaticDialogMethodAttached +{ + Q_OBJECT + Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) + Q_PROPERTY(bool ok READ dialogAccepted NOTIFY dialogAcceptedChanged) + Q_PROPERTY(int options READ options WRITE setOptions NOTIFY optionsChanged) + + public: + DeclarativeFontDialogAttached(QObject *parent = 0); + ~DeclarativeFontDialogAttached(); + + void setTitle(const QString &title); + QString title() const; + + bool dialogAccepted() const; + + void setOptions(int options); + int options() const; + + Q_INVOKABLE QFont getFont(); + Q_INVOKABLE QFont getFont(const QString &fontFamily); + + Q_SIGNALS: + void titleChanged(const QString &title); + void dialogAcceptedChanged(bool accepted); + void optionsChanged(int options); + + private: + void setDialogAccepted(bool accepted); + + class Private; + Private *const d; +}; + class DeclarativeFontDialog : public DeclarativeWidgetProxy { DECLARATIVE_OBJECT public: DeclarativeFontDialog(QObject *parent = 0); + + static DeclarativeFontDialogAttached *qmlAttachedProperties(QObject *parent); }; +QML_DECLARE_TYPEINFO(DeclarativeFontDialog, QML_HAS_ATTACHED_PROPERTIES) + #endif diff --git a/lib/declarativewidgetsdocument.cpp b/lib/declarativewidgetsdocument.cpp index 3cf683d..e861a86 100644 --- a/lib/declarativewidgetsdocument.cpp +++ b/lib/declarativewidgetsdocument.cpp @@ -137,6 +137,7 @@ DeclarativeWidgetsDocument::DeclarativeWidgetsDocument(const QUrl &url, QObject qmlRegisterType("QtGui", 1, 0, "DoubleSpinBox"); qmlRegisterType(); qmlRegisterType("QtGui", 1, 0, "FileDialog"); + qmlRegisterType(); qmlRegisterType("QtGui", 1, 0, "FontDialog"); qmlRegisterType("QtGui", 1, 0, "Frame"); qmlRegisterType("QtGui", 1, 0, "GroupBox");