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