9 #include "branchitem.h"
10 #include "mainwindow.h"
12 #include "warningdialog.h"
13 #include "xlinkitem.h"
16 extern int statusbarTime;
17 extern Main *mainWindow;
18 extern QString tmpVymDir;
19 extern QString clipboardDir;
20 extern QString clipboardFile;
21 extern bool clipboardEmpty;
24 extern QMenu* branchContextMenu;
25 extern QMenu* branchAddContextMenu;
26 extern QMenu* branchRemoveContextMenu;
27 extern QMenu* branchLinksContextMenu;
28 extern QMenu* branchXLinksContextMenuEdit;
29 extern QMenu* branchXLinksContextMenuFollow;
30 extern QMenu* floatimageContextMenu;
31 extern QMenu* canvasContextMenu;
33 extern Settings settings;
34 extern QString iconPath;
36 ///////////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////
38 MapEditor::MapEditor( VymModel *vm)
40 //cout << "Constructor ME "<<this<<endl;
41 mapScene= new QGraphicsScene(NULL);
42 mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
44 zoomFactor=zoomFactorTarget=1;
47 model->setScene (mapScene);
48 model->registerEditor(this);
49 model->makeDefault(); // No changes in model so far
55 // Create bitmap cursors, platform dependant
56 HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);
57 PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 );
58 CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 );
59 XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 );
61 //setFocusPolicy (Qt::StrongFocus); //FIXME-3
73 setAcceptDrops (true);
75 //model->reposition(); //FIXME-3 really still needed?
78 // Shortcuts and actions
80 a = new QAction("Select upper branch", this);
81 a->setShortcut (Qt::Key_Up );
82 a->setShortcutContext (Qt::WidgetShortcut);
84 connect( a, SIGNAL( triggered() ), this, SLOT( cursorUp() ) );
86 a = new QAction( "Select lower branch",this);
87 a->setShortcut ( Qt::Key_Down );
88 a->setShortcutContext (Qt::WidgetShortcut);
90 connect( a, SIGNAL( triggered() ), this, SLOT( cursorDown() ) );
92 a = new QAction( "Select left branch", this);
93 a->setShortcut (Qt::Key_Left );
94 // a->setShortcutContext (Qt::WindowShortcut);
95 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
97 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLeft() ) );
99 a = new QAction( "Select child branch", this);
100 a->setShortcut (Qt::Key_Right);
101 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
103 connect( a, SIGNAL( triggered() ), this, SLOT( cursorRight() ) );
105 a = new QAction( "Select first branch", this);
106 a->setShortcut (Qt::Key_Home );
107 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
109 connect( a, SIGNAL( triggered() ), this, SLOT( cursorFirst() ) );
111 a = new QAction( "Select last branch",this);
112 a->setShortcut ( Qt::Key_End );
113 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
115 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLast() ) );
117 // Action to embed LineEdit for heading in Scene
118 editingHeading=false;
119 lineEdit=new QLineEdit;
121 QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
122 pw->setZValue (Z_LINEEDIT);
124 a = new QAction( tr( "Edit heading","MapEditor" ), this);
125 a->setShortcut ( Qt::Key_Return ); //Edit heading
127 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
128 a = new QAction( tr( "Edit heading","MapEditor" ), this);
129 a->setShortcut ( Qt::Key_Enter); //Edit heading
131 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
134 selectionColor =QColor (255,255,0);
137 // Attributes //FIXME-2 testing only...
140 attrTable= new AttributeTable();
142 ad=attrTable->addKey (k,StringList);
146 sl <<"val 1"<<"val 2"<< "val 3";
147 ad->setValue (QVariant (sl));
149 //attrTable->addValue ("Key A","P 1");
150 //attrTable->addValue ("Key A","P 2");
151 //attrTable->addValue ("Key A","P 3");
152 //attrTable->addValue ("Key A","P 4");
154 ad=attrTable->addKey (k,FreeString);
157 //attrTable->addValue ("Key B","w1");
158 //attrTable->addValue ("Key B","w2");
160 k="C - UniqueString";
161 ad=attrTable->addKey (k,UniqueString);
164 //attrTable->addKey ("Key Prio");
165 //attrTable->addValue ("Key Prio","Prio 1");
166 //attrTable->addValue ("Key Prio","Prio 2");
170 MapEditor::~MapEditor()
172 //cout <<"Destructor MapEditor for "<<model->getMapName().toStdString()<<endl;
173 model->unregisterEditor(this);
176 VymModel* MapEditor::getModel()
181 QGraphicsScene * MapEditor::getScene()
186 void MapEditor::scrollTo (const QModelIndex &index)
190 LinkableMapObj* lmo=NULL;
191 TreeItem *ti= static_cast<TreeItem*>(index.internalPointer());
192 if (ti->getType()==TreeItem::Image ||ti->isBranchLikeType() )
193 lmo=((MapItem*)ti)->getLMO();
194 if (lmo) setScrollBarPosTarget (lmo->getBBox() );
198 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
200 // Code copied from Qt sources
204 qreal width = viewport()->width();
205 qreal height = viewport()->height();
206 QRectF viewRect = matrix().mapRect(rect);
208 qreal left = horizontalScrollBar()->value();
209 qreal right = left + width;
210 qreal top = verticalScrollBar()->value();
211 qreal bottom = top + height;
213 if (viewRect.left() <= left + xmargin) {
214 // need to scroll from the left
215 // if (!d->leftIndent)
216 scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
218 if (viewRect.right() >= right - xmargin) {
219 // need to scroll from the right
220 // if (!d->leftIndent)
221 scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
223 if (viewRect.top() <= top + ymargin) {
224 // need to scroll from the top
225 // if (!d->topIndent)
226 scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
228 if (viewRect.bottom() >= bottom - ymargin) {
229 // need to scroll from the bottom
230 // if (!d->topIndent)
231 scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
234 if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
235 scrollBarPosAnimation.stop();
236 scrollBarPosAnimation.setTargetObject (this);
237 scrollBarPosAnimation.setPropertyName ("scrollBarPos");
238 scrollBarPosAnimation.setDuration(1000);
239 scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
240 scrollBarPosAnimation.setStartValue(
241 QPointF (horizontalScrollBar()->value() ,
242 verticalScrollBar()->value() ) );
243 scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
244 scrollBarPosAnimation.start();
247 QPointF MapEditor::getScrollBarPosTarget()
249 return scrollBarPosTarget;
253 void MapEditor::setScrollBarPos(const QPointF &p)
256 horizontalScrollBar()->setValue(int(p.x()));
257 verticalScrollBar()->setValue(int(p.y()));
260 QPointF MapEditor::getScrollBarPos()
265 void MapEditor::setZoomFactorTarget (const qreal &zft)
267 zoomFactorTarget=zft;
268 if (zoomAnimation.state()==QtAbstractAnimation::Running)
269 zoomAnimation.stop();
270 //zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
271 zoomAnimation.setTargetObject (this);
272 zoomAnimation.setPropertyName ("zoomFactor");
273 zoomAnimation.setDuration(1000);
274 zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
275 zoomAnimation.setStartValue(zoomFactor);
276 zoomAnimation.setEndValue(zft);
277 zoomAnimation.start();
280 qreal MapEditor::getZoomFactorTarget()
282 return zoomFactorTarget;
286 void MapEditor::setZoomFactor(const qreal &zf)
289 setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
292 qreal MapEditor::getZoomFactor()
297 void MapEditor::print()
301 printer = new QPrinter;
302 printer->setColorMode (QPrinter::Color);
303 printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
304 printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
305 printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
308 QRectF totalBBox=getTotalBBox();
310 // Try to set orientation automagically
311 // Note: Interpretation of generated postscript is amibiguous, if
312 // there are problems with landscape mode, see
313 // http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
315 if (totalBBox.width()>totalBBox.height())
316 // recommend landscape
317 printer->setOrientation (QPrinter::Landscape);
319 // recommend portrait
320 printer->setOrientation (QPrinter::Portrait);
322 if ( printer->setup(this) )
323 // returns false, if printing is canceled
325 QPainter pp(printer);
327 pp.setRenderHint(QPainter::Antialiasing,true);
329 // Don't print the visualisation of selection
332 QRectF mapRect=totalBBox;
333 QGraphicsRectItem *frame=NULL;
337 // Print frame around map
338 mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10,
339 totalBBox.width()+20, totalBBox.height()+20);
340 frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
346 double paperAspect = (double)printer->width() / (double)printer->height();
347 double mapAspect = (double)mapRect.width() / (double)mapRect.height();
349 if (mapAspect>=paperAspect)
351 // Fit horizontally to paper width
352 //pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );
353 viewBottom=(int)(printer->width()/mapAspect);
356 // Fit vertically to paper height
357 //pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());
358 viewBottom=printer->height();
363 // Print footer below map
365 font.setPointSize(10);
367 QRectF footerBox(0,viewBottom,printer->width(),15);
368 // FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
369 pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
373 QRectF (0,0,printer->width(),printer->height()-15),
374 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
377 // Viewport has paper dimension
378 if (frame) delete (frame);
383 // Save settings in vymrc
384 settings.writeEntry("/mainwindow/printerName",printer->printerName());
385 settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
386 settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
390 QRectF MapEditor::getTotalBBox() //FIXME-2 needed e.g. for image export
394 for (int i=0;i<rootItem->branchCount(); i++)
395 r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
401 QPixmap MapEditor::getPixmap()
403 QRectF mapRect=getTotalBBox();
404 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
407 pp.setRenderHints(renderHints());
409 // Don't print the visualisation of selection
412 mapScene->render ( &pp,
413 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
414 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
423 void MapEditor::setAntiAlias (bool b)
425 setRenderHint(QPainter::Antialiasing,b);
428 void MapEditor::setSmoothPixmap(bool b)
430 setRenderHint(QPainter::SmoothPixmapTransform,b);
433 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
435 // Start with mapcenter, no images allowed at rootItem
437 BranchItem *bi=model->getRootItem()->getFirstBranch();
438 TreeItem *found=NULL;
441 found=bi->findMapItem (p, exclude);
442 if (found) return found;
444 bi=model->getRootItem()->getBranchNum(i);
449 AttributeTable* MapEditor::attributeTable()
454 void MapEditor::testFunction1()
457 // Code copied from Qt sources
458 QRectF rect=model->getSelectedBranchObj()->getBBox();
462 qreal width = viewport()->width();
463 qreal height = viewport()->height();
464 QRectF viewRect = matrix().mapRect(rect);
466 qreal left = horizontalScrollBar()->value();
467 qreal right = left + width;
468 qreal top = verticalScrollBar()->value();
469 qreal bottom = top + height;
471 if (viewRect.left() <= left + xmargin) {
472 // need to scroll from the left
473 // if (!d->leftIndent)
474 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
476 if (viewRect.right() >= right - xmargin) {
477 // need to scroll from the right
478 // if (!d->leftIndent)
479 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
481 if (viewRect.top() <= top + ymargin) {
482 // need to scroll from the top
483 // if (!d->topIndent)
484 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
486 if (viewRect.bottom() >= bottom - ymargin) {
487 // need to scroll from the bottom
488 // if (!d->topIndent)
489 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
491 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
492 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
497 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
498 animation->setDuration(5000);
499 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
500 animation->setEasingCurve ( QtEasingCurve::OutQuint);
501 animation->setStartValue(sceneRect() );
502 animation->setEndValue(QRectF(50, 50, 1000, 1000));
507 QDialog *dia= new QDialog (this);
508 dia->setGeometry (50,50,10,10);
513 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
514 animation->setDuration(1000);
515 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
516 animation->setEasingCurve ( QtEasingCurve::OutQuint);
517 animation->setStartValue(QRect(50, 50, 10, 10));
518 animation->setEndValue(QRect(250, 250, 100, 100));
523 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
524 if (hidemode==HideNone)
526 setHideTmpMode (HideExport);
527 mapCenter->calcBBoxSizeWithChilds();
528 QRectF totalBBox=mapCenter->getTotalBBox();
529 QRectF mapRect=totalBBox;
530 QCanvasRectangle *frame=NULL;
532 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
534 mapRect.setRect (totalBBox.x(), totalBBox.y(),
535 totalBBox.width(), totalBBox.height());
536 frame=new QCanvasRectangle (mapRect,mapScene);
537 frame->setBrush (QColor(white));
538 frame->setPen (QColor(black));
544 setHideTmpMode (HideNone);
546 cout <<" hidemode="<<hidemode<<endl;
550 void MapEditor::testFunction2()
555 if (hidemode==HideExport)
556 setHideTmpMode (HideNone);
558 setHideTmpMode (HideExport);
562 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
567 if (i>0) return bi->parent()->getBranchNum(i-1);
572 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
576 int dz=selbi->depth(); // original depth
578 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
583 // Look for branch with same parent but directly above
585 bi=getBranchDirectBelow(selbi);
587 bi=getBranchDirectAbove (selbi);
590 // direct predecessor
593 // Go towards center and look for predecessor
594 while (selbi->depth()>0)
596 selbi=(BranchItem*)(selbi->parent());
597 if (selbi->depth()==1 && invert)
598 bi=getBranchDirectBelow (selbi);
600 bi=getBranchDirectAbove (selbi);
605 while (selbi->depth()<dz)
607 // try to get back to original depth dz
608 bi=selbi->getLastBranch();
622 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
627 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
632 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
637 int dz=selbi->depth(); // original depth
639 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
643 // Look for branch with same parent but directly below
645 bi=getBranchDirectAbove (selbi);
647 bi=getBranchDirectBelow (selbi);
653 // Go towards center and look for neighbour
654 while (selbi->depth()>0)
656 selbi=(BranchItem*)(selbi->parent());
657 if (selbi->depth()==1 && invert)
658 bi=getBranchDirectAbove (selbi);
660 bi=getBranchDirectBelow (selbi);
665 while (selbi->depth()<dz)
667 // try to get back to original depth dz
668 bi=selbi->getFirstBranch();
682 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
687 // Special case: use alternative selection index
688 return bi->getLastSelectedBranchAlt();
689 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
691 return (BranchItem*)(bi->parent());
694 if (bi->getType()== TreeItem::Branch )
695 return bi->getLastSelectedBranch();
700 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
704 if (bi->depth()==0) return bi->getLastSelectedBranch();
705 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
707 return (BranchItem*)(bi->parent());
710 if (bi->getType()== TreeItem::Branch )
711 return (BranchItem*)bi->getLastSelectedBranch();
718 void MapEditor::cursorUp()
720 BranchItem *bi=model->getSelectedBranch();
721 if (bi) model->select (getBranchAbove(bi));
724 void MapEditor::cursorDown()
727 BranchItem *bi=model->getSelectedBranch();
728 if (bi) model->select (getBranchBelow(bi));
731 void MapEditor::cursorLeft()
733 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
734 if (bi) model->select (bi);
737 void MapEditor::cursorRight()
739 BranchItem *bi=getRightBranch (model->getSelectedBranch());
740 if (bi) model->select (bi);
743 void MapEditor::cursorFirst()
745 model->selectFirstBranch();
748 void MapEditor::cursorLast()
750 model->selectLastBranch();
754 void MapEditor::editHeading()
758 editHeadingFinished();
761 BranchObj *bo=model->getSelectedBranchObj();
762 BranchItem *bi=model->getSelectedBranch();
765 model->setSelectionBlocked(true);
767 lineEdit->setText (bi->getHeading());
768 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
769 lineEdit->setGeometry(p.x(),p.y(),230,25);
770 lineEdit->selectAll();
772 lineEdit->setFocus();
773 lineEdit->grabKeyboard();
778 void MapEditor::editHeadingFinished()
780 editingHeading=false;
781 lineEdit->releaseKeyboard();
782 model->setHeading (lineEdit->text() );
783 model->setSelectionBlocked(false);
786 // Maybe reselect previous branch
787 mainWindow->editHeadingFinished (model);
791 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
793 // Lineedits are already closed by preceding
794 // mouseEvent, we don't need to close here.
796 QPointF p = mapToScene(e->pos());
797 TreeItem *ti=findMapItem (p, NULL);
798 LinkableMapObj* lmo=NULL;
799 if (ti) lmo=((MapItem*)ti)->getLMO();
802 { // MapObj was found
803 if (model->getSelectedLMO() != lmo)
809 if (model->getSelectedBranchObj() )
811 // Context Menu on branch or mapcenter
812 //FIXME-3 model->updateActions(); needed?
813 branchContextMenu->popup(e->globalPos() );
816 if (model->getSelectedImage() )
818 // Context Menu on floatimage
819 // model->updateActions(); FIXME-3 needed?
820 floatimageContextMenu->popup(e->globalPos() );
824 { // No MapObj found, we are on the Canvas itself
825 // Context Menu on scene
826 // model->updateActions(); FIXME-3 needed?
828 // Open context menu synchronously to position new mapcenter
829 model->setContextPos (p);
830 canvasContextMenu->exec(e->globalPos() );
831 model->unsetContextPos ();
836 void MapEditor::keyPressEvent(QKeyEvent* e)
838 if (e->modifiers() & Qt::ControlModifier)
840 switch (mainWindow->getModMode())
842 case Main::ModModeColor:
843 setCursor (PickColorCursor);
845 case Main::ModModeCopy:
846 setCursor (CopyCursor);
848 case Main::ModModeXLink:
849 setCursor (XLinkCursor);
852 setCursor (Qt::ArrowCursor);
858 void MapEditor::keyReleaseEvent(QKeyEvent* e)
860 if (!(e->modifiers() & Qt::ControlModifier))
861 setCursor (Qt::ArrowCursor);
864 void MapEditor::mousePressEvent(QMouseEvent* e)
866 // Ignore right clicks, these will go to context menus
867 if (e->button() == Qt::RightButton )
873 //Ignore clicks while editing heading
874 if (model->isSelectionBlocked() )
880 QPointF p = mapToScene(e->pos());
881 TreeItem *ti=findMapItem (p, NULL);
882 LinkableMapObj* lmo=NULL;
883 if (ti) lmo=((MapItem*)ti)->getLMO();
888 //Take care of system flags _or_ modifier modes
890 if (lmo && ti->isBranchLikeType() )
892 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
893 if (!foname.isEmpty())
895 // systemFlag clicked
896 model->select (lmo); // FIXME-3 was selectInt
899 if (e->state() & Qt::ControlModifier)
900 mainWindow->editOpenURLTab();
902 mainWindow->editOpenURL();
904 else if (foname=="system-vymLink")
906 mainWindow->editOpenVymLink();
907 // tabWidget may change, better return now
908 // before segfaulting...
909 } else if (foname=="note")
910 mainWindow->windowToggleNoteEditor();
911 else if (foname=="hideInExport")
912 model->toggleHideExport();
913 // FIXME-3 needed? xelection.update();
917 // No system flag clicked, take care of modmodes (CTRL-Click)
918 if (e->state() & Qt::ControlModifier)
920 if (mainWindow->getModMode()==Main::ModModeColor)
923 setCursor (PickColorCursor);
926 if (mainWindow->getModMode()==Main::ModModeXLink)
928 BranchItem *bi_begin=model->getSelectedBranch();
932 tmpXLink=model->createXLink(bi_begin,true);
933 tmpXLink->setColor(model->getMapDefXLinkColor());
934 tmpXLink->setWidth(model->getMapDefXLinkWidth());
935 tmpXLink->setEnd (p);
936 tmpXLink->updateXLink();
945 cout << "ME::mouse pressed\n";
946 cout << " lmo="<<lmo<<endl;
947 cout << " ti="<<ti->getHeadingStd()<<endl;
949 // Select the clicked object
954 // Left Button Move Branches
955 if (e->button() == Qt::LeftButton )
957 movingObj_start.setX( p.x() - lmo->x() );
958 movingObj_start.setY( p.y() - lmo->y() );
959 movingObj_orgPos.setX (lmo->x() );
960 movingObj_orgPos.setY (lmo->y() );
962 movingObj_orgRelPos=lmo->getRelPos();
964 // If modMode==copy, then we want to "move" the _new_ object around
965 // then we need the offset from p to the _old_ selection, because of tmp
966 if (mainWindow->getModMode()==Main::ModModeCopy &&
967 e->state() & Qt::ControlModifier)
969 BranchItem *bi=model->getSelectedBranch();
973 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
974 //bi->addBranch (model->getSelectedBranch());
976 model->select(bi->getLastBranch());
981 movingObj=model->getSelectedLMO();
983 // Middle Button Toggle Scroll
984 // (On Mac OS X this won't work, but we still have
985 // a button in the toolbar)
986 if (e->button() == Qt::MidButton )
987 model->toggleScroll();
988 // model->updateActions(); FIXME-3 needed?
989 // FIXME-3 needed? xelection.update();
991 { // No MapObj found, we are on the scene itself
992 // Left Button move Pos of sceneView
993 if (e->button() == Qt::LeftButton )
995 movingObj=NULL; // move Content not Obj
996 movingObj_start=e->globalPos();
997 movingCont_start=QPointF (
998 horizontalScrollBar()->value(),
999 verticalScrollBar()->value());
1000 movingVec=QPointF(0,0);
1001 setCursor(HandOpenCursor);
1006 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1008 QPointF p = mapToScene(e->pos());
1009 TreeItem *seli=model->getSelectedItem();
1010 LinkableMapObj* lmosel=NULL;
1011 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1012 lmosel=((MapItem*)seli)->getLMO();
1014 // Move the selected MapObj
1015 if ( lmosel && movingObj)
1017 // reset cursor if we are moving and don't copy
1018 if (mainWindow->getModMode()!=Main::ModModeCopy)
1019 setCursor (Qt::ArrowCursor);
1021 // To avoid jumping of the sceneView, only
1022 // show selection, if not tmp linked
1023 if (!lmosel->hasParObjTmp())
1024 model->emitShowSelection();
1026 // Now move the selection, but add relative position
1027 // (movingObj_start) where selection was chosen with
1028 // mousepointer. (This avoids flickering resp. jumping
1029 // of selection back to absPos)
1031 // Check if we could link
1032 TreeItem *ti=findMapItem (p, seli);
1033 BranchItem *dsti=NULL;
1034 LinkableMapObj* dst=NULL;
1035 if (ti && ti!=seli && ti->isBranchLikeType())
1037 dsti=(BranchItem*)ti;
1043 if (lmosel && seli->getType()==TreeItem::Image)
1045 FloatObj *fio=(FloatImageObj*)lmosel;
1046 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1048 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1049 model->emitSelectionChanged(); // position has changed
1051 // Relink float to new mapcenter or branch, if shift is pressed
1052 // Only relink, if selection really has a new parent
1053 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1055 // Also save the move which was done so far
1056 QString pold=qpointfToString(movingObj_orgRelPos);
1057 QString pnow=qpointfToString(fio->getRelPos());
1063 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1064 fio->getParObj()->requestReposition();
1065 model->reposition();
1067 model->relinkImage ((ImageItem*) seli,dsti);
1068 model->select (seli);
1069 //movingObj=lmosel; //FIXME-3
1070 //movingObj_orgRelPos=lmosel->getRelPos();
1072 model->reposition();
1075 { // selection != a FloatObj
1076 if (seli->depth()==0) //FIXME-1 also moved mapcenters could be linked, but not working here...
1079 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1080 ((BranchObj*)lmosel)->moveBy(
1081 QPointF(p.x() -movingObj_start.x(),
1082 p.y()-movingObj_start.y()) );
1084 lmosel->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1085 model->updateRelPositions();
1088 if (seli->depth()==1)
1091 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1092 lmosel->setRelPos();
1095 // Move ordinary branch
1096 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1097 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1098 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1099 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1101 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1102 lmosel->setRelPos();
1105 // Maybe we can relink temporary?
1108 if (e->modifiers()==Qt::ControlModifier)
1110 // Special case: CTRL to link below dst
1111 lmosel->setParObjTmp (dst,p,+1);
1113 else if (e->modifiers()==Qt::ShiftModifier)
1114 lmosel->setParObjTmp (dst,p,-1);
1116 lmosel->setParObjTmp (dst,p,0);
1119 lmosel->unsetParObjTmp();
1121 // reposition subbranch
1122 lmosel->reposition();
1125 QItemSelection sel=model->getSelectionModel()->selection();
1126 updateSelection(sel,sel); // position has changed
1128 } // no FloatImageObj
1132 } // selection && moving_obj
1134 // Draw a link from one branch to another
1137 tmpXLink->setEnd (p);
1138 tmpXLink->updateXLink();
1142 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1144 QPointF p=e->globalPos();
1145 movingVec.setX(-p.x() + movingObj_start.x() );
1146 movingVec.setY(-p.y() + movingObj_start.y() );
1147 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1148 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1153 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1155 QPointF p = mapToScene(e->pos());
1156 TreeItem *seli=model->getSelectedItem();
1158 TreeItem *dsti=NULL;
1159 if (seli) dsti=findMapItem(p, seli);
1160 LinkableMapObj* dst=NULL;
1161 if (dsti && dsti->isBranchLikeType ())
1162 dst=((MapItem*)dsti)->getLMO();
1167 // Have we been picking color?
1171 setCursor (Qt::ArrowCursor);
1172 // Check if we are over another branch
1175 if (e->state() & Qt::ShiftModifier)
1176 model->colorBranch (((BranchObj*)dst)->getColor());
1178 model->colorSubtree (((BranchObj*)dst)->getColor());
1183 // Have we been drawing a link?
1187 // Check if we are over another branch
1190 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1191 tmpXLink->updateXLink();
1192 tmpXLink->activate();
1193 //FIXME-0 model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );
1196 model->deleteItem(tmpXLink);
1202 // Have we been moving something?
1203 if ( seli && movingObj )
1205 if (seli->getType()==TreeItem::Image)
1207 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1210 // Moved FloatObj. Maybe we need to reposition
1211 QString pold=qpointfToString(movingObj_orgRelPos);
1212 QString pnow=qpointfToString(fio->getRelPos());
1218 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1220 cout << "ME::release mouse\n";
1221 fio->getParObj()->requestReposition();
1222 model->reposition();
1226 BranchItem *bi=model->getSelectedBranch();
1227 if (bi && bi->depth()==0)
1229 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1231 QString pold=qpointfToString(movingObj_orgPos);
1232 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1239 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1243 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1244 { // A branch was moved
1245 LinkableMapObj* lmosel=NULL;
1246 lmosel=((MapItem*)seli)->getLMO();
1248 // save the position in case we link to mapcenter
1249 QPointF savePos=QPointF (lmosel->getAbsPos() );
1251 // Reset the temporary drawn link to the original one
1252 lmosel->unsetParObjTmp();
1254 // For Redo we may need to save original selection
1255 QString preSelStr=model->getSelectString(seli);
1260 // We have a destination, relink to that
1262 BranchObj* bsel=model->getSelectedBranchObj();
1264 QString preParStr=model->getSelectString (bsel->getParObj());
1265 QString preNum=QString::number (seli->num(),10);
1266 QString preDstParStr;
1269 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1271 preDstParStr=model->getSelectString (dst->getParObj());
1272 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1274 if (e->state() & Qt::ControlModifier && dst->getParObj())
1277 preDstParStr=model->getSelectString (dst->getParObj());
1278 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1281 preDstParStr=model->getSelectString(dst);
1282 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1283 if (dsti->depth()==0) bsel->move (savePos);
1287 QString postSelStr=model->getSelectString(lmosel);
1288 QString postNum=QString::number (seli->num(),10);
1290 QString undoCom="relinkTo (\""+
1291 preParStr+ "\"," + preNum +"," +
1292 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1294 QString redoCom="relinkTo (\""+
1295 preDstParStr + "\"," + postNum + "," +
1296 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1301 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1306 // No destination, undo temporary move
1308 if (seli->depth()==1)
1310 // The select string might be different _after_ moving around.
1311 // Therefor reposition and then use string of old selection, too
1312 model->reposition();
1314 QPointF rp(lmosel->getRelPos());
1315 if (rp != movingObj_orgRelPos)
1317 QString ps=qpointfToString(rp);
1319 model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos),
1320 preSelStr, "moveRel "+ps,
1321 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1325 // Draw the original link, before selection was moved around
1326 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1328 lmosel->setRelPos(); // calc relPos first for starting point
1329 QPointF dst=bi->getBranchObj()->getParObj()->getChildPos(); // FIXME-3 check getBO here...
1330 // if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
1332 model->startAnimation(
1334 lmosel->getRelPos(),
1336 // QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
1339 model->reposition();
1342 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1343 // Finally resize scene, if needed
1347 // Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
1348 // model->updateActions(); FIXME-3 neeeded?
1350 // maybe we moved View: set old cursor
1351 setCursor (Qt::ArrowCursor);
1355 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1357 if (model->isSelectionBlocked() )
1363 if (e->button() == Qt::LeftButton )
1365 QPointF p = mapToScene(e->pos());
1366 TreeItem *ti=findMapItem (p, NULL);
1367 if (ti) { // MapObj was found
1368 // First select the MapObj than edit heading
1375 void MapEditor::resizeEvent (QResizeEvent* e)
1377 QGraphicsView::resizeEvent( e );
1380 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1382 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1383 // cerr << event->format(i) << endl;
1385 if (event->mimeData()->hasImage())
1386 event->acceptProposedAction();
1388 if (event->mimeData()->hasUrls())
1389 event->acceptProposedAction();
1392 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1396 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1401 void MapEditor::dropEvent(QDropEvent *event)
1403 BranchItem *selbi=model->getSelectedBranch();
1407 foreach (QString format,event->mimeData()->formats())
1408 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1412 if (event->mimeData()->hasImage())
1414 QVariant imageData = event->mimeData()->imageData();
1415 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1417 if (event->mimeData()->hasUrls())
1418 uris=event->mimeData()->urls();
1426 for (int i=0; i<uris.count();i++)
1428 // Workaround to avoid adding empty branches
1429 if (!uris.at(i).toString().isEmpty())
1431 bi=model->addNewBranch();
1435 s=uris.at(i).toLocalFile();
1438 QString file = QDir::fromNativeSeparators(s);
1439 heading = QFileInfo(file).baseName();
1441 if (file.endsWith(".vym", false))
1442 bi->setVymLink(file);
1444 bi->setURL(uris.at(i).toString());
1447 bo->setURL(uris.at(i).toString());
1451 if (!heading.isEmpty())
1452 bi->setHeading(heading);
1454 bi->setHeading(uris.at(i).toString());
1459 model->reposition();
1462 event->acceptProposedAction();
1465 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1467 // Note: Here we are prepared for multiple selections, though this
1468 // is not yet implemented elsewhere
1470 // Here in MapEditor we can only select Branches and Images
1471 QList <TreeItem*> treeItemsNew;
1472 QList <TreeItem*> treeItemsOld;
1475 foreach (ix,newsel.indexes() )
1477 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1478 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1479 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1481 foreach (ix,oldsel.indexes() )
1483 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1484 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1485 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1488 // Trim list of selection rectangles
1489 while (treeItemsNew.count() < selboxList.count() )
1490 delete selboxList.takeFirst();
1492 // Take care to tmp scroll/unscroll
1493 if (!oldsel.isEmpty())
1495 QModelIndex ix=oldsel.indexes().first();
1498 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1501 if (ti->isBranchLikeType() )
1503 // reset tmp scrolled branches
1504 BranchItem *bi=(BranchItem*)ti;
1505 bi->resetTmpUnscroll();
1507 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1508 // Hide link if not needed
1509 ((MapItem*)ti)->getLMO()->updateVisibility();
1514 if (!treeItemsNew.isEmpty())
1516 QModelIndex ix=newsel.indexes().first();
1519 // Temporary unscroll if necessary
1520 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1521 if (ti->isBranchLikeType() )
1523 BranchItem *bi=(BranchItem*)ti;
1524 if (bi->hasScrolledParent(bi) )
1528 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1529 // Show link if needed
1530 ((MapItem*)ti)->getLMO()->updateVisibility();
1534 // Reduce rectangles
1535 while (treeItemsNew.count() < selboxList.count() )
1536 delete selboxList.takeFirst();
1538 // Add additonal rectangles
1539 QGraphicsRectItem *sb;
1540 while (treeItemsNew.count() > selboxList.count() )
1542 sb = mapScene->addRect(
1544 QPen(selectionColor),
1546 sb->setZValue(Z_SELBOX);
1548 selboxList.append (sb);
1553 // Reposition rectangles
1557 LinkableMapObj *lmo;
1558 for (int i=0; i<treeItemsNew.count();++i)
1560 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1561 bbox=lmo->getBBox();
1562 sb=selboxList.at(i);
1565 bbox.width(), bbox.height());
1566 sb->setPen (selectionColor);
1567 sb->setBrush (selectionColor);
1574 void MapEditor::updateData (const QModelIndex &sel)
1576 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1579 cout << "ME::updateData\n";
1581 cout << " ti="<<ti<<endl;
1582 cout << " h="<<ti->getHeading().toStdString()<<endl;
1585 if (ti->isBranchLikeType())
1587 // cout << " ->updating...\n";
1588 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1593 void MapEditor::setSelectionColor (QColor col)
1596 QItemSelection sel=model->getSelectionModel()->selection();
1597 updateSelection(sel,sel);
1601 QColor MapEditor::getSelectionColor ()
1603 return selectionColor;