~ubuntu-branches/ubuntu/intrepid/digikam/intrepid

« back to all changes in this revision

Viewing changes to digikam/libs/widgets/common/previewwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2008-07-17 20:25:39 UTC
  • mfrom: (1.2.15 upstream) (3.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080717202539-6n7dtirbkoo7qvhd
Tags: 2:0.9.4-1
* New upstream release
  - digiKam 0.9.4 Release Plan (KDE3) ~ 13 July 08 (Closes: #490144)
* DEB_CONFIGURE_EXTRA_FLAGS := --without-included-sqlite3
* Debhelper compatibility level V7
* Install pixmaps in debian/*.install
* Add debian/digikam.lintian-overrides

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 * Date        : 2006-06-13
7
7
 * Description : a widget to display an image preview
8
8
 *
9
 
 * Copyright (C) 2006-2007 Gilles Caulier <caulier dot gilles at gmail dot com>
 
9
 * Copyright (C) 2006-2008 Gilles Caulier <caulier dot gilles at gmail dot com>
10
10
 *
11
11
 * This program is free software; you can redistribute it
12
12
 * and/or modify it under the terms of the GNU General
80
80
    int             midButtonY;
81
81
    int             zoomWidth;
82
82
    int             zoomHeight;
83
 
    
 
83
 
84
84
    double          zoom;
85
85
    double          minZoom;
86
86
    double          maxZoom;
89
89
    QPoint          centerZoomPoint;
90
90
 
91
91
    QRect           pixmapRect;
92
 
    
 
92
 
93
93
    QCache<QPixmap> tileCache;
94
94
 
95
95
    QPixmap*        tileTmpPix;
103
103
    d = new PreviewWidgetPriv;
104
104
    d->bgColor.setRgb(0, 0, 0);
105
105
    m_movingInProgress = false;
106
 
    
 
106
 
107
107
    viewport()->setBackgroundMode(Qt::NoBackground);
108
108
    viewport()->setMouseTracking(false);
109
109
 
127
127
{
128
128
    if (d->bgColor == color)
129
129
        return;
130
 
    
 
130
 
131
131
    d->bgColor = color;
132
132
    viewport()->update();
133
133
}
188
188
    return (d->zoom <= d->minZoom);
189
189
}
190
190
 
 
191
double PreviewWidget::snapZoom(double zoom)
 
192
{
 
193
    // If the zoom value gets changed from d->zoom to zoom
 
194
    // across 50%, 100% or fit-to-window, then return the
 
195
    // the corresponding special value. Otherwise zoom is returned unchanged.
 
196
    double fit = calcAutoZoomFactor(ZoomInOrOut);
 
197
    QValueList<double> snapValues;
 
198
    snapValues.append(0.5);
 
199
    snapValues.append(1.0);
 
200
    snapValues.append(fit);
 
201
    qHeapSort(snapValues);
 
202
    QValueList<double>::const_iterator it;
 
203
 
 
204
    if (d->zoom < zoom) 
 
205
    {
 
206
        for(it = snapValues.constBegin(); it != snapValues.constEnd(); ++it)
 
207
        {
 
208
            double z = *it;
 
209
            if ((d->zoom < z) && (zoom > z))
 
210
            {
 
211
                 zoom = z;
 
212
                 break;
 
213
            }
 
214
        }
 
215
    } 
 
216
    else
 
217
    {
 
218
        for(it = snapValues.constEnd(); it != snapValues.constBegin(); --it)
 
219
        {
 
220
            double z = *it;
 
221
            if ((d->zoom > z) && (zoom < z))
 
222
            {
 
223
                 zoom = z;
 
224
                 break;
 
225
            }
 
226
        }
 
227
    }
 
228
 
 
229
    return zoom;
 
230
}
 
231
 
191
232
void PreviewWidget::slotIncreaseZoom()
192
233
{
193
234
    double zoom = d->zoom * d->zoomMultiplier;
194
 
    setZoomFactor(zoom > zoomMax() ? zoomMax() : zoom);
 
235
    zoom = snapZoom(zoom > zoomMax() ? zoomMax() : zoom);
 
236
    setZoomFactor(zoom);
195
237
}
196
238
 
197
239
void PreviewWidget::slotDecreaseZoom()
198
240
{
199
241
    double zoom = d->zoom / d->zoomMultiplier;
200
 
    setZoomFactor(zoom < zoomMin() ? zoomMin() : zoom);
 
242
    zoom = snapZoom(zoom < zoomMin() ? zoomMin() : zoom);
 
243
    setZoomFactor(zoom);
 
244
}
 
245
 
 
246
void PreviewWidget::setZoomFactorSnapped(double zoom)
 
247
{
 
248
    double fit = calcAutoZoomFactor(ZoomInOrOut);
 
249
    if (fabs(zoom-1.0) < 0.05) 
 
250
    {
 
251
        zoom = 1.0;
 
252
    }
 
253
    if (fabs(zoom-0.5) < 0.05) 
 
254
    {
 
255
        zoom = 0.5;
 
256
    }
 
257
    if (fabs(zoom-fit) < 0.05) 
 
258
    {
 
259
        zoom = fit;
 
260
    }
 
261
 
 
262
    setZoomFactor(zoom);
201
263
}
202
264
 
203
265
void PreviewWidget::setZoomFactor(double zoom)
204
266
{
 
267
    setZoomFactor(zoom, false);
 
268
}
 
269
 
 
270
void PreviewWidget::setZoomFactor(double zoom, bool centerView)
 
271
{
205
272
    // Zoom using center of canvas and given zoom factor.
206
273
 
207
274
    double oldZoom = d->zoom;
208
275
    double cpx, cpy;
 
276
 
209
277
    if (d->centerZoomPoint.isNull())
210
278
    {
211
279
        // center on current center
244
312
        cpx = ( cpx * d->tileSize ) / floor(d->tileSize / d->zoom);
245
313
        cpy = ( cpy * d->tileSize ) / floor(d->tileSize / d->zoom);
246
314
 
 
315
        if (centerView)
 
316
        {
 
317
            cpx = d->zoomWidth/2.0;
 
318
            cpy = d->zoomHeight/2.0;
 
319
        }
 
320
 
247
321
        center((int)cpx, (int)(cpy));
248
322
    }
249
323
    else
282
356
    d->autoZoom = !d->autoZoom;
283
357
 
284
358
    if (d->autoZoom)
 
359
    {
285
360
        updateAutoZoom();
 
361
    }
286
362
    else
287
363
    {
288
364
        d->zoom = 1.0;
293
369
    viewport()->update();
294
370
}
295
371
 
 
372
void PreviewWidget::toggleFitToWindowOr100()
 
373
{
 
374
    // If the current zoom is 100%, then fit to window.
 
375
    if (d->zoom == 1.0) 
 
376
    {
 
377
        fitToWindow();
 
378
    }
 
379
    else
 
380
    {
 
381
        setZoomFactor(1.0, true);
 
382
    }
 
383
}
 
384
 
296
385
void PreviewWidget::updateAutoZoom(AutoZoomMode mode)
297
386
{
298
387
    d->zoom       = calcAutoZoomFactor(mode);
325
414
void PreviewWidget::updateContentsSize()
326
415
{
327
416
    viewport()->setUpdatesEnabled(false);
328
 
    
 
417
 
329
418
    if (visibleWidth() > d->zoomWidth || visibleHeight() > d->zoomHeight)
330
419
    {
331
420
        // Center the image
530
619
    }
531
620
    else if (e->state() & Qt::ControlButton)
532
621
    {
 
622
        // When zooming with the mouse-wheel, the image center is kept fixed.
533
623
        d->centerZoomPoint = e->pos();
534
624
        if (e->delta() < 0 && !minZoom())
535
625
            slotDecreaseZoom();