forked from e8tools/tool1cd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_data_window.cpp
More file actions
258 lines (215 loc) · 7.31 KB
/
Copy pathtable_data_window.cpp
File metadata and controls
258 lines (215 loc) · 7.31 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
GTool1CD provides GUI front end to Tool1CD library
Copyright © 2009-2017 awa
Copyright © 2017-2018 E8 Tools contributors
This file is part of GTool1CD.
GTool1CD is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GTool1CD is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GTool1CD. If not, see <http://www.gnu.org/licenses/>.
*/
#include "table_data_window.h"
#include "ui_table_data_window.h"
#include "table_fields_window.h"
#include <Table.h>
#include "skobkatextwindow.h"
#include "models/table_data_model.h"
#include "export_table_to_xml_dialog.h"
#include <QDebug>
#include <QItemSelection>
#include "models/skobka_tree_model.h"
#include <QFileDialog>
#include <QMenu>
#include <QMessageBox>
#include "models/v8catalog_tree_model.h"
QString index_presentation(Index *index)
{
QString presentation = QString::fromStdString(index->get_name());
presentation += QString(": ");
for (int i = 0; i < index->get_num_records(); i++) {
if (i != 0) {
presentation += QString(", ");
}
presentation += QString::fromStdString(index->get_records()[i].field->get_name());
}
return presentation;
}
TableDataWindow::TableDataWindow(QWidget *parent, Table *table)
: QMainWindow(parent), table(table),
ui(new Ui::TableDataWindow),
tableWindow(nullptr)
{
ui->setupUi(this);
setWindowTitle(QString::fromStdString(table->get_name()));
ui->dataView->setModel(new TableDataModel(table));
QVector<Index*> indexes;
indexes.push_back(nullptr); // PK
for (int i = 0; i < table->get_num_indexes(); i++) {
Index *index = table->get_index(i);
if (index->is_primary() && indexes[0] == nullptr) {
indexes[0] = index;
} else {
indexes.push_back(index);
}
}
ui->indexChooseBox->addItem(tr("<без индекса>"));
for (Index *index : indexes) {
if (index == nullptr) {
continue;
}
ui->indexChooseBox->addItem(index_presentation(index), QString::fromStdString(index->get_name()));
}
connect(ui->dataView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SLOT(dataView_selection_changed(QItemSelection)));
if (indexes.size() > 1) {
ui->indexChooseBox->setCurrentIndex(1);
emit ui->indexChooseBox->activated(1);
}
ui->dataView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->dataView, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(show_data_context_menu(QPoint)));
ui->dataView->setFocus();
}
TableDataWindow::~TableDataWindow()
{
}
void TableDataWindow::on_descriptionButton_clicked()
{
SkobkaTextWindow *w = new SkobkaTextWindow(this);
w->setText(QString::fromStdString(table->get_description()),
QString::fromStdString(table->get_name()));
w->show();
}
void TableDataWindow::on_fieldsButton_clicked()
{
if (tableWindow == nullptr) {
tableWindow = new TableFieldsWindow(this, table);
}
tableWindow->show();
tableWindow->activateWindow();
}
void TableDataWindow::on_exportToXmlButton_clicked()
{
ExportTableToXmlDialog *export_dialog = new ExportTableToXmlDialog(table, this);
export_dialog->show_with_file_dialog();
}
void TableDataWindow::on_indexChooseBox_activated(int index)
{
if (index == 0) {
// без индекса
ui->dataView->setModel(new TableDataModel(table));
return;
}
QString index_name = ui->indexChooseBox->itemData(index).toString();
Index *table_index = table->get_index(index_name.toStdString());
ui->dataView->setModel(new TableDataModel(table, table_index));
connect(ui->dataView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SLOT(dataView_selection_changed(QItemSelection)));
}
void TableDataWindow::on_dataView_activated(const QModelIndex &index)
{
}
std::unique_ptr<Tree> try_parse_tree(const QVariant &data)
{
std::string string_data = data.toString().toStdString();
if (string_data.substr(0, 1) != "{") {
return nullptr;
}
try {
auto data_tree = parse_1Ctext(string_data, "");
return data_tree;
} catch (...) {
return nullptr;
}
}
void TableDataWindow::dataView_selection_changed(const QItemSelection &selection)
{
if (selection.empty()) {
return;
}
auto range = selection.at(0);
auto column = range.left();
auto row = range.top();
auto index = ui->dataView->model()->index(row, column);
auto model = static_cast<TableDataModel*>(ui->dataView->model());
auto data = ui->dataView->model()->data(index, Qt::EditRole);
if (model->isBlobValue(index)) {
QString catalog_name("{catalog}");
if (table->find_field("FILENAME") != nullptr) {
auto rec = model->getRecord(index);
catalog_name = QString::fromStdString(rec->get_string("FILENAME"));
} else if (table->find_field("EXTNAME") != nullptr) {
auto rec = model->getRecord(index);
catalog_name = QString::fromStdString(rec->get_string("EXTNAME"));
}
auto stream = model->getBlobStream(index);
if (stream != nullptr) {
ui->dataWidget->setStream(stream, catalog_name);
return;
}
}
ui->dataWidget->setText(data.toString());
}
void TableDataWindow::on_saveBlobButton_clicked()
{
auto index = ui->dataView->currentIndex();
if (!index.isValid()) {
return;
}
QString filename = QFileDialog::getSaveFileName(this, tr("Сохранение..."), "");
if (filename.isNull()) {
return;
}
auto model = static_cast<TableDataModel*>(ui->dataView->model());
model->dumpBlob(index, filename);
}
void TableDataWindow::reload()
{
on_indexChooseBox_activated(ui->indexChooseBox->currentIndex());
}
void TableDataWindow::show_data_context_menu(const QPoint &pos)
{
if (!ui->dataView->currentIndex().isValid()) {
return;
}
QMenu menu(this);
QAction action_delete(tr("Удалить запись"), this);
connect(&action_delete, SIGNAL(triggered()), this, SLOT(delete_record_action()));
menu.addAction(&action_delete);
menu.exec(ui->dataView->mapToGlobal(pos));
}
void TableDataWindow::delete_record_action()
{
auto index = ui->dataView->currentIndex();
if (!index.isValid()) {
return;
}
auto *model = static_cast<TableDataModel*>(ui->dataView->model());
uint32_t phys_numrecord = model->physicalRecordNo(index);
if (table->get_base()->get_readonly()) {
QMessageBox::warning(this, tr("Удаление записи"),
tr("База открыта только для чтения — удаление невозможно."));
return;
}
if (QMessageBox::warning(this, tr("Удаление записи"),
tr("Пометить запись №%1 таблицы «%2» удалённой?\nДействие изменяет базу.")
.arg(phys_numrecord).arg(QString::fromStdString(table->get_name())),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes) {
return;
}
try {
table->begin_edit();
table->mark_record_removed(phys_numrecord);
table->get_base()->flush();
} catch (DetailedException &ex) {
QMessageBox::critical(this, tr("Удаление записи"), QString(ex.what()));
return;
}
model->notifyRowChanged(index.row());
}