File indexing completed on 2024-05-12 05:40:25
0001 /*************************************************************************** 0002 * Copyright (C) 2019 by Renaud Guezennec * 0003 * http://www.rolisteam.org/contact * 0004 * * 0005 * This software is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "mindmap/controller/selectioncontroller.h" 0021 0022 #include "mindmap/command/dragnodecommand.h" 0023 #include "mindmap/data/mindnode.h" 0024 namespace mindmap 0025 { 0026 0027 SelectionController::SelectionController(QObject* parent) : QObject(parent) {} 0028 0029 void SelectionController::setEnabled(bool enable) 0030 { 0031 if(enable == m_enabled) 0032 return; 0033 m_enabled= enable; 0034 emit enabledChanged(); 0035 } 0036 0037 void SelectionController::setUndoStack(QUndoStack* stack) 0038 { 0039 m_undoStack= stack; 0040 } 0041 0042 QString SelectionController::lastSelected() const 0043 { 0044 return m_lastSelectedId; 0045 } 0046 0047 bool SelectionController::enabled() const 0048 { 0049 return m_enabled; 0050 } 0051 0052 const std::vector<mindmap::MindItem*>& SelectionController::selectedNodes() const 0053 { 0054 return m_selection; 0055 } 0056 0057 void SelectionController::addToSelection(mindmap::MindItem* node) 0058 { 0059 if(node == nullptr) 0060 return; 0061 auto selection= hasSelection(); 0062 m_selection.push_back(node); 0063 node->setSelected(true); 0064 if(node->type() == mindmap::MindItem::NodeType || node->type() == mindmap::MindItem::PackageType) 0065 { 0066 auto mindNode= dynamic_cast<PositionedItem*>(node); 0067 connect(mindNode, &PositionedItem::itemDragged, this, &SelectionController::movingNode); 0068 } 0069 if(selection != hasSelection()) 0070 emit hasSelectionChanged(); 0071 } 0072 0073 void SelectionController::removeFromSelection(mindmap::MindItem* node) 0074 { 0075 auto selection= hasSelection(); 0076 node->setSelected(false); 0077 if(node->type() == mindmap::MindItem::NodeType || node->type() == mindmap::MindItem::PackageType) 0078 { 0079 auto mindNode= dynamic_cast<PositionedItem*>(node); 0080 disconnect(mindNode, &PositionedItem::itemDragged, this, &SelectionController::movingNode); 0081 } 0082 m_selection.erase(std::find(m_selection.begin(), m_selection.end(), node)); 0083 if(selection != hasSelection()) 0084 emit hasSelectionChanged(); 0085 } 0086 0087 void SelectionController::clearSelection() 0088 { 0089 std::for_each(m_selection.begin(), m_selection.end(), [this](mindmap::MindItem* node) { 0090 node->setSelected(false); 0091 if(node->type() == mindmap::MindItem::NodeType || node->type() == mindmap::MindItem::PackageType) 0092 { 0093 auto mindNode= dynamic_cast<PositionedItem*>(node); 0094 disconnect(mindNode, &PositionedItem::itemDragged, this, &SelectionController::movingNode); 0095 } 0096 }); 0097 m_selection.clear(); 0098 setLastSelectedId({}); 0099 emit hasSelectionChanged(); 0100 } 0101 0102 void SelectionController::movingNode(const QPointF& motion) 0103 { 0104 std::vector<QPointer<PositionedItem>> vec; 0105 std::transform(m_selection.begin(), m_selection.end(), std::back_inserter(vec), [](mindmap::MindItem* node) { 0106 return QPointer<PositionedItem>(dynamic_cast<PositionedItem*>(node)); 0107 }); 0108 auto cmd= new DragNodeCommand(motion, vec); 0109 0110 m_undoStack->push(cmd); 0111 } 0112 0113 bool SelectionController::hasSelection() const 0114 { 0115 return !m_selection.empty(); 0116 } 0117 0118 void SelectionController::setLastSelectedId(const QString& id) 0119 { 0120 if(id == m_lastSelectedId) 0121 return; 0122 m_lastSelectedId= id; 0123 emit lastSelectedChanged(); 0124 } 0125 } // namespace mindmap