File indexing completed on 2024-05-05 04:35:18
0001 /* This file is part of the TikZKit project. 0002 * 0003 * Copyright (C) 2013 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 "TikzScene.h" 0021 0022 #include "DocumentPrivate.h" 0023 #include "NodeItem.h" 0024 #include "PathItem.h" 0025 #include "AbstractTool.h" 0026 #include "ProxyTool.h" // FIXME: only temporarily 0027 0028 #include <tikz/core/Transaction.h> 0029 #include <tikz/core/Value.h> 0030 0031 #include <QGraphicsItem> 0032 #include <QGraphicsSceneMouseEvent> 0033 #include <QGraphicsView> 0034 #include <QVarLengthArray> 0035 #include <QDebug> 0036 #include <QKeyEvent> 0037 #include <QUndoStack> 0038 0039 #include <math.h> 0040 0041 namespace tikz { 0042 namespace ui { 0043 0044 class TikzScenePrivate 0045 { 0046 public: 0047 // associated tikz Document 0048 DocumentPrivate * doc = nullptr; 0049 0050 // The size of the cells in the grid. 0051 int subDivisions = 1; 0052 0053 // Mouse edit mode 0054 TikzEditMode editMode = TikzEditMode::ModeSelect; 0055 0056 // currently active tool 0057 AbstractTool * tool = nullptr; 0058 }; 0059 0060 TikzScene::TikzScene(DocumentPrivate * doc) 0061 : QGraphicsScene(doc) 0062 , d(new TikzScenePrivate()) 0063 { 0064 d->doc = doc; 0065 d->tool = new ProxyTool(doc, this); 0066 0067 connect(doc, &DocumentPrivate::changed, this, &TikzScene::updateSceneRect); 0068 updateSceneRect(); 0069 } 0070 0071 TikzScene::~TikzScene() 0072 { 0073 delete d; 0074 } 0075 0076 DocumentPrivate * TikzScene::document() const 0077 { 0078 return d->doc; 0079 } 0080 0081 void TikzScene::setEditMode(TikzEditMode mode) 0082 { 0083 if (mode != d->editMode) { 0084 d->editMode = mode; 0085 0086 Q_EMIT editModeChanged(mode); 0087 } 0088 } 0089 0090 TikzEditMode TikzScene::editMode() const 0091 { 0092 return d->editMode; 0093 } 0094 0095 void TikzScene::mousePressEvent(QGraphicsSceneMouseEvent * event) 0096 { 0097 // first let QGraphicsScene do its work 0098 QGraphicsScene::mousePressEvent(event); 0099 if (event->isAccepted()) { 0100 return; 0101 } 0102 0103 // event not accepted, so pass it on to the tool 0104 if (d->tool) { 0105 d->tool->mousePressEvent(event); 0106 event->accept(); 0107 } 0108 } 0109 0110 void TikzScene::mouseMoveEvent(QGraphicsSceneMouseEvent * event) 0111 { 0112 // first let QGraphicsScene do its work 0113 QGraphicsScene::mouseMoveEvent(event); 0114 if (event->isAccepted()) { 0115 return; 0116 } 0117 0118 // event not accepted, so pass it on to the tool 0119 if (d->tool) { 0120 d->tool->mouseMoveEvent(event); 0121 event->accept(); 0122 } 0123 } 0124 0125 void TikzScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) 0126 { 0127 // first let QGraphicsScene do its work 0128 QGraphicsScene::mouseReleaseEvent(event); 0129 if (event->isAccepted()) { 0130 return; 0131 } 0132 0133 if (d->tool) { 0134 d->tool->mouseReleaseEvent(event); 0135 event->accept(); 0136 } 0137 } 0138 0139 void TikzScene::keyPressEvent(QKeyEvent * keyEvent) 0140 { 0141 // pass key event to current tool, if possible 0142 if (d->tool) { 0143 d->tool->keyPressEvent(keyEvent); 0144 if (keyEvent->isAccepted()) { 0145 return; 0146 } 0147 } 0148 0149 // on Del, remove selected items 0150 if (keyEvent->key() == Qt::Key_Delete && !document()->transactionRunning()) { 0151 // group deletion of items 0152 tikz::core::Transaction transaction(d->doc); 0153 0154 // delete all selected items 0155 for (QGraphicsItem* item : selectedItems()) { 0156 if (item->type() == QGraphicsItem::UserType + 2) { 0157 NodeItem* node = dynamic_cast<NodeItem*>(item); 0158 Q_ASSERT(node); 0159 d->doc->deleteNodeItem(node); 0160 } else if (item->type() == QGraphicsItem::UserType + 3) { 0161 tikz::ui::PathItem * path = dynamic_cast<tikz::ui::PathItem*>(item); 0162 Q_ASSERT(path); 0163 d->doc->deletePathItem(path); 0164 } 0165 } 0166 0167 keyEvent->accept(); 0168 return; 0169 } 0170 0171 // nothing done with the event, pass on 0172 QGraphicsScene::keyPressEvent(keyEvent); 0173 } 0174 0175 void TikzScene::updateSceneRect() 0176 { 0177 // set sane scene rect 0178 setSceneRect(-tikz::cm2pt(15), -tikz::cm2pt(15), 0179 tikz::cm2pt(30), tikz::cm2pt(30)); 0180 return; 0181 qDebug() << "ITEM SCENERECT CHANGED"; 0182 setSceneRect(itemsBoundingRect()); 0183 } 0184 0185 } 0186 } 0187 0188 // kate: indent-width 4; replace-tabs on;