Salome HOME
Merge branch 'Dev_GroupsRevision'
[modules/shaper.git] / src / XGUI / XGUI_ActiveControlMgr.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "XGUI_ActiveControlMgr.h"
22 #include "XGUI_ActiveControlSelector.h"
23 #include "XGUI_SelectionActivate.h"
24 #include "XGUI_SelectionMgr.h"
25 #include "XGUI_Tools.h"
26 #include "XGUI_Workshop.h"
27
28 #include <ModuleBase_IModule.h>
29 #include <ModuleBase_IWorkshop.h>
30
31 //#define DEBUG_ACTIVE_SELECTOR
32
33 #ifdef DEBUG_ACTIVE_SELECTOR
34 void debugInfo(const QString& theMessage, XGUI_ActiveControlSelector* theSelector)
35 {
36   std::cout << theMessage.toStdString().c_str() << ", active: "
37     << (theSelector ? theSelector->getType().toStdString().c_str() : "NULL") << std::endl;
38 }
39 #endif
40
41 //********************************************************************
42 XGUI_ActiveControlMgr::XGUI_ActiveControlMgr(ModuleBase_IWorkshop* theWorkshop)
43 : myWorkshop(theWorkshop), myActiveSelector(0), myIsBlocked(false)
44 {
45   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
46 }
47
48 //********************************************************************
49 void XGUI_ActiveControlMgr::addSelector(XGUI_ActiveControlSelector* theSelector)
50 {
51   mySelectors.append(theSelector);
52   connect(theSelector, SIGNAL(activated()), this, SLOT(onSelectorActivated()));
53   connect(theSelector, SIGNAL(deactivated()), this, SLOT(onSelectorDeactivated()));
54 }
55
56 //********************************************************************
57 XGUI_ActiveControlSelector* XGUI_ActiveControlMgr::getSelector(const QString& theType)
58 {
59   XGUI_ActiveControlSelector* aSelector;
60   for (int i = 0, aCount = mySelectors.count(); i < aCount; i++)
61   {
62     if (mySelectors[i]->getType() != theType)
63       continue;
64     aSelector = mySelectors[i];
65     break;
66   }
67   return aSelector;
68 }
69
70 //********************************************************************
71 void XGUI_ActiveControlMgr::onSelectorActivated()
72 {
73   XGUI_ActiveControlSelector* aSelector = qobject_cast<XGUI_ActiveControlSelector*>(sender());
74   if (!aSelector || aSelector == myActiveSelector)
75     return;
76
77   if (myIsBlocked) // we've come here from the same method
78     return;
79   myIsBlocked = true;
80   if (myActiveSelector) {
81     myActiveSelector->setActive(false);
82   }
83   activateSelector(aSelector);
84   XGUI_Tools::workshop(myWorkshop)->selectionActivate()->updateSelectionModes();
85   XGUI_Tools::workshop(myWorkshop)->selectionActivate()->updateSelectionFilters();
86
87 #ifdef DEBUG_ACTIVE_SELECTOR
88   debugInfo("onSelectorActivated", myActiveSelector);
89 #endif
90   myIsBlocked = false;
91 }
92
93 //********************************************************************
94 void XGUI_ActiveControlMgr::onSelectorDeactivated()
95 {
96   XGUI_ActiveControlSelector* aSelector = qobject_cast<XGUI_ActiveControlSelector*>(sender());
97   if (!aSelector || aSelector != myActiveSelector || !myActiveSelector)
98     return;
99
100   if (myIsBlocked) // we've come here from the same method
101     return;
102   myIsBlocked = true;
103
104   myActiveSelector->setActive(false);
105   activateSelector(NULL);
106
107   XGUI_ActiveControlSelector* aSelectorToBeActivated = 0;
108   for (int i = 0, aCount = mySelectors.count(); i < aCount; i++)
109   {
110     if (!mySelectors[i]->needToBeActiated())
111       continue;
112     aSelectorToBeActivated = mySelectors[i];
113     break;
114   }
115   if (aSelectorToBeActivated)
116     activateSelector(aSelectorToBeActivated);
117
118   XGUI_Tools::workshop(myWorkshop)->selectionActivate()->updateSelectionModes();
119   XGUI_Tools::workshop(myWorkshop)->selectionActivate()->updateSelectionFilters();
120 #ifdef DEBUG_ACTIVE_SELECTOR
121   debugInfo("onSelectorDeactivated", myActiveSelector);
122 #endif
123   myIsBlocked = false;
124 }
125
126 //********************************************************************
127 void XGUI_ActiveControlMgr::onSelectionChanged()
128 {
129   if (!myActiveSelector)
130     return;
131
132   myActiveSelector->processSelection();
133 #ifdef DEBUG_ACTIVE_SELECTOR
134   debugInfo("onSelectionChanged", myActiveSelector);
135 #endif
136 }
137
138 //********************************************************************
139 void XGUI_ActiveControlMgr::activateSelector(XGUI_ActiveControlSelector* theSelector)
140 {
141   myActiveSelector = theSelector;
142   if (myActiveSelector)
143     myActiveSelector->setActive(true);
144 }