Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:39

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWGeometryTableManagerBase
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Alja Mrak-Tadel, Matevz Tadel
0010 //         Created:  Thu Jan 27 14:50:57 CET 2011
0011 //
0012 
0013 //#define PERFTOOL_GEO_TABLE
0014 
0015 // user include files
0016 #include <iostream>
0017 #include <functional>
0018 #include <stack>
0019 #ifdef PERFTOOL_GEO_TABLE
0020 #include <google/profiler.h>
0021 #endif
0022 #include "Fireworks/Core/interface/FWGeometryTableManagerBase.h"
0023 #include "Fireworks/Core/src/FWColorBoxIcon.h"
0024 #include "Fireworks/TableWidget/interface/GlobalContexts.h"
0025 #include "Fireworks/TableWidget/interface/FWTabularWidget.h"
0026 #include "Fireworks/Core/interface/fwLog.h"
0027 
0028 #include "TMath.h"
0029 #include "TGeoVolume.h"
0030 #include "TGeoMatrix.h"
0031 #include "TGeoShape.h"
0032 #include "TGeoBBox.h"
0033 #include "TGeoMatrix.h"
0034 #include "TVirtualX.h"
0035 #include "TGFrame.h"
0036 #include "TEveUtil.h"
0037 
0038 const char* FWGeometryTableManagerBase::NodeInfo::name() const { return m_node->GetName(); }
0039 
0040 FWGeometryTableManagerBase::ColorBoxRenderer::ColorBoxRenderer()
0041     : FWTableCellRendererBase(), m_width(1), m_height(1), m_color(0xffffff), m_isSelected(false) {
0042   GCValues_t gval;
0043   gval.fMask = kGCForeground | kGCBackground | kGCStipple | kGCFillStyle | kGCGraphicsExposures;
0044   gval.fStipple = gClient->GetResourcePool()->GetCheckeredBitmap();
0045   gval.fGraphicsExposures = kFALSE;
0046   gval.fBackground = gVirtualX->GetPixel(kGray);
0047   m_colorContext = gClient->GetResourcePool()->GetGCPool()->GetGC(&gval, kTRUE);
0048 }
0049 
0050 FWGeometryTableManagerBase::ColorBoxRenderer::~ColorBoxRenderer() {
0051   gClient->GetResourcePool()->GetGCPool()->FreeGC(m_colorContext->GetGC());
0052 }
0053 
0054 void FWGeometryTableManagerBase::ColorBoxRenderer::setData(Color_t c, bool s) {
0055   m_color = gVirtualX->GetPixel(c);
0056   m_isSelected = s;
0057 }
0058 
0059 void FWGeometryTableManagerBase::ColorBoxRenderer::draw(
0060     Drawable_t iID, int iX, int iY, unsigned int iWidth, unsigned int iHeight) {
0061   iX -= FWTabularWidget::kTextBuffer;
0062   iY -= FWTabularWidget::kTextBuffer;
0063   iWidth += 2 * FWTabularWidget::kTextBuffer;
0064   iHeight += 2 * FWTabularWidget::kTextBuffer;
0065 
0066   m_colorContext->SetFillStyle(kFillSolid);
0067   Pixel_t baq = m_colorContext->GetForeground();
0068   m_colorContext->SetForeground(m_color);
0069   gVirtualX->FillRectangle(iID, m_colorContext->GetGC(), iX, iY, iWidth, iHeight);
0070 
0071   if (m_isSelected) {
0072     m_colorContext->SetFillStyle(kFillOpaqueStippled);
0073     gVirtualX->FillRectangle(iID, m_colorContext->GetGC(), iX, iY, iWidth, iHeight);
0074   }
0075   m_colorContext->SetForeground(baq);
0076 }
0077 
0078 //==============================================================================
0079 //==============================================================================
0080 //
0081 // class FWGeometryTableManagerBase
0082 //
0083 //==============================================================================
0084 //==============================================================================
0085 
0086 FWGeometryTableManagerBase::FWGeometryTableManagerBase()
0087     : m_highlightIdx(-1), m_levelOffset(0), m_editor(nullptr), m_editTransparencyIdx(-1) {
0088   m_colorBoxRenderer.m_width = 50;
0089   m_colorBoxRenderer.m_height = m_renderer.height();
0090 
0091   GCValues_t gval;
0092   gval.fMask = kGCForeground | kGCBackground | kGCStipple | kGCFillStyle | kGCGraphicsExposures;
0093   gval.fForeground = gVirtualX->GetPixel(kGray);   //gClient->GetResourcePool()->GetFrameHiliteColor();
0094   gval.fBackground = gVirtualX->GetPixel(kWhite);  //gClient->GetResourcePool()->GetFrameBgndColor();
0095   gval.fFillStyle = kFillOpaqueStippled;           // kFillTiled;
0096   gval.fStipple = gClient->GetResourcePool()->GetCheckeredBitmap();
0097   gval.fGraphicsExposures = kFALSE;
0098   m_highlightContext = gClient->GetGC(&gval, kTRUE);
0099 
0100   m_renderer.setHighlightContext(m_highlightContext);
0101 }
0102 
0103 FWGeometryTableManagerBase::~FWGeometryTableManagerBase() {}
0104 
0105 int FWGeometryTableManagerBase::unsortedRowNumber(int unsorted) const { return unsorted; }
0106 
0107 int FWGeometryTableManagerBase::numberOfRows() const { return m_row_to_index.size(); }
0108 
0109 std::vector<std::string> FWGeometryTableManagerBase::getTitles() const {
0110   std::vector<std::string> returnValue;
0111   returnValue.reserve(numberOfColumns());
0112 
0113   returnValue.push_back("Name");
0114   returnValue.push_back("Color");
0115   returnValue.push_back("Opcty");
0116   returnValue.push_back("RnrSelf");
0117   returnValue.push_back("RnrChildren");
0118   returnValue.push_back("Material");
0119   return returnValue;
0120 }
0121 
0122 const std::string FWGeometryTableManagerBase::title() const { return "Geometry"; }
0123 
0124 void FWGeometryTableManagerBase::setBackgroundToWhite(bool iToWhite) {
0125   if (iToWhite) {
0126     m_renderer.setGraphicsContext(&TGFrame::GetBlackGC());
0127   } else {
0128     m_renderer.setGraphicsContext(&TGFrame::GetWhiteGC());
0129   }
0130   m_renderer.setBlackIcon(iToWhite);
0131 }
0132 
0133 //______________________________________________________________________________
0134 bool FWGeometryTableManagerBase::firstColumnClicked(int row, int xPos) {
0135   if (row == -1)
0136     return false;
0137 
0138   int idx = rowToIndex()[row];
0139   // printf("click %s \n", m_entries[idx].name());
0140 
0141   int off = 0;
0142   if (idx >= 0)
0143     off = (m_entries[idx].m_level - m_levelOffset) * 20;
0144 
0145   //   printf("compare %d %d level %d\n" , xPos, off, idx);
0146   if (xPos > off && xPos < (off + 20)) {
0147     m_entries[idx].switchBit(kExpanded);
0148 
0149     recalculateVisibility();
0150     dataChanged();
0151     visualPropertiesChanged();
0152     return false;
0153   }
0154 
0155   return true;
0156 }
0157 
0158 //______________________________________________________________________________
0159 
0160 void FWGeometryTableManagerBase::getNodeMatrix(const NodeInfo& data, TGeoHMatrix& mtx) const {
0161   // utility used by browser and FWGeoNode
0162   //   printf("================ FWGeometryTableManagerBase::getNodeMatri \n");
0163   int pIdx = data.m_parent;
0164 
0165   while (pIdx > 0) {
0166     // printf("%s [%d]\n",m_entries.at(pIdx).name(), m_entries.at(pIdx).m_level );
0167     mtx.MultiplyLeft(m_entries.at(pIdx).m_node->GetMatrix());
0168     pIdx = m_entries.at(pIdx).m_parent;
0169   }
0170 
0171   //   printf("right %s [%d]\n",data.name(), data.m_level );
0172   mtx.Multiply(data.m_node->GetMatrix());
0173 }
0174 
0175 //______________________________________________________________________________
0176 void FWGeometryTableManagerBase::redrawTable(bool setExpand) {
0177   //   std::cerr << "GeometryTableManagerBase::redrawTable ------------------------------------- \n";
0178   if (m_entries.empty())
0179     return;
0180 
0181   //   if (setExpand) checkExpandLevel();
0182 
0183   recalculateVisibility();
0184 
0185   dataChanged();
0186   visualPropertiesChanged();
0187 }
0188 
0189 //______________________________________________________________________________
0190 
0191 void FWGeometryTableManagerBase::getNodePath(int idx, std::string& path) const {
0192   std::vector<std::string> relPath;
0193   while (idx >= 0) {
0194     relPath.push_back(m_entries[idx].name());
0195     // printf("push %s \n",m_entries[idx].name() );
0196     idx = m_entries[idx].m_parent;
0197   }
0198 
0199   size_t ns = relPath.size();
0200   for (size_t i = 1; i < ns; ++i) {
0201     path += "/";
0202     path += relPath[ns - i - 1];
0203     // printf("push_back add to path %s\n", path.c_str());
0204   }
0205 }
0206 
0207 //______________________________________________________________________________
0208 
0209 void FWGeometryTableManagerBase::setCellValueEditor(TGTextEntry* editor) {
0210   m_editor = editor;
0211   m_renderer.setCellEditor(m_editor);
0212 }
0213 
0214 void FWGeometryTableManagerBase::showEditor(int row) {
0215   m_editTransparencyIdx = row;
0216   m_editor->UnmapWindow();
0217   m_editor->SetText(Form("%d", 100 - m_entries[row].m_transparency));
0218   m_editor->Resize(40, 17);
0219   m_editor->SetCursorPosition(2);
0220   redrawTable();
0221 }
0222 
0223 void FWGeometryTableManagerBase::applyTransparencyFromEditor() {
0224   printf("transparency idx %d opaci %s \n", m_editTransparencyIdx, m_editor->GetText());
0225   if (m_editTransparencyIdx >= 0) {
0226     try {
0227       int t = std::stoi(m_editor->GetText());
0228       if (t > 100 || t < 0) {
0229         fwLog(fwlog::kError) << "Transparency must be set in procentage [0-100].";
0230         return;
0231       }
0232       m_entries[m_editTransparencyIdx].m_transparency = 100 - t;
0233       printf("SET !! \n");
0234       cancelEditor(true);
0235     } catch (const std::exception&) {
0236       fwLog(fwlog::kError) << "Bad Lexical cast. Transparency must be set in procentage [0-100].";
0237     }
0238   }
0239 }
0240 
0241 void FWGeometryTableManagerBase::cancelEditor(bool redraw) {
0242   m_editTransparencyIdx = -1;
0243 
0244   if (m_editor->IsMapped()) {
0245     m_editor->UnmapWindow();
0246     if (redraw)
0247       redrawTable();
0248   }
0249 }
0250 
0251 //------------------------------------------------------------------------------
0252 
0253 void FWGeometryTableManagerBase::setVisibility(NodeInfo& data, bool x) { data.setBitVal(kVisNodeSelf, x); }
0254 
0255 //------------------------------------------------------------------------------
0256 
0257 void FWGeometryTableManagerBase::setVisibilityChld(NodeInfo& data, bool x) { data.setBitVal(kVisNodeChld, x); }
0258 
0259 //------------------------------------------------------------------------------
0260 
0261 void FWGeometryTableManagerBase::setDaughtersSelfVisibility(int selectedIdx, bool v) {
0262   TGeoNode* parentNode = m_entries[selectedIdx].m_node;
0263   int nD = parentNode->GetNdaughters();
0264   int dOff = 0;
0265   for (int n = 0; n != nD; ++n) {
0266     int idx = selectedIdx + 1 + n + dOff;
0267     NodeInfo& data = m_entries[idx];
0268 
0269     setVisibility(data, v);
0270     setVisibilityChld(data, v);
0271 
0272     getNNodesTotal(parentNode->GetDaughter(n), dOff);
0273   }
0274 }
0275 
0276 //------------------------------------------------------------------------------
0277 
0278 bool FWGeometryTableManagerBase::getVisibility(const NodeInfo& data) const { return data.testBit(kVisNodeSelf); }
0279 
0280 //------------------------------------------------------------------------------
0281 
0282 bool FWGeometryTableManagerBase::getVisibilityChld(const NodeInfo& data) const { return data.testBit(kVisNodeChld); }
0283 
0284 //------------------------------------------------------------------------------
0285 
0286 void FWGeometryTableManagerBase::applyColorTranspToDaughters(int selectedIdx, bool recurse) {
0287   NodeInfo& nInfo = m_entries[selectedIdx];
0288   TGeoNode* parentNode = nInfo.m_node;
0289   int nD = parentNode->GetNdaughters();
0290   int dOff = 0;
0291   for (int n = 0; n != nD; ++n) {
0292     int idx = selectedIdx + 1 + n + dOff;
0293     NodeInfo& data = m_entries[idx];
0294 
0295     data.copyColorTransparency(nInfo);
0296 
0297     if (recurse) {
0298       applyColorTranspToDaughters(idx, recurse);
0299     }
0300 
0301     getNNodesTotal(parentNode->GetDaughter(n), dOff);
0302   }
0303 }
0304 
0305 //------------------------------------------------------------------------------
0306 
0307 bool FWGeometryTableManagerBase::isNodeRendered(int idx, int topNodeIdx) const {
0308   const NodeInfo& data = m_entries[idx];
0309   bool foundParent = false;
0310 
0311   if (data.testBit(kVisNodeSelf)) {
0312     int pidx = data.m_parent;
0313     while (pidx >= 0) {
0314       if (!m_entries[pidx].testBit(kVisNodeChld)) {
0315         // printf ("parent disallow not visible !!! \n");
0316         return false;
0317       }
0318 
0319       if (pidx == topNodeIdx) {
0320         foundParent = true;
0321         // printf("parent found \n");
0322         break;
0323       }
0324       pidx = m_entries[pidx].m_parent;
0325     }
0326 
0327     return foundParent;
0328   }
0329   return false;
0330 }