Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-17 23:10:45

0001 #include <iostream>
0002 #include <fstream>
0003 #include "Fireworks/Core/interface/FWPartialConfig.h"
0004 #include "TGButton.h"
0005 #include "TEveManager.h"
0006 #include "TGString.h"
0007 #include "TGLabel.h"
0008 #include "TSystem.h"
0009 #include "TROOT.h"
0010 
0011 #include "Fireworks/Core/interface/SimpleSAXParser.h"
0012 #include "Fireworks/Core/interface/FWXMLConfigParser.h"
0013 #include "Fireworks/Core/interface/FWEventItemsManager.h"
0014 #include "Fireworks/Core/interface/FWGUIManager.h"
0015 #include "Fireworks/Core/interface/CmsShowMainFrame.h"
0016 #include "Fireworks/Core/interface/FWConfigurationManager.h"
0017 #include "Fireworks/Core/interface/fwLog.h"
0018 
0019 namespace {
0020   struct MyNameMap {
0021     std::vector<std::pair<std::string, std::string> > names = {{"GUI", "Windows"},
0022                                                                {"CommonPreferences", "Colors"},
0023                                                                {"EventNavigator", "EventFilters"},
0024                                                                {"EventItems", "Collections"}};
0025 
0026     std::string realName(std::string btnName) {
0027       for (std::vector<std::pair<std::string, std::string> >::iterator i = names.begin(); i != names.end(); ++i) {
0028         if (i->second == btnName)
0029           return i->first;
0030       }
0031       return btnName;
0032     }
0033 
0034     std::string btnName(const std::string& rlName) {
0035       for (std::vector<std::pair<std::string, std::string> >::iterator i = names.begin(); i != names.end(); ++i) {
0036         if (i->first == rlName)
0037           return i->second;
0038       }
0039       return rlName;
0040     }
0041   };
0042 
0043   MyNameMap nmm;
0044 }  // namespace
0045 
0046 FWPartialConfigGUI::FWPartialConfigGUI(const char* path, FWConfigurationManager* iCfg)
0047     : TGTransientFrame(gClient->GetRoot(), FWGUIManager::getGUIManager()->getMainFrame(), 200, 140), m_cfgMng(iCfg) {
0048   if (path) {
0049     std::ifstream g(path);
0050     FWXMLConfigParser parser(g);
0051     parser.parse();
0052     parser.config()->swap(m_origConfig);
0053   } else {
0054     FWConfiguration curr;
0055     m_cfgMng->to(curr);
0056     curr.swap(m_origConfig);
0057   }
0058 
0059   TGVerticalFrame* vf = new TGVerticalFrame(this);
0060   AddFrame(vf, new TGLayoutHints(kLHintsNormal, 2, 2, 4, 4));
0061 
0062   // can do a cast to non-const since we own the configuration
0063   FWConfiguration::KeyValues* kv = (FWConfiguration::KeyValues*)m_origConfig.keyValues();
0064   std::sort(kv->begin(),
0065             kv->end(),
0066             [](const std::pair<std::string, FWConfiguration>& lhs, const std::pair<std::string, FWConfiguration>& rhs)
0067                 -> bool { return nmm.btnName(lhs.first) < nmm.btnName(rhs.first); });
0068 
0069   for (FWConfiguration::KeyValues::const_iterator it = m_origConfig.keyValues()->begin();
0070        it != m_origConfig.keyValues()->end();
0071        ++it) {
0072     if (it->second.keyValues()) {
0073       std::string nb = nmm.btnName(it->first);
0074       TGCheckButton* cb = new TGCheckButton(vf, nb.c_str());
0075       vf->AddFrame(cb);
0076 
0077       m_entries.push_back(cb);
0078     }
0079   }
0080 }
0081 
0082 void FWPartialConfigGUI::Cancel() {
0083   // AMT Is this eniugh. Should there be a destroy ?
0084   UnmapWindow();
0085 }
0086 
0087 //---------------------------------------------------------------------
0088 //---------------------------------------------------------------------
0089 //---------------------------------------------------------------------
0090 //---------------------------------------------------------------------
0091 
0092 FWPartialConfigLoadGUI::FWPartialConfigLoadGUI(const char* path,
0093                                                FWConfigurationManager* iCfg,
0094                                                FWEventItemsManager* iEiMng)
0095     : FWPartialConfigGUI(path, iCfg), m_eiMng(iEiMng) {
0096   TGHorizontalFrame* hf = new TGHorizontalFrame(this);
0097   AddFrame(hf, new TGLayoutHints(kLHintsRight | kLHintsBottom, 1, 1, 2, 4));
0098   TGTextButton* load = new TGTextButton(hf, " Load ");
0099   load->Connect("Clicked()", "FWPartialConfigLoadGUI", this, "Load()");
0100   hf->AddFrame(load, new TGLayoutHints(kLHintsExpandX, 2, 2, 0, 0));
0101   TGTextButton* cancel = new TGTextButton(hf, " Cancel ");
0102   cancel->Connect("Clicked()", "FWPartialConfigGUI", this, "Cancel()");
0103   hf->AddFrame(cancel, new TGLayoutHints(kLHintsExpandX, 2, 2, 0, 0));
0104 
0105   SetWindowName("Load Config");
0106   MapSubwindows();
0107   Layout();
0108   MapWindow();
0109 }
0110 
0111 FWPartialConfigLoadGUI::~FWPartialConfigLoadGUI() {}
0112 
0113 void FWPartialConfigLoadGUI::Load() {
0114   bool resetViews = true;
0115   bool resetEI = true;
0116 
0117   FWConfiguration::KeyValues* kv = (FWConfiguration::KeyValues*)m_origConfig.keyValues();
0118 
0119   for (auto i = m_entries.begin(); i != m_entries.end(); i++) {
0120     if (!((*i)->IsOn())) {
0121       std::string key = nmm.realName((*i)->GetText()->GetString());
0122       if (key == "EventItems")
0123         resetEI = false;
0124       if (key == "GUI")
0125         resetViews = false;
0126 
0127       for (FWConfiguration::KeyValues::iterator it = kv->begin(); it != kv->end(); ++it) {
0128         if (key == it->first) {
0129           kv->erase(it);
0130           break;
0131         }
0132       }
0133     }
0134   }
0135 
0136   if (resetEI)
0137     m_eiMng->clearItems();
0138 
0139   if (resetViews)
0140     FWGUIManager::getGUIManager()->subviewDestroyAll();
0141 
0142   gEve->DisableRedraw();
0143   m_cfgMng->setFrom(m_origConfig);
0144   gEve->EnableRedraw();
0145 
0146   UnmapWindow();
0147 }
0148 
0149 //---------------------------------------------------------------------
0150 //---------------------------------------------------------------------
0151 //---------------------------------------------------------------------
0152 //---------------------------------------------------------------------
0153 
0154 FWPartialConfigSaveGUI::FWPartialConfigSaveGUI(const char* path_out, const char* path_in, FWConfigurationManager* iCfg)
0155     : FWPartialConfigGUI(nullptr, iCfg), m_outFileName(path_out), m_currFileName(path_in) {
0156   TGHorizontalFrame* hf = new TGHorizontalFrame(this);
0157   AddFrame(hf, new TGLayoutHints(kLHintsRight | kLHintsBottom, 1, 1, 2, 4));
0158 
0159   TGTextButton* write = new TGTextButton(hf, " Write ");
0160   write->Connect("Clicked()", "FWPartialConfigSaveGUI", this, "WriteConfig()");
0161   hf->AddFrame(write, new TGLayoutHints(kLHintsExpandX, 2, 2, 0, 0));
0162 
0163   TGTextButton* cancel = new TGTextButton(hf, " Cancel ");
0164   cancel->Connect("Clicked()", "FWPartialConfigGUI", this, "Cancel()");
0165   hf->AddFrame(cancel, new TGLayoutHints(kLHintsExpandX, 2, 2, 0, 0));
0166 
0167   AddFrame(new TGLabel(this, Form("Output file: %s", gSystem->BaseName(path_out))),
0168            new TGLayoutHints(kLHintsLeft, 8, 2, 3, 3));
0169 
0170   SetWindowName("Save Config");
0171 
0172   MapSubwindows();
0173   Layout();
0174   MapWindow();
0175 }
0176 
0177 void FWPartialConfigSaveGUI::WriteConfig() {
0178   FWConfiguration destination;
0179   {
0180     m_currFileName = gSystem->Which(TROOT::GetMacroPath(), m_currFileName.c_str(), kReadPermission);
0181     // printf("going to parse m_currFileName %s\n", m_currFileName.c_str());
0182     std::ifstream g(m_currFileName.c_str());
0183     if (g.peek() == (int)'<') {
0184       FWXMLConfigParser parser(g);
0185       parser.parse();
0186       parser.config()->swap(destination);
0187       // printf("parsed %s ......... \n",m_currFileName.c_str() );
0188     }
0189   }
0190 
0191   FWConfiguration curr;
0192   m_cfgMng->to(curr);
0193 
0194   FWConfiguration::KeyValues* cur_kv = (FWConfiguration::KeyValues*)m_origConfig.keyValues();
0195   FWConfiguration::KeyValues* old_kv = (FWConfiguration::KeyValues*)destination.keyValues();
0196 
0197   for (auto i = m_entries.begin(); i != m_entries.end(); i++) {
0198     if ((*i)->IsOn()) {
0199       std::string key = nmm.realName((*i)->GetText()->GetString());
0200       // printf("ON check key %s\n", key.c_str());
0201       for (FWConfiguration::KeyValues::iterator it = cur_kv->begin(); it != cur_kv->end(); ++it) {
0202         if (key == it->first) {
0203           bool replace = false;
0204           if (old_kv) {
0205             for (FWConfiguration::KeyValues::iterator oldit = old_kv->begin(); oldit != old_kv->end(); ++oldit) {
0206               if (key == oldit->first) {
0207                 replace = true;
0208                 oldit->second.swap(it->second);
0209                 break;
0210               }
0211             }
0212           }
0213 
0214           if (!replace)
0215             destination.addKeyValue(it->first, it->second);
0216 
0217           break;
0218         }
0219       }
0220     }
0221   }
0222 
0223   // Dump content in the file
0224   std::ofstream file(m_outFileName.c_str());
0225   if (not file) {
0226     fwLog(fwlog::kError) << "FWPartialConfigSaveGUI::Write, can't open output file.!\n";
0227     return;
0228   }
0229   fwLog(fwlog::kInfo) << "Writing configuration to " << m_outFileName << std::endl;
0230   FWConfiguration::streamTo(file, destination, "top");
0231   UnmapWindow();
0232 }