From: Kevin Krammer Date: Mon, 22 Oct 2012 14:14:46 +0000 (+0200) Subject: Support for QInputDialog static get methods X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=4dbc939566895dc067de7dec76f650b46333f1cf;p=web%2Fkonrad%2FDeclarativeWidgets.git Support for QInputDialog static get methods --- diff --git a/declarativeobjects.cpp b/declarativeobjects.cpp index fc174f4..378a874 100644 --- a/declarativeobjects.cpp +++ b/declarativeobjects.cpp @@ -774,11 +774,65 @@ DeclarativeFontDialog::DeclarativeFontDialog(QObject *parent) : DeclarativeWidge CUSTOM_METAOBJECT(DeclarativeFontDialog, QFontDialog) // DeclarativeInputDialog +DeclarativeInputDialogAttached::DeclarativeInputDialogAttached(QObject *parent) : QObject(parent) +{ +} + +double DeclarativeInputDialogAttached::getDouble(QObject *parent, const QString &title, const QString &label, + double value, double min, double max, int decimals) +{ + return QInputDialog::getDouble(bestParentWindow(parent), title, label, value, min, max, decimals); +} + +int DeclarativeInputDialogAttached::getInt(QObject *parent, const QString &title, const QString &label, + int value, int min, int max, int step) +{ + return QInputDialog::getInt(bestParentWindow(parent), title, label, value, min, max, step); +} + +QString DeclarativeInputDialogAttached::getItem(QObject *parent, const QString &title, const QString &label, + const QStringList &items, int current, bool editable) +{ + return QInputDialog::getItem(bestParentWindow(parent), title, label, items, current, editable); +} + +QString DeclarativeInputDialogAttached::getText(QObject *parent, const QString &title, const QString &label, + int echoMode, const QString &text) +{ + return QInputDialog::getText(bestParentWindow(parent), title, label, static_cast(echoMode), text); +} + +QWidget *DeclarativeInputDialogAttached::bestParentWindow(QObject *parent) const +{ + if (!parent) + parent = this->parent(); + + // if parent is a Declarative Object, search the proxied hierarchy + AbstractDeclarativeObject *declarativeObject = dynamic_cast(parent); + if (declarativeObject) + parent = declarativeObject->object(); + + while (parent) { + QWidget *widget = qobject_cast(parent); + if (widget) + return widget->topLevelWidget(); + + parent = parent->parent(); + } + + return 0; +} + DeclarativeInputDialog::DeclarativeInputDialog(QObject *parent) : DeclarativeWidgetProxy(parent) { connectAllSignals(m_proxiedObject, this); } +DeclarativeInputDialogAttached *DeclarativeInputDialog::qmlAttachedProperties(QObject *parent) +{ + return new DeclarativeInputDialogAttached(parent); +} + CUSTOM_METAOBJECT(DeclarativeInputDialog, InputDialog) // DeclarativeLabel diff --git a/declarativeobjects_p.h b/declarativeobjects_p.h index 3cb552b..078212d 100644 --- a/declarativeobjects_p.h +++ b/declarativeobjects_p.h @@ -487,14 +487,41 @@ class DeclarativeFontDialog : public DeclarativeWidgetProxy DeclarativeFontDialog(QObject *parent = 0); }; +class DeclarativeInputDialogAttached : public QObject +{ + Q_OBJECT + + public: + DeclarativeInputDialogAttached(QObject *parent = 0); + + Q_INVOKABLE double getDouble(QObject *parent, const QString &title, const QString &label, + double value = 0, double min = -2147483647, double max = 2147483647, int decimals = 1); + + Q_INVOKABLE int getInt(QObject *parent, const QString &title, const QString &label, + int value = 0, int min = -2147483647, int max = 2147483647, int step = 1); + + Q_INVOKABLE QString getItem(QObject *parent, const QString &title, const QString &label, + const QStringList &items, int current = 0, bool editable = true); + + Q_INVOKABLE QString getText(QObject *parent, const QString &title, const QString &label, + int echoMode = QLineEdit::Normal, const QString &text = QString()); + + private: + QWidget *bestParentWindow(QObject *parent) const; +}; + class DeclarativeInputDialog : public DeclarativeWidgetProxy { DECLARATIVE_OBJECT public: DeclarativeInputDialog(QObject *parent = 0); + + static DeclarativeInputDialogAttached *qmlAttachedProperties(QObject *parent); }; +QML_DECLARE_TYPEINFO(DeclarativeInputDialog, QML_HAS_ATTACHED_PROPERTIES) + class DeclarativeLabel : public DeclarativeWidgetProxy { DECLARATIVE_OBJECT diff --git a/declarativewidgetdocument.cpp b/declarativewidgetdocument.cpp index 05d22ad..1955afb 100644 --- a/declarativewidgetdocument.cpp +++ b/declarativewidgetdocument.cpp @@ -53,6 +53,7 @@ DeclarativeWidgetDocument::DeclarativeWidgetDocument(const QUrl &url, QObject *p qmlRegisterType(); qmlRegisterType("QtGui", 1, 0, "FileDialog"); qmlRegisterType("QtGui", 1, 0, "FontDialog"); + qmlRegisterType(); qmlRegisterType("QtGui", 1, 0, "InputDialog"); qmlRegisterType("QtGui", 1, 0, "Label"); qmlRegisterType("QtGui", 1, 0, "MainWindow"); diff --git a/inputdialog.qml b/inputdialog.qml new file mode 100644 index 0000000..7f4569c --- /dev/null +++ b/inputdialog.qml @@ -0,0 +1,38 @@ +import QtGui 1.0 + +Widget { + VBoxLayout { + PushButton { + text: "InputDialog::getDouble" + onClicked: { + var value = InputDialog.getDouble(0, "Double Input", "A Double number") + console.log("InputDialog.getDouble returned " + value) + } + } + + PushButton { + text: "InputDialog::getInt" + onClicked: { + var value = InputDialog.getInt(0, "Integer Input", "An Integer number") + console.log("InputDialog.getInt returned " + value) + } + } + + PushButton { + text: "InputDialog::getItem" + onClicked: { + var items = [ "Item 1", "Item 2", "Item 3"] + var item = InputDialog.getItem(0, "Item Selection", "Select an item", items) + console.log("InputDialog.getItem returned " + item) + } + } + + PushButton { + text: "InputDialog::getText" + onClicked: { + var text = InputDialog.getText(0, "Text Input", "Any text") + console.log("InputDialog.getText returned " + text) + } + } + } +}