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

0001 // ct_lvtqtc_testing_utils.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_lvtqtc_testing_utils.h>
0021 
0022 using namespace Codethink::lvtqtc;
0023 using namespace Codethink::lvtldr;
0024 using namespace Codethink::lvtclr;
0025 using namespace Codethink::lvtshr;
0026 
0027 namespace Codethink::lvtqtc {
0028 
0029 GraphicsViewWrapperForTesting::GraphicsViewWrapperForTesting(NodeStorage& nodeStorage):
0030     GraphicsView(nodeStorage, projectFileForTesting)
0031 {
0032     setColorManagement(std::make_shared<ColorManagement>(false));
0033 }
0034 
0035 QPoint GraphicsViewWrapperForTesting::getEntityPosition(UniqueId uid) const
0036 {
0037     auto result = QPoint{0, 0};
0038     findEntityAndRun(uid, [&](LakosEntity& entity) {
0039         result = mapFromScene(entity.scenePos());
0040     });
0041     return result;
0042 }
0043 
0044 void GraphicsViewWrapperForTesting::moveEntityTo(UniqueId uid, QPointF newPos) const
0045 {
0046     findEntityAndRun(uid, [&](LakosEntity& entity) {
0047         entity.setPos(newPos);
0048     });
0049 }
0050 
0051 bool GraphicsViewWrapperForTesting::hasEntityWithId(lvtshr::UniqueId uid) const
0052 {
0053     bool found = false;
0054     findEntityAndRun(
0055         uid,
0056         [&](LakosEntity& entity) {
0057             found = true;
0058         },
0059         false);
0060     return found;
0061 }
0062 
0063 bool GraphicsViewWrapperForTesting::hasRelationWithId(lvtshr::UniqueId uidFrom, lvtshr::UniqueId uidTo) const
0064 {
0065     bool found = false;
0066     findRelationAndRun(
0067         uidFrom,
0068         uidTo,
0069         [&](LakosRelation& entity) {
0070             found = true;
0071         },
0072         false);
0073     return found;
0074 }
0075 
0076 int GraphicsViewWrapperForTesting::countEntityChildren(UniqueId uid) const
0077 {
0078     int size = 0;
0079     findEntityAndRun(uid, [&](LakosEntity const& entity) {
0080         size = entity.lakosEntities().size();
0081     });
0082     return size;
0083 }
0084 
0085 void GraphicsViewWrapperForTesting::findEntityAndRun(UniqueId uid,
0086                                                      std::function<void(LakosEntity&)> const& f,
0087                                                      bool assertOnNotFound) const
0088 {
0089     for (const auto& item : items()) { // clazy:exclude=range-loop,range-loop-detach
0090         if (auto *entity = qgraphicsitem_cast<LakosEntity *>(item)) {
0091             if (entity->uniqueId() == uid) {
0092                 f(*entity);
0093                 return;
0094             }
0095         }
0096     }
0097     if (assertOnNotFound) {
0098         assert(false && "Couldn't find entity");
0099     }
0100 }
0101 
0102 void GraphicsViewWrapperForTesting::findRelationAndRun(UniqueId fromUid,
0103                                                        UniqueId toUid,
0104                                                        std::function<void(LakosRelation&)> const& f,
0105                                                        bool assertOnNotFound) const
0106 {
0107     for (const auto& item : items()) { // clazy:exclude=range-loop,range-loop-detach
0108         if (auto *relation = qgraphicsitem_cast<LakosRelation *>(item)) {
0109             if (relation->from()->uniqueId() == fromUid && relation->to()->uniqueId() == toUid) {
0110                 f(*relation);
0111                 return;
0112             }
0113         }
0114     }
0115     if (assertOnNotFound) {
0116         assert(false && "Couldn't find relation");
0117     }
0118 }
0119 
0120 bool mousePressAt(ITool& tool, QPointF pos)
0121 {
0122     auto e = QMouseEvent{QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier};
0123     tool.mousePressEvent(&e);
0124     return e.isAccepted();
0125 }
0126 
0127 bool mouseMoveTo(ITool& tool, QPointF pos)
0128 {
0129     auto e = QMouseEvent{QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier};
0130     tool.mouseMoveEvent(&e);
0131     return e.isAccepted();
0132 }
0133 
0134 bool mouseReleaseAt(ITool& tool, QPointF pos)
0135 {
0136     auto e = QMouseEvent{QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier};
0137     tool.mouseReleaseEvent(&e);
0138     return e.isAccepted();
0139 }
0140 
0141 } // namespace Codethink::lvtqtc