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)
189 //cout <<"ME::scrollTo aborted\n";
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();
197 if (lmo) setScrollBarPosTarget (lmo->getBBox() );
201 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
203 // Code copied from Qt sources
207 qreal width = viewport()->width();
208 qreal height = viewport()->height();
209 QRectF viewRect = matrix().mapRect(rect);
211 qreal left = horizontalScrollBar()->value();
212 qreal right = left + width;
213 qreal top = verticalScrollBar()->value();
214 qreal bottom = top + height;
216 if (viewRect.left() <= left + xmargin) {
217 // need to scroll from the left
218 // if (!d->leftIndent)
219 scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
221 if (viewRect.right() >= right - xmargin) {
222 // need to scroll from the right
223 // if (!d->leftIndent)
224 scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
226 if (viewRect.top() <= top + ymargin) {
227 // need to scroll from the top
228 // if (!d->topIndent)
229 scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
231 if (viewRect.bottom() >= bottom - ymargin) {
232 // need to scroll from the bottom
233 // if (!d->topIndent)
234 scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
237 if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
238 scrollBarPosAnimation.stop();
239 scrollBarPosAnimation.setTargetObject (this);
240 scrollBarPosAnimation.setPropertyName ("scrollBarPos");
241 scrollBarPosAnimation.setDuration(1000);
242 scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
243 scrollBarPosAnimation.setStartValue(
244 QPointF (horizontalScrollBar()->value() ,
245 verticalScrollBar()->value() ) );
246 scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
247 scrollBarPosAnimation.start();
250 QPointF MapEditor::getScrollBarPosTarget()
252 return scrollBarPosTarget;
256 void MapEditor::setScrollBarPos(const QPointF &p)
259 horizontalScrollBar()->setValue(int(p.x()));
260 verticalScrollBar()->setValue(int(p.y()));
263 QPointF MapEditor::getScrollBarPos()
268 void MapEditor::setZoomFactorTarget (const qreal &zft)
270 zoomFactorTarget=zft;
271 if (zoomAnimation.state()==QtAbstractAnimation::Running)
272 zoomAnimation.stop();
273 //zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
274 zoomAnimation.setTargetObject (this);
275 zoomAnimation.setPropertyName ("zoomFactor");
276 zoomAnimation.setDuration(1000);
277 zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
278 zoomAnimation.setStartValue(zoomFactor);
279 zoomAnimation.setEndValue(zft);
280 zoomAnimation.start();
283 qreal MapEditor::getZoomFactorTarget()
285 return zoomFactorTarget;
289 void MapEditor::setZoomFactor(const qreal &zf)
292 setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
295 qreal MapEditor::getZoomFactor()
300 void MapEditor::print()
304 printer = new QPrinter;
305 printer->setColorMode (QPrinter::Color);
306 printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
307 printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
308 printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
311 QRectF totalBBox=getTotalBBox();
313 // Try to set orientation automagically
314 // Note: Interpretation of generated postscript is amibiguous, if
315 // there are problems with landscape mode, see
316 // http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
318 if (totalBBox.width()>totalBBox.height())
319 // recommend landscape
320 printer->setOrientation (QPrinter::Landscape);
322 // recommend portrait
323 printer->setOrientation (QPrinter::Portrait);
325 if ( printer->setup(this) )
326 // returns false, if printing is canceled
328 QPainter pp(printer);
330 pp.setRenderHint(QPainter::Antialiasing,true);
332 // Don't print the visualisation of selection
335 QRectF mapRect=totalBBox;
336 QGraphicsRectItem *frame=NULL;
340 // Print frame around map
341 mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10,
342 totalBBox.width()+20, totalBBox.height()+20);
343 frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
349 double paperAspect = (double)printer->width() / (double)printer->height();
350 double mapAspect = (double)mapRect.width() / (double)mapRect.height();
352 if (mapAspect>=paperAspect)
354 // Fit horizontally to paper width
355 //pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );
356 viewBottom=(int)(printer->width()/mapAspect);
359 // Fit vertically to paper height
360 //pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());
361 viewBottom=printer->height();
366 // Print footer below map
368 font.setPointSize(10);
370 QRectF footerBox(0,viewBottom,printer->width(),15);
371 // FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
372 pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
376 QRectF (0,0,printer->width(),printer->height()-15),
377 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
380 // Viewport has paper dimension
381 if (frame) delete (frame);
386 // Save settings in vymrc
387 settings.writeEntry("/mainwindow/printerName",printer->printerName());
388 settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
389 settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
393 QRectF MapEditor::getTotalBBox() //FIXME-2 needed e.g. for image export
397 for (int i=0;i<rootItem->branchCount(); i++)
398 r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
404 QPixmap MapEditor::getPixmap()
406 QRectF mapRect=getTotalBBox();
407 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
410 pp.setRenderHints(renderHints());
412 // Don't print the visualisation of selection
415 mapScene->render ( &pp,
416 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
417 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
426 void MapEditor::setAntiAlias (bool b)
428 setRenderHint(QPainter::Antialiasing,b);
431 void MapEditor::setSmoothPixmap(bool b)
433 setRenderHint(QPainter::SmoothPixmapTransform,b);
436 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
438 // Start with mapcenter, no images allowed at rootItem
440 BranchItem *bi=model->getRootItem()->getFirstBranch();
441 TreeItem *found=NULL;
444 found=bi->findMapItem (p, exclude);
445 if (found) return found;
447 bi=model->getRootItem()->getBranchNum(i);
452 AttributeTable* MapEditor::attributeTable()
457 void MapEditor::testFunction1()
459 cout << "ME::test1 selected TI="<<model->getSelectedItem()<<endl;
461 // Code copied from Qt sources
462 QRectF rect=model->getSelectedBranchObj()->getBBox();
466 qreal width = viewport()->width();
467 qreal height = viewport()->height();
468 QRectF viewRect = matrix().mapRect(rect);
470 qreal left = horizontalScrollBar()->value();
471 qreal right = left + width;
472 qreal top = verticalScrollBar()->value();
473 qreal bottom = top + height;
475 if (viewRect.left() <= left + xmargin) {
476 // need to scroll from the left
477 // if (!d->leftIndent)
478 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
480 if (viewRect.right() >= right - xmargin) {
481 // need to scroll from the right
482 // if (!d->leftIndent)
483 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
485 if (viewRect.top() <= top + ymargin) {
486 // need to scroll from the top
487 // if (!d->topIndent)
488 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
490 if (viewRect.bottom() >= bottom - ymargin) {
491 // need to scroll from the bottom
492 // if (!d->topIndent)
493 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
495 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
496 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
501 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
502 animation->setDuration(5000);
503 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
504 animation->setEasingCurve ( QtEasingCurve::OutQuint);
505 animation->setStartValue(sceneRect() );
506 animation->setEndValue(QRectF(50, 50, 1000, 1000));
511 QDialog *dia= new QDialog (this);
512 dia->setGeometry (50,50,10,10);
517 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
518 animation->setDuration(1000);
519 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
520 animation->setEasingCurve ( QtEasingCurve::OutQuint);
521 animation->setStartValue(QRect(50, 50, 10, 10));
522 animation->setEndValue(QRect(250, 250, 100, 100));
527 /* FIXME-4 Hide hidden stuff temporary, maybe add this as regular function somewhere
528 if (hidemode==HideNone)
530 setHideTmpMode (HideExport);
531 mapCenter->calcBBoxSizeWithChilds();
532 QRectF totalBBox=mapCenter->getTotalBBox();
533 QRectF mapRect=totalBBox;
534 QCanvasRectangle *frame=NULL;
536 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
538 mapRect.setRect (totalBBox.x(), totalBBox.y(),
539 totalBBox.width(), totalBBox.height());
540 frame=new QCanvasRectangle (mapRect,mapScene);
541 frame->setBrush (QColor(white));
542 frame->setPen (QColor(black));
548 setHideTmpMode (HideNone);
550 cout <<" hidemode="<<hidemode<<endl;
555 if (hidemode==HideExport)
556 setHideTmpMode (HideNone);
558 setHideTmpMode (HideExport);
563 void MapEditor::testFunction2()
565 // Create list with all bounding polygons
566 QList <LinkableMapObj*> mapobjects;
567 QList <ConvexPolygon> polys;
569 QList <Vector> vectors;
570 QList <Vector> orgpos;
571 QStringList headings; //FIXME-3 testing only
577 // Outer loop: Iterate until we no more changes in orientation
578 bool orientationChanged=true;
579 while (orientationChanged)
581 BranchItem *ri=model->getRootItem();
582 for (int i=0;i<ri->branchCount();++i)
584 bi=ri->getBranchNum (i);
585 bo=(BranchObj*)bi->getLMO();
588 mapobjects.append (bo);
589 p=bo->getBoundingPolygon();
592 vectors.append (QPointF(0,0));
593 orgpos.append (p.at(0));
594 headings.append (bi->getHeading());
596 for (int j=0;j<bi->branchCount();++j)
598 bi2=bi->getBranchNum (j);
599 bo=(BranchObj*)bi2->getLMO();
602 mapobjects.append (bo);
603 p=bo->getBoundingPolygon();
606 vectors.append (QPointF(0,0));
607 orgpos.append (p.at(0));
608 headings.append (bi2->getHeading());
613 // Iterate moving bounding polygons until we have no more collisions
618 for (int i=0; i<polys.size()-1; ++i)
620 for (int j=i+1; j<polys.size();++j)
622 if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
625 //cout << "Collision: "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<endl;
626 v=polys.at(j).centroid()-polys.at(i).centroid();
627 // Move also away if centroids are identical
630 //cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<endl;
631 v.setX (rand()%200 -100);
632 v.setY (rand()%200 -100);
637 //cout << " v="<<v<<endl;
644 for (int i=0;i<vectors.size();i++)
646 //cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
647 polys[i].translate (vectors[i]);
649 cout << "Collisions: "<<collisions<<endl;
653 // Finally move the real objects and update
654 QList <LinkableMapObj::Orientation> orients;
655 for (int i=0;i<polys.size();i++)
657 Vector v=polys[i].at(0)-orgpos[i];
658 orients.append (mapobjects[i]->getOrientation());
659 mapobjects[i]->moveBy(v.x(),v.y() );
662 orientationChanged=false;
663 for (int i=0;i<polys.size();i++)
664 if (orients[i]!=mapobjects[i]->getOrientation())
666 orientationChanged=true;
669 cout << "Final: orientChanged="<<orientationChanged<<endl;
671 //orientationChanged=false;
672 } // loop if orientation has changed
675 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
680 if (i>0) return bi->parent()->getBranchNum(i-1);
685 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
689 int dz=selbi->depth(); // original depth
691 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
696 // Look for branch with same parent but directly above
698 bi=getBranchDirectBelow(selbi);
700 bi=getBranchDirectAbove (selbi);
703 // direct predecessor
706 // Go towards center and look for predecessor
707 while (selbi->depth()>0)
709 selbi=(BranchItem*)(selbi->parent());
710 if (selbi->depth()==1 && invert)
711 bi=getBranchDirectBelow (selbi);
713 bi=getBranchDirectAbove (selbi);
718 while (selbi->depth()<dz)
720 // try to get back to original depth dz
721 bi=selbi->getLastBranch();
735 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
740 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
745 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
750 int dz=selbi->depth(); // original depth
752 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
756 // Look for branch with same parent but directly below
758 bi=getBranchDirectAbove (selbi);
760 bi=getBranchDirectBelow (selbi);
766 // Go towards center and look for neighbour
767 while (selbi->depth()>0)
769 selbi=(BranchItem*)(selbi->parent());
770 if (selbi->depth()==1 && invert)
771 bi=getBranchDirectAbove (selbi);
773 bi=getBranchDirectBelow (selbi);
778 while (selbi->depth()<dz)
780 // try to get back to original depth dz
781 bi=selbi->getFirstBranch();
795 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
800 // Special case: use alternative selection index
801 return bi->getLastSelectedBranchAlt();
802 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
804 return (BranchItem*)(bi->parent());
807 if (bi->getType()== TreeItem::Branch )
808 return bi->getLastSelectedBranch();
813 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
817 if (bi->depth()==0) return bi->getLastSelectedBranch();
818 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
820 return (BranchItem*)(bi->parent());
823 if (bi->getType()== TreeItem::Branch )
824 return (BranchItem*)bi->getLastSelectedBranch();
831 void MapEditor::cursorUp()
833 BranchItem *bi=model->getSelectedBranch();
834 if (bi) model->select (getBranchAbove(bi));
837 void MapEditor::cursorDown()
840 BranchItem *bi=model->getSelectedBranch();
841 if (bi) model->select (getBranchBelow(bi));
844 void MapEditor::cursorLeft()
846 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
847 if (bi) model->select (bi);
850 void MapEditor::cursorRight()
852 BranchItem *bi=getRightBranch (model->getSelectedBranch());
853 if (bi) model->select (bi);
856 void MapEditor::cursorFirst()
858 model->selectFirstBranch();
861 void MapEditor::cursorLast()
863 model->selectLastBranch();
867 void MapEditor::editHeading()
871 editHeadingFinished();
874 BranchObj *bo=model->getSelectedBranchObj();
875 BranchItem *bi=model->getSelectedBranch();
878 model->setSelectionBlocked(true);
880 lineEdit->setText (bi->getHeading());
881 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
882 lineEdit->setGeometry(p.x(),p.y(),230,25);
883 lineEdit->selectAll();
885 lineEdit->setFocus();
886 lineEdit->grabKeyboard();
891 void MapEditor::editHeadingFinished()
893 editingHeading=false;
894 lineEdit->releaseKeyboard();
895 model->setHeading (lineEdit->text() );
896 model->setSelectionBlocked(false);
899 // Maybe reselect previous branch
900 mainWindow->editHeadingFinished (model);
904 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
906 // Lineedits are already closed by preceding
907 // mouseEvent, we don't need to close here.
909 QPointF p = mapToScene(e->pos());
910 TreeItem *ti=findMapItem (p, NULL);
911 LinkableMapObj* lmo=NULL;
912 if (ti) lmo=((MapItem*)ti)->getLMO();
915 { // MapObj was found
916 if (model->getSelectedLMO() != lmo)
922 if (model->getSelectedBranchObj() )
924 // Context Menu on branch or mapcenter
925 branchContextMenu->popup(e->globalPos() );
928 if (model->getSelectedImage() )
930 // Context Menu on floatimage
931 floatimageContextMenu->popup(e->globalPos() );
935 { // No MapObj found, we are on the Canvas itself
936 // Context Menu on scene
938 // Open context menu synchronously to position new mapcenter
939 model->setContextPos (p);
940 canvasContextMenu->exec(e->globalPos() );
941 model->unsetContextPos ();
946 void MapEditor::keyPressEvent(QKeyEvent* e)
948 if (e->modifiers() & Qt::ControlModifier)
950 switch (mainWindow->getModMode())
952 case Main::ModModeColor:
953 setCursor (PickColorCursor);
955 case Main::ModModeCopy:
956 setCursor (CopyCursor);
958 case Main::ModModeXLink:
959 setCursor (XLinkCursor);
962 setCursor (Qt::ArrowCursor);
968 void MapEditor::keyReleaseEvent(QKeyEvent* e)
970 if (!(e->modifiers() & Qt::ControlModifier))
971 setCursor (Qt::ArrowCursor);
974 void MapEditor::mousePressEvent(QMouseEvent* e)
976 // Ignore right clicks, these will go to context menus
977 if (e->button() == Qt::RightButton )
983 //Ignore clicks while editing heading
984 if (model->isSelectionBlocked() )
990 QPointF p = mapToScene(e->pos());
991 TreeItem *ti=findMapItem (p, NULL);
992 LinkableMapObj* lmo=NULL;
993 if (ti) lmo=((MapItem*)ti)->getLMO();
998 //Take care of system flags _or_ modifier modes
1000 if (lmo && ti->isBranchLikeType() )
1002 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
1003 if (!foname.isEmpty())
1005 // systemFlag clicked
1006 model->select (lmo); // FIXME-3 was selectInt
1009 if (e->state() & Qt::ControlModifier)
1010 mainWindow->editOpenURLTab();
1012 mainWindow->editOpenURL();
1014 else if (foname=="system-vymLink")
1016 mainWindow->editOpenVymLink();
1017 // tabWidget may change, better return now
1018 // before segfaulting...
1019 } else if (foname=="note")
1020 mainWindow->windowToggleNoteEditor();
1021 else if (foname=="hideInExport")
1022 model->toggleHideExport();
1023 // FIXME-3 needed? xelection.update();
1027 // No system flag clicked, take care of modmodes (CTRL-Click)
1028 if (e->state() & Qt::ControlModifier)
1030 if (mainWindow->getModMode()==Main::ModModeColor)
1033 setCursor (PickColorCursor);
1036 if (mainWindow->getModMode()==Main::ModModeXLink)
1038 BranchItem *bi_begin=model->getSelectedBranch();
1042 tmpXLink=model->createXLink(bi_begin,true);
1043 tmpXLink->setColor(model->getMapDefXLinkColor());
1044 tmpXLink->setWidth(model->getMapDefXLinkWidth());
1045 tmpXLink->setEnd (p);
1046 tmpXLink->updateXLink();
1050 } // End of modmodes
1055 cout << "ME::mouse pressed\n";
1056 cout << " lmo="<<lmo<<endl;
1057 cout << " ti="<<ti->getHeadingStd()<<endl;
1059 // Select the clicked object
1064 // Left Button Move Branches
1065 if (e->button() == Qt::LeftButton )
1067 movingObj_start.setX( p.x() - lmo->x() );
1068 movingObj_start.setY( p.y() - lmo->y() );
1069 movingObj_orgPos.setX (lmo->x() );
1070 movingObj_orgPos.setY (lmo->y() );
1072 movingObj_orgRelPos=lmo->getRelPos();
1074 // If modMode==copy, then we want to "move" the _new_ object around
1075 // then we need the offset from p to the _old_ selection, because of tmp
1076 if (mainWindow->getModMode()==Main::ModModeCopy &&
1077 e->state() & Qt::ControlModifier)
1079 BranchItem *bi=model->getSelectedBranch();
1083 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
1084 //bi->addBranch (model->getSelectedBranch());
1086 model->select(bi->getLastBranch());
1087 model->reposition();
1091 movingObj=model->getSelectedLMO();
1093 // Middle Button Toggle Scroll
1094 // (On Mac OS X this won't work, but we still have
1095 // a button in the toolbar)
1096 if (e->button() == Qt::MidButton )
1097 model->toggleScroll();
1098 // model->updateActions(); FIXME-3 needed?
1099 // FIXME-3 needed? xelection.update();
1101 { // No MapObj found, we are on the scene itself
1102 // Left Button move Pos of sceneView
1103 if (e->button() == Qt::LeftButton )
1105 movingObj=NULL; // move Content not Obj
1106 movingObj_start=e->globalPos();
1107 movingCont_start=QPointF (
1108 horizontalScrollBar()->value(),
1109 verticalScrollBar()->value());
1110 movingVec=QPointF(0,0);
1111 setCursor(HandOpenCursor);
1116 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1118 QPointF p = mapToScene(e->pos());
1119 TreeItem *seli=model->getSelectedItem();
1120 LinkableMapObj* lmosel=NULL;
1121 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1122 lmosel=((MapItem*)seli)->getLMO();
1124 // Move the selected MapObj
1125 if ( lmosel && movingObj)
1127 // reset cursor if we are moving and don't copy
1128 if (mainWindow->getModMode()!=Main::ModModeCopy)
1129 setCursor (Qt::ArrowCursor);
1131 // To avoid jumping of the sceneView, only
1132 // show selection, if not tmp linked
1133 if (!lmosel->hasParObjTmp())
1134 model->emitShowSelection();
1136 // Now move the selection, but add relative position
1137 // (movingObj_start) where selection was chosen with
1138 // mousepointer. (This avoids flickering resp. jumping
1139 // of selection back to absPos)
1141 // Check if we could link
1142 TreeItem *ti=findMapItem (p, seli);
1143 BranchItem *dsti=NULL;
1144 LinkableMapObj* dst=NULL;
1145 if (ti && ti!=seli && ti->isBranchLikeType())
1147 dsti=(BranchItem*)ti;
1153 if (lmosel && seli->getType()==TreeItem::Image)
1155 FloatObj *fio=(FloatImageObj*)lmosel;
1156 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1158 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1159 model->emitSelectionChanged(); // position has changed
1161 // Relink float to new mapcenter or branch, if shift is pressed
1162 // Only relink, if selection really has a new parent
1163 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1165 // Also save the move which was done so far
1166 QString pold=qpointFToString(movingObj_orgRelPos);
1167 QString pnow=qpointFToString(fio->getRelPos());
1173 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1174 fio->getParObj()->requestReposition();
1175 model->reposition();
1177 model->relinkImage ((ImageItem*) seli,dsti);
1178 model->select (seli);
1179 //movingObj=lmosel; //FIXME-3
1180 //movingObj_orgRelPos=lmosel->getRelPos();
1182 model->reposition();
1185 { // selection != a FloatObj
1186 if (seli->depth()==0) //FIXME-1 also moved mapcenters could be linked, but not working so far
1188 // Move MapCenter // FIXME-1 scrolls way too much with shift pressed
1189 lmosel->move (p-movingObj_start);
1190 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1193 v=lmosel->getAbsPos();
1194 for (int i=0; i<seli->branchCount(); ++i)
1196 seli->getBranchObjNum(i)->setRelPos();
1197 seli->getBranchObjNum(i)->setOrientation();
1202 if (seli->depth()==1)
1205 lmosel->move(p-movingObj_start);
1206 lmosel->setRelPos();
1209 // Move ordinary branch
1210 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1211 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1212 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1213 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1215 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1216 lmosel->setRelPos();
1220 // Maybe we can relink temporary?
1221 if (dsti) // FIXME-1 check if dsti is ancestor of myself!
1223 if (e->modifiers()==Qt::ControlModifier)
1225 // Special case: CTRL to link below dst
1226 lmosel->setParObjTmp (dst,p,+1);
1228 else if (e->modifiers()==Qt::ShiftModifier)
1229 lmosel->setParObjTmp (dst,p,-1);
1231 lmosel->setParObjTmp (dst,p,0);
1234 lmosel->unsetParObjTmp();
1236 // reposition subbranch
1237 lmosel->reposition();
1239 QItemSelection sel=model->getSelectionModel()->selection();
1240 updateSelection(sel,sel); // position has changed
1242 } // no FloatImageObj
1246 } // selection && moving_obj
1248 // Draw a link from one branch to another
1251 tmpXLink->setEnd (p);
1252 tmpXLink->updateXLink();
1256 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1258 QPointF p=e->globalPos();
1259 movingVec.setX(-p.x() + movingObj_start.x() );
1260 movingVec.setY(-p.y() + movingObj_start.y() );
1261 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1262 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1267 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1269 QPointF p = mapToScene(e->pos());
1270 TreeItem *seli=model->getSelectedItem();
1272 TreeItem *dsti=NULL;
1273 if (seli) dsti=findMapItem(p, seli);
1274 LinkableMapObj* dst=NULL;
1275 if (dsti && dsti->isBranchLikeType ())
1276 dst=((MapItem*)dsti)->getLMO();
1281 // Have we been picking color?
1285 setCursor (Qt::ArrowCursor);
1286 // Check if we are over another branch
1289 if (e->state() & Qt::ShiftModifier)
1290 model->colorBranch (((BranchObj*)dst)->getColor());
1292 model->colorSubtree (((BranchObj*)dst)->getColor());
1297 // Have we been drawing a link?
1301 // Check if we are over another branch
1304 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1305 tmpXLink->updateXLink();
1306 tmpXLink->activate();
1308 tmpXLink,QString("delete ()"),
1309 dsti,QString("addXLink (\"%1\",\"%2\")").arg(model->getSelectString(tmpXLink->getBegin())).arg(model->getSelectString(dsti)),
1310 QString("Add xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(dsti))
1314 model->deleteItem(tmpXLink);
1320 // Have we been moving something?
1321 if ( seli && movingObj )
1323 if (seli->getType()==TreeItem::Image)
1325 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1328 // Moved FloatObj. Maybe we need to reposition
1329 QString pold=qpointFToString(movingObj_orgRelPos);
1330 QString pnow=qpointFToString(fio->getRelPos());
1336 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1338 cout << "ME::release mouse\n";
1339 fio->getParObj()->requestReposition();
1340 model->reposition();
1344 BranchItem *bi=model->getSelectedBranch();
1345 if (bi && bi->depth()==0)
1347 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1349 QString pold=qpointFToString(movingObj_orgPos);
1350 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1357 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1361 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1362 { // A branch was moved
1363 LinkableMapObj* lmosel=NULL;
1364 lmosel=((MapItem*)seli)->getLMO();
1366 // save the position in case we link to mapcenter
1367 QPointF savePos=QPointF (lmosel->getAbsPos() );
1369 // Reset the temporary drawn link to the original one
1370 lmosel->unsetParObjTmp();
1372 // For Redo we may need to save original selection
1373 QString preSelStr=model->getSelectString(seli);
1378 // We have a destination, relink to that
1380 BranchObj* bsel=model->getSelectedBranchObj();
1382 QString preParStr=model->getSelectString (bsel->getParObj());
1383 QString preNum=QString::number (seli->num(),10);
1384 QString preDstParStr;
1387 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1389 preDstParStr=model->getSelectString (dst->getParObj());
1390 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1392 if (e->state() & Qt::ControlModifier && dst->getParObj())
1395 preDstParStr=model->getSelectString (dst->getParObj());
1396 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1399 preDstParStr=model->getSelectString(dst);
1400 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1401 if (dsti->depth()==0) bsel->move (savePos);
1405 QString postSelStr=model->getSelectString(lmosel);
1406 QString postNum=QString::number (seli->num(),10);
1408 QString undoCom="relinkTo (\""+
1409 preParStr+ "\"," + preNum +"," +
1410 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1412 QString redoCom="relinkTo (\""+
1413 preDstParStr + "\"," + postNum + "," +
1414 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1419 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1424 // No destination, undo temporary move
1426 if (seli->depth()==1)
1428 // The select string might be different _after_ moving around.
1429 // Therefor reposition and then use string of old selection, too
1430 model->reposition();
1432 QPointF rp(lmosel->getRelPos());
1433 if (rp != movingObj_orgRelPos)
1435 QString ps=qpointFToString(rp);
1437 model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1438 preSelStr, "moveRel "+ps,
1439 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1443 // Draw the original link, before selection was moved around
1444 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1446 lmosel->setRelPos(); // calc relPos first for starting point
1447 QPointF dst=bi->getBranchObj()->getParObj()->getChildPos(); // FIXME-3 check getBO here...
1448 // if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
1450 model->startAnimation(
1452 lmosel->getRelPos(),
1454 // QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
1457 model->reposition();
1460 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1461 // Finally resize scene, if needed
1465 // Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
1466 // model->updateActions(); FIXME-3 neeeded?
1468 // maybe we moved View: set old cursor
1469 setCursor (Qt::ArrowCursor);
1473 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1475 if (model->isSelectionBlocked() )
1481 if (e->button() == Qt::LeftButton )
1483 QPointF p = mapToScene(e->pos());
1484 TreeItem *ti=findMapItem (p, NULL);
1485 if (ti) { // MapObj was found
1486 // First select the MapObj than edit heading
1493 void MapEditor::resizeEvent (QResizeEvent* e)
1495 QGraphicsView::resizeEvent( e );
1498 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1500 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1501 // cerr << event->format(i) << endl;
1503 if (event->mimeData()->hasImage())
1504 event->acceptProposedAction();
1506 if (event->mimeData()->hasUrls())
1507 event->acceptProposedAction();
1510 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1514 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1519 void MapEditor::dropEvent(QDropEvent *event)
1521 BranchItem *selbi=model->getSelectedBranch();
1525 foreach (QString format,event->mimeData()->formats())
1526 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1530 if (event->mimeData()->hasImage())
1532 QVariant imageData = event->mimeData()->imageData();
1533 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1535 if (event->mimeData()->hasUrls())
1536 uris=event->mimeData()->urls();
1544 for (int i=0; i<uris.count();i++)
1546 // Workaround to avoid adding empty branches
1547 if (!uris.at(i).toString().isEmpty())
1549 bi=model->addNewBranch();
1553 s=uris.at(i).toLocalFile();
1556 QString file = QDir::fromNativeSeparators(s);
1557 heading = QFileInfo(file).baseName();
1559 if (file.endsWith(".vym", false))
1560 bi->setVymLink(file);
1562 bi->setURL(uris.at(i).toString());
1565 bo->setURL(uris.at(i).toString());
1569 if (!heading.isEmpty())
1570 bi->setHeading(heading);
1572 bi->setHeading(uris.at(i).toString());
1577 model->reposition();
1580 event->acceptProposedAction();
1583 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1585 // Note: Here we are prepared for multiple selections, though this
1586 // is not yet implemented elsewhere
1588 // Here in MapEditor we can only select Branches and Images
1589 QList <TreeItem*> treeItemsNew;
1590 QList <TreeItem*> treeItemsOld;
1592 bool do_reposition=false;
1595 foreach (ix,newsel.indexes() )
1597 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1598 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1599 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1601 foreach (ix,oldsel.indexes() )
1603 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1604 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1605 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1608 // Trim list of selection rectangles
1609 while (treeItemsNew.count() < selboxList.count() )
1610 delete selboxList.takeFirst();
1612 // Take care to tmp scroll/unscroll
1613 if (!oldsel.isEmpty())
1615 QModelIndex ix=oldsel.indexes().first();
1618 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1621 if (ti->isBranchLikeType() )
1623 // reset tmp scrolled branches
1624 BranchItem *bi=(BranchItem*)ti;
1625 if (bi->resetTmpUnscroll() )
1628 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1629 // Hide link if not needed
1630 ((MapItem*)ti)->getLMO()->updateVisibility();
1635 if (!treeItemsNew.isEmpty())
1637 QModelIndex ix=newsel.indexes().first();
1640 // Temporary unscroll if necessary
1641 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1642 if (ti->isBranchLikeType() )
1644 BranchItem *bi=(BranchItem*)ti;
1645 if (bi->hasScrolledParent(bi) )
1647 if (bi->parentBranch()->tmpUnscroll() )
1652 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1653 // Show link if needed
1654 ((MapItem*)ti)->getLMO()->updateVisibility();
1657 if (do_reposition) model->reposition();
1659 // Reduce rectangles
1660 while (treeItemsNew.count() < selboxList.count() )
1661 delete selboxList.takeFirst();
1663 // Add additonal rectangles
1664 QGraphicsRectItem *sb;
1665 while (treeItemsNew.count() > selboxList.count() )
1667 sb = mapScene->addRect(
1669 QPen(selectionColor),
1671 sb->setZValue(Z_SELBOX);
1673 selboxList.append (sb);
1677 // Reposition rectangles
1681 LinkableMapObj *lmo;
1682 for (int i=0; i<treeItemsNew.count();++i)
1684 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1685 bbox=lmo->getBBox();
1686 sb=selboxList.at(i);
1689 bbox.width(), bbox.height());
1690 sb->setPen (selectionColor);
1691 sb->setBrush (selectionColor);
1698 void MapEditor::updateData (const QModelIndex &sel)
1700 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1703 cout << "ME::updateData\n";
1705 cout << " ti="<<ti<<endl;
1706 cout << " h="<<ti->getHeading().toStdString()<<endl;
1709 if (ti->isBranchLikeType())
1711 // cout << " ->updating...\n";
1712 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1717 void MapEditor::setSelectionColor (QColor col)
1720 QItemSelection sel=model->getSelectionModel()->selection();
1721 updateSelection(sel,sel);
1725 QColor MapEditor::getSelectionColor ()
1727 return selectionColor;