KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVNameValueListGUI.cpp
Go to the documentation of this file.
1 //Created by KVClassFactory on Fri Feb 17 09:32:40 2017
2 //Author: John Frankland,,,
3 
4 #include "KVNameValueListGUI.h"
5 
6 #include <TGLabel.h>
7 #include <TGNumberEntry.h>
8 #include <TGTextEntry.h>
9 #include "TTimer.h"
10 #include "TMath.h"
11 
13 
14 
15 
18 {
19  TGTextEntry* text = new TGTextEntry(hf, theList->GetStringValue(i));
20  max_width = TMath::Max(max_width, text->GetDefaultWidth() + 10);
21  text->Resize(text->GetDefaultWidth(), text->GetDefaultHeight());
22  hf->AddFrame(text, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 3, 2, 1, 1));
23  return text;
24 }
25 
26 
27 
29 
31 {
32  TGCheckButton* but = new TGCheckButton(hf);
33  but->Resize(but->GetDefaultWidth(), 10);
34  hf->AddFrame(but, new TGLayoutHints(kLHintsTop | kLHintsCenterX | kLHintsExpandX, 3, 2, 1, 1));
36  return but;
37 }
38 
39 
40 
42 
44 {
45  TGNumberEntry* num = new TGNumberEntry(hf, theList->GetDoubleValue(i), 10, -1,
50  num->Resize(num->GetDefaultWidth(), num->GetDefaultHeight());
51  hf->AddFrame(num, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 3, 2, 1, 1));
52  return num;
53 }
54 
55 
56 
58 
60 {
61  TGNumberEntry* num = new TGNumberEntry(hf, theList->GetDoubleValue(i), 10, -1,
66  num->Resize(num->GetDefaultWidth(), num->GetDefaultHeight());
67  hf->AddFrame(num, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 3, 2, 1, 1));
68  return num;
69 }
70 
71 
72 
73 
74 
76 
78 {
79  for (int i = 0; i < theList->GetNpar(); ++i) {
81  if (par->IsDouble()) {
82  par->Set(dynamic_cast<TGNumberEntry*>(fData[i])->GetNumber());
83  }
84  else if (par->IsInt()) {
85  par->Set((Int_t)dynamic_cast<TGNumberEntry*>(fData[i])->GetNumber());
86  }
87  else if (par->IsString()) {
88  par->Set(dynamic_cast<TGTextEntry*>(fData[i])->GetText());
89  }
90  else if (par->IsBool()) {
91  par->Set(dynamic_cast<TGCheckButton*>(fData[i])->IsDown());
92  }
93  }
94 }
95 
96 
97 
100 
102 {
103  // return all values to original state
105  *fCancel = kTRUE;
106 }
107 
108 
109 
111 
112 void KVNameValueListGUI::DisplayDialog()
113 {
114  fMain->MapSubwindows();
115 
120  fMain->SetMWMHints(kMWMDecorAll | kMWMDecorResizeH | kMWMDecorMaximize |
121  kMWMDecorMinimize | kMWMDecorMenu,
122  kMWMFuncAll | kMWMFuncResize | kMWMFuncMaximize |
123  kMWMFuncMinimize, kMWMInputModeless);
124 
125  // position relative to the parent's window
128 
129  fMain->MapWindow();
130  if (fWaitForMain) gClient->WaitFor(fMain);
131 }
132 
133 
134 
137 
138 KVNameValueListGUI::KVNameValueListGUI(const TGWindow* main, KVNameValueList* params, Bool_t* cancel_pressed, Bool_t wait_for_main)
139  : fData(params->GetNpar()), fWaitForMain(wait_for_main)
140 {
141  // Constructor
142  fMain = new TGTransientFrame(gClient->GetDefaultRoot(), main, 1, 1, kVerticalFrame);
144  fMain->Connect("CloseWindow()", "KVNameValueListGUI", this, "DoClose()");
145  fMain->DontCallClose(); // to avoid double deletions.
146  // use hierarchical cleaning
148 
149  fCancel = cancel_pressed;
150  *fCancel = kFALSE;
151 
152  // SET UP GUI
153  theList = params;
154  theList->Copy(fOriginal); // copy original values in case "Cancel" is pressed
155  // for each name-value pair, add a horizontal frame with a label (name)
156  // and a text entry box (value)
158  max_width = 1;
159  for (Int_t i = 0; i < nentries; ++i) {
161  TGLabel* lab = new TGLabel(hf, theList->GetNameAt(i));
163  lab->Resize(lab->GetDefaultWidth(), lab->GetDefaultHeight());
164  hf->AddFrame(lab, new TGLayoutHints(kLHintsCenterY | kLHintsLeft | kLHintsExpandX, 2, 3, 1, 1));
165 
166  if (theList->GetParameter(i)->IsInt()) {
167  fData[i] = AddAInt(i, hf);
168  }
169  else if (theList->GetParameter(i)->IsDouble()) {
170  fData[i] = AddADouble(i, hf);
171  }
172  else if (theList->GetParameter(i)->IsBool()) {
173  fData[i] = AddABool(i, hf);
174  }
175  else {
176  fData[i] = AddAString(i, hf);
177  }
178 
179  fMain->AddFrame(hf, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 1, 1, 1, 1));
180  }
181 
182  //--- create the OK, Apply and Cancel buttons
183 
184  fWidth = max_width * 2;
185  fHeight = 0;
186 
188 
189  fOKBut = new TGTextButton(hf, "&OK", 1);
190  hf->AddFrame(fOKBut, new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 2, 3, 0, 0));
192  fOKBut->SetToolTipText("Save changes and close");
194  fOKBut->Connect("Clicked()", "KVNameValueListGUI", this, "ReadData()");
195  fOKBut->Connect("Clicked()", "KVNameValueListGUI", this, "DoClose()");
196 
197  fCancelBut = new TGTextButton(hf, "&Cancel", 3);
200  fCancelBut->SetToolTipText("Discard changes and close");
201  fCancelBut->Connect("Clicked()", "KVNameValueListGUI", this, "RestoreData()");
202  fCancelBut->Connect("Clicked()", "KVNameValueListGUI", this, "DoClose()");
203 
205 }
206 
207 
208 
209 
212 
214 {
215  // Destructor
216  if (fMain) {
217  delete fMain;
218  fMain = 0;
219  }
220 }
221 
222 
223 
224 
226 
228 {
229  TTimer::SingleShot(150, "KVNameValueListGUI", this, "CloseWindow()");
230 }
231 
232 
233 
234 
236 
238 {
239  delete this;
240 }
241 
242 
243 
251 
252 bool KVNameValueListGUI::EnableDependingOnBool(const TString& value_to_enable, const TString& bool_parameter)
253 {
254  // If the list of parameters contains a boolean variable with name bool_parameter,
255  // one or more of the widgets (number entry field, text entry field, etc.) can be disabled or enabled
256  // depending on the boolean state.
257  //
258  // if bool_parameter is not the name of a boolean in the list, or value_to_enable cannot be found in the list,
259  // returns false. otherwise, returns true in case of success.
260 
261  if (!theList->HasBoolParameter(bool_parameter)) {
262  Warning("EnableDependingOnBool", "No boolean parameter with name \"%s\" in list", bool_parameter.Data());
263  return false;
264  }
265 
266  auto bool_widget = dynamic_cast<TGCheckButton*>(GetDataWidget(bool_parameter));
267 
268  if (!theList->HasParameter(value_to_enable)) {
269  Warning("EnableDependingOnBool", "No parameter with name \"%s\" in list", value_to_enable.Data());
270  return false;
271  }
272 
273  auto value_widget = GetDataWidget(value_to_enable);
274  if (value_widget->InheritsFrom(TGNumberEntry::Class()))
275  bool_widget->Connect("Toggled(Bool_t)", value_widget->IsA()->GetName(), value_widget, "SetState(Bool_t)");
276  else
277  bool_widget->Connect("Toggled(Bool_t)", value_widget->IsA()->GetName(), value_widget, "SetEnabled(Bool_t)");
278 
279  // set current state according to value
280  if (value_widget->InheritsFrom(TGTextEntry::Class()))
281  dynamic_cast<TGTextEntry*>(value_widget)->SetEnabled(theList->GetBoolValue(bool_parameter));
282  else if (value_widget->InheritsFrom(TGNumberEntry::Class()))
283  dynamic_cast<TGNumberEntry*>(value_widget)->SetState(theList->GetBoolValue(bool_parameter));
284  if (value_widget->InheritsFrom(TGCheckButton::Class()))
285  dynamic_cast<TGCheckButton*>(value_widget)->SetEnabled(theList->GetBoolValue(bool_parameter));
286 
287  return true;
288 }
289 
290 
int Int_t
kVerticalFrame
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
const Bool_t kFALSE
bool Bool_t
const Bool_t kTRUE
kButtonDown
kButtonUp
#define gClient
kDeepCleanup
kLHintsLeft
kLHintsCenterY
kLHintsCenterX
kLHintsBottom
kLHintsTop
kLHintsExpandX
int nentries
GUI for setting KVNameValueList parameters.
TObject * GetDataWidget(int i) const
virtual TObject * AddABool(Int_t i, TGHorizontalFrame *hf)
TGTransientFrame * fMain
virtual TObject * AddAString(Int_t i, TGHorizontalFrame *hf)
virtual TObject * AddAInt(Int_t i, TGHorizontalFrame *hf)
KVNameValueListGUI(const TGWindow *main, KVNameValueList *params, Bool_t *cancel_pressed, Bool_t wait_for_main=kTRUE)
Constructor.
bool EnableDependingOnBool(const TString &value_to_enable, const TString &bool_parameter)
virtual TObject * AddADouble(Int_t i, TGHorizontalFrame *hf)
virtual ~KVNameValueListGUI()
Destructor.
TGTextButton * fCancelBut
Cancel button.
void RestoreData()
return all values to original state
TGTextButton * fOKBut
OK button.
KVNameValueList * theList
KVNameValueList fOriginal
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
KVNamedParameter * GetParameter(Int_t idx) const
return the parameter object with index idx
Double_t GetDoubleValue(const Char_t *name) const
const Char_t * GetNameAt(Int_t idx) const
Int_t GetNpar() const
return the number of stored parameters
Int_t GetEntries() const
Bool_t HasBoolParameter(const Char_t *name) const
Bool_t GetBoolValue(const Char_t *name) const
Bool_t HasParameter(const Char_t *name) const
void Copy(TObject &nvl) const
A generic named parameter storing values of different types.
Bool_t IsDouble() const
void Set(const char *, const char *)
Bool_t IsInt() const
Bool_t IsBool() const
Bool_t IsString() const
virtual void SetToolTipText(const char *text, Long_t delayms=400)
virtual void SetEnabled(Bool_t e=kTRUE)
virtual void SetState(EButtonState state, Bool_t emit=kFALSE)
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
virtual UInt_t GetDefaultWidth() const
virtual void SetCleanup(Int_t mode=kLocalCleanup)
virtual void MapSubwindows()
virtual UInt_t GetDefaultHeight() const
virtual UInt_t GetDefaultWidth() const
virtual UInt_t GetDefaultHeight() const
virtual void Resize(TGDimension size)
virtual void MapWindow()
void DontCallClose()
void SetWMSize(UInt_t w, UInt_t h)
void SetMWMHints(UInt_t value, UInt_t funcs, UInt_t input)
void SetWMSizeHints(UInt_t wmin, UInt_t hmin, UInt_t wmax, UInt_t hmax, UInt_t winc, UInt_t hinc)
void SetWindowName(const char *name=0)
UInt_t GetDefaultHeight() const
virtual void SetState(Bool_t enable=kTRUE)
virtual void CenterOnParent(Bool_t croot=kTRUE, EPlacement pos=kCenter)
virtual const char * GetTitle() const
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
const char * Data() const
static void SingleShot(Int_t milliSec, const char *receiver_class, void *receiver, const char *method)
TText * text
int main(int argc, char **argv)
void Warning(const char *location, const char *va_(fmt),...)
Double_t Max(Double_t a, Double_t b)