vymmodel.cpp
author insilmaril
Fri, 01 Feb 2008 15:28:35 +0000
changeset 661 6a5e2c27f8a4
parent 660 d0e047b8d412
child 665 a7db20d79c32
permissions -rw-r--r--
Added brasilian translation by Amadeu Júnior
     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 	addMapCenter();
    27 }
    28 
    29 void VymModel::setMapEditor(MapEditor *me)
    30 {
    31 	mapEditor=me;
    32 	for (int i=0; i<mapCenters.count(); i++)
    33 		mapCenters.at(i)->setMapEditor(mapEditor);
    34 }
    35 
    36 MapEditor* VymModel::getMapEditor()
    37 {
    38 	return mapEditor;
    39 }
    40 
    41 void VymModel::setVersion (const QString &s)
    42 {
    43 	version=s;
    44 }
    45 
    46 void VymModel::setAuthor (const QString &s)
    47 {
    48 	author=s;
    49 }
    50 
    51 QString VymModel::getAuthor()
    52 {
    53 	return author;
    54 }
    55 
    56 void VymModel::setComment (const QString &s)
    57 {
    58 	comment=s;
    59 }
    60 
    61 QString VymModel::getComment ()
    62 {
    63 	return comment;
    64 }
    65 
    66 QString VymModel::getDate ()
    67 {
    68 	return QDate::currentDate().toString ("yyyy-MM-dd");
    69 }
    70 
    71 void VymModel::setScene (QGraphicsScene *s)
    72 {
    73 	mapScene=s;
    74     init();	// Here we have a mapScene set, 
    75 			// which is (still) needed to create MapCenters
    76 }
    77 
    78 QGraphicsScene* VymModel::getScene ()
    79 {
    80 	return mapScene;
    81 }
    82 
    83 MapCenterObj* VymModel::addMapCenter()
    84 {
    85 	MapCenterObj *mapCenter = new MapCenterObj(mapScene);
    86     mapCenter->setVisibility (true);
    87 	mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
    88 	mapCenter->setMapEditor(mapEditor);		//FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
    89 	mapCenters.append(mapCenter);
    90 	return mapCenter;
    91 }
    92 
    93 MapCenterObj *VymModel::removeMapCenter(MapCenterObj* mco)
    94 {
    95 	int i=mapCenters.indexOf (mco);
    96 	if (i>=0)
    97 	{
    98 		mapCenters.removeAt (i);
    99 		delete (mco);
   100 		if (i>0) return mapCenters.at(i-1);	// Return previous MCO
   101 	}
   102 	return NULL;
   103 }
   104 
   105 BranchObj* VymModel::first()
   106 {
   107 	if (mapCenters.count()>0) 
   108 		return mapCenters.first();
   109 	else	
   110 		return NULL;
   111 }
   112 	
   113 BranchObj* VymModel::next(BranchObj *bo_start)
   114 {
   115 	BranchObj *rbo;
   116 	BranchObj *bo=bo_start;
   117 	if (bo)
   118 	{
   119 		rbo=bo->next();
   120 		if (rbo) return rbo;
   121 
   122 		// Try to find MapCenter of bo
   123 		while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
   124 
   125 
   126 		// Try to find next MapCenter
   127 		int i=mapCenters.indexOf ((MapCenterObj*)bo);
   128 		if (i+1 > mapCenters.count() || i<0) return NULL;
   129 		if (mapCenters.at(i)!=bo_start)
   130 			return mapCenters.at(i);
   131 	} 
   132 	return NULL;
   133 }
   134 
   135 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
   136 {
   137 	LinkableMapObj *lmo;
   138 
   139 	for (int i=0;i<mapCenters.count(); i++)
   140 	{
   141 		lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
   142 		if (lmo) return lmo;
   143 	}
   144 	return NULL;
   145 }
   146 
   147 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
   148 {
   149 	LinkableMapObj *lmo;
   150 	if (!s.isEmpty() )
   151 	{
   152 		QString part;
   153 		QString typ;
   154 		QString num;
   155 		part=s.section(",",0,0);
   156 		typ=part.left (2);
   157 		num=part.right(part.length() - 3);
   158 		if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
   159 			return mapCenters.at(num.toInt() );
   160 	}		
   161 
   162 	for (int i=0; i<mapCenters.count(); i++)
   163 	{
   164 		lmo=mapCenters.at(i)->findObjBySelect(s);
   165 		if (lmo) return lmo;
   166 	}	
   167 	return NULL;
   168 }
   169 
   170 LinkableMapObj* VymModel::findID (const QString &s)
   171 {
   172 	LinkableMapObj *lmo;
   173 	for (int i=0; i<mapCenters.count(); i++)
   174 	{
   175 		lmo=mapCenters.at(i)->findID (s);
   176 		if (lmo) return lmo;
   177 	}	
   178 	return NULL;
   179 }
   180 
   181 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
   182 {
   183     QString s;
   184 
   185 	for (int i=0; i<mapCenters.count(); i++)
   186 		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
   187     return s;
   188 }
   189 
   190 
   191 //////////////////////////////////////////////
   192 // View related
   193 //////////////////////////////////////////////
   194 
   195 	/* FIXME copied from MCO, still needed?
   196 void VymModel::updateLink()
   197 {
   198 	// set childPos to middle of MapCenterObj
   199 	childPos.setX( clickBox.topLeft().x() + (int)(clickBox.width())/2 );
   200 	childPos.setY( clickBox.topLeft().y() + (int)(clickBox.height())/2 );
   201 	parPos=childPos;		
   202 	for (int i=0; i<branch.size(); ++i)
   203 		branch.at(i)->updateLink();
   204 }
   205 
   206 */
   207 void VymModel::updateRelPositions()
   208 {
   209 	for (int i=0; i<mapCenters.count(); i++)
   210 		mapCenters.at(i)->updateRelPositions();
   211 }
   212 
   213 void VymModel::reposition()
   214 {
   215 	for (int i=0;i<mapCenters.count(); i++)
   216 		mapCenters.at(i)->reposition();	//	for positioning heading
   217 }
   218 
   219 
   220 
   221 //////////////////////////////////////////////
   222 // Selection related
   223 //////////////////////////////////////////////
   224 
   225 
   226 // Only as long as we dont have Model/View yet
   227 LinkableMapObj* VymModel::getSelection()
   228 {
   229 	return mapEditor->getSelection();
   230 }
   231 BranchObj* VymModel::getSelectedBranch()
   232 {
   233 	return mapEditor->getSelectedBranch();
   234 }
   235 
   236 
   237 bool VymModel::select (const QString &s)
   238 {
   239 	return mapEditor->select (s);
   240 }
   241 
   242 QString VymModel::getSelectString (LinkableMapObj *lmo)
   243 {
   244 	QString s;
   245 	if (!lmo) return s;
   246 	if (typeid(*lmo)==typeid(BranchObj) ||
   247 		typeid(*lmo)==typeid(MapCenterObj) )
   248 	{	
   249 		LinkableMapObj *par=lmo->getParObj();
   250 		if (par)
   251 		{
   252 			if (lmo->getDepth() ==1)
   253 				// Mainbranch, return 
   254 				s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   255 			else	
   256 				// Branch, call myself recursively
   257 				s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   258 		} else
   259 		{
   260 			// MapCenter
   261 			int i=mapCenters.indexOf ((MapCenterObj*)lmo);
   262 			if (i>=0) s=QString("mc:%1").arg(i);
   263 		}	
   264 	}	
   265 	return s;
   266 
   267 }
   268 
   269 	
   270 void VymModel::setHideTmp (HideTmpMode mode)
   271 {
   272 	for (int i=0;i<mapCenters.count(); i++)
   273 		mapCenters.at(i)->setHideTmp (mode);	
   274 }
   275 
   276 QRectF VymModel::getTotalBBox()
   277 {
   278 	QRectF r;
   279 	for (int i=0;i<mapCenters.count(); i++)
   280 		r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
   281 	return r;	
   282 }
   283