Skip to content

Commit

Permalink
quick: Implement separator support
Browse files Browse the repository at this point in the history
you can now drag separators in the QML example
  • Loading branch information
iamsergio committed Jun 1, 2020
1 parent 83af55a commit 180fb96
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/private/multisplitter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ if (BUILD_MULTISPLITTER_QTQUICK_FRONTEND)
add_definitions(-DKDMULTISPLITTER_QTQUICK)
endif()

add_library(kddockwidgets_multisplitter SHARED ${MULTISPLITTER_SRCS})
qt5_add_resources(MULTISPLITTER_RESOURCES ${CMAKE_CURRENT_SOURCE_DIR}/multisplitter.qrc)

add_library(kddockwidgets_multisplitter SHARED ${MULTISPLITTER_SRCS} ${MULTISPLITTER_RESOURCES})
add_library(KDAB::kddockwidgets_multisplitter ALIAS kddockwidgets_multisplitter)
target_link_libraries(kddockwidgets_multisplitter Qt5::Core Qt5::Widgets)

Expand Down
2 changes: 1 addition & 1 deletion src/private/multisplitter/Separator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct Separator::Private
Private(Widget *host)
: m_hostWidget(host) {}

Qt::Orientation orientation;
Qt::Orientation orientation = Qt::Horizontal;
QRect geometry;
int lazyPosition = 0;
// SeparatorOptions m_options; TODO: Have a Layouting::Config
Expand Down
1 change: 0 additions & 1 deletion src/private/multisplitter/Separator_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class MULTISPLITTER_EXPORT Separator

///@brief Returns whether we're dragging a separator. Can be useful for the app to stop other work while we're not in the final size
static bool isResizing();

virtual Widget* asWidget() = 0;

protected:
Expand Down
35 changes: 34 additions & 1 deletion src/private/multisplitter/Separator_quick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "Logging_p.h"
#include "Item_p.h"

#include <QTimer>

using namespace Layouting;

namespace Layouting {
Expand All @@ -34,7 +36,8 @@ class RubberBand : public QQuickItem
public:
RubberBand(Layouting::Widget *parent)
: QQuickItem(parent ? qobject_cast<QQuickItem*>(parent->asQObject()) : nullptr)
, Layouting::Widget_quick(this) {
, Layouting::Widget_quick(this)
{
}
};

Expand All @@ -45,6 +48,15 @@ SeparatorQuick::SeparatorQuick(Layouting::Widget *parent)
, Separator(parent)
, Layouting::Widget_quick(this)
{
createQQuickItem(QStringLiteral(":/kddockwidgets/multisplitter/qml/Separator.qml"), this);

// Only set on Separator::init(), so single-shot
QTimer::singleShot(0, this, &SeparatorQuick::isVerticalChanged);
}

bool SeparatorQuick::isVertical() const
{
return Separator::isVertical();
}

Layouting::Widget *SeparatorQuick::createRubberBand(Layouting::Widget *parent)
Expand All @@ -61,3 +73,24 @@ Widget *SeparatorQuick::asWidget()
{
return this;
}

void SeparatorQuick::onMousePressed()
{
Separator::onMousePress();
}

void SeparatorQuick::onMouseMoved(QPointF localPos)
{
const QPointF pos = QQuickItem::mapToItem(parentItem(), localPos);
Separator::onMouseMove(pos.toPoint());
}

void SeparatorQuick::onMouseReleased()
{
Separator::onMouseReleased();
}

void SeparatorQuick::onMouseDoubleClicked()
{
Separator::onMouseDoubleClick();
}
15 changes: 14 additions & 1 deletion src/private/multisplitter/Separator_quick.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,25 @@ class MULTISPLITTER_EXPORT SeparatorQuick
, public Layouting::Widget_quick
{
Q_OBJECT
Q_PROPERTY(bool isVertical READ isVertical NOTIFY isVerticalChanged)
public:
explicit SeparatorQuick(Layouting::Widget *parent = nullptr);
protected:

bool isVertical() const;

protected:
Widget* createRubberBand(Widget *parent) override;
Widget *asWidget() override;

public:
// Interface with QML:
Q_INVOKABLE void onMousePressed();
Q_INVOKABLE void onMouseMoved(QPointF localPos);
Q_INVOKABLE void onMouseReleased();
Q_INVOKABLE void onMouseDoubleClicked();
Q_SIGNALS:
// constant but it's only set after Separator::init
void isVerticalChanged();
};

}
Expand Down
5 changes: 5 additions & 0 deletions src/private/multisplitter/multisplitter.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/kddockwidgets/multisplitter/">
<file>qml/Separator.qml</file>
</qresource>
</RCC>
30 changes: 30 additions & 0 deletions src/private/multisplitter/qml/Separator.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import QtQuick 2.6

Rectangle {
id: root
anchors.fill: parent
color: "yellow"

readonly property QtObject kddwSeparator: parent

MouseArea {
cursorShape: kddwSeparator ? (kddwSeparator.isVertical ? Qt.SizeVerCursor : Qt.SizeHorCursor)
: Qt.SizeHorCursor
anchors.fill: parent
onPressed: {
kddwSeparator.onMousePressed();
}

onReleased: {
kddwSeparator.onMouseReleased();
}

onPositionChanged: {
kddwSeparator.onMouseMoved(Qt.point(mouse.x, mouse.y));
}

onDoubleClicked: {
kddwSeparator.onMouseDoubleClicked();
}
}
}

0 comments on commit 180fb96

Please sign in to comment.