File indexing completed on 2025-01-05 05:23:27

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009, 2012 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "bookmarklistmodel.hpp"
0010 
0011 // lib
0012 #include "bookmarkstool.hpp"
0013 // Okteta core
0014 #include <Okteta/Bookmark>
0015 // KF
0016 #include <KLocalizedString>
0017 // Qt
0018 #include <QFontDatabase>
0019 
0020 namespace Kasten {
0021 
0022 BookmarkListModel::BookmarkListModel(BookmarksTool* tool, QObject* parent)
0023     : QAbstractTableModel(parent)
0024     , mTool(tool)
0025     , mFixedFont(QFontDatabase::systemFont(QFontDatabase::FixedFont))
0026 {
0027     mPrintFunction = Okteta::OffsetFormat::printFunction((Okteta::OffsetFormat::Format)tool->offsetCoding());
0028 
0029     connect(mTool, &BookmarksTool::hasBookmarksChanged,
0030             this, &BookmarkListModel::onHasBookmarksChanged);
0031     connect(mTool, &BookmarksTool::bookmarksAdded,
0032             this, qOverload<>(&BookmarkListModel::onBookmarksChanged));
0033     connect(mTool, &BookmarksTool::bookmarksRemoved,
0034             this, qOverload<>(&BookmarkListModel::onBookmarksChanged));
0035     connect(mTool, &BookmarksTool::bookmarksModified,
0036             this, qOverload<const QVector<int>&>(&BookmarkListModel::onBookmarksChanged));
0037     connect(mTool, &BookmarksTool::offsetCodingChanged,
0038             this, &BookmarkListModel::onOffsetCodingChanged);
0039 }
0040 
0041 BookmarkListModel::~BookmarkListModel() = default;
0042 
0043 int BookmarkListModel::rowCount(const QModelIndex& parent) const
0044 {
0045     return (!parent.isValid()) ? mTool->bookmarksCount() : 0;
0046 }
0047 
0048 int BookmarkListModel::columnCount(const QModelIndex& parent) const
0049 {
0050     return (!parent.isValid()) ? NoOfColumnIds : 0;
0051 }
0052 
0053 QVariant BookmarkListModel::data(const QModelIndex& index, int role) const
0054 {
0055     QVariant result;
0056 
0057     switch (role)
0058     {
0059     case Qt::DisplayRole:
0060     {
0061         const int bookmarkIndex = index.row();
0062 
0063         const Okteta::Bookmark& bookmark = mTool->bookmarkAt(bookmarkIndex);
0064 
0065         const int tableColumn = index.column();
0066         switch (tableColumn)
0067         {
0068         case OffsetColumnId:
0069         {
0070             mPrintFunction(mCodedOffset, bookmark.offset());
0071 
0072             result = QString::fromLatin1(mCodedOffset);
0073             break;
0074         }
0075         case TitleColumnId:
0076             result = bookmark.name();
0077             break;
0078         default:
0079             ;
0080         }
0081         break;
0082     }
0083     case Qt::FontRole:
0084     {
0085         const int column = index.column();
0086         if (column == OffsetColumnId) {
0087             result = mFixedFont;
0088         }
0089         break;
0090     }
0091     case Qt::EditRole:
0092     {
0093         const int bookmarkIndex = index.row();
0094 
0095         const int column = index.column();
0096         if (column == TitleColumnId) {
0097             const Okteta::Bookmark& bookmark = mTool->bookmarkAt(bookmarkIndex);
0098             result = bookmark.name();
0099         }
0100         break;
0101     }
0102     default:
0103         break;
0104     }
0105 
0106     return result;
0107 }
0108 
0109 Qt::ItemFlags BookmarkListModel::flags(const QModelIndex& index) const
0110 {
0111     Qt::ItemFlags result = QAbstractTableModel::flags(index);
0112     const int column = index.column();
0113     if (column == TitleColumnId) {
0114         result |= Qt::ItemIsEditable;
0115     }
0116 
0117     return result;
0118 }
0119 
0120 QVariant BookmarkListModel::headerData(int section, Qt::Orientation orientation, int role) const
0121 {
0122     QVariant result;
0123 
0124     if (role == Qt::DisplayRole) {
0125         const QString titel =
0126             section == OffsetColumnId ?  i18nc("@title:column offset of the bookmark",     "Offset") :
0127             section == TitleColumnId ?   i18nc("@title:column title of the bookmark", "Title") :
0128             QString();
0129         result = titel;
0130     } else {
0131         result = QAbstractTableModel::headerData(section, orientation, role);
0132     }
0133 
0134     return result;
0135 }
0136 
0137 bool BookmarkListModel::setData(const QModelIndex& index, const QVariant& value, int role)
0138 {
0139     bool result;
0140 
0141     if (role == Qt::EditRole) {
0142         const int bookmarkIndex = index.row();
0143 
0144         const int column = index.column();
0145         if (column == TitleColumnId) {
0146             mTool->setBookmarkName(bookmarkIndex, value.toString());
0147 //             Q_EMIT dataChanged( index, index );
0148             result = true;
0149         } else {
0150             result = false;
0151         }
0152     } else {
0153         result = QAbstractItemModel::setData(index, value, role);
0154     }
0155 
0156     return result;
0157 }
0158 
0159 const Okteta::Bookmark& BookmarkListModel::bookmark(const QModelIndex& index) const
0160 {
0161     const int bookmarkIndex = index.row();
0162     return mTool->bookmarkAt(bookmarkIndex);
0163 }
0164 
0165 QModelIndex BookmarkListModel::index(const Okteta::Bookmark& bookmark, int column) const
0166 {
0167     QModelIndex result;
0168 
0169     const int indexOfBookmark = mTool->indexOf(bookmark);
0170     if (indexOfBookmark != -1) {
0171         result = createIndex(indexOfBookmark, column);
0172     }
0173 
0174     return result;
0175 }
0176 
0177 void BookmarkListModel::onHasBookmarksChanged(bool hasBookmarks)
0178 {
0179     Q_UNUSED(hasBookmarks)
0180 
0181     beginResetModel();
0182     endResetModel();
0183 }
0184 
0185 void BookmarkListModel::onBookmarksChanged()
0186 {
0187     beginResetModel();
0188     endResetModel();
0189 }
0190 
0191 void BookmarkListModel::onBookmarksChanged(const QVector<int>& bookmarkIndizes)
0192 {
0193     for (int row : bookmarkIndizes) {
0194         Q_EMIT dataChanged(index(row, OffsetColumnId), index(row, TitleColumnId));
0195     }
0196 }
0197 
0198 void BookmarkListModel::onOffsetCodingChanged(int offsetCoding)
0199 {
0200     mPrintFunction = Okteta::OffsetFormat::printFunction((Okteta::OffsetFormat::Format)offsetCoding);
0201     beginResetModel();
0202     endResetModel();
0203 }
0204 
0205 #if 0
0206 void BookmarkListModel::onRevertedToVersionIndex(int versionIndex)
0207 {
0208     if (mVersionIndex == versionIndex) {
0209         return;
0210     }
0211 
0212     const int oldVersionIndex = mVersionIndex;
0213     mVersionIndex = versionIndex;
0214 
0215     Q_EMIT dataChanged(index(versionIndex, CurrentColumnId), index(versionIndex, CurrentColumnId));
0216     Q_EMIT dataChanged(index(oldVersionIndex, CurrentColumnId), index(oldVersionIndex, CurrentColumnId));
0217 }
0218 
0219 void BookmarkListModel::onHeadVersionDataChanged(const DocumentVersionData& versionData)
0220 {
0221     Q_UNUSED(versionData)
0222     const int headVersionIndex = mVersionControl->versionCount() - 1;
0223     Q_EMIT dataChanged(index(headVersionIndex, CurrentColumnId), index(headVersionIndex, ChangeDescriptionColumnId));
0224 }
0225 #endif
0226 
0227 }
0228 
0229 #include "moc_bookmarklistmodel.cpp"