File indexing completed on 2024-04-28 15:27:48

0001 /*
0002     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003     SPDX-FileContributor: Stephen Kelly <stephen@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "modeldumper.h"
0009 
0010 #include <QAbstractItemModel>
0011 
0012 ModelDumper::ModelDumper()
0013 {
0014 }
0015 
0016 static int num;
0017 
0018 void ModelDumper::dumpModel(const QAbstractItemModel *const model, QIODevice *device) const
0019 {
0020     num = 1;
0021 
0022     device->write(dumpLevel(model, QModelIndex(), 1).toLatin1());
0023 }
0024 
0025 QString ModelDumper::dumpModel(const QAbstractItemModel *const model) const
0026 {
0027     num = 1;
0028     return dumpLevel(model, QModelIndex(), 1);
0029 }
0030 
0031 QString ModelDumper::dumpTree(const QAbstractItemModel *const model, const QModelIndex &index) const
0032 {
0033     num = 1;
0034     return dumpLevel(model, index, 1);
0035 }
0036 
0037 QString ModelDumper::dumpTree(const QAbstractItemModel *const model, const QModelIndex &index, int start, int end) const
0038 {
0039     num = 1;
0040     return dumpLevel(model, index, 1, start, end);
0041 }
0042 
0043 void ModelDumper::dumpTree(const QAbstractItemModel *const model, QIODevice *device, const QModelIndex &index) const
0044 {
0045     num = 1;
0046     device->write(dumpLevel(model, index, 1).toLatin1());
0047 }
0048 
0049 void ModelDumper::dumpTree(const QAbstractItemModel *const model, QIODevice *device, const QModelIndex &index, int start, int end) const
0050 {
0051     num = 1;
0052     device->write(dumpLevel(model, index, 1, start, end).toLatin1());
0053 }
0054 
0055 QString ModelDumper::dumpLevel(const QAbstractItemModel *const model, const QModelIndex &parent, int level) const
0056 {
0057     const int rowCount = model->rowCount(parent);
0058     return dumpLevel(model, parent, level, 0, rowCount - 1);
0059 }
0060 
0061 QString ModelDumper::dumpLevel(const QAbstractItemModel *const model, const QModelIndex &parent, int level, int start, int end) const
0062 {
0063     QString lines;
0064     for (int row = start; row <= end; ++row) {
0065         QString line;
0066         line.append("\"");
0067         for (int l = 0; l < level; ++l) {
0068             line.append("- ");
0069         }
0070         line.append(QString::number(num++));
0071         line.append("\"");
0072         line.append("\n");
0073         lines.append(line);
0074         static const int column = 0;
0075         const QModelIndex idx = model->index(row, column, parent);
0076         if (model->hasChildren(idx)) {
0077             lines.append(dumpLevel(model, idx, level + 1));
0078         }
0079     }
0080     return lines;
0081 }