Diana Software
QGCutsFrame.cc
Go to the documentation of this file.
1 #include "QGCutsFrame.hh"
2 #include <iostream>
3 #include "QGDropDownBox.hh"
4 #include "TGButton.h"
5 #include "TGLabel.h"
6 #include "TGListBox.h"
7 #include "TGPicture.h"
8 #include "TGTextEntry.h"
9 
10 // Images
11 #include "ImageAdd.xpm"
12 #include "ImageCopy.xpm"
13 #include "ImageEdit.xpm"
14 #include "ImagePaste.xpm"
15 #include "ImageRemove.xpm"
16 
18 
19 using std::map;
20 using std::string;
21 using std::vector;
22 
23 vector<string> QGCutsFrame::fCopiedCuts;
24 
25 QGCutsFrame::QGCutsFrame(TGWindow *p, UInt_t w, UInt_t h) :
26 TGCompositeFrame(p, w, h),
27 fCutsListBox(0), fId(0), fLabel(0)
28 {
29  SetCleanup(kDeepCleanup);
30 
31  fLabel = new TGLabel(this, "Cuts");
32  AddFrame(fLabel, new TGLayoutHints(kLHintsCenterX, 0, 0, 5, 0));
33  //fLabel->SetTextFont("Times 18", kFALSE);
34 
35  fAddEditFrame = new TGHorizontalFrame(this, w, 25);
36  AddFrame(fAddEditFrame, new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 0));
37 
39  fAddEditFrame->AddFrame(fDropDownBox, new TGLayoutHints(kLHintsExpandX | kLHintsCenterY, 0, 0, 0, 0));
40  fDropDownBox->Resize(GetDefaultWidth(), 25);
41  fDropDownBox->GetTextEntry()->SetToolTipText("Enter a cut and press Return or click Add");
43  fDropDownBox->Connect("ReturnPressed()", "QGCutsFrame", this, "HandleDropDownBox()");
44 
45  fAddButton = new TGPictureButton(fAddEditFrame, gClient->GetPicturePool()->GetPicture("Add", (char**)ImageAdd));
46  fAddEditFrame->AddFrame(fAddButton, new TGLayoutHints(kLHintsCenterY, 1, 0, 0, 0));
47  fAddButton->SetToolTipText("Add the cut typed in the box to the list of cuts");
48  fAddButton->Connect("Clicked()", "QGCutsFrame", this, "HandleDropDownBox()");
49 
50  fCutsListBox = new TGListBox(this);
51  AddFrame(fCutsListBox, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 1, 0));
52  fCutsListBox->SetMultipleSelections(kTRUE);
53 
54  fButtonsFrame = new TGHorizontalFrame(this, w, 25);
55  AddFrame(fButtonsFrame, new TGLayoutHints(kLHintsCenterX, 0, 0, 1, 0));
56 
57  fEditButton = new TGPictureButton(fButtonsFrame, gClient->GetPicturePool()->GetPicture("Edit", (char**)ImageEdit));
58  fButtonsFrame->AddFrame(fEditButton, new TGLayoutHints(kLHintsCenterX, 0, 0, 0, 0));
59  fEditButton->SetToolTipText("Edit the selected entry (only if unique entry is selected)");
60  fEditButton->Connect("Clicked()", "QGCutsFrame", this, "HandleEditButton()");
61 
62  fRemoveButton = new TGPictureButton(fButtonsFrame, gClient->GetPicturePool()->GetPicture("Remove", (char**)ImageRemove));
63  fButtonsFrame->AddFrame(fRemoveButton, new TGLayoutHints(kLHintsCenterX, 0, 0, 0, 0));
64  fRemoveButton->SetToolTipText("Remove the selected entries");
65  fRemoveButton->Connect("Clicked()", "QGCutsFrame", this, "HandleRemoveButton()");
66 
67  fCopyButton = new TGPictureButton(fButtonsFrame, gClient->GetPicturePool()->GetPicture("Copy", (char**)ImageCopy));
68  fButtonsFrame->AddFrame(fCopyButton, new TGLayoutHints(kLHintsCenterX, 0, 0, 0, 0));
69  fCopyButton->SetToolTipText("Copy selected entries to the clipboard");
70  fCopyButton->Connect("Clicked()", "QGCutsFrame", this, "HandleCopyButton()");
71 
72  fPasteButton = new TGPictureButton(fButtonsFrame, gClient->GetPicturePool()->GetPicture("Paste", (char**)ImagePaste));
73  fButtonsFrame->AddFrame(fPasteButton, new TGLayoutHints(kLHintsCenterX, 0, 0, 0, 0));
74  fPasteButton->SetToolTipText("Paste from the clipboard");
75  fPasteButton->Connect("Clicked()", "QGCutsFrame", this, "HandlePasteButton()");
76 }
77 
79 {
80  Cleanup();
81 }
82 
83 void QGCutsFrame::AddCut(const string& cut)
84 {
85  fCuts[fId] = cut;
86  fCutsListBox->AddEntrySort(cut.c_str(), fId);
87  ++fId;
88  fCutsListBox->Layout();
89 }
90 
92 {
93  fCopiedCuts.clear();
94 }
95 
96 void QGCutsFrame::CopyCut(const string& cut)
97 {
98  fCopiedCuts.push_back(cut);
99 }
100 
101 vector<string> QGCutsFrame::GetCuts()
102 {
103  vector<string> cuts;
104  map<Int_t, string>::const_iterator iter;
105  for (iter = fCuts.begin(); iter != fCuts.end(); ++iter) {
106  cuts.push_back(iter->second);
107  }
108  return cuts;
109 }
110 
112 {
113  if (fCuts.size() != 0) {
114  fCopiedCuts.clear();
115  vector<Int_t> selectedEntries = GetSelectedEntries();
116  if (selectedEntries.size() != 0) {
117  vector<Int_t>::const_iterator iter;
118  for (iter = selectedEntries.begin(); iter != selectedEntries.end(); ++iter) {
119  fCopiedCuts.push_back(fCuts[*iter]);
120  }
121  } else {
122  map<Int_t, string>::const_iterator cutIter;
123  for (cutIter = fCuts.begin(); cutIter != fCuts.end(); ++cutIter) {
124  fCopiedCuts.push_back(cutIter->second);
125  }
126  }
127  }
128 }
129 
131 {
132  string cut = fDropDownBox->GetTextEntry()->GetText();
133  if (fAddEditFrame->IsVisible(fAddButton)) {
134  if (cut != "") {
135  AddCut(cut);
136  }
137  } else {
138  if (cut != "") {
139  fCuts[fEditId] = cut;
140  fCutsListBox->RemoveEntry(fEditId);
141  fCutsListBox->AddEntrySort(cut.c_str(), fEditId);
142  fCutsListBox->Layout();
143  }
144  fDropDownBox->GetTextEntry()->SetToolTipText("Enter a cut and press Return or click Add");
145  fAddEditFrame->ShowFrame(fAddButton);
146  }
147  fDropDownBox->GetTextEntry()->Clear();
148 }
149 
151 {
152  vector<Int_t> selectedEntries = GetSelectedEntries();
153  if (selectedEntries.size() == 1) {
154  fEditId = GetSelectedEntries().front();
155  fAddEditFrame->HideFrame(fAddButton);
156  fDropDownBox->GetTextEntry()->SetText(fCuts[fEditId].c_str());
157  fDropDownBox->GetTextEntry()->SelectAll();
158  fDropDownBox->GetTextEntry()->SetFocus();
159  fDropDownBox->GetTextEntry()->SetToolTipText("Edit the cut and press Return. Leave blank to cancel.");
160  }
161 }
162 
164 {
165  vector<string>::const_iterator iter;
166  for (iter = fCopiedCuts.begin(); iter != fCopiedCuts.end(); ++iter) {
167  AddCut(*iter);
168  }
169 }
170 
172 {
173  vector<Int_t> selectedEntries = GetSelectedEntries();
174  vector<Int_t>::const_iterator iter;
175  for (iter = selectedEntries.begin(); iter != selectedEntries.end(); ++iter) {
176  fCuts.erase(*iter);
177  fCutsListBox->RemoveEntry(*iter);
178  fCutsListBox->Layout();
179  }
180 }
181 
183  vector<Int_t> entries;
184  TList *selectedEntries = new TList();
185  fCutsListBox->GetSelectedEntries(selectedEntries);
186  for (int i = 0; i < selectedEntries->GetSize(); ++i)
187  entries.push_back(((TGLBEntry*) selectedEntries->At(i))->EntryId());
188  delete selectedEntries;
189  return entries;
190 }
191 
192 void QGCutsFrame::SetCuts(const vector<string>& cuts)
193 {
194  fCutsListBox->RemoveAll();
195  fCutsListBox->Layout();
196  fCuts.clear();
197  vector<string>::const_iterator cutIter;
198  for (cutIter = cuts.begin(); cutIter != cuts.end(); ++cutIter) {
199  AddCut(*cutIter);
200  }
201 }
202 
203 void QGCutsFrame::SetLabel(const string& label)
204 {
205  fLabel->SetText(label.c_str());
206 }
ClassImp(QObject)
Frame to handle editing of cuts.
Definition: QGCutsFrame.hh:21
std::vector< Int_t > GetSelectedEntries()
Get id numbers of selected entries in list box.
Definition: QGCutsFrame.cc:182
TGPictureButton * fAddButton
Add button.
Definition: QGCutsFrame.hh:76
static std::vector< std::string > fCopiedCuts
Cuts copied to the clipboard.
Definition: QGCutsFrame.hh:85
void AddCut(const std::string &cut)
Add cut.
Definition: QGCutsFrame.cc:83
TGPictureButton * fRemoveButton
Remove button.
Definition: QGCutsFrame.hh:115
TGPictureButton * fCopyButton
Copy button.
Definition: QGCutsFrame.hh:88
static void ClearCopiedCuts()
Clear copied cuts.
Definition: QGCutsFrame.cc:91
void HandleDropDownBox()
Handle drop down box.
Definition: QGCutsFrame.cc:130
Int_t fEditId
Id of cut being edited.
Definition: QGCutsFrame.hh:103
TGPictureButton * fPasteButton
Paste button.
Definition: QGCutsFrame.hh:112
virtual ~QGCutsFrame()
Destructor.
Definition: QGCutsFrame.cc:78
static void CopyCut(const std::string &cut)
Copy cut.
Definition: QGCutsFrame.cc:96
TGPictureButton * fEditButton
Edit button.
Definition: QGCutsFrame.hh:100
TGHorizontalFrame * fButtonsFrame
Cuts buttons frame.
Definition: QGCutsFrame.hh:82
void HandleCopyButton()
Handle copy button.
Definition: QGCutsFrame.cc:111
void HandleEditButton()
Handle edit button.
Definition: QGCutsFrame.cc:150
std::vector< std::string > GetCuts()
Get cuts.
Definition: QGCutsFrame.cc:101
QGCutsFrame(TGWindow *p=0, UInt_t w=100, UInt_t h=100)
Constructor.
Definition: QGCutsFrame.cc:25
QGDropDownBox * fDropDownBox
Drop down box.
Definition: QGCutsFrame.hh:97
TGHorizontalFrame * fAddEditFrame
Frame for drop down box and Add/Edit buttons.
Definition: QGCutsFrame.hh:79
void HandlePasteButton()
Handle paste button.
Definition: QGCutsFrame.cc:163
Int_t fId
Cut id.
Definition: QGCutsFrame.hh:106
TGLabel * fLabel
Label.
Definition: QGCutsFrame.hh:109
TGListBox * fCutsListBox
Cuts list box.
Definition: QGCutsFrame.hh:94
void HandleRemoveButton()
Handle remove button.
Definition: QGCutsFrame.cc:171
void SetCuts(const std::vector< std::string > &cuts)
Set cuts.
Definition: QGCutsFrame.cc:192
void SetLabel(const std::string &label)
Set label.
Definition: QGCutsFrame.cc:203
std::map< Int_t, std::string > fCuts
Cuts.
Definition: QGCutsFrame.hh:91
Drop down box with tab-complete.
void EnableTabCompletion()
Enable tab completion.