-
Notifications
You must be signed in to change notification settings - Fork 363
/
mainwindow.cpp
133 lines (116 loc) · 4.48 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QFile>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
jsonModel = new JsonTreeModel(this);
ui->treeView->setModel(jsonModel);
initLoadDump();
initEdit();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initLoadDump()
{
//选择导入的文件路径
connect(ui->btnLoadPath,&QPushButton::clicked,this,[this](){
const QString jsonpath = QFileDialog::getOpenFileName(this,"File Path");
if(jsonpath.isEmpty()) return;
ui->editLoadPath->setText(jsonpath);
});
//导入Json文件
connect(ui->btnLoadJson,&QPushButton::clicked,this,[this](){
const QString loadpath = ui->editLoadPath->text();
if(loadpath.isEmpty()) return;
//parseJson.loadJson(loadpath);
jsonModel->loadJson(loadpath);
ui->treeView->expandAll();
});
//选择导出的文件路径
connect(ui->btnDumpPath,&QPushButton::clicked,this,[this](){
const QString jsonpath = QFileDialog::getSaveFileName(this,"Save Path");
if(jsonpath.isEmpty()) return;
ui->editDumpPath->setText(jsonpath);
});
//导出Json文件
connect(ui->btnDumpJson,&QPushButton::clicked,this,[this](){
const QString dumppath = ui->editDumpPath->text();
if(dumppath.isEmpty()) return;
//parseJson.dumpJson(dumppath);
jsonModel->dumpJson(dumppath);
});
}
void MainWindow::initEdit()
{
//增删部分参照示例:editable tree
//
//我的item默认为enum:none,但目前还没有delegate来自定义操作
//只在item的appendchilren里测试性的改成了enum:value
//添加节点
connect(ui->btnInsert,&QPushButton::clicked,this,[this](){
QModelIndex index = ui->treeView->selectionModel()->currentIndex();
if(!index.isValid())
return;
QAbstractItemModel *model = ui->treeView->model();
if(!model->insertRow(index.row()+1,index.parent()))
return;
updateIndex();
//修改insert的内容
/*for (int column = 0; column < model->columnCount(index.parent()); ++column) {
QModelIndex child = model->index(index.row()+1, column, index.parent());
model->setData(child, QVariant("[No data]"), Qt::EditRole);
}*/
});
//添加子节点
connect(ui->btnInsertChild,&QPushButton::clicked,this,[this](){
QModelIndex index = ui->treeView->selectionModel()->currentIndex();
QAbstractItemModel *model = ui->treeView->model();
if (model->columnCount(index) == 0) {
if (!model->insertColumn(0, index))
return;
}
if (!model->insertRow(0, index))
return;
//修改insert的内容
/*for (int column = 0; column < model->columnCount(index); ++column) {
QModelIndex child = model->index(0, column, index);
model->setData(child, QVariant("[No data]"), Qt::EditRole);
}*/
ui->treeView->selectionModel()->setCurrentIndex(model->index(0, 0, index),
QItemSelectionModel::ClearAndSelect);
updateIndex();
});
//删除节点树
connect(ui->btnRemove,&QPushButton::clicked,this,[this](){
QModelIndex index = ui->treeView->selectionModel()->currentIndex();
QAbstractItemModel *model = ui->treeView->model();
if (model->removeRow(index.row(), index.parent()))
updateIndex();
});
}
void MainWindow::updateIndex()
{
//抄的示例
//bool hasSelection = !ui->treeView->selectionModel()->selection().isEmpty();
//removeRowAction->setEnabled(hasSelection);
//removeColumnAction->setEnabled(hasSelection);
bool hasCurrent = ui->treeView->selectionModel()->currentIndex().isValid();
//insertRowAction->setEnabled(hasCurrent);
//insertColumnAction->setEnabled(hasCurrent);
if (hasCurrent) {
ui->treeView->closePersistentEditor(ui->treeView->selectionModel()->currentIndex());
int row = ui->treeView->selectionModel()->currentIndex().row();
int column = ui->treeView->selectionModel()->currentIndex().column();
if (ui->treeView->selectionModel()->currentIndex().parent().isValid())
qDebug()<<tr("Position: (%1,%2)").arg(row).arg(column);
else
qDebug()<<tr("Position: (%1,%2) in top level").arg(row).arg(column);
}
}