From 57a700a9ad6c100dff55fd2100802271f3589b53 Mon Sep 17 00:00:00 2001 From: Jens Bache-Wiig <jensbw@gmail.com> Date: Sun, 25 Sep 2011 17:47:19 +0200 Subject: [PATCH] Added support for experimental Dialog component --- components/Button.qml | 2 +- components/Dialog.qml | 74 +++++++++++++++++++++++++++++++++++++++ components/components.pro | 1 + components/qmldir | 1 + examples/Gallery.qml | 2 +- src/qstyleitem.cpp | 11 ++++-- src/qtoplevelwindow.cpp | 1 + src/qwindowitem.cpp | 32 +++++++++++------ src/qwindowitem.h | 12 ++++--- 9 files changed, 117 insertions(+), 19 deletions(-) create mode 100644 components/Dialog.qml diff --git a/components/Button.qml b/components/Button.qml index b0df9273b..4ba2b8d6a 100644 --- a/components/Button.qml +++ b/components/Button.qml @@ -4,7 +4,7 @@ import "custom" as Components Components.Button { id: button - width: Math.max(80, sizehint.width) + width: Math.max(72, sizehint.width) height: Math.max(22, sizehint.height) property alias containsMouse: tooltip.containsMouse diff --git a/components/Dialog.qml b/components/Dialog.qml new file mode 100644 index 000000000..a80a0cd7e --- /dev/null +++ b/components/Dialog.qml @@ -0,0 +1,74 @@ +import QtQuick 1.0 + +Window { + id: dialog + + width: 400 + height: 200 + + signal finished + signal accepted + signal rejected + + property int ok: 0x00000400 + property int cancel: 0x00400000 + property int close: 0x00200000 + property int help: 0x02000000 + + property int buttons: ok | cancel + + modal: true + + default property alias data: content.data + + Item { + id: content + anchors.topMargin:8 + anchors.margins: 16 + anchors.top: parent.top + anchors.right: parent.right + anchors.left: parent.left + anchors.bottom: buttonrow.top + } + + // Dialogs should center on parent + onVisibleChanged: center() + + Row { + id: buttonrow + + anchors.bottom: parent.bottom + anchors.right: parent.right + anchors.margins: 16 + anchors.bottomMargin:8 + spacing: 6 + + Button { + visible: buttons & help + text: "Help" + onClicked: { + visible: dialog.visible = false + rejected() + } + } + + Button { + visible: buttons & cancel + text: "Cancel" + onClicked: { + visible: dialog.visible = false + rejected() + } + } + + Button { + visible: buttons & ok + text: "OK" + defaultbutton: true + onClicked: { + visible: dialog.visible = false + accepted() + } + } + } +} diff --git a/components/components.pro b/components/components.pro index a112b47c5..0dd9ab39e 100644 --- a/components/components.pro +++ b/components/components.pro @@ -12,6 +12,7 @@ QML_FILES = \ Button.qml \ ComboBox.qml \ Dial.qml \ + Dialog.qml \ ProgressBar.qml \ ScrollBar.qml \ Switch.qml \ diff --git a/components/qmldir b/components/qmldir index c4bf902f5..e90008df1 100644 --- a/components/qmldir +++ b/components/qmldir @@ -27,3 +27,4 @@ plugin styleplugin plugin TableColumn 0.1 TableColumn.qml ContextMenu 0.1 ContextMenu.qml MenuItem 0.1 MenuItem.qml +Dialog 0.1 Dialog.qml diff --git a/examples/Gallery.qml b/examples/Gallery.qml index b1a8a66d3..eab71dedb 100644 --- a/examples/Gallery.qml +++ b/examples/Gallery.qml @@ -13,7 +13,7 @@ Rectangle { width: 538 + frame.margins * 2 height: 360 + frame.margins * 2 - ToolBar{ + ToolBar { id: toolbar width: parent.width height: 40 diff --git a/src/qstyleitem.cpp b/src/qstyleitem.cpp index 7e92975a1..9e599600e 100644 --- a/src/qstyleitem.cpp +++ b/src/qstyleitem.cpp @@ -542,8 +542,15 @@ QSize QStyleItem::sizeFromContents(int width, int height) case ToolButton: size = qApp->style()->sizeFromContents(QStyle::CT_ToolButton, m_styleoption, QSize(width,height), widget()); break; - case Button: - size = qApp->style()->sizeFromContents(QStyle::CT_PushButton, m_styleoption, QSize(width,height), widget()); + case Button:{ + QStyleOptionButton *btn = qstyleoption_cast<QStyleOptionButton*>(m_styleoption); + int textWidth = btn->fontMetrics.width(btn->text); + size = qApp->style()->sizeFromContents(QStyle::CT_PushButton, m_styleoption, QSize(textWidth,height), widget()); +#ifdef Q_WS_MAC + // Macstyle adds some weird constants to buttons + return QSize(textWidth + 14, size.height()); +#endif +} break; case Tab: size = qApp->style()->sizeFromContents(QStyle::CT_TabBarTab, m_styleoption, QSize(width,height), widget()); diff --git a/src/qtoplevelwindow.cpp b/src/qtoplevelwindow.cpp index b233183a1..23849cded 100644 --- a/src/qtoplevelwindow.cpp +++ b/src/qtoplevelwindow.cpp @@ -8,6 +8,7 @@ QTopLevelWindow::QTopLevelWindow() // Ensure that we have a default size, otherwise an empty window statement will // result in no window resize(QSize(100, 100)); + _view->setBackgroundBrush(palette().window()); setCentralWidget(_view); setAttribute(Qt::WA_DeleteOnClose); } diff --git a/src/qwindowitem.cpp b/src/qwindowitem.cpp index b57393d68..a97aa1cf1 100644 --- a/src/qwindowitem.cpp +++ b/src/qwindowitem.cpp @@ -46,11 +46,13 @@ QWindowItem::QWindowItem(QTopLevelWindow* tlw) : _window(tlw ? tlw : new QTopLevelWindow), _positionIsDefined(false), _delayedVisible(false) { - connect(_window, SIGNAL(visibilityChanged()), this, SIGNAL(visibilityChanged())); + connect(_window, SIGNAL(visibilityChanged()), this, SIGNAL(visibleChanged())); connect(_window, SIGNAL(windowStateChanged()), this, SIGNAL(windowStateChanged())); connect(_window, SIGNAL(sizeChanged(QSize)), this, SLOT(updateSize(QSize))); + connect(qApp, SIGNAL(aboutToQuit()), _window, SLOT(close())); view()->setResizeMode(QDeclarativeView::SizeRootObjectToView); + _window->installEventFilter(this); } QWindowItem::~QWindowItem() @@ -60,14 +62,19 @@ QWindowItem::~QWindowItem() bool QWindowItem::eventFilter(QObject *, QEvent *ev) { switch(ev->type()) { - case QEvent::Resize: - emit sizeChanged(); - break; - case QEvent::Move: - emit positionChanged(); - break; - default: - break; + case QEvent::Resize: + emit sizeChanged(); + emit widthChanged(); + emit heightChanged(); + break; + + case QEvent::Move: + emit xChanged(); + emit yChanged(); + break; + + default: + break; } return false; } @@ -107,6 +114,11 @@ void QWindowItem::updateSize(QSize newSize) emit sizeChanged(); } +void QWindowItem::center() +{ + _window->center(); +} + void QWindowItem::setX(int x) { _window->move(x, y()); @@ -170,7 +182,7 @@ void QWindowItem::setWindowDecoration(bool s) { bool visible = _window->isVisible(); _window->setWindowFlags(s ? _window->windowFlags() & ~Qt::FramelessWindowHint - : _window->windowFlags() | Qt::FramelessWindowHint); + : _window->windowFlags() | Qt::FramelessWindowHint); if (visible) _window->show(); emit windowDecorationChanged(); diff --git a/src/qwindowitem.h b/src/qwindowitem.h index d444e6a64..50c41b02f 100644 --- a/src/qwindowitem.h +++ b/src/qwindowitem.h @@ -38,15 +38,15 @@ class QWindowItem : public QDeclarativeItem { Q_OBJECT - Q_PROPERTY(int x READ x WRITE setX NOTIFY positionChanged) - Q_PROPERTY(int y READ y WRITE setY NOTIFY positionChanged) + Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged) + Q_PROPERTY(int y READ y WRITE setY NOTIFY yChanged) Q_PROPERTY(int height READ height WRITE setHeight NOTIFY sizeChanged) Q_PROPERTY(int width READ width WRITE setWidth NOTIFY sizeChanged) Q_PROPERTY(int minimumHeight READ minimumHeight WRITE setMinimumHeight NOTIFY minimumHeightChanged) Q_PROPERTY(int maximumHeight READ maximumHeight WRITE setMaximumHeight NOTIFY maximumHeightChanged) Q_PROPERTY(int minimumWidth READ minimumWidth WRITE setMinimumWidth NOTIFY minimumWidthChanged) Q_PROPERTY(int maximumWidth READ maximumWidth WRITE setMaximumWidth NOTIFY maximumWidthChanged) - Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibilityChanged) + Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged) Q_PROPERTY(bool windowDecoration READ windowDecoration WRITE setWindowDecoration NOTIFY windowDecorationChanged) Q_PROPERTY(bool modal READ modal WRITE setModal NOTIFY modalityChanged) Q_PROPERTY(bool close READ close WRITE setClose) @@ -97,11 +97,13 @@ protected: protected Q_SLOTS: void updateSize(QSize newSize); + void center(); Q_SIGNALS: void sizeChanged(); - void positionChanged(); - void visibilityChanged(); + void xChanged(); + void yChanged(); + void visibleChanged(); void windowDecorationChanged(); void windowStateChanged(); void minimumHeightChanged(); -- GitLab