kmagzoomview.cpp 32.1 KB
Newer Older
Sarang Lakare's avatar
Sarang Lakare committed
1 2 3 4
/***************************************************************************
                          kmagview.cpp  -  description
                             -------------------
    begin                : Mon Feb 12 23:45:41 EST 2001
Sarang Lakare's avatar
Sarang Lakare committed
5 6
    copyright            : (C) 2001-2003 by Sarang Lakare
    email                : sarang#users.sourceforge.net
7
    copyright            : (C) 2003-2004 by Olaf Schmidt
8
    email                : ojschmidt@kde.org
9 10
    copyright            : (C) 2008 by Matthew Woehlke
    email                : mw_triad@users.sourceforge.net
Sebastian Sauer's avatar
Sebastian Sauer committed
11 12
    copyright              (C) 2010 Sebastian Sauer
    email                  sebsauer@kdab.com
Sarang Lakare's avatar
Sarang Lakare committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/


// application specific includes
#include "kmagzoomview.h"
27
#include "kmagzoomview.moc"
28
#include "colorsim.h"
Sarang Lakare's avatar
Sarang Lakare committed
29 30

// include files for Qt
Arto Hytönen's avatar
Arto Hytönen committed
31 32 33 34 35
#include <QtGui/QBitmap>
#include <QtGui/QCursor>
#include <QtCore/qglobal.h>
#include <QtGui/QPainter>
#include <QtGui/QWidget>
36
#include <QtGui/QScrollBar>
Arto Hytönen's avatar
Arto Hytönen committed
37 38 39 40 41 42 43 44
#include <QtGui/QPixmap>
#include <QtGui/QFocusEvent>
#include <QtGui/QHideEvent>
#include <QtGui/QKeyEvent>
#include <QtGui/QShowEvent>
#include <QtGui/QResizeEvent>
#include <QtGui/QMouseEvent>
#include <QtGui/QDesktopWidget>
45
#include <QtGui/QCursor>
46
#include <QtDBus/QDBusConnection>
Arto Hytönen's avatar
Arto Hytönen committed
47

Waldo Bastian's avatar
Waldo Bastian committed
48
// include files for KDE
49
#include <kapplication.h>
Waldo Bastian's avatar
Waldo Bastian committed
50 51 52
#include <kcursor.h>
#include <kdebug.h>
#include <klocale.h>
53 54
#ifdef QAccessibilityClient_FOUND
#include <qaccessibilityclient/accessibleobject.h>
Allen Winter's avatar
Allen Winter committed
55
#endif
Sarang Lakare's avatar
Sarang Lakare committed
56

57 58 59 60 61 62 63 64 65 66 67 68
// include bitmaps for cursors
static uchar left_ptr_bits[] = {
   0x00, 0x00, 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00,
   0xf8, 0x01, 0xf8, 0x03, 0xf8, 0x07, 0xf8, 0x00, 0xd8, 0x00, 0x88, 0x01,
   0x80, 0x01, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00};

static uchar left_ptrmsk_bits[] = {
   0x0c, 0x00, 0x1c, 0x00, 0x3c, 0x00, 0x7c, 0x00, 0xfc, 0x00, 0xfc, 0x01,
   0xfc, 0x03, 0xfc, 0x07, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x01, 0xdc, 0x03,
   0xcc, 0x03, 0x80, 0x07, 0x80, 0x07, 0x00, 0x03};

static uchar phand_bits[] = {
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
69 70 71 72 73 74 75 76 77 78 79
  0x00, 0x00, 0x00, 0x00,  0xfe, 0x01, 0x00, 0x00,  0x01, 0x02, 0x00, 0x00,
  0x7e, 0x04, 0x00, 0x00,  0x08, 0x08, 0x00, 0x00,  0x70, 0x08, 0x00, 0x00,
  0x08, 0x08, 0x00, 0x00,  0x70, 0x14, 0x00, 0x00,  0x08, 0x22, 0x00, 0x00,
  0x30, 0x41, 0x00, 0x00,  0xc0, 0x20, 0x00, 0x00,  0x40, 0x12, 0x00, 0x00,
  0x80, 0x08, 0x00, 0x00,  0x00, 0x05, 0x00, 0x00,  0x00, 0x02, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00 };
80
    static uchar phandm_bits[] = {
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
81 82 83 84 85 86 87 88 89 90 91
  0xfe, 0x01, 0x00, 0x00,  0xff, 0x03, 0x00, 0x00,  0xff, 0x07, 0x00, 0x00,
  0xff, 0x0f, 0x00, 0x00,  0xfe, 0x1f, 0x00, 0x00,  0xf8, 0x1f, 0x00, 0x00,
  0xfc, 0x1f, 0x00, 0x00,  0xf8, 0x3f, 0x00, 0x00,  0xfc, 0x7f, 0x00, 0x00,
  0xf8, 0xff, 0x00, 0x00,  0xf0, 0x7f, 0x00, 0x00,  0xe0, 0x3f, 0x00, 0x00,
  0xc0, 0x1f, 0x00, 0x00,  0x80, 0x0f, 0x00, 0x00,  0x00, 0x07, 0x00, 0x00,
  0x00, 0x02, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00 };
92 93


Sarang Lakare's avatar
Sarang Lakare committed
94
KMagZoomView::KMagZoomView(QWidget *parent, const char *name)
95
  : QAbstractScrollArea(parent),
96
    m_selRect(0, 0, 128, 128, this),
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
97 98 99 100
    m_grabTimer(parent),
    m_mouseViewTimer(parent),
    m_latestCursorPos(0,0),
    m_followMouse(false),
101
    m_followFocus(false),
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
102 103
    m_showMouse(1),
    m_zoom(1.0),
104
    m_rotation(0),
Sebastian Sauer's avatar
Sebastian Sauer committed
105
    m_colormode(0),
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
106
    m_fitToWindow(true)
Sarang Lakare's avatar
Sarang Lakare committed
107
{
108
  setObjectName( QLatin1String( name ));
109

110
  viewport()->setMouseTracking(true);
111
  viewport()->setAttribute(Qt::WA_NoSystemBackground, true);
112
  viewport()->setAutoFillBackground(false);
Laurent Montel's avatar
Laurent Montel committed
113
  viewport()->setFocusPolicy(Qt::StrongFocus);
114

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
115
  // init the zoom matrix
116
  setupMatrix();
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
117 118 119 120

  m_ctrlKeyPressed = false;
  m_shiftKeyPressed = false;
  m_refreshSwitch = true;
Sarang Lakare's avatar
Sarang Lakare committed
121
  m_refreshSwitchStateOnHide = m_refreshSwitch;
122

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
123 124 125 126
  // set the refresh rate
  setRefreshRate(10);

  // connect it to grabFrame()
Sarang Lakare's avatar
Sarang Lakare committed
127
  connect(&m_grabTimer, SIGNAL(timeout()), SLOT(grabFrame()));
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
128 129
  // start the grabTimer
  m_grabTimer.start(static_cast<int>(1000.0/m_fps));
Sarang Lakare's avatar
Sarang Lakare committed
130

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
131
  // connect it to updateMouseView()
132
  connect(&m_mouseViewTimer, SIGNAL(timeout()), SLOT(updateMouseView()));
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
133
  // start the grabTimer @ 25 frames per second!
134
  m_mouseViewTimer.start(40);
135

Laurent Montel's avatar
Laurent Montel committed
136
  this->setWhatsThis( i18n("This is the main window which shows the contents of the\
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
137
 selected region. The contents will be magnified according to the zoom level that is set."));
138

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
139
  // different ways to show the cursor.
140
  m_showMouseTypes << QLatin1String( "Hidden" ) << QLatin1String( "Box" ) << QLatin1String( "Arrow" ) << QLatin1String( "Actual" );
Sarang Lakare's avatar
Sarang Lakare committed
141

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
142 143
  if(m_fitToWindow)
    fitToWindow();
144

145
#ifdef QAccessibilityClient_FOUND
146
  //subscribe to focus events from registry
147
  m_registry.subscribeEventListeners(QAccessibleClient::Registry::Focus | QAccessibleClient::Registry::TextCaretMoved);
148
#endif
Sarang Lakare's avatar
Sarang Lakare committed
149 150 151 152 153 154
}

KMagZoomView::~KMagZoomView()
{
}

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
int KMagZoomView::contentsX() const
{
  return horizontalScrollBar()->value();
}

int KMagZoomView::contentsY() const
{
  return verticalScrollBar()->value();
}

int KMagZoomView::contentsWidth() const
{
  return horizontalScrollBar()->pageStep();
}

int KMagZoomView::contentsHeight() const
{
  return verticalScrollBar()->pageStep();
}

int KMagZoomView::visibleWidth() const
{
  return viewport()->width();
}

int KMagZoomView::visibleHeight() const
{
  return viewport()->height();
}

void KMagZoomView::setContentsPos(int x, int y)
{
  horizontalScrollBar()->setValue(x);
  verticalScrollBar()->setValue(y);
}

191 192 193 194 195 196 197
void KMagZoomView::setupMatrix()
{
  m_zoomMatrix.reset();
  m_zoomMatrix.scale(m_zoom, m_zoom);
  m_zoomMatrix.rotate(m_rotation);
}

Sarang Lakare's avatar
Sarang Lakare committed
198 199 200
/**
 * This function will set/reset mouse following of grab window.
 */
201
void KMagZoomView::followMouse(bool follow)
Sarang Lakare's avatar
Sarang Lakare committed
202
{
203 204
  m_followMouse = follow;
  m_mouseMode = Normal;
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
205
  if(follow) {
206 207
    setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
    setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
208
  } else {
209 210
    setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
    setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
211
  }
Sarang Lakare's avatar
Sarang Lakare committed
212 213
}

214
#ifdef QAccessibilityClient_FOUND
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

void KMagZoomView::followBoth(bool follow)
{
    m_followBoth = follow;
    if(follow){
        m_followMouse = true;
        m_followFocus = false;
        setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
        setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff);

        connect(&m_registry, SIGNAL(focusChanged(const QAccessibleClient::AccessibleObject &)),
                this, SLOT(focusChanged(const QAccessibleClient::AccessibleObject &)));
    } else {
        setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
        setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOn);

        disconnect(&m_registry, SIGNAL(focusChanged(const QAccessibleClient::AccessibleObject &)),
                this, SLOT(focusChanged(const QAccessibleClient::AccessibleObject &)));
    }
}

236 237 238 239 240 241 242 243 244 245
/**
 * This function will set/reset keyboard focus following of grab window.
 */
void KMagZoomView::followFocus(bool follow)
{
  if(m_followFocus == follow)
      return;
  m_followFocus = follow;
  m_mouseMode = Normal;
  if(follow) {
246 247
    setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
    setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
248

249 250
    connect(&m_registry,SIGNAL(focusChanged(const QAccessibleClient::AccessibleObject &)),
            this, SLOT(focusChanged(const QAccessibleClient::AccessibleObject &)));
251

252
  } else {
253 254
    setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
    setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
255

256 257
    disconnect(&m_registry, SIGNAL(focusChanged(const QAccessibleClient::AccessibleObject &)),
            this, SLOT(focusChanged(const QAccessibleClient::AccessibleObject &)));
258 259 260
  }
}

261
void KMagZoomView::focusChanged(const QAccessibleClient::AccessibleObject &object)
262
{
263
    m_oldFocus = object.focusPoint();
264 265 266 267 268
    if(m_followBoth && !m_selRect.contains(m_oldFocus)) {
        QCursor::setPos(m_oldFocus);
        m_followFocus = true;
        m_followMouse = false;
    }
269
}
270

271
#endif
272

Sarang Lakare's avatar
Sarang Lakare committed
273 274 275
/**
 * Called when the widget is hidden. Stop refresh when this happens.
 */
Sarang Lakare's avatar
Sarang Lakare committed
276
void KMagZoomView::hideEvent( QHideEvent* )
Sarang Lakare's avatar
Sarang Lakare committed
277 278 279 280
{
  // Save the state of the refresh switch.. the state will be restored
  // when showEvent is called
  m_refreshSwitchStateOnHide = m_refreshSwitch;
281

Sarang Lakare's avatar
Sarang Lakare committed
282 283 284
  // Check if refresh is ON
  if(m_refreshSwitch) {
    toggleRefresh();
285
  }
Sarang Lakare's avatar
Sarang Lakare committed
286 287 288 289 290 291
}


/**
 * Called when the widget is shown. Start refresh when this happens.
 */
Sarang Lakare's avatar
Sarang Lakare committed
292
void KMagZoomView::showEvent( QShowEvent* )
Sarang Lakare's avatar
Sarang Lakare committed
293 294 295 296 297 298 299 300
{
  // Check if refresh switch was ON when hide was called and if currently it is OFF
  if(m_refreshSwitchStateOnHide && !m_refreshSwitch) {
    // start the refresh in that case
    toggleRefresh();
  }
}

301 302 303
/**
 * Called when the widget is resized. Check if fitToWindow is active when this happens.
 */
304
void KMagZoomView::resizeEvent( QResizeEvent * e )
305
{
306 307 308
  horizontalScrollBar()->setRange(0, contentsWidth() - visibleWidth());
  verticalScrollBar()->setRange(0, contentsHeight() - visibleHeight());
  QAbstractScrollArea::resizeEvent(e);
309
  if(m_fitToWindow)
310
    fitToWindow();
311
}
Sarang Lakare's avatar
Sarang Lakare committed
312

Sarang Lakare's avatar
Sarang Lakare committed
313
/**
314
 * Called when the widget is to be repainted.
Sarang Lakare's avatar
Sarang Lakare committed
315 316 317
 *
 * @param p
 */
318
void KMagZoomView::paintEvent(QPaintEvent *e)
Sarang Lakare's avatar
Sarang Lakare committed
319
{
Christoph Feck's avatar
Christoph Feck committed
320
  if(m_coloredPixmap.isNull())
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
321
    return;
Sarang Lakare's avatar
Sarang Lakare committed
322

323 324 325 326 327 328
  QPainter p(viewport());
  int clipx = e->rect().x();
  int clipy = e->rect().x();
  int clipw = e->rect().width();
  int cliph = e->rect().height();

Laurent Montel's avatar
Laurent Montel committed
329
  // Paint empty areas Qt::black
330
  if (contentsX()+contentsWidth() < visibleWidth())
331
    p.fillRect (
332
        QRect (contentsX()+contentsWidth(), clipy, visibleWidth()-contentsX()-contentsWidth(), cliph)
333
        & e->rect(),
334 335
        Qt::black);
  if (contentsY()+contentsHeight() < visibleHeight())
336
    p.fillRect (
337
        QRect (clipx, contentsY()+contentsHeight(), clipw, visibleHeight()-contentsY()-contentsHeight())
338
        & e->rect(),
339
        Qt::black);
340

Christoph Feck's avatar
Christoph Feck committed
341 342 343 344
  p.translate(visibleWidth() / 2.0, visibleHeight() / 2.0);
  p.setMatrix(m_zoomMatrix, true);
  p.translate(-m_coloredPixmap.width() / 2.0, -m_coloredPixmap.height() / 2.0);
  p.drawPixmap(QPoint(clipx-contentsX(), clipy-contentsY()), m_coloredPixmap);
345
  p.end();
346

347 348
  if (m_showMouse)
    paintMouseCursor(viewport(), calcMousePos (m_refreshSwitch));
Sarang Lakare's avatar
Sarang Lakare committed
349 350
}

351 352 353 354
/**
 * Draws the mouse cursor according to the current selection of the type of
 * mouse cursor to draw.
 */
355
void KMagZoomView::paintMouseCursor(QPaintDevice *dev, const QPoint &mousePos)
356
{
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
357 358
  if(!dev)
    return;
359 360 361 362

  // painter for the zoom view
  QPainter pz(dev);

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
363 364 365
    // How to show the mouse :

    switch(m_showMouse) {
366
    case 1:
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
367 368
      // 1. Square around the pixel
      pz.setPen(Qt::white);
369
#ifdef __GNUC__
370
#warning "Port Qt4 pz.setRasterOp(Qt::XorROP);";
Matthew Woehlke's avatar
Matthew Woehlke committed
371
#endif
Laurent Montel's avatar
Laurent Montel committed
372
      //pz.setRasterOp(Qt::XorROP);
373
      pz.drawRect(mousePos.x()-1, mousePos.y()-1, (int)m_zoom+2, (int)m_zoom+2);
374 375
      break;

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
376 377 378 379
    case 2:
    {
      // 2. Arrow cursor
      pz.setPen(Qt::black);
380
      pz.setBackground(Qt::white);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
381

Stephan Kulow's avatar
Stephan Kulow committed
382 383
      QBitmap sCursor = QBitmap::fromData( QSize(16,  16),  left_ptr_bits);
      QBitmap mask = QBitmap::fromData( QSize(16,  16),  left_ptrmsk_bits);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
384
      sCursor.setMask(mask);
Laurent Montel's avatar
Laurent Montel committed
385
      sCursor = sCursor.transformed(m_zoomMatrix);
386

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
387
      // since hot spot is at 3,1
388 389 390 391 392 393 394 395
      if (m_rotation == 0)
        pz.drawPixmap(mousePos.x()-(int)(3.0*m_zoom), mousePos.y()-(int)m_zoom, sCursor);
      else if (m_rotation == 90)
        pz.drawPixmap(mousePos.x()-(int)(16.0*m_zoom), mousePos.y()-(int)(3.0*m_zoom), sCursor);
      else if (m_rotation == 180)
        pz.drawPixmap(mousePos.x()-(int)(13.0*m_zoom), mousePos.y()-(int)(16.0*m_zoom), sCursor);
      else if (m_rotation == 270)
        pz.drawPixmap(mousePos.x()-(int)m_zoom, mousePos.y()-(int)(13.0*m_zoom), sCursor);
396
    }
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
397 398 399
    break;

    case 3:
Matthew Woehlke's avatar
Matthew Woehlke committed
400
    {
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
401 402
      // 3. Actual cursor
      // Get the current cursor type
403
      QWidget *dummy  = KApplication::topLevelAt(QCursor::pos());
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
404 405
      if(!dummy)
        break;
406
      kDebug() << ">" << dummy->objectName() << ":" << dummy->cursor().shape() << "-";
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
407
      switch(this->cursor().shape())  {
Laurent Montel's avatar
Laurent Montel committed
408
			  case Qt::ArrowCursor :
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
409 410 411
         {
          // 2. Arrow cursor
          pz.setPen(Qt::black);
412
          pz.setBackground(Qt::white);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
413

Stephan Kulow's avatar
Stephan Kulow committed
414 415
          QBitmap sCursor = QBitmap::fromData( QSize(16,  16),  left_ptr_bits);
          QBitmap mask = QBitmap::fromData( QSize(16,  16),  left_ptrmsk_bits);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
416
          sCursor.setMask(mask);
Laurent Montel's avatar
Laurent Montel committed
417
          sCursor = sCursor.transformed(m_zoomMatrix);
418

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
419
          // since hot spot is at 3,1
420
          pz.drawPixmap(mousePos.x()-(int)(3.0*m_zoom), mousePos.y()-(int)m_zoom, sCursor);
421
        }
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
422 423
        break;
        default:
Stephan Kulow's avatar
Stephan Kulow committed
424 425
          QBitmap sCursor = QBitmap::fromData( QSize(32,  32),  phand_bits);
          QBitmap mask = QBitmap::fromData( QSize(32,  32),  phandm_bits);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
426
          sCursor.setMask(mask);
427

428
          pz.drawPixmap(mousePos.x(), mousePos.y(), sCursor);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
429 430
        break;
      } // switch(cursor)
431 432 433


    }
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
434
    break;
435

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
436 437 438 439
    default:
      // do not show anything
      break;
    } // switch(m_showMouse)
440 441 442
}


443 444 445 446 447 448 449 450 451
QPoint KMagZoomView::calcMousePos(bool updateMousePos)
{
  // get position of mouse wrt selRect
  if(updateMousePos) { // get a new position only if asked
    m_latestCursorPos = QCursor::pos();
    m_latestCursorPos -= QPoint(m_selRect.x(), m_selRect.y());
  }

  // get coordinates of the pixel w.r.t. the zoomed pixmap
452 453 454 455 456 457 458 459 460 461 462
  if (m_rotation == 90)
    return QPoint ((int)((float)(m_selRect.height()-m_latestCursorPos.y())*m_zoom),
                   (int)((float)m_latestCursorPos.x()*m_zoom));
  else if (m_rotation == 180)
    return QPoint ((int)((float)(m_selRect.width()-m_latestCursorPos.x())*m_zoom),
                   (int)((float)(m_selRect.height()-m_latestCursorPos.y())*m_zoom));
  else if (m_rotation == 270)
    return QPoint ((int)((float)m_latestCursorPos.y()*m_zoom),
                   (int)((float)(m_selRect.width()-m_latestCursorPos.x())*m_zoom));
  else
    return QPoint ((int)((float)m_latestCursorPos.x()*m_zoom),
463 464 465 466
                   (int)((float)m_latestCursorPos.y()*m_zoom));
}


Sarang Lakare's avatar
Sarang Lakare committed
467 468 469
// MOUSE ACTIONS

/**
470
 * Called when mouse is clicked inside the window.
Sarang Lakare's avatar
Sarang Lakare committed
471 472 473 474 475 476
 *
 * @param e
 */
void KMagZoomView::mousePressEvent(QMouseEvent *e)
{
  switch(e->button()) {
Laurent Montel's avatar
Laurent Montel committed
477
  case Qt::LeftButton :
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
478 479
    if(m_ctrlKeyPressed) {
      // check if currently in resize mode
480 481
      // don't do anything if fitToWindow is enabled
      if ((m_mouseMode != ResizeSelection) && !m_fitToWindow) {
482 483 484 485
        // set the mode to ResizeSelection
        m_mouseMode = ResizeSelection;

        // set mouse cursor to "resize all direction"
Laurent Montel's avatar
Laurent Montel committed
486
        setCursor(Qt::SizeAllCursor);
487 488 489 490 491

        // backup the old position
        m_oldMousePos.setX(e->globalX());
        m_oldMousePos.setY(e->globalY());

Sarang Lakare's avatar
Sarang Lakare committed
492
        // set the cursor position to the bottom-right of the selected region
493 494 495 496 497
        QCursor::setPos(m_selRect.bottomRight());

        // show the selection rectangle
        m_selRect.show();
      }
498 499 500 501
      else {
        // ignore this button press.. so it goes to the parent
        e->ignore();
      }
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
502
    } else if(m_shiftKeyPressed) {
Sarang Lakare's avatar
Sarang Lakare committed
503
      // check if currently in move mode
504 505
      // don't do anything if follow mouse is enabled
      if ((m_mouseMode != MoveSelection) && !m_followMouse) {
Sarang Lakare's avatar
Sarang Lakare committed
506 507 508
        m_mouseMode = MoveSelection;

        // set mouse cursor to cross hair
Laurent Montel's avatar
Laurent Montel committed
509
        setCursor(Qt::CrossCursor);
Sarang Lakare's avatar
Sarang Lakare committed
510 511 512 513 514 515 516 517 518 519 520

        // backup the old position
        m_oldMousePos.setX(e->globalX());
        m_oldMousePos.setY(e->globalY());

        // set the cursor position to the center of the selected region
        QCursor::setPos(m_selRect.center());

        // show the selected rectangle
        m_selRect.show();
      }
521 522 523 524
      else {
        // ignore this button press.. so it goes to the parent
        e->ignore();
      }
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
525
    } else {
526
      // check if currently in move mode
527 528
      // don't do anything if follow mouse is enabled
      if ((m_mouseMode != GrabSelection) && !m_followMouse) {
529
        m_mouseMode = GrabSelection;
Sarang Lakare's avatar
Sarang Lakare committed
530

531
        // set mouse cursor to hand
532
        setCursor(Qt::PointingHandCursor);
Sarang Lakare's avatar
Sarang Lakare committed
533

534
        // store the old position
Sarang Lakare's avatar
Sarang Lakare committed
535 536 537
        m_oldMousePos.setX(e->globalX());
        m_oldMousePos.setY(e->globalY());

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
538
        m_oldCenter = m_selRect.center();
Sarang Lakare's avatar
Sarang Lakare committed
539

540
        // show the selected rectangle
Sarang Lakare's avatar
Sarang Lakare committed
541 542
        m_selRect.show();
      }
543 544 545 546
      else {
        // ignore this button press.. so it goes to the parent
        e->ignore();
      }
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
547
    }
Sarang Lakare's avatar
Sarang Lakare committed
548 549
    break;

Laurent Montel's avatar
Laurent Montel committed
550
  case Qt::MidButton :
551 552 553
    // check if currently in move mode
    // don't do anything if follow mouse is enabled
    if ((m_mouseMode != MoveSelection) && !m_followMouse) {
Sarang Lakare's avatar
Sarang Lakare committed
554 555 556
      m_mouseMode = MoveSelection;

      // set mouse cursor to cross hair
Laurent Montel's avatar
Laurent Montel committed
557
      setCursor(Qt::CrossCursor);
Sarang Lakare's avatar
Sarang Lakare committed
558 559 560 561 562 563 564 565 566 567

      // backup the old position
      m_oldMousePos.setX(e->globalX());
      m_oldMousePos.setY(e->globalY());

      // set the cursor position to the center of the selected region
      QCursor::setPos(m_selRect.center());

      // show the selected rectangle
      m_selRect.show();
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
568
    }
569 570 571 572
    else {
      // ignore this button press.. so it goes to the parent
      e->ignore();
    }
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
573
    break;
Sarang Lakare's avatar
Sarang Lakare committed
574 575
  // do nothing
  default:
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
576 577 578
    // ignore this button press.. so it goes to the parent
    e->ignore();
    break;
Sarang Lakare's avatar
Sarang Lakare committed
579 580 581 582 583 584 585 586 587 588 589 590
  }
}


/**
 * Called when a mouse button is released
 *
 * @param e
 */
void KMagZoomView::mouseReleaseEvent(QMouseEvent *e)
{
  switch(e->button()) {
Laurent Montel's avatar
Laurent Montel committed
591 592
  case Qt::LeftButton :
  case Qt::MidButton :
593 594 595 596 597 598 599 600
    // check if currently in move mode
    if(m_mouseMode == MoveSelection) {
      // hide the selection window
      m_selRect.hide();
      // set the mouse mode to normal
      m_mouseMode = Normal;

      // restore the cursor shape
Laurent Montel's avatar
Laurent Montel committed
601
      setCursor(Qt::ArrowCursor);
602 603 604 605 606 607 608 609 610 611

      // restore the cursor position
      QCursor::setPos(m_oldMousePos);
    } else if(m_mouseMode == ResizeSelection) {
      // hide the selection window
      m_selRect.hide();
      // set the mouse mode to normal
      m_mouseMode = Normal;

      // restore the cursor shape
Laurent Montel's avatar
Laurent Montel committed
612
      setCursor(Qt::ArrowCursor);
613 614 615 616 617 618 619 620 621 622 623

      // restore the cursor position
      QCursor::setPos(m_oldMousePos);
    } else if(m_mouseMode == GrabSelection) {
      // hide the selection window
      m_selRect.hide();

      // set the mouse mode to normal
      m_mouseMode = Normal;

      // restore the cursor shape
Laurent Montel's avatar
Laurent Montel committed
624
      setCursor(Qt::ArrowCursor);
Matthew Woehlke's avatar
Matthew Woehlke committed
625
    }
Sarang Lakare's avatar
Sarang Lakare committed
626 627
    break;

Laurent Montel's avatar
Laurent Montel committed
628
  case Qt::RightButton :
Sarang Lakare's avatar
Sarang Lakare committed
629
    break;
Laurent Montel's avatar
Laurent Montel committed
630
  case Qt::NoButton :
Sarang Lakare's avatar
Sarang Lakare committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
    break;

  // do nothing
  default:
    ;
  }
}


/**
 * Called when mouse is moved inside the window
 *
 * @param e
 */
void KMagZoomView::mouseMoveEvent(QMouseEvent *e)
{
647
  if(m_mouseMode == ResizeSelection) {
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
648
    // In resize selection mode
Sarang Lakare's avatar
Sarang Lakare committed
649 650 651 652
    // set the current mouse position as the bottom, right corner
    m_selRect.setRight(e->globalX());
    m_selRect.setBottom(e->globalY());
    m_selRect.update();
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
    grabFrame();
  } else if(m_mouseMode == MoveSelection) {
     QPoint newCenter;

    // set new center to be the current mouse position
    newCenter = e->globalPos();

    // make sure the mouse position is not taking the grab window outside
    // the display
    if(newCenter.x() < m_selRect.width()/2) {
      // set X to the minimum possible X
      newCenter.setX(m_selRect.width()/2);
    } else if(newCenter.x() >=  QApplication::desktop()->width()-m_selRect.width()/2) {
      // set X to the maximum possible X
      newCenter.setX(QApplication::desktop()->width()-m_selRect.width()/2-1);
    }

    if(newCenter.y() < m_selRect.height()/2) {
      // set Y to the minimum possible Y
      newCenter.setY(m_selRect.height()/2);
    } else if(newCenter.y() >=  QApplication::desktop()->height()-m_selRect.height()/2) {
      // set Y to the maximum possible Y
      newCenter.setY(QApplication::desktop()->height()-m_selRect.height()/2-1);
    }
    // move to the new center
    m_selRect.moveCenter(newCenter);
Sarang Lakare's avatar
Sarang Lakare committed
679 680
    // update the grab rectangle display
    m_selRect.update();
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
    grabFrame();
  } else if(m_mouseMode == GrabSelection) {
     QPoint newPos;

    // get new position
    newPos = e->globalPos();

    QPoint delta = (newPos - m_oldMousePos)/m_zoom;
    QPoint newCenter = m_oldCenter-delta;

    // make sure the mouse position is not taking the grab window outside
    // the display
    if(newCenter.x() < m_selRect.width()/2) {
      // set X to the minimum possible X
      newCenter.setX(m_selRect.width()/2);
    } else if(newCenter.x() >=  QApplication::desktop()->width()-m_selRect.width()/2) {
      // set X to the maximum possible X
      newCenter.setX(QApplication::desktop()->width()-m_selRect.width()/2-1);
    }

    if(newCenter.y() < m_selRect.height()/2) {
      // set Y to the minimum possible Y
      newCenter.setY(m_selRect.height()/2);
    } else if(newCenter.y() >=  QApplication::desktop()->height()-m_selRect.height()/2) {
      // set Y to the maximum possible Y
      newCenter.setY(QApplication::desktop()->height()-m_selRect.height()/2-1);
    }
708

Matthew Woehlke's avatar
Matthew Woehlke committed
709
    // move to the new center
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
710
    m_selRect.moveCenter(newCenter);
711 712
    // update the grab rectangle display
    m_selRect.update();
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
713 714
    grabFrame();
  }
Sarang Lakare's avatar
Sarang Lakare committed
715 716 717 718
}

void KMagZoomView::keyPressEvent(QKeyEvent *e)
{
719
  int offset = 16;
720
  if (e->modifiers() & Qt::ShiftModifier)
721 722
    offset = 1;

Laurent Montel's avatar
Laurent Montel committed
723
  if (e->key() == Qt::Key_Control)
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
724
    m_ctrlKeyPressed = true;
Laurent Montel's avatar
Laurent Montel committed
725
  else if (e->key() == Qt::Key_Shift)
Matthew Woehlke's avatar
Matthew Woehlke committed
726
    m_shiftKeyPressed = true;
Laurent Montel's avatar
Laurent Montel committed
727
  else if (e->key() == Qt::Key_Left)
728
  {
729
    if (e->modifiers() & Qt::ControlModifier)
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
    {
      if (offset >= m_selRect.width())
        m_selRect.setWidth (1);
      else
        m_selRect.setWidth (m_selRect.width()-offset);
  }
    else if (contentsX() > 0)
    {
      offset = (int)(offset*m_zoom);
      if (contentsX() > offset)
        setContentsPos (contentsX()-offset, contentsY());
      else
        setContentsPos (0, contentsY());
    }
    else if (m_followMouse == false)
    {
      if (offset > m_selRect.x())
        m_selRect.setX (0);
      else
Laurent Montel's avatar
Laurent Montel committed
749
        m_selRect.translate (-offset,0);
750 751 752
    }
    m_selRect.update();
  }
Laurent Montel's avatar
Laurent Montel committed
753
  else if (e->key() == Qt::Key_Right)
754
  {
755
    if (e->modifiers() & Qt::ControlModifier)
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
    {
      if (m_selRect.right()+offset >= QApplication::desktop()->width())
        m_selRect.setRight (QApplication::desktop()->width()-1);
      else
        m_selRect.setRight (m_selRect.right()+offset);
    }
    else if (contentsX() < contentsWidth()-visibleWidth())
    {
      offset = (int)(offset*m_zoom);
      if (contentsX()+offset < contentsWidth()-visibleWidth())
        setContentsPos (contentsX()+offset, contentsY());
      else
        setContentsPos (contentsWidth()-visibleWidth(), contentsY());
    }
    else if (m_followMouse == false)
    {
      if (m_selRect.right()+offset >= QApplication::desktop()->width())
        m_selRect.moveTopRight (QPoint (QApplication::desktop()->width()-1, m_selRect.top()));
      else
Laurent Montel's avatar
Laurent Montel committed
775
        m_selRect.translate (offset,0);
776 777 778
    }
    m_selRect.update();
  }
Laurent Montel's avatar
Laurent Montel committed
779
  else if (e->key() == Qt::Key_Up)
780
  {
781
    if (e->modifiers() & Qt::ControlModifier)
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
    {
      if (offset >= m_selRect.height())
        m_selRect.setHeight (1);
      else
        m_selRect.setHeight (m_selRect.height()-offset);
    }
    else if (contentsY() > 0)
    {
      offset = (int)(offset*m_zoom);
      if (contentsY() > offset)
        setContentsPos (contentsX(), contentsY()-offset);
      else
        setContentsPos (contentsX(), 0);
    }
    else if (m_followMouse == false)
    {
      if (offset > m_selRect.y())
        m_selRect.setY (0);
      else
Laurent Montel's avatar
Laurent Montel committed
801
        m_selRect.translate (0, -offset);
802 803
    }
    m_selRect.update();
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
804
  }
Laurent Montel's avatar
Laurent Montel committed
805
  else if (e->key() == Qt::Key_Down)
806
  {
807
    if (e->modifiers() & Qt::ControlModifier)
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
    {
      if (m_selRect.bottom()+offset >= QApplication::desktop()->height())
        m_selRect.setBottom (QApplication::desktop()->height()-1);
      else
        m_selRect.setBottom (m_selRect.bottom()+offset);
    }
    else if (contentsY() < contentsHeight()-visibleHeight())
    {
      offset = (int)(offset*m_zoom);
      if (contentsY()+offset < contentsHeight()-visibleHeight())
        setContentsPos (contentsX(), contentsY()+offset);
      else
        setContentsPos (contentsX(), contentsHeight()-visibleHeight());
    }
    else if (m_followMouse == false)
    {
      if (m_selRect.bottom()+offset >= QApplication::desktop()->height())
        m_selRect.moveBottomLeft (QPoint (m_selRect.left(), QApplication::desktop()->height()-1));
      else
Laurent Montel's avatar
Laurent Montel committed
827
        m_selRect.translate (0, offset);
828 829 830 831 832
    }
    m_selRect.update();
  }
  else
    e->ignore();
Sarang Lakare's avatar
Sarang Lakare committed
833 834 835 836
}

void KMagZoomView::keyReleaseEvent(QKeyEvent *e)
{
Laurent Montel's avatar
Laurent Montel committed
837
  if (e->key() == Qt::Key_Control)
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
838
    m_ctrlKeyPressed = false;
Laurent Montel's avatar
Laurent Montel committed
839
  else if (e->key() == Qt::Key_Shift)
840 841
    m_shiftKeyPressed = false;
  else
Sarang Lakare's avatar
Sarang Lakare committed
842 843 844 845 846
    e->ignore();
}

void KMagZoomView::focusOutEvent(QFocusEvent *e)
{
847
  if(e->lostFocus() == true) {
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
848 849 850
    m_ctrlKeyPressed = false;
    m_shiftKeyPressed = false;
  }
Sarang Lakare's avatar
Sarang Lakare committed
851 852 853 854
}

// SLOTS

Sarang Lakare's avatar
Sarang Lakare committed
855 856 857 858 859 860
/**
 * This will fit the zoom view to the view window, thus using the maximum
 * possible space in the window.
 */
void KMagZoomView::fitToWindow()
{
861
  unsigned int newWidth, newHeight;
Matthew Woehlke's avatar
Matthew Woehlke committed
862

863 864 865
  // this is a temporary solution, cast, maybe newWidth and newHeight should be float
  if ((m_rotation == 90) || (m_rotation == 270))
  {
866 867
    newWidth = static_cast<unsigned int>((visibleHeight() + m_zoom - 1) / m_zoom);
    newHeight = static_cast<unsigned int>((visibleWidth() + m_zoom - 1) / m_zoom);
868
  } else {
869 870
    newWidth = static_cast<unsigned int>((visibleWidth() + m_zoom - 1) / m_zoom);
    newHeight = static_cast<unsigned int>((visibleHeight() + m_zoom - 1) / m_zoom);
871
  }
Sarang Lakare's avatar
Sarang Lakare committed
872

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
873
  QPoint currCenter = m_selRect.center();
Sarang Lakare's avatar
Sarang Lakare committed
874

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
875 876
  m_selRect.setWidth(newWidth);
  m_selRect.setHeight(newHeight);
Sarang Lakare's avatar
Sarang Lakare committed
877

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
878 879 880 881 882 883 884 885
   // make sure the selection window does not go outside of the display
   if(currCenter.x() < m_selRect.width()/2) {
     // set X to the minimum possible X
     currCenter.setX(m_selRect.width()/2);
   } else if(currCenter.x() >=  QApplication::desktop()->width()-m_selRect.width()/2) {
     // set X to the maximum possible X
     currCenter.setX(QApplication::desktop()->width()-m_selRect.width()/2-1);
   }
Sarang Lakare's avatar
Sarang Lakare committed
886 887

   if(currCenter.y() < m_selRect.height()/2) {
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
888 889 890 891 892 893 894 895
     // set Y to the minimum possible Y
     currCenter.setY(m_selRect.height()/2);
   } else if(currCenter.y() >=  QApplication::desktop()->height()-m_selRect.height()/2) {
     // set Y to the maximum possible Y
     currCenter.setY(QApplication::desktop()->height()-m_selRect.height()/2-1);
   }

  m_selRect.moveCenter(currCenter);
Sarang Lakare's avatar
Sarang Lakare committed
896 897
  // update the grab rectangle display
  m_selRect.update();
898
//  m_fitToWindow = true;
899
  viewport()->update();
Sarang Lakare's avatar
Sarang Lakare committed
900 901
}

902 903 904 905 906 907 908 909
void KMagZoomView::setFitToWindow(bool fit)
{
  m_fitToWindow = fit;
  if (fit)
    fitToWindow();
}


910 911
/**
 * Grabs frame from X
Sarang Lakare's avatar
Sarang Lakare committed
912 913 914
 */
void KMagZoomView::grabFrame()
{
915 916 917 918
  // check refresh status
  if (!m_refreshSwitch)
     return;

Sebastian Sauer's avatar
Sebastian Sauer committed
919
  // check if follow-mouse or follow-focus are enabled
920
  if((m_followMouse || m_followFocus) && (m_mouseMode != ResizeSelection)) {
Sebastian Sauer's avatar
Sebastian Sauer committed
921 922 923 924 925 926
    // center-position of the grab-area
    QPoint newCenter;

    if(m_followMouse) {
        // set new center to be the current mouse position
        newCenter = QCursor::pos();
927
#ifdef QAccessibilityClient_FOUND
Sebastian Sauer's avatar
Sebastian Sauer committed
928 929
    } else if(m_followFocus) {
        // set the new center to the current keyboard cursor position
Sebastian Sauer's avatar
Sebastian Sauer committed
930
        newCenter = m_oldFocus;
931 932 933 934
        if(m_followBoth) {
            m_followFocus=false;
            m_followMouse=true;
        }
935
#endif
Sebastian Sauer's avatar
Sebastian Sauer committed
936
    }
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953

    // make sure the mouse position is not taking the grab window outside
    // the display
    if(newCenter.x() < m_selRect.width()/2) {
      // set X to the minimum possible X
      newCenter.setX(m_selRect.width()/2);
    } else if(newCenter.x() >=  QApplication::desktop()->width()-m_selRect.width()/2) {
      // set X to the maximum possible X
      newCenter.setX(QApplication::desktop()->width()-m_selRect.width()/2-1);
    }

    if(newCenter.y() < m_selRect.height()/2) {
      // set Y to the minimum possible Y
      newCenter.setY(m_selRect.height()/2);
    } else if(newCenter.y() >=  QApplication::desktop()->height()-m_selRect.height()/2) {
      // set Y to the maximum possible Y
      newCenter.setY(QApplication::desktop()->height()-m_selRect.height()/2-1);
954
    }
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
955 956 957
    // move to the new center
    m_selRect.moveCenter(newCenter);