Flexible models #76
3 changed files with 92 additions and 22 deletions
|
|
@ -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());
|
||||
}
|
||||
else {
|
||||
return QModelIndex();
|
||||
if (activeRow >= 0) {
|
||||
if (proxyIndex.column() == valueColumn) {
|
||||
return sourceModel()->index(activeRow, proxyIndex.row());
|
||||
}
|
||||
}
|
||||
}
|
||||
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()) {
|
||||
return QVariant();
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Qt::Orientation new_orientation = orientation == Qt::Horizontal ?
|
||||
Qt::Vertical : Qt::Horizontal;
|
||||
return sourceModel()->headerData(section, new_orientation, role);
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,15 +4,27 @@
|
|||
#include <QIdentityProxyModel>
|
||||
|
||||
class PropertyProxyModel : public QIdentityProxyModel {
|
||||
Q_OBJECT
|
||||
public:
|
||||
PropertyProxyModel(QObject * parent = nullptr);
|
||||
QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
|
||||
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
|
||||
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
|
||||
QModelIndex parent(const QModelIndex &child) const;
|
||||
int rowCount(const QModelIndex &parent) const;
|
||||
int columnCount(const QModelIndex &parent) const;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
|
||||
PropertyProxyModel(QObject * parent = nullptr);
|
||||
QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
|
||||
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
|
||||
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
|
||||
QModelIndex parent(const QModelIndex &child) const;
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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 ¤t, 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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue