1 
2 
3 #include "toonzqt/tselectionhandle.h"
4 #include "toonzqt/selection.h"
5 
6 //=============================================================================
7 // TSelectionHandle
8 //-----------------------------------------------------------------------------
9 
TSelectionHandle()10 TSelectionHandle::TSelectionHandle() { m_selectionStack.push_back(0); }
11 
12 //-----------------------------------------------------------------------------
13 
~TSelectionHandle()14 TSelectionHandle::~TSelectionHandle() {}
15 
16 //-----------------------------------------------------------------------------
17 
getSelection() const18 TSelection *TSelectionHandle::getSelection() const {
19   return m_selectionStack.back();
20 }
21 
22 //-----------------------------------------------------------------------------
23 
setSelection(TSelection * selection)24 void TSelectionHandle::setSelection(TSelection *selection) {
25   if (getSelection() == selection) return;
26   TSelection *oldSelection = getSelection();
27   if (oldSelection) {
28     oldSelection->selectNone();
29     // disable selection related commands
30     CommandManager *commandManager = CommandManager::instance();
31     int i;
32     for (i = 0; i < (int)m_enabledCommandIds.size(); i++)
33       commandManager->setHandler(m_enabledCommandIds[i].c_str(), 0);
34     m_enabledCommandIds.clear();
35   }
36   m_selectionStack.back() = selection;
37   if (selection) selection->enableCommands();
38   emit selectionSwitched(oldSelection, selection);
39 }
40 
41 //-----------------------------------------------------------------------------
42 
pushSelection()43 void TSelectionHandle::pushSelection() {
44   // NOTE
45   //  We push 0, and NOT a copy of the last item. I think this is done on
46   //  purpose,
47   //  as having it copied and then selecting a different one invokes
48   //  selectNone()
49   //  on the former.
50   m_selectionStack.push_back(0);
51 }
52 
53 //-----------------------------------------------------------------------------
54 
popSelection()55 void TSelectionHandle::popSelection() {
56   if (m_selectionStack.size() > 1) m_selectionStack.pop_back();
57   TSelection *selection = getSelection();
58   if (selection) selection->enableCommands();
59 }
60 
61 //-----------------------------------------------------------------------------
62 
enableCommand(std::string cmdId,CommandHandlerInterface * handler)63 void TSelectionHandle::enableCommand(std::string cmdId,
64                                      CommandHandlerInterface *handler) {
65   CommandManager::instance()->setHandler(cmdId.c_str(), handler);
66   m_enabledCommandIds.push_back(cmdId);
67 }
68 
69 //-----------------------------------------------------------------------------
70 
notifySelectionChanged()71 void TSelectionHandle::notifySelectionChanged() {
72   emit selectionChanged(m_selectionStack.back());
73 }
74 
75 //-----------------------------------------------------------------------------
76 
getCurrent()77 TSelectionHandle *TSelectionHandle::getCurrent() {
78   static TSelectionHandle _currentSelection;
79   return &_currentSelection;
80 }
81