CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWTabularWidget.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: TableWidget
4 // Class : FWTabularWidget
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Mon Feb 2 16:45:21 EST 2009
11 //
12 
13 // system include files
14 #include <cassert>
15 #include <limits.h>
16 #include <iostream>
17 #include "TGResourcePool.h"
18 
19 // user include files
23 
24 
25 //
26 // constants, enums and typedefs
27 //
28 
29 //
30 // static data member definitions
31 //
32 
33 const int FWTabularWidget::kTextBuffer = 2;
35 
36 //
37 // constructors and destructor
38 //
39 FWTabularWidget::FWTabularWidget(FWTableManagerBase* iTable, const TGWindow* p, GContext_t context):
40 TGFrame(p),
41 m_table(iTable),
42 m_widthOfTextInColumns(m_table->numberOfColumns(),static_cast<unsigned int>(0)),
43 m_vOffset(0),
44 m_hOffset(0),
45 m_normGC(context),
46 m_backgroundGC(ULONG_MAX),
47 m_growInWidth(true)
48 {
49 
50  m_textHeight = iTable->cellHeight();
52 
54  for(std::vector<unsigned int>::const_iterator it = m_widthOfTextInColumns.begin(), itEnd = m_widthOfTextInColumns.end();
55  it!=itEnd;
56  ++it){
57  m_tableWidth +=*it;
58  }
59  Resize();
60 
61  gVirtualX->GrabButton(fId,kAnyButton, kAnyModifier, kButtonPressMask|kButtonReleaseMask,kNone,kNone);
62  m_table->Connect("visualPropertiesChanged()","FWTabularWidget",this,"needToRedraw()");
63 }
64 
65 // FWTabularWidget::FWTabularWidget(const FWTabularWidget& rhs)
66 // {
67 // // do actual copying here;
68 // }
69 
71 {
72  m_table->Disconnect("visualPropertiesChanged()", this, "needToRedraw()");
73 }
74 
75 //
76 // assignment operators
77 //
78 // const FWTabularWidget& FWTabularWidget::operator=(const FWTabularWidget& rhs)
79 // {
80 // //An exception safe implementation is
81 // FWTabularWidget temp(rhs);
82 // swap(rhs);
83 //
84 // return *this;
85 // }
86 
87 //
88 // member functions
89 //
90 void
92 {
95 }
96 
97 void
99 {
100  fClient->NeedRedraw(this);
101 }
102 
103 
104 void
105 FWTabularWidget::setWidthOfTextInColumns(const std::vector<unsigned int>& iNew)
106 {
107  assert(iNew.size() == static_cast<unsigned int>(m_table->numberOfColumns()));
108 
110  if (m_growInWidth)
111  {
112  // with of columns grow to prevent resizing/flickering on next event
113  m_widthOfTextInColumnsMax.resize(iNew.size());
114  std::vector<unsigned int>::iterator k = m_widthOfTextInColumnsMax.begin();
115  for(std::vector<unsigned int>::iterator it = m_widthOfTextInColumns.begin(); it != m_widthOfTextInColumns.end(); ++it, ++k)
116  {
117  if ( *it < *k )
118  *it = *k;
119  else
120  *k = *it;
121  }
122  }
123 
124  m_tableWidth=0;
125  for(std::vector<unsigned int>::const_iterator it = m_widthOfTextInColumns.begin(), itEnd = m_widthOfTextInColumns.end();
126  it!=itEnd;
127  ++it){
129  }
131 }
132 
133 void
135 {
136  if(iV != m_vOffset) {
137  m_vOffset = iV;
138  fClient->NeedRedraw(this);
139  }
140 }
141 void
143 {
144  if(iH != m_hOffset){
145  m_hOffset = iH;
146  fClient->NeedRedraw(this);
147  }
148 }
149 
150 Bool_t
152 {
153  if (event->fType==kButtonPress) {
154  Int_t row,col,relX,relY;
155  translateToRowColumn(event->fX, event->fY, row, col,relX,relY);
156  //std::cout <<"Press: "<<relX<<" "<<relY<<" "<<row<<" "<<col<<" "<<m_table->numberOfRows()<<" "<<m_table->numberOfColumns()<<std::endl;
157  if (row >= 0 && row < m_table->numberOfRows() && col >= 0 && col < m_table->numberOfColumns()) {
158  FWTableCellRendererBase* renderer = m_table->cellRenderer(row,col);
159  if (renderer) {
160  renderer->buttonEvent(event,relX,relY);
161  }
162  buttonPressed(row,col,event,relX,relY);
163  }
164  return true;
165  }
166  if (event->fType==kButtonRelease) {
167  Int_t row,col,relX,relY;
168  translateToRowColumn(event->fX, event->fY, row, col,relX, relY);
169  //std::cout <<"Release: "<<relX<<" "<<relY<<" "<<row<<" "<<col<<" "<<m_table->numberOfRows()<<" "<<m_table->numberOfColumns()<<std::endl;
170  if (row >= 0 && row < m_table->numberOfRows() && col >= 0 && col < m_table->numberOfColumns()) {
171  FWTableCellRendererBase* renderer = m_table->cellRenderer(row,col);
172  if (renderer) {
173  renderer->buttonEvent(event,relX,relY);
174  }
175  buttonReleased(row,col,event,relX,relY);
176  }
177  return true;
178  }
179  return false;
180 }
181 
182 void
183 FWTabularWidget::translateToRowColumn(Int_t iX, Int_t iY, Int_t& oRow, Int_t& oCol, Int_t& oRelX, Int_t& oRelY) const
184 {
185  if( iX < 0 ) {
186  oCol = -1;
187  oRelX = 0;
188  } else {
189  if(iX+static_cast<Int_t>(m_hOffset) > static_cast<Int_t>(m_tableWidth) ) {
190  oCol = m_widthOfTextInColumns.size();
191  oRelX = 0;
192  } else {
193  iX +=m_hOffset;
194  oCol = 0;
195  for(std::vector<unsigned int>::const_iterator it = m_widthOfTextInColumns.begin(), itEnd = m_widthOfTextInColumns.end();
196  it!=itEnd;
197  ++it,++oCol){
198  oRelX=iX-kTextBuffer;
199  iX-=2*kTextBuffer+kSeperatorWidth+*it;
200  if(iX <= 0) {
201  break;
202  }
203  }
204  }
205  }
206  if( iY < 0) {
207  oRow = -1;
208  oRelY=0;
209  } else {
210  oRow = (int)(float(iY+m_vOffset)/(m_textHeight+2*kTextBuffer+kSeperatorWidth));
212  Int_t numRows = m_table->numberOfRows();
213  if(oRow > numRows) {
214  oRow = numRows;
215  oRelY=0;
216  }
217  }
218 }
219 
220 void
221 FWTabularWidget::buttonPressed(Int_t row, Int_t column, Event_t* event, Int_t relX, Int_t relY)
222 {
223  //std::cout <<"buttonPressed "<<row<<" "<<column<<std::endl;
224  Long_t args[5];
225  args[0]=(Long_t)row;
226  args[1]=(Long_t)column;
227  args[2]=(Long_t)event;
228  args[3]=(Long_t)relX;
229  args[4]=(Long_t)relY;
230  Emit("buttonPressed(Int_t,Int_t,Event_t*,Int_t,Int_t)",args);
231 }
232 void
233 FWTabularWidget::buttonReleased(Int_t row, Int_t column, Event_t* event, Int_t relX, Int_t relY)
234 {
235  //std::cout <<"buttonReleased "<<row<<" "<<column<<std::endl;
236  Long_t args[6];
237  args[0]=(Long_t)row;
238  args[1]=(Long_t)column;
239  args[2]=(Long_t)event;
240  args[3]=(Long_t)relX;
241  args[4]=(Long_t)relY;
242  Emit("buttonReleased(Int_t,Int_t,Event_t*,Int_t,Int_t)",args);
243 }
244 
245 void
247 {
248  TGFrame::DoRedraw();
249 
250  //std::cout <<"DoRedraw "<<m_tableWidth<<std::endl;
251 
252  const int yOrigin = -m_vOffset;
253  const int xOrigin = -m_hOffset;
254  const int visibleWidth = m_tableWidth+xOrigin-kSeperatorWidth;
255  int y=yOrigin;
256  if(m_backgroundGC != ULONG_MAX) {
257  gVirtualX->FillRectangle(fId,m_backgroundGC,xOrigin,y,m_tableWidth,
258  GetHeight());
259  }
260  gVirtualX->DrawLine(fId, m_normGC, xOrigin, y, visibleWidth, y);
261  //Draw data
262  const int numRows=m_table->numberOfRows();
263 
264  //figure out which rows and columns are visible
265  Int_t startRow, startColumn,relX,relY;
266  translateToRowColumn(0,0,startRow,startColumn,relX,relY);
267  if(startRow<0) { startRow = 0;}
268  if(startColumn<0) { startColumn=0;}
269  Int_t endRow, endColumn;
270  translateToRowColumn(GetWidth(),GetHeight(),endRow,endColumn,relX,relY);
271  if(endRow >= numRows) {
272  endRow = numRows-1;
273  }
274  if(endColumn >= static_cast<Int_t>(m_widthOfTextInColumns.size())) {
275  endColumn = m_widthOfTextInColumns.size()-1;
276  }
277  //std::cout <<"start "<<startRow<<" "<<startColumn<<" end "<<endRow<<" "<<endColumn<<std::endl;
278 
279  //calculate offset for rows and columns
280  Int_t rowOffset = (kSeperatorWidth+2*kTextBuffer+m_textHeight)*startRow;
281  Int_t columnOffset=kSeperatorWidth+kTextBuffer+xOrigin;
282  for(std::vector<unsigned int>::iterator itTextWidth = m_widthOfTextInColumns.begin(), itEnd = m_widthOfTextInColumns.begin()+startColumn;
283  itTextWidth != itEnd; ++itTextWidth) {
284  columnOffset+=*itTextWidth+kTextBuffer+kSeperatorWidth+kTextBuffer;
285  }
286 
287 
288  y+=rowOffset;
289  for(int row = startRow; row <= endRow; ++row) {
290  std::vector<unsigned int>::iterator itTextWidth = m_widthOfTextInColumns.begin()+startColumn;
291  //int x=kSeperatorWidth+kTextBuffer+xOrigin;
292  int x = columnOffset;
294  for(int col = startColumn;
295  col <= endColumn;
296  ++col,++itTextWidth) {
297  m_table->cellRenderer(row,col)->draw(fId,x,y,*itTextWidth,m_textHeight);
298  //UInt_t textWidth = font->TextWidth(itData->c_str(),-1);
299  x+=*itTextWidth+kTextBuffer+kSeperatorWidth+kTextBuffer;
300  }
302  gVirtualX->DrawLine(fId, m_normGC, xOrigin, y, visibleWidth, y);
303  }
304 
305  //draw column separators
306  int x=xOrigin;
307  gVirtualX->DrawLine(fId,m_normGC,x,0,x,y);
308  x+=kSeperatorWidth;
309  for(std::vector<unsigned int>::iterator itTextWidth = m_widthOfTextInColumns.begin();
310  itTextWidth != m_widthOfTextInColumns.end();
311  ++itTextWidth) {
312  x+=2*kTextBuffer+*itTextWidth;
313  gVirtualX->DrawLine(fId,m_normGC,x,0,x,y);
314  x+=kSeperatorWidth;
315  }
316 }
317 
318 void
320 {
321  m_normGC = iContext;
322 }
323 void
325 {
326  m_backgroundGC = iContext;
327 }
328 
329 //
330 // const member functions
331 //
332 TGDimension
334 {
335  // returns default size
336 
337  UInt_t w = fWidth;
338  if(! (GetOptions() & kFixedWidth) ) {
339  w=m_tableWidth;
340  }
341  UInt_t h = fHeight;
342  if(! (GetOptions() & kFixedHeight) ) {
343  unsigned int numRows = m_table->numberOfRows();
344 
346  }
347  return TGDimension(w, h);
348 }
349 
350 //
351 // static member functions
352 //
353 const TGGC&
355 {
356  static const TGGC* s_default = gClient->GetResourcePool()->GetFrameGC();
357  return *s_default;
358 }
359 
360 ClassImp(FWTabularWidget)
void buttonReleased(Int_t row, Int_t column, Event_t *event, Int_t relX, Int_t relY)
virtual int numberOfColumns() const =0
Number of columns in the table.
const double w
Definition: UKUtility.cc:23
void translateToRowColumn(Int_t iX, Int_t iY, Int_t &oRow, Int_t &oCol, Int_t &oRelX, Int_t &oRelY) const
FWTabularWidget(FWTableManagerBase *iManager, const TGWindow *p=0, GContext_t context=getDefaultGC()())
unsigned int m_vOffset
void setLineContext(GContext_t iContext)
virtual void draw(Drawable_t iID, int iX, int iY, unsigned int iWidth, unsigned int iHeight)=0
void buttonPressed(Int_t row, Int_t column, Event_t *event, Int_t relX, Int_t relY)
virtual std::vector< unsigned int > maxWidthForColumns() const
for each column in the table this returns the present maximum width for that column ...
virtual int numberOfRows() const =0
Number of rows in the table.
void setWidthOfTextInColumns(const std::vector< unsigned int > &)
virtual FWTableCellRendererBase * cellRenderer(int iSortedRowNumber, int iCol) const =0
TGDimension GetDefaultSize() const
static const TGGC & getDefaultGC()
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
virtual Bool_t HandleButton(Event_t *event)
GContext_t m_backgroundGC
GContext_t m_normGC
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
std::vector< unsigned int > m_widthOfTextInColumns
void setHorizontalOffset(UInt_t)
#define column(...)
Definition: DbCore.h:74
unsigned int m_hOffset
FWTableManagerBase * m_table
void setBackgroundAreaContext(GContext_t iContext)
std::vector< unsigned int > m_widthOfTextInColumnsMax
virtual void buttonEvent(Event_t *iClickEvent, int iRelClickX, int iRelClickY)
static const int kSeperatorWidth
static const int kTextBuffer
virtual unsigned int cellHeight() const
require all cells to be the same height
Definition: DDAxes.h:10
void setVerticalOffset(UInt_t)
int col
Definition: cuy.py:1008
virtual ~FWTabularWidget()