Privacy
An open-source, flexible 3D physical simulation framework
DataBrokerPlotterLib.cpp
Go to the documentation of this file.
1 
7 #include "DataBrokerPlotter.h"
8 #include "DataBrokerPlotterLib.h"
9 #include <mars/utils/misc.h>
10 #include <cstdio>
11 #include <iostream>
12 
13 namespace data_broker_plotter {
14 
15  using namespace mars::cfg_manager;
16 
18  lib_manager::LibInterface(theManager), gui(NULL),
19  cfg(NULL), dataBroker(NULL), num(0) {
20 
21  // setup GUI with default path
22  setupGUI("../resources/");
23  }
24 
25  void DataBrokerPlotterLib::setupGUI(std::string rPath) {
26 
27  if(libManager == NULL) return;
28 
30 
31  if(cfg) {
32  cfgPropertyStruct path;
33  path = cfg->getOrCreateProperty("Preferences", "resources_path", rPath);
34  rPath = path.sValue;
35 
36  path = cfg->getOrCreateProperty("Config", "config_path", string("."));
37  configPath = path.sValue;
38  } else {
39  fprintf(stderr, "******* gui_cfg: couldn't find cfg_manager\n");
40  }
41 
43 
44  if (gui == NULL)
45  return;
46 
48 
49  //path.append("/data_broker_widget/resources/images/data_broker_symbol.png");
50  std::string tmpString = configPath;
51  tmpString.append("/");
52  tmpString.append("DataBrokerPlotter.yaml");
53  if(mars::utils::pathExists(tmpString)) {
54  cfg->loadConfig(tmpString.c_str());
55  }
56  gui->addGenericMenuAction("../DataBroker/DataBrokerPlotter", 1, this, 0);
57 
58  startTimer(20);
59  }
60 
61 
63  if(libManager == NULL) return;
64 
65  std::map<std::string, DataBrokerPlotter*>::iterator itMap;
66 
67  // deleting plot windows existing
68  for(itMap=plotWindows.begin(); itMap!=plotWindows.end(); ++itMap) {
69  gui->removeDockWidget((void*)itMap->second, 0);
70  delete itMap->second;
71  }
72 
73  std::string tmpString = configPath + std::string("/DataBrokerPlotter.yaml");
74  cfg->writeConfig(tmpString.c_str(), "DataBrokerPlotter");
75 
76  if(cfg) libManager->releaseLibrary("cfg_manager");
77  if(gui) libManager->releaseLibrary("main_gui");
78  if(dataBroker) libManager->releaseLibrary("data_broker");
79  fprintf(stderr, "Delete DataBrokerPlotterLib\n");
80  }
81 
82 
83  void DataBrokerPlotterLib::menuAction(int action, bool checked) {
84  (void)checked;
85 
86  if (gui == NULL)
87  return;
88 
89  switch(action) {
90  case 1:
91  {
92  // search for new name
93  std::map<std::string, DataBrokerPlotter*>::iterator it;
94  char text[25];
95  std::string name;
96  do {
97  sprintf(text, "plot%d", ++num);
98  name = std::string(text);
99  it = plotWindows.find(name);
100  } while(it != plotWindows.end());
101  if(it == plotWindows.end()) {
102  DataBrokerPlotter *dataBrokerPlotter;
103  dataBrokerPlotter = new DataBrokerPlotter(this, dataBroker, cfg, name);
104  gui->addDockWidget((void*)dataBrokerPlotter, 0);
105  dataBrokerPlotter->show();
106  plotWindows[name] = dataBrokerPlotter;
107  }
108  }
109  break;
110  }
111  }
112 
113  void DataBrokerPlotterLib::timerEvent(QTimerEvent* event) {
114  (void)event;
115  std::map<std::string, DataBrokerPlotter*>::iterator itMap;
116 
117  // handle delete
118  std::list<DataBrokerPlotter*>::iterator it;
119  for(it=toDelete.begin(); it!=toDelete.end(); ++it) {
120  itMap = plotWindows.find((*it)->getName());
121  if(itMap != plotWindows.end()) {
122  plotWindows.erase(itMap);
123  }
124 
125  gui->removeDockWidget((void*)(*it), 0);
126  delete (*it);
127  }
128  toDelete.clear();
129 
130  // update existing
131  for(itMap=plotWindows.begin(); itMap!=plotWindows.end(); ++itMap) {
132  itMap->second->update();
133  }
134  }
135 
137  toDelete.push_back(plotWindow);
138  }
139 
140 } // end of namespace: data_broker_plotter
141 
virtual void addDockWidget(void *window, int priority=0, int area=0, bool possibleCentralWidget=false)=0
Makes a widget dockable in the main window.
virtual void menuAction(int action, bool checked=false)
Called whenever a menu item is selected.
std::map< std::string, DataBrokerPlotter * > plotWindows
The abstract GuiInterface class provides the menu and docking functionality of the GUI...
Definition: GuiInterface.h:46
DataBrokerPlotterLib(lib_manager::LibManager *theManager)
virtual const cfgPropertyStruct getOrCreateProperty(const std::string &_group, const std::string &_name, bool val, CFGClient *newClient=0)=0
std::list< DataBrokerPlotter * > toDelete
mars::data_broker::DataBrokerInterface * dataBroker
virtual void removeDockWidget(void *window, int priority=0)=0
Removes a widget from the dockables.
void setupGUI(std::string rPath=std::string("."))
CREATE_LIB(data_broker_plotter::DataBrokerPlotterLib)
T * getLibraryAs(const std::string &libName, bool load=false)
Definition: LibManager.hpp:91
virtual void writeConfig(const char *filename, const char *group, const unsigned char saveOption=saveOnClose) const =0
mars::cfg_manager::CFGManagerInterface * cfg
bool pathExists(const std::string &path)
check whether a file or directory exists.
Definition: misc.h:112
virtual void addGenericMenuAction(const std::string &path, int action, MenuInterface *menu, int qtKey=0, const std::string &icon="", bool toolbar=0, int checkable=0)=0
Adds a menu/menu item with its corresponding action.
The interface every DataBroker should implement.
DESTROY_LIB(data_broker_plotter::DataBrokerPlotterLib)
virtual bool loadConfig(const char *filename)=0
ErrorNumber releaseLibrary(const std::string &libName)
Releases a previously acquired library.
Definition: LibManager.cpp:311