floatimageobj.cpp
author insilmaril
Mon, 18 Jun 2007 15:10:19 +0000
changeset 507 2f57a7d2315e
parent 441 658ad3bc0c8a
child 628 d7d0708b1c60
permissions -rw-r--r--
1.8.73 - beta testing
     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 	positionBBox();
    87 	filetype="PNG";
    88 	filename="No filename given";
    89 }
    90 
    91 bool FloatImageObj::load (const QString &fn)
    92 {	
    93 	QImageReader reader (fn);
    94 	QImage img;
    95 
    96 	if( reader.read (&img))
    97 	{
    98 		icon->setPixmap(QPixmap::fromImage(img));
    99 		bbox.setSize (QSizeF(icon->boundingRect().width()+8, icon->boundingRect().height()+8));
   100 		positionBBox();
   101 		filename=fn;
   102 		filetype=reader.format();
   103 		setOriginalFilename (fn);
   104 		return true;
   105 	} else
   106 		return false;
   107 	
   108 }
   109 
   110 void FloatImageObj::save (const QString &fn, const QString &format)
   111 {	
   112 	icon->save (fn,qPrintable (format));
   113 }
   114 
   115 void FloatImageObj::setOriginalFilename(const QString & fn)
   116 {
   117 	originalFilename=fn;
   118 
   119 	// Set short name, too. Search from behind:
   120 	int i=originalFilename.findRev("/");
   121 	if (i>=0) originalFilename=originalFilename.remove (0,i+1);
   122 }
   123 
   124 QString FloatImageObj::getOriginalFilename()
   125 {
   126 	return originalFilename;
   127 }
   128 
   129 void FloatImageObj::setVisibility(bool v)
   130 {
   131     OrnamentedObj::setVisibility(v);
   132 	if (v)
   133 	    icon->setVisibility(true);
   134 	else
   135 	    icon->setVisibility(false);
   136 }
   137 
   138 void FloatImageObj::move (double x, double y)
   139 {
   140 	FloatObj::move(x,y);
   141 	icon->setPos (x+4,y+4);
   142     positionBBox();
   143 }
   144 
   145 void FloatImageObj::move (QPointF p)
   146 {
   147 	OrnamentedObj::move (p.x(),p.y());
   148 }
   149 
   150 void FloatImageObj::positionBBox()
   151 {
   152 	clickBox=bbox;
   153 }
   154 
   155 void FloatImageObj::calcBBoxSize()
   156 {
   157 	// TODO
   158 }
   159 
   160 QString FloatImageObj::saveToDir (const QString &tmpdir,const QString &prefix)
   161 {
   162 	if (hidden) return "";
   163 
   164 	saveCounter++;
   165 	
   166 	QString useOrientAttr;
   167 	if (useOrientation)
   168 		useOrientAttr=attribut ("useOrientation","true");
   169 	else	
   170 		useOrientAttr=attribut ("useOrientation","false");
   171 		
   172 	QString saveInMapAttr;
   173 	if (saveInMap)
   174 		saveInMapAttr=attribut ("saveInMap","true");
   175 	else	
   176 		
   177 		saveInMapAttr=attribut ("saveInMap","false");
   178 
   179 	QString exportAttr;
   180 	if (floatExport)
   181 		exportAttr=attribut ("floatExport","true");
   182 	else	
   183 		exportAttr=attribut ("floatExport","false");
   184 
   185 	QString zAttr=attribut ("zPlane",QString().setNum(zPlane));
   186 	QString url;
   187 
   188 	url="images/"+prefix+"image-" + QString().number(saveCounter,10) + "." +filetype;
   189 
   190 	// And really save the image
   191 	icon->save (tmpdir + "/" + url, "PNG");
   192  
   193 	QString nameAttr=attribut ("orgName",originalFilename);
   194 
   195     return singleElement ("floatimage",  
   196 		getOrnAttr() 
   197 		+useOrientAttr 
   198 		+saveInMapAttr 
   199 		+exportAttr  
   200 		+zAttr  
   201 		+attribut ("href",QString ("file:")+url)
   202 		+nameAttr
   203 	);	
   204 }
   205 
   206 void FloatImageObj::resetSaveCounter()
   207 {
   208 	saveCounter=0;
   209 }
   210 
   211 
   212 QRectF FloatImageObj::getTotalBBox()
   213 {
   214 	return bbox;
   215 }
   216 
   217 QRectF FloatImageObj::getBBoxSizeWithChilds()
   218 {
   219 	//TODO abstract in linkablemapobj.h, not calculated
   220 	return bboxTotal;
   221 }
   222 
   223 void FloatImageObj::calcBBoxSizeWithChilds()
   224 {
   225 	//TODO abstract in linkablemapobj.h
   226 }
   227 
   228 QString FloatImageObj::getSelectString()
   229 {
   230 	QString s;
   231 	if (parObj)
   232 	{
   233 		if (parObj->getDepth()==0)
   234 			s= "fi:" + QString("%1").arg( ((BranchObj*)(parObj))->getFloatImageNum(this));
   235 		else	
   236 			s= ((BranchObj*)(parObj))->getSelectString() + ",fi:" + QString("%1").arg( ((BranchObj*)(parObj))->getFloatImageNum(this));
   237 	} else
   238 		s="mc:";
   239 	
   240 	return s;
   241 
   242 }
   243