66#include " sqlitedb.h"
77#include " SelectItemsPopup.h"
88
9- #include < QMessageBox>
10- #include < QPushButton>
119#include < QComboBox>
1210#include < QDateTime>
1311#include < QKeyEvent>
12+ #include < QMenu>
13+ #include < QMessageBox>
14+ #include < QPushButton>
15+
1416#include < algorithm>
1517
1618Q_DECLARE_METATYPE (sqlb::ConstraintPtr)
@@ -30,13 +32,22 @@ EditTableDialog::EditTableDialog(DBBrowserDB& db, const sqlb::ObjectIdentifier&
3032 connect (ui->treeWidget , &QTreeWidget::itemChanged, this , &EditTableDialog::fieldItemChanged);
3133 connect (ui->tableConstraints , &QTableWidget::itemChanged, this , &EditTableDialog::constraintItemChanged);
3234
33- // TODO Remove this once we have added support for this button
34- ui->buttonAddConstraint ->setVisible (false );
35-
3635 // Set item delegate for foreign key column
3736 m_fkEditorDelegate = new ForeignKeyEditorDelegate (db, m_table, this );
3837 ui->treeWidget ->setItemDelegateForColumn (kForeignKey , m_fkEditorDelegate);
3938
39+ // Set up popup menu for adding constraints
40+ QMenu* constraint_menu = new QMenu (this );
41+ constraint_menu->addAction (ui->actionAddPrimaryKey );
42+ constraint_menu->addAction (ui->actionAddForeignKey );
43+ constraint_menu->addAction (ui->actionAddUniqueConstraint );
44+ constraint_menu->addAction (ui->actionAddCheckConstraint );
45+ connect (ui->actionAddPrimaryKey , &QAction::triggered, [this ]() { addConstraint (sqlb::Constraint::PrimaryKeyConstraintType); });
46+ connect (ui->actionAddForeignKey , &QAction::triggered, [this ]() { addConstraint (sqlb::Constraint::ForeignKeyConstraintType); });
47+ connect (ui->actionAddUniqueConstraint , &QAction::triggered, [this ]() { addConstraint (sqlb::Constraint::UniqueConstraintType); });
48+ connect (ui->actionAddCheckConstraint , &QAction::triggered, [this ]() { addConstraint (sqlb::Constraint::CheckConstraintType); });
49+ ui->buttonAddConstraint ->setMenu (constraint_menu);
50+
4051 // Editing an existing table?
4152 if (m_bNewTable == false )
4253 {
@@ -241,38 +252,21 @@ void EditTableDialog::populateConstraints()
241252 // Handle change of constraint type. Effectively this means removing the old constraint and replacing it by an entirely new one.
242253 // Only the column list and the name can be migrated to the new constraint.
243254
244- // Create new constraint depending on selected type
245- sqlb::ConstraintPtr new_constraint;
246- switch (index)
255+ // Make sure there is only one primary key at a time
256+ if (index == 0 && !m_table.primaryKey ().empty ())
247257 {
248- case 0 :
249- // Make sure there is only one primary key at a time
250- if (!m_table.primaryKey ().empty ())
251- {
252- QMessageBox::warning (this , qApp->applicationName (), tr (" There can only be one primary key for each table. Please modify the existing primary "
253- " key instead." ));
254-
255- // Set combo box back to original constraint type
256- type->blockSignals (true );
257- type->setCurrentIndex (constraint->type ());
258- type->blockSignals (false );
259- return ;
260- }
258+ QMessageBox::warning (this , qApp->applicationName (), tr (" There can only be one primary key for each table. Please modify the existing primary "
259+ " key instead." ));
261260
262- new_constraint = sqlb::ConstraintPtr (new sqlb::PrimaryKeyConstraint ());
263- break ;
264- case 1 :
265- new_constraint = sqlb::ConstraintPtr (new sqlb::UniqueConstraint ());
266- break ;
267- case 2 :
268- new_constraint = sqlb::ConstraintPtr (new sqlb::ForeignKeyClause ());
269- break ;
270- case 3 :
271- new_constraint = sqlb::ConstraintPtr (new sqlb::CheckConstraint ());
272- break ;
273- default :
261+ // Set combo box back to original constraint type
262+ type->blockSignals (true );
263+ type->setCurrentIndex (constraint->type ());
264+ type->blockSignals (false );
274265 return ;
275266 }
267+
268+ // Create new constraint depending on selected type
269+ sqlb::ConstraintPtr new_constraint = sqlb::Constraint::makeConstraint (static_cast <sqlb::Constraint::ConstraintTypes>(index));
276270 new_constraint->setName (constraint->name ());
277271 new_constraint->column_list = constraint->column_list ;
278272
@@ -907,3 +901,26 @@ void EditTableDialog::removeConstraint()
907901 updateSqlText ();
908902 populateFields ();
909903}
904+
905+ void EditTableDialog::addConstraint (sqlb::Constraint::ConstraintTypes type)
906+ {
907+ // There can only be one primary key
908+ if (type == sqlb::Constraint::PrimaryKeyConstraintType)
909+ {
910+ if (!m_table.primaryKey ().empty ())
911+ {
912+ QMessageBox::information (this , qApp->applicationName (), tr (" There can only be one primary key for each table. Please modify the existing primary "
913+ " key instead." ));
914+ return ;
915+ }
916+ }
917+
918+ // Create new constraint
919+ sqlb::ConstraintPtr constraint = sqlb::Constraint::makeConstraint (type);
920+ m_table.addConstraint (constraint);
921+
922+ // Update SQL and view
923+ populateFields ();
924+ populateConstraints ();
925+ updateSqlText ();
926+ }
0 commit comments