Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TPieEditor.cxx
Go to the documentation of this file.
1 // @(#)root/ged:$Id$
2 // Author: Guido Volpi 12/10/2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 
13 //////////////////////////////////////////////////////////////////////////
14 // //
15 // TPieEditor //
16 // //
17 // Implements GUI for pie-chart attributes. //
18 // //
19 // Title': set the title of the graph //
20 // //
21 //////////////////////////////////////////////////////////////////////////
22 //Begin_Html
23 /*
24 <img src="gif/TPieEditor.gif">
25 */
26 //End_Html
27 
28 #include "TGedEditor.h"
29 #include "TGComboBox.h"
30 #include "TGButtonGroup.h"
31 #include "TPieEditor.h"
32 #include "TGTextEntry.h"
33 #include "TGNumberEntry.h"
34 #include "TGToolTip.h"
35 #include "TGLabel.h"
36 #include "TPie.h"
37 #include "TVirtualPad.h"
38 #include "TGColorSelect.h"
39 #include "TGComboBox.h"
40 #include "TColor.h"
41 #include "TBox.h"
42 #include "TPaveLabel.h"
43 
44 ClassImp(TPieEditor);
45 
46 enum EPieWid {
47  kPie = 0,
48  kPIE_HOR,
49  kPIE_RAD,
50  kPIE_TAN,
51  kPIE_FILL,
52  kPIE_OUTLINE,
53  kPIE_TITLE,
54  kPIE_3D,
55  kPIE_3DANGLE,
56  kPIE_3DTHICKNESS,
57  kFONT_COLOR,
58  kFONT_SIZE,
59  kFONT_STYLE
60 };
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// Constructor of graph editor.
64 
65 TPieEditor::TPieEditor(const TGWindow *p, Int_t width,
66  Int_t height, UInt_t options, Pixel_t back)
67  : TGedFrame(p, width, height, options | kVerticalFrame, back)
68 {
69  fPie = 0;
70  // TextEntry to change the title
71  MakeTitle("Pie Chart");
72 
73  fTitle = new TGTextEntry(this, new TGTextBuffer(50), kPIE_TITLE);
74  fTitle->Resize(135, fTitle->GetDefaultHeight());
75  fTitle->SetToolTipText("Enter the pie title string");
76  // better take kLHintsLeft and Right - Right is not working at the moment
77  AddFrame(fTitle, new TGLayoutHints(kLHintsLeft, 3, 1, 2, 5));
78 
79  // Radio Buttons to change the draw options of the graph
80  TGCompositeFrame *f2 = new TGCompositeFrame(this, 80, 20, kVerticalFrame);
81  fgr = new TGButtonGroup(f2,3,1,3,5,"Label direction");
82  fgr->SetRadioButtonExclusive(kTRUE);
83  fLblDirH = new TGRadioButton(fgr,"Horizontal",kPIE_HOR); // no draw option
84  fLblDirH->SetToolTipText("Draw horizontal labels");
85  fLblDirR = new TGRadioButton(fgr,"Radial",kPIE_RAD); // option C
86  fLblDirR->SetToolTipText("Draw labels radially");
87  fLblDirT = new TGRadioButton(fgr,"Tangential",kPIE_TAN); // option L
88  fLblDirT->SetToolTipText("Draw labels tangential to the piechart");
89 
90  fgr->SetLayoutHints(fShape1lh=new TGLayoutHints(kLHintsLeft, 0,3,0,0), fLblDirH);
91  fgr->Show();
92  fgr->ChangeOptions(kFitWidth|kChildFrame|kVerticalFrame);
93  f2->AddFrame(fgr, new TGLayoutHints(kLHintsLeft, 4, 0, 0, 0));
94 
95  // CheckBox to activate/deactivate the drawing of the Marker
96  fOutlineOnOff = new TGCheckButton(f2,"Outline",kPIE_OUTLINE);
97  fOutlineOnOff->SetToolTipText("Draw a line to mark the pie");
98  f2->AddFrame(fOutlineOnOff, new TGLayoutHints(kLHintsTop, 5, 1, 0, 3));
99  AddFrame(f2, new TGLayoutHints(kLHintsTop, 1, 1, 0, 0));
100 
101  // Exclusion zone parameters
102  MakeTitle("3D options");
103  TGCompositeFrame *f3 = new TGCompositeFrame(this, 80, 20, kHorizontalFrame);
104  AddFrame(f3, new TGLayoutHints(kLHintsTop, 1, 1, 5, 0));
105 
106  fIs3D = new TGCheckButton(f3,"3D",kPIE_3D);
107  fIs3D->SetToolTipText("Draw a 3D charts");
108  f3->AddFrame(fIs3D, new TGLayoutHints(kLHintsTop, 5, 1, 0, 0));
109 
110  f3DAngle = new TGNumberEntry(f3, 0, 2, kPIE_3DANGLE, TGNumberEntry::kNESInteger, TGNumberEntry::kNEANonNegative,TGNumberFormat::kNELLimitMinMax, 0, 90);
111  //f3DAngle->SetToolTipText("3D angle: 0-90")
112  f3DAngle->Resize(50, 20);
113  f3->AddFrame(f3DAngle, new TGLayoutHints(kLHintsLeft, 7, 1, 1, 1));
114  f3DAngle->Associate(f3);
115 
116  f3DHeight = new TGNumberEntry(f3, 0, 3, kPIE_3DTHICKNESS, TGNumberEntry::kNESReal, TGNumberEntry::kNEANonNegative);
117  //f3DHeight->SetToolTipText("3D thick")
118  f3DHeight->Resize(50, 20);
119  f3->AddFrame(f3DHeight, new TGLayoutHints(kLHintsLeft, 7, 1, 1, 1));
120  f3DHeight->Associate(f3);
121 
122  MakeTitle("Text");
123  TGCompositeFrame *f4 = new TGCompositeFrame(this, 80, 20, kHorizontalFrame);
124  fColorSelect = new TGColorSelect(f4, 0, kFONT_COLOR);
125  f4->AddFrame(fColorSelect, new TGLayoutHints(kLHintsLeft, 1, 1, 1, 1));
126  fColorSelect->Associate(this);
127  fSizeCombo = BuildFontSizeComboBox(f4, kFONT_SIZE);
128  f4->AddFrame(fSizeCombo, new TGLayoutHints(kLHintsLeft, 3, 1, 1, 1));
129  fSizeCombo->Resize(91, 20);
130  fSizeCombo->Associate(this);
131  AddFrame(f4, new TGLayoutHints(kLHintsTop, 1, 1, 0, 0));
132  fTypeCombo = new TGFontTypeComboBox(this, kFONT_STYLE);
133  fTypeCombo->Resize(137, 20);
134  AddFrame(fTypeCombo, new TGLayoutHints(kLHintsLeft, 3, 1, 1, 1));
135 }
136 
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// Destructor of pie editor.
140 
141 TPieEditor::~TPieEditor()
142 {
143 }
144 
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// Connect signals to slots.
148 
149 void TPieEditor::ConnectSignals2Slots()
150 {
151  fTitle->Connect("TextChanged(const char *)","TPieEditor",this,"DoTitle(const char *)");
152  fgr->Connect("Clicked(Int_t)","TPieEditor",this,"DoShape()");
153  fOutlineOnOff->Connect("Toggled(Bool_t)","TPieEditor",this,"DoMarkerOnOff(Bool_t)");
154  f3DAngle->Connect("ValueSet(Long_t)", "TPieEditor", this, "DoChange3DAngle()");
155  f3DHeight->Connect("ValueSet(Long_t)", "TPieEditor", this, "DoChange3DAngle()");
156  fIs3D->Connect("Clicked()","TPieEditor",this,"DoGraphLineWidth()");
157 
158  // text attributes connection
159  fTypeCombo->Connect("Selected(Int_t)","TPieEditor",this,"DoTextChange()");
160  fSizeCombo->Connect("Selected(Int_t)","TPieEditor",this,"DoTextChange()");
161  fColorSelect->Connect("ColorSelected(Pixel_t)","TPieEditor",this,"DoTextChange()");
162 
163  fInit = kFALSE; // connect the slots to the signals only once
164 }
165 
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// Exclude TAttTextEditor from this interface.
169 
170 void TPieEditor::ActivateBaseClassEditors(TClass* cl)
171 {
172  TGedEditor *gedEditor = GetGedEditor();
173  gedEditor->ExcludeClassEditor(TAttText::Class());
174  TGedFrame::ActivateBaseClassEditors(cl);
175 }
176 
177 
178 ////////////////////////////////////////////////////////////////////////////////
179 /// Pick up the used values of graph attributes.
180 
181 void TPieEditor::SetModel(TObject* obj)
182 {
183  fPie = (TPie *)obj;
184  fAvoidSignal = kTRUE;
185 
186  // set the Title TextEntry
187  const char *text = fPie->GetTitle();
188  fTitle->SetText(text);
189 
190  TString soption = GetDrawOption();
191 
192  // For the label orientation there are 3 possibilities:
193  // 0: horizontal
194  // 1: radial
195  // 2: tangent
196  Int_t lblor(0);
197 
198  // Parse the options
199  Int_t idx;
200  // Paint the TPie in an existing canvas
201  if ( (idx=soption.Index("same"))>=0 ) {
202  // optionSame = kTRUE;
203  soption.Remove(idx,4);
204  }
205 
206  if ( (idx=soption.Index("nol"))>=0 ) {
207  fOutlineOnOff->SetState(kButtonUp,kFALSE);
208  soption.Remove(idx,3);
209  }
210  else {
211  fOutlineOnOff->SetState(kButtonDown,kFALSE);
212  }
213 
214  // check if is active the pseudo-3d
215  if ( (idx=soption.Index("3d"))>=0 ) {
216  fIs3D->SetState(kButtonDown, kFALSE);
217  f3DAngle->SetNumber(fPie->GetAngle3D());
218  f3DHeight->SetNumber(fPie->GetHeight());
219  soption.Remove(idx,2);
220  } else {
221  fIs3D->SetState(kButtonUp, kFALSE);
222  }
223 
224  // seek if have to draw the labels around the pie chart
225  if ( (idx=soption.Index("t"))>=0 ) {
226  lblor = 2;
227  soption.Remove(idx,1);
228  }
229 
230  // Seek if have to paint the labels along the radii
231  if ( (idx=soption.Index("r"))>=0 ) {
232  lblor = 1;
233  soption.Remove(idx,1);
234  }
235 
236  switch(lblor) {
237  case 0:
238  fLblDirH->SetState(kButtonDown,kTRUE);
239  break;
240  case 1:
241  fLblDirR->SetState(kButtonDown,kTRUE);
242  break;
243  case 2:
244  fLblDirT->SetState(kButtonDown,kTRUE);
245  break;
246  }
247 
248  // set text attributes
249  fTypeCombo->Select(fPie->GetTextFont() / 10);
250 
251  Color_t c = fPie->GetTextColor();
252  Pixel_t p = TColor::Number2Pixel(c);
253  fColorSelect->SetColor(p, kFALSE);
254 
255  Float_t s = fPie->GetTextSize();
256  Float_t dy;
257 
258  if (obj->InheritsFrom(TPaveLabel::Class())) {
259  TBox *pl = (TBox*)obj;
260  dy = s * (pl->GetY2() - pl->GetY1());
261  }
262  else
263  dy = s * (fGedEditor->GetPad()->GetY2() - fGedEditor->GetPad()->GetY1());
264 
265  Int_t size = fGedEditor->GetPad()->YtoPixel(0.0) - fGedEditor->GetPad()->YtoPixel(dy);
266  if (size > 50) size = 50;
267  if (size < 0) size = 0;
268  fSizeCombo->Select(size, kFALSE);
269 
270  if (fInit) ConnectSignals2Slots();
271  fAvoidSignal = kFALSE;
272 }
273 
274 
275 ////////////////////////////////////////////////////////////////////////////////
276 /// Slot for setting the graph title.
277 
278 void TPieEditor::DoTitle(const char *text)
279 {
280  if (fAvoidSignal) return;
281  fPie->SetTitle(text);
282  Update();
283 }
284 
285 
286 ////////////////////////////////////////////////////////////////////////////////
287 /// Slot connected to the draw options.
288 
289 void TPieEditor::DoShape()
290 {
291  if (fAvoidSignal) return;
292 
293  TString opt = GetDrawOption();
294 
295  if (fLblDirH->GetState()==kButtonDown) {
296  if (opt.Contains("t")) opt.Remove(opt.First("t"),1);
297  if (opt.Contains("r")) opt.Remove(opt.First("r"),1);
298  }
299  else if (fLblDirR->GetState()==kButtonDown) {
300  if (opt.Contains("t")) opt.Remove(opt.First("t"),1);
301  if (!opt.Contains("r")) opt += "r";
302  }
303  else if (fLblDirT->GetState()==kButtonDown) {
304  if (!opt.Contains("t")) opt += "t";
305  if (opt.Contains("r")) opt.Remove(opt.First("r"),1);
306  }
307 
308  SetDrawOption(opt);
309  if (gPad && gPad->GetVirtCanvas()) gPad->GetVirtCanvas()->SetCursor(kPointer);
310  gVirtualX->SetCursor(GetId(), gVirtualX->CreateCursor(kPointer));
311 }
312 
313 
314 ////////////////////////////////////////////////////////////////////////////////
315 /// Slot for setting markers as visible/invisible.
316 
317 void TPieEditor::DoMarkerOnOff(Bool_t)
318 {
319  if (fAvoidSignal) return;
320  TString t = GetDrawOption();
321 
322  if (t.Contains("nol") && fOutlineOnOff->GetState() == kButtonDown) {
323  t.Remove(t.First("nol"),3);
324  }
325  else if (!t.Contains("nol") && fOutlineOnOff->GetState() == kButtonUp) {
326  t += "nol";
327  }
328 
329  SetDrawOption(t);
330 }
331 
332 
333 ////////////////////////////////////////////////////////////////////////////////
334 /// Slot for setting the 3D angle
335 
336 void TPieEditor::DoChange3DAngle()
337 {
338  if (fAvoidSignal) return;
339 
340  fPie->SetAngle3D(static_cast<Int_t>(f3DAngle->GetNumber()));
341  fPie->SetHeight(f3DHeight->GetNumber());
342 
343  Update();
344 
345 }
346 
347 
348 ////////////////////////////////////////////////////////////////////////////////
349 /// Slot connected to the graph line width.
350 
351 void TPieEditor::DoGraphLineWidth()
352 {
353  if (fAvoidSignal) return;
354 
355  TString opt = GetDrawOption();
356  if (!opt.Contains("3d") && fIs3D->IsDown())
357  opt += "3d";
358  else if (opt.Contains("3d") && !fIs3D->IsDown())
359  opt.Remove(opt.First("3d"),2);
360 
361  SetDrawOption(opt);
362 
363  Update();
364 }
365 
366 
367 
368 ////////////////////////////////////////////////////////////////////////////////
369 /// Change text.
370 
371 void TPieEditor::DoTextChange()
372 {
373  if (fAvoidSignal) return;
374 
375  // font color
376  fPie->SetTextColor(TColor::GetColor(fColorSelect->GetColor()));
377 
378  // font type
379  Int_t fontPrec = fPie->GetTextFont()%10;
380  Int_t fontType = fTypeCombo->GetSelected();
381  fPie->SetTextFont(fontType*10+fontPrec);
382 
383  // font size
384  TVirtualPad* pad = fGedEditor->GetPad();
385 
386  Float_t val = TString(fSizeCombo->GetSelectedEntry()->GetTitle()).Atoi();
387 
388  Float_t dy = pad->AbsPixeltoY(0) - pad->AbsPixeltoY((Int_t)val);
389  Float_t textSize;
390 
391  if (fGedEditor->GetModel()->InheritsFrom(TPaveLabel::Class())) {
392  TBox *pl = (TBox*)fGedEditor->GetModel();
393  textSize = dy/(pl->GetY2() - pl->GetY1());
394  }
395  else
396  textSize = dy/(pad->GetY2() - pad->GetY1());
397 
398  fPie->SetTextSize(textSize);
399 
400  Update();
401 
402 }
403 
404 
405 ////////////////////////////////////////////////////////////////////////////////
406 /// Create text size combo box.
407 
408 TGComboBox* TPieEditor::BuildFontSizeComboBox(TGFrame* parent, Int_t id)
409 {
410  char a[100];
411  TGComboBox *c = new TGComboBox(parent, id);
412 
413  c->AddEntry("Default", 0);
414  for (int i = 1; i <= 50; i++) {
415  snprintf(a, 99, "%d", i);
416  c->AddEntry(a, i);
417  }
418 
419  return c;
420 }