diff options
| author | 2015-04-04 14:41:10 -0400 | |
|---|---|---|
| committer | 2015-04-04 14:41:10 -0400 | |
| commit | b5f93b05578293d1d233b4920a28a5c2fd826f94 (patch) | |
| tree | 82332679f647e9c76e331206786d07a58dcfa9b8 /SQLiteStudio3/guiSQLiteStudio/dialogs/dbdialog.cpp | |
| parent | af8a7a3e3dccf9c9ad257e3952173d180c8a7421 (diff) | |
| parent | a5b034d4a9c44f9bc1e83b01de82530f8fc63013 (diff) | |
Merge tag 'upstream/3.0.4'
Upstream version 3.0.4
# gpg: Signature made Sat 04 Apr 2015 02:41:09 PM EDT using RSA key ID EBE9BD91
# gpg: Good signature from "Unit 193 <unit193@gmail.com>"
# gpg: aka "Unit 193 <unit193@ninthfloor.org>"
# gpg: aka "Unit 193 <unit193@ubuntu.com>"
# gpg: aka "Unit 193 <unit193@ninthfloor.com>"
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/dialogs/dbdialog.cpp')
| -rw-r--r-- | SQLiteStudio3/guiSQLiteStudio/dialogs/dbdialog.cpp | 173 |
1 files changed, 107 insertions, 66 deletions
diff --git a/SQLiteStudio3/guiSQLiteStudio/dialogs/dbdialog.cpp b/SQLiteStudio3/guiSQLiteStudio/dialogs/dbdialog.cpp index a2a9c36..05d50af 100644 --- a/SQLiteStudio3/guiSQLiteStudio/dialogs/dbdialog.cpp +++ b/SQLiteStudio3/guiSQLiteStudio/dialogs/dbdialog.cpp @@ -15,6 +15,7 @@ #include <QPushButton> #include <QFileDialog> #include <QComboBox> +#include <QTimer> DbDialog::DbDialog(Mode mode, QWidget *parent) : QDialog(parent), @@ -54,26 +55,6 @@ QString DbDialog::getName() return ui->nameEdit->text(); } -Db* DbDialog::getDb() -{ - if (ui->typeCombo->currentIndex() < 0) - return nullptr; - - Db* testDb = nullptr; - QHash<QString, QVariant> options = collectOptions(); - QString path = ui->fileEdit->text(); - foreach (DbPlugin* plugin, dbPlugins) - { - if (options.contains(DB_PLUGIN) && options[DB_PLUGIN].toString() != plugin->getName()) - continue; - - testDb = plugin->getInstance("", path, options); - if (testDb) - return testDb; - } - return testDb; -} - bool DbDialog::isPermanent() { return ui->permamentCheckBox->isChecked(); @@ -95,17 +76,18 @@ void DbDialog::showEvent(QShowEvent *e) { if (db) { + disableTypeAutodetection = true; int idx = ui->typeCombo->findText(db->getTypeLabel()); ui->typeCombo->setCurrentIndex(idx); - ui->typeCombo->setEnabled(false); // converting to other type is in separate dialog, it's different feature ui->generateCheckBox->setChecked(false); ui->fileEdit->setText(db->getPath()); ui->nameEdit->setText(db->getName()); + disableTypeAutodetection = false; } else if (ui->typeCombo->count() > 0) { - int idx = ui->typeCombo->findText("SQLite3"); // we should have SQLite3 plugin + int idx = ui->typeCombo->findText("SQLite 3", Qt::MatchFixedString); // we should have SQLite 3 plugin if (idx > -1) ui->typeCombo->setCurrentIndex(idx); else @@ -126,24 +108,30 @@ void DbDialog::init() { ui->setupUi(this); - ui->browseButton->setIcon(ICONS.DATABASE_FILE); - dbPlugins = PLUGINS->getLoadedPlugins<DbPlugin>(); - foreach (DbPlugin* dbPlugin, dbPlugins) - { - ui->typeCombo->addItem(dbPlugin->getLabel()); - } + ui->browseCreateButton->setIcon(ICONS.PLUS); - ui->browseButton->setVisible(true); + for (DbPlugin* dbPlugin : PLUGINS->getLoadedPlugins<DbPlugin>()) + dbPlugins[dbPlugin->getLabel()] = dbPlugin; + + QStringList typeLabels; + typeLabels += dbPlugins.keys(); + typeLabels.sort(Qt::CaseInsensitive); + ui->typeCombo->addItems(typeLabels); + + ui->browseCreateButton->setVisible(true); ui->testConnIcon->setVisible(false); connect(ui->fileEdit, SIGNAL(textChanged(QString)), this, SLOT(fileChanged(QString))); connect(ui->nameEdit, SIGNAL(textChanged(QString)), this, SLOT(nameModified(QString))); connect(ui->generateCheckBox, SIGNAL(toggled(bool)), this, SLOT(generateNameSwitched(bool))); - connect(ui->browseButton, SIGNAL(clicked()), this, SLOT(browseClicked())); + connect(ui->browseCreateButton, SIGNAL(clicked()), this, SLOT(browseClicked())); + connect(ui->browseOpenButton, SIGNAL(clicked()), this, SLOT(browseClicked())); connect(ui->testConnButton, SIGNAL(clicked()), this, SLOT(testConnectionClicked())); connect(ui->typeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(dbTypeChanged(int))); generateNameSwitched(true); + + layout()->setSizeConstraint(QLayout::SetFixedSize); } void DbDialog::updateOptions() @@ -153,10 +141,14 @@ void DbDialog::updateOptions() // Remove olds foreach (QWidget* w, optionWidgets) { - ui->gridLayout->removeWidget(w); + ui->optionsGrid->removeWidget(w); delete w; } - adjustSize(); + + customBrowseHandler = nullptr; + ui->pathGroup->setTitle(tr("File")); + ui->browseOpenButton->setToolTip(tr("Browse for existing database file on local computer")); + ui->browseCreateButton->setVisible(true); optionWidgets.clear(); optionKeyToWidget.clear(); @@ -166,31 +158,43 @@ void DbDialog::updateOptions() lastWidgetInTabOrder = ui->permamentCheckBox; // Retrieve new list - DbPlugin* plugin = nullptr; - if (dbPlugins.count() > 0) + if (ui->typeCombo->currentIndex() > -1) { - int idx = ui->typeCombo->currentIndex(); - if (idx > -1 ) + DbPlugin* plugin = dbPlugins[ui->typeCombo->currentText()]; + QList<DbPluginOption> optList = plugin->getOptionsList(); + if (optList.size() > 0) { - plugin = dbPlugins[idx]; - QList<DbPluginOption> optList = plugin->getOptionsList(); - if (optList.size() > 0) + // Add new options + int row = ADDITIONAL_ROWS_BEGIN_INDEX; + for (const DbPluginOption& opt : optList) { - // Add new options - int row = ADDITIONAL_ROWS_BEGIN_INDEX; - foreach (DbPluginOption opt, optList) - addOption(opt, row++); + addOption(opt, row); + row++; } } } - adjustSize(); + setUpdatesEnabled(true); } -void DbDialog::addOption(const DbPluginOption& option, int row) +void DbDialog::addOption(const DbPluginOption& option, int& row) { + if (option.type == DbPluginOption::CUSTOM_PATH_BROWSE) + { + // This option does not add any editor, but has it's own label for path edit. + row--; + ui->pathGroup->setTitle(option.label); + ui->browseCreateButton->setVisible(false); + if (!option.toolTip.isEmpty()) + ui->browseOpenButton->setToolTip(option.toolTip); + + customBrowseHandler = option.customBrowseHandler; + return; + } + QLabel* label = new QLabel(option.label, this); + label->setAlignment(Qt::AlignVCenter|Qt::AlignRight); QWidget* editor = nullptr; QWidget* editorHelper = nullptr; // TODO, based on plugins for Url handlers @@ -204,15 +208,15 @@ void DbDialog::addOption(const DbPluginOption& option, int row) optionKeyToWidget[option.key] = editor; optionKeyToType[option.key] = option.type; - ui->gridLayout->addWidget(label, row, 0); - ui->gridLayout->addWidget(editor, row, 1); + ui->optionsGrid->addWidget(label, row, 0); + ui->optionsGrid->addWidget(editor, row, 1); setTabOrder(lastWidgetInTabOrder, editor); lastWidgetInTabOrder = editor; if (editorHelper) { - ui->gridLayout->addWidget(editorHelper, row, 2); + ui->optionsGrid->addWidget(editorHelper, row, 2); optionWidgets << editorHelper; helperToKey[editorHelper] = option.key; @@ -307,6 +311,8 @@ QWidget *DbDialog::getEditor(const DbPluginOption& opt, QWidget*& editorHelper) connect(sb, SIGNAL(valueChanged(double)), this, SLOT(propertyChanged())); break; } + case DbPluginOption::CUSTOM_PATH_BROWSE: + return nullptr; // should not happen ever, asserted one stack level before default: // TODO plugin based handling of custom editors qWarning() << "Unhandled DbDialog option for creating editor."; @@ -344,6 +350,8 @@ QVariant DbDialog::getValueFrom(DbPluginOption::Type type, QWidget *editor) case DbPluginOption::CHOICE: value = dynamic_cast<QComboBox*>(editor)->currentText(); break; + case DbPluginOption::CUSTOM_PATH_BROWSE: + break; // should not happen ever default: // TODO plugin based handling of custom editors qWarning() << "Unhandled DbDialog option for value."; @@ -373,6 +381,8 @@ void DbDialog::setValueFor(DbPluginOption::Type type, QWidget *editor, const QVa case DbPluginOption::CHOICE: dynamic_cast<QComboBox*>(editor)->setCurrentText(value.toString()); break; + case DbPluginOption::CUSTOM_PATH_BROWSE: + break; // should not happen ever default: qWarning() << "Unhandled DbDialog option to set value."; // TODO plugin based handling of custom editors @@ -382,18 +392,19 @@ void DbDialog::setValueFor(DbPluginOption::Type type, QWidget *editor, const QVa void DbDialog::updateType() { + if (disableTypeAutodetection) + return; + QFileInfo file(ui->fileEdit->text()); if (!file.exists() || file.isDir()) - { - ui->typeCombo->setEnabled(true); return; - } - DbPlugin* validPlugin = nullptr; + QString currentPluginLabel = ui->typeCombo->currentText(); + QList<DbPlugin*> validPlugins; QHash<QString,QVariant> options; QString path = ui->fileEdit->text(); Db* probeDb = nullptr; - foreach (DbPlugin* plugin, dbPlugins) + for (DbPlugin* plugin : dbPlugins) { probeDb = plugin->getInstance("", path, options); if (probeDb) @@ -401,15 +412,15 @@ void DbDialog::updateType() delete probeDb; probeDb = nullptr; - validPlugin = plugin; - break; + if (plugin->getLabel() == currentPluginLabel) + return; // current plugin is among valid plugins, no need to change anything + + validPlugins << plugin; } } - if (validPlugin) - ui->typeCombo->setCurrentText(validPlugin->getLabel()); - - ui->typeCombo->setEnabled(!validPlugin); + if (validPlugins.size() > 0) + ui->typeCombo->setCurrentText(validPlugins.first()->getLabel()); } QHash<QString, QVariant> DbDialog::collectOptions() @@ -424,7 +435,7 @@ QHash<QString, QVariant> DbDialog::collectOptions() DbPlugin* plugin = nullptr; if (dbPlugins.count() > 0) { - plugin = dbPlugins[ui->typeCombo->currentIndex()]; + plugin = dbPlugins[ui->typeCombo->currentText()]; options[DB_PLUGIN] = plugin->getName(); } @@ -433,9 +444,32 @@ QHash<QString, QVariant> DbDialog::collectOptions() bool DbDialog::testDatabase() { + if (ui->typeCombo->currentIndex() < 0) + return false; + QString path = ui->fileEdit->text(); - bool existed = QFile::exists(path); - bool res = getDb() != nullptr; + if (path.isEmpty()) + return false; + + QUrl url(path); + if (url.scheme().isEmpty()) + url.setScheme("file"); + + bool existed = false; + if (url.isLocalFile() && QFile::exists(path)) + existed = QFile::exists(path); + + QHash<QString, QVariant> options = collectOptions(); + DbPlugin* plugin = dbPlugins[ui->typeCombo->currentText()]; + Db* testDb = plugin->getInstance("", path, options); + + bool res = false; + if (testDb) + { + res = true; + delete testDb; + } + if (!existed) { QFile file(path); @@ -522,10 +556,9 @@ void DbDialog::valueForNameGenerationChanged() if (!ui->generateCheckBox->isChecked()) return; - DbPlugin* plugin = nullptr; if (dbPlugins.count() > 0) { - plugin = dbPlugins[ui->typeCombo->currentIndex()]; + DbPlugin* plugin = dbPlugins[ui->typeCombo->currentText()]; QString generatedName = plugin->generateDbName(ui->fileEdit->text()); generatedName = generateUniqueName(generatedName, existingDatabaseNames); ui->nameEdit->setText(generatedName); @@ -549,7 +582,7 @@ void DbDialog::generateNameSwitched(bool checked) { if (checked) { - ui->nameEdit->setPlaceholderText(tr("The name will be auto-generated")); + ui->nameEdit->setPlaceholderText(tr("Auto-generated")); valueForNameGenerationChanged(); } else @@ -570,6 +603,14 @@ void DbDialog::fileChanged(const QString &arg1) void DbDialog::browseClicked() { + if (customBrowseHandler) + { + customBrowseHandler(ui->fileEdit->text()); + return; + } + + bool createMode = (sender() == ui->browseCreateButton); + QFileInfo fileInfo(ui->fileEdit->text()); QString dir; if (ui->fileEdit->text().isEmpty()) @@ -581,7 +622,7 @@ void DbDialog::browseClicked() else dir = getFileDialogInitPath(); - QString path = getDbPath(dir); + QString path = getDbPath(createMode, dir); if (path.isNull()) return; |
