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)
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 (r);
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 scrollBarPosTarget=getScrollBarPos();
221 if (viewRect.left() <= left + xmargin) {
222 // need to scroll from the left
223 // if (!d->leftIndent)
224 scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
226 if (viewRect.right() >= right - xmargin) {
227 // need to scroll from the right
228 // if (!d->leftIndent)
229 scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
231 if (viewRect.top() <= top + ymargin) {
232 // need to scroll from the top
233 // if (!d->topIndent)
234 scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
236 if (viewRect.bottom() >= bottom - ymargin) {
237 // need to scroll from the bottom
238 // if (!d->topIndent)
239 scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
242 if (scrollBarPosTarget==getScrollBarPos()) return;
244 if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
245 scrollBarPosAnimation.stop();
247 if (settings.value ("/animation/use/",true).toBool() )
249 scrollBarPosAnimation.setTargetObject (this);
250 scrollBarPosAnimation.setPropertyName ("scrollBarPos");
251 scrollBarPosAnimation.setDuration(1000);
252 scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
253 scrollBarPosAnimation.setStartValue(
254 QPointF (horizontalScrollBar()->value() ,
255 verticalScrollBar()->value() ) );
256 scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
257 scrollBarPosAnimation.start();
259 setScrollBarPos (scrollBarPosTarget);
262 QPointF MapEditor::getScrollBarPosTarget()
264 return scrollBarPosTarget;
268 void MapEditor::setScrollBarPos(const QPointF &p)
271 horizontalScrollBar()->setValue(int(p.x()));
272 verticalScrollBar()->setValue(int(p.y()));
275 QPointF MapEditor::getScrollBarPos()
277 return QPointF (horizontalScrollBar()->value(),verticalScrollBar()->value());
278 //return scrollBarPos;
281 void MapEditor::setZoomFactorTarget (const qreal &zft)
283 zoomFactorTarget=zft;
284 if (zoomAnimation.state()==QtAbstractAnimation::Running)
285 zoomAnimation.stop();
286 if (settings.value ("/animation/use/",true).toBool() )
288 //zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
289 zoomAnimation.setTargetObject (this);
290 zoomAnimation.setPropertyName ("zoomFactor");
291 zoomAnimation.setDuration(1000);
292 zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
293 zoomAnimation.setStartValue(zoomFactor);
294 zoomAnimation.setEndValue(zft);
295 zoomAnimation.start();
300 qreal MapEditor::getZoomFactorTarget()
302 return zoomFactorTarget;
306 void MapEditor::setZoomFactor(const qreal &zf)
309 setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
312 qreal MapEditor::getZoomFactor()
317 void MapEditor::print()
321 printer = new QPrinter;
322 printer->setColorMode (QPrinter::Color);
323 printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
324 printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
325 printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
328 QRectF totalBBox=getTotalBBox();
330 // Try to set orientation automagically
331 // Note: Interpretation of generated postscript is amibiguous, if
332 // there are problems with landscape mode, see
333 // http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
335 if (totalBBox.width()>totalBBox.height())
336 // recommend landscape
337 printer->setOrientation (QPrinter::Landscape);
339 // recommend portrait
340 printer->setOrientation (QPrinter::Portrait);
342 if ( printer->setup(this) )
343 // returns false, if printing is canceled
345 QPainter pp(printer);
347 pp.setRenderHint(QPainter::Antialiasing,true);
349 // Don't print the visualisation of selection
352 QRectF mapRect=totalBBox;
353 QGraphicsRectItem *frame=NULL;
357 // Print frame around map
358 mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10,
359 totalBBox.width()+20, totalBBox.height()+20);
360 frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
366 double paperAspect = (double)printer->width() / (double)printer->height();
367 double mapAspect = (double)mapRect.width() / (double)mapRect.height();
369 if (mapAspect>=paperAspect)
371 // Fit horizontally to paper width
372 //pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );
373 viewBottom=(int)(printer->width()/mapAspect);
376 // Fit vertically to paper height
377 //pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());
378 viewBottom=printer->height();
383 // Print footer below map
385 font.setPointSize(10);
387 QRectF footerBox(0,viewBottom,printer->width(),15);
388 // FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
389 pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
393 QRectF (0,0,printer->width(),printer->height()-15),
394 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
397 // Viewport has paper dimension
398 if (frame) delete (frame);
403 // Save settings in vymrc
404 settings.writeEntry("/mainwindow/printerName",printer->printerName());
405 settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
406 settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
410 QRectF MapEditor::getTotalBBox() //FIXME-2 needed e.g. for image export
414 for (int i=0;i<rootItem->branchCount(); i++)
415 r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
421 QPixmap MapEditor::getPixmap()
423 QRectF mapRect=getTotalBBox();
424 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
427 pp.setRenderHints(renderHints());
429 // Don't print the visualisation of selection
432 mapScene->render ( &pp,
433 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
434 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
443 void MapEditor::setAntiAlias (bool b)
445 setRenderHint(QPainter::Antialiasing,b);
448 void MapEditor::setSmoothPixmap(bool b)
450 setRenderHint(QPainter::SmoothPixmapTransform,b);
453 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
455 // Start with mapcenter, no images allowed at rootItem
457 BranchItem *bi=model->getRootItem()->getFirstBranch();
458 TreeItem *found=NULL;
461 found=bi->findMapItem (p, exclude);
462 if (found) return found;
464 bi=model->getRootItem()->getBranchNum(i);
469 AttributeTable* MapEditor::attributeTable()
474 void MapEditor::testFunction1()
476 cout << "ME::test1 selected TI="<<model->getSelectedItem()<<endl;
478 for (int i=0; i<200;i++)
479 model->addNewBranch();
482 // Code copied from Qt sources
483 QRectF rect=model->getSelectedBranchObj()->getBBox();
487 qreal width = viewport()->width();
488 qreal height = viewport()->height();
489 QRectF viewRect = matrix().mapRect(rect);
491 qreal left = horizontalScrollBar()->value();
492 qreal right = left + width;
493 qreal top = verticalScrollBar()->value();
494 qreal bottom = top + height;
496 if (viewRect.left() <= left + xmargin) {
497 // need to scroll from the left
498 // if (!d->leftIndent)
499 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
501 if (viewRect.right() >= right - xmargin) {
502 // need to scroll from the right
503 // if (!d->leftIndent)
504 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
506 if (viewRect.top() <= top + ymargin) {
507 // need to scroll from the top
508 // if (!d->topIndent)
509 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
511 if (viewRect.bottom() >= bottom - ymargin) {
512 // need to scroll from the bottom
513 // if (!d->topIndent)
514 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
516 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
517 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
522 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
523 animation->setDuration(5000);
524 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
525 animation->setEasingCurve ( QtEasingCurve::OutQuint);
526 animation->setStartValue(sceneRect() );
527 animation->setEndValue(QRectF(50, 50, 1000, 1000));
532 QDialog *dia= new QDialog (this);
533 dia->setGeometry (50,50,10,10);
538 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
539 animation->setDuration(1000);
540 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
541 animation->setEasingCurve ( QtEasingCurve::OutQuint);
542 animation->setStartValue(QRect(50, 50, 10, 10));
543 animation->setEndValue(QRect(250, 250, 100, 100));
548 /* FIXME-4 Hide hidden stuff temporary, maybe add this as regular function somewhere
549 if (hidemode==HideNone)
551 setHideTmpMode (HideExport);
552 mapCenter->calcBBoxSizeWithChilds();
553 QRectF totalBBox=mapCenter->getTotalBBox();
554 QRectF mapRect=totalBBox;
555 QCanvasRectangle *frame=NULL;
557 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
559 mapRect.setRect (totalBBox.x(), totalBBox.y(),
560 totalBBox.width(), totalBBox.height());
561 frame=new QCanvasRectangle (mapRect,mapScene);
562 frame->setBrush (QColor(white));
563 frame->setPen (QColor(black));
569 setHideTmpMode (HideNone);
571 cout <<" hidemode="<<hidemode<<endl;
576 if (hidemode==HideExport)
577 setHideTmpMode (HideNone);
579 setHideTmpMode (HideExport);
584 void MapEditor::testFunction2()
586 // Create list with all bounding polygons
587 QList <LinkableMapObj*> mapobjects;
588 QList <ConvexPolygon> polys;
590 QList <Vector> vectors;
591 QList <Vector> orgpos;
592 QStringList headings; //FIXME-3 testing only
598 // Outer loop: Iterate until we no more changes in orientation
599 bool orientationChanged=true;
600 while (orientationChanged)
602 BranchItem *ri=model->getRootItem();
603 for (int i=0;i<ri->branchCount();++i)
605 bi=ri->getBranchNum (i);
606 bo=(BranchObj*)bi->getLMO();
609 mapobjects.append (bo);
610 p=bo->getBoundingPolygon();
613 vectors.append (QPointF(0,0));
614 orgpos.append (p.at(0));
615 headings.append (bi->getHeading());
617 for (int j=0;j<bi->branchCount();++j)
619 bi2=bi->getBranchNum (j);
620 bo=(BranchObj*)bi2->getLMO();
623 mapobjects.append (bo);
624 p=bo->getBoundingPolygon();
627 vectors.append (QPointF(0,0));
628 orgpos.append (p.at(0));
629 headings.append (bi2->getHeading());
634 // Iterate moving bounding polygons until we have no more collisions
639 for (int i=0; i<polys.size()-1; ++i)
641 for (int j=i+1; j<polys.size();++j)
643 if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
646 //cout << "Collision: "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<endl;
647 v=polys.at(j).centroid()-polys.at(i).centroid();
648 // Move also away if centroids are identical
651 //cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<endl;
652 v.setX (rand()%200 -100);
653 v.setY (rand()%200 -100);
658 //cout << " v="<<v<<endl;
665 for (int i=0;i<vectors.size();i++)
667 //cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
668 polys[i].translate (vectors[i]);
670 cout << "Collisions: "<<collisions<<endl;
674 // Finally move the real objects and update
675 QList <LinkableMapObj::Orientation> orients;
676 for (int i=0;i<polys.size();i++)
678 Vector v=polys[i].at(0)-orgpos[i];
679 orients.append (mapobjects[i]->getOrientation());
680 mapobjects[i]->moveBy(v.x(),v.y() );
681 mapobjects[i]->setRelPos();
684 orientationChanged=false;
685 for (int i=0;i<polys.size();i++)
686 if (orients[i]!=mapobjects[i]->getOrientation())
688 orientationChanged=true;
691 cout << "Final: orientChanged="<<orientationChanged<<endl;
693 //orientationChanged=false;
694 } // loop if orientation has changed
696 model->emitSelectionChanged();
699 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
704 if (i>0) return bi->parent()->getBranchNum(i-1);
709 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
713 int dz=selbi->depth(); // original depth
715 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
720 // Look for branch with same parent but directly above
722 bi=getBranchDirectBelow(selbi);
724 bi=getBranchDirectAbove (selbi);
727 // direct predecessor
730 // Go towards center and look for predecessor
731 while (selbi->depth()>0)
733 selbi=(BranchItem*)(selbi->parent());
734 if (selbi->depth()==1 && invert)
735 bi=getBranchDirectBelow (selbi);
737 bi=getBranchDirectAbove (selbi);
742 while (selbi->depth()<dz)
744 // try to get back to original depth dz
745 bi=selbi->getLastBranch();
759 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
764 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
769 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
774 int dz=selbi->depth(); // original depth
776 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
780 // Look for branch with same parent but directly below
782 bi=getBranchDirectAbove (selbi);
784 bi=getBranchDirectBelow (selbi);
790 // Go towards center and look for neighbour
791 while (selbi->depth()>0)
793 selbi=(BranchItem*)(selbi->parent());
794 if (selbi->depth()==1 && invert)
795 bi=getBranchDirectAbove (selbi);
797 bi=getBranchDirectBelow (selbi);
802 while (selbi->depth()<dz)
804 // try to get back to original depth dz
805 bi=selbi->getFirstBranch();
819 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
824 // Special case: use alternative selection index
825 return bi->getLastSelectedBranchAlt();
826 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
828 return (BranchItem*)(bi->parent());
831 if (bi->getType()== TreeItem::Branch )
832 return bi->getLastSelectedBranch();
837 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
841 if (bi->depth()==0) return bi->getLastSelectedBranch();
842 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
844 return (BranchItem*)(bi->parent());
847 if (bi->getType()== TreeItem::Branch )
848 return (BranchItem*)bi->getLastSelectedBranch();
855 void MapEditor::cursorUp()
857 BranchItem *bi=model->getSelectedBranch();
858 if (bi) model->select (getBranchAbove(bi));
861 void MapEditor::cursorDown()
864 BranchItem *bi=model->getSelectedBranch();
865 if (bi) model->select (getBranchBelow(bi));
868 void MapEditor::cursorLeft()
870 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
871 if (bi) model->select (bi);
874 void MapEditor::cursorRight()
876 BranchItem *bi=getRightBranch (model->getSelectedBranch());
877 if (bi) model->select (bi);
880 void MapEditor::cursorFirst()
882 model->selectFirstBranch();
885 void MapEditor::cursorLast()
887 model->selectLastBranch();
891 void MapEditor::editHeading()
895 editHeadingFinished();
898 BranchObj *bo=model->getSelectedBranchObj();
899 BranchItem *bi=model->getSelectedBranch();
902 model->setSelectionBlocked(true);
904 lineEdit->setText (bi->getHeading());
905 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
906 lineEdit->setGeometry(p.x(),p.y(),230,25);
907 //lineEdit->selectAll();
908 //lineEdit->setCursorPosition (1);
910 lineEdit->setFocus();
911 lineEdit->grabKeyboard();
916 void MapEditor::editHeadingFinished()
918 editingHeading=false;
919 lineEdit->releaseKeyboard();
920 model->setHeading (lineEdit->text() );
921 model->setSelectionBlocked(false);
924 // Maybe reselect previous branch
925 mainWindow->editHeadingFinished (model);
929 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
931 // Lineedits are already closed by preceding
932 // mouseEvent, we don't need to close here.
934 QPointF p = mapToScene(e->pos());
935 TreeItem *ti=findMapItem (p, NULL);
936 LinkableMapObj* lmo=NULL;
937 if (ti) lmo=((MapItem*)ti)->getLMO();
940 { // MapObj was found
941 if (model->getSelectedLMO() != lmo)
947 if (model->getSelectedBranchObj() )
949 // Context Menu on branch or mapcenter
950 branchContextMenu->popup(e->globalPos() );
953 if (model->getSelectedImage() )
955 // Context Menu on floatimage
956 floatimageContextMenu->popup(e->globalPos() );
960 { // No MapObj found, we are on the Canvas itself
961 // Context Menu on scene
963 // Open context menu synchronously to position new mapcenter
964 model->setContextPos (p);
965 canvasContextMenu->exec(e->globalPos() );
966 model->unsetContextPos ();
971 void MapEditor::keyPressEvent(QKeyEvent* e)
973 if (e->modifiers() & Qt::ControlModifier)
975 switch (mainWindow->getModMode())
977 case Main::ModModeColor:
978 setCursor (PickColorCursor);
980 case Main::ModModeCopy:
981 setCursor (CopyCursor);
983 case Main::ModModeXLink:
984 setCursor (XLinkCursor);
987 setCursor (Qt::ArrowCursor);
993 void MapEditor::keyReleaseEvent(QKeyEvent* e)
995 if (!(e->modifiers() & Qt::ControlModifier))
996 setCursor (Qt::ArrowCursor);
999 void MapEditor::mousePressEvent(QMouseEvent* e)
1001 cout << "ME::mousePressed\n";
1002 // Ignore right clicks, these will go to context menus
1003 if (e->button() == Qt::RightButton )
1005 cout << " ME::ignoring right mouse event...\n";
1010 //Ignore clicks while editing heading
1011 if (model->isSelectionBlocked() )
1013 cout << " ME::ignoring other mouse event...\n";
1018 QPointF p = mapToScene(e->pos());
1019 TreeItem *ti=findMapItem (p, NULL);
1020 LinkableMapObj* lmo=NULL;
1021 if (ti) lmo=((MapItem*)ti)->getLMO();
1026 //Take care of system flags _or_ modifier modes
1028 if (lmo && ti->isBranchLikeType() )
1030 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
1031 if (!foname.isEmpty())
1033 // systemFlag clicked
1034 model->select (lmo);
1035 if (foname=="system-url")
1037 if (e->state() & Qt::ControlModifier)
1038 mainWindow->editOpenURLTab();
1040 mainWindow->editOpenURL();
1042 else if (foname=="system-vymLink")
1044 mainWindow->editOpenVymLink();
1045 // tabWidget may change, better return now
1046 // before segfaulting...
1047 } else if (foname=="note")
1048 mainWindow->windowToggleNoteEditor();
1049 else if (foname=="hideInExport")
1050 model->toggleHideExport();
1051 // FIXME-3 needed? xelection.update();
1055 // No system flag clicked, take care of modmodes (CTRL-Click)
1056 if (e->state() & Qt::ControlModifier)
1058 if (mainWindow->getModMode()==Main::ModModeColor)
1061 setCursor (PickColorCursor);
1064 if (mainWindow->getModMode()==Main::ModModeXLink)
1066 BranchItem *bi_begin=model->getSelectedBranch();
1070 tmpXLink=model->createXLink(bi_begin,true);
1071 tmpXLink->setColor(model->getMapDefXLinkColor());
1072 tmpXLink->setWidth(model->getMapDefXLinkWidth());
1073 tmpXLink->setEnd (p);
1074 tmpXLink->updateXLink();
1078 } // End of modmodes
1083 cout << "ME::mouse pressed\n";
1084 cout << " lmo="<<lmo<<endl;
1085 cout << " ti="<<ti->getHeadingStd()<<endl;
1087 // Select the clicked object
1092 // Left Button Move Branches
1093 if (e->button() == Qt::LeftButton )
1095 movingObj_start.setX( p.x() - lmo->x() );
1096 movingObj_start.setY( p.y() - lmo->y() );
1097 movingObj_orgPos.setX (lmo->x() );
1098 movingObj_orgPos.setY (lmo->y() );
1100 movingObj_orgRelPos=lmo->getRelPos();
1102 // If modMode==copy, then we want to "move" the _new_ object around
1103 // then we need the offset from p to the _old_ selection, because of tmp
1104 if (mainWindow->getModMode()==Main::ModModeCopy &&
1105 e->state() & Qt::ControlModifier)
1107 BranchItem *bi=model->getSelectedBranch();
1111 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
1112 //bi->addBranch (model->getSelectedBranch());
1114 model->select(bi->getLastBranch());
1115 model->reposition();
1119 movingObj=model->getSelectedLMO();
1121 // Middle Button Toggle Scroll
1122 // (On Mac OS X this won't work, but we still have
1123 // a button in the toolbar)
1124 if (e->button() == Qt::MidButton )
1125 model->toggleScroll();
1126 // model->updateActions(); FIXME-3 needed?
1127 // FIXME-3 needed? xelection.update();
1129 { // No MapObj found, we are on the scene itself
1130 // Left Button move Pos of sceneView
1131 if (e->button() == Qt::LeftButton )
1133 movingObj=NULL; // move Content not Obj
1134 movingObj_start=e->globalPos();
1135 movingCont_start=QPointF (
1136 horizontalScrollBar()->value(),
1137 verticalScrollBar()->value());
1138 movingVec=QPointF(0,0);
1139 setCursor(HandOpenCursor);
1144 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1146 QPointF p = mapToScene(e->pos());
1147 TreeItem *seli=model->getSelectedItem();
1148 LinkableMapObj* lmosel=NULL;
1149 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1150 lmosel=((MapItem*)seli)->getLMO();
1152 // Move the selected MapObj
1153 if ( lmosel && movingObj)
1156 // reset cursor if we are moving and don't copy
1157 if (mainWindow->getModMode()!=Main::ModModeCopy)
1158 setCursor (Qt::ArrowCursor);
1160 // To avoid jumping of the sceneView, only
1161 // show selection, if not tmp linked
1162 /* FIXME-2 if (!lmosel->hasParObjTmp())
1163 model->emitShowSelection();
1166 // Now move the selection, but add relative position
1167 // (movingObj_start) where selection was chosen with
1168 // mousepointer. (This avoids flickering resp. jumping
1169 // of selection back to absPos)
1171 // Check if we could link
1172 TreeItem *ti=findMapItem (p, seli);
1173 BranchItem *dsti=NULL;
1174 LinkableMapObj* dst=NULL;
1175 if (ti && ti!=seli && ti->isBranchLikeType())
1177 dsti=(BranchItem*)ti;
1183 if (lmosel && seli->getType()==TreeItem::Image)
1185 FloatObj *fio=(FloatImageObj*)lmosel;
1186 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1188 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1189 model->emitSelectionChanged(); // position has changed
1191 // Relink float to new mapcenter or branch, if shift is pressed
1192 // Only relink, if selection really has a new parent
1193 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1195 // Also save the move which was done so far
1196 QString pold=qpointFToString(movingObj_orgRelPos);
1197 QString pnow=qpointFToString(fio->getRelPos());
1203 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1204 fio->getParObj()->requestReposition();
1205 model->reposition();
1207 model->relinkImage ((ImageItem*) seli,dsti);
1208 model->select (seli);
1209 //movingObj=lmosel; //FIXME-3
1210 //movingObj_orgRelPos=lmosel->getRelPos();
1212 model->reposition();
1215 { // selection != a FloatObj
1216 if (seli->depth()==0)
1218 lmosel->move (p-movingObj_start);
1219 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1222 v=lmosel->getAbsPos();
1223 for (int i=0; i<seli->branchCount(); ++i)
1225 seli->getBranchObjNum(i)->setRelPos();
1226 seli->getBranchObjNum(i)->setOrientation();
1231 if (seli->depth()==1)
1234 lmosel->move(p-movingObj_start);
1235 lmosel->setRelPos();
1238 // Move ordinary branch
1239 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1240 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1241 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1242 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1244 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1245 lmosel->setRelPos();
1249 // Maybe we can relink temporary?
1250 if (dsti) // FIXME-1 check if dsti is ancestor of myself!
1252 if (e->modifiers()==Qt::ControlModifier)
1254 // Special case: CTRL to link below dst
1255 lmosel->setParObjTmp (dst,p,+1);
1257 else if (e->modifiers()==Qt::ShiftModifier)
1258 lmosel->setParObjTmp (dst,p,-1);
1260 lmosel->setParObjTmp (dst,p,0);
1263 lmosel->unsetParObjTmp();
1265 // reposition subbranch
1266 lmosel->reposition();
1268 QItemSelection sel=model->getSelectionModel()->selection();
1269 updateSelection(sel,sel); // position has changed
1271 } // no FloatImageObj
1275 } // selection && moving_obj
1277 // Draw a link from one branch to another
1280 tmpXLink->setEnd (p);
1281 tmpXLink->updateXLink();
1285 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1287 QPointF p=e->globalPos();
1288 movingVec.setX(-p.x() + movingObj_start.x() );
1289 movingVec.setY(-p.y() + movingObj_start.y() );
1290 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1291 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1296 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1298 QPointF p = mapToScene(e->pos());
1299 TreeItem *seli=model->getSelectedItem();
1301 TreeItem *dsti=NULL;
1302 if (seli) dsti=findMapItem(p, seli);
1303 LinkableMapObj* dst=NULL;
1304 if (dsti && dsti->isBranchLikeType ())
1305 dst=((MapItem*)dsti)->getLMO();
1310 // Have we been picking color?
1314 setCursor (Qt::ArrowCursor);
1315 // Check if we are over another branch
1318 if (e->state() & Qt::ShiftModifier)
1319 model->colorBranch (((BranchObj*)dst)->getColor());
1321 model->colorSubtree (((BranchObj*)dst)->getColor());
1326 // Have we been drawing a link?
1330 // Check if we are over another branch
1333 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1334 tmpXLink->updateXLink();
1335 tmpXLink->activate();
1337 tmpXLink,QString("delete ()"),
1338 dsti,QString("addXLink (\"%1\",\"%2\")").arg(model->getSelectString(tmpXLink->getBegin())).arg(model->getSelectString(dsti)),
1339 QString("Add xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(dsti))
1343 model->deleteItem(tmpXLink);
1349 // Have we been moving something?
1350 if ( seli && movingObj )
1352 if (seli->getType()==TreeItem::Image)
1354 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1357 // Moved FloatObj. Maybe we need to reposition
1358 QString pold=qpointFToString(movingObj_orgRelPos);
1359 QString pnow=qpointFToString(fio->getRelPos());
1365 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1367 cout << "ME::release mouse\n";
1368 fio->getParObj()->requestReposition();
1369 model->reposition();
1373 BranchItem *bi=model->getSelectedBranch();
1374 if (bi && bi->depth()==0)
1376 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1378 QString pold=qpointFToString(movingObj_orgPos);
1379 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1386 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1390 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1391 { // A branch was moved
1392 LinkableMapObj* lmosel=NULL;
1393 lmosel=((MapItem*)seli)->getLMO();
1395 // save the position in case we link to mapcenter
1396 QPointF savePos=QPointF (lmosel->getAbsPos() );
1398 // Reset the temporary drawn link to the original one
1399 lmosel->unsetParObjTmp();
1401 // For Redo we may need to save original selection
1402 QString preSelStr=model->getSelectString(seli);
1405 if (dsti && objectMoved)
1407 // We have a destination, relink to that
1409 BranchObj* bsel=model->getSelectedBranchObj();
1411 QString preParStr=model->getSelectString (bsel->getParObj());
1412 QString preNum=QString::number (seli->num(),10);
1413 QString preDstParStr;
1416 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1418 preDstParStr=model->getSelectString (dst->getParObj());
1419 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1421 if (e->state() & Qt::ControlModifier && dst->getParObj())
1424 preDstParStr=model->getSelectString (dst->getParObj());
1425 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1428 preDstParStr=model->getSelectString(dst);
1429 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1430 if (dsti->depth()==0) bsel->move (savePos);
1434 QString postSelStr=model->getSelectString(lmosel);
1435 QString postNum=QString::number (seli->num(),10);
1437 QString undoCom="relinkTo (\""+
1438 preParStr+ "\"," + preNum +"," +
1439 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1441 QString redoCom="relinkTo (\""+
1442 preDstParStr + "\"," + postNum + "," +
1443 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1448 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1453 // No destination, undo temporary move
1455 if (seli->depth()==1)
1457 // The select string might be different _after_ moving around.
1458 // Therefor reposition and then use string of old selection, too
1459 model->reposition();
1461 QPointF rp(lmosel->getRelPos());
1462 if (rp != movingObj_orgRelPos)
1464 QString ps=qpointFToString(rp);
1466 model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1467 preSelStr, "moveRel "+ps,
1468 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1472 // Draw the original link, before selection was moved around
1473 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1475 lmosel->setRelPos(); // calc relPos first for starting point
1477 model->startAnimation(
1479 lmosel->getRelPos(),
1483 model->reposition();
1486 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1487 // Finally resize scene, if needed
1493 // maybe we moved View: set old cursor
1494 setCursor (Qt::ArrowCursor);
1498 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1502 cout << "ME p="<<mapToScene (e->pos())<<" scrollBarPos="<<getScrollBarPos();
1503 cout << " min="<<QPointF(horizontalScrollBar()->minimum(),verticalScrollBar()->minimum());
1508 if (model->isSelectionBlocked() )
1514 if (e->button() == Qt::LeftButton )
1516 QPointF p = mapToScene(e->pos());
1517 TreeItem *ti=findMapItem (p, NULL);
1518 if (ti) { // MapObj was found
1519 // First select the MapObj than edit heading
1526 void MapEditor::resizeEvent (QResizeEvent* e)
1528 QGraphicsView::resizeEvent( e );
1531 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1533 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1534 // cerr << event->format(i) << endl;
1536 if (event->mimeData()->hasImage())
1537 event->acceptProposedAction();
1539 if (event->mimeData()->hasUrls())
1540 event->acceptProposedAction();
1543 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1547 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1552 void MapEditor::dropEvent(QDropEvent *event)
1554 BranchItem *selbi=model->getSelectedBranch();
1558 foreach (QString format,event->mimeData()->formats())
1559 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1563 if (event->mimeData()->hasImage())
1565 QVariant imageData = event->mimeData()->imageData();
1566 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1568 if (event->mimeData()->hasUrls())
1569 uris=event->mimeData()->urls();
1577 for (int i=0; i<uris.count();i++)
1579 // Workaround to avoid adding empty branches
1580 if (!uris.at(i).toString().isEmpty())
1582 bi=model->addNewBranch();
1586 s=uris.at(i).toLocalFile();
1589 QString file = QDir::fromNativeSeparators(s);
1590 heading = QFileInfo(file).baseName();
1592 if (file.endsWith(".vym", false))
1593 bi->setVymLink(file);
1595 bi->setURL(uris.at(i).toString());
1598 bo->setURL(uris.at(i).toString());
1602 if (!heading.isEmpty())
1603 bi->setHeading(heading);
1605 bi->setHeading(uris.at(i).toString());
1610 model->reposition();
1613 event->acceptProposedAction();
1616 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1618 // Note: Here we are prepared for multiple selections, though this
1619 // is not yet implemented elsewhere
1621 // Here in MapEditor we can only select Branches and Images
1622 QList <TreeItem*> treeItemsNew;
1623 QList <TreeItem*> treeItemsOld;
1625 bool do_reposition=false;
1628 foreach (ix,newsel.indexes() )
1630 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1631 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1632 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1634 foreach (ix,oldsel.indexes() )
1636 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1637 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1638 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1641 // Trim list of selection rectangles
1642 while (treeItemsNew.count() < selboxList.count() )
1643 delete selboxList.takeFirst();
1645 // Take care to tmp scroll/unscroll
1646 if (!oldsel.isEmpty())
1648 QModelIndex ix=oldsel.indexes().first();
1651 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1654 if (ti->isBranchLikeType() )
1656 // reset tmp scrolled branches
1657 BranchItem *bi=(BranchItem*)ti;
1658 if (bi->resetTmpUnscroll() )
1661 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1662 // Hide link if not needed
1663 ((MapItem*)ti)->getLMO()->updateVisibility();
1668 if (!treeItemsNew.isEmpty())
1670 QModelIndex ix=newsel.indexes().first();
1673 // Temporary unscroll if necessary
1674 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1675 if (ti->isBranchLikeType() )
1677 BranchItem *bi=(BranchItem*)ti;
1678 if (bi->hasScrolledParent(bi) )
1680 if (bi->parentBranch()->tmpUnscroll() )
1684 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1685 // Show link if needed
1686 ((MapItem*)ti)->getLMO()->updateVisibility();
1689 // FIXME-3 cout << "ME::updateSel doRepos="<<do_reposition<<endl;
1690 if (do_reposition) model->reposition();
1692 // Reduce rectangles
1693 while (treeItemsNew.count() < selboxList.count() )
1694 delete selboxList.takeFirst();
1696 // Add additonal rectangles
1697 QGraphicsRectItem *sb;
1698 while (treeItemsNew.count() > selboxList.count() )
1700 sb = mapScene->addRect(
1702 QPen(selectionColor),
1704 sb->setZValue(Z_SELBOX);
1706 selboxList.append (sb);
1709 // Reposition rectangles
1713 LinkableMapObj *lmo;
1714 for (int i=0; i<treeItemsNew.count();++i)
1716 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1717 bbox=lmo->getBBox();
1718 sb=selboxList.at(i);
1721 bbox.width(), bbox.height());
1722 sb->setPen (selectionColor);
1723 sb->setBrush (selectionColor);
1730 void MapEditor::updateData (const QModelIndex &sel)
1732 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1735 cout << "ME::updateData\n";
1737 cout << " ti="<<ti<<endl;
1738 cout << " h="<<ti->getHeading().toStdString()<<endl;
1741 if (ti->isBranchLikeType())
1743 // cout << " ->updating...\n";
1744 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1749 void MapEditor::setSelectionColor (QColor col)
1752 QItemSelection sel=model->getSelectionModel()->selection();
1753 updateSelection(sel,sel);
1757 QColor MapEditor::getSelectionColor ()
1759 return selectionColor;