Skip to content

Commit

Permalink
#5083 Improved cell editing behavior, so entering edition with Enter …
Browse files Browse the repository at this point in the history
…key or mouse Double-Click no longer preselects whole value.
  • Loading branch information
pawelsalawa committed Nov 8, 2024
1 parent 3a28f63 commit 2aeadaa
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 9 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- BUGFIX: #4949 Added extra calls for cleaning up WAL files upon application exit.
- BUGFIX: #4934 Fixed copying (drag&drop or copy&paste) tables between databases when a column in the table requires quoting due to being a keyword.
- BUGFIX: #5076 Fixed unchecking Named Constraint option in constraint configuration (thanks to @marcpley).
- BUGFIX: #5083 Improved cell editing behavior, so entering edition with Enter key or mouse Double-Click no longer preselects whole value.

### 3.4.4
- ADDED: #3488 SQLite ICU extension is boundled into binary packages (thanks to @tuffnatty).
Expand Down
2 changes: 0 additions & 2 deletions SQLiteStudio3/guiSQLiteStudio/datagrid/fkcombobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ void FkComboBox::setValue(const QVariant& value)
bool doExecQuery = (sourceValue != value || comboModel->getQuery().isNull());
sourceValue = value;
setCurrentText(value.toString());
if (!value.isNull() && isEditable())
lineEdit()->selectAll();

if (doExecQuery)
{
Expand Down
16 changes: 16 additions & 0 deletions SQLiteStudio3/guiSQLiteStudio/datagrid/sqlqueryitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,19 @@ QVariant SqlQueryItem::data(int role) const

return QStandardItem::data(role);
}

void SqlQueryItem::resetInitialFocusSelection()
{
QStandardItem::setData(QVariant(), DataRole::EDIT_SKIP_INITIAL_SELECT);

}

void SqlQueryItem::skipInitialFocusSelection()
{
QStandardItem::setData(true, DataRole::EDIT_SKIP_INITIAL_SELECT);
}

bool SqlQueryItem::shoulSkipInitialFocusSelection() const
{
return QStandardItem::data(DataRole::EDIT_SKIP_INITIAL_SELECT).toBool();
}
7 changes: 6 additions & 1 deletion SQLiteStudio3/guiSQLiteStudio/datagrid/sqlqueryitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class GUI_API_EXPORT SqlQueryItem : public QObject, public QStandardItem
DELETED = 1007,
OLD_VALUE = 1008,
JUST_INSERTED_WITHOUT_ROWID = 1009,
COMMITTING_ERROR_MESSAGE = 1010
COMMITTING_ERROR_MESSAGE = 1010,
EDIT_SKIP_INITIAL_SELECT = 1011 // to prevent content selection initially when editing with double-click
};
};

Expand Down Expand Up @@ -72,6 +73,10 @@ class GUI_API_EXPORT SqlQueryItem : public QObject, public QStandardItem
void setData(const QVariant& value, int role = Qt::UserRole + 1);
QVariant data(int role = Qt::UserRole + 1) const;

void resetInitialFocusSelection();
void skipInitialFocusSelection();
bool shoulSkipInitialFocusSelection() const;

private:
QVariant adjustVariantType(const QVariant& value);
QString getToolTip() const;
Expand Down
20 changes: 16 additions & 4 deletions SQLiteStudio3/guiSQLiteStudio/datagrid/sqlqueryitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "common/utils_sql.h"
#include "fkcombobox.h"
#include "schemaresolver.h"
#include "sqlqueryitemlineedit.h"
#include <QHeaderView>
#include <QPainter>
#include <QEvent>
Expand Down Expand Up @@ -74,7 +75,18 @@ QWidget* SqlQueryItemDelegate::createEditor(QWidget* parent, const QStyleOptionV
if (!item->getColumn()->getFkConstraints().isEmpty())
return getFkEditor(item, parent, model);

return getEditor(item->getValue().userType(), parent);
return getEditor(item->getValue().userType(), item->shoulSkipInitialFocusSelection(), parent);
}

void SqlQueryItemDelegate::destroyEditor(QWidget* editor, const QModelIndex& index) const
{
QStyledItemDelegate::destroyEditor(editor, index);
if (!index.isValid())
return;

const SqlQueryModel* model = dynamic_cast<const SqlQueryModel*>(index.model());
SqlQueryItem* item = model->itemFromIndex(index);
item->resetInitialFocusSelection();
}

QString SqlQueryItemDelegate::displayText(const QVariant& value, const QLocale& locale) const
Expand Down Expand Up @@ -216,10 +228,10 @@ SqlQueryItem* SqlQueryItemDelegate::getItem(const QModelIndex &index) const
return queryModel->itemFromIndex(index);
}

QWidget* SqlQueryItemDelegate::getEditor(int type, QWidget* parent) const
QWidget* SqlQueryItemDelegate::getEditor(int type, bool shouldSkipInitialSelection, QWidget* parent) const
{
UNUSED(type);
QLineEdit *editor = new QLineEdit(parent);
SqlQueryItemLineEdit *editor = new SqlQueryItemLineEdit(shouldSkipInitialSelection, parent);
editor->setMaxLength(std::numeric_limits<int>::max());
editor->setFrame(editor->style()->styleHint(QStyle::SH_ItemView_DrawDelegateFrame, 0, editor));
return editor;
Expand Down Expand Up @@ -320,7 +332,7 @@ QWidget* SqlQueryItemDelegate::getFkEditor(SqlQueryItem* item, QWidget* parent,
notifyWarn(tr("Foreign key for column %2 has more than %1 possible values. It's too much to display in drop down list. You need to edit value manually.")
.arg(FkComboBox::MAX_ROWS_FOR_FK).arg(item->getColumn()->column));

return getEditor(item->getValue().userType(), parent);
return getEditor(item->getValue().userType(), item->shoulSkipInitialFocusSelection(), parent);
}

if (rowCount == 0 && countingError && model->isStructureOutOfDate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class GUI_API_EXPORT SqlQueryItemDelegate : public QStyledItemDelegate

void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
void destroyEditor(QWidget *editor, const QModelIndex &index) const;
QString displayText(const QVariant & value, const QLocale & locale) const;
void setEditorData(QWidget * editor, const QModelIndex & index) const;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
Expand All @@ -39,7 +40,7 @@ class GUI_API_EXPORT SqlQueryItemDelegate : public QStyledItemDelegate
};

SqlQueryItem* getItem(const QModelIndex &index) const;
QWidget* getEditor(int type, QWidget* parent) const;
QWidget* getEditor(int type, bool shouldSkipInitialSelection, QWidget* parent) const;
QWidget* getFkEditor(SqlQueryItem* item, QWidget* parent, const SqlQueryModel *model) const;
void setEditorDataForLineEdit(QLineEdit* le, const QModelIndex& index) const;
void setEditorDataForFk(QComboBox* cb, const QModelIndex& index) const;
Expand Down
14 changes: 14 additions & 0 deletions SQLiteStudio3/guiSQLiteStudio/datagrid/sqlqueryitemlineedit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "sqlqueryitemlineedit.h"

SqlQueryItemLineEdit::SqlQueryItemLineEdit(bool shouldSkipInitialSelection, QWidget *parent)
: QLineEdit{parent}, shouldSkipInitialSelection(shouldSkipInitialSelection)
{

}

void SqlQueryItemLineEdit::focusInEvent(QFocusEvent* e)
{
QLineEdit::focusInEvent(e);
if (shouldSkipInitialSelection)
deselect();
}
20 changes: 20 additions & 0 deletions SQLiteStudio3/guiSQLiteStudio/datagrid/sqlqueryitemlineedit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef SQLQUERYITEMLINEEDIT_H
#define SQLQUERYITEMLINEEDIT_H

#include <QLineEdit>

class SqlQueryItemLineEdit : public QLineEdit
{
Q_OBJECT

public:
explicit SqlQueryItemLineEdit(bool shouldSkipInitialSelection, QWidget *parent = nullptr);

protected:
void focusInEvent(QFocusEvent *e);

private:
bool shouldSkipInitialSelection;
};

#endif // SQLQUERYITEMLINEEDIT_H
8 changes: 7 additions & 1 deletion SQLiteStudio3/guiSQLiteStudio/datagrid/sqlqueryview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ void SqlQueryView::init()
itemDelegate = new SqlQueryItemDelegate();
setItemDelegate(itemDelegate);
setMouseTracking(true);
// setEditTriggers(QAbstractItemView::AnyKeyPressed);
setEditTriggers(QAbstractItemView::AnyKeyPressed|QAbstractItemView::EditKeyPressed);

setContextMenuPolicy(Qt::CustomContextMenu);
Expand Down Expand Up @@ -303,6 +302,7 @@ void SqlQueryView::itemActivated(const QModelIndex& index)
if (!editInEditorIfNecessary(item))
return;

item->skipInitialFocusSelection();
edit(getCurrentIndex());
}

Expand Down Expand Up @@ -335,7 +335,13 @@ void SqlQueryView::editCurrent()
{
QModelIndex idx = getCurrentIndex();
if (idx.isValid())
{
SqlQueryItem* item = getModel()->itemFromIndex(idx);
if (item)
item->skipInitialFocusSelection();

edit(idx);
}
}

void SqlQueryView::toggleRowsHeightAdjustment(bool enabled)
Expand Down
2 changes: 2 additions & 0 deletions SQLiteStudio3/guiSQLiteStudio/guiSQLiteStudio.pro
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SOURCES +=\
common/mouseshortcut.cpp \
constraints/columngeneratedpanel.cpp \
datagrid/fkcombobox.cpp \
datagrid/sqlqueryitemlineedit.cpp \
extendedpalette.cpp \
mainwindow.cpp \
iconmanager.cpp \
Expand Down Expand Up @@ -199,6 +200,7 @@ HEADERS += mainwindow.h \
common/mouseshortcut.h \
constraints/columngeneratedpanel.h \
datagrid/fkcombobox.h \
datagrid/sqlqueryitemlineedit.h \
extendedpalette.h \
iconmanager.h \
dbtree/dbtreemodel.h \
Expand Down

0 comments on commit 2aeadaa

Please sign in to comment.