vymmodel.cpp
author insilmaril
Tue, 15 Jan 2008 10:54:41 +0000
changeset 650 65c5a0c28d20
child 652 700553af9ca5
permissions -rw-r--r--
added some missing files
     1 #include <QApplication>
     2 
     3 #include "geometry.h"		// for addBBox
     4 #include "vymmodel.h"
     5 
     6 VymModel::VymModel() 
     7 {
     8 //    cout << "Const VymModel\n";
     9 }
    10 
    11 
    12 VymModel::~VymModel() 
    13 {
    14 //    cout << "Destr VymModel\n";
    15 }	
    16 
    17 void VymModel::clear() 
    18 {
    19 	for (int i=0; i<mapCenters.count(); i++)
    20 		mapCenters.at(i)->clear();
    21 	mapCenters.clear();	
    22 }
    23 
    24 void VymModel::init () 
    25 {
    26 }
    27 
    28 void VymModel::setMapEditor(MapEditor *me)
    29 {
    30 	mapEditor=me;
    31 	for (int i=0; i<mapCenters.count(); i++)
    32 		mapCenters.at(i)->setMapEditor(mapEditor);
    33 }
    34 
    35 MapEditor* VymModel::getMapEditor()
    36 {
    37 	return mapEditor;
    38 }
    39 
    40 void VymModel::setVersion (const QString &s)
    41 {
    42 	version=s;
    43 }
    44 
    45 void VymModel::setAuthor (const QString &s)
    46 {
    47 	author=s;
    48 }
    49 
    50 QString VymModel::getAuthor()
    51 {
    52 	return author;
    53 }
    54 
    55 void VymModel::setComment (const QString &s)
    56 {
    57 	comment=s;
    58 }
    59 
    60 QString VymModel::getComment ()
    61 {
    62 	return comment;
    63 }
    64 
    65 QString VymModel::getDate ()
    66 {
    67 	return QDate::currentDate().toString ("yyyy-MM-dd");
    68 }
    69 
    70 void VymModel::setScene (QGraphicsScene *s)
    71 {
    72 	mapScene=s;
    73     init();	// Here we have a mapScene set, 
    74 			// which is (still) needed to create MapCenters
    75 }
    76 
    77 QGraphicsScene* VymModel::getScene ()
    78 {
    79 	return mapScene;
    80 }
    81 
    82 MapCenterObj* VymModel::addMapCenter()
    83 {
    84 	MapCenterObj *mapCenter = new MapCenterObj(mapScene);
    85     mapCenter->setVisibility (true);
    86 	mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
    87 	mapCenter->setMapEditor(mapEditor);		//FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
    88 	mapCenters.append(mapCenter);
    89 	return mapCenter;
    90 }
    91 
    92 MapCenterObj *VymModel::removeMapCenter(MapCenterObj* mco)
    93 {
    94 	int i=mapCenters.indexOf (mco);
    95 	if (i>=0)
    96 	{
    97 		mapCenters.removeAt (i);
    98 		delete (mco);
    99 		if (i>0) return mapCenters.at(i-1);	// Return previous MCO
   100 	}
   101 	return NULL;
   102 }
   103 
   104 // Only as long as we dont have Model/View yet
   105 LinkableMapObj* VymModel::getSelection()
   106 {
   107 	return mapEditor->getSelection();
   108 }
   109 BranchObj* VymModel::getSelectedBranch()
   110 {
   111 	return mapEditor->getSelectedBranch();
   112 }
   113 
   114 bool VymModel::select (const QString &s)
   115 {
   116 	LinkableMapObj *lmo=model->findObjBySelect(s);
   117 
   118 /*
   119 	// Finally select the found object
   120 	if (lmo)
   121 	{
   122 		xelection.unselect();
   123 		xelection.select(lmo);
   124 		xelection.update();
   125 		ensureSelectionVisible();
   126 		sendSelection ();
   127 		return true;
   128 	} 
   129 	return false;
   130 */
   131 }
   132 
   133 QString VymModel::getSelectString (LinkableMapObj *lmo)
   134 {
   135 	QString s;
   136 	if (!lmo) return s;
   137 	if (typeid(*lmo)==typeid(BranchObj) ||
   138 		typeid(*lmo)==typeid(MapCenterObj) )
   139 	{	
   140 		LinkableMapObj *par=lmo->getParObj();
   141 		if (par)
   142 		{
   143 			if (lmo->getDepth() ==1)
   144 				// Mainbranch, return 
   145 				s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   146 			else	
   147 				// Branch, call myself recursively
   148 				s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   149 		} else
   150 		{
   151 			// MapCenter
   152 			int i=mapCenters.indexOf ((MapCenterObj*)lmo);
   153 			if (i>=0) s=QString("mc:%1").arg(i);
   154 		}	
   155 	}	
   156 	return s;
   157 
   158 }
   159 
   160 /* FIXME copied from MCO, still needed?
   161 void VymModel::move (double x, double y)
   162 {
   163 	BranchObj::move(x,y);
   164 }
   165 
   166 void VymModel::moveBy (double x, double y)
   167 {
   168 	BranchObj::moveBy(x,y);
   169 }
   170 
   171 void VymModel::moveAll (double x, double y)
   172 {
   173 	// Get rel. position
   174 	double dx=x-absPos.x();
   175 	double dy=y-absPos.y();
   176 
   177 	// Move myself and branches
   178 	moveAllBy (dx,dy);
   179 }
   180 
   181 void VymModel::moveAllBy (double dx, double dy)
   182 {
   183 	// Move myself and childs
   184 	BranchObj::moveBy(dx,dy);
   185 }
   186 */
   187 BranchObj* VymModel::first()
   188 {
   189 	if (mapCenters.count()>0) 
   190 		return mapCenters.first();
   191 	else	
   192 		return NULL;
   193 }
   194 	
   195 BranchObj* VymModel::next(BranchObj *bo_start)
   196 {
   197 	BranchObj *rbo;
   198 	BranchObj *bo=bo_start;
   199 	if (bo)
   200 	{
   201 		rbo=bo->next();
   202 		if (rbo) return rbo;
   203 
   204 		// Try to find MapCenter of bo
   205 		while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
   206 
   207 
   208 		// Try to find next MapCenter
   209 		int i=mapCenters.indexOf ((MapCenterObj*)bo);
   210 		if (i+1 > mapCenters.count() || i<0) return NULL;
   211 		if (mapCenters.at(i)!=bo_start)
   212 			return mapCenters.at(i);
   213 	} 
   214 	return NULL;
   215 
   216 }
   217 	
   218 	/* FIXME copied from MCO, still needed?
   219 void VymModel::updateLink()
   220 {
   221 	// set childPos to middle of MapCenterObj
   222 	childPos.setX( clickBox.topLeft().x() + (int)(clickBox.width())/2 );
   223 	childPos.setY( clickBox.topLeft().y() + (int)(clickBox.height())/2 );
   224 	parPos=childPos;		
   225 	for (int i=0; i<branch.size(); ++i)
   226 		branch.at(i)->updateLink();
   227 }
   228 
   229 */
   230 void VymModel::updateRelPositions()
   231 {
   232 	for (int i=0; i<mapCenters.count(); i++)
   233 		mapCenters.at(i)->updateRelPositions();
   234 }
   235 
   236 void VymModel::reposition()
   237 {
   238 	for (int i=0;i<mapCenters.count(); i++)
   239 		mapCenters.at(i)->reposition();	//	for positioning heading
   240 }
   241 
   242 void VymModel::setHideTmp (HideTmpMode mode)
   243 {
   244 	for (int i=0;i<mapCenters.count(); i++)
   245 		mapCenters.at(i)->setHideTmp (mode);	
   246 }
   247 
   248 QRectF VymModel::getTotalBBox()
   249 {
   250 	QRectF r;
   251 	for (int i=0;i<mapCenters.count(); i++)
   252 		r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
   253 	return r;	
   254 }
   255 
   256 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
   257 {
   258 	LinkableMapObj *lmo;
   259 
   260 	for (int i=0;i<mapCenters.count(); i++)
   261 	{
   262 		lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
   263 		if (lmo) return lmo;
   264 	}
   265 	return NULL;
   266 }
   267 
   268 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
   269 {
   270 	LinkableMapObj *lmo;
   271 	if (!s.isEmpty() )
   272 	{
   273 		QString part;
   274 		QString typ;
   275 		QString num;
   276 		part=s.section(",",0,0);
   277 		typ=part.left (3);
   278 		num=part.right(part.length() - 3);
   279 		if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
   280 			return mapCenters.at(num.toInt() );
   281 	}		
   282 
   283 	for (int i=0; i<mapCenters.count(); i++)
   284 	{
   285 		lmo=mapCenters.at(i)->findObjBySelect(s);
   286 		if (lmo) return lmo;
   287 	}	
   288 	return NULL;
   289 }
   290 
   291 LinkableMapObj* VymModel::findID (const QString &s)
   292 {
   293 	LinkableMapObj *lmo;
   294 	for (int i=0; i<mapCenters.count(); i++)
   295 	{
   296 		lmo=mapCenters.at(i)->findID (s);
   297 		if (lmo) return lmo;
   298 	}	
   299 	return NULL;
   300 }
   301 
   302 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
   303 {
   304     QString s;
   305 
   306 	for (int i=0; i<mapCenters.count(); i++)
   307 		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
   308     return s;
   309 }
   310 
   311