Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Add support for Splitter
Browse files Browse the repository at this point in the history
  • Loading branch information
Znurre committed Feb 19, 2018
1 parent 2a75229 commit 7958894
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 10 deletions.
3 changes: 3 additions & 0 deletions extensionplugin/extensionplugin_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
#include "scrollareawidgetcontainer_p.h"
#include "stackedwidgetwidgetcontainer_p.h"
#include "toolbarwidgetcontainer_p.h"
#include "splitterwidgetcontainer_p.h"
#include "declarativesplitter_p.h"

#include <QButtonGroup>
#include <QCalendarWidget>
Expand Down Expand Up @@ -188,4 +190,5 @@ void ExtensionpluginPlugin::registerTypes(const char *uri)
qmlRegisterExtendedType<QWebEngineView, DeclarativeWidgetExtension>(uri, 1, 0, "WebEngineView");
#endif
qmlRegisterExtendedType<QWidget, DeclarativeWidgetExtension>(uri, 1, 0, "Widget");
qmlRegisterExtendedType<DeclarativeSplitter, DeclarativeContainerWidgetExtension<SplitterWidgetContainer>>(uri, 1, 0, "Splitter");
}
88 changes: 88 additions & 0 deletions lib/declarativesplitter.cpp
Original file line number Diff line number Diff line change
@@ -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, [email protected]
Author: Lova Widmark <[email protected]>
Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in
accordance with DeclarativeWidgets Commercial License Agreement provided with the Software.
Contact [email protected] 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 <http://www.gnu.org/licenses/>.
*/

#include "declarativesplitter_p.h"

#include <QPointer>
#include <QWidget>
#include <QQmlInfo>

class DeclarativeSplitterAttached::Private
{
public:
Private(QWidget *w)
: stretch(0)
, widget(w)
{
}

int stretch;

QPointer<QWidget> 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<QWidget*>(parent);
if (widget)
return new DeclarativeSplitterAttached(widget, parent);

qmlInfo(parent) << "Can only attach Splitter to widgets";

return Q_NULLPTR;
}
67 changes: 67 additions & 0 deletions lib/declarativesplitter_p.h
Original file line number Diff line number Diff line change
@@ -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, [email protected]
Author: Lova Widmark <[email protected]>
Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in
accordance with DeclarativeWidgets Commercial License Agreement provided with the Software.
Contact [email protected] 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 <http://www.gnu.org/licenses/>.
*/

#ifndef DECLARATIVESPLITTER_P_H
#define DECLARATIVESPLITTER_P_H

#include <QtGlobal>
#include <QObject>
#include <QSplitter>
#include <QtQml>

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
25 changes: 15 additions & 10 deletions lib/lib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ HEADERS = \
staticdialogmethodattached_p.h \
toolbarwidgetcontainer_p.h \
widgetcontainerinterface_p.h \
declarativeloaderwidget_p.h \
declarativespaceritem_p.h \
declarativeline_p.h \
declarativelabelextension_p.h \
declarativetabstops_p.h
declarativeloaderwidget_p.h \
declarativespaceritem_p.h \
declarativeline_p.h \
declarativelabelextension_p.h \
declarativetabstops_p.h \
splitterwidgetcontainer_p.h \
declarativesplitter_p.h

SOURCES = \
abstractdeclarativeobject.cpp \
Expand Down Expand Up @@ -116,8 +118,11 @@ SOURCES = \
stackedwidgetwidgetcontainer.cpp \
staticdialogmethodattached.cpp \
toolbarwidgetcontainer.cpp \
declarativeloaderwidget.cpp \
declarativespaceritem.cpp \
declarativeline.cpp \
declarativelabelextension.cpp \
declarativetabstops.cpp
declarativeloaderwidget.cpp \
declarativespaceritem.cpp \
declarativeline.cpp \
declarativelabelextension.cpp \
declarativetabstops.cpp \
splitterwidgetcontainer.cpp \
declarativesplitter.cpp

65 changes: 65 additions & 0 deletions lib/splitterwidgetcontainer.cpp
Original file line number Diff line number Diff line change
@@ -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, [email protected]
Author: Lova Widmark <[email protected]>
Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in
accordance with DeclarativeWidgets Commercial License Agreement provided with the Software.
Contact [email protected] 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 <http://www.gnu.org/licenses/>.
*/

#include "splitterwidgetcontainer_p.h"

#include "declarativesplitter_p.h"

#include <QSplitter>
#include <QQmlInfo>
#include <QQmlEngine>

SplitterWidgetContainer::SplitterWidgetContainer(QObject *parent)
: DefaultWidgetContainer(qobject_cast<QSplitter*>(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<QSplitter>(widget, false);
DeclarativeSplitterAttached *properties = qobject_cast<DeclarativeSplitterAttached*>(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<QSplitter*>(m_widget);
}
52 changes: 52 additions & 0 deletions lib/splitterwidgetcontainer_p.h
Original file line number Diff line number Diff line change
@@ -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, [email protected]
Author: Lova Widmark <[email protected]>
Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in
accordance with DeclarativeWidgets Commercial License Agreement provided with the Software.
Contact [email protected] 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 <http://www.gnu.org/licenses/>.
*/

#ifndef SPLITTERWIDGETCONTAINER_P_H
#define SPLITTERWIDGETCONTAINER_P_H

#include <QtGlobal>

#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
1 change: 1 addition & 0 deletions tests/auto/instantiatetypes/qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@
<file>qml/uncreatable/ItemSelectionModel.qml</file>
<file>qml/uncreatable/TextDocument.qml</file>
<file>qml/creatable/webenginewidgets/WebEngineView.qml</file>
<file>qml/creatable/widgets/Splitter.qml</file>
</qresource>
</RCC>
32 changes: 32 additions & 0 deletions tests/auto/instantiatetypes/qml/creatable/widgets/Splitter.qml
Original file line number Diff line number Diff line change
@@ -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, [email protected]
Author: Lova Widmark <[email protected]>
Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in
accordance with DeclarativeWidgets Commercial License Agreement provided with the Software.
Contact [email protected] 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 <http://www.gnu.org/licenses/>.
*/

import QtWidgets 1.0

Splitter {

}

0 comments on commit 7958894

Please sign in to comment.