1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/vymmodel.cpp Tue Jan 15 10:54:41 2008 +0000
1.3 @@ -0,0 +1,311 @@
1.4 +#include <QApplication>
1.5 +
1.6 +#include "geometry.h" // for addBBox
1.7 +#include "vymmodel.h"
1.8 +
1.9 +VymModel::VymModel()
1.10 +{
1.11 +// cout << "Const VymModel\n";
1.12 +}
1.13 +
1.14 +
1.15 +VymModel::~VymModel()
1.16 +{
1.17 +// cout << "Destr VymModel\n";
1.18 +}
1.19 +
1.20 +void VymModel::clear()
1.21 +{
1.22 + for (int i=0; i<mapCenters.count(); i++)
1.23 + mapCenters.at(i)->clear();
1.24 + mapCenters.clear();
1.25 +}
1.26 +
1.27 +void VymModel::init ()
1.28 +{
1.29 +}
1.30 +
1.31 +void VymModel::setMapEditor(MapEditor *me)
1.32 +{
1.33 + mapEditor=me;
1.34 + for (int i=0; i<mapCenters.count(); i++)
1.35 + mapCenters.at(i)->setMapEditor(mapEditor);
1.36 +}
1.37 +
1.38 +MapEditor* VymModel::getMapEditor()
1.39 +{
1.40 + return mapEditor;
1.41 +}
1.42 +
1.43 +void VymModel::setVersion (const QString &s)
1.44 +{
1.45 + version=s;
1.46 +}
1.47 +
1.48 +void VymModel::setAuthor (const QString &s)
1.49 +{
1.50 + author=s;
1.51 +}
1.52 +
1.53 +QString VymModel::getAuthor()
1.54 +{
1.55 + return author;
1.56 +}
1.57 +
1.58 +void VymModel::setComment (const QString &s)
1.59 +{
1.60 + comment=s;
1.61 +}
1.62 +
1.63 +QString VymModel::getComment ()
1.64 +{
1.65 + return comment;
1.66 +}
1.67 +
1.68 +QString VymModel::getDate ()
1.69 +{
1.70 + return QDate::currentDate().toString ("yyyy-MM-dd");
1.71 +}
1.72 +
1.73 +void VymModel::setScene (QGraphicsScene *s)
1.74 +{
1.75 + mapScene=s;
1.76 + init(); // Here we have a mapScene set,
1.77 + // which is (still) needed to create MapCenters
1.78 +}
1.79 +
1.80 +QGraphicsScene* VymModel::getScene ()
1.81 +{
1.82 + return mapScene;
1.83 +}
1.84 +
1.85 +MapCenterObj* VymModel::addMapCenter()
1.86 +{
1.87 + MapCenterObj *mapCenter = new MapCenterObj(mapScene);
1.88 + mapCenter->setVisibility (true);
1.89 + mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
1.90 + mapCenter->setMapEditor(mapEditor); //FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
1.91 + mapCenters.append(mapCenter);
1.92 + return mapCenter;
1.93 +}
1.94 +
1.95 +MapCenterObj *VymModel::removeMapCenter(MapCenterObj* mco)
1.96 +{
1.97 + int i=mapCenters.indexOf (mco);
1.98 + if (i>=0)
1.99 + {
1.100 + mapCenters.removeAt (i);
1.101 + delete (mco);
1.102 + if (i>0) return mapCenters.at(i-1); // Return previous MCO
1.103 + }
1.104 + return NULL;
1.105 +}
1.106 +
1.107 +// Only as long as we dont have Model/View yet
1.108 +LinkableMapObj* VymModel::getSelection()
1.109 +{
1.110 + return mapEditor->getSelection();
1.111 +}
1.112 +BranchObj* VymModel::getSelectedBranch()
1.113 +{
1.114 + return mapEditor->getSelectedBranch();
1.115 +}
1.116 +
1.117 +bool VymModel::select (const QString &s)
1.118 +{
1.119 + LinkableMapObj *lmo=model->findObjBySelect(s);
1.120 +
1.121 +/*
1.122 + // Finally select the found object
1.123 + if (lmo)
1.124 + {
1.125 + xelection.unselect();
1.126 + xelection.select(lmo);
1.127 + xelection.update();
1.128 + ensureSelectionVisible();
1.129 + sendSelection ();
1.130 + return true;
1.131 + }
1.132 + return false;
1.133 +*/
1.134 +}
1.135 +
1.136 +QString VymModel::getSelectString (LinkableMapObj *lmo)
1.137 +{
1.138 + QString s;
1.139 + if (!lmo) return s;
1.140 + if (typeid(*lmo)==typeid(BranchObj) ||
1.141 + typeid(*lmo)==typeid(MapCenterObj) )
1.142 + {
1.143 + LinkableMapObj *par=lmo->getParObj();
1.144 + if (par)
1.145 + {
1.146 + if (lmo->getDepth() ==1)
1.147 + // Mainbranch, return
1.148 + s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
1.149 + else
1.150 + // Branch, call myself recursively
1.151 + s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
1.152 + } else
1.153 + {
1.154 + // MapCenter
1.155 + int i=mapCenters.indexOf ((MapCenterObj*)lmo);
1.156 + if (i>=0) s=QString("mc:%1").arg(i);
1.157 + }
1.158 + }
1.159 + return s;
1.160 +
1.161 +}
1.162 +
1.163 +/* FIXME copied from MCO, still needed?
1.164 +void VymModel::move (double x, double y)
1.165 +{
1.166 + BranchObj::move(x,y);
1.167 +}
1.168 +
1.169 +void VymModel::moveBy (double x, double y)
1.170 +{
1.171 + BranchObj::moveBy(x,y);
1.172 +}
1.173 +
1.174 +void VymModel::moveAll (double x, double y)
1.175 +{
1.176 + // Get rel. position
1.177 + double dx=x-absPos.x();
1.178 + double dy=y-absPos.y();
1.179 +
1.180 + // Move myself and branches
1.181 + moveAllBy (dx,dy);
1.182 +}
1.183 +
1.184 +void VymModel::moveAllBy (double dx, double dy)
1.185 +{
1.186 + // Move myself and childs
1.187 + BranchObj::moveBy(dx,dy);
1.188 +}
1.189 +*/
1.190 +BranchObj* VymModel::first()
1.191 +{
1.192 + if (mapCenters.count()>0)
1.193 + return mapCenters.first();
1.194 + else
1.195 + return NULL;
1.196 +}
1.197 +
1.198 +BranchObj* VymModel::next(BranchObj *bo_start)
1.199 +{
1.200 + BranchObj *rbo;
1.201 + BranchObj *bo=bo_start;
1.202 + if (bo)
1.203 + {
1.204 + rbo=bo->next();
1.205 + if (rbo) return rbo;
1.206 +
1.207 + // Try to find MapCenter of bo
1.208 + while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
1.209 +
1.210 +
1.211 + // Try to find next MapCenter
1.212 + int i=mapCenters.indexOf ((MapCenterObj*)bo);
1.213 + if (i+1 > mapCenters.count() || i<0) return NULL;
1.214 + if (mapCenters.at(i)!=bo_start)
1.215 + return mapCenters.at(i);
1.216 + }
1.217 + return NULL;
1.218 +
1.219 +}
1.220 +
1.221 + /* FIXME copied from MCO, still needed?
1.222 +void VymModel::updateLink()
1.223 +{
1.224 + // set childPos to middle of MapCenterObj
1.225 + childPos.setX( clickBox.topLeft().x() + (int)(clickBox.width())/2 );
1.226 + childPos.setY( clickBox.topLeft().y() + (int)(clickBox.height())/2 );
1.227 + parPos=childPos;
1.228 + for (int i=0; i<branch.size(); ++i)
1.229 + branch.at(i)->updateLink();
1.230 +}
1.231 +
1.232 +*/
1.233 +void VymModel::updateRelPositions()
1.234 +{
1.235 + for (int i=0; i<mapCenters.count(); i++)
1.236 + mapCenters.at(i)->updateRelPositions();
1.237 +}
1.238 +
1.239 +void VymModel::reposition()
1.240 +{
1.241 + for (int i=0;i<mapCenters.count(); i++)
1.242 + mapCenters.at(i)->reposition(); // for positioning heading
1.243 +}
1.244 +
1.245 +void VymModel::setHideTmp (HideTmpMode mode)
1.246 +{
1.247 + for (int i=0;i<mapCenters.count(); i++)
1.248 + mapCenters.at(i)->setHideTmp (mode);
1.249 +}
1.250 +
1.251 +QRectF VymModel::getTotalBBox()
1.252 +{
1.253 + QRectF r;
1.254 + for (int i=0;i<mapCenters.count(); i++)
1.255 + r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
1.256 + return r;
1.257 +}
1.258 +
1.259 +LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
1.260 +{
1.261 + LinkableMapObj *lmo;
1.262 +
1.263 + for (int i=0;i<mapCenters.count(); i++)
1.264 + {
1.265 + lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
1.266 + if (lmo) return lmo;
1.267 + }
1.268 + return NULL;
1.269 +}
1.270 +
1.271 +LinkableMapObj* VymModel::findObjBySelect(const QString &s)
1.272 +{
1.273 + LinkableMapObj *lmo;
1.274 + if (!s.isEmpty() )
1.275 + {
1.276 + QString part;
1.277 + QString typ;
1.278 + QString num;
1.279 + part=s.section(",",0,0);
1.280 + typ=part.left (3);
1.281 + num=part.right(part.length() - 3);
1.282 + if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
1.283 + return mapCenters.at(num.toInt() );
1.284 + }
1.285 +
1.286 + for (int i=0; i<mapCenters.count(); i++)
1.287 + {
1.288 + lmo=mapCenters.at(i)->findObjBySelect(s);
1.289 + if (lmo) return lmo;
1.290 + }
1.291 + return NULL;
1.292 +}
1.293 +
1.294 +LinkableMapObj* VymModel::findID (const QString &s)
1.295 +{
1.296 + LinkableMapObj *lmo;
1.297 + for (int i=0; i<mapCenters.count(); i++)
1.298 + {
1.299 + lmo=mapCenters.at(i)->findID (s);
1.300 + if (lmo) return lmo;
1.301 + }
1.302 + return NULL;
1.303 +}
1.304 +
1.305 +QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
1.306 +{
1.307 + QString s;
1.308 +
1.309 + for (int i=0; i<mapCenters.count(); i++)
1.310 + s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
1.311 + return s;
1.312 +}
1.313 +
1.314 +