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

0001 // ct_lvtmdl_basetreemodel.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 
0020 #include <ct_lvtmdl_basetreemodel.h>
0021 
0022 #include <ct_lvtmdl_modelhelpers.h>
0023 
0024 #include <QDebug>
0025 #include <QIODevice>
0026 #include <QMimeData>
0027 
0028 namespace Codethink::lvtmdl {
0029 
0030 struct BaseTreeModel::BaseTreeModelPrivate {
0031     std::shared_ptr<lvtclr::ColorManagement> colorManagement;
0032 };
0033 
0034 BaseTreeModel::BaseTreeModel(): d(std::make_unique<BaseTreeModelPrivate>())
0035 {
0036 }
0037 
0038 BaseTreeModel::~BaseTreeModel() noexcept = default;
0039 
0040 void BaseTreeModel::setColorManagement(std::shared_ptr<lvtclr::ColorManagement> colorManagement)
0041 {
0042     assert(colorManagement);
0043     d->colorManagement = std::move(colorManagement);
0044     reload();
0045 }
0046 
0047 bool BaseTreeModel::isLoaded(const QString& identifier, const QModelIndex& parent)
0048 {
0049     constexpr int role = Qt::ItemDataRole::DisplayRole;
0050 
0051     const QModelIndex idx = indexForData({{identifier, role}}, parent);
0052     return idx.isValid();
0053 }
0054 
0055 bool BaseTreeModel::isLoaded(const std::string& identifier, const QModelIndex& parent)
0056 {
0057     return isLoaded(QString::fromStdString(identifier), parent);
0058 }
0059 
0060 QModelIndex BaseTreeModel::indexForData(const std::vector<std::pair<QVariant, int>>& variantToRole,
0061                                         const QModelIndex& parent)
0062 {
0063     QModelIndex p_idx = parent;
0064 
0065     const int size = rowCount(p_idx);
0066     for (int i = 0; i < size; i++) {
0067         const QModelIndex idx = index(i, 0, p_idx);
0068         if (!idx.isValid()) {
0069             return {};
0070         }
0071 
0072         bool found = true;
0073         for (const auto& [this_variant, this_role] : variantToRole) {
0074             // Check first for Qualified Name, then for Name.
0075 
0076             const auto this_role_info = idx.data(this_role);
0077 
0078             // makes cppcheck stop complaining that the value is unused.
0079             (void) this_role_info;
0080 
0081             if (this_role_info != this_variant) {
0082                 found = false;
0083                 continue;
0084             }
0085         }
0086         if (found) {
0087             return idx;
0088         }
0089 
0090         // if we are in a branch, recurse.
0091         if (idx.data(ModelRoles::e_IsBranch).value<bool>()) {
0092             const auto inner_idx = indexForData(variantToRole, idx);
0093             if (inner_idx.isValid()) {
0094                 return inner_idx;
0095             }
0096         }
0097     }
0098 
0099     return {};
0100 }
0101 
0102 QModelIndex BaseTreeModel::indexForData(const std::vector<std::pair<std::string, int>>& strToRole,
0103                                         const QModelIndex& parent)
0104 {
0105     std::vector<std::pair<QVariant, int>> strToRoleMapped;
0106     strToRoleMapped.reserve(strToRole.size());
0107     for (const auto& [str, role] : strToRole) {
0108         strToRoleMapped.emplace_back(QString::fromStdString(str), role);
0109     }
0110 
0111     return indexForData(strToRoleMapped, parent);
0112 }
0113 
0114 Qt::ItemFlags BaseTreeModel::flags(const QModelIndex& index) const
0115 {
0116     Qt::ItemFlags baseFlags = QStandardItemModel::flags(index);
0117     baseFlags &= ~Qt::ItemIsEditable;
0118     return baseFlags;
0119 }
0120 
0121 QStringList BaseTreeModel::mimeTypes() const
0122 {
0123     return {"codevis/qualifiednames"};
0124 }
0125 
0126 QMimeData *BaseTreeModel::mimeData(const QModelIndexList& indexes) const
0127 {
0128     QMimeData *mimeData = new QMimeData();
0129     QByteArray encodedData;
0130 
0131     for (auto& idx : indexes) {
0132         const auto qualName = idx.data(lvtmdl::ModelRoles::e_QualifiedName).toByteArray();
0133         if (!encodedData.isEmpty()) {
0134             encodedData.push_back(";");
0135         }
0136         encodedData.push_back(qualName);
0137     }
0138 
0139     mimeData->setData("codevis/qualifiednames", encodedData);
0140     qDebug() << "encodedData: " << QString(encodedData);
0141     return mimeData;
0142 }
0143 } // end namespace Codethink::lvtmdl