floatimageobj.cpp
author insilmaril
Wed, 13 May 2009 08:26:27 +0000
changeset 767 6d2b32f305f9
parent 755 ed5b407975b3
child 776 25e634a7e1dc
permissions -rw-r--r--
Started to use QtKinetic for Animation
     1 #include <QImageReader>
     2 #include <QPixmap>
     3 
     4 #include "floatimageobj.h"
     5 #include "branchobj.h"
     6 
     7 /////////////////////////////////////////////////////////////////
     8 // FloatImageObj
     9 /////////////////////////////////////////////////////////////////
    10 
    11 uint FloatImageObj::saveCounter=0;		// make instance 
    12 
    13 
    14 FloatImageObj::FloatImageObj ():FloatObj()
    15 {
    16 //    cout << "Const FloatImageObj ()\n";
    17     setParObj (this);	
    18     init();
    19 }
    20 
    21 FloatImageObj::FloatImageObj (QGraphicsScene* s):FloatObj(s)
    22 {
    23  //   cout << "Const FloatImageObj (s)  called from MapCenterObj (s)\n";
    24     setParObj (this);	
    25     init();
    26 }
    27 
    28 FloatImageObj::FloatImageObj (QGraphicsScene *s, OrnamentedObj* p):FloatObj(s,p)
    29 {
    30  //   cout << "Const FloatImageObj (c,p)\n";
    31     init();
    32 }
    33 
    34 FloatImageObj::~FloatImageObj ()
    35 {
    36 //	cout << "Destr FloatImageObj "<<this<<"\n";
    37 	delete(icon);
    38 }
    39 
    40 void FloatImageObj::init () 
    41 {
    42 	icon=new ImageObj (scene);
    43 	icon->setPos (absPos.x(), absPos.y() );
    44 	icon->setVisibility (true);
    45 	setZValue (Z_FLOATIMG);
    46 	bbox.setSize (QSizeF(icon->boundingRect().width(), icon->boundingRect().height()));
    47 	clickBox.setSize (QSizeF(icon->boundingRect().width(), icon->boundingRect().height()));filename="";
    48 	originalFilename="no original name available";
    49 	filetype="";
    50 	saveInMap=true;
    51 	useRelPos=true;
    52 
    53 	//Hide flags
    54 	systemFlags->setShowFlags(false);
    55 }
    56 
    57 void FloatImageObj::copy (FloatImageObj* other)
    58 {					
    59 	FloatObj::copy (other);
    60 	icon->copy (other->icon);
    61 	filetype=other->filetype;
    62 	filename=other->filename;
    63 	originalFilename=other->originalFilename;
    64 	saveInMap=other->saveInMap;
    65     positionBBox();
    66 }
    67 
    68 void FloatImageObj::setZValue (const int &i)
    69 {
    70 	icon->setZValue (i);
    71 	zPlane=i;
    72 }
    73 
    74 int FloatImageObj::z ()
    75 {
    76 	return qRound (icon->zValue());
    77 }
    78 
    79 void FloatImageObj::load (const QPixmap &pixmap)
    80 {
    81 	icon->load(pixmap);
    82 	bbox.setSize (QSizeF(icon->boundingRect().width()+8, icon->boundingRect().height()+8));
    83 	clickBox.setSize (QSizeF(icon->boundingRect().width()+8, icon->boundingRect().height()+8));
    84 	positionBBox();
    85 	filetype="PNG";
    86 	filename="No filename given";
    87 }
    88 
    89 bool FloatImageObj::load (const QString &fn)
    90 {	
    91 	QImageReader reader (fn);
    92 	QImage img;
    93 
    94 	if( reader.read (&img))
    95 	{
    96 		icon->setPixmap(QPixmap::fromImage(img));
    97 		bbox.setSize (QSizeF(icon->boundingRect().width()+8, icon->boundingRect().height()+8));
    98 		positionBBox();
    99 		filename=fn;
   100 		filetype=reader.format();
   101 		setOriginalFilename (fn);
   102 		return true;
   103 	} else
   104 		return false;
   105 	
   106 }
   107 
   108 void FloatImageObj::save (const QString &fn, const QString &format)
   109 {	
   110 	icon->save (fn,qPrintable (format));
   111 }
   112 
   113 void FloatImageObj::setOriginalFilename(const QString & fn)
   114 {
   115 	originalFilename=fn;
   116 
   117 	// Set short name, too. Search from behind:
   118 	int i=originalFilename.findRev("/");
   119 	if (i>=0) originalFilename=originalFilename.remove (0,i+1);
   120 }
   121 
   122 QString FloatImageObj::getOriginalFilename()
   123 {
   124 	return originalFilename;
   125 }
   126 
   127 void FloatImageObj::setVisibility(bool v)
   128 {
   129     OrnamentedObj::setVisibility(v);
   130 	if (v)
   131 	    icon->setVisibility(true);
   132 	else
   133 	    icon->setVisibility(false);
   134 }
   135 
   136 void FloatImageObj::move (double x, double y)
   137 {
   138 	FloatObj::move(x,y);
   139 	icon->setPos (x+4,y+4);
   140     positionBBox();
   141 }
   142 
   143 void FloatImageObj::move (QPointF p)
   144 {
   145 	OrnamentedObj::move (p.x(),p.y());
   146 }
   147 
   148 void FloatImageObj::positionBBox()
   149 {
   150 	clickBox=bbox;
   151 }
   152 
   153 void FloatImageObj::calcBBoxSize()
   154 {
   155 	// TODO
   156 }
   157 
   158 QString FloatImageObj::saveToDir (const QString &tmpdir,const QString &prefix)
   159 {
   160 	//FIXME-2 if (hidden) return "";
   161 
   162 	saveCounter++;
   163 	
   164 	QString useOrientAttr;
   165 	if (useOrientation)
   166 		useOrientAttr=attribut ("useOrientation","true");
   167 	else	
   168 		useOrientAttr=attribut ("useOrientation","false");
   169 		
   170 	QString saveInMapAttr;
   171 	if (saveInMap)
   172 		saveInMapAttr=attribut ("saveInMap","true");
   173 	else	
   174 		
   175 		saveInMapAttr=attribut ("saveInMap","false");
   176 
   177 	QString exportAttr;
   178 	if (floatExport)
   179 		exportAttr=attribut ("floatExport","true");
   180 	else	
   181 		exportAttr=attribut ("floatExport","false");
   182 
   183 	QString zAttr=attribut ("zPlane",QString().setNum(zPlane));
   184 	QString url;
   185 
   186 	url="images/"+prefix+"image-" + QString().number(saveCounter,10) + ".png" ;
   187 
   188 	// And really save the image
   189 	icon->save (tmpdir +"/"+ url, "PNG");
   190  
   191 	QString nameAttr=attribut ("orgName",originalFilename);
   192 
   193     return singleElement ("floatimage",  
   194 		getOrnXMLAttr() 
   195 		+useOrientAttr 
   196 		+saveInMapAttr 
   197 		+exportAttr  
   198 		+zAttr  
   199 		+attribut ("href",QString ("file:")+url)
   200 		+nameAttr
   201 	);	
   202 }
   203 
   204 void FloatImageObj::resetSaveCounter()
   205 {
   206 	saveCounter=0;
   207 }
   208 
   209 
   210 QRectF FloatImageObj::getTotalBBox()
   211 {
   212 	return bbox;
   213 }
   214 
   215 QRectF FloatImageObj::getBBoxSizeWithChildren()
   216 {
   217 	//TODO abstract in linkablemapobj.h, not calculated
   218 	return bboxTotal;
   219 }
   220 
   221 void FloatImageObj::calcBBoxSizeWithChildren()
   222 {
   223 	//TODO abstract in linkablemapobj.h
   224 }
   225