[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    QThumbView problem
From:       Hasanen Nawfal <family () warkaa ! net>
Date:       2003-01-31 7:23:56
[Download RAW message or body]

Hi
i bult a thumb viewer class using QT 3.0.5 only ( no KDE ) , when I call it , 
i pass the directory name to it and it will make thumbs from it's image.
at the first call the thumb is viewed well in QThumbView.
but after changing the directory, no thing viewed.

NOTE : before i change the directory i call a fucntion that delete the old 
thumbs in the view.

the files are attached.

thanx for help

Hasanen.
["thumbview.h" (text/x-chdr)]

/***************************************************************************
                          thumbview.h  -  description
                             -------------------
    begin                : Fri Oct 18 2002
    copyright            : (C) 2002 by Hasanen Naufal
    email                : family@warkaa.net
 ***************************************************************************/


#ifndef THUMBVIEW_H
#define THUMBVIEW_H

#include <qwidget.h>
#include <qscrollview.h>
#include <qdir.h>
#include <qstring.h>
#include <math.h>
#include <qthumb.h>
#include <qpalette.h>

/**
  *@author Hasanen Naufal
  */


class thumbView : public QScrollView  {
   Q_OBJECT
public: 
	thumbView(QWidget *parent=0, const char *name=0);
	~thumbView();
  /** No descriptions */
  void setDir(QString Dir);
  /** No descriptions */
  void chkDirs();
  /** No descriptions */
  void updateCache();
public: // Public attributes

  QPalette *pal;
  QThumb *selected;
  int x,y;
  QString *temp;
  QDir *currentDir;
  QDir *homeDir;
  QDir *cacheDir;
  QDir *tempDir;
  QStringList fileList;
  QThumb *thumb,*old,*first;

public slots: // Public slots
  /** No descriptions */
  void makeCache();
  void scalexy(int *x , int *y, int mx, int my);
  /** No descriptions */
  void showThumbs();
  /** No descriptions */
  void clear();
  /** No descriptions */
  void rearrange();
  /** No descriptions */
  void changed(QThumb *t);

signals:
void selectionChanged(QThumb *);

};

#endif

["thumbview.cpp" (text/x-c++src)]

/***************************************************************************
                          thumbview.cpp  -  description
                             -------------------
    begin                : Fri Oct 18 2002
    copyright            : (C) 2002 by Hasanen Naufal
    email                : family@warkaa.net
 ***************************************************************************/


#include "thumbview.h"
#include <qimage.h>
#include <qpixmap.h>
#include <qdir.h>
#include <stdio.h>
#include <qpalette.h>
#include <qcolor.h>


thumbView::thumbView(QWidget *parent, const char *name ) : QScrollView(parent,name)
{
x=y=0;
chkDirs();
QPalette p=parent->palette();
pal=new QPalette(p);
enableClipper(TRUE);
}


thumbView::~thumbView()
{
}

/** No descriptions */
void thumbView::setDir(QString Dir)
{
x=0;y=0;
QString *tmp;
QDir *tDir;
clear();
currentDir->setPath(Dir);
  tmp = new QString(cacheDir->path());
  tmp->append("/");
  tmp->append(Dir);
  tDir = new QDir(*tmp);
if (!tDir->exists())   { cacheDir->mkdir(*tmp,TRUE); makeCache(); }
  else updateCache();
  delete tmp;
  delete tDir;
  showThumbs();
}



void thumbView::chkDirs()
{
currentDir=new QDir(QDir::home());
homeDir=new QDir(QDir::home());
// create home and cache Directories
temp = new QString(homeDir->path());
temp->append("/.linsee");
tempDir=new QDir();
tempDir->setPath(*temp);
if (! tempDir->exists() )  homeDir->mkdir(".linsee",TRUE);
delete tempDir;
delete temp;
temp=new QString(homeDir->path());
temp->append("/.linsee/cache");
tempDir=new QDir();
tempDir->setPath(*temp);
if ( ! tempDir->exists() ) homeDir->mkdir(".linsee/cache",TRUE);
delete temp;
temp = new QString(homeDir->path());
temp->append("/.linsee/cache");
cacheDir=new QDir();
cacheDir->setPath(*temp);
delete temp;
}



void thumbView::makeCache()
{
fileList = currentDir->entryList("*.jpeg; *.jpg; *.bmp; *.png; *.xbm; *.xpm",0x002,0x01);
QImage *src;
QImage *scaled;
int width,height;
QString *tmp,*tmp2;

QStringList::Iterator text;

for (text = fileList.begin(); text!=fileList.end() ; ++text)
 {
   tmp=new QString(currentDir->path());
   tmp->append("/");
   tmp->append(*text);
   src = new QImage();
   src->load(*tmp,0);
//   printf("src = %s \n",tmp->latin1());
//   printf("%d %d \n",src->width(),src->height());
   width = src->width();
   height = src->height();
   scalexy(&width,&height,89,67);
   scalexy(&width,&height,89,67);
   scaled = new QImage(src->smoothScale(width,height));
//   printf("Scaled width,height = %d %d  \n",scaled->width(),scaled->height());
   tmp2 = new QString(cacheDir->path());
   tmp2->append(currentDir->path());
   tmp2->append("/");
   tmp2->append(*text);
//   printf("now saving %s \n",tmp2->latin1());
  scaled->save(*tmp2,QImage::imageFormat(*tmp),40);

if (scaled)  delete scaled;
if (src)  delete src;
if (tmp)  delete tmp;
if (tmp2)  delete tmp2;
}




}



void thumbView::scalexy(int *x , int *y, int mx, int my)
{
int ax,ay;

ax=*x;
ay=*y;
/*
if (ax<=mx && ay<=my ) {

*x=mx
*y=*y;
}

else
*/
if (ax>ay) {
          *x=mx;
          *y=floor( (mx*ay) / ax );
	  		 	}
else

if (ay>ax) {
            *y=my;
            *x=floor( (ax*my) / ay );

            }
else

if (ax==ay) {
						*x=mx;
            *y=my;
            }

}



void thumbView::updateCache()
{
QImage *src,*scaled;
int width,height;
QString *tmp, *tmp2;
QDir *tDir;

printf("now updating the cached files \n");

tmp = new QString(cacheDir->path());
tmp->append("/");
tmp->append(currentDir->path());

tDir = new QDir();
tDir->setPath(*tmp);
delete tmp;

fileList=currentDir->entryList("*.jpeg; *.jpg; *.bmp; *.png; *.xbm; *.xpm",0x002,0x01);

QStringList::Iterator text;

// this loop checks if any file added to the orginial directory.
for (text = fileList.begin(); text!=fileList.end() ; ++text)
{
if (!tDir->exists(*text,TRUE))
 {
  // cache new file
   tmp=new QString(currentDir->path());
   tmp->append("/");
   tmp->append(*text);
   src = new QImage();
   src->load(*tmp,0);
   width = src->width();
   height = src->height();
   scalexy(&width,&height,89,67);
   scalexy(&width,&height,89,67);
   scaled = new QImage(src->smoothScale(width,height));
   tmp2 = new QString(cacheDir->path());
   tmp2->append(currentDir->path());
   tmp2->append("/");
   tmp2->append(*text);
  scaled->save(*tmp2,QImage::imageFormat(*tmp),40);

  delete scaled;
  delete src;
  delete tmp;
  delete tmp2;

  }

}

fileList=tDir->entryList("*.jpeg; *.jpg; *.bmp; *.png; *.xbm; *.xpm; *.gif",0x002,0x01);

// this loop checks if any file deleted from the orginial directory.
for (text = fileList.begin(); text!=fileList.end() ; ++text)
{
if (!currentDir->exists(*text,TRUE))  tDir->remove(*text,FALSE);
}

delete tDir;
//the end.
}


void thumbView::showThumbs()
{
QStringList::Iterator text;
QPixmap *map;
QString *tmp;
QDir *tDir;
QString *fname;

tmp = new QString(cacheDir->path());
tmp->append("/");
tmp->append(currentDir->path());
tDir = new QDir();
tDir->setPath(*tmp);
old=NULL;
printf("viewing thumbs \n");
resizeContents(0,0);
fileList=tDir->entryList("*.jpeg; *.jpg; *.bmp; *.png; *.xbm; *.xpm; *.gif",0x002,0x01);
x=0;y=0;
for (text = fileList.begin(); text!=fileList.end() ; ++text)
 {
    fname = new QString(cacheDir->path());
//    fname->append("/");
    fname->append(currentDir->path());
    fname->append("/");
    fname->append(*text);
    printf("viewing file : %s \n",fname->latin1());
    map = new QPixmap(*fname,0,Auto);
   thumb = new QThumb(viewport(),0,0);
   thumb->prev=old;
   if (old) old->next=thumb;
   old=thumb;
   thumb->image->setPixmap(*map);
   thumb->fileName->setText(*text);
   thumb->image->resize(map->width(),map->height());
   addChild(thumb);
   moveChild(thumb,x,y);
   x+=111;
   if (x>=this->width()-50) { x=0; y+=109; }
   delete map;
   delete fname;
   connect(thumb,SIGNAL(selected(QThumb *)),this,SLOT(changed(QThumb *)));
 }
if (tmp)   delete tmp;
if (tDir)   delete tDir;
resizeContents(this->width(),y+110);
}



void thumbView::clear()
{
printf("clearing the area \n");
while (thumb!=NULL) { old = thumb; thumb = thumb->prev; delete old; }
}


void thumbView::rearrange()
{
QThumb *first;
int x,y,wx=0;
y=x=0;
first = thumb;

while (first->prev!=NULL)  first = first->prev;
while (first!=NULL)
 {
   moveChild(first,x,y);
   x+=111;
   if (y==0) wx=x;
   if (x>=this->width()-50) { x=0; y+=109; }
  first = first->next;
 }
 resizeContents(wx,y+110);

}


void thumbView::changed(QThumb *t)
{
if (selected) selected->setPalette(*pal);
selected=t;
selected->setPalette( QPalette( QColor(149, 200, 250) ) );
emit selectionChanged(selected);
}



>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic