]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ActiveControlMgr.cpp
Salome HOME
Issue #2336 sketch - coloring of selected segments
[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 #include <ModuleBase_IModule.h>
28 #include <ModuleBase_IWorkshop.h>
29
30 //#define DEBUG_ACTIVE_SELECTOR
31
32 #ifdef DEBUG_ACTIVE_SELECTOR
33 void debugInfo(const QString& theMessage, XGUI_ActiveControlSelector* theSelector)
34 {
35   std::cout << theMessage.toStdString().c_str() << ", active: "
36     << theSelector ? theSelector->getType().toStdString().c_str() : "NULL" << std::endl;
37 }
38 #endif
39
40 //********************************************************************
41 XGUI_ActiveControlMgr::XGUI_ActiveControlMgr(ModuleBase_IWorkshop* theWorkshop)
42 : myWorkshop(theWorkshop), myActiveSelector(0)
43 {
44   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
45 }
46
47 //********************************************************************
48 void XGUI_ActiveControlMgr::addSelector(XGUI_ActiveControlSelector* theSelector)
49 {
50   mySelectors.append(theSelector);
51   connect(theSelector, SIGNAL(activated()), this, SLOT(onSelectorActivated()));
52   connect(theSelector, SIGNAL(deactivated()), this, SLOT(onSelectorDeactivated()));
53 }
54
55 //********************************************************************
56 XGUI_ActiveControlSelector* XGUI_ActiveControlMgr::getSelector(const QString& theType)
57 {
58   XGUI_ActiveControlSelector* aSelector;
59   for (int i = 0, aCount = mySelectors.count(); i < aCount; i++)
60   {
61     if (mySelectors[i]->getType() != theType)
62       continue;
63     aSelector = mySelectors[i];
64     break;
65   }
66   return aSelector;
67 }
68
69 //********************************************************************
70 void XGUI_ActiveControlMgr::onSelectorActivated()
71 {
72   XGUI_ActiveControlSelector* aSelector = qobject_cast<XGUI_ActiveControlSelector*>(sender());
73   if (!aSelector || aSelector == myActiveSelector)
74     return;
75
76   if (myActiveSelector)
77     myActiveSelector->setActive(false);
78
79   activateSelector(aSelector);
80   XGUI_Tools::workshop(myWorkshop)->selectionActivate()->updateSelectionModes();
81   XGUI_Tools::workshop(myWorkshop)->selectionActivate()->updateSelectionFilters();
82
83 #ifdef DEBUG_ACTIVE_SELECTOR
84   debugInfo("onSelectorActivated", myActiveSelector);
85 #endif
86 }
87
88 //********************************************************************
89 void XGUI_ActiveControlMgr::onSelectorDeactivated()
90 {
91   XGUI_ActiveControlSelector* aSelector = qobject_cast<XGUI_ActiveControlSelector*>(sender());
92   if (!aSelector || aSelector != myActiveSelector || !myActiveSelector)
93     return;
94
95   myActiveSelector->setActive(false);
96   activateSelector(NULL);
97
98   XGUI_ActiveControlSelector* aSelectorToBeActivated = 0;
99   for (int i = 0, aCount = mySelectors.count(); i < aCount; i++)
100   {
101     if (!mySelectors[i]->needToBeActiated())
102       continue;
103     aSelectorToBeActivated = mySelectors[i];
104     break;
105   }
106   if (aSelectorToBeActivated)
107     activateSelector(aSelectorToBeActivated);
108
109   XGUI_Tools::workshop(myWorkshop)->selectionActivate()->updateSelectionModes();
110   XGUI_Tools::workshop(myWorkshop)->selectionActivate()->updateSelectionFilters();
111 #ifdef DEBUG_ACTIVE_SELECTOR
112   debugInfo("onSelectorDeactivated", myActiveSelector);
113 #endif
114 }
115
116 //********************************************************************
117 void XGUI_ActiveControlMgr::onSelectionChanged()
118 {
119   if (!myActiveSelector)
120     return;
121
122   myActiveSelector->processSelection();
123 #ifdef DEBUG_ACTIVE_SELECTOR
124   debugInfo("onSelectionChanged", myActiveSelector);
125 #endif
126 }
127
128 //********************************************************************
129 void XGUI_ActiveControlMgr::activateSelector(XGUI_ActiveControlSelector* theSelector)
130 {
131   myActiveSelector = theSelector;
132   if (myActiveSelector)
133     myActiveSelector->setActive(true);
134 }