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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
|
#include "exportdialog.h"
#include "ui_exportdialog.h"
#include "dblistmodel.h"
#include "dbobjlistmodel.h"
#include "services/dbmanager.h"
#include "uiutils.h"
#include "services/pluginmanager.h"
#include "formmanager.h"
#include "plugins/exportplugin.h"
#include "configmapper.h"
#include "selectabledbobjmodel.h"
#include "dbtree/dbtree.h"
#include "dbtree/dbtreemodel.h"
#include "schemaresolver.h"
#include "common/widgetcover.h"
#include "services/notifymanager.h"
#include "themetuner.h"
#include "uiconfig.h"
#include "common/dialogsizehandler.h"
#include <QClipboard>
#include <QDebug>
#include <QDir>
#include <QFileDialog>
#include <QTextCodec>
#include <QUiLoader>
#include <QMimeData>
static const QString EXPORT_DIALOG_CFG_GROUP = "ExportDialog";
static const QString EXPORT_DIALOG_CFG_CODEC = "codec";
static const QString EXPORT_DIALOG_CFG_FILE = "outputFileName";
static const QString EXPORT_DIALOG_CFG_CLIP = "intoClipboard";
static const QString EXPORT_DIALOG_CFG_DATA = "exportData";
static const QString EXPORT_DIALOG_CFG_IDX = "exportTableIndexes";
static const QString EXPORT_DIALOG_CFG_TRIG = "exportTableTriggers";
static const QString EXPORT_DIALOG_CFG_FORMAT = "format";
ExportDialog::ExportDialog(QWidget *parent) :
QWizard(parent),
ui(new Ui::ExportDialog)
{
init();
}
ExportDialog::~ExportDialog()
{
EXPORT_MANAGER->interrupt();
safe_delete(configMapper);
delete ui;
}
void ExportDialog::init()
{
ui->setupUi(this);
THEME_TUNER->darkThemeFix(this);
limitDialogWidth(this);
DialogSizeHandler::applyFor(this);
#ifdef Q_OS_MACX
resize(width() + 150, height());
setPixmap(QWizard::BackgroundPixmap, addOpacity(ICONS.DATABASE_EXPORT_WIZARD.toQIcon().pixmap(800, 800), 0.3));
#endif
widgetCover = new WidgetCover(this);
widgetCover->initWithInterruptContainer(tr("Cancel"));
connect(widgetCover, SIGNAL(cancelClicked()), EXPORT_MANAGER, SLOT(interrupt()));
connect(EXPORT_MANAGER, SIGNAL(finishedStep(int)), widgetCover, SLOT(setProgress(int)));
widgetCover->setVisible(false);
widgetCover->displayProgress(0, "%v");
initPageOrder();
initModePage();
initTablePage();
initFormatPage();
initQueryPage();
initDbObjectsPage();
connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(pageChanged(int)));
connect(EXPORT_MANAGER, SIGNAL(exportSuccessful()), this, SLOT(success()));
connect(EXPORT_MANAGER, SIGNAL(exportFinished()), this, SLOT(hideCoverWidget()));
connect(EXPORT_MANAGER, SIGNAL(storeInClipboard(QByteArray, QString)), this, SLOT(storeInClipboard(QByteArray, QString)));
connect(EXPORT_MANAGER, SIGNAL(storeInClipboard(QString)), this, SLOT(storeInClipboard(QString)));
connect(EXPORT_MANAGER, SIGNAL(validationResultFromPlugin(bool,CfgEntry*,QString)), this, SLOT(handleValidationResultFromPlugin(bool,CfgEntry*,QString)));
connect(EXPORT_MANAGER, SIGNAL(stateUpdateRequestFromPlugin(CfgEntry*,bool,bool)), this, SLOT(stateUpdateRequestFromPlugin(CfgEntry*,bool,bool)));
}
void ExportDialog::setTableMode(Db* db, const QString& table)
{
if (!db->isOpen())
{
qWarning() << "Cannot export from closed database.";
return;
}
setStartId(pageId(ui->tablePage));
exportMode = ExportManager::TABLE;
this->db = db;
this->table = table;
ui->exportTableDbNameCombo->addItem(db->getName());
ui->exportTableDbNameCombo->setCurrentText(db->getName());
ui->exportTableDbNameCombo->setEnabled(false);
ui->exportTableNameCombo->addItem(table);
ui->exportTableNameCombo->setCurrentText(table);
ui->exportTableNameCombo->setEnabled(false);
}
void ExportDialog::setQueryMode(Db* db, const QString& query)
{
if (!db->isOpen())
{
qWarning() << "Cannot export from closed database.";
return;
}
setStartId(pageId(ui->queryPage));
exportMode = ExportManager::QUERY_RESULTS;
this->db = db;
this->query = query;
ui->queryDatabaseCombo->addItem(db->getName());
ui->queryDatabaseCombo->setCurrentText(db->getName());
ui->queryDatabaseCombo->setEnabled(false);
ui->queryEdit->setPlainText(query);
updateQueryEditDb();
ui->queryEdit->checkSyntaxNow();
}
void ExportDialog::setDatabaseMode(Db* db)
{
if (!db->isOpen())
{
qWarning() << "Cannot export from closed database.";
return;
}
setStartId(pageId(ui->databaseObjectsPage));
exportMode = ExportManager::DATABASE;
this->db = db;
}
void ExportDialog::setPreselectedDb(Db *db)
{
if (!db->isOpen())
return;
this->db = db;
}
void ExportDialog::initModePage()
{
connect(ui->subjectDatabaseRadio, SIGNAL(clicked()), this, SLOT(updateExportMode()));
connect(ui->subjectTableRadio, SIGNAL(clicked()), this, SLOT(updateExportMode()));
connect(ui->subjectQueryRadio, SIGNAL(clicked()), this, SLOT(updateExportMode()));
}
void ExportDialog::initTablePage()
{
ui->tablePage->setValidator([=]() -> bool
{
bool dbOk = ui->exportTableDbNameCombo->currentIndex() > -1;
bool tableOk = ui->exportTableNameCombo->currentIndex() > -1;
setValidState(ui->exportTableDbNameCombo, dbOk, tr("Select database to export."));
setValidState(ui->exportTableNameCombo, tableOk, tr("Select table to export."));
return dbOk && tableOk;
});
dbListModel = new DbListModel(this);
dbListModel->setCombo(ui->exportTableDbNameCombo);
dbListModel->setSortMode(DbListModel::SortMode::AlphabeticalCaseInsensitive);
tablesModel = new DbObjListModel(this);
tablesModel->setType(DbObjListModel::ObjectType::TABLE);
tablesModel->setSortMode(DbObjListModel::SortMode::AlphabeticalCaseInsensitive);
connect(this, SIGNAL(tablePageCompleteChanged()), ui->tablePage, SIGNAL(completeChanged()));
}
void ExportDialog::initQueryPage()
{
ui->queryPage->setValidator([=]() -> bool
{
bool queryOk = !ui->queryEdit->toPlainText().trimmed().isEmpty();
queryOk &= ui->queryEdit->isSyntaxChecked() && !ui->queryEdit->haveErrors();
bool dbOk = ui->queryDatabaseCombo->currentIndex() > -1;
setValidState(ui->queryDatabaseCombo, dbOk, tr("Select database to export."));
setValidState(ui->queryEdit, queryOk, tr("Enter valid query to export."));
return dbOk && queryOk;
});
ui->queryEdit->setAlwaysEnforceErrorsChecking(true);
connect(ui->queryEdit, SIGNAL(errorsChecked(bool)), ui->queryPage, SIGNAL(completeChanged()));
connect(ui->queryEdit, SIGNAL(textChanged()), ui->queryPage, SIGNAL(completeChanged()));
connect(ui->queryDatabaseCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateQueryEditDb()));
connect(this, SIGNAL(queryPageCompleteChanged()), ui->queryPage, SIGNAL(completeChanged()));
}
void ExportDialog::initDbObjectsPage()
{
selectableDbListModel = new SelectableDbObjModel(this);
selectableDbListModel->setSourceModel(DBTREE->getModel());
ui->dbObjectsTree->setModel(selectableDbListModel);
ui->databaseObjectsPage->setValidator([=]() -> bool
{
bool dbOk = ui->dbObjectsDatabaseCombo->currentIndex() > -1;
bool listOk = selectableDbListModel->getCheckedObjects().size() > 0;
setValidState(ui->dbObjectsDatabaseCombo, dbOk, tr("Select database to export."));
setValidState(ui->dbObjectsTree, listOk, tr("Select at least one object to export."));
return listOk;
});
connect(ui->dbObjectsDatabaseCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateDbObjTree()));
connect(ui->dbObjectsDatabaseCombo, SIGNAL(currentIndexChanged(QString)), ui->databaseObjectsPage, SIGNAL(completeChanged()));
connect(selectableDbListModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), ui->databaseObjectsPage, SIGNAL(completeChanged()));
connect(ui->objectsSelectAllButton, SIGNAL(clicked()), this, SLOT(dbObjectsSelectAll()));
connect(ui->objectsDeselectAllButton, SIGNAL(clicked()), this, SLOT(dbObjectsDeselectAll()));
}
void ExportDialog::initFormatPage()
{
ui->formatAndOptionsPage->setValidator([=]() -> bool
{
setValidState(ui->exportFileEdit, true);
bool outputFileSupported = currentPlugin && currentPlugin->getSupportedModes().testFlag(ExportManager::FILE);
if (outputFileSupported && ui->exportFileRadio->isChecked())
{
QString path = ui->exportFileEdit->text();
if (path.trimmed().isEmpty())
{
setValidState(ui->exportFileEdit, false, tr("You must provide a file name to export to."));
return false;
}
QDir dir(path);
if (dir.exists() && QFileInfo(path).isDir())
{
setValidState(ui->exportFileEdit, false, tr("Path you provided is an existing directory. You cannot overwrite it."));
return false;
}
if (!dir.cdUp())
{
setValidState(ui->exportFileEdit, false, tr("The directory '%1' does not exist.").arg(dir.dirName()));
return false;
}
QFileInfo fi(path);
if (fi.exists())
setValidStateInfo(ui->exportFileEdit, tr("The file '%1' exists and will be overwritten.").arg(fi.fileName()));
}
return ui->formatCombo->currentIndex() > -1 && ui->encodingCombo->currentIndex() > -1 && isPluginConfigValid();
});
ui->exportFileButton->setIcon(ICONS.EXPORT_FILE_BROWSE);
connect(ui->exportFileButton, SIGNAL(clicked()), this, SLOT(browseForExportFile()));
connect(ui->formatCombo, SIGNAL(currentTextChanged(QString)), this, SLOT(pluginSelected()));
connect(ui->formatCombo, SIGNAL(currentTextChanged(QString)), ui->formatAndOptionsPage, SIGNAL(completeChanged()));
connect(ui->encodingCombo, SIGNAL(currentTextChanged(QString)), ui->formatAndOptionsPage, SIGNAL(completeChanged()));
connect(ui->exportFileEdit, SIGNAL(textChanged(QString)), ui->formatAndOptionsPage, SIGNAL(completeChanged()));
connect(ui->exportFileRadio, SIGNAL(clicked()), ui->formatAndOptionsPage, SIGNAL(completeChanged()));
connect(ui->exportClipboardRadio, SIGNAL(clicked()), ui->formatAndOptionsPage, SIGNAL(completeChanged()));
connect(this, SIGNAL(formatPageCompleteChanged()), ui->formatAndOptionsPage, SIGNAL(completeChanged()));
connect(ui->exportFileRadio, SIGNAL(clicked()), this, SLOT(updateOptions()));
connect(ui->exportClipboardRadio, SIGNAL(clicked()), this, SLOT(updateOptions()));
connect(ui->exportFileRadio, SIGNAL(clicked()), this, SLOT(updateExportOutputOptions()));
connect(ui->exportClipboardRadio, SIGNAL(clicked()), this, SLOT(updateExportOutputOptions()));
}
int ExportDialog::nextId() const
{
if (exportMode == ExportManager::UNDEFINED)
return pageId(ui->proxyPage);
QList<QWizardPage*> order = pageOrder[exportMode];
int idx = order.indexOf(currentPage());
idx++;
if (idx < order.size())
return pageId(order[idx]);
return -1;
}
bool ExportDialog::isPluginConfigValid() const
{
return pluginConfigOk.size() == 0;
}
void ExportDialog::initPageOrder()
{
setStartId(pageId(ui->exportSubjectPage));
pageOrder[ExportManager::DATABASE] = {ui->databaseObjectsPage, ui->formatAndOptionsPage};
pageOrder[ExportManager::TABLE] = {ui->tablePage, ui->formatAndOptionsPage};
pageOrder[ExportManager::QUERY_RESULTS] = {ui->queryPage, ui->formatAndOptionsPage};
updateExportMode();
}
int ExportDialog::pageId(QWizardPage* wizardPage) const
{
for (int id : pageIds())
{
if (page(id) == wizardPage)
return id;
}
return -1;
}
void ExportDialog::tablePageDisplayed()
{
if (!tablePageVisited)
{
if (table.isNull()) // table mode selected by user, not forced by setTableMode().
{
ui->exportTableDbNameCombo->setModel(dbListModel);
if (db)
ui->exportTableDbNameCombo->setCurrentText(db->getName());
connect(ui->exportTableDbNameCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateDbTables()));
ui->exportTableNameCombo->setModel(tablesModel);
connect(ui->exportTableNameCombo, SIGNAL(currentTextChanged(QString)), ui->tablePage, SIGNAL(completeChanged()));
}
updateDbTables();
emit tablePageCompleteChanged();
tablePageVisited = true;
}
}
void ExportDialog::queryPageDisplayed()
{
if (!queryPageVisited)
{
if (query.isNull()) // query mode selected by user, not forced by setQueryMode().
{
ui->queryDatabaseCombo->setModel(dbListModel);
if (db)
ui->queryDatabaseCombo->setCurrentText(db->getName());
connect(ui->queryDatabaseCombo, SIGNAL(currentIndexChanged(int)), ui->queryPage, SIGNAL(completeChanged()));
}
updateQueryEditDb();
emit queryPageCompleteChanged();
queryPageVisited = true;
}
}
void ExportDialog::dbObjectsPageDisplayed()
{
if (!dbObjectsPageVisited)
{
ui->dbObjectsDatabaseCombo->setModel(dbListModel);
connect(ui->dbObjectsDatabaseCombo, SIGNAL(currentIndexChanged(int)), ui->queryPage, SIGNAL(completeChanged()));
if (db)
ui->dbObjectsDatabaseCombo->setCurrentText(db->getName());
dbObjectsPageVisited = true;
}
}
void ExportDialog::formatPageDisplayed()
{
if (!formatPageVisited)
{
ui->formatCombo->addItems(EXPORT_MANAGER->getAvailableFormats(exportMode));
ui->encodingCombo->addItems(textCodecNames());
ui->encodingCombo->setCurrentText(defaultCodecName());
formatPageVisited = true;
}
readStdConfigForLastPage();
pluginSelected();
emit formatPageCompleteChanged();
}
ExportPlugin* ExportDialog::getSelectedPlugin() const
{
return EXPORT_MANAGER->getPluginForFormat(ui->formatCombo->currentText());
}
void ExportDialog::updateExportMode()
{
if (ui->subjectDatabaseRadio->isChecked())
exportMode = ExportManager::DATABASE;
else if (ui->subjectTableRadio->isChecked())
exportMode = ExportManager::TABLE;
else if (ui->subjectQueryRadio->isChecked())
exportMode = ExportManager::QUERY_RESULTS;
else
exportMode = ExportManager::UNDEFINED;
}
void ExportDialog::pageChanged(int pageId)
{
QWizardPage* wizardPage = page(pageId);
if (wizardPage == ui->tablePage)
tablePageDisplayed();
else if (wizardPage == ui->queryPage)
queryPageDisplayed();
else if (wizardPage == ui->databaseObjectsPage)
dbObjectsPageDisplayed();
else if (wizardPage == ui->formatAndOptionsPage)
formatPageDisplayed();
else if (wizardPage == ui->proxyPage)
next();
}
void ExportDialog::updateDbTables()
{
if (!table.isNull())
return; // we don't want tables to be automatically updated if this is strictly set table
QString dbName = ui->exportTableDbNameCombo->currentText();
db = DBLIST->getByName(dbName);
tablesModel->setDb(db);
}
void ExportDialog::browseForExportFile()
{
QStringList filters;
if (currentPlugin)
filters << currentPlugin->getFormatName()+" (*." + currentPlugin->defaultFileExtension() + ")";
filters << tr("All files (*)");
QString dir = getFileDialogInitPath();
QString fileName = QFileDialog::getSaveFileName(this, tr("Pick file to export to"), dir, filters.join(";;"), 0, QFileDialog::DontConfirmOverwrite);
if (fileName.isNull())
return;
if (currentPlugin && !fileName.endsWith("." + currentPlugin->defaultFileExtension()))
fileName += "." + currentPlugin->defaultFileExtension();
ui->exportFileEdit->setText(fileName);
setFileDialogInitPathByFile(fileName);
}
void ExportDialog::pluginSelected()
{
pluginConfigOk.clear();
currentPlugin = getSelectedPlugin();
if (!currentPlugin)
{
qCritical() << "Could not find export plugin, while it was selected on ui:" << ui->formatCombo->currentText();
return;
}
currentPlugin->setExportMode(exportMode);
updateExportOutputOptions();
updateOptions();
}
void ExportDialog::updateExportOutputOptions()
{
ExportManager::StandardConfigFlags options = currentPlugin->standardOptionsToEnable();
bool displayCodec = options.testFlag(ExportManager::CODEC) && !ui->exportClipboardRadio->isChecked();
bool clipboardSupported = currentPlugin->getSupportedModes().testFlag(ExportManager::CLIPBOARD);
bool outputFileSupported = currentPlugin->getSupportedModes().testFlag(ExportManager::FILE);
bool enabled = outputFileSupported && ui->exportFileRadio->isChecked();
ui->exportFileEdit->setEnabled(enabled);
ui->exportFileButton->setEnabled(enabled);
ui->exportClipboardRadio->setVisible(clipboardSupported);
ui->exportFileRadio->setVisible(outputFileSupported);
ui->exportFileEdit->setVisible(outputFileSupported);
ui->exportFileButton->setVisible(outputFileSupported);
if (!clipboardSupported && outputFileSupported)
ui->exportFileRadio->setChecked(true);
ui->encodingCombo->setVisible(displayCodec);
ui->encodingLabel->setVisible(displayCodec);
if (displayCodec)
{
QString codec = CFG->get(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_CODEC).toString();
QString defaultCodec = currentPlugin->getDefaultEncoding();
if (codec.isNull())
codec = defaultCodec;
int idx = ui->encodingCombo->findText(codec);
if (idx == -1 && codec != defaultCodec)
{
codec = defaultCodec;
idx = ui->encodingCombo->findText(codec);
}
if (idx > -1)
ui->encodingCombo->setCurrentIndex(idx);
}
ui->exportToGroup->setVisible(clipboardSupported || outputFileSupported || displayCodec);
}
void ExportDialog::updateQueryEditDb()
{
Db* db = getDbForExport(ui->queryDatabaseCombo->currentText());
ui->queryEdit->setDb(db);
}
void ExportDialog::updateOptions()
{
ui->optionsGroup->setVisible(false);
if (!currentPlugin)
{
qCritical() << "Could not find export plugin, while it was selected on ui:" << ui->formatCombo->currentText();
return;
}
int optionsRow = 0;
updatePluginOptions(currentPlugin, optionsRow);
ui->optionsGroup->setVisible(optionsRow > 0);
}
void ExportDialog::updateDbObjTree()
{
selectableDbListModel->setDbName(ui->dbObjectsDatabaseCombo->currentText());
QModelIndex root = selectableDbListModel->index(0, 0);
if (root.isValid())
{
root = setupNewDbObjTreeRoot(root);
ui->dbObjectsTree->setRootIndex(root);
ui->dbObjectsTree->expand(root);
QModelIndex child;
for (int i = 0; (child = selectableDbListModel->index(i, 0, root)).isValid(); i++)
ui->dbObjectsTree->expand(child);
}
dbObjectsSelectAll();
}
void ExportDialog::dbObjectsSelectAll()
{
selectableDbListModel->setRootChecked(true);
}
void ExportDialog::dbObjectsDeselectAll()
{
selectableDbListModel->setRootChecked(false);
}
void ExportDialog::hideCoverWidget()
{
widgetCover->hide();
}
void ExportDialog::storeInClipboard(const QByteArray& bytes, const QString& mimeType)
{
QMimeData* mimeData = new QMimeData;
mimeData->setData(mimeType, bytes);
QApplication::clipboard()->setMimeData(mimeData);
}
void ExportDialog::storeInClipboard(const QString& str)
{
QApplication::clipboard()->setText(str);
}
void ExportDialog::success()
{
QWizard::accept();
}
void ExportDialog::accept()
{
doExport();
}
int ExportDialog::exec()
{
readStdConfigForFirstPage();
return QDialog::exec();
}
void ExportDialog::updatePluginOptions(ExportPlugin* plugin, int& optionsRow)
{
safe_delete(pluginOptionsWidget);
QString formName = plugin->getExportConfigFormName();
CfgMain* cfgMain = plugin->getConfig();
if (formName.isNull() || !cfgMain)
{
if (!formName.isNull())
{
qWarning() << "FormName is given, but cfgMain is null in ExportDialog::updatePluginOptions() for plugin:" << plugin->getName()
<< ", formName:" << formName;
}
return;
}
if (!FORMS->hasWidget(formName))
{
qWarning() << "Export plugin" << plugin->getName() << "requested for form named" << formName << "but FormManager doesn't have it."
<< "Available forms are:" << FORMS->getAvailableForms();
return;
}
safe_delete(configMapper);
QGridLayout* grid = dynamic_cast<QGridLayout*>(ui->optionsGroup->layout());
pluginOptionsWidget = FORMS->createWidget(formName);
if (pluginOptionsWidget->layout())
pluginOptionsWidget->layout()->setMargin(0);
grid->addWidget(pluginOptionsWidget, 1, 0, 1, 2);
optionsRow++;
configMapper = new ConfigMapper(cfgMain);
configMapper->bindToConfig(pluginOptionsWidget);
connect(configMapper, SIGNAL(modified(QWidget*)), this, SLOT(updateValidation()));
plugin->validateOptions();
}
void ExportDialog::storeStdConfig(const ExportManager::StandardExportConfig &stdConfig)
{
CFG->begin();
CFG->set(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_CODEC, stdConfig.codec);
CFG->set(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_FILE, stdConfig.outputFileName);
CFG->set(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_CLIP, stdConfig.intoClipboard);
CFG->set(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_DATA, stdConfig.exportData);
CFG->set(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_IDX, stdConfig.exportIndexes);
CFG->set(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_TRIG, stdConfig.exportTriggers);
CFG->set(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_FORMAT, currentPlugin->getFormatName());
CFG->commit();
}
void ExportDialog::readStdConfigForFirstPage()
{
bool exportData = CFG->get(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_DATA, true).toBool();
bool exportIndexes = CFG->get(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_IDX, true).toBool();
bool exportTriggers = CFG->get(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_TRIG, true).toBool();
if (exportMode == ExportManager::DATABASE)
{
ui->exportDbDataCheck->setChecked(exportData);
ui->exportDbIndexCheck->setChecked(exportIndexes);
ui->exportDbTriggerCheck->setChecked(exportTriggers);
}
else if (exportMode == ExportManager::TABLE)
{
ui->exportTableDataCheck->setChecked(exportData);
ui->exportTableIndexesCheck->setChecked(exportIndexes);
ui->exportTableTriggersCheck->setChecked(exportTriggers);
}
}
void ExportDialog::readStdConfigForLastPage()
{
QString format = CFG->get(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_FORMAT).toString();
int idx = ui->formatCombo->findText(format);
if (idx > -1)
ui->formatCombo->setCurrentIndex(idx);
bool useClipboard = CFG->get(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_CLIP, false).toBool();
ui->exportFileRadio->setChecked(!useClipboard);
ui->exportClipboardRadio->setChecked(useClipboard);
ui->exportFileEdit->setText(CFG->get(EXPORT_DIALOG_CFG_GROUP, EXPORT_DIALOG_CFG_FILE, QString()).toString());
// Codec is read within updateExportOutputOptions()
}
void ExportDialog::updateValidation()
{
if (!currentPlugin)
return;
currentPlugin->validateOptions();
emit formatPageCompleteChanged();
}
void ExportDialog::doExport()
{
widgetCover->show();
ExportManager::StandardExportConfig stdConfig = getExportConfig();
storeStdConfig(stdConfig);
configMapper->saveFromWidget(pluginOptionsWidget);
QString format = ui->formatCombo->currentText();
switch (exportMode)
{
case ExportManager::DATABASE:
exportDatabase(stdConfig, format);
break;
case ExportManager::TABLE:
exportTable(stdConfig, format);
break;
case ExportManager::QUERY_RESULTS:
exportQuery(stdConfig, format);
break;
case ExportManager::UNDEFINED:
qCritical() << "Finished export dialog with undefined mode.";
notifyInternalError();
break;
case ExportManager::FILE:
case ExportManager::CLIPBOARD:
break;
}
}
void ExportDialog::exportDatabase(const ExportManager::StandardExportConfig& stdConfig, const QString& format)
{
Db* db = getDbForExport(ui->dbObjectsDatabaseCombo->currentText());
if (!db || !db->isValid())
return;
EXPORT_MANAGER->configure(format, stdConfig);
EXPORT_MANAGER->exportDatabase(db, selectableDbListModel->getCheckedObjects());
}
void ExportDialog::exportTable(const ExportManager::StandardExportConfig& stdConfig, const QString& format)
{
Db* db = getDbForExport(ui->exportTableDbNameCombo->currentText());
if (!db || !db->isValid())
return;
EXPORT_MANAGER->configure(format, stdConfig);
// TODO when dbnames are fully supported, pass the dbname below
EXPORT_MANAGER->exportTable(db, QString(), ui->exportTableNameCombo->currentText());
}
void ExportDialog::exportQuery(const ExportManager::StandardExportConfig& stdConfig, const QString& format)
{
Db* db = getDbForExport(ui->queryDatabaseCombo->currentText());
if (!db || !db->isValid())
return;
EXPORT_MANAGER->configure(format, stdConfig);
EXPORT_MANAGER->exportQueryResults(db, ui->queryEdit->toPlainText());
}
ExportManager::StandardExportConfig ExportDialog::getExportConfig() const
{
bool clipboardSupported = currentPlugin->getSupportedModes().testFlag(ExportManager::CLIPBOARD);
bool outputFileSupported = currentPlugin->getSupportedModes().testFlag(ExportManager::FILE);
bool clipboard = clipboardSupported && ui->exportClipboardRadio->isChecked();
ExportManager::StandardExportConfig stdConfig;
stdConfig.intoClipboard = clipboard;
if (clipboard)
stdConfig.outputFileName = QString();
else if (outputFileSupported)
stdConfig.outputFileName = ui->exportFileEdit->text();
if (exportMode == ExportManager::DATABASE)
{
stdConfig.exportData = ui->exportDbDataCheck->isChecked();
stdConfig.exportIndexes = ui->exportDbIndexCheck->isChecked();
stdConfig.exportTriggers = ui->exportDbTriggerCheck->isChecked();
}
else if (exportMode == ExportManager::TABLE)
{
stdConfig.exportData = ui->exportTableDataCheck->isChecked();
stdConfig.exportIndexes = ui->exportTableIndexesCheck->isChecked();
stdConfig.exportTriggers = ui->exportTableTriggersCheck->isChecked();
}
else
{
stdConfig.exportData = false;
}
if (ui->encodingCombo->isVisible() && ui->encodingCombo->currentIndex() > -1)
stdConfig.codec = ui->encodingCombo->currentText();
else
stdConfig.codec = defaultCodecName();
return stdConfig;
}
Db* ExportDialog::getDbForExport(const QString& name)
{
Db* db = DBLIST->getByName(name);
if (!db)
{
qCritical() << "Could not find db selected in combo:" << name;
notifyInternalError();
return nullptr;
}
return db;
}
void ExportDialog::notifyInternalError()
{
notifyError(tr("Internal error during export. This is a bug. Please report it."));
}
QModelIndex ExportDialog::setupNewDbObjTreeRoot(const QModelIndex& root)
{
QModelIndex newRoot = root;
DbTreeItem* item = nullptr;
while (newRoot.isValid())
{
item = selectableDbListModel->getItemForIndex(newRoot);
if (item->getType() == DbTreeItem::Type::DB)
return newRoot;
newRoot = selectableDbListModel->index(0, 0, newRoot);
}
return newRoot;
}
void ExportDialog::handleValidationResultFromPlugin(bool valid, CfgEntry* key, const QString& errorMsg)
{
QWidget* w = configMapper->getBindWidgetForConfig(key);
if (w)
setValidState(w, valid, errorMsg);
if (valid == pluginConfigOk.contains(key)) // if state changed
{
if (!valid)
pluginConfigOk[key] = false;
else
pluginConfigOk.remove(key);
emit formatPageCompleteChanged();
}
}
void ExportDialog::stateUpdateRequestFromPlugin(CfgEntry* key, bool visible, bool enabled)
{
QWidget* w = configMapper->getBindWidgetForConfig(key);
if (!w)
return;
w->setVisible(visible);
w->setEnabled(enabled);
}
|