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 cout << "ME::scrollTo "<<ti->getHeadingStd()<<" tL="<<r.topLeft()<<" bR="<<r.bottomRight()<<endl;
200 setScrollBarPosTarget (lmo->getBBox() );
205 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
207 // Code copied from Qt sources
211 qreal width = viewport()->width();
212 qreal height = viewport()->height();
213 QRectF viewRect = matrix().mapRect(rect);
215 qreal left = horizontalScrollBar()->value();
216 qreal right = left + width;
217 qreal top = verticalScrollBar()->value();
218 qreal bottom = top + height;
220 if (viewRect.left() <= left + xmargin) {
221 // need to scroll from the left
222 // if (!d->leftIndent)
223 scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
225 if (viewRect.right() >= right - xmargin) {
226 // need to scroll from the right
227 // if (!d->leftIndent)
228 scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
230 if (viewRect.top() <= top + ymargin) {
231 // need to scroll from the top
232 // if (!d->topIndent)
233 scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
235 if (viewRect.bottom() >= bottom - ymargin) {
236 // need to scroll from the bottom
237 // if (!d->topIndent)
238 scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
241 if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
242 scrollBarPosAnimation.stop();
243 scrollBarPosAnimation.setTargetObject (this);
244 scrollBarPosAnimation.setPropertyName ("scrollBarPos");
245 scrollBarPosAnimation.setDuration(1000);
246 scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
247 scrollBarPosAnimation.setStartValue(
248 QPointF (horizontalScrollBar()->value() ,
249 verticalScrollBar()->value() ) );
250 scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
251 scrollBarPosAnimation.start();
254 QPointF MapEditor::getScrollBarPosTarget()
256 return scrollBarPosTarget;
260 void MapEditor::setScrollBarPos(const QPointF &p)
263 horizontalScrollBar()->setValue(int(p.x()));
264 verticalScrollBar()->setValue(int(p.y()));
267 QPointF MapEditor::getScrollBarPos()
272 void MapEditor::setZoomFactorTarget (const qreal &zft)
274 zoomFactorTarget=zft;
275 if (zoomAnimation.state()==QtAbstractAnimation::Running)
276 zoomAnimation.stop();
277 //zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
278 zoomAnimation.setTargetObject (this);
279 zoomAnimation.setPropertyName ("zoomFactor");
280 zoomAnimation.setDuration(1000);
281 zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
282 zoomAnimation.setStartValue(zoomFactor);
283 zoomAnimation.setEndValue(zft);
284 zoomAnimation.start();
287 qreal MapEditor::getZoomFactorTarget()
289 return zoomFactorTarget;
293 void MapEditor::setZoomFactor(const qreal &zf)
296 setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
299 qreal MapEditor::getZoomFactor()
304 void MapEditor::print()
308 printer = new QPrinter;
309 printer->setColorMode (QPrinter::Color);
310 printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
311 printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
312 printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
315 QRectF totalBBox=getTotalBBox();
317 // Try to set orientation automagically
318 // Note: Interpretation of generated postscript is amibiguous, if
319 // there are problems with landscape mode, see
320 // http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
322 if (totalBBox.width()>totalBBox.height())
323 // recommend landscape
324 printer->setOrientation (QPrinter::Landscape);
326 // recommend portrait
327 printer->setOrientation (QPrinter::Portrait);
329 if ( printer->setup(this) )
330 // returns false, if printing is canceled
332 QPainter pp(printer);
334 pp.setRenderHint(QPainter::Antialiasing,true);
336 // Don't print the visualisation of selection
339 QRectF mapRect=totalBBox;
340 QGraphicsRectItem *frame=NULL;
344 // Print frame around map
345 mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10,
346 totalBBox.width()+20, totalBBox.height()+20);
347 frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
353 double paperAspect = (double)printer->width() / (double)printer->height();
354 double mapAspect = (double)mapRect.width() / (double)mapRect.height();
356 if (mapAspect>=paperAspect)
358 // Fit horizontally to paper width
359 //pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );
360 viewBottom=(int)(printer->width()/mapAspect);
363 // Fit vertically to paper height
364 //pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());
365 viewBottom=printer->height();
370 // Print footer below map
372 font.setPointSize(10);
374 QRectF footerBox(0,viewBottom,printer->width(),15);
375 // FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
376 pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
380 QRectF (0,0,printer->width(),printer->height()-15),
381 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
384 // Viewport has paper dimension
385 if (frame) delete (frame);
390 // Save settings in vymrc
391 settings.writeEntry("/mainwindow/printerName",printer->printerName());
392 settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
393 settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
397 QRectF MapEditor::getTotalBBox() //FIXME-2 needed e.g. for image export
401 for (int i=0;i<rootItem->branchCount(); i++)
402 r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
408 QPixmap MapEditor::getPixmap()
410 QRectF mapRect=getTotalBBox();
411 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
414 pp.setRenderHints(renderHints());
416 // Don't print the visualisation of selection
419 mapScene->render ( &pp,
420 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
421 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
430 void MapEditor::setAntiAlias (bool b)
432 setRenderHint(QPainter::Antialiasing,b);
435 void MapEditor::setSmoothPixmap(bool b)
437 setRenderHint(QPainter::SmoothPixmapTransform,b);
440 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
442 // Start with mapcenter, no images allowed at rootItem
444 BranchItem *bi=model->getRootItem()->getFirstBranch();
445 TreeItem *found=NULL;
448 found=bi->findMapItem (p, exclude);
449 if (found) return found;
451 bi=model->getRootItem()->getBranchNum(i);
456 AttributeTable* MapEditor::attributeTable()
461 void MapEditor::testFunction1()
463 cout << "ME::test1 selected TI="<<model->getSelectedItem()<<endl;
465 BranchObj *bo=model->getSelectedBranchObj();
468 bo->moveBy (100,100);
473 // Code copied from Qt sources
474 QRectF rect=model->getSelectedBranchObj()->getBBox();
478 qreal width = viewport()->width();
479 qreal height = viewport()->height();
480 QRectF viewRect = matrix().mapRect(rect);
482 qreal left = horizontalScrollBar()->value();
483 qreal right = left + width;
484 qreal top = verticalScrollBar()->value();
485 qreal bottom = top + height;
487 if (viewRect.left() <= left + xmargin) {
488 // need to scroll from the left
489 // if (!d->leftIndent)
490 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
492 if (viewRect.right() >= right - xmargin) {
493 // need to scroll from the right
494 // if (!d->leftIndent)
495 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
497 if (viewRect.top() <= top + ymargin) {
498 // need to scroll from the top
499 // if (!d->topIndent)
500 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
502 if (viewRect.bottom() >= bottom - ymargin) {
503 // need to scroll from the bottom
504 // if (!d->topIndent)
505 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
507 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
508 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
513 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
514 animation->setDuration(5000);
515 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
516 animation->setEasingCurve ( QtEasingCurve::OutQuint);
517 animation->setStartValue(sceneRect() );
518 animation->setEndValue(QRectF(50, 50, 1000, 1000));
523 QDialog *dia= new QDialog (this);
524 dia->setGeometry (50,50,10,10);
529 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
530 animation->setDuration(1000);
531 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
532 animation->setEasingCurve ( QtEasingCurve::OutQuint);
533 animation->setStartValue(QRect(50, 50, 10, 10));
534 animation->setEndValue(QRect(250, 250, 100, 100));
539 /* FIXME-4 Hide hidden stuff temporary, maybe add this as regular function somewhere
540 if (hidemode==HideNone)
542 setHideTmpMode (HideExport);
543 mapCenter->calcBBoxSizeWithChilds();
544 QRectF totalBBox=mapCenter->getTotalBBox();
545 QRectF mapRect=totalBBox;
546 QCanvasRectangle *frame=NULL;
548 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
550 mapRect.setRect (totalBBox.x(), totalBBox.y(),
551 totalBBox.width(), totalBBox.height());
552 frame=new QCanvasRectangle (mapRect,mapScene);
553 frame->setBrush (QColor(white));
554 frame->setPen (QColor(black));
560 setHideTmpMode (HideNone);
562 cout <<" hidemode="<<hidemode<<endl;
567 if (hidemode==HideExport)
568 setHideTmpMode (HideNone);
570 setHideTmpMode (HideExport);
575 void MapEditor::testFunction2()
577 // Create list with all bounding polygons
578 QList <LinkableMapObj*> mapobjects;
579 QList <ConvexPolygon> polys;
581 QList <Vector> vectors;
582 QList <Vector> orgpos;
583 QStringList headings; //FIXME-3 testing only
589 // Outer loop: Iterate until we no more changes in orientation
590 bool orientationChanged=true;
591 while (orientationChanged)
593 BranchItem *ri=model->getRootItem();
594 for (int i=0;i<ri->branchCount();++i)
596 bi=ri->getBranchNum (i);
597 bo=(BranchObj*)bi->getLMO();
600 mapobjects.append (bo);
601 p=bo->getBoundingPolygon();
604 vectors.append (QPointF(0,0));
605 orgpos.append (p.at(0));
606 headings.append (bi->getHeading());
608 for (int j=0;j<bi->branchCount();++j)
610 bi2=bi->getBranchNum (j);
611 bo=(BranchObj*)bi2->getLMO();
614 mapobjects.append (bo);
615 p=bo->getBoundingPolygon();
618 vectors.append (QPointF(0,0));
619 orgpos.append (p.at(0));
620 headings.append (bi2->getHeading());
625 // Iterate moving bounding polygons until we have no more collisions
630 for (int i=0; i<polys.size()-1; ++i)
632 for (int j=i+1; j<polys.size();++j)
634 if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
637 //cout << "Collision: "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<endl;
638 v=polys.at(j).centroid()-polys.at(i).centroid();
639 // Move also away if centroids are identical
642 //cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<endl;
643 v.setX (rand()%200 -100);
644 v.setY (rand()%200 -100);
649 //cout << " v="<<v<<endl;
656 for (int i=0;i<vectors.size();i++)
658 //cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
659 polys[i].translate (vectors[i]);
661 cout << "Collisions: "<<collisions<<endl;
665 // Finally move the real objects and update
666 QList <LinkableMapObj::Orientation> orients;
667 for (int i=0;i<polys.size();i++)
669 Vector v=polys[i].at(0)-orgpos[i];
670 orients.append (mapobjects[i]->getOrientation());
671 mapobjects[i]->moveBy(v.x(),v.y() );
672 mapobjects[i]->setRelPos();
675 orientationChanged=false;
676 for (int i=0;i<polys.size();i++)
677 if (orients[i]!=mapobjects[i]->getOrientation())
679 orientationChanged=true;
682 cout << "Final: orientChanged="<<orientationChanged<<endl;
684 //orientationChanged=false;
685 } // loop if orientation has changed
687 model->emitSelectionChanged();
690 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
695 if (i>0) return bi->parent()->getBranchNum(i-1);
700 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
704 int dz=selbi->depth(); // original depth
706 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
711 // Look for branch with same parent but directly above
713 bi=getBranchDirectBelow(selbi);
715 bi=getBranchDirectAbove (selbi);
718 // direct predecessor
721 // Go towards center and look for predecessor
722 while (selbi->depth()>0)
724 selbi=(BranchItem*)(selbi->parent());
725 if (selbi->depth()==1 && invert)
726 bi=getBranchDirectBelow (selbi);
728 bi=getBranchDirectAbove (selbi);
733 while (selbi->depth()<dz)
735 // try to get back to original depth dz
736 bi=selbi->getLastBranch();
750 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
755 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
760 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
765 int dz=selbi->depth(); // original depth
767 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
771 // Look for branch with same parent but directly below
773 bi=getBranchDirectAbove (selbi);
775 bi=getBranchDirectBelow (selbi);
781 // Go towards center and look for neighbour
782 while (selbi->depth()>0)
784 selbi=(BranchItem*)(selbi->parent());
785 if (selbi->depth()==1 && invert)
786 bi=getBranchDirectAbove (selbi);
788 bi=getBranchDirectBelow (selbi);
793 while (selbi->depth()<dz)
795 // try to get back to original depth dz
796 bi=selbi->getFirstBranch();
810 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
815 // Special case: use alternative selection index
816 return bi->getLastSelectedBranchAlt();
817 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
819 return (BranchItem*)(bi->parent());
822 if (bi->getType()== TreeItem::Branch )
823 return bi->getLastSelectedBranch();
828 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
832 if (bi->depth()==0) return bi->getLastSelectedBranch();
833 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
835 return (BranchItem*)(bi->parent());
838 if (bi->getType()== TreeItem::Branch )
839 return (BranchItem*)bi->getLastSelectedBranch();
846 void MapEditor::cursorUp()
848 BranchItem *bi=model->getSelectedBranch();
849 if (bi) model->select (getBranchAbove(bi));
852 void MapEditor::cursorDown()
855 BranchItem *bi=model->getSelectedBranch();
856 if (bi) model->select (getBranchBelow(bi));
859 void MapEditor::cursorLeft()
861 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
862 if (bi) model->select (bi);
865 void MapEditor::cursorRight()
867 BranchItem *bi=getRightBranch (model->getSelectedBranch());
868 if (bi) model->select (bi);
871 void MapEditor::cursorFirst()
873 model->selectFirstBranch();
876 void MapEditor::cursorLast()
878 model->selectLastBranch();
882 void MapEditor::editHeading()
886 editHeadingFinished();
889 BranchObj *bo=model->getSelectedBranchObj();
890 BranchItem *bi=model->getSelectedBranch();
893 model->setSelectionBlocked(true);
895 lineEdit->setText (bi->getHeading());
896 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
897 lineEdit->setGeometry(p.x(),p.y(),230,25);
898 lineEdit->selectAll();
900 lineEdit->setFocus();
901 lineEdit->grabKeyboard();
906 void MapEditor::editHeadingFinished()
908 editingHeading=false;
909 lineEdit->releaseKeyboard();
910 model->setHeading (lineEdit->text() );
911 model->setSelectionBlocked(false);
914 // Maybe reselect previous branch
915 mainWindow->editHeadingFinished (model);
919 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
921 // Lineedits are already closed by preceding
922 // mouseEvent, we don't need to close here.
924 QPointF p = mapToScene(e->pos());
925 TreeItem *ti=findMapItem (p, NULL);
926 LinkableMapObj* lmo=NULL;
927 if (ti) lmo=((MapItem*)ti)->getLMO();
930 { // MapObj was found
931 if (model->getSelectedLMO() != lmo)
937 if (model->getSelectedBranchObj() )
939 // Context Menu on branch or mapcenter
940 branchContextMenu->popup(e->globalPos() );
943 if (model->getSelectedImage() )
945 // Context Menu on floatimage
946 floatimageContextMenu->popup(e->globalPos() );
950 { // No MapObj found, we are on the Canvas itself
951 // Context Menu on scene
953 // Open context menu synchronously to position new mapcenter
954 model->setContextPos (p);
955 canvasContextMenu->exec(e->globalPos() );
956 model->unsetContextPos ();
961 void MapEditor::keyPressEvent(QKeyEvent* e)
963 if (e->modifiers() & Qt::ControlModifier)
965 switch (mainWindow->getModMode())
967 case Main::ModModeColor:
968 setCursor (PickColorCursor);
970 case Main::ModModeCopy:
971 setCursor (CopyCursor);
973 case Main::ModModeXLink:
974 setCursor (XLinkCursor);
977 setCursor (Qt::ArrowCursor);
983 void MapEditor::keyReleaseEvent(QKeyEvent* e)
985 if (!(e->modifiers() & Qt::ControlModifier))
986 setCursor (Qt::ArrowCursor);
989 void MapEditor::mousePressEvent(QMouseEvent* e)
991 // Ignore right clicks, these will go to context menus
992 if (e->button() == Qt::RightButton )
998 //Ignore clicks while editing heading
999 if (model->isSelectionBlocked() )
1005 QPointF p = mapToScene(e->pos());
1006 TreeItem *ti=findMapItem (p, NULL);
1007 LinkableMapObj* lmo=NULL;
1008 if (ti) lmo=((MapItem*)ti)->getLMO();
1013 //Take care of system flags _or_ modifier modes
1015 if (lmo && ti->isBranchLikeType() )
1017 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
1018 if (!foname.isEmpty())
1020 // systemFlag clicked
1021 model->select (lmo);
1022 if (foname=="system-url")
1024 if (e->state() & Qt::ControlModifier)
1025 mainWindow->editOpenURLTab();
1027 mainWindow->editOpenURL();
1029 else if (foname=="system-vymLink")
1031 mainWindow->editOpenVymLink();
1032 // tabWidget may change, better return now
1033 // before segfaulting...
1034 } else if (foname=="note")
1035 mainWindow->windowToggleNoteEditor();
1036 else if (foname=="hideInExport")
1037 model->toggleHideExport();
1038 // FIXME-3 needed? xelection.update();
1042 // No system flag clicked, take care of modmodes (CTRL-Click)
1043 if (e->state() & Qt::ControlModifier)
1045 if (mainWindow->getModMode()==Main::ModModeColor)
1048 setCursor (PickColorCursor);
1051 if (mainWindow->getModMode()==Main::ModModeXLink)
1053 BranchItem *bi_begin=model->getSelectedBranch();
1057 tmpXLink=model->createXLink(bi_begin,true);
1058 tmpXLink->setColor(model->getMapDefXLinkColor());
1059 tmpXLink->setWidth(model->getMapDefXLinkWidth());
1060 tmpXLink->setEnd (p);
1061 tmpXLink->updateXLink();
1065 } // End of modmodes
1070 cout << "ME::mouse pressed\n";
1071 cout << " lmo="<<lmo<<endl;
1072 cout << " ti="<<ti->getHeadingStd()<<endl;
1074 // Select the clicked object
1079 // Left Button Move Branches
1080 if (e->button() == Qt::LeftButton )
1082 movingObj_start.setX( p.x() - lmo->x() );
1083 movingObj_start.setY( p.y() - lmo->y() );
1084 movingObj_orgPos.setX (lmo->x() );
1085 movingObj_orgPos.setY (lmo->y() );
1087 movingObj_orgRelPos=lmo->getRelPos();
1089 // If modMode==copy, then we want to "move" the _new_ object around
1090 // then we need the offset from p to the _old_ selection, because of tmp
1091 if (mainWindow->getModMode()==Main::ModModeCopy &&
1092 e->state() & Qt::ControlModifier)
1094 BranchItem *bi=model->getSelectedBranch();
1098 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
1099 //bi->addBranch (model->getSelectedBranch());
1101 model->select(bi->getLastBranch());
1102 model->reposition();
1106 movingObj=model->getSelectedLMO();
1108 // Middle Button Toggle Scroll
1109 // (On Mac OS X this won't work, but we still have
1110 // a button in the toolbar)
1111 if (e->button() == Qt::MidButton )
1112 model->toggleScroll();
1113 // model->updateActions(); FIXME-3 needed?
1114 // FIXME-3 needed? xelection.update();
1116 { // No MapObj found, we are on the scene itself
1117 // Left Button move Pos of sceneView
1118 if (e->button() == Qt::LeftButton )
1120 movingObj=NULL; // move Content not Obj
1121 movingObj_start=e->globalPos();
1122 movingCont_start=QPointF (
1123 horizontalScrollBar()->value(),
1124 verticalScrollBar()->value());
1125 movingVec=QPointF(0,0);
1126 setCursor(HandOpenCursor);
1131 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1133 QPointF p = mapToScene(e->pos());
1134 TreeItem *seli=model->getSelectedItem();
1135 LinkableMapObj* lmosel=NULL;
1136 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1137 lmosel=((MapItem*)seli)->getLMO();
1139 // Move the selected MapObj
1140 if ( lmosel && movingObj)
1143 // reset cursor if we are moving and don't copy
1144 if (mainWindow->getModMode()!=Main::ModModeCopy)
1145 setCursor (Qt::ArrowCursor);
1147 // To avoid jumping of the sceneView, only
1148 // show selection, if not tmp linked
1149 /* FIXME-2 if (!lmosel->hasParObjTmp())
1150 model->emitShowSelection();
1153 // Now move the selection, but add relative position
1154 // (movingObj_start) where selection was chosen with
1155 // mousepointer. (This avoids flickering resp. jumping
1156 // of selection back to absPos)
1158 // Check if we could link
1159 TreeItem *ti=findMapItem (p, seli);
1160 BranchItem *dsti=NULL;
1161 LinkableMapObj* dst=NULL;
1162 if (ti && ti!=seli && ti->isBranchLikeType())
1164 dsti=(BranchItem*)ti;
1170 if (lmosel && seli->getType()==TreeItem::Image)
1172 FloatObj *fio=(FloatImageObj*)lmosel;
1173 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1175 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1176 model->emitSelectionChanged(); // position has changed
1178 // Relink float to new mapcenter or branch, if shift is pressed
1179 // Only relink, if selection really has a new parent
1180 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1182 // Also save the move which was done so far
1183 QString pold=qpointFToString(movingObj_orgRelPos);
1184 QString pnow=qpointFToString(fio->getRelPos());
1190 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1191 fio->getParObj()->requestReposition();
1192 model->reposition();
1194 model->relinkImage ((ImageItem*) seli,dsti);
1195 model->select (seli);
1196 //movingObj=lmosel; //FIXME-3
1197 //movingObj_orgRelPos=lmosel->getRelPos();
1199 model->reposition();
1202 { // selection != a FloatObj
1203 if (seli->depth()==0)
1205 lmosel->move (p-movingObj_start);
1206 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1209 v=lmosel->getAbsPos();
1210 for (int i=0; i<seli->branchCount(); ++i)
1212 seli->getBranchObjNum(i)->setRelPos();
1213 seli->getBranchObjNum(i)->setOrientation();
1218 if (seli->depth()==1)
1221 lmosel->move(p-movingObj_start);
1222 lmosel->setRelPos();
1225 // Move ordinary branch
1226 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1227 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1228 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1229 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1231 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1232 lmosel->setRelPos();
1236 // Maybe we can relink temporary?
1237 if (dsti) // FIXME-1 check if dsti is ancestor of myself!
1239 if (e->modifiers()==Qt::ControlModifier)
1241 // Special case: CTRL to link below dst
1242 lmosel->setParObjTmp (dst,p,+1);
1244 else if (e->modifiers()==Qt::ShiftModifier)
1245 lmosel->setParObjTmp (dst,p,-1);
1247 lmosel->setParObjTmp (dst,p,0);
1250 lmosel->unsetParObjTmp();
1252 // reposition subbranch
1253 lmosel->reposition();
1255 QItemSelection sel=model->getSelectionModel()->selection();
1256 updateSelection(sel,sel); // position has changed
1258 } // no FloatImageObj
1262 } // selection && moving_obj
1264 // Draw a link from one branch to another
1267 tmpXLink->setEnd (p);
1268 tmpXLink->updateXLink();
1272 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1274 QPointF p=e->globalPos();
1275 movingVec.setX(-p.x() + movingObj_start.x() );
1276 movingVec.setY(-p.y() + movingObj_start.y() );
1277 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1278 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1283 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1285 QPointF p = mapToScene(e->pos());
1286 TreeItem *seli=model->getSelectedItem();
1288 TreeItem *dsti=NULL;
1289 if (seli) dsti=findMapItem(p, seli);
1290 LinkableMapObj* dst=NULL;
1291 if (dsti && dsti->isBranchLikeType ())
1292 dst=((MapItem*)dsti)->getLMO();
1297 // Have we been picking color?
1301 setCursor (Qt::ArrowCursor);
1302 // Check if we are over another branch
1305 if (e->state() & Qt::ShiftModifier)
1306 model->colorBranch (((BranchObj*)dst)->getColor());
1308 model->colorSubtree (((BranchObj*)dst)->getColor());
1313 // Have we been drawing a link?
1317 // Check if we are over another branch
1320 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1321 tmpXLink->updateXLink();
1322 tmpXLink->activate();
1324 tmpXLink,QString("delete ()"),
1325 dsti,QString("addXLink (\"%1\",\"%2\")").arg(model->getSelectString(tmpXLink->getBegin())).arg(model->getSelectString(dsti)),
1326 QString("Add xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(dsti))
1330 model->deleteItem(tmpXLink);
1336 // Have we been moving something?
1337 if ( seli && movingObj )
1339 if (seli->getType()==TreeItem::Image)
1341 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1344 // Moved FloatObj. Maybe we need to reposition
1345 QString pold=qpointFToString(movingObj_orgRelPos);
1346 QString pnow=qpointFToString(fio->getRelPos());
1352 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1354 cout << "ME::release mouse\n";
1355 fio->getParObj()->requestReposition();
1356 model->reposition();
1360 BranchItem *bi=model->getSelectedBranch();
1361 if (bi && bi->depth()==0)
1363 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1365 QString pold=qpointFToString(movingObj_orgPos);
1366 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1373 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1377 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1378 { // A branch was moved
1379 LinkableMapObj* lmosel=NULL;
1380 lmosel=((MapItem*)seli)->getLMO();
1382 // save the position in case we link to mapcenter
1383 QPointF savePos=QPointF (lmosel->getAbsPos() );
1385 // Reset the temporary drawn link to the original one
1386 lmosel->unsetParObjTmp();
1388 // For Redo we may need to save original selection
1389 QString preSelStr=model->getSelectString(seli);
1392 if (dsti && objectMoved)
1394 // We have a destination, relink to that
1396 BranchObj* bsel=model->getSelectedBranchObj();
1398 QString preParStr=model->getSelectString (bsel->getParObj());
1399 QString preNum=QString::number (seli->num(),10);
1400 QString preDstParStr;
1403 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1405 preDstParStr=model->getSelectString (dst->getParObj());
1406 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1408 if (e->state() & Qt::ControlModifier && dst->getParObj())
1411 preDstParStr=model->getSelectString (dst->getParObj());
1412 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1415 preDstParStr=model->getSelectString(dst);
1416 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1417 if (dsti->depth()==0) bsel->move (savePos);
1421 QString postSelStr=model->getSelectString(lmosel);
1422 QString postNum=QString::number (seli->num(),10);
1424 QString undoCom="relinkTo (\""+
1425 preParStr+ "\"," + preNum +"," +
1426 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1428 QString redoCom="relinkTo (\""+
1429 preDstParStr + "\"," + postNum + "," +
1430 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1435 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1440 // No destination, undo temporary move
1442 if (seli->depth()==1)
1444 // The select string might be different _after_ moving around.
1445 // Therefor reposition and then use string of old selection, too
1446 model->reposition();
1448 QPointF rp(lmosel->getRelPos());
1449 if (rp != movingObj_orgRelPos)
1451 QString ps=qpointFToString(rp);
1453 model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1454 preSelStr, "moveRel "+ps,
1455 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1459 // Draw the original link, before selection was moved around
1460 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1462 lmosel->setRelPos(); // calc relPos first for starting point
1464 model->startAnimation(
1466 lmosel->getRelPos(),
1470 model->reposition();
1473 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1474 // Finally resize scene, if needed
1480 // maybe we moved View: set old cursor
1481 setCursor (Qt::ArrowCursor);
1485 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1487 if (debug) cout << "ME p="<<mapToScene (e->pos())<<endl;
1490 if (model->isSelectionBlocked() )
1496 if (e->button() == Qt::LeftButton )
1498 QPointF p = mapToScene(e->pos());
1499 TreeItem *ti=findMapItem (p, NULL);
1500 if (ti) { // MapObj was found
1501 // First select the MapObj than edit heading
1508 void MapEditor::resizeEvent (QResizeEvent* e)
1510 QGraphicsView::resizeEvent( e );
1513 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1515 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1516 // cerr << event->format(i) << endl;
1518 if (event->mimeData()->hasImage())
1519 event->acceptProposedAction();
1521 if (event->mimeData()->hasUrls())
1522 event->acceptProposedAction();
1525 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1529 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1534 void MapEditor::dropEvent(QDropEvent *event)
1536 BranchItem *selbi=model->getSelectedBranch();
1540 foreach (QString format,event->mimeData()->formats())
1541 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1545 if (event->mimeData()->hasImage())
1547 QVariant imageData = event->mimeData()->imageData();
1548 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1550 if (event->mimeData()->hasUrls())
1551 uris=event->mimeData()->urls();
1559 for (int i=0; i<uris.count();i++)
1561 // Workaround to avoid adding empty branches
1562 if (!uris.at(i).toString().isEmpty())
1564 bi=model->addNewBranch();
1568 s=uris.at(i).toLocalFile();
1571 QString file = QDir::fromNativeSeparators(s);
1572 heading = QFileInfo(file).baseName();
1574 if (file.endsWith(".vym", false))
1575 bi->setVymLink(file);
1577 bi->setURL(uris.at(i).toString());
1580 bo->setURL(uris.at(i).toString());
1584 if (!heading.isEmpty())
1585 bi->setHeading(heading);
1587 bi->setHeading(uris.at(i).toString());
1592 model->reposition();
1595 event->acceptProposedAction();
1598 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1600 // Note: Here we are prepared for multiple selections, though this
1601 // is not yet implemented elsewhere
1603 // Here in MapEditor we can only select Branches and Images
1604 QList <TreeItem*> treeItemsNew;
1605 QList <TreeItem*> treeItemsOld;
1607 bool do_reposition=false;
1610 foreach (ix,newsel.indexes() )
1612 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1613 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1614 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1616 foreach (ix,oldsel.indexes() )
1618 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1619 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1620 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1623 // Trim list of selection rectangles
1624 while (treeItemsNew.count() < selboxList.count() )
1625 delete selboxList.takeFirst();
1627 // Take care to tmp scroll/unscroll
1628 if (!oldsel.isEmpty())
1630 QModelIndex ix=oldsel.indexes().first();
1633 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1636 if (ti->isBranchLikeType() )
1638 // reset tmp scrolled branches
1639 BranchItem *bi=(BranchItem*)ti;
1640 if (bi->resetTmpUnscroll() )
1643 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1644 // Hide link if not needed
1645 ((MapItem*)ti)->getLMO()->updateVisibility();
1650 if (!treeItemsNew.isEmpty())
1652 QModelIndex ix=newsel.indexes().first();
1655 // Temporary unscroll if necessary
1656 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1657 if (ti->isBranchLikeType() )
1659 BranchItem *bi=(BranchItem*)ti;
1660 if (bi->hasScrolledParent(bi) )
1662 if (bi->parentBranch()->tmpUnscroll() )
1666 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1667 // Show link if needed
1668 ((MapItem*)ti)->getLMO()->updateVisibility();
1671 // FIXME-3 cout << "ME::updateSel doRepos="<<do_reposition<<endl;
1672 if (do_reposition) model->reposition();
1674 // Reduce rectangles
1675 while (treeItemsNew.count() < selboxList.count() )
1676 delete selboxList.takeFirst();
1678 // Add additonal rectangles
1679 QGraphicsRectItem *sb;
1680 while (treeItemsNew.count() > selboxList.count() )
1682 sb = mapScene->addRect(
1684 QPen(selectionColor),
1686 sb->setZValue(Z_SELBOX);
1688 selboxList.append (sb);
1692 // Reposition rectangles
1696 LinkableMapObj *lmo;
1697 for (int i=0; i<treeItemsNew.count();++i)
1699 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1700 bbox=lmo->getBBox();
1701 sb=selboxList.at(i);
1704 bbox.width(), bbox.height());
1705 sb->setPen (selectionColor);
1706 sb->setBrush (selectionColor);
1713 void MapEditor::updateData (const QModelIndex &sel)
1715 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1718 cout << "ME::updateData\n";
1720 cout << " ti="<<ti<<endl;
1721 cout << " h="<<ti->getHeading().toStdString()<<endl;
1724 if (ti->isBranchLikeType())
1726 // cout << " ->updating...\n";
1727 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1732 void MapEditor::setSelectionColor (QColor col)
1735 QItemSelection sel=model->getSelectionModel()->selection();
1736 updateSelection(sel,sel);
1740 QColor MapEditor::getSelectionColor ()
1742 return selectionColor;