| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2016 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the test suite of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 |
** included in the packaging of this file. Please review the following |
| 22 |
** information to ensure the GNU General Public License requirements will |
| 23 |
** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 |
** |
| 25 |
** $QT_END_LICENSE$ |
| 26 |
** |
| 27 |
****************************************************************************/ |
| 28 |
|
| 29 |
#include <qtest.h> |
| 30 |
#include <QDebug> |
| 31 |
#include <QTableView> |
| 32 |
#include <QImage> |
| 33 |
#include <QPainter> |
| 34 |
#include <QHeaderView> |
| 35 |
#include <QStandardItemModel> |
| 36 |
|
| 37 |
class QtTestTableModel: public QAbstractTableModel |
| 38 |
{ |
| 39 |
Q_OBJECT |
| 40 |
|
| 41 |
|
| 42 |
public: |
| 43 |
QtTestTableModel(int rows = 0, int columns = 0, QObject *parent = 0) |
| 44 |
: QAbstractTableModel(parent), |
| 45 |
row_count(rows), |
| 46 |
column_count(columns) {} |
| 47 |
|
| 48 |
int rowCount(const QModelIndex& = QModelIndex()) const { return row_count; } |
| 49 |
int columnCount(const QModelIndex& = QModelIndex()) const { return column_count; } |
| 50 |
bool isEditable(const QModelIndex &) const { return true; } |
| 51 |
|
| 52 |
QVariant data(const QModelIndex &idx, int role) const |
| 53 |
{ |
| 54 |
if (!idx.isValid() || idx.row() >= row_count || idx.column() >= column_count) { |
| 55 |
qWarning() << "Invalid modelIndex [%d,%d,%p]" << idx; |
| 56 |
return QVariant(); |
| 57 |
} |
| 58 |
|
| 59 |
if (role == Qt::DisplayRole || role == Qt::EditRole) |
| 60 |
return QString("[%1,%2,%3]").arg(idx.row()).arg(idx.column()).arg(0); |
| 61 |
|
| 62 |
return QVariant(); |
| 63 |
} |
| 64 |
|
| 65 |
bool insertRows(int start, int count, const QModelIndex &parent = QModelIndex()) |
| 66 |
{ |
| 67 |
if (start < 0 || start > row_count) |
| 68 |
return false; |
| 69 |
|
| 70 |
beginInsertRows(parent, start, start + count - 1); |
| 71 |
row_count += count; |
| 72 |
endInsertRows(); |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
bool removeRows(int start, int count, const QModelIndex &parent = QModelIndex()) |
| 77 |
{ |
| 78 |
if (start < 0 || start >= row_count || row_count < count) |
| 79 |
return false; |
| 80 |
|
| 81 |
beginRemoveRows(parent, start, start + count - 1); |
| 82 |
row_count -= count; |
| 83 |
endRemoveRows(); |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
bool insertColumns(int start, int count, const QModelIndex &parent = QModelIndex()) |
| 88 |
{ |
| 89 |
if (start < 0 || start > column_count) |
| 90 |
return false; |
| 91 |
|
| 92 |
beginInsertColumns(parent, start, start + count - 1); |
| 93 |
column_count += count; |
| 94 |
endInsertColumns(); |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
bool removeColumns(int start, int count, const QModelIndex &parent = QModelIndex()) |
| 99 |
{ |
| 100 |
if (start < 0 || start >= column_count || column_count < count) |
| 101 |
return false; |
| 102 |
|
| 103 |
beginRemoveColumns(parent, start, start + count - 1); |
| 104 |
column_count -= count; |
| 105 |
endRemoveColumns(); |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
int row_count; |
| 110 |
int column_count; |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
class tst_QTableView : public QObject |
| 117 |
{ |
| 118 |
Q_OBJECT |
| 119 |
|
| 120 |
public: |
| 121 |
tst_QTableView(); |
| 122 |
virtual ~tst_QTableView(); |
| 123 |
|
| 124 |
public slots: |
| 125 |
void init(); |
| 126 |
void cleanup(); |
| 127 |
|
| 128 |
private slots: |
| 129 |
void spanInit(); |
| 130 |
void spanDraw(); |
| 131 |
void spanSelectColumn(); |
| 132 |
void spanSelectAll(); |
| 133 |
void rowInsertion_data(); |
| 134 |
void rowInsertion(); |
| 135 |
void rowRemoval_data(); |
| 136 |
void rowRemoval(); |
| 137 |
void columnInsertion_data(); |
| 138 |
void columnInsertion(); |
| 139 |
void columnRemoval_data(); |
| 140 |
void columnRemoval(); |
| 141 |
void sizeHintForColumnWhenHidden(); |
| 142 |
private: |
| 143 |
static inline void spanInit_helper(QTableView *); |
| 144 |
}; |
| 145 |
|
| 146 |
tst_QTableView::tst_QTableView() |
| 147 |
{ |
| 148 |
} |
| 149 |
|
| 150 |
tst_QTableView::~tst_QTableView() |
| 151 |
{ |
| 152 |
} |
| 153 |
|
| 154 |
void tst_QTableView::init() |
| 155 |
{ |
| 156 |
} |
| 157 |
|
| 158 |
void tst_QTableView::cleanup() |
| 159 |
{ |
| 160 |
} |
| 161 |
|
| 162 |
void tst_QTableView::spanInit_helper(QTableView *view) |
| 163 |
{ |
| 164 |
for (int i=0; i < 40; i++) { |
| 165 |
view->setSpan(1+i%2, 1+4*i, 1+i%3, 2); |
| 166 |
} |
| 167 |
|
| 168 |
for (int i=1; i < 40; i++) { |
| 169 |
view->setSpan(6 + i*7, 4, 4, 50); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
void tst_QTableView::spanInit() |
| 174 |
{ |
| 175 |
QtTestTableModel model(500, 500); |
| 176 |
QTableView v; |
| 177 |
v.setModel(&model); |
| 178 |
|
| 179 |
QBENCHMARK { |
| 180 |
spanInit_helper(&v); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
void tst_QTableView::spanDraw() |
| 185 |
{ |
| 186 |
QtTestTableModel model(500, 500); |
| 187 |
QTableView v; |
| 188 |
v.setModel(&model); |
| 189 |
|
| 190 |
spanInit_helper(&v); |
| 191 |
v.show(); |
| 192 |
v.resize(500,500); |
| 193 |
QTest::qWait(30); |
| 194 |
|
| 195 |
QImage image(500, 500, QImage::Format_ARGB32_Premultiplied); |
| 196 |
QPainter painter(&image); |
| 197 |
QBENCHMARK { |
| 198 |
v.render(&painter); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
void tst_QTableView::spanSelectAll() |
| 203 |
{ |
| 204 |
QtTestTableModel model(500, 500); |
| 205 |
QTableView v; |
| 206 |
v.setModel(&model); |
| 207 |
|
| 208 |
spanInit_helper(&v); |
| 209 |
v.show(); |
| 210 |
QTest::qWait(30); |
| 211 |
|
| 212 |
QBENCHMARK { |
| 213 |
v.selectAll(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
void tst_QTableView::spanSelectColumn() |
| 218 |
{ |
| 219 |
QtTestTableModel model(500, 500); |
| 220 |
QTableView v; |
| 221 |
v.setModel(&model); |
| 222 |
|
| 223 |
spanInit_helper(&v); |
| 224 |
v.show(); |
| 225 |
QTest::qWait(30); |
| 226 |
|
| 227 |
QBENCHMARK { |
| 228 |
v.selectColumn(22); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
typedef QVector<QRect> SpanList; |
| 233 |
Q_DECLARE_METATYPE(SpanList) |
| 234 |
|
| 235 |
void spansData() |
| 236 |
{ |
| 237 |
QTest::addColumn<SpanList>("spans"); |
| 238 |
|
| 239 |
QTest::newRow("Without spans") |
| 240 |
<< SpanList(); |
| 241 |
|
| 242 |
QTest::newRow("With spans") |
| 243 |
<< (SpanList() |
| 244 |
<< QRect(0, 1, 1, 2) |
| 245 |
<< QRect(1, 2, 1, 2) |
| 246 |
<< QRect(2, 2, 1, 5) |
| 247 |
<< QRect(2, 8, 1, 2) |
| 248 |
<< QRect(3, 4, 1, 2) |
| 249 |
<< QRect(4, 4, 1, 4) |
| 250 |
<< QRect(5, 6, 1, 3) |
| 251 |
<< QRect(6, 7, 1, 3)); |
| 252 |
} |
| 253 |
|
| 254 |
void tst_QTableView::rowInsertion_data() |
| 255 |
{ |
| 256 |
spansData(); |
| 257 |
} |
| 258 |
|
| 259 |
void tst_QTableView::rowInsertion() |
| 260 |
{ |
| 261 |
QFETCH(SpanList, spans); |
| 262 |
|
| 263 |
QtTestTableModel model(10, 10); |
| 264 |
QTableView view; |
| 265 |
view.setModel(&model); |
| 266 |
|
| 267 |
foreach (QRect span, spans) |
| 268 |
view.setSpan(span.top(), span.left(), span.height(), span.width()); |
| 269 |
view.show(); |
| 270 |
QTest::qWait(50); |
| 271 |
|
| 272 |
QBENCHMARK_ONCE { |
| 273 |
view.model()->insertRows(0, 2); |
| 274 |
view.model()->insertRows(5, 2); |
| 275 |
view.model()->insertRows(8, 2); |
| 276 |
view.model()->insertRows(12, 2); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
void tst_QTableView::rowRemoval_data() |
| 281 |
{ |
| 282 |
spansData(); |
| 283 |
} |
| 284 |
|
| 285 |
void tst_QTableView::rowRemoval() |
| 286 |
{ |
| 287 |
QFETCH(SpanList, spans); |
| 288 |
|
| 289 |
QtTestTableModel model(10, 10); |
| 290 |
QTableView view; |
| 291 |
view.setModel(&model); |
| 292 |
|
| 293 |
foreach (QRect span, spans) |
| 294 |
view.setSpan(span.top(), span.left(), span.height(), span.width()); |
| 295 |
view.show(); |
| 296 |
QTest::qWait(50); |
| 297 |
|
| 298 |
QBENCHMARK_ONCE { |
| 299 |
view.model()->removeRows(3, 3); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
void tst_QTableView::columnInsertion_data() |
| 304 |
{ |
| 305 |
spansData(); |
| 306 |
} |
| 307 |
|
| 308 |
void tst_QTableView::columnInsertion() |
| 309 |
{ |
| 310 |
QFETCH(SpanList, spans); |
| 311 |
|
| 312 |
QtTestTableModel model(10, 10); |
| 313 |
QTableView view; |
| 314 |
view.setModel(&model); |
| 315 |
|
| 316 |
// Same set as for rowInsertion, just swapping columns and rows. |
| 317 |
foreach (QRect span, spans) |
| 318 |
view.setSpan(span.left(), span.top(), span.width(), span.height()); |
| 319 |
view.show(); |
| 320 |
QTest::qWait(50); |
| 321 |
|
| 322 |
QBENCHMARK_ONCE { |
| 323 |
view.model()->insertColumns(0, 2); |
| 324 |
view.model()->insertColumns(5, 2); |
| 325 |
view.model()->insertColumns(8, 2); |
| 326 |
view.model()->insertColumns(12, 2); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
void tst_QTableView::columnRemoval_data() |
| 331 |
{ |
| 332 |
spansData(); |
| 333 |
} |
| 334 |
|
| 335 |
void tst_QTableView::columnRemoval() |
| 336 |
{ |
| 337 |
QFETCH(SpanList, spans); |
| 338 |
|
| 339 |
QtTestTableModel model(10, 10); |
| 340 |
QTableView view; |
| 341 |
view.setModel(&model); |
| 342 |
|
| 343 |
// Same set as for rowRemoval, just swapping columns and rows. |
| 344 |
foreach (QRect span, spans) |
| 345 |
view.setSpan(span.left(), span.top(), span.width(), span.height()); |
| 346 |
view.show(); |
| 347 |
QTest::qWait(50); |
| 348 |
|
| 349 |
QBENCHMARK_ONCE { |
| 350 |
view.model()->removeColumns(3, 3); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
void tst_QTableView::sizeHintForColumnWhenHidden() |
| 355 |
{ |
| 356 |
QTableView view; |
| 357 |
QStandardItemModel model(12500, 6); |
| 358 |
for (int r = 0; r < model.rowCount(); ++r) |
| 359 |
for (int c = 0; c < model.columnCount(); ++c) { |
| 360 |
QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(r).arg(c)); |
| 361 |
model.setItem(r, c, item); |
| 362 |
} |
| 363 |
|
| 364 |
view.horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); |
| 365 |
view.setModel(&model); |
| 366 |
QBENCHMARK_ONCE { |
| 367 |
view.horizontalHeader()->resizeSection(0, 10); // this force resizeSections - on a hidden view. |
| 368 |
} |
| 369 |
|
| 370 |
} |
| 371 |
|
| 372 |
QTEST_MAIN(tst_QTableView) |
| 373 |
#include "tst_qtableview.moc" |