9 #include "branchitem.h"
11 #include "mainwindow.h"
13 #include "warningdialog.h"
14 #include "xlinkitem.h"
17 extern int statusbarTime;
18 extern Main *mainWindow;
19 extern QString tmpVymDir;
20 extern QString clipboardDir;
21 extern QString clipboardFile;
22 extern bool clipboardEmpty;
25 extern QMenu* branchContextMenu;
26 extern QMenu* branchAddContextMenu;
27 extern QMenu* branchRemoveContextMenu;
28 extern QMenu* branchLinksContextMenu;
29 extern QMenu* branchXLinksContextMenuEdit;
30 extern QMenu* branchXLinksContextMenuFollow;
31 extern QMenu* floatimageContextMenu;
32 extern QMenu* canvasContextMenu;
34 extern Settings settings;
35 extern QString iconPath;
37 ///////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////
39 MapEditor::MapEditor( VymModel *vm)
41 //cout << "Constructor ME "<<this<<endl;
42 mapScene= new QGraphicsScene(NULL);
43 mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
45 zoomFactor=zoomFactorTarget=1;
48 model->setScene (mapScene);
49 model->registerEditor(this);
50 model->makeDefault(); // No changes in model so far
56 // Create bitmap cursors, platform dependant
57 HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);
58 PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 );
59 CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 );
60 XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 );
62 //setFocusPolicy (Qt::StrongFocus); //FIXME-3
75 setAcceptDrops (true);
77 //model->reposition(); //FIXME-3 really still needed?
80 // Shortcuts and actions
82 a = new QAction("Select upper branch", this);
83 a->setShortcut (Qt::Key_Up );
84 a->setShortcutContext (Qt::WidgetShortcut);
86 connect( a, SIGNAL( triggered() ), this, SLOT( cursorUp() ) );
88 a = new QAction( "Select lower branch",this);
89 a->setShortcut ( Qt::Key_Down );
90 a->setShortcutContext (Qt::WidgetShortcut);
92 connect( a, SIGNAL( triggered() ), this, SLOT( cursorDown() ) );
94 a = new QAction( "Select left branch", this);
95 a->setShortcut (Qt::Key_Left );
96 // a->setShortcutContext (Qt::WindowShortcut);
97 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
99 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLeft() ) );
101 a = new QAction( "Select child branch", this);
102 a->setShortcut (Qt::Key_Right);
103 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
105 connect( a, SIGNAL( triggered() ), this, SLOT( cursorRight() ) );
107 a = new QAction( "Select first branch", this);
108 a->setShortcut (Qt::Key_Home );
109 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
111 connect( a, SIGNAL( triggered() ), this, SLOT( cursorFirst() ) );
113 a = new QAction( "Select last branch",this);
114 a->setShortcut ( Qt::Key_End );
115 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
117 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLast() ) );
119 // Action to embed LineEdit for heading in Scene
120 editingHeading=false;
121 lineEdit=new QLineEdit;
123 QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
124 pw->setZValue (Z_LINEEDIT);
126 a = new QAction( tr( "Edit heading","MapEditor" ), this);
127 a->setShortcut ( Qt::Key_Return ); //Edit heading
129 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
130 a = new QAction( tr( "Edit heading","MapEditor" ), this);
131 a->setShortcut ( Qt::Key_Enter); //Edit heading
133 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
136 selectionColor =QColor (255,255,0);
139 // Attributes //FIXME-2 testing only...
142 attrTable= new AttributeTable();
144 ad=attrTable->addKey (k,StringList);
148 sl <<"val 1"<<"val 2"<< "val 3";
149 ad->setValue (QVariant (sl));
151 //attrTable->addValue ("Key A","P 1");
152 //attrTable->addValue ("Key A","P 2");
153 //attrTable->addValue ("Key A","P 3");
154 //attrTable->addValue ("Key A","P 4");
156 ad=attrTable->addKey (k,FreeString);
159 //attrTable->addValue ("Key B","w1");
160 //attrTable->addValue ("Key B","w2");
162 k="C - UniqueString";
163 ad=attrTable->addKey (k,UniqueString);
166 //attrTable->addKey ("Key Prio");
167 //attrTable->addValue ("Key Prio","Prio 1");
168 //attrTable->addValue ("Key Prio","Prio 2");
172 MapEditor::~MapEditor()
174 //cout <<"Destructor MapEditor for "<<model->getMapName().toStdString()<<endl;
175 model->unregisterEditor(this);
178 VymModel* MapEditor::getModel()
183 QGraphicsScene * MapEditor::getScene()
188 void MapEditor::scrollTo (const QModelIndex &index) //FIXME-1 problems with locating stuff in big maps
192 LinkableMapObj* lmo=NULL;
193 TreeItem *ti= static_cast<TreeItem*>(index.internalPointer());
194 if (ti->getType()==TreeItem::Image ||ti->isBranchLikeType() )
195 lmo=((MapItem*)ti)->getLMO();
198 QRectF r=lmo->getBBox();
199 setScrollBarPosTarget (lmo->getBBox() );
204 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
206 // Code copied from Qt sources
210 qreal width = viewport()->width();
211 qreal height = viewport()->height();
212 QRectF viewRect = matrix().mapRect(rect);
214 qreal left = horizontalScrollBar()->value();
215 qreal right = left + width;
216 qreal top = verticalScrollBar()->value();
217 qreal bottom = top + height;
219 if (viewRect.left() <= left + xmargin) {
220 // need to scroll from the left
221 // if (!d->leftIndent)
222 scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
224 if (viewRect.right() >= right - xmargin) {
225 // need to scroll from the right
226 // if (!d->leftIndent)
227 scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
229 if (viewRect.top() <= top + ymargin) {
230 // need to scroll from the top
231 // if (!d->topIndent)
232 scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
234 if (viewRect.bottom() >= bottom - ymargin) {
235 // need to scroll from the bottom
236 // if (!d->topIndent)
237 scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
240 if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
241 scrollBarPosAnimation.stop();
242 scrollBarPosAnimation.setTargetObject (this);
243 scrollBarPosAnimation.setPropertyName ("scrollBarPos");
244 scrollBarPosAnimation.setDuration(1000);
245 scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
246 scrollBarPosAnimation.setStartValue(
247 QPointF (horizontalScrollBar()->value() ,
248 verticalScrollBar()->value() ) );
249 scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
250 scrollBarPosAnimation.start();
253 QPointF MapEditor::getScrollBarPosTarget()
255 return scrollBarPosTarget;
259 void MapEditor::setScrollBarPos(const QPointF &p)
262 horizontalScrollBar()->setValue(int(p.x()));
263 verticalScrollBar()->setValue(int(p.y()));
266 QPointF MapEditor::getScrollBarPos()
271 void MapEditor::setZoomFactorTarget (const qreal &zft)
273 zoomFactorTarget=zft;
274 if (zoomAnimation.state()==QtAbstractAnimation::Running)
275 zoomAnimation.stop();
276 //zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
277 zoomAnimation.setTargetObject (this);
278 zoomAnimation.setPropertyName ("zoomFactor");
279 zoomAnimation.setDuration(1000);
280 zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
281 zoomAnimation.setStartValue(zoomFactor);
282 zoomAnimation.setEndValue(zft);
283 zoomAnimation.start();
286 qreal MapEditor::getZoomFactorTarget()
288 return zoomFactorTarget;
292 void MapEditor::setZoomFactor(const qreal &zf)
295 setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
298 qreal MapEditor::getZoomFactor()
303 void MapEditor::print()
307 printer = new QPrinter;
308 printer->setColorMode (QPrinter::Color);
309 printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
310 printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
311 printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
314 QRectF totalBBox=getTotalBBox();
316 // Try to set orientation automagically
317 // Note: Interpretation of generated postscript is amibiguous, if
318 // there are problems with landscape mode, see
319 // http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
321 if (totalBBox.width()>totalBBox.height())
322 // recommend landscape
323 printer->setOrientation (QPrinter::Landscape);
325 // recommend portrait
326 printer->setOrientation (QPrinter::Portrait);
328 if ( printer->setup(this) )
329 // returns false, if printing is canceled
331 QPainter pp(printer);
333 pp.setRenderHint(QPainter::Antialiasing,true);
335 // Don't print the visualisation of selection
338 QRectF mapRect=totalBBox;
339 QGraphicsRectItem *frame=NULL;
343 // Print frame around map
344 mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10,
345 totalBBox.width()+20, totalBBox.height()+20);
346 frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
352 double paperAspect = (double)printer->width() / (double)printer->height();
353 double mapAspect = (double)mapRect.width() / (double)mapRect.height();
355 if (mapAspect>=paperAspect)
357 // Fit horizontally to paper width
358 //pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );
359 viewBottom=(int)(printer->width()/mapAspect);
362 // Fit vertically to paper height
363 //pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());
364 viewBottom=printer->height();
369 // Print footer below map
371 font.setPointSize(10);
373 QRectF footerBox(0,viewBottom,printer->width(),15);
374 // FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
375 pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
379 QRectF (0,0,printer->width(),printer->height()-15),
380 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
383 // Viewport has paper dimension
384 if (frame) delete (frame);
389 // Save settings in vymrc
390 settings.writeEntry("/mainwindow/printerName",printer->printerName());
391 settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
392 settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
396 QRectF MapEditor::getTotalBBox() //FIXME-2 needed e.g. for image export
400 for (int i=0;i<rootItem->branchCount(); i++)
401 r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
407 QPixmap MapEditor::getPixmap()
409 QRectF mapRect=getTotalBBox();
410 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
413 pp.setRenderHints(renderHints());
415 // Don't print the visualisation of selection
418 mapScene->render ( &pp,
419 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
420 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
429 void MapEditor::setAntiAlias (bool b)
431 setRenderHint(QPainter::Antialiasing,b);
434 void MapEditor::setSmoothPixmap(bool b)
436 setRenderHint(QPainter::SmoothPixmapTransform,b);
439 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
441 // Start with mapcenter, no images allowed at rootItem
443 BranchItem *bi=model->getRootItem()->getFirstBranch();
444 TreeItem *found=NULL;
447 found=bi->findMapItem (p, exclude);
448 if (found) return found;
450 bi=model->getRootItem()->getBranchNum(i);
455 AttributeTable* MapEditor::attributeTable()
460 void MapEditor::testFunction1()
462 cout << "ME::test1 selected TI="<<model->getSelectedItem()<<endl;
464 BranchObj *bo=model->getSelectedBranchObj();
467 bo->moveBy (100,100);
472 // Code copied from Qt sources
473 QRectF rect=model->getSelectedBranchObj()->getBBox();
477 qreal width = viewport()->width();
478 qreal height = viewport()->height();
479 QRectF viewRect = matrix().mapRect(rect);
481 qreal left = horizontalScrollBar()->value();
482 qreal right = left + width;
483 qreal top = verticalScrollBar()->value();
484 qreal bottom = top + height;
486 if (viewRect.left() <= left + xmargin) {
487 // need to scroll from the left
488 // if (!d->leftIndent)
489 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
491 if (viewRect.right() >= right - xmargin) {
492 // need to scroll from the right
493 // if (!d->leftIndent)
494 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
496 if (viewRect.top() <= top + ymargin) {
497 // need to scroll from the top
498 // if (!d->topIndent)
499 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
501 if (viewRect.bottom() >= bottom - ymargin) {
502 // need to scroll from the bottom
503 // if (!d->topIndent)
504 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
506 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
507 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
512 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
513 animation->setDuration(5000);
514 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
515 animation->setEasingCurve ( QtEasingCurve::OutQuint);
516 animation->setStartValue(sceneRect() );
517 animation->setEndValue(QRectF(50, 50, 1000, 1000));
522 QDialog *dia= new QDialog (this);
523 dia->setGeometry (50,50,10,10);
528 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
529 animation->setDuration(1000);
530 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
531 animation->setEasingCurve ( QtEasingCurve::OutQuint);
532 animation->setStartValue(QRect(50, 50, 10, 10));
533 animation->setEndValue(QRect(250, 250, 100, 100));
538 /* FIXME-4 Hide hidden stuff temporary, maybe add this as regular function somewhere
539 if (hidemode==HideNone)
541 setHideTmpMode (HideExport);
542 mapCenter->calcBBoxSizeWithChilds();
543 QRectF totalBBox=mapCenter->getTotalBBox();
544 QRectF mapRect=totalBBox;
545 QCanvasRectangle *frame=NULL;
547 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
549 mapRect.setRect (totalBBox.x(), totalBBox.y(),
550 totalBBox.width(), totalBBox.height());
551 frame=new QCanvasRectangle (mapRect,mapScene);
552 frame->setBrush (QColor(white));
553 frame->setPen (QColor(black));
559 setHideTmpMode (HideNone);
561 cout <<" hidemode="<<hidemode<<endl;
566 if (hidemode==HideExport)
567 setHideTmpMode (HideNone);
569 setHideTmpMode (HideExport);
574 void MapEditor::testFunction2()
576 // Create list with all bounding polygons
577 QList <LinkableMapObj*> mapobjects;
578 QList <ConvexPolygon> polys;
580 QList <Vector> vectors;
581 QList <Vector> orgpos;
582 QStringList headings; //FIXME-3 testing only
588 // Outer loop: Iterate until we no more changes in orientation
589 bool orientationChanged=true;
590 while (orientationChanged)
592 BranchItem *ri=model->getRootItem();
593 for (int i=0;i<ri->branchCount();++i)
595 bi=ri->getBranchNum (i);
596 bo=(BranchObj*)bi->getLMO();
599 mapobjects.append (bo);
600 p=bo->getBoundingPolygon();
603 vectors.append (QPointF(0,0));
604 orgpos.append (p.at(0));
605 headings.append (bi->getHeading());
607 for (int j=0;j<bi->branchCount();++j)
609 bi2=bi->getBranchNum (j);
610 bo=(BranchObj*)bi2->getLMO();
613 mapobjects.append (bo);
614 p=bo->getBoundingPolygon();
617 vectors.append (QPointF(0,0));
618 orgpos.append (p.at(0));
619 headings.append (bi2->getHeading());
624 // Iterate moving bounding polygons until we have no more collisions
629 for (int i=0; i<polys.size()-1; ++i)
631 for (int j=i+1; j<polys.size();++j)
633 if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
636 //cout << "Collision: "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<endl;
637 v=polys.at(j).centroid()-polys.at(i).centroid();
638 // Move also away if centroids are identical
641 //cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<endl;
642 v.setX (rand()%200 -100);
643 v.setY (rand()%200 -100);
648 //cout << " v="<<v<<endl;
655 for (int i=0;i<vectors.size();i++)
657 //cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
658 polys[i].translate (vectors[i]);
660 cout << "Collisions: "<<collisions<<endl;
664 // Finally move the real objects and update
665 QList <LinkableMapObj::Orientation> orients;
666 for (int i=0;i<polys.size();i++)
668 Vector v=polys[i].at(0)-orgpos[i];
669 orients.append (mapobjects[i]->getOrientation());
670 mapobjects[i]->moveBy(v.x(),v.y() );
671 mapobjects[i]->setRelPos();
674 orientationChanged=false;
675 for (int i=0;i<polys.size();i++)
676 if (orients[i]!=mapobjects[i]->getOrientation())
678 orientationChanged=true;
681 cout << "Final: orientChanged="<<orientationChanged<<endl;
683 //orientationChanged=false;
684 } // loop if orientation has changed
686 model->emitSelectionChanged();
689 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
694 if (i>0) return bi->parent()->getBranchNum(i-1);
699 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
703 int dz=selbi->depth(); // original depth
705 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
710 // Look for branch with same parent but directly above
712 bi=getBranchDirectBelow(selbi);
714 bi=getBranchDirectAbove (selbi);
717 // direct predecessor
720 // Go towards center and look for predecessor
721 while (selbi->depth()>0)
723 selbi=(BranchItem*)(selbi->parent());
724 if (selbi->depth()==1 && invert)
725 bi=getBranchDirectBelow (selbi);
727 bi=getBranchDirectAbove (selbi);
732 while (selbi->depth()<dz)
734 // try to get back to original depth dz
735 bi=selbi->getLastBranch();
749 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
754 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
759 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
764 int dz=selbi->depth(); // original depth
766 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
770 // Look for branch with same parent but directly below
772 bi=getBranchDirectAbove (selbi);
774 bi=getBranchDirectBelow (selbi);
780 // Go towards center and look for neighbour
781 while (selbi->depth()>0)
783 selbi=(BranchItem*)(selbi->parent());
784 if (selbi->depth()==1 && invert)
785 bi=getBranchDirectAbove (selbi);
787 bi=getBranchDirectBelow (selbi);
792 while (selbi->depth()<dz)
794 // try to get back to original depth dz
795 bi=selbi->getFirstBranch();
809 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
814 // Special case: use alternative selection index
815 return bi->getLastSelectedBranchAlt();
816 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
818 return (BranchItem*)(bi->parent());
821 if (bi->getType()== TreeItem::Branch )
822 return bi->getLastSelectedBranch();
827 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
831 if (bi->depth()==0) return bi->getLastSelectedBranch();
832 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
834 return (BranchItem*)(bi->parent());
837 if (bi->getType()== TreeItem::Branch )
838 return (BranchItem*)bi->getLastSelectedBranch();
845 void MapEditor::cursorUp()
847 BranchItem *bi=model->getSelectedBranch();
848 if (bi) model->select (getBranchAbove(bi));
851 void MapEditor::cursorDown()
854 BranchItem *bi=model->getSelectedBranch();
855 if (bi) model->select (getBranchBelow(bi));
858 void MapEditor::cursorLeft()
860 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
861 if (bi) model->select (bi);
864 void MapEditor::cursorRight()
866 BranchItem *bi=getRightBranch (model->getSelectedBranch());
867 if (bi) model->select (bi);
870 void MapEditor::cursorFirst()
872 model->selectFirstBranch();
875 void MapEditor::cursorLast()
877 model->selectLastBranch();
881 void MapEditor::editHeading()
885 editHeadingFinished();
888 BranchObj *bo=model->getSelectedBranchObj();
889 BranchItem *bi=model->getSelectedBranch();
892 model->setSelectionBlocked(true);
894 lineEdit->setText (bi->getHeading());
895 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
896 lineEdit->setGeometry(p.x(),p.y(),230,25);
897 lineEdit->selectAll();
899 lineEdit->setFocus();
900 lineEdit->grabKeyboard();
905 void MapEditor::editHeadingFinished()
907 editingHeading=false;
908 lineEdit->releaseKeyboard();
909 model->setHeading (lineEdit->text() );
910 model->setSelectionBlocked(false);
913 // Maybe reselect previous branch
914 mainWindow->editHeadingFinished (model);
918 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
920 // Lineedits are already closed by preceding
921 // mouseEvent, we don't need to close here.
923 QPointF p = mapToScene(e->pos());
924 TreeItem *ti=findMapItem (p, NULL);
925 LinkableMapObj* lmo=NULL;
926 if (ti) lmo=((MapItem*)ti)->getLMO();
929 { // MapObj was found
930 if (model->getSelectedLMO() != lmo)
936 if (model->getSelectedBranchObj() )
938 // Context Menu on branch or mapcenter
939 branchContextMenu->popup(e->globalPos() );
942 if (model->getSelectedImage() )
944 // Context Menu on floatimage
945 floatimageContextMenu->popup(e->globalPos() );
949 { // No MapObj found, we are on the Canvas itself
950 // Context Menu on scene
952 // Open context menu synchronously to position new mapcenter
953 model->setContextPos (p);
954 canvasContextMenu->exec(e->globalPos() );
955 model->unsetContextPos ();
960 void MapEditor::keyPressEvent(QKeyEvent* e)
962 if (e->modifiers() & Qt::ControlModifier)
964 switch (mainWindow->getModMode())
966 case Main::ModModeColor:
967 setCursor (PickColorCursor);
969 case Main::ModModeCopy:
970 setCursor (CopyCursor);
972 case Main::ModModeXLink:
973 setCursor (XLinkCursor);
976 setCursor (Qt::ArrowCursor);
982 void MapEditor::keyReleaseEvent(QKeyEvent* e)
984 if (!(e->modifiers() & Qt::ControlModifier))
985 setCursor (Qt::ArrowCursor);
988 void MapEditor::mousePressEvent(QMouseEvent* e)
990 // Ignore right clicks, these will go to context menus
991 if (e->button() == Qt::RightButton )
997 //Ignore clicks while editing heading
998 if (model->isSelectionBlocked() )
1004 QPointF p = mapToScene(e->pos());
1005 TreeItem *ti=findMapItem (p, NULL);
1006 LinkableMapObj* lmo=NULL;
1007 if (ti) lmo=((MapItem*)ti)->getLMO();
1012 //Take care of system flags _or_ modifier modes
1014 if (lmo && ti->isBranchLikeType() )
1016 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
1017 if (!foname.isEmpty())
1019 // systemFlag clicked
1020 model->select (lmo);
1021 if (foname=="system-url")
1023 if (e->state() & Qt::ControlModifier)
1024 mainWindow->editOpenURLTab();
1026 mainWindow->editOpenURL();
1028 else if (foname=="system-vymLink")
1030 mainWindow->editOpenVymLink();
1031 // tabWidget may change, better return now
1032 // before segfaulting...
1033 } else if (foname=="note")
1034 mainWindow->windowToggleNoteEditor();
1035 else if (foname=="hideInExport")
1036 model->toggleHideExport();
1037 // FIXME-3 needed? xelection.update();
1041 // No system flag clicked, take care of modmodes (CTRL-Click)
1042 if (e->state() & Qt::ControlModifier)
1044 if (mainWindow->getModMode()==Main::ModModeColor)
1047 setCursor (PickColorCursor);
1050 if (mainWindow->getModMode()==Main::ModModeXLink)
1052 BranchItem *bi_begin=model->getSelectedBranch();
1056 tmpXLink=model->createXLink(bi_begin,true);
1057 tmpXLink->setColor(model->getMapDefXLinkColor());
1058 tmpXLink->setWidth(model->getMapDefXLinkWidth());
1059 tmpXLink->setEnd (p);
1060 tmpXLink->updateXLink();
1064 } // End of modmodes
1069 cout << "ME::mouse pressed\n";
1070 cout << " lmo="<<lmo<<endl;
1071 cout << " ti="<<ti->getHeadingStd()<<endl;
1073 // Select the clicked object
1078 // Left Button Move Branches
1079 if (e->button() == Qt::LeftButton )
1081 movingObj_start.setX( p.x() - lmo->x() );
1082 movingObj_start.setY( p.y() - lmo->y() );
1083 movingObj_orgPos.setX (lmo->x() );
1084 movingObj_orgPos.setY (lmo->y() );
1086 movingObj_orgRelPos=lmo->getRelPos();
1088 // If modMode==copy, then we want to "move" the _new_ object around
1089 // then we need the offset from p to the _old_ selection, because of tmp
1090 if (mainWindow->getModMode()==Main::ModModeCopy &&
1091 e->state() & Qt::ControlModifier)
1093 BranchItem *bi=model->getSelectedBranch();
1097 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
1098 //bi->addBranch (model->getSelectedBranch());
1100 model->select(bi->getLastBranch());
1101 model->reposition();
1105 movingObj=model->getSelectedLMO();
1107 // Middle Button Toggle Scroll
1108 // (On Mac OS X this won't work, but we still have
1109 // a button in the toolbar)
1110 if (e->button() == Qt::MidButton )
1111 model->toggleScroll();
1112 // model->updateActions(); FIXME-3 needed?
1113 // FIXME-3 needed? xelection.update();
1115 { // No MapObj found, we are on the scene itself
1116 // Left Button move Pos of sceneView
1117 if (e->button() == Qt::LeftButton )
1119 movingObj=NULL; // move Content not Obj
1120 movingObj_start=e->globalPos();
1121 movingCont_start=QPointF (
1122 horizontalScrollBar()->value(),
1123 verticalScrollBar()->value());
1124 movingVec=QPointF(0,0);
1125 setCursor(HandOpenCursor);
1130 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1132 QPointF p = mapToScene(e->pos());
1133 TreeItem *seli=model->getSelectedItem();
1134 LinkableMapObj* lmosel=NULL;
1135 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1136 lmosel=((MapItem*)seli)->getLMO();
1138 // Move the selected MapObj
1139 if ( lmosel && movingObj)
1142 // reset cursor if we are moving and don't copy
1143 if (mainWindow->getModMode()!=Main::ModModeCopy)
1144 setCursor (Qt::ArrowCursor);
1146 // To avoid jumping of the sceneView, only
1147 // show selection, if not tmp linked
1148 /* FIXME-2 if (!lmosel->hasParObjTmp())
1149 model->emitShowSelection();
1152 // Now move the selection, but add relative position
1153 // (movingObj_start) where selection was chosen with
1154 // mousepointer. (This avoids flickering resp. jumping
1155 // of selection back to absPos)
1157 // Check if we could link
1158 TreeItem *ti=findMapItem (p, seli);
1159 BranchItem *dsti=NULL;
1160 LinkableMapObj* dst=NULL;
1161 if (ti && ti!=seli && ti->isBranchLikeType())
1163 dsti=(BranchItem*)ti;
1169 if (lmosel && seli->getType()==TreeItem::Image)
1171 FloatObj *fio=(FloatImageObj*)lmosel;
1172 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1174 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1175 model->emitSelectionChanged(); // position has changed
1177 // Relink float to new mapcenter or branch, if shift is pressed
1178 // Only relink, if selection really has a new parent
1179 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1181 // Also save the move which was done so far
1182 QString pold=qpointFToString(movingObj_orgRelPos);
1183 QString pnow=qpointFToString(fio->getRelPos());
1189 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1190 fio->getParObj()->requestReposition();
1191 model->reposition();
1193 model->relinkImage ((ImageItem*) seli,dsti);
1194 model->select (seli);
1195 //movingObj=lmosel; //FIXME-3
1196 //movingObj_orgRelPos=lmosel->getRelPos();
1198 model->reposition();
1201 { // selection != a FloatObj
1202 if (seli->depth()==0)
1204 lmosel->move (p-movingObj_start);
1205 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1208 v=lmosel->getAbsPos();
1209 for (int i=0; i<seli->branchCount(); ++i)
1211 seli->getBranchObjNum(i)->setRelPos();
1212 seli->getBranchObjNum(i)->setOrientation();
1217 if (seli->depth()==1)
1220 lmosel->move(p-movingObj_start);
1221 lmosel->setRelPos();
1224 // Move ordinary branch
1225 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1226 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1227 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1228 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1230 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1231 lmosel->setRelPos();
1235 // Maybe we can relink temporary?
1236 if (dsti) // FIXME-1 check if dsti is ancestor of myself!
1238 if (e->modifiers()==Qt::ControlModifier)
1240 // Special case: CTRL to link below dst
1241 lmosel->setParObjTmp (dst,p,+1);
1243 else if (e->modifiers()==Qt::ShiftModifier)
1244 lmosel->setParObjTmp (dst,p,-1);
1246 lmosel->setParObjTmp (dst,p,0);
1249 lmosel->unsetParObjTmp();
1251 // reposition subbranch
1252 lmosel->reposition();
1254 QItemSelection sel=model->getSelectionModel()->selection();
1255 updateSelection(sel,sel); // position has changed
1257 } // no FloatImageObj
1261 } // selection && moving_obj
1263 // Draw a link from one branch to another
1266 tmpXLink->setEnd (p);
1267 tmpXLink->updateXLink();
1271 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1273 QPointF p=e->globalPos();
1274 movingVec.setX(-p.x() + movingObj_start.x() );
1275 movingVec.setY(-p.y() + movingObj_start.y() );
1276 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1277 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1282 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1284 QPointF p = mapToScene(e->pos());
1285 TreeItem *seli=model->getSelectedItem();
1287 TreeItem *dsti=NULL;
1288 if (seli) dsti=findMapItem(p, seli);
1289 LinkableMapObj* dst=NULL;
1290 if (dsti && dsti->isBranchLikeType ())
1291 dst=((MapItem*)dsti)->getLMO();
1296 // Have we been picking color?
1300 setCursor (Qt::ArrowCursor);
1301 // Check if we are over another branch
1304 if (e->state() & Qt::ShiftModifier)
1305 model->colorBranch (((BranchObj*)dst)->getColor());
1307 model->colorSubtree (((BranchObj*)dst)->getColor());
1312 // Have we been drawing a link?
1316 // Check if we are over another branch
1319 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1320 tmpXLink->updateXLink();
1321 tmpXLink->activate();
1323 tmpXLink,QString("delete ()"),
1324 dsti,QString("addXLink (\"%1\",\"%2\")").arg(model->getSelectString(tmpXLink->getBegin())).arg(model->getSelectString(dsti)),
1325 QString("Add xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(dsti))
1329 model->deleteItem(tmpXLink);
1335 // Have we been moving something?
1336 if ( seli && movingObj )
1338 if (seli->getType()==TreeItem::Image)
1340 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1343 // Moved FloatObj. Maybe we need to reposition
1344 QString pold=qpointFToString(movingObj_orgRelPos);
1345 QString pnow=qpointFToString(fio->getRelPos());
1351 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1353 cout << "ME::release mouse\n";
1354 fio->getParObj()->requestReposition();
1355 model->reposition();
1359 BranchItem *bi=model->getSelectedBranch();
1360 if (bi && bi->depth()==0)
1362 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1364 QString pold=qpointFToString(movingObj_orgPos);
1365 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1372 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1376 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1377 { // A branch was moved
1378 LinkableMapObj* lmosel=NULL;
1379 lmosel=((MapItem*)seli)->getLMO();
1381 // save the position in case we link to mapcenter
1382 QPointF savePos=QPointF (lmosel->getAbsPos() );
1384 // Reset the temporary drawn link to the original one
1385 lmosel->unsetParObjTmp();
1387 // For Redo we may need to save original selection
1388 QString preSelStr=model->getSelectString(seli);
1391 if (dsti && objectMoved)
1393 // We have a destination, relink to that
1395 BranchObj* bsel=model->getSelectedBranchObj();
1397 QString preParStr=model->getSelectString (bsel->getParObj());
1398 QString preNum=QString::number (seli->num(),10);
1399 QString preDstParStr;
1402 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1404 preDstParStr=model->getSelectString (dst->getParObj());
1405 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1407 if (e->state() & Qt::ControlModifier && dst->getParObj())
1410 preDstParStr=model->getSelectString (dst->getParObj());
1411 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1414 preDstParStr=model->getSelectString(dst);
1415 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1416 if (dsti->depth()==0) bsel->move (savePos);
1420 QString postSelStr=model->getSelectString(lmosel);
1421 QString postNum=QString::number (seli->num(),10);
1423 QString undoCom="relinkTo (\""+
1424 preParStr+ "\"," + preNum +"," +
1425 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1427 QString redoCom="relinkTo (\""+
1428 preDstParStr + "\"," + postNum + "," +
1429 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1434 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1439 // No destination, undo temporary move
1441 if (seli->depth()==1)
1443 // The select string might be different _after_ moving around.
1444 // Therefor reposition and then use string of old selection, too
1445 model->reposition();
1447 QPointF rp(lmosel->getRelPos());
1448 if (rp != movingObj_orgRelPos)
1450 QString ps=qpointFToString(rp);
1452 model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1453 preSelStr, "moveRel "+ps,
1454 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1458 // Draw the original link, before selection was moved around
1459 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1461 lmosel->setRelPos(); // calc relPos first for starting point
1463 model->startAnimation(
1465 lmosel->getRelPos(),
1469 model->reposition();
1472 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1473 // Finally resize scene, if needed
1479 // maybe we moved View: set old cursor
1480 setCursor (Qt::ArrowCursor);
1484 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1486 if (debug) cout << "ME p="<<mapToScene (e->pos())<<endl;
1489 if (model->isSelectionBlocked() )
1495 if (e->button() == Qt::LeftButton )
1497 QPointF p = mapToScene(e->pos());
1498 TreeItem *ti=findMapItem (p, NULL);
1499 if (ti) { // MapObj was found
1500 // First select the MapObj than edit heading
1507 void MapEditor::resizeEvent (QResizeEvent* e)
1509 QGraphicsView::resizeEvent( e );
1512 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1514 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1515 // cerr << event->format(i) << endl;
1517 if (event->mimeData()->hasImage())
1518 event->acceptProposedAction();
1520 if (event->mimeData()->hasUrls())
1521 event->acceptProposedAction();
1524 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1528 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1533 void MapEditor::dropEvent(QDropEvent *event)
1535 BranchItem *selbi=model->getSelectedBranch();
1539 foreach (QString format,event->mimeData()->formats())
1540 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1544 if (event->mimeData()->hasImage())
1546 QVariant imageData = event->mimeData()->imageData();
1547 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1549 if (event->mimeData()->hasUrls())
1550 uris=event->mimeData()->urls();
1558 for (int i=0; i<uris.count();i++)
1560 // Workaround to avoid adding empty branches
1561 if (!uris.at(i).toString().isEmpty())
1563 bi=model->addNewBranch();
1567 s=uris.at(i).toLocalFile();
1570 QString file = QDir::fromNativeSeparators(s);
1571 heading = QFileInfo(file).baseName();
1573 if (file.endsWith(".vym", false))
1574 bi->setVymLink(file);
1576 bi->setURL(uris.at(i).toString());
1579 bo->setURL(uris.at(i).toString());
1583 if (!heading.isEmpty())
1584 bi->setHeading(heading);
1586 bi->setHeading(uris.at(i).toString());
1591 model->reposition();
1594 event->acceptProposedAction();
1597 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1599 // Note: Here we are prepared for multiple selections, though this
1600 // is not yet implemented elsewhere
1602 // Here in MapEditor we can only select Branches and Images
1603 QList <TreeItem*> treeItemsNew;
1604 QList <TreeItem*> treeItemsOld;
1606 bool do_reposition=false;
1609 foreach (ix,newsel.indexes() )
1611 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1612 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1613 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1615 foreach (ix,oldsel.indexes() )
1617 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1618 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1619 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1622 // Trim list of selection rectangles
1623 while (treeItemsNew.count() < selboxList.count() )
1624 delete selboxList.takeFirst();
1626 // Take care to tmp scroll/unscroll
1627 if (!oldsel.isEmpty())
1629 QModelIndex ix=oldsel.indexes().first();
1632 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1635 if (ti->isBranchLikeType() )
1637 // reset tmp scrolled branches
1638 BranchItem *bi=(BranchItem*)ti;
1639 if (bi->resetTmpUnscroll() )
1642 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1643 // Hide link if not needed
1644 ((MapItem*)ti)->getLMO()->updateVisibility();
1649 if (!treeItemsNew.isEmpty())
1651 QModelIndex ix=newsel.indexes().first();
1654 // Temporary unscroll if necessary
1655 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1656 if (ti->isBranchLikeType() )
1658 BranchItem *bi=(BranchItem*)ti;
1659 if (bi->hasScrolledParent(bi) )
1661 if (bi->parentBranch()->tmpUnscroll() )
1665 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1666 // Show link if needed
1667 ((MapItem*)ti)->getLMO()->updateVisibility();
1670 // FIXME-3 cout << "ME::updateSel doRepos="<<do_reposition<<endl;
1671 if (do_reposition) model->reposition();
1673 // Reduce rectangles
1674 while (treeItemsNew.count() < selboxList.count() )
1675 delete selboxList.takeFirst();
1677 // Add additonal rectangles
1678 QGraphicsRectItem *sb;
1679 while (treeItemsNew.count() > selboxList.count() )
1681 sb = mapScene->addRect(
1683 QPen(selectionColor),
1685 sb->setZValue(Z_SELBOX);
1687 selboxList.append (sb);
1691 // Reposition rectangles
1695 LinkableMapObj *lmo;
1696 for (int i=0; i<treeItemsNew.count();++i)
1698 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1699 bbox=lmo->getBBox();
1700 sb=selboxList.at(i);
1703 bbox.width(), bbox.height());
1704 sb->setPen (selectionColor);
1705 sb->setBrush (selectionColor);
1712 void MapEditor::updateData (const QModelIndex &sel)
1714 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1717 cout << "ME::updateData\n";
1719 cout << " ti="<<ti<<endl;
1720 cout << " h="<<ti->getHeading().toStdString()<<endl;
1723 if (ti->isBranchLikeType())
1725 // cout << " ->updating...\n";
1726 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1731 void MapEditor::setSelectionColor (QColor col)
1734 QItemSelection sel=model->getSelectionModel()->selection();
1735 updateSelection(sel,sel);
1739 QColor MapEditor::getSelectionColor ()
1741 return selectionColor;