tool_zoom.cpp
1/*
2 * Kivio - Visual Modelling and Flowcharting
3 * Copyright (C) 2000-2003 theKompany.com & Dave Marotti,
4 * Peter Simonsson
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include "tool_zoom.h"
21#include "kivio_view.h"
22#include "kivio_page.h"
23#include "kivio_canvas.h"
24#include "kivio_factory.h"
25
26#include <tdeaction.h>
27#include <kiconloader.h>
28#include <tdelocale.h>
29#include <tdepopupmenu.h>
30#include <tdetoolbar.h>
31#include <KoMainWindow.h>
32#include <kdebug.h>
33#include <KoZoomHandler.h>
34#include <KoPoint.h>
35#include <kstdaction.h>
36#include <KoZoomAction.h>
37
38#include <tqapplication.h>
39#include <tqcursor.h>
40
41ZoomTool::ZoomTool(KivioView* parent) : Kivio::MouseTool(parent, "Zoom Mouse Tool")
42{
43 m_zoomAction = new TDERadioAction(i18n("&Zoom"), "viewmag", CTRL + ALT + Key_Z, actionCollection(), "zoom");
44 m_zoomAction->setWhatsThis(i18n("By pressing this button you can zoom in on a specific area."));
45 m_panAction = new TDERadioAction(i18n("&Pan Document"), "kivio_zoom_hand", CTRL + ALT + Key_H, actionCollection(), "pan");
46 m_panAction->setWhatsThis(i18n("You can drag the document by using the mouse."));
47 m_zoomAction->setExclusiveGroup("zoomAction");
48 m_panAction->setExclusiveGroup("zoomAction");
49 connect(m_zoomAction,TQ_SIGNAL(activated()),TQ_SLOT(zoomActivated()));
50 connect(m_panAction,TQ_SIGNAL(activated()),TQ_SLOT(handActivated()));
51 connect(m_zoomAction, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(setActivated(bool)));
52 connect(m_panAction, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(setActivated(bool)));
53
54 KoZoomAction* viewZoom = new KoZoomAction(i18n("Zoom &Level"), "viewmag", 0, actionCollection(), "viewZoom" );
55 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)."));
56 connect(viewZoom, TQ_SIGNAL(zoomChanged(const TQString&)), parent, TQ_SLOT(viewZoom(const TQString&)));
57 connect(parent, TQ_SIGNAL(zoomChanged(int)), viewZoom, TQ_SLOT(setZoom(int)));
58
59 m_pPlus = KStdAction::zoomIn(this, TQ_SLOT(zoomPlus()), actionCollection(), "zoomPlus");
60 m_pPlus->setWhatsThis(i18n("You can zoom in on the document by pressing this button."));
61
62 m_pMinus = KStdAction::zoomOut(this, TQ_SLOT(zoomMinus()), actionCollection(), "zoomMinus");
63 m_pMinus->setWhatsThis(i18n("By pressing this button you can zoom out of the document."));
64
65 m_pZoomWidth = new TDEAction( i18n("Zoom Width"), "kivio_zoom_width", SHIFT+Key_F4, actionCollection(), "zoomWidth" );
66 m_pZoomWidth->setWhatsThis(i18n("You can zoom the document that it fits into the window width."));
67 connect(m_pZoomWidth,TQ_SIGNAL(activated()),TQ_SLOT(zoomWidth()));
68
69 m_pZoomHeight = new TDEAction( i18n("Zoom Height"), "kivio_zoom_height", SHIFT+Key_F5, actionCollection(), "zoomHeight" );
70 m_pZoomHeight->setWhatsThis(i18n("You can zoom the document that it fits into the window height."));
71 connect(m_pZoomHeight,TQ_SIGNAL(activated()),TQ_SLOT(zoomHeight()));
72
73 m_pZoomPage = new TDEAction( i18n("Zoom Page"), "kivio_zoom_page", SHIFT+Key_F6, actionCollection(), "zoomPage" );
74 m_pZoomPage->setWhatsThis(i18n("The Zoom Page button shows the entire page."));
75 connect(m_pZoomPage,TQ_SIGNAL(activated()),TQ_SLOT(zoomPage()));
76
77 m_pZoomSelected = new TDEAction( i18n("Zoom Selected"), "kivio_zoom_selected", CTRL+Key_Y, actionCollection(), "zoomSelected" );
78 m_pZoomSelected->setWhatsThis(i18n("By pressing this button you zoom in on the document, so that all <b>selected</b> objects are visible."));
79 connect(m_pZoomSelected,TQ_SIGNAL(activated()),TQ_SLOT(zoomSelected()));
80
81 m_pZoomAllObjects = new TDEAction( i18n("Zoom All Objects"), "kivio_zoom_allobject", 0, actionCollection(), "zoomAllObjects" );
82 m_pZoomAllObjects->setWhatsThis(i18n("You are able to zoom in on the document, so that all objects are visible by pressing this button."));
83 connect(m_pZoomAllObjects,TQ_SIGNAL(activated()),TQ_SLOT(zoomAllobjects()));
84
85 TQPixmap pix;
86
87 pix = BarIcon("kivio_zoom_plus",KivioFactory::global());
88 m_pPlusCursor = new TQCursor(pix,pix.width()/2,pix.height()/2);
89
90 pix = BarIcon("kivio_zoom_minus",KivioFactory::global());
91 m_pMinusCursor = new TQCursor(pix,pix.width()/2,pix.height()/2);
92
93 pix = BarIcon("kivio_zoom_hand",KivioFactory::global());
94 m_handCursor = new TQCursor(pix,pix.width()/2,pix.height()/2);
95
96 m_pMenu = 0;
97}
98
99ZoomTool::~ZoomTool()
100{
101 delete m_pPlusCursor;
102 delete m_pMinusCursor;
103 delete m_handCursor;
104}
105
106bool ZoomTool::processEvent(TQEvent* e)
107{
108 KivioCanvas* canvas = view()->canvasWidget();
109
110 if(!m_bHandMode) {
111 switch(e->type()) {
112 case TQEvent::KeyPress:
113 if (!m_bLockKeyboard && (static_cast<TQKeyEvent*>(e)->key() == Key_Shift)) {
114 m_pCurrent = m_pMinus;
115 canvas->setCursor(*m_pMinusCursor);
116 return true;
117 }
118 break;
119 case TQEvent::KeyRelease:
120 if (!m_bLockKeyboard && (static_cast<TQKeyEvent*>(e)->key() == Key_Shift)) {
121 m_pCurrent = m_pPlus;
122 canvas->setCursor(*m_pPlusCursor);
123 return true;
124 }
125 break;
126 case TQEvent::MouseButtonPress:
127 {
128 TQMouseEvent* me = static_cast<TQMouseEvent*>(e);
129
130 if(me->button() == TQt::LeftButton) {
131 if(m_pCurrent == m_pMinus) {
132 m_pCurrent->activate();
133 } else {
134 m_bLockKeyboard = true;
135 m_bDrawRubber = true;
136 canvas->startRectDraw(me->pos(), KivioCanvas::Rubber);
137 }
138 } else {
139 showPopupMenu(me->globalPos());
140 }
141
142 return true;
143 break;
144 }
145 case TQEvent::MouseButtonRelease:
146 if(m_pCurrent == m_pPlus && m_bDrawRubber) {
147 canvas->endRectDraw();
148 m_bDrawRubber = false;
149 m_bLockKeyboard = false;
150 zoomRect(canvas->rect());
151 return true;
152 }
153 break;
154 case TQEvent::MouseMove:
155 if (m_bDrawRubber) {
156 canvas->continueRectDraw(static_cast<TQMouseEvent*>(e)->pos(), KivioCanvas::Rubber);
157 return true;
158 }
159 break;
160 default:
161 break;
162 }
163 } else {
164 switch(e->type()) {
165 case TQEvent::MouseButtonPress:
166 isHandMousePressed = true;
167 mousePos = static_cast<TQMouseEvent*>(e)->pos();
168 return true;
169 break;
170 case TQEvent::MouseButtonRelease:
171 isHandMousePressed = false;
172 return true;
173 break;
174 case TQEvent::MouseMove:
175 if (isHandMousePressed) {
176 canvas->setUpdatesEnabled(false);
177 TQPoint newPos = static_cast<TQMouseEvent*>(e)->pos();
178 mousePos -= newPos;
179 canvas->scrollDx(-mousePos.x());
180 canvas->scrollDy(-mousePos.y());
181 mousePos = newPos;
182 canvas->setUpdatesEnabled(true);
183 return true;
184 }
185 break;
186 default:
187 break;
188 }
189 }
190
191 return false;
192}
193
194void ZoomTool::setActivated(bool a)
195{
196 if(a) {
197 m_pCurrent = m_pPlus;
198 emit activated(this);
199 } else {
200 kdDebug(43000) << "ZoomTool DeActivate" << endl;
201
202 m_pCurrent = 0L;
203
204 m_zoomAction->setChecked(false);
205 m_panAction->setChecked(false);
206 view()->setStatusBarInfo("");
207
208 if (!view()->canvasWidget()->isUpdatesEnabled()) {
209 view()->canvasWidget()->setUpdatesEnabled(true);
210 }
211 }
212}
213
214void ZoomTool::zoomActivated()
215{
216 view()->canvasWidget()->setCursor(*m_pPlusCursor);
217 m_bHandMode = false;
218 m_bDrawRubber = false;
219 m_bLockKeyboard = false;
220 m_zoomAction->setChecked(true);
221 m_panAction->setChecked(false);
222 view()->setStatusBarInfo(i18n("Hold Shift to zoom out."));
223}
224
225void ZoomTool::handActivated()
226{
227 view()->canvasWidget()->setCursor(*m_handCursor);
228 m_bHandMode = true;
229 isHandMousePressed = false;
230 m_zoomAction->setChecked(false);
231 m_panAction->setChecked(true);
232}
233
234void ZoomTool::zoomPlus()
235{
236 KivioCanvas* canvas = view()->canvasWidget();
237 canvas->zoomIn(TQPoint(canvas->width()/2, canvas->height()/2));
238 if(view()->zoomHandler()->zoom() >= 2000)
239 {
240 m_pPlus->setEnabled(false);
241 m_pMinus->setEnabled(true);
242 }
243 else
244 {
245 m_pPlus->setEnabled(true);
246 m_pMinus->setEnabled(true);
247 }
248}
249
250void ZoomTool::zoomMinus()
251{
252 KivioCanvas* canvas = view()->canvasWidget();
253 canvas->zoomOut(TQPoint(canvas->width()/2, canvas->height()/2));
254 if(view()->zoomHandler()->zoom() <= 25)
255 {
256 m_pMinus->setEnabled(false);
257 m_pPlus->setEnabled(true);
258 }
259 else
260 {
261 m_pMinus->setEnabled(true);
262 m_pPlus->setEnabled(true);
263 }
264}
265
266void ZoomTool::zoomWidth()
267{
268 KivioCanvas* canvas = view()->canvasWidget();
269 KoZoomHandler zoom;
270 zoom.setZoomAndResolution(100, KoGlobal::dpiX(),
271 KoGlobal::dpiY());
272 int cw = TQMAX(10,canvas->width()-20);
273 KoPageLayout pl = canvas->activePage()->paperLayout();
274 float w = zoom.zoomItX(pl.ptWidth);
275 float z = cw/w;
276
277 canvas->setUpdatesEnabled(false);
278 view()->viewZoom(tqRound(z * 100));
279 canvas->setUpdatesEnabled(true);
280}
281
282void ZoomTool::zoomHeight()
283{
284 KivioCanvas* canvas = view()->canvasWidget();
285 KoZoomHandler zoom;
286 zoom.setZoomAndResolution(100, KoGlobal::dpiX(),
287 KoGlobal::dpiY());
288 int ch = TQMAX(10,canvas->height()-20);
289 KoPageLayout pl = canvas->activePage()->paperLayout();
290 float h = zoom.zoomItY(pl.ptHeight);
291 float zh = ch/h;
292
293 canvas->setUpdatesEnabled(false);
294 view()->viewZoom(tqRound(zh * 100));
295 canvas->setUpdatesEnabled(true);
296}
297
298void ZoomTool::zoomPage()
299{
300 KivioCanvas* canvas = view()->canvasWidget();
301 KoZoomHandler zoom;
302 zoom.setZoomAndResolution(100, KoGlobal::dpiX(),
303 KoGlobal::dpiY());
304 int cw = TQMAX(10,canvas->width()-20);
305 int ch = TQMAX(10,canvas->height()-20);
306
307 KoPageLayout pl = canvas->activePage()->paperLayout();
308 float w = zoom.zoomItX(pl.ptWidth);
309 float h = zoom.zoomItY(pl.ptHeight);
310
311 float z = TQMIN(cw/w,ch/h);
312
313 canvas->setUpdatesEnabled(false);
314 view()->viewZoom(tqRound(z * 100));
315 canvas->setUpdatesEnabled(true);
316}
317
318void ZoomTool::showPopupMenu(const TQPoint& p )
319{
320 if(!m_pMenu) {
321 m_pMenu = static_cast<TDEPopupMenu*>(factory()->container("ZoomPopup", this));
322 }
323
324 if(m_pMenu) {
325 m_pMenu->popup(p);
326 } else {
327 kdDebug(43000) << "What no popup! *ARGH*!" << endl;
328 }
329}
330
331void ZoomTool::zoomSelected()
332{
333 KivioCanvas* canvas = view()->canvasWidget();
334 KoRect r = canvas->activePage()->getRectForAllSelectedStencils();
335
336 if (!r.isNull() && r.isValid()) {
337 canvas->setVisibleArea(r);
338 }
339}
340
341void ZoomTool::zoomAllobjects()
342{
343 KivioCanvas* canvas = view()->canvasWidget();
344 KoRect r = canvas->activePage()->getRectForAllStencils();
345
346 if (!r.isNull() && r.isValid()) {
347 canvas->setVisibleArea(r);
348 }
349}
350
351void ZoomTool::zoomRect(TQRect r)
352{
353 KivioCanvas* canvas = view()->canvasWidget();
354
355 if (r.isEmpty()) {
356 canvas->zoomIn(r.topLeft());
357 return;
358 }
359
360 KoPoint p0 = canvas->mapFromScreen(r.topLeft());
361 canvas->setVisibleArea(KoRect(p0.x(), p0.y(), view()->zoomHandler()
362 ->unzoomItX(r.width()), view()->zoomHandler()->unzoomItY(r.height())));
363}
364#include "tool_zoom.moc"
Baseclass for mouse tools (like eg the select tool)