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