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