2
* This file is a part of Qtpfsgui package.
3
* ----------------------------------------------------------------------
4
* Copyright (C) 2007 Giuseppe Rota
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* ----------------------------------------------------------------------
21
* @author Giuseppe Rota <grota@users.sourceforge.net>
24
#include "smart_scroll_area.h"
26
SmartScrollArea::SmartScrollArea( QWidget *parent, QLabel *imagelabel ) : QScrollArea(parent), imageLabel(imagelabel), scaleFactor(1.0), fittingwin(false) {
27
setBackgroundRole(QPalette::Light);
28
imageLabel->setBackgroundRole(QPalette::Base);
29
//the label ignores the pixmap's size
30
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
31
//the label will scale the image to fill all available space (to its -the label's- size)
32
imageLabel->setScaledContents(true);
33
//false (the default), the scroll area honors the size of its widget.
34
//Regardless of this property, you can programmatically resize the widget using widget()->resize()
35
//indeed, when I zoom in/out I call imageLabel->resize(...)
36
setWidgetResizable(false);
37
setWidget(imageLabel);
40
void SmartScrollArea::zoomIn() {
43
void SmartScrollArea::zoomOut() {
46
void SmartScrollArea::fitToWindow(bool checked) {
51
// restore to the previous zoom factor
54
void SmartScrollArea::normalSize() {
55
//use the image size for the label
56
imageLabel->adjustSize();
59
void SmartScrollArea::scaleLabelToFit() {
60
int sa_width=this->size().width();
61
int sa_height=this->size().height();
62
float imageratio=float(imageLabel->pixmap()->size().width())/float(imageLabel->pixmap()->size().height());
64
if (sa_width<imageratio*sa_height) {
65
factor=float(sa_width)/float(imageLabel->pixmap()->size().width());
67
factor=float(sa_height)/float(imageLabel->pixmap()->size().height());
69
imageLabel->resize(factor * 0.99 * imageLabel->pixmap()->size());
71
void SmartScrollArea::scaleImage(double factor) {
72
scaleFactor *= factor;
73
imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
74
adjustScrollBar(horizontalScrollBar(), factor);
75
adjustScrollBar(verticalScrollBar(), factor);
77
void SmartScrollArea::adjustScrollBar(QScrollBar *scrollBar, double factor) {
78
scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2)));
80
void SmartScrollArea::resizeEvent ( QResizeEvent * e) {
84
//this seems to be a bug in qt4
85
imageLabel->resize(imageLabel->size()-QSize(0,1));
86
imageLabel->resize(imageLabel->size()+QSize(0,1));
88
// QScrollBar *h=horizontalScrollBar();
89
// QScrollBar *v=verticalScrollBar();
90
// int fixwidth=imageLabel->size().width()+v->size().width()-this->size().width();
92
// h->setMaximum(qMax(0,fixwidth));
95
// int fixheight=imageLabel->size().height()-this->size().height();
97
// v->setMaximum(qMax(0,fixheight));
103
void SmartScrollArea::mouseMoveEvent(QMouseEvent *e) {
104
if (e->buttons()==Qt::MidButton) {
105
QPoint diff = e->pos() - mousePos;
106
if (e->modifiers()==Qt::ShiftModifier)
109
verticalScrollBar()->setValue(verticalScrollBar()->value() +
111
horizontalScrollBar()->setValue(horizontalScrollBar()->value() +