mapobj.cpp
author insilmaril
Tue, 03 Jan 2006 09:44:41 +0000
changeset 177 494e8f40f0f7
parent 175 728f51b71e71
child 227 38ad83f1d4ce
permissions -rw-r--r--
1.7.6 New features for floatimages and fixes
     1 #include "mapobj.h"
     2 #include "misc.h"
     3 
     4 /////////////////////////////////////////////////////////////////
     5 // MapObj
     6 /////////////////////////////////////////////////////////////////
     7 MapObj::MapObj ()
     8 {
     9 	//qWarning ( "Const MapObj (): Please set canvas somehow!!!");
    10 	canvas=NULL;
    11     init ();
    12 }
    13 
    14 MapObj::MapObj (QCanvas* c)
    15 {
    16 //  cout << "Const MapObj\n";
    17     canvas=c;
    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=QPoint(0,0);
    36     visible=true;
    37 }
    38 
    39 void MapObj::copy(MapObj* other)
    40 {
    41 //    canvas=other->canvas;	// 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 (QSize(other->bbox.width(), other->bbox.height() ) );
    46 }
    47 
    48 QCanvas* MapObj::getCanvas()
    49 {
    50 	return canvas;
    51 }
    52 
    53 int MapObj::x() 
    54 {
    55     return absPos.x();
    56 }
    57 
    58 int MapObj::y() 
    59 {
    60     return absPos.y();
    61 }
    62 
    63 int MapObj::width() 
    64 {
    65     return bbox.width();
    66 }
    67 
    68 int MapObj::height() 
    69 {
    70     return bbox.height();
    71 }
    72 
    73 QString MapObj::getPos()
    74 {
    75 	return qpointToString(absPos);
    76 }
    77 
    78 void MapObj::move (double x, double y) 
    79 {
    80     int xi=static_cast <int> (x);
    81     int yi=static_cast <int> (y);
    82     absPos.setX( xi);
    83     absPos.setY( yi);
    84     bbox.moveTopLeft(QPoint(xi,yi));
    85     clickBox.moveTopLeft(QPoint(xi,yi));
    86 }
    87 
    88 void MapObj::moveBy (double x, double y) 
    89 {
    90 	int ix=(int)x;
    91 	int iy=(int)y;
    92     move (x+absPos.x(),y+absPos.y() );
    93 	bbox.moveBy (ix,iy);
    94 	clickBox.moveBy (ix,iy);
    95 }
    96 
    97 bool MapObj::inBox(const QPoint &p)
    98 {
    99     if (p.x() >= clickBox.left() && p.x() <= clickBox.right()  
   100 	&& p.y() <= clickBox.bottom() && p.y() >= clickBox.top() )
   101 		return true;
   102     return false;	
   103 }
   104 
   105 QRect MapObj::getBBox()
   106 {
   107     return bbox;
   108 }
   109 
   110 QRect MapObj::addBBox(QRect r1, QRect r2)
   111 {	
   112 	// Find smallest QRect containing given rectangles
   113 
   114 	QRect n;
   115 	// Set left border
   116 	if (r1.left() <= r2.left() )
   117 		n.setLeft(r1.left() );
   118 	else
   119 		n.setLeft(r2.left() );
   120 		
   121 	// Set top border		
   122 	if (r1.top() <= r2.top() )
   123 		n.setTop(r1.top() );
   124 	else
   125 		n.setTop(r2.top() );
   126 		
   127 	// Set right border
   128 	if (r1.right() <= r2.right() )
   129 		n.setRight(r2.right() );
   130 	else
   131 		n.setRight(r1.right() );
   132 		
   133 	// Set bottom 
   134 	if (r1.bottom() <= r2.bottom() )
   135 		n.setBottom(r2.bottom() );
   136 	else
   137 		n.setBottom(r1.bottom() );
   138 	return n;
   139 }
   140 
   141 QSize MapObj::getSize()
   142 {
   143     return bbox.size();
   144 }
   145 
   146 
   147 bool MapObj::isVisibleObj()
   148 {
   149     return visible;
   150 }
   151 
   152 void MapObj::setVisibility(bool v)
   153 {
   154     visible=v;
   155 }
   156