File indexing completed on 2024-05-19 04:36:38

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2014 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library 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 Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "ProxyTool.h"
0021 #include "SelectTool.h"
0022 #include "NodeTool.h"
0023 #include "EllipseTool.h"
0024 #include "LineTool.h"
0025 #include "NodeItem.h"
0026 #include "PathItem.h"
0027 
0028 #include <tikz/core/Path.h>
0029 
0030 #include <QGraphicsScene>
0031 #include <QKeyEvent>
0032 
0033 #include <QDebug>
0034 
0035 namespace tikz {
0036 namespace ui {
0037 
0038 ProxyTool::ProxyTool(tikz::ui::Document * doc, QGraphicsScene * graphicsScene)
0039     : AbstractTool(doc, graphicsScene)
0040     , m_tool(nullptr)
0041 {
0042     // keep track of selected items to always select correct tool
0043     connect(scene(), SIGNAL(selectionChanged()), this, SLOT(updateTool()));
0044 
0045     // update tool once now
0046     updateTool();
0047 }
0048 
0049 ProxyTool::~ProxyTool()
0050 {
0051 }
0052 
0053 void ProxyTool::mouseEnteredScene()
0054 {
0055     m_tool->mouseEnteredScene();
0056 }
0057 
0058 void ProxyTool::mouseLeftScene()
0059 {
0060     m_tool->mouseLeftScene();
0061 }
0062 
0063 void ProxyTool::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
0064 {
0065     m_tool->mouseMoveEvent(event);
0066 }
0067 
0068 void ProxyTool::mousePressEvent(QGraphicsSceneMouseEvent * event)
0069 {
0070     m_tool->mousePressEvent(event);
0071 }
0072 
0073 void ProxyTool::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
0074 {
0075     m_tool->mouseReleaseEvent(event);
0076 }
0077 
0078 void ProxyTool::keyPressEvent(QKeyEvent * event)
0079 {
0080     m_tool->keyPressEvent(event);
0081 }
0082 
0083 void ProxyTool::updateTool()
0084 {
0085     qDebug() << "ProxyTool::updateTool(): Changing tool";
0086     if (m_tool) {
0087         delete m_tool;
0088         m_tool = nullptr;
0089     }
0090 
0091     QList<QGraphicsItem *> items = scene()->selectedItems();
0092     if (items.size() == 1) {
0093         if (dynamic_cast<NodeItem *>(items[0])) {
0094             m_tool = new NodeTool(static_cast<NodeItem *>(items[0]), scene());
0095         } else if (dynamic_cast<tikz::ui::PathItem*>(items[0])) {
0096             tikz::ui::PathItem * path = static_cast<tikz::ui::PathItem *>(items[0]);
0097             const tikz::PathType type = path->path()->type();
0098             switch (type) {
0099                 case tikz::PathType::Line: m_tool = new LineTool(path, scene()); break;
0100                 case tikz::PathType::HVLine: break;
0101                 case tikz::PathType::VHLine: break;
0102                 case tikz::PathType::BendCurve: break;
0103                 case tikz::PathType::InOutCurve: break;
0104                 case tikz::PathType::BezierCurve: break;
0105                 case tikz::PathType::Ellipse: m_tool = new EllipseTool(path, scene()); break;
0106                 case tikz::PathType::Rectangle: break;
0107                 case tikz::PathType::Grid: break;
0108                 case tikz::PathType::Invalid: break;
0109                 default: Q_ASSERT(false);
0110             }
0111         }
0112     }
0113 
0114     //
0115     // make sure we always have a valid tool
0116     //
0117     if (! m_tool) {
0118         m_tool = new SelectTool(document(), scene());
0119     }
0120 }
0121 
0122 }
0123 }
0124 
0125 // kate: indent-width 4; replace-tabs on;