CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Fireworks/Core/src/CmsShowModelPopup.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     CmsShowModelPopup
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:
00010 //         Created:  Fri Jun 27 11:23:08 EDT 2008
00011 // $Id: CmsShowModelPopup.cc,v 1.33 2012/09/21 09:26:26 eulisse Exp $
00012 //
00013 
00014 // system include file
00015 #include <iostream>
00016 #include <sstream>
00017 #include <set>
00018 #include <cassert>
00019 #include <sigc++/sigc++.h>
00020 #include <boost/bind.hpp>
00021 #include "TClass.h"
00022 #include "TGFrame.h"
00023 #include "TGButton.h"
00024 #include "TGLabel.h"
00025 #include "TGString.h"
00026 #include "TColor.h"
00027 #include "TG3DLine.h"
00028 #include "TGFont.h"
00029 #include "TGSlider.h"
00030 #include "TGButton.h"
00031 
00032 // user include files
00033 #include "Fireworks/Core/interface/CmsShowModelPopup.h"
00034 #include "Fireworks/Core/interface/FWDisplayProperties.h"
00035 #include "Fireworks/Core/src/FWColorSelect.h"
00036 #include "Fireworks/Core/src/FWDialogBuilder.h"
00037 #include "Fireworks/Core/interface/FWModelChangeSignal.h"
00038 #include "Fireworks/Core/interface/FWModelChangeManager.h"
00039 #include "Fireworks/Core/interface/FWColorManager.h"
00040 #include "Fireworks/Core/interface/FWEventItem.h"
00041 #include "Fireworks/Core/interface/FWModelId.h"
00042 #include "Fireworks/Core/interface/FWSelectionManager.h"
00043 #include "Fireworks/Core/interface/FWDetailViewManager.h"
00044 
00045 //
00046 // constants, enums and typedefs
00047 //
00048 
00049 //
00050 // static data member definitions
00051 //
00052 //
00053 // constructors and destructor
00054 //
00055 CmsShowModelPopup::CmsShowModelPopup(FWDetailViewManager* iManager,
00056                                      FWSelectionManager* iSelMgr,
00057                                      const FWColorManager* iColorMgr,
00058                                      const TGWindow* p, UInt_t w, UInt_t h) :
00059    TGTransientFrame(gClient->GetDefaultRoot(),p,w,h),
00060    m_detailViewManager(iManager),
00061    m_colorManager(iColorMgr),
00062    m_dialogBuilder(0)
00063 {
00064    m_changes = iSelMgr->selectionChanged_.connect(boost::bind(&CmsShowModelPopup::fillModelPopup, this, _1));
00065 
00066    SetCleanup(kDeepCleanup);
00067 
00068    TGVerticalFrame* vf = new TGVerticalFrame(this);
00069    AddFrame(vf, new TGLayoutHints(kLHintsExpandX|kLHintsExpandY, 2, 2, 2, 4));
00070    m_dialogBuilder =  new FWDialogBuilder(vf);
00071   
00072    // Do the layouting of the various widgets.
00073    m_dialogBuilder->indent(4)
00074       .addLabel(" ", 14, 1, &m_modelLabel)
00075       .addLabel("Color", 8)
00076       .addColorPicker(iColorMgr, &m_colorSelectWidget).expand(false)
00077       .addHSeparator()
00078       .addLabel("Opacity", 8)
00079       .addHSlider(150, &m_opacitySlider)
00080       .addHSeparator(2)
00081       .addCheckbox("Visible", &m_isVisibleButton)
00082       .addHSeparator();
00083 
00084    // Dummy button for detail views. Can be overidden.
00085    TGTextButton *detailedViewButton = new TGTextButton(this, "Open Detailed View", 0); 
00086    AddFrame(detailedViewButton, new TGLayoutHints(kLHintsExpandX, 4, 4, 4, 4));
00087    m_openDetailedViewButtons.push_back(detailedViewButton);
00088 
00089    m_colorSelectWidget->Connect("ColorChosen(Color_t)", "CmsShowModelPopup", this, "changeModelColor(Color_t)");
00090    m_isVisibleButton->Connect("Toggled(Bool_t)", "CmsShowModelPopup", this, "toggleModelVisible(Bool_t)");
00091    m_opacitySlider->Connect("PositionChanged(Int_t)", "CmsShowModelPopup",
00092                             this, "changeModelOpacity(Int_t)");
00093 
00094    SetWindowName("Object Controller");
00095    MapSubwindows();
00096    Resize(GetDefaultSize());
00097    Layout();
00098 
00099    fillModelPopup(*iSelMgr);
00100 }
00101 
00102 // CmsShowModelPopup::CmsShowModelPopup(const CmsShowModelPopup& rhs)
00103 // {
00104 //    // do actual copying here;
00105 // }
00106 
00107 CmsShowModelPopup::~CmsShowModelPopup()
00108 {
00109    m_changes.disconnect();
00110    m_colorSelectWidget->Disconnect("ColorSelected(Pixel_t)", this, "changeModelColor(Pixel_t)");
00111    m_opacitySlider->Disconnect("PositionChanged(Int_t)", this, "changeModelOpacity(Int_t)");
00112    m_isVisibleButton->Disconnect("Toggled(Bool_t)", this, "toggleModelVisible(Bool_t)");
00113    disconnectAll();
00114 }
00115 
00116 //
00117 // assignment operators
00118 //
00119 // const CmsShowModelPopup& CmsShowModelPopup::operator=(const CmsShowModelPopup& rhs)
00120 // {
00121 //   //An exception safe implementation is
00122 //   CmsShowModelPopup temp(rhs);
00123 //   swap(rhs);
00124 //
00125 //   return *this;
00126 // }
00127 
00128 //
00129 // member functions
00130 //
00135 void
00136 CmsShowModelPopup::fillModelPopup(const FWSelectionManager& iSelMgr)
00137 {
00138    disconnectAll();
00139    // Handles the case in which the selection is empty.
00140    if (iSelMgr.selected().empty())
00141       return;
00142    
00143    // Handle the case in which the selection is not empty.
00144    bool multipleNames = false, multipleColors = false, multipleVis = false,
00145       multipleTransparecy = false;
00146 
00147    m_models = iSelMgr.selected();
00148    std::set<FWModelId>::const_iterator id = m_models.begin();
00149    const FWEventItem* item = id->item();
00150    const FWEventItem::ModelInfo info = item->modelInfo(id->index());
00151    const FWDisplayProperties &props = info.displayProperties();
00152    
00153    // The old logic was broken here. It was enought that the last item
00154    // was the same as the first one, and all of them would have been considered
00155    // equal. This should fix it. The idea is that if any of the elements in
00156    // models in [1, N] are different from the first, then we consider the
00157    // set with multipleXYZ.
00158    for (std::set<FWModelId>::const_iterator i = ++(m_models.begin()),
00159            e = m_models.end(); i != e; ++i) 
00160    {
00161       const FWEventItem::ModelInfo &nextInfo = i->item()->modelInfo(i->index());
00162       const FWDisplayProperties &nextProps = nextInfo.displayProperties();
00163       
00164       multipleNames = multipleNames || (item->name() != i->item()->name());
00165       multipleColors = multipleColors || (props.color() != nextProps.color());
00166       multipleVis = multipleVis || (props.isVisible() != nextProps.isVisible());
00167       multipleTransparecy = multipleTransparecy 
00168          || (props.transparency() != nextProps.transparency());
00169    }
00170    
00171    // Handle the name.
00172    std::ostringstream s;
00173    if (multipleNames) 
00174       s << m_models.size() << " objects";
00175    else
00176       s << m_models.size() << " " << item->name();
00177    m_modelLabel->SetText(s.str().c_str());
00178 
00179    if (m_models.size()==1)
00180    {
00181       m_modelLabel->SetText(item->modelName(id->index()).c_str());
00182       std::vector<std::string> viewChoices = m_detailViewManager->detailViewsFor(*id);
00183       m_openDetailedViewButtons.front()->SetEnabled(viewChoices.size()>0);
00184       //be sure we show just the right number of buttons
00185       if(viewChoices.size() > m_openDetailedViewButtons.size()) 
00186       {
00187          for(size_t i = 0, e = m_openDetailedViewButtons.size(); i != e; ++i)
00188          {
00189             // printf("show existing buttons\n");
00190             TGCompositeFrame* cf = (TGCompositeFrame*)m_openDetailedViewButtons[i]->GetParent();
00191             cf->ShowFrame(m_openDetailedViewButtons[i]);
00192          }
00193 
00194          //now we make additional buttons 
00195          TGTextButton *button;
00196          for(size_t index = m_openDetailedViewButtons.size(); index < viewChoices.size(); ++index)
00197          { 
00198             // printf("add new button %s \n ",  viewChoices[index].c_str());
00199             button = new TGTextButton(this, "dummy", index);
00200             AddFrame(button, new TGLayoutHints(kLHintsExpandX, 4, 4, 4, 4));
00201             TGCompositeFrame* cf = (TGCompositeFrame*)button->GetParent();
00202             cf->MapWindow();
00203             cf->MapSubwindows();
00204             m_openDetailedViewButtons.push_back(button);
00205 
00206             button->Connect("Clicked()", "CmsShowModelPopup", this, "clicked()");
00207          }
00208       }
00209       else if (!viewChoices.empty())
00210       {
00211          //  printf("show button subset %d \n",  viewChoices.size());
00212          for (size_t i = 0, e = viewChoices.size(); i != e; ++i)
00213          {
00214             TGCompositeFrame* cf = (TGCompositeFrame*)m_openDetailedViewButtons[i]->GetParent();
00215             cf->ShowFrame(m_openDetailedViewButtons[i]);
00216          }
00217       }
00218       
00219       //set the names
00220       for (size_t i = 0, e = viewChoices.size(); i != e; ++i)
00221       {
00222          m_openDetailedViewButtons[i]->SetText(("Open " + viewChoices[i] + " Detail View ...").c_str());
00223       }
00224    }
00225    
00226    // Set the various widgets.
00227    m_colorSelectWidget->SetColorByIndex(props.color(), kFALSE);
00228    m_opacitySlider->SetPosition(100 - props.transparency());
00229    m_isVisibleButton->SetDisabledAndSelected(props.isVisible());
00230 
00231    m_opacitySlider->SetEnabled(kTRUE);
00232    m_colorSelectWidget->SetEnabled(kTRUE);
00233    m_isVisibleButton->SetEnabled(kTRUE);
00234    
00235    m_modelChangedConn = item->changed_.connect(boost::bind(&CmsShowModelPopup::updateDisplay, this));
00236    m_destroyedConn = item->goingToBeDestroyed_.connect(boost::bind(&CmsShowModelPopup::disconnectAll, this));
00237 
00238    Resize(GetDefaultSize());
00239    Layout();
00240 }
00241 
00244 void
00245 CmsShowModelPopup::updateDisplay()
00246 {
00247    for (std::set<FWModelId>::iterator i = m_models.begin(), e = m_models.end(); 
00248         i != e; ++i)
00249    {
00250       const FWEventItem::ModelInfo &info = i->item()->modelInfo(i->index());
00251       const FWDisplayProperties &p = info.displayProperties();
00252       m_colorSelectWidget->SetColorByIndex(p.color(), kFALSE);
00253       
00254       if (p.isVisible())
00255          m_isVisibleButton->SetState(kButtonDown, kFALSE);
00256       else
00257          m_isVisibleButton->SetState(kButtonUp, kFALSE);
00258 
00259       m_opacitySlider->SetPosition(100 - p.transparency());
00260    }
00261 }
00262 
00263 /* Called by FWGUIManager when change background/colorset. */
00264 void 
00265 CmsShowModelPopup::colorSetChanged()
00266 { 
00267    for (std::set<FWModelId>::iterator i = m_models.begin(), e = m_models.end(); 
00268         i != e; ++i)
00269    {
00270       const FWEventItem::ModelInfo &info = i->item()->modelInfo(i->index());
00271       const FWDisplayProperties &p = info.displayProperties();
00272       m_colorSelectWidget->SetColorByIndex(p.color(), kFALSE);
00273    } 
00274 }
00275  
00279 void
00280 CmsShowModelPopup::disconnectAll() {
00281    m_modelChangedConn.disconnect();
00282    m_destroyedConn.disconnect();
00283    //  m_item = 0;
00284    //  m_model = 0;
00285    m_modelLabel->SetText("No object selected");
00286    m_colorSelectWidget->SetColorByIndex(kRed,kFALSE);
00287    m_isVisibleButton->SetDisabledAndSelected(kTRUE);
00288    m_colorSelectWidget->SetEnabled(kFALSE);
00289    m_opacitySlider->SetEnabled(kFALSE);
00290    m_isVisibleButton->SetEnabled(kFALSE);
00291    m_openDetailedViewButtons.front()->SetEnabled(kFALSE);
00292    m_openDetailedViewButtons.front()->SetText("Open Detail View ...");
00293    assert(!m_openDetailedViewButtons.empty());
00294    for(size_t i = 1, e = m_openDetailedViewButtons.size(); i != e; ++i)
00295    {
00296       TGCompositeFrame* cf = (TGCompositeFrame*)m_openDetailedViewButtons[i]->GetParent();
00297       cf->HideFrame(m_openDetailedViewButtons[i]);
00298    }
00299 
00300 
00301 }
00302 
00310 void
00311 CmsShowModelPopup::changeModelColor(Color_t color)
00312 {
00313    if (m_models.empty())
00314       return;
00315       
00316    FWChangeSentry sentry(*(m_models.begin()->item()->changeManager()));
00317    for (std::set<FWModelId>::iterator i = m_models.begin(), e = m_models.end(); i != e; ++i)
00318    {
00319       const FWEventItem::ModelInfo &info = i->item()->modelInfo(i->index());
00320       FWDisplayProperties changeProperties = info.displayProperties();
00321       changeProperties.setColor(color);
00322       i->item()->setDisplayProperties(i->index(), changeProperties);
00323    }
00324 }
00325 
00329 void
00330 CmsShowModelPopup::changeModelOpacity(Int_t opacity) 
00331 {
00332    if (m_models.empty())
00333       return;
00334    
00335    FWChangeSentry sentry(*(m_models.begin()->item()->changeManager()));
00336    for (std::set<FWModelId>::iterator i = m_models.begin(), e = m_models.end(); i != e; ++i)
00337    {
00338       const FWEventItem::ModelInfo &info = i->item()->modelInfo(i->index());
00339       FWDisplayProperties changeProperties = info.displayProperties();
00340       changeProperties.setTransparency(100 - opacity);
00341       i->item()->setDisplayProperties(i->index(), changeProperties);
00342    }
00343 }
00344 
00348 void
00349 CmsShowModelPopup::toggleModelVisible(Bool_t on) {
00350    if(m_models.empty())
00351       return;
00352    
00353    FWChangeSentry sentry(*(m_models.begin()->item()->changeManager()));
00354    for (std::set<FWModelId>::iterator i = m_models.begin(); i != m_models.end(); ++i) 
00355    {
00356       const FWEventItem::ModelInfo &info = i->item()->modelInfo(i->index());
00357       FWDisplayProperties changeProperties = info.displayProperties();
00358       changeProperties.setIsVisible(on);
00359       i->item()->setDisplayProperties(i->index(), changeProperties);
00360    }
00361 }
00362 
00363 void
00364 CmsShowModelPopup::openDetailedView()
00365 {
00366    std::vector<std::string> viewChoices = m_detailViewManager->detailViewsFor(*(m_models.begin()));
00367 
00368    m_detailViewManager->openDetailViewFor( *(m_models.begin()), viewChoices.front() );
00369 }
00370 
00371 void
00372 CmsShowModelPopup::clicked()
00373 {
00374    TGTextButton *cs = static_cast<TGTextButton *>(gTQSender);
00375    int iIndex = cs->WidgetId();
00376    std::vector<std::string> viewChoices = m_detailViewManager->detailViewsFor(*(m_models.begin()));
00377    
00378    m_detailViewManager->openDetailViewFor( *(m_models.begin()), *(viewChoices.begin()+iIndex) );
00379    
00380 }   
00381 //
00382 // const member functions
00383 //
00384 
00385 //
00386 // static member functions
00387 //