VPTissue Reference Manual
PTreeMenu.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "PTreeMenu.h"
21 
22 #include <QAction>
23 #include <QCloseEvent>
24 #include <QDockWidget>
25 #include <QSignalMapper>
26 
27 #include "PTreeEditorWindow.h"
28 
29 namespace SimShell {
30 namespace Gui {
31 
32 using boost::property_tree::ptree;
33 
34 PTreeMenu::PTreeMenu(QString const& t, QString const& path_prefix, QWidget* parent)
35  : QMenu(t, parent), HasUnsavedChanges("Menu: " + t.toStdString()), m_opened(false), m_path(path_prefix)
36 {
37  m_signal_mapper = new QSignalMapper(this);
38  connect(m_signal_mapper, SIGNAL(mapped(QString const&)), this, SIGNAL(ItemTriggered(QString const&)));
39 
40  AddAllAction();
41 }
42 
44 {
45 }
46 
47 void PTreeMenu::AddAllAction()
48 {
49  m_action_all = new QAction("All...", this);
50  connect(m_action_all, SIGNAL(triggered()), m_signal_mapper, SLOT(map()));
51  m_signal_mapper->setMapping(m_action_all, m_path);
52  addAction(m_action_all);
53 }
54 
56 {
57  m_opened = false;
58 
59  for (auto action : actions()) {
60  m_signal_mapper->removeMappings(action);
61  }
62  clear(); // also deletes action objects
63 
64  AddAllAction();
65 }
66 
68 {
69  return true;
70 }
71 
73 {
74  return true;
75 }
76 
77 bool PTreeMenu::IsOpened() const
78 {
79  return m_opened;
80 }
81 
82 bool PTreeMenu::OpenPTree(ptree const& pt, int depth)
83 {
84  if (SaveAndClose()) {
85  OpenPTreeInternal(pt, depth);
86  m_opened = true;
87  return true;
88  } else {
89  return false;
90  }
91 }
92 
93 void PTreeMenu::OpenPTreeInternal(boost::property_tree::ptree const& pt, int remaining_depth)
94 {
95  addSeparator();
96 
97  // other actions for each subtree
98  if (remaining_depth != 0) {
99  for (auto const & subtree : pt) {
100  if (subtree.second.empty()) {
101  continue; // don't display single items in menu
102  }
103  QString item_name = QString::fromStdString(subtree.first);
104  QString item_path = (m_path == "") ? item_name : m_path + '.' + item_name;
105 
106  if (remaining_depth > 1 || remaining_depth == -1) {
107  PTreeMenu* menu = new PTreeMenu(item_name, item_path, this);
108  menu->OpenPTreeInternal(subtree.second,
109  (remaining_depth == -1) ? -1 : remaining_depth - 1);
110  addMenu(menu);
111 
112  // forward signals all the way up to the root menu
113  connect(menu, SIGNAL(ItemTriggered(QString const&)),
114  this, SIGNAL(ItemTriggered(QString const&)));
115  } else {
116  QAction* action = new QAction(item_name + "...", this);
117  connect(action, SIGNAL(triggered()), m_signal_mapper, SLOT(map()));
118  m_signal_mapper->setMapping(action, item_path);
119  addAction(action);
120  }
121  }
122  }
123 }
124 
125 } // end of namespace Gui
126 } // end of namespace SimShell
virtual bool InternalSave()
Definition: PTreeMenu.cpp:72
virtual ~PTreeMenu()
Destructor does not do a thing, except be virtual.
Definition: PTreeMenu.cpp:43
virtual bool IsOpened() const
Test whether menu is in opened state.
Definition: PTreeMenu.cpp:77
see the online Qt documentation
Interface for PTreeMenu.
virtual bool InternalIsClean() const
Definition: PTreeMenu.cpp:67
virtual void InternalForceClose()
Definition: PTreeMenu.cpp:55
Interface for PTreeEditorWindow.
Abstract class that represents the ability be in closed or opened state, and, if the latter...
see the online Qt documentation
bool OpenPTree(boost::property_tree::ptree const &pt, int depth=-1)
Open a ptree.
Definition: PTreeMenu.cpp:82
bool SaveAndClose()
Try to save this object (and its children) and, if successful, close them all.
PTreeMenu(QString const &title, QString const &path_prefix=QString(), QWidget *parent=0)
Definition: PTreeMenu.cpp:34
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32