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()
410 for (int i=0;i<model->getRootItem()->branchCount(); i++)
412 bo=(BranchObj*)(model->getRootItem()->getBranchNum(i)->getLMO());
413 if (bo) r=addBBox (bo->getTotalBBox(), r);
419 QPixmap MapEditor::getPixmap()
421 QRectF mapRect=getTotalBBox();
422 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+2);
425 pp.setRenderHints(renderHints());
427 // Don't print the visualisation of selection
430 mapScene->render ( &pp,
431 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
432 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
441 void MapEditor::setAntiAlias (bool b)
443 setRenderHint(QPainter::Antialiasing,b);
446 void MapEditor::setSmoothPixmap(bool b)
448 setRenderHint(QPainter::SmoothPixmapTransform,b);
451 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
453 // Start with mapcenter, no images allowed at rootItem
455 BranchItem *bi=model->getRootItem()->getFirstBranch();
456 TreeItem *found=NULL;
459 found=bi->findMapItem (p, exclude);
460 if (found) return found;
462 bi=model->getRootItem()->getBranchNum(i);
467 AttributeTable* MapEditor::attributeTable()
472 void MapEditor::testFunction1()
474 cout << "ME::test1 selected TI="<<model->getSelectedItem()<<endl;
476 for (int i=0; i<200;i++)
477 model->addNewBranch();
480 // Code copied from Qt sources
481 QRectF rect=model->getSelectedBranchObj()->getBBox();
485 qreal width = viewport()->width();
486 qreal height = viewport()->height();
487 QRectF viewRect = matrix().mapRect(rect);
489 qreal left = horizontalScrollBar()->value();
490 qreal right = left + width;
491 qreal top = verticalScrollBar()->value();
492 qreal bottom = top + height;
494 if (viewRect.left() <= left + xmargin) {
495 // need to scroll from the left
496 // if (!d->leftIndent)
497 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
499 if (viewRect.right() >= right - xmargin) {
500 // need to scroll from the right
501 // if (!d->leftIndent)
502 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
504 if (viewRect.top() <= top + ymargin) {
505 // need to scroll from the top
506 // if (!d->topIndent)
507 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
509 if (viewRect.bottom() >= bottom - ymargin) {
510 // need to scroll from the bottom
511 // if (!d->topIndent)
512 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
514 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
515 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
520 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
521 animation->setDuration(5000);
522 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
523 animation->setEasingCurve ( QtEasingCurve::OutQuint);
524 animation->setStartValue(sceneRect() );
525 animation->setEndValue(QRectF(50, 50, 1000, 1000));
530 QDialog *dia= new QDialog (this);
531 dia->setGeometry (50,50,10,10);
536 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
537 animation->setDuration(1000);
538 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
539 animation->setEasingCurve ( QtEasingCurve::OutQuint);
540 animation->setStartValue(QRect(50, 50, 10, 10));
541 animation->setEndValue(QRect(250, 250, 100, 100));
546 /* FIXME-4 Hide hidden stuff temporary, maybe add this as regular function somewhere
547 if (hidemode==HideNone)
549 setHideTmpMode (HideExport);
550 mapCenter->calcBBoxSizeWithChilds();
551 QRectF totalBBox=mapCenter->getTotalBBox();
552 QRectF mapRect=totalBBox;
553 QCanvasRectangle *frame=NULL;
555 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
557 mapRect.setRect (totalBBox.x(), totalBBox.y(),
558 totalBBox.width(), totalBBox.height());
559 frame=new QCanvasRectangle (mapRect,mapScene);
560 frame->setBrush (QColor(white));
561 frame->setPen (QColor(black));
567 setHideTmpMode (HideNone);
569 cout <<" hidemode="<<hidemode<<endl;
574 if (hidemode==HideExport)
575 setHideTmpMode (HideNone);
577 setHideTmpMode (HideExport);
582 void MapEditor::testFunction2()
584 // Create list with all bounding polygons
585 QList <LinkableMapObj*> mapobjects;
586 QList <ConvexPolygon> polys;
588 QList <Vector> vectors;
589 QList <Vector> orgpos;
590 QStringList headings; //FIXME-3 testing only
596 // Outer loop: Iterate until we no more changes in orientation
597 bool orientationChanged=true;
598 while (orientationChanged)
600 BranchItem *ri=model->getRootItem();
601 for (int i=0;i<ri->branchCount();++i)
603 bi=ri->getBranchNum (i);
604 bo=(BranchObj*)bi->getLMO();
607 mapobjects.append (bo);
608 p=bo->getBoundingPolygon();
611 vectors.append (QPointF(0,0));
612 orgpos.append (p.at(0));
613 headings.append (bi->getHeading());
615 for (int j=0;j<bi->branchCount();++j)
617 bi2=bi->getBranchNum (j);
618 bo=(BranchObj*)bi2->getLMO();
621 mapobjects.append (bo);
622 p=bo->getBoundingPolygon();
625 vectors.append (QPointF(0,0));
626 orgpos.append (p.at(0));
627 headings.append (bi2->getHeading());
632 // Iterate moving bounding polygons until we have no more collisions
637 for (int i=0; i<polys.size()-1; ++i)
639 for (int j=i+1; j<polys.size();++j)
641 if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
644 //cout << "Collision: "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<endl;
645 v=polys.at(j).centroid()-polys.at(i).centroid();
646 // Move also away if centroids are identical
649 //cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<endl;
650 v.setX (rand()%200 -100);
651 v.setY (rand()%200 -100);
656 //cout << " v="<<v<<endl;
663 for (int i=0;i<vectors.size();i++)
665 //cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
666 polys[i].translate (vectors[i]);
668 cout << "Collisions: "<<collisions<<endl;
672 // Finally move the real objects and update
673 QList <LinkableMapObj::Orientation> orients;
674 for (int i=0;i<polys.size();i++)
676 Vector v=polys[i].at(0)-orgpos[i];
677 orients.append (mapobjects[i]->getOrientation());
678 mapobjects[i]->moveBy(v.x(),v.y() );
679 mapobjects[i]->setRelPos();
682 orientationChanged=false;
683 for (int i=0;i<polys.size();i++)
684 if (orients[i]!=mapobjects[i]->getOrientation())
686 orientationChanged=true;
689 cout << "Final: orientChanged="<<orientationChanged<<endl;
691 //orientationChanged=false;
692 } // loop if orientation has changed
694 model->emitSelectionChanged();
697 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
702 if (i>0) return bi->parent()->getBranchNum(i-1);
707 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
711 int dz=selbi->depth(); // original depth
713 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
718 // Look for branch with same parent but directly above
720 bi=getBranchDirectBelow(selbi);
722 bi=getBranchDirectAbove (selbi);
725 // direct predecessor
728 // Go towards center and look for predecessor
729 while (selbi->depth()>0)
731 selbi=(BranchItem*)(selbi->parent());
732 if (selbi->depth()==1 && invert)
733 bi=getBranchDirectBelow (selbi);
735 bi=getBranchDirectAbove (selbi);
740 while (selbi->depth()<dz)
742 // try to get back to original depth dz
743 bi=selbi->getLastBranch();
757 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
762 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
767 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
772 int dz=selbi->depth(); // original depth
774 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
778 // Look for branch with same parent but directly below
780 bi=getBranchDirectAbove (selbi);
782 bi=getBranchDirectBelow (selbi);
788 // Go towards center and look for neighbour
789 while (selbi->depth()>0)
791 selbi=(BranchItem*)(selbi->parent());
792 if (selbi->depth()==1 && invert)
793 bi=getBranchDirectAbove (selbi);
795 bi=getBranchDirectBelow (selbi);
800 while (selbi->depth()<dz)
802 // try to get back to original depth dz
803 bi=selbi->getFirstBranch();
817 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
822 // Special case: use alternative selection index
823 return bi->getLastSelectedBranchAlt();
824 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
826 return (BranchItem*)(bi->parent());
829 if (bi->getType()== TreeItem::Branch )
830 return bi->getLastSelectedBranch();
835 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
839 if (bi->depth()==0) return bi->getLastSelectedBranch();
840 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
842 return (BranchItem*)(bi->parent());
845 if (bi->getType()== TreeItem::Branch )
846 return (BranchItem*)bi->getLastSelectedBranch();
853 void MapEditor::cursorUp()
855 BranchItem *bi=model->getSelectedBranch();
856 if (bi) model->select (getBranchAbove(bi));
859 void MapEditor::cursorDown()
862 BranchItem *bi=model->getSelectedBranch();
863 if (bi) model->select (getBranchBelow(bi));
866 void MapEditor::cursorLeft()
868 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
869 if (bi) model->select (bi);
872 void MapEditor::cursorRight()
874 BranchItem *bi=getRightBranch (model->getSelectedBranch());
875 if (bi) model->select (bi);
878 void MapEditor::cursorFirst()
880 model->selectFirstBranch();
883 void MapEditor::cursorLast()
885 model->selectLastBranch();
889 void MapEditor::editHeading()
893 editHeadingFinished();
896 BranchObj *bo=model->getSelectedBranchObj();
897 BranchItem *bi=model->getSelectedBranch();
900 model->setSelectionBlocked(true);
902 lineEdit->setText (bi->getHeading());
903 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
904 lineEdit->setGeometry(p.x(),p.y(),230,25);
905 //lineEdit->selectAll();
906 //lineEdit->setCursorPosition (1);
908 lineEdit->setFocus();
909 lineEdit->grabKeyboard();
914 void MapEditor::editHeadingFinished()
916 editingHeading=false;
917 lineEdit->releaseKeyboard();
918 model->setHeading (lineEdit->text() );
919 model->setSelectionBlocked(false);
922 // Maybe reselect previous branch
923 mainWindow->editHeadingFinished (model);
927 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
929 // Lineedits are already closed by preceding
930 // mouseEvent, we don't need to close here.
932 QPointF p = mapToScene(e->pos());
933 TreeItem *ti=findMapItem (p, NULL);
934 LinkableMapObj* lmo=NULL;
935 if (ti) lmo=((MapItem*)ti)->getLMO();
938 { // MapObj was found
939 if (model->getSelectedLMO() != lmo)
945 if (model->getSelectedBranchObj() )
947 // Context Menu on branch or mapcenter
948 branchContextMenu->popup(e->globalPos() );
951 if (model->getSelectedImage() )
953 // Context Menu on floatimage
954 floatimageContextMenu->popup(e->globalPos() );
958 { // No MapObj found, we are on the Canvas itself
959 // Context Menu on scene
961 // Open context menu synchronously to position new mapcenter
962 model->setContextPos (p);
963 canvasContextMenu->exec(e->globalPos() );
964 model->unsetContextPos ();
969 void MapEditor::keyPressEvent(QKeyEvent* e)
971 if (e->modifiers() & Qt::ControlModifier)
973 switch (mainWindow->getModMode())
975 case Main::ModModeColor:
976 setCursor (PickColorCursor);
978 case Main::ModModeCopy:
979 setCursor (CopyCursor);
981 case Main::ModModeXLink:
982 setCursor (XLinkCursor);
985 setCursor (Qt::ArrowCursor);
991 void MapEditor::keyReleaseEvent(QKeyEvent* e)
993 if (!(e->modifiers() & Qt::ControlModifier))
994 setCursor (Qt::ArrowCursor);
997 void MapEditor::mousePressEvent(QMouseEvent* e)
999 //cout << "ME::mousePressed\n"; //FIXME-3
1000 // Ignore right clicks, these will go to context menus
1001 if (e->button() == Qt::RightButton )
1003 //cout << " ME::ignoring right mouse event...\n";
1008 //Ignore clicks while editing heading
1009 if (model->isSelectionBlocked() )
1011 //cout << " ME::ignoring other mouse event...\n";
1016 QPointF p = mapToScene(e->pos());
1017 TreeItem *ti=findMapItem (p, NULL);
1018 LinkableMapObj* lmo=NULL;
1019 if (ti) lmo=((MapItem*)ti)->getLMO();
1024 //Take care of system flags _or_ modifier modes
1026 if (lmo && ti->isBranchLikeType() )
1028 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
1029 if (!foname.isEmpty())
1031 // systemFlag clicked
1032 model->select (lmo);
1033 if (foname=="system-url")
1035 if (e->state() & Qt::ControlModifier)
1036 mainWindow->editOpenURLTab();
1038 mainWindow->editOpenURL();
1040 else if (foname=="system-vymLink")
1042 mainWindow->editOpenVymLink();
1043 // tabWidget may change, better return now
1044 // before segfaulting...
1045 } else if (foname=="system-note")
1046 mainWindow->windowToggleNoteEditor();
1047 else if (foname=="hideInExport")
1048 model->toggleHideExport();
1049 // FIXME-3 needed? xelection.update();
1053 // Take care of xLink
1054 if (ti->xlinkCount()>0 && lmo->getBBox().width()>30)
1056 if ((lmo->getOrientation()!=LinkableMapObj::RightOfCenter && p.x() < lmo->getBBox().left()+20) ||
1057 (lmo->getOrientation()!=LinkableMapObj::LeftOfCenter && p.x() > lmo->getBBox().right()-20) )
1059 // FIXME-4 if (ti->xlinkCount()>1)
1063 QList <QAction*> alist;
1064 for (int i=0;i<ti->xlinkCount();i++)
1065 alist.append (new QAction(ti->getXLinkNum(i)->getOtherBranch(ti)->getHeading(),&menu));
1066 menu.addActions (alist);
1067 QAction *ra=menu.exec (e->globalPos() );
1069 model->select (ti->getXLinkNum(alist.indexOf(ra))->getOtherBranch (ti));
1070 while (!alist.isEmpty())
1072 QAction *a=alist.takeFirst();
1078 model->select (ti->getXLinkNum(0)->getOtherBranch (ti));
1086 // No system flag clicked, take care of modmodes (CTRL-Click)
1087 if (e->state() & Qt::ControlModifier)
1089 if (mainWindow->getModMode()==Main::ModModeColor)
1092 setCursor (PickColorCursor);
1095 if (mainWindow->getModMode()==Main::ModModeXLink)
1097 BranchItem *bi_begin=model->getSelectedBranch();
1101 tmpXLink=model->createXLink(bi_begin,true);
1102 tmpXLink->setColor(model->getMapDefXLinkColor());
1103 tmpXLink->setWidth(model->getMapDefXLinkWidth());
1104 tmpXLink->setEnd (p);
1105 tmpXLink->updateXLink();
1109 } // End of modmodes
1114 cout << "ME::mouse pressed\n";
1115 cout << " lmo="<<lmo<<endl;
1116 cout << " ti="<<ti->getHeadingStd()<<endl;
1118 // Select the clicked object
1123 // Left Button Move Branches
1124 if (e->button() == Qt::LeftButton )
1126 movingObj_start.setX( p.x() - lmo->x() );
1127 movingObj_start.setY( p.y() - lmo->y() );
1128 movingObj_orgPos.setX (lmo->x() );
1129 movingObj_orgPos.setY (lmo->y() );
1131 movingObj_orgRelPos=lmo->getRelPos();
1133 // If modMode==copy, then we want to "move" the _new_ object around
1134 // then we need the offset from p to the _old_ selection, because of tmp
1135 if (mainWindow->getModMode()==Main::ModModeCopy &&
1136 e->state() & Qt::ControlModifier)
1138 BranchItem *bi=model->getSelectedBranch();
1142 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
1143 //bi->addBranch (model->getSelectedBranch());
1145 model->select(bi->getLastBranch());
1146 model->reposition();
1150 movingObj=model->getSelectedLMO();
1152 // Middle Button Toggle Scroll
1153 // (On Mac OS X this won't work, but we still have
1154 // a button in the toolbar)
1155 if (e->button() == Qt::MidButton )
1156 model->toggleScroll();
1157 // model->updateActions(); FIXME-3 needed?
1158 // FIXME-3 needed? xelection.update();
1160 { // No MapObj found, we are on the scene itself
1161 // Left Button move Pos of sceneView
1162 if (e->button() == Qt::LeftButton )
1164 movingObj=NULL; // move Content not Obj
1165 movingObj_start=e->globalPos();
1166 movingCont_start=QPointF (
1167 horizontalScrollBar()->value(),
1168 verticalScrollBar()->value());
1169 movingVec=QPointF(0,0);
1170 setCursor(HandOpenCursor);
1175 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1177 QPointF p = mapToScene(e->pos());
1178 TreeItem *seli=model->getSelectedItem();
1179 LinkableMapObj* lmosel=NULL;
1180 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1181 lmosel=((MapItem*)seli)->getLMO();
1183 // Move the selected MapObj
1184 if ( lmosel && movingObj)
1187 // reset cursor if we are moving and don't copy
1188 if (mainWindow->getModMode()!=Main::ModModeCopy)
1189 setCursor (Qt::ArrowCursor);
1191 // To avoid jumping of the sceneView, only
1192 // show selection, if not tmp linked
1193 /* FIXME-2 if (!lmosel->hasParObjTmp())
1194 model->emitShowSelection();
1197 // Now move the selection, but add relative position
1198 // (movingObj_start) where selection was chosen with
1199 // mousepointer. (This avoids flickering resp. jumping
1200 // of selection back to absPos)
1202 // Check if we could link
1203 TreeItem *ti=findMapItem (p, seli);
1204 BranchItem *dsti=NULL;
1205 LinkableMapObj* dst=NULL;
1206 if (ti && ti!=seli && ti->isBranchLikeType())
1208 dsti=(BranchItem*)ti;
1214 if (lmosel && seli->getType()==TreeItem::Image)
1216 FloatObj *fio=(FloatImageObj*)lmosel;
1217 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1219 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1220 model->emitSelectionChanged(); // position has changed
1222 // Relink float to new mapcenter or branch, if shift is pressed
1223 // Only relink, if selection really has a new parent
1224 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1226 // Also save the move which was done so far
1227 QString pold=qpointFToString(movingObj_orgRelPos);
1228 QString pnow=qpointFToString(fio->getRelPos());
1234 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1235 fio->getParObj()->requestReposition();
1236 model->reposition();
1238 model->relinkImage ((ImageItem*) seli,dsti);
1239 model->select (seli);
1240 //movingObj=lmosel; //FIXME-3
1241 //movingObj_orgRelPos=lmosel->getRelPos();
1243 model->reposition();
1246 { // selection != a FloatObj
1247 if (seli->depth()==0)
1250 lmosel->move (p-movingObj_start);
1251 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1253 // Move only mapcenter, leave its children where they are
1255 v=lmosel->getAbsPos();
1256 for (int i=0; i<seli->branchCount(); ++i)
1258 seli->getBranchObjNum(i)->setRelPos();
1259 seli->getBranchObjNum(i)->setOrientation();
1264 if (seli->depth()==1)
1267 if (!lmosel->hasParObjTmp())
1268 lmosel->move(p-movingObj_start);
1269 lmosel->setRelPos();
1272 // Move ordinary branch
1273 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1274 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1275 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1276 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1278 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1279 lmosel->setRelPos();
1283 // Maybe we can relink temporary?
1284 if (dsti) // FIXME-1 check if dsti is ancestor of myself!
1286 if (e->modifiers()==Qt::ControlModifier)
1288 // Special case: CTRL to link below dst
1289 lmosel->setParObjTmp (dst,p,+1);
1290 } else if (e->modifiers()==Qt::ShiftModifier)
1291 lmosel->setParObjTmp (dst,p,-1);
1293 lmosel->setParObjTmp (dst,p,0);
1296 lmosel->unsetParObjTmp();
1298 // reposition subbranch
1299 lmosel->reposition();
1301 QItemSelection sel=model->getSelectionModel()->selection();
1302 updateSelection(sel,sel); // position has changed
1304 } // no FloatImageObj
1308 } // selection && moving_obj
1310 // Draw a link from one branch to another
1313 tmpXLink->setEnd (p);
1314 tmpXLink->updateXLink();
1318 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1320 QPointF p=e->globalPos();
1321 movingVec.setX(-p.x() + movingObj_start.x() );
1322 movingVec.setY(-p.y() + movingObj_start.y() );
1323 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1324 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1329 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1331 QPointF p = mapToScene(e->pos());
1332 TreeItem *seli=model->getSelectedItem();
1334 TreeItem *dsti=NULL;
1335 if (seli) dsti=findMapItem(p, seli);
1336 LinkableMapObj* dst=NULL;
1337 if (dsti && dsti->isBranchLikeType ())
1338 dst=((MapItem*)dsti)->getLMO();
1343 // Have we been picking color?
1347 setCursor (Qt::ArrowCursor);
1348 // Check if we are over another branch
1351 if (e->state() & Qt::ShiftModifier)
1352 model->colorBranch (((BranchObj*)dst)->getColor());
1354 model->colorSubtree (((BranchObj*)dst)->getColor());
1359 // Have we been drawing a link?
1363 // Check if we are over another branch
1366 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1367 tmpXLink->updateXLink();
1368 tmpXLink->activate();
1370 tmpXLink,QString("delete ()"),
1371 dsti,QString("addXLink (\"%1\",\"%2\")").arg(model->getSelectString(tmpXLink->getBegin())).arg(model->getSelectString(dsti)),
1372 QString("Add xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(dsti))
1376 model->deleteItem(tmpXLink);
1382 // Have we been moving something?
1383 if ( seli && movingObj )
1385 if (seli->getType()==TreeItem::Image)
1387 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1390 // Moved FloatObj. Maybe we need to reposition
1391 QString pold=qpointFToString(movingObj_orgRelPos);
1392 QString pnow=qpointFToString(fio->getRelPos());
1398 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1400 cout << "ME::release mouse\n";
1401 fio->getParObj()->requestReposition();
1402 model->reposition();
1406 BranchItem *bi=model->getSelectedBranch();
1407 if (bi && bi->depth()==0)
1409 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1411 QString pold=qpointFToString(movingObj_orgPos);
1412 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1419 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1423 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1424 { // A branch was moved
1425 LinkableMapObj* lmosel=NULL;
1426 lmosel=((MapItem*)seli)->getLMO();
1428 // save the position in case we link to mapcenter
1429 QPointF savePos=QPointF (lmosel->getAbsPos() );
1431 // Reset the temporary drawn link to the original one
1432 lmosel->unsetParObjTmp();
1434 // For Redo we may need to save original selection
1435 QString preSelStr=model->getSelectString(seli);
1438 if (dsti && objectMoved)
1440 // We have a destination, relink to that
1442 BranchObj* bsel=model->getSelectedBranchObj();
1444 QString preParStr=model->getSelectString (bsel->getParObj());
1445 QString preNum=QString::number (seli->num(),10);
1446 QString preDstParStr;
1449 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1451 preDstParStr=model->getSelectString (dst->getParObj());
1452 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1454 if (e->state() & Qt::ControlModifier && dst->getParObj())
1457 preDstParStr=model->getSelectString (dst->getParObj());
1458 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1461 preDstParStr=model->getSelectString(dst);
1462 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1463 if (dsti->depth()==0) bsel->move (savePos);
1467 QString postSelStr=model->getSelectString(lmosel);
1468 QString postNum=QString::number (seli->num(),10);
1470 QString undoCom="relinkTo (\""+
1471 preParStr+ "\"," + preNum +"," +
1472 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1474 QString redoCom="relinkTo (\""+
1475 preDstParStr + "\"," + postNum + "," +
1476 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1481 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1486 // No destination, undo temporary move
1488 if (seli->depth()==1)
1490 // The select string might be different _after_ moving around.
1491 // Therefor reposition and then use string of old selection, too
1492 model->reposition();
1494 QPointF rp(lmosel->getRelPos());
1495 if (rp != movingObj_orgRelPos)
1497 QString ps=qpointFToString(rp);
1499 model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1500 preSelStr, "moveRel "+ps,
1501 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1505 // Draw the original link, before selection was moved around
1506 if (settings.value("/animation/use",true).toBool() && seli->depth()>1)
1508 lmosel->setRelPos(); // calc relPos first for starting point
1510 model->startAnimation(
1512 lmosel->getRelPos(),
1516 model->reposition();
1519 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1520 // Finally resize scene, if needed
1526 // maybe we moved View: set old cursor
1527 setCursor (Qt::ArrowCursor);
1531 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1535 cout << "ME p="<<mapToScene (e->pos())<<" scrollBarPos="<<getScrollBarPos();
1536 cout << " min="<<QPointF(horizontalScrollBar()->minimum(),verticalScrollBar()->minimum());
1541 if (model->isSelectionBlocked() )
1547 if (e->button() == Qt::LeftButton )
1549 QPointF p = mapToScene(e->pos());
1550 TreeItem *ti=findMapItem (p, NULL);
1551 if (ti) { // MapObj was found
1552 // First select the MapObj than edit heading
1559 void MapEditor::resizeEvent (QResizeEvent* e)
1561 QGraphicsView::resizeEvent( e );
1564 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1566 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1567 // cerr << event->format(i) << endl;
1569 if (event->mimeData()->hasImage())
1570 event->acceptProposedAction();
1572 if (event->mimeData()->hasUrls())
1573 event->acceptProposedAction();
1576 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1580 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1585 void MapEditor::dropEvent(QDropEvent *event)
1587 BranchItem *selbi=model->getSelectedBranch();
1591 foreach (QString format,event->mimeData()->formats())
1592 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1596 if (event->mimeData()->hasImage())
1598 QVariant imageData = event->mimeData()->imageData();
1599 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1601 if (event->mimeData()->hasUrls())
1602 uris=event->mimeData()->urls();
1610 for (int i=0; i<uris.count();i++)
1612 // Workaround to avoid adding empty branches
1613 if (!uris.at(i).toString().isEmpty())
1615 bi=model->addNewBranch();
1619 s=uris.at(i).toLocalFile();
1622 QString file = QDir::fromNativeSeparators(s);
1623 heading = QFileInfo(file).baseName();
1625 if (file.endsWith(".vym", false))
1626 bi->setVymLink(file);
1628 bi->setURL(uris.at(i).toString());
1631 bo->setURL(uris.at(i).toString());
1635 if (!heading.isEmpty())
1636 bi->setHeading(heading);
1638 bi->setHeading(uris.at(i).toString());
1643 model->reposition();
1646 event->acceptProposedAction();
1649 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1651 // Note: Here we are prepared for multiple selections, though this
1652 // is not yet implemented elsewhere
1654 // Here in MapEditor we can only select Branches and Images
1655 QList <TreeItem*> treeItemsNew;
1656 QList <TreeItem*> treeItemsOld;
1658 QModelIndex newIndex;
1660 bool do_reposition=false;
1663 foreach (ix,newsel.indexes() )
1665 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1666 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1667 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1669 foreach (ix,oldsel.indexes() )
1671 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1672 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1673 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1676 // Trim list of selection rectangles
1677 while (treeItemsNew.count() < selboxList.count() )
1678 delete selboxList.takeFirst();
1680 // Take care to tmp scroll/unscroll
1681 if (!oldsel.isEmpty())
1683 QModelIndex ix=oldsel.indexes().first();
1686 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1689 if (ti->isBranchLikeType() )
1691 // reset tmp scrolled branches
1692 BranchItem *bi=(BranchItem*)ti;
1693 if (bi->resetTmpUnscroll() )
1696 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1697 // Hide link if not needed
1698 ((MapItem*)ti)->getLMO()->updateVisibility();
1703 if (!treeItemsNew.isEmpty())
1705 QModelIndex ix=newsel.indexes().first();
1710 // Temporary unscroll if necessary
1711 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1712 if (ti->isBranchLikeType() )
1714 BranchItem *bi=(BranchItem*)ti;
1715 if (bi->hasScrolledParent(bi) )
1717 if (bi->parentBranch()->tmpUnscroll() )
1721 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1722 // Show link if needed
1723 ((MapItem*)ti)->getLMO()->updateVisibility();
1726 if (do_reposition) model->reposition();
1728 // Reduce rectangles
1729 while (treeItemsNew.count() < selboxList.count() )
1730 delete selboxList.takeFirst();
1732 // Add additonal rectangles
1733 QGraphicsRectItem *sb;
1734 while (treeItemsNew.count() > selboxList.count() )
1736 sb = mapScene->addRect(
1738 QPen(selectionColor),
1740 sb->setZValue(Z_SELBOX);
1742 selboxList.append (sb);
1745 // Reposition rectangles
1749 LinkableMapObj *lmo;
1750 for (int i=0; i<treeItemsNew.count();++i)
1752 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1753 bbox=lmo->getBBox();
1754 sb=selboxList.at(i);
1757 bbox.width(), bbox.height());
1758 sb->setPen (selectionColor);
1759 sb->setBrush (selectionColor);
1766 void MapEditor::updateData (const QModelIndex &sel)
1768 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1771 cout << "ME::updateData\n";
1773 cout << " ti="<<ti<<endl;
1774 cout << " h="<<ti->getHeading().toStdString()<<endl;
1777 if (ti->isBranchLikeType())
1779 // cout << " ->updating...\n";
1780 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1785 void MapEditor::setSelectionColor (QColor col)
1788 QItemSelection sel=model->getSelectionModel()->selection();
1789 updateSelection(sel,sel);
1793 QColor MapEditor::getSelectionColor ()
1795 return selectionColor;