summaryrefslogtreecommitdiffstats
path: root/kivio/kiviopart/ui/kivio_birdeye_panel.cpp
blob: 5185dfc6f0de01ecc0371d9aacf7657c0d97fb6c (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
#include "kivio_birdeye_panel.h"

#include "kivio_screen_painter.h"

#include "kivio_view.h"
#include "kivio_doc.h"
#include "kivio_page.h"
#include "kivio_canvas.h"
#include "kivio_common.h"
#include "kivio_factory.h"

#include <ktoolbar.h>
#include <kaction.h>
#include <klocale.h>
#include <KoGlobal.h>
#include <KoZoomHandler.h>
#include <kdeversion.h>
#include <kiconloader.h>
#include <kapplication.h>

#include <tqpixmap.h>
#include <tqslider.h>
#include <tqspinbox.h>
#include <tqframe.h>
#include <tqcursor.h>
#include <tqtoolbutton.h>

#define TOGGLE_ACTION(X) ((KToggleAction*)child(X))

KivioBirdEyePanel::KivioBirdEyePanel(KivioView* view, TQWidget* parent, const char* name)
: KivioBirdEyePanelBase(parent, name), m_pView(view), m_pCanvas(view->canvasWidget()), m_pDoc(view->doc())
{
  handlePress = false;
  m_buffer = new TQPixmap();
  canvas->installEventFilter(this);
  m_zoomHandler = new KoZoomHandler;

  connect( m_pDoc, TQT_SIGNAL( sig_updateView(KivioPage*)), TQT_SLOT(slotUpdateView(KivioPage*)) );
  connect( m_pView, TQT_SIGNAL(zoomChanged(int)), TQT_SLOT(canvasZoomChanged(int)));
  connect( m_pCanvas, TQT_SIGNAL(visibleAreaChanged()), TQT_SLOT(updateView()));

  m_zoomOutButton->setIconSet(SmallIconSet("viewmag-", 16));
  m_zoomInButton->setIconSet(SmallIconSet("viewmag+", 16));
  connect(m_zoomOutButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(zoomMinus()));
  connect(m_zoomInButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(zoomPlus()));
  connect(m_zoomSlider, TQT_SIGNAL(valueChanged(int)), m_pView, TQT_SLOT(viewZoom(int)));

  canvasZoomChanged(m_pView->zoomHandler()->zoom());
}

KivioBirdEyePanel::~KivioBirdEyePanel()
{
  delete m_buffer;
  delete m_zoomHandler;
}

void KivioBirdEyePanel::zoomMinus()
{
  m_pCanvas->zoomOut(TQPoint(m_pCanvas->width() / 2, m_pCanvas->height() / 2));
}

void KivioBirdEyePanel::zoomPlus()
{
  m_pCanvas->zoomIn(TQPoint(m_pCanvas->width() / 2, m_pCanvas->height() / 2));
}

void KivioBirdEyePanel::canvasZoomChanged(int zoom)
{
  m_zoomSlider->blockSignals(true);
  m_zoomSlider->setValue(zoom);
  m_zoomSlider->blockSignals(false);
  m_zoomOutButton->setEnabled(zoom > 25);
  m_zoomInButton->setEnabled(zoom < 2000);
  slotUpdateView(m_pView->activePage());
}

void KivioBirdEyePanel::slotUpdateView(KivioPage* page)
{
  if (!isVisible() || !page || m_pView->activePage() != page)
    return;

  updateView();
}

bool KivioBirdEyePanel::eventFilter(TQObject* o, TQEvent* ev)
{
  if (TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(canvas) && ev->type() == TQEvent::Show) {
    updateView();
  }

  if (TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(canvas) && ev->type() == TQEvent::Resize) {
    m_buffer->resize(canvas->size());
    slotUpdateView(m_pView->activePage());
  }

  if (TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(canvas) && ev->type() == TQEvent::Paint) {
    updateView();
    return true;
  }

  if (TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(canvas) && ev->type() == TQEvent::MouseMove) {
    TQMouseEvent* me = TQT_TQMOUSEEVENT(ev);
    if (me->state() == Qt::LeftButton)
      handleMouseMoveAction(me->pos());
    else
      handleMouseMove(me->pos());

    lastPos = me->pos();
    return true;
  }

  if (TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(canvas) && ev->type() == TQEvent::MouseButtonPress) {
    TQMouseEvent* me = TQT_TQMOUSEEVENT(ev);
    if (me->button() == Qt::LeftButton)
      handleMousePress(me->pos());

    return true;
  }

  return KivioBirdEyePanelBase::eventFilter(o, ev);
}

void KivioBirdEyePanel::updateView()
{
  if (!m_pView || !m_pView->activePage()) return;

  KoPageLayout pl = m_pView->activePage()->paperLayout();
  m_zoomHandler->setZoomAndResolution(100, KoGlobal::dpiX(),
                                      KoGlobal::dpiY());

  int pw = m_zoomHandler->zoomItX(pl.ptWidth);
  int ph = m_zoomHandler->zoomItY(pl.ptHeight);
  TQSize s2 = TQSize(pw,ph);
  TQSize s1 = canvas->size();

  double zx = (double)s1.width()/(double)s2.width();
  double zy = (double)s1.height()/(double)s2.height();
  double zxy = TQMIN(zx,zy);

  m_zoomHandler->setZoomAndResolution(tqRound(zxy * 100), KoGlobal::dpiX(),
    KoGlobal::dpiY());

  pw = m_zoomHandler->zoomItX(pl.ptWidth);
  ph = m_zoomHandler->zoomItY(pl.ptHeight);
  int px0 = (s1.width()-pw)/2;
  int py0 = (s1.height()-ph)/2;

  TQPoint p0 = TQPoint(px0,py0);

  TQRect rect(TQPoint(0,0),s1);

  KivioScreenPainter kpainter;
  kpainter.start(TQT_TQPAINTDEVICE(m_buffer));
  kpainter.painter()->fillRect(rect, KApplication::tqpalette().active().brush(TQColorGroup::Mid));

  kpainter.painter()->fillRect(px0, py0, pw, ph, white);

  kpainter.painter()->translate(px0, py0);
  m_pDoc->paintContent(kpainter, rect, false, m_pView->activePage(), p0, m_zoomHandler, false, false);

  KoRect vr = m_pCanvas->visibleArea();

  int x = m_zoomHandler->zoomItX(vr.x());
  int y = m_zoomHandler->zoomItY(vr.y());
  int w = m_zoomHandler->zoomItX(vr.width());
  int h = m_zoomHandler->zoomItX(vr.height());

  kpainter.setFGColor(red);
  kpainter.drawRect(x, y, w, h);
  kpainter.setFGColor(red.light());
  kpainter.drawRect(x-1, y-1, w+2, h+2);
  kpainter.stop();

  varea.setRect(x + px0,y + py0,w,h);

  bitBlt(canvas,0,0,m_buffer);
}

void KivioBirdEyePanel::handleMouseMove(TQPoint p)
{
  handlePress = true;

  TQRect r1 = TQRect(varea.x()-1, varea.y()-1, 3, varea.height()+2);
  if (r1.contains(p)) {
    canvas->setCursor(sizeHorCursor);
    apos = AlignLeft;
    return;
  }

  r1.moveBy(varea.width(),0);
  if (r1.contains(p)) {
    canvas->setCursor(sizeHorCursor);
    apos = AlignRight;
    return;
  }

  TQRect r2 = TQRect(varea.x()-1, varea.y()-1, varea.width()+2, 3);
  if (r2.contains(p)) {
    canvas->setCursor(sizeVerCursor);
    apos = AlignTop;
    return;
  }

  r2.moveBy(0, varea.height());
  if (r2.contains(p)) {
    canvas->setCursor(sizeVerCursor);
    apos = AlignBottom;
    return;
  }

  if (varea.contains(p)) {
    canvas->setCursor(sizeAllCursor);
    apos = AlignCenter;
    return;
  }

  canvas->unsetCursor();
  handlePress = false;
}

void KivioBirdEyePanel::handleMouseMoveAction(TQPoint p)
{
  if (!handlePress)
    return;

  p -= lastPos;

  if (apos == AlignCenter) {
    double zy = m_pView->zoomHandler()->zoomedResolutionY() / m_zoomHandler->zoomedResolutionY();
    double zx = m_pView->zoomHandler()->zoomedResolutionX() / m_zoomHandler->zoomedResolutionX();
    m_pCanvas->setUpdatesEnabled(false);
    m_pCanvas->scrollDx(-(int)(p.x()*zx));
    m_pCanvas->scrollDy(-(int)(p.y()*zy));
    m_pCanvas->setUpdatesEnabled(true);
    return;
  }

  double dx = m_zoomHandler->unzoomItX(p.x());
  double dy = m_zoomHandler->unzoomItY(p.y());

  KoRect vr = m_pCanvas->visibleArea();
  if (apos == AlignRight) {
    vr.setWidth(TQMAX(10.0, vr.width() + dx));
    m_pCanvas->setVisibleAreaByWidth(vr);
  }
  else if (apos == AlignLeft) {
    vr.setX(vr.x() + dx);
    vr.setWidth(TQMAX(10.0, vr.width() - dx));
    m_pCanvas->setVisibleAreaByWidth(vr);
  }
  else if (apos == AlignTop) {
    vr.setY(vr.y() + dy);
    vr.setHeight(TQMAX(10.0 ,vr.height() - dy));
    m_pCanvas->setVisibleAreaByHeight(vr);
  }
  else if (apos == AlignBottom) {
    vr.setHeight(TQMAX(10.0 ,vr.height() + dy));
    m_pCanvas->setVisibleAreaByHeight(vr);
  }
}

void KivioBirdEyePanel::handleMousePress(TQPoint p)
{
  if (handlePress)
    return;

  TQSize s1 = canvas->size();
  KoPageLayout pl = m_pView->activePage()->paperLayout();
  int pw = m_zoomHandler->zoomItX(pl.ptWidth);
  int ph = m_zoomHandler->zoomItY(pl.ptHeight);
  int px0 = (s1.width()-pw)/2;
  int py0 = (s1.height()-ph)/2;

  double x = m_zoomHandler->unzoomItX(p.x() - px0);
  double y = m_zoomHandler->unzoomItY(p.y() - py0);

  m_pCanvas->setViewCenterPoint(KoPoint(x,y));
}

#include "kivio_birdeye_panel.moc"