9 #include "branchitem.h"
10 #include "mainwindow.h"
12 #include "warningdialog.h"
15 extern int statusbarTime;
16 extern Main *mainWindow;
17 extern QString tmpVymDir;
18 extern QString clipboardDir;
19 extern QString clipboardFile;
20 extern bool clipboardEmpty;
23 extern QMenu* branchContextMenu;
24 extern QMenu* branchAddContextMenu;
25 extern QMenu* branchRemoveContextMenu;
26 extern QMenu* branchLinksContextMenu;
27 extern QMenu* branchXLinksContextMenuEdit;
28 extern QMenu* branchXLinksContextMenuFollow;
29 extern QMenu* floatimageContextMenu;
30 extern QMenu* canvasContextMenu;
32 extern Settings settings;
33 extern QString iconPath;
35 ///////////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////
37 MapEditor::MapEditor( VymModel *vm)
39 //cout << "Constructor ME "<<this<<endl;
40 mapScene= new QGraphicsScene(NULL);
41 mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
43 zoomFactor=zoomFactorTarget=1;
46 model->setScene (mapScene);
47 model->registerEditor(this);
48 model->makeDefault(); // No changes in model so far
54 // Create bitmap cursors, platform dependant
55 HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);
56 PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 );
57 CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 );
58 XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 );
60 //setFocusPolicy (Qt::StrongFocus); //FIXME-3
72 setAcceptDrops (true);
74 //model->reposition(); //FIXME-3 really still needed?
77 // Shortcuts and actions
79 a = new QAction("Select upper branch", this);
80 a->setShortcut (Qt::Key_Up );
81 a->setShortcutContext (Qt::WidgetShortcut);
83 connect( a, SIGNAL( triggered() ), this, SLOT( cursorUp() ) );
85 a = new QAction( "Select lower branch",this);
86 a->setShortcut ( Qt::Key_Down );
87 a->setShortcutContext (Qt::WidgetShortcut);
89 connect( a, SIGNAL( triggered() ), this, SLOT( cursorDown() ) );
91 a = new QAction( "Select left branch", this);
92 a->setShortcut (Qt::Key_Left );
93 // a->setShortcutContext (Qt::WindowShortcut);
94 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
96 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLeft() ) );
98 a = new QAction( "Select child branch", this);
99 a->setShortcut (Qt::Key_Right);
100 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
102 connect( a, SIGNAL( triggered() ), this, SLOT( cursorRight() ) );
104 a = new QAction( "Select first branch", this);
105 a->setShortcut (Qt::Key_Home );
106 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
108 connect( a, SIGNAL( triggered() ), this, SLOT( cursorFirst() ) );
110 a = new QAction( "Select last branch",this);
111 a->setShortcut ( Qt::Key_End );
112 a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
114 connect( a, SIGNAL( triggered() ), this, SLOT( cursorLast() ) );
116 // Action to embed LineEdit for heading in Scene
117 editingHeading=false;
118 lineEdit=new QLineEdit;
120 QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
121 pw->setZValue (Z_LINEEDIT);
123 a = new QAction( tr( "Edit heading","MapEditor" ), this);
124 a->setShortcut ( Qt::Key_Return ); //Edit heading
126 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
127 a = new QAction( tr( "Edit heading","MapEditor" ), this);
128 a->setShortcut ( Qt::Key_Enter); //Edit heading
130 connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
133 selectionColor =QColor (255,255,0);
136 // Attributes //FIXME-2 testing only...
139 attrTable= new AttributeTable();
141 ad=attrTable->addKey (k,StringList);
145 sl <<"val 1"<<"val 2"<< "val 3";
146 ad->setValue (QVariant (sl));
148 //attrTable->addValue ("Key A","P 1");
149 //attrTable->addValue ("Key A","P 2");
150 //attrTable->addValue ("Key A","P 3");
151 //attrTable->addValue ("Key A","P 4");
153 ad=attrTable->addKey (k,FreeString);
156 //attrTable->addValue ("Key B","w1");
157 //attrTable->addValue ("Key B","w2");
159 k="C - UniqueString";
160 ad=attrTable->addKey (k,UniqueString);
163 //attrTable->addKey ("Key Prio");
164 //attrTable->addValue ("Key Prio","Prio 1");
165 //attrTable->addValue ("Key Prio","Prio 2");
169 MapEditor::~MapEditor()
171 //cout <<"Destructor MapEditor for "<<model->getMapName().toStdString()<<endl;
172 model->unregisterEditor(this);
175 VymModel* MapEditor::getModel()
180 QGraphicsScene * MapEditor::getScene()
185 void MapEditor::scrollTo (const QModelIndex &index)
189 LinkableMapObj* lmo=NULL;
190 TreeItem *ti= static_cast<TreeItem*>(index.internalPointer());
191 if (ti->getType()==TreeItem::Image ||ti->isBranchLikeType() )
192 lmo=((MapItem*)ti)->getLMO();
193 if (lmo) setScrollBarPosTarget (lmo->getBBox() );
197 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
199 // Code copied from Qt sources
203 qreal width = viewport()->width();
204 qreal height = viewport()->height();
205 QRectF viewRect = matrix().mapRect(rect);
207 qreal left = horizontalScrollBar()->value();
208 qreal right = left + width;
209 qreal top = verticalScrollBar()->value();
210 qreal bottom = top + height;
212 if (viewRect.left() <= left + xmargin) {
213 // need to scroll from the left
214 // if (!d->leftIndent)
215 scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
217 if (viewRect.right() >= right - xmargin) {
218 // need to scroll from the right
219 // if (!d->leftIndent)
220 scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
222 if (viewRect.top() <= top + ymargin) {
223 // need to scroll from the top
224 // if (!d->topIndent)
225 scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
227 if (viewRect.bottom() >= bottom - ymargin) {
228 // need to scroll from the bottom
229 // if (!d->topIndent)
230 scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
233 if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
234 scrollBarPosAnimation.stop();
235 scrollBarPosAnimation.setTargetObject (this);
236 scrollBarPosAnimation.setPropertyName ("scrollBarPos");
237 scrollBarPosAnimation.setDuration(1000);
238 scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
239 scrollBarPosAnimation.setStartValue(
240 QPointF (horizontalScrollBar()->value() ,
241 verticalScrollBar()->value() ) );
242 scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
243 scrollBarPosAnimation.start();
246 QPointF MapEditor::getScrollBarPosTarget()
248 return scrollBarPosTarget;
252 void MapEditor::setScrollBarPos(const QPointF &p)
255 horizontalScrollBar()->setValue(int(p.x()));
256 verticalScrollBar()->setValue(int(p.y()));
259 QPointF MapEditor::getScrollBarPos()
264 void MapEditor::setZoomFactorTarget (const qreal &zft)
266 zoomFactorTarget=zft;
267 if (zoomAnimation.state()==QtAbstractAnimation::Running)
268 zoomAnimation.stop();
269 //zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
270 zoomAnimation.setTargetObject (this);
271 zoomAnimation.setPropertyName ("zoomFactor");
272 zoomAnimation.setDuration(1000);
273 zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
274 zoomAnimation.setStartValue(zoomFactor);
275 zoomAnimation.setEndValue(zft);
276 zoomAnimation.start();
279 qreal MapEditor::getZoomFactorTarget()
281 return zoomFactorTarget;
285 void MapEditor::setZoomFactor(const qreal &zf)
288 setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
291 qreal MapEditor::getZoomFactor()
296 void MapEditor::print()
300 printer = new QPrinter;
301 printer->setColorMode (QPrinter::Color);
302 printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
303 printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
304 printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
307 QRectF totalBBox=getTotalBBox();
309 // Try to set orientation automagically
310 // Note: Interpretation of generated postscript is amibiguous, if
311 // there are problems with landscape mode, see
312 // http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
314 if (totalBBox.width()>totalBBox.height())
315 // recommend landscape
316 printer->setOrientation (QPrinter::Landscape);
318 // recommend portrait
319 printer->setOrientation (QPrinter::Portrait);
321 if ( printer->setup(this) )
322 // returns false, if printing is canceled
324 QPainter pp(printer);
326 pp.setRenderHint(QPainter::Antialiasing,true);
328 // Don't print the visualisation of selection
331 QRectF mapRect=totalBBox;
332 QGraphicsRectItem *frame=NULL;
336 // Print frame around map
337 mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10,
338 totalBBox.width()+20, totalBBox.height()+20);
339 frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
345 double paperAspect = (double)printer->width() / (double)printer->height();
346 double mapAspect = (double)mapRect.width() / (double)mapRect.height();
348 if (mapAspect>=paperAspect)
350 // Fit horizontally to paper width
351 //pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );
352 viewBottom=(int)(printer->width()/mapAspect);
355 // Fit vertically to paper height
356 //pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());
357 viewBottom=printer->height();
362 // Print footer below map
364 font.setPointSize(10);
366 QRectF footerBox(0,viewBottom,printer->width(),15);
367 // FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
368 pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
372 QRectF (0,0,printer->width(),printer->height()-15),
373 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
376 // Viewport has paper dimension
377 if (frame) delete (frame);
382 // Save settings in vymrc
383 settings.writeEntry("/mainwindow/printerName",printer->printerName());
384 settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
385 settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
389 QRectF MapEditor::getTotalBBox() //FIXME-2 needed e.g. for image export
393 for (int i=0;i<rootItem->branchCount(); i++)
394 r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
400 QPixmap MapEditor::getPixmap()
402 QRectF mapRect=getTotalBBox();
403 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
406 pp.setRenderHints(renderHints());
408 // Don't print the visualisation of selection
411 mapScene->render ( &pp,
412 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
413 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
422 void MapEditor::setAntiAlias (bool b)
424 setRenderHint(QPainter::Antialiasing,b);
427 void MapEditor::setSmoothPixmap(bool b)
429 setRenderHint(QPainter::SmoothPixmapTransform,b);
432 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
434 // Start with mapcenter, no images allowed at rootItem
436 BranchItem *bi=model->getRootItem()->getFirstBranch();
437 TreeItem *found=NULL;
440 found=bi->findMapItem (p, exclude);
441 if (found) return found;
443 bi=model->getRootItem()->getBranchNum(i);
448 AttributeTable* MapEditor::attributeTable()
453 void MapEditor::testFunction1()
456 // Code copied from Qt sources
457 QRectF rect=model->getSelectedBranchObj()->getBBox();
461 qreal width = viewport()->width();
462 qreal height = viewport()->height();
463 QRectF viewRect = matrix().mapRect(rect);
465 qreal left = horizontalScrollBar()->value();
466 qreal right = left + width;
467 qreal top = verticalScrollBar()->value();
468 qreal bottom = top + height;
470 if (viewRect.left() <= left + xmargin) {
471 // need to scroll from the left
472 // if (!d->leftIndent)
473 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
475 if (viewRect.right() >= right - xmargin) {
476 // need to scroll from the right
477 // if (!d->leftIndent)
478 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
480 if (viewRect.top() <= top + ymargin) {
481 // need to scroll from the top
482 // if (!d->topIndent)
483 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
485 if (viewRect.bottom() >= bottom - ymargin) {
486 // need to scroll from the bottom
487 // if (!d->topIndent)
488 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
490 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
491 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
496 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
497 animation->setDuration(5000);
498 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
499 animation->setEasingCurve ( QtEasingCurve::OutQuint);
500 animation->setStartValue(sceneRect() );
501 animation->setEndValue(QRectF(50, 50, 1000, 1000));
506 QDialog *dia= new QDialog (this);
507 dia->setGeometry (50,50,10,10);
512 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
513 animation->setDuration(1000);
514 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
515 animation->setEasingCurve ( QtEasingCurve::OutQuint);
516 animation->setStartValue(QRect(50, 50, 10, 10));
517 animation->setEndValue(QRect(250, 250, 100, 100));
522 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
523 if (hidemode==HideNone)
525 setHideTmpMode (HideExport);
526 mapCenter->calcBBoxSizeWithChilds();
527 QRectF totalBBox=mapCenter->getTotalBBox();
528 QRectF mapRect=totalBBox;
529 QCanvasRectangle *frame=NULL;
531 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
533 mapRect.setRect (totalBBox.x(), totalBBox.y(),
534 totalBBox.width(), totalBBox.height());
535 frame=new QCanvasRectangle (mapRect,mapScene);
536 frame->setBrush (QColor(white));
537 frame->setPen (QColor(black));
543 setHideTmpMode (HideNone);
545 cout <<" hidemode="<<hidemode<<endl;
549 void MapEditor::testFunction2()
554 if (hidemode==HideExport)
555 setHideTmpMode (HideNone);
557 setHideTmpMode (HideExport);
561 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
566 if (i>0) return bi->parent()->getBranchNum(i-1);
571 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
575 int dz=selbi->depth(); // original depth
577 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
582 // Look for branch with same parent but directly above
584 bi=getBranchDirectBelow(selbi);
586 bi=getBranchDirectAbove (selbi);
589 // direct predecessor
592 // Go towards center and look for predecessor
593 while (selbi->depth()>0)
595 selbi=(BranchItem*)(selbi->parent());
596 if (selbi->depth()==1 && invert)
597 bi=getBranchDirectBelow (selbi);
599 bi=getBranchDirectAbove (selbi);
604 while (selbi->depth()<dz)
606 // try to get back to original depth dz
607 bi=selbi->getLastBranch();
621 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
626 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
631 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
636 int dz=selbi->depth(); // original depth
638 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
642 // Look for branch with same parent but directly below
644 bi=getBranchDirectAbove (selbi);
646 bi=getBranchDirectBelow (selbi);
652 // Go towards center and look for neighbour
653 while (selbi->depth()>0)
655 selbi=(BranchItem*)(selbi->parent());
656 if (selbi->depth()==1 && invert)
657 bi=getBranchDirectAbove (selbi);
659 bi=getBranchDirectBelow (selbi);
664 while (selbi->depth()<dz)
666 // try to get back to original depth dz
667 bi=selbi->getFirstBranch();
681 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
686 // Special case: use alternative selection index
687 return bi->getLastSelectedBranchAlt();
688 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
690 return (BranchItem*)(bi->parent());
693 if (bi->getType()== TreeItem::Branch )
694 return bi->getLastSelectedBranch();
699 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
703 if (bi->depth()==0) return bi->getLastSelectedBranch();
704 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
706 return (BranchItem*)(bi->parent());
709 if (bi->getType()== TreeItem::Branch )
710 return (BranchItem*)bi->getLastSelectedBranch();
717 void MapEditor::cursorUp()
719 BranchItem *bi=model->getSelectedBranch();
720 if (bi) model->select (getBranchAbove(bi));
723 void MapEditor::cursorDown()
726 BranchItem *bi=model->getSelectedBranch();
727 if (bi) model->select (getBranchBelow(bi));
730 void MapEditor::cursorLeft()
732 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
733 if (bi) model->select (bi);
736 void MapEditor::cursorRight()
738 BranchItem *bi=getRightBranch (model->getSelectedBranch());
739 if (bi) model->select (bi);
742 void MapEditor::cursorFirst()
744 model->selectFirstBranch();
747 void MapEditor::cursorLast()
749 model->selectLastBranch();
753 void MapEditor::editHeading()
757 editHeadingFinished();
760 BranchObj *bo=model->getSelectedBranchObj();
761 BranchItem *bi=model->getSelectedBranch();
764 model->setSelectionBlocked(true);
766 lineEdit->setText (bi->getHeading());
767 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
768 lineEdit->setGeometry(p.x(),p.y(),230,25);
769 lineEdit->selectAll();
771 lineEdit->setFocus();
772 lineEdit->grabKeyboard();
777 void MapEditor::editHeadingFinished()
779 editingHeading=false;
780 lineEdit->releaseKeyboard();
781 model->setHeading (lineEdit->text() );
782 model->setSelectionBlocked(false);
785 // Maybe reselect previous branch
786 mainWindow->editHeadingFinished (model);
790 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
792 // Lineedits are already closed by preceding
793 // mouseEvent, we don't need to close here.
795 QPointF p = mapToScene(e->pos());
796 TreeItem *ti=findMapItem (p, NULL);
797 LinkableMapObj* lmo=NULL;
798 if (ti) lmo=((MapItem*)ti)->getLMO();
801 { // MapObj was found
802 if (model->getSelectedLMO() != lmo)
808 if (model->getSelectedBranchObj() )
810 // Context Menu on branch or mapcenter
811 //FIXME-3 model->updateActions(); needed?
812 branchContextMenu->popup(e->globalPos() );
815 if (model->getSelectedImage() )
817 // Context Menu on floatimage
818 // model->updateActions(); FIXME-3 needed?
819 floatimageContextMenu->popup(e->globalPos() );
823 { // No MapObj found, we are on the Canvas itself
824 // Context Menu on scene
825 // model->updateActions(); FIXME-3 needed?
827 // Open context menu synchronously to position new mapcenter
828 model->setContextPos (p);
829 canvasContextMenu->exec(e->globalPos() );
830 model->unsetContextPos ();
835 void MapEditor::keyPressEvent(QKeyEvent* e)
837 if (e->modifiers() & Qt::ControlModifier)
839 switch (mainWindow->getModMode())
841 case Main::ModModeColor:
842 setCursor (PickColorCursor);
844 case Main::ModModeCopy:
845 setCursor (CopyCursor);
847 case Main::ModModeXLink:
848 setCursor (XLinkCursor);
851 setCursor (Qt::ArrowCursor);
857 void MapEditor::keyReleaseEvent(QKeyEvent* e)
859 if (!(e->modifiers() & Qt::ControlModifier))
860 setCursor (Qt::ArrowCursor);
863 void MapEditor::mousePressEvent(QMouseEvent* e)
865 // Ignore right clicks, these will go to context menus
866 if (e->button() == Qt::RightButton )
872 //Ignore clicks while editing heading
873 if (model->isSelectionBlocked() )
879 QPointF p = mapToScene(e->pos());
880 TreeItem *ti=findMapItem (p, NULL);
881 LinkableMapObj* lmo=NULL;
882 if (ti) lmo=((MapItem*)ti)->getLMO();
886 //Take care of system flags _or_ modifier modes
888 if (lmo && ti->isBranchLikeType() )
890 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
891 if (!foname.isEmpty())
893 // systemFlag clicked
894 model->select (lmo); // FIXME-3 was selectInt
897 if (e->state() & Qt::ControlModifier)
898 mainWindow->editOpenURLTab();
900 mainWindow->editOpenURL();
902 else if (foname=="system-vymLink")
904 mainWindow->editOpenVymLink();
905 // tabWidget may change, better return now
906 // before segfaulting...
907 } else if (foname=="note")
908 mainWindow->windowToggleNoteEditor();
909 else if (foname=="hideInExport")
910 model->toggleHideExport();
911 // FIXME-3 needed? xelection.update();
915 // No system flag clicked, take care of modmodes (CTRL-Click)
916 if (e->state() & Qt::ControlModifier)
918 if (mainWindow->getModMode()==Main::ModModeColor)
921 setCursor (PickColorCursor);
924 if (mainWindow->getModMode()==Main::ModModeXLink)
926 BranchObj *bo_begin=NULL;
928 bo_begin=(BranchObj*)(lmo);
930 bo_begin=model->getSelectedBranchObj();
934 linkingObj_src=bo_begin;
935 tmpXLink=new XLinkObj (mapScene);
936 tmpXLink->setBegin (bo_begin);
937 tmpXLink->setEnd (p);
938 tmpXLink->setColor(model->getMapDefXLinkColor());
939 tmpXLink->setWidth(model->getMapDefXLinkWidth());
940 tmpXLink->updateXLink();
941 tmpXLink->setVisibility (true);
950 cout << "ME::mouse pressed\n";
951 cout << " lmo="<<lmo<<endl;
952 cout << " ti="<<ti->getHeadingStd()<<endl;
954 // Select the clicked object
959 // Left Button Move Branches
960 if (e->button() == Qt::LeftButton )
962 movingObj_start.setX( p.x() - lmo->x() );
963 movingObj_start.setY( p.y() - lmo->y() );
964 movingObj_orgPos.setX (lmo->x() );
965 movingObj_orgPos.setY (lmo->y() );
967 movingObj_orgRelPos=lmo->getRelPos();
969 // If modMode==copy, then we want to "move" the _new_ object around
970 // then we need the offset from p to the _old_ selection, because of tmp
971 if (mainWindow->getModMode()==Main::ModModeCopy &&
972 e->state() & Qt::ControlModifier)
974 BranchItem *bi=model->getSelectedBranch();
978 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
979 //bi->addBranch (model->getSelectedBranch());
981 model->select(bi->getLastBranch());
986 movingObj=model->getSelectedLMO();
988 // Middle Button Toggle Scroll
989 // (On Mac OS X this won't work, but we still have
990 // a button in the toolbar)
991 if (e->button() == Qt::MidButton )
992 model->toggleScroll();
993 // model->updateActions(); FIXME-3 needed?
994 // FIXME-3 needed? xelection.update();
996 { // No MapObj found, we are on the scene itself
997 // Left Button move Pos of sceneView
998 if (e->button() == Qt::LeftButton )
1000 movingObj=NULL; // move Content not Obj
1001 movingObj_start=e->globalPos();
1002 movingCont_start=QPointF (
1003 horizontalScrollBar()->value(),
1004 verticalScrollBar()->value());
1005 movingVec=QPointF(0,0);
1006 setCursor(HandOpenCursor);
1011 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1013 QPointF p = mapToScene(e->pos());
1014 TreeItem *seli=model->getSelectedItem();
1015 LinkableMapObj* lmosel=NULL;
1016 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1017 lmosel=((MapItem*)seli)->getLMO();
1019 // Move the selected MapObj
1020 if ( lmosel && movingObj)
1022 // reset cursor if we are moving and don't copy
1023 if (mainWindow->getModMode()!=Main::ModModeCopy)
1024 setCursor (Qt::ArrowCursor);
1026 // To avoid jumping of the sceneView, only
1027 // show selection, if not tmp linked
1028 if (!lmosel->hasParObjTmp())
1029 model->emitShowSelection();
1031 // Now move the selection, but add relative position
1032 // (movingObj_start) where selection was chosen with
1033 // mousepointer. (This avoids flickering resp. jumping
1034 // of selection back to absPos)
1036 // Check if we could link
1037 TreeItem *ti=findMapItem (p, seli);
1038 BranchItem *dsti=NULL;
1039 LinkableMapObj* dst=NULL;
1040 if (ti && ti!=seli && ti->isBranchLikeType())
1042 dsti=(BranchItem*)ti;
1048 if (lmosel && seli->getType()==TreeItem::Image)
1050 FloatObj *fio=(FloatImageObj*)lmosel;
1051 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1053 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1054 model->emitSelectionChanged(); // position has changed
1056 // Relink float to new mapcenter or branch, if shift is pressed
1057 // Only relink, if selection really has a new parent
1058 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1060 // Also save the move which was done so far
1061 QString pold=qpointfToString(movingObj_orgRelPos);
1062 QString pnow=qpointfToString(fio->getRelPos());
1068 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1069 fio->getParObj()->requestReposition();
1070 model->reposition();
1072 model->relinkImage ((ImageItem*) seli,dsti);
1073 model->select (seli);
1074 //movingObj=lmosel; //FIXME-3
1075 //movingObj_orgRelPos=lmosel->getRelPos();
1077 model->reposition();
1080 { // selection != a FloatObj
1081 if (seli->depth()==0) //FIXME-1 also moved mapcenters could be linked, but not working here...
1084 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1085 ((BranchObj*)lmosel)->moveBy(
1086 QPointF(p.x() -movingObj_start.x(),
1087 p.y()-movingObj_start.y()) );
1089 lmosel->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1090 model->updateRelPositions();
1093 if (seli->depth()==1)
1096 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1097 lmosel->setRelPos();
1100 // Move ordinary branch
1101 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1102 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1103 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1104 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1106 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1107 lmosel->setRelPos();
1110 // Maybe we can relink temporary?
1113 if (e->modifiers()==Qt::ControlModifier)
1115 // Special case: CTRL to link below dst
1116 lmosel->setParObjTmp (dst,p,+1);
1118 else if (e->modifiers()==Qt::ShiftModifier)
1119 lmosel->setParObjTmp (dst,p,-1);
1121 lmosel->setParObjTmp (dst,p,0);
1124 lmosel->unsetParObjTmp();
1126 // reposition subbranch
1127 lmosel->reposition();
1130 QItemSelection sel=model->getSelectionModel()->selection();
1131 updateSelection(sel,sel); // position has changed
1133 } // no FloatImageObj
1137 } // selection && moving_obj
1139 // Draw a link from one branch to another
1142 tmpXLink->setEnd (p);
1143 tmpXLink->updateXLink();
1147 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1149 QPointF p=e->globalPos();
1150 movingVec.setX(-p.x() + movingObj_start.x() );
1151 movingVec.setY(-p.y() + movingObj_start.y() );
1152 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1153 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1158 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1160 QPointF p = mapToScene(e->pos());
1161 TreeItem *seli=model->getSelectedItem();
1163 TreeItem *dsti=NULL;
1164 if (seli) dsti=findMapItem(p, seli);
1165 LinkableMapObj* dst=NULL;
1166 if (dsti && dsti->isBranchLikeType ())
1167 dst=((MapItem*)dsti)->getLMO();
1172 // Have we been picking color?
1176 setCursor (Qt::ArrowCursor);
1177 // Check if we are over another branch
1180 if (e->state() & Qt::ShiftModifier)
1181 model->colorBranch (((BranchObj*)dst)->getColor());
1183 model->colorSubtree (((BranchObj*)dst)->getColor());
1188 // Have we been drawing a link?
1192 // Check if we are over another branch
1195 tmpXLink->setEnd ( ((BranchObj*)(dst)) );
1196 tmpXLink->updateXLink();
1197 tmpXLink->activate(); //FIXME-2 savestate missing
1198 //model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );
1207 // Have we been moving something?
1208 if ( seli && movingObj )
1210 if (seli->getType()==TreeItem::Image)
1212 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1215 // Moved FloatObj. Maybe we need to reposition
1216 QString pold=qpointfToString(movingObj_orgRelPos);
1217 QString pnow=qpointfToString(fio->getRelPos());
1223 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1225 cout << "ME::release mouse\n";
1226 fio->getParObj()->requestReposition();
1227 model->reposition();
1231 BranchItem *bi=model->getSelectedBranch();
1232 if (bi && bi->depth()==0)
1234 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1236 QString pold=qpointfToString(movingObj_orgPos);
1237 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1244 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1248 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1249 { // A branch was moved
1250 LinkableMapObj* lmosel=NULL;
1251 lmosel=((MapItem*)seli)->getLMO();
1253 // save the position in case we link to mapcenter
1254 QPointF savePos=QPointF (lmosel->getAbsPos() );
1256 // Reset the temporary drawn link to the original one
1257 lmosel->unsetParObjTmp();
1259 // For Redo we may need to save original selection
1260 QString preSelStr=model->getSelectString(seli);
1265 // We have a destination, relink to that
1267 BranchObj* bsel=model->getSelectedBranchObj();
1269 QString preParStr=model->getSelectString (bsel->getParObj());
1270 QString preNum=QString::number (seli->num(),10);
1271 QString preDstParStr;
1274 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1276 preDstParStr=model->getSelectString (dst->getParObj());
1277 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1279 if (e->state() & Qt::ControlModifier && dst->getParObj())
1282 preDstParStr=model->getSelectString (dst->getParObj());
1283 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1286 preDstParStr=model->getSelectString(dst);
1287 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1288 if (dsti->depth()==0) bsel->move (savePos);
1292 QString postSelStr=model->getSelectString(lmosel);
1293 QString postNum=QString::number (seli->num(),10);
1295 QString undoCom="relinkTo (\""+
1296 preParStr+ "\"," + preNum +"," +
1297 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1299 QString redoCom="relinkTo (\""+
1300 preDstParStr + "\"," + postNum + "," +
1301 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1306 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1311 // No destination, undo temporary move
1313 if (seli->depth()==1)
1315 // The select string might be different _after_ moving around.
1316 // Therefor reposition and then use string of old selection, too
1317 model->reposition();
1319 QPointF rp(lmosel->getRelPos());
1320 if (rp != movingObj_orgRelPos)
1322 QString ps=qpointfToString(rp);
1324 model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos),
1325 preSelStr, "moveRel "+ps,
1326 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1330 // Draw the original link, before selection was moved around
1331 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1333 lmosel->setRelPos(); // calc relPos first for starting point
1334 QPointF dst=bi->getBranchObj()->getParObj()->getChildPos(); // FIXME-3 check getBO here...
1335 // if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
1337 model->startAnimation(
1339 lmosel->getRelPos(),
1341 // QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
1344 model->reposition();
1347 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1348 // Finally resize scene, if needed
1352 // Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
1353 // model->updateActions(); FIXME-3 neeeded?
1355 // maybe we moved View: set old cursor
1356 setCursor (Qt::ArrowCursor);
1360 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1362 if (model->isSelectionBlocked() )
1368 if (e->button() == Qt::LeftButton )
1370 QPointF p = mapToScene(e->pos());
1371 TreeItem *ti=findMapItem (p, NULL);
1372 if (ti) { // MapObj was found
1373 // First select the MapObj than edit heading
1380 void MapEditor::resizeEvent (QResizeEvent* e)
1382 QGraphicsView::resizeEvent( e );
1385 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1387 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1388 // cerr << event->format(i) << endl;
1390 if (event->mimeData()->hasImage())
1391 event->acceptProposedAction();
1393 if (event->mimeData()->hasUrls())
1394 event->acceptProposedAction();
1397 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1401 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1406 void MapEditor::dropEvent(QDropEvent *event)
1408 BranchItem *selbi=model->getSelectedBranch();
1412 foreach (QString format,event->mimeData()->formats())
1413 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1417 if (event->mimeData()->hasImage())
1419 QVariant imageData = event->mimeData()->imageData();
1420 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1422 if (event->mimeData()->hasUrls())
1423 uris=event->mimeData()->urls();
1431 for (int i=0; i<uris.count();i++)
1433 // Workaround to avoid adding empty branches
1434 if (!uris.at(i).toString().isEmpty())
1436 bi=model->addNewBranch();
1440 s=uris.at(i).toLocalFile();
1443 QString file = QDir::fromNativeSeparators(s);
1444 heading = QFileInfo(file).baseName();
1446 if (file.endsWith(".vym", false))
1447 bi->setVymLink(file);
1449 bi->setURL(uris.at(i).toString());
1452 bo->setURL(uris.at(i).toString());
1456 if (!heading.isEmpty())
1457 bi->setHeading(heading);
1459 bi->setHeading(uris.at(i).toString());
1464 model->reposition();
1467 event->acceptProposedAction();
1470 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1472 // Note: Here we are prepared for multiple selections, though this
1473 // is not yet implemented elsewhere
1475 // Here in MapEditor we can only select Branches and Images
1476 QList <TreeItem*> treeItemsNew;
1477 QList <TreeItem*> treeItemsOld;
1480 foreach (ix,newsel.indexes() )
1482 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1483 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1484 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1486 foreach (ix,oldsel.indexes() )
1488 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1489 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1490 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1493 // Trim list of selection rectangles
1494 while (treeItemsNew.count() < selboxList.count() )
1495 delete selboxList.takeFirst();
1497 // Take care to tmp scroll/unscroll
1498 if (!oldsel.isEmpty())
1500 QModelIndex ix=oldsel.indexes().first();
1503 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1506 if (ti->isBranchLikeType() )
1508 // reset tmp scrolled branches
1509 BranchItem *bi=(BranchItem*)ti;
1510 bi->resetTmpUnscroll();
1512 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1513 // Hide link if not needed
1514 ((MapItem*)ti)->getLMO()->updateVisibility();
1519 if (!treeItemsNew.isEmpty())
1521 QModelIndex ix=newsel.indexes().first();
1524 // Temporary unscroll if necessary
1525 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1526 if (ti->isBranchLikeType() )
1528 BranchItem *bi=(BranchItem*)ti;
1529 if (bi->hasScrolledParent(bi) )
1533 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1534 // Show link if needed
1535 ((MapItem*)ti)->getLMO()->updateVisibility();
1539 // Reduce rectangles
1540 while (treeItemsNew.count() < selboxList.count() )
1541 delete selboxList.takeFirst();
1543 // Add additonal rectangles
1544 QGraphicsRectItem *sb;
1545 while (treeItemsNew.count() > selboxList.count() )
1547 sb = mapScene->addRect(
1549 QPen(selectionColor),
1551 sb->setZValue(Z_SELBOX);
1553 selboxList.append (sb);
1558 // Reposition rectangles
1562 LinkableMapObj *lmo;
1563 for (int i=0; i<treeItemsNew.count();++i)
1565 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1566 bbox=lmo->getBBox();
1567 sb=selboxList.at(i);
1570 bbox.width(), bbox.height());
1571 sb->setPen (selectionColor);
1572 sb->setBrush (selectionColor);
1579 void MapEditor::updateData (const QModelIndex &sel)
1581 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1584 cout << "ME::updateData\n";
1586 cout << " ti="<<ti<<endl;
1587 cout << " h="<<ti->getHeading().toStdString()<<endl;
1590 if (ti->isBranchLikeType())
1592 // cout << " ->updating...\n";
1593 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1598 void MapEditor::setSelectionColor (QColor col)
1601 QItemSelection sel=model->getSelectionModel()->selection();
1602 updateSelection(sel,sel);
1606 QColor MapEditor::getSelectionColor ()
1608 return selectionColor;