mapobj.cpp
author insilmaril
Tue, 06 Jun 2006 14:58:11 +0000
branchqt4-port
changeset 2 608f976aa7bb
parent 0 7a96bd401351
permissions -rw-r--r--
First compilation on QT4
     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 (Q3Canvas* 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 Q3Canvas* 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 QPoint MapObj::getAbsPos() 
    74 {
    75     return absPos;
    76 }
    77 
    78 QString MapObj::getPos()
    79 {
    80 	return qpointToString(absPos);
    81 }
    82 
    83 void MapObj::move (double x, double y) 
    84 {
    85     int xi=static_cast <int> (x);
    86     int yi=static_cast <int> (y);
    87     absPos.setX( xi);
    88     absPos.setY( yi);
    89     bbox.moveTopLeft(QPoint(xi,yi));
    90     clickBox.moveTopLeft(QPoint(xi,yi));
    91 }
    92 
    93 void MapObj::moveBy (double x, double y) 
    94 {
    95 	int ix=(int)x;
    96 	int iy=(int)y;
    97     MapObj::move (x+absPos.x(),y+absPos.y() );
    98 	bbox.moveBy (ix,iy);
    99 	clickBox.moveBy (ix,iy);
   100 }
   101 
   102 bool MapObj::inBox(const QPoint &p)
   103 {
   104     if (p.x() >= clickBox.left() && p.x() <= clickBox.right()  
   105 	&& p.y() <= clickBox.bottom() && p.y() >= clickBox.top() )
   106 		return true;
   107     return false;	
   108 }
   109 
   110 QRect MapObj::getBBox()
   111 {
   112     return bbox;
   113 }
   114 
   115 QRect MapObj::addBBox(QRect r1, QRect r2)
   116 {	
   117 	// Find smallest QRect containing given rectangles
   118 
   119 	QRect n;
   120 	// Set left border
   121 	if (r1.left() <= r2.left() )
   122 		n.setLeft(r1.left() );
   123 	else
   124 		n.setLeft(r2.left() );
   125 		
   126 	// Set top border		
   127 	if (r1.top() <= r2.top() )
   128 		n.setTop(r1.top() );
   129 	else
   130 		n.setTop(r2.top() );
   131 		
   132 	// Set right border
   133 	if (r1.right() <= r2.right() )
   134 		n.setRight(r2.right() );
   135 	else
   136 		n.setRight(r1.right() );
   137 		
   138 	// Set bottom 
   139 	if (r1.bottom() <= r2.bottom() )
   140 		n.setBottom(r2.bottom() );
   141 	else
   142 		n.setBottom(r1.bottom() );
   143 	return n;
   144 }
   145 
   146 QSize MapObj::getSize()
   147 {
   148     return bbox.size();
   149 }
   150 
   151 
   152 bool MapObj::isVisibleObj()
   153 {
   154     return visible;
   155 }
   156 
   157 void MapObj::setVisibility(bool v)
   158 {
   159     visible=v;
   160 }
   161