/* Marcion
Copyright (C) 2009 - 2011 Milan Konvicka
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program */
#include "mimage.h"
//
CMImage::CMImage(CMessages * const messages,
QWidget * parent)
: QGraphicsView(parent),start(),scene(),current(0),
pixmap(),
messages(messages)
{
setScene(&scene);
}
//
void CMImage::mousePressEvent ( QMouseEvent * e )
{
if(current)
{
start=mapToScene(e->pos()+
QPoint(viewport()->width()/2,viewport()->height()/2));
current->setCursor(Qt::ClosedHandCursor);
}
}
void CMImage::mouseReleaseEvent ( QMouseEvent * )
{
if(current)
{
current->setCursor(Qt::OpenHandCursor);
start=QPointF();
}
}
void CMImage::mouseMoveEvent ( QMouseEvent * e )
{
if(!start.isNull()&¤t)
centerOn(start-e->pos());
}
bool CMImage::loadPage(QString const & filename,qreal stretch)
{
USE_CLEAN_WAIT
messages->MsgMsg(tr("displaying '")+filename+"' ...");
if(current)
delete current;
current=0;
//QPixmap pixmap(filename);
//try{
if(!pixmap.load(filename))
{
messages->MsgErr("'"+filename+tr("' not loaded"));
return false;
}
/*}
catch(...)
{
messages->MsgErr("'"+filename+tr("exception"));
return false;
}*/
int newwidth;
if(stretch==(qreal)0)
newwidth=viewport()->width()-10;
else
newwidth=pixmap.width()*stretch;
scene.clear();
QPixmap sp=pixmap.scaledToWidth(newwidth);
scene.setSceneRect(sp.rect());
current=scene.addPixmap(sp);
centerOn(1,1);
messages->MsgOk();
return true;
}
/*void CMImage::init(CMessages * const messages)
{
CMessages ** m((CMessages**)&this->messages);
*m=messages;
}*/
void CMImage::scale(qreal zoom)
{
USE_CLEAN_WAIT
scene.clear();
QPixmap sp=pixmap.scaledToWidth(pixmap.width()*zoom);
scene.setSceneRect(sp.rect());
current=scene.addPixmap(sp);
}