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;
566 QList <Vector> vectors;
567 QList <Vector> orgpos;
568 QStringList headings; //FIXME-3 testing only
573 BranchItem *ri=model->getRootItem();
574 for (int i=0;i<ri->branchCount();++i)
576 bi=ri->getBranchNum (i);
577 bo=(BranchObj*)bi->getLMO();
580 mapobjects.append (bo);
581 p=bo->getBoundingPolygon();
584 vectors.append (QPointF(0,0));
585 orgpos.append (p.at(0));
586 headings.append (bi->getHeading());
588 for (int j=0;j<bi->branchCount();++j)
590 bi2=bi->getBranchNum (j);
591 bo=(BranchObj*)bi2->getLMO();
594 mapobjects.append (bo);
595 p=bo->getBoundingPolygon();
598 vectors.append (QPointF(0,0));
599 orgpos.append (p.at(0));
600 headings.append (bi2->getHeading());
605 // Iterate moving bounding polygons until we have no more collisions
610 for (int i=0; i<polys.size()-1; ++i)
612 for (int j=i+1; j<polys.size();++j)
614 if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
617 v=polys.at(j).centroid()-polys.at(i).centroid();
618 // Move also away if centroids are identical
621 //cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<" "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<" ";
622 v.setX (rand()%200 -100);
623 v.setY (rand()%200 -100);
628 //cout << " "<<v<<endl;
635 for (int i=0;i<vectors.size();i++)
637 //cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
638 polys[i].translate (vectors[i]);
640 //cout << "Collisions: "<<collisions<<endl;
644 // Finally move the real objects and update
645 for (int i=0;i<polys.size();i++)
647 Vector v=polys[i].at(0)-orgpos[i];
648 mapobjects[i]->moveBy(v.x(),v.y() );
653 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
658 if (i>0) return bi->parent()->getBranchNum(i-1);
663 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
667 int dz=selbi->depth(); // original depth
669 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
674 // Look for branch with same parent but directly above
676 bi=getBranchDirectBelow(selbi);
678 bi=getBranchDirectAbove (selbi);
681 // direct predecessor
684 // Go towards center and look for predecessor
685 while (selbi->depth()>0)
687 selbi=(BranchItem*)(selbi->parent());
688 if (selbi->depth()==1 && invert)
689 bi=getBranchDirectBelow (selbi);
691 bi=getBranchDirectAbove (selbi);
696 while (selbi->depth()<dz)
698 // try to get back to original depth dz
699 bi=selbi->getLastBranch();
713 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
718 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
723 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
728 int dz=selbi->depth(); // original depth
730 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
734 // Look for branch with same parent but directly below
736 bi=getBranchDirectAbove (selbi);
738 bi=getBranchDirectBelow (selbi);
744 // Go towards center and look for neighbour
745 while (selbi->depth()>0)
747 selbi=(BranchItem*)(selbi->parent());
748 if (selbi->depth()==1 && invert)
749 bi=getBranchDirectAbove (selbi);
751 bi=getBranchDirectBelow (selbi);
756 while (selbi->depth()<dz)
758 // try to get back to original depth dz
759 bi=selbi->getFirstBranch();
773 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
778 // Special case: use alternative selection index
779 return bi->getLastSelectedBranchAlt();
780 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
782 return (BranchItem*)(bi->parent());
785 if (bi->getType()== TreeItem::Branch )
786 return bi->getLastSelectedBranch();
791 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
795 if (bi->depth()==0) return bi->getLastSelectedBranch();
796 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
798 return (BranchItem*)(bi->parent());
801 if (bi->getType()== TreeItem::Branch )
802 return (BranchItem*)bi->getLastSelectedBranch();
809 void MapEditor::cursorUp()
811 BranchItem *bi=model->getSelectedBranch();
812 if (bi) model->select (getBranchAbove(bi));
815 void MapEditor::cursorDown()
818 BranchItem *bi=model->getSelectedBranch();
819 if (bi) model->select (getBranchBelow(bi));
822 void MapEditor::cursorLeft()
824 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
825 if (bi) model->select (bi);
828 void MapEditor::cursorRight()
830 BranchItem *bi=getRightBranch (model->getSelectedBranch());
831 if (bi) model->select (bi);
834 void MapEditor::cursorFirst()
836 model->selectFirstBranch();
839 void MapEditor::cursorLast()
841 model->selectLastBranch();
845 void MapEditor::editHeading()
849 editHeadingFinished();
852 BranchObj *bo=model->getSelectedBranchObj();
853 BranchItem *bi=model->getSelectedBranch();
856 model->setSelectionBlocked(true);
858 lineEdit->setText (bi->getHeading());
859 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
860 lineEdit->setGeometry(p.x(),p.y(),230,25);
861 lineEdit->selectAll();
863 lineEdit->setFocus();
864 lineEdit->grabKeyboard();
869 void MapEditor::editHeadingFinished()
871 editingHeading=false;
872 lineEdit->releaseKeyboard();
873 model->setHeading (lineEdit->text() );
874 model->setSelectionBlocked(false);
877 // Maybe reselect previous branch
878 mainWindow->editHeadingFinished (model);
882 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
884 // Lineedits are already closed by preceding
885 // mouseEvent, we don't need to close here.
887 QPointF p = mapToScene(e->pos());
888 TreeItem *ti=findMapItem (p, NULL);
889 LinkableMapObj* lmo=NULL;
890 if (ti) lmo=((MapItem*)ti)->getLMO();
893 { // MapObj was found
894 if (model->getSelectedLMO() != lmo)
900 if (model->getSelectedBranchObj() )
902 // Context Menu on branch or mapcenter
903 branchContextMenu->popup(e->globalPos() );
906 if (model->getSelectedImage() )
908 // Context Menu on floatimage
909 floatimageContextMenu->popup(e->globalPos() );
913 { // No MapObj found, we are on the Canvas itself
914 // Context Menu on scene
916 // Open context menu synchronously to position new mapcenter
917 model->setContextPos (p);
918 canvasContextMenu->exec(e->globalPos() );
919 model->unsetContextPos ();
924 void MapEditor::keyPressEvent(QKeyEvent* e)
926 if (e->modifiers() & Qt::ControlModifier)
928 switch (mainWindow->getModMode())
930 case Main::ModModeColor:
931 setCursor (PickColorCursor);
933 case Main::ModModeCopy:
934 setCursor (CopyCursor);
936 case Main::ModModeXLink:
937 setCursor (XLinkCursor);
940 setCursor (Qt::ArrowCursor);
946 void MapEditor::keyReleaseEvent(QKeyEvent* e)
948 if (!(e->modifiers() & Qt::ControlModifier))
949 setCursor (Qt::ArrowCursor);
952 void MapEditor::mousePressEvent(QMouseEvent* e)
954 // Ignore right clicks, these will go to context menus
955 if (e->button() == Qt::RightButton )
961 //Ignore clicks while editing heading
962 if (model->isSelectionBlocked() )
968 QPointF p = mapToScene(e->pos());
969 TreeItem *ti=findMapItem (p, NULL);
970 LinkableMapObj* lmo=NULL;
971 if (ti) lmo=((MapItem*)ti)->getLMO();
976 //Take care of system flags _or_ modifier modes
978 if (lmo && ti->isBranchLikeType() )
980 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
981 if (!foname.isEmpty())
983 // systemFlag clicked
984 model->select (lmo); // FIXME-3 was selectInt
987 if (e->state() & Qt::ControlModifier)
988 mainWindow->editOpenURLTab();
990 mainWindow->editOpenURL();
992 else if (foname=="system-vymLink")
994 mainWindow->editOpenVymLink();
995 // tabWidget may change, better return now
996 // before segfaulting...
997 } else if (foname=="note")
998 mainWindow->windowToggleNoteEditor();
999 else if (foname=="hideInExport")
1000 model->toggleHideExport();
1001 // FIXME-3 needed? xelection.update();
1005 // No system flag clicked, take care of modmodes (CTRL-Click)
1006 if (e->state() & Qt::ControlModifier)
1008 if (mainWindow->getModMode()==Main::ModModeColor)
1011 setCursor (PickColorCursor);
1014 if (mainWindow->getModMode()==Main::ModModeXLink)
1016 BranchItem *bi_begin=model->getSelectedBranch();
1020 tmpXLink=model->createXLink(bi_begin,true);
1021 tmpXLink->setColor(model->getMapDefXLinkColor());
1022 tmpXLink->setWidth(model->getMapDefXLinkWidth());
1023 tmpXLink->setEnd (p);
1024 tmpXLink->updateXLink();
1028 } // End of modmodes
1033 cout << "ME::mouse pressed\n";
1034 cout << " lmo="<<lmo<<endl;
1035 cout << " ti="<<ti->getHeadingStd()<<endl;
1037 // Select the clicked object
1042 // Left Button Move Branches
1043 if (e->button() == Qt::LeftButton )
1045 movingObj_start.setX( p.x() - lmo->x() );
1046 movingObj_start.setY( p.y() - lmo->y() );
1047 movingObj_orgPos.setX (lmo->x() );
1048 movingObj_orgPos.setY (lmo->y() );
1050 movingObj_orgRelPos=lmo->getRelPos();
1052 // If modMode==copy, then we want to "move" the _new_ object around
1053 // then we need the offset from p to the _old_ selection, because of tmp
1054 if (mainWindow->getModMode()==Main::ModModeCopy &&
1055 e->state() & Qt::ControlModifier)
1057 BranchItem *bi=model->getSelectedBranch();
1061 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
1062 //bi->addBranch (model->getSelectedBranch());
1064 model->select(bi->getLastBranch());
1065 model->reposition();
1069 movingObj=model->getSelectedLMO();
1071 // Middle Button Toggle Scroll
1072 // (On Mac OS X this won't work, but we still have
1073 // a button in the toolbar)
1074 if (e->button() == Qt::MidButton )
1075 model->toggleScroll();
1076 // model->updateActions(); FIXME-3 needed?
1077 // FIXME-3 needed? xelection.update();
1079 { // No MapObj found, we are on the scene itself
1080 // Left Button move Pos of sceneView
1081 if (e->button() == Qt::LeftButton )
1083 movingObj=NULL; // move Content not Obj
1084 movingObj_start=e->globalPos();
1085 movingCont_start=QPointF (
1086 horizontalScrollBar()->value(),
1087 verticalScrollBar()->value());
1088 movingVec=QPointF(0,0);
1089 setCursor(HandOpenCursor);
1094 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1096 QPointF p = mapToScene(e->pos());
1097 TreeItem *seli=model->getSelectedItem();
1098 LinkableMapObj* lmosel=NULL;
1099 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1100 lmosel=((MapItem*)seli)->getLMO();
1102 // Move the selected MapObj
1103 if ( lmosel && movingObj)
1105 // reset cursor if we are moving and don't copy
1106 if (mainWindow->getModMode()!=Main::ModModeCopy)
1107 setCursor (Qt::ArrowCursor);
1109 // To avoid jumping of the sceneView, only
1110 // show selection, if not tmp linked
1111 if (!lmosel->hasParObjTmp())
1112 model->emitShowSelection();
1114 // Now move the selection, but add relative position
1115 // (movingObj_start) where selection was chosen with
1116 // mousepointer. (This avoids flickering resp. jumping
1117 // of selection back to absPos)
1119 // Check if we could link
1120 TreeItem *ti=findMapItem (p, seli);
1121 BranchItem *dsti=NULL;
1122 LinkableMapObj* dst=NULL;
1123 if (ti && ti!=seli && ti->isBranchLikeType())
1125 dsti=(BranchItem*)ti;
1131 if (lmosel && seli->getType()==TreeItem::Image)
1133 FloatObj *fio=(FloatImageObj*)lmosel;
1134 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1136 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1137 model->emitSelectionChanged(); // position has changed
1139 // Relink float to new mapcenter or branch, if shift is pressed
1140 // Only relink, if selection really has a new parent
1141 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1143 // Also save the move which was done so far
1144 QString pold=qpointFToString(movingObj_orgRelPos);
1145 QString pnow=qpointFToString(fio->getRelPos());
1151 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1152 fio->getParObj()->requestReposition();
1153 model->reposition();
1155 model->relinkImage ((ImageItem*) seli,dsti);
1156 model->select (seli);
1157 //movingObj=lmosel; //FIXME-3
1158 //movingObj_orgRelPos=lmosel->getRelPos();
1160 model->reposition();
1163 { // selection != a FloatObj
1164 if (seli->depth()==0) //FIXME-1 also moved mapcenters could be linked, but not working here...
1167 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1168 ((BranchObj*)lmosel)->moveBy(
1169 QPointF(p.x() -movingObj_start.x(),
1170 p.y()-movingObj_start.y()) );
1172 lmosel->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1173 model->updateRelPositions();
1176 if (seli->depth()==1)
1179 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1180 lmosel->setRelPos();
1183 // Move ordinary branch
1184 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1185 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1186 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1187 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1189 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1190 lmosel->setRelPos();
1193 // Maybe we can relink temporary?
1196 if (e->modifiers()==Qt::ControlModifier)
1198 // Special case: CTRL to link below dst
1199 lmosel->setParObjTmp (dst,p,+1);
1201 else if (e->modifiers()==Qt::ShiftModifier)
1202 lmosel->setParObjTmp (dst,p,-1);
1204 lmosel->setParObjTmp (dst,p,0);
1207 lmosel->unsetParObjTmp();
1209 // reposition subbranch
1210 lmosel->reposition();
1213 QItemSelection sel=model->getSelectionModel()->selection();
1214 updateSelection(sel,sel); // position has changed
1216 } // no FloatImageObj
1220 } // selection && moving_obj
1222 // Draw a link from one branch to another
1225 tmpXLink->setEnd (p);
1226 tmpXLink->updateXLink();
1230 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1232 QPointF p=e->globalPos();
1233 movingVec.setX(-p.x() + movingObj_start.x() );
1234 movingVec.setY(-p.y() + movingObj_start.y() );
1235 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1236 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1241 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1243 QPointF p = mapToScene(e->pos());
1244 TreeItem *seli=model->getSelectedItem();
1246 TreeItem *dsti=NULL;
1247 if (seli) dsti=findMapItem(p, seli);
1248 LinkableMapObj* dst=NULL;
1249 if (dsti && dsti->isBranchLikeType ())
1250 dst=((MapItem*)dsti)->getLMO();
1255 // Have we been picking color?
1259 setCursor (Qt::ArrowCursor);
1260 // Check if we are over another branch
1263 if (e->state() & Qt::ShiftModifier)
1264 model->colorBranch (((BranchObj*)dst)->getColor());
1266 model->colorSubtree (((BranchObj*)dst)->getColor());
1271 // Have we been drawing a link?
1275 // Check if we are over another branch
1278 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1279 tmpXLink->updateXLink();
1280 tmpXLink->activate();
1281 //FIXME-0 model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );
1284 model->deleteItem(tmpXLink);
1290 // Have we been moving something?
1291 if ( seli && movingObj )
1293 if (seli->getType()==TreeItem::Image)
1295 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1298 // Moved FloatObj. Maybe we need to reposition
1299 QString pold=qpointFToString(movingObj_orgRelPos);
1300 QString pnow=qpointFToString(fio->getRelPos());
1306 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1308 cout << "ME::release mouse\n";
1309 fio->getParObj()->requestReposition();
1310 model->reposition();
1314 BranchItem *bi=model->getSelectedBranch();
1315 if (bi && bi->depth()==0)
1317 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1319 QString pold=qpointFToString(movingObj_orgPos);
1320 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1327 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1331 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1332 { // A branch was moved
1333 LinkableMapObj* lmosel=NULL;
1334 lmosel=((MapItem*)seli)->getLMO();
1336 // save the position in case we link to mapcenter
1337 QPointF savePos=QPointF (lmosel->getAbsPos() );
1339 // Reset the temporary drawn link to the original one
1340 lmosel->unsetParObjTmp();
1342 // For Redo we may need to save original selection
1343 QString preSelStr=model->getSelectString(seli);
1348 // We have a destination, relink to that
1350 BranchObj* bsel=model->getSelectedBranchObj();
1352 QString preParStr=model->getSelectString (bsel->getParObj());
1353 QString preNum=QString::number (seli->num(),10);
1354 QString preDstParStr;
1357 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1359 preDstParStr=model->getSelectString (dst->getParObj());
1360 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1362 if (e->state() & Qt::ControlModifier && dst->getParObj())
1365 preDstParStr=model->getSelectString (dst->getParObj());
1366 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1369 preDstParStr=model->getSelectString(dst);
1370 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1371 if (dsti->depth()==0) bsel->move (savePos);
1375 QString postSelStr=model->getSelectString(lmosel);
1376 QString postNum=QString::number (seli->num(),10);
1378 QString undoCom="relinkTo (\""+
1379 preParStr+ "\"," + preNum +"," +
1380 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1382 QString redoCom="relinkTo (\""+
1383 preDstParStr + "\"," + postNum + "," +
1384 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1389 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1394 // No destination, undo temporary move
1396 if (seli->depth()==1)
1398 // The select string might be different _after_ moving around.
1399 // Therefor reposition and then use string of old selection, too
1400 model->reposition();
1402 QPointF rp(lmosel->getRelPos());
1403 if (rp != movingObj_orgRelPos)
1405 QString ps=qpointFToString(rp);
1407 model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1408 preSelStr, "moveRel "+ps,
1409 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1413 // Draw the original link, before selection was moved around
1414 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1416 lmosel->setRelPos(); // calc relPos first for starting point
1417 QPointF dst=bi->getBranchObj()->getParObj()->getChildPos(); // FIXME-3 check getBO here...
1418 // if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
1420 model->startAnimation(
1422 lmosel->getRelPos(),
1424 // QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
1427 model->reposition();
1430 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1431 // Finally resize scene, if needed
1435 // Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
1436 // model->updateActions(); FIXME-3 neeeded?
1438 // maybe we moved View: set old cursor
1439 setCursor (Qt::ArrowCursor);
1443 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1445 if (model->isSelectionBlocked() )
1451 if (e->button() == Qt::LeftButton )
1453 QPointF p = mapToScene(e->pos());
1454 TreeItem *ti=findMapItem (p, NULL);
1455 if (ti) { // MapObj was found
1456 // First select the MapObj than edit heading
1463 void MapEditor::resizeEvent (QResizeEvent* e)
1465 QGraphicsView::resizeEvent( e );
1468 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1470 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1471 // cerr << event->format(i) << endl;
1473 if (event->mimeData()->hasImage())
1474 event->acceptProposedAction();
1476 if (event->mimeData()->hasUrls())
1477 event->acceptProposedAction();
1480 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1484 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1489 void MapEditor::dropEvent(QDropEvent *event)
1491 BranchItem *selbi=model->getSelectedBranch();
1495 foreach (QString format,event->mimeData()->formats())
1496 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1500 if (event->mimeData()->hasImage())
1502 QVariant imageData = event->mimeData()->imageData();
1503 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1505 if (event->mimeData()->hasUrls())
1506 uris=event->mimeData()->urls();
1514 for (int i=0; i<uris.count();i++)
1516 // Workaround to avoid adding empty branches
1517 if (!uris.at(i).toString().isEmpty())
1519 bi=model->addNewBranch();
1523 s=uris.at(i).toLocalFile();
1526 QString file = QDir::fromNativeSeparators(s);
1527 heading = QFileInfo(file).baseName();
1529 if (file.endsWith(".vym", false))
1530 bi->setVymLink(file);
1532 bi->setURL(uris.at(i).toString());
1535 bo->setURL(uris.at(i).toString());
1539 if (!heading.isEmpty())
1540 bi->setHeading(heading);
1542 bi->setHeading(uris.at(i).toString());
1547 model->reposition();
1550 event->acceptProposedAction();
1553 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1555 // Note: Here we are prepared for multiple selections, though this
1556 // is not yet implemented elsewhere
1558 // Here in MapEditor we can only select Branches and Images
1559 QList <TreeItem*> treeItemsNew;
1560 QList <TreeItem*> treeItemsOld;
1563 foreach (ix,newsel.indexes() )
1565 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1566 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1567 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1569 foreach (ix,oldsel.indexes() )
1571 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1572 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1573 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1576 // Trim list of selection rectangles
1577 while (treeItemsNew.count() < selboxList.count() )
1578 delete selboxList.takeFirst();
1580 // Take care to tmp scroll/unscroll
1581 if (!oldsel.isEmpty())
1583 QModelIndex ix=oldsel.indexes().first();
1586 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1589 if (ti->isBranchLikeType() )
1591 // reset tmp scrolled branches
1592 BranchItem *bi=(BranchItem*)ti;
1593 bi->resetTmpUnscroll();
1595 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1596 // Hide link if not needed
1597 ((MapItem*)ti)->getLMO()->updateVisibility();
1602 if (!treeItemsNew.isEmpty())
1604 QModelIndex ix=newsel.indexes().first();
1607 // Temporary unscroll if necessary
1608 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1609 if (ti->isBranchLikeType() )
1611 BranchItem *bi=(BranchItem*)ti;
1612 if (bi->hasScrolledParent(bi) )
1616 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1617 // Show link if needed
1618 ((MapItem*)ti)->getLMO()->updateVisibility();
1622 // Reduce rectangles
1623 while (treeItemsNew.count() < selboxList.count() )
1624 delete selboxList.takeFirst();
1626 // Add additonal rectangles
1627 QGraphicsRectItem *sb;
1628 while (treeItemsNew.count() > selboxList.count() )
1630 sb = mapScene->addRect(
1632 QPen(selectionColor),
1634 sb->setZValue(Z_SELBOX);
1636 selboxList.append (sb);
1641 // Reposition rectangles
1645 LinkableMapObj *lmo;
1646 for (int i=0; i<treeItemsNew.count();++i)
1648 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1649 bbox=lmo->getBBox();
1650 sb=selboxList.at(i);
1653 bbox.width(), bbox.height());
1654 sb->setPen (selectionColor);
1655 sb->setBrush (selectionColor);
1662 void MapEditor::updateData (const QModelIndex &sel)
1664 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1667 cout << "ME::updateData\n";
1669 cout << " ti="<<ti<<endl;
1670 cout << " h="<<ti->getHeading().toStdString()<<endl;
1673 if (ti->isBranchLikeType())
1675 // cout << " ->updating...\n";
1676 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1681 void MapEditor::setSelectionColor (QColor col)
1684 QItemSelection sel=model->getSelectionModel()->selection();
1685 updateSelection(sel,sel);
1689 QColor MapEditor::getSelectionColor ()
1691 return selectionColor;