mapobj.cpp
author insilmaril
Thu Dec 14 17:01:44 2006 +0000 (2006-12-14)
changeset 411 910ba9fab728
parent 366 e95081c21da2
child 412 8059b6aa74d7
permissions -rw-r--r--
1.8.61 various fixes, see changelog
     1 #include "mapobj.h"
     2 #include "misc.h"
     3 
     4 /////////////////////////////////////////////////////////////////
     5 // MapObj
     6 /////////////////////////////////////////////////////////////////
     7 MapObj::MapObj ()
     8 {
     9 	//qWarning ( "Const MapObj (): Please set scene somehow!!!");
    10 	scene=NULL;
    11     init ();
    12 }
    13 
    14 MapObj::MapObj (QGraphicsScene *s)
    15 {
    16 //  cout << "Const MapObj\n";
    17     scene=s;
    18     init ();
    19 }
    20 
    21 
    22 MapObj::MapObj (MapObj* mo)
    23 {
    24 //    cout << "CopyConst MapObj\n";
    25     copy (mo);
    26 }
    27 
    28 MapObj::~MapObj ()
    29 {
    30 //    cout << "Destr MapObj\n";
    31 }
    32 
    33 void MapObj::init ()
    34 {
    35     absPos=QPointF(0,0);
    36     visible=true;
    37 }
    38 
    39 void MapObj::copy(MapObj* other)
    40 {
    41 //    scene=other->scene;	// already set in constr. of child, use that one...
    42     absPos=other->absPos;
    43 	bbox.setX (other->bbox.x() );
    44 	bbox.setY (other->bbox.y() );
    45 	bbox.setSize (QSizeF(other->bbox.width(), other->bbox.height() ) );
    46 }
    47 
    48 QGraphicsScene* MapObj::getScene()
    49 {
    50 	return scene;
    51 }
    52 
    53 qreal MapObj::x() 
    54 {
    55     return absPos.x();
    56 }
    57 
    58 qreal MapObj::y() 
    59 {
    60     return absPos.y();
    61 }
    62 
    63 qreal MapObj::width() 
    64 {
    65     return bbox.width();
    66 }
    67 
    68 qreal MapObj::height() 
    69 {
    70     return bbox.height();
    71 }
    72 
    73 QPointF MapObj::getAbsPos() 
    74 {
    75     return absPos;
    76 }
    77 
    78 QString MapObj::getPos()
    79 {
    80 	return qpointfToString(absPos);
    81 }
    82 
    83 void MapObj::move (double x, double y) 
    84 {
    85     absPos.setX( x);
    86     absPos.setY( y);
    87     bbox.moveTo(QPointF(x,y));
    88     clickBox.moveTo(QPointF(x,y));
    89 }
    90 
    91 void MapObj::move (QPointF p)
    92 {
    93 	absPos=p;
    94 	bbox.moveTo (p);
    95 	clickBox.moveTo (p);
    96 }
    97 
    98 void MapObj::moveBy (double x, double y) 
    99 {
   100     MapObj::move (x+absPos.x(),y+absPos.y() );
   101 	bbox.moveTo (bbox.x()+x,bbox.y()+y);
   102 	clickBox.moveTo (clickBox.x()+x,clickBox.y()+y);
   103 }
   104 
   105 bool MapObj::inBox(const QPointF &p)
   106 {
   107 /* FIXME testing QT4 only
   108 	cout << "MO::inBox  "<<p<<"  "<<
   109 		clickBox.left()<<","<<
   110 		clickBox.right()<<","<<
   111 		clickBox.top()<<","<<
   112 		clickBox.bottom()<<endl;
   113 		*/
   114     if (p.x() >= clickBox.left() && p.x() <= clickBox.right()  
   115 	&& p.y() <= clickBox.bottom() && p.y() >= clickBox.top() )
   116 		return true;
   117     return false;	
   118 }
   119 
   120 QRectF MapObj::getBBox()
   121 {
   122     return bbox;
   123 }
   124 
   125 QRectF MapObj::addBBox(QRectF r1, QRectF r2)
   126 {	
   127 	// Find smallest QRectF containing given rectangles
   128 
   129 	QRectF n;
   130 	// Set left border
   131 	if (r1.left() <= r2.left() )
   132 		n.setLeft(r1.left() );
   133 	else
   134 		n.setLeft(r2.left() );
   135 		
   136 	// Set top border		
   137 	if (r1.top() <= r2.top() )
   138 		n.setTop(r1.top() );
   139 	else
   140 		n.setTop(r2.top() );
   141 		
   142 	// Set right border
   143 	if (r1.right() <= r2.right() )
   144 		n.setRight(r2.right() );
   145 	else
   146 		n.setRight(r1.right() );
   147 		
   148 	// Set bottom 
   149 	if (r1.bottom() <= r2.bottom() )
   150 		n.setBottom(r2.bottom() );
   151 	else
   152 		n.setBottom(r1.bottom() );
   153 	return n;
   154 }
   155 
   156 QSizeF MapObj::getSize()
   157 {
   158     return bbox.size();
   159 }
   160 
   161 
   162 bool MapObj::isVisibleObj()
   163 {
   164     return visible;
   165 }
   166 
   167 void MapObj::setVisibility(bool v)
   168 {
   169     visible=v;
   170 }
   171