TonicTones
Src/ImageScrollArea.cpp
00001 //      ImageScrollArea.cpp
00002 //      
00003 //      Copyright 2010 Jérémy Laumon <jeremy.laumon@gmail.com>
00004 //      
00005 //      This program is free software; you can redistribute it and/or modify
00006 //      it under the terms of the GNU General Public License as published by
00007 //      the Free Software Foundation; either version 2 of the License, or
00008 //      (at your option) any later version.
00009 //      
00010 //      This program is distributed in the hope that it will be useful,
00011 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //      GNU General Public License for more details.
00014 //      
00015 //      You should have received a copy of the GNU General Public License
00016 //      along with this program; if not, write to the Free Software
00017 //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018 //      MA 02110-1301, USA.
00019 
00020 
00021 #include <ImageScrollArea.h>
00022 
00035 ImageScrollArea::ImageScrollArea(QWidget *parent):
00036     QScrollArea(parent),
00037     imageLabel(new QLabel),
00038     scaleFactor(1.0)
00039 {
00040     setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
00041     
00042     imageLabel->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
00043     imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
00044     imageLabel->setScaledContents(true);
00045     
00046     setWidget(imageLabel);
00047     scaleImage(1.0);
00048 }
00049 
00053 QLabel* ImageScrollArea::image()
00054 {
00055     return imageLabel;
00056 }
00057 
00061 void ImageScrollArea::wheelEvent(QWheelEvent *event)
00062 {
00063     int numDegrees = event->delta() / 8;
00064     int numSteps = numDegrees / 15;
00065     
00066     if (event->modifiers() & Qt::ShiftModifier)
00067         horizontalScrollBar()->setValue(horizontalScrollBar()->value() - 3*numSteps*horizontalScrollBar()->singleStep());
00068     else if (event->modifiers() & Qt::ControlModifier)
00069         scaleImage(1.0+(double(numSteps)/10.0));
00070     else 
00071         verticalScrollBar()->setValue(verticalScrollBar()->value() - 3*numSteps*verticalScrollBar()->singleStep());
00072     
00073     event->accept();
00074 }
00075 
00082 void ImageScrollArea::scaleImage(double factor, bool relative)
00083 {
00084     if(imageLabel->pixmap())
00085     {
00086         if (relative)
00087             scaleFactor *= factor;
00088         else
00089             scaleFactor = factor;
00090         imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
00091 
00092         adjustScrollBar(horizontalScrollBar(), factor, relative);
00093         adjustScrollBar(verticalScrollBar(), factor, relative);
00094         
00095         emit scaleChanged(scaleFactor);
00096     }
00097 }
00098 
00099 
00103 void ImageScrollArea::adjustScrollBar(QScrollBar *scrollBar, double factor, bool relative)
00104 {
00105     if (relative)
00106         scrollBar->setValue(int(factor * scrollBar->value()
00107                                 + ((factor - 1) * scrollBar->pageStep()/2)));
00108     else
00109         scrollBar->setValue((scrollBar->maximum() - scrollBar->minimum())/2);
00110         
00111 }
00112 
 All Classes Functions Variables