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 here...
1189 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1190 ((BranchObj*)lmosel)->moveBy(
1191 QPointF(p.x() -movingObj_start.x(),
1192 p.y()-movingObj_start.y()) );
1194 lmosel->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1195 model->updateRelPositions();
1198 if (seli->depth()==1)
1201 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1202 lmosel->setRelPos();
1205 // Move ordinary branch
1206 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1207 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1208 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1209 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1211 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1212 lmosel->setRelPos();
1216 // Maybe we can relink temporary?
1219 if (e->modifiers()==Qt::ControlModifier)
1221 // Special case: CTRL to link below dst
1222 lmosel->setParObjTmp (dst,p,+1);
1224 else if (e->modifiers()==Qt::ShiftModifier)
1225 lmosel->setParObjTmp (dst,p,-1);
1227 lmosel->setParObjTmp (dst,p,0);
1230 lmosel->unsetParObjTmp();
1232 // reposition subbranch
1233 lmosel->reposition();
1235 QItemSelection sel=model->getSelectionModel()->selection();
1236 updateSelection(sel,sel); // position has changed
1238 } // no FloatImageObj
1242 } // selection && moving_obj
1244 // Draw a link from one branch to another
1247 tmpXLink->setEnd (p);
1248 tmpXLink->updateXLink();
1252 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1254 QPointF p=e->globalPos();
1255 movingVec.setX(-p.x() + movingObj_start.x() );
1256 movingVec.setY(-p.y() + movingObj_start.y() );
1257 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1258 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1263 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1265 QPointF p = mapToScene(e->pos());
1266 TreeItem *seli=model->getSelectedItem();
1268 TreeItem *dsti=NULL;
1269 if (seli) dsti=findMapItem(p, seli);
1270 LinkableMapObj* dst=NULL;
1271 if (dsti && dsti->isBranchLikeType ())
1272 dst=((MapItem*)dsti)->getLMO();
1277 // Have we been picking color?
1281 setCursor (Qt::ArrowCursor);
1282 // Check if we are over another branch
1285 if (e->state() & Qt::ShiftModifier)
1286 model->colorBranch (((BranchObj*)dst)->getColor());
1288 model->colorSubtree (((BranchObj*)dst)->getColor());
1293 // Have we been drawing a link?
1297 // Check if we are over another branch
1300 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1301 tmpXLink->updateXLink();
1302 tmpXLink->activate();
1304 tmpXLink,QString("delete ()"),
1305 dsti,QString("addXLink (\"%1\",\"%2\")").arg(model->getSelectString(tmpXLink->getBegin())).arg(model->getSelectString(dsti)),
1306 QString("Add xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(dsti))
1310 model->deleteItem(tmpXLink);
1316 // Have we been moving something?
1317 if ( seli && movingObj )
1319 if (seli->getType()==TreeItem::Image)
1321 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1324 // Moved FloatObj. Maybe we need to reposition
1325 QString pold=qpointFToString(movingObj_orgRelPos);
1326 QString pnow=qpointFToString(fio->getRelPos());
1332 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1334 cout << "ME::release mouse\n";
1335 fio->getParObj()->requestReposition();
1336 model->reposition();
1340 BranchItem *bi=model->getSelectedBranch();
1341 if (bi && bi->depth()==0)
1343 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1345 QString pold=qpointFToString(movingObj_orgPos);
1346 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1353 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1357 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1358 { // A branch was moved
1359 LinkableMapObj* lmosel=NULL;
1360 lmosel=((MapItem*)seli)->getLMO();
1362 // save the position in case we link to mapcenter
1363 QPointF savePos=QPointF (lmosel->getAbsPos() );
1365 // Reset the temporary drawn link to the original one
1366 lmosel->unsetParObjTmp();
1368 // For Redo we may need to save original selection
1369 QString preSelStr=model->getSelectString(seli);
1374 // We have a destination, relink to that
1376 BranchObj* bsel=model->getSelectedBranchObj();
1378 QString preParStr=model->getSelectString (bsel->getParObj());
1379 QString preNum=QString::number (seli->num(),10);
1380 QString preDstParStr;
1383 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1385 preDstParStr=model->getSelectString (dst->getParObj());
1386 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1388 if (e->state() & Qt::ControlModifier && dst->getParObj())
1391 preDstParStr=model->getSelectString (dst->getParObj());
1392 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1395 preDstParStr=model->getSelectString(dst);
1396 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1397 if (dsti->depth()==0) bsel->move (savePos);
1401 QString postSelStr=model->getSelectString(lmosel);
1402 QString postNum=QString::number (seli->num(),10);
1404 QString undoCom="relinkTo (\""+
1405 preParStr+ "\"," + preNum +"," +
1406 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1408 QString redoCom="relinkTo (\""+
1409 preDstParStr + "\"," + postNum + "," +
1410 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1415 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1420 // No destination, undo temporary move
1422 if (seli->depth()==1)
1424 // The select string might be different _after_ moving around.
1425 // Therefor reposition and then use string of old selection, too
1426 model->reposition();
1428 QPointF rp(lmosel->getRelPos());
1429 if (rp != movingObj_orgRelPos)
1431 QString ps=qpointFToString(rp);
1433 model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1434 preSelStr, "moveRel "+ps,
1435 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1439 // Draw the original link, before selection was moved around
1440 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1442 lmosel->setRelPos(); // calc relPos first for starting point
1443 QPointF dst=bi->getBranchObj()->getParObj()->getChildPos(); // FIXME-3 check getBO here...
1444 // if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
1446 model->startAnimation(
1448 lmosel->getRelPos(),
1450 // QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
1453 model->reposition();
1456 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1457 // Finally resize scene, if needed
1461 // Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
1462 // model->updateActions(); FIXME-3 neeeded?
1464 // maybe we moved View: set old cursor
1465 setCursor (Qt::ArrowCursor);
1469 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1471 if (model->isSelectionBlocked() )
1477 if (e->button() == Qt::LeftButton )
1479 QPointF p = mapToScene(e->pos());
1480 TreeItem *ti=findMapItem (p, NULL);
1481 if (ti) { // MapObj was found
1482 // First select the MapObj than edit heading
1489 void MapEditor::resizeEvent (QResizeEvent* e)
1491 QGraphicsView::resizeEvent( e );
1494 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1496 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1497 // cerr << event->format(i) << endl;
1499 if (event->mimeData()->hasImage())
1500 event->acceptProposedAction();
1502 if (event->mimeData()->hasUrls())
1503 event->acceptProposedAction();
1506 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1510 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1515 void MapEditor::dropEvent(QDropEvent *event)
1517 BranchItem *selbi=model->getSelectedBranch();
1521 foreach (QString format,event->mimeData()->formats())
1522 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1526 if (event->mimeData()->hasImage())
1528 QVariant imageData = event->mimeData()->imageData();
1529 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1531 if (event->mimeData()->hasUrls())
1532 uris=event->mimeData()->urls();
1540 for (int i=0; i<uris.count();i++)
1542 // Workaround to avoid adding empty branches
1543 if (!uris.at(i).toString().isEmpty())
1545 bi=model->addNewBranch();
1549 s=uris.at(i).toLocalFile();
1552 QString file = QDir::fromNativeSeparators(s);
1553 heading = QFileInfo(file).baseName();
1555 if (file.endsWith(".vym", false))
1556 bi->setVymLink(file);
1558 bi->setURL(uris.at(i).toString());
1561 bo->setURL(uris.at(i).toString());
1565 if (!heading.isEmpty())
1566 bi->setHeading(heading);
1568 bi->setHeading(uris.at(i).toString());
1573 model->reposition();
1576 event->acceptProposedAction();
1579 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1581 // Note: Here we are prepared for multiple selections, though this
1582 // is not yet implemented elsewhere
1584 // Here in MapEditor we can only select Branches and Images
1585 QList <TreeItem*> treeItemsNew;
1586 QList <TreeItem*> treeItemsOld;
1589 foreach (ix,newsel.indexes() )
1591 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1592 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1593 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1595 foreach (ix,oldsel.indexes() )
1597 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1598 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1599 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1602 // Trim list of selection rectangles
1603 while (treeItemsNew.count() < selboxList.count() )
1604 delete selboxList.takeFirst();
1606 // Take care to tmp scroll/unscroll
1607 if (!oldsel.isEmpty())
1609 QModelIndex ix=oldsel.indexes().first();
1612 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1615 if (ti->isBranchLikeType() )
1617 // reset tmp scrolled branches
1618 BranchItem *bi=(BranchItem*)ti;
1619 bi->resetTmpUnscroll();
1621 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1622 // Hide link if not needed
1623 ((MapItem*)ti)->getLMO()->updateVisibility();
1628 if (!treeItemsNew.isEmpty())
1630 QModelIndex ix=newsel.indexes().first();
1633 // Temporary unscroll if necessary
1634 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1635 if (ti->isBranchLikeType() )
1637 BranchItem *bi=(BranchItem*)ti;
1638 if (bi->hasScrolledParent(bi) )
1642 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1643 // Show link if needed
1644 ((MapItem*)ti)->getLMO()->updateVisibility();
1648 // Reduce rectangles
1649 while (treeItemsNew.count() < selboxList.count() )
1650 delete selboxList.takeFirst();
1652 // Add additonal rectangles
1653 QGraphicsRectItem *sb;
1654 while (treeItemsNew.count() > selboxList.count() )
1656 sb = mapScene->addRect(
1658 QPen(selectionColor),
1660 sb->setZValue(Z_SELBOX);
1662 selboxList.append (sb);
1667 // Reposition rectangles
1671 LinkableMapObj *lmo;
1672 for (int i=0; i<treeItemsNew.count();++i)
1674 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1675 bbox=lmo->getBBox();
1676 sb=selboxList.at(i);
1679 bbox.width(), bbox.height());
1680 sb->setPen (selectionColor);
1681 sb->setBrush (selectionColor);
1688 void MapEditor::updateData (const QModelIndex &sel)
1690 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1693 cout << "ME::updateData\n";
1695 cout << " ti="<<ti<<endl;
1696 cout << " h="<<ti->getHeading().toStdString()<<endl;
1699 if (ti->isBranchLikeType())
1701 // cout << " ->updating...\n";
1702 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1707 void MapEditor::setSelectionColor (QColor col)
1710 QItemSelection sel=model->getSelectionModel()->selection();
1711 updateSelection(sel,sel);
1715 QColor MapEditor::getSelectionColor ()
1717 return selectionColor;