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

0001 // ct_lvtmdl_namespacetreemodel.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_namespacetreemodel.h>
0021 
0022 #include <ct_lvtmdl_modelhelpers.h>
0023 
0024 namespace Codethink::lvtmdl {
0025 
0026 // --------------------------------------------
0027 // struct NamespaceTreeModelPrivate
0028 // --------------------------------------------
0029 struct NamespaceTreeModel::NamespaceTreeModelPrivate {
0030     std::optional<QString> rootNamespace;
0031     bool hasError = true;
0032     QString error;
0033 };
0034 
0035 // --------------------------------------------
0036 // class NamespaceTreeModel
0037 // --------------------------------------------
0038 NamespaceTreeModel::NamespaceTreeModel(): d(std::make_unique<NamespaceTreeModel::NamespaceTreeModelPrivate>())
0039 {
0040 }
0041 
0042 NamespaceTreeModel::~NamespaceTreeModel() = default;
0043 
0044 void NamespaceTreeModel::setRootNamespace(const std::optional<QString>& rootNamespace)
0045 {
0046     d->rootNamespace = rootNamespace;
0047     reload();
0048 }
0049 
0050 namespace {
0051 
0052 } // namespace
0053 void NamespaceTreeModel::reload()
0054 {
0055     auto *root = invisibleRootItem();
0056     root->removeRows(0, root->rowCount());
0057 
0058     // TODO: Retrieve namespace list from lvtldr
0059     //    Transaction transaction(*session());
0060     //    lvtcdb::CdbUtil::NamespaceDeclarationList cxx_namespaces;
0061     //    if (!d->rootNamespace) {
0062     //        cxx_namespaces =
0063     //            session()->find<lvtcdb::NamespaceDeclaration>().where("parent_id IS
0064     //            NULL").orderBy("name").resultList();
0065     //
0066     //        qDebug() << "Repopulating with global namespace that contains" << cxx_namespaces.size() << "children";
0067     //    } else {
0068     //        // TODO: Communicate errors to the interface.
0069     //        auto root_namespace = find_namespace(session(), d->rootNamespace);
0070     //        if (!root_namespace) {
0071     //            d->error = "Requested namespace does not exist";
0072     //            d->hasError = true;
0073     //            return;
0074     //        }
0075     //
0076     //        auto id = root_namespace.id();
0077     //        cxx_namespaces = session()
0078     //                             ->find<lvtcdb::NamespaceDeclaration>()
0079     //                             .where("parent_id IS ?")
0080     //                             .bind(id)
0081     //                             .orderBy("name")
0082     //                             .resultList();
0083     //
0084     //        qDebug() << "Repopulating with root namespace" << root_namespace->name() << "That contains"
0085     //                 << cxx_namespaces.size() << "children";
0086     //    }
0087     //
0088     //    using NamespacePtr = ptr<lvtcdb::NamespaceDeclaration>;
0089     //    for (const NamespacePtr& cxx_namespace : cxx_namespaces) {
0090     //        auto *item = ModelUtil::createTreeItem(cxx_namespace, NodeType::e_Namespace);
0091     //        fillNamespace(item, cxx_namespace);
0092     //        root->appendRow(item);
0093     //    }
0094     //
0095     //    lvtcdb::CdbUtil::UserDefinedTypeList cxx_classes;
0096     //    cxx_classes = session()
0097     //                      ->find<lvtcdb::UserDefinedType>()
0098     //                      .where("parent_namespace_id IS NULL")
0099     //                      .orderBy("qualified_name")
0100     //                      .resultList();
0101     //
0102     //    using ClassPtr = ptr<lvtcdb::UserDefinedType>;
0103     //    for (const ClassPtr& cxx_class : cxx_classes) {
0104     //        auto *item = ModelUtil::createLeafItem(cxx_class, NodeType::e_Class);
0105     //        root->appendRow(item);
0106     //    }
0107 }
0108 
0109 QVariant NamespaceTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
0110 {
0111     (void) orientation;
0112 
0113     if (section != 0) {
0114         return {};
0115     }
0116     if (role != Qt::DisplayRole) {
0117         return {};
0118     }
0119 
0120     if (d->rootNamespace) {
0121         return *d->rootNamespace;
0122     }
0123     return "Global";
0124 }
0125 
0126 std::optional<QString> NamespaceTreeModel::rootNamespace() const
0127 {
0128     return d->rootNamespace;
0129 }
0130 
0131 QString NamespaceTreeModel::errorString() const
0132 {
0133     return d->error;
0134 }
0135 
0136 bool NamespaceTreeModel::hasError() const
0137 {
0138     return d->hasError;
0139 }
0140 
0141 } // end namespace Codethink::lvtmdl