Navigation Menu

Skip to content

Commit

Permalink
Make table cell editing behave more like the spreadsheet style conven…
Browse files Browse the repository at this point in the history
…tions

Where selecting a cell and then typing enters a "weak" edit mode, whereby
pressing a cursor key will immediately end editing and move to another
cell. But double-clicking a cell to edit enters a "strong" edit mode,
where cursor key presses are used to navigate through the current
cell text
  • Loading branch information
nyalldawson committed Jan 14, 2020
1 parent 515a6b1 commit f14a06c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/gui/tableeditor/qgstableeditorwidget.cpp
Expand Up @@ -122,6 +122,15 @@ QgsTableEditorWidget::QgsTableEditorWidget( QWidget *parent )

setItemDelegate( new QgsTableEditorDelegate( this ) );


connect( this, &QTableWidget::cellDoubleClicked, this, [ = ]
{
if ( QgsTableEditorDelegate *d = qobject_cast< QgsTableEditorDelegate *>( itemDelegate( ) ) )
{
d->setWeakEditorMode( false );
}
} );

connect( selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsTableEditorWidget::activeCellChanged );
}

Expand Down Expand Up @@ -261,6 +270,10 @@ void QgsTableEditorWidget::keyPressEvent( QKeyEvent *event )
default:
QTableWidget::keyPressEvent( event );
}
if ( QgsTableEditorDelegate *d = qobject_cast< QgsTableEditorDelegate *>( itemDelegate( ) ) )
{
d->setWeakEditorMode( true );
}
}

void QgsTableEditorWidget::setTableContents( const QgsTableContents &contents )
Expand Down Expand Up @@ -903,21 +916,50 @@ void QgsTableEditorTextEdit::keyPressEvent( QKeyEvent *event )
break;
}

case Qt::Key_Right:
case Qt::Key_Left:
case Qt::Key_Up:
case Qt::Key_Down:
{
if ( mWeakEditorMode )
{
// close editor and defer to table
event->ignore();
}
else
{
QPlainTextEdit::keyPressEvent( event );
}
break;
}

default:
QPlainTextEdit::keyPressEvent( event );
}
}


void QgsTableEditorTextEdit::setWeakEditorMode( bool weakEditorMode )
{
mWeakEditorMode = weakEditorMode;
}


QgsTableEditorDelegate::QgsTableEditorDelegate( QObject *parent )
: QStyledItemDelegate( parent )
{

}

void QgsTableEditorDelegate::setWeakEditorMode( bool weakEditorMode )
{
mWeakEditorMode = weakEditorMode;
}

QWidget *QgsTableEditorDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &, const QModelIndex & ) const
{
QgsTableEditorTextEdit *w = new QgsTableEditorTextEdit( parent );
w->setWeakEditorMode( mWeakEditorMode );
return w;
}

Expand Down
20 changes: 20 additions & 0 deletions src/gui/tableeditor/qgstableeditorwidget.h
Expand Up @@ -34,22 +34,42 @@ class QgsTableEditorTextEdit : public QPlainTextEdit
public:
QgsTableEditorTextEdit( QWidget *parent );

/**
* Sets whether the editor is an a "weak" editor mode, where any
* cursor key presses will be ignored by the editor and deferred to the table instead.
*/
void setWeakEditorMode( bool weakEditorMode );

protected:

void keyPressEvent( QKeyEvent *e ) override;

private:

bool mWeakEditorMode = false;

};

class QgsTableEditorDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
QgsTableEditorDelegate( QObject *parent );

/**
* Sets whether the editor is an a "weak" editor mode, where any
* cursor key presses will be ignored by the editor and deferred to the table instead.
*/
void setWeakEditorMode( bool weakEditorMode );

protected:
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem & /*option*/, const QModelIndex &index ) const override;
void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;

private:

bool mWeakEditorMode = false;
};

///@endcond
Expand Down

0 comments on commit f14a06c

Please sign in to comment.