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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
#include "populatedialog.h"
#include "ui_populatedialog.h"
#include "dblistmodel.h"
#include "dbobjlistmodel.h"
#include "services/dbmanager.h"
#include "schemaresolver.h"
#include "services/pluginmanager.h"
#include "plugins/populateplugin.h"
#include "populateconfigdialog.h"
#include "uiutils.h"
#include "services/populatemanager.h"
#include "common/widgetcover.h"
#include "common/compatibility.h"
#include <QPushButton>
#include <QGridLayout>
#include <QCheckBox>
#include <QToolButton>
#include <QDebug>
#include <QSignalMapper>
PopulateDialog::PopulateDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::PopulateDialog)
{
init();
}
PopulateDialog::~PopulateDialog()
{
delete ui;
}
void PopulateDialog::setDbAndTable(Db* db, const QString& table)
{
QString oldTable = ui->tableCombo->currentText();
ui->databaseCombo->setCurrentText(db->getName());
ui->tableCombo->setCurrentText(table);
// #4177
if (oldTable == table)
refreshColumns();
}
void PopulateDialog::init()
{
ui->setupUi(this);
limitDialogWidth(this);
ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Populate", "populate dialog button"));
plugins = PLUGINS->getLoadedPlugins<PopulatePlugin>();
sSort(plugins, [](PopulatePlugin* p1, PopulatePlugin* p2) -> bool
{
return p1->getTitle().compare(p2->getTitle()) < 0;
});
for (PopulatePlugin*& plugin : plugins)
{
pluginByName[plugin->getName()] = plugin;
pluginTitles << plugin->getTitle();
}
widgetCover = new WidgetCover(this);
widgetCover->initWithInterruptContainer(tr("Abort"));
widgetCover->setVisible(false);
connect(widgetCover, SIGNAL(cancelClicked()), POPULATE_MANAGER, SLOT(interrupt()));
ui->scrollArea->setAutoFillBackground(false);
ui->scrollArea->viewport()->setAutoFillBackground(false);
ui->columnsWidget->setAutoFillBackground(false);
dbListModel = new DbListModel(this);
dbListModel->setCombo(ui->databaseCombo);
dbListModel->setSortMode(DbListModel::SortMode::Alphabetical);
ui->databaseCombo->setModel(dbListModel);
tablesModel = new DbObjListModel(this);
tablesModel->setIncludeSystemObjects(false);
tablesModel->setType(DbObjListModel::ObjectType::TABLE);
ui->tableCombo->setModel(tablesModel);
refreshTables();
connect(ui->databaseCombo, SIGNAL(currentTextChanged(QString)), this, SLOT(refreshTables()));
connect(ui->tableCombo, SIGNAL(currentTextChanged(QString)), this, SLOT(refreshColumns()));
connect(POPULATE_MANAGER, SIGNAL(populatingFinished()), widgetCover, SLOT(hide()));
connect(POPULATE_MANAGER, SIGNAL(finishedStep(int)), widgetCover, SLOT(setProgress(int)));
connect(POPULATE_MANAGER, SIGNAL(populatingSuccessful()), this, SLOT(finished()));
}
PopulateEngine* PopulateDialog::getEngine(int selectedPluginIndex)
{
if (selectedPluginIndex < 0 || selectedPluginIndex >= plugins.size())
{
qCritical() << "Selected populate plugin out of range!";
return nullptr;
}
return plugins[selectedPluginIndex]->createEngine();
}
void PopulateDialog::deleteEngines(const QList<PopulateEngine*>& engines)
{
for (PopulateEngine* engine : engines)
delete engine;
}
void PopulateDialog::rebuildEngines(const QHash<QString, QPair<QString, QVariant>>& columnConfig)
{
int row = 0;
QVariant config;
QString pluginName;
for (ColumnEntry& entry : columnEntries)
{
pluginName.clear();
if (columnConfig.contains(entry.column))
pluginName = columnConfig[entry.column].first;
if (pluginName.isNull())
{
pluginName = plugins[entry.combo->currentIndex()]->getName();
config = CFG->getPopulateHistory(pluginName);
}
else
{
entry.combo->setCurrentIndex(plugins.indexOf(pluginByName[pluginName]));
config = columnConfig[entry.column].second;
}
pluginSelected(entry.combo, entry.combo->currentIndex(), config);
updateColumnState(row++, false);
}
}
void PopulateDialog::refreshTables()
{
db = DBLIST->getByName(ui->databaseCombo->currentText());
if (db)
tablesModel->setDb(db);
updateState();
}
void PopulateDialog::refreshColumns()
{
for (ColumnEntry& entry : columnEntries)
{
delete entry.check;
delete entry.combo;
delete entry.button;
}
columnEntries.clear();
safe_delete(buttonMapper);
safe_delete(checkMapper);
delete ui->columnsLayout;
ui->columnsLayout = new QGridLayout();
ui->columnsWidget->setLayout(ui->columnsLayout);
if (!db)
{
qCritical() << "No Db while refreshing columns in PopulateDialog!";
return;
}
QString table = ui->tableCombo->currentText();
buttonMapper = new QSignalMapper(this);
connect(buttonMapper, SIGNAL(mapped(int)), this, SLOT(configurePlugin(int)));
checkMapper = new QSignalMapper(this);
connect(checkMapper, SIGNAL(mapped(int)), this, SLOT(updateColumnState(int)));
SchemaResolver resolver(db);
QStringList columns = resolver.getTableColumns(table);
QCheckBox* check = nullptr;
QComboBox* combo = nullptr;
QToolButton* btn = nullptr;
int rows = -1;
QHash<QString, QPair<QString, QVariant>> columnConfig = CFG->getPopulateHistory(db->getName(), table, rows);
if (rows > -1)
ui->rowsSpin->setValue(rows);
int row = 0;
for (const QString& column : columns)
{
check = new QCheckBox(column);
if (columnConfig.contains(column))
check->setChecked(true);
connect(check, SIGNAL(toggled(bool)), checkMapper, SLOT(map()));
checkMapper->setMapping(check, row);
combo = new QComboBox();
combo->addItems(pluginTitles);
connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(pluginSelected(int)));
btn = new QToolButton();
btn->setText(tr("Configure"));
connect(btn, SIGNAL(clicked()), buttonMapper, SLOT(map()));
buttonMapper->setMapping(btn, row);
ui->columnsLayout->addWidget(check, row, 0);
ui->columnsLayout->addWidget(combo, row, 1);
ui->columnsLayout->addWidget(btn, row, 2);
columnEntries << ColumnEntry(column, check, combo, btn);
row++;
}
rebuildEngines(columnConfig);
QSpacerItem* spacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
ui->columnsLayout->addItem(spacer, row, 0, 1, 3);
updateState();
}
void PopulateDialog::pluginSelected(int index)
{
QVariant config;
if (index >= 0 && index < plugins.size())
config = CFG->getPopulateHistory(plugins[index]->getName());
QComboBox* cb = dynamic_cast<QComboBox*>(sender());
pluginSelected(cb, index, config);
}
void PopulateDialog::pluginSelected(QComboBox* combo, int index, const QVariant& config)
{
if (!combo)
return;
ColumnEntry* entry = nullptr;
int columnIndex = 0;
for (ColumnEntry& e : columnEntries)
{
if (e.combo == combo)
{
entry = &e;
break;
}
columnIndex++;
}
if (!entry)
return;
safe_delete(entry->engine);
if (index < 0 || index >= plugins.size())
return;
entry->plugin = plugins[index];
entry->engine = entry->plugin->createEngine();
if (config.isValid())
entry->engine->getConfig()->setValuesFromQVariant(config);
updateColumnState(columnIndex);
}
void PopulateDialog::configurePlugin(int index)
{
if (index < 0 || index >= columnEntries.size())
{
qCritical() << "Plugin configure index out of range:" << index << "," << columnEntries.size();
return;
}
PopulateEngine* engine = columnEntries[index].engine;
if (!engine->getConfig())
{
qWarning() << "Called config on populate plugin, but it has no CfgMain.";
return;
}
engine->getConfig()->savepoint();
QString colName = columnEntries[index].column;
PopulateConfigDialog dialog(engine, colName, columnEntries[index].combo->currentText(), this);
if (dialog.exec() != QDialog::Accepted)
engine->getConfig()->restore();
engine->getConfig()->release();
updateColumnState(index);
}
void PopulateDialog::updateColumnState(int index, bool updateGlobalState)
{
if (index < 0 || index >= columnEntries.size())
{
qCritical() << "Column update called but index out of range:" << index << "," << columnEntries.size();
return;
}
bool checked = columnEntries[index].check->isChecked();
bool hasConfig = columnEntries[index].engine->getConfig() != nullptr;
columnEntries[index].combo->setEnabled(checked);
columnEntries[index].button->setEnabled(checked && hasConfig);
bool valid = true;
if (checked && hasConfig)
{
valid = columnEntries[index].engine->validateOptions();
setValidState(columnEntries[index].button, valid, tr("Populating configuration for this column is invalid or incomplete."));
}
if (valid == columnsValid.contains(index)) // if state changed
{
if (!valid)
columnsValid[index] = false;
else
columnsValid.remove(index);
}
if (updateGlobalState)
updateState();
}
void PopulateDialog::updateState()
{
bool columnsOk = columnsValid.size() == 0;
bool dbOk = !ui->databaseCombo->currentText().isNull();
bool tableOk = !ui->tableCombo->currentText().isNull();
bool colCountOk = false;
for (ColumnEntry& entry : columnEntries)
{
if (entry.check->isChecked())
{
colCountOk = true;
break;
}
}
setValidState(ui->databaseCombo, dbOk, tr("Select database with table to populate"));
setValidState(ui->tableCombo, tableOk, tr("Select table to populate"));
setValidState(ui->columnsGroup, (!tableOk || colCountOk), tr("You have to select at least one column."));
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(columnsOk && tableOk && colCountOk);
}
void PopulateDialog::finished()
{
QDialog::accept();
}
void PopulateDialog::accept()
{
if (!db)
return;
QHash<QString, QPair<QString, QVariant>> configForHistory;
QHash<QString,PopulateEngine*> engines;
for (ColumnEntry& entry : columnEntries)
{
if (!entry.check->isChecked())
continue;
if (!entry.engine)
return;
engines[entry.column] = entry.engine;
// entry.engine = nullptr; // to avoid deleting it in the entry's destructor - worker will delete it after it's done
configForHistory[entry.column] = QPair<QString, QVariant>(entry.plugin->getName(), entry.engine->getConfig()->toQVariant());
}
QString table = ui->tableCombo->currentText();
int rows = ui->rowsSpin->value();
started = true;
widgetCover->displayProgress(rows, "%v / %m");
widgetCover->show();
CFG->addPopulateHistory(db->getName(), table, rows, configForHistory);
POPULATE_MANAGER->populate(db, table, engines, rows);
}
void PopulateDialog::reject()
{
if (started)
POPULATE_MANAGER->interrupt();
QDialog::reject();
}
PopulateDialog::ColumnEntry::ColumnEntry(const QString& column, QCheckBox* check, QComboBox* combo, QToolButton* button) :
column(column), check(check), combo(combo), button(button)
{
}
PopulateDialog::ColumnEntry::~ColumnEntry()
{
safe_delete(engine);
}
|