-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented data manipulation. Added example to show the new feature
- Loading branch information
Showing
16 changed files
with
397 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
QT += quick | ||
|
||
CONFIG += c++11 | ||
|
||
# You can make your code fail to compile if it uses deprecated APIs. | ||
# In order to do so, uncomment the following line. | ||
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 | ||
|
||
RESOURCES += qml.qrc \ | ||
../../plugin/treeview.qrc \ | ||
qml.qrc | ||
|
||
# Additional import path used to resolve QML modules in Qt Creator's code model | ||
QML_IMPORT_PATH = | ||
|
||
# Additional import path used to resolve QML modules just for Qt Quick Designer | ||
QML_DESIGNER_IMPORT_PATH = | ||
|
||
HEADERS += \ | ||
../../plugin/tree_item.h \ | ||
../../plugin/tree_model.h \ | ||
tree_manipulator.h | ||
|
||
SOURCES += \ | ||
../../plugin/tree_item.cpp \ | ||
../../plugin/tree_model.cpp \ | ||
main.cpp \ | ||
tree_manipulator.cpp | ||
|
||
INCLUDEPATH += \ | ||
../../plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <QGuiApplication> | ||
#include <QQmlApplicationEngine> | ||
#include <QQmlContext> | ||
|
||
#include "tree_model.h" | ||
#include "tree_manipulator.h" | ||
|
||
|
||
int main(int argc, char *argv[]) { | ||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) | ||
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); | ||
#endif | ||
QGuiApplication app(argc, argv); | ||
QQmlApplicationEngine engine; | ||
|
||
auto treeModel = new TreeModel(&engine); | ||
auto treeManipulator = new TreeManipulator(*treeModel, &engine); | ||
|
||
engine.rootContext()->setContextProperty("treeManipulator", QVariant::fromValue(treeManipulator)); | ||
|
||
const QUrl url(QStringLiteral("qrc:/main.qml")); | ||
QObject::connect( | ||
&engine, &QQmlApplicationEngine::objectCreated, &app, | ||
[url](QObject *obj, const QUrl &objUrl) { | ||
if (!obj && url == objUrl) QCoreApplication::exit(-1); | ||
}, | ||
Qt::QueuedConnection); | ||
engine.load(url); | ||
|
||
return app.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import QtQuick 2.15 | ||
import QtQuick.Window 2.15 | ||
import QtQuick.Controls 2.15 | ||
import QtQuick.Layouts 1.12 | ||
|
||
Window { | ||
width: 600 | ||
height: 400 | ||
visible: true | ||
title: qsTr("Tree Manipulation") | ||
|
||
|
||
ColumnLayout { | ||
anchors.fill: parent | ||
|
||
|
||
TreeView { | ||
id: treeView | ||
|
||
Layout.fillWidth: true | ||
Layout.fillHeight: true | ||
|
||
model: treeManipulator.sourceModel() | ||
selectionEnabled: true | ||
|
||
onCurrentIndexChanged: if(currentIndex) console.log("current index is (row=" + currentIndex.row + ", depth=" + model.depth(currentIndex) + ")") | ||
onCurrentDataChanged: if(currentData) console.log("current data is " + currentData) | ||
onCurrentItemChanged: if(currentItem) console.log("current item is " + currentItem) | ||
|
||
} | ||
|
||
ColumnLayout { | ||
Layout.fillWidth: true | ||
Layout.fillHeight: true | ||
|
||
MenuSeparator { | ||
Layout.fillWidth: true | ||
} | ||
|
||
TextArea { | ||
id: txtEdit | ||
|
||
Layout.fillWidth: true | ||
|
||
placeholderText: "Write data to add..." | ||
|
||
function notEmpty() { return text !== ""} | ||
function clear() { text = "" } | ||
} | ||
|
||
Row { | ||
Layout.margins: 8 | ||
spacing: 16 | ||
|
||
Button { | ||
id: addBtn | ||
text: "Add top level item" | ||
enabled: txtEdit.notEmpty() | ||
|
||
onClicked: { | ||
treeManipulator.addTopLevelItem(txtEdit.text) | ||
txtEdit.clear() | ||
} | ||
} | ||
|
||
Button { | ||
id: addChildBtn | ||
text: "Add child item" | ||
enabled: txtEdit.notEmpty() && treeView.currentItem | ||
|
||
onClicked: { | ||
treeManipulator.addItem(treeView.currentIndex, txtEdit.text) | ||
txtEdit.clear() | ||
} | ||
} | ||
|
||
Button { | ||
id: delBtn | ||
text: "Remove item" | ||
enabled: treeView.currentItem | ||
|
||
onClicked: { | ||
treeManipulator.removeItem(treeView.currentIndex) | ||
} | ||
} | ||
|
||
|
||
Button { | ||
id: editBtn | ||
text: "Edit item" | ||
enabled: txtEdit.notEmpty() && treeView.currentItem | ||
|
||
onClicked: { | ||
treeManipulator.editItem(treeView.currentIndex, txtEdit.text) | ||
txtEdit.clear() | ||
} | ||
} | ||
|
||
Button { | ||
id: clearBtn | ||
text: "Clear tree" | ||
|
||
onClicked: { | ||
treeManipulator.reset(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<RCC> | ||
<qresource prefix="/"> | ||
<file>main.qml</file> | ||
</qresource> | ||
</RCC> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "tree_manipulator.h" | ||
#include "tree_model.h" | ||
#include "tree_item.h" | ||
|
||
TreeManipulator::TreeManipulator(TreeModel& model, QObject* parent) | ||
: QObject(parent), | ||
_model(&model) | ||
{ | ||
} | ||
|
||
QVariant TreeManipulator::sourceModel() const | ||
{ | ||
return QVariant::fromValue(_model); | ||
} | ||
|
||
void TreeManipulator::addTopLevelItem(const QString& data) | ||
{ | ||
_model->addTopLevelItem(new TreeItem(data)); | ||
} | ||
|
||
void TreeManipulator::addItem(const QModelIndex& index, const QString& data) | ||
{ | ||
if(!index.isValid()){ | ||
return; | ||
} | ||
|
||
auto parent = static_cast<TreeItem*>(index.internalPointer()); | ||
_model->addItem(parent, new TreeItem(data)); | ||
} | ||
|
||
void TreeManipulator::removeItem(const QModelIndex& index) | ||
{ | ||
if(!index.isValid()){ | ||
return; | ||
} | ||
|
||
auto item = static_cast<TreeItem*>(index.internalPointer()); | ||
_model->removeItem(item); | ||
} | ||
|
||
void TreeManipulator::editItem(const QModelIndex& index, const QString& data) | ||
{ | ||
if(!index.isValid()){ | ||
return; | ||
} | ||
|
||
_model->setData(index, data); | ||
} | ||
|
||
void TreeManipulator::reset() | ||
{ | ||
_model->clear(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef TREE_MANIPULATOR_H | ||
#define TREE_MANIPULATOR_H | ||
|
||
#include <QObject> | ||
#include <QVariant> | ||
|
||
class TreeModel; | ||
|
||
/*! | ||
* Expose manipulation feature of the Tree Model to QML | ||
*/ | ||
class TreeManipulator : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit TreeManipulator(TreeModel& model, QObject* parent = nullptr); | ||
|
||
Q_INVOKABLE QVariant sourceModel() const; | ||
Q_INVOKABLE void addTopLevelItem(const QString& data); | ||
Q_INVOKABLE void addItem(const QModelIndex& index, const QString& data); | ||
Q_INVOKABLE void removeItem(const QModelIndex& index); | ||
Q_INVOKABLE void editItem(const QModelIndex& index, const QString& data); | ||
Q_INVOKABLE void reset(); | ||
|
||
private: | ||
TreeModel* _model; | ||
}; | ||
|
||
#endif // TREE_MANIPULATOR_H |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.