diff options
Diffstat (limited to 'src/quicktemplates/qquickheaderview.cpp')
-rw-r--r-- | src/quicktemplates/qquickheaderview.cpp | 219 |
1 files changed, 114 insertions, 105 deletions
diff --git a/src/quicktemplates/qquickheaderview.cpp b/src/quicktemplates/qquickheaderview.cpp index 0d9bc5588c..f218388e79 100644 --- a/src/quicktemplates/qquickheaderview.cpp +++ b/src/quicktemplates/qquickheaderview.cpp @@ -11,19 +11,9 @@ \inherits TableView \brief Provides a horizontal header view to accompany a \l TableView. - A HorizontalHeaderView provides labeling of the columns of a \l TableView. - To add a horizontal header to a TableView, bind the - \l {HorizontalHeaderView::syncView} {syncView} property to the TableView: + \include qquickheaderview.qdocinc {detailed-description} {HorizontalHeaderView} - \snippet qtquickcontrols-headerview-simple.qml horizontal - - The header displays data from the {syncView}'s model by default, but can - also have its own model. If the model is a QAbstractTableModel, then - the header will display the model's horizontal headerData(); otherwise, - the model's data(). - - A HorizontalHeaderView will have - \l {resizableColumns}{TableView::resizableColumns} set to \c true by default. + \sa VerticalHeaderView */ /*! @@ -31,117 +21,61 @@ \inqmlmodule QtQuick.Controls \ingroup qtquickcontrols-containers \inherits TableView - \brief Provides a vertical header view to accompany a \l TableView. - - A VerticalHeaderView provides labeling of the rows of a \l TableView. - To add a vertical header to a TableView, bind the - \l {VerticalHeaderView::syncView} {syncView} property to the TableView: + \brief Offers a vertical header view to accompany a \l TableView. - \snippet qtquickcontrols-headerview-simple.qml vertical + \include qquickheaderview.qdocinc {detailed-description} {VerticalHeaderView} - The header displays data from the {syncView}'s model by default, but can - also have its own model. If the model is a QAbstractTableModel, then - the header will display the model's vertical headerData(); otherwise, - the model's data(). - - A VerticalHeaderView will have - \l {resizableRows}{TableView::resizableRows} set to \c true by default. + \sa HorizontalHeaderView */ /*! - \qmlproperty TableView QtQuick::HorizontalHeaderView::syncView - - This property holds the TableView to synchronize with. + \qmlproperty TableView QtQuick.Controls::HorizontalHeaderView::syncView - Once this property is bound to another TableView, both header and table - will synchronize with regard to column widths, column spacing, and flicking - horizontally. - - If the \l model is not explicitly set, then the header will use the syncView's - model to label the columns. - - \sa model TableView + \include qquickheaderview.qdocinc {syncView} {horizontally} */ /*! - \qmlproperty TableView QtQuick::VerticalHeaderView::syncView - - This property holds the TableView to synchronize with. - - Once this property is bound to another TableView, both header and table - will synchronize with regard to row heights, row spacing, and flicking - vertically. + \qmlproperty TableView QtQuick.Controls::VerticalHeaderView::syncView - If the \l model is not explicitly set, then the header will use the syncView's - model to label the rows. - - \sa model TableView + \include qquickheaderview.qdocinc {syncView} {vertically} */ /*! - \qmlproperty QVariant QtQuick::HorizontalHeaderView::model - - This property holds the model providing data for the horizontal header view. - - When model is not explicitly set, the header will use the syncView's - model once syncView is set. - - If model is a QAbstractTableModel, its horizontal headerData() will - be accessed. - - If model is a QAbstractItemModel other than QAbstractTableModel, model's data() - will be accessed. + \qmlproperty QVariant QtQuick.Controls::HorizontalHeaderView::model - Otherwise, the behavior is same as setting TableView::model. - - \sa TableView {TableView::model} {model} QAbstractTableModel + \include qquickheaderview.qdocinc {model} {horizontal} */ /*! - \qmlproperty QVariant QtQuick::VerticalHeaderView::model - - This property holds the model providing data for the vertical header view. - - When model is not explicitly set, it will be synchronized with syncView's model - once syncView is set. - - If model is a QAbstractTableModel, its vertical headerData() will - be accessed. + \qmlproperty QVariant QtQuick.Controls::VerticalHeaderView::model - If model is a QAbstractItemModel other than QAbstractTableModel, model's data() - will be accessed. - - Otherwise, the behavior is same as setting TableView::model. - - \sa TableView {TableView::model} {model} QAbstractTableModel + \include qquickheaderview.qdocinc {model} {vertical} */ /*! - \qmlproperty QString QtQuick::HorizontalHeaderView::textRole - - This property holds the model role used to display text in each header cell. + \qmlproperty QString QtQuick.Controls::HorizontalHeaderView::textRole - When the model has multiple roles, textRole can be set to determine which - role should be displayed. + \include qquickheaderview.qdocinc {textRole} +*/ - If model is a QAbstractItemModel then it will default to "display"; otherwise - it is empty. +/*! + \qmlproperty QString QtQuick.Controls::VerticalHeaderView::textRole - \sa QAbstractItemModel::roleNames() + \include qquickheaderview.qdocinc {textRole} */ /*! - \qmlproperty QString QtQuick::VerticalHeaderView::textRole + \qmlproperty bool QtQuick.Controls::HorizontalHeaderView::movableColumns + \since 6.8 - This property holds the model role used to display text in each header cell. - - When the model has multiple roles, textRole can be set to determine which - role should be displayed. + \include qquickheaderview.qdocinc {movableColumns} +*/ - If model is a QAbstractItemModel then it will default to "display"; otherwise - it is empty. +/*! + \qmlproperty bool QtQuick.Controls::VerticalHeaderView::movableRows + \since 6.8 - \sa QAbstractItemModel::roleNames() + \include qquickheaderview.qdocinc {movableRows} */ QT_BEGIN_NAMESPACE @@ -234,36 +168,61 @@ void QQuickHeaderViewBasePrivate::syncModel() void QQuickHeaderViewBasePrivate::syncSyncView() { - Q_Q(QQuickHeaderViewBase); if (assignedSyncDirection != orientation()) { qmlWarning(q_func()) << "Setting syncDirection other than Qt::" << QVariant::fromValue(orientation()).toString() << " is invalid."; assignedSyncDirection = orientation(); } - if (assignedSyncView) { - QBoolBlocker fixupGuard(inUpdateContentSize, true); - if (orientation() == Qt::Horizontal) { - q->setLeftMargin(assignedSyncView->leftMargin()); - q->setRightMargin(assignedSyncView->rightMargin()); - } else { - q->setTopMargin(assignedSyncView->topMargin()); - q->setBottomMargin(assignedSyncView->bottomMargin()); - } - } QQuickTableViewPrivate::syncSyncView(); } +QAbstractItemModel *QQuickHeaderViewBasePrivate::selectionSourceModel() +{ + // Our proxy model shares no common model items with HeaderView.model. So + // selections done in HeaderView cannot be represented in an ItemSelectionModel + // that is shared with the syncView (and for the same reason, the mapping functions + // modelIndex(cell) and cellAtIndex(index) have not been overridden either). + // Instead, we set the internal proxy model as selection source model. + return &m_headerDataProxyModel; +} + +int QQuickHeaderViewBasePrivate::logicalRowIndex(const int visualIndex) const +{ + return (m_headerDataProxyModel.orientation() == Qt::Horizontal) ? visualIndex : QQuickTableViewPrivate::logicalRowIndex(visualIndex); +} + +int QQuickHeaderViewBasePrivate::logicalColumnIndex(const int visualIndex) const +{ + return (m_headerDataProxyModel.orientation() == Qt::Vertical) ? visualIndex : QQuickTableViewPrivate::logicalColumnIndex(visualIndex); +} + +int QQuickHeaderViewBasePrivate::visualRowIndex(const int logicalIndex) const +{ + return (m_headerDataProxyModel.orientation() == Qt::Horizontal) ? logicalIndex : QQuickTableViewPrivate::visualRowIndex(logicalIndex); +} + +int QQuickHeaderViewBasePrivate::visualColumnIndex(const int logicalIndex) const +{ + return (m_headerDataProxyModel.orientation() == Qt::Vertical) ? logicalIndex : QQuickTableViewPrivate::visualColumnIndex(logicalIndex); +} + QQuickHeaderViewBase::QQuickHeaderViewBase(Qt::Orientation orient, QQuickItem *parent) : QQuickTableView(*(new QQuickHeaderViewBasePrivate), parent) { - d_func()->setOrientation(orient); + Q_D(QQuickHeaderViewBase); + d->m_headerDataProxyModel.m_headerView = this; + d->setSizePolicy(orient == Qt::Horizontal ? QLayoutPolicy::Preferred : QLayoutPolicy::Fixed, + orient == Qt::Horizontal ? QLayoutPolicy::Fixed : QLayoutPolicy::Preferred); + d->setOrientation(orient); setSyncDirection(orient); } QQuickHeaderViewBase::QQuickHeaderViewBase(QQuickHeaderViewBasePrivate &dd, QQuickItem *parent) : QQuickTableView(dd, parent) { + Q_D(QQuickHeaderViewBase); + d->m_headerDataProxyModel.m_headerView = this; } QQuickHeaderViewBase::~QQuickHeaderViewBase() @@ -384,6 +343,24 @@ bool QHeaderDataProxyModel::hasChildren(const QModelIndex &parent) const return false; } +QHash<int, QByteArray> QHeaderDataProxyModel::roleNames() const +{ + using namespace Qt::Literals::StringLiterals; + + auto names = m_model ? m_model->roleNames() : QAbstractItemModel::roleNames(); + if (m_headerView) { + QString textRole = m_headerView->textRole(); + if (textRole.isEmpty()) + textRole = u"display"_s; + if (!names.values().contains(textRole.toUtf8().constData())) { + qmlWarning(m_headerView).nospace() << "The 'textRole' property contains a role that doesn't exist in the model: " + << textRole << ". Check your model's roleNames() implementation"; + } + } + + return names; +} + QVariant QHeaderDataProxyModel::variantValue() const { return QVariant::fromValue(static_cast<QObject *>(const_cast<QHeaderDataProxyModel *>(this))); @@ -474,6 +451,22 @@ QQuickHorizontalHeaderView::~QQuickHorizontalHeaderView() { } +bool QQuickHorizontalHeaderView::movableColumns() const +{ + Q_D(const QQuickHorizontalHeaderView); + return d->m_movableColumns; +} + +void QQuickHorizontalHeaderView::setMovableColumns(bool movableColumns) +{ + Q_D(QQuickHorizontalHeaderView); + if (d->m_movableColumns == movableColumns) + return; + + d->m_movableColumns = movableColumns; + emit movableColumnsChanged(); +} + QQuickVerticalHeaderView::QQuickVerticalHeaderView(QQuickItem *parent) : QQuickHeaderViewBase(Qt::Vertical, parent) { @@ -485,6 +478,22 @@ QQuickVerticalHeaderView::~QQuickVerticalHeaderView() { } +bool QQuickVerticalHeaderView::movableRows() const +{ + Q_D(const QQuickVerticalHeaderView); + return d->m_movableRows ; +} + +void QQuickVerticalHeaderView::setMovableRows(bool movableRows) +{ + Q_D(QQuickVerticalHeaderView); + if (d->m_movableRows == movableRows) + return; + + d->m_movableRows = movableRows; + emit movableRowsChanged(); +} + QQuickHorizontalHeaderViewPrivate::QQuickHorizontalHeaderViewPrivate() = default; QQuickHorizontalHeaderViewPrivate::~QQuickHorizontalHeaderViewPrivate() = default; |