|
31 | 31 | #include "configurations_window.h" |
32 | 32 | #include "about_dialog.h" |
33 | 33 | #include <QSortFilterProxyModel> |
| 34 | +#include <QMenu> |
| 35 | +#include <QMessageBox> |
34 | 36 |
|
35 | 37 | void MainWindow::AddDetailedMessage( |
36 | 38 | const std::string &description, |
@@ -70,7 +72,13 @@ void MainWindow::show_table_context_menu(const QPoint &pos) |
70 | 72 | QAction action_import_blob(tr("Импорт BLOB"), this); |
71 | 73 | connect(&action_import_blob, SIGNAL(triggered()), this, SLOT(import_blob_file())); |
72 | 74 | contextMenu.addAction(&action_import_blob); |
73 | | - |
| 75 | + |
| 76 | + contextMenu.addSeparator(); |
| 77 | + |
| 78 | + QAction action_clear_table(tr("Очистить таблицу"), this); |
| 79 | + connect(&action_clear_table, SIGNAL(triggered()), this, SLOT(clear_table_action())); |
| 80 | + contextMenu.addAction(&action_clear_table); |
| 81 | + |
74 | 82 | contextMenu.exec(mapToGlobal(pos)); |
75 | 83 | } |
76 | 84 |
|
@@ -109,6 +117,58 @@ void MainWindow::import_blob_file() |
109 | 117 | } |
110 | 118 | } |
111 | 119 |
|
| 120 | +void MainWindow::clear_table_action() |
| 121 | +{ |
| 122 | + auto indexes = ui->tableListView->selectionModel()->selectedIndexes(); |
| 123 | + if (indexes.empty()) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + auto *proxy = qobject_cast<QSortFilterProxyModel*>(ui->tableListView->model()); |
| 128 | + QModelIndex src = proxy ? proxy->mapToSource(indexes.first()) : indexes.first(); |
| 129 | + Table *t = db->get_table(src.row()); |
| 130 | + |
| 131 | + if (db->get_readonly()) { |
| 132 | + QMessageBox::warning(this, tr("Очистка таблицы"), |
| 133 | + tr("База открыта только для чтения — очистка невозможна.")); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + if (QMessageBox::warning(this, tr("Очистка таблицы"), |
| 138 | + tr("Пометить удалёнными ВСЕ записи таблицы «%1»?\nДействие изменяет базу и необратимо.") |
| 139 | + .arg(QString::fromStdString(t->get_name())), |
| 140 | + QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + uint32_t deleted = 0; |
| 145 | + try { |
| 146 | + t->begin_edit(); |
| 147 | + uint32_t total = t->get_phys_numrecords(); |
| 148 | + for (uint32_t i = 1; i < total; i++) { |
| 149 | + auto *rec = t->get_record(i); |
| 150 | + bool removed = rec->is_removed(); |
| 151 | + delete rec; |
| 152 | + if (!removed) { |
| 153 | + t->mark_record_removed(i); |
| 154 | + deleted++; |
| 155 | + } |
| 156 | + } |
| 157 | + db->flush(); |
| 158 | + } catch (DetailedException &ex) { |
| 159 | + QMessageBox::critical(this, tr("Очистка таблицы"), QString(ex.what())); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + auto it = table_windows.find(t); |
| 164 | + if (it != table_windows.end()) { |
| 165 | + static_cast<TableDataWindow*>(it.value())->reload(); |
| 166 | + } |
| 167 | + |
| 168 | + QMessageBox::information(this, tr("Очистка таблицы"), |
| 169 | + tr("Готово. Помечено удалёнными записей: %1.").arg(deleted)); |
| 170 | +} |
| 171 | + |
112 | 172 | MainWindow::~MainWindow() |
113 | 173 | { |
114 | 174 | delete ui; |
|
0 commit comments