Support modifying the column list of a constraint in Edit Table dialog

This adds support for modifying the columns a constraint is applied on
in the Constraints tab of the Edit Table dialog. This is a major step
towards full constraint editing capabilities (adding new constraints,
modifying constraint type, and editing constraint type-dependent
parameters are the other missing pieces).

The reasoning behind the popup dialog is to not introduce another full
dialog on top of the Edit Table dialog. After opening the Edit Table
dialog, then switching to the Constraints tab, then opening another
modal dialog, I felt like losing track of what I am currently editing.
The popup dialog is certainly not great but feels a bit less intrusive.
Any suggestions regarding this are appreciated.
This commit is contained in:
Martin Kleusberg
2019-07-27 18:26:01 +02:00
parent 3f60142abc
commit 9d654a19ba
6 changed files with 560 additions and 3 deletions

View File

@@ -175,6 +175,7 @@ set(SQLB_MOC_HDR
src/CondFormat.h
src/RunSql.h
src/ProxyDialog.h
src/SelectItemsPopup.h
)
set(SQLB_SRC
@@ -230,6 +231,7 @@ set(SQLB_SRC
src/RunSql.cpp
src/ProxyDialog.cpp
src/IconCache.cpp
src/SelectItemsPopup.cpp
)
set(SQLB_FORMS
@@ -254,6 +256,7 @@ set(SQLB_FORMS
src/FileExtensionManager.ui
src/CondFormatManager.ui
src/ProxyDialog.ui
src/SelectItemsPopup.ui
)
set(SQLB_RESOURCES

View File

@@ -4,6 +4,7 @@
#include "ui_EditTableDialog.h"
#include "sqlitetablemodel.h"
#include "sqlitedb.h"
#include "SelectItemsPopup.h"
#include <QMessageBox>
#include <QPushButton>
@@ -81,6 +82,45 @@ EditTableDialog::EditTableDialog(DBBrowserDB& db, const sqlb::ObjectIdentifier&
ui->editTableName->setText(QString::fromStdString(curTable.name()));
updateColumnWidth();
// Allow editing of constraint columns by double clicking the columns column of the constraints table
connect(ui->tableConstraints, &QTableWidget::itemDoubleClicked, [this](QTableWidgetItem* item) {
// Check whether the double clicked item is in the columns column
if(item->column() == kConstraintColumns)
{
sqlb::ConstraintPtr constraint = ui->tableConstraints->item(item->row(), kConstraintName)->data(Qt::UserRole).value<sqlb::ConstraintPtr>();
// Do not allow editing the columns list of a CHECK constraint because CHECK constraints are independent of column lists
if(constraint->type() == sqlb::Constraint::CheckConstraintType)
return;
// Show the select items popup dialog
SelectItemsPopup* dialog = new SelectItemsPopup(m_table.fieldNames(), item->data(Qt::UserRole).value<sqlb::StringVector>(), this);
QRect item_rect = ui->tableConstraints->visualItemRect(item);
dialog->move(ui->tableConstraints->mapToGlobal(QPoint(ui->tableConstraints->x() + item_rect.x(),
ui->tableConstraints->y() + item_rect.y() + item_rect.height() / 2)));
dialog->show();
// When clicking the Apply button in the popup dialog, save the new columns list
connect(dialog, &SelectItemsPopup::accepted, [this, dialog, item, constraint]() {
// Check if column selection changed at all
sqlb::StringVector columns_before = ui->tableConstraints->item(item->row(), kConstraintColumns)->data(Qt::UserRole).value<sqlb::StringVector>();
sqlb::StringVector columns_after = dialog->selectedItems();
if(columns_before != columns_after)
{
// Remove the constraint with the old columns and add a new one with the new columns
m_table.removeConstraint(columns_before, constraint);
m_table.addConstraint(columns_after, constraint);
// Update the UI
populateFields();
populateConstraints();
updateSqlText();
}
});
}
});
// (De-)activate fields
checkInput();
}

136
src/SelectItemsPopup.cpp Normal file
View File

@@ -0,0 +1,136 @@
#include "SelectItemsPopup.h"
#include "ui_SelectItemsPopup.h"
#include <QPushButton>
SelectItemsPopup::SelectItemsPopup(const std::vector<std::string>& available, const std::vector<std::string>& selected, QWidget* parent) :
QDialog(parent),
ui(new Ui::SelectItemsPopup)
{
ui->setupUi(this);
setWindowFlags(Qt::Popup);
// Load initial items
for(const auto& s : available)
{
if(std::find(selected.begin(), selected.end(), s) == selected.end())
new QListWidgetItem(QString::fromStdString(s), ui->listAvailable);
}
for(const auto& s : selected)
new QListWidgetItem(QString::fromStdString(s), ui->listSelected);
}
SelectItemsPopup::~SelectItemsPopup()
{
delete ui;
}
std::vector<std::string> SelectItemsPopup::selectedItems() const
{
std::vector<std::string> result;
for(int i=0;i<ui->listSelected->count();i++)
result.push_back(ui->listSelected->item(i)->text().toStdString());
return result;
}
void SelectItemsPopup::selectItem(const QModelIndex& idx)
{
// Get currently selected iitem if none was provided
QListWidgetItem* item;
if(idx.isValid())
item = ui->listAvailable->item(idx.row());
else
item = ui->listAvailable->currentItem();
if(!item)
return;
// Add it to the selected items list
new QListWidgetItem(item->text(), ui->listSelected);
// Remove it from available items list
delete item;
}
void SelectItemsPopup::unselectItem(const QModelIndex& idx)
{
// Get currently selected iitem if none was provided
QListWidgetItem* item;
if(idx.isValid())
item = ui->listSelected->item(idx.row());
else
item = ui->listSelected->currentItem();
if(!item)
return;
// Add it to the available items list
new QListWidgetItem(item->text(), ui->listAvailable);
// Remove it from selected items list
delete item;
}
void SelectItemsPopup::resizeEvent(QResizeEvent*)
{
// We modify the shape of the dialog to add an arrow shaped edge. See the ascii art image below for details. The edges
// are numbered, their order is the same as in the polygon definition.
/*
/3\
/ \
1---2 4--------5
| |
| |
7------------------6
*/
const int arrow_height = ui->spacer->geometry().height();
const int arrow_width = arrow_height * 3;
const int arrow_position_div = 5;
QPolygon poly;
poly << QPoint(rect().x(), rect().y() + arrow_height)
<< QPoint(rect().x() + rect().width() / arrow_position_div - arrow_width / 2, rect().y() + arrow_height)
<< QPoint(rect().x() + rect().width() / arrow_position_div, rect().y())
<< QPoint(rect().x() + rect().width() / arrow_position_div + arrow_width / 2, rect().y() + arrow_height)
<< QPoint(rect().x() + rect().width(), rect().y() + arrow_height)
<< QPoint(rect().x() + rect().width(), rect().y() + rect().height())
<< QPoint(rect().x(), rect().y() + rect().height());
setMask(QRegion(poly));
}
void SelectItemsPopup::buttonBoxClicked(QAbstractButton* button)
{
if(button == ui->buttonBox->button(QDialogButtonBox::Apply))
accept();
}
void SelectItemsPopup::moveItemUp()
{
moveCurrentItem(false);
}
void SelectItemsPopup::moveItemDown()
{
moveCurrentItem(true);
}
void SelectItemsPopup::moveCurrentItem(bool down)
{
// Get current row number and calculate row number after the movement. Check the values
int currentRow = ui->listSelected->currentRow();
if(currentRow == -1)
return;
int newRow = currentRow + (down ? 1 : -1);
if(newRow < 0)
return;
if(newRow >= ui->listSelected->count())
return;
// Swap items
ui->listSelected->insertItem(newRow, ui->listSelected->takeItem(currentRow));
// Select old item at new position
ui->listSelected->setCurrentRow(newRow);
}

44
src/SelectItemsPopup.h Normal file
View File

@@ -0,0 +1,44 @@
#ifndef SELECTITEMS_H
#define SELECTITEMS_H
#include <QDialog>
#include <QModelIndex>
#include <string>
#include <vector>
class QAbstractButton;
namespace Ui {
class SelectItemsPopup;
}
class SelectItemsPopup : public QDialog
{
Q_OBJECT
public:
explicit SelectItemsPopup(const std::vector<std::string>& available, const std::vector<std::string>& selected = {}, QWidget* parent = nullptr);
~SelectItemsPopup();
std::vector<std::string> selectedItems() const;
private slots:
void buttonBoxClicked(QAbstractButton* button);
void selectItem(const QModelIndex& idx = QModelIndex());
void unselectItem(const QModelIndex& idx = QModelIndex());
void moveItemUp();
void moveItemDown();
protected:
void resizeEvent(QResizeEvent* ev);
private:
Ui::SelectItemsPopup* ui;
void moveCurrentItem(bool down);
};
#endif

331
src/SelectItemsPopup.ui Normal file
View File

@@ -0,0 +1,331 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SelectItemsPopup</class>
<widget class="QDialog" name="SelectItemsPopup">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>537</width>
<height>290</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<spacer name="spacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>15</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>A&amp;vailable</string>
</property>
<property name="buddy">
<cstring>listAvailable</cstring>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="listAvailable">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="buttonSelect">
<property name="arrowType">
<enum>Qt::RightArrow</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="buttonUnselect">
<property name="arrowType">
<enum>Qt::LeftArrow</enum>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Sele&amp;cted</string>
</property>
<property name="buddy">
<cstring>listSelected</cstring>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="listSelected">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="buttonUp">
<property name="arrowType">
<enum>Qt::UpArrow</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="buttonDown">
<property name="arrowType">
<enum>Qt::DownArrow</enum>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel</set>
</property>
</widget>
</item>
</layout>
</widget>
<tabstops>
<tabstop>listAvailable</tabstop>
<tabstop>listSelected</tabstop>
<tabstop>buttonSelect</tabstop>
<tabstop>buttonUnselect</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
<sender>buttonSelect</sender>
<signal>clicked()</signal>
<receiver>SelectItemsPopup</receiver>
<slot>selectItem()</slot>
<hints>
<hint type="sourcelabel">
<x>263</x>
<y>115</y>
</hint>
<hint type="destinationlabel">
<x>2</x>
<y>203</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonUnselect</sender>
<signal>clicked()</signal>
<receiver>SelectItemsPopup</receiver>
<slot>unselectItem()</slot>
<hints>
<hint type="sourcelabel">
<x>257</x>
<y>159</y>
</hint>
<hint type="destinationlabel">
<x>515</x>
<y>186</y>
</hint>
</hints>
</connection>
<connection>
<sender>listAvailable</sender>
<signal>doubleClicked(QModelIndex)</signal>
<receiver>SelectItemsPopup</receiver>
<slot>selectItem(QModelIndex)</slot>
<hints>
<hint type="sourcelabel">
<x>124</x>
<y>45</y>
</hint>
<hint type="destinationlabel">
<x>115</x>
<y>0</y>
</hint>
</hints>
</connection>
<connection>
<sender>listSelected</sender>
<signal>doubleClicked(QModelIndex)</signal>
<receiver>SelectItemsPopup</receiver>
<slot>unselectItem(QModelIndex)</slot>
<hints>
<hint type="sourcelabel">
<x>377</x>
<y>96</y>
</hint>
<hint type="destinationlabel">
<x>383</x>
<y>4</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>SelectItemsPopup</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>262</x>
<y>258</y>
</hint>
<hint type="destinationlabel">
<x>262</x>
<y>140</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>clicked(QAbstractButton*)</signal>
<receiver>SelectItemsPopup</receiver>
<slot>buttonBoxClicked(QAbstractButton*)</slot>
<hints>
<hint type="sourcelabel">
<x>262</x>
<y>258</y>
</hint>
<hint type="destinationlabel">
<x>262</x>
<y>140</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonDown</sender>
<signal>clicked()</signal>
<receiver>SelectItemsPopup</receiver>
<slot>moveItemDown()</slot>
<hints>
<hint type="sourcelabel">
<x>513</x>
<y>153</y>
</hint>
<hint type="destinationlabel">
<x>268</x>
<y>144</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonUp</sender>
<signal>clicked()</signal>
<receiver>SelectItemsPopup</receiver>
<slot>moveItemUp()</slot>
<hints>
<hint type="sourcelabel">
<x>513</x>
<y>124</y>
</hint>
<hint type="destinationlabel">
<x>268</x>
<y>144</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>selectItem(QModelIndex)</slot>
<slot>unselectItem(QModelIndex)</slot>
<slot>selectItem()</slot>
<slot>unselectItem()</slot>
<slot>buttonBoxClicked(QAbstractButton*)</slot>
<slot>moveItemUp()</slot>
<slot>moveItemDown()</slot>
</slots>
</ui>

View File

@@ -76,7 +76,8 @@ HEADERS += \
RunSql.h \
sql/ObjectIdentifier.h \
ProxyDialog.h \
IconCache.h
IconCache.h \
SelectItemsPopup.h
SOURCES += \
sqlitedb.cpp \
@@ -129,7 +130,8 @@ SOURCES += \
RunSql.cpp \
sql/ObjectIdentifier.cpp \
ProxyDialog.cpp \
IconCache.cpp
IconCache.cpp \
SelectItemsPopup.cpp
RESOURCES += icons/icons.qrc \
translations/flags/flags.qrc \
@@ -158,7 +160,8 @@ FORMS += \
FindReplaceDialog.ui \
FileExtensionManager.ui \
CondFormatManager.ui \
ProxyDialog.ui
ProxyDialog.ui \
SelectItemsPopup.ui
TRANSLATIONS += \
translations/sqlb_ar_SA.ts \