summaryrefslogtreecommitdiff
path: root/noncore/graphics/opie-eye/gui/imagescrollview.cpp
Side-by-side diff
Diffstat (limited to 'noncore/graphics/opie-eye/gui/imagescrollview.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/graphics/opie-eye/gui/imagescrollview.cpp56
1 files changed, 46 insertions, 10 deletions
diff --git a/noncore/graphics/opie-eye/gui/imagescrollview.cpp b/noncore/graphics/opie-eye/gui/imagescrollview.cpp
index 7d83e29..ee20f40 100644
--- a/noncore/graphics/opie-eye/gui/imagescrollview.cpp
+++ b/noncore/graphics/opie-eye/gui/imagescrollview.cpp
@@ -11,64 +11,94 @@ ImageScrollView::ImageScrollView( QWidget* parent, const char* name, WFlags f )
:QScrollView(parent,name,f|Qt::WRepaintNoErase ),_image_data(),_original_data(),scale_to_fit(true),
rotate_to_fit(true),first_resize_done(false)
{
init();
}
ImageScrollView::ImageScrollView (const QImage&img, QWidget * parent, const char * name, WFlags f,bool always_scale,bool rfit)
:QScrollView(parent,name,f|Qt::WRepaintNoErase),_image_data(),_original_data(img),scale_to_fit(always_scale),
rotate_to_fit(rfit),first_resize_done(false)
{
init();
}
ImageScrollView::ImageScrollView (const QString&img, QWidget * parent, const char * name, WFlags f,bool always_scale,bool rfit)
:QScrollView(parent,name,f|Qt::WRepaintNoErase),_image_data(),_original_data(img),scale_to_fit(always_scale),
rotate_to_fit(rfit),first_resize_done(false)
{
init();
}
void ImageScrollView::setImage(const QImage&img)
{
_image_data = QImage();
_original_data=img;
- first_resize_done = false;
- init();
+ if (first_resize_done) {
+ generateImage();
+ }
}
void ImageScrollView::setImage( const QString& path ) {
-
+ odebug << "load new image " << oendl;
+ _original_data.load(path);
+ _image_data = QImage();
+ if (first_resize_done) {
+ generateImage();
+ }
}
/* should be called every time the QImage changed it content */
void ImageScrollView::init()
{
+ odebug << "init " << oendl;
viewport()->setBackgroundColor(white);
- if (_original_data.size().isValid()) {
+ if (first_resize_done) {
+ last_rot = Rotate0;
+ generateImage();
+ odebug << "reinit display " << oendl;
+ } else if (_original_data.size().isValid()) {
resizeContents(_original_data.width(),_original_data.height());
}
- last_rot = Rotate0;
+}
+
+void ImageScrollView::setAutoRotate(bool how)
+{
+ /* to avoid double repaints */
+ if (rotate_to_fit != how) {
+ rotate_to_fit = how;
+ _image_data = QImage();
+ generateImage();
+ }
+}
+
+void ImageScrollView::setAutoScale(bool how)
+{
+ scale_to_fit = how;
+ if (!how) {
+ rotate_to_fit = false;
+ }
+ _image_data = QImage();
+ generateImage();
}
ImageScrollView::~ImageScrollView()
{
}
void ImageScrollView::rescaleImage(int w, int h)
{
if (_image_data.width()==w && _image_data.height()==h) {
return;
}
double hs = (double)h / (double)_image_data.height() ;
double ws = (double)w / (double)_image_data.width() ;
double scaleFactor = (hs > ws) ? ws : hs;
int smoothW = (int)(scaleFactor * _image_data.width());
int smoothH = (int)(scaleFactor * _image_data.height());
_image_data = _image_data.smoothScale(smoothW,smoothH);
}
void ImageScrollView::rotate_into_data(Rotation r)
{
/* realy - we must do this that way, 'cause when acting direct on _image_data the app will
segfault :( */
QImage dest;
@@ -160,77 +190,83 @@ void ImageScrollView::rotate_into_data(Rotation r)
dest.setNumColors(_original_data.numColors());
srcTable = (unsigned int *)_original_data.colorTable();
destTable = (unsigned int *)dest.colorTable();
for ( x=0; x < _original_data.numColors(); ++x )
destTable[x] = srcTable[x];
for ( y=0; y < _original_data.height(); ++y )
{
srcData = (unsigned char *)_original_data.scanLine(y);
for ( x=0; x < _original_data.width(); ++x )
{
destData = (unsigned char *)dest.scanLine(_original_data.width()-x-1);
destData[y] = srcData[x];
}
}
break;
default:
dest = _original_data;
break;
}
}
_image_data = dest;
}
-void ImageScrollView::resizeEvent(QResizeEvent * e)
+void ImageScrollView::generateImage()
{
- odebug << "ImageScrollView resizeEvent" << oendl;
- QScrollView::resizeEvent(e);
Rotation r = Rotate0;
if (width()>height()&&_original_data.width()<_original_data.height() ||
width()<height()&&_original_data.width()>_original_data.height()) {
if (rotate_to_fit) r = Rotate90;
}
odebug << " r = " << r << oendl;
if (scale_to_fit) {
if (!_image_data.size().isValid()||width()>_image_data.width()||height()>_image_data.height()) {
+ odebug << "Rescaling data" << oendl;
if (r==Rotate0) {
_image_data = _original_data;
} else {
rotate_into_data(r);
}
}
rescaleImage(width(),height());
- resizeContents(width()-10,height()-10);
- } else if (!first_resize_done||r!=last_rot) {
+ resizeContents(_image_data.width(),_image_data.height());
+ } else if (!first_resize_done||r!=last_rot||_image_data.width()==0) {
if (r==Rotate0) {
_image_data = _original_data;
} else {
rotate_into_data(r);
}
last_rot = r;
resizeContents(_image_data.width(),_image_data.height());
}
+}
+
+void ImageScrollView::resizeEvent(QResizeEvent * e)
+{
+ odebug << "ImageScrollView resizeEvent" << oendl;
+ QScrollView::resizeEvent(e);
+ generateImage();
first_resize_done = true;
}
void ImageScrollView::drawContents(QPainter * p, int clipx, int clipy, int clipw, int cliph)
{
int w = clipw;
int h = cliph;
int x = clipx;
int y = clipy;
bool erase = false;
if (!_image_data.size().isValid()) {
p->fillRect(clipx,clipy,clipw,cliph,white);
return;
}
if (w>_image_data.width()) {
w=_image_data.width();
x = 0;
erase = true;
} else if (x+w>_image_data.width()){
x = _image_data.width()-w;
}
if (h>_image_data.height()) {
h=_image_data.height();