/****************************************************************************
**
**  indeview_slide.cpp   of the IndeView project, see http://www.indeview.org
**
**
**  The IndeView application is a stand-alone viewer to display presentations
**  created by IndeView creator modules like IndeViewExport.bas or kpr2iv.sh.
**  -------------------------------------------------------------------------
**  This enables you to show OpenOffice/StarOffice or KOffice presentations
**  on machines where the respective presentation program is not available.
**
**
**  Copyright (C) 2003..2004 Michael & Karl-Heinz Zimmer.
**  All rights reserved.
**
**  This file is distributed under the terms of the
**  GNU Lesser General Public License version 2.1 as published by the
**  Free Software Foundation and appearing in the file LICENSE included
**  in the packaging of this file.
**
**  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
**  THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
**  PURPOSE.
**
**  See http://www.indeview.org for further information.
**
**  Contact info@indeview.org if any conditions of this
**  licensing are not clear to you.
**
**
**  Please consider sending
**  a little picture postcard to   Michael & Karl-Heinz Zimmer
**                                 Butzengasse 15
**                                 54343 Föhren
**                                 Germany
**
**  Your feedback will be very much appreciated: we would be glad about
**  receiving a visual representation of where on earth you are living
**  using our little module.
**
*****************************************************************************/

#include <qapplication.h>
#include <qpainter.h>
#include <qcursor.h>
#include <qfile.h>
#include <qurl.h>
#include <qptrlist.h>
#include <qvaluelist.h>

#include "indeview_slide.h"


IndeViewCachedPixmap& IndeViewCachedPixmap::operator=( const IndeViewCachedPixmap& R )
{
  if( &R != this ){
    info = R.info;
    pix  = R.pix;
  }
  return *this;
}

void IndeViewCachedPixmaps::set(int i, const IndeViewSizeAndFile& _info, const QPixmap& _pix)
{
  if( 0 <= i && i < INDEVIEW_N_CACHED_PIXMAPS ){
    list[i].info.set( _info.fileName(), _info.size() );
    list[i].pix = _pix;
  }
}

QPixmap* IndeViewSlide::getImage(QSize size, bool justPreloading)const
{
  QPixmap pix;
  QString fileName;
  IndeViewImageList::ConstIterator it;
  for ( it = images.begin(); it != images.end(); ++it )
    if( size == (*it).size() )
      fileName = (*it).fileName();

  //qDebug("getImage() file:");
  //qDebug(fileName);

  if( !fileName.isEmpty() ){
    for(int i=0; i<INDEVIEW_N_CACHED_PIXMAPS; ++i){
      const IndeViewSizeAndFile& saf = rCache.list[i].info;
      if( !saf.isEmpty() && IndeViewSizeAndFile(fileName, size) == saf ){

        //qDebug("  found in cache\n");

        // move cached record to the top of the cache
        if( 0 < i && !justPreloading ){
          IndeViewCachedPixmap tmp = rCache.list[i];
          for(int j=i; 0<j; --j){
            rCache.list[j] = rCache.list[j-1];
          }
          rCache.list[0] = tmp;
        }
        return &rCache.list[0].pix;
      }
    }
  }

  if( !justPreloading )
    QApplication::setOverrideCursor(QCursor( Qt::WaitCursor ));

  //qDebug("  must be read\n");

  if( !fileName.isEmpty() ){
    // try to read image from file
    QUrl fileURL( fileName );
    QString name;
    if( fileURL.isLocalFile() ){
      name =fileURL.toString(true, false);
      if( pix.load( name ) ){
        //qDebug(QString("%1 -> %2 loaded successfully.").arg(fileName).arg(name));
      }else{
        qDebug(QString("ERROR: %1 -> %2 could not be loaded.").arg(fileName).arg(name));
      }
    }else{
      qDebug(QString("ERROR: %1 -> %2 is not a local file.").arg(fileName).arg(name));
    }
  }
  // use error image if image could not be loaded
  if( pix.isNull() ){
    pix = INDEVIEWERROR_Xpm;
    fileName.prepend("ERROR_");
  }
  // update cache
  for(int i=INDEVIEW_N_CACHED_PIXMAPS-1; 0<i; --i){
    rCache.list[i] = rCache.list[i-1];
  }
  rCache.set( 0, IndeViewSizeAndFile(fileName, size), pix );

  if( !justPreloading )
    QApplication::restoreOverrideCursor();	// restore original cursor

  // return the image
  return &rCache.list[0].pix;
}

void IndeViewSlide::addImageDescription(const IndeViewSizeAndFile& description)
{
  images << description;
}
