kivio

tool_zoom.cpp

00001 /*
00002  * Kivio - Visual Modelling and Flowcharting
00003  * Copyright (C) 2000-2003 theKompany.com & Dave Marotti,
00004  *                         Peter Simonsson
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  */
00020 #include "tool_zoom.h"
00021 #include "kivio_view.h"
00022 #include "kivio_page.h"
00023 #include "kivio_canvas.h"
00024 #include "kivio_factory.h"
00025 
00026 #include <kaction.h>
00027 #include <kiconloader.h>
00028 #include <klocale.h>
00029 #include <kpopupmenu.h>
00030 #include <ktoolbar.h>
00031 #include <KoMainWindow.h>
00032 #include <kdebug.h>
00033 #include <KoZoomHandler.h>
00034 #include <KoPoint.h>
00035 #include <kstdaction.h>
00036 #include <KoZoomAction.h>
00037 
00038 #include <qapplication.h>
00039 #include <qcursor.h>
00040 
00041 ZoomTool::ZoomTool(KivioView* parent) : Kivio::MouseTool(parent, "Zoom Mouse Tool")
00042 {
00043   m_zoomAction = new KRadioAction(i18n("&Zoom"), "viewmag", CTRL + ALT + Key_Z, actionCollection(), "zoom");
00044   m_zoomAction->setWhatsThis(i18n("By pressing this button you can zoom in on a specific area."));
00045   m_panAction = new KRadioAction(i18n("&Pan Document"), "kivio_zoom_hand", CTRL + ALT + Key_H, actionCollection(), "pan");
00046   m_panAction->setWhatsThis(i18n("You can drag the document by using the mouse."));
00047   m_zoomAction->setExclusiveGroup("zoomAction");
00048   m_panAction->setExclusiveGroup("zoomAction");
00049   connect(m_zoomAction,SIGNAL(activated()),SLOT(zoomActivated()));
00050   connect(m_panAction,SIGNAL(activated()),SLOT(handActivated()));
00051   connect(m_zoomAction, SIGNAL(toggled(bool)), this, SLOT(setActivated(bool)));
00052   connect(m_panAction, SIGNAL(toggled(bool)), this, SLOT(setActivated(bool)));
00053 
00054   KoZoomAction* viewZoom = new KoZoomAction(i18n("Zoom &Level"), "viewmag", 0, actionCollection(), "viewZoom" );
00055   viewZoom->setWhatsThis(i18n("This allows you to zoom in or out of a document. You can either choose one of the predefined zoomfactors or enter a new zoomfactor (in percent)."));
00056   connect(viewZoom, SIGNAL(zoomChanged(const QString&)), parent, SLOT(viewZoom(const QString&)));
00057   connect(parent, SIGNAL(zoomChanged(int)), viewZoom, SLOT(setZoom(int)));
00058   
00059   m_pPlus = KStdAction::zoomIn(this, SLOT(zoomPlus()), actionCollection(), "zoomPlus");
00060   m_pPlus->setWhatsThis(i18n("You can zoom in on the document by pressing this button."));
00061 
00062   m_pMinus = KStdAction::zoomOut(this, SLOT(zoomMinus()), actionCollection(), "zoomMinus");
00063   m_pMinus->setWhatsThis(i18n("By pressing this button you can zoom out of the document."));
00064 
00065   m_pZoomWidth = new KAction( i18n("Zoom Width"), "kivio_zoom_width", SHIFT+Key_F4, actionCollection(), "zoomWidth" );
00066   m_pZoomWidth->setWhatsThis(i18n("You can zoom the document that it fits into the window width."));
00067   connect(m_pZoomWidth,SIGNAL(activated()),SLOT(zoomWidth()));
00068 
00069   m_pZoomHeight = new KAction( i18n("Zoom Height"), "kivio_zoom_height", SHIFT+Key_F5, actionCollection(), "zoomHeight" );
00070   m_pZoomHeight->setWhatsThis(i18n("You can zoom the document that it fits into the window height."));
00071   connect(m_pZoomHeight,SIGNAL(activated()),SLOT(zoomHeight()));
00072 
00073   m_pZoomPage = new KAction( i18n("Zoom Page"), "kivio_zoom_page", SHIFT+Key_F6, actionCollection(), "zoomPage" );
00074   m_pZoomPage->setWhatsThis(i18n("The Zoom Page button shows the entire page."));
00075   connect(m_pZoomPage,SIGNAL(activated()),SLOT(zoomPage()));
00076 
00077   m_pZoomSelected = new KAction( i18n("Zoom Selected"), "kivio_zoom_selected", CTRL+Key_Y, actionCollection(), "zoomSelected" );
00078   m_pZoomSelected->setWhatsThis(i18n("By pressing this button you zoom in on the document, so that all <b>selected</b> objects are visible."));
00079   connect(m_pZoomSelected,SIGNAL(activated()),SLOT(zoomSelected()));
00080 
00081   m_pZoomAllObjects = new KAction( i18n("Zoom All Objects"), "kivio_zoom_allobject", 0, actionCollection(), "zoomAllObjects" );
00082   m_pZoomAllObjects->setWhatsThis(i18n("You are able to zoom in on the document, so that all objects are visible by pressing this button."));
00083   connect(m_pZoomAllObjects,SIGNAL(activated()),SLOT(zoomAllobjects()));
00084 
00085   QPixmap pix;
00086 
00087   pix = BarIcon("kivio_zoom_plus",KivioFactory::global());
00088   m_pPlusCursor = new QCursor(pix,pix.width()/2,pix.height()/2);
00089 
00090   pix = BarIcon("kivio_zoom_minus",KivioFactory::global());
00091   m_pMinusCursor = new QCursor(pix,pix.width()/2,pix.height()/2);
00092 
00093   pix = BarIcon("kivio_zoom_hand",KivioFactory::global());
00094   m_handCursor = new QCursor(pix,pix.width()/2,pix.height()/2);
00095 
00096   m_pMenu = 0;
00097 }
00098 
00099 ZoomTool::~ZoomTool()
00100 {
00101   delete m_pPlusCursor;
00102   delete m_pMinusCursor;
00103   delete m_handCursor;
00104 }
00105 
00106 bool ZoomTool::processEvent(QEvent* e)
00107 {
00108   KivioCanvas* canvas = view()->canvasWidget();
00109 
00110   if(!m_bHandMode) {
00111     switch(e->type()) {
00112       case QEvent::KeyPress:
00113         if (!m_bLockKeyboard && (static_cast<QKeyEvent*>(e)->key() == Key_Shift)) {
00114           m_pCurrent = m_pMinus;
00115           canvas->setCursor(*m_pMinusCursor);
00116           return true;
00117         }
00118         break;
00119       case QEvent::KeyRelease:
00120         if (!m_bLockKeyboard && (static_cast<QKeyEvent*>(e)->key() == Key_Shift)) {
00121           m_pCurrent = m_pPlus;
00122           canvas->setCursor(*m_pPlusCursor);
00123           return true;
00124         }
00125         break;
00126       case QEvent::MouseButtonPress:
00127       {
00128         QMouseEvent* me = static_cast<QMouseEvent*>(e);
00129         
00130         if(me->button() == LeftButton) {
00131           if(m_pCurrent == m_pMinus) {
00132             m_pCurrent->activate();
00133           } else {
00134             m_bLockKeyboard = true;
00135             m_bDrawRubber = true;
00136             canvas->startRectDraw(me->pos(), KivioCanvas::Rubber);
00137           }
00138         } else {
00139           showPopupMenu(me->globalPos());
00140         }
00141         
00142         return true;
00143         break;
00144       }
00145       case QEvent::MouseButtonRelease:
00146         if(m_pCurrent == m_pPlus && m_bDrawRubber) {
00147           canvas->endRectDraw();
00148           m_bDrawRubber = false;
00149           m_bLockKeyboard = false;
00150           zoomRect(canvas->rect());
00151           return true;
00152         }
00153         break;
00154       case QEvent::MouseMove:
00155         if (m_bDrawRubber) {
00156           canvas->continueRectDraw(static_cast<QMouseEvent*>(e)->pos(), KivioCanvas::Rubber);
00157           return true;
00158         }
00159         break;
00160       default:
00161         break;
00162     }
00163   } else {
00164     switch(e->type()) {
00165       case QEvent::MouseButtonPress:
00166         isHandMousePressed = true;
00167         mousePos = static_cast<QMouseEvent*>(e)->pos();
00168         return true;
00169         break;
00170       case QEvent::MouseButtonRelease:
00171         isHandMousePressed = false;
00172         return true;
00173         break;
00174       case QEvent::MouseMove:
00175         if (isHandMousePressed) {
00176           canvas->setUpdatesEnabled(false);
00177           QPoint newPos = static_cast<QMouseEvent*>(e)->pos();
00178           mousePos -= newPos;
00179           canvas->scrollDx(-mousePos.x());
00180           canvas->scrollDy(-mousePos.y());
00181           mousePos = newPos;
00182           canvas->setUpdatesEnabled(true);
00183           return true;
00184         }
00185         break;
00186       default:
00187         break;
00188     }
00189   }
00190 
00191   return false;
00192 }
00193 
00194 void ZoomTool::setActivated(bool a)
00195 {
00196   if(a) {
00197     m_pCurrent = m_pPlus;
00198     emit activated(this);
00199   } else {
00200     kdDebug(43000) << "ZoomTool DeActivate" << endl;
00201   
00202     m_pCurrent = 0L;
00203   
00204     m_zoomAction->setChecked(false);
00205     m_panAction->setChecked(false);
00206     view()->setStatusBarInfo("");
00207   
00208     if (!view()->canvasWidget()->isUpdatesEnabled()) {
00209       view()->canvasWidget()->setUpdatesEnabled(true);
00210     }
00211   }
00212 }
00213 
00214 void ZoomTool::zoomActivated()
00215 {
00216   view()->canvasWidget()->setCursor(*m_pPlusCursor);
00217   m_bHandMode = false;
00218   m_bDrawRubber = false;
00219   m_bLockKeyboard = false;
00220   m_zoomAction->setChecked(true);
00221   m_panAction->setChecked(false);
00222   view()->setStatusBarInfo(i18n("Hold Shift to zoom out."));
00223 }
00224 
00225 void ZoomTool::handActivated()
00226 {
00227   view()->canvasWidget()->setCursor(*m_handCursor);
00228   m_bHandMode = true;
00229   isHandMousePressed = false;
00230   m_zoomAction->setChecked(false);
00231   m_panAction->setChecked(true);
00232 }
00233 
00234 void ZoomTool::zoomPlus()
00235 {
00236   KivioCanvas* canvas = view()->canvasWidget();
00237   canvas->zoomIn(QPoint(canvas->width()/2, canvas->height()/2));
00238   if(view()->zoomHandler()->zoom() >= 2000)
00239   {
00240     m_pPlus->setEnabled(false);
00241     m_pMinus->setEnabled(true);
00242   }
00243   else
00244   {
00245     m_pPlus->setEnabled(true);
00246     m_pMinus->setEnabled(true);
00247   }
00248 }
00249 
00250 void ZoomTool::zoomMinus()
00251 {
00252   KivioCanvas* canvas = view()->canvasWidget();
00253   canvas->zoomOut(QPoint(canvas->width()/2, canvas->height()/2));
00254   if(view()->zoomHandler()->zoom() <= 25)
00255   {
00256     m_pMinus->setEnabled(false);
00257     m_pPlus->setEnabled(true);
00258   }
00259   else
00260   {
00261     m_pMinus->setEnabled(true);
00262     m_pPlus->setEnabled(true);
00263   }
00264 }
00265 
00266 void ZoomTool::zoomWidth()
00267 {
00268   KivioCanvas* canvas = view()->canvasWidget();
00269   KoZoomHandler zoom;
00270   zoom.setZoomAndResolution(100, KoGlobal::dpiX(),
00271     KoGlobal::dpiY());
00272   int cw = QMAX(10,canvas->width()-20);
00273   KoPageLayout pl = canvas->activePage()->paperLayout();
00274   float w = zoom.zoomItX(pl.ptWidth);
00275   float z = cw/w;
00276 
00277   canvas->setUpdatesEnabled(false);
00278   view()->viewZoom(qRound(z * 100));
00279   canvas->setUpdatesEnabled(true);
00280 }
00281 
00282 void ZoomTool::zoomHeight()
00283 {
00284   KivioCanvas* canvas = view()->canvasWidget();
00285   KoZoomHandler zoom;
00286   zoom.setZoomAndResolution(100, KoGlobal::dpiX(),
00287     KoGlobal::dpiY());
00288   int ch = QMAX(10,canvas->height()-20);
00289   KoPageLayout pl = canvas->activePage()->paperLayout();
00290   float h = zoom.zoomItY(pl.ptHeight);
00291   float zh = ch/h;
00292 
00293   canvas->setUpdatesEnabled(false);
00294   view()->viewZoom(qRound(zh * 100));
00295   canvas->setUpdatesEnabled(true);
00296 }
00297 
00298 void ZoomTool::zoomPage()
00299 {
00300   KivioCanvas* canvas = view()->canvasWidget();
00301   KoZoomHandler zoom;
00302   zoom.setZoomAndResolution(100, KoGlobal::dpiX(),
00303     KoGlobal::dpiY());
00304   int cw = QMAX(10,canvas->width()-20);
00305   int ch = QMAX(10,canvas->height()-20);
00306 
00307   KoPageLayout pl = canvas->activePage()->paperLayout();
00308   float w = zoom.zoomItX(pl.ptWidth);
00309   float h = zoom.zoomItY(pl.ptHeight);
00310 
00311   float z = QMIN(cw/w,ch/h);
00312 
00313   canvas->setUpdatesEnabled(false);
00314   view()->viewZoom(qRound(z * 100));
00315   canvas->setUpdatesEnabled(true);
00316 }
00317 
00318 void ZoomTool::showPopupMenu(const QPoint& p )
00319 {
00320   if(!m_pMenu) {
00321     m_pMenu = static_cast<KPopupMenu*>(factory()->container("ZoomPopup", this));
00322   }
00323   
00324   if(m_pMenu) {
00325     m_pMenu->popup(p);
00326   } else {
00327     kdDebug(43000) << "What no popup! *ARGH*!" << endl;
00328   }
00329 }
00330 
00331 void ZoomTool::zoomSelected()
00332 {
00333   KivioCanvas* canvas = view()->canvasWidget();
00334   KoRect r = canvas->activePage()->getRectForAllSelectedStencils();
00335 
00336   if (!r.isNull() && r.isValid()) {
00337     canvas->setVisibleArea(r);
00338   }
00339 }
00340 
00341 void ZoomTool::zoomAllobjects()
00342 {
00343   KivioCanvas* canvas = view()->canvasWidget();
00344   KoRect r = canvas->activePage()->getRectForAllStencils();
00345 
00346   if (!r.isNull() && r.isValid()) {
00347     canvas->setVisibleArea(r);
00348   }
00349 }
00350 
00351 void ZoomTool::zoomRect(QRect r)
00352 {
00353   KivioCanvas* canvas = view()->canvasWidget();
00354   
00355   if (r.isEmpty()) {
00356     canvas->zoomIn(r.topLeft());
00357     return;
00358   }
00359 
00360   KoPoint p0 = canvas->mapFromScreen(r.topLeft());
00361   canvas->setVisibleArea(KoRect(p0.x(), p0.y(), view()->zoomHandler()
00362     ->unzoomItX(r.width()), view()->zoomHandler()->unzoomItY(r.height())));
00363 }
00364 #include "tool_zoom.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys