diff --git a/src/declarativesplitter.cpp b/src/declarativesplitter.cpp new file mode 100644 index 0000000..86905e4 --- /dev/null +++ b/src/declarativesplitter.cpp @@ -0,0 +1,88 @@ +/* + declarativesplitter.cpp + + This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML. + + Copyright (C) 2013-2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Lova Widmark + + Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in + accordance with DeclarativeWidgets Commercial License Agreement provided with the Software. + + Contact info@kdab.com if any conditions of this licensing are not clear to you. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "declarativesplitter_p.h" + +#include +#include +#include + +class DeclarativeSplitterAttached::Private +{ + public: + Private(QWidget *w) + : stretch(0) + , widget(w) + { + } + + int stretch; + + QPointer widget; +}; + +DeclarativeSplitterAttached::DeclarativeSplitterAttached(QWidget *widget, QObject *parent) + : QObject(parent) + , d(new Private(widget)) +{ +} + +DeclarativeSplitterAttached::~DeclarativeSplitterAttached() +{ + delete d; +} + +void DeclarativeSplitterAttached::setStretch(int stretch) +{ + if (stretch == d->stretch) + return; + + d->stretch = stretch; + + emit stretchChanged(stretch); +} + +int DeclarativeSplitterAttached::stretch() const +{ + return d->stretch; +} + +DeclarativeSplitter::DeclarativeSplitter(QWidget *parent) + : QSplitter(parent) +{ +} + +DeclarativeSplitterAttached *DeclarativeSplitter::qmlAttachedProperties(QObject *parent) +{ + QWidget *widget = qobject_cast(parent); + if (widget) + return new DeclarativeSplitterAttached(widget, parent); + + qmlInfo(parent) << "Can only attach Splitter to widgets"; + + return Q_NULLPTR; +} diff --git a/src/declarativesplitter_p.h b/src/declarativesplitter_p.h new file mode 100644 index 0000000..f5ed2aa --- /dev/null +++ b/src/declarativesplitter_p.h @@ -0,0 +1,67 @@ +/* + declarativesplitter_p.h + + This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML. + + Copyright (C) 2013-2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Lova Widmark + + Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in + accordance with DeclarativeWidgets Commercial License Agreement provided with the Software. + + Contact info@kdab.com if any conditions of this licensing are not clear to you. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef DECLARATIVESPLITTER_P_H +#define DECLARATIVESPLITTER_P_H + +#include +#include +#include +#include + +class DeclarativeSplitterAttached : public QObject +{ + Q_OBJECT + + Q_PROPERTY(int stretch READ stretch WRITE setStretch NOTIFY stretchChanged) + + public: + DeclarativeSplitterAttached(QWidget *widget, QObject *parent); + ~DeclarativeSplitterAttached(); + + void setStretch(int stretch); + int stretch() const; + + Q_SIGNALS: + void stretchChanged(int stretch); + + private: + class Private; + Private *const d; +}; + +class DeclarativeSplitter : public QSplitter +{ + public: + DeclarativeSplitter(QWidget *parent = Q_NULLPTR); + + static DeclarativeSplitterAttached *qmlAttachedProperties(QObject *parent); +}; + +QML_DECLARE_TYPEINFO(DeclarativeSplitter, QML_HAS_ATTACHED_PROPERTIES) + +#endif // DECLARATIVESPLITTER_P_H diff --git a/src/declarativewidgets_plugin.cpp b/src/declarativewidgets_plugin.cpp index 02a7be2..4ae5c73 100644 --- a/src/declarativewidgets_plugin.cpp +++ b/src/declarativewidgets_plugin.cpp @@ -68,6 +68,8 @@ #include "scrollareawidgetcontainer_p.h" #include "stackedwidgetwidgetcontainer_p.h" #include "toolbarwidgetcontainer_p.h" +#include "splitterwidgetcontainer_p.h" +#include "declarativesplitter_p.h" #include #include @@ -199,4 +201,5 @@ void ExtensionpluginPlugin::registerTypes(const char *uri) qmlRegisterExtendedType(uri, 1, 0, "WebEngineView"); #endif qmlRegisterExtendedType(uri, 1, 0, "Widget"); + qmlRegisterExtendedType>(uri, 1, 0, "Splitter"); } diff --git a/src/splitterwidgetcontainer.cpp b/src/splitterwidgetcontainer.cpp new file mode 100644 index 0000000..fc2bbac --- /dev/null +++ b/src/splitterwidgetcontainer.cpp @@ -0,0 +1,65 @@ +/* + splitterwidgetcontainer.cpp + + This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML. + + Copyright (C) 2013-2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Lova Widmark + + Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in + accordance with DeclarativeWidgets Commercial License Agreement provided with the Software. + + Contact info@kdab.com if any conditions of this licensing are not clear to you. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "splitterwidgetcontainer_p.h" + +#include "declarativesplitter_p.h" + +#include +#include +#include + +SplitterWidgetContainer::SplitterWidgetContainer(QObject *parent) + : DefaultWidgetContainer(qobject_cast(parent)) +{ + Q_ASSERT(m_widget); +} + +void SplitterWidgetContainer::setLayout(QLayout *layout) +{ + Q_UNUSED(layout); + qmlInfo(m_widget) << "Can not set a Layout to a Splitter"; +} + +void SplitterWidgetContainer::addWidget(QWidget *widget) +{ + QObject *attachedProperties = qmlAttachedPropertiesObject(widget, false); + DeclarativeSplitterAttached *properties = qobject_cast(attachedProperties); + if (properties) { + QSizePolicy policy = widget->sizePolicy(); + policy.setHorizontalStretch(properties->stretch()); + policy.setVerticalStretch(properties->stretch()); + widget->setSizePolicy(policy); + } + + extendedSplitter()->addWidget(widget); +} + +QSplitter *SplitterWidgetContainer::extendedSplitter() const +{ + return static_cast(m_widget); +} diff --git a/src/splitterwidgetcontainer_p.h b/src/splitterwidgetcontainer_p.h new file mode 100644 index 0000000..3c4ff3e --- /dev/null +++ b/src/splitterwidgetcontainer_p.h @@ -0,0 +1,52 @@ +/* + splitterwidgetcontainer_p.h + + This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML. + + Copyright (C) 2013-2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Lova Widmark + + Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in + accordance with DeclarativeWidgets Commercial License Agreement provided with the Software. + + Contact info@kdab.com if any conditions of this licensing are not clear to you. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef SPLITTERWIDGETCONTAINER_P_H +#define SPLITTERWIDGETCONTAINER_P_H + +#include + +#include "defaultwidgetcontainer.h" + +QT_BEGIN_NAMESPACE +class QSplitter; +class QObject; +QT_END_NAMESPACE + +class SplitterWidgetContainer : public DefaultWidgetContainer +{ + public: + explicit SplitterWidgetContainer(QObject *parent = Q_NULLPTR); + + void setLayout(QLayout *layout) Q_DECL_OVERRIDE; + void addWidget(QWidget *widget) Q_DECL_OVERRIDE; + + private: + QSplitter *extendedSplitter() const; +}; + +#endif // SPLITTERWIDGETCONTAINER_P_H diff --git a/src/src.pro b/src/src.pro index d8c0224..6d4b028 100644 --- a/src/src.pro +++ b/src/src.pro @@ -85,7 +85,9 @@ HEADERS = \ stackedwidgetwidgetcontainer_p.h \ staticdialogmethodattached_p.h \ toolbarwidgetcontainer_p.h \ - widgetcontainerinterface_p.h + widgetcontainerinterface_p.h \ + splitterwidgetcontainer_p.h \ + declarativesplitter_p.h SOURCES = \ abstractdeclarativeobject.cpp \ @@ -135,4 +137,6 @@ SOURCES = \ scrollareawidgetcontainer.cpp \ stackedwidgetwidgetcontainer.cpp \ staticdialogmethodattached.cpp \ - toolbarwidgetcontainer.cpp + toolbarwidgetcontainer.cpp \ + splitterwidgetcontainer.cpp \ + declarativesplitter.cpp diff --git a/tests/auto/instantiatetypes/qml.qrc b/tests/auto/instantiatetypes/qml.qrc index 1a8ebb9..192dec0 100644 --- a/tests/auto/instantiatetypes/qml.qrc +++ b/tests/auto/instantiatetypes/qml.qrc @@ -66,5 +66,6 @@ qml/uncreatable/ItemSelectionModel.qml qml/uncreatable/TextDocument.qml qml/creatable/webenginewidgets/WebEngineView.qml + qml/creatable/widgets/Splitter.qml diff --git a/tests/auto/instantiatetypes/qml/creatable/widgets/Splitter.qml b/tests/auto/instantiatetypes/qml/creatable/widgets/Splitter.qml new file mode 100644 index 0000000..2dfca9b --- /dev/null +++ b/tests/auto/instantiatetypes/qml/creatable/widgets/Splitter.qml @@ -0,0 +1,32 @@ +/* + Splitter.qml + + This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML. + + Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Lova Widmark + + Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in + accordance with DeclarativeWidgets Commercial License Agreement provided with the Software. + + Contact info@kdab.com if any conditions of this licensing are not clear to you. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +import QtWidgets 1.0 + +Splitter { + +}