mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-19 10:20:17 -06:00
EditTableDialog: Add buttons to move a field up and down
Add two buttons to move the currently selected field up or down to allow changing the field order of a table. Extend the DBBrowserDB::renameColumn() method to support changing the position of a field.
This commit is contained in:
@@ -378,5 +378,86 @@ void EditTableDialog::removeField()
|
||||
|
||||
void EditTableDialog::fieldSelectionChanged()
|
||||
{
|
||||
ui->removeFieldButton->setEnabled(ui->treeWidget->selectionModel()->hasSelection());
|
||||
bool hasSelection = ui->treeWidget->selectionModel()->hasSelection();
|
||||
|
||||
// Enable the remove and the move up/down buttons if a field is selected, disable it otherwise
|
||||
ui->removeFieldButton->setEnabled(hasSelection);
|
||||
ui->buttonMoveUp->setEnabled(hasSelection);
|
||||
ui->buttonMoveDown->setEnabled(hasSelection);
|
||||
|
||||
// If the selected line is the first one disable the move up button, it it's the last one disable the move down button
|
||||
if(hasSelection)
|
||||
{
|
||||
ui->buttonMoveUp->setEnabled(ui->treeWidget->selectionModel()->currentIndex().row() != 0);
|
||||
ui->buttonMoveDown->setEnabled(ui->treeWidget->selectionModel()->currentIndex().row() != ui->treeWidget->topLevelItemCount() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void EditTableDialog::moveUp()
|
||||
{
|
||||
moveCurrentField(false);
|
||||
}
|
||||
|
||||
void EditTableDialog::moveDown()
|
||||
{
|
||||
moveCurrentField(true);
|
||||
}
|
||||
|
||||
void EditTableDialog::moveCurrentField(bool down)
|
||||
{
|
||||
int currentRow = ui->treeWidget->currentIndex().row();
|
||||
int newRow = currentRow + (down ? 1 : -1);
|
||||
|
||||
// Are we creating a new table or editing an old one?
|
||||
if(m_bNewTable)
|
||||
{
|
||||
// Creating a new one
|
||||
|
||||
// Save the combobox first by making a copy
|
||||
QComboBox* oldCombo = qobject_cast<QComboBox*>(ui->treeWidget->itemWidget(ui->treeWidget->topLevelItem(currentRow), kType));
|
||||
QComboBox* newCombo = new QComboBox(ui->treeWidget);
|
||||
newCombo->setProperty("column", oldCombo->property("column"));
|
||||
connect(newCombo, SIGNAL(activated(int)), this, SLOT(updateTypes()));
|
||||
newCombo->setEditable(false);
|
||||
for(int i=0;i<oldCombo->count();i++)
|
||||
newCombo->addItem(oldCombo->itemText(i));
|
||||
newCombo->setCurrentIndex(oldCombo->currentIndex());
|
||||
|
||||
// Now, just remove the item and insert it at it's new position, then restore the combobox
|
||||
QTreeWidgetItem* item = ui->treeWidget->takeTopLevelItem(currentRow);
|
||||
ui->treeWidget->insertTopLevelItem(newRow, item);
|
||||
ui->treeWidget->setItemWidget(item, kType, newCombo);
|
||||
|
||||
// Select the old item at its new position
|
||||
ui->treeWidget->setCurrentIndex(ui->treeWidget->currentIndex().sibling(newRow, 0));
|
||||
|
||||
// Finally update the table SQL
|
||||
sqlb::FieldVector fields = m_table.fields();
|
||||
std::swap(fields[newRow], fields[currentRow]);
|
||||
m_table.setFields(fields);
|
||||
} else {
|
||||
// Editing an old one
|
||||
|
||||
// Move the actual column
|
||||
if(!pdb->renameColumn(
|
||||
curTable,
|
||||
ui->treeWidget->currentItem()->text(0),
|
||||
m_table.fields().at(ui->treeWidget->indexOfTopLevelItem(ui->treeWidget->currentItem())),
|
||||
(down ? 1 : -1)
|
||||
))
|
||||
{
|
||||
QMessageBox::warning(0, QApplication::applicationName(), pdb->lastErrorMessage);
|
||||
} else {
|
||||
// Reload table SQL
|
||||
QString sTablesql = pdb->getObjectByName(curTable).getsql();
|
||||
m_table = sqlb::Table::parseSQL(sTablesql);
|
||||
populateFields();
|
||||
|
||||
// Select old item at new position
|
||||
ui->treeWidget->setCurrentIndex(ui->treeWidget->indexAt(QPoint(1, 1)).sibling(newRow, 0));
|
||||
}
|
||||
}
|
||||
|
||||
// Update the SQL preview
|
||||
updateSqlText();
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ private:
|
||||
void updateColumnWidth();
|
||||
void updateSqlText();
|
||||
|
||||
void moveCurrentField(bool down);
|
||||
|
||||
private slots:
|
||||
virtual void populateFields();
|
||||
virtual void addField();
|
||||
@@ -44,6 +46,8 @@ private slots:
|
||||
virtual void checkInput();
|
||||
virtual void itemChanged(QTreeWidgetItem* item, int column);
|
||||
virtual void updateTypes();
|
||||
virtual void moveUp();
|
||||
virtual void moveDown();
|
||||
|
||||
private:
|
||||
Ui::EditTableDialog* ui;
|
||||
|
||||
@@ -85,6 +85,46 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="buttonMoveUp">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Move field up</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="icons/icons.qrc">
|
||||
<normaloff>:/icons/up</normaloff>:/icons/up</iconset>
|
||||
</property>
|
||||
<property name="toolButtonStyle">
|
||||
<enum>Qt::ToolButtonTextBesideIcon</enum>
|
||||
</property>
|
||||
<property name="autoRaise">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="buttonMoveDown">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Move field down</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="icons/icons.qrc">
|
||||
<normaloff>:/icons/down</normaloff>:/icons/down</iconset>
|
||||
</property>
|
||||
<property name="toolButtonStyle">
|
||||
<enum>Qt::ToolButtonTextBesideIcon</enum>
|
||||
</property>
|
||||
<property name="autoRaise">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
@@ -292,6 +332,38 @@
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>buttonMoveUp</sender>
|
||||
<signal>clicked()</signal>
|
||||
<receiver>EditTableDialog</receiver>
|
||||
<slot>moveUp()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>282</x>
|
||||
<y>107</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>308</x>
|
||||
<y>235</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>buttonMoveDown</sender>
|
||||
<signal>clicked()</signal>
|
||||
<receiver>EditTableDialog</receiver>
|
||||
<slot>moveDown()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>413</x>
|
||||
<y>107</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>308</x>
|
||||
<y>235</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
<slots>
|
||||
<slot>fieldSelectionChanged()</slot>
|
||||
@@ -300,5 +372,7 @@
|
||||
<slot>removeField()</slot>
|
||||
<slot>checkInput()</slot>
|
||||
<slot>itemChanged()</slot>
|
||||
<slot>moveUp()</slot>
|
||||
<slot>moveDown()</slot>
|
||||
</slots>
|
||||
</ui>
|
||||
|
||||
BIN
src/icons/bullet_arrow_down.png
Normal file
BIN
src/icons/bullet_arrow_down.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 201 B |
BIN
src/icons/bullet_arrow_up.png
Normal file
BIN
src/icons/bullet_arrow_up.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 201 B |
@@ -33,6 +33,8 @@
|
||||
<file alias="save_table">table_save.png</file>
|
||||
<file alias="run_line">resultset_last.png</file>
|
||||
<file alias="log_dock">layout_sidebar.png</file>
|
||||
<file alias="down">bullet_arrow_down.png</file>
|
||||
<file alias="up">bullet_arrow_up.png</file>
|
||||
</qresource>
|
||||
<qresource prefix="/oldimages">
|
||||
<file alias="128">oldimages/128.png</file>
|
||||
|
||||
@@ -518,7 +518,7 @@ bool DBBrowserDB::addColumn(const QString& tablename, const sqlb::FieldPtr& fiel
|
||||
return result;
|
||||
}
|
||||
|
||||
bool DBBrowserDB::renameColumn(const QString& tablename, const QString& name, sqlb::FieldPtr to)
|
||||
bool DBBrowserDB::renameColumn(const QString& tablename, const QString& name, sqlb::FieldPtr to, int move)
|
||||
{
|
||||
// NOTE: This function is working around the incomplete ALTER TABLE command in SQLite.
|
||||
// If SQLite should fully support this command one day, this entire
|
||||
@@ -574,9 +574,20 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const QString& name, sq
|
||||
select_cols.chop(1); // remove last comma
|
||||
} else {
|
||||
// We want to modify it
|
||||
newSchema.setField(newSchema.findField(name), to);
|
||||
|
||||
select_cols = "*";
|
||||
// Move field
|
||||
int index = newSchema.findField(name);
|
||||
sqlb::FieldPtr temp = newSchema.fields().at(index);
|
||||
newSchema.setField(index, newSchema.fields().at(index + move));
|
||||
newSchema.setField(index + move, temp);
|
||||
|
||||
// Get names of fields to select from old table now - after the field has been moved and before it might be renamed
|
||||
for(int i=0;i<newSchema.fields().count();++i)
|
||||
select_cols.append(QString("`%1`,").arg(newSchema.fields().at(i)->name()));
|
||||
select_cols.chop(1); // remove last comma
|
||||
|
||||
// Modify field
|
||||
newSchema.setField(index + move, to);
|
||||
}
|
||||
|
||||
// Create the new table
|
||||
|
||||
@@ -79,9 +79,10 @@ public:
|
||||
* @param tablename Specifies the table name
|
||||
* @param name Name of the column to edit
|
||||
* @param to The new field definition with changed name, type or the like. If Null-Pointer is given the column is dropped.
|
||||
* @param move Set this to a value != 0 to move the new column to a different position
|
||||
* @return true if renaming was successfull, false if not. In the latter case also lastErrorMessage is set
|
||||
*/
|
||||
bool renameColumn(const QString& tablename, const QString& name, sqlb::FieldPtr to);
|
||||
bool renameColumn(const QString& tablename, const QString& name, sqlb::FieldPtr to, int move = 0);
|
||||
|
||||
QStringList getTableFields(const QString & tablename) const;
|
||||
QStringList getBrowsableObjectNames() const;
|
||||
|
||||
Reference in New Issue
Block a user