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* floatimageContextMenu;
27 extern QMenu* canvasContextMenu;
29 extern Settings settings;
30 extern QString iconPath;
32 ///////////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////
34 MapEditor::MapEditor( VymModel *vm)
36 //cout << "Constructor ME "<<this<<endl;
37 mapScene= new QGraphicsScene(NULL);
38 mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
40 zoomFactor=zoomFactorTarget=1;
43 model->setScene (mapScene);
44 model->registerEditor(this);
45 model->makeDefault(); // No changes in model so far
51 // Create bitmap cursors, platform dependant
52 HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);
53 PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 );
54 CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 );
55 XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 );
57 //setFocusPolicy (Qt::StrongFocus); //FIXME-3
70 setAcceptDrops (true);
72 //model->reposition(); //FIXME-3 really still needed?
75 // Shortcuts and actions
77 a = new QAction("Select upper branch", this);
78 a->setShortcut (Qt::Key_Up );
79 a->setShortcutContext (Qt::WidgetShortcut);
81 connect( a, SIGNAL( triggered() ), this, SLOT( cursorUp() ) );
83 a = new QAction( "Select lower branch",this);
84 a->setShortcut ( Qt::Key_Down );
85 a->setShortcutContext (Qt::WidgetShortcut);
87 connect( a, SIGNAL( triggered() ), this, SLOT( cursorDown() ) );
89 a = new QAction( "Select left branch", this);
90 a->setShortcut (Qt::Key_Left );
91 // a->setShortcutContext (Qt::WindowShortcut);
92 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
94 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLeft() ) );
96 a = new QAction( "Select child branch", this);
97 a->setShortcut (Qt::Key_Right);
98 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
100 connect( a, SIGNAL( triggered() ), this, SLOT( cursorRight() ) );
102 a = new QAction( "Select first branch", this);
103 a->setShortcut (Qt::Key_Home );
104 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
106 connect( a, SIGNAL( triggered() ), this, SLOT( cursorFirst() ) );
108 a = new QAction( "Select last branch",this);
109 a->setShortcut ( Qt::Key_End );
110 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
112 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLast() ) );
114 // Action to embed LineEdit for heading in Scene
115 editingHeading=false;
116 lineEdit=new QLineEdit;
118 QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
119 pw->setZValue (Z_LINEEDIT);
121 a = new QAction( tr( "Edit heading","MapEditor" ), this);
122 a->setShortcut ( Qt::Key_Return ); //Edit heading
123 a->setShortcutContext (Qt::WidgetShortcut);
125 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
126 a = new QAction( tr( "Edit heading","MapEditor" ), this);
127 a->setShortcut ( Qt::Key_Enter); //Edit heading
129 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
132 selectionColor =QColor (255,255,0);
135 // Attributes //FIXME-2 testing only...
138 attrTable= new AttributeTable();
140 ad=attrTable->addKey (k,StringList);
144 sl <<"val 1"<<"val 2"<< "val 3";
145 ad->setValue (QVariant (sl));
147 //attrTable->addValue ("Key A","P 1");
148 //attrTable->addValue ("Key A","P 2");
149 //attrTable->addValue ("Key A","P 3");
150 //attrTable->addValue ("Key A","P 4");
152 ad=attrTable->addKey (k,FreeString);
155 //attrTable->addValue ("Key B","w1");
156 //attrTable->addValue ("Key B","w2");
158 k="C - UniqueString";
159 ad=attrTable->addKey (k,UniqueString);
162 //attrTable->addKey ("Key Prio");
163 //attrTable->addValue ("Key Prio","Prio 1");
164 //attrTable->addValue ("Key Prio","Prio 2");
168 MapEditor::~MapEditor()
170 //cout <<"Destructor MapEditor for "<<model->getMapName().toStdString()<<endl;
171 model->unregisterEditor(this);
174 VymModel* MapEditor::getModel()
179 QGraphicsScene * MapEditor::getScene()
184 void MapEditor::scrollTo (const QModelIndex &index)
188 LinkableMapObj* lmo=NULL;
189 TreeItem *ti= static_cast<TreeItem*>(index.internalPointer());
190 if (ti->getType()==TreeItem::Image ||ti->isBranchLikeType() )
191 lmo=((MapItem*)ti)->getLMO();
194 QRectF r=lmo->getBBox();
195 setScrollBarPosTarget (r);
200 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
202 // Code copied from Qt sources
206 qreal width = viewport()->width();
207 qreal height = viewport()->height();
208 QRectF viewRect = matrix().mapRect(rect);
210 qreal left = horizontalScrollBar()->value();
211 qreal right = left + width;
212 qreal top = verticalScrollBar()->value();
213 qreal bottom = top + height;
215 scrollBarPosTarget=getScrollBarPos();
217 if (viewRect.left() <= left + xmargin) {
218 // need to scroll from the left
219 // if (!d->leftIndent)
220 scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
222 if (viewRect.right() >= right - xmargin) {
223 // need to scroll from the right
224 // if (!d->leftIndent)
225 scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
227 if (viewRect.top() <= top + ymargin) {
228 // need to scroll from the top
229 // if (!d->topIndent)
230 scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
232 if (viewRect.bottom() >= bottom - ymargin) {
233 // need to scroll from the bottom
234 // if (!d->topIndent)
235 scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
238 if (scrollBarPosTarget==getScrollBarPos()) return;
240 if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
241 scrollBarPosAnimation.stop();
243 if (settings.value ("/animation/use/",true).toBool() )
245 scrollBarPosAnimation.setTargetObject (this);
246 scrollBarPosAnimation.setPropertyName ("scrollBarPos");
247 scrollBarPosAnimation.setDuration(1000);
248 scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
249 scrollBarPosAnimation.setStartValue(
250 QPointF (horizontalScrollBar()->value() ,
251 verticalScrollBar()->value() ) );
252 scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
253 scrollBarPosAnimation.start();
255 setScrollBarPos (scrollBarPosTarget);
258 QPointF MapEditor::getScrollBarPosTarget()
260 return scrollBarPosTarget;
264 void MapEditor::setScrollBarPos(const QPointF &p)
267 horizontalScrollBar()->setValue(int(p.x()));
268 verticalScrollBar()->setValue(int(p.y()));
271 QPointF MapEditor::getScrollBarPos()
273 return QPointF (horizontalScrollBar()->value(),verticalScrollBar()->value());
274 //return scrollBarPos;
277 void MapEditor::setZoomFactorTarget (const qreal &zft)
279 zoomFactorTarget=zft;
280 if (zoomAnimation.state()==QtAbstractAnimation::Running)
281 zoomAnimation.stop();
282 if (settings.value ("/animation/use/",true).toBool() )
284 //zoomAnimation=QPropertyAnimation(this, "zoomFactor");
285 zoomAnimation.setTargetObject (this);
286 zoomAnimation.setPropertyName ("zoomFactor");
287 zoomAnimation.setDuration(1000);
288 zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
289 zoomAnimation.setStartValue(zoomFactor);
290 zoomAnimation.setEndValue(zft);
291 zoomAnimation.start();
296 qreal MapEditor::getZoomFactorTarget()
298 return zoomFactorTarget;
302 void MapEditor::setZoomFactor(const qreal &zf)
305 setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
308 qreal MapEditor::getZoomFactor()
313 void MapEditor::print()
317 printer = new QPrinter;
318 printer->setColorMode (QPrinter::Color);
319 printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
320 printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
321 printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
324 QRectF totalBBox=getTotalBBox();
326 // Try to set orientation automagically
327 // Note: Interpretation of generated postscript is amibiguous, if
328 // there are problems with landscape mode, see
329 // http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
331 if (totalBBox.width()>totalBBox.height())
332 // recommend landscape
333 printer->setOrientation (QPrinter::Landscape);
335 // recommend portrait
336 printer->setOrientation (QPrinter::Portrait);
338 if ( printer->setup(this) )
339 // returns false, if printing is canceled
341 QPainter pp(printer);
343 pp.setRenderHint(QPainter::Antialiasing,true);
345 // Don't print the visualisation of selection
348 QRectF mapRect=totalBBox;
349 QGraphicsRectItem *frame=NULL;
353 // Print frame around map
354 mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10,
355 totalBBox.width()+20, totalBBox.height()+20);
356 frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
362 double paperAspect = (double)printer->width() / (double)printer->height();
363 double mapAspect = (double)mapRect.width() / (double)mapRect.height();
365 if (mapAspect>=paperAspect)
367 // Fit horizontally to paper width
368 //pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );
369 viewBottom=(int)(printer->width()/mapAspect);
372 // Fit vertically to paper height
373 //pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());
374 viewBottom=printer->height();
379 // Print footer below map
381 font.setPointSize(10);
383 QRectF footerBox(0,viewBottom,printer->width(),15);
384 // FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
385 pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
389 QRectF (0,0,printer->width(),printer->height()-15),
390 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
393 // Viewport has paper dimension
394 if (frame) delete (frame);
399 // Save settings in vymrc
400 settings.writeEntry("/mainwindow/printerName",printer->printerName());
401 settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
402 settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
406 QRectF MapEditor::getTotalBBox() //FIXME-2 needed e.g. for image export
410 for (int i=0;i<rootItem->branchCount(); i++)
411 r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
417 QPixmap MapEditor::getPixmap()
419 QRectF mapRect=getTotalBBox();
420 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
423 pp.setRenderHints(renderHints());
425 // Don't print the visualisation of selection
428 mapScene->render ( &pp,
429 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
430 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
439 void MapEditor::setAntiAlias (bool b)
441 setRenderHint(QPainter::Antialiasing,b);
444 void MapEditor::setSmoothPixmap(bool b)
446 setRenderHint(QPainter::SmoothPixmapTransform,b);
449 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
451 // Start with mapcenter, no images allowed at rootItem
453 BranchItem *bi=model->getRootItem()->getFirstBranch();
454 TreeItem *found=NULL;
457 found=bi->findMapItem (p, exclude);
458 if (found) return found;
460 bi=model->getRootItem()->getBranchNum(i);
465 AttributeTable* MapEditor::attributeTable()
470 void MapEditor::testFunction1()
472 cout << "ME::test1 selected TI="<<model->getSelectedItem()<<endl;
474 for (int i=0; i<200;i++)
475 model->addNewBranch();
478 // Code copied from Qt sources
479 QRectF rect=model->getSelectedBranchObj()->getBBox();
483 qreal width = viewport()->width();
484 qreal height = viewport()->height();
485 QRectF viewRect = matrix().mapRect(rect);
487 qreal left = horizontalScrollBar()->value();
488 qreal right = left + width;
489 qreal top = verticalScrollBar()->value();
490 qreal bottom = top + height;
492 if (viewRect.left() <= left + xmargin) {
493 // need to scroll from the left
494 // if (!d->leftIndent)
495 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
497 if (viewRect.right() >= right - xmargin) {
498 // need to scroll from the right
499 // if (!d->leftIndent)
500 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
502 if (viewRect.top() <= top + ymargin) {
503 // need to scroll from the top
504 // if (!d->topIndent)
505 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
507 if (viewRect.bottom() >= bottom - ymargin) {
508 // need to scroll from the bottom
509 // if (!d->topIndent)
510 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
512 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
513 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
518 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
519 animation->setDuration(5000);
520 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
521 animation->setEasingCurve ( QtEasingCurve::OutQuint);
522 animation->setStartValue(sceneRect() );
523 animation->setEndValue(QRectF(50, 50, 1000, 1000));
528 QDialog *dia= new QDialog (this);
529 dia->setGeometry (50,50,10,10);
534 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
535 animation->setDuration(1000);
536 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
537 animation->setEasingCurve ( QtEasingCurve::OutQuint);
538 animation->setStartValue(QRect(50, 50, 10, 10));
539 animation->setEndValue(QRect(250, 250, 100, 100));
544 /* FIXME-4 Hide hidden stuff temporary, maybe add this as regular function somewhere
545 if (hidemode==HideNone)
547 setHideTmpMode (HideExport);
548 mapCenter->calcBBoxSizeWithChilds();
549 QRectF totalBBox=mapCenter->getTotalBBox();
550 QRectF mapRect=totalBBox;
551 QCanvasRectangle *frame=NULL;
553 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
555 mapRect.setRect (totalBBox.x(), totalBBox.y(),
556 totalBBox.width(), totalBBox.height());
557 frame=new QCanvasRectangle (mapRect,mapScene);
558 frame->setBrush (QColor(white));
559 frame->setPen (QColor(black));
565 setHideTmpMode (HideNone);
567 cout <<" hidemode="<<hidemode<<endl;
572 if (hidemode==HideExport)
573 setHideTmpMode (HideNone);
575 setHideTmpMode (HideExport);
580 void MapEditor::testFunction2()
582 // Create list with all bounding polygons
583 QList <LinkableMapObj*> mapobjects;
584 QList <ConvexPolygon> polys;
586 QList <Vector> vectors;
587 QList <Vector> orgpos;
588 QStringList headings; //FIXME-3 testing only
594 // Outer loop: Iterate until we no more changes in orientation
595 bool orientationChanged=true;
596 while (orientationChanged)
598 BranchItem *ri=model->getRootItem();
599 for (int i=0;i<ri->branchCount();++i)
601 bi=ri->getBranchNum (i);
602 bo=(BranchObj*)bi->getLMO();
605 mapobjects.append (bo);
606 p=bo->getBoundingPolygon();
609 vectors.append (QPointF(0,0));
610 orgpos.append (p.at(0));
611 headings.append (bi->getHeading());
613 for (int j=0;j<bi->branchCount();++j)
615 bi2=bi->getBranchNum (j);
616 bo=(BranchObj*)bi2->getLMO();
619 mapobjects.append (bo);
620 p=bo->getBoundingPolygon();
623 vectors.append (QPointF(0,0));
624 orgpos.append (p.at(0));
625 headings.append (bi2->getHeading());
630 // Iterate moving bounding polygons until we have no more collisions
635 for (int i=0; i<polys.size()-1; ++i)
637 for (int j=i+1; j<polys.size();++j)
639 if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
642 //cout << "Collision: "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<endl;
643 v=polys.at(j).centroid()-polys.at(i).centroid();
644 // Move also away if centroids are identical
647 //cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<endl;
648 v.setX (rand()%200 -100);
649 v.setY (rand()%200 -100);
654 //cout << " v="<<v<<endl;
661 for (int i=0;i<vectors.size();i++)
663 //cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
664 polys[i].translate (vectors[i]);
666 cout << "Collisions: "<<collisions<<endl;
670 // Finally move the real objects and update
671 QList <LinkableMapObj::Orientation> orients;
672 for (int i=0;i<polys.size();i++)
674 Vector v=polys[i].at(0)-orgpos[i];
675 orients.append (mapobjects[i]->getOrientation());
676 mapobjects[i]->moveBy(v.x(),v.y() );
677 mapobjects[i]->setRelPos();
680 orientationChanged=false;
681 for (int i=0;i<polys.size();i++)
682 if (orients[i]!=mapobjects[i]->getOrientation())
684 orientationChanged=true;
687 cout << "Final: orientChanged="<<orientationChanged<<endl;
689 //orientationChanged=false;
690 } // loop if orientation has changed
692 model->emitSelectionChanged();
695 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
700 if (i>0) return bi->parent()->getBranchNum(i-1);
705 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
709 int dz=selbi->depth(); // original depth
711 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
716 // Look for branch with same parent but directly above
718 bi=getBranchDirectBelow(selbi);
720 bi=getBranchDirectAbove (selbi);
723 // direct predecessor
726 // Go towards center and look for predecessor
727 while (selbi->depth()>0)
729 selbi=(BranchItem*)(selbi->parent());
730 if (selbi->depth()==1 && invert)
731 bi=getBranchDirectBelow (selbi);
733 bi=getBranchDirectAbove (selbi);
738 while (selbi->depth()<dz)
740 // try to get back to original depth dz
741 bi=selbi->getLastBranch();
755 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
760 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
765 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
770 int dz=selbi->depth(); // original depth
772 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
776 // Look for branch with same parent but directly below
778 bi=getBranchDirectAbove (selbi);
780 bi=getBranchDirectBelow (selbi);
786 // Go towards center and look for neighbour
787 while (selbi->depth()>0)
789 selbi=(BranchItem*)(selbi->parent());
790 if (selbi->depth()==1 && invert)
791 bi=getBranchDirectAbove (selbi);
793 bi=getBranchDirectBelow (selbi);
798 while (selbi->depth()<dz)
800 // try to get back to original depth dz
801 bi=selbi->getFirstBranch();
815 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
820 // Special case: use alternative selection index
821 return bi->getLastSelectedBranchAlt();
822 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
824 return (BranchItem*)(bi->parent());
827 if (bi->getType()== TreeItem::Branch )
828 return bi->getLastSelectedBranch();
833 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
837 if (bi->depth()==0) return bi->getLastSelectedBranch();
838 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
840 return (BranchItem*)(bi->parent());
843 if (bi->getType()== TreeItem::Branch )
844 return (BranchItem*)bi->getLastSelectedBranch();
851 void MapEditor::cursorUp()
853 BranchItem *bi=model->getSelectedBranch();
854 if (bi) model->select (getBranchAbove(bi));
857 void MapEditor::cursorDown()
860 BranchItem *bi=model->getSelectedBranch();
861 if (bi) model->select (getBranchBelow(bi));
864 void MapEditor::cursorLeft()
866 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
867 if (bi) model->select (bi);
870 void MapEditor::cursorRight()
872 BranchItem *bi=getRightBranch (model->getSelectedBranch());
873 if (bi) model->select (bi);
876 void MapEditor::cursorFirst()
878 model->selectFirstBranch();
881 void MapEditor::cursorLast()
883 model->selectLastBranch();
887 void MapEditor::editHeading()
891 editHeadingFinished();
894 BranchObj *bo=model->getSelectedBranchObj();
895 BranchItem *bi=model->getSelectedBranch();
898 model->setSelectionBlocked(true);
900 lineEdit->setText (bi->getHeading());
901 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
902 lineEdit->setGeometry(p.x(),p.y(),230,25);
903 //lineEdit->selectAll();
904 //lineEdit->setCursorPosition (1);
906 lineEdit->setFocus();
907 lineEdit->grabKeyboard();
912 void MapEditor::editHeadingFinished()
914 editingHeading=false;
915 lineEdit->releaseKeyboard();
916 model->setHeading (lineEdit->text() );
917 model->setSelectionBlocked(false);
920 // Maybe reselect previous branch
921 mainWindow->editHeadingFinished (model);
925 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
927 // Lineedits are already closed by preceding
928 // mouseEvent, we don't need to close here.
930 QPointF p = mapToScene(e->pos());
931 TreeItem *ti=findMapItem (p, NULL);
932 LinkableMapObj* lmo=NULL;
933 if (ti) lmo=((MapItem*)ti)->getLMO();
936 { // MapObj was found
937 if (model->getSelectedLMO() != lmo)
943 if (model->getSelectedBranchObj() )
945 // Context Menu on branch or mapcenter
946 branchContextMenu->popup(e->globalPos() );
949 if (model->getSelectedImage() )
951 // Context Menu on floatimage
952 floatimageContextMenu->popup(e->globalPos() );
956 { // No MapObj found, we are on the Canvas itself
957 // Context Menu on scene
959 // Open context menu synchronously to position new mapcenter
960 model->setContextPos (p);
961 canvasContextMenu->exec(e->globalPos() );
962 model->unsetContextPos ();
967 void MapEditor::keyPressEvent(QKeyEvent* e)
969 if (e->modifiers() & Qt::ControlModifier)
971 switch (mainWindow->getModMode())
973 case Main::ModModeColor:
974 setCursor (PickColorCursor);
976 case Main::ModModeCopy:
977 setCursor (CopyCursor);
979 case Main::ModModeXLink:
980 setCursor (XLinkCursor);
983 setCursor (Qt::ArrowCursor);
989 void MapEditor::keyReleaseEvent(QKeyEvent* e)
991 if (!(e->modifiers() & Qt::ControlModifier))
992 setCursor (Qt::ArrowCursor);
995 void MapEditor::mousePressEvent(QMouseEvent* e)
997 //cout << "ME::mousePressed\n"; //FIXME-3
998 // Ignore right clicks, these will go to context menus
999 if (e->button() == Qt::RightButton )
1001 //cout << " ME::ignoring right mouse event...\n";
1006 //Ignore clicks while editing heading
1007 if (model->isSelectionBlocked() )
1009 //cout << " ME::ignoring other mouse event...\n";
1014 QPointF p = mapToScene(e->pos());
1015 TreeItem *ti=findMapItem (p, NULL);
1016 LinkableMapObj* lmo=NULL;
1017 if (ti) lmo=((MapItem*)ti)->getLMO();
1022 //Take care of system flags _or_ modifier modes
1024 if (lmo && ti->isBranchLikeType() )
1026 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
1027 if (!foname.isEmpty())
1029 // systemFlag clicked
1030 model->select (lmo);
1031 if (foname=="system-url")
1033 if (e->state() & Qt::ControlModifier)
1034 mainWindow->editOpenURLTab();
1036 mainWindow->editOpenURL();
1038 else if (foname=="system-vymLink")
1040 mainWindow->editOpenVymLink();
1041 // tabWidget may change, better return now
1042 // before segfaulting...
1043 } else if (foname=="system-note")
1044 mainWindow->windowToggleNoteEditor();
1045 else if (foname=="hideInExport")
1046 model->toggleHideExport();
1047 // FIXME-3 needed? xelection.update();
1051 // Take care of xLink
1052 if (ti->xlinkCount()>0 && lmo->getBBox().width()>30)
1054 if ((lmo->getOrientation()==LinkableMapObj::RightOfCenter && p.x() > lmo->getBBox().right()-20) ||
1055 (lmo->getOrientation()==LinkableMapObj::LeftOfCenter && p.x() < lmo->getBBox().left()+20) )
1057 if (ti->xlinkCount()>1)
1060 QList <QAction*> alist;
1061 for (int i=0;i<ti->xlinkCount();i++)
1062 alist.append (new QAction(ti->getXLinkNum(i)->getOtherBranch(ti)->getHeading(),&menu));
1063 menu.addActions (alist);
1064 QAction *ra=menu.exec (e->globalPos() );
1066 model->select (ti->getXLinkNum(alist.indexOf(ra))->getOtherBranch (ti));
1067 while (!alist.isEmpty())
1069 QAction *a=alist.takeFirst();
1075 model->select (ti->getXLinkNum(0)->getOtherBranch (ti));
1083 // No system flag clicked, take care of modmodes (CTRL-Click)
1084 if (e->state() & Qt::ControlModifier)
1086 if (mainWindow->getModMode()==Main::ModModeColor)
1089 setCursor (PickColorCursor);
1092 if (mainWindow->getModMode()==Main::ModModeXLink)
1094 BranchItem *bi_begin=model->getSelectedBranch();
1098 tmpXLink=model->createXLink(bi_begin,true);
1099 tmpXLink->setColor(model->getMapDefXLinkColor());
1100 tmpXLink->setWidth(model->getMapDefXLinkWidth());
1101 tmpXLink->setEnd (p);
1102 tmpXLink->updateXLink();
1106 } // End of modmodes
1111 cout << "ME::mouse pressed\n";
1112 cout << " lmo="<<lmo<<endl;
1113 cout << " ti="<<ti->getHeadingStd()<<endl;
1115 // Select the clicked object
1120 // Left Button Move Branches
1121 if (e->button() == Qt::LeftButton )
1123 movingObj_start.setX( p.x() - lmo->x() );
1124 movingObj_start.setY( p.y() - lmo->y() );
1125 movingObj_orgPos.setX (lmo->x() );
1126 movingObj_orgPos.setY (lmo->y() );
1128 movingObj_orgRelPos=lmo->getRelPos();
1130 // If modMode==copy, then we want to "move" the _new_ object around
1131 // then we need the offset from p to the _old_ selection, because of tmp
1132 if (mainWindow->getModMode()==Main::ModModeCopy &&
1133 e->state() & Qt::ControlModifier)
1135 BranchItem *bi=model->getSelectedBranch();
1139 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
1140 //bi->addBranch (model->getSelectedBranch());
1142 model->select(bi->getLastBranch());
1143 model->reposition();
1147 movingObj=model->getSelectedLMO();
1149 // Middle Button Toggle Scroll
1150 // (On Mac OS X this won't work, but we still have
1151 // a button in the toolbar)
1152 if (e->button() == Qt::MidButton )
1153 model->toggleScroll();
1154 // model->updateActions(); FIXME-3 needed?
1155 // FIXME-3 needed? xelection.update();
1157 { // No MapObj found, we are on the scene itself
1158 // Left Button move Pos of sceneView
1159 if (e->button() == Qt::LeftButton )
1161 movingObj=NULL; // move Content not Obj
1162 movingObj_start=e->globalPos();
1163 movingCont_start=QPointF (
1164 horizontalScrollBar()->value(),
1165 verticalScrollBar()->value());
1166 movingVec=QPointF(0,0);
1167 setCursor(HandOpenCursor);
1172 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1174 QPointF p = mapToScene(e->pos());
1175 TreeItem *seli=model->getSelectedItem();
1176 LinkableMapObj* lmosel=NULL;
1177 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1178 lmosel=((MapItem*)seli)->getLMO();
1180 // Move the selected MapObj
1181 if ( lmosel && movingObj)
1184 // reset cursor if we are moving and don't copy
1185 if (mainWindow->getModMode()!=Main::ModModeCopy)
1186 setCursor (Qt::ArrowCursor);
1188 // To avoid jumping of the sceneView, only
1189 // show selection, if not tmp linked
1190 /* FIXME-2 if (!lmosel->hasParObjTmp())
1191 model->emitShowSelection();
1194 // Now move the selection, but add relative position
1195 // (movingObj_start) where selection was chosen with
1196 // mousepointer. (This avoids flickering resp. jumping
1197 // of selection back to absPos)
1199 // Check if we could link
1200 TreeItem *ti=findMapItem (p, seli);
1201 BranchItem *dsti=NULL;
1202 LinkableMapObj* dst=NULL;
1203 if (ti && ti!=seli && ti->isBranchLikeType())
1205 dsti=(BranchItem*)ti;
1211 if (lmosel && seli->getType()==TreeItem::Image)
1213 FloatObj *fio=(FloatImageObj*)lmosel;
1214 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1216 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1217 model->emitSelectionChanged(); // position has changed
1219 // Relink float to new mapcenter or branch, if shift is pressed
1220 // Only relink, if selection really has a new parent
1221 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1223 // Also save the move which was done so far
1224 QString pold=qpointFToString(movingObj_orgRelPos);
1225 QString pnow=qpointFToString(fio->getRelPos());
1231 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1232 fio->getParObj()->requestReposition();
1233 model->reposition();
1235 model->relinkImage ((ImageItem*) seli,dsti);
1236 model->select (seli);
1237 //movingObj=lmosel; //FIXME-3
1238 //movingObj_orgRelPos=lmosel->getRelPos();
1240 model->reposition();
1243 { // selection != a FloatObj
1244 if (seli->depth()==0)
1247 lmosel->move (p-movingObj_start);
1248 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1250 // Move only mapcenter, leave its children where they are
1252 v=lmosel->getAbsPos();
1253 for (int i=0; i<seli->branchCount(); ++i)
1255 seli->getBranchObjNum(i)->setRelPos();
1256 seli->getBranchObjNum(i)->setOrientation();
1261 if (seli->depth()==1)
1264 if (!lmosel->hasParObjTmp())
1265 lmosel->move(p-movingObj_start);
1266 lmosel->setRelPos();
1269 // Move ordinary branch
1270 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1271 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1272 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1273 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1275 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1276 lmosel->setRelPos();
1280 // Maybe we can relink temporary?
1281 if (dsti) // FIXME-1 check if dsti is ancestor of myself!
1283 if (e->modifiers()==Qt::ControlModifier)
1285 // Special case: CTRL to link below dst
1286 lmosel->setParObjTmp (dst,p,+1);
1287 } else if (e->modifiers()==Qt::ShiftModifier)
1288 lmosel->setParObjTmp (dst,p,-1);
1290 lmosel->setParObjTmp (dst,p,0);
1293 lmosel->unsetParObjTmp();
1295 // reposition subbranch
1296 lmosel->reposition();
1298 QItemSelection sel=model->getSelectionModel()->selection();
1299 updateSelection(sel,sel); // position has changed
1301 } // no FloatImageObj
1305 } // selection && moving_obj
1307 // Draw a link from one branch to another
1310 tmpXLink->setEnd (p);
1311 tmpXLink->updateXLink();
1315 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1317 QPointF p=e->globalPos();
1318 movingVec.setX(-p.x() + movingObj_start.x() );
1319 movingVec.setY(-p.y() + movingObj_start.y() );
1320 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1321 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1326 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1328 QPointF p = mapToScene(e->pos());
1329 TreeItem *seli=model->getSelectedItem();
1331 TreeItem *dsti=NULL;
1332 if (seli) dsti=findMapItem(p, seli);
1333 LinkableMapObj* dst=NULL;
1334 if (dsti && dsti->isBranchLikeType ())
1335 dst=((MapItem*)dsti)->getLMO();
1340 // Have we been picking color?
1344 setCursor (Qt::ArrowCursor);
1345 // Check if we are over another branch
1348 if (e->state() & Qt::ShiftModifier)
1349 model->colorBranch (((BranchObj*)dst)->getColor());
1351 model->colorSubtree (((BranchObj*)dst)->getColor());
1356 // Have we been drawing a link?
1360 // Check if we are over another branch
1363 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1364 tmpXLink->updateXLink();
1365 tmpXLink->activate();
1367 tmpXLink,QString("delete ()"),
1368 dsti,QString("addXLink (\"%1\",\"%2\")").arg(model->getSelectString(tmpXLink->getBegin())).arg(model->getSelectString(dsti)),
1369 QString("Add xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(dsti))
1373 model->deleteItem(tmpXLink);
1379 // Have we been moving something?
1380 if ( seli && movingObj )
1382 if (seli->getType()==TreeItem::Image)
1384 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1387 // Moved FloatObj. Maybe we need to reposition
1388 QString pold=qpointFToString(movingObj_orgRelPos);
1389 QString pnow=qpointFToString(fio->getRelPos());
1395 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1397 cout << "ME::release mouse\n";
1398 fio->getParObj()->requestReposition();
1399 model->reposition();
1403 BranchItem *bi=model->getSelectedBranch();
1404 if (bi && bi->depth()==0)
1406 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1408 QString pold=qpointFToString(movingObj_orgPos);
1409 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1416 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1420 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1421 { // A branch was moved
1422 LinkableMapObj* lmosel=NULL;
1423 lmosel=((MapItem*)seli)->getLMO();
1425 // save the position in case we link to mapcenter
1426 QPointF savePos=QPointF (lmosel->getAbsPos() );
1428 // Reset the temporary drawn link to the original one
1429 lmosel->unsetParObjTmp();
1431 // For Redo we may need to save original selection
1432 QString preSelStr=model->getSelectString(seli);
1435 if (dsti && objectMoved)
1437 // We have a destination, relink to that
1439 BranchObj* bsel=model->getSelectedBranchObj();
1441 QString preParStr=model->getSelectString (bsel->getParObj());
1442 QString preNum=QString::number (seli->num(),10);
1443 QString preDstParStr;
1446 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1448 preDstParStr=model->getSelectString (dst->getParObj());
1449 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1451 if (e->state() & Qt::ControlModifier && dst->getParObj())
1454 preDstParStr=model->getSelectString (dst->getParObj());
1455 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1458 preDstParStr=model->getSelectString(dst);
1459 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1460 if (dsti->depth()==0) bsel->move (savePos);
1464 QString postSelStr=model->getSelectString(lmosel);
1465 QString postNum=QString::number (seli->num(),10);
1467 QString undoCom="relinkTo (\""+
1468 preParStr+ "\"," + preNum +"," +
1469 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1471 QString redoCom="relinkTo (\""+
1472 preDstParStr + "\"," + postNum + "," +
1473 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1478 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1483 // No destination, undo temporary move
1485 if (seli->depth()==1)
1487 // The select string might be different _after_ moving around.
1488 // Therefor reposition and then use string of old selection, too
1489 model->reposition();
1491 QPointF rp(lmosel->getRelPos());
1492 if (rp != movingObj_orgRelPos)
1494 QString ps=qpointFToString(rp);
1496 model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1497 preSelStr, "moveRel "+ps,
1498 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1502 // Draw the original link, before selection was moved around
1503 if (settings.value("/animation/use",true).toBool() && seli->depth()>1)
1505 lmosel->setRelPos(); // calc relPos first for starting point
1507 model->startAnimation(
1509 lmosel->getRelPos(),
1513 model->reposition();
1516 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1517 // Finally resize scene, if needed
1523 // maybe we moved View: set old cursor
1524 setCursor (Qt::ArrowCursor);
1528 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1532 cout << "ME p="<<mapToScene (e->pos())<<" scrollBarPos="<<getScrollBarPos();
1533 cout << " min="<<QPointF(horizontalScrollBar()->minimum(),verticalScrollBar()->minimum());
1538 if (model->isSelectionBlocked() )
1544 if (e->button() == Qt::LeftButton )
1546 QPointF p = mapToScene(e->pos());
1547 TreeItem *ti=findMapItem (p, NULL);
1548 if (ti) { // MapObj was found
1549 // First select the MapObj than edit heading
1556 void MapEditor::resizeEvent (QResizeEvent* e)
1558 QGraphicsView::resizeEvent( e );
1561 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1563 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1564 // cerr << event->format(i) << endl;
1566 if (event->mimeData()->hasImage())
1567 event->acceptProposedAction();
1569 if (event->mimeData()->hasUrls())
1570 event->acceptProposedAction();
1573 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1577 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1582 void MapEditor::dropEvent(QDropEvent *event)
1584 BranchItem *selbi=model->getSelectedBranch();
1588 foreach (QString format,event->mimeData()->formats())
1589 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1593 if (event->mimeData()->hasImage())
1595 QVariant imageData = event->mimeData()->imageData();
1596 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1598 if (event->mimeData()->hasUrls())
1599 uris=event->mimeData()->urls();
1607 for (int i=0; i<uris.count();i++)
1609 // Workaround to avoid adding empty branches
1610 if (!uris.at(i).toString().isEmpty())
1612 bi=model->addNewBranch();
1616 s=uris.at(i).toLocalFile();
1619 QString file = QDir::fromNativeSeparators(s);
1620 heading = QFileInfo(file).baseName();
1622 if (file.endsWith(".vym", false))
1623 bi->setVymLink(file);
1625 bi->setURL(uris.at(i).toString());
1628 bo->setURL(uris.at(i).toString());
1632 if (!heading.isEmpty())
1633 bi->setHeading(heading);
1635 bi->setHeading(uris.at(i).toString());
1640 model->reposition();
1643 event->acceptProposedAction();
1646 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1648 // Note: Here we are prepared for multiple selections, though this
1649 // is not yet implemented elsewhere
1651 // Here in MapEditor we can only select Branches and Images
1652 QList <TreeItem*> treeItemsNew;
1653 QList <TreeItem*> treeItemsOld;
1655 QModelIndex newIndex;
1657 bool do_reposition=false;
1660 foreach (ix,newsel.indexes() )
1662 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1663 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1664 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1666 foreach (ix,oldsel.indexes() )
1668 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1669 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1670 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1673 // Trim list of selection rectangles
1674 while (treeItemsNew.count() < selboxList.count() )
1675 delete selboxList.takeFirst();
1677 // Take care to tmp scroll/unscroll
1678 if (!oldsel.isEmpty())
1680 QModelIndex ix=oldsel.indexes().first();
1683 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1686 if (ti->isBranchLikeType() )
1688 // reset tmp scrolled branches
1689 BranchItem *bi=(BranchItem*)ti;
1690 if (bi->resetTmpUnscroll() )
1693 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1694 // Hide link if not needed
1695 ((MapItem*)ti)->getLMO()->updateVisibility();
1700 if (!treeItemsNew.isEmpty())
1702 QModelIndex ix=newsel.indexes().first();
1707 // Temporary unscroll if necessary
1708 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1709 if (ti->isBranchLikeType() )
1711 BranchItem *bi=(BranchItem*)ti;
1712 if (bi->hasScrolledParent(bi) )
1714 if (bi->parentBranch()->tmpUnscroll() )
1718 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1719 // Show link if needed
1720 ((MapItem*)ti)->getLMO()->updateVisibility();
1723 if (do_reposition) model->reposition();
1725 // Reduce rectangles
1726 while (treeItemsNew.count() < selboxList.count() )
1727 delete selboxList.takeFirst();
1729 // Add additonal rectangles
1730 QGraphicsRectItem *sb;
1731 while (treeItemsNew.count() > selboxList.count() )
1733 sb = mapScene->addRect(
1735 QPen(selectionColor),
1737 sb->setZValue(Z_SELBOX);
1739 selboxList.append (sb);
1742 // Reposition rectangles
1746 LinkableMapObj *lmo;
1747 for (int i=0; i<treeItemsNew.count();++i)
1749 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1750 bbox=lmo->getBBox();
1751 sb=selboxList.at(i);
1754 bbox.width(), bbox.height());
1755 sb->setPen (selectionColor);
1756 sb->setBrush (selectionColor);
1763 void MapEditor::updateData (const QModelIndex &sel)
1765 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1768 cout << "ME::updateData\n";
1770 cout << " ti="<<ti<<endl;
1771 cout << " h="<<ti->getHeading().toStdString()<<endl;
1774 if (ti->isBranchLikeType())
1776 // cout << " ->updating...\n";
1777 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1782 void MapEditor::setSelectionColor (QColor col)
1785 QItemSelection sel=model->getSelectionModel()->selection();
1786 updateSelection(sel,sel);
1790 QColor MapEditor::getSelectionColor ()
1792 return selectionColor;