File indexing completed on 2024-05-19 05:42:14

0001 #include <ct_lvtmdl_simpletextmodel.h>
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <QDebug>
0021 #include <QSettings>
0022 
0023 namespace Codethink::lvtmdl {
0024 
0025 struct SimpleTextModel::Private {
0026     QStringList strings;
0027     QString storageGroup;
0028 };
0029 
0030 SimpleTextModel::SimpleTextModel(QObject *parent):
0031     QAbstractListModel(parent), d(std::make_unique<SimpleTextModel::Private>())
0032 {
0033 }
0034 
0035 SimpleTextModel::~SimpleTextModel()
0036 {
0037     saveData();
0038 }
0039 
0040 QStringList SimpleTextModel::stringList() const
0041 {
0042     return d->strings;
0043 }
0044 
0045 void SimpleTextModel::addString(const QString& str)
0046 {
0047     beginInsertRows(QModelIndex(), rowCount(), rowCount());
0048     d->strings.push_back(str);
0049     endInsertRows();
0050 }
0051 
0052 void SimpleTextModel::setStorageGroup(const QString& group)
0053 {
0054     d->storageGroup = group;
0055     loadData();
0056 }
0057 
0058 bool SimpleTextModel::removeRows(int row, int count, const QModelIndex& parent)
0059 {
0060     Q_UNUSED(parent);
0061     beginRemoveRows(QModelIndex(), row, row + count - 1);
0062     for (int i = row; i < row + count; i++) {
0063         d->strings.removeAt(row);
0064     }
0065     endRemoveRows();
0066     return true;
0067 }
0068 
0069 int SimpleTextModel::rowCount(const QModelIndex& idx) const
0070 {
0071     Q_UNUSED(idx);
0072     return d->strings.count();
0073 }
0074 
0075 QVariant SimpleTextModel::data(const QModelIndex& idx, int role) const
0076 {
0077     if (!idx.isValid()) {
0078         return {};
0079     }
0080 
0081     if (role != Qt::DisplayRole) {
0082         return {};
0083     }
0084 
0085     return d->strings[idx.row()];
0086 }
0087 
0088 void SimpleTextModel::saveData()
0089 {
0090     qDebug() << "Saving data";
0091 
0092     if (d->storageGroup.isEmpty()) {
0093         return;
0094     }
0095 
0096     QSettings settings;
0097     settings.beginGroup(d->storageGroup);
0098     settings.setValue("ModelData", d->strings);
0099 }
0100 
0101 void SimpleTextModel::loadData()
0102 {
0103     qDebug() << "Loading data";
0104     if (d->storageGroup.isEmpty()) {
0105         return;
0106     }
0107 
0108     QSettings settings;
0109     settings.beginGroup(d->storageGroup);
0110     beginResetModel();
0111     d->strings = settings.value("ModelData", QStringList()).toStringList();
0112     endResetModel();
0113 }
0114 
0115 } // namespace Codethink::lvtmdl