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

0001 // ct_lvtmdl_historylistmodel.cpp                                         -*-C++-*-
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 #include <ct_lvtmdl_historylistmodel.h>
0020 
0021 #include <QString>
0022 
0023 #include <QDebug>
0024 
0025 namespace Codethink::lvtmdl {
0026 
0027 struct HistoryListModel::Private {
0028     int currentIndex = -1;
0029     int maximumHistory = 10;
0030     std::vector<QString> bookmarkHistory;
0031 };
0032 
0033 HistoryListModel::HistoryListModel(QObject *parent):
0034     QAbstractListModel(parent), d(std::make_unique<HistoryListModel::Private>())
0035 {
0036 }
0037 
0038 HistoryListModel::~HistoryListModel() noexcept = default;
0039 
0040 int HistoryListModel::currentIndex() const
0041 {
0042     return d->currentIndex;
0043 }
0044 
0045 void HistoryListModel::setCurrentIndex(int idx)
0046 {
0047     if (d->currentIndex == idx) {
0048         qDebug() << "Current index is the same, returning";
0049         return;
0050     }
0051     qDebug() << "Current index changed to " << idx;
0052     d->currentIndex = idx;
0053     Q_EMIT currentIndexChanged(idx);
0054 }
0055 
0056 void HistoryListModel::next()
0057 {
0058     if (currentIndex() + 1 < rowCount()) {
0059         qDebug() << "Setting current index to" << currentIndex() + 1;
0060         setCurrentIndex(currentIndex() + 1);
0061     }
0062 }
0063 
0064 void HistoryListModel::previous()
0065 {
0066     if (currentIndex() - 1 >= 0 && rowCount() > 0) {
0067         qDebug() << "Setting current index to" << currentIndex() - 1;
0068         setCurrentIndex(currentIndex() - 1);
0069     }
0070 }
0071 
0072 void HistoryListModel::append(const QString& bookmarkName)
0073 {
0074     qDebug() << "Adding " << bookmarkName << "on the history model";
0075     beginInsertRows({}, rowCount(), rowCount());
0076     d->bookmarkHistory.push_back(bookmarkName);
0077     assert(d->bookmarkHistory.size() < (std::size_t) INT_MAX);
0078     d->currentIndex = static_cast<int>(d->bookmarkHistory.size()) - 1;
0079     endInsertRows();
0080 }
0081 
0082 void HistoryListModel::setMaximumHistory(int maximum)
0083 {
0084     d->maximumHistory = maximum;
0085     if ((int) d->bookmarkHistory.size() > maximum) {
0086         beginResetModel();
0087         d->bookmarkHistory.resize(maximum);
0088         d->bookmarkHistory.shrink_to_fit();
0089         endResetModel();
0090     }
0091 }
0092 
0093 int HistoryListModel::rowCount(const QModelIndex& parent) const
0094 {
0095     Q_UNUSED(parent);
0096     const std::size_t size = d->bookmarkHistory.size();
0097     assert(size <= (std::size_t) INT_MAX);
0098     return static_cast<int>(size);
0099 }
0100 
0101 QVariant HistoryListModel::data(const QModelIndex& idx, int role) const
0102 {
0103     if (role != Qt::DisplayRole) {
0104         return {};
0105     }
0106 
0107     if (!idx.isValid()) {
0108         return {};
0109     }
0110 
0111     if (idx.row() > rowCount()) {
0112         return {};
0113     }
0114 
0115     return d->bookmarkHistory[idx.row()];
0116 }
0117 
0118 QString Codethink::lvtmdl::HistoryListModel::at(int idx) const
0119 {
0120     if (idx >= rowCount()) {
0121         return {};
0122     }
0123     return d->bookmarkHistory[idx];
0124 }
0125 
0126 } // namespace Codethink::lvtmdl