Salome HOME
Activation objects redesign.
[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_Tools.h"
25 #include "XGUI_Workshop.h"
26
27
28 #include <ModuleBase_IModule.h>
29 #include <ModuleBase_IWorkshop.h>
30
31 //********************************************************************
32 XGUI_ActiveControlMgr::XGUI_ActiveControlMgr(ModuleBase_IWorkshop* theWorkshop)
33 : myWorkshop(theWorkshop), myActiveSelector(0)
34 {
35   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
36 }
37
38 //********************************************************************
39 void XGUI_ActiveControlMgr::addSelector(XGUI_ActiveControlSelector* theSelector)
40 {
41   mySelectors.append(theSelector);
42   connect(theSelector, SIGNAL(activated()), this, SLOT(onSelectorActivated()));
43   connect(theSelector, SIGNAL(deactivated()), this, SLOT(onSelectorDeactivated()));
44 }
45
46 //********************************************************************
47 XGUI_ActiveControlSelector* XGUI_ActiveControlMgr::getSelector(const QString& theType)
48 {
49   XGUI_ActiveControlSelector* aSelector;
50   for (int i = 0, aCount = mySelectors.count(); i < aCount; i++)
51   {
52     if (mySelectors[i]->getType() != theType)
53       continue;
54     aSelector = mySelectors[i];
55     break;
56   }
57   return aSelector;
58 }
59
60 //********************************************************************
61 void XGUI_ActiveControlMgr::onSelectorActivated()
62 {
63   XGUI_ActiveControlSelector* aSelector = qobject_cast<XGUI_ActiveControlSelector*>(sender());
64   if (!aSelector || aSelector == myActiveSelector)
65     return;
66
67   if (myActiveSelector)
68     myActiveSelector->setActive(false);
69
70   activateSelector(aSelector);
71   XGUI_Tools::workshop(myWorkshop)->selectionActivate()->updateSelectionModes();
72   XGUI_Tools::workshop(myWorkshop)->selectionActivate()->updateSelectionFilters();
73 }
74
75 //********************************************************************
76 void XGUI_ActiveControlMgr::onSelectorDeactivated()
77 {
78   XGUI_ActiveControlSelector* aSelector = qobject_cast<XGUI_ActiveControlSelector*>(sender());
79   if (!aSelector)
80     return;
81
82   myActiveSelector = NULL;
83
84   aSelector->setActive(false);
85
86   XGUI_ActiveControlSelector* aSelectorToBeActivated = 0;
87   for (int i = 0, aCount = mySelectors.count(); i < aCount; i++)
88   {
89     if (!mySelectors[i]->needToBeActiated())
90       continue;
91     aSelectorToBeActivated = mySelectors[i];
92     break;
93   }
94   if (aSelectorToBeActivated)
95     activateSelector(aSelectorToBeActivated);
96
97   XGUI_Tools::workshop(myWorkshop)->selectionActivate()->updateSelectionModes();
98   XGUI_Tools::workshop(myWorkshop)->selectionActivate()->updateSelectionFilters();
99 }
100
101 //********************************************************************
102 void XGUI_ActiveControlMgr::onSelectionChanged()
103 {
104   if (!myActiveSelector)
105     return;
106
107   myActiveSelector->processSelection();
108 }
109
110 //********************************************************************
111 void XGUI_ActiveControlMgr::activateSelector(XGUI_ActiveControlSelector* theSelector)
112 {
113   myActiveSelector = theSelector;
114   theSelector->setActive(true);
115 }