~ubuntu-branches/ubuntu/natty/qtpfsgui/natty

« back to all changes in this revision

Viewing changes to src/smart_scroll_area.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-01-06 04:39:36 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080106043936-a9u9g7yih3w16ru5
Tags: 1.9.0-1
* New upstream release.
* Replace “COPYING” with “LICENSE” in the NOT_NEEDED variable of
  debian/rules, following upstream's renaming.
* Update debian/links accordingly.
* Delete the matching TODO item since there's no longer needed to have a
  patched (with HTML tags) license file to get a correct display in the
  “License agreement” tab.
* Update the gcc4.3 patch (drop the hunk touching src/Libpfs/pfs.cpp):
   - 20_gcc4.3_includes.
* Add a link from /usr/share/qtpfsgui/html to the HTML documentation
  under /usr/share/doc/qtpfsgui/html since the former is used at runtime
  to display the manual.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * This file is a part of Qtpfsgui package.
3
 
 * ---------------------------------------------------------------------- 
4
 
 * Copyright (C) 2007 Giuseppe Rota
5
 
 * 
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.
10
 
 *
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.
15
 
 *
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
 
 * ---------------------------------------------------------------------- 
20
 
 *
21
 
 * @author Giuseppe Rota <grota@users.sourceforge.net>
22
 
 */
23
 
 
24
 
#include "smart_scroll_area.h"
25
 
#include <QPixmap>
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);
38
 
}
39
 
 
40
 
void SmartScrollArea::zoomIn() {
41
 
        scaleImage(1.25);
42
 
}
43
 
void SmartScrollArea::zoomOut() {
44
 
        scaleImage(0.8);
45
 
}
46
 
void SmartScrollArea::fitToWindow(bool checked) {
47
 
        fittingwin=checked;
48
 
        if (checked)
49
 
                scaleLabelToFit();
50
 
        else
51
 
                // restore to the previous zoom factor
52
 
                scaleImage(1);
53
 
}
54
 
void SmartScrollArea::normalSize() {
55
 
        //use the image size for the label
56
 
        imageLabel->adjustSize();
57
 
        scaleFactor = 1.0;
58
 
}
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());
63
 
        float factor=1;
64
 
        if (sa_width<imageratio*sa_height) {
65
 
                factor=float(sa_width)/float(imageLabel->pixmap()->size().width());
66
 
        } else {
67
 
                factor=float(sa_height)/float(imageLabel->pixmap()->size().height());
68
 
        }
69
 
        imageLabel->resize(factor * 0.99 * imageLabel->pixmap()->size());
70
 
}
71
 
void SmartScrollArea::scaleImage(double factor) {
72
 
        scaleFactor *= factor;
73
 
        imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
74
 
        adjustScrollBar(horizontalScrollBar(), factor);
75
 
        adjustScrollBar(verticalScrollBar(), factor);
76
 
}
77
 
void SmartScrollArea::adjustScrollBar(QScrollBar *scrollBar, double factor) {
78
 
        scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2)));
79
 
}
80
 
void SmartScrollArea::resizeEvent ( QResizeEvent * e) {
81
 
        if (fittingwin) {
82
 
                scaleLabelToFit();
83
 
        } else {
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));
87
 
                
88
 
//              QScrollBar *h=horizontalScrollBar();
89
 
//              QScrollBar *v=verticalScrollBar();
90
 
//              int fixwidth=imageLabel->size().width()+v->size().width()-this->size().width();
91
 
//              if (fixwidth>0)
92
 
//                      h->setMaximum(qMax(0,fixwidth));
93
 
//              else
94
 
//                      h->setMaximum(0);
95
 
//              int fixheight=imageLabel->size().height()-this->size().height();
96
 
//              if (fixheight>0)
97
 
//                      v->setMaximum(qMax(0,fixheight));
98
 
//              else
99
 
//                      v->setMaximum(0);
100
 
        }
101
 
}
102
 
 
103
 
void SmartScrollArea::mouseMoveEvent(QMouseEvent *e) {
104
 
        if (e->buttons()==Qt::MidButton) {
105
 
                QPoint diff = e->pos() - mousePos;
106
 
                if (e->modifiers()==Qt::ShiftModifier)
107
 
                        diff*=5;
108
 
                mousePos = e->pos();
109
 
                verticalScrollBar()->setValue(verticalScrollBar()->value() + 
110
 
        diff.y());
111
 
                horizontalScrollBar()->setValue(horizontalScrollBar()->value() + 
112
 
        diff.x());
113
 
        }
114
 
}