imageitem.cpp
author insilmaril
Wed, 25 Nov 2009 10:58:21 +0000
changeset 807 f9f7922989d8
parent 798 d251c7b2de54
permissions -rw-r--r--
Added demos/vym-contribute.vym, fixes for selecting items
insilmaril@778
     1
#include "imageitem.h"
insilmaril@778
     2
insilmaril@778
     3
#include "branchitem.h"
insilmaril@778
     4
#include "mapobj.h"		// z-values
insilmaril@778
     5
insilmaril@778
     6
#include <QString>
insilmaril@778
     7
#include <iostream>
insilmaril@778
     8
using namespace std;
insilmaril@778
     9
insilmaril@778
    10
ImageItem::ImageItem()
insilmaril@778
    11
{
insilmaril@778
    12
	init();
insilmaril@778
    13
}
insilmaril@778
    14
insilmaril@779
    15
ImageItem::ImageItem (const QList<QVariant> &data, TreeItem *parent):MapItem (data,parent)
insilmaril@778
    16
{
insilmaril@778
    17
	init();
insilmaril@778
    18
}
insilmaril@778
    19
insilmaril@778
    20
ImageItem::~ImageItem()
insilmaril@778
    21
{
insilmaril@778
    22
	//cout <<"Destr ImageItem\n";
insilmaril@778
    23
	if (lmo) delete lmo;
insilmaril@778
    24
}
insilmaril@778
    25
insilmaril@778
    26
void ImageItem::init()
insilmaril@778
    27
{
insilmaril@788
    28
	setType (Image);
insilmaril@778
    29
	imageType=Undefined;
insilmaril@779
    30
	hideLinkUnselected=true;
insilmaril@778
    31
	originalFilename="no original name available";
insilmaril@778
    32
	zValue=Z_FLOATIMG;
insilmaril@778
    33
	posMode=Relative;
insilmaril@778
    34
}
insilmaril@778
    35
insilmaril@778
    36
ImageItem::ImageType ImageItem::getImageType()
insilmaril@778
    37
{
insilmaril@778
    38
	return imageType;
insilmaril@778
    39
}
insilmaril@778
    40
insilmaril@791
    41
void ImageItem::load(const QPixmap &pm)
insilmaril@791
    42
{
insilmaril@791
    43
	pixmap=pm;
insilmaril@791
    44
	if (lmo) ((FloatImageObj*)lmo)->load (pixmap);
insilmaril@791
    45
}
insilmaril@791
    46
insilmaril@778
    47
bool ImageItem::load(const QString &fname)
insilmaril@778
    48
{
insilmaril@778
    49
	bool ok=pixmap.load (fname);
insilmaril@778
    50
	if (lmo && ok)
insilmaril@778
    51
	{
insilmaril@778
    52
		setOriginalFilename (fname);
insilmaril@778
    53
		setHeading (originalFilename);
insilmaril@778
    54
		((FloatImageObj*)lmo)->load (pixmap);
insilmaril@778
    55
	}	
insilmaril@778
    56
	return ok;	
insilmaril@778
    57
}
insilmaril@778
    58
insilmaril@778
    59
FloatImageObj* ImageItem::createMapObj(QGraphicsScene *scene)
insilmaril@778
    60
{
insilmaril@790
    61
	FloatImageObj *fio=new FloatImageObj (scene,this);
insilmaril@778
    62
	lmo=fio;
insilmaril@779
    63
	fio->setParObj ( ((MapItem*)parentItem)->getLMO());
insilmaril@779
    64
	if (((BranchItem*)parentItem)->isScrolled() || !((MapItem*)parentItem)->getLMO()->isVisibleObj() )
insilmaril@778
    65
			fio->setVisibility (false);
insilmaril@778
    66
	initLMO();
insilmaril@778
    67
	fio->setZValue(zValue);
insilmaril@778
    68
	fio->setRelPos (pos);
insilmaril@798
    69
	//cout << "II::createMO   fio="<<fio<<"   tI="<<fio->getTreeItem()<<endl;
insilmaril@778
    70
	return fio;
insilmaril@778
    71
}
insilmaril@778
    72
insilmaril@778
    73
void ImageItem::setZValue(int z)
insilmaril@778
    74
{
insilmaril@778
    75
	zValue=z;
insilmaril@778
    76
	if (lmo) ((FloatImageObj*)lmo)->setZValue(z);
insilmaril@778
    77
}
insilmaril@778
    78
insilmaril@778
    79
void ImageItem::setOriginalFilename(const QString & fn)
insilmaril@778
    80
{
insilmaril@778
    81
	originalFilename=fn;
insilmaril@778
    82
insilmaril@778
    83
	// Set short name. Search from behind:
insilmaril@778
    84
	int i=originalFilename.findRev("/");
insilmaril@778
    85
	if (i>=0) originalFilename=originalFilename.remove (0,i+1);
insilmaril@778
    86
	setHeading (originalFilename);
insilmaril@778
    87
}
insilmaril@778
    88
insilmaril@778
    89
QString ImageItem::getOriginalFilename()
insilmaril@778
    90
{
insilmaril@778
    91
	return originalFilename;
insilmaril@778
    92
}
insilmaril@778
    93
insilmaril@778
    94
void ImageItem::save(const QString &fn, const QString &format)
insilmaril@778
    95
{
insilmaril@778
    96
	pixmap.save (fn,qPrintable (format));
insilmaril@778
    97
}
insilmaril@778
    98
insilmaril@778
    99
QString ImageItem::saveToDir (const QString &tmpdir,const QString &prefix) 
insilmaril@778
   100
{
insilmaril@778
   101
	if (hidden) return "";
insilmaril@778
   102
insilmaril@778
   103
	QString zAttr=attribut ("zValue",QString().setNum(zValue));
insilmaril@778
   104
	QString url;
insilmaril@778
   105
insilmaril@788
   106
	ulong n=reinterpret_cast <ulong> (this);
insilmaril@788
   107
insilmaril@788
   108
	url="images/"+prefix+"image-" + QString().number(n,10) + ".png" ;
insilmaril@778
   109
insilmaril@778
   110
	// And really save the image
insilmaril@778
   111
	pixmap.save (tmpdir +"/"+ url, "PNG");
insilmaril@778
   112
 
insilmaril@778
   113
	QString nameAttr=attribut ("originalName",originalFilename);
insilmaril@778
   114
insilmaril@778
   115
    return singleElement ("floatimage",  
insilmaril@779
   116
		getMapAttr() 
insilmaril@779
   117
		+getGeneralAttr()
insilmaril@778
   118
		+zAttr  
insilmaril@778
   119
		+attribut ("href",QString ("file:")+url)
insilmaril@778
   120
		+nameAttr
insilmaril@778
   121
	);	
insilmaril@778
   122
}
insilmaril@778
   123