CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWTableViewTableManager.cc
Go to the documentation of this file.
1 // $Id: FWTableViewTableManager.cc,v 1.18 2012/09/21 09:26:26 eulisse Exp $
2 
3 #include <math.h>
4 #include <sstream>
5 #include <cassert>
6 
7 #include "TClass.h"
8 #include "TGClient.h"
9 
15 
18 
20  : m_view(view),
21  m_graphicsContext(0),
22  m_renderer(0),
23  m_rowContext(0),
24  m_rowRenderer(0),
25  m_tableFormats(0),
26  m_caughtExceptionInCellRender(false)
27 {
28  GCValues_t gc = *(m_view->m_tableWidget->GetWhiteGC().GetAttributes());
29  m_graphicsContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc,kTRUE);
30  m_highlightContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc,kTRUE);
31  m_highlightContext->SetForeground(gVirtualX->GetPixel(kBlue));
32  m_highlightContext->SetBackground(gVirtualX->GetPixel(kBlue));
36  //m_rowContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc,kTRUE);
37  //m_rowContext->SetForeground(gVirtualX->GetPixel(kWhite));
38  //m_rowContext->SetBackground(gVirtualX->GetPixel(kBlack));
39  m_rowFillContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc,kTRUE);
43 
44 }
45 
47 {
48  delete m_renderer;
49  delete m_rowRenderer;
50 }
51 
53 {
54  if (m_view->item() != 0)
55  return m_view->item()->size();
56  else return 0;
57 }
58 
60 {
61  return m_evaluators.size();
62 }
63 
64 std::vector<std::string> FWTableViewTableManager::getTitles () const
65 {
66  unsigned int n = numberOfColumns();
67  std::vector<std::string> ret;
68  ret.reserve(n);
69  for (unsigned int i = 0; i < n; ++i) {
70  ret.push_back(m_tableFormats->at(i).name);
71 // printf("%s\n", ret.back().c_str());
72  }
73  return ret;
74 }
75 
76 int FWTableViewTableManager::unsortedRowNumber(int iSortedRowNumber) const
77 {
78  if (iSortedRowNumber >= (int)m_sortedToUnsortedIndices.size())
79  return 0;
80  return m_sortedToUnsortedIndices[iSortedRowNumber];
81 }
82 
83 FWTableCellRendererBase *FWTableViewTableManager::cellRenderer(int iSortedRowNumber, int iCol) const
84 {
85  const int realRowNumber = unsortedRowNumber(iSortedRowNumber);
86  if (m_view->item() != 0 &&
87  m_view->item()->size() &&
88  m_view->item()->modelData(realRowNumber) != 0 &&
89  iCol < (int)m_evaluators.size()) {
90  double ret;
91  try {
92 // printf("iCol %d, size %d\n", iCol, m_evaluators.size());
93  ret = m_evaluators[iCol].evalExpression(m_view->item()->modelData(realRowNumber));
94  } catch (...) {
96  fwLog(fwlog::kError) << "Error: caught exception in the cell renderer while evaluating an expression. Return -999. Error is suppressed in future\n";
97  }
99  ret = -999;
100  }
101  int precision = m_tableFormats->at(iCol).precision;
102  char s[100];
103  char fs[100];
104  switch (precision) {
106  snprintf(s, sizeof(s), "%d", int(rint(ret)));
107  break;
109  snprintf(s, sizeof(s), "0x%x", int(rint(ret)));
110  break;
112  snprintf(s, sizeof(s), int(rint(ret)) != 0 ? "true" : "false");
113  break;
114  default:
115  snprintf(fs, sizeof(fs), "%%.%df", precision);
116  snprintf(s, sizeof(s), fs, ret);
117  break;
118  }
119  if (not m_view->item()->modelInfo(realRowNumber).isSelected()) {
120  if (m_view->item()->modelInfo(realRowNumber).displayProperties().isVisible())
121  if (m_view->m_manager->colorManager().background() == kBlack) {
123  SetForeground(gVirtualX->GetPixel(kWhite));
124  } else {
126  SetForeground(gVirtualX->GetPixel(kBlack));
127  }
128  else {
129  if (m_view->m_manager->colorManager().background() == kBlack) {
130  m_graphicsContext->SetForeground(0x888888);
131  } else {
132  m_graphicsContext->SetForeground(0x888888);
133  }
134  }
136  } else {
138  SetForeground(0xffffff);
140  }
141  m_renderer->setData(s, m_view->item()->modelInfo(realRowNumber).isSelected());
142  } else {
143  m_renderer->setData("invalid", false);
144  }
145  return m_renderer;
146 }
147 
148 namespace {
149  struct itemOrderGt {
150  bool operator () (const std::pair<bool, double> &i1,
151  const std::pair<bool, double> &i2)
152  {
153  // sort first by visibility
154  if (i1.first and not i2.first)
155  return true;
156  if (i2.first and not i1.first)
157  return false;
158  // then by value
159  else return i1.second > i2.second;
160  }
161  };
162  struct itemOrderLt {
163  bool operator () (const std::pair<bool, double> &i1,
164  const std::pair<bool, double> &i2)
165  {
166  // sort first by visibility
167  if (i1.first and not i2.first)
168  return true;
169  if (i2.first and not i1.first)
170  return false;
171  // then by value
172  else return i1.second < i2.second;
173  }
174  };
175  template<typename S>
176  void doSort(const FWEventItem& iItem,
177  int iCol,
178  const std::vector<FWExpressionEvaluator> &evaluators,
179  std::multimap<std::pair<bool, double>, int, S>& iMap,
180  std::vector<int>& oNewSort)
181  {
182  int size = iItem.size();
183  for(int index = 0; index < size; ++index) {
184  double ret;
185  try {
186 // printf("iCol %d, size %d\n", iCol, m_evaluators.size());
187  ret = evaluators[iCol].evalExpression(iItem.modelData(index));
188  } catch (...) {
189  ret = -999;
190  }
191  iMap.insert(std::make_pair(
192  std::make_pair(
194  index));
195  }
196  std::vector<int>::iterator itVec = oNewSort.begin();
197  for(typename std::multimap<std::pair<bool, double>,int,S>::iterator
198  it = iMap.begin(),
199  itEnd = iMap.end();
200  it != itEnd;
201  ++it,++itVec) {
202  *itVec = it->second;
203  }
204  }
205 }
206 
207 void FWTableViewTableManager::implSort(int iCol, bool iSortOrder)
208 {
209  static const bool sort_down = true;
210  if (iCol >= (int)m_evaluators.size())
211  return;
212  if (0!=m_view->item()) {
213  // printf("sorting %s\n", iSortOrder == sort_down ? "down" : "up");
214  if (iSortOrder == sort_down) {
215  std::multimap<std::pair<bool, double>, int, itemOrderGt> s;
216  doSort(*m_view->item(), iCol, m_evaluators, s, m_sortedToUnsortedIndices);
217  } else {
218  std::multimap<std::pair<bool, double>, int, itemOrderLt> s;
219  doSort(*m_view->item(), iCol, m_evaluators, s, m_sortedToUnsortedIndices);
220  }
221  }
223 }
224 
225 void
227 {
228  if (0!=m_view->item()) {
229  std::vector<int> visible;
230  visible.reserve(m_view->item()->size());
231  std::vector<int> invisible;
232  invisible.reserve(m_view->item()->size());
234  m_sortedToUnsortedIndices.reserve(m_view->item()->size());
235  for(int i=0; i< static_cast<int>(m_view->item()->size()); ++i) {
237  visible.push_back(i);
238  else invisible.push_back(i);
239  }
241  visible.begin(), visible.end());
243  invisible.begin(), invisible.end());
244  assert(m_sortedToUnsortedIndices.size() == m_view->item()->size());
245  } else {
247  }
249 }
250 
252 {
253  if (m_view->m_iColl == -1) {
254  //printf("what should I do with collection -1?\n");
255  m_evaluators.clear();
256  return;
257  }
258  const FWEventItem *item = m_view->m_manager->items()[m_view->m_iColl];
259  if(0==item) { return;}
260  std::vector<FWExpressionEvaluator> &ev = m_evaluators;
261  ev.clear();
262  for (std::vector<FWTableViewManager::TableEntry>::const_iterator
263  i = m_tableFormats->begin(),
264  end = m_tableFormats->end();
265  i != end; ++i) {
266  try {
267  ev.push_back(FWExpressionEvaluator(i->expression, item->modelType()->GetName()));
268  } catch (...) {
269  fwLog(fwlog::kError) << "expression "<< i->expression << " is not valid, skipping\n";
270  ev.push_back(FWExpressionEvaluator("0", item->modelType()->GetName()));
271  }
272  }
273  //printf("Got evaluators\n");
274 }
275 
277 {
278  return true;
279 }
281 {
282  const int realRowNumber = unsortedRowNumber(iSortedRowNumber);
283  if (m_view->item() != 0 &&
284  m_view->item()->size() &&
285  m_view->item()->modelData(realRowNumber) != 0) {
286  if (m_view->item()->modelInfo(realRowNumber).displayProperties().isVisible()) {
287  if (m_view->m_manager->colorManager().background() == kBlack) {
289  SetForeground(gVirtualX->GetPixel(kWhite));
290  } else {
292  SetForeground(gVirtualX->GetPixel(kBlack));
293  }
295  SetForeground(gVirtualX->GetPixel(m_view->item()->modelInfo(realRowNumber).
296  displayProperties().color()));
297  } else {
298  m_graphicsContext->SetForeground(0x888888);
300  }
301 
302  std::ostringstream s;
303  s<<realRowNumber;
304  m_rowRenderer->setData(s.str().c_str());
305  } else {
306  m_rowRenderer->setData("");
307  }
308  return m_rowRenderer;
309 }
310 
virtual FWTableCellRendererBase * cellRenderer(int iSortedRowNumber, int iCol) const
int i
Definition: DBlmapReader.cc:9
FWTableViewTableManager(const FWTableView *)
Color_t background() const
const FWDisplayProperties & displayProperties() const
Definition: FWEventItem.h:68
FWTableViewManager * m_manager
Definition: FWTableView.h:91
void dataChanged()
Called if mouse button pressed in Row Header, defaults is to do nothing.
virtual int unsortedRowNumber(int iSortedRowNumber) const
virtual int numberOfRows() const
Number of rows in the table.
FWFramedTextTableCellRenderer * m_rowRenderer
std::vector< int > m_sortedToUnsortedIndices
bool isSelected() const
Definition: FWEventItem.h:71
std::vector< FWTableViewManager::TableEntry > * m_tableFormats
virtual std::vector< std::string > getTitles() const
returns the title names for each column
void dataChanged()
Classes which inherit from FWTableManagerBase must call this when their underlying data changes...
FWColorManager & colorManager() const
virtual int numberOfColumns() const
Number of columns in the table.
std::vector< FWExpressionEvaluator > m_evaluators
const Items & items() const
virtual bool hasRowHeaders() const
require all cells to be the same height
const FWEventItem * item() const
Definition: FWTableView.cc:579
FWTableWidget * m_tableWidget
Definition: FWTableView.h:93
virtual void implSort(int iCol, bool iSortOrder)
#define end
Definition: vmac.h:38
void setGraphicsContext(const TGGC *iContext)
virtual FWTableCellRendererBase * rowHeader(int iSortedRowNumber) const
Returns the renderer for the row header for the sorted row number iSortedRowNumber.
FWTextTableCellRenderer * m_renderer
void setData(const std::string &, bool isSelected)
size_t size() const
Definition: FWEventItem.cc:549
#define fwLog(_level_)
Definition: fwLog.h:51
const TClass * modelType() const
Definition: FWEventItem.cc:562
const void * modelData(int iIndex) const
Definition: FWEventItem.cc:568
ModelInfo modelInfo(int iIndex) const
Definition: FWEventItem.cc:536
tuple size
Write out results.