Skip to content

Commit 7f3381a

Browse files
committed
Add support for Splitter
1 parent 7f1d0ca commit 7f3381a

File tree

8 files changed

+314
-2
lines changed

8 files changed

+314
-2
lines changed

src/declarativesplitter.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
declarativesplitter.cpp
3+
4+
This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML.
5+
6+
Copyright (C) 2013-2017 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
7+
Author: Lova Widmark <[email protected]>
8+
9+
Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in
10+
accordance with DeclarativeWidgets Commercial License Agreement provided with the Software.
11+
12+
Contact [email protected] if any conditions of this licensing are not clear to you.
13+
14+
This program is free software; you can redistribute it and/or modify
15+
it under the terms of the GNU General Public License as published by
16+
the Free Software Foundation, either version 2 of the License, or
17+
(at your option) any later version.
18+
19+
This program is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
GNU General Public License for more details.
23+
24+
You should have received a copy of the GNU General Public License
25+
along with this program. If not, see <http://www.gnu.org/licenses/>.
26+
*/
27+
28+
#include "declarativesplitter_p.h"
29+
30+
#include <QPointer>
31+
#include <QWidget>
32+
#include <QQmlInfo>
33+
34+
class DeclarativeSplitterAttached::Private
35+
{
36+
public:
37+
Private(QWidget *w)
38+
: stretch(0)
39+
, widget(w)
40+
{
41+
}
42+
43+
int stretch;
44+
45+
QPointer<QWidget> widget;
46+
};
47+
48+
DeclarativeSplitterAttached::DeclarativeSplitterAttached(QWidget *widget, QObject *parent)
49+
: QObject(parent)
50+
, d(new Private(widget))
51+
{
52+
}
53+
54+
DeclarativeSplitterAttached::~DeclarativeSplitterAttached()
55+
{
56+
delete d;
57+
}
58+
59+
void DeclarativeSplitterAttached::setStretch(int stretch)
60+
{
61+
if (stretch == d->stretch)
62+
return;
63+
64+
d->stretch = stretch;
65+
66+
emit stretchChanged(stretch);
67+
}
68+
69+
int DeclarativeSplitterAttached::stretch() const
70+
{
71+
return d->stretch;
72+
}
73+
74+
DeclarativeSplitter::DeclarativeSplitter(QWidget *parent)
75+
: QSplitter(parent)
76+
{
77+
}
78+
79+
DeclarativeSplitterAttached *DeclarativeSplitter::qmlAttachedProperties(QObject *parent)
80+
{
81+
QWidget *widget = qobject_cast<QWidget*>(parent);
82+
if (widget)
83+
return new DeclarativeSplitterAttached(widget, parent);
84+
85+
qmlInfo(parent) << "Can only attach Splitter to widgets";
86+
87+
return Q_NULLPTR;
88+
}

src/declarativesplitter_p.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
declarativesplitter_p.h
3+
4+
This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML.
5+
6+
Copyright (C) 2013-2017 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
7+
Author: Lova Widmark <[email protected]>
8+
9+
Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in
10+
accordance with DeclarativeWidgets Commercial License Agreement provided with the Software.
11+
12+
Contact [email protected] if any conditions of this licensing are not clear to you.
13+
14+
This program is free software; you can redistribute it and/or modify
15+
it under the terms of the GNU General Public License as published by
16+
the Free Software Foundation, either version 2 of the License, or
17+
(at your option) any later version.
18+
19+
This program is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
GNU General Public License for more details.
23+
24+
You should have received a copy of the GNU General Public License
25+
along with this program. If not, see <http://www.gnu.org/licenses/>.
26+
*/
27+
28+
#ifndef DECLARATIVESPLITTER_P_H
29+
#define DECLARATIVESPLITTER_P_H
30+
31+
#include <QtGlobal>
32+
#include <QObject>
33+
#include <QSplitter>
34+
#include <QtQml>
35+
36+
class DeclarativeSplitterAttached : public QObject
37+
{
38+
Q_OBJECT
39+
40+
Q_PROPERTY(int stretch READ stretch WRITE setStretch NOTIFY stretchChanged)
41+
42+
public:
43+
DeclarativeSplitterAttached(QWidget *widget, QObject *parent);
44+
~DeclarativeSplitterAttached();
45+
46+
void setStretch(int stretch);
47+
int stretch() const;
48+
49+
Q_SIGNALS:
50+
void stretchChanged(int stretch);
51+
52+
private:
53+
class Private;
54+
Private *const d;
55+
};
56+
57+
class DeclarativeSplitter : public QSplitter
58+
{
59+
public:
60+
DeclarativeSplitter(QWidget *parent = Q_NULLPTR);
61+
62+
static DeclarativeSplitterAttached *qmlAttachedProperties(QObject *parent);
63+
};
64+
65+
QML_DECLARE_TYPEINFO(DeclarativeSplitter, QML_HAS_ATTACHED_PROPERTIES)
66+
67+
#endif // DECLARATIVESPLITTER_P_H

src/declarativewidgets_plugin.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
#include "scrollareawidgetcontainer_p.h"
6969
#include "stackedwidgetwidgetcontainer_p.h"
7070
#include "toolbarwidgetcontainer_p.h"
71+
#include "splitterwidgetcontainer_p.h"
72+
#include "declarativesplitter_p.h"
7173

7274
#include <QButtonGroup>
7375
#include <QCalendarWidget>
@@ -199,4 +201,5 @@ void ExtensionpluginPlugin::registerTypes(const char *uri)
199201
qmlRegisterExtendedType<QWebEngineView, DeclarativeWidgetExtension>(uri, 1, 0, "WebEngineView");
200202
#endif
201203
qmlRegisterExtendedType<QWidget, DeclarativeWidgetExtension>(uri, 1, 0, "Widget");
204+
qmlRegisterExtendedType<DeclarativeSplitter, DeclarativeContainerWidgetExtension<SplitterWidgetContainer>>(uri, 1, 0, "Splitter");
202205
}

src/splitterwidgetcontainer.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
splitterwidgetcontainer.cpp
3+
4+
This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML.
5+
6+
Copyright (C) 2013-2017 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
7+
Author: Lova Widmark <[email protected]>
8+
9+
Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in
10+
accordance with DeclarativeWidgets Commercial License Agreement provided with the Software.
11+
12+
Contact [email protected] if any conditions of this licensing are not clear to you.
13+
14+
This program is free software; you can redistribute it and/or modify
15+
it under the terms of the GNU General Public License as published by
16+
the Free Software Foundation, either version 2 of the License, or
17+
(at your option) any later version.
18+
19+
This program is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
GNU General Public License for more details.
23+
24+
You should have received a copy of the GNU General Public License
25+
along with this program. If not, see <http://www.gnu.org/licenses/>.
26+
*/
27+
28+
#include "splitterwidgetcontainer_p.h"
29+
30+
#include "declarativesplitter_p.h"
31+
32+
#include <QSplitter>
33+
#include <QQmlInfo>
34+
#include <QQmlEngine>
35+
36+
SplitterWidgetContainer::SplitterWidgetContainer(QObject *parent)
37+
: DefaultWidgetContainer(qobject_cast<QSplitter*>(parent))
38+
{
39+
Q_ASSERT(m_widget);
40+
}
41+
42+
void SplitterWidgetContainer::setLayout(QLayout *layout)
43+
{
44+
Q_UNUSED(layout);
45+
qmlInfo(m_widget) << "Can not set a Layout to a Splitter";
46+
}
47+
48+
void SplitterWidgetContainer::addWidget(QWidget *widget)
49+
{
50+
QObject *attachedProperties = qmlAttachedPropertiesObject<QSplitter>(widget, false);
51+
DeclarativeSplitterAttached *properties = qobject_cast<DeclarativeSplitterAttached*>(attachedProperties);
52+
if (properties) {
53+
QSizePolicy policy = widget->sizePolicy();
54+
policy.setHorizontalStretch(properties->stretch());
55+
policy.setVerticalStretch(properties->stretch());
56+
widget->setSizePolicy(policy);
57+
}
58+
59+
extendedSplitter()->addWidget(widget);
60+
}
61+
62+
QSplitter *SplitterWidgetContainer::extendedSplitter() const
63+
{
64+
return static_cast<QSplitter*>(m_widget);
65+
}

src/splitterwidgetcontainer_p.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
splitterwidgetcontainer_p.h
3+
4+
This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML.
5+
6+
Copyright (C) 2013-2017 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
7+
Author: Lova Widmark <[email protected]>
8+
9+
Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in
10+
accordance with DeclarativeWidgets Commercial License Agreement provided with the Software.
11+
12+
Contact [email protected] if any conditions of this licensing are not clear to you.
13+
14+
This program is free software; you can redistribute it and/or modify
15+
it under the terms of the GNU General Public License as published by
16+
the Free Software Foundation, either version 2 of the License, or
17+
(at your option) any later version.
18+
19+
This program is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
GNU General Public License for more details.
23+
24+
You should have received a copy of the GNU General Public License
25+
along with this program. If not, see <http://www.gnu.org/licenses/>.
26+
*/
27+
28+
#ifndef SPLITTERWIDGETCONTAINER_P_H
29+
#define SPLITTERWIDGETCONTAINER_P_H
30+
31+
#include <QtGlobal>
32+
33+
#include "defaultwidgetcontainer.h"
34+
35+
QT_BEGIN_NAMESPACE
36+
class QSplitter;
37+
class QObject;
38+
QT_END_NAMESPACE
39+
40+
class SplitterWidgetContainer : public DefaultWidgetContainer
41+
{
42+
public:
43+
explicit SplitterWidgetContainer(QObject *parent = Q_NULLPTR);
44+
45+
void setLayout(QLayout *layout) Q_DECL_OVERRIDE;
46+
void addWidget(QWidget *widget) Q_DECL_OVERRIDE;
47+
48+
private:
49+
QSplitter *extendedSplitter() const;
50+
};
51+
52+
#endif // SPLITTERWIDGETCONTAINER_P_H

src/src.pro

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ HEADERS = \
8585
stackedwidgetwidgetcontainer_p.h \
8686
staticdialogmethodattached_p.h \
8787
toolbarwidgetcontainer_p.h \
88-
widgetcontainerinterface_p.h
88+
widgetcontainerinterface_p.h \
89+
splitterwidgetcontainer_p.h \
90+
declarativesplitter_p.h
8991

9092
SOURCES = \
9193
abstractdeclarativeobject.cpp \
@@ -135,4 +137,6 @@ SOURCES = \
135137
scrollareawidgetcontainer.cpp \
136138
stackedwidgetwidgetcontainer.cpp \
137139
staticdialogmethodattached.cpp \
138-
toolbarwidgetcontainer.cpp
140+
toolbarwidgetcontainer.cpp \
141+
splitterwidgetcontainer.cpp \
142+
declarativesplitter.cpp

tests/auto/instantiatetypes/qml.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@
6666
<file>qml/uncreatable/ItemSelectionModel.qml</file>
6767
<file>qml/uncreatable/TextDocument.qml</file>
6868
<file>qml/creatable/webenginewidgets/WebEngineView.qml</file>
69+
<file>qml/creatable/widgets/Splitter.qml</file>
6970
</qresource>
7071
</RCC>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Splitter.qml
3+
4+
This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML.
5+
6+
Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
7+
Author: Lova Widmark <[email protected]>
8+
9+
Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in
10+
accordance with DeclarativeWidgets Commercial License Agreement provided with the Software.
11+
12+
Contact [email protected] if any conditions of this licensing are not clear to you.
13+
14+
This program is free software; you can redistribute it and/or modify
15+
it under the terms of the GNU General Public License as published by
16+
the Free Software Foundation, either version 2 of the License, or
17+
(at your option) any later version.
18+
19+
This program is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
GNU General Public License for more details.
23+
24+
You should have received a copy of the GNU General Public License
25+
along with this program. If not, see <http://www.gnu.org/licenses/>.
26+
*/
27+
28+
import QtWidgets 1.0
29+
30+
Splitter {
31+
32+
}

0 commit comments

Comments
 (0)