Issue #530: constraints on table prevents new record being added (#1477)

* Issue #530: constraints on table prevents new record being added

This adds a new dialog for adding records to a table. The current approach
is broken for several cases where foreign keys and check constraints are
impeding the insertion of an empty record. With the dialog, the user can
inspect the constraints (tooltip) and type of fields and add values
consistent with the requirements. The data is only inserted when the user
presses the Save button.

The dialog is modelled after the Edit Table or Edit Index dialog. An upper
frame allows entering the data using widgets. The lower frame previews the
SQL statement that will be used.

The old approach for adding records is still accessible pressing Tab on
the last cell of the table.

* Fix build problem introduced in previous commit on this branch

* Dialog as fallback for failure after empty row insertion and read only text

The insertion of an empty row is always tried. When it fails due to
constraints and foreign keys, the Add Record Dialog is open so the user
can enter values for the new record considering the constraints.

When the table has not constraints, or the row insertion provides valid
values, the user is still able to insert rows using the simple approach.

SQL preview in dialog is now read-only.

* Visual improvements for the Add Record dialog

QLineEdit as item delegate for the value, so it is more visible that we
are supposed to edit the value.

Remove last end-of-line in tool-tip.

* Improvements in the "Add Record" dialog

Display of NULL values using DisplayRole (no focus) or place holder text
(when focus).

Set value to NULL through a context menu and shortcut in the value line
edit.

Take text type affinity into account for quoting or not entered numbers.
New isType and affinity functions in sqlitetypes.

Clarify wording of constraints in tooltip for value. Added the same tooltip
for the type.

Escape quotes inside string values.

Removed unused parameters warnings.

Other wording or code improvements based on the pull-request review: #1477.

* User access to the Add Record dialog

The Add Record dialog is now accessible for the user. The New Record button
is converted to a QToolButton and a new pop-up menu is added to it for
invoking the in-line table insertion (New Record) or the Add Record dialog
(Insert Values...). What's This information for the button updated.
This commit is contained in:
Manuel
2018-08-27 21:35:09 +02:00
committed by Martin Kleusberg
parent d5a049062d
commit ce032d95e6
10 changed files with 670 additions and 4 deletions

View File

@@ -5,6 +5,7 @@
#include "EditIndexDialog.h"
#include "AboutDialog.h"
#include "EditTableDialog.h"
#include "AddRecordDialog.h"
#include "ImportCsvDialog.h"
#include "ExportDataDialog.h"
#include "Settings.h"
@@ -180,6 +181,11 @@ void MainWindow::init()
popupOpenDbMenu->addAction(ui->fileOpenReadOnlyAction);
ui->fileOpenActionPopup->setMenu(popupOpenDbMenu);
popupNewRecordMenu = new QMenu(this);
popupNewRecordMenu->addAction(ui->newRecordAction);
popupNewRecordMenu->addAction(ui->insertValuesAction);
ui->buttonNewRecord->setMenu(popupNewRecordMenu);
popupSaveSqlFileMenu = new QMenu(this);
popupSaveSqlFileMenu->addAction(ui->actionSqlSaveFile);
popupSaveSqlFileMenu->addAction(ui->actionSqlSaveFileAs);
@@ -720,10 +726,19 @@ void MainWindow::addRecord()
{
selectTableLine(row);
} else {
QMessageBox::warning(this, QApplication::applicationName(), tr("Error adding record:\n") + db.lastError());
// Error inserting empty row.
// User has to provide values acomplishing the constraints. Open Add Record Dialog.
insertValues();
}
}
void MainWindow::insertValues()
{
AddRecordDialog dialog(db, currentlyBrowsedTableName(), this);
if (dialog.exec())
populateTable();
}
void MainWindow::deleteRecord()
{
if(ui->dataTable->selectionModel()->hasSelection())