File indexing completed on 2024-11-10 04:40:16

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 #include <QSignalSpy>
0009 #include <QStandardItem>
0010 #include <QString>
0011 
0012 namespace TestModelHelpers
0013 {
0014 // Prepares one row for a QStandardItemModel
0015 inline QList<QStandardItem *> makeStandardItems(const QStringList &texts)
0016 {
0017     QList<QStandardItem *> items;
0018     items.reserve(texts.count());
0019     for (const QString &txt : std::as_const(texts)) {
0020         items << new QStandardItem(txt);
0021     }
0022     return items;
0023 }
0024 
0025 // Extracts a full row from a model as a string
0026 // Works best if every cell contains only one character
0027 inline QString extractRowTexts(QAbstractItemModel *model, int row, const QModelIndex &parent = QModelIndex())
0028 {
0029     QString result;
0030     const int colCount = model->columnCount();
0031     for (int col = 0; col < colCount; ++col) {
0032         const QString txt = model->index(row, col, parent).data().toString();
0033         result += txt.isEmpty() ? QStringLiteral(" ") : txt;
0034     }
0035     return result;
0036 }
0037 
0038 // Extracts all headers
0039 inline QString extractHorizontalHeaderTexts(QAbstractItemModel *model)
0040 {
0041     QString result;
0042     const int colCount = model->columnCount();
0043     for (int col = 0; col < colCount; ++col) {
0044         const QString txt = model->headerData(col, Qt::Horizontal).toString();
0045         result += txt.isEmpty() ? QStringLiteral(" ") : txt;
0046     }
0047     return result;
0048 }
0049 
0050 inline QString rowSpyToText(const QSignalSpy &spy)
0051 {
0052     if (!spy.isValid()) {
0053         return QStringLiteral("THE SIGNALSPY IS INVALID!");
0054     }
0055     QString str;
0056     for (int i = 0; i < spy.count(); ++i) {
0057         str += spy.at(i).at(1).toString() + QLatin1Char(',') + spy.at(i).at(2).toString();
0058         if (i + 1 < spy.count()) {
0059             str += QLatin1Char(';');
0060         }
0061     }
0062     return str;
0063 }
0064 
0065 }