summaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp')
-rw-r--r--SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp145
1 files changed, 112 insertions, 33 deletions
diff --git a/SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp b/SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp
index cd1ba72..fd344e8 100644
--- a/SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp
+++ b/SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp
@@ -33,9 +33,11 @@
#include "services/importmanager.h"
#include "dbobjectdialogs.h"
#include "dialogs/exportdialog.h"
+#include "common/centerediconitemdelegate.h"
#include "themetuner.h"
#include "dialogs/importdialog.h"
#include "dialogs/populatedialog.h"
+#include "datagrid/sqlqueryitem.h"
#include <QMenu>
#include <QToolButton>
#include <QLabel>
@@ -46,7 +48,6 @@
#include <QPushButton>
#include <QDebug>
#include <QStyleFactory>
-#include <datagrid/sqlqueryitem.h>
// TODO extend QTableView for columns and constraints, so they show full-row-width drop indicator,
// instead of single column drop indicator.
@@ -142,6 +143,9 @@ void TableWindow::init()
{
ui->setupUi(this);
ui->structureSplitter->setStretchFactor(0, 2);
+ ui->structureView->horizontalHeader()->setSectionsClickable(false);
+ ui->structureView->verticalHeader()->setSectionsClickable(false);
+ constraintColumnsDelegate = new CenteredIconItemDelegate(this);
#ifdef Q_OS_MACX
QStyle *fusion = QStyleFactory::create("Fusion");
@@ -159,6 +163,7 @@ void TableWindow::init()
ui->dataView->init(dataModel);
initActions();
+ updateTabsOrder();
connect(dataModel, SIGNAL(executionSuccessful()), this, SLOT(executionSuccessful()));
connect(dataModel, SIGNAL(executionFailed(QString)), this, SLOT(executionFailed(QString)));
@@ -168,6 +173,9 @@ void TableWindow::init()
connect(ui->tableNameEdit, SIGNAL(textChanged(QString)), this, SLOT(nameChanged()));
connect(ui->indexList, SIGNAL(itemSelectionChanged()), this, SLOT(updateIndexesState()));
connect(ui->triggerList, SIGNAL(itemSelectionChanged()), this, SLOT(updateTriggersState()));
+ connect(CFG_UI.General.DataTabAsFirstInTables, SIGNAL(changed(const QVariant&)), this, SLOT(updateTabsOrder()));
+ connect(ui->structureView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(structureViewDoubleClicked(const QModelIndex&)));
+ connect(ui->tableConstraintsView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(constraintsViewDoubleClicked(const QModelIndex&)));
structureExecutor = new ChainExecutor(this);
connect(structureExecutor, SIGNAL(success()), this, SLOT(changesSuccessfullyCommited()));
@@ -253,9 +261,9 @@ void TableWindow::createIndexActions()
createAction(REFRESH_INDEXES, ICONS.RELOAD, tr("Refresh index list", "table window"), this, SLOT(updateIndexes()), ui->indexToolBar, ui->indexList);
ui->indexToolBar->addSeparator();
createAction(ADD_INDEX, ICONS.INDEX_ADD, tr("Create index", "table window"), this, SLOT(addIndex()), ui->indexToolBar, ui->indexList);
- createAction(EDIT_INDEX, ICONS.INDEX_EDIT, tr("Edit index", "table window"), this, SLOT(editIndex()), ui->indexToolBar, ui->indexList);
+ createAction(EDIT_INDEX, ICONS.INDEX_EDIT, tr("Edit index", "table window"), this, SLOT(editCurrentIndex()), ui->indexToolBar, ui->indexList);
createAction(DEL_INDEX, ICONS.INDEX_DEL, tr("Delete index", "table window"), this, SLOT(delIndex()), ui->indexToolBar, ui->indexList);
- connect(ui->indexList, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(editIndex()));
+ connect(ui->indexList, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(indexViewDoubleClicked(QModelIndex)));
}
void TableWindow::createTriggerActions()
@@ -265,14 +273,14 @@ void TableWindow::createTriggerActions()
createAction(ADD_TRIGGER, ICONS.TRIGGER_ADD, tr("Create trigger", "table window"), this, SLOT(addTrigger()), ui->triggerToolBar, ui->triggerList);
createAction(EDIT_TRIGGER, ICONS.TRIGGER_EDIT, tr("Edit trigger", "table window"), this, SLOT(editTrigger()), ui->triggerToolBar, ui->triggerList);
createAction(DEL_TRIGGER, ICONS.TRIGGER_DEL, tr("Delete trigger", "table window"), this, SLOT(delTrigger()), ui->triggerToolBar, ui->triggerList);
- connect(ui->triggerList, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(editTrigger()));
+ connect(ui->triggerList, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(triggerViewDoubleClicked(QModelIndex)));
}
void TableWindow::editColumn(const QModelIndex& idx)
{
if (!idx.isValid())
{
- qWarning() << "Called TableWindow::editColumn() with invalid index.";
+ addColumn();
return;
}
@@ -352,6 +360,8 @@ void TableWindow::executeStructureChanges()
modifyingThisTable = true;
structureExecutor->setDb(db);
structureExecutor->setQueries(sqls);
+ structureExecutor->setDisableForeignKeys(true);
+ structureExecutor->setDisableObjectDropsDetection(true);
widgetCover->show();
structureExecutor->exec();
}
@@ -458,23 +468,27 @@ void TableWindow::setupDefShortcuts()
void TableWindow::executionSuccessful()
{
- modifyingThisTable = false;
dataLoaded = true;
}
void TableWindow::executionFailed(const QString& errorText)
{
- modifyingThisTable = false;
notifyError(tr("Could not load data for table %1. Error details: %2").arg(table).arg(errorText));
}
void TableWindow::initDbAndTable()
{
+ int totalConstrCols = 6;
if (db->getVersion() == 2)
{
ui->withoutRowIdCheck->setVisible(false);
+ totalConstrCols -= 2;
}
+ totalConstrCols += 2; // we start at 3rd column
+ for (int colIdx = 2; colIdx < totalConstrCols; colIdx++)
+ ui->structureView->setItemDelegateForColumn(colIdx, constraintColumnsDelegate);
+
if (existingTable)
{
dataModel->setDb(db);
@@ -785,6 +799,8 @@ void TableWindow::commitStructure(bool skipWarning)
void TableWindow::changesSuccessfullyCommited()
{
+ modifyingThisTable = false;
+
QStringList sqls = structureExecutor->getQueries();
CFG->addDdlHistory(sqls.join("\n"), db->getName(), db->getPath());
@@ -804,6 +820,11 @@ void TableWindow::changesSuccessfullyCommited()
updateNewTableState();
updateWindowTitle();
+ if (oldTable.compare(table, Qt::CaseInsensitive) == 0 || oldTable.isEmpty())
+ notifyInfo(tr("Commited changes for table '%1' successfly.").arg(table));
+ else
+ notifyInfo(tr("Commited changes for table '%1' (named before '%2') successfly.").arg(table, oldTable));
+
DBTREE->refreshSchema(db);
if (tableModifier)
@@ -832,6 +853,7 @@ void TableWindow::changesFailedToCommit(int errorCode, const QString& errorText)
{
qDebug() << "TableWindow::changesFailedToCommit:" << errorCode << errorText;
+ modifyingThisTable = false;
widgetCover->hide();
notifyError(tr("Could not commit table structure. Error message: %1", "table window").arg(errorText));
}
@@ -1011,8 +1033,8 @@ TokenList TableWindow::indexColumnTokens(SqliteCreateIndexPtr index)
if (index->indexedColumns.size() == 0)
return TokenList();
- SqliteIndexedColumn* firstCol = index->indexedColumns.first();
- SqliteIndexedColumn* lastCol = index->indexedColumns.last();
+ SqliteOrderBy* firstCol = index->indexedColumns.first();
+ SqliteOrderBy* lastCol = index->indexedColumns.last();
if (firstCol->tokens.size() == 0)
return TokenList();
@@ -1048,9 +1070,9 @@ QString TableWindow::getCurrentTrigger() const
void TableWindow::applyInitialTab()
{
if (existingTable && !table.isNull() && CFG_UI.General.OpenTablesOnData.get())
- ui->tabWidget->setCurrentIndex(1);
+ ui->tabWidget->setCurrentIndex(getDataTabIdx());
else
- ui->tabWidget->setCurrentIndex(0);
+ ui->tabWidget->setCurrentIndex(getStructureTabIdx());
}
void TableWindow::resizeStructureViewColumns()
@@ -1060,6 +1082,16 @@ void TableWindow::resizeStructureViewColumns()
ui->structureView->resizeColumnToContents(c);
}
+int TableWindow::getDataTabIdx() const
+{
+ return ui->tabWidget->indexOf(ui->dataTab);
+}
+
+int TableWindow::getStructureTabIdx() const
+{
+ return ui->tabWidget->indexOf(ui->structureTab);
+}
+
void TableWindow::updateDdlTab()
{
createTable->rebuildTokens();
@@ -1103,7 +1135,10 @@ void TableWindow::delConstraint()
void TableWindow::editConstraint(const QModelIndex& idx)
{
if (!idx.isValid())
+ {
+ addConstraint();
return;
+ }
SqliteCreateTable::Constraint* constr = structureConstraintsModel->getConstraint(idx.row());
ConstraintDialog dialog(ConstraintDialog::EDIT, constr, createTable.data(), db, this);
@@ -1217,39 +1252,37 @@ void TableWindow::createSimilarTable()
void TableWindow::tabChanged(int newTab)
{
- switch (newTab)
+ if (tabsMoving)
+ return;
+
+ if (newTab == getDataTabIdx())
{
- case 1:
+ if (isModified())
{
- if (isModified())
- {
- int res = QMessageBox::question(this, tr("Uncommited changes"),
- tr("There are uncommited structure modifications. You cannot browse or edit data until you have "
- "table structure settled.\n"
- "Do you want to commit the structure, or do you want to go back to the structure tab?"),
- tr("Go back to structure tab"), tr("Commit modifications and browse data."));
-
- ui->tabWidget->setCurrentIndex(0);
- if (res == 1)
- commitStructure(true);
-
- break;
- }
+ int res = QMessageBox::question(this, tr("Uncommited changes"),
+ tr("There are uncommited structure modifications. You cannot browse or edit data until you have "
+ "table structure settled.\n"
+ "Do you want to commit the structure, or do you want to go back to the structure tab?"),
+ tr("Go back to structure tab"), tr("Commit modifications and browse data."));
- if (!dataLoaded)
- ui->dataView->refreshData();
+ ui->tabWidget->setCurrentIndex(0);
+ if (res == 1)
+ commitStructure(true);
- break;
+ return;
}
+
+ if (!dataLoaded)
+ ui->dataView->refreshData();
}
}
-void TableWindow::on_structureView_doubleClicked(const QModelIndex &index)
+void TableWindow::structureViewDoubleClicked(const QModelIndex &index)
{
editColumn(index);
}
-void TableWindow::on_tableConstraintsView_doubleClicked(const QModelIndex &index)
+void TableWindow::constraintsViewDoubleClicked(const QModelIndex &index)
{
editConstraint(index);
}
@@ -1280,7 +1313,7 @@ void TableWindow::addIndex()
updateIndexes();
}
-void TableWindow::editIndex()
+void TableWindow::editCurrentIndex()
{
QString index = getCurrentIndex();
if (index.isNull())
@@ -1291,6 +1324,22 @@ void TableWindow::editIndex()
updateIndexes();
}
+void TableWindow::indexViewDoubleClicked(const QModelIndex& idx)
+{
+ if (!idx.isValid())
+ {
+ addIndex();
+ return;
+ }
+
+ QString index = ui->indexList->item(idx.row(), 0)->text();
+
+ DbObjectDialogs dialogs(db, this);
+ dialogs.editIndex(index);
+ updateIndexes();
+}
+
+
void TableWindow::delIndex()
{
QString index = getCurrentIndex();
@@ -1313,7 +1362,25 @@ void TableWindow::editTrigger()
{
QString trigger = getCurrentTrigger();
if (trigger.isNull())
+ {
+ addTrigger();
+ return;
+ }
+
+ DbObjectDialogs dialogs(db, this);
+ dialogs.editTrigger(trigger);
+ updateTriggers();
+}
+
+void TableWindow::triggerViewDoubleClicked(const QModelIndex& idx)
+{
+ if (!idx.isValid())
+ {
+ addTrigger();
return;
+ }
+
+ QString trigger = ui->triggerList->item(idx.row(), 0)->text();
DbObjectDialogs dialogs(db, this);
dialogs.editTrigger(trigger);
@@ -1488,6 +1555,18 @@ void TableWindow::delColumn(const QString& columnName)
delColumn(colIdx);
}
+void TableWindow::updateTabsOrder()
+{
+ tabsMoving = true;
+ ui->tabWidget->removeTab(getDataTabIdx());
+ int idx = 1;
+ if (CFG_UI.General.DataTabAsFirstInTables.get())
+ idx = 0;
+
+ ui->tabWidget->insertTab(idx, ui->dataTab, tr("Data"));
+ tabsMoving = false;
+}
+
bool TableWindow::restoreSessionNextTime()
{
return existingTable && db && !DBLIST->isTemporary(db);