mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 02:50:46 -06:00
Rename dialog files and classes
Rename the files and classes for the dialogs to share all the same naming pattern. This should make navigation in the code a bit easier. Do not include dialogs not rewritten yet; they'll be edited as they are redesigned.
This commit is contained in:
200
src/EditTableDialog.cpp
Normal file
200
src/EditTableDialog.cpp
Normal file
@@ -0,0 +1,200 @@
|
||||
#include "EditTableDialog.h"
|
||||
#include "ui_EditTableDialog.h"
|
||||
#include "EditFieldDialog.h"
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
#include "sqlitedb.h"
|
||||
|
||||
EditTableDialog::EditTableDialog(QWidget* parent)
|
||||
: QDialog(parent),
|
||||
modified(false),
|
||||
pdb(0),
|
||||
ui(new Ui::EditTableDialog)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
}
|
||||
|
||||
EditTableDialog::~EditTableDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void EditTableDialog::setActiveTable(DBBrowserDB * thedb, QString tableName)
|
||||
{
|
||||
// Set variables
|
||||
pdb = thedb;
|
||||
curTable = tableName;
|
||||
|
||||
// Editing an existing table?
|
||||
if(curTable != "")
|
||||
{
|
||||
// Existing table, so load and set the current layout
|
||||
populateFields();
|
||||
|
||||
// And create a savepoint
|
||||
pdb->executeSQL(QString("SAVEPOINT edittable_%1_save;").arg(curTable));
|
||||
}
|
||||
|
||||
// Update UI
|
||||
ui->editTableName->setText(curTable);
|
||||
checkInput();
|
||||
}
|
||||
|
||||
void EditTableDialog::populateFields()
|
||||
{
|
||||
if (!pdb) return;
|
||||
|
||||
//make sure we are not using cached information
|
||||
pdb->updateSchema();
|
||||
|
||||
fields= pdb->getTableFields(curTable);
|
||||
types= pdb->getTableTypes(curTable);
|
||||
ui->treeWidget->model()->removeRows(0, ui->treeWidget->model()->rowCount());
|
||||
QStringList::Iterator tt = types.begin();
|
||||
for ( QStringList::Iterator ct = fields.begin(); ct != fields.end(); ++ct ) {
|
||||
QTreeWidgetItem *fldItem = new QTreeWidgetItem();
|
||||
fldItem->setText( 0, *ct );
|
||||
fldItem->setText( 1, *tt );
|
||||
ui->treeWidget->addTopLevelItem(fldItem);
|
||||
++tt;
|
||||
}
|
||||
}
|
||||
|
||||
void EditTableDialog::accept()
|
||||
{
|
||||
// Are we editing an already existing table or designing a new one? In the first case there is a table name set,
|
||||
// in the latter the current table name is empty
|
||||
if(curTable == "")
|
||||
{
|
||||
// Creation of new table
|
||||
|
||||
// Prepare creation of the table
|
||||
QList<DBBrowserField> tbl_structure;
|
||||
for(int i=0;i<ui->treeWidget->topLevelItemCount();i++)
|
||||
tbl_structure.push_back(DBBrowserField(ui->treeWidget->topLevelItem(i)->text(0), ui->treeWidget->topLevelItem(i)->text(1)));
|
||||
|
||||
// Create table
|
||||
if(!pdb->createTable(ui->editTableName->text(), tbl_structure))
|
||||
{
|
||||
QMessageBox::warning(this, QApplication::applicationName(), QString("Error creating table. Message from database engine:\n%1").arg(pdb->lastErrorMessage));
|
||||
return;
|
||||
}
|
||||
modified = true;
|
||||
} else {
|
||||
// Editing of old table
|
||||
|
||||
// Rename table if necessary
|
||||
if(ui->editTableName->text() != curTable)
|
||||
{
|
||||
QApplication::setOverrideCursor( Qt::WaitCursor ); // this might take time
|
||||
modified = true;
|
||||
if(!pdb->renameTable(curTable, ui->editTableName->text()))
|
||||
{
|
||||
QApplication::restoreOverrideCursor();
|
||||
QMessageBox::warning(this, QApplication::applicationName(), pdb->lastErrorMessage);
|
||||
return;
|
||||
} else {
|
||||
QApplication::restoreOverrideCursor();
|
||||
}
|
||||
}
|
||||
|
||||
// Release the savepoint
|
||||
pdb->executeSQL(QString("RELEASE SAVEPOINT edittable_%1_save;").arg(curTable));
|
||||
}
|
||||
|
||||
QDialog::accept();
|
||||
}
|
||||
|
||||
void EditTableDialog::reject()
|
||||
{
|
||||
// Have we been in the process of editing an old table?
|
||||
if(curTable != "")
|
||||
{
|
||||
// Then rollback to our savepoint
|
||||
pdb->executeSQL(QString("ROLLBACK TO SAVEPOINT edittable_%1_save;").arg(curTable));
|
||||
}
|
||||
|
||||
QDialog::reject();
|
||||
}
|
||||
|
||||
void EditTableDialog::checkInput()
|
||||
{
|
||||
ui->editTableName->setText(ui->editTableName->text().trimmed());
|
||||
bool valid = true;
|
||||
if(ui->editTableName->text().isEmpty() || ui->editTableName->text().contains(" "))
|
||||
valid = false;
|
||||
if(ui->treeWidget->topLevelItemCount() == 0)
|
||||
valid = false;
|
||||
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
|
||||
}
|
||||
|
||||
void EditTableDialog::editField()
|
||||
{
|
||||
if(!ui->treeWidget->currentItem())
|
||||
return;
|
||||
|
||||
// Show the edit dialog
|
||||
QTreeWidgetItem *item = ui->treeWidget->currentItem();
|
||||
EditFieldDialog dialog(this);
|
||||
dialog.setInitialValues(pdb, curTable == "", curTable, item->text(0), item->text(1));
|
||||
if(dialog.exec())
|
||||
{
|
||||
modified = true;
|
||||
item->setText(0, dialog.field_name);
|
||||
item->setText(1, dialog.field_type);
|
||||
}
|
||||
}
|
||||
|
||||
void EditTableDialog::addField()
|
||||
{
|
||||
EditFieldDialog dialog(this);
|
||||
dialog.setInitialValues(pdb, true, curTable, QString(""), QString(""));
|
||||
if(dialog.exec())
|
||||
{
|
||||
QTreeWidgetItem *tbitem = new QTreeWidgetItem(ui->treeWidget);
|
||||
tbitem->setText(0, dialog.field_name);
|
||||
tbitem->setText(1, dialog.field_type);
|
||||
modified = true;
|
||||
ui->treeWidget->addTopLevelItem(tbitem);
|
||||
checkInput();
|
||||
}
|
||||
}
|
||||
|
||||
void EditTableDialog::removeField()
|
||||
{
|
||||
// Is there any item selected to delete?
|
||||
if(!ui->treeWidget->currentItem())
|
||||
return;
|
||||
|
||||
// Are we creating a new table or editing an old one?
|
||||
if(curTable == "")
|
||||
{
|
||||
// Creating a new one
|
||||
|
||||
// Just delete that item. At this point there is no DB table to edit or data to be lost anyway
|
||||
delete ui->treeWidget->currentItem();
|
||||
} else {
|
||||
// Editing an old one
|
||||
|
||||
// Ask user wether he really wants to delete that column
|
||||
QString msg = tr("Are you sure you want to delete the field '%1'?\nAll data currently stored in this field will be lost.").arg(ui->treeWidget->currentItem()->text(0));
|
||||
if(QMessageBox::warning(this, QApplication::applicationName(), msg, QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape) == QMessageBox::Yes)
|
||||
{
|
||||
if(!pdb->dropColumn(curTable, ui->treeWidget->currentItem()->text(0)))
|
||||
{
|
||||
QMessageBox::warning(0, QApplication::applicationName(), pdb->lastErrorMessage);
|
||||
} else {
|
||||
delete ui->treeWidget->currentItem();
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
checkInput();
|
||||
}
|
||||
|
||||
void EditTableDialog::fieldSelectionChanged()
|
||||
{
|
||||
ui->renameFieldButton->setEnabled(ui->treeWidget->selectionModel()->hasSelection());
|
||||
ui->removeFieldButton->setEnabled(ui->treeWidget->selectionModel()->hasSelection());
|
||||
}
|
||||
Reference in New Issue
Block a user