9 #include "branchitem.h"
11 #include "mainwindow.h"
13 #include "warningdialog.h"
14 #include "xlinkitem.h"
17 extern int statusbarTime;
18 extern Main *mainWindow;
19 extern QString tmpVymDir;
20 extern QString clipboardDir;
21 extern QString clipboardFile;
22 extern bool clipboardEmpty;
25 extern QMenu* branchContextMenu;
26 extern QMenu* branchAddContextMenu;
27 extern QMenu* branchRemoveContextMenu;
28 extern QMenu* branchLinksContextMenu;
29 extern QMenu* branchXLinksContextMenuEdit;
30 extern QMenu* branchXLinksContextMenuFollow;
31 extern QMenu* floatimageContextMenu;
32 extern QMenu* canvasContextMenu;
34 extern Settings settings;
35 extern QString iconPath;
37 ///////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////
39 MapEditor::MapEditor( VymModel *vm)
41 //cout << "Constructor ME "<<this<<endl;
42 mapScene= new QGraphicsScene(NULL);
43 mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
45 zoomFactor=zoomFactorTarget=1;
48 model->setScene (mapScene);
49 model->registerEditor(this);
50 model->makeDefault(); // No changes in model so far
56 // Create bitmap cursors, platform dependant
57 HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);
58 PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 );
59 CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 );
60 XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 );
62 //setFocusPolicy (Qt::StrongFocus); //FIXME-3
74 setAcceptDrops (true);
76 //model->reposition(); //FIXME-3 really still needed?
79 // Shortcuts and actions
81 a = new QAction("Select upper branch", this);
82 a->setShortcut (Qt::Key_Up );
83 a->setShortcutContext (Qt::WidgetShortcut);
85 connect( a, SIGNAL( triggered() ), this, SLOT( cursorUp() ) );
87 a = new QAction( "Select lower branch",this);
88 a->setShortcut ( Qt::Key_Down );
89 a->setShortcutContext (Qt::WidgetShortcut);
91 connect( a, SIGNAL( triggered() ), this, SLOT( cursorDown() ) );
93 a = new QAction( "Select left branch", this);
94 a->setShortcut (Qt::Key_Left );
95 // a->setShortcutContext (Qt::WindowShortcut);
96 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
98 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLeft() ) );
100 a = new QAction( "Select child branch", this);
101 a->setShortcut (Qt::Key_Right);
102 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
104 connect( a, SIGNAL( triggered() ), this, SLOT( cursorRight() ) );
106 a = new QAction( "Select first branch", this);
107 a->setShortcut (Qt::Key_Home );
108 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
110 connect( a, SIGNAL( triggered() ), this, SLOT( cursorFirst() ) );
112 a = new QAction( "Select last branch",this);
113 a->setShortcut ( Qt::Key_End );
114 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
116 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLast() ) );
118 // Action to embed LineEdit for heading in Scene
119 editingHeading=false;
120 lineEdit=new QLineEdit;
122 QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
123 pw->setZValue (Z_LINEEDIT);
125 a = new QAction( tr( "Edit heading","MapEditor" ), this);
126 a->setShortcut ( Qt::Key_Return ); //Edit heading
128 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
129 a = new QAction( tr( "Edit heading","MapEditor" ), this);
130 a->setShortcut ( Qt::Key_Enter); //Edit heading
132 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
135 selectionColor =QColor (255,255,0);
138 // Attributes //FIXME-2 testing only...
141 attrTable= new AttributeTable();
143 ad=attrTable->addKey (k,StringList);
147 sl <<"val 1"<<"val 2"<< "val 3";
148 ad->setValue (QVariant (sl));
150 //attrTable->addValue ("Key A","P 1");
151 //attrTable->addValue ("Key A","P 2");
152 //attrTable->addValue ("Key A","P 3");
153 //attrTable->addValue ("Key A","P 4");
155 ad=attrTable->addKey (k,FreeString);
158 //attrTable->addValue ("Key B","w1");
159 //attrTable->addValue ("Key B","w2");
161 k="C - UniqueString";
162 ad=attrTable->addKey (k,UniqueString);
165 //attrTable->addKey ("Key Prio");
166 //attrTable->addValue ("Key Prio","Prio 1");
167 //attrTable->addValue ("Key Prio","Prio 2");
171 MapEditor::~MapEditor()
173 //cout <<"Destructor MapEditor for "<<model->getMapName().toStdString()<<endl;
174 model->unregisterEditor(this);
177 VymModel* MapEditor::getModel()
182 QGraphicsScene * MapEditor::getScene()
187 void MapEditor::scrollTo (const QModelIndex &index)
191 LinkableMapObj* lmo=NULL;
192 TreeItem *ti= static_cast<TreeItem*>(index.internalPointer());
193 if (ti->getType()==TreeItem::Image ||ti->isBranchLikeType() )
194 lmo=((MapItem*)ti)->getLMO();
195 if (lmo) setScrollBarPosTarget (lmo->getBBox() );
199 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
201 // Code copied from Qt sources
205 qreal width = viewport()->width();
206 qreal height = viewport()->height();
207 QRectF viewRect = matrix().mapRect(rect);
209 qreal left = horizontalScrollBar()->value();
210 qreal right = left + width;
211 qreal top = verticalScrollBar()->value();
212 qreal bottom = top + height;
214 if (viewRect.left() <= left + xmargin) {
215 // need to scroll from the left
216 // if (!d->leftIndent)
217 scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
219 if (viewRect.right() >= right - xmargin) {
220 // need to scroll from the right
221 // if (!d->leftIndent)
222 scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
224 if (viewRect.top() <= top + ymargin) {
225 // need to scroll from the top
226 // if (!d->topIndent)
227 scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
229 if (viewRect.bottom() >= bottom - ymargin) {
230 // need to scroll from the bottom
231 // if (!d->topIndent)
232 scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
235 if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
236 scrollBarPosAnimation.stop();
237 scrollBarPosAnimation.setTargetObject (this);
238 scrollBarPosAnimation.setPropertyName ("scrollBarPos");
239 scrollBarPosAnimation.setDuration(1000);
240 scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
241 scrollBarPosAnimation.setStartValue(
242 QPointF (horizontalScrollBar()->value() ,
243 verticalScrollBar()->value() ) );
244 scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
245 scrollBarPosAnimation.start();
248 QPointF MapEditor::getScrollBarPosTarget()
250 return scrollBarPosTarget;
254 void MapEditor::setScrollBarPos(const QPointF &p)
257 horizontalScrollBar()->setValue(int(p.x()));
258 verticalScrollBar()->setValue(int(p.y()));
261 QPointF MapEditor::getScrollBarPos()
266 void MapEditor::setZoomFactorTarget (const qreal &zft)
268 zoomFactorTarget=zft;
269 if (zoomAnimation.state()==QtAbstractAnimation::Running)
270 zoomAnimation.stop();
271 //zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
272 zoomAnimation.setTargetObject (this);
273 zoomAnimation.setPropertyName ("zoomFactor");
274 zoomAnimation.setDuration(1000);
275 zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
276 zoomAnimation.setStartValue(zoomFactor);
277 zoomAnimation.setEndValue(zft);
278 zoomAnimation.start();
281 qreal MapEditor::getZoomFactorTarget()
283 return zoomFactorTarget;
287 void MapEditor::setZoomFactor(const qreal &zf)
290 setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
293 qreal MapEditor::getZoomFactor()
298 void MapEditor::print()
302 printer = new QPrinter;
303 printer->setColorMode (QPrinter::Color);
304 printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
305 printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
306 printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
309 QRectF totalBBox=getTotalBBox();
311 // Try to set orientation automagically
312 // Note: Interpretation of generated postscript is amibiguous, if
313 // there are problems with landscape mode, see
314 // http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
316 if (totalBBox.width()>totalBBox.height())
317 // recommend landscape
318 printer->setOrientation (QPrinter::Landscape);
320 // recommend portrait
321 printer->setOrientation (QPrinter::Portrait);
323 if ( printer->setup(this) )
324 // returns false, if printing is canceled
326 QPainter pp(printer);
328 pp.setRenderHint(QPainter::Antialiasing,true);
330 // Don't print the visualisation of selection
333 QRectF mapRect=totalBBox;
334 QGraphicsRectItem *frame=NULL;
338 // Print frame around map
339 mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10,
340 totalBBox.width()+20, totalBBox.height()+20);
341 frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
347 double paperAspect = (double)printer->width() / (double)printer->height();
348 double mapAspect = (double)mapRect.width() / (double)mapRect.height();
350 if (mapAspect>=paperAspect)
352 // Fit horizontally to paper width
353 //pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );
354 viewBottom=(int)(printer->width()/mapAspect);
357 // Fit vertically to paper height
358 //pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());
359 viewBottom=printer->height();
364 // Print footer below map
366 font.setPointSize(10);
368 QRectF footerBox(0,viewBottom,printer->width(),15);
369 // FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
370 pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
374 QRectF (0,0,printer->width(),printer->height()-15),
375 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
378 // Viewport has paper dimension
379 if (frame) delete (frame);
384 // Save settings in vymrc
385 settings.writeEntry("/mainwindow/printerName",printer->printerName());
386 settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
387 settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
391 QRectF MapEditor::getTotalBBox() //FIXME-2 needed e.g. for image export
395 for (int i=0;i<rootItem->branchCount(); i++)
396 r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
402 QPixmap MapEditor::getPixmap()
404 QRectF mapRect=getTotalBBox();
405 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
408 pp.setRenderHints(renderHints());
410 // Don't print the visualisation of selection
413 mapScene->render ( &pp,
414 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
415 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
424 void MapEditor::setAntiAlias (bool b)
426 setRenderHint(QPainter::Antialiasing,b);
429 void MapEditor::setSmoothPixmap(bool b)
431 setRenderHint(QPainter::SmoothPixmapTransform,b);
434 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
436 // Start with mapcenter, no images allowed at rootItem
438 BranchItem *bi=model->getRootItem()->getFirstBranch();
439 TreeItem *found=NULL;
442 found=bi->findMapItem (p, exclude);
443 if (found) return found;
445 bi=model->getRootItem()->getBranchNum(i);
450 AttributeTable* MapEditor::attributeTable()
455 void MapEditor::testFunction1()
458 // Code copied from Qt sources
459 QRectF rect=model->getSelectedBranchObj()->getBBox();
463 qreal width = viewport()->width();
464 qreal height = viewport()->height();
465 QRectF viewRect = matrix().mapRect(rect);
467 qreal left = horizontalScrollBar()->value();
468 qreal right = left + width;
469 qreal top = verticalScrollBar()->value();
470 qreal bottom = top + height;
472 if (viewRect.left() <= left + xmargin) {
473 // need to scroll from the left
474 // if (!d->leftIndent)
475 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
477 if (viewRect.right() >= right - xmargin) {
478 // need to scroll from the right
479 // if (!d->leftIndent)
480 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
482 if (viewRect.top() <= top + ymargin) {
483 // need to scroll from the top
484 // if (!d->topIndent)
485 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
487 if (viewRect.bottom() >= bottom - ymargin) {
488 // need to scroll from the bottom
489 // if (!d->topIndent)
490 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
492 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
493 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
498 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
499 animation->setDuration(5000);
500 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
501 animation->setEasingCurve ( QtEasingCurve::OutQuint);
502 animation->setStartValue(sceneRect() );
503 animation->setEndValue(QRectF(50, 50, 1000, 1000));
508 QDialog *dia= new QDialog (this);
509 dia->setGeometry (50,50,10,10);
514 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
515 animation->setDuration(1000);
516 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
517 animation->setEasingCurve ( QtEasingCurve::OutQuint);
518 animation->setStartValue(QRect(50, 50, 10, 10));
519 animation->setEndValue(QRect(250, 250, 100, 100));
524 /* FIXME-4 Hide hidden stuff temporary, maybe add this as regular function somewhere
525 if (hidemode==HideNone)
527 setHideTmpMode (HideExport);
528 mapCenter->calcBBoxSizeWithChilds();
529 QRectF totalBBox=mapCenter->getTotalBBox();
530 QRectF mapRect=totalBBox;
531 QCanvasRectangle *frame=NULL;
533 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
535 mapRect.setRect (totalBBox.x(), totalBBox.y(),
536 totalBBox.width(), totalBBox.height());
537 frame=new QCanvasRectangle (mapRect,mapScene);
538 frame->setBrush (QColor(white));
539 frame->setPen (QColor(black));
545 setHideTmpMode (HideNone);
547 cout <<" hidemode="<<hidemode<<endl;
552 if (hidemode==HideExport)
553 setHideTmpMode (HideNone);
555 setHideTmpMode (HideExport);
560 void MapEditor::testFunction2()
562 // Create list with all bounding polygons
563 QList <MapObj*> mapobjects;
564 QList <ConvexPolygon> polys;
565 QList <Vector> vectors;
566 QList <Vector> orgpos;
569 TreeItem *ri=model->getRootItem();
570 for (int i=0;i<ri->branchCount();++i)
572 bi=ri->getBranchNum (i);
573 bo=(BranchObj*)bi->getLMO();
576 mapobjects.append (bo);
577 polys.append(bo->getBoundingPolygon());
578 polys[i].calcCentroid();
579 vectors.append (QPointF(0,0));
580 orgpos.append (polys[i].at(0));
584 // Iterate moving bounding polygons until we have no more collisions
589 for (int i=0; i<polys.size()-1; ++i)
591 for (int j=i+1; j<polys.size();++j)
593 if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
596 Vector v=polys.at(j).centroid()-polys.at(i).centroid();
597 // Move also away if centroids are identical
600 v.setX (rand()%200 -100);
601 v.setY (rand()%200 -100);
604 cout << "v="<<v<<endl;
612 for (int i=0;i<vectors.size();i++)
613 polys[i].translate (vectors[i]);
614 //cout << "Collisions: "<<collisions<<endl;
617 // Finally move the real objects and update
618 for (int i=0;i<polys.size();i++)
620 Vector v=polys[i].at(0)-orgpos[i];
621 mapobjects[i]->moveBy(v.x(),v.y() );
626 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
631 if (i>0) return bi->parent()->getBranchNum(i-1);
636 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
640 int dz=selbi->depth(); // original depth
642 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
647 // Look for branch with same parent but directly above
649 bi=getBranchDirectBelow(selbi);
651 bi=getBranchDirectAbove (selbi);
654 // direct predecessor
657 // Go towards center and look for predecessor
658 while (selbi->depth()>0)
660 selbi=(BranchItem*)(selbi->parent());
661 if (selbi->depth()==1 && invert)
662 bi=getBranchDirectBelow (selbi);
664 bi=getBranchDirectAbove (selbi);
669 while (selbi->depth()<dz)
671 // try to get back to original depth dz
672 bi=selbi->getLastBranch();
686 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
691 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
696 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
701 int dz=selbi->depth(); // original depth
703 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
707 // Look for branch with same parent but directly below
709 bi=getBranchDirectAbove (selbi);
711 bi=getBranchDirectBelow (selbi);
717 // Go towards center and look for neighbour
718 while (selbi->depth()>0)
720 selbi=(BranchItem*)(selbi->parent());
721 if (selbi->depth()==1 && invert)
722 bi=getBranchDirectAbove (selbi);
724 bi=getBranchDirectBelow (selbi);
729 while (selbi->depth()<dz)
731 // try to get back to original depth dz
732 bi=selbi->getFirstBranch();
746 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
751 // Special case: use alternative selection index
752 return bi->getLastSelectedBranchAlt();
753 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
755 return (BranchItem*)(bi->parent());
758 if (bi->getType()== TreeItem::Branch )
759 return bi->getLastSelectedBranch();
764 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
768 if (bi->depth()==0) return bi->getLastSelectedBranch();
769 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
771 return (BranchItem*)(bi->parent());
774 if (bi->getType()== TreeItem::Branch )
775 return (BranchItem*)bi->getLastSelectedBranch();
782 void MapEditor::cursorUp()
784 BranchItem *bi=model->getSelectedBranch();
785 if (bi) model->select (getBranchAbove(bi));
788 void MapEditor::cursorDown()
791 BranchItem *bi=model->getSelectedBranch();
792 if (bi) model->select (getBranchBelow(bi));
795 void MapEditor::cursorLeft()
797 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
798 if (bi) model->select (bi);
801 void MapEditor::cursorRight()
803 BranchItem *bi=getRightBranch (model->getSelectedBranch());
804 if (bi) model->select (bi);
807 void MapEditor::cursorFirst()
809 model->selectFirstBranch();
812 void MapEditor::cursorLast()
814 model->selectLastBranch();
818 void MapEditor::editHeading()
822 editHeadingFinished();
825 BranchObj *bo=model->getSelectedBranchObj();
826 BranchItem *bi=model->getSelectedBranch();
829 model->setSelectionBlocked(true);
831 lineEdit->setText (bi->getHeading());
832 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
833 lineEdit->setGeometry(p.x(),p.y(),230,25);
834 lineEdit->selectAll();
836 lineEdit->setFocus();
837 lineEdit->grabKeyboard();
842 void MapEditor::editHeadingFinished()
844 editingHeading=false;
845 lineEdit->releaseKeyboard();
846 model->setHeading (lineEdit->text() );
847 model->setSelectionBlocked(false);
850 // Maybe reselect previous branch
851 mainWindow->editHeadingFinished (model);
855 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
857 // Lineedits are already closed by preceding
858 // mouseEvent, we don't need to close here.
860 QPointF p = mapToScene(e->pos());
861 TreeItem *ti=findMapItem (p, NULL);
862 LinkableMapObj* lmo=NULL;
863 if (ti) lmo=((MapItem*)ti)->getLMO();
866 { // MapObj was found
867 if (model->getSelectedLMO() != lmo)
873 if (model->getSelectedBranchObj() )
875 // Context Menu on branch or mapcenter
876 branchContextMenu->popup(e->globalPos() );
879 if (model->getSelectedImage() )
881 // Context Menu on floatimage
882 floatimageContextMenu->popup(e->globalPos() );
886 { // No MapObj found, we are on the Canvas itself
887 // Context Menu on scene
889 // Open context menu synchronously to position new mapcenter
890 model->setContextPos (p);
891 canvasContextMenu->exec(e->globalPos() );
892 model->unsetContextPos ();
897 void MapEditor::keyPressEvent(QKeyEvent* e)
899 if (e->modifiers() & Qt::ControlModifier)
901 switch (mainWindow->getModMode())
903 case Main::ModModeColor:
904 setCursor (PickColorCursor);
906 case Main::ModModeCopy:
907 setCursor (CopyCursor);
909 case Main::ModModeXLink:
910 setCursor (XLinkCursor);
913 setCursor (Qt::ArrowCursor);
919 void MapEditor::keyReleaseEvent(QKeyEvent* e)
921 if (!(e->modifiers() & Qt::ControlModifier))
922 setCursor (Qt::ArrowCursor);
925 void MapEditor::mousePressEvent(QMouseEvent* e)
927 // Ignore right clicks, these will go to context menus
928 if (e->button() == Qt::RightButton )
934 //Ignore clicks while editing heading
935 if (model->isSelectionBlocked() )
941 QPointF p = mapToScene(e->pos());
942 TreeItem *ti=findMapItem (p, NULL);
943 LinkableMapObj* lmo=NULL;
944 if (ti) lmo=((MapItem*)ti)->getLMO();
949 //Take care of system flags _or_ modifier modes
951 if (lmo && ti->isBranchLikeType() )
953 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
954 if (!foname.isEmpty())
956 // systemFlag clicked
957 model->select (lmo); // FIXME-3 was selectInt
960 if (e->state() & Qt::ControlModifier)
961 mainWindow->editOpenURLTab();
963 mainWindow->editOpenURL();
965 else if (foname=="system-vymLink")
967 mainWindow->editOpenVymLink();
968 // tabWidget may change, better return now
969 // before segfaulting...
970 } else if (foname=="note")
971 mainWindow->windowToggleNoteEditor();
972 else if (foname=="hideInExport")
973 model->toggleHideExport();
974 // FIXME-3 needed? xelection.update();
978 // No system flag clicked, take care of modmodes (CTRL-Click)
979 if (e->state() & Qt::ControlModifier)
981 if (mainWindow->getModMode()==Main::ModModeColor)
984 setCursor (PickColorCursor);
987 if (mainWindow->getModMode()==Main::ModModeXLink)
989 BranchItem *bi_begin=model->getSelectedBranch();
993 tmpXLink=model->createXLink(bi_begin,true);
994 tmpXLink->setColor(model->getMapDefXLinkColor());
995 tmpXLink->setWidth(model->getMapDefXLinkWidth());
996 tmpXLink->setEnd (p);
997 tmpXLink->updateXLink();
1001 } // End of modmodes
1006 cout << "ME::mouse pressed\n";
1007 cout << " lmo="<<lmo<<endl;
1008 cout << " ti="<<ti->getHeadingStd()<<endl;
1010 // Select the clicked object
1015 // Left Button Move Branches
1016 if (e->button() == Qt::LeftButton )
1018 movingObj_start.setX( p.x() - lmo->x() );
1019 movingObj_start.setY( p.y() - lmo->y() );
1020 movingObj_orgPos.setX (lmo->x() );
1021 movingObj_orgPos.setY (lmo->y() );
1023 movingObj_orgRelPos=lmo->getRelPos();
1025 // If modMode==copy, then we want to "move" the _new_ object around
1026 // then we need the offset from p to the _old_ selection, because of tmp
1027 if (mainWindow->getModMode()==Main::ModModeCopy &&
1028 e->state() & Qt::ControlModifier)
1030 BranchItem *bi=model->getSelectedBranch();
1034 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
1035 //bi->addBranch (model->getSelectedBranch());
1037 model->select(bi->getLastBranch());
1038 model->reposition();
1042 movingObj=model->getSelectedLMO();
1044 // Middle Button Toggle Scroll
1045 // (On Mac OS X this won't work, but we still have
1046 // a button in the toolbar)
1047 if (e->button() == Qt::MidButton )
1048 model->toggleScroll();
1049 // model->updateActions(); FIXME-3 needed?
1050 // FIXME-3 needed? xelection.update();
1052 { // No MapObj found, we are on the scene itself
1053 // Left Button move Pos of sceneView
1054 if (e->button() == Qt::LeftButton )
1056 movingObj=NULL; // move Content not Obj
1057 movingObj_start=e->globalPos();
1058 movingCont_start=QPointF (
1059 horizontalScrollBar()->value(),
1060 verticalScrollBar()->value());
1061 movingVec=QPointF(0,0);
1062 setCursor(HandOpenCursor);
1067 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1069 QPointF p = mapToScene(e->pos());
1070 TreeItem *seli=model->getSelectedItem();
1071 LinkableMapObj* lmosel=NULL;
1072 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1073 lmosel=((MapItem*)seli)->getLMO();
1075 // Move the selected MapObj
1076 if ( lmosel && movingObj)
1078 // reset cursor if we are moving and don't copy
1079 if (mainWindow->getModMode()!=Main::ModModeCopy)
1080 setCursor (Qt::ArrowCursor);
1082 // To avoid jumping of the sceneView, only
1083 // show selection, if not tmp linked
1084 if (!lmosel->hasParObjTmp())
1085 model->emitShowSelection();
1087 // Now move the selection, but add relative position
1088 // (movingObj_start) where selection was chosen with
1089 // mousepointer. (This avoids flickering resp. jumping
1090 // of selection back to absPos)
1092 // Check if we could link
1093 TreeItem *ti=findMapItem (p, seli);
1094 BranchItem *dsti=NULL;
1095 LinkableMapObj* dst=NULL;
1096 if (ti && ti!=seli && ti->isBranchLikeType())
1098 dsti=(BranchItem*)ti;
1104 if (lmosel && seli->getType()==TreeItem::Image)
1106 FloatObj *fio=(FloatImageObj*)lmosel;
1107 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1109 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1110 model->emitSelectionChanged(); // position has changed
1112 // Relink float to new mapcenter or branch, if shift is pressed
1113 // Only relink, if selection really has a new parent
1114 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1116 // Also save the move which was done so far
1117 QString pold=qpointFToString(movingObj_orgRelPos);
1118 QString pnow=qpointFToString(fio->getRelPos());
1124 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1125 fio->getParObj()->requestReposition();
1126 model->reposition();
1128 model->relinkImage ((ImageItem*) seli,dsti);
1129 model->select (seli);
1130 //movingObj=lmosel; //FIXME-3
1131 //movingObj_orgRelPos=lmosel->getRelPos();
1133 model->reposition();
1136 { // selection != a FloatObj
1137 if (seli->depth()==0) //FIXME-1 also moved mapcenters could be linked, but not working here...
1140 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1141 ((BranchObj*)lmosel)->moveBy(
1142 QPointF(p.x() -movingObj_start.x(),
1143 p.y()-movingObj_start.y()) );
1145 lmosel->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1146 model->updateRelPositions();
1149 if (seli->depth()==1)
1152 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1153 lmosel->setRelPos();
1156 // Move ordinary branch
1157 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1158 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1159 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1160 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1162 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1163 lmosel->setRelPos();
1166 // Maybe we can relink temporary?
1169 if (e->modifiers()==Qt::ControlModifier)
1171 // Special case: CTRL to link below dst
1172 lmosel->setParObjTmp (dst,p,+1);
1174 else if (e->modifiers()==Qt::ShiftModifier)
1175 lmosel->setParObjTmp (dst,p,-1);
1177 lmosel->setParObjTmp (dst,p,0);
1180 lmosel->unsetParObjTmp();
1182 // reposition subbranch
1183 lmosel->reposition();
1186 QItemSelection sel=model->getSelectionModel()->selection();
1187 updateSelection(sel,sel); // position has changed
1189 } // no FloatImageObj
1193 } // selection && moving_obj
1195 // Draw a link from one branch to another
1198 tmpXLink->setEnd (p);
1199 tmpXLink->updateXLink();
1203 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1205 QPointF p=e->globalPos();
1206 movingVec.setX(-p.x() + movingObj_start.x() );
1207 movingVec.setY(-p.y() + movingObj_start.y() );
1208 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1209 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1214 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1216 QPointF p = mapToScene(e->pos());
1217 TreeItem *seli=model->getSelectedItem();
1219 TreeItem *dsti=NULL;
1220 if (seli) dsti=findMapItem(p, seli);
1221 LinkableMapObj* dst=NULL;
1222 if (dsti && dsti->isBranchLikeType ())
1223 dst=((MapItem*)dsti)->getLMO();
1228 // Have we been picking color?
1232 setCursor (Qt::ArrowCursor);
1233 // Check if we are over another branch
1236 if (e->state() & Qt::ShiftModifier)
1237 model->colorBranch (((BranchObj*)dst)->getColor());
1239 model->colorSubtree (((BranchObj*)dst)->getColor());
1244 // Have we been drawing a link?
1248 // Check if we are over another branch
1251 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1252 tmpXLink->updateXLink();
1253 tmpXLink->activate();
1254 //FIXME-0 model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );
1257 model->deleteItem(tmpXLink);
1263 // Have we been moving something?
1264 if ( seli && movingObj )
1266 if (seli->getType()==TreeItem::Image)
1268 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1271 // Moved FloatObj. Maybe we need to reposition
1272 QString pold=qpointFToString(movingObj_orgRelPos);
1273 QString pnow=qpointFToString(fio->getRelPos());
1279 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1281 cout << "ME::release mouse\n";
1282 fio->getParObj()->requestReposition();
1283 model->reposition();
1287 BranchItem *bi=model->getSelectedBranch();
1288 if (bi && bi->depth()==0)
1290 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1292 QString pold=qpointFToString(movingObj_orgPos);
1293 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1300 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1304 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1305 { // A branch was moved
1306 LinkableMapObj* lmosel=NULL;
1307 lmosel=((MapItem*)seli)->getLMO();
1309 // save the position in case we link to mapcenter
1310 QPointF savePos=QPointF (lmosel->getAbsPos() );
1312 // Reset the temporary drawn link to the original one
1313 lmosel->unsetParObjTmp();
1315 // For Redo we may need to save original selection
1316 QString preSelStr=model->getSelectString(seli);
1321 // We have a destination, relink to that
1323 BranchObj* bsel=model->getSelectedBranchObj();
1325 QString preParStr=model->getSelectString (bsel->getParObj());
1326 QString preNum=QString::number (seli->num(),10);
1327 QString preDstParStr;
1330 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1332 preDstParStr=model->getSelectString (dst->getParObj());
1333 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1335 if (e->state() & Qt::ControlModifier && dst->getParObj())
1338 preDstParStr=model->getSelectString (dst->getParObj());
1339 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1342 preDstParStr=model->getSelectString(dst);
1343 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1344 if (dsti->depth()==0) bsel->move (savePos);
1348 QString postSelStr=model->getSelectString(lmosel);
1349 QString postNum=QString::number (seli->num(),10);
1351 QString undoCom="relinkTo (\""+
1352 preParStr+ "\"," + preNum +"," +
1353 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1355 QString redoCom="relinkTo (\""+
1356 preDstParStr + "\"," + postNum + "," +
1357 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1362 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1367 // No destination, undo temporary move
1369 if (seli->depth()==1)
1371 // The select string might be different _after_ moving around.
1372 // Therefor reposition and then use string of old selection, too
1373 model->reposition();
1375 QPointF rp(lmosel->getRelPos());
1376 if (rp != movingObj_orgRelPos)
1378 QString ps=qpointFToString(rp);
1380 model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1381 preSelStr, "moveRel "+ps,
1382 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1386 // Draw the original link, before selection was moved around
1387 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1389 lmosel->setRelPos(); // calc relPos first for starting point
1390 QPointF dst=bi->getBranchObj()->getParObj()->getChildPos(); // FIXME-3 check getBO here...
1391 // if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
1393 model->startAnimation(
1395 lmosel->getRelPos(),
1397 // QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
1400 model->reposition();
1403 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1404 // Finally resize scene, if needed
1408 // Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
1409 // model->updateActions(); FIXME-3 neeeded?
1411 // maybe we moved View: set old cursor
1412 setCursor (Qt::ArrowCursor);
1416 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1418 if (model->isSelectionBlocked() )
1424 if (e->button() == Qt::LeftButton )
1426 QPointF p = mapToScene(e->pos());
1427 TreeItem *ti=findMapItem (p, NULL);
1428 if (ti) { // MapObj was found
1429 // First select the MapObj than edit heading
1436 void MapEditor::resizeEvent (QResizeEvent* e)
1438 QGraphicsView::resizeEvent( e );
1441 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1443 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1444 // cerr << event->format(i) << endl;
1446 if (event->mimeData()->hasImage())
1447 event->acceptProposedAction();
1449 if (event->mimeData()->hasUrls())
1450 event->acceptProposedAction();
1453 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1457 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1462 void MapEditor::dropEvent(QDropEvent *event)
1464 BranchItem *selbi=model->getSelectedBranch();
1468 foreach (QString format,event->mimeData()->formats())
1469 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1473 if (event->mimeData()->hasImage())
1475 QVariant imageData = event->mimeData()->imageData();
1476 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1478 if (event->mimeData()->hasUrls())
1479 uris=event->mimeData()->urls();
1487 for (int i=0; i<uris.count();i++)
1489 // Workaround to avoid adding empty branches
1490 if (!uris.at(i).toString().isEmpty())
1492 bi=model->addNewBranch();
1496 s=uris.at(i).toLocalFile();
1499 QString file = QDir::fromNativeSeparators(s);
1500 heading = QFileInfo(file).baseName();
1502 if (file.endsWith(".vym", false))
1503 bi->setVymLink(file);
1505 bi->setURL(uris.at(i).toString());
1508 bo->setURL(uris.at(i).toString());
1512 if (!heading.isEmpty())
1513 bi->setHeading(heading);
1515 bi->setHeading(uris.at(i).toString());
1520 model->reposition();
1523 event->acceptProposedAction();
1526 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1528 // Note: Here we are prepared for multiple selections, though this
1529 // is not yet implemented elsewhere
1531 // Here in MapEditor we can only select Branches and Images
1532 QList <TreeItem*> treeItemsNew;
1533 QList <TreeItem*> treeItemsOld;
1536 foreach (ix,newsel.indexes() )
1538 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1539 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1540 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1542 foreach (ix,oldsel.indexes() )
1544 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1545 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1546 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1549 // Trim list of selection rectangles
1550 while (treeItemsNew.count() < selboxList.count() )
1551 delete selboxList.takeFirst();
1553 // Take care to tmp scroll/unscroll
1554 if (!oldsel.isEmpty())
1556 QModelIndex ix=oldsel.indexes().first();
1559 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1562 if (ti->isBranchLikeType() )
1564 // reset tmp scrolled branches
1565 BranchItem *bi=(BranchItem*)ti;
1566 bi->resetTmpUnscroll();
1568 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1569 // Hide link if not needed
1570 ((MapItem*)ti)->getLMO()->updateVisibility();
1575 if (!treeItemsNew.isEmpty())
1577 QModelIndex ix=newsel.indexes().first();
1580 // Temporary unscroll if necessary
1581 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1582 if (ti->isBranchLikeType() )
1584 BranchItem *bi=(BranchItem*)ti;
1585 if (bi->hasScrolledParent(bi) )
1589 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1590 // Show link if needed
1591 ((MapItem*)ti)->getLMO()->updateVisibility();
1595 // Reduce rectangles
1596 while (treeItemsNew.count() < selboxList.count() )
1597 delete selboxList.takeFirst();
1599 // Add additonal rectangles
1600 QGraphicsRectItem *sb;
1601 while (treeItemsNew.count() > selboxList.count() )
1603 sb = mapScene->addRect(
1605 QPen(selectionColor),
1607 sb->setZValue(Z_SELBOX);
1609 selboxList.append (sb);
1614 // Reposition rectangles
1618 LinkableMapObj *lmo;
1619 for (int i=0; i<treeItemsNew.count();++i)
1621 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1622 bbox=lmo->getBBox();
1623 sb=selboxList.at(i);
1626 bbox.width(), bbox.height());
1627 sb->setPen (selectionColor);
1628 sb->setBrush (selectionColor);
1635 void MapEditor::updateData (const QModelIndex &sel)
1637 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1640 cout << "ME::updateData\n";
1642 cout << " ti="<<ti<<endl;
1643 cout << " h="<<ti->getHeading().toStdString()<<endl;
1646 if (ti->isBranchLikeType())
1648 // cout << " ->updating...\n";
1649 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1654 void MapEditor::setSelectionColor (QColor col)
1657 QItemSelection sel=model->getSelectionModel()->selection();
1658 updateSelection(sel,sel);
1662 QColor MapEditor::getSelectionColor ()
1664 return selectionColor;