Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:27:19

0001 
0002 #include <cmath>
0003 #include <sstream>
0004 #include <cassert>
0005 
0006 #include "TClass.h"
0007 #include "TGClient.h"
0008 #include "TVirtualX.h"
0009 
0010 #include "Fireworks/Core/interface/FWTableViewTableManager.h"
0011 #include "Fireworks/Core/interface/FWTableView.h"
0012 #include "Fireworks/Core/interface/FWEventItem.h"
0013 #include "Fireworks/Core/interface/FWColorManager.h"
0014 #include "Fireworks/Core/interface/fwLog.h"
0015 
0016 #include "Fireworks/TableWidget/interface/FWTableWidget.h"
0017 #include "Fireworks/TableWidget/interface/FWFramedTextTableCellRenderer.h"
0018 
0019 FWTableViewTableManager::FWTableViewTableManager(const FWTableView *view)
0020     : m_view(view),
0021       m_graphicsContext(nullptr),
0022       m_renderer(nullptr),
0023       m_rowContext(nullptr),
0024       m_rowRenderer(nullptr),
0025       m_tableFormats(nullptr),
0026       m_caughtExceptionInCellRender(false) {
0027   GCValues_t gc = *(m_view->m_tableWidget->GetWhiteGC().GetAttributes());
0028   m_graphicsContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc, kTRUE);
0029   m_highlightContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc, kTRUE);
0030   m_highlightContext->SetForeground(gVirtualX->GetPixel(kBlue));
0031   m_highlightContext->SetBackground(gVirtualX->GetPixel(kBlue));
0032   m_renderer =
0033       new FWTextTableCellRenderer(m_graphicsContext, m_highlightContext, FWTextTableCellRenderer::kJustifyRight);
0034   //m_rowContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc,kTRUE);
0035   //m_rowContext->SetForeground(gVirtualX->GetPixel(kWhite));
0036   //m_rowContext->SetBackground(gVirtualX->GetPixel(kBlack));
0037   m_rowFillContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gc, kTRUE);
0038   m_rowRenderer = new FWFramedTextTableCellRenderer(
0039       m_graphicsContext, m_rowFillContext, FWFramedTextTableCellRenderer::kJustifyRight);
0040 }
0041 
0042 FWTableViewTableManager::~FWTableViewTableManager() {
0043   delete m_renderer;
0044   delete m_rowRenderer;
0045 }
0046 
0047 int FWTableViewTableManager::numberOfRows() const {
0048   if (m_view->item() != nullptr)
0049     return m_view->item()->size();
0050   else
0051     return 0;
0052 }
0053 
0054 int FWTableViewTableManager::numberOfColumns() const { return m_evaluators.size(); }
0055 
0056 std::vector<std::string> FWTableViewTableManager::getTitles() const {
0057   unsigned int n = numberOfColumns();
0058   std::vector<std::string> ret;
0059   ret.reserve(n);
0060   for (unsigned int i = 0; i < n; ++i) {
0061     ret.push_back(m_tableFormats->at(i).name);
0062     //    printf("%s\n", ret.back().c_str());
0063   }
0064   return ret;
0065 }
0066 
0067 int FWTableViewTableManager::unsortedRowNumber(int iSortedRowNumber) const {
0068   if (iSortedRowNumber >= (int)m_sortedToUnsortedIndices.size())
0069     return 0;
0070   return m_sortedToUnsortedIndices[iSortedRowNumber];
0071 }
0072 
0073 FWTableCellRendererBase *FWTableViewTableManager::cellRenderer(int iSortedRowNumber, int iCol) const {
0074   const int realRowNumber = unsortedRowNumber(iSortedRowNumber);
0075   if (m_view->item() != nullptr && m_view->item()->size() && m_view->item()->modelData(realRowNumber) != nullptr &&
0076       iCol < (int)m_evaluators.size()) {
0077     double ret;
0078     try {
0079       //           printf("iCol %d, size %d\n", iCol, m_evaluators.size());
0080       ret = m_evaluators[iCol].evalExpression(m_view->item()->modelData(realRowNumber));
0081     } catch (...) {
0082       if (!m_caughtExceptionInCellRender) {
0083         fwLog(fwlog::kError) << "Error: caught exception in the cell renderer while evaluating an expression. Return "
0084                                 "-999. Error is suppressed in future\n";
0085       }
0086       m_caughtExceptionInCellRender = true;
0087       ret = -999;
0088     }
0089     int precision = m_tableFormats->at(iCol).precision;
0090     char s[100];
0091     char fs[100];
0092     switch (precision) {
0093       case FWTableViewManager::TableEntry::INT:
0094         snprintf(s, sizeof(s), "%d", int(rint(ret)));
0095         break;
0096       case FWTableViewManager::TableEntry::INT_HEX:
0097         snprintf(s, sizeof(s), "0x%x", int(rint(ret)));
0098         break;
0099       case FWTableViewManager::TableEntry::BOOL:
0100         snprintf(s, sizeof(s), int(rint(ret)) != 0 ? "true" : "false");
0101         break;
0102       default:
0103         snprintf(fs, sizeof(fs), "%%.%df", precision);
0104         snprintf(s, sizeof(s), fs, ret);
0105         break;
0106     }
0107     if (not m_view->item()->modelInfo(realRowNumber).isSelected()) {
0108       if (m_view->item()->modelInfo(realRowNumber).displayProperties().isVisible())
0109         if (m_view->m_manager->colorManager().background() == kBlack) {
0110           m_graphicsContext->SetForeground(gVirtualX->GetPixel(kWhite));
0111         } else {
0112           m_graphicsContext->SetForeground(gVirtualX->GetPixel(kBlack));
0113         }
0114       else {
0115         if (m_view->m_manager->colorManager().background() == kBlack) {
0116           m_graphicsContext->SetForeground(0x888888);
0117         } else {
0118           m_graphicsContext->SetForeground(0x888888);
0119         }
0120       }
0121       m_renderer->setGraphicsContext(m_graphicsContext);
0122     } else {
0123       m_graphicsContext->SetForeground(0xffffff);
0124       m_renderer->setGraphicsContext(m_graphicsContext);
0125     }
0126     m_renderer->setData(s, m_view->item()->modelInfo(realRowNumber).isSelected());
0127   } else {
0128     m_renderer->setData("invalid", false);
0129   }
0130   return m_renderer;
0131 }
0132 
0133 namespace {
0134   struct itemOrderGt {
0135     bool operator()(const std::pair<bool, double> &i1, const std::pair<bool, double> &i2) const {
0136       // sort first by visibility
0137       if (i1.first and not i2.first)
0138         return true;
0139       if (i2.first and not i1.first)
0140         return false;
0141       // then by value
0142       else
0143         return i1.second > i2.second;
0144     }
0145   };
0146   struct itemOrderLt {
0147     bool operator()(const std::pair<bool, double> &i1, const std::pair<bool, double> &i2) const {
0148       // sort first by visibility
0149       if (i1.first and not i2.first)
0150         return true;
0151       if (i2.first and not i1.first)
0152         return false;
0153       // then by value
0154       else
0155         return i1.second < i2.second;
0156     }
0157   };
0158   template <typename S>
0159   void doSort(const FWEventItem &iItem,
0160               int iCol,
0161               const std::vector<FWExpressionEvaluator> &evaluators,
0162               std::multimap<std::pair<bool, double>, int, S> &iMap,
0163               std::vector<int> &oNewSort) {
0164     int size = iItem.size();
0165     for (int index = 0; index < size; ++index) {
0166       double ret;
0167       try {
0168         //         printf("iCol %d, size %d\n", iCol, m_evaluators.size());
0169         ret = evaluators[iCol].evalExpression(iItem.modelData(index));
0170       } catch (...) {
0171         ret = -999;
0172       }
0173       iMap.insert(std::make_pair(std::make_pair(iItem.modelInfo(index).displayProperties().isVisible(), ret), index));
0174     }
0175     std::vector<int>::iterator itVec = oNewSort.begin();
0176     for (typename std::multimap<std::pair<bool, double>, int, S>::iterator it = iMap.begin(), itEnd = iMap.end();
0177          it != itEnd;
0178          ++it, ++itVec) {
0179       *itVec = it->second;
0180     }
0181   }
0182 }  // namespace
0183 
0184 void FWTableViewTableManager::implSort(int iCol, bool iSortOrder) {
0185   static const bool sort_down = true;
0186   if (iCol >= (int)m_evaluators.size())
0187     return;
0188   if (nullptr != m_view->item()) {
0189     //      printf("sorting %s\n", iSortOrder == sort_down ? "down" : "up");
0190     if (iSortOrder == sort_down) {
0191       std::multimap<std::pair<bool, double>, int, itemOrderGt> s;
0192       doSort(*m_view->item(), iCol, m_evaluators, s, m_sortedToUnsortedIndices);
0193     } else {
0194       std::multimap<std::pair<bool, double>, int, itemOrderLt> s;
0195       doSort(*m_view->item(), iCol, m_evaluators, s, m_sortedToUnsortedIndices);
0196     }
0197   }
0198   m_view->m_tableWidget->dataChanged();
0199 }
0200 
0201 void FWTableViewTableManager::dataChanged() {
0202   if (nullptr != m_view->item()) {
0203     std::vector<int> visible;
0204     visible.reserve(m_view->item()->size());
0205     std::vector<int> invisible;
0206     invisible.reserve(m_view->item()->size());
0207     m_sortedToUnsortedIndices.clear();
0208     m_sortedToUnsortedIndices.reserve(m_view->item()->size());
0209     for (int i = 0; i < static_cast<int>(m_view->item()->size()); ++i) {
0210       if (m_view->item()->modelInfo(i).displayProperties().isVisible())
0211         visible.push_back(i);
0212       else
0213         invisible.push_back(i);
0214     }
0215     m_sortedToUnsortedIndices.insert(m_sortedToUnsortedIndices.end(), visible.begin(), visible.end());
0216     m_sortedToUnsortedIndices.insert(m_sortedToUnsortedIndices.end(), invisible.begin(), invisible.end());
0217     assert(m_sortedToUnsortedIndices.size() == m_view->item()->size());
0218   } else {
0219     m_sortedToUnsortedIndices.clear();
0220   }
0221   FWTableManagerBase::dataChanged();
0222 }
0223 
0224 void FWTableViewTableManager::updateEvaluators() {
0225   if (m_view->m_iColl == -1) {
0226     //printf("what should I do with collection -1?\n");
0227     m_evaluators.clear();
0228     return;
0229   }
0230   const FWEventItem *item = m_view->m_manager->items()[m_view->m_iColl];
0231   if (nullptr == item) {
0232     return;
0233   }
0234   std::vector<FWExpressionEvaluator> &ev = m_evaluators;
0235   ev.clear();
0236   for (std::vector<FWTableViewManager::TableEntry>::const_iterator i = m_tableFormats->begin(),
0237                                                                    end = m_tableFormats->end();
0238        i != end;
0239        ++i) {
0240     try {
0241       ev.push_back(FWExpressionEvaluator(i->expression, item->modelType()->GetName()));
0242     } catch (...) {
0243       fwLog(fwlog::kError) << "expression " << i->expression << " is not valid, skipping\n";
0244       ev.push_back(FWExpressionEvaluator("0", item->modelType()->GetName()));
0245     }
0246   }
0247   //printf("Got evaluators\n");
0248 }
0249 
0250 bool FWTableViewTableManager::hasRowHeaders() const { return true; }
0251 FWTableCellRendererBase *FWTableViewTableManager::rowHeader(int iSortedRowNumber) const {
0252   const int realRowNumber = unsortedRowNumber(iSortedRowNumber);
0253   if (m_view->item() != nullptr && m_view->item()->size() && m_view->item()->modelData(realRowNumber) != nullptr) {
0254     if (m_view->item()->modelInfo(realRowNumber).displayProperties().isVisible()) {
0255       if (m_view->m_manager->colorManager().background() == kBlack) {
0256         m_graphicsContext->SetForeground(gVirtualX->GetPixel(kWhite));
0257       } else {
0258         m_graphicsContext->SetForeground(gVirtualX->GetPixel(kBlack));
0259       }
0260       m_rowFillContext->SetForeground(
0261           gVirtualX->GetPixel(m_view->item()->modelInfo(realRowNumber).displayProperties().color()));
0262     } else {
0263       m_graphicsContext->SetForeground(0x888888);
0264       m_rowFillContext->SetForeground(m_view->m_manager->colorManager().background());
0265     }
0266 
0267     std::ostringstream s;
0268     s << realRowNumber;
0269     m_rowRenderer->setData(s.str());
0270   } else {
0271     m_rowRenderer->setData("");
0272   }
0273   return m_rowRenderer;
0274 }