vymmodel.cpp
author insilmaril
Wed, 16 Jan 2008 15:45:18 +0000
changeset 653 8be6ac05960b
parent 652 700553af9ca5
child 660 d0e047b8d412
permissions -rw-r--r--
Fixed missing MapCenter
     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 BranchObj* VymModel::first()
   105 {
   106 	if (mapCenters.count()>0) 
   107 		return mapCenters.first();
   108 	else	
   109 		return NULL;
   110 }
   111 	
   112 BranchObj* VymModel::next(BranchObj *bo_start)
   113 {
   114 	BranchObj *rbo;
   115 	BranchObj *bo=bo_start;
   116 	if (bo)
   117 	{
   118 		rbo=bo->next();
   119 		if (rbo) return rbo;
   120 
   121 		// Try to find MapCenter of bo
   122 		while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
   123 
   124 
   125 		// Try to find next MapCenter
   126 		int i=mapCenters.indexOf ((MapCenterObj*)bo);
   127 		if (i+1 > mapCenters.count() || i<0) return NULL;
   128 		if (mapCenters.at(i)!=bo_start)
   129 			return mapCenters.at(i);
   130 	} 
   131 	return NULL;
   132 }
   133 
   134 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
   135 {
   136 	LinkableMapObj *lmo;
   137 
   138 	for (int i=0;i<mapCenters.count(); i++)
   139 	{
   140 		lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
   141 		if (lmo) return lmo;
   142 	}
   143 	return NULL;
   144 }
   145 
   146 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
   147 {
   148 	LinkableMapObj *lmo;
   149 	if (!s.isEmpty() )
   150 	{
   151 		QString part;
   152 		QString typ;
   153 		QString num;
   154 		part=s.section(",",0,0);
   155 		typ=part.left (2);
   156 		num=part.right(part.length() - 3);
   157 		if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
   158 			return mapCenters.at(num.toInt() );
   159 	}		
   160 
   161 	for (int i=0; i<mapCenters.count(); i++)
   162 	{
   163 		lmo=mapCenters.at(i)->findObjBySelect(s);
   164 		if (lmo) return lmo;
   165 	}	
   166 	return NULL;
   167 }
   168 
   169 LinkableMapObj* VymModel::findID (const QString &s)
   170 {
   171 	LinkableMapObj *lmo;
   172 	for (int i=0; i<mapCenters.count(); i++)
   173 	{
   174 		lmo=mapCenters.at(i)->findID (s);
   175 		if (lmo) return lmo;
   176 	}	
   177 	return NULL;
   178 }
   179 
   180 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
   181 {
   182     QString s;
   183 
   184 	for (int i=0; i<mapCenters.count(); i++)
   185 		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
   186     return s;
   187 }
   188 
   189 
   190 //////////////////////////////////////////////
   191 // View related
   192 //////////////////////////////////////////////
   193 
   194 	/* FIXME copied from MCO, still needed?
   195 void VymModel::updateLink()
   196 {
   197 	// set childPos to middle of MapCenterObj
   198 	childPos.setX( clickBox.topLeft().x() + (int)(clickBox.width())/2 );
   199 	childPos.setY( clickBox.topLeft().y() + (int)(clickBox.height())/2 );
   200 	parPos=childPos;		
   201 	for (int i=0; i<branch.size(); ++i)
   202 		branch.at(i)->updateLink();
   203 }
   204 
   205 */
   206 void VymModel::updateRelPositions()
   207 {
   208 	for (int i=0; i<mapCenters.count(); i++)
   209 		mapCenters.at(i)->updateRelPositions();
   210 }
   211 
   212 void VymModel::reposition()
   213 {
   214 	for (int i=0;i<mapCenters.count(); i++)
   215 		mapCenters.at(i)->reposition();	//	for positioning heading
   216 }
   217 
   218 
   219 
   220 //////////////////////////////////////////////
   221 // Selection related
   222 //////////////////////////////////////////////
   223 
   224 
   225 // Only as long as we dont have Model/View yet
   226 LinkableMapObj* VymModel::getSelection()
   227 {
   228 	return mapEditor->getSelection();
   229 }
   230 BranchObj* VymModel::getSelectedBranch()
   231 {
   232 	return mapEditor->getSelectedBranch();
   233 }
   234 
   235 
   236 bool VymModel::select (const QString &s)
   237 {
   238 	return mapEditor->select (s);
   239 }
   240 
   241 QString VymModel::getSelectString (LinkableMapObj *lmo)
   242 {
   243 	QString s;
   244 	if (!lmo) return s;
   245 	if (typeid(*lmo)==typeid(BranchObj) ||
   246 		typeid(*lmo)==typeid(MapCenterObj) )
   247 	{	
   248 		LinkableMapObj *par=lmo->getParObj();
   249 		if (par)
   250 		{
   251 			if (lmo->getDepth() ==1)
   252 				// Mainbranch, return 
   253 				s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   254 			else	
   255 				// Branch, call myself recursively
   256 				s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   257 		} else
   258 		{
   259 			// MapCenter
   260 			int i=mapCenters.indexOf ((MapCenterObj*)lmo);
   261 			if (i>=0) s=QString("mc:%1").arg(i);
   262 		}	
   263 	}	
   264 	return s;
   265 
   266 }
   267 
   268 	
   269 void VymModel::setHideTmp (HideTmpMode mode)
   270 {
   271 	for (int i=0;i<mapCenters.count(); i++)
   272 		mapCenters.at(i)->setHideTmp (mode);	
   273 }
   274 
   275 QRectF VymModel::getTotalBBox()
   276 {
   277 	QRectF r;
   278 	for (int i=0;i<mapCenters.count(); i++)
   279 		r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
   280 	return r;	
   281 }
   282