9 #include "branchitem.h"
11 #include "mainwindow.h"
13 #include "warningdialog.h"
14 #include "xlinkitem.h"
17 extern int statusbarTime;
18 extern Main *mainWindow;
19 extern QString tmpVymDir;
20 extern QString clipboardDir;
21 extern QString clipboardFile;
22 extern bool clipboardEmpty;
25 extern QMenu* branchContextMenu;
26 extern QMenu* branchAddContextMenu;
27 extern QMenu* branchRemoveContextMenu;
28 extern QMenu* branchLinksContextMenu;
29 extern QMenu* branchXLinksContextMenuEdit;
30 extern QMenu* branchXLinksContextMenuFollow;
31 extern QMenu* floatimageContextMenu;
32 extern QMenu* canvasContextMenu;
34 extern Settings settings;
35 extern QString iconPath;
37 ///////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////
39 MapEditor::MapEditor( VymModel *vm)
41 //cout << "Constructor ME "<<this<<endl;
42 mapScene= new QGraphicsScene(NULL);
43 mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
45 zoomFactor=zoomFactorTarget=1;
48 model->setScene (mapScene);
49 model->registerEditor(this);
50 model->makeDefault(); // No changes in model so far
56 // Create bitmap cursors, platform dependant
57 HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);
58 PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 );
59 CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 );
60 XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 );
62 //setFocusPolicy (Qt::StrongFocus); //FIXME-3
75 setAcceptDrops (true);
77 //model->reposition(); //FIXME-3 really still needed?
80 // Shortcuts and actions
82 a = new QAction("Select upper branch", this);
83 a->setShortcut (Qt::Key_Up );
84 a->setShortcutContext (Qt::WidgetShortcut);
86 connect( a, SIGNAL( triggered() ), this, SLOT( cursorUp() ) );
88 a = new QAction( "Select lower branch",this);
89 a->setShortcut ( Qt::Key_Down );
90 a->setShortcutContext (Qt::WidgetShortcut);
92 connect( a, SIGNAL( triggered() ), this, SLOT( cursorDown() ) );
94 a = new QAction( "Select left branch", this);
95 a->setShortcut (Qt::Key_Left );
96 // a->setShortcutContext (Qt::WindowShortcut);
97 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
99 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLeft() ) );
101 a = new QAction( "Select child branch", this);
102 a->setShortcut (Qt::Key_Right);
103 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
105 connect( a, SIGNAL( triggered() ), this, SLOT( cursorRight() ) );
107 a = new QAction( "Select first branch", this);
108 a->setShortcut (Qt::Key_Home );
109 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
111 connect( a, SIGNAL( triggered() ), this, SLOT( cursorFirst() ) );
113 a = new QAction( "Select last branch",this);
114 a->setShortcut ( Qt::Key_End );
115 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
117 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLast() ) );
119 // Action to embed LineEdit for heading in Scene
120 editingHeading=false;
121 lineEdit=new QLineEdit;
123 QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
124 pw->setZValue (Z_LINEEDIT);
126 a = new QAction( tr( "Edit heading","MapEditor" ), this);
127 a->setShortcut ( Qt::Key_Return ); //Edit heading
129 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
130 a = new QAction( tr( "Edit heading","MapEditor" ), this);
131 a->setShortcut ( Qt::Key_Enter); //Edit heading
133 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
136 selectionColor =QColor (255,255,0);
139 // Attributes //FIXME-2 testing only...
142 attrTable= new AttributeTable();
144 ad=attrTable->addKey (k,StringList);
148 sl <<"val 1"<<"val 2"<< "val 3";
149 ad->setValue (QVariant (sl));
151 //attrTable->addValue ("Key A","P 1");
152 //attrTable->addValue ("Key A","P 2");
153 //attrTable->addValue ("Key A","P 3");
154 //attrTable->addValue ("Key A","P 4");
156 ad=attrTable->addKey (k,FreeString);
159 //attrTable->addValue ("Key B","w1");
160 //attrTable->addValue ("Key B","w2");
162 k="C - UniqueString";
163 ad=attrTable->addKey (k,UniqueString);
166 //attrTable->addKey ("Key Prio");
167 //attrTable->addValue ("Key Prio","Prio 1");
168 //attrTable->addValue ("Key Prio","Prio 2");
172 MapEditor::~MapEditor()
174 //cout <<"Destructor MapEditor for "<<model->getMapName().toStdString()<<endl;
175 model->unregisterEditor(this);
178 VymModel* MapEditor::getModel()
183 QGraphicsScene * MapEditor::getScene()
188 void MapEditor::scrollTo (const QModelIndex &index)
192 LinkableMapObj* lmo=NULL;
193 TreeItem *ti= static_cast<TreeItem*>(index.internalPointer());
194 if (ti->getType()==TreeItem::Image ||ti->isBranchLikeType() )
195 lmo=((MapItem*)ti)->getLMO();
198 QRectF r=lmo->getBBox();
199 setScrollBarPosTarget (r);
204 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
206 // Code copied from Qt sources
210 qreal width = viewport()->width();
211 qreal height = viewport()->height();
212 QRectF viewRect = matrix().mapRect(rect);
214 qreal left = horizontalScrollBar()->value();
215 qreal right = left + width;
216 qreal top = verticalScrollBar()->value();
217 qreal bottom = top + height;
219 scrollBarPosTarget=getScrollBarPos();
221 if (viewRect.left() <= left + xmargin) {
222 // need to scroll from the left
223 // if (!d->leftIndent)
224 scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
226 if (viewRect.right() >= right - xmargin) {
227 // need to scroll from the right
228 // if (!d->leftIndent)
229 scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
231 if (viewRect.top() <= top + ymargin) {
232 // need to scroll from the top
233 // if (!d->topIndent)
234 scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
236 if (viewRect.bottom() >= bottom - ymargin) {
237 // need to scroll from the bottom
238 // if (!d->topIndent)
239 scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
242 if (scrollBarPosTarget==getScrollBarPos()) return;
244 if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
245 scrollBarPosAnimation.stop();
247 if (settings.value ("/animation/use/",true).toBool() )
249 scrollBarPosAnimation.setTargetObject (this);
250 scrollBarPosAnimation.setPropertyName ("scrollBarPos");
251 scrollBarPosAnimation.setDuration(1000);
252 scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
253 scrollBarPosAnimation.setStartValue(
254 QPointF (horizontalScrollBar()->value() ,
255 verticalScrollBar()->value() ) );
256 scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
257 scrollBarPosAnimation.start();
259 setScrollBarPos (scrollBarPosTarget);
262 QPointF MapEditor::getScrollBarPosTarget()
264 return scrollBarPosTarget;
268 void MapEditor::setScrollBarPos(const QPointF &p)
271 horizontalScrollBar()->setValue(int(p.x()));
272 verticalScrollBar()->setValue(int(p.y()));
275 QPointF MapEditor::getScrollBarPos()
277 return QPointF (horizontalScrollBar()->value(),verticalScrollBar()->value());
278 //return scrollBarPos;
281 void MapEditor::setZoomFactorTarget (const qreal &zft)
283 zoomFactorTarget=zft;
284 if (zoomAnimation.state()==QtAbstractAnimation::Running)
285 zoomAnimation.stop();
286 if (settings.value ("/animation/use/",true).toBool() )
288 //zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
289 zoomAnimation.setTargetObject (this);
290 zoomAnimation.setPropertyName ("zoomFactor");
291 zoomAnimation.setDuration(1000);
292 zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
293 zoomAnimation.setStartValue(zoomFactor);
294 zoomAnimation.setEndValue(zft);
295 zoomAnimation.start();
300 qreal MapEditor::getZoomFactorTarget()
302 return zoomFactorTarget;
306 void MapEditor::setZoomFactor(const qreal &zf)
309 setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
312 qreal MapEditor::getZoomFactor()
317 void MapEditor::print()
321 printer = new QPrinter;
322 printer->setColorMode (QPrinter::Color);
323 printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
324 printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
325 printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
328 QRectF totalBBox=getTotalBBox();
330 // Try to set orientation automagically
331 // Note: Interpretation of generated postscript is amibiguous, if
332 // there are problems with landscape mode, see
333 // http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
335 if (totalBBox.width()>totalBBox.height())
336 // recommend landscape
337 printer->setOrientation (QPrinter::Landscape);
339 // recommend portrait
340 printer->setOrientation (QPrinter::Portrait);
342 if ( printer->setup(this) )
343 // returns false, if printing is canceled
345 QPainter pp(printer);
347 pp.setRenderHint(QPainter::Antialiasing,true);
349 // Don't print the visualisation of selection
352 QRectF mapRect=totalBBox;
353 QGraphicsRectItem *frame=NULL;
357 // Print frame around map
358 mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10,
359 totalBBox.width()+20, totalBBox.height()+20);
360 frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
366 double paperAspect = (double)printer->width() / (double)printer->height();
367 double mapAspect = (double)mapRect.width() / (double)mapRect.height();
369 if (mapAspect>=paperAspect)
371 // Fit horizontally to paper width
372 //pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );
373 viewBottom=(int)(printer->width()/mapAspect);
376 // Fit vertically to paper height
377 //pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());
378 viewBottom=printer->height();
383 // Print footer below map
385 font.setPointSize(10);
387 QRectF footerBox(0,viewBottom,printer->width(),15);
388 // FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
389 pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
393 QRectF (0,0,printer->width(),printer->height()-15),
394 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
397 // Viewport has paper dimension
398 if (frame) delete (frame);
403 // Save settings in vymrc
404 settings.writeEntry("/mainwindow/printerName",printer->printerName());
405 settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
406 settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
410 QRectF MapEditor::getTotalBBox() //FIXME-2 needed e.g. for image export
414 for (int i=0;i<rootItem->branchCount(); i++)
415 r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
421 QPixmap MapEditor::getPixmap()
423 QRectF mapRect=getTotalBBox();
424 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
427 pp.setRenderHints(renderHints());
429 // Don't print the visualisation of selection
432 mapScene->render ( &pp,
433 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
434 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
443 void MapEditor::setAntiAlias (bool b)
445 setRenderHint(QPainter::Antialiasing,b);
448 void MapEditor::setSmoothPixmap(bool b)
450 setRenderHint(QPainter::SmoothPixmapTransform,b);
453 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
455 // Start with mapcenter, no images allowed at rootItem
457 BranchItem *bi=model->getRootItem()->getFirstBranch();
458 TreeItem *found=NULL;
461 found=bi->findMapItem (p, exclude);
462 if (found) return found;
464 bi=model->getRootItem()->getBranchNum(i);
469 AttributeTable* MapEditor::attributeTable()
474 void MapEditor::testFunction1()
476 cout << "ME::test1 selected TI="<<model->getSelectedItem()<<endl;
478 BranchObj *bo=model->getSelectedBranchObj();
481 bo->moveBy (100,100);
486 // Code copied from Qt sources
487 QRectF rect=model->getSelectedBranchObj()->getBBox();
491 qreal width = viewport()->width();
492 qreal height = viewport()->height();
493 QRectF viewRect = matrix().mapRect(rect);
495 qreal left = horizontalScrollBar()->value();
496 qreal right = left + width;
497 qreal top = verticalScrollBar()->value();
498 qreal bottom = top + height;
500 if (viewRect.left() <= left + xmargin) {
501 // need to scroll from the left
502 // if (!d->leftIndent)
503 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
505 if (viewRect.right() >= right - xmargin) {
506 // need to scroll from the right
507 // if (!d->leftIndent)
508 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
510 if (viewRect.top() <= top + ymargin) {
511 // need to scroll from the top
512 // if (!d->topIndent)
513 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
515 if (viewRect.bottom() >= bottom - ymargin) {
516 // need to scroll from the bottom
517 // if (!d->topIndent)
518 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
520 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
521 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
526 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
527 animation->setDuration(5000);
528 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
529 animation->setEasingCurve ( QtEasingCurve::OutQuint);
530 animation->setStartValue(sceneRect() );
531 animation->setEndValue(QRectF(50, 50, 1000, 1000));
536 QDialog *dia= new QDialog (this);
537 dia->setGeometry (50,50,10,10);
542 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
543 animation->setDuration(1000);
544 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
545 animation->setEasingCurve ( QtEasingCurve::OutQuint);
546 animation->setStartValue(QRect(50, 50, 10, 10));
547 animation->setEndValue(QRect(250, 250, 100, 100));
552 /* FIXME-4 Hide hidden stuff temporary, maybe add this as regular function somewhere
553 if (hidemode==HideNone)
555 setHideTmpMode (HideExport);
556 mapCenter->calcBBoxSizeWithChilds();
557 QRectF totalBBox=mapCenter->getTotalBBox();
558 QRectF mapRect=totalBBox;
559 QCanvasRectangle *frame=NULL;
561 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
563 mapRect.setRect (totalBBox.x(), totalBBox.y(),
564 totalBBox.width(), totalBBox.height());
565 frame=new QCanvasRectangle (mapRect,mapScene);
566 frame->setBrush (QColor(white));
567 frame->setPen (QColor(black));
573 setHideTmpMode (HideNone);
575 cout <<" hidemode="<<hidemode<<endl;
580 if (hidemode==HideExport)
581 setHideTmpMode (HideNone);
583 setHideTmpMode (HideExport);
588 void MapEditor::testFunction2()
590 // Create list with all bounding polygons
591 QList <LinkableMapObj*> mapobjects;
592 QList <ConvexPolygon> polys;
594 QList <Vector> vectors;
595 QList <Vector> orgpos;
596 QStringList headings; //FIXME-3 testing only
602 // Outer loop: Iterate until we no more changes in orientation
603 bool orientationChanged=true;
604 while (orientationChanged)
606 BranchItem *ri=model->getRootItem();
607 for (int i=0;i<ri->branchCount();++i)
609 bi=ri->getBranchNum (i);
610 bo=(BranchObj*)bi->getLMO();
613 mapobjects.append (bo);
614 p=bo->getBoundingPolygon();
617 vectors.append (QPointF(0,0));
618 orgpos.append (p.at(0));
619 headings.append (bi->getHeading());
621 for (int j=0;j<bi->branchCount();++j)
623 bi2=bi->getBranchNum (j);
624 bo=(BranchObj*)bi2->getLMO();
627 mapobjects.append (bo);
628 p=bo->getBoundingPolygon();
631 vectors.append (QPointF(0,0));
632 orgpos.append (p.at(0));
633 headings.append (bi2->getHeading());
638 // Iterate moving bounding polygons until we have no more collisions
643 for (int i=0; i<polys.size()-1; ++i)
645 for (int j=i+1; j<polys.size();++j)
647 if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
650 //cout << "Collision: "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<endl;
651 v=polys.at(j).centroid()-polys.at(i).centroid();
652 // Move also away if centroids are identical
655 //cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<endl;
656 v.setX (rand()%200 -100);
657 v.setY (rand()%200 -100);
662 //cout << " v="<<v<<endl;
669 for (int i=0;i<vectors.size();i++)
671 //cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
672 polys[i].translate (vectors[i]);
674 cout << "Collisions: "<<collisions<<endl;
678 // Finally move the real objects and update
679 QList <LinkableMapObj::Orientation> orients;
680 for (int i=0;i<polys.size();i++)
682 Vector v=polys[i].at(0)-orgpos[i];
683 orients.append (mapobjects[i]->getOrientation());
684 mapobjects[i]->moveBy(v.x(),v.y() );
685 mapobjects[i]->setRelPos();
688 orientationChanged=false;
689 for (int i=0;i<polys.size();i++)
690 if (orients[i]!=mapobjects[i]->getOrientation())
692 orientationChanged=true;
695 cout << "Final: orientChanged="<<orientationChanged<<endl;
697 //orientationChanged=false;
698 } // loop if orientation has changed
700 model->emitSelectionChanged();
703 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
708 if (i>0) return bi->parent()->getBranchNum(i-1);
713 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
717 int dz=selbi->depth(); // original depth
719 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
724 // Look for branch with same parent but directly above
726 bi=getBranchDirectBelow(selbi);
728 bi=getBranchDirectAbove (selbi);
731 // direct predecessor
734 // Go towards center and look for predecessor
735 while (selbi->depth()>0)
737 selbi=(BranchItem*)(selbi->parent());
738 if (selbi->depth()==1 && invert)
739 bi=getBranchDirectBelow (selbi);
741 bi=getBranchDirectAbove (selbi);
746 while (selbi->depth()<dz)
748 // try to get back to original depth dz
749 bi=selbi->getLastBranch();
763 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
768 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
773 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
778 int dz=selbi->depth(); // original depth
780 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
784 // Look for branch with same parent but directly below
786 bi=getBranchDirectAbove (selbi);
788 bi=getBranchDirectBelow (selbi);
794 // Go towards center and look for neighbour
795 while (selbi->depth()>0)
797 selbi=(BranchItem*)(selbi->parent());
798 if (selbi->depth()==1 && invert)
799 bi=getBranchDirectAbove (selbi);
801 bi=getBranchDirectBelow (selbi);
806 while (selbi->depth()<dz)
808 // try to get back to original depth dz
809 bi=selbi->getFirstBranch();
823 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
828 // Special case: use alternative selection index
829 return bi->getLastSelectedBranchAlt();
830 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
832 return (BranchItem*)(bi->parent());
835 if (bi->getType()== TreeItem::Branch )
836 return bi->getLastSelectedBranch();
841 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
845 if (bi->depth()==0) return bi->getLastSelectedBranch();
846 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
848 return (BranchItem*)(bi->parent());
851 if (bi->getType()== TreeItem::Branch )
852 return (BranchItem*)bi->getLastSelectedBranch();
859 void MapEditor::cursorUp()
861 BranchItem *bi=model->getSelectedBranch();
862 if (bi) model->select (getBranchAbove(bi));
865 void MapEditor::cursorDown()
868 BranchItem *bi=model->getSelectedBranch();
869 if (bi) model->select (getBranchBelow(bi));
872 void MapEditor::cursorLeft()
874 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
875 if (bi) model->select (bi);
878 void MapEditor::cursorRight()
880 BranchItem *bi=getRightBranch (model->getSelectedBranch());
881 if (bi) model->select (bi);
884 void MapEditor::cursorFirst()
886 model->selectFirstBranch();
889 void MapEditor::cursorLast()
891 model->selectLastBranch();
895 void MapEditor::editHeading()
899 editHeadingFinished();
902 BranchObj *bo=model->getSelectedBranchObj();
903 BranchItem *bi=model->getSelectedBranch();
906 model->setSelectionBlocked(true);
908 lineEdit->setText (bi->getHeading());
909 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
910 lineEdit->setGeometry(p.x(),p.y(),230,25);
911 //lineEdit->selectAll();
912 //lineEdit->setCursorPosition (1);
914 lineEdit->setFocus();
915 lineEdit->grabKeyboard();
920 void MapEditor::editHeadingFinished()
922 editingHeading=false;
923 lineEdit->releaseKeyboard();
924 model->setHeading (lineEdit->text() );
925 model->setSelectionBlocked(false);
928 // Maybe reselect previous branch
929 mainWindow->editHeadingFinished (model);
933 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
935 // Lineedits are already closed by preceding
936 // mouseEvent, we don't need to close here.
938 QPointF p = mapToScene(e->pos());
939 TreeItem *ti=findMapItem (p, NULL);
940 LinkableMapObj* lmo=NULL;
941 if (ti) lmo=((MapItem*)ti)->getLMO();
944 { // MapObj was found
945 if (model->getSelectedLMO() != lmo)
951 if (model->getSelectedBranchObj() )
953 // Context Menu on branch or mapcenter
954 branchContextMenu->popup(e->globalPos() );
957 if (model->getSelectedImage() )
959 // Context Menu on floatimage
960 floatimageContextMenu->popup(e->globalPos() );
964 { // No MapObj found, we are on the Canvas itself
965 // Context Menu on scene
967 // Open context menu synchronously to position new mapcenter
968 model->setContextPos (p);
969 canvasContextMenu->exec(e->globalPos() );
970 model->unsetContextPos ();
975 void MapEditor::keyPressEvent(QKeyEvent* e)
977 if (e->modifiers() & Qt::ControlModifier)
979 switch (mainWindow->getModMode())
981 case Main::ModModeColor:
982 setCursor (PickColorCursor);
984 case Main::ModModeCopy:
985 setCursor (CopyCursor);
987 case Main::ModModeXLink:
988 setCursor (XLinkCursor);
991 setCursor (Qt::ArrowCursor);
997 void MapEditor::keyReleaseEvent(QKeyEvent* e)
999 if (!(e->modifiers() & Qt::ControlModifier))
1000 setCursor (Qt::ArrowCursor);
1003 void MapEditor::mousePressEvent(QMouseEvent* e)
1005 cout << "ME::mousePressed\n";
1006 // Ignore right clicks, these will go to context menus
1007 if (e->button() == Qt::RightButton )
1009 cout << " ME::ignoring right mouse event...\n";
1014 //Ignore clicks while editing heading
1015 if (model->isSelectionBlocked() )
1017 cout << " ME::ignoring other mouse event...\n";
1022 QPointF p = mapToScene(e->pos());
1023 TreeItem *ti=findMapItem (p, NULL);
1024 LinkableMapObj* lmo=NULL;
1025 if (ti) lmo=((MapItem*)ti)->getLMO();
1030 //Take care of system flags _or_ modifier modes
1032 if (lmo && ti->isBranchLikeType() )
1034 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
1035 if (!foname.isEmpty())
1037 // systemFlag clicked
1038 model->select (lmo);
1039 if (foname=="system-url")
1041 if (e->state() & Qt::ControlModifier)
1042 mainWindow->editOpenURLTab();
1044 mainWindow->editOpenURL();
1046 else if (foname=="system-vymLink")
1048 mainWindow->editOpenVymLink();
1049 // tabWidget may change, better return now
1050 // before segfaulting...
1051 } else if (foname=="note")
1052 mainWindow->windowToggleNoteEditor();
1053 else if (foname=="hideInExport")
1054 model->toggleHideExport();
1055 // FIXME-3 needed? xelection.update();
1059 // No system flag clicked, take care of modmodes (CTRL-Click)
1060 if (e->state() & Qt::ControlModifier)
1062 if (mainWindow->getModMode()==Main::ModModeColor)
1065 setCursor (PickColorCursor);
1068 if (mainWindow->getModMode()==Main::ModModeXLink)
1070 BranchItem *bi_begin=model->getSelectedBranch();
1074 tmpXLink=model->createXLink(bi_begin,true);
1075 tmpXLink->setColor(model->getMapDefXLinkColor());
1076 tmpXLink->setWidth(model->getMapDefXLinkWidth());
1077 tmpXLink->setEnd (p);
1078 tmpXLink->updateXLink();
1082 } // End of modmodes
1087 cout << "ME::mouse pressed\n";
1088 cout << " lmo="<<lmo<<endl;
1089 cout << " ti="<<ti->getHeadingStd()<<endl;
1091 // Select the clicked object
1096 // Left Button Move Branches
1097 if (e->button() == Qt::LeftButton )
1099 movingObj_start.setX( p.x() - lmo->x() );
1100 movingObj_start.setY( p.y() - lmo->y() );
1101 movingObj_orgPos.setX (lmo->x() );
1102 movingObj_orgPos.setY (lmo->y() );
1104 movingObj_orgRelPos=lmo->getRelPos();
1106 // If modMode==copy, then we want to "move" the _new_ object around
1107 // then we need the offset from p to the _old_ selection, because of tmp
1108 if (mainWindow->getModMode()==Main::ModModeCopy &&
1109 e->state() & Qt::ControlModifier)
1111 BranchItem *bi=model->getSelectedBranch();
1115 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
1116 //bi->addBranch (model->getSelectedBranch());
1118 model->select(bi->getLastBranch());
1119 model->reposition();
1123 movingObj=model->getSelectedLMO();
1125 // Middle Button Toggle Scroll
1126 // (On Mac OS X this won't work, but we still have
1127 // a button in the toolbar)
1128 if (e->button() == Qt::MidButton )
1129 model->toggleScroll();
1130 // model->updateActions(); FIXME-3 needed?
1131 // FIXME-3 needed? xelection.update();
1133 { // No MapObj found, we are on the scene itself
1134 // Left Button move Pos of sceneView
1135 if (e->button() == Qt::LeftButton )
1137 movingObj=NULL; // move Content not Obj
1138 movingObj_start=e->globalPos();
1139 movingCont_start=QPointF (
1140 horizontalScrollBar()->value(),
1141 verticalScrollBar()->value());
1142 movingVec=QPointF(0,0);
1143 setCursor(HandOpenCursor);
1148 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1150 QPointF p = mapToScene(e->pos());
1151 TreeItem *seli=model->getSelectedItem();
1152 LinkableMapObj* lmosel=NULL;
1153 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1154 lmosel=((MapItem*)seli)->getLMO();
1156 // Move the selected MapObj
1157 if ( lmosel && movingObj)
1160 // reset cursor if we are moving and don't copy
1161 if (mainWindow->getModMode()!=Main::ModModeCopy)
1162 setCursor (Qt::ArrowCursor);
1164 // To avoid jumping of the sceneView, only
1165 // show selection, if not tmp linked
1166 /* FIXME-2 if (!lmosel->hasParObjTmp())
1167 model->emitShowSelection();
1170 // Now move the selection, but add relative position
1171 // (movingObj_start) where selection was chosen with
1172 // mousepointer. (This avoids flickering resp. jumping
1173 // of selection back to absPos)
1175 // Check if we could link
1176 TreeItem *ti=findMapItem (p, seli);
1177 BranchItem *dsti=NULL;
1178 LinkableMapObj* dst=NULL;
1179 if (ti && ti!=seli && ti->isBranchLikeType())
1181 dsti=(BranchItem*)ti;
1187 if (lmosel && seli->getType()==TreeItem::Image)
1189 FloatObj *fio=(FloatImageObj*)lmosel;
1190 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1192 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1193 model->emitSelectionChanged(); // position has changed
1195 // Relink float to new mapcenter or branch, if shift is pressed
1196 // Only relink, if selection really has a new parent
1197 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1199 // Also save the move which was done so far
1200 QString pold=qpointFToString(movingObj_orgRelPos);
1201 QString pnow=qpointFToString(fio->getRelPos());
1207 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1208 fio->getParObj()->requestReposition();
1209 model->reposition();
1211 model->relinkImage ((ImageItem*) seli,dsti);
1212 model->select (seli);
1213 //movingObj=lmosel; //FIXME-3
1214 //movingObj_orgRelPos=lmosel->getRelPos();
1216 model->reposition();
1219 { // selection != a FloatObj
1220 if (seli->depth()==0)
1222 lmosel->move (p-movingObj_start);
1223 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1226 v=lmosel->getAbsPos();
1227 for (int i=0; i<seli->branchCount(); ++i)
1229 seli->getBranchObjNum(i)->setRelPos();
1230 seli->getBranchObjNum(i)->setOrientation();
1235 if (seli->depth()==1)
1238 lmosel->move(p-movingObj_start);
1239 lmosel->setRelPos();
1242 // Move ordinary branch
1243 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1244 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1245 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1246 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1248 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1249 lmosel->setRelPos();
1253 // Maybe we can relink temporary?
1254 if (dsti) // FIXME-1 check if dsti is ancestor of myself!
1256 if (e->modifiers()==Qt::ControlModifier)
1258 // Special case: CTRL to link below dst
1259 lmosel->setParObjTmp (dst,p,+1);
1261 else if (e->modifiers()==Qt::ShiftModifier)
1262 lmosel->setParObjTmp (dst,p,-1);
1264 lmosel->setParObjTmp (dst,p,0);
1267 lmosel->unsetParObjTmp();
1269 // reposition subbranch
1270 lmosel->reposition();
1272 QItemSelection sel=model->getSelectionModel()->selection();
1273 updateSelection(sel,sel); // position has changed
1275 } // no FloatImageObj
1279 } // selection && moving_obj
1281 // Draw a link from one branch to another
1284 tmpXLink->setEnd (p);
1285 tmpXLink->updateXLink();
1289 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1291 QPointF p=e->globalPos();
1292 movingVec.setX(-p.x() + movingObj_start.x() );
1293 movingVec.setY(-p.y() + movingObj_start.y() );
1294 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1295 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1300 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1302 QPointF p = mapToScene(e->pos());
1303 TreeItem *seli=model->getSelectedItem();
1305 TreeItem *dsti=NULL;
1306 if (seli) dsti=findMapItem(p, seli);
1307 LinkableMapObj* dst=NULL;
1308 if (dsti && dsti->isBranchLikeType ())
1309 dst=((MapItem*)dsti)->getLMO();
1314 // Have we been picking color?
1318 setCursor (Qt::ArrowCursor);
1319 // Check if we are over another branch
1322 if (e->state() & Qt::ShiftModifier)
1323 model->colorBranch (((BranchObj*)dst)->getColor());
1325 model->colorSubtree (((BranchObj*)dst)->getColor());
1330 // Have we been drawing a link?
1334 // Check if we are over another branch
1337 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1338 tmpXLink->updateXLink();
1339 tmpXLink->activate();
1341 tmpXLink,QString("delete ()"),
1342 dsti,QString("addXLink (\"%1\",\"%2\")").arg(model->getSelectString(tmpXLink->getBegin())).arg(model->getSelectString(dsti)),
1343 QString("Add xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(dsti))
1347 model->deleteItem(tmpXLink);
1353 // Have we been moving something?
1354 if ( seli && movingObj )
1356 if (seli->getType()==TreeItem::Image)
1358 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1361 // Moved FloatObj. Maybe we need to reposition
1362 QString pold=qpointFToString(movingObj_orgRelPos);
1363 QString pnow=qpointFToString(fio->getRelPos());
1369 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1371 cout << "ME::release mouse\n";
1372 fio->getParObj()->requestReposition();
1373 model->reposition();
1377 BranchItem *bi=model->getSelectedBranch();
1378 if (bi && bi->depth()==0)
1380 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1382 QString pold=qpointFToString(movingObj_orgPos);
1383 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1390 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1394 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1395 { // A branch was moved
1396 LinkableMapObj* lmosel=NULL;
1397 lmosel=((MapItem*)seli)->getLMO();
1399 // save the position in case we link to mapcenter
1400 QPointF savePos=QPointF (lmosel->getAbsPos() );
1402 // Reset the temporary drawn link to the original one
1403 lmosel->unsetParObjTmp();
1405 // For Redo we may need to save original selection
1406 QString preSelStr=model->getSelectString(seli);
1409 if (dsti && objectMoved)
1411 // We have a destination, relink to that
1413 BranchObj* bsel=model->getSelectedBranchObj();
1415 QString preParStr=model->getSelectString (bsel->getParObj());
1416 QString preNum=QString::number (seli->num(),10);
1417 QString preDstParStr;
1420 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1422 preDstParStr=model->getSelectString (dst->getParObj());
1423 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1425 if (e->state() & Qt::ControlModifier && dst->getParObj())
1428 preDstParStr=model->getSelectString (dst->getParObj());
1429 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1432 preDstParStr=model->getSelectString(dst);
1433 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1434 if (dsti->depth()==0) bsel->move (savePos);
1438 QString postSelStr=model->getSelectString(lmosel);
1439 QString postNum=QString::number (seli->num(),10);
1441 QString undoCom="relinkTo (\""+
1442 preParStr+ "\"," + preNum +"," +
1443 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1445 QString redoCom="relinkTo (\""+
1446 preDstParStr + "\"," + postNum + "," +
1447 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1452 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1457 // No destination, undo temporary move
1459 if (seli->depth()==1)
1461 // The select string might be different _after_ moving around.
1462 // Therefor reposition and then use string of old selection, too
1463 model->reposition();
1465 QPointF rp(lmosel->getRelPos());
1466 if (rp != movingObj_orgRelPos)
1468 QString ps=qpointFToString(rp);
1470 model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1471 preSelStr, "moveRel "+ps,
1472 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1476 // Draw the original link, before selection was moved around
1477 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1479 lmosel->setRelPos(); // calc relPos first for starting point
1481 model->startAnimation(
1483 lmosel->getRelPos(),
1487 model->reposition();
1490 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1491 // Finally resize scene, if needed
1497 // maybe we moved View: set old cursor
1498 setCursor (Qt::ArrowCursor);
1502 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1506 cout << "ME p="<<mapToScene (e->pos())<<" scrollBarPos="<<getScrollBarPos();
1507 cout << " min="<<QPointF(horizontalScrollBar()->minimum(),verticalScrollBar()->minimum());
1512 if (model->isSelectionBlocked() )
1518 if (e->button() == Qt::LeftButton )
1520 QPointF p = mapToScene(e->pos());
1521 TreeItem *ti=findMapItem (p, NULL);
1522 if (ti) { // MapObj was found
1523 // First select the MapObj than edit heading
1530 void MapEditor::resizeEvent (QResizeEvent* e)
1532 QGraphicsView::resizeEvent( e );
1535 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1537 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1538 // cerr << event->format(i) << endl;
1540 if (event->mimeData()->hasImage())
1541 event->acceptProposedAction();
1543 if (event->mimeData()->hasUrls())
1544 event->acceptProposedAction();
1547 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1551 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1556 void MapEditor::dropEvent(QDropEvent *event)
1558 BranchItem *selbi=model->getSelectedBranch();
1562 foreach (QString format,event->mimeData()->formats())
1563 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1567 if (event->mimeData()->hasImage())
1569 QVariant imageData = event->mimeData()->imageData();
1570 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1572 if (event->mimeData()->hasUrls())
1573 uris=event->mimeData()->urls();
1581 for (int i=0; i<uris.count();i++)
1583 // Workaround to avoid adding empty branches
1584 if (!uris.at(i).toString().isEmpty())
1586 bi=model->addNewBranch();
1590 s=uris.at(i).toLocalFile();
1593 QString file = QDir::fromNativeSeparators(s);
1594 heading = QFileInfo(file).baseName();
1596 if (file.endsWith(".vym", false))
1597 bi->setVymLink(file);
1599 bi->setURL(uris.at(i).toString());
1602 bo->setURL(uris.at(i).toString());
1606 if (!heading.isEmpty())
1607 bi->setHeading(heading);
1609 bi->setHeading(uris.at(i).toString());
1614 model->reposition();
1617 event->acceptProposedAction();
1620 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1622 // Note: Here we are prepared for multiple selections, though this
1623 // is not yet implemented elsewhere
1625 // Here in MapEditor we can only select Branches and Images
1626 QList <TreeItem*> treeItemsNew;
1627 QList <TreeItem*> treeItemsOld;
1629 bool do_reposition=false;
1632 foreach (ix,newsel.indexes() )
1634 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1635 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1636 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1638 foreach (ix,oldsel.indexes() )
1640 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1641 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1642 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1645 // Trim list of selection rectangles
1646 while (treeItemsNew.count() < selboxList.count() )
1647 delete selboxList.takeFirst();
1649 // Take care to tmp scroll/unscroll
1650 if (!oldsel.isEmpty())
1652 QModelIndex ix=oldsel.indexes().first();
1655 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1658 if (ti->isBranchLikeType() )
1660 // reset tmp scrolled branches
1661 BranchItem *bi=(BranchItem*)ti;
1662 if (bi->resetTmpUnscroll() )
1665 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1666 // Hide link if not needed
1667 ((MapItem*)ti)->getLMO()->updateVisibility();
1672 if (!treeItemsNew.isEmpty())
1674 QModelIndex ix=newsel.indexes().first();
1677 // Temporary unscroll if necessary
1678 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1679 if (ti->isBranchLikeType() )
1681 BranchItem *bi=(BranchItem*)ti;
1682 if (bi->hasScrolledParent(bi) )
1684 if (bi->parentBranch()->tmpUnscroll() )
1688 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1689 // Show link if needed
1690 ((MapItem*)ti)->getLMO()->updateVisibility();
1693 // FIXME-3 cout << "ME::updateSel doRepos="<<do_reposition<<endl;
1694 if (do_reposition) model->reposition();
1696 // Reduce rectangles
1697 while (treeItemsNew.count() < selboxList.count() )
1698 delete selboxList.takeFirst();
1700 // Add additonal rectangles
1701 QGraphicsRectItem *sb;
1702 while (treeItemsNew.count() > selboxList.count() )
1704 sb = mapScene->addRect(
1706 QPen(selectionColor),
1708 sb->setZValue(Z_SELBOX);
1710 selboxList.append (sb);
1713 // Reposition rectangles
1717 LinkableMapObj *lmo;
1718 for (int i=0; i<treeItemsNew.count();++i)
1720 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1721 bbox=lmo->getBBox();
1722 sb=selboxList.at(i);
1725 bbox.width(), bbox.height());
1726 sb->setPen (selectionColor);
1727 sb->setBrush (selectionColor);
1734 void MapEditor::updateData (const QModelIndex &sel)
1736 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1739 cout << "ME::updateData\n";
1741 cout << " ti="<<ti<<endl;
1742 cout << " h="<<ti->getHeading().toStdString()<<endl;
1745 if (ti->isBranchLikeType())
1747 // cout << " ->updating...\n";
1748 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1753 void MapEditor::setSelectionColor (QColor col)
1756 QItemSelection sel=model->getSelectionModel()->selection();
1757 updateSelection(sel,sel);
1761 QColor MapEditor::getSelectionColor ()
1763 return selectionColor;