150 lines
4.6 KiB
C++
150 lines
4.6 KiB
C++
#ifndef CMAKEUSERPRESETS_JSON_TREE_TABLE_H
|
|
#define CMAKEUSERPRESETS_JSON_TREE_TABLE_H
|
|
#include <QApplication>
|
|
#include <QLineEdit>
|
|
#include <QStyledItemDelegate>
|
|
#include <QTreeWidget>
|
|
#include <QTreeWidgetItem>
|
|
#include <QVBoxLayout>
|
|
#include <QWidget>
|
|
#include "ScrollBar.h"
|
|
struct TreeNode {
|
|
QStringList data; // 节点数据,每个字段一个字符串
|
|
QList<TreeNode*> children; // 子节点列表
|
|
TreeNode* parent; // 父节点指针
|
|
|
|
TreeNode(const QStringList& data, TreeNode* parent = nullptr)
|
|
: data(data), parent(parent) {}
|
|
~TreeNode() {
|
|
qDeleteAll(children);
|
|
}
|
|
};
|
|
|
|
|
|
class TreeModel : public QAbstractItemModel {
|
|
public:
|
|
TreeModel(const QStringList& headers, QObject* parent = nullptr)
|
|
: QAbstractItemModel(parent), headers(headers) {
|
|
rootItem = new TreeNode(headers);
|
|
}
|
|
|
|
~TreeModel() {
|
|
delete rootItem;
|
|
}
|
|
|
|
// 必要的虚函数实现
|
|
int rowCount(const QModelIndex& parent = QModelIndex()) const override {
|
|
TreeNode* parentNode = nodeFromIndex(parent);
|
|
return parentNode->children.count();
|
|
}
|
|
|
|
int columnCount(const QModelIndex& parent = QModelIndex()) const override {
|
|
return headers.count();
|
|
}
|
|
|
|
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override {
|
|
if (!index.isValid() || role != Qt::DisplayRole)
|
|
return QVariant();
|
|
|
|
TreeNode* item = nodeFromIndex(index);
|
|
return item->data.value(index.column());
|
|
}
|
|
|
|
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override {
|
|
if (!hasIndex(row, column, parent))
|
|
return QModelIndex();
|
|
|
|
TreeNode* parentNode = nodeFromIndex(parent);
|
|
TreeNode* childItem = parentNode->children.value(row);
|
|
if (childItem)
|
|
return createIndex(row, column, childItem);
|
|
else
|
|
return QModelIndex();
|
|
}
|
|
|
|
QModelIndex parent(const QModelIndex& index) const override {
|
|
if (!index.isValid())
|
|
return QModelIndex();
|
|
|
|
TreeNode* childItem = nodeFromIndex(index);
|
|
TreeNode* parentItem = childItem->parent;
|
|
|
|
if (parentItem == rootItem || !parentItem)
|
|
return QModelIndex();
|
|
|
|
int row = parentItem->parent->children.indexOf(parentItem);
|
|
return createIndex(row, 0, parentItem);
|
|
}
|
|
|
|
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override {
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
|
return headers.value(section);
|
|
|
|
return QVariant();
|
|
}
|
|
|
|
// 添加节点的函数
|
|
void addNode(const QStringList& data, const QModelIndex& parent = QModelIndex()) {
|
|
TreeNode* parentNode = nodeFromIndex(parent);
|
|
beginInsertRows(parent, parentNode->children.count(), parentNode->children.count());
|
|
TreeNode* newNode = new TreeNode(data, parentNode);
|
|
parentNode->children.append(newNode);
|
|
endInsertRows();
|
|
}
|
|
|
|
private:
|
|
TreeNode* rootItem;
|
|
QStringList headers;
|
|
|
|
TreeNode* nodeFromIndex(const QModelIndex& index) const {
|
|
if (index.isValid())
|
|
return static_cast<TreeNode*>(index.internalPointer());
|
|
else
|
|
return rootItem;
|
|
}
|
|
};
|
|
|
|
|
|
class TreeDelegate : public QStyledItemDelegate {
|
|
public:
|
|
TreeDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {}
|
|
|
|
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option,
|
|
const QModelIndex& index) const override {
|
|
// 根据需要创建编辑器,例如 QLineEdit
|
|
QLineEdit* editor = new QLineEdit(parent);
|
|
return editor;
|
|
}
|
|
|
|
void setEditorData(QWidget* editor, const QModelIndex& index) const override {
|
|
QString value = index.model()->data(index, Qt::EditRole).toString();
|
|
QLineEdit* lineEdit = static_cast<QLineEdit*>(editor);
|
|
lineEdit->setText(value);
|
|
}
|
|
|
|
void setModelData(QWidget* editor, QAbstractItemModel* model,
|
|
const QModelIndex& index) const override {
|
|
QLineEdit* lineEdit = static_cast<QLineEdit*>(editor);
|
|
model->setData(index, lineEdit->text(), Qt::EditRole);
|
|
}
|
|
|
|
void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option,
|
|
const QModelIndex& index) const override {
|
|
editor->setGeometry(option.rect);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
class TreeView : public QTreeView {
|
|
public:
|
|
TreeView(){
|
|
setVerticalScrollBar(new ScrollBar());
|
|
setHorizontalScrollBar(new ScrollBar());
|
|
}
|
|
};
|
|
|
|
#endif
|
|
|