| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qheaderview.h" |
| 5 | |
| 6 | #include <qabstractitemdelegate.h> |
| 7 | #include <qapplication.h> |
| 8 | #include <qbitarray.h> |
| 9 | #include <qbrush.h> |
| 10 | #include <qdebug.h> |
| 11 | #include <qevent.h> |
| 12 | #include <qlist.h> |
| 13 | #include <qpainter.h> |
| 14 | #include <qscrollbar.h> |
| 15 | #include <qstyle.h> |
| 16 | #include <qstyleoption.h> |
| 17 | #if QT_CONFIG(tooltip) |
| 18 | #include <qtooltip.h> |
| 19 | #endif |
| 20 | #include <qvarlengtharray.h> |
| 21 | #include <qvariant.h> |
| 22 | #if QT_CONFIG(whatsthis) |
| 23 | #include <qwhatsthis.h> |
| 24 | #endif |
| 25 | #include <private/qheaderview_p.h> |
| 26 | #include <private/qabstractitemmodel_p.h> |
| 27 | #include <private/qabstractitemdelegate_p.h> |
| 28 | |
| 29 | #ifndef QT_NO_DATASTREAM |
| 30 | #include <qdatastream.h> |
| 31 | #endif |
| 32 | |
| 33 | QT_BEGIN_NAMESPACE |
| 34 | |
| 35 | #ifndef QT_NO_DATASTREAM |
| 36 | QDataStream &(QDataStream &out, const QHeaderViewPrivate::SectionItem §ion) |
| 37 | { |
| 38 | section.write(out); |
| 39 | return out; |
| 40 | } |
| 41 | |
| 42 | QDataStream &(QDataStream &in, QHeaderViewPrivate::SectionItem §ion) |
| 43 | { |
| 44 | section.read(in); |
| 45 | return in; |
| 46 | } |
| 47 | #endif // QT_NO_DATASTREAM |
| 48 | |
| 49 | static const int maxSizeSection = 1048575; // since section size is in a bitfield (uint 20). See qheaderview_p.h |
| 50 | // if this is changed then the docs in maximumSectionSize should be changed. |
| 51 | |
| 52 | /*! |
| 53 | \class QHeaderView |
| 54 | |
| 55 | \brief The QHeaderView class provides a header row or header column for |
| 56 | item views. |
| 57 | |
| 58 | \ingroup model-view |
| 59 | \inmodule QtWidgets |
| 60 | |
| 61 | A QHeaderView displays the headers used in item views such as the |
| 62 | QTableView and QTreeView classes. It takes the place of Qt3's \c QHeader |
| 63 | class previously used for the same purpose, but uses the Qt's model/view |
| 64 | architecture for consistency with the item view classes. |
| 65 | |
| 66 | The QHeaderView class is one of the \l{Model/View Classes} and is part of |
| 67 | Qt's \l{Model/View Programming}{model/view framework}. |
| 68 | |
| 69 | The header gets the data for each section from the model using the |
| 70 | QAbstractItemModel::headerData() function. You can set the data by using |
| 71 | QAbstractItemModel::setHeaderData(). |
| 72 | |
| 73 | Each header has an orientation() and a number of sections, given by the |
| 74 | count() function. A section refers to a part of the header - either a row |
| 75 | or a column, depending on the orientation. |
| 76 | |
| 77 | Sections can be moved and resized using moveSection() and resizeSection(); |
| 78 | they can also be hidden and shown with hideSection() and showSection(). |
| 79 | |
| 80 | Each section of a header is described by a section ID, specified by its |
| 81 | section(), and can be located at a particular visualIndex() in the header. |
| 82 | A section can have a sort indicator set with setSortIndicator(); this |
| 83 | indicates whether the items in the associated item view will be sorted in |
| 84 | the order given by the section. |
| 85 | |
| 86 | For a horizontal header the section is equivalent to a column in the model, |
| 87 | and for a vertical header the section is equivalent to a row in the model. |
| 88 | |
| 89 | \section1 Moving Header Sections |
| 90 | |
| 91 | A header can be fixed in place, or made movable with setSectionsMovable(). It can |
| 92 | be made clickable with setSectionsClickable(), and has resizing behavior in |
| 93 | accordance with setSectionResizeMode(). |
| 94 | |
| 95 | \note Double-clicking on a header to resize a section only applies for |
| 96 | visible rows. |
| 97 | |
| 98 | A header will emit sectionMoved() if the user moves a section, |
| 99 | sectionResized() if the user resizes a section, and sectionClicked() as |
| 100 | well as sectionHandleDoubleClicked() in response to mouse clicks. A header |
| 101 | will also emit sectionCountChanged(). |
| 102 | |
| 103 | You can identify a section using the logicalIndex() and logicalIndexAt() |
| 104 | functions, or by its index position, using the visualIndex() and |
| 105 | visualIndexAt() functions. The visual index will change if a section is |
| 106 | moved, but the logical index will not change. |
| 107 | |
| 108 | \section1 Appearance |
| 109 | |
| 110 | QTableWidget and QTableView create default headers. If you want |
| 111 | the headers to be visible, you can use \l{QFrame::}{setVisible()}. |
| 112 | |
| 113 | Not all \l{Qt::}{ItemDataRole}s will have an effect on a |
| 114 | QHeaderView. If you need to draw other roles, you can subclass |
| 115 | QHeaderView and reimplement \l{QHeaderView::}{paintEvent()}. |
| 116 | QHeaderView respects the following item data roles, unless they are |
| 117 | in conflict with the style (which can happen for styles that follow |
| 118 | the desktop theme): |
| 119 | |
| 120 | \l{Qt::}{TextAlignmentRole}, \l{Qt::}{DisplayRole}, |
| 121 | \l{Qt::}{FontRole}, \l{Qt::}{DecorationRole}, |
| 122 | \l{Qt::}{ForegroundRole}, and \l{Qt::}{BackgroundRole}. |
| 123 | |
| 124 | \note Each header renders the data for each section itself, and does not |
| 125 | rely on a delegate. As a result, calling a header's setItemDelegate() |
| 126 | function will have no effect. |
| 127 | |
| 128 | \sa {Model/View Programming}, QListView, QTableView, QTreeView |
| 129 | */ |
| 130 | |
| 131 | /*! |
| 132 | \enum QHeaderView::ResizeMode |
| 133 | |
| 134 | The resize mode specifies the behavior of the header sections. It can be |
| 135 | set on the entire header view or on individual sections using |
| 136 | setSectionResizeMode(). |
| 137 | |
| 138 | \value Interactive The user can resize the section. The section can also be |
| 139 | resized programmatically using resizeSection(). The section size |
| 140 | defaults to \l defaultSectionSize. (See also |
| 141 | \l cascadingSectionResizes.) |
| 142 | |
| 143 | \value Fixed The user cannot resize the section. The section can only be |
| 144 | resized programmatically using resizeSection(). The section size |
| 145 | defaults to \l defaultSectionSize. |
| 146 | |
| 147 | \value Stretch QHeaderView will automatically resize the section to fill |
| 148 | the available space. The size cannot be changed by the user or |
| 149 | programmatically. |
| 150 | |
| 151 | \value ResizeToContents QHeaderView will automatically resize the section |
| 152 | to its optimal size based on the contents of the entire column or |
| 153 | row. The size cannot be changed by the user or programmatically. |
| 154 | (This value was introduced in 4.2) |
| 155 | |
| 156 | The following values are obsolete: |
| 157 | \value Custom Use Fixed instead. |
| 158 | |
| 159 | \sa setSectionResizeMode(), stretchLastSection, minimumSectionSize |
| 160 | */ |
| 161 | |
| 162 | /*! |
| 163 | \fn void QHeaderView::sectionMoved(int logicalIndex, int oldVisualIndex, |
| 164 | int newVisualIndex) |
| 165 | |
| 166 | This signal is emitted when a section is moved. The section's logical index |
| 167 | is specified by \a logicalIndex, the old index by \a oldVisualIndex, and |
| 168 | the new index position by \a newVisualIndex. |
| 169 | |
| 170 | \sa moveSection() |
| 171 | */ |
| 172 | |
| 173 | /*! |
| 174 | \fn void QHeaderView::sectionResized(int logicalIndex, int oldSize, |
| 175 | int newSize) |
| 176 | |
| 177 | This signal is emitted when a section is resized. The section's logical |
| 178 | number is specified by \a logicalIndex, the old size by \a oldSize, and the |
| 179 | new size by \a newSize. |
| 180 | |
| 181 | \sa resizeSection() |
| 182 | */ |
| 183 | |
| 184 | /*! |
| 185 | \fn void QHeaderView::sectionPressed(int logicalIndex) |
| 186 | |
| 187 | This signal is emitted when a section is pressed. The section's logical |
| 188 | index is specified by \a logicalIndex. |
| 189 | |
| 190 | \sa setSectionsClickable() |
| 191 | */ |
| 192 | |
| 193 | /*! |
| 194 | \fn void QHeaderView::sectionClicked(int logicalIndex) |
| 195 | |
| 196 | This signal is emitted when a section is clicked. The section's logical |
| 197 | index is specified by \a logicalIndex. |
| 198 | |
| 199 | Note that the sectionPressed signal will also be emitted. |
| 200 | |
| 201 | \sa setSectionsClickable(), sectionPressed() |
| 202 | */ |
| 203 | |
| 204 | /*! |
| 205 | \fn void QHeaderView::sectionEntered(int logicalIndex) |
| 206 | \since 4.3 |
| 207 | |
| 208 | This signal is emitted when the cursor moves over the section and the left |
| 209 | mouse button is pressed. The section's logical index is specified by |
| 210 | \a logicalIndex. |
| 211 | |
| 212 | \sa setSectionsClickable(), sectionPressed() |
| 213 | */ |
| 214 | |
| 215 | /*! |
| 216 | \fn void QHeaderView::sectionDoubleClicked(int logicalIndex) |
| 217 | |
| 218 | This signal is emitted when a section is double-clicked. The section's |
| 219 | logical index is specified by \a logicalIndex. |
| 220 | |
| 221 | \sa setSectionsClickable() |
| 222 | */ |
| 223 | |
| 224 | /*! |
| 225 | \fn void QHeaderView::sectionCountChanged(int oldCount, int newCount) |
| 226 | |
| 227 | This signal is emitted when the number of sections changes, i.e., when |
| 228 | sections are added or deleted. The original count is specified by |
| 229 | \a oldCount, and the new count by \a newCount. |
| 230 | |
| 231 | \sa count(), length(), headerDataChanged() |
| 232 | */ |
| 233 | |
| 234 | /*! |
| 235 | \fn void QHeaderView::sectionHandleDoubleClicked(int logicalIndex) |
| 236 | |
| 237 | This signal is emitted when a section is double-clicked. The section's |
| 238 | logical index is specified by \a logicalIndex. |
| 239 | |
| 240 | \sa setSectionsClickable() |
| 241 | */ |
| 242 | |
| 243 | /*! |
| 244 | \fn void QHeaderView::sortIndicatorChanged(int logicalIndex, |
| 245 | Qt::SortOrder order) |
| 246 | \since 4.3 |
| 247 | |
| 248 | This signal is emitted when the section containing the sort indicator or |
| 249 | the order indicated is changed. The section's logical index is specified |
| 250 | by \a logicalIndex and the sort order is specified by \a order. |
| 251 | |
| 252 | \sa setSortIndicator() |
| 253 | */ |
| 254 | |
| 255 | /*! |
| 256 | \fn void QHeaderView::geometriesChanged() |
| 257 | \since 4.2 |
| 258 | |
| 259 | This signal is emitted when the header's geometries have changed. |
| 260 | */ |
| 261 | |
| 262 | /*! |
| 263 | \property QHeaderView::highlightSections |
| 264 | \brief whether the sections containing selected items are highlighted |
| 265 | |
| 266 | By default, this property is \c false. |
| 267 | */ |
| 268 | |
| 269 | /*! |
| 270 | Creates a new generic header with the given \a orientation and \a parent. |
| 271 | */ |
| 272 | QHeaderView::(Qt::Orientation orientation, QWidget *parent) |
| 273 | : QAbstractItemView(*new QHeaderViewPrivate, parent) |
| 274 | { |
| 275 | Q_D(QHeaderView); |
| 276 | d->setDefaultValues(orientation); |
| 277 | initialize(); |
| 278 | } |
| 279 | |
| 280 | /*! |
| 281 | \internal |
| 282 | */ |
| 283 | QHeaderView::(QHeaderViewPrivate &dd, |
| 284 | Qt::Orientation orientation, QWidget *parent) |
| 285 | : QAbstractItemView(dd, parent) |
| 286 | { |
| 287 | Q_D(QHeaderView); |
| 288 | d->setDefaultValues(orientation); |
| 289 | initialize(); |
| 290 | } |
| 291 | |
| 292 | /*! |
| 293 | Destroys the header. |
| 294 | */ |
| 295 | |
| 296 | QHeaderView::() |
| 297 | { |
| 298 | Q_D(QHeaderView); |
| 299 | d->disconnectModel(); |
| 300 | } |
| 301 | |
| 302 | /*! |
| 303 | \internal |
| 304 | */ |
| 305 | void QHeaderView::() |
| 306 | { |
| 307 | Q_D(QHeaderView); |
| 308 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 309 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 310 | setFrameStyle(NoFrame); |
| 311 | setFocusPolicy(Qt::NoFocus); |
| 312 | d->viewport->setMouseTracking(true); |
| 313 | d->viewport->setBackgroundRole(QPalette::Button); |
| 314 | d->textElideMode = Qt::ElideNone; |
| 315 | delete d->itemDelegate; |
| 316 | } |
| 317 | |
| 318 | /*! |
| 319 | \reimp |
| 320 | */ |
| 321 | void QHeaderView::(QAbstractItemModel *model) |
| 322 | { |
| 323 | if (model == this->model()) |
| 324 | return; |
| 325 | Q_D(QHeaderView); |
| 326 | d->layoutChangePersistentSections.clear(); |
| 327 | if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) |
| 328 | d->disconnectModel(); |
| 329 | |
| 330 | if (model && model != QAbstractItemModelPrivate::staticEmptyModel()) { |
| 331 | const bool hor = d->orientation == Qt::Horizontal; |
| 332 | d->modelConnections = { |
| 333 | QObject::connect(sender: model, signal: hor ? &QAbstractItemModel::columnsInserted |
| 334 | : &QAbstractItemModel::rowsInserted, |
| 335 | context: this, slot: &QHeaderView::sectionsInserted), |
| 336 | QObject::connect(sender: model, signal: hor ? &QAbstractItemModel::columnsAboutToBeRemoved |
| 337 | : &QAbstractItemModel::rowsAboutToBeRemoved, |
| 338 | context: this, slot: &QHeaderView::sectionsAboutToBeRemoved), |
| 339 | QObjectPrivate::connect(sender: model, signal: hor ? &QAbstractItemModel::columnsRemoved |
| 340 | : &QAbstractItemModel::rowsRemoved, |
| 341 | receiverPrivate: d, slot: &QHeaderViewPrivate::sectionsRemoved), |
| 342 | QObjectPrivate::connect(sender: model, signal: hor ? &QAbstractItemModel::columnsAboutToBeMoved |
| 343 | : &QAbstractItemModel::rowsAboutToBeMoved, |
| 344 | receiverPrivate: d, slot: &QHeaderViewPrivate::sectionsAboutToBeMoved), |
| 345 | QObjectPrivate::connect(sender: model, signal: hor ? &QAbstractItemModel::columnsMoved |
| 346 | : &QAbstractItemModel::rowsMoved, |
| 347 | receiverPrivate: d, slot: &QHeaderViewPrivate::sectionsMoved), |
| 348 | |
| 349 | QObject::connect(sender: model, signal: &QAbstractItemModel::headerDataChanged, |
| 350 | context: this, slot: &QHeaderView::headerDataChanged), |
| 351 | QObjectPrivate::connect(sender: model, signal: &QAbstractItemModel::layoutAboutToBeChanged, |
| 352 | receiverPrivate: d, slot: &QHeaderViewPrivate::sectionsAboutToBeChanged), |
| 353 | QObjectPrivate::connect(sender: model, signal: &QAbstractItemModel::layoutChanged, |
| 354 | receiverPrivate: d, slot: &QHeaderViewPrivate::sectionsChanged) |
| 355 | }; |
| 356 | } |
| 357 | |
| 358 | d->state = QHeaderViewPrivate::NoClear; |
| 359 | QAbstractItemView::setModel(model); |
| 360 | d->state = QHeaderViewPrivate::NoState; |
| 361 | |
| 362 | // Users want to set sizes and modes before the widget is shown. |
| 363 | // Thus, we have to initialize when the model is set, |
| 364 | // and not lazily like we do in the other views. |
| 365 | initializeSections(); |
| 366 | } |
| 367 | |
| 368 | /*! |
| 369 | Returns the orientation of the header. |
| 370 | |
| 371 | \sa Qt::Orientation |
| 372 | */ |
| 373 | |
| 374 | Qt::Orientation QHeaderView::() const |
| 375 | { |
| 376 | Q_D(const QHeaderView); |
| 377 | return d->orientation; |
| 378 | } |
| 379 | |
| 380 | /*! |
| 381 | Returns the offset of the header: this is the header's left-most (or |
| 382 | top-most for vertical headers) visible pixel. |
| 383 | |
| 384 | \sa setOffset() |
| 385 | */ |
| 386 | |
| 387 | int QHeaderView::() const |
| 388 | { |
| 389 | Q_D(const QHeaderView); |
| 390 | return d->headerOffset; |
| 391 | } |
| 392 | |
| 393 | /*! |
| 394 | \fn void QHeaderView::setOffset(int offset) |
| 395 | |
| 396 | Sets the header's offset to \a offset. |
| 397 | |
| 398 | \sa offset(), length() |
| 399 | */ |
| 400 | |
| 401 | void QHeaderView::(int newOffset) |
| 402 | { |
| 403 | Q_D(QHeaderView); |
| 404 | if (d->headerOffset == newOffset) |
| 405 | return; |
| 406 | int ndelta = d->headerOffset - newOffset; |
| 407 | d->headerOffset = newOffset; |
| 408 | if (d->orientation == Qt::Horizontal) |
| 409 | d->viewport->scroll(dx: isRightToLeft() ? -ndelta : ndelta, dy: 0); |
| 410 | else |
| 411 | d->viewport->scroll(dx: 0, dy: ndelta); |
| 412 | if (d->state == QHeaderViewPrivate::ResizeSection && !d->preventCursorChangeInSetOffset) { |
| 413 | QPoint cursorPos = QCursor::pos(); |
| 414 | if (d->orientation == Qt::Horizontal) |
| 415 | QCursor::setPos(x: cursorPos.x() + ndelta, y: cursorPos.y()); |
| 416 | else |
| 417 | QCursor::setPos(x: cursorPos.x(), y: cursorPos.y() + ndelta); |
| 418 | d->firstPos += ndelta; |
| 419 | d->lastPos += ndelta; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /*! |
| 424 | \since 4.2 |
| 425 | Sets the offset to the start of the section at the given \a visualSectionNumber. |
| 426 | \a visualSectionNumber is the actual visible section when hiddenSections are |
| 427 | not considered. That is not always the same as visualIndex(). |
| 428 | |
| 429 | \sa setOffset(), sectionPosition() |
| 430 | */ |
| 431 | void QHeaderView::(int visualSectionNumber) |
| 432 | { |
| 433 | Q_D(QHeaderView); |
| 434 | if (visualSectionNumber > -1 && visualSectionNumber < d->sectionCount()) { |
| 435 | int position = d->headerSectionPosition(visual: d->adjustedVisualIndex(visualIndex: visualSectionNumber)); |
| 436 | setOffset(position); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /*! |
| 441 | \since 4.2 |
| 442 | Sets the offset to make the last section visible. |
| 443 | |
| 444 | \sa setOffset(), sectionPosition(), setOffsetToSectionPosition() |
| 445 | */ |
| 446 | void QHeaderView::() |
| 447 | { |
| 448 | Q_D(const QHeaderView); |
| 449 | int size = (d->orientation == Qt::Horizontal ? viewport()->width() : viewport()->height()); |
| 450 | int position = length() - size; |
| 451 | setOffset(position); |
| 452 | } |
| 453 | |
| 454 | /*! |
| 455 | Returns the length along the orientation of the header. |
| 456 | |
| 457 | \sa sizeHint(), setSectionResizeMode(), offset() |
| 458 | */ |
| 459 | |
| 460 | int QHeaderView::() const |
| 461 | { |
| 462 | Q_D(const QHeaderView); |
| 463 | d->executePostedLayout(); |
| 464 | d->executePostedResize(); |
| 465 | //Q_ASSERT(d->headerLength() == d->length); |
| 466 | return d->length; |
| 467 | } |
| 468 | |
| 469 | /*! |
| 470 | Returns a suitable size hint for this header. |
| 471 | |
| 472 | \sa sectionSizeHint() |
| 473 | */ |
| 474 | |
| 475 | QSize QHeaderView::() const |
| 476 | { |
| 477 | Q_D(const QHeaderView); |
| 478 | if (d->cachedSizeHint.isValid()) |
| 479 | return d->cachedSizeHint; |
| 480 | d->cachedSizeHint = QSize(0, 0); //reinitialize the cached size hint |
| 481 | const int sectionCount = count(); |
| 482 | |
| 483 | // get size hint for the first n sections |
| 484 | int i = 0; |
| 485 | for (int checked = 0; checked < 100 && i < sectionCount; ++i) { |
| 486 | if (isSectionHidden(logicalIndex: i)) |
| 487 | continue; |
| 488 | checked++; |
| 489 | QSize hint = sectionSizeFromContents(logicalIndex: i); |
| 490 | d->cachedSizeHint = d->cachedSizeHint.expandedTo(otherSize: hint); |
| 491 | } |
| 492 | // get size hint for the last n sections |
| 493 | i = qMax(a: i, b: sectionCount - 100 ); |
| 494 | for (int j = sectionCount - 1, checked = 0; j >= i && checked < 100; --j) { |
| 495 | if (isSectionHidden(logicalIndex: j)) |
| 496 | continue; |
| 497 | checked++; |
| 498 | QSize hint = sectionSizeFromContents(logicalIndex: j); |
| 499 | d->cachedSizeHint = d->cachedSizeHint.expandedTo(otherSize: hint); |
| 500 | } |
| 501 | return d->cachedSizeHint; |
| 502 | } |
| 503 | |
| 504 | /*! |
| 505 | \reimp |
| 506 | */ |
| 507 | |
| 508 | void QHeaderView::(bool v) |
| 509 | { |
| 510 | bool actualChange = (v != isVisible()); |
| 511 | QAbstractItemView::setVisible(v); |
| 512 | if (actualChange) { |
| 513 | QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea*>(object: parentWidget()); |
| 514 | if (parent) |
| 515 | parent->updateGeometry(); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | |
| 520 | /*! |
| 521 | Returns a suitable size hint for the section specified by \a logicalIndex. |
| 522 | |
| 523 | \sa sizeHint(), defaultSectionSize(), minimumSectionSize(), maximumSectionSize() |
| 524 | Qt::SizeHintRole |
| 525 | */ |
| 526 | |
| 527 | int QHeaderView::(int logicalIndex) const |
| 528 | { |
| 529 | Q_D(const QHeaderView); |
| 530 | if (isSectionHidden(logicalIndex)) |
| 531 | return 0; |
| 532 | if (logicalIndex < 0 || logicalIndex >= count()) |
| 533 | return -1; |
| 534 | QSize size; |
| 535 | QVariant value = d->model->headerData(section: logicalIndex, orientation: d->orientation, role: Qt::SizeHintRole); |
| 536 | if (value.isValid()) |
| 537 | size = qvariant_cast<QSize>(v: value); |
| 538 | else |
| 539 | size = sectionSizeFromContents(logicalIndex); |
| 540 | int hint = d->orientation == Qt::Horizontal ? size.width() : size.height(); |
| 541 | return qBound(min: minimumSectionSize(), val: hint, max: maximumSectionSize()); |
| 542 | } |
| 543 | |
| 544 | /*! |
| 545 | Returns the visual index of the section that covers the given \a position |
| 546 | in the viewport. |
| 547 | |
| 548 | \sa logicalIndexAt() |
| 549 | */ |
| 550 | |
| 551 | int QHeaderView::(int position) const |
| 552 | { |
| 553 | Q_D(const QHeaderView); |
| 554 | int vposition = position; |
| 555 | d->executePostedLayout(); |
| 556 | d->executePostedResize(); |
| 557 | const int count = d->sectionCount(); |
| 558 | if (count < 1) |
| 559 | return -1; |
| 560 | |
| 561 | if (d->reverse()) |
| 562 | vposition = d->viewport->width() - vposition - 1; |
| 563 | vposition += d->headerOffset; |
| 564 | |
| 565 | if (vposition > d->length) |
| 566 | return -1; |
| 567 | int visual = d->headerVisualIndexAt(position: vposition); |
| 568 | if (visual < 0) |
| 569 | return -1; |
| 570 | |
| 571 | while (d->isVisualIndexHidden(visual)){ |
| 572 | ++visual; |
| 573 | if (visual >= count) |
| 574 | return -1; |
| 575 | } |
| 576 | return visual; |
| 577 | } |
| 578 | |
| 579 | /*! |
| 580 | Returns the section that covers the given \a position in the viewport. |
| 581 | |
| 582 | \sa visualIndexAt(), isSectionHidden() |
| 583 | */ |
| 584 | |
| 585 | int QHeaderView::(int position) const |
| 586 | { |
| 587 | const int visual = visualIndexAt(position); |
| 588 | if (visual > -1) |
| 589 | return logicalIndex(visualIndex: visual); |
| 590 | return -1; |
| 591 | } |
| 592 | |
| 593 | /*! |
| 594 | Returns the width (or height for vertical headers) of the given |
| 595 | \a logicalIndex. |
| 596 | |
| 597 | \sa length(), setSectionResizeMode(), defaultSectionSize() |
| 598 | */ |
| 599 | |
| 600 | int QHeaderView::(int logicalIndex) const |
| 601 | { |
| 602 | Q_D(const QHeaderView); |
| 603 | if (isSectionHidden(logicalIndex)) |
| 604 | return 0; |
| 605 | if (logicalIndex < 0 || logicalIndex >= count()) |
| 606 | return 0; |
| 607 | int visual = visualIndex(logicalIndex); |
| 608 | if (visual == -1) |
| 609 | return 0; |
| 610 | d->executePostedResize(); |
| 611 | return d->headerSectionSize(visual); |
| 612 | } |
| 613 | |
| 614 | /*! |
| 615 | |
| 616 | Returns the section position of the given \a logicalIndex, or -1 |
| 617 | if the section is hidden. The position is measured in pixels from |
| 618 | the first visible item's top-left corner to the top-left corner of |
| 619 | the item with \a logicalIndex. The measurement is along the x-axis |
| 620 | for horizontal headers and along the y-axis for vertical headers. |
| 621 | |
| 622 | \sa sectionViewportPosition() |
| 623 | */ |
| 624 | |
| 625 | int QHeaderView::(int logicalIndex) const |
| 626 | { |
| 627 | Q_D(const QHeaderView); |
| 628 | int visual = visualIndex(logicalIndex); |
| 629 | // in some cases users may change the selections |
| 630 | // before we have a chance to do the layout |
| 631 | if (visual == -1) |
| 632 | return -1; |
| 633 | d->executePostedResize(); |
| 634 | return d->headerSectionPosition(visual); |
| 635 | } |
| 636 | |
| 637 | /*! |
| 638 | Returns the section viewport position of the given \a logicalIndex. |
| 639 | |
| 640 | If the section is hidden, the return value is undefined. |
| 641 | |
| 642 | \sa sectionPosition(), isSectionHidden() |
| 643 | */ |
| 644 | |
| 645 | int QHeaderView::(int logicalIndex) const |
| 646 | { |
| 647 | Q_D(const QHeaderView); |
| 648 | if (logicalIndex >= count()) |
| 649 | return -1; |
| 650 | int position = sectionPosition(logicalIndex); |
| 651 | if (position < 0) |
| 652 | return position; // the section was hidden |
| 653 | int offsetPosition = position - d->headerOffset; |
| 654 | if (d->reverse()) |
| 655 | return d->viewport->width() - (offsetPosition + sectionSize(logicalIndex)); |
| 656 | return offsetPosition; |
| 657 | } |
| 658 | |
| 659 | /*! |
| 660 | \fn int QHeaderView::logicalIndexAt(int x, int y) const |
| 661 | |
| 662 | Returns the logical index of the section at the given coordinate. If the |
| 663 | header is horizontal \a x will be used, otherwise \a y will be used to |
| 664 | find the logical index. |
| 665 | */ |
| 666 | |
| 667 | /*! |
| 668 | \fn int QHeaderView::logicalIndexAt(const QPoint &pos) const |
| 669 | |
| 670 | Returns the logical index of the section at the position given in \a pos. |
| 671 | If the header is horizontal the x-coordinate will be used, otherwise the |
| 672 | y-coordinate will be used to find the logical index. |
| 673 | |
| 674 | \sa sectionPosition() |
| 675 | */ |
| 676 | |
| 677 | template<typename Container> |
| 678 | static void qMoveRange(Container& c, |
| 679 | typename Container::size_type rangeStart, |
| 680 | typename Container::size_type rangeEnd, |
| 681 | typename Container::size_type targetPosition) |
| 682 | { |
| 683 | Q_ASSERT(targetPosition <= c.size()); |
| 684 | Q_ASSERT(targetPosition < rangeStart || targetPosition >= rangeEnd); |
| 685 | |
| 686 | const bool forwardMove = targetPosition > rangeStart; |
| 687 | typename Container::size_type first = std::min(rangeStart, targetPosition); |
| 688 | typename Container::size_type mid = forwardMove ? rangeEnd : rangeStart; |
| 689 | typename Container::size_type last = forwardMove ? targetPosition + 1 : rangeEnd; |
| 690 | std::rotate(c.begin() + first, c.begin() + mid, c.begin() + last); |
| 691 | } |
| 692 | |
| 693 | /*! |
| 694 | Moves the section at visual index \a from to occupy visual index \a to. |
| 695 | |
| 696 | \sa sectionsMoved() |
| 697 | */ |
| 698 | |
| 699 | void QHeaderView::(int from, int to) |
| 700 | { |
| 701 | Q_D(QHeaderView); |
| 702 | |
| 703 | d->executePostedLayout(); |
| 704 | if (from < 0 || from >= d->sectionCount() || to < 0 || to >= d->sectionCount()) |
| 705 | return; |
| 706 | |
| 707 | if (from == to) { |
| 708 | int logical = logicalIndex(visualIndex: from); |
| 709 | Q_ASSERT(logical != -1); |
| 710 | updateSection(logicalIndex: logical); |
| 711 | return; |
| 712 | } |
| 713 | |
| 714 | d->initializeIndexMapping(); |
| 715 | |
| 716 | int *visualIndices = d->visualIndices.data(); |
| 717 | int *logicalIndices = d->logicalIndices.data(); |
| 718 | int logical = logicalIndices[from]; |
| 719 | int visual = from; |
| 720 | |
| 721 | if (to > from) { |
| 722 | while (visual < to) { |
| 723 | visualIndices[logicalIndices[visual + 1]] = visual; |
| 724 | logicalIndices[visual] = logicalIndices[visual + 1]; |
| 725 | ++visual; |
| 726 | } |
| 727 | } else { |
| 728 | while (visual > to) { |
| 729 | visualIndices[logicalIndices[visual - 1]] = visual; |
| 730 | logicalIndices[visual] = logicalIndices[visual - 1]; |
| 731 | --visual; |
| 732 | } |
| 733 | } |
| 734 | visualIndices[logical] = to; |
| 735 | logicalIndices[to] = logical; |
| 736 | |
| 737 | qMoveRange(c&: d->sectionItems, rangeStart: from, rangeEnd: from + 1, targetPosition: to); |
| 738 | |
| 739 | d->sectionStartposRecalc = true; |
| 740 | |
| 741 | if (d->hasAutoResizeSections()) |
| 742 | d->doDelayedResizeSections(); |
| 743 | d->viewport->update(); |
| 744 | |
| 745 | emit sectionMoved(logicalIndex: logical, oldVisualIndex: from, newVisualIndex: to); |
| 746 | |
| 747 | if (stretchLastSection()) { |
| 748 | const int lastSectionVisualIdx = visualIndex(logicalIndex: d->lastSectionLogicalIdx); |
| 749 | if (from >= lastSectionVisualIdx || to >= lastSectionVisualIdx) |
| 750 | d->maybeRestorePrevLastSectionAndStretchLast(); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | /*! |
| 755 | \since 4.2 |
| 756 | Swaps the section at visual index \a first with the section at visual |
| 757 | index \a second. |
| 758 | |
| 759 | \sa moveSection() |
| 760 | */ |
| 761 | void QHeaderView::(int first, int second) |
| 762 | { |
| 763 | Q_D(QHeaderView); |
| 764 | |
| 765 | if (first == second) |
| 766 | return; |
| 767 | d->executePostedLayout(); |
| 768 | if (first < 0 || first >= d->sectionCount() || second < 0 || second >= d->sectionCount()) |
| 769 | return; |
| 770 | |
| 771 | int firstSize = d->headerSectionSize(visual: first); |
| 772 | ResizeMode firstMode = d->headerSectionResizeMode(visual: first); |
| 773 | int firstLogical = d->logicalIndex(visualIndex: first); |
| 774 | |
| 775 | int secondSize = d->headerSectionSize(visual: second); |
| 776 | ResizeMode secondMode = d->headerSectionResizeMode(visual: second); |
| 777 | int secondLogical = d->logicalIndex(visualIndex: second); |
| 778 | |
| 779 | if (d->state == QHeaderViewPrivate::ResizeSection) |
| 780 | d->preventCursorChangeInSetOffset = true; |
| 781 | |
| 782 | d->createSectionItems(start: second, end: second, sectionSize: firstSize, mode: firstMode); |
| 783 | d->createSectionItems(start: first, end: first, sectionSize: secondSize, mode: secondMode); |
| 784 | |
| 785 | d->initializeIndexMapping(); |
| 786 | |
| 787 | d->visualIndices[firstLogical] = second; |
| 788 | d->logicalIndices[second] = firstLogical; |
| 789 | |
| 790 | d->visualIndices[secondLogical] = first; |
| 791 | d->logicalIndices[first] = secondLogical; |
| 792 | |
| 793 | if (!d->hiddenSectionSize.isEmpty()) { |
| 794 | bool firstHidden = d->isVisualIndexHidden(visual: first); |
| 795 | bool secondHidden = d->isVisualIndexHidden(visual: second); |
| 796 | d->setVisualIndexHidden(visual: first, hidden: secondHidden); |
| 797 | d->setVisualIndexHidden(visual: second, hidden: firstHidden); |
| 798 | } |
| 799 | |
| 800 | d->viewport->update(); |
| 801 | emit sectionMoved(logicalIndex: firstLogical, oldVisualIndex: first, newVisualIndex: second); |
| 802 | emit sectionMoved(logicalIndex: secondLogical, oldVisualIndex: second, newVisualIndex: first); |
| 803 | |
| 804 | if (stretchLastSection()) { |
| 805 | const int lastSectionVisualIdx = visualIndex(logicalIndex: d->lastSectionLogicalIdx); |
| 806 | if (first >= lastSectionVisualIdx || second >= lastSectionVisualIdx) |
| 807 | d->maybeRestorePrevLastSectionAndStretchLast(); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /*! |
| 812 | \fn void QHeaderView::resizeSection(int logicalIndex, int size) |
| 813 | |
| 814 | Resizes the section specified by \a logicalIndex to \a size measured in |
| 815 | pixels. The size parameter must be a value larger or equal to zero. A |
| 816 | size equal to zero is however not recommended. In that situation hideSection |
| 817 | should be used instead. |
| 818 | |
| 819 | \sa sectionResized(), sectionSize(), hideSection() |
| 820 | */ |
| 821 | |
| 822 | void QHeaderView::(int logical, int size) |
| 823 | { |
| 824 | Q_D(QHeaderView); |
| 825 | if (logical < 0 || logical >= count() || size < 0 || size > maxSizeSection) |
| 826 | return; |
| 827 | |
| 828 | // make sure to not exceed bounds when setting size programmatically |
| 829 | if (size > 0) |
| 830 | size = qBound(min: minimumSectionSize(), val: size, max: maximumSectionSize()); |
| 831 | |
| 832 | if (isSectionHidden(logicalIndex: logical)) { |
| 833 | d->hiddenSectionSize.insert(key: logical, value: size); |
| 834 | return; |
| 835 | } |
| 836 | |
| 837 | int visual = visualIndex(logicalIndex: logical); |
| 838 | if (visual == -1) |
| 839 | return; |
| 840 | |
| 841 | if (d->state == QHeaderViewPrivate::ResizeSection && !d->cascadingResizing && logical != d->section) |
| 842 | d->preventCursorChangeInSetOffset = true; |
| 843 | |
| 844 | int oldSize = d->headerSectionSize(visual); |
| 845 | if (oldSize == size) |
| 846 | return; |
| 847 | |
| 848 | d->executePostedLayout(); |
| 849 | d->invalidateCachedSizeHint(); |
| 850 | |
| 851 | if (stretchLastSection() && logical == d->lastSectionLogicalIdx) |
| 852 | d->lastSectionSize = size; |
| 853 | |
| 854 | d->createSectionItems(start: visual, end: visual, sectionSize: size, mode: d->headerSectionResizeMode(visual)); |
| 855 | |
| 856 | if (!updatesEnabled()) { |
| 857 | if (d->hasAutoResizeSections()) |
| 858 | d->doDelayedResizeSections(); |
| 859 | emit sectionResized(logicalIndex: logical, oldSize, newSize: size); |
| 860 | return; |
| 861 | } |
| 862 | |
| 863 | int w = d->viewport->width(); |
| 864 | int h = d->viewport->height(); |
| 865 | int pos = sectionViewportPosition(logicalIndex: logical); |
| 866 | QRect r; |
| 867 | if (d->orientation == Qt::Horizontal) |
| 868 | if (isRightToLeft()) |
| 869 | r.setRect(ax: 0, ay: 0, aw: pos + size, ah: h); |
| 870 | else |
| 871 | r.setRect(ax: pos, ay: 0, aw: w - pos, ah: h); |
| 872 | else |
| 873 | r.setRect(ax: 0, ay: pos, aw: w, ah: h - pos); |
| 874 | |
| 875 | if (d->hasAutoResizeSections()) { |
| 876 | d->doDelayedResizeSections(); |
| 877 | r = d->viewport->rect(); |
| 878 | } |
| 879 | |
| 880 | // If the parent is a QAbstractScrollArea with QAbstractScrollArea::AdjustToContents |
| 881 | // then we want to change the geometry on that widget. Not doing it at once can/will |
| 882 | // cause scrollbars flicker as they would be shown at first but then removed. |
| 883 | // In the same situation it will also allow shrinking the whole view when stretchLastSection is set |
| 884 | // (It is default on QTreeViews - and it wouldn't shrink since the last stretch was made before the |
| 885 | // viewport was resized) |
| 886 | |
| 887 | QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea *>(object: parentWidget()); |
| 888 | if (parent && parent->sizeAdjustPolicy() == QAbstractScrollArea::AdjustToContents) |
| 889 | parent->updateGeometry(); |
| 890 | |
| 891 | d->viewport->update(r.normalized()); |
| 892 | emit sectionResized(logicalIndex: logical, oldSize, newSize: size); |
| 893 | } |
| 894 | |
| 895 | /*! |
| 896 | Resizes the sections according to the given \a mode, ignoring the current |
| 897 | resize mode. |
| 898 | |
| 899 | \sa sectionResized() |
| 900 | */ |
| 901 | |
| 902 | void QHeaderView::(QHeaderView::ResizeMode mode) |
| 903 | { |
| 904 | Q_D(QHeaderView); |
| 905 | d->resizeSections(globalMode: mode, useGlobalMode: true); |
| 906 | } |
| 907 | |
| 908 | /*! |
| 909 | \fn void QHeaderView::hideSection(int logicalIndex) |
| 910 | Hides the section specified by \a logicalIndex. |
| 911 | |
| 912 | \sa showSection(), isSectionHidden(), hiddenSectionCount(), |
| 913 | setSectionHidden() |
| 914 | */ |
| 915 | |
| 916 | /*! |
| 917 | \fn void QHeaderView::showSection(int logicalIndex) |
| 918 | Shows the section specified by \a logicalIndex. |
| 919 | |
| 920 | \sa hideSection(), isSectionHidden(), hiddenSectionCount(), |
| 921 | setSectionHidden() |
| 922 | */ |
| 923 | |
| 924 | /*! |
| 925 | Returns \c true if the section specified by \a logicalIndex is explicitly |
| 926 | hidden from the user; otherwise returns \c false. |
| 927 | |
| 928 | \sa hideSection(), showSection(), setSectionHidden(), hiddenSectionCount() |
| 929 | */ |
| 930 | |
| 931 | bool QHeaderView::(int logicalIndex) const |
| 932 | { |
| 933 | Q_D(const QHeaderView); |
| 934 | d->executePostedLayout(); |
| 935 | if (d->hiddenSectionSize.isEmpty() || logicalIndex < 0 || logicalIndex >= d->sectionCount()) |
| 936 | return false; |
| 937 | int visual = visualIndex(logicalIndex); |
| 938 | Q_ASSERT(visual != -1); |
| 939 | return d->isVisualIndexHidden(visual); |
| 940 | } |
| 941 | |
| 942 | /*! |
| 943 | \since 4.1 |
| 944 | |
| 945 | Returns the number of sections in the header that has been hidden. |
| 946 | |
| 947 | \sa setSectionHidden(), isSectionHidden() |
| 948 | */ |
| 949 | int QHeaderView::() const |
| 950 | { |
| 951 | Q_D(const QHeaderView); |
| 952 | return d->hiddenSectionSize.size(); |
| 953 | } |
| 954 | |
| 955 | /*! |
| 956 | If \a hide is true the section specified by \a logicalIndex is hidden; |
| 957 | otherwise the section is shown. |
| 958 | |
| 959 | \sa isSectionHidden(), hiddenSectionCount() |
| 960 | */ |
| 961 | |
| 962 | void QHeaderView::(int logicalIndex, bool hide) |
| 963 | { |
| 964 | Q_D(QHeaderView); |
| 965 | if (logicalIndex < 0 || logicalIndex >= count()) |
| 966 | return; |
| 967 | |
| 968 | d->executePostedLayout(); |
| 969 | int visual = visualIndex(logicalIndex); |
| 970 | Q_ASSERT(visual != -1); |
| 971 | if (hide == d->isVisualIndexHidden(visual)) |
| 972 | return; |
| 973 | if (hide) { |
| 974 | const bool isHidingLastSection = (stretchLastSection() && logicalIndex == d->lastSectionLogicalIdx); |
| 975 | if (isHidingLastSection) |
| 976 | d->restoreSizeOnPrevLastSection(); // Restore here/now to get the right restore size. |
| 977 | int size = d->headerSectionSize(visual); |
| 978 | if (!d->hasAutoResizeSections()) |
| 979 | resizeSection(logical: logicalIndex, size: 0); |
| 980 | d->hiddenSectionSize.insert(key: logicalIndex, value: size); |
| 981 | d->setVisualIndexHidden(visual, hidden: true); |
| 982 | if (isHidingLastSection) |
| 983 | d->setNewLastSection(d->lastVisibleVisualIndex()); |
| 984 | if (d->hasAutoResizeSections()) |
| 985 | d->doDelayedResizeSections(); |
| 986 | } else { |
| 987 | int size = d->hiddenSectionSize.value(key: logicalIndex, defaultValue: d->defaultSectionSize); |
| 988 | d->hiddenSectionSize.remove(key: logicalIndex); |
| 989 | d->setVisualIndexHidden(visual, hidden: false); |
| 990 | resizeSection(logical: logicalIndex, size); |
| 991 | |
| 992 | const bool newLastSection = (stretchLastSection() && visual > visualIndex(logicalIndex: d->lastSectionLogicalIdx)); |
| 993 | if (newLastSection) { |
| 994 | d->restoreSizeOnPrevLastSection(); |
| 995 | d->setNewLastSection(visual); |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | /*! |
| 1001 | Returns the number of sections in the header. |
| 1002 | |
| 1003 | \sa sectionCountChanged(), length() |
| 1004 | */ |
| 1005 | |
| 1006 | int QHeaderView::() const |
| 1007 | { |
| 1008 | Q_D(const QHeaderView); |
| 1009 | //Q_ASSERT(d->sectionCount == d->headerSectionCount()); |
| 1010 | // ### this may affect the lazy layout |
| 1011 | d->executePostedLayout(); |
| 1012 | return d->sectionCount(); |
| 1013 | } |
| 1014 | |
| 1015 | /*! |
| 1016 | Returns the visual index position of the section specified by the given |
| 1017 | \a logicalIndex, or -1 otherwise. |
| 1018 | |
| 1019 | Hidden sections still have valid visual indexes. |
| 1020 | |
| 1021 | \sa logicalIndex() |
| 1022 | */ |
| 1023 | |
| 1024 | int QHeaderView::(int logicalIndex) const |
| 1025 | { |
| 1026 | Q_D(const QHeaderView); |
| 1027 | if (logicalIndex < 0) |
| 1028 | return -1; |
| 1029 | d->executePostedLayout(); |
| 1030 | if (d->visualIndices.isEmpty()) { // nothing has been moved, so we have no mapping |
| 1031 | if (logicalIndex < d->sectionCount()) |
| 1032 | return logicalIndex; |
| 1033 | } else if (logicalIndex < d->visualIndices.size()) { |
| 1034 | int visual = d->visualIndices.at(i: logicalIndex); |
| 1035 | Q_ASSERT(visual < d->sectionCount()); |
| 1036 | return visual; |
| 1037 | } |
| 1038 | return -1; |
| 1039 | } |
| 1040 | |
| 1041 | /*! |
| 1042 | Returns the logicalIndex for the section at the given \a visualIndex |
| 1043 | position, or -1 if visualIndex < 0 or visualIndex >= QHeaderView::count(). |
| 1044 | |
| 1045 | Note that the visualIndex is not affected by hidden sections. |
| 1046 | |
| 1047 | \sa visualIndex(), sectionPosition() |
| 1048 | */ |
| 1049 | |
| 1050 | int QHeaderView::(int visualIndex) const |
| 1051 | { |
| 1052 | Q_D(const QHeaderView); |
| 1053 | if (visualIndex < 0 || visualIndex >= d->sectionCount()) |
| 1054 | return -1; |
| 1055 | return d->logicalIndex(visualIndex); |
| 1056 | } |
| 1057 | |
| 1058 | /*! |
| 1059 | \property QHeaderView::sectionsMovable |
| 1060 | |
| 1061 | If \a sectionsMovable is true, the header sections may be moved by the user; |
| 1062 | otherwise they are fixed in place. |
| 1063 | |
| 1064 | When used in combination with QTreeView, the first column is not |
| 1065 | movable (since it contains the tree structure), by default. |
| 1066 | You can make it movable with setFirstSectionMovable(true). |
| 1067 | |
| 1068 | \sa sectionMoved() |
| 1069 | \sa setFirstSectionMovable() |
| 1070 | */ |
| 1071 | |
| 1072 | /*! |
| 1073 | Sets \l sectionsMovable to \a movable. |
| 1074 | */ |
| 1075 | void QHeaderView::(bool movable) |
| 1076 | { |
| 1077 | Q_D(QHeaderView); |
| 1078 | d->movableSections = movable; |
| 1079 | } |
| 1080 | |
| 1081 | /*! |
| 1082 | Returns \l sectionsMovable. |
| 1083 | */ |
| 1084 | bool QHeaderView::() const |
| 1085 | { |
| 1086 | Q_D(const QHeaderView); |
| 1087 | return d->movableSections; |
| 1088 | } |
| 1089 | |
| 1090 | /*! |
| 1091 | \property QHeaderView::firstSectionMovable |
| 1092 | \brief Whether the first column can be moved by the user |
| 1093 | |
| 1094 | This property controls whether the first column can be moved by the user. |
| 1095 | In a QTreeView, the first column holds the tree structure and is |
| 1096 | therefore non-movable by default, even after setSectionsMovable(true). |
| 1097 | |
| 1098 | It can be made movable again, for instance in the case of flat lists |
| 1099 | without a tree structure, by calling this method. |
| 1100 | In such a scenario, it is recommended to call QTreeView::setRootIsDecorated(false) |
| 1101 | as well. |
| 1102 | |
| 1103 | \code |
| 1104 | treeView->setRootIsDecorated(false); |
| 1105 | treeView->header()->setFirstSectionMovable(true); |
| 1106 | \endcode |
| 1107 | |
| 1108 | Setting it to true has no effect unless setSectionsMovable(true) is called |
| 1109 | as well. |
| 1110 | |
| 1111 | \sa setSectionsMovable() |
| 1112 | \since 5.11 |
| 1113 | */ |
| 1114 | void QHeaderView::(bool movable) |
| 1115 | { |
| 1116 | Q_D(QHeaderView); |
| 1117 | d->allowUserMoveOfSection0 = movable; |
| 1118 | } |
| 1119 | |
| 1120 | bool QHeaderView::() const |
| 1121 | { |
| 1122 | Q_D(const QHeaderView); |
| 1123 | return d->allowUserMoveOfSection0; |
| 1124 | } |
| 1125 | |
| 1126 | /*! |
| 1127 | \property QHeaderView::sectionsClickable |
| 1128 | |
| 1129 | Holds \c true if the header is clickable; otherwise \c false. A |
| 1130 | clickable header could be set up to allow the user to change the |
| 1131 | representation of the data in the view related to the header. |
| 1132 | |
| 1133 | \sa sectionPressed(), setSortIndicatorShown() |
| 1134 | */ |
| 1135 | /*! |
| 1136 | Set \l sectionsClickable to \a clickable. |
| 1137 | */ |
| 1138 | void QHeaderView::(bool clickable) |
| 1139 | { |
| 1140 | Q_D(QHeaderView); |
| 1141 | d->clickableSections = clickable; |
| 1142 | } |
| 1143 | |
| 1144 | /*! |
| 1145 | Returns \l sectionsClickable. |
| 1146 | */ |
| 1147 | bool QHeaderView::() const |
| 1148 | { |
| 1149 | Q_D(const QHeaderView); |
| 1150 | return d->clickableSections; |
| 1151 | } |
| 1152 | |
| 1153 | void QHeaderView::(bool highlight) |
| 1154 | { |
| 1155 | Q_D(QHeaderView); |
| 1156 | d->highlightSelected = highlight; |
| 1157 | } |
| 1158 | |
| 1159 | bool QHeaderView::() const |
| 1160 | { |
| 1161 | Q_D(const QHeaderView); |
| 1162 | return d->highlightSelected; |
| 1163 | } |
| 1164 | |
| 1165 | /*! |
| 1166 | \since 5.0 |
| 1167 | |
| 1168 | Sets the constraints on how the header can be resized to those described |
| 1169 | by the given \a mode. |
| 1170 | |
| 1171 | \sa length(), sectionResized() |
| 1172 | */ |
| 1173 | |
| 1174 | void QHeaderView::(ResizeMode mode) |
| 1175 | { |
| 1176 | Q_D(QHeaderView); |
| 1177 | initializeSections(); |
| 1178 | d->stretchSections = (mode == Stretch ? count() : 0); |
| 1179 | d->contentsSections = (mode == ResizeToContents ? count() : 0); |
| 1180 | d->setGlobalHeaderResizeMode(mode); |
| 1181 | if (d->hasAutoResizeSections()) |
| 1182 | d->doDelayedResizeSections(); // section sizes may change as a result of the new mode |
| 1183 | } |
| 1184 | |
| 1185 | /*! |
| 1186 | \since 5.0 |
| 1187 | |
| 1188 | Sets the constraints on how the section specified by \a logicalIndex in |
| 1189 | the header can be resized to those described by the given \a mode. The logical |
| 1190 | index should exist at the time this function is called. |
| 1191 | |
| 1192 | \note This setting will be ignored for the last section if the stretchLastSection |
| 1193 | property is set to true. This is the default for the horizontal headers provided |
| 1194 | by QTreeView. |
| 1195 | |
| 1196 | \sa setStretchLastSection(), resizeContentsPrecision() |
| 1197 | */ |
| 1198 | |
| 1199 | void QHeaderView::(int logicalIndex, ResizeMode mode) |
| 1200 | { |
| 1201 | Q_D(QHeaderView); |
| 1202 | int visual = visualIndex(logicalIndex); |
| 1203 | Q_ASSERT(visual != -1); |
| 1204 | |
| 1205 | ResizeMode old = d->headerSectionResizeMode(visual); |
| 1206 | d->setHeaderSectionResizeMode(visual, mode); |
| 1207 | |
| 1208 | if (mode == Stretch && old != Stretch) |
| 1209 | ++d->stretchSections; |
| 1210 | else if (mode == ResizeToContents && old != ResizeToContents) |
| 1211 | ++d->contentsSections; |
| 1212 | else if (mode != Stretch && old == Stretch) |
| 1213 | --d->stretchSections; |
| 1214 | else if (mode != ResizeToContents && old == ResizeToContents) |
| 1215 | --d->contentsSections; |
| 1216 | |
| 1217 | if (d->hasAutoResizeSections() && d->state == QHeaderViewPrivate::NoState) |
| 1218 | d->doDelayedResizeSections(); // section sizes may change as a result of the new mode |
| 1219 | } |
| 1220 | |
| 1221 | /*! |
| 1222 | \since 5.0 |
| 1223 | |
| 1224 | Returns the resize mode that applies to the section specified by the given |
| 1225 | \a logicalIndex. |
| 1226 | |
| 1227 | \sa setSectionResizeMode() |
| 1228 | */ |
| 1229 | |
| 1230 | QHeaderView::ResizeMode QHeaderView::(int logicalIndex) const |
| 1231 | { |
| 1232 | Q_D(const QHeaderView); |
| 1233 | int visual = visualIndex(logicalIndex); |
| 1234 | if (visual == -1) |
| 1235 | return Fixed; //the default value |
| 1236 | return d->headerSectionResizeMode(visual); |
| 1237 | } |
| 1238 | |
| 1239 | /*! |
| 1240 | \since 5.2 |
| 1241 | Sets how precise QHeaderView should calculate the size when ResizeToContents is used. |
| 1242 | A low value will provide a less accurate but fast auto resize while a higher |
| 1243 | value will provide a more accurate resize that however can be slow. |
| 1244 | |
| 1245 | The number \a precision specifies how many sections that should be consider |
| 1246 | when calculating the preferred size. |
| 1247 | |
| 1248 | The default value is 1000 meaning that a horizontal column with auto-resize will look |
| 1249 | at maximum 1000 rows on calculating when doing an auto resize. |
| 1250 | |
| 1251 | Special value 0 means that it will look at only the visible area. |
| 1252 | Special value -1 will imply looking at all elements. |
| 1253 | |
| 1254 | This value is used in QTableView::sizeHintForColumn(), QTableView::sizeHintForRow() |
| 1255 | and QTreeView::sizeHintForColumn(). Reimplementing these functions can make this |
| 1256 | function not having an effect. |
| 1257 | |
| 1258 | \sa resizeContentsPrecision(), setSectionResizeMode(), resizeSections(), QTableView::sizeHintForColumn(), QTableView::sizeHintForRow(), QTreeView::sizeHintForColumn() |
| 1259 | */ |
| 1260 | |
| 1261 | void QHeaderView::(int precision) |
| 1262 | { |
| 1263 | Q_D(QHeaderView); |
| 1264 | d->resizeContentsPrecision = precision; |
| 1265 | } |
| 1266 | |
| 1267 | /*! |
| 1268 | \since 5.2 |
| 1269 | Returns how precise QHeaderView will calculate on ResizeToContents. |
| 1270 | |
| 1271 | \sa setResizeContentsPrecision(), setSectionResizeMode() |
| 1272 | |
| 1273 | */ |
| 1274 | |
| 1275 | int QHeaderView::() const |
| 1276 | { |
| 1277 | Q_D(const QHeaderView); |
| 1278 | return d->resizeContentsPrecision; |
| 1279 | } |
| 1280 | |
| 1281 | /*! |
| 1282 | \since 4.1 |
| 1283 | |
| 1284 | Returns the number of sections that are set to resize mode stretch. In |
| 1285 | views, this can be used to see if the headerview needs to resize the |
| 1286 | sections when the view's geometry changes. |
| 1287 | |
| 1288 | \sa stretchLastSection |
| 1289 | */ |
| 1290 | |
| 1291 | int QHeaderView::() const |
| 1292 | { |
| 1293 | Q_D(const QHeaderView); |
| 1294 | return d->stretchSections; |
| 1295 | } |
| 1296 | |
| 1297 | /*! |
| 1298 | \property QHeaderView::showSortIndicator |
| 1299 | \brief whether the sort indicator is shown |
| 1300 | |
| 1301 | By default, this property is \c false. |
| 1302 | |
| 1303 | \sa setSectionsClickable() |
| 1304 | */ |
| 1305 | |
| 1306 | void QHeaderView::(bool show) |
| 1307 | { |
| 1308 | Q_D(QHeaderView); |
| 1309 | if (d->sortIndicatorShown == show) |
| 1310 | return; |
| 1311 | |
| 1312 | d->sortIndicatorShown = show; |
| 1313 | |
| 1314 | if (sortIndicatorSection() < 0 || sortIndicatorSection() > count()) |
| 1315 | return; |
| 1316 | |
| 1317 | if (d->headerSectionResizeMode(visual: sortIndicatorSection()) == ResizeToContents) |
| 1318 | resizeSections(); |
| 1319 | |
| 1320 | d->viewport->update(); |
| 1321 | } |
| 1322 | |
| 1323 | bool QHeaderView::() const |
| 1324 | { |
| 1325 | Q_D(const QHeaderView); |
| 1326 | return d->sortIndicatorShown; |
| 1327 | } |
| 1328 | |
| 1329 | /*! |
| 1330 | Sets the sort indicator for the section specified by the given |
| 1331 | \a logicalIndex in the direction specified by \a order, and removes the |
| 1332 | sort indicator from any other section that was showing it. |
| 1333 | |
| 1334 | \a logicalIndex may be -1, in which case no sort indicator will be shown |
| 1335 | and the model will return to its natural, unsorted order. Note that not |
| 1336 | all models support this and may even crash in this case. |
| 1337 | |
| 1338 | \sa sortIndicatorSection(), sortIndicatorOrder() |
| 1339 | */ |
| 1340 | |
| 1341 | void QHeaderView::(int logicalIndex, Qt::SortOrder order) |
| 1342 | { |
| 1343 | Q_D(QHeaderView); |
| 1344 | |
| 1345 | // This is so that people can set the position of the sort indicator before the fill the model |
| 1346 | int old = d->sortIndicatorSection; |
| 1347 | if (old == logicalIndex && order == d->sortIndicatorOrder) |
| 1348 | return; |
| 1349 | d->sortIndicatorSection = logicalIndex; |
| 1350 | d->sortIndicatorOrder = order; |
| 1351 | |
| 1352 | if (logicalIndex >= d->sectionCount()) { |
| 1353 | emit sortIndicatorChanged(logicalIndex, order); |
| 1354 | return; // nothing to do |
| 1355 | } |
| 1356 | |
| 1357 | if (old != logicalIndex |
| 1358 | && ((logicalIndex >= 0 && sectionResizeMode(logicalIndex) == ResizeToContents) |
| 1359 | || old >= d->sectionCount() || (old >= 0 && sectionResizeMode(logicalIndex: old) == ResizeToContents))) { |
| 1360 | resizeSections(); |
| 1361 | d->viewport->update(); |
| 1362 | } else { |
| 1363 | if (old >= 0 && old != logicalIndex) |
| 1364 | updateSection(logicalIndex: old); |
| 1365 | if (logicalIndex >= 0) |
| 1366 | updateSection(logicalIndex); |
| 1367 | } |
| 1368 | |
| 1369 | emit sortIndicatorChanged(logicalIndex, order); |
| 1370 | } |
| 1371 | |
| 1372 | /*! |
| 1373 | Returns the logical index of the section that has a sort indicator. |
| 1374 | By default this is section 0. |
| 1375 | |
| 1376 | \sa setSortIndicator(), sortIndicatorOrder(), setSortIndicatorShown() |
| 1377 | */ |
| 1378 | |
| 1379 | int QHeaderView::() const |
| 1380 | { |
| 1381 | Q_D(const QHeaderView); |
| 1382 | return d->sortIndicatorSection; |
| 1383 | } |
| 1384 | |
| 1385 | /*! |
| 1386 | Returns the order for the sort indicator. If no section has a sort |
| 1387 | indicator the return value of this function is undefined. |
| 1388 | |
| 1389 | \sa setSortIndicator(), sortIndicatorSection() |
| 1390 | */ |
| 1391 | |
| 1392 | Qt::SortOrder QHeaderView::() const |
| 1393 | { |
| 1394 | Q_D(const QHeaderView); |
| 1395 | return d->sortIndicatorOrder; |
| 1396 | } |
| 1397 | |
| 1398 | /*! |
| 1399 | \property QHeaderView::sortIndicatorClearable |
| 1400 | \brief Whether the sort indicator can be cleared by clicking on a section multiple times |
| 1401 | \since 6.1 |
| 1402 | |
| 1403 | This property controls whether the user is able to remove the |
| 1404 | sorting indicator on a given section by clicking on the section |
| 1405 | multiple times. Normally, clicking on a section will simply change |
| 1406 | the sorting order for that section. By setting this property to |
| 1407 | true, the sorting indicator will be cleared after alternating to |
| 1408 | ascending and descending; this will typically restore the original |
| 1409 | sorting of a model. |
| 1410 | |
| 1411 | Setting this property to true has no effect unless |
| 1412 | sectionsClickable() is also true (which is the default for certain |
| 1413 | views, for instance QTableView, or is automatically set when making |
| 1414 | a view sortable, for instance by calling |
| 1415 | QTreeView::setSortingEnabled). |
| 1416 | */ |
| 1417 | |
| 1418 | void QHeaderView::(bool clearable) |
| 1419 | { |
| 1420 | Q_D(QHeaderView); |
| 1421 | if (d->sortIndicatorClearable == clearable) |
| 1422 | return; |
| 1423 | d->sortIndicatorClearable = clearable; |
| 1424 | emit sortIndicatorClearableChanged(clearable); |
| 1425 | } |
| 1426 | |
| 1427 | bool QHeaderView::() const |
| 1428 | { |
| 1429 | Q_D(const QHeaderView); |
| 1430 | return d->sortIndicatorClearable; |
| 1431 | } |
| 1432 | |
| 1433 | /*! |
| 1434 | \property QHeaderView::stretchLastSection |
| 1435 | \brief whether the last visible section in the header takes up all the |
| 1436 | available space |
| 1437 | |
| 1438 | The default value is false. |
| 1439 | |
| 1440 | \note The horizontal headers provided by QTreeView are configured with this |
| 1441 | property set to true, ensuring that the view does not waste any of the |
| 1442 | space assigned to it for its header. If this value is set to true, this |
| 1443 | property will override the resize mode set on the last section in the |
| 1444 | header. |
| 1445 | |
| 1446 | \sa setSectionResizeMode() |
| 1447 | */ |
| 1448 | bool QHeaderView::() const |
| 1449 | { |
| 1450 | Q_D(const QHeaderView); |
| 1451 | return d->stretchLastSection; |
| 1452 | } |
| 1453 | |
| 1454 | void QHeaderView::(bool stretch) |
| 1455 | { |
| 1456 | Q_D(QHeaderView); |
| 1457 | if (d->stretchLastSection == stretch) |
| 1458 | return; |
| 1459 | d->stretchLastSection = stretch; |
| 1460 | if (d->state != QHeaderViewPrivate::NoState) |
| 1461 | return; |
| 1462 | if (stretch) { |
| 1463 | d->setNewLastSection(d->lastVisibleVisualIndex()); |
| 1464 | resizeSections(); |
| 1465 | } else { |
| 1466 | d->restoreSizeOnPrevLastSection(); |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | /*! |
| 1471 | \since 4.2 |
| 1472 | \property QHeaderView::cascadingSectionResizes |
| 1473 | \brief whether interactive resizing will be cascaded to the following |
| 1474 | sections once the section being resized by the user has reached its |
| 1475 | minimum size |
| 1476 | |
| 1477 | This property only affects sections that have \l Interactive as their |
| 1478 | resize mode. |
| 1479 | |
| 1480 | The default value is false. |
| 1481 | |
| 1482 | \sa setSectionResizeMode() |
| 1483 | */ |
| 1484 | bool QHeaderView::() const |
| 1485 | { |
| 1486 | Q_D(const QHeaderView); |
| 1487 | return d->cascadingResizing; |
| 1488 | } |
| 1489 | |
| 1490 | void QHeaderView::(bool enable) |
| 1491 | { |
| 1492 | Q_D(QHeaderView); |
| 1493 | d->cascadingResizing = enable; |
| 1494 | } |
| 1495 | |
| 1496 | /*! |
| 1497 | \property QHeaderView::defaultSectionSize |
| 1498 | \brief the default size of the header sections before resizing. |
| 1499 | |
| 1500 | This property only affects sections that have \l Interactive or \l Fixed |
| 1501 | as their resize mode. |
| 1502 | |
| 1503 | By default, the value of this property is style dependent. |
| 1504 | Thus, when the style changes, this property updates from it. |
| 1505 | Calling setDefaultSectionSize() stops the updates, calling |
| 1506 | resetDefaultSectionSize() will restore default behavior. |
| 1507 | |
| 1508 | \sa setSectionResizeMode(), minimumSectionSize |
| 1509 | */ |
| 1510 | int QHeaderView::() const |
| 1511 | { |
| 1512 | Q_D(const QHeaderView); |
| 1513 | return d->defaultSectionSize; |
| 1514 | } |
| 1515 | |
| 1516 | void QHeaderView::(int size) |
| 1517 | { |
| 1518 | Q_D(QHeaderView); |
| 1519 | if (size < 0 || size > maxSizeSection) |
| 1520 | return; |
| 1521 | d->oldDefaultSectionSize = d->defaultSectionSize; |
| 1522 | d->setDefaultSectionSize(size); |
| 1523 | } |
| 1524 | |
| 1525 | void QHeaderView::() |
| 1526 | { |
| 1527 | Q_D(QHeaderView); |
| 1528 | if (d->customDefaultSectionSize) { |
| 1529 | d->oldDefaultSectionSize = d->defaultSectionSize; |
| 1530 | d->updateDefaultSectionSizeFromStyle(); |
| 1531 | d->setDefaultSectionSize(d->defaultSectionSize); |
| 1532 | d->customDefaultSectionSize = false; |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | /*! |
| 1537 | \since 4.2 |
| 1538 | \property QHeaderView::minimumSectionSize |
| 1539 | \brief the minimum size of the header sections. |
| 1540 | |
| 1541 | The minimum section size is the smallest section size allowed. If the |
| 1542 | minimum section size is set to -1, QHeaderView will use the |
| 1543 | \l{fontMetrics()}{font metrics} size. |
| 1544 | |
| 1545 | This property is honored by all \l{ResizeMode}{resize modes}. |
| 1546 | |
| 1547 | \sa setSectionResizeMode(), defaultSectionSize |
| 1548 | */ |
| 1549 | int QHeaderView::() const |
| 1550 | { |
| 1551 | Q_D(const QHeaderView); |
| 1552 | if (d->minimumSectionSize == -1) { |
| 1553 | int margin = 2 * style()->pixelMetric(metric: QStyle::PM_HeaderMargin, option: nullptr, widget: this); |
| 1554 | if (d->orientation == Qt::Horizontal) |
| 1555 | return fontMetrics().maxWidth() + margin; |
| 1556 | return fontMetrics().height() + margin; |
| 1557 | } |
| 1558 | return d->minimumSectionSize; |
| 1559 | } |
| 1560 | |
| 1561 | void QHeaderView::(int size) |
| 1562 | { |
| 1563 | Q_D(QHeaderView); |
| 1564 | if (size < -1 || size > maxSizeSection) |
| 1565 | return; |
| 1566 | // larger new min size - check current section sizes |
| 1567 | const bool needSizeCheck = size > d->minimumSectionSize; |
| 1568 | d->minimumSectionSize = size; |
| 1569 | if (d->minimumSectionSize > maximumSectionSize()) |
| 1570 | setMaximumSectionSize(size); |
| 1571 | |
| 1572 | if (needSizeCheck) { |
| 1573 | if (d->hasAutoResizeSections()) { |
| 1574 | d->doDelayedResizeSections(); |
| 1575 | } else { |
| 1576 | for (int visual = 0; visual < d->sectionCount(); ++visual) { |
| 1577 | if (d->isVisualIndexHidden(visual)) |
| 1578 | continue; |
| 1579 | if (d->headerSectionSize(visual) < d->minimumSectionSize) |
| 1580 | resizeSection(logical: logicalIndex(visualIndex: visual), size); |
| 1581 | } |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | } |
| 1586 | |
| 1587 | /*! |
| 1588 | \since 5.2 |
| 1589 | \property QHeaderView::maximumSectionSize |
| 1590 | \brief the maximum size of the header sections. |
| 1591 | |
| 1592 | The maximum section size is the largest section size allowed. |
| 1593 | The default value for this property is 1048575, which is also the largest |
| 1594 | possible size for a section. Setting maximum to -1 will reset the value to |
| 1595 | the largest section size. |
| 1596 | |
| 1597 | With exception of stretch this property is honored by all \l{ResizeMode}{resize modes} |
| 1598 | |
| 1599 | \sa setSectionResizeMode(), defaultSectionSize |
| 1600 | */ |
| 1601 | int QHeaderView::() const |
| 1602 | { |
| 1603 | Q_D(const QHeaderView); |
| 1604 | if (d->maximumSectionSize == -1) |
| 1605 | return maxSizeSection; |
| 1606 | return d->maximumSectionSize; |
| 1607 | } |
| 1608 | |
| 1609 | void QHeaderView::(int size) |
| 1610 | { |
| 1611 | Q_D(QHeaderView); |
| 1612 | if (size == -1) { |
| 1613 | d->maximumSectionSize = maxSizeSection; |
| 1614 | return; |
| 1615 | } |
| 1616 | if (size < 0 || size > maxSizeSection) |
| 1617 | return; |
| 1618 | if (minimumSectionSize() > size) |
| 1619 | d->minimumSectionSize = size; |
| 1620 | |
| 1621 | // smaller new max size - check current section sizes |
| 1622 | const bool needSizeCheck = size < d->maximumSectionSize; |
| 1623 | d->maximumSectionSize = size; |
| 1624 | |
| 1625 | if (needSizeCheck) { |
| 1626 | if (d->hasAutoResizeSections()) { |
| 1627 | d->doDelayedResizeSections(); |
| 1628 | } else { |
| 1629 | for (int visual = 0; visual < d->sectionCount(); ++visual) { |
| 1630 | if (d->isVisualIndexHidden(visual)) |
| 1631 | continue; |
| 1632 | if (d->headerSectionSize(visual) > d->maximumSectionSize) |
| 1633 | resizeSection(logical: logicalIndex(visualIndex: visual), size); |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | |
| 1640 | /*! |
| 1641 | \since 4.1 |
| 1642 | \property QHeaderView::defaultAlignment |
| 1643 | \brief the default alignment of the text in each header section |
| 1644 | */ |
| 1645 | |
| 1646 | Qt::Alignment QHeaderView::() const |
| 1647 | { |
| 1648 | Q_D(const QHeaderView); |
| 1649 | return d->defaultAlignment; |
| 1650 | } |
| 1651 | |
| 1652 | void QHeaderView::(Qt::Alignment alignment) |
| 1653 | { |
| 1654 | Q_D(QHeaderView); |
| 1655 | if (d->defaultAlignment == alignment) |
| 1656 | return; |
| 1657 | |
| 1658 | d->defaultAlignment = alignment; |
| 1659 | d->viewport->update(); |
| 1660 | } |
| 1661 | |
| 1662 | /*! |
| 1663 | \internal |
| 1664 | */ |
| 1665 | void QHeaderView::() |
| 1666 | { |
| 1667 | initializeSections(); |
| 1668 | QAbstractItemView::doItemsLayout(); |
| 1669 | } |
| 1670 | |
| 1671 | /*! |
| 1672 | Returns \c true if sections in the header has been moved; otherwise returns |
| 1673 | false; |
| 1674 | |
| 1675 | \sa moveSection() |
| 1676 | */ |
| 1677 | bool QHeaderView::() const |
| 1678 | { |
| 1679 | Q_D(const QHeaderView); |
| 1680 | return !d->visualIndices.isEmpty(); |
| 1681 | } |
| 1682 | |
| 1683 | /*! |
| 1684 | \since 4.1 |
| 1685 | |
| 1686 | Returns \c true if sections in the header has been hidden; otherwise returns |
| 1687 | false; |
| 1688 | |
| 1689 | \sa setSectionHidden() |
| 1690 | */ |
| 1691 | bool QHeaderView::() const |
| 1692 | { |
| 1693 | Q_D(const QHeaderView); |
| 1694 | return !d->hiddenSectionSize.isEmpty(); |
| 1695 | } |
| 1696 | |
| 1697 | #ifndef QT_NO_DATASTREAM |
| 1698 | /*! |
| 1699 | \since 4.3 |
| 1700 | |
| 1701 | Saves the current state of this header view. |
| 1702 | |
| 1703 | To restore the saved state, pass the return value to restoreState(). |
| 1704 | |
| 1705 | \sa restoreState() |
| 1706 | */ |
| 1707 | QByteArray QHeaderView::() const |
| 1708 | { |
| 1709 | Q_D(const QHeaderView); |
| 1710 | QByteArray data; |
| 1711 | QDataStream stream(&data, QIODevice::WriteOnly); |
| 1712 | stream.setVersion(QDataStream::Qt_5_0); |
| 1713 | stream << QHeaderViewPrivate::VersionMarker; |
| 1714 | stream << 0; // current version is 0 |
| 1715 | d->write(out&: stream); |
| 1716 | return data; |
| 1717 | } |
| 1718 | |
| 1719 | /*! |
| 1720 | \since 4.3 |
| 1721 | Restores the \a state of this header view. |
| 1722 | This function returns \c true if the state was restored; otherwise returns |
| 1723 | false. |
| 1724 | |
| 1725 | \sa saveState() |
| 1726 | */ |
| 1727 | bool QHeaderView::(const QByteArray &state) |
| 1728 | { |
| 1729 | Q_D(QHeaderView); |
| 1730 | if (state.isEmpty()) |
| 1731 | return false; |
| 1732 | |
| 1733 | for (const auto dataStreamVersion : {QDataStream::Qt_5_0, QDataStream::Qt_6_0}) { |
| 1734 | |
| 1735 | QByteArray data = state; |
| 1736 | QDataStream stream(&data, QIODevice::ReadOnly); |
| 1737 | stream.setVersion(dataStreamVersion); |
| 1738 | int marker; |
| 1739 | int ver; |
| 1740 | stream >> marker; |
| 1741 | stream >> ver; |
| 1742 | if (stream.status() != QDataStream::Ok |
| 1743 | || marker != QHeaderViewPrivate::VersionMarker |
| 1744 | || ver != 0) { // current version is 0 |
| 1745 | return false; |
| 1746 | } |
| 1747 | |
| 1748 | if (d->read(in&: stream)) { |
| 1749 | emit sortIndicatorChanged(logicalIndex: d->sortIndicatorSection, order: d->sortIndicatorOrder ); |
| 1750 | d->viewport->update(); |
| 1751 | return true; |
| 1752 | } |
| 1753 | } |
| 1754 | return false; |
| 1755 | } |
| 1756 | #endif // QT_NO_DATASTREAM |
| 1757 | |
| 1758 | /*! |
| 1759 | \reimp |
| 1760 | */ |
| 1761 | void QHeaderView::() |
| 1762 | { |
| 1763 | Q_D(QHeaderView); |
| 1764 | QAbstractItemView::reset(); |
| 1765 | // it would be correct to call clear, but some apps rely |
| 1766 | // on the header keeping the sections, even after calling reset |
| 1767 | //d->clear(); |
| 1768 | initializeSections(); |
| 1769 | d->invalidateCachedSizeHint(); |
| 1770 | } |
| 1771 | |
| 1772 | /*! |
| 1773 | Updates the changed header sections with the given \a orientation, from |
| 1774 | \a logicalFirst to \a logicalLast inclusive. |
| 1775 | */ |
| 1776 | void QHeaderView::(Qt::Orientation orientation, int logicalFirst, int logicalLast) |
| 1777 | { |
| 1778 | Q_D(QHeaderView); |
| 1779 | if (d->orientation != orientation) |
| 1780 | return; |
| 1781 | |
| 1782 | if (logicalFirst < 0 || logicalLast < 0 || logicalFirst >= count() || logicalLast >= count()) |
| 1783 | return; |
| 1784 | |
| 1785 | d->invalidateCachedSizeHint(); |
| 1786 | |
| 1787 | int firstVisualIndex = INT_MAX, lastVisualIndex = -1; |
| 1788 | |
| 1789 | for (int section = logicalFirst; section <= logicalLast; ++section) { |
| 1790 | const int visual = visualIndex(logicalIndex: section); |
| 1791 | firstVisualIndex = qMin(a: firstVisualIndex, b: visual); |
| 1792 | lastVisualIndex = qMax(a: lastVisualIndex, b: visual); |
| 1793 | } |
| 1794 | |
| 1795 | d->executePostedResize(); |
| 1796 | const int first = d->headerSectionPosition(visual: firstVisualIndex), |
| 1797 | last = d->headerSectionPosition(visual: lastVisualIndex) |
| 1798 | + d->headerSectionSize(visual: lastVisualIndex); |
| 1799 | |
| 1800 | if (orientation == Qt::Horizontal) { |
| 1801 | d->viewport->update(ax: first, ay: 0, aw: last - first, ah: d->viewport->height()); |
| 1802 | } else { |
| 1803 | d->viewport->update(ax: 0, ay: first, aw: d->viewport->width(), ah: last - first); |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | /*! |
| 1808 | \internal |
| 1809 | \since 4.2 |
| 1810 | |
| 1811 | Updates the section specified by the given \a logicalIndex. |
| 1812 | */ |
| 1813 | |
| 1814 | void QHeaderView::(int logicalIndex) |
| 1815 | { |
| 1816 | Q_D(QHeaderView); |
| 1817 | if (d->orientation == Qt::Horizontal) |
| 1818 | d->viewport->update(QRect(sectionViewportPosition(logicalIndex), |
| 1819 | 0, sectionSize(logicalIndex), d->viewport->height())); |
| 1820 | else |
| 1821 | d->viewport->update(QRect(0, sectionViewportPosition(logicalIndex), |
| 1822 | d->viewport->width(), sectionSize(logicalIndex))); |
| 1823 | } |
| 1824 | |
| 1825 | /*! |
| 1826 | Resizes the sections according to their size hints. Normally, you do not |
| 1827 | have to call this function. |
| 1828 | */ |
| 1829 | |
| 1830 | void QHeaderView::() |
| 1831 | { |
| 1832 | Q_D(QHeaderView); |
| 1833 | if (d->hasAutoResizeSections()) |
| 1834 | d->resizeSections(globalMode: Interactive, useGlobalMode: false); // no global resize mode |
| 1835 | } |
| 1836 | |
| 1837 | /*! |
| 1838 | This slot is called when sections are inserted into the \a parent. |
| 1839 | \a logicalFirst and \a logicalLast indices signify where the new sections |
| 1840 | were inserted. |
| 1841 | |
| 1842 | If only one section is inserted, \a logicalFirst and \a logicalLast will |
| 1843 | be the same. |
| 1844 | */ |
| 1845 | |
| 1846 | void QHeaderView::(const QModelIndex &parent, |
| 1847 | int logicalFirst, int logicalLast) |
| 1848 | { |
| 1849 | Q_D(QHeaderView); |
| 1850 | // only handle root level changes and return on no-op |
| 1851 | if (parent != d->root || d->modelSectionCount() == d->sectionCount()) |
| 1852 | return; |
| 1853 | int oldCount = d->sectionCount(); |
| 1854 | |
| 1855 | d->invalidateCachedSizeHint(); |
| 1856 | |
| 1857 | if (d->state == QHeaderViewPrivate::ResizeSection) |
| 1858 | d->preventCursorChangeInSetOffset = true; |
| 1859 | |
| 1860 | // add the new sections |
| 1861 | int insertAt = logicalFirst; |
| 1862 | int insertCount = logicalLast - logicalFirst + 1; |
| 1863 | |
| 1864 | bool lastSectionActualChange = false; |
| 1865 | if (stretchLastSection()) { |
| 1866 | |
| 1867 | int visualIndexForStretch = d->lastSectionLogicalIdx; |
| 1868 | if (d->lastSectionLogicalIdx >= 0 && d->lastSectionLogicalIdx < d->visualIndices.size()) |
| 1869 | visualIndexForStretch = d->visualIndices[d->lastSectionLogicalIdx]; // We cannot call visualIndex since it executes executePostedLayout() |
| 1870 | // and it is likely to bypass initializeSections() and we may end up here again. Doing the insert twice. |
| 1871 | |
| 1872 | if (d->lastSectionLogicalIdx < 0 || insertAt >= visualIndexForStretch) |
| 1873 | lastSectionActualChange = true; |
| 1874 | |
| 1875 | if (d->lastSectionLogicalIdx >= logicalFirst) |
| 1876 | d->lastSectionLogicalIdx += insertCount; // We do not want to emit resize before we have fixed the count |
| 1877 | } |
| 1878 | |
| 1879 | QHeaderViewPrivate::SectionItem section(d->defaultSectionSize, d->globalResizeMode); |
| 1880 | d->sectionStartposRecalc = true; |
| 1881 | |
| 1882 | if (d->sectionItems.isEmpty() || insertAt >= d->sectionItems.size()) { |
| 1883 | int insertLength = d->defaultSectionSize * insertCount; |
| 1884 | d->length += insertLength; |
| 1885 | d->sectionItems.insert(i: d->sectionItems.size(), n: insertCount, t: section); // append |
| 1886 | } else { |
| 1887 | // separate them out into their own sections |
| 1888 | int insertLength = d->defaultSectionSize * insertCount; |
| 1889 | d->length += insertLength; |
| 1890 | d->sectionItems.insert(i: insertAt, n: insertCount, t: section); |
| 1891 | } |
| 1892 | |
| 1893 | // update sorting column |
| 1894 | if (d->sortIndicatorSection >= logicalFirst) |
| 1895 | d->sortIndicatorSection += insertCount; |
| 1896 | |
| 1897 | // update resize mode section counts |
| 1898 | if (d->globalResizeMode == Stretch) |
| 1899 | d->stretchSections = d->sectionCount(); |
| 1900 | else if (d->globalResizeMode == ResizeToContents) |
| 1901 | d->contentsSections = d->sectionCount(); |
| 1902 | |
| 1903 | // clear selection cache |
| 1904 | d->sectionSelected.clear(); |
| 1905 | |
| 1906 | // update mapping |
| 1907 | if (!d->visualIndices.isEmpty() && !d->logicalIndices.isEmpty()) { |
| 1908 | Q_ASSERT(d->visualIndices.size() == d->logicalIndices.size()); |
| 1909 | int mappingCount = d->visualIndices.size(); |
| 1910 | for (int i = 0; i < mappingCount; ++i) { |
| 1911 | if (d->visualIndices.at(i) >= logicalFirst) |
| 1912 | d->visualIndices[i] += insertCount; |
| 1913 | if (d->logicalIndices.at(i) >= logicalFirst) |
| 1914 | d->logicalIndices[i] += insertCount; |
| 1915 | } |
| 1916 | for (int j = logicalFirst; j <= logicalLast; ++j) { |
| 1917 | d->visualIndices.insert(i: j, t: j); |
| 1918 | d->logicalIndices.insert(i: j, t: j); |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | // insert sections into hiddenSectionSize |
| 1923 | QHash<int, int> newHiddenSectionSize; // from logical index to section size |
| 1924 | for (QHash<int, int>::const_iterator it = d->hiddenSectionSize.cbegin(), |
| 1925 | end = d->hiddenSectionSize.cend(); it != end; ++it) { |
| 1926 | const int oldIndex = it.key(); |
| 1927 | const int newIndex = (oldIndex < logicalFirst) ? oldIndex : oldIndex + insertCount; |
| 1928 | newHiddenSectionSize[newIndex] = it.value(); |
| 1929 | } |
| 1930 | d->hiddenSectionSize.swap(other&: newHiddenSectionSize); |
| 1931 | |
| 1932 | d->doDelayedResizeSections(); |
| 1933 | emit sectionCountChanged(oldCount, newCount: count()); |
| 1934 | |
| 1935 | if (lastSectionActualChange) |
| 1936 | d->maybeRestorePrevLastSectionAndStretchLast(); |
| 1937 | |
| 1938 | // if the new sections were not updated by resizing, we need to update now |
| 1939 | if (!d->hasAutoResizeSections()) |
| 1940 | d->viewport->update(); |
| 1941 | } |
| 1942 | |
| 1943 | /*! |
| 1944 | This slot is called when sections are removed from the \a parent. |
| 1945 | \a logicalFirst and \a logicalLast signify where the sections were removed. |
| 1946 | |
| 1947 | If only one section is removed, \a logicalFirst and \a logicalLast will |
| 1948 | be the same. |
| 1949 | */ |
| 1950 | |
| 1951 | void QHeaderView::(const QModelIndex &parent, |
| 1952 | int logicalFirst, int logicalLast) |
| 1953 | { |
| 1954 | Q_UNUSED(parent); |
| 1955 | Q_UNUSED(logicalFirst); |
| 1956 | Q_UNUSED(logicalLast); |
| 1957 | } |
| 1958 | |
| 1959 | void QHeaderViewPrivate::(int logicalFirst, int logicalLast) |
| 1960 | { |
| 1961 | Q_Q(QHeaderView); |
| 1962 | const int changeCount = logicalLast - logicalFirst + 1; |
| 1963 | |
| 1964 | // remove sections from hiddenSectionSize |
| 1965 | QHash<int, int> newHiddenSectionSize; // from logical index to section size |
| 1966 | for (int i = 0; i < logicalFirst; ++i) |
| 1967 | if (q->isSectionHidden(logicalIndex: i)) |
| 1968 | newHiddenSectionSize[i] = hiddenSectionSize[i]; |
| 1969 | for (int j = logicalLast + 1; j < sectionCount(); ++j) |
| 1970 | if (q->isSectionHidden(logicalIndex: j)) |
| 1971 | newHiddenSectionSize[j - changeCount] = hiddenSectionSize[j]; |
| 1972 | hiddenSectionSize = newHiddenSectionSize; |
| 1973 | } |
| 1974 | |
| 1975 | void QHeaderViewPrivate::(const QModelIndex &parent, |
| 1976 | int logicalFirst, int logicalLast) |
| 1977 | { |
| 1978 | Q_Q(QHeaderView); |
| 1979 | if (parent != root) |
| 1980 | return; // we only handle changes in the root level |
| 1981 | if (qMin(a: logicalFirst, b: logicalLast) < 0 |
| 1982 | || qMax(a: logicalLast, b: logicalFirst) >= sectionCount()) |
| 1983 | return; |
| 1984 | int oldCount = q->count(); |
| 1985 | int changeCount = logicalLast - logicalFirst + 1; |
| 1986 | |
| 1987 | if (state == QHeaderViewPrivate::ResizeSection) |
| 1988 | preventCursorChangeInSetOffset = true; |
| 1989 | |
| 1990 | updateHiddenSections(logicalFirst, logicalLast); |
| 1991 | |
| 1992 | if (visualIndices.isEmpty() && logicalIndices.isEmpty()) { |
| 1993 | //Q_ASSERT(headerSectionCount() == sectionCount); |
| 1994 | removeSectionsFromSectionItems(start: logicalFirst, end: logicalLast); |
| 1995 | } else { |
| 1996 | if (logicalFirst == logicalLast) { // Remove just one index. |
| 1997 | int l = logicalFirst; |
| 1998 | int visual = visualIndices.at(i: l); |
| 1999 | Q_ASSERT(sectionCount() == logicalIndices.size()); |
| 2000 | for (int v = 0; v < sectionCount(); ++v) { |
| 2001 | if (v > visual) { |
| 2002 | int logical = logicalIndices.at(i: v); |
| 2003 | --(visualIndices[logical]); |
| 2004 | } |
| 2005 | if (logicalIndex(visualIndex: v) > l) // no need to move the positions before l |
| 2006 | --(logicalIndices[v]); |
| 2007 | } |
| 2008 | logicalIndices.remove(i: visual); |
| 2009 | visualIndices.remove(i: l); |
| 2010 | //Q_ASSERT(headerSectionCount() == sectionCount); |
| 2011 | removeSectionsFromSectionItems(start: visual, end: visual); |
| 2012 | } else { |
| 2013 | sectionStartposRecalc = true; // We will need to recalc positions after removing items |
| 2014 | for (int u = 0; u < sectionItems.size(); ++u) // Store section info |
| 2015 | sectionItems.at(i: u).tmpLogIdx = logicalIndices.at(i: u); |
| 2016 | for (int v = sectionItems.size() - 1; v >= 0; --v) { // Remove the sections |
| 2017 | if (logicalFirst <= sectionItems.at(i: v).tmpLogIdx && sectionItems.at(i: v).tmpLogIdx <= logicalLast) |
| 2018 | removeSectionsFromSectionItems(start: v, end: v); |
| 2019 | } |
| 2020 | visualIndices.resize(size: sectionItems.size()); |
| 2021 | logicalIndices.resize(size: sectionItems.size()); |
| 2022 | int* visual_data = visualIndices.data(); |
| 2023 | int* logical_data = logicalIndices.data(); |
| 2024 | for (int w = 0; w < sectionItems.size(); ++w) { // Restore visual and logical indexes |
| 2025 | int logindex = sectionItems.at(i: w).tmpLogIdx; |
| 2026 | if (logindex > logicalFirst) |
| 2027 | logindex -= changeCount; |
| 2028 | visual_data[logindex] = w; |
| 2029 | logical_data[w] = logindex; |
| 2030 | } |
| 2031 | } |
| 2032 | // ### handle sectionSelection (sectionHidden is handled by updateHiddenSections) |
| 2033 | } |
| 2034 | |
| 2035 | // update sorting column |
| 2036 | if (sortIndicatorSection >= logicalFirst) { |
| 2037 | if (sortIndicatorSection <= logicalLast) |
| 2038 | sortIndicatorSection = -1; |
| 2039 | else |
| 2040 | sortIndicatorSection -= changeCount; |
| 2041 | } |
| 2042 | |
| 2043 | // if we only have the last section (the "end" position) left, the header is empty |
| 2044 | if (sectionCount() <= 0) |
| 2045 | clear(); |
| 2046 | invalidateCachedSizeHint(); |
| 2047 | emit q->sectionCountChanged(oldCount, newCount: q->count()); |
| 2048 | |
| 2049 | if (q->stretchLastSection()) { |
| 2050 | const bool lastSectionRemoved = lastSectionLogicalIdx >= logicalFirst && lastSectionLogicalIdx <= logicalLast; |
| 2051 | if (lastSectionRemoved) |
| 2052 | setNewLastSection(lastVisibleVisualIndex()); |
| 2053 | else |
| 2054 | lastSectionLogicalIdx = logicalIndex(visualIndex: lastVisibleVisualIndex()); // Just update the last log index. |
| 2055 | doDelayedResizeSections(); |
| 2056 | } |
| 2057 | |
| 2058 | viewport->update(); |
| 2059 | } |
| 2060 | |
| 2061 | void QHeaderViewPrivate::(const QModelIndex &sourceParent, int logicalStart, |
| 2062 | int logicalEnd, const QModelIndex &destinationParent, |
| 2063 | int logicalDestination) |
| 2064 | { |
| 2065 | if (sourceParent != root || destinationParent != root) |
| 2066 | return; // we only handle changes in the root level |
| 2067 | Q_UNUSED(logicalStart); |
| 2068 | Q_UNUSED(logicalEnd); |
| 2069 | Q_UNUSED(logicalDestination); |
| 2070 | sectionsAboutToBeChanged(); |
| 2071 | } |
| 2072 | |
| 2073 | void QHeaderViewPrivate::(const QModelIndex &sourceParent, int logicalStart, |
| 2074 | int logicalEnd, const QModelIndex &destinationParent, |
| 2075 | int logicalDestination) |
| 2076 | { |
| 2077 | if (sourceParent != root || destinationParent != root) |
| 2078 | return; // we only handle changes in the root level |
| 2079 | Q_UNUSED(logicalStart); |
| 2080 | Q_UNUSED(logicalEnd); |
| 2081 | Q_UNUSED(logicalDestination); |
| 2082 | sectionsChanged(); |
| 2083 | } |
| 2084 | |
| 2085 | void QHeaderViewPrivate::(const QList<QPersistentModelIndex> &, |
| 2086 | QAbstractItemModel::LayoutChangeHint hint) |
| 2087 | { |
| 2088 | if ((hint == QAbstractItemModel::VerticalSortHint && orientation == Qt::Horizontal) || |
| 2089 | (hint == QAbstractItemModel::HorizontalSortHint && orientation == Qt::Vertical)) |
| 2090 | return; |
| 2091 | |
| 2092 | //if there is no row/column we can't have mapping for columns |
| 2093 | //because no QModelIndex in the model would be valid |
| 2094 | // ### this is far from being bullet-proof and we would need a real system to |
| 2095 | // ### map columns or rows persistently |
| 2096 | if ((orientation == Qt::Horizontal && model->rowCount(parent: root) == 0) |
| 2097 | || model->columnCount(parent: root) == 0) |
| 2098 | return; |
| 2099 | |
| 2100 | layoutChangePersistentSections.clear(); |
| 2101 | layoutChangePersistentSections.reserve(asize: std::min(a: 10, b: int(sectionItems.size()))); |
| 2102 | // after layoutChanged another section can be last stretched section |
| 2103 | if (stretchLastSection && lastSectionLogicalIdx >= 0 && lastSectionLogicalIdx < sectionItems.size()) { |
| 2104 | const int visual = visualIndex(logicalIndex: lastSectionLogicalIdx); |
| 2105 | if (visual >= 0 && visual < sectionItems.size()) { |
| 2106 | auto &itemRef = sectionItems[visual]; |
| 2107 | if (itemRef.size != lastSectionSize) { |
| 2108 | length += lastSectionSize - itemRef.size; |
| 2109 | itemRef.size = lastSectionSize; |
| 2110 | } |
| 2111 | } |
| 2112 | } |
| 2113 | for (int i = 0; i < sectionItems.size(); ++i) { |
| 2114 | auto s = sectionItems.at(i); |
| 2115 | // only add if the section is not default and not visually moved |
| 2116 | if (s.size == defaultSectionSize && !s.isHidden && s.resizeMode == globalResizeMode) |
| 2117 | continue; |
| 2118 | |
| 2119 | const int logical = logicalIndex(visualIndex: i); |
| 2120 | if (s.isHidden) |
| 2121 | s.size = hiddenSectionSize.value(key: logical); |
| 2122 | |
| 2123 | // ### note that we are using column or row 0 |
| 2124 | layoutChangePersistentSections.append(t: {.index: orientation == Qt::Horizontal |
| 2125 | ? model->index(row: 0, column: logical, parent: root) |
| 2126 | : model->index(row: logical, column: 0, parent: root), |
| 2127 | .section: s}); |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | void QHeaderViewPrivate::(const QList<QPersistentModelIndex> &, |
| 2132 | QAbstractItemModel::LayoutChangeHint hint) |
| 2133 | { |
| 2134 | if ((hint == QAbstractItemModel::VerticalSortHint && orientation == Qt::Horizontal) || |
| 2135 | (hint == QAbstractItemModel::HorizontalSortHint && orientation == Qt::Vertical)) |
| 2136 | return; |
| 2137 | |
| 2138 | Q_Q(QHeaderView); |
| 2139 | viewport->update(); |
| 2140 | |
| 2141 | const auto oldPersistentSections = layoutChangePersistentSections; |
| 2142 | layoutChangePersistentSections.clear(); |
| 2143 | |
| 2144 | const int newCount = modelSectionCount(); |
| 2145 | const int oldCount = sectionItems.size(); |
| 2146 | if (newCount == 0) { |
| 2147 | clear(); |
| 2148 | if (oldCount != 0) |
| 2149 | emit q->sectionCountChanged(oldCount, newCount: 0); |
| 2150 | return; |
| 2151 | } |
| 2152 | |
| 2153 | bool hasPersistantIndexes = false; |
| 2154 | for (const auto &item : oldPersistentSections) { |
| 2155 | if (item.index.isValid()) { |
| 2156 | hasPersistantIndexes = true; |
| 2157 | break; |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | // Though far from perfect we here try to retain earlier/existing behavior |
| 2162 | // ### See QHeaderViewPrivate::layoutAboutToBeChanged() |
| 2163 | // When we don't have valid hasPersistantIndexes it can be due to |
| 2164 | // - all sections are default sections |
| 2165 | // - the row/column 0 which is used for persistent indexes is gone |
| 2166 | // - all non-default sections were removed |
| 2167 | // case one is trivial, in case two we assume nothing else changed (it's the best |
| 2168 | // guess we can do - everything else can not be handled correctly for now) |
| 2169 | // case three can not be handled correctly with layoutChanged - removeSections |
| 2170 | // should be used instead for this |
| 2171 | if (!hasPersistantIndexes) { |
| 2172 | if (oldCount != newCount) |
| 2173 | q->initializeSections(); |
| 2174 | return; |
| 2175 | } |
| 2176 | |
| 2177 | // adjust section size |
| 2178 | if (newCount != oldCount) { |
| 2179 | const int min = qBound(min: 0, val: oldCount, max: newCount - 1); |
| 2180 | q->initializeSections(start: min, end: newCount - 1); |
| 2181 | } |
| 2182 | // reset sections |
| 2183 | sectionItems.fill(t: SectionItem(defaultSectionSize, globalResizeMode), newSize: newCount); |
| 2184 | |
| 2185 | // all hidden sections are in oldPersistentSections |
| 2186 | hiddenSectionSize.clear(); |
| 2187 | |
| 2188 | for (const auto &item : oldPersistentSections) { |
| 2189 | const auto &index = item.index; |
| 2190 | if (!index.isValid()) |
| 2191 | continue; |
| 2192 | |
| 2193 | const int newLogicalIndex = (orientation == Qt::Horizontal |
| 2194 | ? index.column() |
| 2195 | : index.row()); |
| 2196 | // the new visualIndices are already adjusted / reset by initializeSections() |
| 2197 | const int newVisualIndex = visualIndex(logicalIndex: newLogicalIndex); |
| 2198 | if (newVisualIndex < sectionItems.size()) { |
| 2199 | auto &newSection = sectionItems[newVisualIndex]; |
| 2200 | newSection = item.section; |
| 2201 | |
| 2202 | if (newSection.isHidden) { |
| 2203 | // otherwise setSectionHidden will return without doing anything |
| 2204 | newSection.isHidden = false; |
| 2205 | q->setSectionHidden(logicalIndex: newLogicalIndex, hide: true); |
| 2206 | } |
| 2207 | } |
| 2208 | } |
| 2209 | |
| 2210 | recalcSectionStartPos(); |
| 2211 | length = headerLength(); |
| 2212 | |
| 2213 | if (stretchLastSection) { |
| 2214 | // force rebuild of stretched section later on |
| 2215 | lastSectionLogicalIdx = -1; |
| 2216 | maybeRestorePrevLastSectionAndStretchLast(); |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | /*! |
| 2221 | \internal |
| 2222 | */ |
| 2223 | |
| 2224 | void QHeaderView::() |
| 2225 | { |
| 2226 | Q_D(QHeaderView); |
| 2227 | const int oldCount = d->sectionCount(); |
| 2228 | const int newCount = d->modelSectionCount(); |
| 2229 | if (newCount <= 0) { |
| 2230 | d->clear(); |
| 2231 | emit sectionCountChanged(oldCount, newCount: 0); |
| 2232 | } else if (newCount != oldCount) { |
| 2233 | const int min = qBound(min: 0, val: oldCount, max: newCount - 1); |
| 2234 | initializeSections(start: min, end: newCount - 1); |
| 2235 | if (stretchLastSection()) // we've already gotten the size hint |
| 2236 | d->maybeRestorePrevLastSectionAndStretchLast(); |
| 2237 | |
| 2238 | // make sure we update the hidden sections |
| 2239 | // simulate remove from newCount to oldCount |
| 2240 | if (newCount < oldCount) |
| 2241 | d->updateHiddenSections(logicalFirst: newCount, logicalLast: oldCount); |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | /*! |
| 2246 | \internal |
| 2247 | */ |
| 2248 | |
| 2249 | void QHeaderView::(int start, int end) |
| 2250 | { |
| 2251 | Q_D(QHeaderView); |
| 2252 | |
| 2253 | Q_ASSERT(start >= 0); |
| 2254 | Q_ASSERT(end >= 0); |
| 2255 | |
| 2256 | d->invalidateCachedSizeHint(); |
| 2257 | int oldCount = d->sectionCount(); |
| 2258 | |
| 2259 | if (end + 1 < d->sectionCount()) { |
| 2260 | int newCount = end + 1; |
| 2261 | d->removeSectionsFromSectionItems(start: newCount, end: d->sectionCount() - 1); |
| 2262 | if (!d->hiddenSectionSize.isEmpty()) { |
| 2263 | if (oldCount - newCount > d->hiddenSectionSize.size()) { |
| 2264 | for (int i = end + 1; i < d->sectionCount(); ++i) |
| 2265 | d->hiddenSectionSize.remove(key: i); |
| 2266 | } else { |
| 2267 | QHash<int, int>::iterator it = d->hiddenSectionSize.begin(); |
| 2268 | while (it != d->hiddenSectionSize.end()) { |
| 2269 | if (it.key() > end) |
| 2270 | it = d->hiddenSectionSize.erase(it); |
| 2271 | else |
| 2272 | ++it; |
| 2273 | } |
| 2274 | } |
| 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | int newSectionCount = end + 1; |
| 2279 | |
| 2280 | if (!d->logicalIndices.isEmpty()) { |
| 2281 | if (oldCount <= newSectionCount) { |
| 2282 | d->logicalIndices.resize(size: newSectionCount); |
| 2283 | d->visualIndices.resize(size: newSectionCount); |
| 2284 | for (int i = oldCount; i < newSectionCount; ++i) { |
| 2285 | d->logicalIndices[i] = i; |
| 2286 | d->visualIndices[i] = i; |
| 2287 | } |
| 2288 | } else { |
| 2289 | int j = 0; |
| 2290 | for (int i = 0; i < oldCount; ++i) { |
| 2291 | int v = d->logicalIndices.at(i); |
| 2292 | if (v < newSectionCount) { |
| 2293 | d->logicalIndices[j] = v; |
| 2294 | d->visualIndices[v] = j; |
| 2295 | j++; |
| 2296 | } |
| 2297 | } |
| 2298 | d->logicalIndices.resize(size: newSectionCount); |
| 2299 | d->visualIndices.resize(size: newSectionCount); |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | if (d->globalResizeMode == Stretch) |
| 2304 | d->stretchSections = newSectionCount; |
| 2305 | else if (d->globalResizeMode == ResizeToContents) |
| 2306 | d->contentsSections = newSectionCount; |
| 2307 | |
| 2308 | if (newSectionCount > oldCount) |
| 2309 | d->createSectionItems(start, end, sectionSize: d->defaultSectionSize, mode: d->globalResizeMode); |
| 2310 | //Q_ASSERT(d->headerLength() == d->length); |
| 2311 | |
| 2312 | if (d->sectionCount() != oldCount) |
| 2313 | emit sectionCountChanged(oldCount, newCount: d->sectionCount()); |
| 2314 | d->viewport->update(); |
| 2315 | } |
| 2316 | |
| 2317 | /*! |
| 2318 | \reimp |
| 2319 | */ |
| 2320 | |
| 2321 | void QHeaderView::(const QModelIndex ¤t, const QModelIndex &old) |
| 2322 | { |
| 2323 | Q_D(QHeaderView); |
| 2324 | |
| 2325 | if (d->orientation == Qt::Horizontal && current.column() != old.column()) { |
| 2326 | if (old.isValid() && old.parent() == d->root) |
| 2327 | d->viewport->update(QRect(sectionViewportPosition(logicalIndex: old.column()), 0, |
| 2328 | sectionSize(logicalIndex: old.column()), d->viewport->height())); |
| 2329 | if (current.isValid() && current.parent() == d->root) |
| 2330 | d->viewport->update(QRect(sectionViewportPosition(logicalIndex: current.column()), 0, |
| 2331 | sectionSize(logicalIndex: current.column()), d->viewport->height())); |
| 2332 | } else if (d->orientation == Qt::Vertical && current.row() != old.row()) { |
| 2333 | if (old.isValid() && old.parent() == d->root) |
| 2334 | d->viewport->update(QRect(0, sectionViewportPosition(logicalIndex: old.row()), |
| 2335 | d->viewport->width(), sectionSize(logicalIndex: old.row()))); |
| 2336 | if (current.isValid() && current.parent() == d->root) |
| 2337 | d->viewport->update(QRect(0, sectionViewportPosition(logicalIndex: current.row()), |
| 2338 | d->viewport->width(), sectionSize(logicalIndex: current.row()))); |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | |
| 2343 | /*! |
| 2344 | \reimp |
| 2345 | */ |
| 2346 | |
| 2347 | bool QHeaderView::(QEvent *e) |
| 2348 | { |
| 2349 | Q_D(QHeaderView); |
| 2350 | switch (e->type()) { |
| 2351 | case QEvent::HoverEnter: { |
| 2352 | QHoverEvent *he = static_cast<QHoverEvent*>(e); |
| 2353 | d->hover = logicalIndexAt(apos: he->position().toPoint()); |
| 2354 | if (d->hover != -1) |
| 2355 | updateSection(logicalIndex: d->hover); |
| 2356 | break; } |
| 2357 | case QEvent::Leave: |
| 2358 | case QEvent::HoverLeave: { |
| 2359 | if (d->hover != -1) |
| 2360 | updateSection(logicalIndex: d->hover); |
| 2361 | d->hover = -1; |
| 2362 | break; } |
| 2363 | case QEvent::HoverMove: { |
| 2364 | QHoverEvent *he = static_cast<QHoverEvent*>(e); |
| 2365 | int oldHover = d->hover; |
| 2366 | d->hover = logicalIndexAt(apos: he->position().toPoint()); |
| 2367 | if (d->hover != oldHover) { |
| 2368 | if (oldHover != -1) |
| 2369 | updateSection(logicalIndex: oldHover); |
| 2370 | if (d->hover != -1) |
| 2371 | updateSection(logicalIndex: d->hover); |
| 2372 | } |
| 2373 | break; } |
| 2374 | case QEvent::Timer: { |
| 2375 | QTimerEvent *te = static_cast<QTimerEvent*>(e); |
| 2376 | if (te->timerId() == d->delayedResize.timerId()) { |
| 2377 | d->delayedResize.stop(); |
| 2378 | resizeSections(); |
| 2379 | } |
| 2380 | break; } |
| 2381 | case QEvent::StyleChange: |
| 2382 | if (!d->customDefaultSectionSize) { |
| 2383 | d->oldDefaultSectionSize = d->defaultSectionSize; |
| 2384 | d->updateDefaultSectionSizeFromStyle(); |
| 2385 | d->setDefaultSectionSize(d->defaultSectionSize); |
| 2386 | d->customDefaultSectionSize = false; |
| 2387 | } |
| 2388 | break; |
| 2389 | default: |
| 2390 | break; |
| 2391 | } |
| 2392 | return QAbstractItemView::event(event: e); |
| 2393 | } |
| 2394 | |
| 2395 | /*! |
| 2396 | \reimp |
| 2397 | */ |
| 2398 | |
| 2399 | void QHeaderView::(QPaintEvent *e) |
| 2400 | { |
| 2401 | Q_D(QHeaderView); |
| 2402 | |
| 2403 | if (count() == 0) |
| 2404 | return; |
| 2405 | |
| 2406 | QPainter painter(d->viewport); |
| 2407 | const QPoint offset = d->scrollDelayOffset; |
| 2408 | QRect translatedEventRect = e->rect(); |
| 2409 | translatedEventRect.translate(p: offset); |
| 2410 | |
| 2411 | int start = -1; |
| 2412 | int end = -1; |
| 2413 | if (d->orientation == Qt::Horizontal) { |
| 2414 | start = visualIndexAt(position: translatedEventRect.left()); |
| 2415 | end = visualIndexAt(position: translatedEventRect.right()); |
| 2416 | } else { |
| 2417 | start = visualIndexAt(position: translatedEventRect.top()); |
| 2418 | end = visualIndexAt(position: translatedEventRect.bottom()); |
| 2419 | } |
| 2420 | |
| 2421 | if (d->reverse()) { |
| 2422 | start = (start == -1 ? count() - 1 : start); |
| 2423 | end = (end == -1 ? 0 : end); |
| 2424 | } else { |
| 2425 | start = (start == -1 ? 0 : start); |
| 2426 | end = (end == -1 ? count() - 1 : end); |
| 2427 | } |
| 2428 | |
| 2429 | int tmp = start; |
| 2430 | start = qMin(a: start, b: end); |
| 2431 | end = qMax(a: tmp, b: end); |
| 2432 | |
| 2433 | d->prepareSectionSelected(); // clear and resize the bit array |
| 2434 | |
| 2435 | QRect currentSectionRect; |
| 2436 | const int width = d->viewport->width(); |
| 2437 | const int height = d->viewport->height(); |
| 2438 | const int rtlHorizontalOffset = d->reverse() ? 1 : 0; |
| 2439 | for (int i = start; i <= end; ++i) { |
| 2440 | if (d->isVisualIndexHidden(visual: i)) |
| 2441 | continue; |
| 2442 | painter.save(); |
| 2443 | const int logical = logicalIndex(visualIndex: i); |
| 2444 | if (d->orientation == Qt::Horizontal) { |
| 2445 | currentSectionRect.setRect(ax: sectionViewportPosition(logicalIndex: logical) + rtlHorizontalOffset, |
| 2446 | ay: 0, aw: sectionSize(logicalIndex: logical), ah: height); |
| 2447 | } else { |
| 2448 | currentSectionRect.setRect(ax: 0, ay: sectionViewportPosition(logicalIndex: logical), |
| 2449 | aw: width, ah: sectionSize(logicalIndex: logical)); |
| 2450 | } |
| 2451 | currentSectionRect.translate(p: offset); |
| 2452 | |
| 2453 | QVariant variant = d->model->headerData(section: logical, orientation: d->orientation, |
| 2454 | role: Qt::FontRole); |
| 2455 | if (variant.isValid() && variant.canConvert<QFont>()) { |
| 2456 | QFont sectionFont = qvariant_cast<QFont>(v: variant); |
| 2457 | painter.setFont(sectionFont); |
| 2458 | } |
| 2459 | paintSection(painter: &painter, rect: currentSectionRect, logicalIndex: logical); |
| 2460 | painter.restore(); |
| 2461 | } |
| 2462 | |
| 2463 | QStyleOption opt; |
| 2464 | opt.initFrom(w: this); |
| 2465 | // Paint the area beyond where there are indexes |
| 2466 | if (d->reverse()) { |
| 2467 | opt.state |= QStyle::State_Horizontal; |
| 2468 | if (currentSectionRect.left() > translatedEventRect.left()) { |
| 2469 | opt.rect = QRect(translatedEventRect.left(), 0, |
| 2470 | currentSectionRect.left() - translatedEventRect.left(), height); |
| 2471 | style()->drawControl(element: QStyle::CE_HeaderEmptyArea, opt: &opt, p: &painter, w: this); |
| 2472 | } |
| 2473 | } else if (currentSectionRect.right() < translatedEventRect.right()) { |
| 2474 | // paint to the right |
| 2475 | opt.state |= QStyle::State_Horizontal; |
| 2476 | opt.rect = QRect(currentSectionRect.right() + 1, 0, |
| 2477 | translatedEventRect.right() - currentSectionRect.right(), height); |
| 2478 | style()->drawControl(element: QStyle::CE_HeaderEmptyArea, opt: &opt, p: &painter, w: this); |
| 2479 | } else if (currentSectionRect.bottom() < translatedEventRect.bottom()) { |
| 2480 | // paint the bottom section |
| 2481 | opt.state &= ~QStyle::State_Horizontal; |
| 2482 | opt.rect = QRect(0, currentSectionRect.bottom() + 1, |
| 2483 | width, height - currentSectionRect.bottom() - 1); |
| 2484 | style()->drawControl(element: QStyle::CE_HeaderEmptyArea, opt: &opt, p: &painter, w: this); |
| 2485 | } |
| 2486 | |
| 2487 | #if 0 |
| 2488 | // ### visualize sections |
| 2489 | for (int a = 0, i = 0; i < d->sectionItems.count(); ++i) { |
| 2490 | QColor color((i & 4 ? 255 : 0), (i & 2 ? 255 : 0), (i & 1 ? 255 : 0)); |
| 2491 | if (d->orientation == Qt::Horizontal) |
| 2492 | painter.fillRect(a - d->headerOffset, 0, d->sectionItems.at(i).size, 4, color); |
| 2493 | else |
| 2494 | painter.fillRect(0, a - d->headerOffset, 4, d->sectionItems.at(i).size, color); |
| 2495 | a += d->sectionItems.at(i).size; |
| 2496 | } |
| 2497 | |
| 2498 | #endif |
| 2499 | } |
| 2500 | |
| 2501 | /*! |
| 2502 | \reimp |
| 2503 | */ |
| 2504 | |
| 2505 | void QHeaderView::(QMouseEvent *e) |
| 2506 | { |
| 2507 | Q_D(QHeaderView); |
| 2508 | if (d->state != QHeaderViewPrivate::NoState || e->button() != Qt::LeftButton) |
| 2509 | return; |
| 2510 | int pos = d->orientation == Qt::Horizontal ? e->position().toPoint().x() : e->position().toPoint().y(); |
| 2511 | int handle = d->sectionHandleAt(position: pos); |
| 2512 | d->originalSize = -1; // clear the stored original size |
| 2513 | if (handle == -1) { |
| 2514 | d->firstPressed = d->pressed = logicalIndexAt(position: pos); |
| 2515 | if (d->clickableSections) |
| 2516 | emit sectionPressed(logicalIndex: d->pressed); |
| 2517 | |
| 2518 | bool acceptMoveSection = d->movableSections; |
| 2519 | if (acceptMoveSection && d->pressed == 0 && !d->allowUserMoveOfSection0) |
| 2520 | acceptMoveSection = false; // Do not allow moving the tree nod |
| 2521 | |
| 2522 | if (acceptMoveSection) { |
| 2523 | d->target = -1; |
| 2524 | d->section = d->pressed; |
| 2525 | if (d->section == -1) |
| 2526 | return; |
| 2527 | d->state = QHeaderViewPrivate::MoveSection; |
| 2528 | d->setupSectionIndicator(section: d->section, position: pos); |
| 2529 | } else if (d->clickableSections && d->pressed != -1) { |
| 2530 | updateSection(logicalIndex: d->pressed); |
| 2531 | d->state = QHeaderViewPrivate::SelectSections; |
| 2532 | } |
| 2533 | } else if (sectionResizeMode(logicalIndex: handle) == Interactive) { |
| 2534 | d->originalSize = sectionSize(logicalIndex: handle); |
| 2535 | d->state = QHeaderViewPrivate::ResizeSection; |
| 2536 | d->section = handle; |
| 2537 | d->preventCursorChangeInSetOffset = false; |
| 2538 | } |
| 2539 | |
| 2540 | d->firstPos = pos; |
| 2541 | d->lastPos = pos; |
| 2542 | |
| 2543 | d->clearCascadingSections(); |
| 2544 | } |
| 2545 | |
| 2546 | /*! |
| 2547 | \reimp |
| 2548 | */ |
| 2549 | |
| 2550 | void QHeaderView::(QMouseEvent *e) |
| 2551 | { |
| 2552 | Q_D(QHeaderView); |
| 2553 | const int pos = d->orientation == Qt::Horizontal ? e->position().toPoint().x() : e->position().toPoint().y(); |
| 2554 | if (pos < 0 && d->state != QHeaderViewPrivate::SelectSections) |
| 2555 | return; |
| 2556 | if (e->buttons() == Qt::NoButton) { |
| 2557 | // Under Cocoa, when the mouse button is released, may include an extra |
| 2558 | // simulated mouse moved event. The state of the buttons when this event |
| 2559 | // is generated is already "no button" and the code below gets executed |
| 2560 | // just before the mouseReleaseEvent and resets the state. This prevents |
| 2561 | // column dragging from working. So this code is disabled under Cocoa. |
| 2562 | d->state = QHeaderViewPrivate::NoState; |
| 2563 | d->firstPressed = d->pressed = -1; |
| 2564 | } |
| 2565 | switch (d->state) { |
| 2566 | case QHeaderViewPrivate::ResizeSection: { |
| 2567 | Q_ASSERT(d->originalSize != -1); |
| 2568 | if (d->cascadingResizing) { |
| 2569 | int delta = d->reverse() ? d->lastPos - pos : pos - d->lastPos; |
| 2570 | int visual = visualIndex(logicalIndex: d->section); |
| 2571 | d->cascadingResize(visual, newSize: d->headerSectionSize(visual) + delta); |
| 2572 | } else { |
| 2573 | int delta = d->reverse() ? d->firstPos - pos : pos - d->firstPos; |
| 2574 | int newsize = qBound(min: minimumSectionSize(), val: d->originalSize + delta, max: maximumSectionSize()); |
| 2575 | resizeSection(logical: d->section, size: newsize); |
| 2576 | } |
| 2577 | d->lastPos = pos; |
| 2578 | return; |
| 2579 | } |
| 2580 | case QHeaderViewPrivate::MoveSection: { |
| 2581 | if (d->shouldAutoScroll(pos: e->position().toPoint())) { |
| 2582 | d->draggedPosition = e->pos() + d->offset(); |
| 2583 | d->startAutoScroll(); |
| 2584 | } |
| 2585 | if (qAbs(t: pos - d->firstPos) >= QApplication::startDragDistance() |
| 2586 | #if QT_CONFIG(label) |
| 2587 | || !d->sectionIndicator->isHidden() |
| 2588 | #endif |
| 2589 | ) { |
| 2590 | int visual = visualIndexAt(position: pos); |
| 2591 | if (visual == -1) |
| 2592 | return; |
| 2593 | if (visual == 0 && logicalIndex(visualIndex: 0) == 0 && !d->allowUserMoveOfSection0) |
| 2594 | return; |
| 2595 | |
| 2596 | const int posThreshold = d->headerSectionPosition(visual) - d->headerOffset + d->headerSectionSize(visual) / 2; |
| 2597 | const int checkPos = d->reverse() ? d->viewport->width() - pos : pos; |
| 2598 | int moving = visualIndex(logicalIndex: d->section); |
| 2599 | int oldTarget = d->target; |
| 2600 | if (visual < moving) { |
| 2601 | if (checkPos < posThreshold) |
| 2602 | d->target = d->logicalIndex(visualIndex: visual); |
| 2603 | else |
| 2604 | d->target = d->logicalIndex(visualIndex: visual + 1); |
| 2605 | } else if (visual > moving) { |
| 2606 | if (checkPos > posThreshold) |
| 2607 | d->target = d->logicalIndex(visualIndex: visual); |
| 2608 | else |
| 2609 | d->target = d->logicalIndex(visualIndex: visual - 1); |
| 2610 | } else { |
| 2611 | d->target = d->section; |
| 2612 | } |
| 2613 | if (oldTarget != d->target || oldTarget == -1) |
| 2614 | d->updateSectionsBeforeAfter(logical: d->target); |
| 2615 | d->updateSectionIndicator(section: d->section, position: pos); |
| 2616 | } |
| 2617 | return; |
| 2618 | } |
| 2619 | case QHeaderViewPrivate::SelectSections: { |
| 2620 | int logical = logicalIndexAt(position: qMax(a: -d->headerOffset, b: pos)); |
| 2621 | if (logical == -1 && pos > 0) |
| 2622 | logical = logicalIndex(visualIndex: d->lastVisibleVisualIndex()); |
| 2623 | if (logical == d->pressed) |
| 2624 | return; // nothing to do |
| 2625 | else if (d->pressed != -1) |
| 2626 | updateSection(logicalIndex: d->pressed); |
| 2627 | d->pressed = logical; |
| 2628 | if (d->clickableSections && logical != -1) { |
| 2629 | emit sectionEntered(logicalIndex: d->pressed); |
| 2630 | updateSection(logicalIndex: d->pressed); |
| 2631 | } |
| 2632 | return; |
| 2633 | } |
| 2634 | case QHeaderViewPrivate::NoState: { |
| 2635 | #ifndef QT_NO_CURSOR |
| 2636 | int handle = d->sectionHandleAt(position: pos); |
| 2637 | bool hasCursor = testAttribute(attribute: Qt::WA_SetCursor); |
| 2638 | if (handle != -1 && (sectionResizeMode(logicalIndex: handle) == Interactive)) { |
| 2639 | if (!hasCursor) |
| 2640 | setCursor(d->orientation == Qt::Horizontal ? Qt::SplitHCursor : Qt::SplitVCursor); |
| 2641 | } else { |
| 2642 | if (hasCursor) |
| 2643 | unsetCursor(); |
| 2644 | #ifndef QT_NO_STATUSTIP |
| 2645 | int logical = logicalIndexAt(position: pos); |
| 2646 | QString statusTip; |
| 2647 | if (logical != -1) |
| 2648 | statusTip = d->model->headerData(section: logical, orientation: d->orientation, role: Qt::StatusTipRole).toString(); |
| 2649 | if (d->shouldClearStatusTip || !statusTip.isEmpty()) { |
| 2650 | QStatusTipEvent tip(statusTip); |
| 2651 | QCoreApplication::sendEvent(receiver: d->parent ? d->parent : this, event: &tip); |
| 2652 | d->shouldClearStatusTip = !statusTip.isEmpty(); |
| 2653 | } |
| 2654 | #endif // !QT_NO_STATUSTIP |
| 2655 | } |
| 2656 | #endif |
| 2657 | return; |
| 2658 | } |
| 2659 | default: |
| 2660 | break; |
| 2661 | } |
| 2662 | } |
| 2663 | |
| 2664 | /*! |
| 2665 | \reimp |
| 2666 | */ |
| 2667 | |
| 2668 | void QHeaderView::(QMouseEvent *e) |
| 2669 | { |
| 2670 | Q_D(QHeaderView); |
| 2671 | int pos = d->orientation == Qt::Horizontal ? e->position().toPoint().x() : e->position().toPoint().y(); |
| 2672 | switch (d->state) { |
| 2673 | case QHeaderViewPrivate::MoveSection: |
| 2674 | if (true |
| 2675 | #if QT_CONFIG(label) |
| 2676 | && !d->sectionIndicator->isHidden() |
| 2677 | #endif |
| 2678 | ) { // moving |
| 2679 | int from = visualIndex(logicalIndex: d->section); |
| 2680 | Q_ASSERT(from != -1); |
| 2681 | int to = visualIndex(logicalIndex: d->target); |
| 2682 | Q_ASSERT(to != -1); |
| 2683 | moveSection(from, to); |
| 2684 | d->section = d->target = -1; |
| 2685 | d->updateSectionIndicator(section: d->section, position: pos); |
| 2686 | if (from == to) |
| 2687 | d->updateSectionsBeforeAfter(logical: from); |
| 2688 | break; |
| 2689 | } // not moving |
| 2690 | Q_FALLTHROUGH(); |
| 2691 | case QHeaderViewPrivate::SelectSections: |
| 2692 | if (!d->clickableSections) { |
| 2693 | int section = logicalIndexAt(position: pos); |
| 2694 | updateSection(logicalIndex: section); |
| 2695 | } |
| 2696 | Q_FALLTHROUGH(); |
| 2697 | case QHeaderViewPrivate::NoState: |
| 2698 | if (d->clickableSections) { |
| 2699 | int section = logicalIndexAt(position: pos); |
| 2700 | if (section != -1 && section == d->firstPressed) { |
| 2701 | QRect firstPressedSectionRect; |
| 2702 | switch (d->orientation) { |
| 2703 | case Qt::Horizontal: |
| 2704 | firstPressedSectionRect.setRect(ax: sectionViewportPosition(logicalIndex: d->firstPressed), |
| 2705 | ay: 0, |
| 2706 | aw: sectionSize(logicalIndex: d->firstPressed), |
| 2707 | ah: d->viewport->height()); |
| 2708 | break; |
| 2709 | case Qt::Vertical: |
| 2710 | firstPressedSectionRect.setRect(ax: 0, |
| 2711 | ay: sectionViewportPosition(logicalIndex: d->firstPressed), |
| 2712 | aw: d->viewport->width(), |
| 2713 | ah: sectionSize(logicalIndex: d->firstPressed)); |
| 2714 | break; |
| 2715 | }; |
| 2716 | |
| 2717 | if (firstPressedSectionRect.contains(p: e->position().toPoint())) { |
| 2718 | d->flipSortIndicator(section); |
| 2719 | emit sectionClicked(logicalIndex: section); |
| 2720 | } |
| 2721 | } |
| 2722 | if (d->pressed != -1) |
| 2723 | updateSection(logicalIndex: d->pressed); |
| 2724 | } |
| 2725 | break; |
| 2726 | case QHeaderViewPrivate::ResizeSection: |
| 2727 | d->originalSize = -1; |
| 2728 | d->clearCascadingSections(); |
| 2729 | break; |
| 2730 | default: |
| 2731 | break; |
| 2732 | } |
| 2733 | d->state = QHeaderViewPrivate::NoState; |
| 2734 | d->firstPressed = d->pressed = -1; |
| 2735 | } |
| 2736 | |
| 2737 | /*! |
| 2738 | \reimp |
| 2739 | */ |
| 2740 | |
| 2741 | void QHeaderView::(QMouseEvent *e) |
| 2742 | { |
| 2743 | Q_D(QHeaderView); |
| 2744 | int pos = d->orientation == Qt::Horizontal ? e->position().toPoint().x() : e->position().toPoint().y(); |
| 2745 | int handle = d->sectionHandleAt(position: pos); |
| 2746 | if (handle > -1 && sectionResizeMode(logicalIndex: handle) == Interactive) { |
| 2747 | emit sectionHandleDoubleClicked(logicalIndex: handle); |
| 2748 | #ifndef QT_NO_CURSOR |
| 2749 | Qt::CursorShape splitCursor = (d->orientation == Qt::Horizontal) |
| 2750 | ? Qt::SplitHCursor : Qt::SplitVCursor; |
| 2751 | if (cursor().shape() == splitCursor) { |
| 2752 | // signal handlers may have changed the section size |
| 2753 | handle = d->sectionHandleAt(position: pos); |
| 2754 | if (!(handle > -1 && sectionResizeMode(logicalIndex: handle) == Interactive)) |
| 2755 | setCursor(Qt::ArrowCursor); |
| 2756 | } |
| 2757 | #endif |
| 2758 | } else { |
| 2759 | emit sectionDoubleClicked(logicalIndex: logicalIndexAt(apos: e->position().toPoint())); |
| 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | /*! |
| 2764 | \reimp |
| 2765 | */ |
| 2766 | |
| 2767 | bool QHeaderView::(QEvent *e) |
| 2768 | { |
| 2769 | Q_D(QHeaderView); |
| 2770 | switch (e->type()) { |
| 2771 | #if QT_CONFIG(tooltip) |
| 2772 | case QEvent::ToolTip: { |
| 2773 | QHelpEvent *he = static_cast<QHelpEvent*>(e); |
| 2774 | int logical = logicalIndexAt(apos: he->pos()); |
| 2775 | if (logical != -1) { |
| 2776 | QVariant variant = d->model->headerData(section: logical, orientation: d->orientation, role: Qt::ToolTipRole); |
| 2777 | if (variant.isValid()) { |
| 2778 | QToolTip::showText(pos: he->globalPos(), text: variant.toString(), w: this); |
| 2779 | return true; |
| 2780 | } |
| 2781 | } |
| 2782 | break; } |
| 2783 | #endif |
| 2784 | #if QT_CONFIG(whatsthis) |
| 2785 | case QEvent::QueryWhatsThis: { |
| 2786 | QHelpEvent *he = static_cast<QHelpEvent*>(e); |
| 2787 | int logical = logicalIndexAt(apos: he->pos()); |
| 2788 | if (logical != -1 |
| 2789 | && d->model->headerData(section: logical, orientation: d->orientation, role: Qt::WhatsThisRole).isValid()) |
| 2790 | return true; |
| 2791 | break; } |
| 2792 | case QEvent::WhatsThis: { |
| 2793 | QHelpEvent *he = static_cast<QHelpEvent*>(e); |
| 2794 | int logical = logicalIndexAt(apos: he->pos()); |
| 2795 | if (logical != -1) { |
| 2796 | QVariant whatsthis = d->model->headerData(section: logical, orientation: d->orientation, |
| 2797 | role: Qt::WhatsThisRole); |
| 2798 | if (whatsthis.isValid()) { |
| 2799 | QWhatsThis::showText(pos: he->globalPos(), text: whatsthis.toString(), w: this); |
| 2800 | return true; |
| 2801 | } |
| 2802 | } |
| 2803 | break; } |
| 2804 | #endif // QT_CONFIG(whatsthis) |
| 2805 | #if QT_CONFIG(statustip) |
| 2806 | case QEvent::StatusTip: { |
| 2807 | QHelpEvent *he = static_cast<QHelpEvent*>(e); |
| 2808 | int logical = logicalIndexAt(apos: he->pos()); |
| 2809 | if (logical != -1) { |
| 2810 | QString statustip = d->model->headerData(section: logical, orientation: d->orientation, |
| 2811 | role: Qt::StatusTipRole).toString(); |
| 2812 | if (!statustip.isEmpty()) |
| 2813 | setStatusTip(statustip); |
| 2814 | } |
| 2815 | return true; } |
| 2816 | #endif // QT_CONFIG(statustip) |
| 2817 | case QEvent::Resize: |
| 2818 | case QEvent::FontChange: |
| 2819 | case QEvent::StyleChange: |
| 2820 | d->invalidateCachedSizeHint(); |
| 2821 | Q_FALLTHROUGH(); |
| 2822 | case QEvent::Hide: |
| 2823 | case QEvent::Show: { |
| 2824 | QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea *>(object: parentWidget()); |
| 2825 | if (parent && parent->isVisible()) // Only resize if we have a visible parent |
| 2826 | resizeSections(); |
| 2827 | emit geometriesChanged(); |
| 2828 | break;} |
| 2829 | case QEvent::ContextMenu: { |
| 2830 | d->state = QHeaderViewPrivate::NoState; |
| 2831 | d->pressed = d->section = d->target = -1; |
| 2832 | d->updateSectionIndicator(section: d->section, position: -1); |
| 2833 | break; } |
| 2834 | case QEvent::Wheel: { |
| 2835 | QAbstractScrollArea *asa = qobject_cast<QAbstractScrollArea *>(object: parentWidget()); |
| 2836 | if (asa) |
| 2837 | return QCoreApplication::sendEvent(receiver: asa->viewport(), event: e); |
| 2838 | break; } |
| 2839 | default: |
| 2840 | break; |
| 2841 | } |
| 2842 | return QAbstractItemView::viewportEvent(event: e); |
| 2843 | } |
| 2844 | |
| 2845 | /*! |
| 2846 | \fn void QHeaderView::initStyleOptionForIndex(QStyleOptionHeader *option, int logicalIndex) const |
| 2847 | \since 6.0 |
| 2848 | |
| 2849 | Initializes the style \a option from the specified \a logicalIndex. |
| 2850 | This function is called by the default implementation of paintSection after |
| 2851 | initStyleOption has been called. |
| 2852 | |
| 2853 | \sa paintSection(), initStyleOption() |
| 2854 | */ |
| 2855 | |
| 2856 | void QHeaderView::(QStyleOptionHeader *option, int logicalIndex) const |
| 2857 | { |
| 2858 | Q_D(const QHeaderView); |
| 2859 | |
| 2860 | if (!option) |
| 2861 | return; |
| 2862 | QStyleOptionHeader &opt = *option; |
| 2863 | QStyleOptionHeaderV2 *optV2 = qstyleoption_cast<QStyleOptionHeaderV2*>(opt: option); |
| 2864 | |
| 2865 | QStyle::State state = QStyle::State_None; |
| 2866 | if (window()->isActiveWindow()) |
| 2867 | state |= QStyle::State_Active; |
| 2868 | if (d->clickableSections) { |
| 2869 | if (logicalIndex == d->hover) |
| 2870 | state |= QStyle::State_MouseOver; |
| 2871 | if (logicalIndex == d->pressed) |
| 2872 | state |= QStyle::State_Sunken; |
| 2873 | else if (d->highlightSelected) { |
| 2874 | if (d->sectionIntersectsSelection(logical: logicalIndex)) |
| 2875 | state |= QStyle::State_On; |
| 2876 | if (d->isSectionSelected(section: logicalIndex)) |
| 2877 | state |= QStyle::State_Sunken; |
| 2878 | } |
| 2879 | } |
| 2880 | if (isSortIndicatorShown() && sortIndicatorSection() == logicalIndex) |
| 2881 | opt.sortIndicator = (sortIndicatorOrder() == Qt::AscendingOrder) |
| 2882 | ? QStyleOptionHeader::SortDown : QStyleOptionHeader::SortUp; |
| 2883 | |
| 2884 | // setup the style options structure |
| 2885 | QVariant textAlignment = d->model->headerData(section: logicalIndex, orientation: d->orientation, |
| 2886 | role: Qt::TextAlignmentRole); |
| 2887 | opt.section = logicalIndex; |
| 2888 | opt.state |= state; |
| 2889 | opt.textAlignment = textAlignment.isValid() |
| 2890 | ? QtPrivate::legacyFlagValueFromModelData<Qt::Alignment>(data: textAlignment) |
| 2891 | : d->defaultAlignment; |
| 2892 | |
| 2893 | opt.iconAlignment = Qt::AlignVCenter; |
| 2894 | opt.text = d->model->headerData(section: logicalIndex, orientation: d->orientation, |
| 2895 | role: Qt::DisplayRole).toString(); |
| 2896 | |
| 2897 | const QVariant variant = d->model->headerData(section: logicalIndex, orientation: d->orientation, |
| 2898 | role: Qt::DecorationRole); |
| 2899 | opt.icon = qvariant_cast<QIcon>(v: variant); |
| 2900 | if (opt.icon.isNull()) |
| 2901 | opt.icon = qvariant_cast<QPixmap>(v: variant); |
| 2902 | |
| 2903 | QVariant var = d->model->headerData(section: logicalIndex, orientation: d->orientation, |
| 2904 | role: Qt::FontRole); |
| 2905 | if (var.isValid() && var.canConvert<QFont>()) |
| 2906 | opt.fontMetrics = QFontMetrics(qvariant_cast<QFont>(v: var)); |
| 2907 | if (optV2) |
| 2908 | optV2->textElideMode = d->textElideMode; |
| 2909 | |
| 2910 | QVariant foregroundBrush = d->model->headerData(section: logicalIndex, orientation: d->orientation, |
| 2911 | role: Qt::ForegroundRole); |
| 2912 | if (foregroundBrush.canConvert<QBrush>()) |
| 2913 | opt.palette.setBrush(acr: QPalette::ButtonText, abrush: qvariant_cast<QBrush>(v: foregroundBrush)); |
| 2914 | |
| 2915 | QVariant backgroundBrush = d->model->headerData(section: logicalIndex, orientation: d->orientation, |
| 2916 | role: Qt::BackgroundRole); |
| 2917 | if (backgroundBrush.canConvert<QBrush>()) { |
| 2918 | opt.palette.setBrush(acr: QPalette::Button, abrush: qvariant_cast<QBrush>(v: backgroundBrush)); |
| 2919 | opt.palette.setBrush(acr: QPalette::Window, abrush: qvariant_cast<QBrush>(v: backgroundBrush)); |
| 2920 | } |
| 2921 | |
| 2922 | // the section position |
| 2923 | int visual = visualIndex(logicalIndex); |
| 2924 | Q_ASSERT(visual != -1); |
| 2925 | bool first = d->isFirstVisibleSection(section: visual); |
| 2926 | bool last = d->isLastVisibleSection(section: visual); |
| 2927 | if (first && last) |
| 2928 | opt.position = QStyleOptionHeader::OnlyOneSection; |
| 2929 | else if (first) |
| 2930 | opt.position = d->reverse() ? QStyleOptionHeader::End : QStyleOptionHeader::Beginning; |
| 2931 | else if (last) |
| 2932 | opt.position = d->reverse() ? QStyleOptionHeader::Beginning : QStyleOptionHeader::End; |
| 2933 | else |
| 2934 | opt.position = QStyleOptionHeader::Middle; |
| 2935 | opt.orientation = d->orientation; |
| 2936 | // the selected position |
| 2937 | bool previousSelected = d->isSectionSelected(section: this->logicalIndex(visualIndex: visual - 1)); |
| 2938 | bool nextSelected = d->isSectionSelected(section: this->logicalIndex(visualIndex: visual + 1)); |
| 2939 | if (previousSelected && nextSelected) |
| 2940 | opt.selectedPosition = QStyleOptionHeader::NextAndPreviousAreSelected; |
| 2941 | else if (previousSelected) |
| 2942 | opt.selectedPosition = QStyleOptionHeader::PreviousIsSelected; |
| 2943 | else if (nextSelected) |
| 2944 | opt.selectedPosition = QStyleOptionHeader::NextIsSelected; |
| 2945 | else |
| 2946 | opt.selectedPosition = QStyleOptionHeader::NotAdjacent; |
| 2947 | if (optV2) |
| 2948 | optV2->isSectionDragTarget = d->target == logicalIndex; |
| 2949 | } |
| 2950 | |
| 2951 | /*! |
| 2952 | Paints the section specified by the given \a logicalIndex, using the given |
| 2953 | \a painter and \a rect. |
| 2954 | |
| 2955 | Normally, you do not have to call this function. |
| 2956 | */ |
| 2957 | |
| 2958 | void QHeaderView::(QPainter *painter, const QRect &rect, int logicalIndex) const |
| 2959 | { |
| 2960 | if (!rect.isValid()) |
| 2961 | return; |
| 2962 | |
| 2963 | QStyleOptionHeaderV2 opt; |
| 2964 | QPointF oldBO = painter->brushOrigin(); |
| 2965 | |
| 2966 | initStyleOption(option: &opt); |
| 2967 | |
| 2968 | QBrush oBrushButton = opt.palette.brush(cr: QPalette::Button); |
| 2969 | QBrush oBrushWindow = opt.palette.brush(cr: QPalette::Window); |
| 2970 | |
| 2971 | initStyleOptionForIndex(option: &opt, logicalIndex); |
| 2972 | // We set rect here. If it needs to be changed it can be changed by overriding this function |
| 2973 | opt.rect = rect; |
| 2974 | |
| 2975 | QBrush nBrushButton = opt.palette.brush(cr: QPalette::Button); |
| 2976 | QBrush nBrushWindow = opt.palette.brush(cr: QPalette::Window); |
| 2977 | |
| 2978 | // If relevant brushes are not the same as from the regular widgets we set the brush origin |
| 2979 | if (oBrushButton != nBrushButton || oBrushWindow != nBrushWindow) { |
| 2980 | painter->setBrushOrigin(opt.rect.topLeft()); |
| 2981 | } |
| 2982 | |
| 2983 | // draw the section. |
| 2984 | style()->drawControl(element: QStyle::CE_Header, opt: &opt, p: painter, w: this); |
| 2985 | painter->setBrushOrigin(oldBO); |
| 2986 | } |
| 2987 | |
| 2988 | /*! |
| 2989 | Returns the size of the contents of the section specified by the given |
| 2990 | \a logicalIndex. |
| 2991 | |
| 2992 | \sa defaultSectionSize() |
| 2993 | */ |
| 2994 | |
| 2995 | QSize QHeaderView::(int logicalIndex) const |
| 2996 | { |
| 2997 | Q_D(const QHeaderView); |
| 2998 | Q_ASSERT(logicalIndex >= 0); |
| 2999 | |
| 3000 | ensurePolished(); |
| 3001 | |
| 3002 | // use SizeHintRole |
| 3003 | QVariant variant = d->model->headerData(section: logicalIndex, orientation: d->orientation, role: Qt::SizeHintRole); |
| 3004 | if (variant.isValid()) |
| 3005 | return qvariant_cast<QSize>(v: variant); |
| 3006 | |
| 3007 | // otherwise use the contents |
| 3008 | QStyleOptionHeaderV2 opt; |
| 3009 | initStyleOption(option: &opt); |
| 3010 | opt.section = logicalIndex; |
| 3011 | QVariant var = d->model->headerData(section: logicalIndex, orientation: d->orientation, |
| 3012 | role: Qt::FontRole); |
| 3013 | QFont fnt; |
| 3014 | if (var.isValid() && var.canConvert<QFont>()) |
| 3015 | fnt = qvariant_cast<QFont>(v: var); |
| 3016 | else |
| 3017 | fnt = font(); |
| 3018 | fnt.setBold(true); |
| 3019 | opt.fontMetrics = QFontMetrics(fnt); |
| 3020 | opt.text = d->model->headerData(section: logicalIndex, orientation: d->orientation, |
| 3021 | role: Qt::DisplayRole).toString(); |
| 3022 | variant = d->model->headerData(section: logicalIndex, orientation: d->orientation, role: Qt::DecorationRole); |
| 3023 | opt.icon = qvariant_cast<QIcon>(v: variant); |
| 3024 | if (opt.icon.isNull()) |
| 3025 | opt.icon = qvariant_cast<QPixmap>(v: variant); |
| 3026 | if (isSortIndicatorShown()) |
| 3027 | opt.sortIndicator = QStyleOptionHeader::SortDown; |
| 3028 | return style()->sizeFromContents(ct: QStyle::CT_HeaderSection, opt: &opt, contentsSize: QSize(), w: this); |
| 3029 | } |
| 3030 | |
| 3031 | /*! |
| 3032 | Returns the horizontal offset of the header. This is 0 for vertical |
| 3033 | headers. |
| 3034 | |
| 3035 | \sa offset() |
| 3036 | */ |
| 3037 | |
| 3038 | int QHeaderView::() const |
| 3039 | { |
| 3040 | Q_D(const QHeaderView); |
| 3041 | if (d->orientation == Qt::Horizontal) |
| 3042 | return d->headerOffset; |
| 3043 | return 0; |
| 3044 | } |
| 3045 | |
| 3046 | /*! |
| 3047 | Returns the vertical offset of the header. This is 0 for horizontal |
| 3048 | headers. |
| 3049 | |
| 3050 | \sa offset() |
| 3051 | */ |
| 3052 | |
| 3053 | int QHeaderView::() const |
| 3054 | { |
| 3055 | Q_D(const QHeaderView); |
| 3056 | if (d->orientation == Qt::Vertical) |
| 3057 | return d->headerOffset; |
| 3058 | return 0; |
| 3059 | } |
| 3060 | |
| 3061 | /*! |
| 3062 | \reimp |
| 3063 | \internal |
| 3064 | */ |
| 3065 | |
| 3066 | void QHeaderView::() |
| 3067 | { |
| 3068 | Q_D(QHeaderView); |
| 3069 | d->layoutChildren(); |
| 3070 | if (d->hasAutoResizeSections()) |
| 3071 | d->doDelayedResizeSections(); |
| 3072 | } |
| 3073 | |
| 3074 | /*! |
| 3075 | \reimp |
| 3076 | \internal |
| 3077 | */ |
| 3078 | |
| 3079 | void QHeaderView::(int dx, int dy) |
| 3080 | { |
| 3081 | Q_D(QHeaderView); |
| 3082 | d->scrollDirtyRegion(dx, dy); |
| 3083 | } |
| 3084 | |
| 3085 | /*! |
| 3086 | \reimp |
| 3087 | \internal |
| 3088 | */ |
| 3089 | void QHeaderView::(const QModelIndex &topLeft, const QModelIndex &bottomRight, |
| 3090 | const QList<int> &roles) |
| 3091 | { |
| 3092 | Q_D(QHeaderView); |
| 3093 | if (!roles.isEmpty()) { |
| 3094 | const auto doesRoleAffectSize = [](int role) -> bool { |
| 3095 | switch (role) { |
| 3096 | case Qt::DisplayRole: |
| 3097 | case Qt::DecorationRole: |
| 3098 | case Qt::SizeHintRole: |
| 3099 | case Qt::FontRole: |
| 3100 | return true; |
| 3101 | default: |
| 3102 | // who knows what a subclass or custom style might do |
| 3103 | return role >= Qt::UserRole; |
| 3104 | } |
| 3105 | }; |
| 3106 | if (std::none_of(first: roles.begin(), last: roles.end(), pred: doesRoleAffectSize)) |
| 3107 | return; |
| 3108 | } |
| 3109 | d->invalidateCachedSizeHint(); |
| 3110 | if (d->hasAutoResizeSections()) { |
| 3111 | bool resizeRequired = d->globalResizeMode == ResizeToContents; |
| 3112 | int first = orientation() == Qt::Horizontal ? topLeft.column() : topLeft.row(); |
| 3113 | int last = orientation() == Qt::Horizontal ? bottomRight.column() : bottomRight.row(); |
| 3114 | for (int i = first; i <= last && !resizeRequired; ++i) |
| 3115 | resizeRequired = (sectionResizeMode(logicalIndex: i) == ResizeToContents); |
| 3116 | if (resizeRequired) |
| 3117 | d->doDelayedResizeSections(); |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | /*! |
| 3122 | \reimp |
| 3123 | \internal |
| 3124 | |
| 3125 | Empty implementation because the header doesn't show QModelIndex items. |
| 3126 | */ |
| 3127 | void QHeaderView::(const QModelIndex &, int, int) |
| 3128 | { |
| 3129 | // do nothing |
| 3130 | } |
| 3131 | |
| 3132 | /*! |
| 3133 | \reimp |
| 3134 | \internal |
| 3135 | |
| 3136 | Empty implementation because the header doesn't show QModelIndex items. |
| 3137 | */ |
| 3138 | |
| 3139 | QRect QHeaderView::(const QModelIndex &) const |
| 3140 | { |
| 3141 | return QRect(); |
| 3142 | } |
| 3143 | |
| 3144 | /*! |
| 3145 | \reimp |
| 3146 | \internal |
| 3147 | |
| 3148 | Empty implementation because the header doesn't show QModelIndex items. |
| 3149 | */ |
| 3150 | |
| 3151 | void QHeaderView::(const QModelIndex &, ScrollHint) |
| 3152 | { |
| 3153 | // do nothing - the header only displays sections |
| 3154 | } |
| 3155 | |
| 3156 | /*! |
| 3157 | \reimp |
| 3158 | \internal |
| 3159 | |
| 3160 | Empty implementation because the header doesn't show QModelIndex items. |
| 3161 | */ |
| 3162 | |
| 3163 | QModelIndex QHeaderView::(const QPoint &) const |
| 3164 | { |
| 3165 | return QModelIndex(); |
| 3166 | } |
| 3167 | |
| 3168 | /*! |
| 3169 | \reimp |
| 3170 | \internal |
| 3171 | |
| 3172 | Empty implementation because the header doesn't show QModelIndex items. |
| 3173 | */ |
| 3174 | |
| 3175 | bool QHeaderView::(const QModelIndex &) const |
| 3176 | { |
| 3177 | return true; // the header view has no items, just sections |
| 3178 | } |
| 3179 | |
| 3180 | /*! |
| 3181 | \reimp |
| 3182 | \internal |
| 3183 | |
| 3184 | Empty implementation because the header doesn't show QModelIndex items. |
| 3185 | */ |
| 3186 | |
| 3187 | QModelIndex QHeaderView::(CursorAction, Qt::KeyboardModifiers) |
| 3188 | { |
| 3189 | return QModelIndex(); |
| 3190 | } |
| 3191 | |
| 3192 | /*! |
| 3193 | \reimp |
| 3194 | |
| 3195 | Selects the items in the given \a rect according to the specified |
| 3196 | \a flags. |
| 3197 | |
| 3198 | The base class implementation does nothing. |
| 3199 | */ |
| 3200 | |
| 3201 | void QHeaderView::(const QRect&, QItemSelectionModel::SelectionFlags) |
| 3202 | { |
| 3203 | // do nothing |
| 3204 | } |
| 3205 | |
| 3206 | /*! |
| 3207 | \internal |
| 3208 | */ |
| 3209 | |
| 3210 | QRegion QHeaderView::(const QItemSelection &selection) const |
| 3211 | { |
| 3212 | Q_D(const QHeaderView); |
| 3213 | const int max = d->modelSectionCount(); |
| 3214 | |
| 3215 | if (d->orientation == Qt::Horizontal) { |
| 3216 | int logicalLeft = max; |
| 3217 | int logicalRight = 0; |
| 3218 | |
| 3219 | if (d->visualIndices.empty()) { |
| 3220 | // If no reordered sections, skip redundant visual-to-logical transformations |
| 3221 | for (const auto &r : selection) { |
| 3222 | if (r.parent().isValid() || !r.isValid()) |
| 3223 | continue; // we only know about toplevel items and we don't want invalid ranges |
| 3224 | if (r.left() < logicalLeft) |
| 3225 | logicalLeft = r.left(); |
| 3226 | if (r.right() > logicalRight) |
| 3227 | logicalRight = r.right(); |
| 3228 | } |
| 3229 | } else { |
| 3230 | int left = max; |
| 3231 | int right = 0; |
| 3232 | for (const auto &r : selection) { |
| 3233 | if (r.parent().isValid() || !r.isValid()) |
| 3234 | continue; // we only know about toplevel items and we don't want invalid ranges |
| 3235 | for (int k = r.left(); k <= r.right(); ++k) { |
| 3236 | int visual = visualIndex(logicalIndex: k); |
| 3237 | if (visual == -1) // in some cases users may change the selections |
| 3238 | continue; // before we have a chance to do the layout |
| 3239 | if (visual < left) |
| 3240 | left = visual; |
| 3241 | if (visual > right) |
| 3242 | right = visual; |
| 3243 | } |
| 3244 | } |
| 3245 | logicalLeft = logicalIndex(visualIndex: left); |
| 3246 | logicalRight = logicalIndex(visualIndex: right); |
| 3247 | } |
| 3248 | |
| 3249 | if (logicalLeft < 0 || logicalLeft >= count() || |
| 3250 | logicalRight < 0 || logicalRight >= count()) |
| 3251 | return QRegion(); |
| 3252 | |
| 3253 | int leftPos = sectionViewportPosition(logicalIndex: logicalLeft); |
| 3254 | int rightPos = sectionViewportPosition(logicalIndex: logicalRight); |
| 3255 | rightPos += sectionSize(logicalIndex: logicalRight); |
| 3256 | return QRect(leftPos, 0, rightPos - leftPos, height()); |
| 3257 | } |
| 3258 | // orientation() == Qt::Vertical |
| 3259 | int logicalTop = max; |
| 3260 | int logicalBottom = 0; |
| 3261 | |
| 3262 | if (d->visualIndices.empty()) { |
| 3263 | // If no reordered sections, skip redundant visual-to-logical transformations |
| 3264 | for (const auto &r : selection) { |
| 3265 | if (r.parent().isValid() || !r.isValid()) |
| 3266 | continue; // we only know about toplevel items and we don't want invalid ranges |
| 3267 | if (r.top() < logicalTop) |
| 3268 | logicalTop = r.top(); |
| 3269 | if (r.bottom() > logicalBottom) |
| 3270 | logicalBottom = r.bottom(); |
| 3271 | } |
| 3272 | } else { |
| 3273 | int top = max; |
| 3274 | int bottom = 0; |
| 3275 | |
| 3276 | for (const auto &r : selection) { |
| 3277 | if (r.parent().isValid() || !r.isValid()) |
| 3278 | continue; // we only know about toplevel items and we don't want invalid ranges |
| 3279 | for (int k = r.top(); k <= r.bottom(); ++k) { |
| 3280 | int visual = visualIndex(logicalIndex: k); |
| 3281 | if (visual == -1) // in some cases users may change the selections |
| 3282 | continue; // before we have a chance to do the layout |
| 3283 | if (visual < top) |
| 3284 | top = visual; |
| 3285 | if (visual > bottom) |
| 3286 | bottom = visual; |
| 3287 | } |
| 3288 | } |
| 3289 | |
| 3290 | logicalTop = logicalIndex(visualIndex: top); |
| 3291 | logicalBottom = logicalIndex(visualIndex: bottom); |
| 3292 | } |
| 3293 | |
| 3294 | if (logicalTop < 0 || logicalTop >= count() || |
| 3295 | logicalBottom < 0 || logicalBottom >= count()) |
| 3296 | return QRegion(); |
| 3297 | |
| 3298 | int topPos = sectionViewportPosition(logicalIndex: logicalTop); |
| 3299 | int bottomPos = sectionViewportPosition(logicalIndex: logicalBottom) + sectionSize(logicalIndex: logicalBottom); |
| 3300 | |
| 3301 | return QRect(0, topPos, width(), bottomPos - topPos); |
| 3302 | } |
| 3303 | |
| 3304 | |
| 3305 | // private implementation |
| 3306 | |
| 3307 | int QHeaderViewPrivate::sectionHandleAt(int position) |
| 3308 | { |
| 3309 | Q_Q(QHeaderView); |
| 3310 | int visual = q->visualIndexAt(position); |
| 3311 | if (visual == -1) |
| 3312 | return -1; |
| 3313 | int log = logicalIndex(visualIndex: visual); |
| 3314 | int pos = q->sectionViewportPosition(logicalIndex: log); |
| 3315 | int grip = q->style()->pixelMetric(metric: QStyle::PM_HeaderGripMargin, option: nullptr, widget: q); |
| 3316 | |
| 3317 | bool atLeft = position < pos + grip; |
| 3318 | bool atRight = (position > pos + q->sectionSize(logicalIndex: log) - grip); |
| 3319 | if (reverse()) |
| 3320 | qSwap(value1&: atLeft, value2&: atRight); |
| 3321 | |
| 3322 | if (atLeft) { |
| 3323 | //grip at the beginning of the section |
| 3324 | while(visual > -1) { |
| 3325 | int logical = q->logicalIndex(visualIndex: --visual); |
| 3326 | if (!q->isSectionHidden(logicalIndex: logical)) |
| 3327 | return logical; |
| 3328 | } |
| 3329 | } else if (atRight) { |
| 3330 | //grip at the end of the section |
| 3331 | return log; |
| 3332 | } |
| 3333 | return -1; |
| 3334 | } |
| 3335 | |
| 3336 | void QHeaderViewPrivate::(int section, int position) |
| 3337 | { |
| 3338 | Q_Q(QHeaderView); |
| 3339 | #if QT_CONFIG(label) |
| 3340 | if (!sectionIndicator) { |
| 3341 | sectionIndicator = new QLabel(viewport); |
| 3342 | } |
| 3343 | #endif |
| 3344 | |
| 3345 | int w, h; |
| 3346 | int p = q->sectionViewportPosition(logicalIndex: section); |
| 3347 | if (orientation == Qt::Horizontal) { |
| 3348 | w = q->sectionSize(logicalIndex: section); |
| 3349 | h = viewport->height(); |
| 3350 | } else { |
| 3351 | w = viewport->width(); |
| 3352 | h = q->sectionSize(logicalIndex: section); |
| 3353 | } |
| 3354 | #if QT_CONFIG(label) |
| 3355 | sectionIndicator->resize(w, h); |
| 3356 | #endif |
| 3357 | |
| 3358 | const qreal pixmapDevicePixelRatio = q->devicePixelRatio(); |
| 3359 | QPixmap pm(QSize(w, h) * pixmapDevicePixelRatio); |
| 3360 | pm.setDevicePixelRatio(pixmapDevicePixelRatio); |
| 3361 | pm.fill(fillColor: QColor(0, 0, 0, 45)); |
| 3362 | QRect rect(0, 0, w, h); |
| 3363 | |
| 3364 | QPainter painter(&pm); |
| 3365 | const QVariant variant = model->headerData(section, orientation, |
| 3366 | role: Qt::FontRole); |
| 3367 | if (variant.isValid() && variant.canConvert<QFont>()) { |
| 3368 | const QFont sectionFont = qvariant_cast<QFont>(v: variant); |
| 3369 | painter.setFont(sectionFont); |
| 3370 | } else { |
| 3371 | painter.setFont(q->font()); |
| 3372 | } |
| 3373 | |
| 3374 | painter.setOpacity(0.75); |
| 3375 | q->paintSection(painter: &painter, rect, logicalIndex: section); |
| 3376 | painter.end(); |
| 3377 | |
| 3378 | #if QT_CONFIG(label) |
| 3379 | sectionIndicator->setPixmap(pm); |
| 3380 | #endif |
| 3381 | sectionIndicatorOffset = position - qMax(a: p, b: 0); |
| 3382 | } |
| 3383 | |
| 3384 | void QHeaderViewPrivate::(int section, int position) |
| 3385 | { |
| 3386 | #if QT_CONFIG(label) |
| 3387 | if (!sectionIndicator) |
| 3388 | return; |
| 3389 | |
| 3390 | if (section == -1 || target == -1) { |
| 3391 | sectionIndicator->hide(); |
| 3392 | return; |
| 3393 | } |
| 3394 | |
| 3395 | if (orientation == Qt::Horizontal) |
| 3396 | sectionIndicator->move(ax: position - sectionIndicatorOffset, ay: 0); |
| 3397 | else |
| 3398 | sectionIndicator->move(ax: 0, ay: position - sectionIndicatorOffset); |
| 3399 | |
| 3400 | sectionIndicator->show(); |
| 3401 | #endif |
| 3402 | } |
| 3403 | |
| 3404 | /*! |
| 3405 | Initialize \a option with the values from this QHeaderView. This method is |
| 3406 | useful for subclasses when they need a QStyleOptionHeader, but do not want |
| 3407 | to fill in all the information themselves. |
| 3408 | |
| 3409 | \sa QStyleOption::initFrom(), initStyleOptionForIndex() |
| 3410 | */ |
| 3411 | void QHeaderView::(QStyleOptionHeader *option) const |
| 3412 | { |
| 3413 | Q_D(const QHeaderView); |
| 3414 | option->initFrom(w: this); |
| 3415 | option->state = QStyle::State_None | QStyle::State_Raised; |
| 3416 | option->orientation = d->orientation; |
| 3417 | if (d->orientation == Qt::Horizontal) |
| 3418 | option->state |= QStyle::State_Horizontal; |
| 3419 | if (isEnabled()) |
| 3420 | option->state |= QStyle::State_Enabled; |
| 3421 | option->section = 0; |
| 3422 | } |
| 3423 | |
| 3424 | void QHeaderView::(QStyleOptionFrame *option) const |
| 3425 | { |
| 3426 | // The QFrame version is only here to avoid compiler warnings. |
| 3427 | // If invoked we just pass it on to the base class. |
| 3428 | QFrame::initStyleOption(option); |
| 3429 | } |
| 3430 | |
| 3431 | bool QHeaderViewPrivate::(int section) const |
| 3432 | { |
| 3433 | int i = section * 2; |
| 3434 | if (i < 0 || i >= sectionSelected.size()) |
| 3435 | return false; |
| 3436 | if (sectionSelected.testBit(i)) // if the value was cached |
| 3437 | return sectionSelected.testBit(i: i + 1); |
| 3438 | bool s = false; |
| 3439 | if (orientation == Qt::Horizontal) |
| 3440 | s = isColumnSelected(column: section); |
| 3441 | else |
| 3442 | s = isRowSelected(row: section); |
| 3443 | sectionSelected.setBit(i: i + 1, val: s); // selection state |
| 3444 | sectionSelected.setBit(i, val: true); // cache state |
| 3445 | return s; |
| 3446 | } |
| 3447 | |
| 3448 | bool QHeaderViewPrivate::(int section) const |
| 3449 | { |
| 3450 | if (sectionStartposRecalc) |
| 3451 | recalcSectionStartPos(); |
| 3452 | const SectionItem &item = sectionItems.at(i: section); |
| 3453 | return item.size > 0 && item.calculated_startpos == 0; |
| 3454 | } |
| 3455 | |
| 3456 | bool QHeaderViewPrivate::(int section) const |
| 3457 | { |
| 3458 | if (sectionStartposRecalc) |
| 3459 | recalcSectionStartPos(); |
| 3460 | const SectionItem &item = sectionItems.at(i: section); |
| 3461 | return item.size > 0 && item.calculatedEndPos() == length; |
| 3462 | } |
| 3463 | |
| 3464 | /*! |
| 3465 | \internal |
| 3466 | Returns the last visible (ie. not hidden) visual index |
| 3467 | */ |
| 3468 | int QHeaderViewPrivate::() const |
| 3469 | { |
| 3470 | Q_Q(const QHeaderView); |
| 3471 | for (int visual = q->count()-1; visual >= 0; --visual) { |
| 3472 | if (!q->isSectionHidden(logicalIndex: q->logicalIndex(visualIndex: visual))) |
| 3473 | return visual; |
| 3474 | } |
| 3475 | |
| 3476 | //default value if no section is actually visible |
| 3477 | return -1; |
| 3478 | } |
| 3479 | |
| 3480 | void QHeaderViewPrivate::() |
| 3481 | { |
| 3482 | Q_Q(QHeaderView); |
| 3483 | if (lastSectionLogicalIdx < 0) |
| 3484 | return; |
| 3485 | int resizeLogIdx = lastSectionLogicalIdx; |
| 3486 | lastSectionLogicalIdx = -1; // We do not want resize to catch it as the last section. |
| 3487 | q->resizeSection(logical: resizeLogIdx, size: lastSectionSize); |
| 3488 | } |
| 3489 | |
| 3490 | void QHeaderViewPrivate::(int visualIndexForLastSection) |
| 3491 | { |
| 3492 | Q_Q(QHeaderView); |
| 3493 | lastSectionSize = -1; |
| 3494 | lastSectionLogicalIdx = q->logicalIndex(visualIndex: visualIndexForLastSection); |
| 3495 | lastSectionSize = headerSectionSize(visual: visualIndexForLastSection); // pick size directly since ... |
| 3496 | // q->sectionSize(lastSectionLogicalIdx) may do delayed resize and stretch it before we get the value. |
| 3497 | } |
| 3498 | |
| 3499 | void QHeaderViewPrivate::maybeRestorePrevLastSectionAndStretchLast() |
| 3500 | { |
| 3501 | Q_Q(const QHeaderView); |
| 3502 | if (!q->stretchLastSection()) |
| 3503 | return; |
| 3504 | |
| 3505 | int nowLastVisualSection = lastVisibleVisualIndex(); |
| 3506 | if (lastSectionLogicalIdx == q->logicalIndex(visualIndex: nowLastVisualSection)) |
| 3507 | return; |
| 3508 | |
| 3509 | // restore old last section. |
| 3510 | restoreSizeOnPrevLastSection(); |
| 3511 | setNewLastSection(nowLastVisualSection); |
| 3512 | doDelayedResizeSections(); // Do stretch of last section soon (but not now). |
| 3513 | } |
| 3514 | |
| 3515 | |
| 3516 | /*! |
| 3517 | \internal |
| 3518 | Go through and resize all of the sections applying stretchLastSection, |
| 3519 | manual stretches, sizes, and useGlobalMode. |
| 3520 | |
| 3521 | The different resize modes are: |
| 3522 | Interactive - the user decides the size |
| 3523 | Stretch - take up whatever space is left |
| 3524 | Fixed - the size is set programmatically outside the header |
| 3525 | ResizeToContentes - the size is set based on the contents of the row or column in the parent view |
| 3526 | |
| 3527 | The resize mode will not affect the last section if stretchLastSection is true. |
| 3528 | */ |
| 3529 | void QHeaderViewPrivate::(QHeaderView::ResizeMode globalMode, bool useGlobalMode) |
| 3530 | { |
| 3531 | Q_Q(QHeaderView); |
| 3532 | //stop the timer in case it is delayed |
| 3533 | delayedResize.stop(); |
| 3534 | |
| 3535 | executePostedLayout(); |
| 3536 | if (sectionCount() == 0) |
| 3537 | return; |
| 3538 | |
| 3539 | if (resizeRecursionBlock) |
| 3540 | return; |
| 3541 | resizeRecursionBlock = true; |
| 3542 | |
| 3543 | invalidateCachedSizeHint(); |
| 3544 | const int lastSectionVisualIdx = q->visualIndex(logicalIndex: lastSectionLogicalIdx); |
| 3545 | |
| 3546 | // find stretchLastSection if we have it |
| 3547 | int stretchSection = -1; |
| 3548 | if (stretchLastSection && !useGlobalMode) |
| 3549 | stretchSection = lastSectionVisualIdx; |
| 3550 | |
| 3551 | // count up the number of stretched sections and how much space left for them |
| 3552 | int lengthToStretch = (orientation == Qt::Horizontal ? viewport->width() : viewport->height()); |
| 3553 | int numberOfStretchedSections = 0; |
| 3554 | QList<int> section_sizes; |
| 3555 | for (int i = 0; i < sectionCount(); ++i) { |
| 3556 | if (isVisualIndexHidden(visual: i)) |
| 3557 | continue; |
| 3558 | |
| 3559 | QHeaderView::ResizeMode resizeMode; |
| 3560 | if (useGlobalMode && (i != stretchSection)) |
| 3561 | resizeMode = globalMode; |
| 3562 | else |
| 3563 | resizeMode = (i == stretchSection ? QHeaderView::Stretch : headerSectionResizeMode(visual: i)); |
| 3564 | |
| 3565 | if (resizeMode == QHeaderView::Stretch) { |
| 3566 | ++numberOfStretchedSections; |
| 3567 | section_sizes.append(t: headerSectionSize(visual: i)); |
| 3568 | continue; |
| 3569 | } |
| 3570 | |
| 3571 | // because it isn't stretch, determine its width and remove that from lengthToStretch |
| 3572 | int sectionSize = 0; |
| 3573 | if (resizeMode == QHeaderView::Interactive || resizeMode == QHeaderView::Fixed) { |
| 3574 | sectionSize = qBound(min: q->minimumSectionSize(), val: headerSectionSize(visual: i), max: q->maximumSectionSize()); |
| 3575 | } else { // resizeMode == QHeaderView::ResizeToContents |
| 3576 | int logicalIndex = q->logicalIndex(visualIndex: i); |
| 3577 | sectionSize = qMax(a: viewSectionSizeHint(logical: logicalIndex), |
| 3578 | b: q->sectionSizeHint(logicalIndex)); |
| 3579 | } |
| 3580 | sectionSize = qBound(min: q->minimumSectionSize(), |
| 3581 | val: sectionSize, |
| 3582 | max: q->maximumSectionSize()); |
| 3583 | |
| 3584 | section_sizes.append(t: sectionSize); |
| 3585 | lengthToStretch -= sectionSize; |
| 3586 | } |
| 3587 | |
| 3588 | // calculate the new length for all of the stretched sections |
| 3589 | int stretchSectionLength = -1; |
| 3590 | int pixelReminder = 0; |
| 3591 | if (numberOfStretchedSections > 0 && lengthToStretch > 0) { // we have room to stretch in |
| 3592 | int hintLengthForEveryStretchedSection = lengthToStretch / numberOfStretchedSections; |
| 3593 | stretchSectionLength = qMax(a: hintLengthForEveryStretchedSection, b: q->minimumSectionSize()); |
| 3594 | pixelReminder = lengthToStretch % numberOfStretchedSections; |
| 3595 | } |
| 3596 | |
| 3597 | // ### The code below would be nicer if it was cleaned up a bit (since spans has been replaced with items) |
| 3598 | int spanStartSection = 0; |
| 3599 | int previousSectionLength = 0; |
| 3600 | |
| 3601 | QHeaderView::ResizeMode previousSectionResizeMode = QHeaderView::Interactive; |
| 3602 | |
| 3603 | // resize each section along the total length |
| 3604 | for (int i = 0; i < sectionCount(); ++i) { |
| 3605 | int oldSectionLength = headerSectionSize(visual: i); |
| 3606 | int newSectionLength = -1; |
| 3607 | QHeaderView::ResizeMode newSectionResizeMode = headerSectionResizeMode(visual: i); |
| 3608 | |
| 3609 | if (isVisualIndexHidden(visual: i)) { |
| 3610 | newSectionLength = 0; |
| 3611 | } else { |
| 3612 | QHeaderView::ResizeMode resizeMode; |
| 3613 | if (useGlobalMode) |
| 3614 | resizeMode = globalMode; |
| 3615 | else |
| 3616 | resizeMode = (i == stretchSection |
| 3617 | ? QHeaderView::Stretch |
| 3618 | : newSectionResizeMode); |
| 3619 | if (resizeMode == QHeaderView::Stretch && stretchSectionLength != -1) { |
| 3620 | if (i == lastSectionVisualIdx) |
| 3621 | newSectionLength = qMax(a: stretchSectionLength, b: lastSectionSize); |
| 3622 | else |
| 3623 | newSectionLength = stretchSectionLength; |
| 3624 | if (pixelReminder > 0) { |
| 3625 | newSectionLength += 1; |
| 3626 | --pixelReminder; |
| 3627 | } |
| 3628 | section_sizes.removeFirst(); |
| 3629 | } else { |
| 3630 | newSectionLength = section_sizes.takeFirst(); |
| 3631 | } |
| 3632 | } |
| 3633 | |
| 3634 | //Q_ASSERT(newSectionLength > 0); |
| 3635 | if ((previousSectionResizeMode != newSectionResizeMode |
| 3636 | || previousSectionLength != newSectionLength) && i > 0) { |
| 3637 | createSectionItems(start: spanStartSection, end: i - 1, sectionSize: previousSectionLength, mode: previousSectionResizeMode); |
| 3638 | //Q_ASSERT(headerLength() == length); |
| 3639 | spanStartSection = i; |
| 3640 | } |
| 3641 | |
| 3642 | if (newSectionLength != oldSectionLength) |
| 3643 | emit q->sectionResized(logicalIndex: logicalIndex(visualIndex: i), oldSize: oldSectionLength, newSize: newSectionLength); |
| 3644 | |
| 3645 | previousSectionLength = newSectionLength; |
| 3646 | previousSectionResizeMode = newSectionResizeMode; |
| 3647 | } |
| 3648 | |
| 3649 | createSectionItems(start: spanStartSection, end: sectionCount() - 1, |
| 3650 | sectionSize: previousSectionLength, mode: previousSectionResizeMode); |
| 3651 | //Q_ASSERT(headerLength() == length); |
| 3652 | resizeRecursionBlock = false; |
| 3653 | viewport->update(); |
| 3654 | } |
| 3655 | |
| 3656 | void QHeaderViewPrivate::(int start, int end, int sizePerSection, QHeaderView::ResizeMode mode) |
| 3657 | { |
| 3658 | if (end >= sectionItems.size()) { |
| 3659 | sectionItems.resize(size: end + 1); |
| 3660 | sectionStartposRecalc = true; |
| 3661 | } |
| 3662 | SectionItem *sectiondata = sectionItems.data(); |
| 3663 | for (int i = start; i <= end; ++i) { |
| 3664 | length += (sizePerSection - sectiondata[i].size); |
| 3665 | sectionStartposRecalc |= (sectiondata[i].size != sizePerSection); |
| 3666 | sectiondata[i].size = sizePerSection; |
| 3667 | sectiondata[i].resizeMode = mode; |
| 3668 | } |
| 3669 | } |
| 3670 | |
| 3671 | void QHeaderViewPrivate::(int start, int end) |
| 3672 | { |
| 3673 | // remove sections |
| 3674 | sectionStartposRecalc |= (end != sectionItems.size() - 1); |
| 3675 | int removedlength = 0; |
| 3676 | for (int u = start; u <= end; ++u) |
| 3677 | removedlength += sectionItems.at(i: u).size; |
| 3678 | length -= removedlength; |
| 3679 | sectionItems.remove(i: start, n: end - start + 1); |
| 3680 | } |
| 3681 | |
| 3682 | void QHeaderViewPrivate::() |
| 3683 | { |
| 3684 | if (state != NoClear) { |
| 3685 | length = 0; |
| 3686 | visualIndices.clear(); |
| 3687 | logicalIndices.clear(); |
| 3688 | sectionSelected.clear(); |
| 3689 | hiddenSectionSize.clear(); |
| 3690 | sectionItems.clear(); |
| 3691 | lastSectionLogicalIdx = -1; |
| 3692 | invalidateCachedSizeHint(); |
| 3693 | } |
| 3694 | } |
| 3695 | |
| 3696 | static Qt::SortOrder flipOrder(Qt::SortOrder order) |
| 3697 | { |
| 3698 | switch (order) { |
| 3699 | case Qt::AscendingOrder: |
| 3700 | return Qt::DescendingOrder; |
| 3701 | case Qt::DescendingOrder: |
| 3702 | return Qt::AscendingOrder; |
| 3703 | }; |
| 3704 | Q_UNREACHABLE_RETURN(Qt::AscendingOrder); |
| 3705 | }; |
| 3706 | |
| 3707 | void QHeaderViewPrivate::(int section) |
| 3708 | { |
| 3709 | Q_Q(QHeaderView); |
| 3710 | Qt::SortOrder sortOrder; |
| 3711 | if (sortIndicatorSection == section) { |
| 3712 | if (sortIndicatorClearable) { |
| 3713 | const Qt::SortOrder defaultSortOrder = defaultSortOrderForSection(section); |
| 3714 | if (sortIndicatorOrder == defaultSortOrder) { |
| 3715 | sortOrder = flipOrder(order: sortIndicatorOrder); |
| 3716 | } else { |
| 3717 | section = -1; |
| 3718 | sortOrder = Qt::AscendingOrder; |
| 3719 | } |
| 3720 | } else { |
| 3721 | sortOrder = flipOrder(order: sortIndicatorOrder); |
| 3722 | } |
| 3723 | } else { |
| 3724 | sortOrder = defaultSortOrderForSection(section); |
| 3725 | } |
| 3726 | q->setSortIndicator(logicalIndex: section, order: sortOrder); |
| 3727 | } |
| 3728 | |
| 3729 | Qt::SortOrder QHeaderViewPrivate::(int section) const |
| 3730 | { |
| 3731 | const QVariant value = model->headerData(section, orientation, role: Qt::InitialSortOrderRole); |
| 3732 | if (value.canConvert<int>()) |
| 3733 | return static_cast<Qt::SortOrder>(value.toInt()); |
| 3734 | return Qt::AscendingOrder; |
| 3735 | } |
| 3736 | |
| 3737 | void QHeaderViewPrivate::(int visual, int newSize) |
| 3738 | { |
| 3739 | Q_Q(QHeaderView); |
| 3740 | const int minimumSize = q->minimumSectionSize(); |
| 3741 | const int oldSize = headerSectionSize(visual); |
| 3742 | int delta = newSize - oldSize; |
| 3743 | |
| 3744 | if (delta > 0) { // larger |
| 3745 | bool sectionResized = false; |
| 3746 | |
| 3747 | // restore old section sizes |
| 3748 | for (int i = firstCascadingSection; i < visual; ++i) { |
| 3749 | if (cascadingSectionSize.contains(key: i)) { |
| 3750 | int currentSectionSize = headerSectionSize(visual: i); |
| 3751 | int originalSectionSize = cascadingSectionSize.value(key: i); |
| 3752 | if (currentSectionSize < originalSectionSize) { |
| 3753 | int newSectionSize = currentSectionSize + delta; |
| 3754 | resizeSectionItem(visualIndex: i, oldSize: currentSectionSize, newSize: newSectionSize); |
| 3755 | if (newSectionSize >= originalSectionSize && false) |
| 3756 | cascadingSectionSize.remove(key: i); // the section is now restored |
| 3757 | sectionResized = true; |
| 3758 | break; |
| 3759 | } |
| 3760 | } |
| 3761 | |
| 3762 | } |
| 3763 | |
| 3764 | // resize the section |
| 3765 | if (!sectionResized) { |
| 3766 | newSize = qMax(a: newSize, b: minimumSize); |
| 3767 | if (oldSize != newSize) |
| 3768 | resizeSectionItem(visualIndex: visual, oldSize, newSize); |
| 3769 | } |
| 3770 | |
| 3771 | // cascade the section size change |
| 3772 | for (int i = visual + 1; i < sectionCount(); ++i) { |
| 3773 | if (isVisualIndexHidden(visual: i)) |
| 3774 | continue; |
| 3775 | if (!sectionIsCascadable(visual: i)) |
| 3776 | continue; |
| 3777 | int currentSectionSize = headerSectionSize(visual: i); |
| 3778 | if (currentSectionSize <= minimumSize) |
| 3779 | continue; |
| 3780 | int newSectionSize = qMax(a: currentSectionSize - delta, b: minimumSize); |
| 3781 | resizeSectionItem(visualIndex: i, oldSize: currentSectionSize, newSize: newSectionSize); |
| 3782 | saveCascadingSectionSize(visual: i, size: currentSectionSize); |
| 3783 | delta = delta - (currentSectionSize - newSectionSize); |
| 3784 | if (delta <= 0) |
| 3785 | break; |
| 3786 | } |
| 3787 | } else { // smaller |
| 3788 | bool sectionResized = false; |
| 3789 | |
| 3790 | // restore old section sizes |
| 3791 | for (int i = lastCascadingSection; i > visual; --i) { |
| 3792 | if (!cascadingSectionSize.contains(key: i)) |
| 3793 | continue; |
| 3794 | int currentSectionSize = headerSectionSize(visual: i); |
| 3795 | int originalSectionSize = cascadingSectionSize.value(key: i); |
| 3796 | if (currentSectionSize >= originalSectionSize) |
| 3797 | continue; |
| 3798 | int newSectionSize = currentSectionSize - delta; |
| 3799 | resizeSectionItem(visualIndex: i, oldSize: currentSectionSize, newSize: newSectionSize); |
| 3800 | if (newSectionSize >= originalSectionSize && false) { |
| 3801 | cascadingSectionSize.remove(key: i); // the section is now restored |
| 3802 | } |
| 3803 | sectionResized = true; |
| 3804 | break; |
| 3805 | } |
| 3806 | |
| 3807 | // resize the section |
| 3808 | resizeSectionItem(visualIndex: visual, oldSize, newSize: qMax(a: newSize, b: minimumSize)); |
| 3809 | |
| 3810 | // cascade the section size change |
| 3811 | if (delta < 0 && newSize < minimumSize) { |
| 3812 | for (int i = visual - 1; i >= 0; --i) { |
| 3813 | if (isVisualIndexHidden(visual: i)) |
| 3814 | continue; |
| 3815 | if (!sectionIsCascadable(visual: i)) |
| 3816 | continue; |
| 3817 | int sectionSize = headerSectionSize(visual: i); |
| 3818 | if (sectionSize <= minimumSize) |
| 3819 | continue; |
| 3820 | resizeSectionItem(visualIndex: i, oldSize: sectionSize, newSize: qMax(a: sectionSize + delta, b: minimumSize)); |
| 3821 | saveCascadingSectionSize(visual: i, size: sectionSize); |
| 3822 | break; |
| 3823 | } |
| 3824 | } |
| 3825 | |
| 3826 | // let the next section get the space from the resized section |
| 3827 | if (!sectionResized) { |
| 3828 | for (int i = visual + 1; i < sectionCount(); ++i) { |
| 3829 | if (isVisualIndexHidden(visual: i)) |
| 3830 | continue; |
| 3831 | if (!sectionIsCascadable(visual: i)) |
| 3832 | continue; |
| 3833 | int currentSectionSize = headerSectionSize(visual: i); |
| 3834 | int newSectionSize = qMax(a: currentSectionSize - delta, b: minimumSize); |
| 3835 | resizeSectionItem(visualIndex: i, oldSize: currentSectionSize, newSize: newSectionSize); |
| 3836 | break; |
| 3837 | } |
| 3838 | } |
| 3839 | } |
| 3840 | |
| 3841 | if (hasAutoResizeSections()) |
| 3842 | doDelayedResizeSections(); |
| 3843 | |
| 3844 | viewport->update(); |
| 3845 | } |
| 3846 | |
| 3847 | void QHeaderViewPrivate::(int size) |
| 3848 | { |
| 3849 | Q_Q(QHeaderView); |
| 3850 | size = qBound(min: q->minimumSectionSize(), val: size, max: q->maximumSectionSize()); |
| 3851 | executePostedLayout(); |
| 3852 | invalidateCachedSizeHint(); |
| 3853 | defaultSectionSize = size; |
| 3854 | customDefaultSectionSize = true; |
| 3855 | if (state == QHeaderViewPrivate::ResizeSection) |
| 3856 | preventCursorChangeInSetOffset = true; |
| 3857 | for (int i = 0; i < sectionItems.size(); ++i) { |
| 3858 | QHeaderViewPrivate::SectionItem §ion = sectionItems[i]; |
| 3859 | if (hiddenSectionSize.isEmpty() || !isVisualIndexHidden(visual: i)) { // resize on not hidden. |
| 3860 | // Resize to the new default if the current size is the old default, |
| 3861 | // or 0. Otherwise don't change. |
| 3862 | const int newSize = (section.size == oldDefaultSectionSize || !section.size) |
| 3863 | ? size |
| 3864 | : section.size; |
| 3865 | if (newSize != section.size) { |
| 3866 | length += newSize - section.size; //the whole length is changed |
| 3867 | const int oldSectionSize = section.sectionSize(); |
| 3868 | section.size = size; |
| 3869 | emit q->sectionResized(logicalIndex: logicalIndex(visualIndex: i), oldSize: oldSectionSize, newSize: size); |
| 3870 | } |
| 3871 | } |
| 3872 | } |
| 3873 | sectionStartposRecalc = true; |
| 3874 | if (hasAutoResizeSections()) |
| 3875 | doDelayedResizeSections(); |
| 3876 | viewport->update(); |
| 3877 | } |
| 3878 | |
| 3879 | void QHeaderViewPrivate::() |
| 3880 | { |
| 3881 | Q_Q(QHeaderView); |
| 3882 | if (orientation == Qt::Horizontal) { |
| 3883 | defaultSectionSize = q->style()->pixelMetric(metric: QStyle::PM_HeaderDefaultSectionSizeHorizontal, option: nullptr, widget: q); |
| 3884 | } else { |
| 3885 | defaultSectionSize = qMax(a: q->minimumSectionSize(), |
| 3886 | b: q->style()->pixelMetric(metric: QStyle::PM_HeaderDefaultSectionSizeVertical, option: nullptr, widget: q)); |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | void QHeaderViewPrivate::() const // linear (but fast) |
| 3891 | { |
| 3892 | int pixelpos = 0; |
| 3893 | for (const SectionItem &i : sectionItems) { |
| 3894 | i.calculated_startpos = pixelpos; // write into const mutable |
| 3895 | pixelpos += i.size; |
| 3896 | } |
| 3897 | sectionStartposRecalc = false; |
| 3898 | } |
| 3899 | |
| 3900 | void QHeaderViewPrivate::(int visualIndex, int oldSize, int newSize) |
| 3901 | { |
| 3902 | Q_Q(QHeaderView); |
| 3903 | QHeaderView::ResizeMode mode = headerSectionResizeMode(visual: visualIndex); |
| 3904 | createSectionItems(start: visualIndex, end: visualIndex, sizePerSection: newSize, mode); |
| 3905 | emit q->sectionResized(logicalIndex: logicalIndex(visualIndex), oldSize, newSize); |
| 3906 | } |
| 3907 | |
| 3908 | int QHeaderViewPrivate::(int visual) const |
| 3909 | { |
| 3910 | if (visual < sectionCount() && visual >= 0) |
| 3911 | return sectionItems.at(i: visual).sectionSize(); |
| 3912 | return -1; |
| 3913 | } |
| 3914 | |
| 3915 | int QHeaderViewPrivate::(int visual) const |
| 3916 | { |
| 3917 | if (visual < sectionCount() && visual >= 0) { |
| 3918 | if (sectionStartposRecalc) |
| 3919 | recalcSectionStartPos(); |
| 3920 | return sectionItems.at(i: visual).calculated_startpos; |
| 3921 | } |
| 3922 | return -1; |
| 3923 | } |
| 3924 | |
| 3925 | int QHeaderViewPrivate::(int position) const |
| 3926 | { |
| 3927 | if (sectionStartposRecalc) |
| 3928 | recalcSectionStartPos(); |
| 3929 | int startidx = 0; |
| 3930 | int endidx = sectionItems.size() - 1; |
| 3931 | while (startidx <= endidx) { |
| 3932 | int middle = (endidx + startidx) / 2; |
| 3933 | if (sectionItems.at(i: middle).calculated_startpos > position) { |
| 3934 | endidx = middle - 1; |
| 3935 | } else { |
| 3936 | if (sectionItems.at(i: middle).calculatedEndPos() <= position) |
| 3937 | startidx = middle + 1; |
| 3938 | else // we found it. |
| 3939 | return middle; |
| 3940 | } |
| 3941 | } |
| 3942 | return -1; |
| 3943 | } |
| 3944 | |
| 3945 | void QHeaderViewPrivate::(int visual, QHeaderView::ResizeMode mode) |
| 3946 | { |
| 3947 | int size = headerSectionSize(visual); |
| 3948 | createSectionItems(start: visual, end: visual, sizePerSection: size, mode); |
| 3949 | } |
| 3950 | |
| 3951 | QHeaderView::ResizeMode QHeaderViewPrivate::(int visual) const |
| 3952 | { |
| 3953 | if (visual < 0 || visual >= sectionItems.size()) |
| 3954 | return globalResizeMode; |
| 3955 | return static_cast<QHeaderView::ResizeMode>(sectionItems.at(i: visual).resizeMode); |
| 3956 | } |
| 3957 | |
| 3958 | void QHeaderViewPrivate::(QHeaderView::ResizeMode mode) |
| 3959 | { |
| 3960 | globalResizeMode = mode; |
| 3961 | for (int i = 0; i < sectionItems.size(); ++i) |
| 3962 | sectionItems[i].resizeMode = mode; |
| 3963 | } |
| 3964 | |
| 3965 | int QHeaderViewPrivate::(int logical) const |
| 3966 | { |
| 3967 | if (QAbstractItemView *view = qobject_cast<QAbstractItemView*>(object: parent)) { |
| 3968 | return (orientation == Qt::Horizontal |
| 3969 | ? view->sizeHintForColumn(column: logical) |
| 3970 | : view->sizeHintForRow(row: logical)); |
| 3971 | } |
| 3972 | return 0; |
| 3973 | } |
| 3974 | |
| 3975 | int QHeaderViewPrivate::(int visualIndex) const |
| 3976 | { |
| 3977 | if (!hiddenSectionSize.isEmpty()) { |
| 3978 | int adjustedVisualIndex = visualIndex; |
| 3979 | int currentVisualIndex = 0; |
| 3980 | for (int i = 0; i < sectionItems.size(); ++i) { |
| 3981 | if (isVisualIndexHidden(visual: i)) |
| 3982 | ++adjustedVisualIndex; |
| 3983 | else |
| 3984 | ++currentVisualIndex; |
| 3985 | if (currentVisualIndex >= visualIndex) |
| 3986 | break; |
| 3987 | } |
| 3988 | visualIndex = adjustedVisualIndex; |
| 3989 | } |
| 3990 | return visualIndex; |
| 3991 | } |
| 3992 | |
| 3993 | void QHeaderViewPrivate::(const QScrollBar *scrollBar, QAbstractItemView::ScrollMode scrollMode) |
| 3994 | { |
| 3995 | Q_Q(QHeaderView); |
| 3996 | if (scrollMode == QAbstractItemView::ScrollPerItem) { |
| 3997 | if (scrollBar->maximum() > 0 && scrollBar->value() == scrollBar->maximum()) |
| 3998 | q->setOffsetToLastSection(); |
| 3999 | else |
| 4000 | q->setOffsetToSectionPosition(scrollBar->value()); |
| 4001 | } else { |
| 4002 | q->setOffset(scrollBar->value()); |
| 4003 | } |
| 4004 | } |
| 4005 | |
| 4006 | void QHeaderViewPrivate::(int logical) |
| 4007 | { |
| 4008 | Q_Q(QHeaderView); |
| 4009 | const int visual = visualIndex(logicalIndex: logical); |
| 4010 | int from = logicalIndex(visualIndex: visual > 1 ? visual - 1 : 0); |
| 4011 | int to = logicalIndex(visualIndex: visual + 1 >= sectionCount() ? visual : visual + 1); |
| 4012 | QRect updateRect; |
| 4013 | if (orientation == Qt::Horizontal) { |
| 4014 | if (reverse()) |
| 4015 | std::swap(a&: from, b&: to); |
| 4016 | updateRect = QRect(QPoint(q->sectionViewportPosition(logicalIndex: from), 0), |
| 4017 | QPoint(q->sectionViewportPosition(logicalIndex: to) + headerSectionSize(visual: to), viewport->height())); |
| 4018 | } else { |
| 4019 | updateRect = QRect(QPoint(0, q->sectionViewportPosition(logicalIndex: from)), |
| 4020 | QPoint(viewport->width(), q->sectionViewportPosition(logicalIndex: to) + headerSectionSize(visual: to))); |
| 4021 | } |
| 4022 | viewport->update(updateRect); |
| 4023 | } |
| 4024 | |
| 4025 | #ifndef QT_NO_DATASTREAM |
| 4026 | void QHeaderViewPrivate::(QDataStream &out) const |
| 4027 | { |
| 4028 | out << int(orientation); |
| 4029 | out << int(sortIndicatorOrder); |
| 4030 | out << sortIndicatorSection; |
| 4031 | out << sortIndicatorShown; |
| 4032 | |
| 4033 | out << visualIndices; |
| 4034 | out << logicalIndices; |
| 4035 | |
| 4036 | out << sectionsHiddenToBitVector(); |
| 4037 | out << hiddenSectionSize; |
| 4038 | |
| 4039 | out << length; |
| 4040 | out << sectionCount(); |
| 4041 | out << movableSections; |
| 4042 | out << clickableSections; |
| 4043 | out << highlightSelected; |
| 4044 | out << stretchLastSection; |
| 4045 | out << cascadingResizing; |
| 4046 | out << stretchSections; |
| 4047 | out << contentsSections; |
| 4048 | out << defaultSectionSize; |
| 4049 | out << minimumSectionSize; |
| 4050 | |
| 4051 | out << int(defaultAlignment); |
| 4052 | out << int(globalResizeMode); |
| 4053 | |
| 4054 | out << sectionItems; |
| 4055 | out << resizeContentsPrecision; |
| 4056 | out << customDefaultSectionSize; |
| 4057 | out << lastSectionSize; |
| 4058 | out << int(sortIndicatorClearable); |
| 4059 | } |
| 4060 | |
| 4061 | bool QHeaderViewPrivate::(QDataStream &in) |
| 4062 | { |
| 4063 | Q_Q(QHeaderView); |
| 4064 | int orient, order, align, global; |
| 4065 | int sortIndicatorSectionIn; |
| 4066 | bool sortIndicatorShownIn; |
| 4067 | int lengthIn; |
| 4068 | QList<int> visualIndicesIn; |
| 4069 | QList<int> logicalIndicesIn; |
| 4070 | QHash<int, int> hiddenSectionSizeIn; |
| 4071 | bool movableSectionsIn; |
| 4072 | bool clickableSectionsIn; |
| 4073 | bool highlightSelectedIn; |
| 4074 | bool stretchLastSectionIn; |
| 4075 | bool cascadingResizingIn; |
| 4076 | int stretchSectionsIn; |
| 4077 | int contentsSectionsIn; |
| 4078 | int defaultSectionSizeIn; |
| 4079 | int minimumSectionSizeIn; |
| 4080 | QList<SectionItem> sectionItemsIn; |
| 4081 | |
| 4082 | in >> orient; |
| 4083 | in >> order; |
| 4084 | |
| 4085 | in >> sortIndicatorSectionIn; |
| 4086 | in >> sortIndicatorShownIn; |
| 4087 | |
| 4088 | in >> visualIndicesIn; |
| 4089 | in >> logicalIndicesIn; |
| 4090 | |
| 4091 | QBitArray sectionHidden; |
| 4092 | in >> sectionHidden; |
| 4093 | in >> hiddenSectionSizeIn; |
| 4094 | in >> lengthIn; |
| 4095 | |
| 4096 | int unusedSectionCount; // For compatibility |
| 4097 | in >> unusedSectionCount; |
| 4098 | |
| 4099 | if (in.status() != QDataStream::Ok || lengthIn < 0) |
| 4100 | return false; |
| 4101 | |
| 4102 | in >> movableSectionsIn; |
| 4103 | in >> clickableSectionsIn; |
| 4104 | in >> highlightSelectedIn; |
| 4105 | in >> stretchLastSectionIn; |
| 4106 | in >> cascadingResizingIn; |
| 4107 | in >> stretchSectionsIn; |
| 4108 | in >> contentsSectionsIn; |
| 4109 | in >> defaultSectionSizeIn; |
| 4110 | in >> minimumSectionSizeIn; |
| 4111 | |
| 4112 | in >> align; |
| 4113 | |
| 4114 | in >> global; |
| 4115 | |
| 4116 | // Check parameter consistency |
| 4117 | // Global orientation out of bounds? |
| 4118 | if (global < 0 || global > QHeaderView::ResizeToContents) |
| 4119 | return false; |
| 4120 | |
| 4121 | // Alignment out of bounds? |
| 4122 | if (align < 0 || align > Qt::AlignVertical_Mask) |
| 4123 | return false; |
| 4124 | |
| 4125 | in >> sectionItemsIn; |
| 4126 | // In Qt4 we had a vector of spans where one span could hold information on more sections. |
| 4127 | // Now we have an itemvector where one items contains information about one section |
| 4128 | // For backward compatibility with Qt4 we do the following |
| 4129 | QList<SectionItem> newSectionItems; |
| 4130 | for (int u = 0; u < sectionItemsIn.size(); ++u) { |
| 4131 | int count = sectionItemsIn.at(i: u).tmpDataStreamSectionCount; |
| 4132 | if (count > 1) |
| 4133 | sectionItemsIn[u].size /= count; |
| 4134 | for (int n = 0; n < count; ++n) |
| 4135 | newSectionItems.append(t: sectionItemsIn[u]); |
| 4136 | } |
| 4137 | |
| 4138 | int sectionItemsLengthTotal = 0; |
| 4139 | for (const SectionItem §ion : std::as_const(t&: newSectionItems)) |
| 4140 | sectionItemsLengthTotal += section.size; |
| 4141 | if (sectionItemsLengthTotal != lengthIn) |
| 4142 | return false; |
| 4143 | |
| 4144 | const int currentCount = (orient == Qt::Horizontal ? model->columnCount(parent: root) : model->rowCount(parent: root)); |
| 4145 | if (newSectionItems.size() < currentCount) { |
| 4146 | // we have sections not in the saved state, give them default settings |
| 4147 | if (!visualIndicesIn.isEmpty() && !logicalIndicesIn.isEmpty()) { |
| 4148 | for (int i = newSectionItems.size(); i < currentCount; ++i) { |
| 4149 | visualIndicesIn.append(t: i); |
| 4150 | logicalIndicesIn.append(t: i); |
| 4151 | } |
| 4152 | } |
| 4153 | const int insertCount = currentCount - newSectionItems.size(); |
| 4154 | const int insertLength = defaultSectionSizeIn * insertCount; |
| 4155 | lengthIn += insertLength; |
| 4156 | SectionItem section(defaultSectionSizeIn, globalResizeMode); |
| 4157 | newSectionItems.insert(i: newSectionItems.size(), n: insertCount, t: section); // append |
| 4158 | } |
| 4159 | |
| 4160 | orientation = static_cast<Qt::Orientation>(orient); |
| 4161 | sortIndicatorOrder = static_cast<Qt::SortOrder>(order); |
| 4162 | sortIndicatorSection = sortIndicatorSectionIn; |
| 4163 | sortIndicatorShown = sortIndicatorShownIn; |
| 4164 | visualIndices = visualIndicesIn; |
| 4165 | logicalIndices = logicalIndicesIn; |
| 4166 | hiddenSectionSize = hiddenSectionSizeIn; |
| 4167 | length = lengthIn; |
| 4168 | |
| 4169 | movableSections = movableSectionsIn; |
| 4170 | clickableSections = clickableSectionsIn; |
| 4171 | highlightSelected = highlightSelectedIn; |
| 4172 | stretchLastSection = stretchLastSectionIn; |
| 4173 | cascadingResizing = cascadingResizingIn; |
| 4174 | stretchSections = stretchSectionsIn; |
| 4175 | contentsSections = contentsSectionsIn; |
| 4176 | defaultSectionSize = defaultSectionSizeIn; |
| 4177 | minimumSectionSize = minimumSectionSizeIn; |
| 4178 | |
| 4179 | defaultAlignment = Qt::Alignment(align); |
| 4180 | globalResizeMode = static_cast<QHeaderView::ResizeMode>(global); |
| 4181 | |
| 4182 | sectionItems = newSectionItems; |
| 4183 | setHiddenSectionsFromBitVector(sectionHidden); |
| 4184 | recalcSectionStartPos(); |
| 4185 | |
| 4186 | int tmpint; |
| 4187 | in >> tmpint; |
| 4188 | if (in.status() == QDataStream::Ok) // we haven't read past end |
| 4189 | resizeContentsPrecision = tmpint; |
| 4190 | |
| 4191 | bool tmpbool; |
| 4192 | in >> tmpbool; |
| 4193 | if (in.status() == QDataStream::Ok) { // we haven't read past end |
| 4194 | customDefaultSectionSize = tmpbool; |
| 4195 | if (!customDefaultSectionSize) |
| 4196 | updateDefaultSectionSizeFromStyle(); |
| 4197 | } |
| 4198 | |
| 4199 | lastSectionSize = -1; |
| 4200 | int inLastSectionSize; |
| 4201 | in >> inLastSectionSize; |
| 4202 | if (in.status() == QDataStream::Ok) |
| 4203 | lastSectionSize = inLastSectionSize; |
| 4204 | |
| 4205 | lastSectionLogicalIdx = -1; |
| 4206 | if (stretchLastSection) { |
| 4207 | lastSectionLogicalIdx = q->logicalIndex(visualIndex: lastVisibleVisualIndex()); |
| 4208 | doDelayedResizeSections(); |
| 4209 | } |
| 4210 | |
| 4211 | int inSortIndicatorClearable; |
| 4212 | in >> inSortIndicatorClearable; |
| 4213 | if (in.status() == QDataStream::Ok) // we haven't read past end |
| 4214 | sortIndicatorClearable = inSortIndicatorClearable; |
| 4215 | |
| 4216 | return true; |
| 4217 | } |
| 4218 | |
| 4219 | #endif // QT_NO_DATASTREAM |
| 4220 | |
| 4221 | QT_END_NAMESPACE |
| 4222 | |
| 4223 | #include "moc_qheaderview.cpp" |
| 4224 | |