flagobj.cpp
author insilmaril
Fri, 01 May 2009 10:30:29 +0000
changeset 763 8c028a5d9083
parent 655 d4b49c6c6069
child 767 6d2b32f305f9
permissions -rw-r--r--
add above/below
     1 #include "flagobj.h"
     2 
     3 /////////////////////////////////////////////////////////////////
     4 // FlagObj
     5 /////////////////////////////////////////////////////////////////
     6 FlagObj::FlagObj()
     7 {
     8 //	cout << "Const FlagObj ()\n";
     9     init ();
    10 }
    11 
    12 FlagObj::FlagObj(QGraphicsScene* s):MapObj(s) 
    13 {
    14 //	cout << "Const FlagObj  this="<<this<<"  scene="<<s<<endl;
    15     init ();
    16 }
    17 
    18 FlagObj::FlagObj (FlagObj* io)
    19 {
    20     copy (io);
    21 }
    22 
    23 FlagObj::~FlagObj()
    24 {
    25 //   cout << "Destr FlagObj  this="<<this <<"  " << qPrintable(name) << "\n";
    26 	if (icon) delete (icon);
    27 }
    28 
    29 
    30 void FlagObj::init ()
    31 {
    32 	name="undefined";
    33 	unsetGroup();
    34 
    35 	icon=new ImageObj (scene);
    36 	icon->setPos (absPos.x(), absPos.y() );
    37 	state=false;
    38 	used=false;
    39 	avis=true;
    40 	action=NULL;
    41 }
    42 
    43 void FlagObj::copy (FlagObj* other)
    44 {
    45     MapObj::copy(other);
    46 	name=other->name;
    47 	group=other->group;
    48 	tooltip=other->tooltip;
    49 	state=other->state;
    50 	avis=other->avis;
    51 	used=other->used;
    52 	icon->copy(other->icon);
    53 	action=other->action;
    54 	setVisibility (other->isVisibleObj() );
    55 }
    56 
    57 void FlagObj::move(double x, double y)
    58 {
    59     MapObj::move(x,y);
    60 	icon->setPos(x,y);
    61 	positionBBox();
    62 }
    63 
    64 void FlagObj::moveBy(double x, double y)
    65 {
    66     move (x+absPos.x(),y+absPos.y() );
    67 }
    68 
    69 void FlagObj::setVisibility (bool v)
    70 {
    71     MapObj::setVisibility(v);
    72 	if (v && state)
    73 	    icon->setVisibility(true);
    74 	else
    75 	    icon->setVisibility(false);
    76 }
    77 
    78 void FlagObj::load (const QString &fn)
    79 {
    80 	icon->load(fn);
    81 	calcBBoxSize();
    82 	positionBBox();
    83 }
    84 
    85 void FlagObj::load (const QPixmap &pm)
    86 {
    87 	icon->load(pm);
    88 	calcBBoxSize();
    89 	positionBBox();
    90 }
    91 
    92 void FlagObj::setName(const QString &n)
    93 {
    94 	name=n;
    95 }
    96 
    97 const QString FlagObj::getName()
    98 {
    99 	return name;
   100 }
   101 
   102 void FlagObj::setGroup (const QString &n)
   103 {
   104 	group=n;
   105 }
   106 
   107 const QString FlagObj::getGroup()
   108 {
   109 	return group;
   110 }
   111 
   112 void FlagObj::unsetGroup()
   113 {
   114 	group="undefined";
   115 }
   116 
   117 void FlagObj::setToolTip(const QString &n)
   118 {
   119 	tooltip=n;
   120 }
   121 
   122 const QString FlagObj::getToolTip()
   123 {
   124 	return tooltip;
   125 }
   126 
   127 QPixmap FlagObj::getPixmap()
   128 {
   129 	return icon->pixmap();
   130 }
   131 
   132 void FlagObj::setAction (QAction* a)
   133 {
   134 	action=a;
   135 }
   136 
   137 void FlagObj::updateAction ()
   138 {
   139 	if (action) 
   140 	{
   141 		action->setChecked(state);
   142 		if (!avis)
   143 			action->setVisible(state);
   144 	}
   145 }
   146 
   147 void FlagObj::setAlwaysVisible(bool b)
   148 {
   149 	avis=b;
   150 }
   151 
   152 bool FlagObj::isAlwaysVisible()
   153 {
   154 	return avis;
   155 }
   156 
   157 bool FlagObj::isActive()
   158 {
   159 	return state;
   160 }
   161 
   162 void FlagObj::toggle()
   163 {
   164 	if (state)
   165 		deactivate();
   166 	else
   167 		activate();
   168 }
   169 
   170 void FlagObj::activate()
   171 {
   172 	state=true;
   173 	// only show icon, if flag itself is visible 
   174 	if (visible) 
   175 	{
   176 		icon->setVisibility (true);
   177 		calcBBoxSize();
   178 	}	
   179 }
   180 
   181 void FlagObj::deactivate()
   182 {
   183 	state=false;
   184 	// if flag itself is invisible we don't need to call 
   185 	if (visible) 
   186 	{
   187 		icon->setVisibility (false);
   188 		calcBBoxSize();
   189 	}	
   190 }
   191 
   192 void FlagObj::setUsed (bool b)
   193 {
   194 	used=b;
   195 }
   196 
   197 bool FlagObj::isUsed()
   198 {
   199 	return used;
   200 }
   201 
   202 void FlagObj::saveToDir (const QString &tmpdir, const QString &prefix)
   203 {
   204 	QString fn=tmpdir + prefix + name + ".png";
   205 	icon->save (fn,"PNG");
   206 }
   207 
   208 void FlagObj::positionBBox()
   209 {
   210     bbox.moveTopLeft (absPos );
   211     clickBox.moveTopLeft (absPos );
   212 }
   213 
   214 void FlagObj::calcBBoxSize()
   215 {
   216 	if (visible && state)
   217 		bbox.setSize (	QSizeF(
   218 			icon->boundingRect().width(), 
   219 			icon->boundingRect().height() ) );
   220 	else
   221 		bbox.setSize (QSizeF(0,0));
   222 	clickBox.setSize (bbox.size());
   223 }
   224