From 733179c2c30bce2a449b7b119b8e03ec7325d951 Mon Sep 17 00:00:00 2001 From: Oliver Giles Date: Sun, 25 Jan 2015 17:00:58 +0100 Subject: [PATCH] submit on click-away, improve tabbing behaviour --- src/sqlmodel.cpp | 3 +++ src/tableview.cpp | 36 ++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/sqlmodel.cpp b/src/sqlmodel.cpp index 7040986..d0c7a24 100644 --- a/src/sqlmodel.cpp +++ b/src/sqlmodel.cpp @@ -59,6 +59,9 @@ int SqlModel::rowCount(const QModelIndex &parent) const { } QVariant SqlModel::data(const QModelIndex &index, int role) const { + if(role == EditorTypeRole) + return SJCellEditDefault; + if(role == HeightMultiple) { if(dataSafe && index.column() < metadata.count()) { QString type = metadata.columnTypes.at(index.column()); diff --git a/src/tableview.cpp b/src/tableview.cpp index 4e0cf69..1687fd0 100644 --- a/src/tableview.cpp +++ b/src/tableview.cpp @@ -50,24 +50,40 @@ TableView::TableView(QWidget *parent) : } void TableView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint) { - QTreeView::closeEditor(editor, hint); - QModelIndex idx = currentIndex(); - QModelIndex nextIndex; + QModelIndex nextIndex = idx; + + // submit if the user clicks away + if(hint == QAbstractItemDelegate::NoHint) { + return QTreeView::closeEditor(editor, QAbstractItemDelegate::SubmitModelCache); + } - if(hint == QAbstractItemDelegate::EditNextItem) - nextIndex = model()->index(idx.row(), idx.column() + 1, idx.parent()); + // don't allow custom editors to be popped up during tabbing, + // it's more trouble than it's worth + auto canEditThroughTabbing = [&](QModelIndex i) { + return (model()->flags(i) & Qt::ItemIsEditable) && model()->data(i, EditorTypeRole) == SJCellEditDefault; + }; - if(hint == QAbstractItemDelegate::EditPreviousItem) - nextIndex = model()->index(idx.row(), idx.column() - 1, idx.parent()); + // find next editible cell if user tabs + if(hint == QAbstractItemDelegate::EditNextItem) do { + nextIndex = model()->index(nextIndex.row(), nextIndex.column() + 1, nextIndex.parent()); + } while(nextIndex.isValid() && !canEditThroughTabbing(nextIndex)); - if(nextIndex.isValid()) { - if((model()->flags(nextIndex) & Qt::ItemIsEditable)) { + if(hint == QAbstractItemDelegate::EditPreviousItem) do { + nextIndex = model()->index(nextIndex.row(), nextIndex.column() - 1, nextIndex.parent()); + } while(nextIndex.isValid() && !canEditThroughTabbing(nextIndex)); + + if(idx != nextIndex) { // EditNextItem || EditPreviousItem + if(nextIndex.isValid()) { + QTreeView::closeEditor(editor, QAbstractItemDelegate::NoHint); setCurrentIndex(nextIndex); edit(nextIndex); - } else { + } else { // end of row, submit QTreeView::closeEditor(editor, QAbstractItemDelegate::SubmitModelCache); } + } else { + // parent behaviour for SubmitModelCache and RevertModelCache + QTreeView::closeEditor(editor, hint); } }