Skip to content

Commit

Permalink
#228 (bottom-up) Tree layout experimentation. WIP.
Browse files Browse the repository at this point in the history
Signed-off-by: cneben <benoit@destrat.io>
  • Loading branch information
cneben committed Aug 13, 2024
1 parent 88d56d8 commit 3bb0216
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ if (${QUICK_QANAVA_BUILD_SAMPLES})
add_subdirectory(samples/groups)
#add_subdirectory(samples/navigable)
add_subdirectory(samples/nodes)
add_subdirectory(samples/layouts)
#add_subdirectory(samples/selection)
#add_subdirectory(samples/style)
#add_subdirectory(samples/topology)
Expand Down
37 changes: 37 additions & 0 deletions samples/layouts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 3.16)

project(QuickQanava_sample_layouts VERSION 2.5.0 LANGUAGES CXX)

# Require C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Configure Qt
find_package(Qt6 REQUIRED COMPONENTS Core Quick Qml Quick QuickControls2)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:QT_QML_DEBUG>)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories(${CMAKE_CURRENT_SOURCE_DIR} "../../src")

if(NOT TARGET QuickQanava)
add_subdirectory(../../ quickqanava_build) # Use ../../src as quickqanava_build subdirectory
# see https://stackoverflow.com/questions/50408169/cmake-error-add-subdirectory-not-given-a-binary-directory/50408555
endif()

set(source_files
layouts.cpp
)

set (header_files)

add_executable(sample_layouts ${source_files} layouts.qrc)
target_include_directories(sample_layouts PUBLIC QuickQanava Qt${QT_VERSION_MAJOR}::QuickControls2)
target_link_libraries(sample_layouts
QuickQanava
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Gui
Qt${QT_VERSION_MAJOR}::QuickControls2
)
157 changes: 157 additions & 0 deletions samples/layouts/default.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
Copyright (c) 2008-2024, Benoit AUTHEMAN All rights reserved.
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 the author or Destrat.io 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 AUTHOR 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.
*/

import QtQuick
import QtQuick.Controls.Material
import QtQuick.Layouts

import QuickQanava 2.0 as Qan
import "qrc:/QuickQanava" as Qan

Qan.GraphView {
id: graphView
anchors.fill: parent
navigable : true
resizeHandlerColor: "#03a9f4" // SAMPLE: Set resize handler color to blue for 'resizable' nodes
gridThickColor: Material.theme === Material.Dark ? "#4e4e4e" : "#c1c1c1"
PinchHandler {
target: null
onActiveScaleChanged: {
console.error('centroid.position=' + centroid.position)
console.error('activeScale=' + activeScale)
var p = centroid.position
var f = activeScale > 1.0 ? 1. : -1.
graphView.zoomOn(p, graphView.zoom + (f * 0.03))
}
}
graph: Qan.Graph {
parent: graphView
id: graph
Component.onCompleted: {
var n1 = graph.insertNode()
n1.label = "Hello World"; n1.item.x=15; n1.item.y= 25
n1.item.ratio = 0.4
var n2 = graph.insertNode()
n2.label = "Node 2"; n2.item.x=15; n2.item.y= 125

var e = graph.insertEdge(n1, n2);
//defaultEdgeStyle.lineType = Qan.EdgeStyle.Curved
}
onNodeClicked: (node) => {
notifyUser( "Node <b>" + node.label + "</b> clicked" )
nodeEditor.node = node
}
onNodeRightClicked: (node) => { notifyUser( "Node <b>" + node.label + "</b> right clicked" ) }
onNodeDoubleClicked: (node) => { notifyUser( "Node <b>" + node.label + "</b> double clicked" ) }
onNodeMoved: (node) => { notifyUser("Node <b>" + node.label + "</b> moved") }
} // Qan.Graph

Menu { // Context menu demonstration
id: contextMenu
property var node: undefined
MenuItem {
text: "Insert Node"
onClicked: {
let n = graph.insertNode()
n.label = 'New Node'
n.item.x = contextMenu.x
n.item.y = contextMenu.y
}
}
MenuItem {
text: "Remove node"
enabled: contextMenu.node !== undefined
onClicked: {
graph.removeNode(contextMenu.node)
contextMenu.node = undefined
}
}
onClosed: { // Clean internal state when context menu us closed
contextMenu.node = undefined
}
} // Menu

onRightClicked: function(pos) {
contextMenu.x = pos.x
contextMenu.y = pos.y
contextMenu.open()
}

ToolTip { id: toolTip; timeout: 2500 }
function notifyUser(message) { toolTip.text=message; toolTip.open() }
Label {
anchors.left: parent.left; anchors.leftMargin: 15
anchors.bottom: parent.bottom; anchors.bottomMargin: 15
text: "Use CTRL+Click to select multiples nodes"
}
Pane {
id: nodeEditor
property var node: undefined
onNodeChanged: nodeItem = node ? node.item : undefined
property var nodeItem: undefined
anchors.bottom: parent.bottom; anchors.bottomMargin: 15
anchors.right: parent.right; anchors.rightMargin: 15
padding: 0
Frame {
ColumnLayout {
Label {
text: nodeEditor.node ? "Editing node <b>" + nodeEditor.node.label + "</b>": "Select a node..."
}
CheckBox {
text: "Draggable"
enabled: nodeEditor.nodeItem !== undefined
checked: nodeEditor.nodeItem ? nodeEditor.nodeItem.draggable : false
onClicked: nodeEditor.nodeItem.draggable = checked
}
CheckBox {
text: "Resizable"
enabled: nodeEditor.nodeItem !== undefined
checked: nodeEditor.nodeItem ? nodeEditor.nodeItem.resizable : false
onClicked: nodeEditor.nodeItem.resizable = checked
}
CheckBox {
text: "Selected (read-only)"
enabled: false
checked: nodeEditor.nodeItem ? nodeEditor.nodeItem.selected : false
}
CheckBox {
text: "Selectable"
enabled: nodeEditor.nodeItem != null
checked: nodeEditor.nodeItem ? nodeEditor.nodeItem.selectable : false
onClicked: nodeEditor.nodeItem.selectable = checked
}
Label { text: "style.backRadius" }
Slider {
from: 0.; to: 15.0;
value: defaultNodeStyle.backRadius
stepSize: 1.0
onMoved: defaultNodeStyle.backRadius = value
}
}
}
}
} // Qan.GraphView

58 changes: 58 additions & 0 deletions samples/layouts/layouts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright (c) 2008-2024, Benoit AUTHEMAN All rights reserved.
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 the author or Destrat.io 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 AUTHOR 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.
*/

//-----------------------------------------------------------------------------
// This file is a part of the QuickQanava software library.
//
// \file layouts.cpp
// \author benoit@destrat.io
// \date 20240813
//-----------------------------------------------------------------------------

// Qt headers
#include <QGuiApplication>
#include <QtQml>
#include <QQuickStyle>

// QuickQanava headers
#include <QuickQanava.h>

//-----------------------------------------------------------------------------
int main( int argc, char** argv )
{
QGuiApplication app(argc, argv);
QQuickStyle::setStyle("Material");
QQmlApplicationEngine* engine = new QQmlApplicationEngine();
engine->addPluginPath(QStringLiteral("../../src")); // Necessary only for development when plugin is not installed to QTDIR/qml
QuickQanava::initialize(engine);
engine->load(QUrl("qrc:/layouts.qml"));
const auto status = app.exec();
delete engine;
return status;
}
//-----------------------------------------------------------------------------


79 changes: 79 additions & 0 deletions samples/layouts/layouts.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright (c) 2008-2024, Benoit AUTHEMAN All rights reserved.
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 the author or Destrat.io 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 AUTHOR 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.
*/

import QtQuick
import QtQuick.Controls.Material
import QtQuick.Layouts

import QuickQanava 2.0 as Qan
import "qrc:/QuickQanava" as Qan

ApplicationWindow {
id: window
visible: true
width: 1280; height: 720
title: "Layouts Sample"

Qan.GraphView {
id: graphView
anchors.fill: parent
navigable : true
resizeHandlerColor: "#03a9f4"
gridThickColor: Material.theme === Material.Dark ? "#4e4e4e" : "#c1c1c1"
graph: Qan.Graph {
parent: graphView
id: graph
Component.onCompleted: {
var n1 = graph.insertNode()
n1.label = "Hello World"; n1.item.x=15; n1.item.y= 25
n1.item.ratio = 0.4
var n2 = graph.insertNode()
n2.label = "Node 2"; n2.item.x=15; n2.item.y= 125

var e = graph.insertEdge(n1, n2);
}
} // Qan.Graph

Menu { // Context menu demonstration
id: contextMenu
MenuItem {
text: "Insert Node"
onClicked: {
let n = graph.insertNode()
n.label = 'New Node'
n.item.x = contextMenu.x
n.item.y = contextMenu.y
}
}
} // Menu
onRightClicked: function(pos) {
contextMenu.x = pos.x
contextMenu.y = pos.y
contextMenu.open()
}
} // Qan.GraphView
}

6 changes: 6 additions & 0 deletions samples/layouts/layouts.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/">
<file>layouts.qml</file>
<file>qtquickcontrols2.conf</file>
</qresource>
</RCC>
9 changes: 9 additions & 0 deletions samples/layouts/qtquickcontrols2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Material]
Primary=#03A9F4
Accent=#03A9F4
Theme=Light
Variant=Dense

[Universal]
Accent=#41cd52
Theme=Light

0 comments on commit 3bb0216

Please sign in to comment.