summaryrefslogtreecommitdiffstats
path: root/kivio/plugins/kiviozoomtool/tool_zoom.cpp
blob: d2a10db87aeb4d055476fcfc589996848be91ec5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * Kivio - Visual Modelling and Flowcharting
 * Copyright (C) 2000-2003 theKompany.com & Dave Marotti,
 *                         Peter Simonsson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include "tool_zoom.h"
#include "kivio_view.h"
#include "kivio_page.h"
#include "kivio_canvas.h"
#include "kivio_factory.h"

#include <tdeaction.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <tdetoolbar.h>
#include <KoMainWindow.h>
#include <kdebug.h>
#include <KoZoomHandler.h>
#include <KoPoint.h>
#include <kstdaction.h>
#include <KoZoomAction.h>

#include <tqapplication.h>
#include <tqcursor.h>

ZoomTool::ZoomTool(KivioView* parent) : Kivio::MouseTool(parent, "Zoom Mouse Tool")
{
  m_zoomAction = new TDERadioAction(i18n("&Zoom"), "viewmag", CTRL + ALT + Key_Z, actionCollection(), "zoom");
  m_zoomAction->setWhatsThis(i18n("By pressing this button you can zoom in on a specific area."));
  m_panAction = new TDERadioAction(i18n("&Pan Document"), "kivio_zoom_hand", CTRL + ALT + Key_H, actionCollection(), "pan");
  m_panAction->setWhatsThis(i18n("You can drag the document by using the mouse."));
  m_zoomAction->setExclusiveGroup("zoomAction");
  m_panAction->setExclusiveGroup("zoomAction");
  connect(m_zoomAction,TQT_SIGNAL(activated()),TQT_SLOT(zoomActivated()));
  connect(m_panAction,TQT_SIGNAL(activated()),TQT_SLOT(handActivated()));
  connect(m_zoomAction, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(setActivated(bool)));
  connect(m_panAction, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(setActivated(bool)));

  KoZoomAction* viewZoom = new KoZoomAction(i18n("Zoom &Level"), "viewmag", 0, actionCollection(), "viewZoom" );
  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)."));
  connect(viewZoom, TQT_SIGNAL(zoomChanged(const TQString&)), parent, TQT_SLOT(viewZoom(const TQString&)));
  connect(parent, TQT_SIGNAL(zoomChanged(int)), viewZoom, TQT_SLOT(setZoom(int)));
  
  m_pPlus = KStdAction::zoomIn(this, TQT_SLOT(zoomPlus()), actionCollection(), "zoomPlus");
  m_pPlus->setWhatsThis(i18n("You can zoom in on the document by pressing this button."));

  m_pMinus = KStdAction::zoomOut(this, TQT_SLOT(zoomMinus()), actionCollection(), "zoomMinus");
  m_pMinus->setWhatsThis(i18n("By pressing this button you can zoom out of the document."));

  m_pZoomWidth = new TDEAction( i18n("Zoom Width"), "kivio_zoom_width", SHIFT+Key_F4, actionCollection(), "zoomWidth" );
  m_pZoomWidth->setWhatsThis(i18n("You can zoom the document that it fits into the window width."));
  connect(m_pZoomWidth,TQT_SIGNAL(activated()),TQT_SLOT(zoomWidth()));

  m_pZoomHeight = new TDEAction( i18n("Zoom Height"), "kivio_zoom_height", SHIFT+Key_F5, actionCollection(), "zoomHeight" );
  m_pZoomHeight->setWhatsThis(i18n("You can zoom the document that it fits into the window height."));
  connect(m_pZoomHeight,TQT_SIGNAL(activated()),TQT_SLOT(zoomHeight()));

  m_pZoomPage = new TDEAction( i18n("Zoom Page"), "kivio_zoom_page", SHIFT+Key_F6, actionCollection(), "zoomPage" );
  m_pZoomPage->setWhatsThis(i18n("The Zoom Page button shows the entire page."));
  connect(m_pZoomPage,TQT_SIGNAL(activated()),TQT_SLOT(zoomPage()));

  m_pZoomSelected = new TDEAction( i18n("Zoom Selected"), "kivio_zoom_selected", CTRL+Key_Y, actionCollection(), "zoomSelected" );
  m_pZoomSelected->setWhatsThis(i18n("By pressing this button you zoom in on the document, so that all <b>selected</b> objects are visible."));
  connect(m_pZoomSelected,TQT_SIGNAL(activated()),TQT_SLOT(zoomSelected()));

  m_pZoomAllObjects = new TDEAction( i18n("Zoom All Objects"), "kivio_zoom_allobject", 0, actionCollection(), "zoomAllObjects" );
  m_pZoomAllObjects->setWhatsThis(i18n("You are able to zoom in on the document, so that all objects are visible by pressing this button."));
  connect(m_pZoomAllObjects,TQT_SIGNAL(activated()),TQT_SLOT(zoomAllobjects()));

  TQPixmap pix;

  pix = BarIcon("kivio_zoom_plus",KivioFactory::global());
  m_pPlusCursor = new TQCursor(pix,pix.width()/2,pix.height()/2);

  pix = BarIcon("kivio_zoom_minus",KivioFactory::global());
  m_pMinusCursor = new TQCursor(pix,pix.width()/2,pix.height()/2);

  pix = BarIcon("kivio_zoom_hand",KivioFactory::global());
  m_handCursor = new TQCursor(pix,pix.width()/2,pix.height()/2);

  m_pMenu = 0;
}

ZoomTool::~ZoomTool()
{
  delete m_pPlusCursor;
  delete m_pMinusCursor;
  delete m_handCursor;
}

bool ZoomTool::processEvent(TQEvent* e)
{
  KivioCanvas* canvas = view()->canvasWidget();

  if(!m_bHandMode) {
    switch(e->type()) {
      case TQEvent::KeyPress:
        if (!m_bLockKeyboard && (TQT_TQKEYEVENT(e)->key() == Key_Shift)) {
          m_pCurrent = m_pMinus;
          canvas->setCursor(*m_pMinusCursor);
          return true;
        }
        break;
      case TQEvent::KeyRelease:
        if (!m_bLockKeyboard && (TQT_TQKEYEVENT(e)->key() == Key_Shift)) {
          m_pCurrent = m_pPlus;
          canvas->setCursor(*m_pPlusCursor);
          return true;
        }
        break;
      case TQEvent::MouseButtonPress:
      {
        TQMouseEvent* me = TQT_TQMOUSEEVENT(e);
        
        if(me->button() == TQt::LeftButton) {
          if(m_pCurrent == m_pMinus) {
            m_pCurrent->activate();
          } else {
            m_bLockKeyboard = true;
            m_bDrawRubber = true;
            canvas->startRectDraw(me->pos(), KivioCanvas::Rubber);
          }
        } else {
          showPopupMenu(me->globalPos());
        }
        
        return true;
        break;
      }
      case TQEvent::MouseButtonRelease:
        if(m_pCurrent == m_pPlus && m_bDrawRubber) {
          canvas->endRectDraw();
          m_bDrawRubber = false;
          m_bLockKeyboard = false;
          zoomRect(canvas->rect());
          return true;
        }
        break;
      case TQEvent::MouseMove:
        if (m_bDrawRubber) {
          canvas->continueRectDraw(TQT_TQMOUSEEVENT(e)->pos(), KivioCanvas::Rubber);
          return true;
        }
        break;
      default:
        break;
    }
  } else {
    switch(e->type()) {
      case TQEvent::MouseButtonPress:
        isHandMousePressed = true;
        mousePos = TQT_TQMOUSEEVENT(e)->pos();
        return true;
        break;
      case TQEvent::MouseButtonRelease:
        isHandMousePressed = false;
        return true;
        break;
      case TQEvent::MouseMove:
        if (isHandMousePressed) {
          canvas->setUpdatesEnabled(false);
          TQPoint newPos = TQT_TQMOUSEEVENT(e)->pos();
          mousePos -= newPos;
          canvas->scrollDx(-mousePos.x());
          canvas->scrollDy(-mousePos.y());
          mousePos = newPos;
          canvas->setUpdatesEnabled(true);
          return true;
        }
        break;
      default:
        break;
    }
  }

  return false;
}

void ZoomTool::setActivated(bool a)
{
  if(a) {
    m_pCurrent = m_pPlus;
    emit activated(this);
  } else {
    kdDebug(43000) << "ZoomTool DeActivate" << endl;
  
    m_pCurrent = 0L;
  
    m_zoomAction->setChecked(false);
    m_panAction->setChecked(false);
    view()->setStatusBarInfo("");
  
    if (!view()->canvasWidget()->isUpdatesEnabled()) {
      view()->canvasWidget()->setUpdatesEnabled(true);
    }
  }
}

void ZoomTool::zoomActivated()
{
  view()->canvasWidget()->setCursor(*m_pPlusCursor);
  m_bHandMode = false;
  m_bDrawRubber = false;
  m_bLockKeyboard = false;
  m_zoomAction->setChecked(true);
  m_panAction->setChecked(false);
  view()->setStatusBarInfo(i18n("Hold Shift to zoom out."));
}

void ZoomTool::handActivated()
{
  view()->canvasWidget()->setCursor(*m_handCursor);
  m_bHandMode = true;
  isHandMousePressed = false;
  m_zoomAction->setChecked(false);
  m_panAction->setChecked(true);
}

void ZoomTool::zoomPlus()
{
  KivioCanvas* canvas = view()->canvasWidget();
  canvas->zoomIn(TQPoint(canvas->width()/2, canvas->height()/2));
  if(view()->zoomHandler()->zoom() >= 2000)
  {
    m_pPlus->setEnabled(false);
    m_pMinus->setEnabled(true);
  }
  else
  {
    m_pPlus->setEnabled(true);
    m_pMinus->setEnabled(true);
  }
}

void ZoomTool::zoomMinus()
{
  KivioCanvas* canvas = view()->canvasWidget();
  canvas->zoomOut(TQPoint(canvas->width()/2, canvas->height()/2));
  if(view()->zoomHandler()->zoom() <= 25)
  {
    m_pMinus->setEnabled(false);
    m_pPlus->setEnabled(true);
  }
  else
  {
    m_pMinus->setEnabled(true);
    m_pPlus->setEnabled(true);
  }
}

void ZoomTool::zoomWidth()
{
  KivioCanvas* canvas = view()->canvasWidget();
  KoZoomHandler zoom;
  zoom.setZoomAndResolution(100, KoGlobal::dpiX(),
    KoGlobal::dpiY());
  int cw = TQMAX(10,canvas->width()-20);
  KoPageLayout pl = canvas->activePage()->paperLayout();
  float w = zoom.zoomItX(pl.ptWidth);
  float z = cw/w;

  canvas->setUpdatesEnabled(false);
  view()->viewZoom(tqRound(z * 100));
  canvas->setUpdatesEnabled(true);
}

void ZoomTool::zoomHeight()
{
  KivioCanvas* canvas = view()->canvasWidget();
  KoZoomHandler zoom;
  zoom.setZoomAndResolution(100, KoGlobal::dpiX(),
    KoGlobal::dpiY());
  int ch = TQMAX(10,canvas->height()-20);
  KoPageLayout pl = canvas->activePage()->paperLayout();
  float h = zoom.zoomItY(pl.ptHeight);
  float zh = ch/h;

  canvas->setUpdatesEnabled(false);
  view()->viewZoom(tqRound(zh * 100));
  canvas->setUpdatesEnabled(true);
}

void ZoomTool::zoomPage()
{
  KivioCanvas* canvas = view()->canvasWidget();
  KoZoomHandler zoom;
  zoom.setZoomAndResolution(100, KoGlobal::dpiX(),
    KoGlobal::dpiY());
  int cw = TQMAX(10,canvas->width()-20);
  int ch = TQMAX(10,canvas->height()-20);

  KoPageLayout pl = canvas->activePage()->paperLayout();
  float w = zoom.zoomItX(pl.ptWidth);
  float h = zoom.zoomItY(pl.ptHeight);

  float z = TQMIN(cw/w,ch/h);

  canvas->setUpdatesEnabled(false);
  view()->viewZoom(tqRound(z * 100));
  canvas->setUpdatesEnabled(true);
}

void ZoomTool::showPopupMenu(const TQPoint& p )
{
  if(!m_pMenu) {
    m_pMenu = static_cast<TDEPopupMenu*>(factory()->container("ZoomPopup", this));
  }
  
  if(m_pMenu) {
    m_pMenu->popup(p);
  } else {
    kdDebug(43000) << "What no popup! *ARGH*!" << endl;
  }
}

void ZoomTool::zoomSelected()
{
  KivioCanvas* canvas = view()->canvasWidget();
  KoRect r = canvas->activePage()->getRectForAllSelectedStencils();

  if (!r.isNull() && r.isValid()) {
    canvas->setVisibleArea(r);
  }
}

void ZoomTool::zoomAllobjects()
{
  KivioCanvas* canvas = view()->canvasWidget();
  KoRect r = canvas->activePage()->getRectForAllStencils();

  if (!r.isNull() && r.isValid()) {
    canvas->setVisibleArea(r);
  }
}

void ZoomTool::zoomRect(TQRect r)
{
  KivioCanvas* canvas = view()->canvasWidget();
  
  if (r.isEmpty()) {
    canvas->zoomIn(r.topLeft());
    return;
  }

  KoPoint p0 = canvas->mapFromScreen(r.topLeft());
  canvas->setVisibleArea(KoRect(p0.x(), p0.y(), view()->zoomHandler()
    ->unzoomItX(r.width()), view()->zoomHandler()->unzoomItY(r.height())));
}
#include "tool_zoom.moc"