Skip to content
Permalink
Browse files

Make table cell editing behave more like the spreadsheet style conven…

…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 f14a06c2fda7e127ba2166a0763c0e11262f338d
Showing with 62 additions and 0 deletions.
  1. +42 −0 src/gui/tableeditor/qgstableeditorwidget.cpp
  2. +20 −0 src/gui/tableeditor/qgstableeditorwidget.h
@@ -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 );
}

@@ -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 )
@@ -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;
}

@@ -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

0 comments on commit f14a06c

Please sign in to comment.
You can’t perform that action at this time.