Minsky
selection.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2019
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "cairoItems.h"
21 #include "selection.h"
22 #include "lasso.h"
23 #include "plotWidget.h"
24 #include "selection.rcd"
25 #include "minsky_epilogue.h"
26 using namespace std;
27 
28 namespace minsky
29 {
30  void Selection::clear()
31  {
32  for (auto& i: items) i->selected=false;
33  for (auto& i: groups) i->selected=false;
34  Group::clear();
35  }
36 
37  void Selection::toggleItemMembership(const ItemPtr& item)
38  {
39  if (!item) return;
40  if (removeItem(*item))
41  {
42  item->selected=false;
43  item->removeControlledItems(*this);
44  }
45  else if (auto gPtr=std::dynamic_pointer_cast<Group>(item))
46  {
47  auto it=find(groups.begin(), groups.end(),gPtr);
48  if (it!=groups.end())
49  {
50  groups.erase(it);
51  gPtr->selected=false;
52  }
53  else
54  insertGroup(gPtr);
55  }
56  else
57  insertItem(item);
58  }
59 
60  void Selection::ensureItemInserted(const ItemPtr& item)
61  {
62  if (!item) return; //nothing to do
63  if (auto g=dynamic_pointer_cast<Group>(item))
64  {
65  ensureGroupInserted(g);
66  return;
67  }
68  auto i=find(items.begin(), items.end(), item);
69  if (i==items.end())
70  insertItem(item);
71  }
72 
73  void Selection::ensureGroupInserted(const GroupPtr& item)
74  {
75  if (!item) return; //nothing to do
76  auto i=find(groups.begin(), groups.end(), item);
77  if (i==groups.end())
78  insertGroup(item);
79  }
80 
81  void Selection::insertItem(const ItemPtr& item)
82  {
83  items.push_back(item);
84  item->insertControlled(*this);
85  item->selected=true;
86  // insert any attached wires that connect to already selected items
87  if (auto g=item->group.lock())
88  for (size_t i=0; i<item->portsSize(); ++i)
89  {
90  auto p=item->ports(i).lock();
91  for (auto w: p->wires())
92  {
93  auto& other_end=p->input()? w->from()->item(): w->to()->item();
94  if (find_if(items.begin(), items.end(),
95  [&](const ItemPtr& i) {return i.get()==&other_end;})
96  !=items.end())
97  wires.push_back(g->findWire(*w));
98  }
99  }
100  }
101 
102  bool Selection::contains(const ItemPtr& item) const
103  {
104  if (!item) return false;
105  if (auto g=std::dynamic_pointer_cast<Group>(item))
106  {
107  if (find(groups.begin(), groups.end(), g)!=groups.end())
108  return true;
109  }
110  else if (find(items.begin(), items.end(), item)!=items.end())
111  return true;
112  // at this point, we need to check if item is contained in any of
113  // the selected groups
114  if (auto gi=item->group.lock())
115  for (auto& g: groups)
116  if (g==gi || g->higher(*gi))
117  return true;
118  return false;
119  }
120 
121 }
122 
represents items that have been selected
Definition: selection.h:32
STL namespace.
std::shared_ptr< Item > ItemPtr
Definition: item.h:57
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
CLASSDESC_ACCESS_EXPLICIT_INSTANTIATION(minsky::Selection)
std::shared_ptr< Group > GroupPtr
Definition: port.h:32