In MergeView, replace QTableWidget with QTableView with custom model (#266,#217)
QTableWidget was a major bottleneck for large merge sources (#217). This is because a QTableWidgetItem needed to be created for every field in every record of the merge data, whether they are being displayed or not. This was not a problem for small merge sources (only a few dozen records max), however for larger data sets this would severely affect performance and make the application unresponsive. QTableView only renders the fields and records currently visible.
This commit is contained in:
@@ -21,6 +21,7 @@ set (glabels_sources
|
||||
Help.cpp
|
||||
LabelEditor.cpp
|
||||
MainWindow.cpp
|
||||
MergeTableModel.cpp
|
||||
MergeView.cpp
|
||||
MiniPreviewPixmap.cpp
|
||||
NotebookUtil.cpp
|
||||
@@ -57,6 +58,7 @@ set (glabels_qobject_headers
|
||||
File.h
|
||||
LabelEditor.h
|
||||
MainWindow.h
|
||||
MergeTableModel.h
|
||||
MergeView.h
|
||||
ObjectEditor.h
|
||||
PreferencesDialog.h
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
/* MergeTableModel.cpp
|
||||
*
|
||||
* Copyright (C) 2025 Jaye Evins <evins@snaught.com>
|
||||
*
|
||||
* This file is part of gLabels-qt.
|
||||
*
|
||||
* gLabels-qt is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* gLabels-qt is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "MergeTableModel.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
namespace glabels
|
||||
{
|
||||
|
||||
///
|
||||
/// Constructor
|
||||
///
|
||||
MergeTableModel::MergeTableModel( merge::Merge* merge, QObject* parent )
|
||||
: QAbstractTableModel( parent ),
|
||||
mMerge( merge )
|
||||
{
|
||||
// Copy keys, make sure primary key is first
|
||||
mDisplayKeys.push_back( mMerge->primaryKey() );
|
||||
for ( auto& key : mMerge->keys() )
|
||||
{
|
||||
if ( key != mMerge->primaryKey() )
|
||||
{
|
||||
mDisplayKeys.push_back( key );
|
||||
}
|
||||
}
|
||||
|
||||
connect( mMerge, SIGNAL(selectionChanged()),
|
||||
this, SLOT(onSelectionChanged()) );
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Row count
|
||||
///
|
||||
int MergeTableModel::rowCount( const QModelIndex& parent ) const
|
||||
{
|
||||
return mMerge->recordList().size();
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Column count
|
||||
///
|
||||
int MergeTableModel::columnCount( const QModelIndex& parent ) const
|
||||
{
|
||||
return mDisplayKeys.size() + 1;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Header data
|
||||
///
|
||||
QVariant MergeTableModel::headerData( int section, Qt::Orientation orientation, int role ) const
|
||||
{
|
||||
if ( orientation == Qt::Vertical )
|
||||
{
|
||||
return QAbstractTableModel::headerData( section, orientation, role );
|
||||
}
|
||||
|
||||
if ( (role != Qt::DisplayRole) || section >= mDisplayKeys.size() )
|
||||
{
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
return mDisplayKeys[ section ];
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Data
|
||||
///
|
||||
QVariant MergeTableModel::data( const QModelIndex& index, int role ) const
|
||||
{
|
||||
if ( !index.isValid() )
|
||||
{
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
if ( (index.row() >= mMerge->recordList().size()) ||
|
||||
(index.column() >= mDisplayKeys.size()) )
|
||||
{
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
|
||||
if ( (role == Qt::CheckStateRole) && (index.column() == 0) )
|
||||
{
|
||||
auto record = mMerge->recordList()[ index.row() ];
|
||||
return record.isSelected() ? Qt::Checked : Qt::Unchecked;
|
||||
}
|
||||
|
||||
if ( role == Qt::DisplayRole )
|
||||
{
|
||||
auto record = mMerge->recordList()[ index.row() ];
|
||||
auto key = mDisplayKeys[ index.column() ];
|
||||
|
||||
if ( record.contains( key ) )
|
||||
{
|
||||
return record[ key ];
|
||||
}
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Set data
|
||||
///
|
||||
bool MergeTableModel::setData( const QModelIndex& index, const QVariant& value, int role )
|
||||
{
|
||||
if ( !index.isValid() || (index.column() != 0) || (role != Qt::CheckStateRole) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isChecked = static_cast<Qt::CheckState>(value.toInt()) != Qt::Unchecked;
|
||||
|
||||
mMerge->blockSignals( true );
|
||||
mMerge->setSelected( index.row(), isChecked );
|
||||
mMerge->blockSignals( false );
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Flags
|
||||
///
|
||||
Qt::ItemFlags MergeTableModel::flags( const QModelIndex& index ) const
|
||||
{
|
||||
if ( !index.isValid() )
|
||||
{
|
||||
return Qt::NoItemFlags;
|
||||
}
|
||||
|
||||
if ( index.column() == 0 )
|
||||
{
|
||||
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
|
||||
}
|
||||
|
||||
return Qt::ItemIsEnabled;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Selection changed handler
|
||||
///
|
||||
void MergeTableModel::onSelectionChanged()
|
||||
{
|
||||
for ( int iRow = 0; iRow < mMerge->recordList().size(); iRow++ )
|
||||
{
|
||||
auto index = createIndex( iRow, 0 );
|
||||
emit dataChanged( index, index, {Qt::CheckStateRole} );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace glabels
|
||||
@@ -0,0 +1,80 @@
|
||||
/* MergeTableModel.h
|
||||
*
|
||||
* Copyright (C) 2025 Jaye Evins <evins@snaught.com>
|
||||
*
|
||||
* This file is part of gLabels-qt.
|
||||
*
|
||||
* gLabels-qt is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* gLabels-qt is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef MergeTableModel_h
|
||||
#define MergeTableModel_h
|
||||
|
||||
|
||||
#include "merge/Merge.h"
|
||||
|
||||
#include <QAbstractTableModel>
|
||||
|
||||
|
||||
namespace glabels
|
||||
{
|
||||
|
||||
///
|
||||
/// MergeTable proxy model
|
||||
///
|
||||
class MergeTableModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
/////////////////////////////////
|
||||
// Life Cycle
|
||||
/////////////////////////////////
|
||||
public:
|
||||
MergeTableModel( merge::Merge* merge, QObject* parent = nullptr );
|
||||
|
||||
|
||||
/////////////////////////////////
|
||||
// Public methods
|
||||
/////////////////////////////////
|
||||
public:
|
||||
int rowCount( const QModelIndex& parent = QModelIndex() ) const override;
|
||||
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
|
||||
|
||||
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
|
||||
QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const override;
|
||||
bool setData( const QModelIndex& index, const QVariant& value, int role = Qt::EditRole ) override;
|
||||
Qt::ItemFlags flags( const QModelIndex& index ) const override;
|
||||
|
||||
|
||||
/////////////////////////////////
|
||||
// Private slots
|
||||
/////////////////////////////////
|
||||
private slots:
|
||||
void onSelectionChanged();
|
||||
|
||||
|
||||
/////////////////////////////////
|
||||
// Private Members
|
||||
/////////////////////////////////
|
||||
private:
|
||||
merge::Merge* mMerge;
|
||||
|
||||
QStringList mDisplayKeys;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // MergeTableModel_h
|
||||
+9
-149
@@ -21,6 +21,8 @@
|
||||
|
||||
#include "MergeView.h"
|
||||
|
||||
#include "MergeTableModel.h"
|
||||
|
||||
#include "merge/Factory.h"
|
||||
|
||||
#include "model/FileUtil.h"
|
||||
@@ -38,7 +40,7 @@ namespace glabels
|
||||
/// Constructor
|
||||
///
|
||||
MergeView::MergeView( QWidget *parent )
|
||||
: QWidget(parent), mModel(nullptr), mUndoRedoModel(nullptr), mBlock(false), mOldFormatComboIndex(0)
|
||||
: QWidget(parent), mModel(nullptr), mUndoRedoModel(nullptr), mOldFormatComboIndex(0)
|
||||
{
|
||||
setupUi( this );
|
||||
|
||||
@@ -98,19 +100,11 @@ namespace glabels
|
||||
break;
|
||||
}
|
||||
|
||||
recordsTable->clear();
|
||||
recordsTable->setColumnCount( 0 );
|
||||
loadHeaders( mModel->merge() );
|
||||
loadTable( mModel->merge() );
|
||||
recordsTableView->setModel( new MergeTableModel( mModel->merge() ) );
|
||||
recordsTableView->resizeColumnsToContents();
|
||||
|
||||
connect( mModel->merge(), SIGNAL(sourceChanged()),
|
||||
this, SLOT(onMergeSourceChanged()) );
|
||||
|
||||
connect( mModel->merge(), SIGNAL(selectionChanged()),
|
||||
this, SLOT(onMergeSelectionChanged()) );
|
||||
|
||||
connect( recordsTable, SIGNAL(cellChanged(int,int)),
|
||||
this, SLOT(onCellChanged(int,int)) );
|
||||
}
|
||||
|
||||
|
||||
@@ -122,32 +116,8 @@ namespace glabels
|
||||
QString fn = model::FileUtil::makeRelativeIfInDir( mModel->dir(), mModel->merge()->source() );
|
||||
locationLineEdit->setText( fn );
|
||||
|
||||
recordsTable->clear();
|
||||
recordsTable->setColumnCount( 0 );
|
||||
loadHeaders( mModel->merge() );
|
||||
loadTable( mModel->merge() );
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Merge selection changed handler
|
||||
///
|
||||
void MergeView::onMergeSelectionChanged()
|
||||
{
|
||||
mBlock = true; // Don't recurse
|
||||
|
||||
auto& records = mModel->merge()->recordList();
|
||||
|
||||
int iRow = 0;
|
||||
for ( auto& record : records )
|
||||
{
|
||||
QTableWidgetItem* item = recordsTable->item( iRow, 0 );
|
||||
item->setCheckState( record.isSelected() ? Qt::Checked : Qt::Unchecked );
|
||||
|
||||
iRow++;
|
||||
}
|
||||
|
||||
mBlock = false;
|
||||
recordsTableView->setModel( new MergeTableModel( mModel->merge() ) );
|
||||
recordsTableView->resizeColumnsToContents();
|
||||
}
|
||||
|
||||
|
||||
@@ -212,118 +182,6 @@ namespace glabels
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Cell changed handler
|
||||
///
|
||||
void MergeView::onCellChanged( int iRow, int iCol )
|
||||
{
|
||||
if ( !mBlock )
|
||||
{
|
||||
QTableWidgetItem* item = recordsTable->item( iRow, 0 );
|
||||
bool state = (item->checkState() == Qt::Unchecked) ? false : true;
|
||||
|
||||
mModel->merge()->setSelected( iRow, state );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Load headers
|
||||
///
|
||||
void MergeView::loadHeaders( merge::Merge* merge )
|
||||
{
|
||||
mPrimaryKey = merge->primaryKey();
|
||||
mKeys = merge->keys();
|
||||
|
||||
if ( mKeys.size() > 0 )
|
||||
{
|
||||
recordsTable->setColumnCount( mKeys.size() + 1 ); // Include extra column
|
||||
|
||||
// First column = primary Key
|
||||
auto* item = new QTableWidgetItem( mPrimaryKey );
|
||||
item->setFlags( Qt::ItemIsEnabled );
|
||||
recordsTable->setHorizontalHeaderItem( 0, item );
|
||||
|
||||
// Starting on second column, one column per key, skip primary Key
|
||||
int iCol = 1;
|
||||
foreach ( QString key, mKeys )
|
||||
{
|
||||
if ( key != mPrimaryKey )
|
||||
{
|
||||
auto* item = new QTableWidgetItem( key );
|
||||
item->setFlags( Qt::ItemIsEnabled );
|
||||
recordsTable->setHorizontalHeaderItem( iCol, item );
|
||||
|
||||
iCol++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Extra dummy column to fill any extra horizontal space
|
||||
auto* fillItem = new QTableWidgetItem();
|
||||
fillItem->setFlags( Qt::NoItemFlags );
|
||||
recordsTable->setHorizontalHeaderItem( iCol, fillItem );
|
||||
recordsTable->horizontalHeader()->setStretchLastSection( true );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Load table
|
||||
///
|
||||
void MergeView::loadTable( merge::Merge* merge )
|
||||
{
|
||||
mBlock = true;
|
||||
|
||||
auto& records = merge->recordList();
|
||||
recordsTable->setRowCount( records.size() );
|
||||
|
||||
int iRow = 0;
|
||||
for ( auto record : records )
|
||||
{
|
||||
// First column for primary field
|
||||
auto* item = new QTableWidgetItem();
|
||||
if ( record.contains( mPrimaryKey ) )
|
||||
{
|
||||
auto text = printableTextForView( record[mPrimaryKey] );
|
||||
item->setText( text );
|
||||
}
|
||||
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsUserCheckable );
|
||||
item->setCheckState( record.isSelected() ? Qt::Checked : Qt::Unchecked );
|
||||
recordsTable->setItem( iRow, 0, item );
|
||||
recordsTable->resizeColumnToContents( 0 );
|
||||
|
||||
// Starting on 2nd column, 1 column per field, skip primary field
|
||||
int iCol = 1;
|
||||
for ( auto& key : mKeys )
|
||||
{
|
||||
if ( key != mPrimaryKey )
|
||||
{
|
||||
if ( record.contains( key ) )
|
||||
{
|
||||
auto text = printableTextForView( record[key] );
|
||||
auto* item = new QTableWidgetItem( text );
|
||||
item->setFlags( Qt::ItemIsEnabled );
|
||||
recordsTable->setItem( iRow, iCol, item );
|
||||
recordsTable->resizeColumnToContents( iCol );
|
||||
}
|
||||
|
||||
iCol++;
|
||||
}
|
||||
}
|
||||
|
||||
// Extra dummy column to fill any extra horizontal space
|
||||
auto* fillItem = new QTableWidgetItem();
|
||||
fillItem->setFlags( Qt::NoItemFlags );
|
||||
recordsTable->setItem( iRow, iCol, fillItem );
|
||||
|
||||
iRow++;
|
||||
}
|
||||
|
||||
mBlock = false;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// modify text to be printable e.g. replace newlines
|
||||
///
|
||||
@@ -337,4 +195,6 @@ namespace glabels
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
|
||||
} // namespace glabels
|
||||
|
||||
@@ -64,22 +64,18 @@ namespace glabels
|
||||
private slots:
|
||||
void onMergeChanged();
|
||||
void onMergeSourceChanged();
|
||||
void onMergeSelectionChanged();
|
||||
|
||||
void onFormatComboActivated();
|
||||
void onLocationBrowseButtonClicked();
|
||||
void onSelectAllButtonClicked();
|
||||
void onUnselectAllButtonClicked();
|
||||
void onReloadButtonClicked();
|
||||
void onCellChanged( int iRow, int iCol );
|
||||
|
||||
|
||||
/////////////////////////////////
|
||||
// Private methods
|
||||
/////////////////////////////////
|
||||
private:
|
||||
void loadHeaders( merge::Merge* merge );
|
||||
void loadTable( merge::Merge* merge );
|
||||
static QString printableTextForView( QString text );
|
||||
|
||||
|
||||
@@ -97,7 +93,6 @@ namespace glabels
|
||||
|
||||
QString mCwd;
|
||||
|
||||
bool mBlock;
|
||||
int mOldFormatComboIndex;
|
||||
|
||||
};
|
||||
|
||||
+22
-7
@@ -95,13 +95,6 @@
|
||||
<string>Records</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_4">
|
||||
<item row="0" column="0">
|
||||
<widget class="QTableWidget" name="recordsTable">
|
||||
<property name="focusPolicy">
|
||||
<enum>Qt::NoFocus</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
@@ -140,6 +133,28 @@
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QTableView" name="recordsTableView">
|
||||
<property name="focusPolicy">
|
||||
<enum>Qt::NoFocus</enum>
|
||||
</property>
|
||||
<property name="selectionMode">
|
||||
<enum>QAbstractItemView::NoSelection</enum>
|
||||
</property>
|
||||
<property name="textElideMode">
|
||||
<enum>Qt::ElideRight</enum>
|
||||
</property>
|
||||
<attribute name="horizontalHeaderCascadingSectionResizes">
|
||||
<bool>true</bool>
|
||||
</attribute>
|
||||
<attribute name="horizontalHeaderStretchLastSection">
|
||||
<bool>true</bool>
|
||||
</attribute>
|
||||
<attribute name="verticalHeaderStretchLastSection">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
|
||||
Reference in New Issue
Block a user