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
128 a->setShortcutContext (Qt::WidgetShortcut);
130 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
131 a = new QAction( tr( "Edit heading","MapEditor" ), this);
132 a->setShortcut ( Qt::Key_Enter); //Edit heading
134 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
137 selectionColor =QColor (255,255,0);
140 // Attributes //FIXME-2 testing only...
143 attrTable= new AttributeTable();
145 ad=attrTable->addKey (k,StringList);
149 sl <<"val 1"<<"val 2"<< "val 3";
150 ad->setValue (QVariant (sl));
152 //attrTable->addValue ("Key A","P 1");
153 //attrTable->addValue ("Key A","P 2");
154 //attrTable->addValue ("Key A","P 3");
155 //attrTable->addValue ("Key A","P 4");
157 ad=attrTable->addKey (k,FreeString);
160 //attrTable->addValue ("Key B","w1");
161 //attrTable->addValue ("Key B","w2");
163 k="C - UniqueString";
164 ad=attrTable->addKey (k,UniqueString);
167 //attrTable->addKey ("Key Prio");
168 //attrTable->addValue ("Key Prio","Prio 1");
169 //attrTable->addValue ("Key Prio","Prio 2");
173 MapEditor::~MapEditor()
175 //cout <<"Destructor MapEditor for "<<model->getMapName().toStdString()<<endl;
176 model->unregisterEditor(this);
179 VymModel* MapEditor::getModel()
184 QGraphicsScene * MapEditor::getScene()
189 void MapEditor::scrollTo (const QModelIndex &index)
193 LinkableMapObj* lmo=NULL;
194 TreeItem *ti= static_cast<TreeItem*>(index.internalPointer());
195 if (ti->getType()==TreeItem::Image ||ti->isBranchLikeType() )
196 lmo=((MapItem*)ti)->getLMO();
199 QRectF r=lmo->getBBox();
200 setScrollBarPosTarget (r);
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 scrollBarPosTarget=getScrollBarPos();
222 if (viewRect.left() <= left + xmargin) {
223 // need to scroll from the left
224 // if (!d->leftIndent)
225 scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
227 if (viewRect.right() >= right - xmargin) {
228 // need to scroll from the right
229 // if (!d->leftIndent)
230 scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
232 if (viewRect.top() <= top + ymargin) {
233 // need to scroll from the top
234 // if (!d->topIndent)
235 scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
237 if (viewRect.bottom() >= bottom - ymargin) {
238 // need to scroll from the bottom
239 // if (!d->topIndent)
240 scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
243 if (scrollBarPosTarget==getScrollBarPos()) return;
245 if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
246 scrollBarPosAnimation.stop();
248 if (settings.value ("/animation/use/",true).toBool() )
250 scrollBarPosAnimation.setTargetObject (this);
251 scrollBarPosAnimation.setPropertyName ("scrollBarPos");
252 scrollBarPosAnimation.setDuration(1000);
253 scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
254 scrollBarPosAnimation.setStartValue(
255 QPointF (horizontalScrollBar()->value() ,
256 verticalScrollBar()->value() ) );
257 scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
258 scrollBarPosAnimation.start();
260 setScrollBarPos (scrollBarPosTarget);
263 QPointF MapEditor::getScrollBarPosTarget()
265 return scrollBarPosTarget;
269 void MapEditor::setScrollBarPos(const QPointF &p)
272 horizontalScrollBar()->setValue(int(p.x()));
273 verticalScrollBar()->setValue(int(p.y()));
276 QPointF MapEditor::getScrollBarPos()
278 return QPointF (horizontalScrollBar()->value(),verticalScrollBar()->value());
279 //return scrollBarPos;
282 void MapEditor::setZoomFactorTarget (const qreal &zft)
284 zoomFactorTarget=zft;
285 if (zoomAnimation.state()==QtAbstractAnimation::Running)
286 zoomAnimation.stop();
287 if (settings.value ("/animation/use/",true).toBool() )
289 //zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
290 zoomAnimation.setTargetObject (this);
291 zoomAnimation.setPropertyName ("zoomFactor");
292 zoomAnimation.setDuration(1000);
293 zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
294 zoomAnimation.setStartValue(zoomFactor);
295 zoomAnimation.setEndValue(zft);
296 zoomAnimation.start();
301 qreal MapEditor::getZoomFactorTarget()
303 return zoomFactorTarget;
307 void MapEditor::setZoomFactor(const qreal &zf)
310 setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
313 qreal MapEditor::getZoomFactor()
318 void MapEditor::print()
322 printer = new QPrinter;
323 printer->setColorMode (QPrinter::Color);
324 printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
325 printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
326 printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
329 QRectF totalBBox=getTotalBBox();
331 // Try to set orientation automagically
332 // Note: Interpretation of generated postscript is amibiguous, if
333 // there are problems with landscape mode, see
334 // http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
336 if (totalBBox.width()>totalBBox.height())
337 // recommend landscape
338 printer->setOrientation (QPrinter::Landscape);
340 // recommend portrait
341 printer->setOrientation (QPrinter::Portrait);
343 if ( printer->setup(this) )
344 // returns false, if printing is canceled
346 QPainter pp(printer);
348 pp.setRenderHint(QPainter::Antialiasing,true);
350 // Don't print the visualisation of selection
353 QRectF mapRect=totalBBox;
354 QGraphicsRectItem *frame=NULL;
358 // Print frame around map
359 mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10,
360 totalBBox.width()+20, totalBBox.height()+20);
361 frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
367 double paperAspect = (double)printer->width() / (double)printer->height();
368 double mapAspect = (double)mapRect.width() / (double)mapRect.height();
370 if (mapAspect>=paperAspect)
372 // Fit horizontally to paper width
373 //pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );
374 viewBottom=(int)(printer->width()/mapAspect);
377 // Fit vertically to paper height
378 //pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());
379 viewBottom=printer->height();
384 // Print footer below map
386 font.setPointSize(10);
388 QRectF footerBox(0,viewBottom,printer->width(),15);
389 // FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
390 pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
394 QRectF (0,0,printer->width(),printer->height()-15),
395 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
398 // Viewport has paper dimension
399 if (frame) delete (frame);
404 // Save settings in vymrc
405 settings.writeEntry("/mainwindow/printerName",printer->printerName());
406 settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
407 settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
411 QRectF MapEditor::getTotalBBox() //FIXME-2 needed e.g. for image export
415 for (int i=0;i<rootItem->branchCount(); i++)
416 r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
422 QPixmap MapEditor::getPixmap()
424 QRectF mapRect=getTotalBBox();
425 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
428 pp.setRenderHints(renderHints());
430 // Don't print the visualisation of selection
433 mapScene->render ( &pp,
434 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
435 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
444 void MapEditor::setAntiAlias (bool b)
446 setRenderHint(QPainter::Antialiasing,b);
449 void MapEditor::setSmoothPixmap(bool b)
451 setRenderHint(QPainter::SmoothPixmapTransform,b);
454 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
456 // Start with mapcenter, no images allowed at rootItem
458 BranchItem *bi=model->getRootItem()->getFirstBranch();
459 TreeItem *found=NULL;
462 found=bi->findMapItem (p, exclude);
463 if (found) return found;
465 bi=model->getRootItem()->getBranchNum(i);
470 AttributeTable* MapEditor::attributeTable()
475 void MapEditor::testFunction1()
477 cout << "ME::test1 selected TI="<<model->getSelectedItem()<<endl;
479 for (int i=0; i<200;i++)
480 model->addNewBranch();
483 // Code copied from Qt sources
484 QRectF rect=model->getSelectedBranchObj()->getBBox();
488 qreal width = viewport()->width();
489 qreal height = viewport()->height();
490 QRectF viewRect = matrix().mapRect(rect);
492 qreal left = horizontalScrollBar()->value();
493 qreal right = left + width;
494 qreal top = verticalScrollBar()->value();
495 qreal bottom = top + height;
497 if (viewRect.left() <= left + xmargin) {
498 // need to scroll from the left
499 // if (!d->leftIndent)
500 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
502 if (viewRect.right() >= right - xmargin) {
503 // need to scroll from the right
504 // if (!d->leftIndent)
505 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
507 if (viewRect.top() <= top + ymargin) {
508 // need to scroll from the top
509 // if (!d->topIndent)
510 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
512 if (viewRect.bottom() >= bottom - ymargin) {
513 // need to scroll from the bottom
514 // if (!d->topIndent)
515 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
517 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
518 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
523 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
524 animation->setDuration(5000);
525 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
526 animation->setEasingCurve ( QtEasingCurve::OutQuint);
527 animation->setStartValue(sceneRect() );
528 animation->setEndValue(QRectF(50, 50, 1000, 1000));
533 QDialog *dia= new QDialog (this);
534 dia->setGeometry (50,50,10,10);
539 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
540 animation->setDuration(1000);
541 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
542 animation->setEasingCurve ( QtEasingCurve::OutQuint);
543 animation->setStartValue(QRect(50, 50, 10, 10));
544 animation->setEndValue(QRect(250, 250, 100, 100));
549 /* FIXME-4 Hide hidden stuff temporary, maybe add this as regular function somewhere
550 if (hidemode==HideNone)
552 setHideTmpMode (HideExport);
553 mapCenter->calcBBoxSizeWithChilds();
554 QRectF totalBBox=mapCenter->getTotalBBox();
555 QRectF mapRect=totalBBox;
556 QCanvasRectangle *frame=NULL;
558 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
560 mapRect.setRect (totalBBox.x(), totalBBox.y(),
561 totalBBox.width(), totalBBox.height());
562 frame=new QCanvasRectangle (mapRect,mapScene);
563 frame->setBrush (QColor(white));
564 frame->setPen (QColor(black));
570 setHideTmpMode (HideNone);
572 cout <<" hidemode="<<hidemode<<endl;
577 if (hidemode==HideExport)
578 setHideTmpMode (HideNone);
580 setHideTmpMode (HideExport);
585 void MapEditor::testFunction2()
587 // Create list with all bounding polygons
588 QList <LinkableMapObj*> mapobjects;
589 QList <ConvexPolygon> polys;
591 QList <Vector> vectors;
592 QList <Vector> orgpos;
593 QStringList headings; //FIXME-3 testing only
599 // Outer loop: Iterate until we no more changes in orientation
600 bool orientationChanged=true;
601 while (orientationChanged)
603 BranchItem *ri=model->getRootItem();
604 for (int i=0;i<ri->branchCount();++i)
606 bi=ri->getBranchNum (i);
607 bo=(BranchObj*)bi->getLMO();
610 mapobjects.append (bo);
611 p=bo->getBoundingPolygon();
614 vectors.append (QPointF(0,0));
615 orgpos.append (p.at(0));
616 headings.append (bi->getHeading());
618 for (int j=0;j<bi->branchCount();++j)
620 bi2=bi->getBranchNum (j);
621 bo=(BranchObj*)bi2->getLMO();
624 mapobjects.append (bo);
625 p=bo->getBoundingPolygon();
628 vectors.append (QPointF(0,0));
629 orgpos.append (p.at(0));
630 headings.append (bi2->getHeading());
635 // Iterate moving bounding polygons until we have no more collisions
640 for (int i=0; i<polys.size()-1; ++i)
642 for (int j=i+1; j<polys.size();++j)
644 if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
647 //cout << "Collision: "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<endl;
648 v=polys.at(j).centroid()-polys.at(i).centroid();
649 // Move also away if centroids are identical
652 //cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<endl;
653 v.setX (rand()%200 -100);
654 v.setY (rand()%200 -100);
659 //cout << " v="<<v<<endl;
666 for (int i=0;i<vectors.size();i++)
668 //cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
669 polys[i].translate (vectors[i]);
671 cout << "Collisions: "<<collisions<<endl;
675 // Finally move the real objects and update
676 QList <LinkableMapObj::Orientation> orients;
677 for (int i=0;i<polys.size();i++)
679 Vector v=polys[i].at(0)-orgpos[i];
680 orients.append (mapobjects[i]->getOrientation());
681 mapobjects[i]->moveBy(v.x(),v.y() );
682 mapobjects[i]->setRelPos();
685 orientationChanged=false;
686 for (int i=0;i<polys.size();i++)
687 if (orients[i]!=mapobjects[i]->getOrientation())
689 orientationChanged=true;
692 cout << "Final: orientChanged="<<orientationChanged<<endl;
694 //orientationChanged=false;
695 } // loop if orientation has changed
697 model->emitSelectionChanged();
700 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
705 if (i>0) return bi->parent()->getBranchNum(i-1);
710 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
714 int dz=selbi->depth(); // original depth
716 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
721 // Look for branch with same parent but directly above
723 bi=getBranchDirectBelow(selbi);
725 bi=getBranchDirectAbove (selbi);
728 // direct predecessor
731 // Go towards center and look for predecessor
732 while (selbi->depth()>0)
734 selbi=(BranchItem*)(selbi->parent());
735 if (selbi->depth()==1 && invert)
736 bi=getBranchDirectBelow (selbi);
738 bi=getBranchDirectAbove (selbi);
743 while (selbi->depth()<dz)
745 // try to get back to original depth dz
746 bi=selbi->getLastBranch();
760 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
765 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
770 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
775 int dz=selbi->depth(); // original depth
777 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
781 // Look for branch with same parent but directly below
783 bi=getBranchDirectAbove (selbi);
785 bi=getBranchDirectBelow (selbi);
791 // Go towards center and look for neighbour
792 while (selbi->depth()>0)
794 selbi=(BranchItem*)(selbi->parent());
795 if (selbi->depth()==1 && invert)
796 bi=getBranchDirectAbove (selbi);
798 bi=getBranchDirectBelow (selbi);
803 while (selbi->depth()<dz)
805 // try to get back to original depth dz
806 bi=selbi->getFirstBranch();
820 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
825 // Special case: use alternative selection index
826 return bi->getLastSelectedBranchAlt();
827 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
829 return (BranchItem*)(bi->parent());
832 if (bi->getType()== TreeItem::Branch )
833 return bi->getLastSelectedBranch();
838 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
842 if (bi->depth()==0) return bi->getLastSelectedBranch();
843 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
845 return (BranchItem*)(bi->parent());
848 if (bi->getType()== TreeItem::Branch )
849 return (BranchItem*)bi->getLastSelectedBranch();
856 void MapEditor::cursorUp()
858 BranchItem *bi=model->getSelectedBranch();
859 if (bi) model->select (getBranchAbove(bi));
862 void MapEditor::cursorDown()
865 BranchItem *bi=model->getSelectedBranch();
866 if (bi) model->select (getBranchBelow(bi));
869 void MapEditor::cursorLeft()
871 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
872 if (bi) model->select (bi);
875 void MapEditor::cursorRight()
877 BranchItem *bi=getRightBranch (model->getSelectedBranch());
878 if (bi) model->select (bi);
881 void MapEditor::cursorFirst()
883 model->selectFirstBranch();
886 void MapEditor::cursorLast()
888 model->selectLastBranch();
892 void MapEditor::editHeading()
896 editHeadingFinished();
899 BranchObj *bo=model->getSelectedBranchObj();
900 BranchItem *bi=model->getSelectedBranch();
903 model->setSelectionBlocked(true);
905 lineEdit->setText (bi->getHeading());
906 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
907 lineEdit->setGeometry(p.x(),p.y(),230,25);
908 //lineEdit->selectAll();
909 //lineEdit->setCursorPosition (1);
911 lineEdit->setFocus();
912 lineEdit->grabKeyboard();
917 void MapEditor::editHeadingFinished()
919 editingHeading=false;
920 lineEdit->releaseKeyboard();
921 model->setHeading (lineEdit->text() );
922 model->setSelectionBlocked(false);
925 // Maybe reselect previous branch
926 mainWindow->editHeadingFinished (model);
930 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
932 // Lineedits are already closed by preceding
933 // mouseEvent, we don't need to close here.
935 QPointF p = mapToScene(e->pos());
936 TreeItem *ti=findMapItem (p, NULL);
937 LinkableMapObj* lmo=NULL;
938 if (ti) lmo=((MapItem*)ti)->getLMO();
941 { // MapObj was found
942 if (model->getSelectedLMO() != lmo)
948 if (model->getSelectedBranchObj() )
950 // Context Menu on branch or mapcenter
951 branchContextMenu->popup(e->globalPos() );
954 if (model->getSelectedImage() )
956 // Context Menu on floatimage
957 floatimageContextMenu->popup(e->globalPos() );
961 { // No MapObj found, we are on the Canvas itself
962 // Context Menu on scene
964 // Open context menu synchronously to position new mapcenter
965 model->setContextPos (p);
966 canvasContextMenu->exec(e->globalPos() );
967 model->unsetContextPos ();
972 void MapEditor::keyPressEvent(QKeyEvent* e)
974 if (e->modifiers() & Qt::ControlModifier)
976 switch (mainWindow->getModMode())
978 case Main::ModModeColor:
979 setCursor (PickColorCursor);
981 case Main::ModModeCopy:
982 setCursor (CopyCursor);
984 case Main::ModModeXLink:
985 setCursor (XLinkCursor);
988 setCursor (Qt::ArrowCursor);
994 void MapEditor::keyReleaseEvent(QKeyEvent* e)
996 if (!(e->modifiers() & Qt::ControlModifier))
997 setCursor (Qt::ArrowCursor);
1000 void MapEditor::mousePressEvent(QMouseEvent* e)
1002 //cout << "ME::mousePressed\n"; //FIXME-3
1003 // Ignore right clicks, these will go to context menus
1004 if (e->button() == Qt::RightButton )
1006 //cout << " ME::ignoring right mouse event...\n";
1011 //Ignore clicks while editing heading
1012 if (model->isSelectionBlocked() )
1014 //cout << " ME::ignoring other mouse event...\n";
1019 QPointF p = mapToScene(e->pos());
1020 TreeItem *ti=findMapItem (p, NULL);
1021 LinkableMapObj* lmo=NULL;
1022 if (ti) lmo=((MapItem*)ti)->getLMO();
1027 //Take care of system flags _or_ modifier modes
1029 if (lmo && ti->isBranchLikeType() )
1031 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
1032 if (!foname.isEmpty())
1034 // systemFlag clicked
1035 model->select (lmo);
1036 if (foname=="system-url")
1038 if (e->state() & Qt::ControlModifier)
1039 mainWindow->editOpenURLTab();
1041 mainWindow->editOpenURL();
1043 else if (foname=="system-vymLink")
1045 mainWindow->editOpenVymLink();
1046 // tabWidget may change, better return now
1047 // before segfaulting...
1048 } else if (foname=="system-note")
1049 mainWindow->windowToggleNoteEditor();
1050 else if (foname=="hideInExport")
1051 model->toggleHideExport();
1052 // FIXME-3 needed? xelection.update();
1056 // Take care of xLink
1057 if (ti->xlinkCount()>0 && lmo->getBBox().width()>30)
1059 if ((lmo->getOrientation()==LinkableMapObj::RightOfCenter && p.x() > lmo->getBBox().right()-20) ||
1060 (lmo->getOrientation()==LinkableMapObj::LeftOfCenter && p.x() < lmo->getBBox().left()+20) )
1062 if (ti->xlinkCount()>1)
1065 QList <QAction*> alist;
1066 for (int i=0;i<ti->xlinkCount();i++)
1067 alist.append (new QAction(ti->getXLinkNum(i)->getOtherBranch(ti)->getHeading(),&menu));
1068 menu.addActions (alist);
1069 QAction *ra=menu.exec (e->globalPos() );
1071 model->select (ti->getXLinkNum(alist.indexOf(ra))->getOtherBranch (ti));
1072 while (!alist.isEmpty())
1074 QAction *a=alist.takeFirst();
1080 model->select (ti->getXLinkNum(0)->getOtherBranch (ti));
1088 // No system flag clicked, take care of modmodes (CTRL-Click)
1089 if (e->state() & Qt::ControlModifier)
1091 if (mainWindow->getModMode()==Main::ModModeColor)
1094 setCursor (PickColorCursor);
1097 if (mainWindow->getModMode()==Main::ModModeXLink)
1099 BranchItem *bi_begin=model->getSelectedBranch();
1103 tmpXLink=model->createXLink(bi_begin,true);
1104 tmpXLink->setColor(model->getMapDefXLinkColor());
1105 tmpXLink->setWidth(model->getMapDefXLinkWidth());
1106 tmpXLink->setEnd (p);
1107 tmpXLink->updateXLink();
1111 } // End of modmodes
1116 cout << "ME::mouse pressed\n";
1117 cout << " lmo="<<lmo<<endl;
1118 cout << " ti="<<ti->getHeadingStd()<<endl;
1120 // Select the clicked object
1125 // Left Button Move Branches
1126 if (e->button() == Qt::LeftButton )
1128 movingObj_start.setX( p.x() - lmo->x() );
1129 movingObj_start.setY( p.y() - lmo->y() );
1130 movingObj_orgPos.setX (lmo->x() );
1131 movingObj_orgPos.setY (lmo->y() );
1133 movingObj_orgRelPos=lmo->getRelPos();
1135 // If modMode==copy, then we want to "move" the _new_ object around
1136 // then we need the offset from p to the _old_ selection, because of tmp
1137 if (mainWindow->getModMode()==Main::ModModeCopy &&
1138 e->state() & Qt::ControlModifier)
1140 BranchItem *bi=model->getSelectedBranch();
1144 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
1145 //bi->addBranch (model->getSelectedBranch());
1147 model->select(bi->getLastBranch());
1148 model->reposition();
1152 movingObj=model->getSelectedLMO();
1154 // Middle Button Toggle Scroll
1155 // (On Mac OS X this won't work, but we still have
1156 // a button in the toolbar)
1157 if (e->button() == Qt::MidButton )
1158 model->toggleScroll();
1159 // model->updateActions(); FIXME-3 needed?
1160 // FIXME-3 needed? xelection.update();
1162 { // No MapObj found, we are on the scene itself
1163 // Left Button move Pos of sceneView
1164 if (e->button() == Qt::LeftButton )
1166 movingObj=NULL; // move Content not Obj
1167 movingObj_start=e->globalPos();
1168 movingCont_start=QPointF (
1169 horizontalScrollBar()->value(),
1170 verticalScrollBar()->value());
1171 movingVec=QPointF(0,0);
1172 setCursor(HandOpenCursor);
1177 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1179 QPointF p = mapToScene(e->pos());
1180 TreeItem *seli=model->getSelectedItem();
1181 LinkableMapObj* lmosel=NULL;
1182 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1183 lmosel=((MapItem*)seli)->getLMO();
1185 // Move the selected MapObj
1186 if ( lmosel && movingObj)
1189 // reset cursor if we are moving and don't copy
1190 if (mainWindow->getModMode()!=Main::ModModeCopy)
1191 setCursor (Qt::ArrowCursor);
1193 // To avoid jumping of the sceneView, only
1194 // show selection, if not tmp linked
1195 /* FIXME-2 if (!lmosel->hasParObjTmp())
1196 model->emitShowSelection();
1199 // Now move the selection, but add relative position
1200 // (movingObj_start) where selection was chosen with
1201 // mousepointer. (This avoids flickering resp. jumping
1202 // of selection back to absPos)
1204 // Check if we could link
1205 TreeItem *ti=findMapItem (p, seli);
1206 BranchItem *dsti=NULL;
1207 LinkableMapObj* dst=NULL;
1208 if (ti && ti!=seli && ti->isBranchLikeType())
1210 dsti=(BranchItem*)ti;
1216 if (lmosel && seli->getType()==TreeItem::Image)
1218 FloatObj *fio=(FloatImageObj*)lmosel;
1219 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1221 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1222 model->emitSelectionChanged(); // position has changed
1224 // Relink float to new mapcenter or branch, if shift is pressed
1225 // Only relink, if selection really has a new parent
1226 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1228 // Also save the move which was done so far
1229 QString pold=qpointFToString(movingObj_orgRelPos);
1230 QString pnow=qpointFToString(fio->getRelPos());
1236 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1237 fio->getParObj()->requestReposition();
1238 model->reposition();
1240 model->relinkImage ((ImageItem*) seli,dsti);
1241 model->select (seli);
1242 //movingObj=lmosel; //FIXME-3
1243 //movingObj_orgRelPos=lmosel->getRelPos();
1245 model->reposition();
1248 { // selection != a FloatObj
1249 if (seli->depth()==0)
1252 lmosel->move (p-movingObj_start);
1253 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1255 // Move only mapcenter, leave its children where they are
1257 v=lmosel->getAbsPos();
1258 for (int i=0; i<seli->branchCount(); ++i)
1260 seli->getBranchObjNum(i)->setRelPos();
1261 seli->getBranchObjNum(i)->setOrientation();
1266 if (seli->depth()==1)
1269 if (!lmosel->hasParObjTmp())
1270 lmosel->move(p-movingObj_start);
1271 lmosel->setRelPos();
1274 // Move ordinary branch
1275 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1276 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1277 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1278 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1280 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1281 lmosel->setRelPos();
1285 // Maybe we can relink temporary?
1286 if (dsti) // FIXME-1 check if dsti is ancestor of myself!
1288 if (e->modifiers()==Qt::ControlModifier)
1290 // Special case: CTRL to link below dst
1291 lmosel->setParObjTmp (dst,p,+1);
1292 } else if (e->modifiers()==Qt::ShiftModifier)
1293 lmosel->setParObjTmp (dst,p,-1);
1295 lmosel->setParObjTmp (dst,p,0);
1298 lmosel->unsetParObjTmp();
1300 // reposition subbranch
1301 lmosel->reposition();
1303 QItemSelection sel=model->getSelectionModel()->selection();
1304 updateSelection(sel,sel); // position has changed
1306 } // no FloatImageObj
1310 } // selection && moving_obj
1312 // Draw a link from one branch to another
1315 tmpXLink->setEnd (p);
1316 tmpXLink->updateXLink();
1320 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1322 QPointF p=e->globalPos();
1323 movingVec.setX(-p.x() + movingObj_start.x() );
1324 movingVec.setY(-p.y() + movingObj_start.y() );
1325 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1326 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1331 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1333 QPointF p = mapToScene(e->pos());
1334 TreeItem *seli=model->getSelectedItem();
1336 TreeItem *dsti=NULL;
1337 if (seli) dsti=findMapItem(p, seli);
1338 LinkableMapObj* dst=NULL;
1339 if (dsti && dsti->isBranchLikeType ())
1340 dst=((MapItem*)dsti)->getLMO();
1345 // Have we been picking color?
1349 setCursor (Qt::ArrowCursor);
1350 // Check if we are over another branch
1353 if (e->state() & Qt::ShiftModifier)
1354 model->colorBranch (((BranchObj*)dst)->getColor());
1356 model->colorSubtree (((BranchObj*)dst)->getColor());
1361 // Have we been drawing a link?
1365 // Check if we are over another branch
1368 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1369 tmpXLink->updateXLink();
1370 tmpXLink->activate();
1372 tmpXLink,QString("delete ()"),
1373 dsti,QString("addXLink (\"%1\",\"%2\")").arg(model->getSelectString(tmpXLink->getBegin())).arg(model->getSelectString(dsti)),
1374 QString("Add xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(dsti))
1378 model->deleteItem(tmpXLink);
1384 // Have we been moving something?
1385 if ( seli && movingObj )
1387 if (seli->getType()==TreeItem::Image)
1389 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1392 // Moved FloatObj. Maybe we need to reposition
1393 QString pold=qpointFToString(movingObj_orgRelPos);
1394 QString pnow=qpointFToString(fio->getRelPos());
1400 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1402 cout << "ME::release mouse\n";
1403 fio->getParObj()->requestReposition();
1404 model->reposition();
1408 BranchItem *bi=model->getSelectedBranch();
1409 if (bi && bi->depth()==0)
1411 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1413 QString pold=qpointFToString(movingObj_orgPos);
1414 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1421 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1425 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1426 { // A branch was moved
1427 LinkableMapObj* lmosel=NULL;
1428 lmosel=((MapItem*)seli)->getLMO();
1430 // save the position in case we link to mapcenter
1431 QPointF savePos=QPointF (lmosel->getAbsPos() );
1433 // Reset the temporary drawn link to the original one
1434 lmosel->unsetParObjTmp();
1436 // For Redo we may need to save original selection
1437 QString preSelStr=model->getSelectString(seli);
1440 if (dsti && objectMoved)
1442 // We have a destination, relink to that
1444 BranchObj* bsel=model->getSelectedBranchObj();
1446 QString preParStr=model->getSelectString (bsel->getParObj());
1447 QString preNum=QString::number (seli->num(),10);
1448 QString preDstParStr;
1451 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1453 preDstParStr=model->getSelectString (dst->getParObj());
1454 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1456 if (e->state() & Qt::ControlModifier && dst->getParObj())
1459 preDstParStr=model->getSelectString (dst->getParObj());
1460 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1463 preDstParStr=model->getSelectString(dst);
1464 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1465 if (dsti->depth()==0) bsel->move (savePos);
1469 QString postSelStr=model->getSelectString(lmosel);
1470 QString postNum=QString::number (seli->num(),10);
1472 QString undoCom="relinkTo (\""+
1473 preParStr+ "\"," + preNum +"," +
1474 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1476 QString redoCom="relinkTo (\""+
1477 preDstParStr + "\"," + postNum + "," +
1478 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1483 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1488 // No destination, undo temporary move // FIXME-1 animation missing now?
1490 if (seli->depth()==1)
1492 // The select string might be different _after_ moving around.
1493 // Therefor reposition and then use string of old selection, too
1494 model->reposition();
1496 QPointF rp(lmosel->getRelPos());
1497 if (rp != movingObj_orgRelPos)
1499 QString ps=qpointFToString(rp);
1501 model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1502 preSelStr, "moveRel "+ps,
1503 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1507 // Draw the original link, before selection was moved around
1508 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1510 lmosel->setRelPos(); // calc relPos first for starting point
1512 model->startAnimation(
1514 lmosel->getRelPos(),
1518 model->reposition();
1521 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1522 // Finally resize scene, if needed
1528 // maybe we moved View: set old cursor
1529 setCursor (Qt::ArrowCursor);
1533 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1537 cout << "ME p="<<mapToScene (e->pos())<<" scrollBarPos="<<getScrollBarPos();
1538 cout << " min="<<QPointF(horizontalScrollBar()->minimum(),verticalScrollBar()->minimum());
1543 if (model->isSelectionBlocked() )
1549 if (e->button() == Qt::LeftButton )
1551 QPointF p = mapToScene(e->pos());
1552 TreeItem *ti=findMapItem (p, NULL);
1553 if (ti) { // MapObj was found
1554 // First select the MapObj than edit heading
1561 void MapEditor::resizeEvent (QResizeEvent* e)
1563 QGraphicsView::resizeEvent( e );
1566 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1568 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1569 // cerr << event->format(i) << endl;
1571 if (event->mimeData()->hasImage())
1572 event->acceptProposedAction();
1574 if (event->mimeData()->hasUrls())
1575 event->acceptProposedAction();
1578 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1582 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1587 void MapEditor::dropEvent(QDropEvent *event)
1589 BranchItem *selbi=model->getSelectedBranch();
1593 foreach (QString format,event->mimeData()->formats())
1594 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1598 if (event->mimeData()->hasImage())
1600 QVariant imageData = event->mimeData()->imageData();
1601 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1603 if (event->mimeData()->hasUrls())
1604 uris=event->mimeData()->urls();
1612 for (int i=0; i<uris.count();i++)
1614 // Workaround to avoid adding empty branches
1615 if (!uris.at(i).toString().isEmpty())
1617 bi=model->addNewBranch();
1621 s=uris.at(i).toLocalFile();
1624 QString file = QDir::fromNativeSeparators(s);
1625 heading = QFileInfo(file).baseName();
1627 if (file.endsWith(".vym", false))
1628 bi->setVymLink(file);
1630 bi->setURL(uris.at(i).toString());
1633 bo->setURL(uris.at(i).toString());
1637 if (!heading.isEmpty())
1638 bi->setHeading(heading);
1640 bi->setHeading(uris.at(i).toString());
1645 model->reposition();
1648 event->acceptProposedAction();
1651 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1653 // Note: Here we are prepared for multiple selections, though this
1654 // is not yet implemented elsewhere
1656 // Here in MapEditor we can only select Branches and Images
1657 QList <TreeItem*> treeItemsNew;
1658 QList <TreeItem*> treeItemsOld;
1660 QModelIndex newIndex;
1662 bool do_reposition=false;
1665 foreach (ix,newsel.indexes() )
1667 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1668 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1669 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1671 foreach (ix,oldsel.indexes() )
1673 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1674 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1675 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1678 // Trim list of selection rectangles
1679 while (treeItemsNew.count() < selboxList.count() )
1680 delete selboxList.takeFirst();
1682 // Take care to tmp scroll/unscroll
1683 if (!oldsel.isEmpty())
1685 QModelIndex ix=oldsel.indexes().first();
1688 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1691 if (ti->isBranchLikeType() )
1693 // reset tmp scrolled branches
1694 BranchItem *bi=(BranchItem*)ti;
1695 if (bi->resetTmpUnscroll() )
1698 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1699 // Hide link if not needed
1700 ((MapItem*)ti)->getLMO()->updateVisibility();
1705 if (!treeItemsNew.isEmpty())
1707 QModelIndex ix=newsel.indexes().first();
1712 // Temporary unscroll if necessary
1713 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1714 if (ti->isBranchLikeType() )
1716 BranchItem *bi=(BranchItem*)ti;
1717 if (bi->hasScrolledParent(bi) )
1719 if (bi->parentBranch()->tmpUnscroll() )
1723 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1724 // Show link if needed
1725 ((MapItem*)ti)->getLMO()->updateVisibility();
1728 if (do_reposition) model->reposition();
1730 // Reduce rectangles
1731 while (treeItemsNew.count() < selboxList.count() )
1732 delete selboxList.takeFirst();
1734 // Add additonal rectangles
1735 QGraphicsRectItem *sb;
1736 while (treeItemsNew.count() > selboxList.count() )
1738 sb = mapScene->addRect(
1740 QPen(selectionColor),
1742 sb->setZValue(Z_SELBOX);
1744 selboxList.append (sb);
1747 // Reposition rectangles
1751 LinkableMapObj *lmo;
1752 for (int i=0; i<treeItemsNew.count();++i)
1754 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1755 bbox=lmo->getBBox();
1756 sb=selboxList.at(i);
1759 bbox.width(), bbox.height());
1760 sb->setPen (selectionColor);
1761 sb->setBrush (selectionColor);
1768 void MapEditor::updateData (const QModelIndex &sel)
1770 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1773 cout << "ME::updateData\n";
1775 cout << " ti="<<ti<<endl;
1776 cout << " h="<<ti->getHeading().toStdString()<<endl;
1779 if (ti->isBranchLikeType())
1781 // cout << " ->updating...\n";
1782 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1787 void MapEditor::setSelectionColor (QColor col)
1790 QItemSelection sel=model->getSelectionModel()->selection();
1791 updateSelection(sel,sel);
1795 QColor MapEditor::getSelectionColor ()
1797 return selectionColor;