TPieEditor.cxx

Go to the documentation of this file.
00001 // @(#)root/ged:$Id: TPieEditor.cxx 35527 2010-09-21 12:27:01Z brun $
00002 // Author: Guido Volpi 12/10/2007
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 
00013 //////////////////////////////////////////////////////////////////////////
00014 //                                                                      //
00015 //  TPieEditor                                                          //
00016 //                                                                      //
00017 //  Implements GUI for pie-chart attributes.                            //
00018 //                                                                      //
00019 //  Title': set the title of the graph                                  //
00020 //                                                                      //
00021 //////////////////////////////////////////////////////////////////////////
00022 //Begin_Html
00023 /*
00024 <img src="gif/TPieEditor.gif">
00025 */
00026 //End_Html
00027 
00028 #include "TGedEditor.h"
00029 #include "TGComboBox.h"
00030 #include "TGButtonGroup.h"
00031 #include "TPieEditor.h"
00032 #include "TGTextEntry.h"
00033 #include "TGNumberEntry.h"
00034 #include "TGToolTip.h"
00035 #include "TGLabel.h"
00036 #include "TPie.h"
00037 #include "TVirtualPad.h"
00038 #include "TGColorSelect.h"
00039 #include "TGComboBox.h"
00040 #include "TColor.h"
00041 #include "TBox.h"
00042 #include "TPaveLabel.h"
00043 
00044 ClassImp(TPieEditor)
00045 
00046 enum EPieWid {
00047    kPie = 0,
00048    kPIE_HOR,
00049    kPIE_RAD,
00050    kPIE_TAN,
00051    kPIE_FILL,
00052    kPIE_OUTLINE,
00053    kPIE_TITLE,
00054    kPIE_3D,
00055    kPIE_3DANGLE,
00056    kPIE_3DTHICKNESS,
00057    kFONT_COLOR,
00058    kFONT_SIZE,
00059    kFONT_STYLE
00060 };
00061 
00062 //______________________________________________________________________________
00063 TPieEditor::TPieEditor(const TGWindow *p, Int_t width,
00064                          Int_t height, UInt_t options, Pixel_t back)
00065    : TGedFrame(p, width, height, options | kVerticalFrame, back)
00066 {
00067    // Constructor of graph editor.
00068 
00069    fPie = 0;
00070    // TextEntry to change the title
00071    MakeTitle("Pie Chart");
00072 
00073    fTitle = new TGTextEntry(this, new TGTextBuffer(50), kPIE_TITLE);
00074    fTitle->Resize(135, fTitle->GetDefaultHeight());
00075    fTitle->SetToolTipText("Enter the pie title string");
00076    // better take kLHintsLeft and Right - Right is not working at the moment
00077    AddFrame(fTitle, new TGLayoutHints(kLHintsLeft, 3, 1, 2, 5));
00078 
00079    // Radio Buttons to change the draw options of the graph
00080    TGCompositeFrame *f2 = new TGCompositeFrame(this, 80, 20, kVerticalFrame);
00081    fgr = new TGButtonGroup(f2,3,1,3,5,"Label direction");
00082    fgr->SetRadioButtonExclusive(kTRUE);
00083    fLblDirH = new TGRadioButton(fgr,"Horizontal",kPIE_HOR);   // no draw option
00084    fLblDirH->SetToolTipText("Draw horizontal labels");
00085    fLblDirR = new TGRadioButton(fgr,"Radial",kPIE_RAD);  // option C
00086    fLblDirR->SetToolTipText("Draw labels radially");
00087    fLblDirT = new TGRadioButton(fgr,"Tangential",kPIE_TAN); // option L
00088    fLblDirT->SetToolTipText("Draw labels tangential to the piechart");
00089 
00090    fgr->SetLayoutHints(fShape1lh=new TGLayoutHints(kLHintsLeft, 0,3,0,0), fLblDirH);
00091    fgr->Show();
00092    fgr->ChangeOptions(kFitWidth|kChildFrame|kVerticalFrame);
00093    f2->AddFrame(fgr, new TGLayoutHints(kLHintsLeft, 4, 0, 0, 0));
00094 
00095    // CheckBox to activate/deactivate the drawing of the Marker
00096    fOutlineOnOff = new TGCheckButton(f2,"Outline",kPIE_OUTLINE);
00097    fOutlineOnOff->SetToolTipText("Draw a line to mark the pie");
00098    f2->AddFrame(fOutlineOnOff, new TGLayoutHints(kLHintsTop, 5, 1, 0, 3));
00099    AddFrame(f2, new TGLayoutHints(kLHintsTop, 1, 1, 0, 0));
00100 
00101    // Exclusion zone parameters
00102    MakeTitle("3D options");
00103    TGCompositeFrame *f3 = new TGCompositeFrame(this, 80, 20, kHorizontalFrame);
00104    AddFrame(f3, new TGLayoutHints(kLHintsTop, 1, 1, 5, 0));
00105 
00106    fIs3D = new TGCheckButton(f3,"3D",kPIE_3D);
00107    fIs3D->SetToolTipText("Draw a 3D charts");
00108    f3->AddFrame(fIs3D, new TGLayoutHints(kLHintsTop, 5, 1, 0, 0));
00109 
00110    f3DAngle = new TGNumberEntry(f3, 0, 2, kPIE_3DANGLE,  TGNumberEntry::kNESInteger, TGNumberEntry::kNEANonNegative,TGNumberFormat::kNELLimitMinMax, 0, 90);
00111    //f3DAngle->SetToolTipText("3D angle: 0-90")
00112    f3DAngle->Resize(50, 20);
00113    f3->AddFrame(f3DAngle, new TGLayoutHints(kLHintsLeft, 7, 1, 1, 1));
00114    f3DAngle->Associate(f3);
00115 
00116    f3DHeight = new TGNumberEntry(f3, 0, 3, kPIE_3DTHICKNESS,  TGNumberEntry::kNESReal, TGNumberEntry::kNEANonNegative);
00117    //f3DHeight->SetToolTipText("3D thick")
00118    f3DHeight->Resize(50, 20);
00119    f3->AddFrame(f3DHeight, new TGLayoutHints(kLHintsLeft, 7, 1, 1, 1));
00120    f3DHeight->Associate(f3);
00121 
00122    MakeTitle("Text");
00123    TGCompositeFrame *f4 = new TGCompositeFrame(this, 80, 20, kHorizontalFrame);
00124    fColorSelect = new TGColorSelect(f4, 0, kFONT_COLOR);
00125    f4->AddFrame(fColorSelect, new TGLayoutHints(kLHintsLeft, 1, 1, 1, 1));
00126    fColorSelect->Associate(this);
00127    fSizeCombo = BuildFontSizeComboBox(f4, kFONT_SIZE);
00128    f4->AddFrame(fSizeCombo, new TGLayoutHints(kLHintsLeft, 3, 1, 1, 1));
00129    fSizeCombo->Resize(91, 20);
00130    fSizeCombo->Associate(this);
00131    AddFrame(f4, new TGLayoutHints(kLHintsTop, 1, 1, 0, 0));
00132    fTypeCombo = new TGFontTypeComboBox(this, kFONT_STYLE);
00133    fTypeCombo->Resize(137, 20);
00134    AddFrame(fTypeCombo, new TGLayoutHints(kLHintsLeft, 3, 1, 1, 1));
00135 }
00136 
00137 
00138 //______________________________________________________________________________
00139 TPieEditor::~TPieEditor()
00140 {
00141    // Destructor of pie editor.
00142 }
00143 
00144 
00145 //_____________________________________________________________________________
00146 void TPieEditor::ConnectSignals2Slots()
00147 {
00148    // Connect signals to slots.
00149 
00150    fTitle->Connect("TextChanged(const char *)","TPieEditor",this,"DoTitle(const char *)");
00151    fgr->Connect("Clicked(Int_t)","TPieEditor",this,"DoShape()");
00152    fOutlineOnOff->Connect("Toggled(Bool_t)","TPieEditor",this,"DoMarkerOnOff(Bool_t)");
00153    f3DAngle->Connect("ValueSet(Long_t)", "TPieEditor", this, "DoChange3DAngle()");
00154    f3DHeight->Connect("ValueSet(Long_t)", "TPieEditor", this, "DoChange3DAngle()");
00155    fIs3D->Connect("Clicked()","TPieEditor",this,"DoGraphLineWidth()");
00156 
00157    // text attributes connection
00158    fTypeCombo->Connect("Selected(Int_t)","TPieEditor",this,"DoTextChange()");
00159    fSizeCombo->Connect("Selected(Int_t)","TPieEditor",this,"DoTextChange()");
00160    fColorSelect->Connect("ColorSelected(Pixel_t)","TPieEditor",this,"DoTextChange()");
00161 
00162    fInit = kFALSE;  // connect the slots to the signals only once
00163 }
00164 
00165 
00166 //______________________________________________________________________________
00167 void TPieEditor::ActivateBaseClassEditors(TClass* cl)
00168 {
00169    // Exclude TAttTextEditor from this interface.
00170 
00171    TGedEditor *gedEditor = GetGedEditor();
00172    gedEditor->ExcludeClassEditor(TAttText::Class());
00173    TGedFrame::ActivateBaseClassEditors(cl);
00174 }
00175 
00176 
00177 //______________________________________________________________________________
00178 void TPieEditor::SetModel(TObject* obj)
00179 {
00180    // Pick up the used values of graph attributes.
00181 
00182    fPie = (TPie *)obj;
00183    fAvoidSignal = kTRUE;
00184 
00185    // set the Title TextEntry
00186    const char *text = fPie->GetTitle();
00187    fTitle->SetText(text);
00188 
00189    TString soption = GetDrawOption();
00190 
00191    bool optionSame(kFALSE);
00192 
00193    // For the label orientation there are 3 possibilities:
00194    //   0: horizontal
00195    //   1: radial
00196    //   2: tangent
00197    Int_t lblor(0);
00198 
00199    // Parse the options
00200    Int_t idx;
00201    // Paint the TPie in an existing canvas
00202    if ( (idx=soption.Index("same"))>=0 ) {
00203       optionSame = kTRUE;
00204       soption.Remove(idx,4);
00205    }
00206 
00207    if ( (idx=soption.Index("nol"))>=0 ) {
00208       fOutlineOnOff->SetState(kButtonUp,kFALSE);
00209       soption.Remove(idx,3);
00210    }
00211    else {
00212       fOutlineOnOff->SetState(kButtonDown,kFALSE);
00213    }
00214 
00215    // check if is active the pseudo-3d
00216    if ( (idx=soption.Index("3d"))>=0 ) {
00217       fIs3D->SetState(kButtonDown, kFALSE);
00218       f3DAngle->SetNumber(fPie->GetAngle3D());
00219       f3DHeight->SetNumber(fPie->GetHeight());
00220       soption.Remove(idx,2);
00221    } else {
00222       fIs3D->SetState(kButtonUp, kFALSE);
00223    }
00224 
00225    // seek if have to draw the labels around the pie chart
00226    if ( (idx=soption.Index("t"))>=0 ) {
00227       lblor = 2;
00228       soption.Remove(idx,1);
00229    }
00230 
00231    // Seek if have to paint the labels along the radii
00232    if ( (idx=soption.Index("r"))>=0 ) {
00233       lblor = 1;
00234       soption.Remove(idx,1);
00235    }
00236 
00237    switch(lblor) {
00238    case 0:
00239       fLblDirH->SetState(kButtonDown,kTRUE);
00240       break;
00241    case 1:
00242       fLblDirR->SetState(kButtonDown,kTRUE);
00243       break;
00244    case 2:
00245       fLblDirT->SetState(kButtonDown,kTRUE);
00246       break;
00247    }
00248 
00249    // set text attributes
00250    fTypeCombo->Select(fPie->GetTextFont() / 10);
00251 
00252    Color_t c = fPie->GetTextColor();
00253    Pixel_t p = TColor::Number2Pixel(c);
00254    fColorSelect->SetColor(p, kFALSE);
00255 
00256    Float_t s = fPie->GetTextSize();
00257    Float_t dy;
00258 
00259    if (obj->InheritsFrom(TPaveLabel::Class())) {
00260       TBox *pl = (TBox*)obj;
00261       dy = s * (pl->GetY2() - pl->GetY1());
00262    }
00263    else
00264       dy = s * (fGedEditor->GetPad()->GetY2() - fGedEditor->GetPad()->GetY1());
00265 
00266    Int_t size = fGedEditor->GetPad()->YtoPixel(0.0) - fGedEditor->GetPad()->YtoPixel(dy);
00267    if (size > 50) size = 50;
00268    if (size < 0)  size = 0;
00269    fSizeCombo->Select(size, kFALSE);
00270 
00271    if (fInit) ConnectSignals2Slots();
00272    fAvoidSignal = kFALSE;
00273 }
00274 
00275 
00276 //______________________________________________________________________________
00277 void TPieEditor::DoTitle(const char *text)
00278 {
00279    // Slot for setting the graph title.
00280 
00281    if (fAvoidSignal) return;
00282    fPie->SetTitle(text);
00283    Update();
00284 }
00285 
00286 
00287 //______________________________________________________________________________
00288 void TPieEditor::DoShape()
00289 {
00290    // Slot connected to the draw options.
00291 
00292    if (fAvoidSignal) return;
00293 
00294    TString opt = GetDrawOption();
00295 
00296    if (fLblDirH->GetState()==kButtonDown) {
00297       if (opt.Contains("t")) opt.Remove(opt.First("t"),1);
00298       if (opt.Contains("r")) opt.Remove(opt.First("r"),1);
00299    }
00300    else if (fLblDirR->GetState()==kButtonDown) {
00301       if (opt.Contains("t")) opt.Remove(opt.First("t"),1);
00302       if (!opt.Contains("r")) opt += "r";
00303    }
00304    else if (fLblDirT->GetState()==kButtonDown) {
00305       if (!opt.Contains("t")) opt += "t";
00306       if (opt.Contains("r")) opt.Remove(opt.First("r"),1);
00307    }
00308 
00309    SetDrawOption(opt);
00310    if (gPad) gPad->GetVirtCanvas()->SetCursor(kPointer);
00311    gVirtualX->SetCursor(GetId(), gVirtualX->CreateCursor(kPointer));
00312 }
00313 
00314 
00315 //______________________________________________________________________________
00316 void TPieEditor::DoMarkerOnOff(Bool_t)
00317 {
00318    // Slot for setting markers as visible/invisible.
00319 
00320    if (fAvoidSignal) return;
00321    TString t = GetDrawOption();
00322 
00323    if (t.Contains("nol") && fOutlineOnOff->GetState() == kButtonDown) {
00324       t.Remove(t.First("nol"),3);
00325    }
00326    else if (!t.Contains("nol") && fOutlineOnOff->GetState() == kButtonUp) {
00327       t += "nol";
00328    }
00329 
00330    SetDrawOption(t);
00331 }
00332 
00333 
00334 //______________________________________________________________________________
00335 void TPieEditor::DoChange3DAngle()
00336 {
00337    // Slot for setting the 3D angle
00338    if (fAvoidSignal) return;
00339 
00340    fPie->SetAngle3D(static_cast<Int_t>(f3DAngle->GetNumber()));
00341    fPie->SetHeight(f3DHeight->GetNumber());
00342 
00343    Update();
00344 
00345 }
00346 
00347 
00348 //______________________________________________________________________________
00349 void TPieEditor::DoGraphLineWidth()
00350 {
00351    // Slot connected to the graph line width.
00352 
00353    if (fAvoidSignal) return;
00354 
00355    TString opt = GetDrawOption();
00356    if (!opt.Contains("3d") && fIs3D->IsDown())
00357       opt += "3d";
00358    else if (opt.Contains("3d") && !fIs3D->IsDown())
00359       opt.Remove(opt.First("3d"),2);
00360 
00361    SetDrawOption(opt);
00362 
00363    Update();
00364 }
00365 
00366 
00367 
00368 //______________________________________________________________________________
00369 void TPieEditor::DoTextChange()
00370 {
00371    // Change text.
00372 
00373    if (fAvoidSignal) return;
00374 
00375    // font color
00376    fPie->SetTextColor(TColor::GetColor(fColorSelect->GetColor()));
00377 
00378    // font type
00379    Int_t fontPrec = fPie->GetTextFont()%10;
00380    Int_t fontType = fTypeCombo->GetSelected();
00381    fPie->SetTextFont(fontType*10+fontPrec);
00382 
00383    // font size
00384    TVirtualPad* pad = fGedEditor->GetPad();
00385 
00386    Float_t val = TString(fSizeCombo->GetSelectedEntry()->GetTitle()).Atoi();
00387 
00388    Float_t dy = pad->AbsPixeltoY(0) - pad->AbsPixeltoY((Int_t)val);
00389    Float_t textSize;
00390 
00391    if (fGedEditor->GetModel()->InheritsFrom(TPaveLabel::Class())) {
00392       TBox *pl = (TBox*)fGedEditor->GetModel();
00393       textSize = dy/(pl->GetY2() - pl->GetY1());
00394    }
00395    else
00396       textSize = dy/(pad->GetY2() - pad->GetY1());
00397 
00398    fPie->SetTextSize(textSize);
00399 
00400    Update();
00401 
00402 }
00403 
00404 
00405 //______________________________________________________________________________
00406 TGComboBox* TPieEditor::BuildFontSizeComboBox(TGFrame* parent, Int_t id)
00407 {
00408    // Create text size combo box.
00409 
00410    char a[100];
00411    TGComboBox *c = new TGComboBox(parent, id);
00412 
00413    c->AddEntry("Default", 0);
00414    for (int i = 1; i <= 50; i++) {
00415       snprintf(a, 99, "%d", i);
00416       c->AddEntry(a, i);
00417    }
00418 
00419    return c;
00420 }

Generated on Tue Jul 5 14:19:48 2011 for ROOT_528-00b_version by  doxygen 1.5.1