Skip to content
Permalink
Browse files

Separate display role from cell content role in table editor, so that

we can show properly formatted numbers in the table editor for cells
with a numeric format set
  • Loading branch information
nyalldawson committed Feb 7, 2020
1 parent b38d78e commit c76c3904050ae3660ee42435380ae479958023bd
Showing with 50 additions and 8 deletions.
  1. +37 −7 src/gui/tableeditor/qgstableeditorwidget.cpp
  2. +13 −1 src/gui/tableeditor/qgstableeditorwidget.h
@@ -120,7 +120,9 @@ QgsTableEditorWidget::QgsTableEditorWidget( QWidget *parent )
} );


setItemDelegate( new QgsTableEditorDelegate( this ) );
QgsTableEditorDelegate *delegate = new QgsTableEditorDelegate( this );
connect( delegate, &QgsTableEditorDelegate::updateNumericFormatForIndex, this, &QgsTableEditorWidget::updateNumericFormatForIndex );
setItemDelegate( delegate );


connect( this, &QTableWidget::cellDoubleClicked, this, [ = ]
@@ -139,6 +141,17 @@ QgsTableEditorWidget::~QgsTableEditorWidget()
qDeleteAll( mNumericFormats );
}

void QgsTableEditorWidget::updateNumericFormatForIndex( const QModelIndex &index )
{
if ( QTableWidgetItem *i = item( index.row(), index.column() ) )
{
if ( QgsNumericFormat *format = mNumericFormats.value( i ) )
{
i->setData( Qt::DisplayRole, format->formatDouble( index.data( CellContent ).toDouble(), QgsNumericFormatContext() ) );
}
}
}

void QgsTableEditorWidget::updateHeaders()
{
QStringList headers;
@@ -282,6 +295,7 @@ void QgsTableEditorWidget::setTableContents( const QgsTableContents &contents )
qDeleteAll( mNumericFormats );
mNumericFormats.clear();

QgsNumericFormatContext numericContext;
int rowNumber = 0;
setRowCount( contents.size() );
for ( const QgsTableRow &row : contents )
@@ -293,11 +307,15 @@ void QgsTableEditorWidget::setTableContents( const QgsTableContents &contents )
for ( const QgsTableCell &col : row )
{
QTableWidgetItem *item = new QTableWidgetItem( col.content().toString() );
item->setData( CellContent, col.content() ); // can't use EditRole, because Qt. (https://bugreports.qt.io/browse/QTBUG-11549)
item->setData( Qt::BackgroundRole, col.backgroundColor().isValid() ? col.backgroundColor() : QColor( 255, 255, 255 ) );
item->setData( PresetBackgroundColorRole, col.backgroundColor().isValid() ? col.backgroundColor() : QVariant() );
item->setData( Qt::ForegroundRole, col.foregroundColor().isValid() ? col.foregroundColor() : QVariant() );
if ( col.numericFormat() )
{
mNumericFormats.insert( item, col.numericFormat()->clone() );
item->setData( Qt::DisplayRole, mNumericFormats.value( item )->formatDouble( col.content().toDouble(), numericContext ) );
}
setItem( rowNumber, colNumber, item );
colNumber++;
}
@@ -323,7 +341,7 @@ QgsTableContents QgsTableEditorWidget::tableContents() const
QgsTableCell cell;
if ( QTableWidgetItem *i = item( r, c ) )
{
cell.setContent( i->text() );
cell.setContent( i->data( CellContent ) );
cell.setBackgroundColor( i->data( PresetBackgroundColorRole ).value< QColor >() );
cell.setForegroundColor( i->data( Qt::ForegroundRole ).value< QColor >() );

@@ -346,6 +364,7 @@ void QgsTableEditorWidget::setSelectionNumericFormat( QgsNumericFormat *format )
mBlockSignals++;
std::unique_ptr< QgsNumericFormat > newFormat( format );
const QModelIndexList selection = selectedIndexes();
QgsNumericFormatContext numericContext;
for ( const QModelIndex &index : selection )
{
QTableWidgetItem *i = item( index.row(), index.column() );
@@ -371,6 +390,7 @@ void QgsTableEditorWidget::setSelectionNumericFormat( QgsNumericFormat *format )
delete mNumericFormats.value( i );
mNumericFormats.insert( i, newFormat->clone() );
}
i->setData( Qt::DisplayRole, newFormat ? mNumericFormats.value( i )->formatDouble( i->data( CellContent ).toDouble(), numericContext ) : i->data( CellContent ) );
}
mBlockSignals--;
if ( changed && !mBlockSignals )
@@ -759,6 +779,7 @@ void QgsTableEditorWidget::clearSelectedCells()
if ( QTableWidgetItem *i = item( index.row(), index.column() ) )
{
i->setText( QString() );
i->setData( CellContent, QVariant() );
changed = true;
}
}
@@ -1044,20 +1065,29 @@ QWidget *QgsTableEditorDelegate::createEditor( QWidget *parent, const QStyleOpti

void QgsTableEditorDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
{
QVariant value = index.model()->data( index, Qt::EditRole );
QVariant value = index.model()->data( index, QgsTableEditorWidget::CellContent );
if ( QgsTableEditorTextEdit *lineEdit = qobject_cast<QgsTableEditorTextEdit * >( editor ) )
{
lineEdit->setPlainText( value.toString() );
lineEdit->selectAll();
if ( index != mLastIndex || lineEdit->toPlainText() != value.toString() )
{
lineEdit->setPlainText( value.toString() );
lineEdit->selectAll();
}
}
mLastIndex = index;
}

void QgsTableEditorDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
if ( QgsTableEditorTextEdit *lineEdit = qobject_cast<QgsTableEditorTextEdit * >( editor ) )
{
model->setData( index, lineEdit->toPlainText(), Qt::EditRole );
model->setData( index, lineEdit->toPlainText(), Qt::DisplayRole );
const QString text = lineEdit->toPlainText();
if ( text != model->data( index, QgsTableEditorWidget::CellContent ).toString() )
{
model->setData( index, text, QgsTableEditorWidget::CellContent );
model->setData( index, text, Qt::DisplayRole );
emit updateNumericFormatForIndex( index );
}
}
}

@@ -77,6 +77,10 @@ class QgsTableEditorDelegate : public QStyledItemDelegate
*/
void setWeakEditorMode( bool weakEditorMode );

signals:

void updateNumericFormatForIndex( const QModelIndex &index ) const;

protected:
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem & /*option*/, const QModelIndex &index ) const override;
void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
@@ -85,6 +89,7 @@ class QgsTableEditorDelegate : public QStyledItemDelegate
private:

bool mWeakEditorMode = false;
mutable QModelIndex mLastIndex;
};

///@endcond
@@ -347,14 +352,19 @@ class GUI_EXPORT QgsTableEditorWidget : public QTableWidget
*/
void activeCellChanged();

private slots:

void updateNumericFormatForIndex( const QModelIndex &index );

private:

//! Custom roles
enum Roles
{
PresetBackgroundColorRole = Qt::UserRole + 1,
RowHeight,
ColumnWidth
ColumnWidth,
CellContent
};

void updateHeaders();
@@ -368,6 +378,8 @@ class GUI_EXPORT QgsTableEditorWidget : public QTableWidget
QHash< QTableWidgetItem *, QgsNumericFormat * > mNumericFormats;
QMenu *mHeaderMenu = nullptr;

friend class QgsTableEditorDelegate;

};

#endif // QGSTABLEEDITORWIDGET_H

0 comments on commit c76c390

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