kivio

kiviotargettool.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Peter Simonsson <psn@linux.se>,
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 #include "kiviotargettool.h"
00020 
00021 #include <qcursor.h>
00022 
00023 #include <klocale.h>
00024 
00025 #include "kivio_stencil.h"
00026 #include "kivio_view.h"
00027 #include "kivio_canvas.h"
00028 #include "kivio_page.h"
00029 #include "kivio_doc.h"
00030 #include "mousetoolaction.h"
00031 #include "kivio_pluginmanager.h"
00032 #include "kivio_command.h"
00033 
00034 namespace Kivio {
00035   TargetTool::TargetTool(KivioView* parent) : MouseTool(parent, "Add Target Mouse Tool")
00036   {
00037     m_targetAction = new Kivio::MouseToolAction(i18n("Add Connector Target"),
00038         "add_target", 0, actionCollection(), "addTargetTool");
00039     connect(m_targetAction, SIGNAL(toggled(bool)), this, SLOT(setActivated(bool)));
00040     connect(m_targetAction, SIGNAL(doubleClicked()), this, SLOT(makePermanent()));
00041 
00042     m_permanent = false;
00043   }
00044   
00045   TargetTool::~TargetTool()
00046   {
00047   }
00048   
00049   bool TargetTool::processEvent(QEvent* e)
00050   {
00051     if(e->type() == QEvent::MouseButtonPress) {
00052       mousePress(static_cast<QMouseEvent*>(e));
00053       return true;
00054     } else if(e->type() == QEvent::MouseMove) {
00055       mouseMove(static_cast<QMouseEvent*>(e));
00056       return true;
00057     }
00058     
00059     return false;
00060   }
00061   
00062   void TargetTool::setActivated(bool a)
00063   {
00064     if(a) {
00065       m_targetAction->setChecked(true);
00066       emit activated(this);
00067     } else if(m_targetAction->isChecked()) {
00068       m_targetAction->setChecked(false);
00069       m_permanent = false;
00070     }
00071   }
00072   
00073   void TargetTool::applyToolAction(KivioStencil* stencil, const KoPoint& pos)
00074   {
00075     KivioAddConnectorTargetCommand* command = new KivioAddConnectorTargetCommand(i18n("Add Connector Target"), view()->activePage(), stencil, pos);
00076     command->execute();
00077     view()->doc()->addCommand(command);
00078   }
00079 
00080   void TargetTool::mousePress(QMouseEvent* e)
00081   {
00082     KoPoint p = view()->canvasWidget()->mapFromScreen(e->pos());
00083     int colType;
00084     KivioStencil* stencil = view()->canvasWidget()->activePage()->checkForStencil(&p, &colType, 0, false);
00085     
00086     if(stencil) {
00087       applyToolAction(stencil, p);
00088       
00089       if(!m_permanent) {
00090         view()->pluginManager()->activateDefaultTool();
00091       }
00092     }
00093   }
00094 
00095   void TargetTool::mouseMove(QMouseEvent* e)
00096   {
00097     KoPoint p = view()->canvasWidget()->mapFromScreen(e->pos());
00098     int colType;
00099     
00100     if(view()->canvasWidget()->activePage()->checkForStencil(&p, &colType, 0, false)) {
00101       view()->canvasWidget()->setCursor(Qt::CrossCursor);
00102     } else {
00103       view()->canvasWidget()->setCursor(Qt::ForbiddenCursor);
00104     }
00105   }
00106 
00107   void TargetTool::makePermanent()
00108   {
00109     m_permanent = true;
00110   }
00111 }
00112 
00113 #include "kiviotargettool.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys