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 
2 #include <math.h>
3 #include <sstream>
4 #include <cassert>
5 
6 #include "TClass.h"
7 #include "TGClient.h"
8 
14 
17 
19  : m_view(view),
20  m_graphicsContext(0),
21  m_renderer(0),
22  m_rowContext(0),
23  m_rowRenderer(0),
24  m_tableFormats(0),
25  m_caughtExceptionInCellRender(false)
26 {
27  GCValues_t gc = *(m_view->m_tableWidget->GetWhiteGC().GetAttributes());
28  m_graphicsContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc,kTRUE);
29  m_highlightContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc,kTRUE);
30  m_highlightContext->SetForeground(gVirtualX->GetPixel(kBlue));
31  m_highlightContext->SetBackground(gVirtualX->GetPixel(kBlue));
35  //m_rowContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc,kTRUE);
36  //m_rowContext->SetForeground(gVirtualX->GetPixel(kWhite));
37  //m_rowContext->SetBackground(gVirtualX->GetPixel(kBlack));
38  m_rowFillContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc,kTRUE);
42 
43 }
44 
46 {
47  delete m_renderer;
48  delete m_rowRenderer;
49 }
50 
51 const FWEventItem*
53 {
54  return m_view->item();
55 }
56 
58 {
59  if (collection () != 0) {
60  if (collection()->showFilteredEntries() || collection()->filterExpression().empty())
61  {
62  return collection()->size();
63  }
64  else
65  {
66  int cs = collection()->size();
67  int n = 0;
68  for(int index = 0; index < cs; ++index) {
69  if (collection()->modelInfo(index).displayProperties().filterPassed()) { ++n;}
70  }
71  return n;
72  }
73  }
74  else return 0;
75 }
76 
78 {
79  return m_evaluators.size();
80 }
81 
82 std::vector<std::string> FWTableViewTableManager::getTitles () const
83 {
84  unsigned int n = numberOfColumns();
85  std::vector<std::string> ret;
86  ret.reserve(n);
87  for (unsigned int i = 0; i < n; ++i) {
88  ret.push_back(m_tableFormats->at(i).name);
89 // printf("%s\n", ret.back().c_str());
90  }
91  return ret;
92 }
93 
94 int FWTableViewTableManager::unsortedRowNumber(int iSortedRowNumber) const
95 {
96  if (iSortedRowNumber >= (int)m_sortedToUnsortedIndices.size())
97  return 0;
98  return m_sortedToUnsortedIndices[iSortedRowNumber];
99 }
100 
102 {
103  const int realRowNumber = unsortedRowNumber(iSortedRowNumber);
104  if (m_view->item() != 0 &&
105  m_view->item()->size() &&
106  m_view->item()->modelData(realRowNumber) != 0 &&
107  iCol < (int)m_evaluators.size()) {
108  double ret;
109  try {
110 // printf("iCol %d, size %d\n", iCol, m_evaluators.size());
111  ret = m_evaluators[iCol].evalExpression(m_view->item()->modelData(realRowNumber));
112  } catch (...) {
114  fwLog(fwlog::kError) << "Error: caught exception in the cell renderer while evaluating an expression. Return -999. Error is suppressed in future\n";
115  }
117  ret = -999;
118  }
119  int precision = m_tableFormats->at(iCol).precision;
120  char s[100];
121  char fs[100];
122  switch (precision) {
124  snprintf(s, sizeof(s), "%d", int(rint(ret)));
125  break;
127  snprintf(s, sizeof(s), "0x%x", int(rint(ret)));
128  break;
130  snprintf(s, sizeof(s), int(rint(ret)) != 0 ? "true" : "false");
131  break;
132  default:
133  snprintf(fs, sizeof(fs), "%%.%df", precision);
134  snprintf(s, sizeof(s), fs, ret);
135  break;
136  }
137  if (not m_view->item()->modelInfo(realRowNumber).isSelected()) {
138  if (m_view->item()->modelInfo(realRowNumber).displayProperties().isVisible())
139  if (m_view->m_manager->colorManager().background() == kBlack) {
141  SetForeground(gVirtualX->GetPixel(kWhite));
142  } else {
144  SetForeground(gVirtualX->GetPixel(kBlack));
145  }
146  else {
147  if (m_view->m_manager->colorManager().background() == kBlack) {
148  m_graphicsContext->SetForeground(0x888888);
149  } else {
150  m_graphicsContext->SetForeground(0x888888);
151  }
152  }
154  } else {
156  SetForeground(0xffffff);
158  }
159  m_renderer->setData(s, m_view->item()->modelInfo(realRowNumber).isSelected());
160  } else {
161  m_renderer->setData("invalid", false);
162  }
163  return m_renderer;
164 }
165 
166 namespace {
167  struct itemOrderGt {
168  bool operator () (const std::pair<bool, double> &i1,
169  const std::pair<bool, double> &i2)
170  {
171  // sort first by visibility
172  if (i1.first and not i2.first)
173  return true;
174  if (i2.first and not i1.first)
175  return false;
176  // then by value
177  else return i1.second > i2.second;
178  }
179  };
180  struct itemOrderLt {
181  bool operator () (const std::pair<bool, double> &i1,
182  const std::pair<bool, double> &i2)
183  {
184  // sort first by visibility
185  if (i1.first and not i2.first)
186  return true;
187  if (i2.first and not i1.first)
188  return false;
189  // then by value
190  else return i1.second < i2.second;
191  }
192  };
193  template<typename S>
194  void doSort(const FWEventItem& iItem,
195  int iCol,
196  const std::vector<FWExpressionEvaluator> &evaluators,
197  std::multimap<std::pair<bool, double>, int, S>& iMap,
198  std::vector<int>& oNewSort)
199  {
200  int size = iItem.size();
201  for(int index = 0; index < size; ++index) {
203  {
204  double ret;
205  try {
206 // printf("iCol %d, size %d\n", iCol, m_evaluators.size());
207  ret = evaluators[iCol].evalExpression(iItem.modelData(index));
208  } catch (...) {
209  ret = -999;
210  }
211  iMap.insert(std::make_pair(
212  std::make_pair(
214  index));
215  }
216  }
217  std::vector<int>::iterator itVec = oNewSort.begin();
218  for(typename std::multimap<std::pair<bool, double>,int,S>::iterator
219  it = iMap.begin(),
220  itEnd = iMap.end();
221  it != itEnd;
222  ++it,++itVec) {
223  *itVec = it->second;
224  }
225  }
226 }
227 
228 void FWTableViewTableManager::implSort(int iCol, bool iSortOrder)
229 {
230  static const bool sort_down = true;
231  if (iCol >= (int)m_evaluators.size())
232  return;
233  if (0!=m_view->item()) {
234  // printf("sorting %s\n", iSortOrder == sort_down ? "down" : "up");
235  if (iSortOrder == sort_down) {
236  std::multimap<std::pair<bool, double>, int, itemOrderGt> s;
237  doSort(*m_view->item(), iCol, m_evaluators, s, m_sortedToUnsortedIndices);
238  } else {
239  std::multimap<std::pair<bool, double>, int, itemOrderLt> s;
240  doSort(*m_view->item(), iCol, m_evaluators, s, m_sortedToUnsortedIndices);
241  }
242  }
244 }
245 
246 void
248 {
249  if (0!=m_view->item()) {
250  std::vector<int> visible;
251  visible.reserve(m_view->item()->size());
252  std::vector<int> invisible;
253  invisible.reserve(m_view->item()->size());
255  m_sortedToUnsortedIndices.reserve(m_view->item()->size());
256  for(int i=0; i< static_cast<int>(m_view->item()->size()); ++i) {
258  {
260  visible.push_back(i);
261  else invisible.push_back(i);
262  }
263  }
265  visible.begin(), visible.end());
267  invisible.begin(), invisible.end());
268 
271 
272  } else {
274  }
276 }
277 
279 {
280  if (m_view->m_iColl == -1) {
281  //printf("what should I do with collection -1?\n");
282  m_evaluators.clear();
283  return;
284  }
285  const FWEventItem *item = m_view->m_manager->items()[m_view->m_iColl];
286  if(0==item) { return;}
287  std::vector<FWExpressionEvaluator> &ev = m_evaluators;
288  ev.clear();
289  for (std::vector<FWTableViewManager::TableEntry>::const_iterator
290  i = m_tableFormats->begin(),
291  end = m_tableFormats->end();
292  i != end; ++i) {
293  try {
294  ev.push_back(FWExpressionEvaluator(i->expression, item->modelType()->GetName()));
295  } catch (...) {
296  fwLog(fwlog::kError) << "expression "<< i->expression << " is not valid, skipping\n";
297  ev.push_back(FWExpressionEvaluator("0", item->modelType()->GetName()));
298  }
299  }
300  //printf("Got evaluators\n");
301 }
302 
304 {
305  return true;
306 }
308 {
309  const int realRowNumber = unsortedRowNumber(iSortedRowNumber);
310  if (m_view->item() != 0 &&
311  m_view->item()->size() &&
312  m_view->item()->modelData(realRowNumber) != 0) {
313  if (m_view->item()->modelInfo(realRowNumber).displayProperties().isVisible()) {
314  if (m_view->m_manager->colorManager().background() == kBlack) {
316  SetForeground(gVirtualX->GetPixel(kWhite));
317  } else {
319  SetForeground(gVirtualX->GetPixel(kBlack));
320  }
322  SetForeground(gVirtualX->GetPixel(m_view->item()->modelInfo(realRowNumber).
323  displayProperties().color()));
324  } else {
325  m_graphicsContext->SetForeground(0x888888);
327  }
328 
329  std::ostringstream s;
330  s<<realRowNumber;
331  m_rowRenderer->setData(s.str().c_str());
332  } else {
333  m_rowRenderer->setData("");
334  }
335  return m_rowRenderer;
336 }
337 
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:67
auto_ptr< ClusterSequence > cs
FWTableViewManager * m_manager
Definition: FWTableView.h:90
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.
const FWEventItem * collection() const
FWFramedTextTableCellRenderer * m_rowRenderer
std::vector< int > m_sortedToUnsortedIndices
bool isSelected() const
Definition: FWEventItem.h:70
assert(m_qm.get())
const std::string & filterExpression() const
Definition: FWEventItem.cc:616
std::vector< FWTableViewManager::TableEntry > * m_tableFormats
bool ev
bool filterPassed() const
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:578
FWTableWidget * m_tableWidget
Definition: FWTableView.h:92
virtual void implSort(int iCol, bool iSortOrder)
#define end
Definition: vmac.h:37
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
bool showFilteredEntries() const
Definition: FWEventItem.h:100
void setData(const std::string &, bool isSelected)
size_t size() const
Definition: FWEventItem.cc:562
#define fwLog(_level_)
Definition: fwLog.h:50
double S(const TLorentzVector &, const TLorentzVector &)
Definition: Particle.cc:99
const TClass * modelType() const
Definition: FWEventItem.cc:575
volatile std::atomic< bool > shutdown_flag false
const void * modelData(int iIndex) const
Definition: FWEventItem.cc:581
ModelInfo modelInfo(int iIndex) const
Definition: FWEventItem.cc:548
tuple size
Write out results.