The PropertyProxyModel now properly maps the second column to the active row of the source.

It has a slot to receive the activeRow.
Used this in the TablesPage to let the property tab follow the currentIndex in the list of tables.
This commit is contained in:
eelke 2018-08-26 07:59:15 +02:00
parent c6faafec59
commit ad4c6fd442
3 changed files with 92 additions and 22 deletions

View file

@ -1,6 +1,11 @@
#include "PropertyProxyModel.h"
/*
* Code borrowed from: https://stackoverflow.com/questions/21653253/how-to-change-orientation-of-qt-tableview
*
* Originally it was called Horizontal_proxy_model however some adjustments were made to it.
* Instead of the column headers becoming row headers we now convert them to the first column.
* The second column show the values of a single row from the source model. Which is determined
* by the setActiveRow call.
*/
@ -12,16 +17,23 @@ PropertyProxyModel::PropertyProxyModel(QObject *parent)
QModelIndex PropertyProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
if (sourceModel()) {
return sourceModel()->index(proxyIndex.column(), proxyIndex.row());
if (activeRow >= 0) {
if (proxyIndex.column() == valueColumn) {
return sourceModel()->index(activeRow, proxyIndex.row());
}
}
}
else {
return QModelIndex();
}
}
QModelIndex PropertyProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
{
return index(sourceIndex.column(), sourceIndex.row());
if (activeRow >= 0) {
if (sourceIndex.row() == activeRow) {
return index(sourceIndex.column(), valueColumn);
}
}
return QModelIndex();
}
QModelIndex PropertyProxyModel::index(int row, int column, const QModelIndex &) const
@ -41,16 +53,52 @@ int PropertyProxyModel::rowCount(const QModelIndex &) const
int PropertyProxyModel::columnCount(const QModelIndex &) const
{
return sourceModel() ? sourceModel()->rowCount() : 0;
return 2;
}
QVariant PropertyProxyModel::headerData(
int section, Qt::Orientation orientation, int role) const
{
if (!sourceModel()) {
// if (!sourceModel()) {
// return QVariant();
// }
// Qt::Orientation new_orientation = orientation == Qt::Horizontal ?
// Qt::Vertical : Qt::Horizontal;
// return sourceModel()->headerData(section, new_orientation, role);
if (orientation == Qt::Horizontal) {
if (role == Qt::DisplayRole) {
switch (section) {
case 0:
return tr("Property");
case 1:
return tr("Value");
}
}
}
return QVariant();
}
Qt::Orientation new_orientation = orientation == Qt::Horizontal ?
Qt::Vertical : Qt::Horizontal;
return sourceModel()->headerData(section, new_orientation, role);
QVariant PropertyProxyModel::data(const QModelIndex &proxyIndex, int role) const
{
auto sm = sourceModel();
if (sm) {
switch (proxyIndex.column()) {
case 0:
// return source header data
return sm->headerData(proxyIndex.row(), Qt::Horizontal, role);
case 1:
// return value if activeRow is set
if (activeRow >= 0) {
return QIdentityProxyModel::data(proxyIndex, role);
}
}
}
//return d->model->data(mapToSource(proxyIndex), role);
return QVariant();
}
void PropertyProxyModel::setActiveRow(int row)
{
activeRow = row;
emit dataChanged(index(0, valueColumn), index(rowCount(QModelIndex()), valueColumn), QVector<int>() << Qt::DisplayRole);
}

View file

@ -4,6 +4,7 @@
#include <QIdentityProxyModel>
class PropertyProxyModel : public QIdentityProxyModel {
Q_OBJECT
public:
PropertyProxyModel(QObject * parent = nullptr);
QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
@ -13,6 +14,17 @@ public:
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QVariant data(const QModelIndex &proxyIndex, int role) const;
public Q_SLOTS:
void setActiveRow(int row);
private:
enum Columns {
propertyColumn = 0,
valueColumn
};
int activeRow = -1;
};
#endif // HORIZONTALPROXYMODEL_H

View file

@ -53,9 +53,19 @@ TablesPage::TablesPage(MainWindow *parent)
ui->indexesTable->setItemDelegate(new PgLabItemDelegate(ui->indexesTable));
ui->indexesTable->setItemDelegateForColumn(0, delegate);
PropertyProxyModel* proxy_model = new PropertyProxyModel(this);
proxy_model->setSourceModel(m_tablesModel);
ui->tablePropertiesTable->setModel(proxy_model);
PropertyProxyModel* property_model = new PropertyProxyModel(this);
property_model->setSourceModel(m_tablesModel);
ui->tablePropertiesTable->setModel(property_model);
connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentChanged,
[property_model](const QModelIndex &current, const QModelIndex &) {
int row = -1;
if (current.isValid())
row = current.row();
property_model->setActiveRow(row);
});
//m_namespaceFilterWidget = new NamespaceFilterWidget(this);
//ui->verticalLayoutTableView->addWidget(m_namespaceFilterWidget);