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 /* FIXME-4 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 branchContextMenu->popup(e->globalPos() );
815 if (model->getSelectedImage() )
817 // Context Menu on floatimage
818 floatimageContextMenu->popup(e->globalPos() );
822 { // No MapObj found, we are on the Canvas itself
823 // Context Menu on scene
825 // Open context menu synchronously to position new mapcenter
826 model->setContextPos (p);
827 canvasContextMenu->exec(e->globalPos() );
828 model->unsetContextPos ();
833 void MapEditor::keyPressEvent(QKeyEvent* e)
835 if (e->modifiers() & Qt::ControlModifier)
837 switch (mainWindow->getModMode())
839 case Main::ModModeColor:
840 setCursor (PickColorCursor);
842 case Main::ModModeCopy:
843 setCursor (CopyCursor);
845 case Main::ModModeXLink:
846 setCursor (XLinkCursor);
849 setCursor (Qt::ArrowCursor);
855 void MapEditor::keyReleaseEvent(QKeyEvent* e)
857 if (!(e->modifiers() & Qt::ControlModifier))
858 setCursor (Qt::ArrowCursor);
861 void MapEditor::mousePressEvent(QMouseEvent* e)
863 // Ignore right clicks, these will go to context menus
864 if (e->button() == Qt::RightButton )
870 //Ignore clicks while editing heading
871 if (model->isSelectionBlocked() )
877 QPointF p = mapToScene(e->pos());
878 TreeItem *ti=findMapItem (p, NULL);
879 LinkableMapObj* lmo=NULL;
880 if (ti) lmo=((MapItem*)ti)->getLMO();
885 //Take care of system flags _or_ modifier modes
887 if (lmo && ti->isBranchLikeType() )
889 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
890 if (!foname.isEmpty())
892 // systemFlag clicked
893 model->select (lmo); // FIXME-3 was selectInt
896 if (e->state() & Qt::ControlModifier)
897 mainWindow->editOpenURLTab();
899 mainWindow->editOpenURL();
901 else if (foname=="system-vymLink")
903 mainWindow->editOpenVymLink();
904 // tabWidget may change, better return now
905 // before segfaulting...
906 } else if (foname=="note")
907 mainWindow->windowToggleNoteEditor();
908 else if (foname=="hideInExport")
909 model->toggleHideExport();
910 // FIXME-3 needed? xelection.update();
914 // No system flag clicked, take care of modmodes (CTRL-Click)
915 if (e->state() & Qt::ControlModifier)
917 if (mainWindow->getModMode()==Main::ModModeColor)
920 setCursor (PickColorCursor);
923 if (mainWindow->getModMode()==Main::ModModeXLink)
925 BranchItem *bi_begin=model->getSelectedBranch();
929 tmpXLink=model->createXLink(bi_begin,true);
930 tmpXLink->setColor(model->getMapDefXLinkColor());
931 tmpXLink->setWidth(model->getMapDefXLinkWidth());
932 tmpXLink->setEnd (p);
933 tmpXLink->updateXLink();
942 cout << "ME::mouse pressed\n";
943 cout << " lmo="<<lmo<<endl;
944 cout << " ti="<<ti->getHeadingStd()<<endl;
946 // Select the clicked object
951 // Left Button Move Branches
952 if (e->button() == Qt::LeftButton )
954 movingObj_start.setX( p.x() - lmo->x() );
955 movingObj_start.setY( p.y() - lmo->y() );
956 movingObj_orgPos.setX (lmo->x() );
957 movingObj_orgPos.setY (lmo->y() );
959 movingObj_orgRelPos=lmo->getRelPos();
961 // If modMode==copy, then we want to "move" the _new_ object around
962 // then we need the offset from p to the _old_ selection, because of tmp
963 if (mainWindow->getModMode()==Main::ModModeCopy &&
964 e->state() & Qt::ControlModifier)
966 BranchItem *bi=model->getSelectedBranch();
970 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
971 //bi->addBranch (model->getSelectedBranch());
973 model->select(bi->getLastBranch());
978 movingObj=model->getSelectedLMO();
980 // Middle Button Toggle Scroll
981 // (On Mac OS X this won't work, but we still have
982 // a button in the toolbar)
983 if (e->button() == Qt::MidButton )
984 model->toggleScroll();
985 // model->updateActions(); FIXME-3 needed?
986 // FIXME-3 needed? xelection.update();
988 { // No MapObj found, we are on the scene itself
989 // Left Button move Pos of sceneView
990 if (e->button() == Qt::LeftButton )
992 movingObj=NULL; // move Content not Obj
993 movingObj_start=e->globalPos();
994 movingCont_start=QPointF (
995 horizontalScrollBar()->value(),
996 verticalScrollBar()->value());
997 movingVec=QPointF(0,0);
998 setCursor(HandOpenCursor);
1003 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1005 QPointF p = mapToScene(e->pos());
1006 TreeItem *seli=model->getSelectedItem();
1007 LinkableMapObj* lmosel=NULL;
1008 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1009 lmosel=((MapItem*)seli)->getLMO();
1011 // Move the selected MapObj
1012 if ( lmosel && movingObj)
1014 // reset cursor if we are moving and don't copy
1015 if (mainWindow->getModMode()!=Main::ModModeCopy)
1016 setCursor (Qt::ArrowCursor);
1018 // To avoid jumping of the sceneView, only
1019 // show selection, if not tmp linked
1020 if (!lmosel->hasParObjTmp())
1021 model->emitShowSelection();
1023 // Now move the selection, but add relative position
1024 // (movingObj_start) where selection was chosen with
1025 // mousepointer. (This avoids flickering resp. jumping
1026 // of selection back to absPos)
1028 // Check if we could link
1029 TreeItem *ti=findMapItem (p, seli);
1030 BranchItem *dsti=NULL;
1031 LinkableMapObj* dst=NULL;
1032 if (ti && ti!=seli && ti->isBranchLikeType())
1034 dsti=(BranchItem*)ti;
1040 if (lmosel && seli->getType()==TreeItem::Image)
1042 FloatObj *fio=(FloatImageObj*)lmosel;
1043 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1045 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1046 model->emitSelectionChanged(); // position has changed
1048 // Relink float to new mapcenter or branch, if shift is pressed
1049 // Only relink, if selection really has a new parent
1050 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1052 // Also save the move which was done so far
1053 QString pold=qpointfToString(movingObj_orgRelPos);
1054 QString pnow=qpointfToString(fio->getRelPos());
1060 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1061 fio->getParObj()->requestReposition();
1062 model->reposition();
1064 model->relinkImage ((ImageItem*) seli,dsti);
1065 model->select (seli);
1066 //movingObj=lmosel; //FIXME-3
1067 //movingObj_orgRelPos=lmosel->getRelPos();
1069 model->reposition();
1072 { // selection != a FloatObj
1073 if (seli->depth()==0) //FIXME-1 also moved mapcenters could be linked, but not working here...
1076 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1077 ((BranchObj*)lmosel)->moveBy(
1078 QPointF(p.x() -movingObj_start.x(),
1079 p.y()-movingObj_start.y()) );
1081 lmosel->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1082 model->updateRelPositions();
1085 if (seli->depth()==1)
1088 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1089 lmosel->setRelPos();
1092 // Move ordinary branch
1093 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1094 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1095 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1096 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1098 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1099 lmosel->setRelPos();
1102 // Maybe we can relink temporary?
1105 if (e->modifiers()==Qt::ControlModifier)
1107 // Special case: CTRL to link below dst
1108 lmosel->setParObjTmp (dst,p,+1);
1110 else if (e->modifiers()==Qt::ShiftModifier)
1111 lmosel->setParObjTmp (dst,p,-1);
1113 lmosel->setParObjTmp (dst,p,0);
1116 lmosel->unsetParObjTmp();
1118 // reposition subbranch
1119 lmosel->reposition();
1122 QItemSelection sel=model->getSelectionModel()->selection();
1123 updateSelection(sel,sel); // position has changed
1125 } // no FloatImageObj
1129 } // selection && moving_obj
1131 // Draw a link from one branch to another
1134 tmpXLink->setEnd (p);
1135 tmpXLink->updateXLink();
1139 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1141 QPointF p=e->globalPos();
1142 movingVec.setX(-p.x() + movingObj_start.x() );
1143 movingVec.setY(-p.y() + movingObj_start.y() );
1144 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1145 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1150 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1152 QPointF p = mapToScene(e->pos());
1153 TreeItem *seli=model->getSelectedItem();
1155 TreeItem *dsti=NULL;
1156 if (seli) dsti=findMapItem(p, seli);
1157 LinkableMapObj* dst=NULL;
1158 if (dsti && dsti->isBranchLikeType ())
1159 dst=((MapItem*)dsti)->getLMO();
1164 // Have we been picking color?
1168 setCursor (Qt::ArrowCursor);
1169 // Check if we are over another branch
1172 if (e->state() & Qt::ShiftModifier)
1173 model->colorBranch (((BranchObj*)dst)->getColor());
1175 model->colorSubtree (((BranchObj*)dst)->getColor());
1180 // Have we been drawing a link?
1184 // Check if we are over another branch
1187 tmpXLink->setEnd ( ((BranchItem*)dsti) );
1188 tmpXLink->updateXLink();
1189 tmpXLink->activate();
1190 //FIXME-0 model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );
1193 model->deleteItem(tmpXLink);
1199 // Have we been moving something?
1200 if ( seli && movingObj )
1202 if (seli->getType()==TreeItem::Image)
1204 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1207 // Moved FloatObj. Maybe we need to reposition
1208 QString pold=qpointfToString(movingObj_orgRelPos);
1209 QString pnow=qpointfToString(fio->getRelPos());
1215 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1217 cout << "ME::release mouse\n";
1218 fio->getParObj()->requestReposition();
1219 model->reposition();
1223 BranchItem *bi=model->getSelectedBranch();
1224 if (bi && bi->depth()==0)
1226 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1228 QString pold=qpointfToString(movingObj_orgPos);
1229 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1236 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1240 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1241 { // A branch was moved
1242 LinkableMapObj* lmosel=NULL;
1243 lmosel=((MapItem*)seli)->getLMO();
1245 // save the position in case we link to mapcenter
1246 QPointF savePos=QPointF (lmosel->getAbsPos() );
1248 // Reset the temporary drawn link to the original one
1249 lmosel->unsetParObjTmp();
1251 // For Redo we may need to save original selection
1252 QString preSelStr=model->getSelectString(seli);
1257 // We have a destination, relink to that
1259 BranchObj* bsel=model->getSelectedBranchObj();
1261 QString preParStr=model->getSelectString (bsel->getParObj());
1262 QString preNum=QString::number (seli->num(),10);
1263 QString preDstParStr;
1266 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1268 preDstParStr=model->getSelectString (dst->getParObj());
1269 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1271 if (e->state() & Qt::ControlModifier && dst->getParObj())
1274 preDstParStr=model->getSelectString (dst->getParObj());
1275 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1278 preDstParStr=model->getSelectString(dst);
1279 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1280 if (dsti->depth()==0) bsel->move (savePos);
1284 QString postSelStr=model->getSelectString(lmosel);
1285 QString postNum=QString::number (seli->num(),10);
1287 QString undoCom="relinkTo (\""+
1288 preParStr+ "\"," + preNum +"," +
1289 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1291 QString redoCom="relinkTo (\""+
1292 preDstParStr + "\"," + postNum + "," +
1293 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1298 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1303 // No destination, undo temporary move
1305 if (seli->depth()==1)
1307 // The select string might be different _after_ moving around.
1308 // Therefor reposition and then use string of old selection, too
1309 model->reposition();
1311 QPointF rp(lmosel->getRelPos());
1312 if (rp != movingObj_orgRelPos)
1314 QString ps=qpointfToString(rp);
1316 model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos),
1317 preSelStr, "moveRel "+ps,
1318 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1322 // Draw the original link, before selection was moved around
1323 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1325 lmosel->setRelPos(); // calc relPos first for starting point
1326 QPointF dst=bi->getBranchObj()->getParObj()->getChildPos(); // FIXME-3 check getBO here...
1327 // if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
1329 model->startAnimation(
1331 lmosel->getRelPos(),
1333 // QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
1336 model->reposition();
1339 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1340 // Finally resize scene, if needed
1344 // Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
1345 // model->updateActions(); FIXME-3 neeeded?
1347 // maybe we moved View: set old cursor
1348 setCursor (Qt::ArrowCursor);
1352 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1354 if (model->isSelectionBlocked() )
1360 if (e->button() == Qt::LeftButton )
1362 QPointF p = mapToScene(e->pos());
1363 TreeItem *ti=findMapItem (p, NULL);
1364 if (ti) { // MapObj was found
1365 // First select the MapObj than edit heading
1372 void MapEditor::resizeEvent (QResizeEvent* e)
1374 QGraphicsView::resizeEvent( e );
1377 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1379 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1380 // cerr << event->format(i) << endl;
1382 if (event->mimeData()->hasImage())
1383 event->acceptProposedAction();
1385 if (event->mimeData()->hasUrls())
1386 event->acceptProposedAction();
1389 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1393 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1398 void MapEditor::dropEvent(QDropEvent *event)
1400 BranchItem *selbi=model->getSelectedBranch();
1404 foreach (QString format,event->mimeData()->formats())
1405 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1409 if (event->mimeData()->hasImage())
1411 QVariant imageData = event->mimeData()->imageData();
1412 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1414 if (event->mimeData()->hasUrls())
1415 uris=event->mimeData()->urls();
1423 for (int i=0; i<uris.count();i++)
1425 // Workaround to avoid adding empty branches
1426 if (!uris.at(i).toString().isEmpty())
1428 bi=model->addNewBranch();
1432 s=uris.at(i).toLocalFile();
1435 QString file = QDir::fromNativeSeparators(s);
1436 heading = QFileInfo(file).baseName();
1438 if (file.endsWith(".vym", false))
1439 bi->setVymLink(file);
1441 bi->setURL(uris.at(i).toString());
1444 bo->setURL(uris.at(i).toString());
1448 if (!heading.isEmpty())
1449 bi->setHeading(heading);
1451 bi->setHeading(uris.at(i).toString());
1456 model->reposition();
1459 event->acceptProposedAction();
1462 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1464 // Note: Here we are prepared for multiple selections, though this
1465 // is not yet implemented elsewhere
1467 // Here in MapEditor we can only select Branches and Images
1468 QList <TreeItem*> treeItemsNew;
1469 QList <TreeItem*> treeItemsOld;
1472 foreach (ix,newsel.indexes() )
1474 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1475 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1476 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1478 foreach (ix,oldsel.indexes() )
1480 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1481 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1482 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1485 // Trim list of selection rectangles
1486 while (treeItemsNew.count() < selboxList.count() )
1487 delete selboxList.takeFirst();
1489 // Take care to tmp scroll/unscroll
1490 if (!oldsel.isEmpty())
1492 QModelIndex ix=oldsel.indexes().first();
1495 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1498 if (ti->isBranchLikeType() )
1500 // reset tmp scrolled branches
1501 BranchItem *bi=(BranchItem*)ti;
1502 bi->resetTmpUnscroll();
1504 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1505 // Hide link if not needed
1506 ((MapItem*)ti)->getLMO()->updateVisibility();
1511 if (!treeItemsNew.isEmpty())
1513 QModelIndex ix=newsel.indexes().first();
1516 // Temporary unscroll if necessary
1517 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1518 if (ti->isBranchLikeType() )
1520 BranchItem *bi=(BranchItem*)ti;
1521 if (bi->hasScrolledParent(bi) )
1525 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1526 // Show link if needed
1527 ((MapItem*)ti)->getLMO()->updateVisibility();
1531 // Reduce rectangles
1532 while (treeItemsNew.count() < selboxList.count() )
1533 delete selboxList.takeFirst();
1535 // Add additonal rectangles
1536 QGraphicsRectItem *sb;
1537 while (treeItemsNew.count() > selboxList.count() )
1539 sb = mapScene->addRect(
1541 QPen(selectionColor),
1543 sb->setZValue(Z_SELBOX);
1545 selboxList.append (sb);
1550 // Reposition rectangles
1554 LinkableMapObj *lmo;
1555 for (int i=0; i<treeItemsNew.count();++i)
1557 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1558 bbox=lmo->getBBox();
1559 sb=selboxList.at(i);
1562 bbox.width(), bbox.height());
1563 sb->setPen (selectionColor);
1564 sb->setBrush (selectionColor);
1571 void MapEditor::updateData (const QModelIndex &sel)
1573 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1576 cout << "ME::updateData\n";
1578 cout << " ti="<<ti<<endl;
1579 cout << " h="<<ti->getHeading().toStdString()<<endl;
1582 if (ti->isBranchLikeType())
1584 // cout << " ->updating...\n";
1585 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1590 void MapEditor::setSelectionColor (QColor col)
1593 QItemSelection sel=model->getSelectionModel()->selection();
1594 updateSelection(sel,sel);
1598 QColor MapEditor::getSelectionColor ()
1600 return selectionColor;