ScenarioToolState.hpp
1 #pragma once
2 #include <Scenario/Document/ScenarioDocument/ScenarioDocumentViewConstants.hpp>
3 #include <Scenario/Palette/ScenarioPaletteBaseTransitions.hpp>
4 #include <Scenario/Palette/ScenarioPoint.hpp>
5 #include <Scenario/Palette/Tools/ObjectMapper.hpp>
6 
7 #include <score/model/Identifier.hpp>
8 #include <score/statemachine/GraphicsSceneTool.hpp>
9 
10 #include <QGraphicsItem>
11 
12 #include <chrono>
13 
14 namespace score
15 {
16 class Command;
17 }
18 
19 template <typename Element>
20 bool isUnderMouse(Element ev, const QPointF& scenePos)
21 {
22  return ev->mapRectToScene(ev->boundingRect()).contains(scenePos);
23 }
24 
25 template <typename PresenterContainer, typename IdToIgnore>
26 QList<Id<typename PresenterContainer::model_type>> getCollidingModels(
27  const PresenterContainer& array, const QVector<IdToIgnore>& ids, QPointF scenePt)
28 {
29  using namespace std;
30  QList<Id<typename PresenterContainer::model_type>> colliding;
31 
32  for(const auto& elt : array)
33  {
34  if(!ids.contains(elt.id()) && isUnderMouse(elt.view(), scenePt))
35  {
36  colliding.push_back(elt.model().id());
37  }
38  }
39  // TODO sort the elements according to their Z pos.
40 
41  return colliding;
42 }
43 namespace Scenario
44 {
45 template <typename ToolPalette_T>
46 class ToolBase
47  : public GraphicsSceneTool<Scenario::Point>
48  , public ObjectMapper
49 {
50 public:
51  ToolBase(const ToolPalette_T& palette)
52  : GraphicsSceneTool<Scenario::Point>{palette.scene()}
53  , m_palette{palette}
54  {
55  }
56 
57 protected:
58  template <
59  typename EventFun, typename StateFun, typename TimeSyncFun, typename IntervalFun,
60  typename LeftBraceFun, typename RightBraceFun, typename SlotHandleFun,
61  typename NothingFun>
62  void mapTopItem(
63  const QGraphicsItem* item, StateFun st_fun, EventFun ev_fun, TimeSyncFun tn_fun,
64  IntervalFun cst_fun, LeftBraceFun lbrace_fun, RightBraceFun rbrace_fun,
65  SlotHandleFun handle_fun, NothingFun nothing_fun) const
66  {
67  if(!item)
68  {
69  nothing_fun();
70  return;
71  }
72  auto tryFun = [=](auto fun, const auto& id) {
73  if(id)
74  fun(*id);
75  else
76  nothing_fun();
77  };
78 
79  // Each time :
80  // Check if it is an event / timesync / interval /state
81  // The itemToXXXId methods check that we are in the correct scenario, too.
82  auto parent = &this->m_palette.model();
83 
84  switch(item->type())
85  {
86  case ItemType::Condition:
87  tryFun(ev_fun, itemToConditionId(item, parent));
88  break;
89  case ItemType::Event:
90  tryFun(ev_fun, itemToEventId(item, parent));
91  break;
92 
93  case ItemType::Interval:
94  tryFun(cst_fun, itemToIntervalId(item, parent));
95  break;
96  case ItemType::GraphInterval:
97  tryFun(cst_fun, itemToGraphIntervalId(item, parent));
98  break;
99  case ItemType::IntervalHeader:
100  tryFun(cst_fun, itemToIntervalId(item->parentItem(), parent));
101  break;
102  case ItemType::LeftBrace:
103  tryFun(lbrace_fun, itemToIntervalId(item->parentItem(), parent));
104  break;
105  case ItemType::RightBrace:
106  tryFun(rbrace_fun, itemToIntervalId(item->parentItem(), parent));
107  break;
108 
109  case ItemType::Trigger:
110  tryFun(tn_fun, itemToTriggerId(item, parent));
111  break;
112  case ItemType::TimeSync:
113  tryFun(tn_fun, itemToTimeSyncId(item, parent));
114  break;
115 
116  case ItemType::StateOverlay:
117  tryFun(st_fun, itemToStateId(item->parentItem(), parent));
118  break;
119  case ItemType::State:
120  tryFun(st_fun, itemToStateId(item, parent));
121  break;
122 
123  case ItemType::SlotFooter: {
124  if(auto slot = itemToIntervalFromFooter(item, parent))
125  handle_fun(*slot);
126  else
127  nothing_fun();
128  break;
129  }
130  case ItemType::SlotFooterDelegate: {
131  if(auto slot = itemToIntervalFromFooter(item->parentItem(), parent))
132  handle_fun(*slot);
133  else
134  nothing_fun();
135  break;
136  }
137 
138  default:
139  nothing_fun();
140  break;
141  }
142  }
143 
144  const ToolPalette_T& m_palette;
145 };
146 }
Definition: GraphicsSceneTool.hpp:11
Definition: ScenarioToolState.hpp:49
Main plug-in of score.
Definition: score-plugin-dataflow/Dataflow/PortItem.hpp:14
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: ObjectMapper.hpp:15