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
74 setAcceptDrops (true);
76 //model->reposition(); //FIXME-3 really still needed?
79 // Shortcuts and actions
81 a = new QAction("Select upper branch", this);
82 a->setShortcut (Qt::Key_Up );
83 a->setShortcutContext (Qt::WidgetShortcut);
85 connect( a, SIGNAL( triggered() ), this, SLOT( cursorUp() ) );
87 a = new QAction( "Select lower branch",this);
88 a->setShortcut ( Qt::Key_Down );
89 a->setShortcutContext (Qt::WidgetShortcut);
91 connect( a, SIGNAL( triggered() ), this, SLOT( cursorDown() ) );
93 a = new QAction( "Select left branch", this);
94 a->setShortcut (Qt::Key_Left );
95 // a->setShortcutContext (Qt::WindowShortcut);
96 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
98 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLeft() ) );
100 a = new QAction( "Select child branch", this);
101 a->setShortcut (Qt::Key_Right);
102 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
104 connect( a, SIGNAL( triggered() ), this, SLOT( cursorRight() ) );
106 a = new QAction( "Select first branch", this);
107 a->setShortcut (Qt::Key_Home );
108 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
110 connect( a, SIGNAL( triggered() ), this, SLOT( cursorFirst() ) );
112 a = new QAction( "Select last branch",this);
113 a->setShortcut ( Qt::Key_End );
114 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
116 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLast() ) );
118 // Action to embed LineEdit for heading in Scene
119 editingHeading=false;
120 lineEdit=new QLineEdit;
122 QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
123 pw->setZValue (Z_LINEEDIT);
125 a = new QAction( tr( "Edit heading","MapEditor" ), this);
126 a->setShortcut ( Qt::Key_Return ); //Edit heading
128 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
129 a = new QAction( tr( "Edit heading","MapEditor" ), this);
130 a->setShortcut ( Qt::Key_Enter); //Edit heading
132 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
135 selectionColor =QColor (255,255,0);
138 // Attributes //FIXME-2 testing only...
141 attrTable= new AttributeTable();
143 ad=attrTable->addKey (k,StringList);
147 sl <<"val 1"<<"val 2"<< "val 3";
148 ad->setValue (QVariant (sl));
150 //attrTable->addValue ("Key A","P 1");
151 //attrTable->addValue ("Key A","P 2");
152 //attrTable->addValue ("Key A","P 3");
153 //attrTable->addValue ("Key A","P 4");
155 ad=attrTable->addKey (k,FreeString);
158 //attrTable->addValue ("Key B","w1");
159 //attrTable->addValue ("Key B","w2");
161 k="C - UniqueString";
162 ad=attrTable->addKey (k,UniqueString);
165 //attrTable->addKey ("Key Prio");
166 //attrTable->addValue ("Key Prio","Prio 1");
167 //attrTable->addValue ("Key Prio","Prio 2");
171 MapEditor::~MapEditor()
173 //cout <<"Destructor MapEditor for "<<model->getMapName().toStdString()<<endl;
174 model->unregisterEditor(this);
177 VymModel* MapEditor::getModel()
182 QGraphicsScene * MapEditor::getScene()
187 void MapEditor::scrollTo (const QModelIndex &index)
191 LinkableMapObj* lmo=NULL;
192 TreeItem *ti= static_cast<TreeItem*>(index.internalPointer());
193 if (ti->getType()==TreeItem::Image ||ti->isBranchLikeType() )
194 lmo=((MapItem*)ti)->getLMO();
195 if (lmo) setScrollBarPosTarget (lmo->getBBox() );
199 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
201 // Code copied from Qt sources
205 qreal width = viewport()->width();
206 qreal height = viewport()->height();
207 QRectF viewRect = matrix().mapRect(rect);
209 qreal left = horizontalScrollBar()->value();
210 qreal right = left + width;
211 qreal top = verticalScrollBar()->value();
212 qreal bottom = top + height;
214 if (viewRect.left() <= left + xmargin) {
215 // need to scroll from the left
216 // if (!d->leftIndent)
217 scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
219 if (viewRect.right() >= right - xmargin) {
220 // need to scroll from the right
221 // if (!d->leftIndent)
222 scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
224 if (viewRect.top() <= top + ymargin) {
225 // need to scroll from the top
226 // if (!d->topIndent)
227 scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
229 if (viewRect.bottom() >= bottom - ymargin) {
230 // need to scroll from the bottom
231 // if (!d->topIndent)
232 scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
235 if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
236 scrollBarPosAnimation.stop();
237 scrollBarPosAnimation.setTargetObject (this);
238 scrollBarPosAnimation.setPropertyName ("scrollBarPos");
239 scrollBarPosAnimation.setDuration(1000);
240 scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
241 scrollBarPosAnimation.setStartValue(
242 QPointF (horizontalScrollBar()->value() ,
243 verticalScrollBar()->value() ) );
244 scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
245 scrollBarPosAnimation.start();
248 QPointF MapEditor::getScrollBarPosTarget()
250 return scrollBarPosTarget;
254 void MapEditor::setScrollBarPos(const QPointF &p)
257 horizontalScrollBar()->setValue(int(p.x()));
258 verticalScrollBar()->setValue(int(p.y()));
261 QPointF MapEditor::getScrollBarPos()
266 void MapEditor::setZoomFactorTarget (const qreal &zft)
268 zoomFactorTarget=zft;
269 if (zoomAnimation.state()==QtAbstractAnimation::Running)
270 zoomAnimation.stop();
271 //zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
272 zoomAnimation.setTargetObject (this);
273 zoomAnimation.setPropertyName ("zoomFactor");
274 zoomAnimation.setDuration(1000);
275 zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
276 zoomAnimation.setStartValue(zoomFactor);
277 zoomAnimation.setEndValue(zft);
278 zoomAnimation.start();
281 qreal MapEditor::getZoomFactorTarget()
283 return zoomFactorTarget;
287 void MapEditor::setZoomFactor(const qreal &zf)
290 setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
293 qreal MapEditor::getZoomFactor()
298 void MapEditor::print()
302 printer = new QPrinter;
303 printer->setColorMode (QPrinter::Color);
304 printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
305 printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
306 printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
309 QRectF totalBBox=getTotalBBox();
311 // Try to set orientation automagically
312 // Note: Interpretation of generated postscript is amibiguous, if
313 // there are problems with landscape mode, see
314 // http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
316 if (totalBBox.width()>totalBBox.height())
317 // recommend landscape
318 printer->setOrientation (QPrinter::Landscape);
320 // recommend portrait
321 printer->setOrientation (QPrinter::Portrait);
323 if ( printer->setup(this) )
324 // returns false, if printing is canceled
326 QPainter pp(printer);
328 pp.setRenderHint(QPainter::Antialiasing,true);
330 // Don't print the visualisation of selection
333 QRectF mapRect=totalBBox;
334 QGraphicsRectItem *frame=NULL;
338 // Print frame around map
339 mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10,
340 totalBBox.width()+20, totalBBox.height()+20);
341 frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
347 double paperAspect = (double)printer->width() / (double)printer->height();
348 double mapAspect = (double)mapRect.width() / (double)mapRect.height();
350 if (mapAspect>=paperAspect)
352 // Fit horizontally to paper width
353 //pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );
354 viewBottom=(int)(printer->width()/mapAspect);
357 // Fit vertically to paper height
358 //pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());
359 viewBottom=printer->height();
364 // Print footer below map
366 font.setPointSize(10);
368 QRectF footerBox(0,viewBottom,printer->width(),15);
369 // FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
370 pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
374 QRectF (0,0,printer->width(),printer->height()-15),
375 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
378 // Viewport has paper dimension
379 if (frame) delete (frame);
384 // Save settings in vymrc
385 settings.writeEntry("/mainwindow/printerName",printer->printerName());
386 settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
387 settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
391 QRectF MapEditor::getTotalBBox() //FIXME-2 needed e.g. for image export
395 for (int i=0;i<rootItem->branchCount(); i++)
396 r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
402 QPixmap MapEditor::getPixmap()
404 QRectF mapRect=getTotalBBox();
405 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
408 pp.setRenderHints(renderHints());
410 // Don't print the visualisation of selection
413 mapScene->render ( &pp,
414 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
415 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
424 void MapEditor::setAntiAlias (bool b)
426 setRenderHint(QPainter::Antialiasing,b);
429 void MapEditor::setSmoothPixmap(bool b)
431 setRenderHint(QPainter::SmoothPixmapTransform,b);
434 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
436 // Start with mapcenter, no images allowed at rootItem
438 BranchItem *bi=model->getRootItem()->getFirstBranch();
439 TreeItem *found=NULL;
442 found=bi->findMapItem (p, exclude);
443 if (found) return found;
445 bi=model->getRootItem()->getBranchNum(i);
450 AttributeTable* MapEditor::attributeTable()
455 void MapEditor::testFunction1()
458 // Code copied from Qt sources
459 QRectF rect=model->getSelectedBranchObj()->getBBox();
463 qreal width = viewport()->width();
464 qreal height = viewport()->height();
465 QRectF viewRect = matrix().mapRect(rect);
467 qreal left = horizontalScrollBar()->value();
468 qreal right = left + width;
469 qreal top = verticalScrollBar()->value();
470 qreal bottom = top + height;
472 if (viewRect.left() <= left + xmargin) {
473 // need to scroll from the left
474 // if (!d->leftIndent)
475 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
477 if (viewRect.right() >= right - xmargin) {
478 // need to scroll from the right
479 // if (!d->leftIndent)
480 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
482 if (viewRect.top() <= top + ymargin) {
483 // need to scroll from the top
484 // if (!d->topIndent)
485 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
487 if (viewRect.bottom() >= bottom - ymargin) {
488 // need to scroll from the bottom
489 // if (!d->topIndent)
490 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
492 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
493 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
498 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
499 animation->setDuration(5000);
500 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
501 animation->setEasingCurve ( QtEasingCurve::OutQuint);
502 animation->setStartValue(sceneRect() );
503 animation->setEndValue(QRectF(50, 50, 1000, 1000));
508 QDialog *dia= new QDialog (this);
509 dia->setGeometry (50,50,10,10);
514 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
515 animation->setDuration(1000);
516 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
517 animation->setEasingCurve ( QtEasingCurve::OutQuint);
518 animation->setStartValue(QRect(50, 50, 10, 10));
519 animation->setEndValue(QRect(250, 250, 100, 100));
524 /* FIXME-4 Hide hidden stuff temporary, maybe add this as regular function somewhere
525 if (hidemode==HideNone)
527 setHideTmpMode (HideExport);
528 mapCenter->calcBBoxSizeWithChilds();
529 QRectF totalBBox=mapCenter->getTotalBBox();
530 QRectF mapRect=totalBBox;
531 QCanvasRectangle *frame=NULL;
533 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
535 mapRect.setRect (totalBBox.x(), totalBBox.y(),
536 totalBBox.width(), totalBBox.height());
537 frame=new QCanvasRectangle (mapRect,mapScene);
538 frame->setBrush (QColor(white));
539 frame->setPen (QColor(black));
545 setHideTmpMode (HideNone);
547 cout <<" hidemode="<<hidemode<<endl;
552 if (hidemode==HideExport)
553 setHideTmpMode (HideNone);
555 setHideTmpMode (HideExport);
560 void MapEditor::testFunction2()
562 // Create list with all bounding polygons
563 QList <LinkableMapObj*> mapobjects;
564 QList <ConvexPolygon> polys;
566 QList <Vector> vectors;
567 QList <Vector> orgpos;
568 QStringList headings; //FIXME-3 testing only
574 // Outer loop: Iterate until we no more changes in orientation
575 bool orientationChanged=true;
576 while (orientationChanged)
578 BranchItem *ri=model->getRootItem();
579 for (int i=0;i<ri->branchCount();++i)
581 bi=ri->getBranchNum (i);
582 bo=(BranchObj*)bi->getLMO();
585 mapobjects.append (bo);
586 p=bo->getBoundingPolygon();
589 vectors.append (QPointF(0,0));
590 orgpos.append (p.at(0));
591 headings.append (bi->getHeading());
593 for (int j=0;j<bi->branchCount();++j)
595 bi2=bi->getBranchNum (j);
596 bo=(BranchObj*)bi2->getLMO();
599 mapobjects.append (bo);
600 p=bo->getBoundingPolygon();
603 vectors.append (QPointF(0,0));
604 orgpos.append (p.at(0));
605 headings.append (bi2->getHeading());
610 // Iterate moving bounding polygons until we have no more collisions
615 for (int i=0; i<polys.size()-1; ++i)
617 for (int j=i+1; j<polys.size();++j)
619 if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
622 //cout << "Collision: "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<endl;
623 v=polys.at(j).centroid()-polys.at(i).centroid();
624 // Move also away if centroids are identical
627 //cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<endl;
628 v.setX (rand()%200 -100);
629 v.setY (rand()%200 -100);
634 //cout << " v="<<v<<endl;
641 for (int i=0;i<vectors.size();i++)
643 //cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
644 polys[i].translate (vectors[i]);
646 cout << "Collisions: "<<collisions<<endl;
650 // Finally move the real objects and update
651 QList <LinkableMapObj::Orientation> orients;
652 for (int i=0;i<polys.size();i++)
654 Vector v=polys[i].at(0)-orgpos[i];
655 orients.append (mapobjects[i]->getOrientation());
656 mapobjects[i]->moveBy(v.x(),v.y() );
659 orientationChanged=false;
660 for (int i=0;i<polys.size();i++)
661 if (orients[i]!=mapobjects[i]->getOrientation())
663 orientationChanged=true;
666 cout << "Final: orientChanged="<<orientationChanged<<endl;
668 //orientationChanged=false;
669 } // loop if orientation has changed
672 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
677 if (i>0) return bi->parent()->getBranchNum(i-1);
682 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
686 int dz=selbi->depth(); // original depth
688 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
693 // Look for branch with same parent but directly above
695 bi=getBranchDirectBelow(selbi);
697 bi=getBranchDirectAbove (selbi);
700 // direct predecessor
703 // Go towards center and look for predecessor
704 while (selbi->depth()>0)
706 selbi=(BranchItem*)(selbi->parent());
707 if (selbi->depth()==1 && invert)
708 bi=getBranchDirectBelow (selbi);
710 bi=getBranchDirectAbove (selbi);
715 while (selbi->depth()<dz)
717 // try to get back to original depth dz
718 bi=selbi->getLastBranch();
732 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
737 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
742 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
747 int dz=selbi->depth(); // original depth
749 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
753 // Look for branch with same parent but directly below
755 bi=getBranchDirectAbove (selbi);
757 bi=getBranchDirectBelow (selbi);
763 // Go towards center and look for neighbour
764 while (selbi->depth()>0)
766 selbi=(BranchItem*)(selbi->parent());
767 if (selbi->depth()==1 && invert)
768 bi=getBranchDirectAbove (selbi);
770 bi=getBranchDirectBelow (selbi);
775 while (selbi->depth()<dz)
777 // try to get back to original depth dz
778 bi=selbi->getFirstBranch();
792 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
797 // Special case: use alternative selection index
798 return bi->getLastSelectedBranchAlt();
799 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
801 return (BranchItem*)(bi->parent());
804 if (bi->getType()== TreeItem::Branch )
805 return bi->getLastSelectedBranch();
810 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
814 if (bi->depth()==0) return bi->getLastSelectedBranch();
815 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
817 return (BranchItem*)(bi->parent());
820 if (bi->getType()== TreeItem::Branch )
821 return (BranchItem*)bi->getLastSelectedBranch();
828 void MapEditor::cursorUp()
830 BranchItem *bi=model->getSelectedBranch();
831 if (bi) model->select (getBranchAbove(bi));
834 void MapEditor::cursorDown()
837 BranchItem *bi=model->getSelectedBranch();
838 if (bi) model->select (getBranchBelow(bi));
841 void MapEditor::cursorLeft()
843 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
844 if (bi) model->select (bi);
847 void MapEditor::cursorRight()
849 BranchItem *bi=getRightBranch (model->getSelectedBranch());
850 if (bi) model->select (bi);
853 void MapEditor::cursorFirst()
855 model->selectFirstBranch();
858 void MapEditor::cursorLast()
860 model->selectLastBranch();
864 void MapEditor::editHeading()
868 editHeadingFinished();
871 BranchObj *bo=model->getSelectedBranchObj();
872 BranchItem *bi=model->getSelectedBranch();
875 model->setSelectionBlocked(true);
877 lineEdit->setText (bi->getHeading());
878 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
879 lineEdit->setGeometry(p.x(),p.y(),230,25);
880 lineEdit->selectAll();
882 lineEdit->setFocus();
883 lineEdit->grabKeyboard();
888 void MapEditor::editHeadingFinished()
890 editingHeading=false;
891 lineEdit->releaseKeyboard();
892 model->setHeading (lineEdit->text() );
893 model->setSelectionBlocked(false);
896 // Maybe reselect previous branch
897 mainWindow->editHeadingFinished (model);
901 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
903 // Lineedits are already closed by preceding
904 // mouseEvent, we don't need to close here.
906 QPointF p = mapToScene(e->pos());
907 TreeItem *ti=findMapItem (p, NULL);
908 LinkableMapObj* lmo=NULL;
909 if (ti) lmo=((MapItem*)ti)->getLMO();
912 { // MapObj was found
913 if (model->getSelectedLMO() != lmo)
919 if (model->getSelectedBranchObj() )
921 // Context Menu on branch or mapcenter
922 branchContextMenu->popup(e->globalPos() );
925 if (model->getSelectedImage() )
927 // Context Menu on floatimage
928 floatimageContextMenu->popup(e->globalPos() );
932 { // No MapObj found, we are on the Canvas itself
933 // Context Menu on scene
935 // Open context menu synchronously to position new mapcenter
936 model->setContextPos (p);
937 canvasContextMenu->exec(e->globalPos() );
938 model->unsetContextPos ();
943 void MapEditor::keyPressEvent(QKeyEvent* e)
945 if (e->modifiers() & Qt::ControlModifier)
947 switch (mainWindow->getModMode())
949 case Main::ModModeColor:
950 setCursor (PickColorCursor);
952 case Main::ModModeCopy:
953 setCursor (CopyCursor);
955 case Main::ModModeXLink:
956 setCursor (XLinkCursor);
959 setCursor (Qt::ArrowCursor);
965 void MapEditor::keyReleaseEvent(QKeyEvent* e)
967 if (!(e->modifiers() & Qt::ControlModifier))
968 setCursor (Qt::ArrowCursor);
971 void MapEditor::mousePressEvent(QMouseEvent* e)
973 // Ignore right clicks, these will go to context menus
974 if (e->button() == Qt::RightButton )
980 //Ignore clicks while editing heading
981 if (model->isSelectionBlocked() )
987 QPointF p = mapToScene(e->pos());
988 TreeItem *ti=findMapItem (p, NULL);
989 LinkableMapObj* lmo=NULL;
990 if (ti) lmo=((MapItem*)ti)->getLMO();
995 //Take care of system flags _or_ modifier modes
997 if (lmo && ti->isBranchLikeType() )
999 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
1000 if (!foname.isEmpty())
1002 // systemFlag clicked
1003 model->select (lmo); // FIXME-3 was selectInt
1006 if (e->state() & Qt::ControlModifier)
1007 mainWindow->editOpenURLTab();
1009 mainWindow->editOpenURL();
1011 else if (foname=="system-vymLink")
1013 mainWindow->editOpenVymLink();
1014 // tabWidget may change, better return now
1015 // before segfaulting...
1016 } else if (foname=="note")
1017 mainWindow->windowToggleNoteEditor();
1018 else if (foname=="hideInExport")
1019 model->toggleHideExport();
1020 // FIXME-3 needed? xelection.update();
1024 // No system flag clicked, take care of modmodes (CTRL-Click)
1025 if (e->state() & Qt::ControlModifier)
1027 if (mainWindow->getModMode()==Main::ModModeColor)
1030 setCursor (PickColorCursor);
1033 if (mainWindow->getModMode()==Main::ModModeXLink)
1035 BranchItem *bi_begin=model->getSelectedBranch();
1039 tmpXLink=model->createXLink(bi_begin,true);
1040 tmpXLink->setColor(model->getMapDefXLinkColor());
1041 tmpXLink->setWidth(model->getMapDefXLinkWidth());
1042 tmpXLink->setEnd (p);
1043 tmpXLink->updateXLink();
1047 } // End of modmodes
1052 cout << "ME::mouse pressed\n";
1053 cout << " lmo="<<lmo<<endl;
1054 cout << " ti="<<ti->getHeadingStd()<<endl;
1056 // Select the clicked object
1061 // Left Button Move Branches
1062 if (e->button() == Qt::LeftButton )
1064 movingObj_start.setX( p.x() - lmo->x() );
1065 movingObj_start.setY( p.y() - lmo->y() );
1066 movingObj_orgPos.setX (lmo->x() );
1067 movingObj_orgPos.setY (lmo->y() );
1069 movingObj_orgRelPos=lmo->getRelPos();
1071 // If modMode==copy, then we want to "move" the _new_ object around
1072 // then we need the offset from p to the _old_ selection, because of tmp
1073 if (mainWindow->getModMode()==Main::ModModeCopy &&
1074 e->state() & Qt::ControlModifier)
1076 BranchItem *bi=model->getSelectedBranch();
1080 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
1081 //bi->addBranch (model->getSelectedBranch());
1083 model->select(bi->getLastBranch());
1084 model->reposition();
1088 movingObj=model->getSelectedLMO();
1090 // Middle Button Toggle Scroll
1091 // (On Mac OS X this won't work, but we still have
1092 // a button in the toolbar)
1093 if (e->button() == Qt::MidButton )
1094 model->toggleScroll();
1095 // model->updateActions(); FIXME-3 needed?
1096 // FIXME-3 needed? xelection.update();
1098 { // No MapObj found, we are on the scene itself
1099 // Left Button move Pos of sceneView
1100 if (e->button() == Qt::LeftButton )
1102 movingObj=NULL; // move Content not Obj
1103 movingObj_start=e->globalPos();
1104 movingCont_start=QPointF (
1105 horizontalScrollBar()->value(),
1106 verticalScrollBar()->value());
1107 movingVec=QPointF(0,0);
1108 setCursor(HandOpenCursor);
1113 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1115 QPointF p = mapToScene(e->pos());
1116 TreeItem *seli=model->getSelectedItem();
1117 LinkableMapObj* lmosel=NULL;
1118 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1119 lmosel=((MapItem*)seli)->getLMO();
1121 // Move the selected MapObj
1122 if ( lmosel && movingObj)
1124 // reset cursor if we are moving and don't copy
1125 if (mainWindow->getModMode()!=Main::ModModeCopy)
1126 setCursor (Qt::ArrowCursor);
1128 // To avoid jumping of the sceneView, only
1129 // show selection, if not tmp linked
1130 if (!lmosel->hasParObjTmp())
1131 model->emitShowSelection();
1133 // Now move the selection, but add relative position
1134 // (movingObj_start) where selection was chosen with
1135 // mousepointer. (This avoids flickering resp. jumping
1136 // of selection back to absPos)
1138 // Check if we could link
1139 TreeItem *ti=findMapItem (p, seli);
1140 BranchItem *dsti=NULL;
1141 LinkableMapObj* dst=NULL;
1142 if (ti && ti!=seli && ti->isBranchLikeType())
1144 dsti=(BranchItem*)ti;
1150 if (lmosel && seli->getType()==TreeItem::Image)
1152 FloatObj *fio=(FloatImageObj*)lmosel;
1153 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1155 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1156 model->emitSelectionChanged(); // position has changed
1158 // Relink float to new mapcenter or branch, if shift is pressed
1159 // Only relink, if selection really has a new parent
1160 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1162 // Also save the move which was done so far
1163 QString pold=qpointFToString(movingObj_orgRelPos);
1164 QString pnow=qpointFToString(fio->getRelPos());
1170 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1171 fio->getParObj()->requestReposition();
1172 model->reposition();
1174 model->relinkImage ((ImageItem*) seli,dsti);
1175 model->select (seli);
1176 //movingObj=lmosel; //FIXME-3
1177 //movingObj_orgRelPos=lmosel->getRelPos();
1179 model->reposition();
1182 { // selection != a FloatObj
1183 if (seli->depth()==0) //FIXME-1 also moved mapcenters could be linked, but not working here...
1186 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1187 ((BranchObj*)lmosel)->moveBy(
1188 QPointF(p.x() -movingObj_start.x(),
1189 p.y()-movingObj_start.y()) );
1191 lmosel->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1192 model->updateRelPositions();
1195 if (seli->depth()==1)
1198 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1199 lmosel->setRelPos();
1202 // Move ordinary branch
1203 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1204 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1205 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1206 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1208 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1209 lmosel->setRelPos();
1213 // Maybe we can relink temporary?
1216 if (e->modifiers()==Qt::ControlModifier)
1218 // Special case: CTRL to link below dst
1219 lmosel->setParObjTmp (dst,p,+1);
1221 else if (e->modifiers()==Qt::ShiftModifier)
1222 lmosel->setParObjTmp (dst,p,-1);
1224 lmosel->setParObjTmp (dst,p,0);
1227 lmosel->unsetParObjTmp();
1229 // reposition subbranch
1230 lmosel->reposition();
1232 QItemSelection sel=model->getSelectionModel()->selection();
1233 updateSelection(sel,sel); // position has changed
1235 } // no FloatImageObj
1239 } // selection && moving_obj
1241 // Draw a link from one branch to another
1244 tmpXLink->setEnd (p);
1245 tmpXLink->updateXLink();
1249 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1251 QPointF p=e->globalPos();
1252 movingVec.setX(-p.x() + movingObj_start.x() );
1253 movingVec.setY(-p.y() + movingObj_start.y() );
1254 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1255 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1260 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1262 QPointF p = mapToScene(e->pos());
1263 TreeItem *seli=model->getSelectedItem();
1265 TreeItem *dsti=NULL;
1266 if (seli) dsti=findMapItem(p, seli);
1267 LinkableMapObj* dst=NULL;
1268 if (dsti && dsti->isBranchLikeType ())
1269 dst=((MapItem*)dsti)->getLMO();
1274 // Have we been picking color?
1278 setCursor (Qt::ArrowCursor);
1279 // Check if we are over another branch
1282 if (e->state() & Qt::ShiftModifier)
1283 model->colorBranch (((BranchObj*)dst)->getColor());
1285 model->colorSubtree (((BranchObj*)dst)->getColor());
1290 // Have we been drawing a link?
1294 // Check if we are over another branch
1297 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1298 tmpXLink->updateXLink();
1299 tmpXLink->activate();
1301 tmpXLink,QString("delete ()"),
1302 dsti,QString("addXLink (\"%1\",\"%2\")").arg(model->getSelectString(tmpXLink->getBegin())).arg(model->getSelectString(dsti)),
1303 QString("Add xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(dsti))
1307 model->deleteItem(tmpXLink);
1313 // Have we been moving something?
1314 if ( seli && movingObj )
1316 if (seli->getType()==TreeItem::Image)
1318 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1321 // Moved FloatObj. Maybe we need to reposition
1322 QString pold=qpointFToString(movingObj_orgRelPos);
1323 QString pnow=qpointFToString(fio->getRelPos());
1329 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1331 cout << "ME::release mouse\n";
1332 fio->getParObj()->requestReposition();
1333 model->reposition();
1337 BranchItem *bi=model->getSelectedBranch();
1338 if (bi && bi->depth()==0)
1340 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1342 QString pold=qpointFToString(movingObj_orgPos);
1343 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1350 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1354 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1355 { // A branch was moved
1356 LinkableMapObj* lmosel=NULL;
1357 lmosel=((MapItem*)seli)->getLMO();
1359 // save the position in case we link to mapcenter
1360 QPointF savePos=QPointF (lmosel->getAbsPos() );
1362 // Reset the temporary drawn link to the original one
1363 lmosel->unsetParObjTmp();
1365 // For Redo we may need to save original selection
1366 QString preSelStr=model->getSelectString(seli);
1371 // We have a destination, relink to that
1373 BranchObj* bsel=model->getSelectedBranchObj();
1375 QString preParStr=model->getSelectString (bsel->getParObj());
1376 QString preNum=QString::number (seli->num(),10);
1377 QString preDstParStr;
1380 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1382 preDstParStr=model->getSelectString (dst->getParObj());
1383 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1385 if (e->state() & Qt::ControlModifier && dst->getParObj())
1388 preDstParStr=model->getSelectString (dst->getParObj());
1389 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1392 preDstParStr=model->getSelectString(dst);
1393 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1394 if (dsti->depth()==0) bsel->move (savePos);
1398 QString postSelStr=model->getSelectString(lmosel);
1399 QString postNum=QString::number (seli->num(),10);
1401 QString undoCom="relinkTo (\""+
1402 preParStr+ "\"," + preNum +"," +
1403 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1405 QString redoCom="relinkTo (\""+
1406 preDstParStr + "\"," + postNum + "," +
1407 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1412 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1417 // No destination, undo temporary move
1419 if (seli->depth()==1)
1421 // The select string might be different _after_ moving around.
1422 // Therefor reposition and then use string of old selection, too
1423 model->reposition();
1425 QPointF rp(lmosel->getRelPos());
1426 if (rp != movingObj_orgRelPos)
1428 QString ps=qpointFToString(rp);
1430 model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1431 preSelStr, "moveRel "+ps,
1432 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1436 // Draw the original link, before selection was moved around
1437 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1439 lmosel->setRelPos(); // calc relPos first for starting point
1440 QPointF dst=bi->getBranchObj()->getParObj()->getChildPos(); // FIXME-3 check getBO here...
1441 // if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
1443 model->startAnimation(
1445 lmosel->getRelPos(),
1447 // QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
1450 model->reposition();
1453 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1454 // Finally resize scene, if needed
1458 // Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
1459 // model->updateActions(); FIXME-3 neeeded?
1461 // maybe we moved View: set old cursor
1462 setCursor (Qt::ArrowCursor);
1466 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1468 if (model->isSelectionBlocked() )
1474 if (e->button() == Qt::LeftButton )
1476 QPointF p = mapToScene(e->pos());
1477 TreeItem *ti=findMapItem (p, NULL);
1478 if (ti) { // MapObj was found
1479 // First select the MapObj than edit heading
1486 void MapEditor::resizeEvent (QResizeEvent* e)
1488 QGraphicsView::resizeEvent( e );
1491 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1493 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1494 // cerr << event->format(i) << endl;
1496 if (event->mimeData()->hasImage())
1497 event->acceptProposedAction();
1499 if (event->mimeData()->hasUrls())
1500 event->acceptProposedAction();
1503 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1507 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1512 void MapEditor::dropEvent(QDropEvent *event)
1514 BranchItem *selbi=model->getSelectedBranch();
1518 foreach (QString format,event->mimeData()->formats())
1519 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1523 if (event->mimeData()->hasImage())
1525 QVariant imageData = event->mimeData()->imageData();
1526 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1528 if (event->mimeData()->hasUrls())
1529 uris=event->mimeData()->urls();
1537 for (int i=0; i<uris.count();i++)
1539 // Workaround to avoid adding empty branches
1540 if (!uris.at(i).toString().isEmpty())
1542 bi=model->addNewBranch();
1546 s=uris.at(i).toLocalFile();
1549 QString file = QDir::fromNativeSeparators(s);
1550 heading = QFileInfo(file).baseName();
1552 if (file.endsWith(".vym", false))
1553 bi->setVymLink(file);
1555 bi->setURL(uris.at(i).toString());
1558 bo->setURL(uris.at(i).toString());
1562 if (!heading.isEmpty())
1563 bi->setHeading(heading);
1565 bi->setHeading(uris.at(i).toString());
1570 model->reposition();
1573 event->acceptProposedAction();
1576 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1578 // Note: Here we are prepared for multiple selections, though this
1579 // is not yet implemented elsewhere
1581 // Here in MapEditor we can only select Branches and Images
1582 QList <TreeItem*> treeItemsNew;
1583 QList <TreeItem*> treeItemsOld;
1586 foreach (ix,newsel.indexes() )
1588 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1589 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1590 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1592 foreach (ix,oldsel.indexes() )
1594 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1595 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1596 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1599 // Trim list of selection rectangles
1600 while (treeItemsNew.count() < selboxList.count() )
1601 delete selboxList.takeFirst();
1603 // Take care to tmp scroll/unscroll
1604 if (!oldsel.isEmpty())
1606 QModelIndex ix=oldsel.indexes().first();
1609 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1612 if (ti->isBranchLikeType() )
1614 // reset tmp scrolled branches
1615 BranchItem *bi=(BranchItem*)ti;
1616 bi->resetTmpUnscroll();
1618 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1619 // Hide link if not needed
1620 ((MapItem*)ti)->getLMO()->updateVisibility();
1625 if (!treeItemsNew.isEmpty())
1627 QModelIndex ix=newsel.indexes().first();
1630 // Temporary unscroll if necessary
1631 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1632 if (ti->isBranchLikeType() )
1634 BranchItem *bi=(BranchItem*)ti;
1635 if (bi->hasScrolledParent(bi) )
1639 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1640 // Show link if needed
1641 ((MapItem*)ti)->getLMO()->updateVisibility();
1645 // Reduce rectangles
1646 while (treeItemsNew.count() < selboxList.count() )
1647 delete selboxList.takeFirst();
1649 // Add additonal rectangles
1650 QGraphicsRectItem *sb;
1651 while (treeItemsNew.count() > selboxList.count() )
1653 sb = mapScene->addRect(
1655 QPen(selectionColor),
1657 sb->setZValue(Z_SELBOX);
1659 selboxList.append (sb);
1664 // Reposition rectangles
1668 LinkableMapObj *lmo;
1669 for (int i=0; i<treeItemsNew.count();++i)
1671 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1672 bbox=lmo->getBBox();
1673 sb=selboxList.at(i);
1676 bbox.width(), bbox.height());
1677 sb->setPen (selectionColor);
1678 sb->setBrush (selectionColor);
1685 void MapEditor::updateData (const QModelIndex &sel)
1687 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1690 cout << "ME::updateData\n";
1692 cout << " ti="<<ti<<endl;
1693 cout << " h="<<ti->getHeading().toStdString()<<endl;
1696 if (ti->isBranchLikeType())
1698 // cout << " ->updating...\n";
1699 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1704 void MapEditor::setSelectionColor (QColor col)
1707 QItemSelection sel=model->getSelectionModel()->selection();
1708 updateSelection(sel,sel);
1712 QColor MapEditor::getSelectionColor ()
1714 return selectionColor;