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 #include "TVirtualX.h"
9 
15 
18 
20  : m_view(view),
21  m_graphicsContext(nullptr),
22  m_renderer(nullptr),
23  m_rowContext(nullptr),
24  m_rowRenderer(nullptr),
25  m_tableFormats(nullptr),
26  m_caughtExceptionInCellRender(false) {
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));
32  m_renderer =
34  //m_rowContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc,kTRUE);
35  //m_rowContext->SetForeground(gVirtualX->GetPixel(kWhite));
36  //m_rowContext->SetBackground(gVirtualX->GetPixel(kBlack));
37  m_rowFillContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc, kTRUE);
40 }
41 
43  delete m_renderer;
44  delete m_rowRenderer;
45 }
46 
48  if (m_view->item() != nullptr)
49  return m_view->item()->size();
50  else
51  return 0;
52 }
53 
55 
56 std::vector<std::string> FWTableViewTableManager::getTitles() const {
57  unsigned int n = numberOfColumns();
58  std::vector<std::string> ret;
59  ret.reserve(n);
60  for (unsigned int i = 0; i < n; ++i) {
61  ret.push_back(m_tableFormats->at(i).name);
62  // printf("%s\n", ret.back().c_str());
63  }
64  return ret;
65 }
66 
67 int FWTableViewTableManager::unsortedRowNumber(int iSortedRowNumber) const {
68  if (iSortedRowNumber >= (int)m_sortedToUnsortedIndices.size())
69  return 0;
70  return m_sortedToUnsortedIndices[iSortedRowNumber];
71 }
72 
73 FWTableCellRendererBase *FWTableViewTableManager::cellRenderer(int iSortedRowNumber, int iCol) const {
74  const int realRowNumber = unsortedRowNumber(iSortedRowNumber);
75  if (m_view->item() != nullptr && m_view->item()->size() && m_view->item()->modelData(realRowNumber) != nullptr &&
76  iCol < (int)m_evaluators.size()) {
77  double ret;
78  try {
79  // printf("iCol %d, size %d\n", iCol, m_evaluators.size());
80  ret = m_evaluators[iCol].evalExpression(m_view->item()->modelData(realRowNumber));
81  } catch (...) {
83  fwLog(fwlog::kError) << "Error: caught exception in the cell renderer while evaluating an expression. Return "
84  "-999. Error is suppressed in future\n";
85  }
87  ret = -999;
88  }
89  int precision = m_tableFormats->at(iCol).precision;
90  char s[100];
91  char fs[100];
92  switch (precision) {
94  snprintf(s, sizeof(s), "%d", int(rint(ret)));
95  break;
97  snprintf(s, sizeof(s), "0x%x", int(rint(ret)));
98  break;
100  snprintf(s, sizeof(s), int(rint(ret)) != 0 ? "true" : "false");
101  break;
102  default:
103  snprintf(fs, sizeof(fs), "%%.%df", precision);
104  snprintf(s, sizeof(s), fs, ret);
105  break;
106  }
107  if (not m_view->item()->modelInfo(realRowNumber).isSelected()) {
108  if (m_view->item()->modelInfo(realRowNumber).displayProperties().isVisible())
109  if (m_view->m_manager->colorManager().background() == kBlack) {
110  m_graphicsContext->SetForeground(gVirtualX->GetPixel(kWhite));
111  } else {
112  m_graphicsContext->SetForeground(gVirtualX->GetPixel(kBlack));
113  }
114  else {
115  if (m_view->m_manager->colorManager().background() == kBlack) {
116  m_graphicsContext->SetForeground(0x888888);
117  } else {
118  m_graphicsContext->SetForeground(0x888888);
119  }
120  }
122  } else {
123  m_graphicsContext->SetForeground(0xffffff);
125  }
126  m_renderer->setData(s, m_view->item()->modelInfo(realRowNumber).isSelected());
127  } else {
128  m_renderer->setData("invalid", false);
129  }
130  return m_renderer;
131 }
132 
133 namespace {
134  struct itemOrderGt {
135  bool operator()(const std::pair<bool, double> &i1, const std::pair<bool, double> &i2) const {
136  // sort first by visibility
137  if (i1.first and not i2.first)
138  return true;
139  if (i2.first and not i1.first)
140  return false;
141  // then by value
142  else
143  return i1.second > i2.second;
144  }
145  };
146  struct itemOrderLt {
147  bool operator()(const std::pair<bool, double> &i1, const std::pair<bool, double> &i2) const {
148  // sort first by visibility
149  if (i1.first and not i2.first)
150  return true;
151  if (i2.first and not i1.first)
152  return false;
153  // then by value
154  else
155  return i1.second < i2.second;
156  }
157  };
158  template <typename S>
159  void doSort(const FWEventItem &iItem,
160  int iCol,
161  const std::vector<FWExpressionEvaluator> &evaluators,
162  std::multimap<std::pair<bool, double>, int, S> &iMap,
163  std::vector<int> &oNewSort) {
164  int size = iItem.size();
165  for (int index = 0; index < size; ++index) {
166  double ret;
167  try {
168  // printf("iCol %d, size %d\n", iCol, m_evaluators.size());
169  ret = evaluators[iCol].evalExpression(iItem.modelData(index));
170  } catch (...) {
171  ret = -999;
172  }
173  iMap.insert(std::make_pair(std::make_pair(iItem.modelInfo(index).displayProperties().isVisible(), ret), index));
174  }
175  std::vector<int>::iterator itVec = oNewSort.begin();
176  for (typename std::multimap<std::pair<bool, double>, int, S>::iterator it = iMap.begin(), itEnd = iMap.end();
177  it != itEnd;
178  ++it, ++itVec) {
179  *itVec = it->second;
180  }
181  }
182 } // namespace
183 
184 void FWTableViewTableManager::implSort(int iCol, bool iSortOrder) {
185  static const bool sort_down = true;
186  if (iCol >= (int)m_evaluators.size())
187  return;
188  if (nullptr != m_view->item()) {
189  // printf("sorting %s\n", iSortOrder == sort_down ? "down" : "up");
190  if (iSortOrder == sort_down) {
191  std::multimap<std::pair<bool, double>, int, itemOrderGt> s;
193  } else {
194  std::multimap<std::pair<bool, double>, int, itemOrderLt> s;
196  }
197  }
199 }
200 
202  if (nullptr != m_view->item()) {
203  std::vector<int> visible;
204  visible.reserve(m_view->item()->size());
205  std::vector<int> invisible;
206  invisible.reserve(m_view->item()->size());
208  m_sortedToUnsortedIndices.reserve(m_view->item()->size());
209  for (int i = 0; i < static_cast<int>(m_view->item()->size()); ++i) {
211  visible.push_back(i);
212  else
213  invisible.push_back(i);
214  }
215  m_sortedToUnsortedIndices.insert(m_sortedToUnsortedIndices.end(), visible.begin(), visible.end());
216  m_sortedToUnsortedIndices.insert(m_sortedToUnsortedIndices.end(), invisible.begin(), invisible.end());
218  } else {
220  }
222 }
223 
225  if (m_view->m_iColl == -1) {
226  //printf("what should I do with collection -1?\n");
227  m_evaluators.clear();
228  return;
229  }
231  if (nullptr == item) {
232  return;
233  }
234  std::vector<FWExpressionEvaluator> &ev = m_evaluators;
235  ev.clear();
236  for (std::vector<FWTableViewManager::TableEntry>::const_iterator i = m_tableFormats->begin(),
237  end = m_tableFormats->end();
238  i != end;
239  ++i) {
240  try {
241  ev.push_back(FWExpressionEvaluator(i->expression, item->modelType()->GetName()));
242  } catch (...) {
243  fwLog(fwlog::kError) << "expression " << i->expression << " is not valid, skipping\n";
244  ev.push_back(FWExpressionEvaluator("0", item->modelType()->GetName()));
245  }
246  }
247  //printf("Got evaluators\n");
248 }
249 
250 bool FWTableViewTableManager::hasRowHeaders() const { return true; }
252  const int realRowNumber = unsortedRowNumber(iSortedRowNumber);
253  if (m_view->item() != nullptr && m_view->item()->size() && m_view->item()->modelData(realRowNumber) != nullptr) {
254  if (m_view->item()->modelInfo(realRowNumber).displayProperties().isVisible()) {
255  if (m_view->m_manager->colorManager().background() == kBlack) {
256  m_graphicsContext->SetForeground(gVirtualX->GetPixel(kWhite));
257  } else {
258  m_graphicsContext->SetForeground(gVirtualX->GetPixel(kBlack));
259  }
260  m_rowFillContext->SetForeground(
261  gVirtualX->GetPixel(m_view->item()->modelInfo(realRowNumber).displayProperties().color()));
262  } else {
263  m_graphicsContext->SetForeground(0x888888);
265  }
266 
267  std::ostringstream s;
268  s << realRowNumber;
269  m_rowRenderer->setData(s.str());
270  } else {
271  m_rowRenderer->setData("");
272  }
273  return m_rowRenderer;
274 }
size
Write out results.
size_t size() const
Definition: FWEventItem.cc:457
FWTableViewTableManager(const FWTableView *)
const void * modelData(int iIndex) const
Definition: FWEventItem.cc:466
FWTableCellRendererBase * cellRenderer(int iSortedRowNumber, int iCol) const override
FWTableViewManager * m_manager
Definition: FWTableView.h:89
void dataChanged()
Called if mouse button pressed in Row Header, defaults is to do nothing.
bool hasRowHeaders() const override
require all cells to be the same height
FWFramedTextTableCellRenderer * m_rowRenderer
std::vector< int > m_sortedToUnsortedIndices
ret
prodAgent to be discontinued
FWColorManager & colorManager() const
std::vector< FWTableViewManager::TableEntry > * m_tableFormats
Color_t background() const
void dataChanged()
Classes which inherit from FWTableManagerBase must call this when their underlying data changes...
assert(be >=bs)
int unsortedRowNumber(int iSortedRowNumber) const override
const FWDisplayProperties & displayProperties() const
Definition: FWEventItem.h:64
FWTableCellRendererBase * rowHeader(int iSortedRowNumber) const override
Returns the renderer for the row header for the sorted row number iSortedRowNumber.
std::vector< FWExpressionEvaluator > m_evaluators
void implSort(int iCol, bool iSortOrder) override
bool isSelected() const
Definition: FWEventItem.h:65
int numberOfRows() const override
Number of rows in the table.
FWTableWidget * m_tableWidget
Definition: FWTableView.h:91
void setGraphicsContext(const TGGC *iContext)
FWTextTableCellRenderer * m_renderer
std::vector< std::string > getTitles() const override
returns the title names for each column
const Items & items() const
void setData(const std::string &, bool isSelected)
#define fwLog(_level_)
Definition: fwLog.h:45
const FWEventItem * item() const
Definition: FWTableView.cc:545
int numberOfColumns() const override
Number of columns in the table.
ModelInfo modelInfo(int iIndex) const
Definition: FWEventItem.cc:446