Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TAttLineEditor.cxx
Go to the documentation of this file.
1 // @(#)root/ged:$Id$
2 // Author: Ilka Antcheva 10/05/04
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2002, 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 // TAttLineEditor //
15 // //
16 // Implements GUI for editing line attributes. //
17 // color, line width, line style //
18 // //
19 //////////////////////////////////////////////////////////////////////////
20 //Begin_Html
21 /*
22 <img src="gif/TAttLineEditor.gif">
23 */
24 //End_Html
25 
26 
27 #include "TAttLineEditor.h"
28 #include "TGColorSelect.h"
29 #include "TGComboBox.h"
30 #include "TColor.h"
31 #include "TGraph.h"
32 #include "TGLabel.h"
33 #include "TGNumberEntry.h"
34 #include "TPad.h"
35 #include "TCanvas.h"
36 #include "TROOT.h"
37 
38 ClassImp(TAttLineEditor);
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// Constructor of line attributes GUI.
42 
43 TAttLineEditor::TAttLineEditor(const TGWindow *p, Int_t width,
44  Int_t height, UInt_t options, Pixel_t back)
45  : TGedFrame(p, width, height, options | kVerticalFrame, back)
46 {
47  enum ELineWid {
48  kCOLOR,
49  kLINE_WIDTH,
50  kLINE_STYLE,
51  kALPHA,
52  kALPHAFIELD
53  };
54 
55  fPriority = 1;
56  fAttLine = 0;
57 
58  MakeTitle("Line");
59 
60  TGCompositeFrame *f2 = new TGCompositeFrame(this, 80, 20, kHorizontalFrame);
61  AddFrame(f2, new TGLayoutHints(kLHintsTop, 1, 1, 0, 0));
62 
63  fColorSelect = new TGColorSelect(f2, 0, kCOLOR);
64  f2->AddFrame(fColorSelect, new TGLayoutHints(kLHintsLeft, 1, 1, 1, 1));
65  fColorSelect->Associate(this);
66 
67  fStyleCombo = new TGLineStyleComboBox(this, kLINE_STYLE);
68  fStyleCombo->Resize(137, 20);
69  AddFrame(fStyleCombo, new TGLayoutHints(kLHintsLeft, 3, 1, 1, 1));
70  fStyleCombo->Associate(this);
71 
72  fWidthCombo = new TGLineWidthComboBox(f2, kLINE_WIDTH);
73  fWidthCombo->Resize(90, 20);
74  f2->AddFrame(fWidthCombo, new TGLayoutHints(kLHintsLeft, 3, 1, 1, 1));
75  fWidthCombo->Associate(this);
76 
77  TGLabel *AlphaLabel = new TGLabel(this,"Opacity");
78  AddFrame(AlphaLabel,
79  new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
80  TGHorizontalFrame *f2a = new TGHorizontalFrame(this);
81  fAlpha = new TGHSlider(f2a,100,kSlider2|kScaleNo,kALPHA);
82  fAlpha->SetRange(0,1000);
83  f2a->AddFrame(fAlpha,new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
84  fAlphaField = new TGNumberEntryField(f2a, kALPHAFIELD, 0,
85  TGNumberFormat::kNESReal,
86  TGNumberFormat::kNEANonNegative);
87  fAlphaField->Resize(40,20);
88  if (!TCanvas::SupportAlpha()) {
89  fAlpha->SetEnabled(kFALSE);
90  AlphaLabel->Disable(kTRUE);
91  fAlphaField->SetEnabled(kFALSE);
92  }
93  f2a->AddFrame(fAlphaField,new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
94  AddFrame(f2a, new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Destructor of line editor.
99 
100 TAttLineEditor::~TAttLineEditor()
101 {
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// Connect signals to slots.
106 
107 void TAttLineEditor::ConnectSignals2Slots()
108 {
109  fColorSelect->Connect("ColorSelected(Pixel_t)", "TAttLineEditor", this, "DoLineColor(Pixel_t)");
110  fColorSelect->Connect("AlphaColorSelected(ULong_t)", "TAttLineEditor", this, "DoLineAlphaColor(ULong_t)");
111  fStyleCombo->Connect("Selected(Int_t)", "TAttLineEditor", this, "DoLineStyle(Int_t)");
112  fWidthCombo->Connect("Selected(Int_t)", "TAttLineEditor", this, "DoLineWidth(Int_t)");
113  fAlpha->Connect("Released()","TAttLineEditor", this, "DoAlpha()");
114  fAlpha->Connect("PositionChanged(Int_t)","TAttLineEditor", this, "DoLiveAlpha(Int_t)");
115  fAlphaField->Connect("ReturnPressed()","TAttLineEditor", this, "DoAlphaField()");
116  fAlpha->Connect("Pressed()","TAttLineEditor", this, "GetCurAlpha()");
117 
118  fInit = kFALSE;
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Pick up the used line attributes.
123 
124 void TAttLineEditor::SetModel(TObject* obj)
125 {
126  TAttLine *attline = dynamic_cast<TAttLine*>(obj);
127  if (!attline) return;
128 
129  fAttLine = attline;
130  fAvoidSignal = kTRUE;
131 
132  fStyleCombo->Select(fAttLine->GetLineStyle());
133 
134  if (obj->InheritsFrom(TGraph::Class())) {
135  fWidthCombo->Select(TMath::Abs(fAttLine->GetLineWidth()%100));
136  } else {
137  fWidthCombo->Select(fAttLine->GetLineWidth());
138  }
139 
140  Color_t c = fAttLine->GetLineColor();
141  Pixel_t p = TColor::Number2Pixel(c);
142  fColorSelect->SetColor(p);
143 
144  if (fInit) ConnectSignals2Slots();
145 
146  fAvoidSignal = kFALSE;
147 
148  if (TColor *color = gROOT->GetColor(fAttLine->GetLineColor())) {
149  fAlpha->SetPosition((Int_t)(color->GetAlpha()*1000));
150  fAlphaField->SetNumber(color->GetAlpha());
151  }
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// Slot connected to the line color.
156 
157 void TAttLineEditor::DoLineColor(Pixel_t color)
158 {
159  if (fAvoidSignal) return;
160  fAttLine->SetLineColor(TColor::GetColor(color));
161 
162  if (TColor *tcolor = gROOT->GetColor(TColor::GetColor(color))) {
163  fAlpha->SetPosition((Int_t)(tcolor->GetAlpha()*1000));
164  fAlphaField->SetNumber(tcolor->GetAlpha());
165  }
166 
167  Update();
168 }
169 
170 
171 ////////////////////////////////////////////////////////////////////////////////
172 /// Slot connected to the color with alpha.
173 
174 void TAttLineEditor::DoLineAlphaColor(ULong_t p)
175 {
176  TColor *color = (TColor *)p;
177 
178  if (fAvoidSignal) return;
179  fAttLine->SetLineColor(color->GetNumber());
180  fAlpha->SetPosition((Int_t)(color->GetAlpha()*1000));
181  fAlphaField->SetNumber(color->GetAlpha());
182 
183  Update();
184 }
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// Slot connected to the line style.
188 
189 void TAttLineEditor::DoLineStyle(Int_t style)
190 {
191  if (fAvoidSignal) return;
192  fAttLine->SetLineStyle(style);
193  Update();
194 }
195 
196 
197 ////////////////////////////////////////////////////////////////////////////////
198 /// Slot connected to the line width.
199 
200 void TAttLineEditor::DoLineWidth(Int_t width)
201 {
202  if (fAvoidSignal) return;
203  if (dynamic_cast<TGraph*>(fAttLine)) {
204  Int_t graphLineWidth = 100*Int_t(fAttLine->GetLineWidth()/100);
205  if (graphLineWidth >= 0) {
206  fAttLine->SetLineWidth(graphLineWidth+width);
207  } else {
208  fAttLine->SetLineWidth(-(TMath::Abs(graphLineWidth)+width));
209  }
210  } else {
211  fAttLine->SetLineWidth(width);
212  }
213  Update();
214 }
215 
216 ////////////////////////////////////////////////////////////////////////////////
217 /// Slot to set the alpha value from the entry field.
218 
219 void TAttLineEditor::DoAlphaField()
220 {
221  if (fAvoidSignal) return;
222 
223  if (TColor *color = gROOT->GetColor(fAttLine->GetLineColor())) {
224  color->SetAlpha((Float_t)fAlphaField->GetNumber());
225  fAlpha->SetPosition((Int_t)fAlphaField->GetNumber()*1000);
226  }
227  Update();
228 }
229 
230 ////////////////////////////////////////////////////////////////////////////////
231 /// Slot to set the alpha value
232 
233 void TAttLineEditor::DoAlpha()
234 {
235  if (fAvoidSignal) return;
236 
237  if (TColor *color = gROOT->GetColor(fAttLine->GetLineColor())) {
238  color->SetAlpha((Float_t)fAlpha->GetPosition()/1000);
239  fAlphaField->SetNumber((Float_t)fAlpha->GetPosition()/1000);
240  }
241  Update();
242 }
243 
244 ////////////////////////////////////////////////////////////////////////////////
245 /// Slot to set alpha value online.
246 
247 void TAttLineEditor::DoLiveAlpha(Int_t a)
248 {
249  if (fAvoidSignal) return;
250  fAlphaField->SetNumber((Float_t)a/1000);
251 
252  if (TColor *color = gROOT->GetColor(fAttLine->GetLineColor())) {
253  // In case the color is not transparent a new color is created.
254  if (color->GetAlpha() == 1.) {
255  fAttLine->SetLineColor(TColor::GetColorTransparent(color->GetNumber(),0.99));
256  } else {
257  color->SetAlpha((Float_t)a/1000);
258  }
259  }
260  Update();
261 }
262 
263 ////////////////////////////////////////////////////////////////////////////////
264 /// Slot to update alpha value on click on Slider
265 
266 void TAttLineEditor::GetCurAlpha()
267 {
268  if (fAvoidSignal) return;
269 
270  if (TColor *color = gROOT->GetColor(fAttLine->GetLineColor())) {
271  fAlpha->SetPosition((Int_t)(color->GetAlpha()*1000));
272  fAlphaField->SetNumber(color->GetAlpha());
273  }
274  Update();
275 }