kivio
addstenciltool.cpp
00001 /* 00002 * Kivio - Visual Modelling and Flowcharting 00003 * Copyright (C) 2005 Peter Simonsson <psn@linux.se> 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 */ 00019 #include "addstenciltool.h" 00020 00021 #include <qevent.h> 00022 #include <qcursor.h> 00023 00024 #include "kdebug.h" 00025 00026 #include "kivio_canvas.h" 00027 #include "kivio_stencil_spawner.h" 00028 #include "kivio_stencil.h" 00029 #include "kivio_page.h" 00030 #include "kivio_doc.h" 00031 #include "kivio_pluginmanager.h" 00032 00033 namespace Kivio { 00034 00035 AddStencilTool::AddStencilTool(KivioView* parent) 00036 : MouseTool(parent, "Add Stencil Mouse Tool") 00037 { 00038 m_leftMouseButtonPressed = false; 00039 m_stencil = 0; 00040 m_spawner = 0; 00041 } 00042 00043 AddStencilTool::~AddStencilTool() 00044 { 00045 delete m_stencil; 00046 m_stencil = 0; 00047 } 00048 00049 bool AddStencilTool::processEvent(QEvent* e) 00050 { 00051 switch(e->type()) { 00052 case QEvent::MouseButtonPress: 00053 { 00054 QMouseEvent* me = static_cast<QMouseEvent*>(e); 00055 00056 if(me->button() == LeftButton) { 00057 m_leftMouseButtonPressed = true; 00058 createStencil(me->pos()); 00059 return true; 00060 } 00061 00062 break; 00063 } 00064 case QEvent::MouseButtonRelease: 00065 { 00066 QMouseEvent* me = static_cast<QMouseEvent*>(e); 00067 00068 if(m_leftMouseButtonPressed && (me->button() == LeftButton)) { 00069 m_leftMouseButtonPressed = false; 00070 endOperation(me->pos()); 00071 return true; 00072 } 00073 00074 break; 00075 } 00076 case QEvent::MouseMove: 00077 { 00078 QMouseEvent* me = static_cast<QMouseEvent*>(e); 00079 00080 if(m_leftMouseButtonPressed) { 00081 resize(me->pos()); 00082 return true; 00083 } 00084 00085 break; 00086 } 00087 default: 00088 break; 00089 } 00090 00091 return false; 00092 } 00093 00094 void AddStencilTool::setActivated(bool a) 00095 { 00096 if(a) { 00097 m_leftMouseButtonPressed = false; 00098 view()->canvasWidget()->setCursor(Qt::CrossCursor); 00099 emit activated(this); 00100 } else { 00101 view()->canvasWidget()->unsetCursor(); 00102 m_spawner = 0; 00103 delete m_stencil; 00104 m_stencil = 0; 00105 } 00106 } 00107 00108 void AddStencilTool::activateNewStencil(KivioStencilSpawner* spawner) 00109 { 00110 m_spawner = spawner; 00111 setActivated(true); 00112 } 00113 00114 void AddStencilTool::createStencil(const QPoint& position) 00115 { 00116 if(!m_spawner) { 00117 return; 00118 } 00119 00120 m_startPoint = view()->canvasWidget()->mapFromScreen(position); 00121 m_startPoint = view()->canvasWidget()->snapToGridAndGuides(m_startPoint); 00122 00123 m_stencil = m_spawner->newStencil(); 00124 00125 if(!m_stencil) { 00126 return; 00127 } 00128 00129 m_stencil->setPosition(m_startPoint.x(), m_startPoint.y()); 00130 m_originalSize.setWidth(m_stencil->w()); 00131 m_originalSize.setHeight(m_stencil->h()); 00132 m_stencil->setW(1); 00133 m_stencil->setH(1); 00134 view()->canvasWidget()->beginUnclippedSpawnerPainter(); 00135 view()->canvasWidget()->drawStencilXOR(m_stencil); 00136 } 00137 00138 void AddStencilTool::endOperation(const QPoint& position) 00139 { 00140 if(!m_stencil) { 00141 return; 00142 } 00143 00144 KoPoint endPoint = view()->canvasWidget()->mapFromScreen(position); 00145 endPoint = view()->canvasWidget()->snapToGridAndGuides(endPoint); 00146 00147 if(m_startPoint == endPoint) { 00148 m_stencil->setW(m_originalSize.width()); 00149 m_stencil->setH(m_originalSize.height()); 00150 } 00151 00152 KivioPage* page = view()->canvasWidget()->activePage(); 00153 page->unselectAllStencils(); 00154 page->addStencil(m_stencil); 00155 page->selectStencil(m_stencil); 00156 m_stencil = 0; 00157 00158 view()->canvasWidget()->endUnclippedSpawnerPainter(); 00159 view()->doc()->updateView(page); 00160 00161 view()->pluginManager()->activateDefaultTool(); 00162 } 00163 00164 void AddStencilTool::resize(const QPoint& position) 00165 { 00166 KoPoint endPoint = view()->canvasWidget()->mapFromScreen(position); 00167 endPoint = view()->canvasWidget()->snapToGridAndGuides(endPoint); 00168 00169 view()->canvasWidget()->drawStencilXOR(m_stencil); 00170 m_stencil->setW(endPoint.x() - m_startPoint.x()); 00171 m_stencil->setH(endPoint.y() - m_startPoint.y()); 00172 view()->canvasWidget()->drawStencilXOR(m_stencil); 00173 } 00174 00175 } 00176 00177 #include "addstenciltool.moc"