File indexing completed on 2024-09-22 05:16:05

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 "documentlistmodel.hpp"
0010 
0011 // lib
0012 #include "documentstool.hpp"
0013 // Kasten core
0014 #include <Kasten/AbstractModelSynchronizer>
0015 #include <Kasten/AbstractDocument>
0016 // KF
0017 #include <KLocalizedString>
0018 
0019 #include <QIcon>
0020 
0021 namespace Kasten {
0022 
0023 DocumentListModel::DocumentListModel(DocumentsTool* documentsTool, QObject* parent)
0024     : QAbstractTableModel(parent)
0025     , mDocumentsTool(documentsTool)
0026 {
0027     connect(mDocumentsTool, &DocumentsTool::documentsAdded,
0028             this, &DocumentListModel::onDocumentsAdded);
0029     connect(mDocumentsTool, &DocumentsTool::documentsClosing,
0030             this, &DocumentListModel::onDocumentsClosing);
0031     connect(mDocumentsTool, &DocumentsTool::focussedDocumentChanged,
0032             this, &DocumentListModel::onFocussedDocumentChanged);
0033 }
0034 
0035 DocumentListModel::~DocumentListModel() = default;
0036 
0037 int DocumentListModel::rowCount(const QModelIndex& parent) const
0038 {
0039     return (!parent.isValid()) ? mDocumentsTool->documents().size() : 0;
0040 }
0041 
0042 int DocumentListModel::columnCount(const QModelIndex& parent) const
0043 {
0044     return (!parent.isValid()) ? NoOfColumnIds : 0;
0045 }
0046 
0047 QVariant DocumentListModel::data(const QModelIndex& index, int role) const
0048 {
0049     QVariant result;
0050 
0051     if (role == Qt::DisplayRole) {
0052         const int documentIndex = index.row();
0053         const AbstractDocument* document = mDocumentsTool->documents().at(documentIndex);
0054 
0055         const int tableColumn = index.column();
0056         switch (tableColumn)
0057         {
0058         case TitleColumnId:
0059             result = document->title();
0060             break;
0061         default:
0062             ;
0063         }
0064     } else if (role == Qt::DecorationRole) {
0065         const int documentIndex = index.row();
0066         const AbstractDocument* document = mDocumentsTool->documents().at(documentIndex);
0067         const AbstractModelSynchronizer* synchronizer = document ? document->synchronizer() : nullptr;
0068 
0069         const int tableColumn = index.column();
0070         switch (tableColumn)
0071         {
0072         case CurrentColumnId:
0073             if (document == mDocumentsTool->focussedDocument()) {
0074                 result = QIcon::fromTheme(QStringLiteral("arrow-right"));
0075             }
0076             break;
0077         case LocalStateColumnId:
0078             if (synchronizer && synchronizer->localSyncState() == LocalHasChanges) {
0079                 result = QIcon::fromTheme(QStringLiteral("document-save"));
0080             }
0081             break;
0082         case RemoteStateColumnId:
0083             // TODO: use static map, syncState int -> iconname
0084             if (!synchronizer) {
0085                 result = QIcon::fromTheme(QStringLiteral("document-new"));
0086             } else {
0087                 const RemoteSyncState remoteSyncState = synchronizer->remoteSyncState();
0088                 if (remoteSyncState == RemoteHasChanges) {
0089                     result = QIcon::fromTheme(QStringLiteral("document-save"));
0090                 } else if (remoteSyncState == RemoteDeleted) {
0091                     result = QIcon::fromTheme(QStringLiteral("edit-delete"));
0092                 } else if (remoteSyncState == RemoteUnknown) {
0093                     result = QIcon::fromTheme(QStringLiteral("flag-yellow"));
0094                 } else if (remoteSyncState == RemoteUnreachable) {
0095                     result = QIcon::fromTheme(QStringLiteral("network-disconnect"));
0096                 }
0097             }
0098             break;
0099         default:
0100             ;
0101         }
0102     }
0103 
0104     return result;
0105 }
0106 
0107 QVariant DocumentListModel::headerData(int section, Qt::Orientation orientation, int role) const
0108 {
0109     QVariant result;
0110 
0111     if (role == Qt::DisplayRole) {
0112         const QString titel =
0113 //             section == LocalStateColumnId ?  i18nc("@title:column Id of the version",         "Id") :
0114             section == TitleColumnId ?     i18nc("@title:column description of the change", "Title") :
0115             QString();
0116         result = titel;
0117     } else if (role == Qt::ToolTipRole) {
0118         const QString titel =
0119 //             section == LocalStateColumnId ?                i18nc("@info:tooltip","Id of the version") :
0120             section == TitleColumnId ? i18nc("@info:tooltip", "Title of the document") :
0121             QString();
0122         result = titel;
0123     } else {
0124         result = QAbstractTableModel::headerData(section, orientation, role);
0125     }
0126 
0127     return result;
0128 }
0129 
0130 void DocumentListModel::onFocussedDocumentChanged(AbstractDocument* document)
0131 {
0132     Q_UNUSED(document)
0133 
0134     beginResetModel();
0135     endResetModel();
0136 // TODO: store current focused document, then only emit for changed
0137 #if 0
0138     const int oldVersionIndex = mVersionIndex;
0139     mVersionIndex = versionIndex;
0140 
0141     Q_EMIT dataChanged(index(versionIndex, CurrentColumnId), index(versionIndex, CurrentColumnId));
0142     Q_EMIT dataChanged(index(oldVersionIndex, CurrentColumnId), index(oldVersionIndex, CurrentColumnId));
0143 #endif
0144 }
0145 
0146 void DocumentListModel::onDocumentsAdded(const QVector<Kasten::AbstractDocument*>& documents)
0147 {
0148     for (AbstractDocument* document : documents) {
0149         connect(document, &AbstractDocument::synchronizerChanged,
0150                 this, &DocumentListModel::onSynchronizerChanged);
0151         AbstractModelSynchronizer* synchronizer = document->synchronizer();
0152         if (synchronizer) {
0153             connect(synchronizer, &AbstractModelSynchronizer::localSyncStateChanged,
0154                     this, &DocumentListModel::onSyncStatesChanged);
0155             connect(synchronizer, &AbstractModelSynchronizer::remoteSyncStateChanged,
0156                     this, &DocumentListModel::onSyncStatesChanged);
0157         }
0158     }
0159 
0160     // TODO: try to understand how this would be done with {begin,end}{Insert,Remove}Columns
0161     beginResetModel();
0162     endResetModel();
0163 }
0164 
0165 void DocumentListModel::onDocumentsClosing(const QVector<Kasten::AbstractDocument*>& documents)
0166 {
0167     Q_UNUSED(documents)
0168     // TODO: try to understand how this would be done with {begin,end}{Insert,Remove}Columns
0169     beginResetModel();
0170     endResetModel();
0171 }
0172 
0173 void DocumentListModel::onSynchronizerChanged(AbstractModelSynchronizer* synchronizer)
0174 {
0175     // TODO: what about the old synchronizer? assume it is deleted and that way disconnects?
0176     if (synchronizer) {
0177         connect(synchronizer, &AbstractModelSynchronizer::localSyncStateChanged,
0178                 this, &DocumentListModel::onSyncStatesChanged);
0179         connect(synchronizer, &AbstractModelSynchronizer::remoteSyncStateChanged,
0180                 this, &DocumentListModel::onSyncStatesChanged);
0181     }
0182     // TODO: try to understand how this would be done with {begin,end}{Insert,Remove}Columns
0183     beginResetModel();
0184     endResetModel();
0185 }
0186 
0187 void DocumentListModel::onSyncStatesChanged()
0188 {
0189     // TODO: try to understand how this would be done with {begin,end}{Insert,Remove}Columns
0190     beginResetModel();
0191     endResetModel();
0192 }
0193 
0194 }
0195 
0196 #include "moc_documentlistmodel.cpp"