-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainwindow.cpp
166 lines (132 loc) · 6.03 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include "treeitem.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
insertRowAction = new QAction(this);
insertRowAction->setObjectName(QString::fromUtf8("insertRowAction"));
removeRowAction = new QAction(this);
removeRowAction->setObjectName(QString::fromUtf8("removeRowAction"));
insertColumnAction = new QAction(this);
insertColumnAction->setObjectName(QString::fromUtf8("insertColumnAction"));
removeColumnAction = new QAction(this);
removeColumnAction->setObjectName(QString::fromUtf8("removeColumnAction"));
insertChildAction = new QAction(this);
insertChildAction->setObjectName(QString::fromUtf8("insertChildAction"));
insertRowAction->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+I, R", nullptr));
removeRowAction->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+R, R", nullptr));
insertColumnAction->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+I, C", nullptr));
removeColumnAction->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+R, C", nullptr));
insertChildAction->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+N", nullptr));
const QStringList headers({tr("Title"), tr("Description")});
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
model = new TreeModel(headers, file.readAll());
file.close();
ui->viewTx->setModel(model);
for (int column = 0; column < model->columnCount(); ++column)
ui->viewTx->resizeColumnToContents(column);
connect(ui->viewTx->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &MainWindow::updateActions);
connect(insertRowAction, &QAction::triggered, this, &MainWindow::insertRow);
connect(insertColumnAction, &QAction::triggered, this, &MainWindow::insertColumn);
connect(removeRowAction, &QAction::triggered, this, &MainWindow::removeRow);
connect(removeColumnAction, &QAction::triggered, this, &MainWindow::removeColumn);
connect(insertChildAction, &QAction::triggered, this, &MainWindow::insertChild);
lStatusTitle = new QLabel(this);
lStatusTitle->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
lStatusTitle->setText("Last command status:");
lStatusValue = new QLabel(this);
lStatusValue->setPixmap(QPixmap(":/icon_ok.png", "PNG"));
lMsgCntValue = new QLabel(this);
lMsgCntValue->setText("Message count: 0");
ui->statusbar->addPermanentWidget(lStatusTitle);
ui->statusbar->addPermanentWidget(lStatusValue);
ui->statusbar->addWidget(lMsgCntValue);
usbHid = new USBHIDDevice(this, 0000, 0000);
}
void MainWindow::updateActions()
{
const bool hasSelection = !ui->viewTx->selectionModel()->selection().isEmpty();
removeRowAction->setEnabled(hasSelection);
removeColumnAction->setEnabled(hasSelection);
const bool hasCurrent = ui->viewTx->selectionModel()->currentIndex().isValid();
insertRowAction->setEnabled(hasCurrent);
insertColumnAction->setEnabled(hasCurrent);
if (hasCurrent) {
ui->viewTx->closePersistentEditor(ui->viewTx->selectionModel()->currentIndex());
const int row = ui->viewTx->selectionModel()->currentIndex().row();
const int column = ui->viewTx->selectionModel()->currentIndex().column();
if (ui->viewTx->selectionModel()->currentIndex().parent().isValid())
statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column));
else
statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column));
}
}
void MainWindow::insertChild()
{
const QModelIndex index = ui->viewTx->selectionModel()->currentIndex();
QAbstractItemModel *model = ui->viewTx->model();
if (model->columnCount(index) == 0) {
if (!model->insertColumn(0, index))
return;
}
if (!model->insertRow(0, index))
return;
for (int column = 0; column < model->columnCount(index); ++column) {
const QModelIndex child = model->index(0, column, index);
model->setData(child, QVariant(tr("[No data]")), Qt::EditRole);
if (!model->headerData(column, Qt::Horizontal).isValid())
model->setHeaderData(column, Qt::Horizontal, QVariant(tr("[No header]")), Qt::EditRole);
}
ui->viewTx->selectionModel()->setCurrentIndex(model->index(0, 0, index),
QItemSelectionModel::ClearAndSelect);
updateActions();
}
bool MainWindow::insertColumn()
{
QAbstractItemModel *model = ui->viewTx->model();
int column = ui->viewTx->selectionModel()->currentIndex().column();
// Insert a column in the parent item.
bool changed = model->insertColumn(column + 1);
if (changed)
model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"), Qt::EditRole);
updateActions();
return changed;
}
void MainWindow::insertRow()
{
const QModelIndex index = ui->viewTx->selectionModel()->currentIndex();
QAbstractItemModel *model = ui->viewTx->model();
if (!model->insertRow(index.row()+1, index.parent()))
return;
updateActions();
for (int column = 0; column < model->columnCount(index.parent()); ++column) {
const QModelIndex child = model->index(index.row() + 1, column, index.parent());
model->setData(child, QVariant(tr("[No data]")), Qt::EditRole);
}
}
bool MainWindow::removeColumn()
{
QAbstractItemModel *model = ui->viewTx->model();
const int column = ui->viewTx->selectionModel()->currentIndex().column();
// Insert columns in each child of the parent item.
const bool changed = model->removeColumn(column);
if (changed)
updateActions();
return changed;
}
void MainWindow::removeRow()
{
const QModelIndex index = ui->viewTx->selectionModel()->currentIndex();
QAbstractItemModel *model = ui->viewTx->model();
if (model->removeRow(index.row(), index.parent()))
updateActions();
}
MainWindow::~MainWindow()
{
delete ui;
}