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  GCValues_t gc = *(m_view->m_tableWidget->GetWhiteGC().GetAttributes());
27  m_graphicsContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc, kTRUE);
28  m_highlightContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc, kTRUE);
29  m_highlightContext->SetForeground(gVirtualX->GetPixel(kBlue));
30  m_highlightContext->SetBackground(gVirtualX->GetPixel(kBlue));
31  m_renderer =
33  //m_rowContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc,kTRUE);
34  //m_rowContext->SetForeground(gVirtualX->GetPixel(kWhite));
35  //m_rowContext->SetBackground(gVirtualX->GetPixel(kBlack));
36  m_rowFillContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc, kTRUE);
39 }
40 
42  delete m_renderer;
43  delete m_rowRenderer;
44 }
45 
47  if (m_view->item() != nullptr)
48  return m_view->item()->size();
49  else
50  return 0;
51 }
52 
54 
55 std::vector<std::string> FWTableViewTableManager::getTitles() const {
56  unsigned int n = numberOfColumns();
57  std::vector<std::string> ret;
58  ret.reserve(n);
59  for (unsigned int i = 0; i < n; ++i) {
60  ret.push_back(m_tableFormats->at(i).name);
61  // printf("%s\n", ret.back().c_str());
62  }
63  return ret;
64 }
65 
66 int FWTableViewTableManager::unsortedRowNumber(int iSortedRowNumber) const {
67  if (iSortedRowNumber >= (int)m_sortedToUnsortedIndices.size())
68  return 0;
69  return m_sortedToUnsortedIndices[iSortedRowNumber];
70 }
71 
72 FWTableCellRendererBase *FWTableViewTableManager::cellRenderer(int iSortedRowNumber, int iCol) const {
73  const int realRowNumber = unsortedRowNumber(iSortedRowNumber);
74  if (m_view->item() != nullptr && m_view->item()->size() && m_view->item()->modelData(realRowNumber) != nullptr &&
75  iCol < (int)m_evaluators.size()) {
76  double ret;
77  try {
78  // printf("iCol %d, size %d\n", iCol, m_evaluators.size());
79  ret = m_evaluators[iCol].evalExpression(m_view->item()->modelData(realRowNumber));
80  } catch (...) {
82  fwLog(fwlog::kError) << "Error: caught exception in the cell renderer while evaluating an expression. Return "
83  "-999. Error is suppressed in future\n";
84  }
86  ret = -999;
87  }
88  int precision = m_tableFormats->at(iCol).precision;
89  char s[100];
90  char fs[100];
91  switch (precision) {
93  snprintf(s, sizeof(s), "%d", int(rint(ret)));
94  break;
96  snprintf(s, sizeof(s), "0x%x", int(rint(ret)));
97  break;
99  snprintf(s, sizeof(s), int(rint(ret)) != 0 ? "true" : "false");
100  break;
101  default:
102  snprintf(fs, sizeof(fs), "%%.%df", precision);
103  snprintf(s, sizeof(s), fs, ret);
104  break;
105  }
106  if (not m_view->item()->modelInfo(realRowNumber).isSelected()) {
107  if (m_view->item()->modelInfo(realRowNumber).displayProperties().isVisible())
108  if (m_view->m_manager->colorManager().background() == kBlack) {
109  m_graphicsContext->SetForeground(gVirtualX->GetPixel(kWhite));
110  } else {
111  m_graphicsContext->SetForeground(gVirtualX->GetPixel(kBlack));
112  }
113  else {
114  if (m_view->m_manager->colorManager().background() == kBlack) {
115  m_graphicsContext->SetForeground(0x888888);
116  } else {
117  m_graphicsContext->SetForeground(0x888888);
118  }
119  }
121  } else {
122  m_graphicsContext->SetForeground(0xffffff);
124  }
125  m_renderer->setData(s, m_view->item()->modelInfo(realRowNumber).isSelected());
126  } else {
127  m_renderer->setData("invalid", false);
128  }
129  return m_renderer;
130 }
131 
132 namespace {
133  struct itemOrderGt {
134  bool operator()(const std::pair<bool, double> &i1, const std::pair<bool, double> &i2) const {
135  // sort first by visibility
136  if (i1.first and not i2.first)
137  return true;
138  if (i2.first and not i1.first)
139  return false;
140  // then by value
141  else
142  return i1.second > i2.second;
143  }
144  };
145  struct itemOrderLt {
146  bool operator()(const std::pair<bool, double> &i1, const std::pair<bool, double> &i2) const {
147  // sort first by visibility
148  if (i1.first and not i2.first)
149  return true;
150  if (i2.first and not i1.first)
151  return false;
152  // then by value
153  else
154  return i1.second < i2.second;
155  }
156  };
157  template <typename S>
158  void doSort(const FWEventItem &iItem,
159  int iCol,
160  const std::vector<FWExpressionEvaluator> &evaluators,
161  std::multimap<std::pair<bool, double>, int, S> &iMap,
162  std::vector<int> &oNewSort) {
163  int size = iItem.size();
164  for (int index = 0; index < size; ++index) {
165  double ret;
166  try {
167  // printf("iCol %d, size %d\n", iCol, m_evaluators.size());
168  ret = evaluators[iCol].evalExpression(iItem.modelData(index));
169  } catch (...) {
170  ret = -999;
171  }
172  iMap.insert(std::make_pair(std::make_pair(iItem.modelInfo(index).displayProperties().isVisible(), ret), index));
173  }
174  std::vector<int>::iterator itVec = oNewSort.begin();
175  for (typename std::multimap<std::pair<bool, double>, int, S>::iterator it = iMap.begin(), itEnd = iMap.end();
176  it != itEnd;
177  ++it, ++itVec) {
178  *itVec = it->second;
179  }
180  }
181 } // namespace
182 
183 void FWTableViewTableManager::implSort(int iCol, bool iSortOrder) {
184  static const bool sort_down = true;
185  if (iCol >= (int)m_evaluators.size())
186  return;
187  if (nullptr != m_view->item()) {
188  // printf("sorting %s\n", iSortOrder == sort_down ? "down" : "up");
189  if (iSortOrder == sort_down) {
190  std::multimap<std::pair<bool, double>, int, itemOrderGt> s;
191  doSort(*m_view->item(), iCol, m_evaluators, s, m_sortedToUnsortedIndices);
192  } else {
193  std::multimap<std::pair<bool, double>, int, itemOrderLt> s;
194  doSort(*m_view->item(), iCol, m_evaluators, s, m_sortedToUnsortedIndices);
195  }
196  }
198 }
199 
201  if (nullptr != m_view->item()) {
202  std::vector<int> visible;
203  visible.reserve(m_view->item()->size());
204  std::vector<int> invisible;
205  invisible.reserve(m_view->item()->size());
207  m_sortedToUnsortedIndices.reserve(m_view->item()->size());
208  for (int i = 0; i < static_cast<int>(m_view->item()->size()); ++i) {
210  visible.push_back(i);
211  else
212  invisible.push_back(i);
213  }
214  m_sortedToUnsortedIndices.insert(m_sortedToUnsortedIndices.end(), visible.begin(), visible.end());
215  m_sortedToUnsortedIndices.insert(m_sortedToUnsortedIndices.end(), invisible.begin(), invisible.end());
216  assert(m_sortedToUnsortedIndices.size() == m_view->item()->size());
217  } else {
219  }
221 }
222 
224  if (m_view->m_iColl == -1) {
225  //printf("what should I do with collection -1?\n");
226  m_evaluators.clear();
227  return;
228  }
230  if (nullptr == item) {
231  return;
232  }
233  std::vector<FWExpressionEvaluator> &ev = m_evaluators;
234  ev.clear();
235  for (std::vector<FWTableViewManager::TableEntry>::const_iterator i = m_tableFormats->begin(),
236  end = m_tableFormats->end();
237  i != end;
238  ++i) {
239  try {
240  ev.push_back(FWExpressionEvaluator(i->expression, item->modelType()->GetName()));
241  } catch (...) {
242  fwLog(fwlog::kError) << "expression " << i->expression << " is not valid, skipping\n";
243  ev.push_back(FWExpressionEvaluator("0", item->modelType()->GetName()));
244  }
245  }
246  //printf("Got evaluators\n");
247 }
248 
249 bool FWTableViewTableManager::hasRowHeaders() const { return true; }
251  const int realRowNumber = unsortedRowNumber(iSortedRowNumber);
252  if (m_view->item() != nullptr && m_view->item()->size() && m_view->item()->modelData(realRowNumber) != nullptr) {
253  if (m_view->item()->modelInfo(realRowNumber).displayProperties().isVisible()) {
254  if (m_view->m_manager->colorManager().background() == kBlack) {
255  m_graphicsContext->SetForeground(gVirtualX->GetPixel(kWhite));
256  } else {
257  m_graphicsContext->SetForeground(gVirtualX->GetPixel(kBlack));
258  }
259  m_rowFillContext->SetForeground(
260  gVirtualX->GetPixel(m_view->item()->modelInfo(realRowNumber).displayProperties().color()));
261  } else {
262  m_graphicsContext->SetForeground(0x888888);
264  }
265 
266  std::ostringstream s;
267  s << realRowNumber;
268  m_rowRenderer->setData(s.str());
269  } else {
270  m_rowRenderer->setData("");
271  }
272  return m_rowRenderer;
273 }
size
Write out results.
FWTableViewTableManager(const FWTableView *)
Color_t background() const
const FWDisplayProperties & displayProperties() const
Definition: FWEventItem.h:64
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
ret
prodAgent to be discontinued
int numberOfColumns() const override
Number of columns in the table.
bool isSelected() const
Definition: FWEventItem.h:65
#define nullptr
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
Color_t color() const
std::vector< FWExpressionEvaluator > m_evaluators
void implSort(int iCol, bool iSortOrder) override
const Items & items() const
const FWEventItem * item() const
Definition: FWTableView.cc:544
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:457
#define fwLog(_level_)
Definition: fwLog.h:45
int numberOfRows() const override
Number of rows in the table.
const TClass * modelType() const
Definition: FWEventItem.cc:464
FWTableCellRendererBase * cellRenderer(int iSortedRowNumber, int iCol) const override
const void * modelData(int iIndex) const
Definition: FWEventItem.cc:466
ModelInfo modelInfo(int iIndex) const
Definition: FWEventItem.cc:446
bool hasRowHeaders() const override
require all cells to be the same height