diff --git a/examples/Gallery.qml b/examples/Gallery.qml index fdb3a4e736f5877ae36f3064f9657f60238faec7..7d8b67bd462ff1cbf6df9d8fd618ad5ad2b34e92 100644 --- a/examples/Gallery.qml +++ b/examples/Gallery.qml @@ -108,6 +108,34 @@ ApplicationWindow { ChildWindow { id: window1 } + Action { + id: openAction + text: "Open" + shortcut: "Ctrl+O" + onTriggered: fileDialogLoad.open(); + } + + Action { + id: copyAction + text: "Copy" + shortcut: "Ctrl+C" + iconName: "edit-copy" + } + + Action { + id: cutAction + text: "Cut" + shortcut: "Ctrl+X" + iconName: "edit-cut" + } + + Action { + id: pasteAction + text: "Paste" + shortcut: "Ctrl+V" + iconName: "edit-paste" + } + ContextMenu { id: editmenu MenuItem { text: "Copy" ; iconName: "edit-copy" } diff --git a/src/qtdesktop/qstyleplugin.cpp b/src/qtdesktop/qstyleplugin.cpp index be265d9bf78bd03ca9af5f4fd5383829cbe1d345..4c56caf1c4ba9cd2e27df17718d494e98b77743d 100644 --- a/src/qtdesktop/qstyleplugin.cpp +++ b/src/qtdesktop/qstyleplugin.cpp @@ -42,6 +42,8 @@ #include "qstyleplugin_p.h" #include "qstyleitem_p.h" #include "qrangemodel_p.h" +#include "qtaction_p.h" +#include "qtexclusivegroup_p.h" #include "qtmenu_p.h" #include "qtmenubar_p.h" #include "qdesktopitem_p.h" @@ -101,6 +103,8 @@ void StylePlugin::registerTypes(const char *uri) qmlRegisterType<QRangeModel>(uri, 1, 0, "RangeModel"); qmlRegisterType<QWheelArea>(uri, 1, 0, "WheelArea"); + qmlRegisterType<QtAction>(uri, 1, 0, "Action"); + qmlRegisterType<QtExclusiveGroup>(uri, 1, 0, "ExclusiveGroup"); qmlRegisterType<QtMenu>(uri, 1, 0, "Menu"); qmlRegisterType<QtMenuBar>(uri, 1, 0, "MenuBar"); qmlRegisterType<QtMenuItem>(uri, 1, 0, "MenuItem"); diff --git a/src/qtdesktop/qtaction.cpp b/src/qtdesktop/qtaction.cpp new file mode 100644 index 0000000000000000000000000000000000000000..feb1956cf2a868b964926b5541f261e33e9d8cd6 --- /dev/null +++ b/src/qtdesktop/qtaction.cpp @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Components project. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qtaction_p.h" +#include "qtexclusivegroup_p.h" + +#include "qdebug.h" + + +QtAction::QtAction(QObject *parent) + : QObject(parent) + , m_enabled(true) + , m_checkable(false) + , m_checked(false) + , m_exclusiveGroup(0) +{ +} + +void QtAction::setText(const QString &text) +{ + if (text == m_text) + return; + m_text = text; + emit textChanged(); +} + +void QtAction::setShortcut(const QString &arg) +{ + if (arg == m_shortcut) + return; + m_shortcut = arg; + emit shortcutChanged(m_shortcut); +} + +void QtAction::setIconSource(const QUrl &iconSource) +{ + if (iconSource == m_iconSource) + return; + + m_iconSource = iconSource; + emit iconSourceChanged(); +} + +void QtAction::setIconName(const QString &iconName) +{ + if (iconName == m_iconName) + return; + + m_iconName = iconName; + emit iconNameChanged(); +} + +void QtAction::setToolTip(const QString &arg) +{ + if (m_toolTip != arg) { + m_toolTip = arg; + emit toolTipChanged(arg); + } +} + +void QtAction::setEnabled(bool e) +{ + if (e == m_enabled) + return; + m_enabled = e; + emit enabledChanged(); +} + +void QtAction::setCheckable(bool c) +{ + if (c == m_checkable) + return; + m_checkable = c; + emit checkableChanged(); +} + +void QtAction::setChecked(bool c) +{ + if (c == m_checked) + return; + m_checked = c; + emit toggled(m_checked); +} + +void QtAction::setExclusiveGroup(QtExclusiveGroup *eg) +{ + if (m_exclusiveGroup == eg) + return; + + if (m_exclusiveGroup) + m_exclusiveGroup->unregisterCheckable(this); + m_exclusiveGroup = eg; + if (m_exclusiveGroup) + m_exclusiveGroup->registerCheckable(this); + + emit exclusiveGroupChanged(); +} diff --git a/src/qtdesktop/qtaction_p.h b/src/qtdesktop/qtaction_p.h new file mode 100644 index 0000000000000000000000000000000000000000..e348b51c0687a7d26226e04c3520a30988913db8 --- /dev/null +++ b/src/qtdesktop/qtaction_p.h @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Components project. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTACTION_H +#define QTACTION_H + +#include <QtCore/QObject> +#include <QtCore/QUrl> +#include <QtGui/QIcon> + +class QtExclusiveGroup; + +class QtAction : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) + Q_PROPERTY(QUrl iconSource READ iconSource WRITE setIconSource NOTIFY iconSourceChanged) + Q_PROPERTY(QString iconName READ iconName WRITE setIconName NOTIFY iconNameChanged) + Q_PROPERTY(QString toolTip READ tooltip WRITE setToolTip NOTIFY toolTipChanged) + Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged) + Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable NOTIFY checkableChanged) + Q_PROPERTY(bool checked READ isChecked WRITE setChecked DESIGNABLE isCheckable NOTIFY toggled) + + Q_PROPERTY(QtExclusiveGroup *exclusiveGroup READ exclusiveGroup WRITE setExclusiveGroup NOTIFY exclusiveGroupChanged) +#ifndef QT_NO_SHORTCUT + Q_PROPERTY(QString shortcut READ shortcut WRITE setShortcut NOTIFY shortcutChanged) +#endif + +public: + explicit QtAction(QObject *parent = 0); + + QString text() const { return m_text; } + void setText(const QString &text); + + QString shortcut() const { return m_shortcut; } + void setShortcut(const QString &shortcut); + + QString iconName() const { return m_iconName; } + void setIconName(const QString &iconName); + + QUrl iconSource() const { return m_iconSource; } + void setIconSource(const QUrl &iconSource); + + QString tooltip() const { return m_toolTip; } + void setToolTip(const QString &toolTip); + + bool isEnabled() const { return m_enabled; } + void setEnabled(bool e); + + bool isCheckable() const { return m_checkable; } + void setCheckable(bool c); + + bool isChecked() const { return m_checked; } + void setChecked(bool c); + + QtExclusiveGroup *exclusiveGroup() const { return m_exclusiveGroup; } + void setExclusiveGroup(QtExclusiveGroup * arg); + +public Q_SLOTS: + void trigger() { emit triggered(); } + void hover() { emit hovered(); } + +Q_SIGNALS: + void triggered(); + void hovered(); + void toggled(bool); + + void textChanged(); + void shortcutChanged(QString arg); + void iconNameChanged(); + void iconSourceChanged(); + void toolTipChanged(QString arg); + void enabledChanged(); + void checkableChanged(); + + void exclusiveGroupChanged(); + +private: + QString m_text; + QUrl m_iconSource; + QString m_iconName; + bool m_enabled; + bool m_checkable; + bool m_checked; + QtExclusiveGroup *m_exclusiveGroup; + QString m_shortcut; + QString m_toolTip; +}; + +#endif // QTACTION_H diff --git a/src/qtdesktop/qtexclusivegroup.cpp b/src/qtdesktop/qtexclusivegroup.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14622834ee006553f9de73d8eabdb143c511b6bb --- /dev/null +++ b/src/qtdesktop/qtexclusivegroup.cpp @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Components project. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qtexclusivegroup_p.h" + +#include <QtCore/QVariant> +#include "qdebug.h" + +#define CHECKED_PROPERTY "checked" + +static const char *checkableSignals[] = { + CHECKED_PROPERTY"Changed()", + "toggled(bool)", + "toggled()", + 0 +}; + +QtExclusiveGroup::QtExclusiveGroup(QObject *parent) + : QObject(parent) +{ + int index = metaObject()->indexOfMethod("updateCurrent()"); + m_updateCurrentMethod = metaObject()->method(index); +} + +void QtExclusiveGroup::setCurrent(QObject * o) +{ + if (m_current == o) + return; + + if (m_current) + m_current->setProperty(CHECKED_PROPERTY, QVariant(false)); + m_current = o; + if (m_current) + m_current->setProperty(CHECKED_PROPERTY, QVariant(true)); + emit currentChanged(); +} + +void QtExclusiveGroup::updateCurrent() +{ + QObject *checkable = sender(); + QVariant checkedVariant = checkable->property(CHECKED_PROPERTY); + if (checkedVariant.isValid() && checkedVariant.toBool()) + setCurrent(checkable); +} + +void QtExclusiveGroup::registerCheckable(QObject *o) +{ + for (const char **signalName = checkableSignals; *signalName; signalName++) { + int signalIndex = o->metaObject()->indexOfSignal(*signalName); + if (signalIndex != -1) { + QMetaMethod signalMethod = o->metaObject()->method(signalIndex); + connect(o, signalMethod, this, m_updateCurrentMethod, Qt::UniqueConnection); + connect(o, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterCheckable(QObject*)), Qt::UniqueConnection); + return; + } + } + + qWarning() << "QtExclusiveGroup::registerCheckable(): Cannot register" << o; +} + +void QtExclusiveGroup::unregisterCheckable(QObject *o) +{ + for (const char **signalName = checkableSignals; *signalName; signalName++) { + int signalIndex = o->metaObject()->indexOfSignal(*signalName); + if (signalIndex != -1) { + QMetaMethod signalMethod = o->metaObject()->method(signalIndex); + if (disconnect(o, signalMethod, this, m_updateCurrentMethod)) { + disconnect(o, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterCheckable(QObject*))); + break; + } + } + } +} diff --git a/src/qtdesktop/qtexclusivegroup_p.h b/src/qtdesktop/qtexclusivegroup_p.h new file mode 100644 index 0000000000000000000000000000000000000000..28ae93dcc34301816d151f74598c153d81a21599 --- /dev/null +++ b/src/qtdesktop/qtexclusivegroup_p.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Components project. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTEXCLUSIVEGROUP_H +#define QTEXCLUSIVEGROUP_H + +#include <QObject> +#include <QtQml/QtQml> + +class QtExclusiveGroup : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QObject *current READ current WRITE setCurrent NOTIFY currentChanged) + +public: + explicit QtExclusiveGroup(QObject *parent = 0); + +public Q_SLOTS: + void registerCheckable(QObject *o); + void unregisterCheckable(QObject *o); + + QObject *current() const { return m_current; } + void setCurrent(QObject * o); + +Q_SIGNALS: + void currentChanged(); + +private Q_SLOTS: + void updateCurrent(); + +private: + QObject * m_current; + QMetaMethod m_updateCurrentMethod; +}; + +#endif // QTEXCLUSIVEGROUP_H diff --git a/src/qtdesktop/styleplugin.pri b/src/qtdesktop/styleplugin.pri index 1c3618c034729a1dfa16a684d0f341e2a633cff5..5694f4d5bd5a835e62915a2b801d255f319bc079 100644 --- a/src/qtdesktop/styleplugin.pri +++ b/src/qtdesktop/styleplugin.pri @@ -9,6 +9,8 @@ HEADERS += \ $$PWD/qrangemodel_p_p.h \ $$PWD/qstyleitem_p.h \ $$PWD/qstyleplugin_p.h \ + $$PWD/qtaction_p.h \ + $$PWD/qtexclusivegroup_p.h \ $$PWD/qtmenu_p.h \ $$PWD/qtmenubar_p.h \ $$PWD/qtmenuitem_p.h \ @@ -25,6 +27,8 @@ SOURCES += \ $$PWD/qrangemodel.cpp \ $$PWD/qstyleitem.cpp \ $$PWD/qstyleplugin.cpp \ + $$PWD/qtaction.cpp \ + $$PWD/qtexclusivegroup.cpp \ $$PWD/qtmenu.cpp \ $$PWD/qtmenubar.cpp \ $$PWD/qtmenuitem.cpp \