| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// Copyright (C) 2012 Thorbjørn Lund Martsum - tmartsum[at]gmail.com |
| 3 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 |
|
| 5 |
#include <QHeaderView> |
| 6 |
#include <QProxyStyle> |
| 7 |
#include <QSignalSpy> |
| 8 |
#include <QSortFilterProxyModel> |
| 9 |
#include <QStandardItemModel> |
| 10 |
#include <QStringListModel> |
| 11 |
#include <QTableView> |
| 12 |
#include <QTest> |
| 13 |
#include <QTreeWidget> |
| 14 |
#include <QStyledItemDelegate> |
| 15 |
#include <QtWidgets/private/qheaderview_p.h> |
| 16 |
#include <QtWidgets/private/qapplication_p.h> |
| 17 |
|
| 18 |
using BoolList = QList<bool>; |
| 19 |
using IntList = QList<int>; |
| 20 |
using ResizeVec = QList<QHeaderView::ResizeMode>; |
| 21 |
|
| 22 |
class TestStyle : public QProxyStyle |
| 23 |
{ |
| 24 |
Q_OBJECT |
| 25 |
public: |
| 26 |
void drawControl(ControlElement element, const QStyleOption *option, |
| 27 |
QPainter *painter, const QWidget *widget) const override |
| 28 |
{ |
| 29 |
if (element == CE_HeaderSection) { |
| 30 |
if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(option)) |
| 31 |
lastPosition = header->position; |
| 32 |
} |
| 33 |
QProxyStyle::drawControl(element, option, painter, widget); |
| 34 |
} |
| 35 |
mutable QStyleOptionHeader::SectionPosition lastPosition = QStyleOptionHeader::Beginning; |
| 36 |
}; |
| 37 |
|
| 38 |
class protected_QHeaderView : public QHeaderView |
| 39 |
{ |
| 40 |
Q_OBJECT |
| 41 |
public: |
| 42 |
protected_QHeaderView(Qt::Orientation orientation) : QHeaderView(orientation) |
| 43 |
{ |
| 44 |
resizeSections(); |
| 45 |
} |
| 46 |
|
| 47 |
void testEvent(); |
| 48 |
void testhorizontalOffset(); |
| 49 |
void testverticalOffset(); |
| 50 |
void testVisualRegionForSelection(); |
| 51 |
friend class tst_QHeaderView; |
| 52 |
}; |
| 53 |
|
| 54 |
class tst_QHeaderView : public QObject |
| 55 |
{ |
| 56 |
Q_OBJECT |
| 57 |
|
| 58 |
public: |
| 59 |
tst_QHeaderView(); |
| 60 |
|
| 61 |
private slots: |
| 62 |
void initTestCase(); |
| 63 |
void cleanupTestCase(); |
| 64 |
void init(); |
| 65 |
void cleanup(); |
| 66 |
void getSetCheck(); |
| 67 |
void visualIndex(); |
| 68 |
|
| 69 |
void visualIndexAt_data(); |
| 70 |
void visualIndexAt(); |
| 71 |
|
| 72 |
void noModel(); |
| 73 |
void emptyModel(); |
| 74 |
void removeRows(); |
| 75 |
void removeCols(); |
| 76 |
|
| 77 |
void clickable(); |
| 78 |
void movable(); |
| 79 |
void hidden(); |
| 80 |
void stretch(); |
| 81 |
|
| 82 |
void sectionSize_data(); |
| 83 |
void sectionSize(); |
| 84 |
|
| 85 |
void length(); |
| 86 |
void offset(); |
| 87 |
void sectionSizeHint(); |
| 88 |
void logicalIndex(); |
| 89 |
void logicalIndexAt(); |
| 90 |
void swapSections(); |
| 91 |
|
| 92 |
void moveSection_data(); |
| 93 |
void moveSection(); |
| 94 |
|
| 95 |
void resizeMode(); |
| 96 |
|
| 97 |
void resizeSection_data(); |
| 98 |
void resizeSection(); |
| 99 |
|
| 100 |
void resizeAndMoveSection_data(); |
| 101 |
void resizeAndMoveSection(); |
| 102 |
void resizeHiddenSection_data(); |
| 103 |
void resizeHiddenSection(); |
| 104 |
void resizeAndInsertSection_data(); |
| 105 |
void resizeAndInsertSection(); |
| 106 |
void resizeWithResizeModes_data(); |
| 107 |
void resizeWithResizeModes(); |
| 108 |
void moveAndInsertSection_data(); |
| 109 |
void moveAndInsertSection(); |
| 110 |
void highlightSections(); |
| 111 |
void showSortIndicator(); |
| 112 |
void clearSectionSorting(); |
| 113 |
void sortIndicatorTracking(); |
| 114 |
void removeAndInsertRow(); |
| 115 |
void unhideSection(); |
| 116 |
void testEvent(); |
| 117 |
void headerDataChanged(); |
| 118 |
void currentChanged(); |
| 119 |
void horizontalOffset(); |
| 120 |
void verticalOffset(); |
| 121 |
void stretchSectionCount(); |
| 122 |
void hiddenSectionCount(); |
| 123 |
void focusPolicy(); |
| 124 |
void moveSectionAndReset(); |
| 125 |
void moveSectionAndRemove(); |
| 126 |
void saveRestore(); |
| 127 |
void QTBUG99487_saveRestoreQt5Compat(); |
| 128 |
void restoreToMoreColumns(); |
| 129 |
void restoreToMoreColumnsNoMovedColumns(); |
| 130 |
void restoreBeforeSetModel(); |
| 131 |
void defaultSectionSizeTest(); |
| 132 |
void defaultSectionSizeTestStyles(); |
| 133 |
|
| 134 |
void defaultAlignment_data(); |
| 135 |
void defaultAlignment(); |
| 136 |
|
| 137 |
void globalResizeMode_data(); |
| 138 |
void globalResizeMode(); |
| 139 |
|
| 140 |
void sectionPressedSignal_data(); |
| 141 |
void sectionPressedSignal(); |
| 142 |
void sectionClickedSignal_data() { sectionPressedSignal_data(); } |
| 143 |
void sectionClickedSignal(); |
| 144 |
|
| 145 |
void defaultSectionSize_data(); |
| 146 |
void defaultSectionSize(); |
| 147 |
|
| 148 |
void oneSectionSize(); |
| 149 |
|
| 150 |
void hideAndInsert_data(); |
| 151 |
void hideAndInsert(); |
| 152 |
|
| 153 |
void removeSection(); |
| 154 |
void preserveHiddenSectionWidth(); |
| 155 |
void invisibleStretchLastSection(); |
| 156 |
void noSectionsWithNegativeSize(); |
| 157 |
|
| 158 |
void emptySectionSpan(); |
| 159 |
void task236450_hidden_data(); |
| 160 |
void task236450_hidden(); |
| 161 |
void task248050_hideRow(); |
| 162 |
void QTBUG6058_reset(); |
| 163 |
void QTBUG7833_sectionClicked(); |
| 164 |
void checkLayoutChangeEmptyModel(); |
| 165 |
void QTBUG8650_crashOnInsertSections(); |
| 166 |
void QTBUG12268_hiddenMovedSectionSorting(); |
| 167 |
void QTBUG14242_hideSectionAutoSize(); |
| 168 |
void QTBUG50171_visualRegionForSwappedItems(); |
| 169 |
void QTBUG53221_assertShiftHiddenRow(); |
| 170 |
void QTBUG75615_sizeHintWithStylesheet(); |
| 171 |
void ensureNoIndexAtLength(); |
| 172 |
void offsetConsistent(); |
| 173 |
void sectionsDontSortWhenNotClickingInThem(); |
| 174 |
|
| 175 |
void initialSortOrderRole(); |
| 176 |
|
| 177 |
void logicalIndexAtTest_data() { setupTestData(); } |
| 178 |
void visualIndexAtTest_data() { setupTestData(); } |
| 179 |
void hideShowTest_data() { setupTestData(); } |
| 180 |
void swapSectionsTest_data() { setupTestData(); } |
| 181 |
void moveSectionTest_data() { setupTestData(); } |
| 182 |
void defaultSizeTest_data() { setupTestData(); } |
| 183 |
void removeTest_data() { setupTestData(); } |
| 184 |
void insertTest_data() { setupTestData(); } |
| 185 |
void resizeToContentTest_data() { setupTestData(); } |
| 186 |
void logicalIndexAtTest(); |
| 187 |
void visualIndexAtTest(); |
| 188 |
void hideShowTest(); |
| 189 |
void swapSectionsTest(); |
| 190 |
void moveSectionTest(); |
| 191 |
void defaultSizeTest(); |
| 192 |
void removeTest(); |
| 193 |
void insertTest(); |
| 194 |
void resizeToContentTest(); |
| 195 |
void testStreamWithHide(); |
| 196 |
void testStylePosition(); |
| 197 |
void stretchAndRestoreLastSection(); |
| 198 |
void testMinMaxSectionSize_data(); |
| 199 |
void testMinMaxSectionSize(); |
| 200 |
void sizeHintCrash(); |
| 201 |
void testResetCachedSizeHint(); |
| 202 |
void statusTips(); |
| 203 |
void testRemovingColumnsViaLayoutChanged(); |
| 204 |
void testModelMovingColumns(); |
| 205 |
void testModelMovingRows(); |
| 206 |
|
| 207 |
void lowMemoryUsageInBasicMode(); |
| 208 |
void lowMememoryUsageOnSetDefaultSectionSize(); |
| 209 |
void normalMemoryUsageOnSwap(); |
| 210 |
void normalMemoryUsageOnMove(); |
| 211 |
void normalMemoryUsageOnResize(); |
| 212 |
void normalMemoryUsageOnHide(); |
| 213 |
void storeRestoreLowMemoryMode(); |
| 214 |
void setSectionResizeModeWithSectionWillTakeMemory(); |
| 215 |
void setModelWithAutoSizeWillSwitchToMemoryMode(); |
| 216 |
void tableViewResizeSectionsWillSwitchToMemoryMode(); |
| 217 |
|
| 218 |
void setDefaultSectionSizeRespectsColumnWidth(); |
| 219 |
|
| 220 |
protected: |
| 221 |
void setupTestData(); |
| 222 |
void additionalInit(); |
| 223 |
void testMinMaxSectionSize(bool stretchLastSection); |
| 224 |
|
| 225 |
QWidget *topLevel = nullptr; |
| 226 |
QHeaderView *view = nullptr; |
| 227 |
QStandardItemModel *model = nullptr; |
| 228 |
QTableView *m_tableview = nullptr; |
| 229 |
bool m_special_prepare = false; |
| 230 |
QElapsedTimer timer; |
| 231 |
}; |
| 232 |
|
| 233 |
class QtTestModel: public QAbstractTableModel |
| 234 |
{ |
| 235 |
Q_OBJECT |
| 236 |
public: |
| 237 |
QtTestModel(int rc, int cc, QObject *parent = nullptr) |
| 238 |
: QAbstractTableModel(parent), rows(rc), cols(cc) {} |
| 239 |
int rowCount(const QModelIndex &) const override { return rows; } |
| 240 |
int columnCount(const QModelIndex &) const override { return cols; } |
| 241 |
bool isEditable(const QModelIndex &) const { return true; } |
| 242 |
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override |
| 243 |
{ |
| 244 |
if (section < 0 || (role != Qt::DisplayRole && role != Qt::StatusTipRole)) |
| 245 |
return QVariant(); |
| 246 |
const int row = (orientation == Qt::Vertical ? section : 0); |
| 247 |
const int col = (orientation == Qt::Horizontal ? section : 0); |
| 248 |
if (orientation == Qt::Vertical && row >= rows) |
| 249 |
return QVariant(); |
| 250 |
if (orientation == Qt::Horizontal && col >= cols) |
| 251 |
return QVariant(); |
| 252 |
if (m_bMultiLine) |
| 253 |
return QString("%1\n%1").arg(section); |
| 254 |
return QLatin1Char('[') + QString::number(row) + QLatin1Char(',') |
| 255 |
+ QString::number(col) + QLatin1String(",0] -- Header"); |
| 256 |
} |
| 257 |
QVariant data(const QModelIndex &idx, int role = Qt::DisplayRole) const override |
| 258 |
{ |
| 259 |
if (role != Qt::DisplayRole) |
| 260 |
return QVariant(); |
| 261 |
if (idx.row() < 0 || idx.column() < 0 || idx.column() >= cols || idx.row() >= rows) { |
| 262 |
wrongIndex = true; |
| 263 |
qWarning("Invalid modelIndex [%d,%d,%p]", idx.row(), idx.column(), idx.internalPointer()); |
| 264 |
} |
| 265 |
return QLatin1Char('[') + QString::number(idx.row()) + QLatin1Char(',') |
| 266 |
+ QString::number(idx.column()) + QLatin1String(",0]"); |
| 267 |
} |
| 268 |
|
| 269 |
void insertOneColumn(int col) |
| 270 |
{ |
| 271 |
beginInsertColumns(QModelIndex(), col, col); |
| 272 |
--cols; |
| 273 |
endInsertColumns(); |
| 274 |
} |
| 275 |
|
| 276 |
void removeFirstRow() |
| 277 |
{ |
| 278 |
beginRemoveRows(QModelIndex(), 0, 0); |
| 279 |
--rows; |
| 280 |
endRemoveRows(); |
| 281 |
} |
| 282 |
|
| 283 |
void removeLastRow() |
| 284 |
{ |
| 285 |
beginRemoveRows(QModelIndex(), rows - 1, rows - 1); |
| 286 |
--rows; |
| 287 |
endRemoveRows(); |
| 288 |
} |
| 289 |
|
| 290 |
void removeAllRows() |
| 291 |
{ |
| 292 |
beginRemoveRows(QModelIndex(), 0, rows - 1); |
| 293 |
rows = 0; |
| 294 |
endRemoveRows(); |
| 295 |
} |
| 296 |
|
| 297 |
void moveRow(int from, int to) |
| 298 |
{ |
| 299 |
beginMoveRows(QModelIndex(), from, from, QModelIndex(), to); |
| 300 |
endMoveRows(); |
| 301 |
} |
| 302 |
|
| 303 |
void removeOneColumn(int col) |
| 304 |
{ |
| 305 |
beginRemoveColumns(QModelIndex(), col, col); |
| 306 |
--cols; |
| 307 |
endRemoveColumns(); |
| 308 |
} |
| 309 |
|
| 310 |
void removeLastColumn() |
| 311 |
{ |
| 312 |
beginRemoveColumns(QModelIndex(), cols - 1, cols - 1); |
| 313 |
--cols; |
| 314 |
endRemoveColumns(); |
| 315 |
} |
| 316 |
|
| 317 |
void removeAllColumns() |
| 318 |
{ |
| 319 |
beginRemoveColumns(QModelIndex(), 0, cols - 1); |
| 320 |
cols = 0; |
| 321 |
endRemoveColumns(); |
| 322 |
} |
| 323 |
|
| 324 |
void moveColumn(int from, int to) |
| 325 |
{ |
| 326 |
beginMoveColumns(QModelIndex(), from, from, QModelIndex(), to); |
| 327 |
endMoveColumns(); |
| 328 |
} |
| 329 |
|
| 330 |
void cleanup() |
| 331 |
{ |
| 332 |
emit layoutAboutToBeChanged(); |
| 333 |
cols = 3; |
| 334 |
rows = 3; |
| 335 |
emit layoutChanged(); |
| 336 |
} |
| 337 |
|
| 338 |
void emitLayoutChanged() |
| 339 |
{ |
| 340 |
emit layoutAboutToBeChanged(); |
| 341 |
emit layoutChanged(); |
| 342 |
} |
| 343 |
|
| 344 |
void emitLayoutChangedWithRemoveFirstRow() |
| 345 |
{ |
| 346 |
emit layoutAboutToBeChanged(); |
| 347 |
QModelIndexList milNew; |
| 348 |
const auto milOld = persistentIndexList(); |
| 349 |
milNew.reserve(milOld.size()); |
| 350 |
for (int i = 0; i < milOld.size(); ++i) |
| 351 |
milNew += QModelIndex(); |
| 352 |
changePersistentIndexList(milOld, milNew); |
| 353 |
emit layoutChanged(); |
| 354 |
} |
| 355 |
|
| 356 |
void setMultiLineHeader(bool bEnable) |
| 357 |
{ |
| 358 |
beginResetModel(); |
| 359 |
m_bMultiLine = bEnable; |
| 360 |
endResetModel(); |
| 361 |
} |
| 362 |
|
| 363 |
int rows = 0; |
| 364 |
int cols = 0; |
| 365 |
mutable bool wrongIndex = false; |
| 366 |
bool m_bMultiLine = false; |
| 367 |
}; |
| 368 |
|
| 369 |
static Qt::LayoutDirection otherLayoutDirection(Qt::LayoutDirection current) |
| 370 |
{ |
| 371 |
switch (current) { |
| 372 |
case Qt::LayoutDirection::LeftToRight: return Qt::LayoutDirection::RightToLeft; |
| 373 |
case Qt::LayoutDirection::RightToLeft: return Qt::LayoutDirection::LeftToRight; |
| 374 |
case Qt::LayoutDirection::LayoutDirectionAuto: |
| 375 |
; |
| 376 |
} |
| 377 |
Q_UNREACHABLE_RETURN(Qt::LayoutDirection::LayoutDirectionAuto); |
| 378 |
} |
| 379 |
|
| 380 |
static void swapLayoutDirection(QWidget &w) |
| 381 |
{ |
| 382 |
w.setLayoutDirection(otherLayoutDirection(w.layoutDirection())); |
| 383 |
} |
| 384 |
|
| 385 |
// Testing get/set functions |
| 386 |
void tst_QHeaderView::getSetCheck() |
| 387 |
{ |
| 388 |
protected_QHeaderView obj1(Qt::Horizontal); |
| 389 |
// bool QHeaderView::highlightSections() |
| 390 |
// void QHeaderView::setHighlightSections(bool) |
| 391 |
obj1.setHighlightSections(false); |
| 392 |
QCOMPARE(false, obj1.highlightSections()); |
| 393 |
obj1.setHighlightSections(true); |
| 394 |
QCOMPARE(true, obj1.highlightSections()); |
| 395 |
|
| 396 |
// bool QHeaderView::stretchLastSection() |
| 397 |
// void QHeaderView::setStretchLastSection(bool) |
| 398 |
obj1.setStretchLastSection(false); |
| 399 |
QCOMPARE(false, obj1.stretchLastSection()); |
| 400 |
obj1.setStretchLastSection(true); |
| 401 |
QCOMPARE(true, obj1.stretchLastSection()); |
| 402 |
|
| 403 |
// int QHeaderView::defaultSectionSize() |
| 404 |
// void QHeaderView::setDefaultSectionSize(int) |
| 405 |
obj1.setMinimumSectionSize(0); |
| 406 |
obj1.setDefaultSectionSize(-1); |
| 407 |
QCOMPARE_GE(obj1.defaultSectionSize(), 0); |
| 408 |
obj1.setDefaultSectionSize(0); |
| 409 |
QCOMPARE(0, obj1.defaultSectionSize()); |
| 410 |
obj1.setDefaultSectionSize(99999); |
| 411 |
QCOMPARE(99999, obj1.defaultSectionSize()); |
| 412 |
|
| 413 |
// int QHeaderView::minimumSectionSize() |
| 414 |
// void QHeaderView::setMinimumSectionSize(int) |
| 415 |
obj1.setMinimumSectionSize(-1); |
| 416 |
QCOMPARE_GE(obj1.minimumSectionSize(), 0); |
| 417 |
obj1.setMinimumSectionSize(0); |
| 418 |
QCOMPARE(0, obj1.minimumSectionSize()); |
| 419 |
obj1.setMinimumSectionSize(99999); |
| 420 |
QCOMPARE(99999, obj1.minimumSectionSize()); |
| 421 |
obj1.setMinimumSectionSize(-1); |
| 422 |
QCOMPARE_LT(obj1.minimumSectionSize(), 100); |
| 423 |
|
| 424 |
// int QHeaderView::offset() |
| 425 |
// void QHeaderView::setOffset(int) |
| 426 |
obj1.setOffset(0); |
| 427 |
QCOMPARE(0, obj1.offset()); |
| 428 |
obj1.setOffset(std::numeric_limits<int>::min()); |
| 429 |
QCOMPARE(std::numeric_limits<int>::min(), obj1.offset()); |
| 430 |
QTest::ignoreMessage(QtWarningMsg, // one of the INT_MAX will hit this; not necessarily the first one |
| 431 |
"Integer argument 2147483647 causes overflow in QHeaderView::setOffset " |
| 432 |
"when calling QWidget::scroll, results may not be as you expect"); |
| 433 |
obj1.setOffset(std::numeric_limits<int>::max()); |
| 434 |
QCOMPARE(std::numeric_limits<int>::max(), obj1.offset()); |
| 435 |
|
| 436 |
// and again with RTL (or LTR, if it was RTL before): |
| 437 |
swapLayoutDirection(obj1); |
| 438 |
obj1.setOffset(0); |
| 439 |
QCOMPARE(0, obj1.offset()); |
| 440 |
obj1.setOffset(std::numeric_limits<int>::min()); |
| 441 |
QCOMPARE(std::numeric_limits<int>::min(), obj1.offset()); |
| 442 |
obj1.setOffset(std::numeric_limits<int>::max()); |
| 443 |
QCOMPARE(std::numeric_limits<int>::max(), obj1.offset()); |
| 444 |
} |
| 445 |
|
| 446 |
tst_QHeaderView::tst_QHeaderView() |
| 447 |
{ |
| 448 |
qRegisterMetaType<Qt::SortOrder>("Qt::SortOrder"); |
| 449 |
} |
| 450 |
|
| 451 |
void tst_QHeaderView::initTestCase() |
| 452 |
{ |
| 453 |
m_tableview = new QTableView; |
| 454 |
qDebug().noquote().nospace() |
| 455 |
<< "default min section size is " |
| 456 |
<< QString::number(m_tableview->verticalHeader()->minimumSectionSize()) |
| 457 |
<< QLatin1Char('/') |
| 458 |
<< m_tableview->horizontalHeader()->minimumSectionSize() |
| 459 |
<< " (v/h)"; |
| 460 |
} |
| 461 |
|
| 462 |
void tst_QHeaderView::cleanupTestCase() |
| 463 |
{ |
| 464 |
delete m_tableview; |
| 465 |
} |
| 466 |
|
| 467 |
void tst_QHeaderView::init() |
| 468 |
{ |
| 469 |
topLevel = new QWidget; |
| 470 |
view = new QHeaderView(Qt::Vertical, topLevel); |
| 471 |
// Some initial value tests before a model is added |
| 472 |
QCOMPARE(view->length(), 0); |
| 473 |
QCOMPARE(view->sizeHint(), QSize(0,0)); |
| 474 |
QCOMPARE(view->sectionSizeHint(0), -1); |
| 475 |
view->setMinimumSectionSize(0); // system default min size can be to large |
| 476 |
|
| 477 |
/* |
| 478 |
model = new QStandardItemModel(1, 1); |
| 479 |
view->setModel(model); |
| 480 |
//qDebug() << view->count(); |
| 481 |
view->sizeHint(); |
| 482 |
*/ |
| 483 |
|
| 484 |
int rows = 4; |
| 485 |
int columns = 4; |
| 486 |
model = new QStandardItemModel(rows, columns); |
| 487 |
/* |
| 488 |
for (int row = 0; row < rows; ++row) { |
| 489 |
for (int column = 0; column < columns; ++column) { |
| 490 |
QModelIndex index = model->index(row, column, QModelIndex()); |
| 491 |
model->setData(index, QVariant((row+1) * (column+1))); |
| 492 |
} |
| 493 |
} |
| 494 |
*/ |
| 495 |
|
| 496 |
QSignalSpy spy(view, &QHeaderView::sectionCountChanged); |
| 497 |
view->setModel(model); |
| 498 |
QCOMPARE(spy.size(), 1); |
| 499 |
view->resize(200,200); |
| 500 |
} |
| 501 |
|
| 502 |
void tst_QHeaderView::cleanup() |
| 503 |
{ |
| 504 |
m_tableview->setUpdatesEnabled(true); |
| 505 |
m_tableview->horizontalHeader()->setUpdatesEnabled(true); |
| 506 |
m_tableview->verticalHeader()->setUpdatesEnabled(true); |
| 507 |
if (view && view->parent() != m_tableview) |
| 508 |
delete view; |
| 509 |
view = nullptr; |
| 510 |
delete model; |
| 511 |
model = nullptr; |
| 512 |
delete topLevel; |
| 513 |
topLevel = nullptr; |
| 514 |
} |
| 515 |
|
| 516 |
void tst_QHeaderView::noModel() |
| 517 |
{ |
| 518 |
QHeaderView emptyView(Qt::Vertical); |
| 519 |
QCOMPARE(emptyView.count(), 0); |
| 520 |
} |
| 521 |
|
| 522 |
void tst_QHeaderView::emptyModel() |
| 523 |
{ |
| 524 |
QtTestModel testmodel(0, 0); |
| 525 |
view->setModel(&testmodel); |
| 526 |
QVERIFY(!testmodel.wrongIndex); |
| 527 |
QCOMPARE(view->count(), testmodel.rows); |
| 528 |
view->setModel(model); |
| 529 |
} |
| 530 |
|
| 531 |
void tst_QHeaderView::removeRows() |
| 532 |
{ |
| 533 |
QtTestModel model(10, 10); |
| 534 |
|
| 535 |
QHeaderView vertical(Qt::Vertical); |
| 536 |
QHeaderView horizontal(Qt::Horizontal); |
| 537 |
|
| 538 |
vertical.setModel(&model); |
| 539 |
horizontal.setModel(&model); |
| 540 |
vertical.show(); |
| 541 |
horizontal.show(); |
| 542 |
QCOMPARE(vertical.count(), model.rows); |
| 543 |
QCOMPARE(horizontal.count(), model.cols); |
| 544 |
|
| 545 |
model.removeLastRow(); |
| 546 |
QVERIFY(!model.wrongIndex); |
| 547 |
QCOMPARE(vertical.count(), model.rows); |
| 548 |
QCOMPARE(horizontal.count(), model.cols); |
| 549 |
|
| 550 |
model.removeAllRows(); |
| 551 |
QVERIFY(!model.wrongIndex); |
| 552 |
QCOMPARE(vertical.count(), model.rows); |
| 553 |
QCOMPARE(horizontal.count(), model.cols); |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
void tst_QHeaderView::removeCols() |
| 558 |
{ |
| 559 |
QtTestModel model(10, 10); |
| 560 |
|
| 561 |
QHeaderView vertical(Qt::Vertical); |
| 562 |
QHeaderView horizontal(Qt::Horizontal); |
| 563 |
vertical.setModel(&model); |
| 564 |
horizontal.setModel(&model); |
| 565 |
vertical.show(); |
| 566 |
horizontal.show(); |
| 567 |
QCOMPARE(vertical.count(), model.rows); |
| 568 |
QCOMPARE(horizontal.count(), model.cols); |
| 569 |
|
| 570 |
model.removeLastColumn(); |
| 571 |
QVERIFY(!model.wrongIndex); |
| 572 |
QCOMPARE(vertical.count(), model.rows); |
| 573 |
QCOMPARE(horizontal.count(), model.cols); |
| 574 |
|
| 575 |
model.removeAllColumns(); |
| 576 |
QVERIFY(!model.wrongIndex); |
| 577 |
QCOMPARE(vertical.count(), model.rows); |
| 578 |
QCOMPARE(horizontal.count(), model.cols); |
| 579 |
} |
| 580 |
|
| 581 |
void tst_QHeaderView::movable() |
| 582 |
{ |
| 583 |
QCOMPARE(view->sectionsMovable(), false); |
| 584 |
view->setSectionsMovable(false); |
| 585 |
QCOMPARE(view->sectionsMovable(), false); |
| 586 |
view->setSectionsMovable(true); |
| 587 |
QCOMPARE(view->sectionsMovable(), true); |
| 588 |
|
| 589 |
QCOMPARE(view->isFirstSectionMovable(), true); |
| 590 |
view->setFirstSectionMovable(false); |
| 591 |
QCOMPARE(view->isFirstSectionMovable(), false); |
| 592 |
view->setFirstSectionMovable(true); |
| 593 |
QCOMPARE(view->isFirstSectionMovable(), true); |
| 594 |
} |
| 595 |
|
| 596 |
void tst_QHeaderView::clickable() |
| 597 |
{ |
| 598 |
QCOMPARE(view->sectionsClickable(), false); |
| 599 |
view->setSectionsClickable(false); |
| 600 |
QCOMPARE(view->sectionsClickable(), false); |
| 601 |
view->setSectionsClickable(true); |
| 602 |
QCOMPARE(view->sectionsClickable(), true); |
| 603 |
} |
| 604 |
|
| 605 |
void tst_QHeaderView::hidden() |
| 606 |
{ |
| 607 |
//hideSection() & showSection call setSectionHidden |
| 608 |
// Test bad arguments |
| 609 |
QCOMPARE(view->isSectionHidden(-1), false); |
| 610 |
QCOMPARE(view->isSectionHidden(view->count()), false); |
| 611 |
QCOMPARE(view->isSectionHidden(999999), false); |
| 612 |
|
| 613 |
view->setSectionHidden(-1, true); |
| 614 |
view->setSectionHidden(view->count(), true); |
| 615 |
view->setSectionHidden(999999, true); |
| 616 |
view->setSectionHidden(-1, false); |
| 617 |
view->setSectionHidden(view->count(), false); |
| 618 |
view->setSectionHidden(999999, false); |
| 619 |
|
| 620 |
// Hidden sections shouldn't have visual properties (except position) |
| 621 |
int pos = view->defaultSectionSize(); |
| 622 |
view->setSectionHidden(1, true); |
| 623 |
QCOMPARE(view->sectionSize(1), 0); |
| 624 |
QCOMPARE(view->sectionPosition(1), pos); |
| 625 |
view->resizeSection(1, 100); |
| 626 |
QCOMPARE(view->sectionViewportPosition(1), pos); |
| 627 |
QCOMPARE(view->sectionSize(1), 0); |
| 628 |
view->setSectionHidden(1, false); |
| 629 |
QCOMPARE(view->isSectionHidden(0), false); |
| 630 |
QCOMPARE(view->sectionSize(0), view->defaultSectionSize()); |
| 631 |
|
| 632 |
// d->hiddenSectionSize could go out of sync when a new model |
| 633 |
// was set which has fewer sections than before and some of them |
| 634 |
// were hidden |
| 635 |
QStandardItemModel model2(model->rowCount() - 1, model->columnCount()); |
| 636 |
|
| 637 |
for (int i = 0; i < model->rowCount(); ++i) |
| 638 |
view->setSectionHidden(i, true); |
| 639 |
view->setModel(&model2); |
| 640 |
QVERIFY(view->sectionsHidden()); |
| 641 |
for (int i = 0; i < model2.rowCount(); ++i) |
| 642 |
QVERIFY(view->isSectionHidden(i)); |
| 643 |
|
| 644 |
view->setModel(model); |
| 645 |
for (int i = 0; i < model2.rowCount(); ++i) |
| 646 |
QVERIFY(view->isSectionHidden(i)); |
| 647 |
QCOMPARE(view->isSectionHidden(model->rowCount() - 1), false); |
| 648 |
for (int i = 0; i < model->rowCount(); ++i) |
| 649 |
view->setSectionHidden(i, false); |
| 650 |
} |
| 651 |
|
| 652 |
void tst_QHeaderView::stretch() |
| 653 |
{ |
| 654 |
// Show before resize and setStretchLastSection |
| 655 |
QSize viewSize(500, 500); |
| 656 |
view->resize(viewSize); |
| 657 |
view->setStretchLastSection(true); |
| 658 |
QCOMPARE(view->stretchLastSection(), true); |
| 659 |
topLevel->show(); |
| 660 |
QVERIFY(QTest::qWaitForWindowExposed(topLevel)); |
| 661 |
QCOMPARE(view->width(), viewSize.width()); |
| 662 |
QCOMPARE(view->visualIndexAt(view->viewport()->height() - 5), 3); |
| 663 |
|
| 664 |
view->setSectionHidden(3, true); |
| 665 |
QCOMPARE(view->visualIndexAt(view->viewport()->height() - 5), 2); |
| 666 |
|
| 667 |
view->setStretchLastSection(false); |
| 668 |
QCOMPARE(view->stretchLastSection(), false); |
| 669 |
} |
| 670 |
|
| 671 |
void tst_QHeaderView::oneSectionSize() |
| 672 |
{ |
| 673 |
//this ensures that if there is only one section, it gets a correct width (more than 0) |
| 674 |
QHeaderView view (Qt::Vertical); |
| 675 |
QtTestModel model(1, 1); |
| 676 |
|
| 677 |
view.setSectionResizeMode(QHeaderView::Interactive); |
| 678 |
view.setModel(&model); |
| 679 |
view.show(); |
| 680 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 681 |
|
| 682 |
QCOMPARE_GT(view.sectionSize(0), 0); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
void tst_QHeaderView::sectionSize_data() |
| 687 |
{ |
| 688 |
QTest::addColumn<IntList>("boundsCheck"); |
| 689 |
QTest::addColumn<IntList>("defaultSizes"); |
| 690 |
QTest::addColumn<int>("initialDefaultSize"); |
| 691 |
QTest::addColumn<int>("lastVisibleSectionSize"); |
| 692 |
QTest::addColumn<int>("persistentSectionSize"); |
| 693 |
|
| 694 |
QTest::newRow("data set one") |
| 695 |
<< (IntList{ -1, 0, 4, 9999 }) |
| 696 |
<< (IntList{ 10, 30, 30 }) |
| 697 |
<< 30 |
| 698 |
<< 300 |
| 699 |
<< 20; |
| 700 |
} |
| 701 |
|
| 702 |
void tst_QHeaderView::sectionSize() |
| 703 |
{ |
| 704 |
#if defined Q_OS_QNX |
| 705 |
QSKIP("The section size is dpi dependent on QNX"); |
| 706 |
#endif |
| 707 |
QFETCH(const IntList, boundsCheck); |
| 708 |
QFETCH(const IntList, defaultSizes); |
| 709 |
QFETCH(int, initialDefaultSize); |
| 710 |
QFETCH(int, lastVisibleSectionSize); |
| 711 |
QFETCH(int, persistentSectionSize); |
| 712 |
|
| 713 |
// bounds check |
| 714 |
for (int val : boundsCheck) |
| 715 |
view->sectionSize(val); |
| 716 |
|
| 717 |
// default size |
| 718 |
QCOMPARE(view->defaultSectionSize(), initialDefaultSize); |
| 719 |
for (int def : defaultSizes) { |
| 720 |
view->setDefaultSectionSize(def); |
| 721 |
QCOMPARE(view->defaultSectionSize(), def); |
| 722 |
} |
| 723 |
|
| 724 |
view->setDefaultSectionSize(initialDefaultSize); |
| 725 |
for (int s = 0; s < view->count(); ++s) |
| 726 |
QCOMPARE(view->sectionSize(s), initialDefaultSize); |
| 727 |
view->doItemsLayout(); |
| 728 |
|
| 729 |
// stretch last section |
| 730 |
view->setStretchLastSection(true); |
| 731 |
int lastSection = view->count() - 1; |
| 732 |
|
| 733 |
//test that when hiding the last column, |
| 734 |
//resizing the new last visible columns still works |
| 735 |
view->hideSection(lastSection); |
| 736 |
view->resizeSection(lastSection - 1, lastVisibleSectionSize); |
| 737 |
QCOMPARE(view->sectionSize(lastSection - 1), lastVisibleSectionSize); |
| 738 |
view->showSection(lastSection); |
| 739 |
|
| 740 |
// turn off stretching |
| 741 |
view->setStretchLastSection(false); |
| 742 |
QCOMPARE(view->sectionSize(lastSection), initialDefaultSize); |
| 743 |
|
| 744 |
// test persistence |
| 745 |
int sectionCount = view->count(); |
| 746 |
for (int i = 0; i < sectionCount; ++i) |
| 747 |
view->resizeSection(i, persistentSectionSize); |
| 748 |
QtTestModel model(sectionCount * 2, sectionCount * 2); |
| 749 |
view->setModel(&model); |
| 750 |
for (int j = 0; j < sectionCount; ++j) |
| 751 |
QCOMPARE(view->sectionSize(j), persistentSectionSize); |
| 752 |
for (int k = sectionCount; k < view->count(); ++k) |
| 753 |
QCOMPARE(view->sectionSize(k), initialDefaultSize); |
| 754 |
} |
| 755 |
|
| 756 |
void tst_QHeaderView::visualIndex() |
| 757 |
{ |
| 758 |
// Test bad arguments |
| 759 |
QCOMPARE(view->visualIndex(999999), -1); |
| 760 |
QCOMPARE(view->visualIndex(-1), -1); |
| 761 |
QCOMPARE(view->visualIndex(1), 1); |
| 762 |
view->setSectionHidden(1, true); |
| 763 |
QCOMPARE(view->visualIndex(1), 1); |
| 764 |
QCOMPARE(view->visualIndex(2), 2); |
| 765 |
|
| 766 |
view->setSectionHidden(1, false); |
| 767 |
QCOMPARE(view->visualIndex(1), 1); |
| 768 |
QCOMPARE(view->visualIndex(2), 2); |
| 769 |
} |
| 770 |
|
| 771 |
void tst_QHeaderView::visualIndexAt_data() |
| 772 |
{ |
| 773 |
QTest::addColumn<IntList>("hidden"); |
| 774 |
QTest::addColumn<IntList>("from"); |
| 775 |
QTest::addColumn<IntList>("to"); |
| 776 |
QTest::addColumn<IntList>("coordinate"); |
| 777 |
QTest::addColumn<IntList>("visual"); |
| 778 |
|
| 779 |
const IntList coordinateList{ -1, 0, 31, 91, 99999 }; |
| 780 |
|
| 781 |
QTest::newRow("no hidden, no moved sections") |
| 782 |
<< IntList() |
| 783 |
<< IntList() |
| 784 |
<< IntList() |
| 785 |
<< coordinateList |
| 786 |
<< (IntList{ -1, 0, 1, 3, -1 }); |
| 787 |
|
| 788 |
QTest::newRow("no hidden, moved sections") |
| 789 |
<< IntList() |
| 790 |
<< (IntList{ 0 }) |
| 791 |
<< (IntList{ 1 }) |
| 792 |
<< coordinateList |
| 793 |
<< (IntList{ -1, 0, 1, 3, -1 }); |
| 794 |
|
| 795 |
QTest::newRow("hidden, no moved sections") |
| 796 |
<< (IntList{ 0 }) |
| 797 |
<< IntList() |
| 798 |
<< IntList() |
| 799 |
<< coordinateList |
| 800 |
<< (IntList{ -1, 1, 2, 3, -1 }); |
| 801 |
} |
| 802 |
|
| 803 |
void tst_QHeaderView::visualIndexAt() |
| 804 |
{ |
| 805 |
#if defined Q_OS_QNX |
| 806 |
QSKIP("The section size is dpi dependent on QNX"); |
| 807 |
#endif |
| 808 |
QFETCH(const IntList, hidden); |
| 809 |
QFETCH(const IntList, from); |
| 810 |
QFETCH(const IntList, to); |
| 811 |
QFETCH(const IntList, coordinate); |
| 812 |
QFETCH(const IntList, visual); |
| 813 |
|
| 814 |
view->setStretchLastSection(true); |
| 815 |
topLevel->show(); |
| 816 |
QVERIFY(QTest::qWaitForWindowExposed(topLevel)); |
| 817 |
|
| 818 |
for (int i : hidden) |
| 819 |
view->setSectionHidden(i, true); |
| 820 |
|
| 821 |
for (int j = 0; j < from.size(); ++j) |
| 822 |
view->moveSection(from.at(j), to.at(j)); |
| 823 |
|
| 824 |
for (int k = 0; k < coordinate.size(); ++k) |
| 825 |
QTRY_COMPARE(view->visualIndexAt(coordinate.at(k)), visual.at(k)); |
| 826 |
} |
| 827 |
|
| 828 |
void tst_QHeaderView::length() |
| 829 |
{ |
| 830 |
view->setStretchLastSection(true); |
| 831 |
topLevel->show(); |
| 832 |
QVERIFY(QTest::qWaitForWindowExposed(topLevel)); |
| 833 |
|
| 834 |
//minimumSectionSize should be the size of the last section of the widget is not tall enough |
| 835 |
int length = view->minimumSectionSize(); |
| 836 |
for (int i = 0; i < view->count() - 1; i++) |
| 837 |
length += view->sectionSize(i); |
| 838 |
|
| 839 |
length = qMax(length, view->viewport()->height()); |
| 840 |
QCOMPARE(length, view->length()); |
| 841 |
|
| 842 |
view->setStretchLastSection(false); |
| 843 |
topLevel->show(); |
| 844 |
QVERIFY(QTest::qWaitForWindowExposed(topLevel)); |
| 845 |
|
| 846 |
QCOMPARE_NE(length, view->length()); |
| 847 |
|
| 848 |
// layoutChanged might mean rows have been removed |
| 849 |
QtTestModel model(10, 10); |
| 850 |
view->setModel(&model); |
| 851 |
int oldLength = view->length(); |
| 852 |
model.cleanup(); |
| 853 |
QCOMPARE(model.rows, view->count()); |
| 854 |
QCOMPARE_NE(oldLength, view->length()); |
| 855 |
} |
| 856 |
|
| 857 |
void tst_QHeaderView::offset() |
| 858 |
{ |
| 859 |
QCOMPARE(view->offset(), 0); |
| 860 |
view->setOffset(10); |
| 861 |
QCOMPARE(view->offset(), 10); |
| 862 |
view->setOffset(0); |
| 863 |
QCOMPARE(view->offset(), 0); |
| 864 |
|
| 865 |
// Test odd arguments |
| 866 |
view->setOffset(-1); |
| 867 |
} |
| 868 |
|
| 869 |
void tst_QHeaderView::sectionSizeHint() |
| 870 |
{ |
| 871 |
QCOMPARE(view->sectionSizeHint(-1), -1); |
| 872 |
QCOMPARE(view->sectionSizeHint(99999), -1); |
| 873 |
QCOMPARE_GE(view->sectionSizeHint(0), 0); |
| 874 |
} |
| 875 |
|
| 876 |
void tst_QHeaderView::logicalIndex() |
| 877 |
{ |
| 878 |
// Test bad arguments |
| 879 |
QCOMPARE(view->logicalIndex(-1), -1); |
| 880 |
QCOMPARE(view->logicalIndex(99999), -1); |
| 881 |
} |
| 882 |
|
| 883 |
void tst_QHeaderView::logicalIndexAt() |
| 884 |
{ |
| 885 |
// Test bad arguments |
| 886 |
view->logicalIndexAt(-1); |
| 887 |
view->logicalIndexAt(99999); |
| 888 |
QCOMPARE(view->logicalIndexAt(0), 0); |
| 889 |
QCOMPARE(view->logicalIndexAt(1), 0); |
| 890 |
|
| 891 |
topLevel->show(); |
| 892 |
QVERIFY(QTest::qWaitForWindowExposed(topLevel)); |
| 893 |
view->setStretchLastSection(true); |
| 894 |
// First item |
| 895 |
QCOMPARE(view->logicalIndexAt(0), 0); |
| 896 |
QCOMPARE(view->logicalIndexAt(view->sectionSize(0)-1), 0); |
| 897 |
QCOMPARE(view->logicalIndexAt(view->sectionSize(0)+1), 1); |
| 898 |
// Last item |
| 899 |
int last = view->length() - 1;//view->viewport()->height() - 10; |
| 900 |
QCOMPARE(view->logicalIndexAt(last), 3); |
| 901 |
// Not in widget |
| 902 |
int outofbounds = view->length() + 1;//view->viewport()->height() + 1; |
| 903 |
QCOMPARE(view->logicalIndexAt(outofbounds), -1); |
| 904 |
|
| 905 |
view->moveSection(0,1); |
| 906 |
// First item |
| 907 |
QCOMPARE(view->logicalIndexAt(0), 1); |
| 908 |
QCOMPARE(view->logicalIndexAt(view->sectionSize(0)-1), 1); |
| 909 |
QCOMPARE(view->logicalIndexAt(view->sectionSize(0)+1), 0); |
| 910 |
// Last item |
| 911 |
QCOMPARE(view->logicalIndexAt(last), 3); |
| 912 |
view->moveSection(1,0); |
| 913 |
|
| 914 |
} |
| 915 |
|
| 916 |
void tst_QHeaderView::swapSections() |
| 917 |
{ |
| 918 |
view->swapSections(-1, 1); |
| 919 |
view->swapSections(99999, 1); |
| 920 |
view->swapSections(1, -1); |
| 921 |
view->swapSections(1, 99999); |
| 922 |
|
| 923 |
IntList logical{ 0, 1, 2, 3 }; |
| 924 |
|
| 925 |
QSignalSpy spy1(view, &QHeaderView::sectionMoved); |
| 926 |
|
| 927 |
QCOMPARE(view->sectionsMoved(), false); |
| 928 |
view->swapSections(1, 1); |
| 929 |
QCOMPARE(view->sectionsMoved(), false); |
| 930 |
view->swapSections(1, 2); |
| 931 |
QCOMPARE(view->sectionsMoved(), true); |
| 932 |
view->swapSections(2, 1); |
| 933 |
QCOMPARE(view->sectionsMoved(), true); |
| 934 |
for (int i = 0; i < view->count(); ++i) |
| 935 |
QCOMPARE(view->logicalIndex(i), logical.at(i)); |
| 936 |
QCOMPARE(spy1.size(), 4); |
| 937 |
|
| 938 |
logical = { 3, 1, 2, 0 }; |
| 939 |
view->swapSections(3, 0); |
| 940 |
QCOMPARE(view->sectionsMoved(), true); |
| 941 |
for (int j = 0; j < view->count(); ++j) |
| 942 |
QCOMPARE(view->logicalIndex(j), logical.at(j)); |
| 943 |
QCOMPARE(spy1.size(), 6); |
| 944 |
} |
| 945 |
|
| 946 |
void tst_QHeaderView::moveSection_data() |
| 947 |
{ |
| 948 |
QTest::addColumn<IntList>("hidden"); |
| 949 |
QTest::addColumn<IntList>("from"); |
| 950 |
QTest::addColumn<IntList>("to"); |
| 951 |
QTest::addColumn<BoolList>("moved"); |
| 952 |
QTest::addColumn<IntList>("logical"); |
| 953 |
QTest::addColumn<int>("count"); |
| 954 |
|
| 955 |
QTest::newRow("bad args, no hidden") |
| 956 |
<< IntList() |
| 957 |
<< (IntList{ -1, 1, 99999, 1 }) |
| 958 |
<< (IntList{ 1, -1, 1, 99999 }) |
| 959 |
<< (BoolList{ false, false, false, false }) |
| 960 |
<< (IntList{ 0, 1, 2, 3 }) |
| 961 |
<< 0; |
| 962 |
|
| 963 |
QTest::newRow("good args, no hidden") |
| 964 |
<< IntList() |
| 965 |
<< (IntList{ 1, 1, 2, 1 }) |
| 966 |
<< (IntList{ 1, 2, 1, 2 }) |
| 967 |
<< (BoolList{ false, true, true, true }) |
| 968 |
<< (IntList{ 0, 2, 1, 3 }) |
| 969 |
<< 3; |
| 970 |
|
| 971 |
QTest::newRow("hidden sections") |
| 972 |
<< (IntList{ 0, 3 }) |
| 973 |
<< (IntList{ 1, 1, 2, 1 }) |
| 974 |
<< (IntList{ 1, 2, 1, 2 }) |
| 975 |
<< (BoolList{ false, true, true, true }) |
| 976 |
<< (IntList{ 0, 2, 1, 3 }) |
| 977 |
<< 3; |
| 978 |
} |
| 979 |
|
| 980 |
void tst_QHeaderView::moveSection() |
| 981 |
{ |
| 982 |
QFETCH(const IntList, hidden); |
| 983 |
QFETCH(const IntList, from); |
| 984 |
QFETCH(const IntList, to); |
| 985 |
QFETCH(const BoolList, moved); |
| 986 |
QFETCH(const IntList, logical); |
| 987 |
QFETCH(int, count); |
| 988 |
|
| 989 |
QCOMPARE(from.size(), to.size()); |
| 990 |
QCOMPARE(from.size(), moved.size()); |
| 991 |
QCOMPARE(view->count(), logical.size()); |
| 992 |
|
| 993 |
QSignalSpy spy1(view, &QHeaderView::sectionMoved); |
| 994 |
QCOMPARE(view->sectionsMoved(), false); |
| 995 |
|
| 996 |
for (int h : hidden) |
| 997 |
view->setSectionHidden(h, true); |
| 998 |
|
| 999 |
for (int i = 0; i < from.size(); ++i) { |
| 1000 |
view->moveSection(from.at(i), to.at(i)); |
| 1001 |
QCOMPARE(view->sectionsMoved(), moved.at(i)); |
| 1002 |
} |
| 1003 |
|
| 1004 |
for (int j = 0; j < view->count(); ++j) |
| 1005 |
QCOMPARE(view->logicalIndex(j), logical.at(j)); |
| 1006 |
|
| 1007 |
QCOMPARE(spy1.size(), count); |
| 1008 |
} |
| 1009 |
|
| 1010 |
void tst_QHeaderView::resizeAndMoveSection_data() |
| 1011 |
{ |
| 1012 |
QTest::addColumn<IntList>("logicalIndexes"); |
| 1013 |
QTest::addColumn<IntList>("sizes"); |
| 1014 |
QTest::addColumn<int>("logicalFrom"); |
| 1015 |
QTest::addColumn<int>("logicalTo"); |
| 1016 |
|
| 1017 |
QTest::newRow("resizeAndMove-1") |
| 1018 |
<< (IntList{ 0, 1 }) |
| 1019 |
<< (IntList{ 20, 40 }) |
| 1020 |
<< 0 << 1; |
| 1021 |
|
| 1022 |
QTest::newRow("resizeAndMove-2") |
| 1023 |
<< (IntList{ 0, 1, 2, 3 }) |
| 1024 |
<< (IntList{ 20, 60, 10, 80 }) |
| 1025 |
<< 0 << 2; |
| 1026 |
|
| 1027 |
QTest::newRow("resizeAndMove-3") |
| 1028 |
<< (IntList{ 0, 1, 2, 3 }) |
| 1029 |
<< (IntList{ 100, 60, 40, 10 }) |
| 1030 |
<< 0 << 3; |
| 1031 |
|
| 1032 |
QTest::newRow("resizeAndMove-4") |
| 1033 |
<< (IntList{ 0, 1, 2, 3 }) |
| 1034 |
<< (IntList{ 10, 40, 80, 30 }) |
| 1035 |
<< 1 << 2; |
| 1036 |
|
| 1037 |
QTest::newRow("resizeAndMove-5") |
| 1038 |
<< (IntList{ 2, 3 }) |
| 1039 |
<< (IntList{ 100, 200}) |
| 1040 |
<< 3 << 2; |
| 1041 |
} |
| 1042 |
|
| 1043 |
void tst_QHeaderView::resizeAndMoveSection() |
| 1044 |
{ |
| 1045 |
QFETCH(const IntList, logicalIndexes); |
| 1046 |
QFETCH(const IntList, sizes); |
| 1047 |
QFETCH(int, logicalFrom); |
| 1048 |
QFETCH(int, logicalTo); |
| 1049 |
|
| 1050 |
// Save old visual indexes and sizes |
| 1051 |
IntList oldVisualIndexes; |
| 1052 |
IntList oldSizes; |
| 1053 |
for (int logical : logicalIndexes) { |
| 1054 |
oldVisualIndexes.append(view->visualIndex(logical)); |
| 1055 |
oldSizes.append(view->sectionSize(logical)); |
| 1056 |
} |
| 1057 |
|
| 1058 |
// Resize sections |
| 1059 |
for (int i = 0; i < logicalIndexes.size(); ++i) { |
| 1060 |
int logical = logicalIndexes.at(i); |
| 1061 |
view->resizeSection(logical, sizes.at(i)); |
| 1062 |
} |
| 1063 |
|
| 1064 |
// Move sections |
| 1065 |
int visualFrom = view->visualIndex(logicalFrom); |
| 1066 |
int visualTo = view->visualIndex(logicalTo); |
| 1067 |
view->moveSection(visualFrom, visualTo); |
| 1068 |
QCOMPARE(view->visualIndex(logicalFrom), visualTo); |
| 1069 |
|
| 1070 |
// Check that sizes are still correct |
| 1071 |
for (int i = 0; i < logicalIndexes.size(); ++i) { |
| 1072 |
int logical = logicalIndexes.at(i); |
| 1073 |
QCOMPARE(view->sectionSize(logical), sizes.at(i)); |
| 1074 |
} |
| 1075 |
|
| 1076 |
// Move sections back |
| 1077 |
view->moveSection(visualTo, visualFrom); |
| 1078 |
|
| 1079 |
// Check that sizes are still correct |
| 1080 |
for (int i = 0; i < logicalIndexes.size(); ++i) { |
| 1081 |
int logical = logicalIndexes.at(i); |
| 1082 |
QCOMPARE(view->sectionSize(logical), sizes.at(i)); |
| 1083 |
} |
| 1084 |
|
| 1085 |
// Put everything back as it was |
| 1086 |
for (int i = 0; i < logicalIndexes.size(); ++i) { |
| 1087 |
int logical = logicalIndexes.at(i); |
| 1088 |
view->resizeSection(logical, oldSizes.at(i)); |
| 1089 |
QCOMPARE(view->visualIndex(logical), oldVisualIndexes.at(i)); |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
void tst_QHeaderView::resizeHiddenSection_data() |
| 1094 |
{ |
| 1095 |
QTest::addColumn<int>("section"); |
| 1096 |
QTest::addColumn<int>("initialSize"); |
| 1097 |
QTest::addColumn<int>("finalSize"); |
| 1098 |
|
| 1099 |
QTest::newRow("section 0 resize 50 to 20") |
| 1100 |
<< 0 << 50 << 20; |
| 1101 |
|
| 1102 |
QTest::newRow("section 1 resize 50 to 20") |
| 1103 |
<< 1 << 50 << 20; |
| 1104 |
|
| 1105 |
QTest::newRow("section 2 resize 50 to 20") |
| 1106 |
<< 2 << 50 << 20; |
| 1107 |
|
| 1108 |
QTest::newRow("section 3 resize 50 to 20") |
| 1109 |
<< 3 << 50 << 20; |
| 1110 |
} |
| 1111 |
|
| 1112 |
void tst_QHeaderView::resizeHiddenSection() |
| 1113 |
{ |
| 1114 |
QFETCH(int, section); |
| 1115 |
QFETCH(int, initialSize); |
| 1116 |
QFETCH(int, finalSize); |
| 1117 |
|
| 1118 |
view->resizeSection(section, initialSize); |
| 1119 |
view->setSectionHidden(section, true); |
| 1120 |
QCOMPARE(view->sectionSize(section), 0); |
| 1121 |
|
| 1122 |
view->resizeSection(section, finalSize); |
| 1123 |
QCOMPARE(view->sectionSize(section), 0); |
| 1124 |
|
| 1125 |
view->setSectionHidden(section, false); |
| 1126 |
QCOMPARE(view->sectionSize(section), finalSize); |
| 1127 |
} |
| 1128 |
|
| 1129 |
void tst_QHeaderView::resizeAndInsertSection_data() |
| 1130 |
{ |
| 1131 |
QTest::addColumn<int>("section"); |
| 1132 |
QTest::addColumn<int>("size"); |
| 1133 |
QTest::addColumn<int>("insert"); |
| 1134 |
QTest::addColumn<int>("compare"); |
| 1135 |
QTest::addColumn<int>("expected"); |
| 1136 |
|
| 1137 |
QTest::newRow("section 0 size 50 insert 0") |
| 1138 |
<< 0 << 50 << 0 << 1 << 50; |
| 1139 |
|
| 1140 |
QTest::newRow("section 1 size 50 insert 1") |
| 1141 |
<< 0 << 50 << 1 << 0 << 50; |
| 1142 |
|
| 1143 |
QTest::newRow("section 1 size 50 insert 0") |
| 1144 |
<< 1 << 50 << 0 << 2 << 50; |
| 1145 |
|
| 1146 |
} |
| 1147 |
|
| 1148 |
void tst_QHeaderView::resizeAndInsertSection() |
| 1149 |
{ |
| 1150 |
QFETCH(int, section); |
| 1151 |
QFETCH(int, size); |
| 1152 |
QFETCH(int, insert); |
| 1153 |
QFETCH(int, compare); |
| 1154 |
QFETCH(int, expected); |
| 1155 |
|
| 1156 |
view->setStretchLastSection(false); |
| 1157 |
|
| 1158 |
view->resizeSection(section, size); |
| 1159 |
QCOMPARE(view->sectionSize(section), size); |
| 1160 |
|
| 1161 |
model->insertRow(insert); |
| 1162 |
|
| 1163 |
QCOMPARE(view->sectionSize(compare), expected); |
| 1164 |
} |
| 1165 |
|
| 1166 |
void tst_QHeaderView::resizeWithResizeModes_data() |
| 1167 |
{ |
| 1168 |
QTest::addColumn<int>("size"); |
| 1169 |
QTest::addColumn<IntList>("sections"); |
| 1170 |
QTest::addColumn<ResizeVec>("modes"); |
| 1171 |
QTest::addColumn<IntList>("expected"); |
| 1172 |
|
| 1173 |
QTest::newRow("stretch first section") |
| 1174 |
<< 600 |
| 1175 |
<< (IntList{ 100, 100, 100, 100 }) |
| 1176 |
<< (ResizeVec |
| 1177 |
{ QHeaderView::Stretch, |
| 1178 |
QHeaderView::Interactive, |
| 1179 |
QHeaderView::Interactive, |
| 1180 |
QHeaderView::Interactive }) |
| 1181 |
<< (IntList{ 300, 100, 100, 100 }); |
| 1182 |
} |
| 1183 |
|
| 1184 |
void tst_QHeaderView::resizeWithResizeModes() |
| 1185 |
{ |
| 1186 |
QFETCH(int, size); |
| 1187 |
QFETCH(const IntList, sections); |
| 1188 |
QFETCH(const ResizeVec, modes); |
| 1189 |
QFETCH(const IntList, expected); |
| 1190 |
|
| 1191 |
view->setStretchLastSection(false); |
| 1192 |
for (int i = 0; i < sections.size(); ++i) { |
| 1193 |
view->resizeSection(i, sections.at(i)); |
| 1194 |
view->setSectionResizeMode(i, modes.at(i)); |
| 1195 |
} |
| 1196 |
topLevel->show(); |
| 1197 |
QVERIFY(QTest::qWaitForWindowExposed(topLevel)); |
| 1198 |
view->resize(size, size); |
| 1199 |
for (int j = 0; j < expected.size(); ++j) |
| 1200 |
QCOMPARE(view->sectionSize(j), expected.at(j)); |
| 1201 |
} |
| 1202 |
|
| 1203 |
void tst_QHeaderView::moveAndInsertSection_data() |
| 1204 |
{ |
| 1205 |
QTest::addColumn<int>("from"); |
| 1206 |
QTest::addColumn<int>("to"); |
| 1207 |
QTest::addColumn<int>("insert"); |
| 1208 |
QTest::addColumn<IntList>("mapping"); |
| 1209 |
|
| 1210 |
QTest::newRow("move from 1 to 3, insert 0") |
| 1211 |
<< 1 << 3 << 0 <<(IntList{ 0, 1, 3, 4, 2 }); |
| 1212 |
|
| 1213 |
} |
| 1214 |
|
| 1215 |
void tst_QHeaderView::moveAndInsertSection() |
| 1216 |
{ |
| 1217 |
QFETCH(int, from); |
| 1218 |
QFETCH(int, to); |
| 1219 |
QFETCH(int, insert); |
| 1220 |
QFETCH(IntList, mapping); |
| 1221 |
|
| 1222 |
view->setStretchLastSection(false); |
| 1223 |
view->moveSection(from, to); |
| 1224 |
model->insertRow(insert); |
| 1225 |
|
| 1226 |
for (int i = 0; i < mapping.size(); ++i) |
| 1227 |
QCOMPARE(view->logicalIndex(i), mapping.at(i)); |
| 1228 |
} |
| 1229 |
|
| 1230 |
void tst_QHeaderView::resizeMode() |
| 1231 |
{ |
| 1232 |
// resizeMode must not be called with an invalid index |
| 1233 |
int last = view->count() - 1; |
| 1234 |
view->setSectionResizeMode(QHeaderView::Interactive); |
| 1235 |
QCOMPARE(view->sectionResizeMode(last), QHeaderView::Interactive); |
| 1236 |
QCOMPARE(view->sectionResizeMode(1), QHeaderView::Interactive); |
| 1237 |
view->setSectionResizeMode(QHeaderView::Stretch); |
| 1238 |
QCOMPARE(view->sectionResizeMode(last), QHeaderView::Stretch); |
| 1239 |
QCOMPARE(view->sectionResizeMode(1), QHeaderView::Stretch); |
| 1240 |
view->setSectionResizeMode(QHeaderView::Custom); |
| 1241 |
QCOMPARE(view->sectionResizeMode(last), QHeaderView::Custom); |
| 1242 |
QCOMPARE(view->sectionResizeMode(1), QHeaderView::Custom); |
| 1243 |
|
| 1244 |
// test when sections have been moved |
| 1245 |
view->setStretchLastSection(false); |
| 1246 |
for (int i = 0; i < (view->count() - 1); ++i) |
| 1247 |
view->setSectionResizeMode(i, QHeaderView::Interactive); |
| 1248 |
int logicalIndex = view->count() / 2; |
| 1249 |
view->setSectionResizeMode(logicalIndex, QHeaderView::Stretch); |
| 1250 |
view->moveSection(view->visualIndex(logicalIndex), 0); |
| 1251 |
for (int i = 0; i < (view->count() - 1); ++i) { |
| 1252 |
if (i == logicalIndex) |
| 1253 |
QCOMPARE(view->sectionResizeMode(i), QHeaderView::Stretch); |
| 1254 |
else |
| 1255 |
QCOMPARE(view->sectionResizeMode(i), QHeaderView::Interactive); |
| 1256 |
} |
| 1257 |
} |
| 1258 |
|
| 1259 |
void tst_QHeaderView::resizeSection_data() |
| 1260 |
{ |
| 1261 |
QTest::addColumn<int>("initial"); |
| 1262 |
QTest::addColumn<IntList>("logical"); |
| 1263 |
QTest::addColumn<IntList>("size"); |
| 1264 |
QTest::addColumn<ResizeVec>("mode"); |
| 1265 |
QTest::addColumn<int>("resized"); |
| 1266 |
QTest::addColumn<IntList>("expected"); |
| 1267 |
|
| 1268 |
QTest::newRow("bad args") |
| 1269 |
<< 100 |
| 1270 |
<< (IntList{ -1, -1, 99999, 99999, 4 }) |
| 1271 |
<< (IntList{ -1, 0, 99999, -1, -1 }) |
| 1272 |
<< (ResizeVec{ |
| 1273 |
QHeaderView::Interactive, |
| 1274 |
QHeaderView::Interactive, |
| 1275 |
QHeaderView::Interactive, |
| 1276 |
QHeaderView::Interactive }) |
| 1277 |
<< 0 |
| 1278 |
<< (IntList{ 0, 0, 0, 0, 0 }); |
| 1279 |
} |
| 1280 |
|
| 1281 |
void tst_QHeaderView::resizeSection() |
| 1282 |
{ |
| 1283 |
QFETCH(int, initial); |
| 1284 |
QFETCH(const IntList, logical); |
| 1285 |
QFETCH(const IntList, size); |
| 1286 |
QFETCH(const ResizeVec, mode); |
| 1287 |
QFETCH(int, resized); |
| 1288 |
QFETCH(const IntList, expected); |
| 1289 |
|
| 1290 |
view->resize(400, 400); |
| 1291 |
|
| 1292 |
topLevel->show(); |
| 1293 |
QVERIFY(QTest::qWaitForWindowExposed(topLevel)); |
| 1294 |
view->setSectionsMovable(true); |
| 1295 |
view->setStretchLastSection(false); |
| 1296 |
|
| 1297 |
for (int i = 0; i < logical.size(); ++i) |
| 1298 |
if (logical.at(i) > -1 && logical.at(i) < view->count()) // for now |
| 1299 |
view->setSectionResizeMode(logical.at(i), mode.at(i)); |
| 1300 |
|
| 1301 |
for (int j = 0; j < logical.size(); ++j) |
| 1302 |
view->resizeSection(logical.at(j), initial); |
| 1303 |
|
| 1304 |
QSignalSpy spy(view, &QHeaderView::sectionResized); |
| 1305 |
|
| 1306 |
for (int k = 0; k < logical.size(); ++k) |
| 1307 |
view->resizeSection(logical.at(k), size.at(k)); |
| 1308 |
|
| 1309 |
QCOMPARE(spy.size(), resized); |
| 1310 |
|
| 1311 |
for (int l = 0; l < logical.size(); ++l) |
| 1312 |
QCOMPARE(view->sectionSize(logical.at(l)), expected.at(l)); |
| 1313 |
} |
| 1314 |
|
| 1315 |
void tst_QHeaderView::highlightSections() |
| 1316 |
{ |
| 1317 |
view->setHighlightSections(true); |
| 1318 |
QCOMPARE(view->highlightSections(), true); |
| 1319 |
view->setHighlightSections(false); |
| 1320 |
QCOMPARE(view->highlightSections(), false); |
| 1321 |
} |
| 1322 |
|
| 1323 |
void tst_QHeaderView::showSortIndicator() |
| 1324 |
{ |
| 1325 |
view->setSortIndicatorShown(true); |
| 1326 |
QCOMPARE(view->isSortIndicatorShown(), true); |
| 1327 |
QCOMPARE(view->sortIndicatorOrder(), Qt::DescendingOrder); |
| 1328 |
view->setSortIndicator(1, Qt::AscendingOrder); |
| 1329 |
QCOMPARE(view->sortIndicatorOrder(), Qt::AscendingOrder); |
| 1330 |
view->setSortIndicator(1, Qt::DescendingOrder); |
| 1331 |
QCOMPARE(view->sortIndicatorOrder(), Qt::DescendingOrder); |
| 1332 |
view->setSortIndicatorShown(false); |
| 1333 |
QCOMPARE(view->isSortIndicatorShown(), false); |
| 1334 |
|
| 1335 |
view->setSortIndicator(999999, Qt::DescendingOrder); |
| 1336 |
// Don't segfault baby :) |
| 1337 |
view->setSortIndicatorShown(true); |
| 1338 |
|
| 1339 |
view->setSortIndicator(0, Qt::DescendingOrder); |
| 1340 |
// Don't assert baby :) |
| 1341 |
} |
| 1342 |
|
| 1343 |
void tst_QHeaderView::clearSectionSorting() |
| 1344 |
{ |
| 1345 |
QStandardItemModel m(4, 4); |
| 1346 |
QHeaderView h(Qt::Horizontal); |
| 1347 |
|
| 1348 |
QCOMPARE(h.sortIndicatorSection(), 0); |
| 1349 |
QCOMPARE(h.sortIndicatorOrder(), Qt::DescendingOrder); |
| 1350 |
|
| 1351 |
h.setModel(&m); |
| 1352 |
h.setSectionsClickable(true); |
| 1353 |
h.setSortIndicatorShown(true); |
| 1354 |
h.setSortIndicator(-1, Qt::DescendingOrder); |
| 1355 |
h.show(); |
| 1356 |
|
| 1357 |
QVERIFY(QTest::qWaitForWindowExposed(&h)); |
| 1358 |
|
| 1359 |
QCOMPARE(h.sortIndicatorSection(), -1); |
| 1360 |
QCOMPARE(h.sortIndicatorOrder(), Qt::DescendingOrder); |
| 1361 |
|
| 1362 |
QSignalSpy sectionClickedSpy(&h, &QHeaderView::sectionClicked); |
| 1363 |
QVERIFY(sectionClickedSpy.isValid()); |
| 1364 |
QCOMPARE(sectionClickedSpy.size(), 0); |
| 1365 |
|
| 1366 |
QSignalSpy sortIndicatorChangedSpy(&h, &QHeaderView::sortIndicatorChanged); |
| 1367 |
QVERIFY(sortIndicatorChangedSpy.isValid()); |
| 1368 |
QCOMPARE(sortIndicatorChangedSpy.size(), 0); |
| 1369 |
|
| 1370 |
enum { Count = 30 }; |
| 1371 |
|
| 1372 |
// normal behavior: clicking multiple times will just toggle the sort indicator |
| 1373 |
for (int i = 0; i < Count; ++i) { |
| 1374 |
QTest::mouseClick(h.viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(5, 5)); |
| 1375 |
QCOMPARE(sectionClickedSpy.size(), i + 1); |
| 1376 |
QCOMPARE(sortIndicatorChangedSpy.size(), i + 1); |
| 1377 |
QCOMPARE(h.sortIndicatorSection(), 0); |
| 1378 |
const auto expectedOrder = (i % 2) == 0 ? Qt::AscendingOrder : Qt::DescendingOrder; |
| 1379 |
QCOMPARE(h.sortIndicatorOrder(), expectedOrder); |
| 1380 |
} |
| 1381 |
|
| 1382 |
h.setSortIndicator(-1, Qt::DescendingOrder); |
| 1383 |
h.setSortIndicatorClearable(true); |
| 1384 |
QCOMPARE(h.sortIndicatorSection(), -1); |
| 1385 |
QCOMPARE(h.sortIndicatorOrder(), Qt::DescendingOrder); |
| 1386 |
|
| 1387 |
sectionClickedSpy.clear(); |
| 1388 |
sortIndicatorChangedSpy.clear(); |
| 1389 |
|
| 1390 |
// clearing behavior: clicking multiple times will be tristate (asc, desc, nothing) |
| 1391 |
for (int i = 0; i < Count; ++i) { |
| 1392 |
QTest::mouseClick(h.viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(5, 5)); |
| 1393 |
QCOMPARE(sectionClickedSpy.size(), i + 1); |
| 1394 |
QCOMPARE(sortIndicatorChangedSpy.size(), i + 1); |
| 1395 |
switch (i % 3) { |
| 1396 |
case 0: |
| 1397 |
QCOMPARE(h.sortIndicatorSection(), 0); |
| 1398 |
QCOMPARE(h.sortIndicatorOrder(), Qt::AscendingOrder); |
| 1399 |
break; |
| 1400 |
case 1: |
| 1401 |
QCOMPARE(h.sortIndicatorSection(), 0); |
| 1402 |
QCOMPARE(h.sortIndicatorOrder(), Qt::DescendingOrder); |
| 1403 |
break; |
| 1404 |
case 2: |
| 1405 |
QCOMPARE(h.sortIndicatorSection(), -1); |
| 1406 |
QCOMPARE(h.sortIndicatorOrder(), Qt::AscendingOrder); |
| 1407 |
break; |
| 1408 |
} |
| 1409 |
} |
| 1410 |
} |
| 1411 |
|
| 1412 |
void tst_QHeaderView::sortIndicatorTracking() |
| 1413 |
{ |
| 1414 |
QtTestModel model(10, 10); |
| 1415 |
QHeaderView hv(Qt::Horizontal); |
| 1416 |
|
| 1417 |
hv.setModel(&model); |
| 1418 |
hv.show(); |
| 1419 |
hv.setSortIndicatorShown(true); |
| 1420 |
hv.setSortIndicator(1, Qt::DescendingOrder); |
| 1421 |
|
| 1422 |
model.removeOneColumn(8); |
| 1423 |
QCOMPARE(hv.sortIndicatorSection(), 1); |
| 1424 |
|
| 1425 |
model.removeOneColumn(2); |
| 1426 |
QCOMPARE(hv.sortIndicatorSection(), 1); |
| 1427 |
|
| 1428 |
model.insertOneColumn(2); |
| 1429 |
QCOMPARE(hv.sortIndicatorSection(), 1); |
| 1430 |
|
| 1431 |
model.insertOneColumn(1); |
| 1432 |
QCOMPARE(hv.sortIndicatorSection(), 2); |
| 1433 |
|
| 1434 |
model.removeOneColumn(0); |
| 1435 |
QCOMPARE(hv.sortIndicatorSection(), 1); |
| 1436 |
|
| 1437 |
model.removeOneColumn(1); |
| 1438 |
QCOMPARE(hv.sortIndicatorSection(), -1); |
| 1439 |
} |
| 1440 |
|
| 1441 |
void tst_QHeaderView::removeAndInsertRow() |
| 1442 |
{ |
| 1443 |
// Check if logicalIndex returns the correct value after we have removed a row |
| 1444 |
// we might as well te |
| 1445 |
for (int i = 0; i < model->rowCount(); ++i) |
| 1446 |
QCOMPARE(i, view->logicalIndex(i)); |
| 1447 |
|
| 1448 |
while (model->removeRow(0)) { |
| 1449 |
for (int i = 0; i < model->rowCount(); ++i) |
| 1450 |
QCOMPARE(i, view->logicalIndex(i)); |
| 1451 |
} |
| 1452 |
|
| 1453 |
for (int pass = 0; pass < 5; pass++) { |
| 1454 |
for (int i = 0; i < model->rowCount(); ++i) |
| 1455 |
QCOMPARE(i, view->logicalIndex(i)); |
| 1456 |
model->insertRow(0); |
| 1457 |
} |
| 1458 |
|
| 1459 |
while (model->removeRows(0, 2)) { |
| 1460 |
for (int i = 0; i < model->rowCount(); ++i) |
| 1461 |
QCOMPARE(i, view->logicalIndex(i)); |
| 1462 |
} |
| 1463 |
|
| 1464 |
for (int pass = 0; pass < 3; pass++) { |
| 1465 |
model->insertRows(0, 2); |
| 1466 |
for (int i = 0; i < model->rowCount(); ++i) { |
| 1467 |
QCOMPARE(i, view->logicalIndex(i)); |
| 1468 |
} |
| 1469 |
} |
| 1470 |
|
| 1471 |
for (int pass = 0; pass < 3; pass++) { |
| 1472 |
model->insertRows(3, 2); |
| 1473 |
for (int i = 0; i < model->rowCount(); ++i) |
| 1474 |
QCOMPARE(i, view->logicalIndex(i)); |
| 1475 |
} |
| 1476 |
|
| 1477 |
// Insert at end |
| 1478 |
for (int pass = 0; pass < 3; pass++) { |
| 1479 |
int rowCount = model->rowCount(); |
| 1480 |
model->insertRows(rowCount, 1); |
| 1481 |
for (int i = 0; i < rowCount; ++i) |
| 1482 |
QCOMPARE(i, view->logicalIndex(i)); |
| 1483 |
} |
| 1484 |
|
| 1485 |
} |
| 1486 |
void tst_QHeaderView::unhideSection() |
| 1487 |
{ |
| 1488 |
// You should not necessarily expect the same size back again, so the best test we can do is to test if it is larger than 0 after a unhide. |
| 1489 |
QCOMPARE(view->sectionsHidden(), false); |
| 1490 |
view->setSectionHidden(0, true); |
| 1491 |
QCOMPARE(view->sectionsHidden(), true); |
| 1492 |
QCOMPARE(view->sectionSize(0), 0); |
| 1493 |
view->setSectionResizeMode(QHeaderView::Interactive); |
| 1494 |
view->setSectionHidden(0, false); |
| 1495 |
QCOMPARE_GT(view->sectionSize(0), 0); |
| 1496 |
|
| 1497 |
view->setSectionHidden(0, true); |
| 1498 |
QCOMPARE(view->sectionSize(0), 0); |
| 1499 |
view->setSectionHidden(0, true); |
| 1500 |
QCOMPARE(view->sectionSize(0), 0); |
| 1501 |
view->setSectionResizeMode(QHeaderView::Stretch); |
| 1502 |
view->setSectionHidden(0, false); |
| 1503 |
QCOMPARE_GT(view->sectionSize(0), 0); |
| 1504 |
|
| 1505 |
} |
| 1506 |
|
| 1507 |
void tst_QHeaderView::testEvent() |
| 1508 |
{ |
| 1509 |
protected_QHeaderView x(Qt::Vertical); |
| 1510 |
x.testEvent(); |
| 1511 |
protected_QHeaderView y(Qt::Horizontal); |
| 1512 |
y.testEvent(); |
| 1513 |
} |
| 1514 |
|
| 1515 |
|
| 1516 |
void protected_QHeaderView::testEvent() |
| 1517 |
{ |
| 1518 |
// No crashy please |
| 1519 |
QHoverEvent enterEvent(QEvent::HoverEnter, QPoint(), QPoint(), QPoint()); |
| 1520 |
event(&enterEvent); |
| 1521 |
QHoverEvent eventLeave(QEvent::HoverLeave, QPoint(), QPoint(), QPoint()); |
| 1522 |
event(&eventLeave); |
| 1523 |
QHoverEvent eventMove(QEvent::HoverMove, QPoint(), QPoint(), QPoint()); |
| 1524 |
event(&eventMove); |
| 1525 |
} |
| 1526 |
|
| 1527 |
void tst_QHeaderView::headerDataChanged() |
| 1528 |
{ |
| 1529 |
// This shouldn't assert because view is Vertical |
| 1530 |
view->headerDataChanged(Qt::Horizontal, -1, -1); |
| 1531 |
#if 0 |
| 1532 |
// This will assert |
| 1533 |
view->headerDataChanged(Qt::Vertical, -1, -1); |
| 1534 |
#endif |
| 1535 |
|
| 1536 |
// No crashing please |
| 1537 |
view->headerDataChanged(Qt::Horizontal, 0, 1); |
| 1538 |
view->headerDataChanged(Qt::Vertical, 0, 1); |
| 1539 |
} |
| 1540 |
|
| 1541 |
void tst_QHeaderView::currentChanged() |
| 1542 |
{ |
| 1543 |
view->setCurrentIndex(QModelIndex()); |
| 1544 |
} |
| 1545 |
|
| 1546 |
void tst_QHeaderView::horizontalOffset() |
| 1547 |
{ |
| 1548 |
protected_QHeaderView x(Qt::Vertical); |
| 1549 |
x.testhorizontalOffset(); |
| 1550 |
protected_QHeaderView y(Qt::Horizontal); |
| 1551 |
y.testhorizontalOffset(); |
| 1552 |
} |
| 1553 |
|
| 1554 |
void tst_QHeaderView::verticalOffset() |
| 1555 |
{ |
| 1556 |
protected_QHeaderView x(Qt::Vertical); |
| 1557 |
x.testverticalOffset(); |
| 1558 |
protected_QHeaderView y(Qt::Horizontal); |
| 1559 |
y.testverticalOffset(); |
| 1560 |
} |
| 1561 |
|
| 1562 |
void protected_QHeaderView::testhorizontalOffset() |
| 1563 |
{ |
| 1564 |
if (orientation() == Qt::Horizontal) { |
| 1565 |
QCOMPARE(horizontalOffset(), 0); |
| 1566 |
setOffset(10); |
| 1567 |
QCOMPARE(horizontalOffset(), 10); |
| 1568 |
} |
| 1569 |
else |
| 1570 |
QCOMPARE(horizontalOffset(), 0); |
| 1571 |
} |
| 1572 |
|
| 1573 |
void protected_QHeaderView::testverticalOffset() |
| 1574 |
{ |
| 1575 |
if (orientation() == Qt::Vertical) { |
| 1576 |
QCOMPARE(verticalOffset(), 0); |
| 1577 |
setOffset(10); |
| 1578 |
QCOMPARE(verticalOffset(), 10); |
| 1579 |
} |
| 1580 |
else |
| 1581 |
QCOMPARE(verticalOffset(), 0); |
| 1582 |
} |
| 1583 |
|
| 1584 |
void tst_QHeaderView::stretchSectionCount() |
| 1585 |
{ |
| 1586 |
view->setStretchLastSection(false); |
| 1587 |
QCOMPARE(view->stretchSectionCount(), 0); |
| 1588 |
view->setStretchLastSection(true); |
| 1589 |
QCOMPARE(view->stretchSectionCount(), 0); |
| 1590 |
|
| 1591 |
view->setSectionResizeMode(0, QHeaderView::Stretch); |
| 1592 |
QCOMPARE(view->stretchSectionCount(), 1); |
| 1593 |
} |
| 1594 |
|
| 1595 |
void tst_QHeaderView::hiddenSectionCount() |
| 1596 |
{ |
| 1597 |
model->clear(); |
| 1598 |
model->insertRows(0, 10); |
| 1599 |
// Hide every other one |
| 1600 |
for (int i = 0; i < 10; i++) |
| 1601 |
view->setSectionHidden(i, (i & 1) == 0); |
| 1602 |
|
| 1603 |
QCOMPARE(view->hiddenSectionCount(), 5); |
| 1604 |
|
| 1605 |
view->setSectionResizeMode(QHeaderView::Stretch); |
| 1606 |
QCOMPARE(view->hiddenSectionCount(), 5); |
| 1607 |
|
| 1608 |
// Remove some rows and make sure they are now still counted |
| 1609 |
model->removeRow(9); |
| 1610 |
model->removeRow(8); |
| 1611 |
model->removeRow(7); |
| 1612 |
model->removeRow(6); |
| 1613 |
QCOMPARE(view->count(), 6); |
| 1614 |
QCOMPARE(view->hiddenSectionCount(), 3); |
| 1615 |
model->removeRows(0, 5); |
| 1616 |
QCOMPARE(view->count(), 1); |
| 1617 |
QCOMPARE(view->hiddenSectionCount(), 0); |
| 1618 |
QCOMPARE_GE(view->count(), view->hiddenSectionCount()); |
| 1619 |
} |
| 1620 |
|
| 1621 |
void tst_QHeaderView::focusPolicy() |
| 1622 |
{ |
| 1623 |
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) |
| 1624 |
QSKIP("Wayland: This fails. Figure out why."); |
| 1625 |
|
| 1626 |
QHeaderView view(Qt::Horizontal); |
| 1627 |
QCOMPARE(view.focusPolicy(), Qt::NoFocus); |
| 1628 |
|
| 1629 |
QTreeWidget widget; |
| 1630 |
QCOMPARE(widget.header()->focusPolicy(), Qt::NoFocus); |
| 1631 |
QVERIFY(!widget.focusProxy()); |
| 1632 |
QVERIFY(!widget.hasFocus()); |
| 1633 |
QVERIFY(!widget.header()->focusProxy()); |
| 1634 |
QVERIFY(!widget.header()->hasFocus()); |
| 1635 |
|
| 1636 |
widget.show(); |
| 1637 |
widget.setFocus(Qt::OtherFocusReason); |
| 1638 |
widget.activateWindow(); |
| 1639 |
QVERIFY(QTest::qWaitForWindowActive(&widget)); |
| 1640 |
QVERIFY(widget.hasFocus()); |
| 1641 |
QVERIFY(!widget.header()->hasFocus()); |
| 1642 |
|
| 1643 |
widget.setFocusPolicy(Qt::NoFocus); |
| 1644 |
widget.clearFocus(); |
| 1645 |
QTRY_VERIFY(!widget.hasFocus()); |
| 1646 |
QVERIFY(!widget.header()->hasFocus()); |
| 1647 |
|
| 1648 |
QTest::keyPress(&widget, Qt::Key_Tab); |
| 1649 |
|
| 1650 |
QCoreApplication::processEvents(); |
| 1651 |
QCoreApplication::processEvents(); |
| 1652 |
|
| 1653 |
QVERIFY(!widget.hasFocus()); |
| 1654 |
QVERIFY(!widget.header()->hasFocus()); |
| 1655 |
} |
| 1656 |
|
| 1657 |
class SimpleModel : public QAbstractItemModel |
| 1658 |
{ |
| 1659 |
Q_OBJECT |
| 1660 |
public: |
| 1661 |
using QAbstractItemModel::QAbstractItemModel; |
| 1662 |
QModelIndex parent(const QModelIndex &/*child*/) const override |
| 1663 |
{ |
| 1664 |
return QModelIndex(); |
| 1665 |
} |
| 1666 |
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override |
| 1667 |
{ |
| 1668 |
return hasIndex(row, column, parent) ? createIndex(row, column) : QModelIndex(); |
| 1669 |
} |
| 1670 |
int rowCount(const QModelIndex & /*parent*/ = QModelIndex()) const override |
| 1671 |
{ |
| 1672 |
return 8; |
| 1673 |
} |
| 1674 |
int columnCount(const QModelIndex &/*parent*/ = QModelIndex()) const override |
| 1675 |
{ |
| 1676 |
return m_col_count; |
| 1677 |
} |
| 1678 |
QVariant data(const QModelIndex &index, int role) const override |
| 1679 |
{ |
| 1680 |
if (!index.isValid()) |
| 1681 |
return QVariant(); |
| 1682 |
if (role == Qt::DisplayRole) |
| 1683 |
return QString::number(index.row()) + QLatin1Char(',') + QString::number(index.column()); |
| 1684 |
return QVariant(); |
| 1685 |
} |
| 1686 |
void setColumnCount(int c) |
| 1687 |
{ |
| 1688 |
m_col_count = c; |
| 1689 |
} |
| 1690 |
private: |
| 1691 |
int m_col_count = 3; |
| 1692 |
}; |
| 1693 |
|
| 1694 |
void tst_QHeaderView::moveSectionAndReset() |
| 1695 |
{ |
| 1696 |
SimpleModel m; |
| 1697 |
QHeaderView v(Qt::Horizontal); |
| 1698 |
v.setModel(&m); |
| 1699 |
int cc = 2; |
| 1700 |
for (cc = 2; cc < 4; ++cc) { |
| 1701 |
m.setColumnCount(cc); |
| 1702 |
int movefrom = 0; |
| 1703 |
int moveto; |
| 1704 |
for (moveto = 1; moveto < cc; ++moveto) { |
| 1705 |
v.moveSection(movefrom, moveto); |
| 1706 |
m.setColumnCount(cc - 1); |
| 1707 |
v.reset(); |
| 1708 |
for (int i = 0; i < cc - 1; ++i) |
| 1709 |
QCOMPARE(v.logicalIndex(v.visualIndex(i)), i); |
| 1710 |
} |
| 1711 |
} |
| 1712 |
} |
| 1713 |
|
| 1714 |
void tst_QHeaderView::moveSectionAndRemove() |
| 1715 |
{ |
| 1716 |
QStandardItemModel m; |
| 1717 |
QHeaderView v(Qt::Horizontal); |
| 1718 |
|
| 1719 |
v.setModel(&m); |
| 1720 |
v.model()->insertColumns(0, 3); |
| 1721 |
v.moveSection(0, 1); |
| 1722 |
|
| 1723 |
QCOMPARE(v.count(), 3); |
| 1724 |
v.model()->removeColumns(0, v.model()->columnCount()); |
| 1725 |
QCOMPARE(v.count(), 0); |
| 1726 |
} |
| 1727 |
|
| 1728 |
static QByteArray savedState() |
| 1729 |
{ |
| 1730 |
QStandardItemModel m(4, 4); |
| 1731 |
QHeaderView h1(Qt::Horizontal); |
| 1732 |
h1.setModel(&m); |
| 1733 |
h1.setMinimumSectionSize(0); // system default min size can be to large |
| 1734 |
h1.swapSections(0, 2); |
| 1735 |
h1.resizeSection(1, 10); |
| 1736 |
h1.setSortIndicatorShown(true); |
| 1737 |
h1.setSortIndicator(2, Qt::DescendingOrder); |
| 1738 |
h1.setSectionHidden(3, true); |
| 1739 |
return h1.saveState(); |
| 1740 |
} |
| 1741 |
|
| 1742 |
// As generated by savedState() |
| 1743 |
static const QByteArray qt5SavedSate = QByteArrayLiteral("\x00\x00\x00\xFF\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x02\x01\x00\x00\x00\x04\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x04\b\x00\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00""d\x00\x00\x00\xD2\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00""d\x00\x00\x00\x00\x00\x00\x00\x84\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00""d\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00""d\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03\xE8\x00\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 1744 |
|
| 1745 |
enum class SaveRestoreOption |
| 1746 |
{ |
| 1747 |
CheckGeneratedState, |
| 1748 |
DoNotCheckGeneratedState, |
| 1749 |
}; |
| 1750 |
|
| 1751 |
static void saveRestoreImpl(const QByteArray &state, SaveRestoreOption option) |
| 1752 |
{ |
| 1753 |
QStandardItemModel m(4, 4); |
| 1754 |
|
| 1755 |
QHeaderView h2(Qt::Vertical); |
| 1756 |
QSignalSpy spy(&h2, &QHeaderView::sortIndicatorChanged); |
| 1757 |
|
| 1758 |
h2.setModel(&m); |
| 1759 |
QVERIFY(h2.restoreState(state)); |
| 1760 |
|
| 1761 |
QCOMPARE(spy.size(), 1); |
| 1762 |
QCOMPARE(spy.at(0).at(0).toInt(), 2); |
| 1763 |
|
| 1764 |
QCOMPARE(h2.logicalIndex(0), 2); |
| 1765 |
QCOMPARE(h2.logicalIndex(2), 0); |
| 1766 |
QCOMPARE(h2.sectionSize(1), 10); |
| 1767 |
QCOMPARE(h2.sortIndicatorSection(), 2); |
| 1768 |
QCOMPARE(h2.sortIndicatorOrder(), Qt::DescendingOrder); |
| 1769 |
QCOMPARE(h2.isSortIndicatorShown(), true); |
| 1770 |
QVERIFY(!h2.isSectionHidden(2)); |
| 1771 |
QVERIFY(h2.isSectionHidden(3)); |
| 1772 |
QCOMPARE(h2.hiddenSectionCount(), 1); |
| 1773 |
|
| 1774 |
switch (option) { |
| 1775 |
case SaveRestoreOption::CheckGeneratedState: |
| 1776 |
{ |
| 1777 |
QByteArray s2 = h2.saveState(); |
| 1778 |
QCOMPARE(state, s2); |
| 1779 |
break; |
| 1780 |
} |
| 1781 |
case SaveRestoreOption::DoNotCheckGeneratedState: |
| 1782 |
break; |
| 1783 |
}; |
| 1784 |
|
| 1785 |
QVERIFY(!h2.restoreState(QByteArrayLiteral("Garbage"))); |
| 1786 |
} |
| 1787 |
|
| 1788 |
void tst_QHeaderView::saveRestore() |
| 1789 |
{ |
| 1790 |
saveRestoreImpl(savedState(), SaveRestoreOption::CheckGeneratedState); |
| 1791 |
} |
| 1792 |
|
| 1793 |
void tst_QHeaderView::QTBUG99487_saveRestoreQt5Compat() |
| 1794 |
{ |
| 1795 |
saveRestoreImpl(qt5SavedSate, SaveRestoreOption::DoNotCheckGeneratedState); |
| 1796 |
} |
| 1797 |
|
| 1798 |
void tst_QHeaderView::restoreToMoreColumns() |
| 1799 |
{ |
| 1800 |
// Restore state onto a model with more columns |
| 1801 |
const QByteArray s1 = savedState(); |
| 1802 |
QHeaderView h4(Qt::Horizontal); |
| 1803 |
QStandardItemModel fiveColumnsModel(1, 5); |
| 1804 |
h4.setModel(&fiveColumnsModel); |
| 1805 |
QCOMPARE(fiveColumnsModel.columnCount(), 5); |
| 1806 |
QCOMPARE(h4.count(), 5); |
| 1807 |
QVERIFY(h4.restoreState(s1)); |
| 1808 |
QCOMPARE(fiveColumnsModel.columnCount(), 5); |
| 1809 |
QCOMPARE(h4.count(), 5); |
| 1810 |
QCOMPARE(h4.sectionSize(1), 10); |
| 1811 |
for (int i = 0; i < h4.count(); ++i) |
| 1812 |
QVERIFY(h4.sectionSize(i) > 0 || h4.isSectionHidden(i)); |
| 1813 |
QVERIFY(!h4.isSectionHidden(2)); |
| 1814 |
QVERIFY(h4.isSectionHidden(3)); |
| 1815 |
QCOMPARE(h4.hiddenSectionCount(), 1); |
| 1816 |
QCOMPARE(h4.sortIndicatorSection(), 2); |
| 1817 |
QCOMPARE(h4.sortIndicatorOrder(), Qt::DescendingOrder); |
| 1818 |
QCOMPARE(h4.logicalIndex(0), 2); |
| 1819 |
QCOMPARE(h4.logicalIndex(1), 1); |
| 1820 |
QCOMPARE(h4.logicalIndex(2), 0); |
| 1821 |
QCOMPARE(h4.visualIndex(0), 2); |
| 1822 |
QCOMPARE(h4.visualIndex(1), 1); |
| 1823 |
QCOMPARE(h4.visualIndex(2), 0); |
| 1824 |
|
| 1825 |
// Repainting shouldn't crash |
| 1826 |
h4.show(); |
| 1827 |
QVERIFY(QTest::qWaitForWindowExposed(&h4)); |
| 1828 |
} |
| 1829 |
|
| 1830 |
void tst_QHeaderView::restoreToMoreColumnsNoMovedColumns() |
| 1831 |
{ |
| 1832 |
// Given a model with 2 columns, for saving state |
| 1833 |
QHeaderView h1(Qt::Horizontal); |
| 1834 |
QStandardItemModel model1(1, 2); |
| 1835 |
h1.setModel(&model1); |
| 1836 |
QCOMPARE(h1.visualIndex(0), 0); |
| 1837 |
QCOMPARE(h1.visualIndex(1), 1); |
| 1838 |
QCOMPARE(h1.logicalIndex(0), 0); |
| 1839 |
QCOMPARE(h1.logicalIndex(1), 1); |
| 1840 |
const QByteArray savedState = h1.saveState(); |
| 1841 |
|
| 1842 |
// And a model with 3 columns, to apply that state upon |
| 1843 |
QHeaderView h2(Qt::Horizontal); |
| 1844 |
QStandardItemModel model2(1, 3); |
| 1845 |
h2.setModel(&model2); |
| 1846 |
QCOMPARE(h2.visualIndex(0), 0); |
| 1847 |
QCOMPARE(h2.visualIndex(1), 1); |
| 1848 |
QCOMPARE(h2.visualIndex(2), 2); |
| 1849 |
QCOMPARE(h2.logicalIndex(0), 0); |
| 1850 |
QCOMPARE(h2.logicalIndex(1), 1); |
| 1851 |
QCOMPARE(h2.logicalIndex(2), 2); |
| 1852 |
|
| 1853 |
// When calling restoreState() |
| 1854 |
QVERIFY(h2.restoreState(savedState)); |
| 1855 |
|
| 1856 |
// Then the index mapping should still be as default |
| 1857 |
QCOMPARE(h2.visualIndex(0), 0); |
| 1858 |
QCOMPARE(h2.visualIndex(1), 1); |
| 1859 |
QCOMPARE(h2.visualIndex(2), 2); |
| 1860 |
QCOMPARE(h2.logicalIndex(0), 0); |
| 1861 |
QCOMPARE(h2.logicalIndex(1), 1); |
| 1862 |
QCOMPARE(h2.logicalIndex(2), 2); |
| 1863 |
|
| 1864 |
// And repainting shouldn't crash |
| 1865 |
h2.show(); |
| 1866 |
QVERIFY(QTest::qWaitForWindowExposed(&h2)); |
| 1867 |
} |
| 1868 |
|
| 1869 |
void tst_QHeaderView::restoreBeforeSetModel() |
| 1870 |
{ |
| 1871 |
QHeaderView h2(Qt::Horizontal); |
| 1872 |
const QByteArray s1 = savedState(); |
| 1873 |
// First restore |
| 1874 |
QVERIFY(h2.restoreState(s1)); |
| 1875 |
// Then setModel |
| 1876 |
QStandardItemModel model(4, 4); |
| 1877 |
h2.setModel(&model); |
| 1878 |
|
| 1879 |
// Check the result |
| 1880 |
QCOMPARE(h2.logicalIndex(0), 2); |
| 1881 |
QCOMPARE(h2.logicalIndex(2), 0); |
| 1882 |
QCOMPARE(h2.sectionSize(1), 10); |
| 1883 |
QCOMPARE(h2.sortIndicatorSection(), 2); |
| 1884 |
QCOMPARE(h2.sortIndicatorOrder(), Qt::DescendingOrder); |
| 1885 |
QCOMPARE(h2.isSortIndicatorShown(), true); |
| 1886 |
QVERIFY(!h2.isSectionHidden(2)); |
| 1887 |
QVERIFY(h2.isSectionHidden(3)); |
| 1888 |
QCOMPARE(h2.hiddenSectionCount(), 1); |
| 1889 |
} |
| 1890 |
|
| 1891 |
void tst_QHeaderView::defaultSectionSizeTest() |
| 1892 |
{ |
| 1893 |
// Setup |
| 1894 |
QTableView qtv; |
| 1895 |
QHeaderView *hv = qtv.verticalHeader(); |
| 1896 |
hv->setMinimumSectionSize(10); |
| 1897 |
hv->setDefaultSectionSize(99); // Set it to a value different from defaultSize. |
| 1898 |
QStandardItemModel amodel(4, 4); |
| 1899 |
qtv.setModel(&amodel); |
| 1900 |
QCOMPARE(hv->sectionSize(0), 99); |
| 1901 |
QCOMPARE(hv->visualIndexAt(50), 0); // <= also make sure that indexes are calculated |
| 1902 |
hv->setDefaultSectionSize(40); // Set it to a value different from defaultSize. |
| 1903 |
QCOMPARE(hv->visualIndexAt(50), 1); |
| 1904 |
|
| 1905 |
const int defaultSize = 26; |
| 1906 |
hv->setDefaultSectionSize(defaultSize + 1); // Set it to a value different from defaultSize. |
| 1907 |
|
| 1908 |
// no hidden Sections |
| 1909 |
hv->resizeSection(1, 0); |
| 1910 |
hv->setDefaultSectionSize(defaultSize); |
| 1911 |
QCOMPARE(hv->sectionSize(1), defaultSize); |
| 1912 |
|
| 1913 |
// with hidden sections |
| 1914 |
hv->resizeSection(1, 0); |
| 1915 |
hv->hideSection(2); |
| 1916 |
hv->setDefaultSectionSize(defaultSize); |
| 1917 |
|
| 1918 |
QCOMPARE_EQ(hv->sectionSize(0), defaultSize); // trivial case. |
| 1919 |
QCOMPARE_EQ(hv->sectionSize(1), defaultSize); // just sized 0. Now it should be 10 |
| 1920 |
QCOMPARE_EQ(hv->sectionSize(2), 0); // section is hidden. It should not be resized. |
| 1921 |
} |
| 1922 |
|
| 1923 |
class TestHeaderViewStyle : public QProxyStyle |
| 1924 |
{ |
| 1925 |
Q_OBJECT |
| 1926 |
public: |
| 1927 |
using QProxyStyle::QProxyStyle; |
| 1928 |
int pixelMetric(PixelMetric metric, const QStyleOption *option = nullptr, |
| 1929 |
const QWidget *widget = nullptr) const override |
| 1930 |
{ |
| 1931 |
if (metric == QStyle::PM_HeaderDefaultSectionSizeHorizontal) |
| 1932 |
return horizontalSectionSize; |
| 1933 |
else |
| 1934 |
return QProxyStyle::pixelMetric(metric, option, widget); |
| 1935 |
} |
| 1936 |
int horizontalSectionSize = 100; |
| 1937 |
}; |
| 1938 |
|
| 1939 |
void tst_QHeaderView::defaultSectionSizeTestStyles() |
| 1940 |
{ |
| 1941 |
TestHeaderViewStyle style1; |
| 1942 |
TestHeaderViewStyle style2; |
| 1943 |
style1.horizontalSectionSize = 100; |
| 1944 |
style2.horizontalSectionSize = 200; |
| 1945 |
|
| 1946 |
QHeaderView hv(Qt::Horizontal); |
| 1947 |
hv.setStyle(&style1); |
| 1948 |
QCOMPARE(hv.defaultSectionSize(), style1.horizontalSectionSize); |
| 1949 |
hv.setStyle(&style2); |
| 1950 |
QCOMPARE(hv.defaultSectionSize(), style2.horizontalSectionSize); |
| 1951 |
hv.setDefaultSectionSize(70); |
| 1952 |
QCOMPARE(hv.defaultSectionSize(), 70); |
| 1953 |
hv.setStyle(&style1); |
| 1954 |
QCOMPARE(hv.defaultSectionSize(), 70); |
| 1955 |
hv.resetDefaultSectionSize(); |
| 1956 |
QCOMPARE(hv.defaultSectionSize(), style1.horizontalSectionSize); |
| 1957 |
hv.setStyle(&style2); |
| 1958 |
QCOMPARE(hv.defaultSectionSize(), style2.horizontalSectionSize); |
| 1959 |
} |
| 1960 |
|
| 1961 |
void tst_QHeaderView::defaultAlignment_data() |
| 1962 |
{ |
| 1963 |
QTest::addColumn<Qt::Orientation>("direction"); |
| 1964 |
QTest::addColumn<Qt::Alignment>("initial"); |
| 1965 |
QTest::addColumn<Qt::Alignment>("alignment"); |
| 1966 |
|
| 1967 |
QTest::newRow("horizontal right aligned") |
| 1968 |
<< Qt::Horizontal |
| 1969 |
<< Qt::Alignment(Qt::AlignCenter) |
| 1970 |
<< Qt::Alignment(Qt::AlignRight); |
| 1971 |
|
| 1972 |
QTest::newRow("horizontal left aligned") |
| 1973 |
<< Qt::Horizontal |
| 1974 |
<< Qt::Alignment(Qt::AlignCenter) |
| 1975 |
<< Qt::Alignment(Qt::AlignLeft); |
| 1976 |
|
| 1977 |
QTest::newRow("vertical right aligned") |
| 1978 |
<< Qt::Vertical |
| 1979 |
<< Qt::Alignment(Qt::AlignLeft|Qt::AlignVCenter) |
| 1980 |
<< Qt::Alignment(Qt::AlignRight); |
| 1981 |
|
| 1982 |
QTest::newRow("vertical left aligned") |
| 1983 |
<< Qt::Vertical |
| 1984 |
<< Qt::Alignment(Qt::AlignLeft|Qt::AlignVCenter) |
| 1985 |
<< Qt::Alignment(Qt::AlignLeft); |
| 1986 |
} |
| 1987 |
|
| 1988 |
void tst_QHeaderView::defaultAlignment() |
| 1989 |
{ |
| 1990 |
QFETCH(Qt::Orientation, direction); |
| 1991 |
QFETCH(Qt::Alignment, initial); |
| 1992 |
QFETCH(Qt::Alignment, alignment); |
| 1993 |
|
| 1994 |
SimpleModel m; |
| 1995 |
|
| 1996 |
QHeaderView header(direction); |
| 1997 |
header.setModel(&m); |
| 1998 |
|
| 1999 |
QCOMPARE(header.defaultAlignment(), initial); |
| 2000 |
header.setDefaultAlignment(alignment); |
| 2001 |
QCOMPARE(header.defaultAlignment(), alignment); |
| 2002 |
} |
| 2003 |
|
| 2004 |
void tst_QHeaderView::globalResizeMode_data() |
| 2005 |
{ |
| 2006 |
QTest::addColumn<Qt::Orientation>("direction"); |
| 2007 |
QTest::addColumn<QHeaderView::ResizeMode>("mode"); |
| 2008 |
QTest::addColumn<int>("insert"); |
| 2009 |
|
| 2010 |
QTest::newRow("horizontal ResizeToContents 0") |
| 2011 |
<< Qt::Horizontal |
| 2012 |
<< QHeaderView::ResizeToContents |
| 2013 |
<< 0; |
| 2014 |
} |
| 2015 |
|
| 2016 |
void tst_QHeaderView::globalResizeMode() |
| 2017 |
{ |
| 2018 |
QFETCH(Qt::Orientation, direction); |
| 2019 |
QFETCH(QHeaderView::ResizeMode, mode); |
| 2020 |
QFETCH(int, insert); |
| 2021 |
|
| 2022 |
QStandardItemModel m(4, 4); |
| 2023 |
QHeaderView h(direction); |
| 2024 |
h.setModel(&m); |
| 2025 |
|
| 2026 |
h.setSectionResizeMode(mode); |
| 2027 |
m.insertRow(insert); |
| 2028 |
for (int i = 0; i < h.count(); ++i) |
| 2029 |
QCOMPARE(h.sectionResizeMode(i), mode); |
| 2030 |
} |
| 2031 |
|
| 2032 |
|
| 2033 |
void tst_QHeaderView::sectionPressedSignal_data() |
| 2034 |
{ |
| 2035 |
QTest::addColumn<Qt::Orientation>("direction"); |
| 2036 |
QTest::addColumn<bool>("clickable"); |
| 2037 |
QTest::addColumn<int>("count"); |
| 2038 |
|
| 2039 |
QTest::newRow("horizontal unclickable 0") |
| 2040 |
<< Qt::Horizontal |
| 2041 |
<< false |
| 2042 |
<< 0; |
| 2043 |
|
| 2044 |
QTest::newRow("horizontal clickable 1") |
| 2045 |
<< Qt::Horizontal |
| 2046 |
<< true |
| 2047 |
<< 1; |
| 2048 |
} |
| 2049 |
|
| 2050 |
void tst_QHeaderView::sectionPressedSignal() |
| 2051 |
{ |
| 2052 |
QFETCH(Qt::Orientation, direction); |
| 2053 |
QFETCH(bool, clickable); |
| 2054 |
QFETCH(int, count); |
| 2055 |
|
| 2056 |
QStandardItemModel m(4, 4); |
| 2057 |
QHeaderView h(direction); |
| 2058 |
|
| 2059 |
h.setModel(&m); |
| 2060 |
h.show(); |
| 2061 |
h.setSectionsClickable(clickable); |
| 2062 |
|
| 2063 |
QSignalSpy spy(&h, &QHeaderView::sectionPressed); |
| 2064 |
|
| 2065 |
QCOMPARE(spy.size(), 0); |
| 2066 |
QTest::mousePress(h.viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(5, 5)); |
| 2067 |
QCOMPARE(spy.size(), count); |
| 2068 |
} |
| 2069 |
|
| 2070 |
void tst_QHeaderView::sectionClickedSignal() |
| 2071 |
{ |
| 2072 |
QFETCH(Qt::Orientation, direction); |
| 2073 |
QFETCH(bool, clickable); |
| 2074 |
QFETCH(int, count); |
| 2075 |
|
| 2076 |
QStandardItemModel m(4, 4); |
| 2077 |
QHeaderView h(direction); |
| 2078 |
|
| 2079 |
h.setModel(&m); |
| 2080 |
h.show(); |
| 2081 |
h.setSectionsClickable(clickable); |
| 2082 |
h.setSortIndicatorShown(true); |
| 2083 |
|
| 2084 |
QSignalSpy spy(&h, &QHeaderView::sectionClicked); |
| 2085 |
QSignalSpy spy2(&h, &QHeaderView::sortIndicatorChanged); |
| 2086 |
|
| 2087 |
QCOMPARE(spy.size(), 0); |
| 2088 |
QCOMPARE(spy2.size(), 0); |
| 2089 |
QTest::mouseClick(h.viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(5, 5)); |
| 2090 |
QCOMPARE(spy.size(), count); |
| 2091 |
QCOMPARE(spy2.size(), count); |
| 2092 |
|
| 2093 |
//now let's try with the sort indicator hidden (the result should be the same |
| 2094 |
spy.clear(); |
| 2095 |
spy2.clear(); |
| 2096 |
h.setSortIndicatorShown(false); |
| 2097 |
QTest::mouseClick(h.viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(5, 5)); |
| 2098 |
QCOMPARE(spy.size(), count); |
| 2099 |
QCOMPARE(spy2.size(), count); |
| 2100 |
} |
| 2101 |
|
| 2102 |
void tst_QHeaderView::defaultSectionSize_data() |
| 2103 |
{ |
| 2104 |
QTest::addColumn<Qt::Orientation>("direction"); |
| 2105 |
QTest::addColumn<int>("oldDefaultSize"); |
| 2106 |
QTest::addColumn<int>("newDefaultSize"); |
| 2107 |
|
| 2108 |
//QTest::newRow("horizontal,-5") << int(Qt::Horizontal) << 100 << -5; |
| 2109 |
QTest::newRow("horizontal, 0") << Qt::Horizontal << 100 << 0; |
| 2110 |
QTest::newRow("horizontal, 5") << Qt::Horizontal << 100 << 5; |
| 2111 |
QTest::newRow("horizontal,25") << Qt::Horizontal << 100 << 5; |
| 2112 |
} |
| 2113 |
|
| 2114 |
void tst_QHeaderView::defaultSectionSize() |
| 2115 |
{ |
| 2116 |
QFETCH(Qt::Orientation, direction); |
| 2117 |
QFETCH(int, oldDefaultSize); |
| 2118 |
QFETCH(int, newDefaultSize); |
| 2119 |
|
| 2120 |
QStandardItemModel m(4, 4); |
| 2121 |
QHeaderView h(direction); |
| 2122 |
|
| 2123 |
h.setModel(&m); |
| 2124 |
h.setMinimumSectionSize(0); |
| 2125 |
|
| 2126 |
QCOMPARE(h.defaultSectionSize(), oldDefaultSize); |
| 2127 |
h.setDefaultSectionSize(newDefaultSize); |
| 2128 |
QCOMPARE(h.defaultSectionSize(), newDefaultSize); |
| 2129 |
h.reset(); |
| 2130 |
for (int i = 0; i < h.count(); ++i) |
| 2131 |
QCOMPARE(h.sectionSize(i), newDefaultSize); |
| 2132 |
} |
| 2133 |
|
| 2134 |
void tst_QHeaderView::hideAndInsert_data() |
| 2135 |
{ |
| 2136 |
QTest::addColumn<Qt::Orientation>("direction"); |
| 2137 |
QTest::addColumn<int>("hide"); |
| 2138 |
QTest::addColumn<int>("insert"); |
| 2139 |
QTest::addColumn<int>("hidden"); |
| 2140 |
|
| 2141 |
QTest::newRow("horizontal, 0, 0") << Qt::Horizontal << 0 << 0 << 1; |
| 2142 |
} |
| 2143 |
|
| 2144 |
void tst_QHeaderView::hideAndInsert() |
| 2145 |
{ |
| 2146 |
QFETCH(Qt::Orientation, direction); |
| 2147 |
QFETCH(int, hide); |
| 2148 |
QFETCH(int, insert); |
| 2149 |
QFETCH(int, hidden); |
| 2150 |
|
| 2151 |
QStandardItemModel m(4, 4); |
| 2152 |
QHeaderView h(direction); |
| 2153 |
h.setModel(&m); |
| 2154 |
h.setSectionHidden(hide, true); |
| 2155 |
|
| 2156 |
if (direction == Qt::Vertical) |
| 2157 |
m.insertRow(insert); |
| 2158 |
else |
| 2159 |
m.insertColumn(insert); |
| 2160 |
|
| 2161 |
for (int i = 0; i < h.count(); ++i) |
| 2162 |
QCOMPARE(h.isSectionHidden(i), i == hidden); |
| 2163 |
} |
| 2164 |
|
| 2165 |
void tst_QHeaderView::removeSection() |
| 2166 |
{ |
| 2167 |
const int hidden = 3; //section that will be hidden |
| 2168 |
|
| 2169 |
QStringListModel model({ "0", "1", "2", "3", "4", "5", "6" }); |
| 2170 |
QHeaderView view(Qt::Vertical); |
| 2171 |
view.setModel(&model); |
| 2172 |
view.hideSection(hidden); |
| 2173 |
view.hideSection(1); |
| 2174 |
model.removeRow(1); |
| 2175 |
view.show(); |
| 2176 |
|
| 2177 |
for(int i = 0; i < view.count(); i++) { |
| 2178 |
if (i == (hidden - 1)) { //-1 because we removed a row in the meantime |
| 2179 |
QCOMPARE(view.sectionSize(i), 0); |
| 2180 |
QVERIFY(view.isSectionHidden(i)); |
| 2181 |
} else { |
| 2182 |
QCOMPARE(view.sectionSize(i), view.defaultSectionSize() ); |
| 2183 |
QVERIFY(!view.isSectionHidden(i)); |
| 2184 |
} |
| 2185 |
} |
| 2186 |
} |
| 2187 |
|
| 2188 |
void tst_QHeaderView::preserveHiddenSectionWidth() |
| 2189 |
{ |
| 2190 |
QStringListModel model({ "0", "1", "2", "3" }); |
| 2191 |
QHeaderView view(Qt::Vertical); |
| 2192 |
view.setModel(&model); |
| 2193 |
view.resizeSection(0, 100); |
| 2194 |
view.resizeSection(1, 10); |
| 2195 |
view.resizeSection(2, 50); |
| 2196 |
view.setSectionResizeMode(3, QHeaderView::Stretch); |
| 2197 |
view.show(); |
| 2198 |
|
| 2199 |
view.hideSection(2); |
| 2200 |
model.removeRow(1); |
| 2201 |
view.showSection(1); |
| 2202 |
QCOMPARE(view.sectionSize(0), 100); |
| 2203 |
QCOMPARE(view.sectionSize(1), 50); |
| 2204 |
|
| 2205 |
view.hideSection(1); |
| 2206 |
model.insertRow(1); |
| 2207 |
view.showSection(2); |
| 2208 |
QCOMPARE(view.sectionSize(0), 100); |
| 2209 |
QCOMPARE(view.sectionSize(1), view.defaultSectionSize()); |
| 2210 |
QCOMPARE(view.sectionSize(2), 50); |
| 2211 |
} |
| 2212 |
|
| 2213 |
void tst_QHeaderView::invisibleStretchLastSection() |
| 2214 |
{ |
| 2215 |
if (QGuiApplication::platformName().startsWith(QLatin1String("eglfs"), Qt::CaseInsensitive)) |
| 2216 |
QSKIP("EGLFS does not allow resizing on top window"); |
| 2217 |
|
| 2218 |
int count = 6; |
| 2219 |
QStandardItemModel model(1, count); |
| 2220 |
QHeaderView view(Qt::Horizontal); |
| 2221 |
view.setModel(&model); |
| 2222 |
int height = view.height(); |
| 2223 |
|
| 2224 |
view.resize(view.defaultSectionSize() * (count / 2), height); // don't show all sections |
| 2225 |
view.show(); |
| 2226 |
view.setStretchLastSection(true); |
| 2227 |
// stretch section is not visible; it should not be stretched |
| 2228 |
for (int i = 0; i < count; ++i) |
| 2229 |
QCOMPARE(view.sectionSize(i), view.defaultSectionSize()); |
| 2230 |
|
| 2231 |
view.resize(view.defaultSectionSize() * (count + 1), height); // give room to stretch |
| 2232 |
|
| 2233 |
// stretch section is visible; it should be stretched |
| 2234 |
for (int i = 0; i < count - 1; ++i) |
| 2235 |
QCOMPARE(view.sectionSize(i), view.defaultSectionSize()); |
| 2236 |
QCOMPARE(view.sectionSize(count - 1), view.defaultSectionSize() * 2); |
| 2237 |
} |
| 2238 |
|
| 2239 |
void tst_QHeaderView::noSectionsWithNegativeSize() |
| 2240 |
{ |
| 2241 |
QStandardItemModel m(4, 4); |
| 2242 |
QHeaderView h(Qt::Horizontal); |
| 2243 |
h.setModel(&m); |
| 2244 |
h.resizeSection(1, -5); |
| 2245 |
QCOMPARE_GE(h.sectionSize(1), 0); // Sections with negative sizes not well defined. |
| 2246 |
} |
| 2247 |
|
| 2248 |
void tst_QHeaderView::emptySectionSpan() |
| 2249 |
{ |
| 2250 |
QHeaderViewPrivate::SectionItem section; |
| 2251 |
QCOMPARE(section.sectionSize(), 0); |
| 2252 |
} |
| 2253 |
|
| 2254 |
void tst_QHeaderView::task236450_hidden_data() |
| 2255 |
{ |
| 2256 |
QTest::addColumn<IntList>("hide1"); |
| 2257 |
QTest::addColumn<IntList>("hide2"); |
| 2258 |
|
| 2259 |
QTest::newRow("set 1") << (IntList{ 1, 3 }) |
| 2260 |
<< (IntList{ 1, 5 }); |
| 2261 |
|
| 2262 |
QTest::newRow("set 2") << (IntList{ 2, 3 }) |
| 2263 |
<< (IntList{ 1, 5 }); |
| 2264 |
|
| 2265 |
QTest::newRow("set 3") << (IntList{ 0, 2, 4 }) |
| 2266 |
<< (IntList{ 2, 3, 5 }); |
| 2267 |
|
| 2268 |
} |
| 2269 |
|
| 2270 |
void tst_QHeaderView::task236450_hidden() |
| 2271 |
{ |
| 2272 |
QFETCH(const IntList, hide1); |
| 2273 |
QFETCH(const IntList, hide2); |
| 2274 |
|
| 2275 |
QStringListModel model({ "0", "1", "2", "3", "4", "5" }); |
| 2276 |
protected_QHeaderView view(Qt::Vertical); |
| 2277 |
view.setModel(&model); |
| 2278 |
view.show(); |
| 2279 |
|
| 2280 |
for (int i : hide1) |
| 2281 |
view.hideSection(i); |
| 2282 |
|
| 2283 |
QCOMPARE(view.hiddenSectionCount(), hide1.size()); |
| 2284 |
for (int i = 0; i < 6; i++) |
| 2285 |
QCOMPARE(!view.isSectionHidden(i), !hide1.contains(i)); |
| 2286 |
|
| 2287 |
view.setDefaultSectionSize(2); |
| 2288 |
view.scheduleDelayedItemsLayout(); |
| 2289 |
view.executeDelayedItemsLayout(); //force to do a relayout |
| 2290 |
|
| 2291 |
QCOMPARE(view.hiddenSectionCount(), hide1.size()); |
| 2292 |
for (int i = 0; i < 6; i++) { |
| 2293 |
QCOMPARE(!view.isSectionHidden(i), !hide1.contains(i)); |
| 2294 |
view.setSectionHidden(i, hide2.contains(i)); |
| 2295 |
} |
| 2296 |
|
| 2297 |
QCOMPARE(view.hiddenSectionCount(), hide2.size()); |
| 2298 |
for (int i = 0; i < 6; i++) |
| 2299 |
QCOMPARE(!view.isSectionHidden(i), !hide2.contains(i)); |
| 2300 |
} |
| 2301 |
|
| 2302 |
void tst_QHeaderView::task248050_hideRow() |
| 2303 |
{ |
| 2304 |
//this is the sequence of events that make the task fail |
| 2305 |
protected_QHeaderView header(Qt::Vertical); |
| 2306 |
QStandardItemModel model(0, 1); |
| 2307 |
header.setMinimumSectionSize(0); // system default min size can be to large |
| 2308 |
header.setStretchLastSection(false); |
| 2309 |
header.setDefaultSectionSize(17); |
| 2310 |
header.setModel(&model); |
| 2311 |
header.doItemsLayout(); |
| 2312 |
|
| 2313 |
model.setRowCount(3); |
| 2314 |
|
| 2315 |
QCOMPARE(header.sectionPosition(2), 17*2); |
| 2316 |
|
| 2317 |
header.hideSection(1); |
| 2318 |
QCOMPARE(header.sectionPosition(2), 17); |
| 2319 |
|
| 2320 |
QTest::qWait(100); |
| 2321 |
//the size of the section shouldn't have changed |
| 2322 |
QCOMPARE(header.sectionPosition(2), 17); |
| 2323 |
} |
| 2324 |
|
| 2325 |
|
| 2326 |
//returns 0 if everything is fine. |
| 2327 |
static int checkHeaderViewOrder(const QHeaderView *view, const IntList &expected) |
| 2328 |
{ |
| 2329 |
if (view->count() != expected.size()) |
| 2330 |
return 1; |
| 2331 |
|
| 2332 |
for (int i = 0; i < expected.size(); i++) { |
| 2333 |
if (view->logicalIndex(i) != expected.at(i)) |
| 2334 |
return i + 10; |
| 2335 |
if (view->visualIndex(expected.at(i)) != i) |
| 2336 |
return i + 100; |
| 2337 |
} |
| 2338 |
return 0; |
| 2339 |
} |
| 2340 |
|
| 2341 |
|
| 2342 |
void tst_QHeaderView::QTBUG6058_reset() |
| 2343 |
{ |
| 2344 |
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) |
| 2345 |
QSKIP("Wayland: This fails. Figure out why."); |
| 2346 |
|
| 2347 |
QStringListModel model1({ "0", "1", "2", "3", "4", "5" }); |
| 2348 |
QStringListModel model2({ "a", "b", "c" }); |
| 2349 |
QSortFilterProxyModel proxy; |
| 2350 |
|
| 2351 |
QHeaderView view(Qt::Vertical); |
| 2352 |
view.setModel(&proxy); |
| 2353 |
view.show(); |
| 2354 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 2355 |
QVERIFY(QTest::qWaitForWindowActive(&view)); |
| 2356 |
|
| 2357 |
proxy.setSourceModel(&model1); |
| 2358 |
view.swapSections(0, 2); |
| 2359 |
view.swapSections(1, 4); |
| 2360 |
IntList expectedOrder{2, 4, 0, 3, 1, 5}; |
| 2361 |
QTRY_COMPARE(checkHeaderViewOrder(&view, expectedOrder) , 0); |
| 2362 |
|
| 2363 |
proxy.setSourceModel(&model2); |
| 2364 |
expectedOrder = {2, 0, 1}; |
| 2365 |
QTRY_COMPARE(checkHeaderViewOrder(&view, expectedOrder) , 0); |
| 2366 |
|
| 2367 |
proxy.setSourceModel(&model1); |
| 2368 |
expectedOrder = {2, 0, 1, 3, 4, 5}; |
| 2369 |
QTRY_COMPARE(checkHeaderViewOrder(&view, expectedOrder) , 0); |
| 2370 |
} |
| 2371 |
|
| 2372 |
void tst_QHeaderView::QTBUG7833_sectionClicked() |
| 2373 |
{ |
| 2374 |
QTableView tv; |
| 2375 |
QStandardItemModel *sim = new QStandardItemModel(&tv); |
| 2376 |
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(&tv); |
| 2377 |
proxyModel->setSourceModel(sim); |
| 2378 |
proxyModel->setDynamicSortFilter(true); |
| 2379 |
proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive); |
| 2380 |
|
| 2381 |
QList<QStandardItem *> row; |
| 2382 |
for (char i = 0; i < 12; i++) |
| 2383 |
row.append(new QStandardItem(QString(QLatin1Char('A' + i)))); |
| 2384 |
sim->appendRow(row); |
| 2385 |
row.clear(); |
| 2386 |
for (char i = 12; i > 0; i--) |
| 2387 |
row.append(new QStandardItem(QString(QLatin1Char('A' + i)))); |
| 2388 |
sim->appendRow(row); |
| 2389 |
|
| 2390 |
tv.setSortingEnabled(true); |
| 2391 |
tv.horizontalHeader()->setSortIndicatorShown(true); |
| 2392 |
tv.horizontalHeader()->setSectionsClickable(true); |
| 2393 |
tv.horizontalHeader()->setStretchLastSection(true); |
| 2394 |
tv.horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); |
| 2395 |
|
| 2396 |
tv.setModel(proxyModel); |
| 2397 |
const int section4Size = tv.horizontalHeader()->sectionSize(4) + 1; |
| 2398 |
const int section5Size = section4Size + 1; |
| 2399 |
tv.horizontalHeader()->resizeSection(4, section4Size); |
| 2400 |
tv.horizontalHeader()->resizeSection(5, section5Size); |
| 2401 |
tv.setColumnHidden(5, true); |
| 2402 |
tv.setColumnHidden(6, true); |
| 2403 |
tv.horizontalHeader()->swapSections(8, 10); |
| 2404 |
tv.sortByColumn(1, Qt::AscendingOrder); |
| 2405 |
|
| 2406 |
QCOMPARE(tv.isColumnHidden(5), true); |
| 2407 |
QCOMPARE(tv.isColumnHidden(6), true); |
| 2408 |
QCOMPARE(tv.horizontalHeader()->sectionsMoved(), true); |
| 2409 |
QCOMPARE(tv.horizontalHeader()->logicalIndex(8), 10); |
| 2410 |
QCOMPARE(tv.horizontalHeader()->logicalIndex(10), 8); |
| 2411 |
QCOMPARE(tv.horizontalHeader()->sectionSize(4), section4Size); |
| 2412 |
tv.setColumnHidden(5, false); // unhide, section size must be properly restored |
| 2413 |
QCOMPARE(tv.horizontalHeader()->sectionSize(5), section5Size); |
| 2414 |
tv.setColumnHidden(5, true); |
| 2415 |
|
| 2416 |
QSignalSpy clickedSpy(tv.horizontalHeader(), &QHeaderView::sectionClicked); |
| 2417 |
QSignalSpy pressedSpy(tv.horizontalHeader(), &QHeaderView::sectionPressed); |
| 2418 |
|
| 2419 |
|
| 2420 |
QTest::mouseClick(tv.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier, |
| 2421 |
QPoint(tv.horizontalHeader()->sectionViewportPosition(11) + |
| 2422 |
tv.horizontalHeader()->sectionSize(11) / 2, 5)); |
| 2423 |
QCOMPARE(clickedSpy.size(), 1); |
| 2424 |
QCOMPARE(pressedSpy.size(), 1); |
| 2425 |
QCOMPARE(clickedSpy.at(0).at(0).toInt(), 11); |
| 2426 |
QCOMPARE(pressedSpy.at(0).at(0).toInt(), 11); |
| 2427 |
|
| 2428 |
QTest::mouseClick(tv.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier, |
| 2429 |
QPoint(tv.horizontalHeader()->sectionViewportPosition(8) + |
| 2430 |
tv.horizontalHeader()->sectionSize(0) / 2, 5)); |
| 2431 |
|
| 2432 |
QCOMPARE(clickedSpy.size(), 2); |
| 2433 |
QCOMPARE(pressedSpy.size(), 2); |
| 2434 |
QCOMPARE(clickedSpy.at(1).at(0).toInt(), 8); |
| 2435 |
QCOMPARE(pressedSpy.at(1).at(0).toInt(), 8); |
| 2436 |
|
| 2437 |
QTest::mouseClick(tv.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier, |
| 2438 |
QPoint(tv.horizontalHeader()->sectionViewportPosition(0) + |
| 2439 |
tv.horizontalHeader()->sectionSize(0) / 2, 5)); |
| 2440 |
|
| 2441 |
QCOMPARE(clickedSpy.size(), 3); |
| 2442 |
QCOMPARE(pressedSpy.size(), 3); |
| 2443 |
QCOMPARE(clickedSpy.at(2).at(0).toInt(), 0); |
| 2444 |
QCOMPARE(pressedSpy.at(2).at(0).toInt(), 0); |
| 2445 |
} |
| 2446 |
|
| 2447 |
void tst_QHeaderView::checkLayoutChangeEmptyModel() |
| 2448 |
{ |
| 2449 |
QtTestModel tm(0, 11); |
| 2450 |
QTableView tv; |
| 2451 |
tv.verticalHeader()->setStretchLastSection(true); |
| 2452 |
tv.setModel(&tm); |
| 2453 |
|
| 2454 |
const int section4Size = tv.horizontalHeader()->sectionSize(4) + 1; |
| 2455 |
const int section5Size = section4Size + 1; |
| 2456 |
tv.horizontalHeader()->resizeSection(4, section4Size); |
| 2457 |
tv.horizontalHeader()->resizeSection(5, section5Size); |
| 2458 |
tv.setColumnHidden(5, true); |
| 2459 |
tv.setColumnHidden(6, true); |
| 2460 |
tv.horizontalHeader()->swapSections(8, 10); |
| 2461 |
|
| 2462 |
tv.sortByColumn(1, Qt::AscendingOrder); |
| 2463 |
tm.emitLayoutChanged(); |
| 2464 |
|
| 2465 |
QCOMPARE(tv.isColumnHidden(5), true); |
| 2466 |
QCOMPARE(tv.isColumnHidden(6), true); |
| 2467 |
QCOMPARE(tv.horizontalHeader()->sectionsMoved(), true); |
| 2468 |
QCOMPARE(tv.horizontalHeader()->logicalIndex(8), 10); |
| 2469 |
QCOMPARE(tv.horizontalHeader()->logicalIndex(10), 8); |
| 2470 |
QCOMPARE(tv.horizontalHeader()->sectionSize(4), section4Size); |
| 2471 |
tv.setColumnHidden(5, false); // unhide, section size must be properly restored |
| 2472 |
QCOMPARE(tv.horizontalHeader()->sectionSize(5), section5Size); |
| 2473 |
tv.setColumnHidden(5, true); |
| 2474 |
|
| 2475 |
// adjust |
| 2476 |
tm.rows = 3; |
| 2477 |
tm.emitLayoutChanged(); |
| 2478 |
|
| 2479 |
// remove the row used for QPersistenModelIndexes |
| 2480 |
tm.emitLayoutChangedWithRemoveFirstRow(); |
| 2481 |
QCOMPARE(tv.isColumnHidden(5), true); |
| 2482 |
QCOMPARE(tv.isColumnHidden(6), true); |
| 2483 |
QCOMPARE(tv.horizontalHeader()->sectionsMoved(), true); |
| 2484 |
QCOMPARE(tv.horizontalHeader()->logicalIndex(8), 10); |
| 2485 |
QCOMPARE(tv.horizontalHeader()->logicalIndex(10), 8); |
| 2486 |
QCOMPARE(tv.horizontalHeader()->sectionSize(4), section4Size); |
| 2487 |
tv.setColumnHidden(5, false); // unhide, section size must be properly restored |
| 2488 |
QCOMPARE(tv.horizontalHeader()->sectionSize(5), section5Size); |
| 2489 |
tv.setColumnHidden(5, true); |
| 2490 |
} |
| 2491 |
|
| 2492 |
void tst_QHeaderView::QTBUG8650_crashOnInsertSections() |
| 2493 |
{ |
| 2494 |
QStringList headerLabels; |
| 2495 |
QHeaderView view(Qt::Horizontal); |
| 2496 |
QStandardItemModel model(2, 2); |
| 2497 |
view.setModel(&model); |
| 2498 |
view.moveSection(1, 0); |
| 2499 |
view.hideSection(0); |
| 2500 |
|
| 2501 |
model.insertColumn(0, { new QStandardItem("c") }); |
| 2502 |
} |
| 2503 |
|
| 2504 |
static void setModelTexts(QStandardItemModel *model) |
| 2505 |
{ |
| 2506 |
const int columnCount = model->columnCount(); |
| 2507 |
for (int i = 0, rowCount = model->rowCount(); i < rowCount; ++i) { |
| 2508 |
const QString prefix = QLatin1String("item [") + QString::number(i) + QLatin1Char(','); |
| 2509 |
for (int j = 0; j < columnCount; ++j) |
| 2510 |
model->setData(model->index(i, j), prefix + QString::number(j) + QLatin1Char(']')); |
| 2511 |
} |
| 2512 |
} |
| 2513 |
|
| 2514 |
void tst_QHeaderView::QTBUG12268_hiddenMovedSectionSorting() |
| 2515 |
{ |
| 2516 |
QTableView view; // ### this test fails on QTableView &view = *m_tableview; !? + shadowing view member |
| 2517 |
QStandardItemModel model(4, 3); |
| 2518 |
setModelTexts(&model); |
| 2519 |
view.setModel(&model); |
| 2520 |
view.horizontalHeader()->setSectionsMovable(true); |
| 2521 |
view.setSortingEnabled(true); |
| 2522 |
view.sortByColumn(1, Qt::AscendingOrder); |
| 2523 |
view.horizontalHeader()->moveSection(0,2); |
| 2524 |
view.setColumnHidden(1, true); |
| 2525 |
view.show(); |
| 2526 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 2527 |
QCOMPARE(view.horizontalHeader()->hiddenSectionCount(), 1); |
| 2528 |
QTest::mouseClick(view.horizontalHeader()->viewport(), Qt::LeftButton); |
| 2529 |
QCOMPARE(view.horizontalHeader()->hiddenSectionCount(), 1); |
| 2530 |
} |
| 2531 |
|
| 2532 |
void tst_QHeaderView::QTBUG14242_hideSectionAutoSize() |
| 2533 |
{ |
| 2534 |
QTableView qtv; |
| 2535 |
QStandardItemModel amodel(4, 4); |
| 2536 |
qtv.setModel(&amodel); |
| 2537 |
QHeaderView *hv = qtv.verticalHeader(); |
| 2538 |
hv->setDefaultSectionSize(25); |
| 2539 |
hv->setSectionResizeMode(QHeaderView::ResizeToContents); |
| 2540 |
qtv.show(); |
| 2541 |
|
| 2542 |
hv->hideSection(0); |
| 2543 |
int afterlength = hv->length(); |
| 2544 |
|
| 2545 |
int calced_length = 0; |
| 2546 |
for (int u = 0; u < hv->count(); ++u) |
| 2547 |
calced_length += hv->sectionSize(u); |
| 2548 |
|
| 2549 |
QCOMPARE(calced_length, afterlength); |
| 2550 |
} |
| 2551 |
|
| 2552 |
void tst_QHeaderView::QTBUG50171_visualRegionForSwappedItems() |
| 2553 |
{ |
| 2554 |
protected_QHeaderView headerView(Qt::Horizontal); |
| 2555 |
QtTestModel model(2, 3); |
| 2556 |
headerView.setModel(&model); |
| 2557 |
headerView.swapSections(1, 2); |
| 2558 |
headerView.hideSection(0); |
| 2559 |
headerView.testVisualRegionForSelection(); |
| 2560 |
} |
| 2561 |
|
| 2562 |
class QTBUG53221_Model : public QAbstractItemModel |
| 2563 |
{ |
| 2564 |
Q_OBJECT |
| 2565 |
public: |
| 2566 |
void insertRowAtBeginning() |
| 2567 |
{ |
| 2568 |
Q_EMIT layoutAboutToBeChanged(); |
| 2569 |
m_displayNames.insert(0, QStringLiteral("Item %1").arg(m_displayNames.size())); |
| 2570 |
// Rows are always inserted at the beginning, so move all others. |
| 2571 |
const auto pl = persistentIndexList(); |
| 2572 |
// The vertical header view will have a persistent index stored here on the second call to insertRowAtBeginning. |
| 2573 |
for (const QModelIndex &persIndex : pl) |
| 2574 |
changePersistentIndex(persIndex, index(persIndex.row() + 1, persIndex.column(), persIndex.parent())); |
| 2575 |
Q_EMIT layoutChanged(); |
| 2576 |
} |
| 2577 |
|
| 2578 |
QVariant data(const QModelIndex &index, int role) const override |
| 2579 |
{ |
| 2580 |
return (role == Qt::DisplayRole) ? m_displayNames.at(index.row()) : QVariant(); |
| 2581 |
} |
| 2582 |
|
| 2583 |
QModelIndex index(int row, int column, const QModelIndex &) const override { return createIndex(row, column); } |
| 2584 |
QModelIndex parent(const QModelIndex &) const override { return QModelIndex(); } |
| 2585 |
int rowCount(const QModelIndex &) const override { return m_displayNames.size(); } |
| 2586 |
int columnCount(const QModelIndex &) const override { return 1; } |
| 2587 |
|
| 2588 |
private: |
| 2589 |
QStringList m_displayNames; |
| 2590 |
}; |
| 2591 |
|
| 2592 |
void tst_QHeaderView::QTBUG53221_assertShiftHiddenRow() |
| 2593 |
{ |
| 2594 |
QTableView tableView; |
| 2595 |
QTBUG53221_Model modelTableView; |
| 2596 |
tableView.setModel(&modelTableView); |
| 2597 |
|
| 2598 |
modelTableView.insertRowAtBeginning(); |
| 2599 |
tableView.setRowHidden(0, true); |
| 2600 |
QCOMPARE(tableView.verticalHeader()->isSectionHidden(0), true); |
| 2601 |
modelTableView.insertRowAtBeginning(); |
| 2602 |
QCOMPARE(tableView.verticalHeader()->isSectionHidden(0), false); |
| 2603 |
QCOMPARE(tableView.verticalHeader()->isSectionHidden(1), true); |
| 2604 |
modelTableView.insertRowAtBeginning(); |
| 2605 |
QCOMPARE(tableView.verticalHeader()->isSectionHidden(0), false); |
| 2606 |
QCOMPARE(tableView.verticalHeader()->isSectionHidden(1), false); |
| 2607 |
QCOMPARE(tableView.verticalHeader()->isSectionHidden(2), true); |
| 2608 |
} |
| 2609 |
|
| 2610 |
void tst_QHeaderView::QTBUG75615_sizeHintWithStylesheet() |
| 2611 |
{ |
| 2612 |
QTableView tableView; |
| 2613 |
QStandardItemModel model(1, 1); |
| 2614 |
tableView.setModel(&model); |
| 2615 |
tableView.show(); |
| 2616 |
|
| 2617 |
const auto headerView = tableView.horizontalHeader(); |
| 2618 |
const auto oldSizeHint = headerView->sizeHint(); |
| 2619 |
QVERIFY(oldSizeHint.isValid()); |
| 2620 |
|
| 2621 |
tableView.setStyleSheet("QTableView QHeaderView::section { height: 100px;}"); |
| 2622 |
QCOMPARE(headerView->sizeHint().width(), oldSizeHint.width()); |
| 2623 |
QCOMPARE(headerView->sizeHint().height(), 100); |
| 2624 |
|
| 2625 |
tableView.setStyleSheet("QTableView QHeaderView::section { width: 100px;}"); |
| 2626 |
QCOMPARE(headerView->sizeHint().height(), oldSizeHint.height()); |
| 2627 |
QCOMPARE(headerView->sizeHint().width(), 100); |
| 2628 |
} |
| 2629 |
|
| 2630 |
void protected_QHeaderView::testVisualRegionForSelection() |
| 2631 |
{ |
| 2632 |
QRegion r = visualRegionForSelection(QItemSelection(model()->index(1, 0), model()->index(1, 2))); |
| 2633 |
QCOMPARE(r.boundingRect().contains(QRect(1, 1, length() - 2, 1)), true); |
| 2634 |
} |
| 2635 |
|
| 2636 |
void tst_QHeaderView::ensureNoIndexAtLength() |
| 2637 |
{ |
| 2638 |
QTableView qtv; |
| 2639 |
QStandardItemModel amodel(4, 4); |
| 2640 |
qtv.setModel(&amodel); |
| 2641 |
QHeaderView *hv = qtv.verticalHeader(); |
| 2642 |
QCOMPARE(hv->visualIndexAt(hv->length()), -1); |
| 2643 |
hv->resizeSection(hv->count() - 1, 0); |
| 2644 |
QCOMPARE(hv->visualIndexAt(hv->length()), -1); |
| 2645 |
} |
| 2646 |
|
| 2647 |
void tst_QHeaderView::offsetConsistent() |
| 2648 |
{ |
| 2649 |
// Ensure that a hidden section 'far away' |
| 2650 |
// does not affect setOffsetToSectionPosition .. |
| 2651 |
const int sectionToHide = 513; |
| 2652 |
QTableView qtv; |
| 2653 |
QStandardItemModel amodel(1000, 4); |
| 2654 |
qtv.setModel(&amodel); |
| 2655 |
QHeaderView *hv = qtv.verticalHeader(); |
| 2656 |
for (int u = 0; u < 100; u += 2) |
| 2657 |
hv->resizeSection(u, 0); |
| 2658 |
hv->setOffsetToSectionPosition(150); |
| 2659 |
int offset1 = hv->offset(); |
| 2660 |
hv->hideSection(sectionToHide); |
| 2661 |
hv->setOffsetToSectionPosition(150); |
| 2662 |
int offset2 = hv->offset(); |
| 2663 |
QCOMPARE(offset1, offset2); |
| 2664 |
// Ensure that hidden indexes (still) is considered. |
| 2665 |
hv->resizeSection(sectionToHide, hv->sectionSize(200) * 2); |
| 2666 |
hv->setOffsetToSectionPosition(800); |
| 2667 |
offset1 = hv->offset(); |
| 2668 |
hv->showSection(sectionToHide); |
| 2669 |
hv->setOffsetToSectionPosition(800); |
| 2670 |
offset2 = hv->offset(); |
| 2671 |
QCOMPARE_GT(offset2, offset1); |
| 2672 |
} |
| 2673 |
|
| 2674 |
void tst_QHeaderView::sectionsDontSortWhenNotClickingInThem() |
| 2675 |
{ |
| 2676 |
QTableView qtv; |
| 2677 |
QStandardItemModel amodel(1000, 4); |
| 2678 |
qtv.setModel(&amodel); |
| 2679 |
QHeaderView *hv = qtv.horizontalHeader(); |
| 2680 |
hv->setSectionsClickable(true); |
| 2681 |
hv->setFirstSectionMovable(true); |
| 2682 |
hv->setSectionsMovable(false); |
| 2683 |
|
| 2684 |
enum { DefaultYOffset = 5, OutOfRangeYOffset = 10000 }; |
| 2685 |
|
| 2686 |
const auto pressOnSection = [&](int section, int yOffset = DefaultYOffset) |
| 2687 |
{ |
| 2688 |
QTest::mousePress(hv->viewport(), Qt::LeftButton, Qt::NoModifier, |
| 2689 |
QPoint(hv->sectionViewportPosition(section) + hv->sectionSize(section) / 2, yOffset)); |
| 2690 |
}; |
| 2691 |
const auto moveOntoSection = [&](int section, int yOffset = DefaultYOffset) |
| 2692 |
{ |
| 2693 |
QTest::mouseMove(hv->viewport(), |
| 2694 |
QPoint(hv->sectionViewportPosition(section) + hv->sectionSize(section) / 2, yOffset)); |
| 2695 |
}; |
| 2696 |
const auto releaseOnSection = [&](int section, int yOffset = DefaultYOffset) |
| 2697 |
{ |
| 2698 |
QTest::mouseRelease(hv->viewport(), Qt::LeftButton, Qt::NoModifier, |
| 2699 |
QPoint(hv->sectionViewportPosition(section) + hv->sectionSize(section) / 2, yOffset)); |
| 2700 |
}; |
| 2701 |
|
| 2702 |
hv->setSortIndicator(-1, Qt::AscendingOrder); |
| 2703 |
QCOMPARE(hv->sortIndicatorSection(), -1); |
| 2704 |
|
| 2705 |
pressOnSection(0); |
| 2706 |
releaseOnSection(0); |
| 2707 |
// RESULT: sorting |
| 2708 |
QCOMPARE(hv->sortIndicatorSection(), 0); |
| 2709 |
|
| 2710 |
hv->setSortIndicator(-1, Qt::AscendingOrder); |
| 2711 |
QCOMPARE(hv->sortIndicatorSection(), -1); |
| 2712 |
|
| 2713 |
pressOnSection(0); |
| 2714 |
moveOntoSection(1); |
| 2715 |
releaseOnSection(1); |
| 2716 |
// RESULT: no sorting |
| 2717 |
QCOMPARE(hv->sortIndicatorSection(), -1); |
| 2718 |
|
| 2719 |
pressOnSection(0); |
| 2720 |
moveOntoSection(1); |
| 2721 |
moveOntoSection(2); |
| 2722 |
releaseOnSection(2); |
| 2723 |
// RESULT: no sorting |
| 2724 |
QCOMPARE(hv->sortIndicatorSection(), -1); |
| 2725 |
|
| 2726 |
pressOnSection(0); |
| 2727 |
moveOntoSection(1); |
| 2728 |
moveOntoSection(0); |
| 2729 |
releaseOnSection(0); |
| 2730 |
// RESULT: sorting by 0 |
| 2731 |
QCOMPARE(hv->sortIndicatorSection(), 0); |
| 2732 |
|
| 2733 |
pressOnSection(0); |
| 2734 |
moveOntoSection(1); |
| 2735 |
releaseOnSection(1); |
| 2736 |
// RESULT: no change, still sorting by 0 |
| 2737 |
QCOMPARE(hv->sortIndicatorSection(), 0); |
| 2738 |
|
| 2739 |
auto sortOrder = hv->sortIndicatorOrder(); |
| 2740 |
pressOnSection(1); |
| 2741 |
moveOntoSection(0); |
| 2742 |
releaseOnSection(0); |
| 2743 |
// RESULT: no change, still sorting by 0 |
| 2744 |
QCOMPARE(hv->sortIndicatorSection(), 0); |
| 2745 |
QCOMPARE(hv->sortIndicatorOrder(), sortOrder); |
| 2746 |
|
| 2747 |
pressOnSection(1); |
| 2748 |
moveOntoSection(0); |
| 2749 |
moveOntoSection(1); |
| 2750 |
releaseOnSection(1); |
| 2751 |
// RESULT: sorting by 1 |
| 2752 |
QCOMPARE(hv->sortIndicatorSection(), 1); |
| 2753 |
|
| 2754 |
pressOnSection(1); |
| 2755 |
moveOntoSection(0); |
| 2756 |
releaseOnSection(0); |
| 2757 |
// RESULT: no change, still sorting by 1 |
| 2758 |
QCOMPARE(hv->sortIndicatorSection(), 1); |
| 2759 |
|
| 2760 |
hv->setSortIndicator(-1, Qt::AscendingOrder); |
| 2761 |
QCOMPARE(hv->sortIndicatorSection(), -1); |
| 2762 |
|
| 2763 |
pressOnSection(0); |
| 2764 |
releaseOnSection(0, OutOfRangeYOffset); |
| 2765 |
// RESULT: no sorting |
| 2766 |
QCOMPARE(hv->sortIndicatorSection(), -1); |
| 2767 |
|
| 2768 |
pressOnSection(0); |
| 2769 |
moveOntoSection(0, OutOfRangeYOffset); |
| 2770 |
releaseOnSection(0, OutOfRangeYOffset); |
| 2771 |
// RESULT: no sorting |
| 2772 |
QCOMPARE(hv->sortIndicatorSection(), -1); |
| 2773 |
|
| 2774 |
pressOnSection(0); |
| 2775 |
moveOntoSection(0, OutOfRangeYOffset); |
| 2776 |
moveOntoSection(0); |
| 2777 |
releaseOnSection(0); |
| 2778 |
// RESULT: sorting by 0 |
| 2779 |
QCOMPARE(hv->sortIndicatorSection(), 0); |
| 2780 |
|
| 2781 |
pressOnSection(1); |
| 2782 |
releaseOnSection(1, OutOfRangeYOffset); |
| 2783 |
// RESULT: no change, still sorting by 0 |
| 2784 |
QCOMPARE(hv->sortIndicatorSection(), 0); |
| 2785 |
|
| 2786 |
pressOnSection(1); |
| 2787 |
moveOntoSection(1, OutOfRangeYOffset); |
| 2788 |
releaseOnSection(1, OutOfRangeYOffset); |
| 2789 |
// RESULT: no change, still sorting by 0 |
| 2790 |
QCOMPARE(hv->sortIndicatorSection(), 0); |
| 2791 |
|
| 2792 |
pressOnSection(1); |
| 2793 |
moveOntoSection(1, OutOfRangeYOffset); |
| 2794 |
moveOntoSection(1); |
| 2795 |
releaseOnSection(1); |
| 2796 |
// RESULT: sorting by 1 |
| 2797 |
QCOMPARE(hv->sortIndicatorSection(), 1); |
| 2798 |
|
| 2799 |
pressOnSection(2); |
| 2800 |
moveOntoSection(1); |
| 2801 |
moveOntoSection(2); |
| 2802 |
moveOntoSection(2, OutOfRangeYOffset); |
| 2803 |
releaseOnSection(2, OutOfRangeYOffset); |
| 2804 |
// RESULT: no change, still sorting by 1 |
| 2805 |
QCOMPARE(hv->sortIndicatorSection(), 1); |
| 2806 |
} |
| 2807 |
|
| 2808 |
void tst_QHeaderView::initialSortOrderRole() |
| 2809 |
{ |
| 2810 |
QTableView view; // ### Shadowing member view (of type QHeaderView) |
| 2811 |
QStandardItemModel model(4, 3); |
| 2812 |
setModelTexts(&model); |
| 2813 |
QStandardItem *ascendingItem = new QStandardItem(); |
| 2814 |
QStandardItem *descendingItem = new QStandardItem(); |
| 2815 |
ascendingItem->setData(Qt::AscendingOrder, Qt::InitialSortOrderRole); |
| 2816 |
descendingItem->setData(Qt::DescendingOrder, Qt::InitialSortOrderRole); |
| 2817 |
model.setHorizontalHeaderItem(1, ascendingItem); |
| 2818 |
model.setHorizontalHeaderItem(2, descendingItem); |
| 2819 |
view.setModel(&model); |
| 2820 |
view.setSortingEnabled(true); |
| 2821 |
view.sortByColumn(0, Qt::AscendingOrder); |
| 2822 |
view.show(); |
| 2823 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 2824 |
|
| 2825 |
const int column1Pos = view.horizontalHeader()->sectionViewportPosition(1) + 5; // +5 not to be on the handle |
| 2826 |
QTest::mouseClick(view.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(column1Pos, 0)); |
| 2827 |
QCOMPARE(view.horizontalHeader()->sortIndicatorSection(), 1); |
| 2828 |
QCOMPARE(view.horizontalHeader()->sortIndicatorOrder(), Qt::AscendingOrder); |
| 2829 |
|
| 2830 |
const int column2Pos = view.horizontalHeader()->sectionViewportPosition(2) + 5; // +5 not to be on the handle |
| 2831 |
QTest::mouseClick(view.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(column2Pos, 0)); |
| 2832 |
QCOMPARE(view.horizontalHeader()->sortIndicatorSection(), 2); |
| 2833 |
QCOMPARE(view.horizontalHeader()->sortIndicatorOrder(), Qt::DescendingOrder); |
| 2834 |
|
| 2835 |
const int column0Pos = view.horizontalHeader()->sectionViewportPosition(0) + 5; // +5 not to be on the handle |
| 2836 |
QTest::mouseClick(view.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(column0Pos, 0)); |
| 2837 |
QCOMPARE(view.horizontalHeader()->sortIndicatorSection(), 0); |
| 2838 |
QCOMPARE(view.horizontalHeader()->sortIndicatorOrder(), Qt::AscendingOrder); |
| 2839 |
} |
| 2840 |
|
| 2841 |
const bool block_some_signals = true; // The test should also work with this set to false |
| 2842 |
const int rowcount = 500; |
| 2843 |
const int colcount = 10; |
| 2844 |
|
| 2845 |
void tst_QHeaderView::setupTestData() |
| 2846 |
{ |
| 2847 |
QTest::addColumn<bool>("updates_enabled"); |
| 2848 |
QTest::addColumn<bool>("special_prepare"); |
| 2849 |
|
| 2850 |
QTest::newRow("no_updates+normal") << false << false; |
| 2851 |
QTest::newRow("hasupdates+normal") << true << false; |
| 2852 |
QTest::newRow("no_updates+special") << false << true; |
| 2853 |
QTest::newRow("hasupdates+special") << true << true; |
| 2854 |
} |
| 2855 |
|
| 2856 |
void tst_QHeaderView::additionalInit() |
| 2857 |
{ |
| 2858 |
m_tableview->setVerticalHeader(view); |
| 2859 |
|
| 2860 |
QFETCH(bool, updates_enabled); |
| 2861 |
QFETCH(bool, special_prepare); |
| 2862 |
|
| 2863 |
m_special_prepare = special_prepare; |
| 2864 |
m_tableview->setModel(model); |
| 2865 |
|
| 2866 |
const int default_section_size = 25; |
| 2867 |
view->setDefaultSectionSize(default_section_size); // Important - otherwise there will be semantic changes |
| 2868 |
|
| 2869 |
model->clear(); |
| 2870 |
|
| 2871 |
if (special_prepare) { |
| 2872 |
|
| 2873 |
for (int u = 0; u <= rowcount; ++u) // ensures fragmented spans in e.g Qt 4.7 |
| 2874 |
model->setRowCount(u); |
| 2875 |
|
| 2876 |
model->setColumnCount(colcount); |
| 2877 |
view->swapSections(0, rowcount - 1); |
| 2878 |
view->swapSections(0, rowcount - 1); // No real change - setup visual and log-indexes |
| 2879 |
view->hideSection(rowcount - 1); |
| 2880 |
view->showSection(rowcount - 1); // No real change - (maybe init hidden vector) |
| 2881 |
} else { |
| 2882 |
model->setColumnCount(colcount); |
| 2883 |
model->setRowCount(rowcount); |
| 2884 |
} |
| 2885 |
|
| 2886 |
QString s; |
| 2887 |
for (int i = 0; i < model->rowCount(); ++i) { |
| 2888 |
model->setData(model->index(i, 0), QVariant(i)); |
| 2889 |
s.setNum(i); |
| 2890 |
s += QLatin1Char('.'); |
| 2891 |
s += QChar('a' + (i % 25)); |
| 2892 |
model->setData(model->index(i, 1), QVariant(s)); |
| 2893 |
} |
| 2894 |
m_tableview->setUpdatesEnabled(updates_enabled); |
| 2895 |
view->blockSignals(block_some_signals); |
| 2896 |
timer.start(); |
| 2897 |
} |
| 2898 |
|
| 2899 |
void tst_QHeaderView::logicalIndexAtTest() |
| 2900 |
{ |
| 2901 |
additionalInit(); |
| 2902 |
|
| 2903 |
const int sectionSz = view->defaultSectionSize(); |
| 2904 |
view->swapSections(4, 9); // Make sure that visual and logical Indexes are not just the same. |
| 2905 |
|
| 2906 |
QCOMPARE(view->logicalIndexAt(0), 0); |
| 2907 |
QCOMPARE(view->logicalIndexAt(4 * sectionSz + 2), 9); |
| 2908 |
QCOMPARE(view->logicalIndexAt(9 * sectionSz + 2), 4); |
| 2909 |
|
| 2910 |
view->hideSection(5); |
| 2911 |
QCOMPARE(view->logicalIndexAt(5 * sectionSz + 2), 6); |
| 2912 |
|
| 2913 |
// Check edges move section |
| 2914 |
view->moveSection(1, 4); |
| 2915 |
QCOMPARE(view->logicalIndexAt(sectionSz), 2); |
| 2916 |
QCOMPARE(view->logicalIndexAt(sectionSz * 2), 3); |
| 2917 |
QCOMPARE(view->logicalIndexAt(sectionSz * 3), 9); // Due the 4<->9 swap |
| 2918 |
QCOMPARE(view->logicalIndexAt(sectionSz * 4), 1); |
| 2919 |
} |
| 2920 |
|
| 2921 |
void tst_QHeaderView::visualIndexAtTest() |
| 2922 |
{ |
| 2923 |
additionalInit(); |
| 2924 |
|
| 2925 |
const int sectionSz = view->defaultSectionSize(); |
| 2926 |
view->swapSections(4, 9); // Make sure that visual and logical Indexes are not just the same. |
| 2927 |
|
| 2928 |
QCOMPARE(view->visualIndexAt(0), 0); |
| 2929 |
// Make sure we return the visual index and not e.g the logical |
| 2930 |
QCOMPARE(view->visualIndexAt(4 * sectionSz + 2), 4); |
| 2931 |
QCOMPARE(view->visualIndexAt(9 * sectionSz + 2), 9); |
| 2932 |
|
| 2933 |
view->hideSection(5); |
| 2934 |
QCOMPARE(view->visualIndexAt(5 * sectionSz + 2), 6); |
| 2935 |
|
| 2936 |
// Visual section shouldn't be confused about sections moving around |
| 2937 |
view->moveSection(1, 4); |
| 2938 |
QCOMPARE(view->visualIndexAt(0), 0); |
| 2939 |
QCOMPARE(view->visualIndexAt(sectionSz), 1); |
| 2940 |
QCOMPARE(view->visualIndexAt(sectionSz * 2), 2); |
| 2941 |
QCOMPARE(view->visualIndexAt(sectionSz * 3), 3); |
| 2942 |
QCOMPARE(view->visualIndexAt(sectionSz * 4), 4); |
| 2943 |
|
| 2944 |
// But making section 0 slightly bigger will cause an index change. |
| 2945 |
view->resizeSection(0, view->sectionSize(0) + 1); |
| 2946 |
QCOMPARE(view->visualIndexAt(sectionSz), 0); |
| 2947 |
QCOMPARE(view->visualIndexAt(sectionSz * 2), 1); |
| 2948 |
QCOMPARE(view->visualIndexAt(sectionSz * 3), 2); |
| 2949 |
QCOMPARE(view->visualIndexAt(sectionSz * 4), 3); |
| 2950 |
} |
| 2951 |
|
| 2952 |
void tst_QHeaderView::hideShowTest() |
| 2953 |
{ |
| 2954 |
additionalInit(); |
| 2955 |
|
| 2956 |
for (int u = model->rowCount(); u >= 0; --u) |
| 2957 |
if (u % 8 != 0) |
| 2958 |
view->hideSection(u); |
| 2959 |
|
| 2960 |
for (int u = model->rowCount(); u >= 0; --u) |
| 2961 |
if (u % 3 == 0) |
| 2962 |
view->showSection(u); |
| 2963 |
|
| 2964 |
QCOMPARE(view->isSectionHidden(6), false); // Set hidden, but shown again |
| 2965 |
QCOMPARE(view->isSectionHidden(7), true); // Just set hidden. |
| 2966 |
QCOMPARE(view->isSectionHidden(24), false); // Not hidden, but shown. |
| 2967 |
|
| 2968 |
// We (currently) expect headers outside the numberof sections not to be hidden |
| 2969 |
view->setSectionHidden(-1, true); // invalid hide (should be ignored) |
| 2970 |
QCOMPARE(view->isSectionHidden(-1), false); |
| 2971 |
const int tooHighCount = model->rowCount(); |
| 2972 |
view->setSectionHidden(tooHighCount, true); // invalid hide (should be ignored) |
| 2973 |
QCOMPARE(view->isSectionHidden(tooHighCount), false); |
| 2974 |
} |
| 2975 |
|
| 2976 |
void tst_QHeaderView::swapSectionsTest() |
| 2977 |
{ |
| 2978 |
additionalInit(); |
| 2979 |
|
| 2980 |
view->swapSections(3, 7); |
| 2981 |
QCOMPARE(view->logicalIndex(3), 7); |
| 2982 |
QCOMPARE(view->visualIndex(3), 7); |
| 2983 |
QCOMPARE(view->logicalIndex(7), 3); |
| 2984 |
QCOMPARE(view->visualIndex(7), 3); |
| 2985 |
} |
| 2986 |
|
| 2987 |
void tst_QHeaderView::moveSectionTest() |
| 2988 |
{ |
| 2989 |
additionalInit(); |
| 2990 |
|
| 2991 |
view->moveSection(3, 7); |
| 2992 |
QCOMPARE(view->logicalIndex(3), 4); |
| 2993 |
QCOMPARE(view->logicalIndex(4), 5); |
| 2994 |
QCOMPARE(view->logicalIndex(5), 6); |
| 2995 |
QCOMPARE(view->logicalIndex(6), 7); |
| 2996 |
QCOMPARE(view->logicalIndex(7), 3); |
| 2997 |
|
| 2998 |
QCOMPARE(view->visualIndex(3), 7); |
| 2999 |
QCOMPARE(view->visualIndex(4), 3); |
| 3000 |
QCOMPARE(view->visualIndex(5), 4); |
| 3001 |
QCOMPARE(view->visualIndex(6), 5); |
| 3002 |
QCOMPARE(view->visualIndex(7), 6); |
| 3003 |
} |
| 3004 |
|
| 3005 |
void tst_QHeaderView::defaultSizeTest() |
| 3006 |
{ |
| 3007 |
additionalInit(); |
| 3008 |
const int specialRow1 = rowcount / 2; |
| 3009 |
const int specialRow2 = 2; |
| 3010 |
const int oldDefaultSectionSize = view->defaultSectionSize(); |
| 3011 |
|
| 3012 |
view->hideSection(specialRow1); |
| 3013 |
const int specialSizeForMiddleRow = oldDefaultSectionSize - 3; |
| 3014 |
view->resizeSection(specialRow2, specialSizeForMiddleRow); |
| 3015 |
|
| 3016 |
int restore_to = view->defaultSectionSize(); |
| 3017 |
const int newDefaultSectionSize = restore_to + 5; |
| 3018 |
view->setDefaultSectionSize(newDefaultSectionSize); |
| 3019 |
|
| 3020 |
QCOMPARE(view->defaultSectionSize(), restore_to + 5); |
| 3021 |
for (int row = 0; row < rowcount; ++row) { |
| 3022 |
if (row != specialRow1 && row != specialRow2) { |
| 3023 |
QCOMPARE(view->sectionSize(row), newDefaultSectionSize); |
| 3024 |
} |
| 3025 |
} |
| 3026 |
|
| 3027 |
const int expectedHeight1 = (view->count() - 2) * newDefaultSectionSize + specialSizeForMiddleRow; |
| 3028 |
QCOMPARE(view->length(), expectedHeight1); |
| 3029 |
|
| 3030 |
QCOMPARE(view->sectionSize(specialRow1), 0); |
| 3031 |
view->showSection(specialRow1); |
| 3032 |
// It could also be argued that this should be newDefaultSectionSize, but this is our current semantics. |
| 3033 |
QCOMPARE(view->sectionSize(specialRow1), oldDefaultSectionSize); |
| 3034 |
|
| 3035 |
const int expectedHeight2 = (view->count() - 2) * newDefaultSectionSize + specialSizeForMiddleRow + oldDefaultSectionSize; |
| 3036 |
QCOMPARE(view->length(), expectedHeight2); |
| 3037 |
} |
| 3038 |
|
| 3039 |
void tst_QHeaderView::removeTest() |
| 3040 |
{ |
| 3041 |
additionalInit(); |
| 3042 |
view->swapSections(0, 5); |
| 3043 |
const int orgCount = view->count(); |
| 3044 |
const int removeCount = 10; |
| 3045 |
model->removeRows(4, removeCount); |
| 3046 |
|
| 3047 |
QCOMPARE(view->count(), orgCount - removeCount); |
| 3048 |
|
| 3049 |
// Before we did first remove the situation was order: |
| 3050 |
// logicalIndex(0) = 5 |
| 3051 |
// [1]-[4] = identity |
| 3052 |
// logicalIndex(5) = 0 |
| 3053 |
|
| 3054 |
// Ater visual order: |
| 3055 |
// logicalIndex(0) = 1 top pos has idx 1 (5 here was removed - and 0 was moved downwards!) |
| 3056 |
// logicalIndex(1) = 2 moved up for same reason |
| 3057 |
// ... |
| 3058 |
// visualIndex(0) = (logidx) 3 - (because row 4 and row 5 was removed) |
| 3059 |
|
| 3060 |
QCOMPARE(view->logicalIndex(5), 5); |
| 3061 |
QCOMPARE(view->visualIndex(5), 5); |
| 3062 |
QCOMPARE(view->logicalIndex(4), 4); |
| 3063 |
QCOMPARE(view->visualIndex(4), 4); |
| 3064 |
|
| 3065 |
QCOMPARE(view->logicalIndex(0), 1); |
| 3066 |
QCOMPARE(view->logicalIndex(1), 2); |
| 3067 |
QCOMPARE(view->logicalIndex(2), 3); |
| 3068 |
QCOMPARE(view->logicalIndex(3), 0); |
| 3069 |
QCOMPARE(view->visualIndex(0), 3); |
| 3070 |
QCOMPARE(view->visualIndex(1), 0); |
| 3071 |
QCOMPARE(view->visualIndex(2), 1); |
| 3072 |
QCOMPARE(view->visualIndex(3), 2); |
| 3073 |
|
| 3074 |
const int newCount = model->rowCount() / 2; |
| 3075 |
model->setRowCount(newCount); |
| 3076 |
|
| 3077 |
QCOMPARE(view->count(), newCount); |
| 3078 |
} |
| 3079 |
|
| 3080 |
void tst_QHeaderView::insertTest() |
| 3081 |
{ |
| 3082 |
additionalInit(); |
| 3083 |
|
| 3084 |
const int oldModelRowCount = model->rowCount(); |
| 3085 |
view->swapSections(0, oldModelRowCount - 1); |
| 3086 |
const int insertCount = 10; |
| 3087 |
model->insertRows(4, insertCount); |
| 3088 |
|
| 3089 |
QCOMPARE(view->logicalIndex(0), oldModelRowCount - 1 + insertCount); |
| 3090 |
QCOMPARE(view->visualIndex(0), oldModelRowCount - 1 + insertCount); |
| 3091 |
QCOMPARE(view->logicalIndex(oldModelRowCount - 1), oldModelRowCount - 1); |
| 3092 |
QCOMPARE(view->visualIndex(oldModelRowCount - 1), oldModelRowCount - 1); |
| 3093 |
QCOMPARE(view->logicalIndex(model->rowCount() - 1), 0); |
| 3094 |
QCOMPARE(view->visualIndex(model->rowCount() - 1), 0); |
| 3095 |
|
| 3096 |
const int newCount = oldModelRowCount + insertCount; |
| 3097 |
QCOMPARE(view->count(), newCount); |
| 3098 |
} |
| 3099 |
|
| 3100 |
|
| 3101 |
void tst_QHeaderView::resizeToContentTest() |
| 3102 |
{ |
| 3103 |
additionalInit(); |
| 3104 |
|
| 3105 |
QModelIndex idx = model->index(2, 2); |
| 3106 |
model->setData(idx, QVariant("A normal string")); |
| 3107 |
|
| 3108 |
idx = model->index(1, 3); |
| 3109 |
model->setData(idx, QVariant("A normal longer string to test resize")); |
| 3110 |
|
| 3111 |
QHeaderView *hh = m_tableview->horizontalHeader(); |
| 3112 |
hh->resizeSections(QHeaderView::ResizeToContents); |
| 3113 |
QCOMPARE_GT(hh->sectionSize(3), hh->sectionSize(2)); |
| 3114 |
|
| 3115 |
for (int u = 0; u < 10; ++u) |
| 3116 |
view->resizeSection(u, 1); |
| 3117 |
|
| 3118 |
view->resizeSections(QHeaderView::ResizeToContents); |
| 3119 |
QCOMPARE_GT(view->sectionSize(1), 1); |
| 3120 |
QCOMPARE_GT(view->sectionSize(2), 1); |
| 3121 |
|
| 3122 |
// Check minimum section size |
| 3123 |
hh->setMinimumSectionSize(150); |
| 3124 |
model->setData(idx, QVariant("i")); |
| 3125 |
hh->resizeSections(QHeaderView::ResizeToContents); |
| 3126 |
QCOMPARE(hh->sectionSize(3), 150); |
| 3127 |
hh->setMinimumSectionSize(-1); |
| 3128 |
|
| 3129 |
// Check maximumSection size |
| 3130 |
hh->setMaximumSectionSize(200); |
| 3131 |
model->setData(idx, QVariant("This is a even longer string that is expected to be more than 200 pixels")); |
| 3132 |
hh->resizeSections(QHeaderView::ResizeToContents); |
| 3133 |
QCOMPARE(hh->sectionSize(3), 200); |
| 3134 |
hh->setMaximumSectionSize(-1); |
| 3135 |
} |
| 3136 |
|
| 3137 |
void tst_QHeaderView::testStreamWithHide() |
| 3138 |
{ |
| 3139 |
#ifndef QT_NO_DATASTREAM |
| 3140 |
m_tableview->setVerticalHeader(view); |
| 3141 |
m_tableview->setModel(model); |
| 3142 |
view->setDefaultSectionSize(25); |
| 3143 |
view->hideSection(2); |
| 3144 |
view->swapSections(1, 2); |
| 3145 |
|
| 3146 |
QByteArray s = view->saveState(); |
| 3147 |
view->swapSections(1, 2); |
| 3148 |
view->showSection(2); |
| 3149 |
view->setDefaultSectionSize(30); |
| 3150 |
|
| 3151 |
view->restoreState(s); |
| 3152 |
QVERIFY(view->isSectionHidden(2)); |
| 3153 |
QVERIFY(!view->isSectionHidden(1)); |
| 3154 |
|
| 3155 |
QCOMPARE(view->logicalIndex(1), 2); |
| 3156 |
QCOMPARE(view->visualIndex(1), 2); |
| 3157 |
QCOMPARE(view->logicalIndex(2), 1); |
| 3158 |
QCOMPARE(view->visualIndex(2), 1); |
| 3159 |
#else |
| 3160 |
QSKIP("Datastream required for testStreamWithHide. Skipping this test."); |
| 3161 |
#endif |
| 3162 |
} |
| 3163 |
|
| 3164 |
void tst_QHeaderView::testStylePosition() |
| 3165 |
{ |
| 3166 |
topLevel->show(); |
| 3167 |
QVERIFY(QTest::qWaitForWindowExposed(topLevel)); |
| 3168 |
|
| 3169 |
TestStyle proxy; |
| 3170 |
view->setStyle(&proxy); |
| 3171 |
|
| 3172 |
QImage image(1, 1, QImage::Format_ARGB32); |
| 3173 |
QPainter p(&image); |
| 3174 |
|
| 3175 |
// 0, 1, 2, 3 |
| 3176 |
view->paintSection(&p, view->rect(), 0); |
| 3177 |
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::Beginning); |
| 3178 |
view->paintSection(&p, view->rect(), 1); |
| 3179 |
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::Middle); |
| 3180 |
view->paintSection(&p, view->rect(), 2); |
| 3181 |
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::Middle); |
| 3182 |
view->paintSection(&p, view->rect(), 3); |
| 3183 |
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::End); |
| 3184 |
|
| 3185 |
// (0),2,1,3 |
| 3186 |
view->setSectionHidden(0, true); |
| 3187 |
view->swapSections(1, 2); |
| 3188 |
view->paintSection(&p, view->rect(), 1); |
| 3189 |
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::Middle); |
| 3190 |
view->paintSection(&p, view->rect(), 2); |
| 3191 |
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::Beginning); |
| 3192 |
view->paintSection(&p, view->rect(), 3); |
| 3193 |
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::End); |
| 3194 |
|
| 3195 |
// (1),2,0,(3) |
| 3196 |
view->setSectionHidden(3, true); |
| 3197 |
view->setSectionHidden(0, false); |
| 3198 |
view->setSectionHidden(1, true); |
| 3199 |
view->swapSections(0, 1); |
| 3200 |
view->paintSection(&p, view->rect(), 0); |
| 3201 |
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::End); |
| 3202 |
view->paintSection(&p, view->rect(), 2); |
| 3203 |
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::Beginning); |
| 3204 |
|
| 3205 |
// (1),2,(0),(3) |
| 3206 |
view->setSectionHidden(0, true); |
| 3207 |
view->paintSection(&p, view->rect(), 2); |
| 3208 |
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::OnlyOneSection); |
| 3209 |
} |
| 3210 |
|
| 3211 |
void tst_QHeaderView::sizeHintCrash() |
| 3212 |
{ |
| 3213 |
QTreeView treeView; |
| 3214 |
QStandardItemModel *model = new QStandardItemModel(&treeView); |
| 3215 |
model->appendRow(new QStandardItem("QTBUG-48543")); |
| 3216 |
treeView.setModel(model); |
| 3217 |
treeView.header()->sizeHintForColumn(0); |
| 3218 |
treeView.header()->sizeHintForRow(0); |
| 3219 |
} |
| 3220 |
|
| 3221 |
void tst_QHeaderView::stretchAndRestoreLastSection() |
| 3222 |
{ |
| 3223 |
QStandardItemModel m(10, 10); |
| 3224 |
QTableView tv; |
| 3225 |
tv.setModel(&m); |
| 3226 |
tv.showMaximized(); |
| 3227 |
|
| 3228 |
const int minimumSectionSize = 20; |
| 3229 |
const int defaultSectionSize = 30; |
| 3230 |
const int someOtherSectionSize = 40; |
| 3231 |
const int biggerSizeThanAnySection = 50; |
| 3232 |
|
| 3233 |
QVERIFY(QTest::qWaitForWindowFocused(&tv)); |
| 3234 |
|
| 3235 |
QHeaderView &header = *tv.horizontalHeader(); |
| 3236 |
// set minimum size before resizeSections() is called |
| 3237 |
// which is done inside setStretchLastSection |
| 3238 |
header.setMinimumSectionSize(minimumSectionSize); |
| 3239 |
header.setDefaultSectionSize(defaultSectionSize); |
| 3240 |
header.resizeSection(9, someOtherSectionSize); |
| 3241 |
header.setStretchLastSection(true); |
| 3242 |
|
| 3243 |
// Default last section is larger |
| 3244 |
QCOMPARE(header.sectionSize(8), defaultSectionSize); |
| 3245 |
QTRY_COMPARE_GE(header.sectionSize(9), biggerSizeThanAnySection); |
| 3246 |
|
| 3247 |
// Moving last section away (restore old last section 9 - and make 8 larger) |
| 3248 |
header.swapSections(9, 8); |
| 3249 |
QCOMPARE(header.sectionSize(9), someOtherSectionSize); |
| 3250 |
QCOMPARE_GE(header.sectionSize(8), biggerSizeThanAnySection); |
| 3251 |
|
| 3252 |
// Make section 9 the large one again |
| 3253 |
header.hideSection(8); |
| 3254 |
QCOMPARE_GE(header.sectionSize(9), biggerSizeThanAnySection); |
| 3255 |
|
| 3256 |
// Show section 8 again - and make that one the last one. |
| 3257 |
header.showSection(8); |
| 3258 |
QCOMPARE_GT(header.sectionSize(8), biggerSizeThanAnySection); |
| 3259 |
QCOMPARE(header.sectionSize(9), someOtherSectionSize); |
| 3260 |
|
| 3261 |
// Swap the sections so the logical indexes are equal to visible indexes again. |
| 3262 |
header.moveSection(9, 8); |
| 3263 |
QCOMPARE(header.sectionSize(8), defaultSectionSize); |
| 3264 |
QCOMPARE_GE(header.sectionSize(9), biggerSizeThanAnySection); |
| 3265 |
|
| 3266 |
// Append sections |
| 3267 |
m.setColumnCount(15); |
| 3268 |
QCOMPARE(header.sectionSize(9), someOtherSectionSize); |
| 3269 |
QCOMPARE_GE(header.sectionSize(14), biggerSizeThanAnySection); |
| 3270 |
|
| 3271 |
// Truncate sections (remove sections with the last section) |
| 3272 |
m.setColumnCount(10); |
| 3273 |
QCOMPARE_GE(header.sectionSize(9), biggerSizeThanAnySection); |
| 3274 |
for (int u = 0; u < 9; ++u) |
| 3275 |
QCOMPARE(header.sectionSize(u), defaultSectionSize); |
| 3276 |
|
| 3277 |
// Insert sections |
| 3278 |
m.insertColumns(2, 2); |
| 3279 |
QCOMPARE(header.sectionSize(9), defaultSectionSize); |
| 3280 |
QCOMPARE(header.sectionSize(10), defaultSectionSize); |
| 3281 |
QCOMPARE_GE(header.sectionSize(11), biggerSizeThanAnySection); |
| 3282 |
|
| 3283 |
// Append an extra section and check restore |
| 3284 |
m.setColumnCount(m.columnCount() + 1); |
| 3285 |
QCOMPARE(header.sectionSize(11), someOtherSectionSize); |
| 3286 |
QCOMPARE_GE(header.sectionSize(12), biggerSizeThanAnySection); |
| 3287 |
|
| 3288 |
// Remove some sections but not the last one. |
| 3289 |
m.removeColumns(2, 2); |
| 3290 |
QCOMPARE(header.sectionSize(9), someOtherSectionSize); |
| 3291 |
QCOMPARE_GE(header.sectionSize(10), biggerSizeThanAnySection); |
| 3292 |
for (int u = 0; u < 9; ++u) |
| 3293 |
QCOMPARE(header.sectionSize(u), defaultSectionSize); |
| 3294 |
|
| 3295 |
// Empty the header and start over with some more tests |
| 3296 |
m.setColumnCount(0); |
| 3297 |
m.setColumnCount(10); |
| 3298 |
QCOMPARE_GE(header.sectionSize(9), biggerSizeThanAnySection); |
| 3299 |
|
| 3300 |
// Check resize of the last section |
| 3301 |
header.resizeSection(9, someOtherSectionSize); |
| 3302 |
QCOMPARE_GE(header.sectionSize(9), biggerSizeThanAnySection); // It should still be stretched |
| 3303 |
header.swapSections(9, 8); |
| 3304 |
QCOMPARE(header.sectionSize(9), someOtherSectionSize); |
| 3305 |
|
| 3306 |
// Restore the order |
| 3307 |
header.swapSections(9, 8); |
| 3308 |
QCOMPARE_GE(header.sectionSize(9), biggerSizeThanAnySection); |
| 3309 |
|
| 3310 |
// Hide the last 3 sections and test stretch last section on swap/move |
| 3311 |
// when hidden sections with a larger visual index exists. |
| 3312 |
header.hideSection(7); |
| 3313 |
header.hideSection(8); |
| 3314 |
header.hideSection(9); |
| 3315 |
QCOMPARE_GE(header.sectionSize(6), biggerSizeThanAnySection); |
| 3316 |
header.moveSection(2, 7); |
| 3317 |
QCOMPARE_GE(header.sectionSize(2), biggerSizeThanAnySection); |
| 3318 |
header.swapSections(1, 8); |
| 3319 |
QCOMPARE(header.sectionSize(2), defaultSectionSize); |
| 3320 |
QCOMPARE_GE(header.sectionSize(1), biggerSizeThanAnySection); |
| 3321 |
|
| 3322 |
// Inserting sections 2 |
| 3323 |
m.setColumnCount(0); |
| 3324 |
m.setColumnCount(10); |
| 3325 |
header.resizeSection(1, someOtherSectionSize); |
| 3326 |
header.swapSections(1, 9); |
| 3327 |
m.insertColumns(9, 2); |
| 3328 |
header.swapSections(1, 11); |
| 3329 |
QCOMPARE(header.sectionSize(1), someOtherSectionSize); |
| 3330 |
|
| 3331 |
// Clear and re-add. This triggers a different code path than seColumnCount(0) |
| 3332 |
m.clear(); |
| 3333 |
m.setColumnCount(3); |
| 3334 |
QCOMPARE_GE(header.sectionSize(2), biggerSizeThanAnySection); |
| 3335 |
|
| 3336 |
// Test import/export of the original (not stretched) sectionSize. |
| 3337 |
m.setColumnCount(0); |
| 3338 |
m.setColumnCount(10); |
| 3339 |
header.resizeSection(9, someOtherSectionSize); |
| 3340 |
QCOMPARE_GE(header.sectionSize(9), biggerSizeThanAnySection); |
| 3341 |
QByteArray b = header.saveState(); |
| 3342 |
m.setColumnCount(0); |
| 3343 |
m.setColumnCount(10); |
| 3344 |
header.restoreState(b); |
| 3345 |
header.setStretchLastSection(false); |
| 3346 |
QCOMPARE(header.sectionSize(9), someOtherSectionSize); |
| 3347 |
} |
| 3348 |
|
| 3349 |
void tst_QHeaderView::testMinMaxSectionSize_data() |
| 3350 |
{ |
| 3351 |
QTest::addColumn<bool>("stretchLastSection"); |
| 3352 |
QTest::addRow("stretched") << true; |
| 3353 |
QTest::addRow("not stretched") << false; |
| 3354 |
} |
| 3355 |
|
| 3356 |
void tst_QHeaderView::testMinMaxSectionSize() |
| 3357 |
{ |
| 3358 |
QFETCH(bool, stretchLastSection); |
| 3359 |
|
| 3360 |
QStandardItemModel m(5, 5); |
| 3361 |
QTableView tv; |
| 3362 |
tv.setModel(&m); |
| 3363 |
tv.show(); |
| 3364 |
|
| 3365 |
const int sectionSizeMin = 20; |
| 3366 |
const int sectionSizeMax = 40; |
| 3367 |
const int defaultSectionSize = 30; |
| 3368 |
|
| 3369 |
QVERIFY(QTest::qWaitForWindowExposed(&tv)); |
| 3370 |
|
| 3371 |
QHeaderView &header = *tv.horizontalHeader(); |
| 3372 |
header.setMinimumSectionSize(sectionSizeMin); |
| 3373 |
header.setMaximumSectionSize(sectionSizeMax); |
| 3374 |
// check bounds for default section size |
| 3375 |
header.setDefaultSectionSize(sectionSizeMin - 1); |
| 3376 |
QCOMPARE(header.defaultSectionSize(), sectionSizeMin); |
| 3377 |
header.setDefaultSectionSize(sectionSizeMax + 1); |
| 3378 |
QCOMPARE(header.defaultSectionSize(), sectionSizeMax); |
| 3379 |
|
| 3380 |
header.setDefaultSectionSize(defaultSectionSize); |
| 3381 |
QCOMPARE(header.defaultSectionSize(), defaultSectionSize); |
| 3382 |
header.setStretchLastSection(stretchLastSection); |
| 3383 |
QCOMPARE(header.stretchLastSection(), stretchLastSection); |
| 3384 |
|
| 3385 |
// check defaults |
| 3386 |
QCOMPARE(header.sectionSize(0), defaultSectionSize); |
| 3387 |
QCOMPARE(header.sectionSize(3), defaultSectionSize); |
| 3388 |
|
| 3389 |
// do not go above maxSectionSize |
| 3390 |
header.resizeSection(0, sectionSizeMax + 1); |
| 3391 |
QCOMPARE(header.sectionSize(0), sectionSizeMax); |
| 3392 |
|
| 3393 |
// do not go below minSectionSize |
| 3394 |
header.resizeSection(0, sectionSizeMin - 1); |
| 3395 |
QCOMPARE(header.sectionSize(0), sectionSizeMin); |
| 3396 |
|
| 3397 |
// change section size on max change |
| 3398 |
header.setMinimumSectionSize(sectionSizeMin); |
| 3399 |
header.setMaximumSectionSize(sectionSizeMax); |
| 3400 |
header.resizeSection(0, sectionSizeMax); |
| 3401 |
QCOMPARE(header.sectionSize(0), sectionSizeMax); |
| 3402 |
header.setMaximumSectionSize(defaultSectionSize); |
| 3403 |
QTRY_COMPARE(header.sectionSize(0), defaultSectionSize); |
| 3404 |
|
| 3405 |
// change section size on min change |
| 3406 |
header.setMinimumSectionSize(sectionSizeMin); |
| 3407 |
header.setMaximumSectionSize(sectionSizeMax); |
| 3408 |
header.resizeSection(0, sectionSizeMin); |
| 3409 |
QCOMPARE(header.sectionSize(0), sectionSizeMin); |
| 3410 |
header.setMinimumSectionSize(defaultSectionSize); |
| 3411 |
QTRY_COMPARE(header.sectionSize(0), defaultSectionSize); |
| 3412 |
} |
| 3413 |
|
| 3414 |
void tst_QHeaderView::testResetCachedSizeHint() |
| 3415 |
{ |
| 3416 |
QtTestModel model(10, 10); |
| 3417 |
QTableView tv; |
| 3418 |
tv.setModel(&model); |
| 3419 |
tv.show(); |
| 3420 |
QVERIFY(QTest::qWaitForWindowExposed(&tv)); |
| 3421 |
|
| 3422 |
QSize s1 = tv.horizontalHeader()->sizeHint(); |
| 3423 |
model.setMultiLineHeader(true); |
| 3424 |
QSize s2 = tv.horizontalHeader()->sizeHint(); |
| 3425 |
model.setMultiLineHeader(false); |
| 3426 |
QSize s3 = tv.horizontalHeader()->sizeHint(); |
| 3427 |
QCOMPARE(s1, s3); |
| 3428 |
QCOMPARE_NE(s1, s2); |
| 3429 |
} |
| 3430 |
|
| 3431 |
|
| 3432 |
class StatusTipHeaderView : public QHeaderView |
| 3433 |
{ |
| 3434 |
Q_OBJECT |
| 3435 |
public: |
| 3436 |
using QHeaderView::QHeaderView; |
| 3437 |
QString statusTipText; |
| 3438 |
bool gotStatusTipEvent = false; |
| 3439 |
protected: |
| 3440 |
bool event(QEvent *e) override |
| 3441 |
{ |
| 3442 |
if (e->type() == QEvent::StatusTip) { |
| 3443 |
gotStatusTipEvent = true; |
| 3444 |
statusTipText = static_cast<QStatusTipEvent *>(e)->tip(); |
| 3445 |
} |
| 3446 |
return QHeaderView::event(e); |
| 3447 |
} |
| 3448 |
}; |
| 3449 |
|
| 3450 |
void tst_QHeaderView::statusTips() |
| 3451 |
{ |
| 3452 |
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) |
| 3453 |
QSKIP("Wayland: This fails. Figure out why."); |
| 3454 |
|
| 3455 |
StatusTipHeaderView headerView(Qt::Horizontal); |
| 3456 |
QtTestModel model(5, 5); |
| 3457 |
headerView.setModel(&model); |
| 3458 |
headerView.viewport()->setMouseTracking(true); |
| 3459 |
headerView.setGeometry(QRect(QPoint(QGuiApplication::primaryScreen()->geometry().center() - QPoint(250, 250)), |
| 3460 |
QSize(500, 500))); |
| 3461 |
headerView.show(); |
| 3462 |
QVERIFY(QTest::qWaitForWindowActive(&headerView)); |
| 3463 |
|
| 3464 |
// Ensure it is moved away first and then moved to the relevant section |
| 3465 |
QTest::mouseMove(&headerView, |
| 3466 |
headerView.rect().bottomLeft() + QPoint(20, 20)); |
| 3467 |
QPoint centerPoint = QRect(headerView.sectionPosition(0), 0, |
| 3468 |
headerView.sectionSize(0), headerView.height()).center(); |
| 3469 |
QTest::mouseMove(headerView.windowHandle(), centerPoint); |
| 3470 |
QTRY_VERIFY(headerView.gotStatusTipEvent); |
| 3471 |
QCOMPARE(headerView.statusTipText, QLatin1String("[0,0,0] -- Header")); |
| 3472 |
|
| 3473 |
headerView.gotStatusTipEvent = false; |
| 3474 |
headerView.statusTipText.clear(); |
| 3475 |
centerPoint = QRect(headerView.sectionPosition(1), 0, |
| 3476 |
headerView.sectionSize(1), headerView.height()).center(); |
| 3477 |
QTest::mouseMove(headerView.windowHandle(), centerPoint); |
| 3478 |
QTRY_VERIFY(headerView.gotStatusTipEvent); |
| 3479 |
QCOMPARE(headerView.statusTipText, QLatin1String("[0,1,0] -- Header")); |
| 3480 |
} |
| 3481 |
|
| 3482 |
void tst_QHeaderView::testRemovingColumnsViaLayoutChanged() |
| 3483 |
{ |
| 3484 |
const int persistentSectionSize = 101; |
| 3485 |
|
| 3486 |
QtTestModel model(5, 5); |
| 3487 |
view->setModel(&model); |
| 3488 |
for (int i = 0; i < model.cols; ++i) |
| 3489 |
view->resizeSection(i, persistentSectionSize + i); |
| 3490 |
model.cleanup(); // down to 3 via layoutChanged (not columnsRemoved) |
| 3491 |
for (int j = 0; j < model.cols; ++j) |
| 3492 |
QCOMPARE(view->sectionSize(j), persistentSectionSize + j); |
| 3493 |
// The main point of this test is that the section-size restoring code didn't go out of bounds. |
| 3494 |
} |
| 3495 |
|
| 3496 |
void tst_QHeaderView::testModelMovingColumns() |
| 3497 |
{ |
| 3498 |
QtTestModel model(10, 10); |
| 3499 |
QHeaderView hv(Qt::Horizontal); |
| 3500 |
hv.setModel(&model); |
| 3501 |
hv.resizeSections(QHeaderView::ResizeToContents); |
| 3502 |
hv.show(); |
| 3503 |
hv.hideSection(3); |
| 3504 |
QVERIFY(!hv.isSectionHidden(1)); |
| 3505 |
QVERIFY(hv.isSectionHidden(3)); |
| 3506 |
|
| 3507 |
QPersistentModelIndex index3 = model.index(0, 3); |
| 3508 |
model.moveColumn(3, 1); |
| 3509 |
QCOMPARE(index3.column(), 1); |
| 3510 |
QVERIFY(hv.isSectionHidden(1)); |
| 3511 |
QVERIFY(!hv.isSectionHidden(3)); |
| 3512 |
} |
| 3513 |
|
| 3514 |
void tst_QHeaderView::testModelMovingRows() |
| 3515 |
{ |
| 3516 |
QtTestModel model(10, 10); |
| 3517 |
QHeaderView hv(Qt::Vertical); |
| 3518 |
hv.setModel(&model); |
| 3519 |
hv.resizeSections(QHeaderView::ResizeToContents); |
| 3520 |
hv.show(); |
| 3521 |
hv.hideSection(3); |
| 3522 |
QVERIFY(!hv.isSectionHidden(1)); |
| 3523 |
QVERIFY(hv.isSectionHidden(3)); |
| 3524 |
|
| 3525 |
QPersistentModelIndex index3 = model.index(3, 0); |
| 3526 |
model.moveRow(3, 1); |
| 3527 |
QCOMPARE(index3.row(), 1); |
| 3528 |
QVERIFY(hv.isSectionHidden(1)); |
| 3529 |
QVERIFY(!hv.isSectionHidden(3)); |
| 3530 |
} |
| 3531 |
|
| 3532 |
// Helpers for memory tests |
| 3533 |
struct BasicModel : public QStandardItemModel |
| 3534 |
{ |
| 3535 |
BasicModel() |
| 3536 |
{ |
| 3537 |
setRowCount(1000); |
| 3538 |
setColumnCount(3); |
| 3539 |
} |
| 3540 |
void resetModel() |
| 3541 |
{ |
| 3542 |
beginResetModel(); |
| 3543 |
endResetModel(); |
| 3544 |
} |
| 3545 |
}; |
| 3546 |
|
| 3547 |
struct TableViewWithBasicModel : public QTableView |
| 3548 |
{ |
| 3549 |
TableViewWithBasicModel() |
| 3550 |
{ |
| 3551 |
emptyState = verticalHeader()->saveState(); |
| 3552 |
setModel(&m); |
| 3553 |
header = verticalHeader(); |
| 3554 |
horizontal_header = horizontalHeader(); |
| 3555 |
} |
| 3556 |
|
| 3557 |
bool hasLowMemoryUsage() const { |
| 3558 |
return emptyState.size() == header->saveState().size(); |
| 3559 |
} |
| 3560 |
|
| 3561 |
bool horizontalLowMememorUsage() const { |
| 3562 |
return emptyState.size() == horizontal_header->saveState().size(); |
| 3563 |
} |
| 3564 |
|
| 3565 |
bool hasHigherMemoryUsage() const { |
| 3566 |
const int delta = 1000; |
| 3567 |
return header->saveState().size() > delta + emptyState.size(); |
| 3568 |
} |
| 3569 |
|
| 3570 |
BasicModel m; |
| 3571 |
QHeaderView *header; |
| 3572 |
QHeaderView *horizontal_header; |
| 3573 |
QByteArray emptyState; |
| 3574 |
}; |
| 3575 |
|
| 3576 |
void tst_QHeaderView::lowMemoryUsageInBasicMode() |
| 3577 |
{ |
| 3578 |
TableViewWithBasicModel tv; |
| 3579 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3580 |
tv.m.setRowCount(10000); |
| 3581 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3582 |
tv.m.setRowCount(500); |
| 3583 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3584 |
tv.m.insertRows(200, 100); |
| 3585 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3586 |
tv.m.removeRows(100, 50); |
| 3587 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3588 |
} |
| 3589 |
|
| 3590 |
void tst_QHeaderView::normalMemoryUsageOnSwap() |
| 3591 |
{ |
| 3592 |
TableViewWithBasicModel tv; |
| 3593 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3594 |
tv.header->swapSections(0, 1); |
| 3595 |
QVERIFY(tv.hasHigherMemoryUsage()); |
| 3596 |
} |
| 3597 |
|
| 3598 |
void tst_QHeaderView::normalMemoryUsageOnMove() |
| 3599 |
{ |
| 3600 |
TableViewWithBasicModel tv; |
| 3601 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3602 |
tv.header->moveSection(2, 7); |
| 3603 |
QVERIFY(tv.hasHigherMemoryUsage()); |
| 3604 |
} |
| 3605 |
|
| 3606 |
void tst_QHeaderView::normalMemoryUsageOnResize() |
| 3607 |
{ |
| 3608 |
TableViewWithBasicModel tv; |
| 3609 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3610 |
tv.header->resizeSection(6, 10); |
| 3611 |
QVERIFY(tv.hasHigherMemoryUsage()); |
| 3612 |
} |
| 3613 |
|
| 3614 |
void tst_QHeaderView::normalMemoryUsageOnHide() |
| 3615 |
{ |
| 3616 |
TableViewWithBasicModel tv; |
| 3617 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3618 |
tv.header->hideSection(9); |
| 3619 |
QVERIFY(tv.hasHigherMemoryUsage()); |
| 3620 |
} |
| 3621 |
|
| 3622 |
void tst_QHeaderView::storeRestoreLowMemoryMode() |
| 3623 |
{ |
| 3624 |
TableViewWithBasicModel tv; |
| 3625 |
QByteArray stateLowMemory = tv.header->saveState(); |
| 3626 |
tv.header->swapSections(0, 1); |
| 3627 |
tv.header->swapSections(0, 1); |
| 3628 |
QByteArray stateNormalMemoryUsage = tv.header->saveState(); |
| 3629 |
|
| 3630 |
QVERIFY(!tv.hasLowMemoryUsage()); |
| 3631 |
QVERIFY(tv.header->restoreState(stateLowMemory)); |
| 3632 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3633 |
|
| 3634 |
QVERIFY(tv.header->restoreState(stateNormalMemoryUsage)); |
| 3635 |
QVERIFY(tv.hasHigherMemoryUsage()); |
| 3636 |
} |
| 3637 |
|
| 3638 |
void tst_QHeaderView::lowMememoryUsageOnSetDefaultSectionSize() |
| 3639 |
{ |
| 3640 |
TableViewWithBasicModel tv; |
| 3641 |
tv.header->setMinimumSectionSize(10); |
| 3642 |
tv.header->setMaximumSectionSize(30); |
| 3643 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3644 |
tv.header->setDefaultSectionSize(12); |
| 3645 |
const int oldLength = tv.header->length(); |
| 3646 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3647 |
tv.header->setDefaultSectionSize(18); |
| 3648 |
const int newLength = tv.header->length(); |
| 3649 |
QCOMPARE_GT(newLength, oldLength); // It has grown |
| 3650 |
QVERIFY(tv.hasLowMemoryUsage()); |
| 3651 |
} |
| 3652 |
|
| 3653 |
void tst_QHeaderView::setSectionResizeModeWithSectionWillTakeMemory() |
| 3654 |
{ |
| 3655 |
// The global default is only a default for new sections |
| 3656 |
// (and working fine in no memory usage mode when it hasn't been specified) |
| 3657 |
// A (forced) resize mode on a section requires memory for keeping the information. |
| 3658 |
TableViewWithBasicModel tv1; |
| 3659 |
tv1.header->setSectionResizeMode(3, QHeaderView::Fixed); |
| 3660 |
QVERIFY(!tv1.hasLowMemoryUsage()); |
| 3661 |
|
| 3662 |
TableViewWithBasicModel tv2; |
| 3663 |
tv2.header->setSectionResizeMode(500, QHeaderView::Interactive); |
| 3664 |
QVERIFY(!tv2.hasLowMemoryUsage()); |
| 3665 |
} |
| 3666 |
|
| 3667 |
class SpecialResizeModeTestDelegate : public QStyledItemDelegate |
| 3668 |
{ |
| 3669 |
Q_OBJECT |
| 3670 |
|
| 3671 |
public: |
| 3672 |
SpecialResizeModeTestDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) |
| 3673 |
{} |
| 3674 |
QSize sizeHint(const QStyleOptionViewItem &/*option*/, const QModelIndex &/*index*/) const override |
| 3675 |
{ |
| 3676 |
return QSize(m_cellWidth, m_cellWidth); |
| 3677 |
} |
| 3678 |
int cellWidth() const { return m_cellWidth; } |
| 3679 |
void setCellWidth(int width) { m_cellWidth = width; } |
| 3680 |
|
| 3681 |
private: |
| 3682 |
int m_cellWidth{100}; |
| 3683 |
}; |
| 3684 |
|
| 3685 |
class SpecialResizeModeTestModel : public QAbstractTableModel |
| 3686 |
{ |
| 3687 |
public: |
| 3688 |
SpecialResizeModeTestModel(QObject *parent = nullptr) : QAbstractTableModel(parent) {} |
| 3689 |
int rowCount(const QModelIndex & = {}) const override { return 105; } |
| 3690 |
int columnCount(const QModelIndex & = {}) const override { return 15; } |
| 3691 |
|
| 3692 |
QVariant data(const QModelIndex &i, int role) const override |
| 3693 |
{ |
| 3694 |
return (role == Qt::DisplayRole) ? QString("R: %1, C: %2").arg(i.row()).arg(i.column()) : QVariant(); |
| 3695 |
} |
| 3696 |
|
| 3697 |
QVariant headerData(int /*section*/, Qt::Orientation /*orientation*/, int role = Qt::DisplayRole) const override |
| 3698 |
{ |
| 3699 |
return (role == Qt::SizeHintRole) ? QSize(1, 1) : QVariant(); |
| 3700 |
} |
| 3701 |
}; |
| 3702 |
|
| 3703 |
// Custom table view that sets the cell sizes based on a property |
| 3704 |
class SpecialResizeModeTableView : public QTableView |
| 3705 |
{ |
| 3706 |
Q_OBJECT |
| 3707 |
public: |
| 3708 |
SpecialResizeModeTableView(QWidget *parent = nullptr) : QTableView(parent) |
| 3709 |
{ |
| 3710 |
QHeaderView *hHeader = horizontalHeader(); |
| 3711 |
QHeaderView *vHeader = verticalHeader(); |
| 3712 |
// Hide the headers, otherwise it appears their size will be used |
| 3713 |
hHeader->hide(); |
| 3714 |
vHeader->hide(); |
| 3715 |
hHeader->setSectionResizeMode(QHeaderView::ResizeToContents); |
| 3716 |
vHeader->setSectionResizeMode(QHeaderView::ResizeToContents); |
| 3717 |
hHeader->setMinimumSectionSize(1); |
| 3718 |
vHeader->setMinimumSectionSize(1); |
| 3719 |
|
| 3720 |
setItemDelegate(&delegate_); |
| 3721 |
} |
| 3722 |
|
| 3723 |
int cellWidth() const { return delegate_.cellWidth(); } |
| 3724 |
void setCellWidth(int width) |
| 3725 |
{ |
| 3726 |
delegate_.setCellWidth(width); |
| 3727 |
scheduleDelayedItemsLayout(); |
| 3728 |
} |
| 3729 |
|
| 3730 |
// The following are overridden for optimization purposes but not relevant to the example |
| 3731 |
int sizeHintForRow(int) const override { return cellWidth(); } |
| 3732 |
int sizeHintForColumn(int) const override { return cellWidth(); } |
| 3733 |
|
| 3734 |
QSize sizeHint() const override |
| 3735 |
{ |
| 3736 |
return QSize(720, 480); // Fixed size for the example |
| 3737 |
} |
| 3738 |
|
| 3739 |
private: |
| 3740 |
SpecialResizeModeTestDelegate delegate_; |
| 3741 |
}; |
| 3742 |
|
| 3743 |
void tst_QHeaderView::setModelWithAutoSizeWillSwitchToMemoryMode() |
| 3744 |
{ |
| 3745 |
SpecialResizeModeTableView v; |
| 3746 |
const int defaultWidth = 20; |
| 3747 |
|
| 3748 |
QByteArray emptyState = v.horizontalHeader()->saveState(); |
| 3749 |
v.horizontalHeader()->setDefaultSectionSize(defaultWidth); |
| 3750 |
v.horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); |
| 3751 |
|
| 3752 |
v.show(); |
| 3753 |
QVERIFY(QTest::qWaitForWindowExposed(&v)); |
| 3754 |
|
| 3755 |
auto *model = new SpecialResizeModeTestModel(&v); |
| 3756 |
v.setModel(model); |
| 3757 |
const int headerLength = v.horizontalHeader()->length(); |
| 3758 |
const int unexpectedLength = defaultWidth * model->columnCount(); |
| 3759 |
// The length of the header is not the default section size times sections. |
| 3760 |
// If it fails we like to see this. |
| 3761 |
QVERIFY(headerLength != unexpectedLength); |
| 3762 |
// A secondary test is that obviously the header length is the bigger one. |
| 3763 |
QCOMPARE_GT(headerLength, unexpectedLength); |
| 3764 |
// and finally we should have switched memory model. |
| 3765 |
QByteArray nonEmptyState = v.horizontalHeader()->saveState(); |
| 3766 |
const int delta = model->columnCount() * 8; |
| 3767 |
// even with delta help the nonEmptyState should now be bigger. |
| 3768 |
QCOMPARE_GT(nonEmptyState.size(), emptyState.size() + delta); |
| 3769 |
} |
| 3770 |
|
| 3771 |
void tst_QHeaderView::tableViewResizeSectionsWillSwitchToMemoryMode() |
| 3772 |
{ |
| 3773 |
TableViewWithBasicModel tv; |
| 3774 |
QVERIFY(tv.horizontalLowMememorUsage()); |
| 3775 |
tv.horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed); |
| 3776 |
tv.resizeColumnsToContents(); |
| 3777 |
QVERIFY(!tv.horizontalLowMememorUsage()); |
| 3778 |
} |
| 3779 |
|
| 3780 |
void tst_QHeaderView::setDefaultSectionSizeRespectsColumnWidth() |
| 3781 |
{ |
| 3782 |
QTreeWidget tree; |
| 3783 |
tree.setHeaderItem(new QTreeWidgetItem({"Col 0", "Col 1", "Col 2"})); |
| 3784 |
tree.header()->setStretchLastSection(false); |
| 3785 |
|
| 3786 |
int columnWidths[3] = {}; |
| 3787 |
for (int c = tree.columnCount() - 1; c >= 0; --c) { |
| 3788 |
columnWidths[c] = tree.columnWidth(c) * 2; |
| 3789 |
tree.setColumnWidth(c, columnWidths[c]); |
| 3790 |
} |
| 3791 |
tree.show(); |
| 3792 |
QVERIFY(QTest::qWaitForWindowExposed(&tree)); |
| 3793 |
|
| 3794 |
for (int c = 0; c < tree.columnCount(); ++c) |
| 3795 |
QTRY_COMPARE(tree.columnWidth(c), columnWidths[c]); |
| 3796 |
|
| 3797 |
// trigger a style change event |
| 3798 |
tree.setStyleSheet("QTreeView { qproperty-headerHidden: true }"); |
| 3799 |
QTRY_COMPARE(tree.isHeaderHidden(), true); |
| 3800 |
tree.setStyleSheet("QTreeView { qproperty-headerHidden: false }"); |
| 3801 |
QTRY_COMPARE(tree.isHeaderHidden(), false); |
| 3802 |
|
| 3803 |
for (int c = 0; c < tree.columnCount(); ++c) |
| 3804 |
QTRY_COMPARE(tree.columnWidth(c), columnWidths[c]); |
| 3805 |
} |
| 3806 |
|
| 3807 |
QTEST_MAIN(tst_QHeaderView) |
| 3808 |
#include "tst_qheaderview.moc" |