Implement "static methods" for font dialog
authorKevin Krammer <kevin.krammer@kdab.com>
Fri, 2 Nov 2012 15:19:09 +0000 (16:19 +0100)
committerKevin Krammer <kevin.krammer@kdab.com>
Fri, 2 Nov 2012 15:20:24 +0000 (16:20 +0100)
examples/dialogs.qml
lib/declarativefontdialog.cpp
lib/declarativefontdialog_p.h
lib/declarativewidgetsdocument.cpp

index 85c372f..ff0aa15 100644 (file)
@@ -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"))
+      }
+    }
+
   }
 }
index c7e433d..c6498ff 100644 (file)
 
 #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<QFontDialog::FontDialogOptions>(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<QFontDialog>(parent)
 {
   connectAllSignals(m_proxiedObject, this);
 }
 
+DeclarativeFontDialogAttached *DeclarativeFontDialog::qmlAttachedProperties(QObject *parent)
+{
+  return new DeclarativeFontDialogAttached(parent);
+}
+
 CUSTOM_METAOBJECT(DeclarativeFontDialog, QFontDialog)
index 354e6e1..6f55790 100644 (file)
 
 #include "declarativewidgetproxy_p.h"
 
+#include "staticdialogmethodattached_p.h"
+
 #include <QFontDialog>
 
+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<QFontDialog>
 {
   DECLARATIVE_OBJECT
 
   public:
     DeclarativeFontDialog(QObject *parent = 0);
+
+    static DeclarativeFontDialogAttached *qmlAttachedProperties(QObject *parent);
 };
 
+QML_DECLARE_TYPEINFO(DeclarativeFontDialog, QML_HAS_ATTACHED_PROPERTIES)
+
 #endif
index 3cf683d..e861a86 100644 (file)
@@ -137,6 +137,7 @@ DeclarativeWidgetsDocument::DeclarativeWidgetsDocument(const QUrl &url, QObject
   qmlRegisterType<DeclarativeDoubleSpinBox>("QtGui", 1, 0, "DoubleSpinBox");
   qmlRegisterType<DeclarativeFileDialogAttached>();
   qmlRegisterType<DeclarativeFileDialog>("QtGui", 1, 0, "FileDialog");
+  qmlRegisterType<DeclarativeFontDialogAttached>();
   qmlRegisterType<DeclarativeFontDialog>("QtGui", 1, 0, "FontDialog");
   qmlRegisterType<DeclarativeFrame>("QtGui", 1, 0, "Frame");
   qmlRegisterType<DeclarativeGroupBox>("QtGui", 1, 0, "GroupBox");