Skip to content

Commit

Permalink
submit on click-away, improve tabbing behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
ohwgiles committed Jan 25, 2015
1 parent 8ce31ca commit 733179c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/sqlmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
36 changes: 26 additions & 10 deletions src/tableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 733179c

Please sign in to comment.