iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NetworkCanvas.H
Go to the documentation of this file.
1 /*! @file
2  @author Unknown
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifndef SRC_APPS_NRTDESIGNER_NETWORKCANVAS_H
36 #define SRC_APPS_NRTDESIGNER_NETWORKCANVAS_H
37 
38 #include "Common.H"
39 #include "LoaderLibrary.H"
40 class ConnectionRequest;
41 
46 
47 NRT_BEGIN_UNCHECKED_INCLUDES
48 #include <QGraphicsScene>
49 #include <QGraphicsItem>
50 #include <QtGui/QGraphicsView>
51 #include <QWheelEvent>
52 NRT_END_UNCHECKED_INCLUDES
53 
54 class ConnectionRequest;
55 class ModuleRepBase;
56 class ModuleRep;
57 class NamespaceRep;
58 class BorderRep;
59 class Connection;
60 class QLabel;
61 class Port;
62 
63 // The NetworkCanvas is the central display of nrtDesigner where users can drag
64 // ModuleReps around, make connections, etc..
65 class NetworkCanvas : public QGraphicsView,
68 {
69  Q_OBJECT;
70 
71  public:
72  //! Constructor
73  NetworkCanvas();
74 
75  //! Callback function each time a summary arrives
76  void update(std::shared_ptr<nrt::blackboard::BlackboardFederationSummary const> s);
77 
78  //! Callback function called when new GUI data arrives
79  void onBlackboardGUIdata(std::string const & key, nrt::blackboard::GUIdata const & gd);
80 
81  //! Default size hint
82  QSize sizeHint() const { return QSize(800,800); }
83 
84  //! Return a pointer to our border poster zone if we are in a macro-module, or nullptr if root namespace
85  BorderRep * posterBorderRep() const { return itsPosterBorderRep; }
86  BorderRep * checkerBorderRep() const { return itsCheckerBorderRep; }
87  BorderRep * subscriberBorderRep() const { return itsSubscriberBorderRep; }
88 
89  static QRectF const defaultViewRect;
90 
91  protected:
92  QSize minimumSizeHint() const;
93 
94  //! Zoom the view by using the mouse wheel
95  void wheelEvent(QWheelEvent *event);
96 
97  //! Initiate a Module load when one is dropped in from the ModuleLibrary
98  void dropEvent(QDropEvent * event);
99 
100  //! Accept or reject a Module Load
101  void dragEnterEvent(QDragEnterEvent * event);
102 
103  //! Accept or reject a Module Load
104  void dragMoveEvent(QDragMoveEvent * event);
105 
106  void mouseDoubleClickEvent(QMouseEvent * event);
107 
108  //! Determine if an item was clicked, and emit an itemSelectedSignal
109  void mousePressEvent(QMouseEvent * event);
110 
111  //! If there is a current ConnectionRequest, then call requestConnection()
112  void mouseReleaseEvent(QMouseEvent * event);
113 
114  //! Update the current ConnectionRequest's free end position
115  void mouseMoveEvent(QMouseEvent * event);
116 
117  //! Delete objects when the user his backspace
118  void keyPressEvent(QKeyEvent * event);
119 
120  //! List of all our modules and macro-modules
121  /*! For macro-modules, the RELATIVE namespace name (not full path) is used as UID */
122  std::map<std::string /* moduleUID */, ModuleRepBase *> itsModules;
123  std::vector<Connection *> itsConnections;
124 
125  signals:
126  void connectionAddedSignal(Connection*);
127  void deleteModuleSignal(ModuleRep*);
128  void itemSelectedSignal(QGraphicsItem * item);
129  void keyPressedSignal(QKeyEvent*);
130 
131  void bbSummaryDequeuedSignal(std::shared_ptr<nrt::blackboard::BlackboardFederationSummary const> s);
132 
133  //! Callback from our GUIdata listener, triggered each time new GUI data arrives
134  void guiDataSignal(QString const & key, nrt::blackboard::GUIdata const & gd);
135 
136  public slots:
137  void moduleSelectedSlot(ModuleRepBase * module);
138  void portSelectedSlot(Port * p);
139  void namespaceChangeRequest(QString ns);
140  void exportCurrentNamespaceAsYAML();
141  void exportCurrentNamespaceAsPython();
142 
143  void bbSummaryDequeuedSlot(std::shared_ptr<nrt::blackboard::BlackboardFederationSummary const> s);
144  void guiDataSlot(QString const & key, nrt::blackboard::GUIdata const & gd);
145 
146  //! Send a message to a remote nrtLoader requesting a particular module to be unloaded
147  void removeModuleSlot(ModuleRep * module);
148 
149  void removeConnectionSlot(Connection *);
150 
151  //! Clear any selected item. For example, on runstate change, etc
152  void clearSelectionSlot();
153 
154  //! Get a notification when a module is moved. We just want to trigger a redraw here.
155  void moduleMovedSlot(ModuleRepBase* rep);
156 
157  private:
158  // Allow ConnectionRequest to access our internals
159  friend class ConnectionRequest;
160 
161  mutable boost::shared_mutex itsBFSmtx;
162 
163  nrt::Blackboard & itsBB; //!< Shortcut access to our Blackboard
164 
165  //! Update the Master Blackboard with the position of just one object
166  void sendGUIdataUpdate(ModuleRepBase const * moduleRepBase);
167 
168  //! Create a new unique topic name
169  QString generateAutoTopic();
170 
171  //! Send a request to the ModuleLoader to change the topic(filter) of the given port
172  void requestPortTopicChange(Port* p, QString topi);
173 
174  //! Connect the given Ports by sending messages to the appropriate ModuleLoaders
175  void requestConnection(ConnectionRequest *c);
176 
177  //! Create a border connector
178  void requestBorderConnector(BorderRep *b, ConnectionRequest *c, QString name);
179 
180  //! Create a menu the pops up when the user right clicks on empty canvas
181  void createCanvasRightClickMenu(QMouseEvent* event);
182 
183  //! Create a menu that pops up when the user right clicks on a NamespaceRep
184  void createNamespaceRepRightClickMenu(QMouseEvent* event, NamespaceRep* rep);
185 
186  //! Set the current Namespace to the given path.
187  /*! itsBFSmtx must be write-locked (unique_lock) by the caller! */
188  void setNamespace(std::string const & path);
189 
190  //! Either a valid ConnectionRequest if the user has initiated a drag from a port, or nullptr
191  ConnectionRequest *itsCurrentConnectionRequest;
192 
193  //! The currently viewed Namespace (/-separated list of names, or empty if root)
194  std::string itsCurrentNamespace;
195 
196  //! The most recently received blackboard manager summaries
197  std::map<std::string /* bbuid */, nrt::blackboard::BlackboardManagerSummary> itsBBms;
198 
199  //! A cute breadcrumb-style label that shows the current namespace level, with links to all parent levels
200  QLabel * itsNamespaceLabel;
201 
202  //! A CSS style for itsNamespaceLabel
203  QString itsNSLabelStyle;
204 
205  std::shared_ptr<nrt::blackboard::BlackboardFederationSummary const> itsLatestBFS;
206 
207  BorderRep * itsPosterBorderRep;
208  BorderRep * itsCheckerBorderRep;
209  BorderRep * itsSubscriberBorderRep;
210 
211  Port * findConnectionPort(std::string const & module, std::string const & portname,
212  nrt::blackboard::NamespaceSummary const * nss, std::string const & nsspath);
213 };
214 
215 
216 
217 
218 #endif // SRC_APPS_NRTDESIGNER_NETWORKCANVAS_H