LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ParameterSetEdit.cxx
Go to the documentation of this file.
1 #include <iostream>
9 #include <sstream>
10 #include <string>
11 #include <vector>
12 #include <cstdlib>
13 extern "C" {
14 #include <sys/types.h>
15 #include <unistd.h>
16 }
17 #include "TROOT.h"
18 #include "TApplication.h"
19 #include "TGCanvas.h"
20 #include "TGLabel.h"
21 #include "TGWindow.h"
22 #include "TGButton.h"
23 #include "TGTextEntry.h"
24 #include "TVirtualX.h"
25 
28 
29 namespace evdb{
30 
31  static void parse_pset_string(const std::string& pset,
32  std::vector<std::string>& names,
33  std::vector<std::string>& values)
34  {
35  // Parse out the content of the parameter set
36  size_t istart = 0;
37  size_t iend = 0;
38  while (1) {
39  iend = pset.find(' ',istart);
40 
41  std::string param = pset.substr(istart, iend-istart);
42 
43  size_t ieq = param.find(':');
44  if (ieq == param.npos) { abort(); }
45 
46  std::string nm = param.substr(0,ieq);
47  std::string value = param.substr(ieq+1,param.size());
48 
49  names. push_back(nm);
50  values.push_back(value);
51 
52  if (iend==pset.npos) break;
53  istart = iend+1;
54  }
55 
56  }
57 
58 
59  //......................................................................
60 
61  ParamFrame::ParamFrame(const TGWindow* p,
62  std::vector<std::string>& name,
63  std::vector<std::string>& value,
64  std::vector<TGTextEntry*>& fT2)
65  {
66  // Create tile view container. Used to show colormap.
67 
68  fFrame = new TGGroupFrame(p, "Parameters", kVerticalFrame);
69 
70  TGLayoutHints* fLH3 = new TGLayoutHints(kLHintsCenterX|kLHintsExpandX,
71  2,2,2,2);
72 
73  fML = new TGMatrixLayout(fFrame, 0, 2, 2);
74  fFrame->SetLayoutManager(fML);
75  int h=26;
76 
77  for (unsigned int i=0; i<name.size(); ++i) {
78  // skip if the name is module_label, module_type or service_type
79  if((name[i].compare("module_label") == 0) ||
80  (name[i].compare("module_type") == 0) ||
81  (name[i].compare("service_type") == 0)) continue;
82 
83  // Build the parameter label
84  TGTextButton*
85  b = new TGTextButton(fFrame,
86  name[i].c_str(),
87  -1,
88  TGButton::GetDefaultGC()(),
89  TGTextButton::GetDefaultFontStruct(),
90  0);
91  fFrame->AddFrame(b, fLH3);
92 
93  // Build the text edit box for the values
94  TGTextEntry* t = new TGTextEntry(fFrame, value[i].c_str());
95 
96  // Set the size of the edit box
97  t->Resize(225,18);
98  fFrame->AddFrame(t, fLH3);
99  fT2.push_back(t);
100  h += 26;
101  }
102  if (h>30*26) h = 30*26;
103 
104  fFrame->Resize(fFrame->GetWidth(), h);
105 
106  fFrame->Connect("ProcessedEvent(Event_t*)", "evdb::ParamFrame", this,
107  "HandleMouseWheel(Event_t*)");
108  fCanvas = 0;
109 
110  delete fLH3;
111  }
112 
113  //......................................................................
114 
116  {
117  if (fFrame) return fFrame->GetHeight();
118  else return 0;
119  }
120 
121  //......................................................................
122 
124  {
125  if (fFrame) return fFrame->GetWidth();
126  else return 0;
127  }
128 
129  //......................................................................
130 
132  {
133  // Handle mouse wheel to scroll.
134 
135  if (event->fType != kButtonPress && event->fType != kButtonRelease)
136  return;
137 
138  Int_t page = 0;
139  if (event->fCode == kButton4 || event->fCode == kButton5) {
140  if (!fCanvas) return;
141  if (fCanvas->GetContainer()->GetHeight())
142  page = Int_t(Float_t(fCanvas->GetViewPort()->GetHeight() *
143  fCanvas->GetViewPort()->GetHeight()) /
144  fCanvas->GetContainer()->GetHeight());
145  }
146 
147  if (event->fCode == kButton4) {
148  //scroll up
149  Int_t newpos = fCanvas->GetVsbPosition() - page;
150  if (newpos < 0) newpos = 0;
151  fCanvas->SetVsbPosition(newpos);
152  }
153  if (event->fCode == kButton5) {
154  // scroll down
155  Int_t newpos = fCanvas->GetVsbPosition() + page;
156  fCanvas->SetVsbPosition(newpos);
157  }
158  }
159 
160  //......................................................................
161 
163  const std::string& module,
164  const std::string& label,
165  const std::string& pset,
166  std::string* newpset) :
167  TGTransientFrame(gClient->GetRoot(), gClient->GetRoot(), 4, 4),
168  fResult(newpset)
169  {
170  int h = 800;
171  int w = 500;
172 
173  // Convert the parameter set to a list of names, types, and values.
175 
176  fLH1 = new TGLayoutHints(kLHintsLeft|kLHintsExpandX, 2,2,2,2);
177  fLH2 = new TGLayoutHints(kLHintsRight|kLHintsExpandX, 2,2,2,2);
178  fLH3 = new TGLayoutHints(kLHintsCenterX|kLHintsExpandX,2,2,2,2);
179  fLH4 = new TGLayoutHints(kLHintsLeft|kLHintsExpandY, 4,4,4,4);
180 
181  // Add the heading at the top of the window
182  h = 0;
183  fF1 = new TGCompositeFrame(this, w, h, kVerticalFrame);
184  std::ostringstream lbl1;
185  lbl1 << "Module " << module << " - " << label;
186 
187  fL1 = new TGLabel(fF1, lbl1.str().c_str());
188 
189  fF1->AddFrame(fL1,fLH3);
190 
191  fL1->SetHeight(26);
192  this->AddFrame(fF1);
193  h = 30;
194 
195  // Add the parameter fields and edit boxes
196  fCanvas = new TGCanvas(this, w, h);
197  fParam = new ParamFrame(fCanvas->GetViewPort(),
198  fName,
199  fValue,
200  fT2);
202  fCanvas->SetContainer(fParam->GetFrame());
203  fParam->GetFrame()->SetCleanup(kDeepCleanup);
204 
205  for(unsigned int n = 0; n < fT2.size(); ++n){
206  // Pressing enter in a field applies the changes
207  fT2[n]->Connect("ReturnPressed()", "evdb::ParameterSetEdit", this,
208  "Apply()");
209  fT2[n]->Connect("TabPressed()", "evdb::ParameterSetEdit", this,
210  "HandleTab()");
211  }
212 
213  h = fParam->GetHeight();
214  if (h>800) h = 800;
215  fCanvas->Resize(w,h);
216 
217  this->AddFrame(fCanvas);
218 
219  // Button bar across the bottom
220  fF3 = new TGCompositeFrame(this, w, 16, kHorizontalFrame);
221  this->AddFrame(fF3);
222 
223  fB3 = new TGTextButton(fF3, " Apply ");
224  fB4 = new TGTextButton(fF3, " Cancel ");
225  fB5 = new TGTextButton(fF3, " Done ");
226  fF3->AddFrame(fB3, fLH1);
227  fF3->AddFrame(fB4, fLH1);
228  fF3->AddFrame(fB5, fLH1);
229 
230  fB3->Connect("Clicked()","evdb::ParameterSetEdit",this,"Apply()");
231  fB4->Connect("Clicked()","evdb::ParameterSetEdit",this,"Cancel()");
232  fB5->Connect("Clicked()","evdb::ParameterSetEdit",this,"Done()");
233 
234  this->Connect("CloseWindow()","evdb::ParameterSetEdit",this,"CloseWindow()");
235 
236  h += 50;
237 
238  this->Resize(w+8,h);
239  this->MapSubwindows();
240  this->MapWindow();
241 
242  if(!fT2.empty()){
243  // TRy to focus the first text field
244  fT2[0]->SetFocus();
245  fT2[0]->End();
246  }
247 
248  (*fResult) = "";
249  }
250 
251  //......................................................................
252 
254  {
255  unsigned int i;
256  const char* values;
257  std::ostringstream pset;
258 
259  for (i=0; i<fName.size(); ++i) {
260  if(i < fT2.size() ) values = fT2[i]->GetText();
261  else values = fValue[i].c_str();
262  pset << fName[i] << ":" << values << " ";
263  }
264 
265  (*fResult) = pset.str();
266 
267  return 1;
268  }
269 
270  //......................................................................
271 
273  {
274  unsigned int i;
275  delete fB5;
276  delete fB4;
277  delete fB3;
278  for (i=0; i<fT2.size(); ++i) delete fT2[i];
279  delete fL1;
280  delete fF3;
281  delete fF1;
282  delete fLH4;
283  delete fLH3;
284  delete fLH2;
285  delete fLH1;
286  }
287 
288  //......................................................................
289 
290  void ParameterSetEdit::CloseWindow() { delete this; }
291 
292  //......................................................................
293 
295  {
296  this->SendCloseMessage();
297  }
298 
299  //......................................................................
300 
302  {
303  this->Edit();
304  this->SendCloseMessage();
306  }
307 
308  //......................................................................
309 
311  {
312  this->Edit();
314  }
315 
316  //......................................................................
317 
319  {
320  // Work out which text field has focus
321  Window_t focusId = gVirtualX->GetInputFocus();
322  int focusIdx = -1;
323  for(unsigned int n = 0; n < fT2.size(); ++n){
324  if(fT2[n]->GetId() == focusId) focusIdx = n;
325  }
326  // We don't know. Bail out
327  if(focusIdx == -1) return;
328 
329  // Move focus to the next field cyclically
330  ++focusIdx;
331  focusIdx %= fT2.size();
332  fT2[focusIdx]->SetFocus();
333  fT2[focusIdx]->End();
334  }
335 
336 }// namespace
337 
static void Set(int which)
Definition: NavState.cxx:24
int GetHeight() const
TGGroupFrame * GetFrame() const
std::vector< TGTextEntry * > fT2
std::vector< std::string > fName
Manage all things related to colors for the event display.
TGMatrixLayout * fML
static void parse_pset_string(const std::string &pset, std::vector< std::string > &names, std::vector< std::string > &values)
TGCompositeFrame * fF3
void SetCanvas(TGCanvas *canvas)
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
ParamFrame(const TGWindow *p, std::vector< std::string > &names, std::vector< std::string > &value, std::vector< TGTextEntry * > &fT2)
double value
Definition: spectrum.C:18
TGGroupFrame * fFrame
std::vector< std::string > fValue
TGCompositeFrame * fF1
Char_t n[5]
ParameterSetEdit(TGMainFrame *mf, const std::string &module, const std::string &label, const std::string &params, std::string *newpset)
Float_t w
Definition: plot.C:20
Helper class to setup scroll bars in evdb::ParameterSetEdit.
void HandleMouseWheel(Event_t *event)
Event finding and building.