File indexing completed on 2024-04-21 03:56:09

0001 /*
0002     SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003     SPDX-FileContributor: David Faure <david.faure@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef TEST_MODEL_HELPERS_H
0009 #define TEST_MODEL_HELPERS_H
0010 
0011 #include <QSignalSpy>
0012 #include <QStandardItem>
0013 #include <QString>
0014 
0015 namespace TestModelHelpers
0016 {
0017 // Prepares one row for a QStandardItemModel
0018 inline QList<QStandardItem *> makeStandardItems(const QStringList &texts)
0019 {
0020     QList<QStandardItem *> items;
0021     for (const QString &txt : texts) {
0022         items << new QStandardItem(txt);
0023     }
0024     return items;
0025 }
0026 
0027 // Extracts a full row from a model as a string
0028 // Works best if every cell contains only one character
0029 inline QString extractRowTexts(QAbstractItemModel *model, int row, const QModelIndex &parent = QModelIndex())
0030 {
0031     QString result;
0032     const int colCount = model->columnCount();
0033     for (int col = 0; col < colCount; ++col) {
0034         const QString txt = model->index(row, col, parent).data().toString();
0035         result += txt.isEmpty() ? QStringLiteral(" ") : txt;
0036     }
0037     return result;
0038 }
0039 
0040 // Extracts all headers
0041 inline QString extractHorizontalHeaderTexts(QAbstractItemModel *model)
0042 {
0043     QString result;
0044     const int colCount = model->columnCount();
0045     for (int col = 0; col < colCount; ++col) {
0046         const QString txt = model->headerData(col, Qt::Horizontal).toString();
0047         result += txt.isEmpty() ? QStringLiteral(" ") : txt;
0048     }
0049     return result;
0050 }
0051 
0052 inline QString rowSpyToText(const QSignalSpy &spy)
0053 {
0054     if (!spy.isValid()) {
0055         return QStringLiteral("THE SIGNALSPY IS INVALID!");
0056     }
0057     QString str;
0058     for (int i = 0; i < spy.count(); ++i) {
0059         str += spy.at(i).at(1).toString() + QLatin1Char(',') + spy.at(i).at(2).toString();
0060         if (i + 1 < spy.count()) {
0061             str += QLatin1Char(';');
0062         }
0063     }
0064     return str;
0065 }
0066 
0067 }
0068 #endif