CMS 3D CMS Logo

FWTableViewTableManager.cc
Go to the documentation of this file.
1 
2 #include <cmath>
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(nullptr),
21  m_renderer(nullptr),
22  m_rowContext(nullptr),
23  m_rowRenderer(nullptr),
24  m_tableFormats(nullptr),
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 
52 {
53  if (m_view->item() != nullptr)
54  return m_view->item()->size();
55  else return 0;
56 }
57 
59 {
60  return m_evaluators.size();
61 }
62 
63 std::vector<std::string> FWTableViewTableManager::getTitles () const
64 {
65  unsigned int n = numberOfColumns();
66  std::vector<std::string> ret;
67  ret.reserve(n);
68  for (unsigned int i = 0; i < n; ++i) {
69  ret.push_back(m_tableFormats->at(i).name);
70 // printf("%s\n", ret.back().c_str());
71  }
72  return ret;
73 }
74 
75 int FWTableViewTableManager::unsortedRowNumber(int iSortedRowNumber) const
76 {
77  if (iSortedRowNumber >= (int)m_sortedToUnsortedIndices.size())
78  return 0;
79  return m_sortedToUnsortedIndices[iSortedRowNumber];
80 }
81 
82 FWTableCellRendererBase *FWTableViewTableManager::cellRenderer(int iSortedRowNumber, int iCol) const
83 {
84  const int realRowNumber = unsortedRowNumber(iSortedRowNumber);
85  if (m_view->item() != nullptr &&
86  m_view->item()->size() &&
87  m_view->item()->modelData(realRowNumber) != nullptr &&
88  iCol < (int)m_evaluators.size()) {
89  double ret;
90  try {
91 // printf("iCol %d, size %d\n", iCol, m_evaluators.size());
92  ret = m_evaluators[iCol].evalExpression(m_view->item()->modelData(realRowNumber));
93  } catch (...) {
95  fwLog(fwlog::kError) << "Error: caught exception in the cell renderer while evaluating an expression. Return -999. Error is suppressed in future\n";
96  }
98  ret = -999;
99  }
100  int precision = m_tableFormats->at(iCol).precision;
101  char s[100];
102  char fs[100];
103  switch (precision) {
105  snprintf(s, sizeof(s), "%d", int(rint(ret)));
106  break;
108  snprintf(s, sizeof(s), "0x%x", int(rint(ret)));
109  break;
111  snprintf(s, sizeof(s), int(rint(ret)) != 0 ? "true" : "false");
112  break;
113  default:
114  snprintf(fs, sizeof(fs), "%%.%df", precision);
115  snprintf(s, sizeof(s), fs, ret);
116  break;
117  }
118  if (not m_view->item()->modelInfo(realRowNumber).isSelected()) {
119  if (m_view->item()->modelInfo(realRowNumber).displayProperties().isVisible())
120  if (m_view->m_manager->colorManager().background() == kBlack) {
122  SetForeground(gVirtualX->GetPixel(kWhite));
123  } else {
125  SetForeground(gVirtualX->GetPixel(kBlack));
126  }
127  else {
128  if (m_view->m_manager->colorManager().background() == kBlack) {
129  m_graphicsContext->SetForeground(0x888888);
130  } else {
131  m_graphicsContext->SetForeground(0x888888);
132  }
133  }
135  } else {
137  SetForeground(0xffffff);
139  }
140  m_renderer->setData(s, m_view->item()->modelInfo(realRowNumber).isSelected());
141  } else {
142  m_renderer->setData("invalid", false);
143  }
144  return m_renderer;
145 }
146 
147 namespace {
148  struct itemOrderGt {
149  bool operator () (const std::pair<bool, double> &i1,
150  const std::pair<bool, double> &i2) const
151  {
152  // sort first by visibility
153  if (i1.first and not i2.first)
154  return true;
155  if (i2.first and not i1.first)
156  return false;
157  // then by value
158  else return i1.second > i2.second;
159  }
160  };
161  struct itemOrderLt {
162  bool operator () (const std::pair<bool, double> &i1,
163  const std::pair<bool, double> &i2) const
164  {
165  // sort first by visibility
166  if (i1.first and not i2.first)
167  return true;
168  if (i2.first and not i1.first)
169  return false;
170  // then by value
171  else return i1.second < i2.second;
172  }
173  };
174  template<typename S>
175  void doSort(const FWEventItem& iItem,
176  int iCol,
177  const std::vector<FWExpressionEvaluator> &evaluators,
178  std::multimap<std::pair<bool, double>, int, S>& iMap,
179  std::vector<int>& oNewSort)
180  {
181  int size = iItem.size();
182  for(int index = 0; index < size; ++index) {
183  double ret;
184  try {
185 // printf("iCol %d, size %d\n", iCol, m_evaluators.size());
186  ret = evaluators[iCol].evalExpression(iItem.modelData(index));
187  } catch (...) {
188  ret = -999;
189  }
190  iMap.insert(std::make_pair(
191  std::make_pair(
192  iItem.modelInfo(index).displayProperties().isVisible(), ret),
193  index));
194  }
195  std::vector<int>::iterator itVec = oNewSort.begin();
196  for(typename std::multimap<std::pair<bool, double>,int,S>::iterator
197  it = iMap.begin(),
198  itEnd = iMap.end();
199  it != itEnd;
200  ++it,++itVec) {
201  *itVec = it->second;
202  }
203  }
204 }
205 
206 void FWTableViewTableManager::implSort(int iCol, bool iSortOrder)
207 {
208  static const bool sort_down = true;
209  if (iCol >= (int)m_evaluators.size())
210  return;
211  if (nullptr!=m_view->item()) {
212  // printf("sorting %s\n", iSortOrder == sort_down ? "down" : "up");
213  if (iSortOrder == sort_down) {
214  std::multimap<std::pair<bool, double>, int, itemOrderGt> s;
215  doSort(*m_view->item(), iCol, m_evaluators, s, m_sortedToUnsortedIndices);
216  } else {
217  std::multimap<std::pair<bool, double>, int, itemOrderLt> s;
218  doSort(*m_view->item(), iCol, m_evaluators, s, m_sortedToUnsortedIndices);
219  }
220  }
222 }
223 
224 void
226 {
227  if (nullptr!=m_view->item()) {
228  std::vector<int> visible;
229  visible.reserve(m_view->item()->size());
230  std::vector<int> invisible;
231  invisible.reserve(m_view->item()->size());
233  m_sortedToUnsortedIndices.reserve(m_view->item()->size());
234  for(int i=0; i< static_cast<int>(m_view->item()->size()); ++i) {
236  visible.push_back(i);
237  else invisible.push_back(i);
238  }
240  visible.begin(), visible.end());
242  invisible.begin(), invisible.end());
243  assert(m_sortedToUnsortedIndices.size() == m_view->item()->size());
244  } else {
246  }
248 }
249 
251 {
252  if (m_view->m_iColl == -1) {
253  //printf("what should I do with collection -1?\n");
254  m_evaluators.clear();
255  return;
256  }
257  const FWEventItem *item = m_view->m_manager->items()[m_view->m_iColl];
258  if(nullptr==item) { return;}
259  std::vector<FWExpressionEvaluator> &ev = m_evaluators;
260  ev.clear();
261  for (std::vector<FWTableViewManager::TableEntry>::const_iterator
262  i = m_tableFormats->begin(),
263  end = m_tableFormats->end();
264  i != end; ++i) {
265  try {
266  ev.push_back(FWExpressionEvaluator(i->expression, item->modelType()->GetName()));
267  } catch (...) {
268  fwLog(fwlog::kError) << "expression "<< i->expression << " is not valid, skipping\n";
269  ev.push_back(FWExpressionEvaluator("0", item->modelType()->GetName()));
270  }
271  }
272  //printf("Got evaluators\n");
273 }
274 
276 {
277  return true;
278 }
280 {
281  const int realRowNumber = unsortedRowNumber(iSortedRowNumber);
282  if (m_view->item() != nullptr &&
283  m_view->item()->size() &&
284  m_view->item()->modelData(realRowNumber) != nullptr) {
285  if (m_view->item()->modelInfo(realRowNumber).displayProperties().isVisible()) {
286  if (m_view->m_manager->colorManager().background() == kBlack) {
288  SetForeground(gVirtualX->GetPixel(kWhite));
289  } else {
291  SetForeground(gVirtualX->GetPixel(kBlack));
292  }
294  SetForeground(gVirtualX->GetPixel(m_view->item()->modelInfo(realRowNumber).
295  displayProperties().color()));
296  } else {
297  m_graphicsContext->SetForeground(0x888888);
299  }
300 
301  std::ostringstream s;
302  s<<realRowNumber;
303  m_rowRenderer->setData(s.str());
304  } else {
305  m_rowRenderer->setData("");
306  }
307  return m_rowRenderer;
308 }
309 
size
Write out results.
FWTableViewTableManager(const FWTableView *)
Color_t background() const
const FWDisplayProperties & displayProperties() const
Definition: FWEventItem.h:68
FWTableViewManager * m_manager
Definition: FWTableView.h:90
void dataChanged()
Called if mouse button pressed in Row Header, defaults is to do nothing.
FWFramedTextTableCellRenderer * m_rowRenderer
std::vector< int > m_sortedToUnsortedIndices
int numberOfColumns() const override
Number of columns in the table.
bool isSelected() const
Definition: FWEventItem.h:71
int unsortedRowNumber(int iSortedRowNumber) const override
std::vector< std::string > getTitles() const override
returns the title names for each column
std::vector< FWTableViewManager::TableEntry > * m_tableFormats
bool ev
FWTableCellRendererBase * rowHeader(int iSortedRowNumber) const override
Returns the renderer for the row header for the sorted row number iSortedRowNumber.
void dataChanged()
Classes which inherit from FWTableManagerBase must call this when their underlying data changes...
FWColorManager & colorManager() const
#define nullptr
std::vector< FWExpressionEvaluator > m_evaluators
void implSort(int iCol, bool iSortOrder) override
const Items & items() const
const FWEventItem * item() const
Definition: FWTableView.cc:579
FWTableWidget * m_tableWidget
Definition: FWTableView.h:92
#define end
Definition: vmac.h:39
void setGraphicsContext(const TGGC *iContext)
FWTextTableCellRenderer * m_renderer
void setData(const std::string &, bool isSelected)
size_t size() const
Definition: FWEventItem.cc:550
#define fwLog(_level_)
Definition: fwLog.h:50
int numberOfRows() const override
Number of rows in the table.
const TClass * modelType() const
Definition: FWEventItem.cc:563
FWTableCellRendererBase * cellRenderer(int iSortedRowNumber, int iCol) const override
const void * modelData(int iIndex) const
Definition: FWEventItem.cc:569
ModelInfo modelInfo(int iIndex) const
Definition: FWEventItem.cc:537
bool hasRowHeaders() const override
require all cells to be the same height