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;
494 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
495 animation->setDuration(5000);
496 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
497 animation->setEasingCurve ( QtEasingCurve::OutQuint);
498 animation->setStartValue(sceneRect() );
499 animation->setEndValue(QRectF(50, 50, 1000, 1000));
504 QDialog *dia= new QDialog (this);
505 dia->setGeometry (50,50,10,10);
510 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
511 animation->setDuration(1000);
512 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
513 animation->setEasingCurve ( QtEasingCurve::OutQuint);
514 animation->setStartValue(QRect(50, 50, 10, 10));
515 animation->setEndValue(QRect(250, 250, 100, 100));
520 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
521 if (hidemode==HideNone)
523 setHideTmpMode (HideExport);
524 mapCenter->calcBBoxSizeWithChilds();
525 QRectF totalBBox=mapCenter->getTotalBBox();
526 QRectF mapRect=totalBBox;
527 QCanvasRectangle *frame=NULL;
529 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
531 mapRect.setRect (totalBBox.x(), totalBBox.y(),
532 totalBBox.width(), totalBBox.height());
533 frame=new QCanvasRectangle (mapRect,mapScene);
534 frame->setBrush (QColor(white));
535 frame->setPen (QColor(black));
541 setHideTmpMode (HideNone);
543 cout <<" hidemode="<<hidemode<<endl;
546 void MapEditor::testFunction2()
551 if (hidemode==HideExport)
552 setHideTmpMode (HideNone);
554 setHideTmpMode (HideExport);
558 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
563 if (i>0) return bi->parent()->getBranchNum(i-1);
568 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
572 int dz=selbi->depth(); // original depth
574 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
579 // Look for branch with same parent but directly above
581 bi=getBranchDirectBelow(selbi);
583 bi=getBranchDirectAbove (selbi);
586 // direct predecessor
589 // Go towards center and look for predecessor
590 while (selbi->depth()>0)
592 selbi=(BranchItem*)(selbi->parent());
593 if (selbi->depth()==1 && invert)
594 bi=getBranchDirectBelow (selbi);
596 bi=getBranchDirectAbove (selbi);
601 while (selbi->depth()<dz)
603 // try to get back to original depth dz
604 bi=selbi->getLastBranch();
618 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
623 if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
628 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
633 int dz=selbi->depth(); // original depth
635 if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
639 // Look for branch with same parent but directly below
641 bi=getBranchDirectAbove (selbi);
643 bi=getBranchDirectBelow (selbi);
649 // Go towards center and look for neighbour
650 while (selbi->depth()>0)
652 selbi=(BranchItem*)(selbi->parent());
653 if (selbi->depth()==1 && invert)
654 bi=getBranchDirectAbove (selbi);
656 bi=getBranchDirectBelow (selbi);
661 while (selbi->depth()<dz)
663 // try to get back to original depth dz
664 bi=selbi->getFirstBranch();
678 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
683 // Special case: use alternative selection index
684 return bi->getLastSelectedBranchAlt();
685 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)
687 return (BranchItem*)(bi->parent());
690 if (bi->getType()== TreeItem::Branch )
691 return bi->getLastSelectedBranch();
696 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
700 if (bi->depth()==0) return bi->getLastSelectedBranch();
701 if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)
703 return (BranchItem*)(bi->parent());
706 if (bi->getType()== TreeItem::Branch )
707 return (BranchItem*)bi->getLastSelectedBranch();
714 void MapEditor::cursorUp()
716 BranchItem *bi=model->getSelectedBranch();
717 if (bi) model->select (getBranchAbove(bi));
720 void MapEditor::cursorDown()
723 BranchItem *bi=model->getSelectedBranch();
724 if (bi) model->select (getBranchBelow(bi));
727 void MapEditor::cursorLeft()
729 BranchItem *bi=getLeftBranch (model->getSelectedBranch());
730 if (bi) model->select (bi);
733 void MapEditor::cursorRight()
735 BranchItem *bi=getRightBranch (model->getSelectedBranch());
736 if (bi) model->select (bi);
739 void MapEditor::cursorFirst()
741 model->selectFirstBranch();
744 void MapEditor::cursorLast()
746 model->selectLastBranch();
750 void MapEditor::editHeading()
754 editHeadingFinished();
757 BranchObj *bo=model->getSelectedBranchObj();
758 BranchItem *bi=model->getSelectedBranch();
761 model->setSelectionBlocked(true);
763 lineEdit->setText (bi->getHeading());
764 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
765 lineEdit->setGeometry(p.x(),p.y(),230,25);
766 lineEdit->selectAll();
768 lineEdit->setFocus();
769 lineEdit->grabKeyboard();
774 void MapEditor::editHeadingFinished()
776 editingHeading=false;
777 lineEdit->releaseKeyboard();
778 model->setHeading (lineEdit->text() );
779 model->setSelectionBlocked(false);
782 // Maybe reselect previous branch
783 mainWindow->editHeadingFinished (model);
787 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
789 // Lineedits are already closed by preceding
790 // mouseEvent, we don't need to close here.
792 QPointF p = mapToScene(e->pos());
793 TreeItem *ti=findMapItem (p, NULL);
794 LinkableMapObj* lmo=NULL;
795 if (ti) lmo=((MapItem*)ti)->getLMO();
798 { // MapObj was found
799 if (model->getSelectedLMO() != lmo)
805 if (model->getSelectedBranchObj() )
807 // Context Menu on branch or mapcenter
808 //FIXME-3 model->updateActions(); needed?
809 branchContextMenu->popup(e->globalPos() );
812 if (model->getSelectedImage() )
814 // Context Menu on floatimage
815 // model->updateActions(); FIXME-3 needed?
816 floatimageContextMenu->popup(e->globalPos() );
820 { // No MapObj found, we are on the Canvas itself
821 // Context Menu on scene
822 // model->updateActions(); FIXME-3 needed?
824 // Open context menu synchronously to position new mapcenter
825 model->setContextPos (p);
826 canvasContextMenu->exec(e->globalPos() );
827 model->unsetContextPos ();
832 void MapEditor::keyPressEvent(QKeyEvent* e)
834 if (e->modifiers() & Qt::ControlModifier)
836 switch (mainWindow->getModMode())
838 case Main::ModModeColor:
839 setCursor (PickColorCursor);
841 case Main::ModModeCopy:
842 setCursor (CopyCursor);
844 case Main::ModModeXLink:
845 setCursor (XLinkCursor);
848 setCursor (Qt::ArrowCursor);
854 void MapEditor::keyReleaseEvent(QKeyEvent* e)
856 if (!(e->modifiers() & Qt::ControlModifier))
857 setCursor (Qt::ArrowCursor);
860 void MapEditor::mousePressEvent(QMouseEvent* e)
862 // Ignore right clicks, these will go to context menus
863 if (e->button() == Qt::RightButton )
869 //Ignore clicks while editing heading
870 if (model->isSelectionBlocked() )
876 QPointF p = mapToScene(e->pos());
877 TreeItem *ti=findMapItem (p, NULL);
878 LinkableMapObj* lmo=NULL;
879 if (ti) lmo=((MapItem*)ti)->getLMO();
883 //Take care of system flags _or_ modifier modes
885 if (lmo && ti->isBranchLikeType() )
887 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
888 if (!foname.isEmpty())
890 // systemFlag clicked
891 model->select (lmo); // FIXME-3 was selectInt
894 if (e->state() & Qt::ControlModifier)
895 mainWindow->editOpenURLTab();
897 mainWindow->editOpenURL();
899 else if (foname=="system-vymLink")
901 mainWindow->editOpenVymLink();
902 // tabWidget may change, better return now
903 // before segfaulting...
904 } else if (foname=="note")
905 mainWindow->windowToggleNoteEditor();
906 else if (foname=="hideInExport")
907 model->toggleHideExport();
908 // FIXME-3 needed? xelection.update();
912 // No system flag clicked, take care of modmodes (CTRL-Click)
913 if (e->state() & Qt::ControlModifier)
915 if (mainWindow->getModMode()==Main::ModModeColor)
918 setCursor (PickColorCursor);
921 if (mainWindow->getModMode()==Main::ModModeXLink)
923 BranchObj *bo_begin=NULL;
925 bo_begin=(BranchObj*)(lmo);
927 bo_begin=model->getSelectedBranchObj();
931 linkingObj_src=bo_begin;
932 tmpXLink=new XLinkObj (mapScene);
933 tmpXLink->setBegin (bo_begin);
934 tmpXLink->setEnd (p);
935 tmpXLink->setColor(model->getMapDefXLinkColor());
936 tmpXLink->setWidth(model->getMapDefXLinkWidth());
937 tmpXLink->updateXLink();
938 tmpXLink->setVisibility (true);
947 cout << "ME::mouse pressed\n";
948 cout << " lmo="<<lmo<<endl;
949 cout << " ti="<<ti->getHeadingStd()<<endl;
951 // Select the clicked object
956 // Left Button Move Branches
957 if (e->button() == Qt::LeftButton )
959 movingObj_start.setX( p.x() - lmo->x() );
960 movingObj_start.setY( p.y() - lmo->y() );
961 movingObj_orgPos.setX (lmo->x() );
962 movingObj_orgPos.setY (lmo->y() );
964 movingObj_orgRelPos=lmo->getRelPos();
966 // If modMode==copy, then we want to "move" the _new_ object around
967 // then we need the offset from p to the _old_ selection, because of tmp
968 if (mainWindow->getModMode()==Main::ModModeCopy &&
969 e->state() & Qt::ControlModifier)
971 BranchItem *bi=model->getSelectedBranch();
975 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
976 //bi->addBranch (model->getSelectedBranch());
978 model->select(bi->getLastBranch());
983 movingObj=model->getSelectedLMO();
985 // Middle Button Toggle Scroll
986 // (On Mac OS X this won't work, but we still have
987 // a button in the toolbar)
988 if (e->button() == Qt::MidButton )
989 model->toggleScroll();
990 // model->updateActions(); FIXME-3 needed?
991 // FIXME-3 needed? xelection.update();
993 { // No MapObj found, we are on the scene itself
994 // Left Button move Pos of sceneView
995 if (e->button() == Qt::LeftButton )
997 movingObj=NULL; // move Content not Obj
998 movingObj_start=e->globalPos();
999 movingCont_start=QPointF (
1000 horizontalScrollBar()->value(),
1001 verticalScrollBar()->value());
1002 movingVec=QPointF(0,0);
1003 setCursor(HandOpenCursor);
1008 void MapEditor::mouseMoveEvent(QMouseEvent* e)
1010 QPointF p = mapToScene(e->pos());
1011 TreeItem *seli=model->getSelectedItem();
1012 LinkableMapObj* lmosel=NULL;
1013 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
1014 lmosel=((MapItem*)seli)->getLMO();
1016 // Move the selected MapObj
1017 if ( lmosel && movingObj)
1019 // reset cursor if we are moving and don't copy
1020 if (mainWindow->getModMode()!=Main::ModModeCopy)
1021 setCursor (Qt::ArrowCursor);
1023 // To avoid jumping of the sceneView, only
1024 // show selection, if not tmp linked
1025 if (!lmosel->hasParObjTmp())
1026 model->emitShowSelection();
1028 // Now move the selection, but add relative position
1029 // (movingObj_start) where selection was chosen with
1030 // mousepointer. (This avoids flickering resp. jumping
1031 // of selection back to absPos)
1033 // Check if we could link
1034 TreeItem *ti=findMapItem (p, seli);
1035 BranchItem *dsti=NULL;
1036 LinkableMapObj* dst=NULL;
1037 if (ti && ti!=seli && ti->isBranchLikeType())
1039 dsti=(BranchItem*)ti;
1045 if (lmosel && seli->getType()==TreeItem::Image)
1047 FloatObj *fio=(FloatImageObj*)lmosel;
1048 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1050 fio->updateLinkGeometry(); //no need for reposition, if we update link here
1051 model->emitSelectionChanged(); // position has changed
1053 // Relink float to new mapcenter or branch, if shift is pressed
1054 // Only relink, if selection really has a new parent
1055 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1057 // Also save the move which was done so far
1058 QString pold=qpointfToString(movingObj_orgRelPos);
1059 QString pnow=qpointfToString(fio->getRelPos());
1065 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
1066 fio->getParObj()->requestReposition();
1067 model->reposition();
1069 model->relinkImage ((ImageItem*) seli,dsti);
1070 model->select (seli);
1071 //movingObj=lmosel; //FIXME-3
1072 //movingObj_orgRelPos=lmosel->getRelPos();
1074 model->reposition();
1077 { // selection != a FloatObj
1078 if (seli->depth()==0) //FIXME-1 also moved mapcenters could be linked, but not working here...
1081 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
1082 ((BranchObj*)lmosel)->moveBy(
1083 QPointF(p.x() -movingObj_start.x(),
1084 p.y()-movingObj_start.y()) );
1086 lmosel->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1087 model->updateRelPositions();
1090 if (seli->depth()==1)
1093 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
1094 lmosel->setRelPos();
1097 // Move ordinary branch
1098 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
1099 // Add width of bbox here, otherwise alignRelTo will cause jumping around
1100 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
1101 p.y()-movingObj_start.y() +lmosel->getTopPad() );
1103 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
1104 lmosel->setRelPos();
1107 // Maybe we can relink temporary?
1110 if (e->modifiers()==Qt::ControlModifier)
1112 // Special case: CTRL to link below dst
1113 lmosel->setParObjTmp (dst,p,+1);
1115 else if (e->modifiers()==Qt::ShiftModifier)
1116 lmosel->setParObjTmp (dst,p,-1);
1118 lmosel->setParObjTmp (dst,p,0);
1121 lmosel->unsetParObjTmp();
1123 // reposition subbranch
1124 lmosel->reposition();
1127 QItemSelection sel=model->getSelectionModel()->selection();
1128 updateSelection(sel,sel); // position has changed
1130 } // no FloatImageObj
1134 } // selection && moving_obj
1136 // Draw a link from one branch to another
1139 tmpXLink->setEnd (p);
1140 tmpXLink->updateXLink();
1144 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
1146 QPointF p=e->globalPos();
1147 movingVec.setX(-p.x() + movingObj_start.x() );
1148 movingVec.setY(-p.y() + movingObj_start.y() );
1149 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
1150 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
1155 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
1157 QPointF p = mapToScene(e->pos());
1158 TreeItem *seli=model->getSelectedItem();
1160 TreeItem *dsti=NULL;
1161 if (seli) dsti=findMapItem(p, seli);
1162 LinkableMapObj* dst=NULL;
1163 if (dsti && dsti->isBranchLikeType ())
1164 dst=((MapItem*)dsti)->getLMO();
1169 // Have we been picking color?
1173 setCursor (Qt::ArrowCursor);
1174 // Check if we are over another branch
1177 if (e->state() & Qt::ShiftModifier)
1178 model->colorBranch (((BranchObj*)dst)->getColor());
1180 model->colorSubtree (((BranchObj*)dst)->getColor());
1185 // Have we been drawing a link?
1189 // Check if we are over another branch
1192 tmpXLink->setEnd ( ((BranchObj*)(dst)) );
1193 tmpXLink->updateXLink();
1194 tmpXLink->activate(); //FIXME-2 savestate missing
1195 //model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );
1204 // Have we been moving something?
1205 if ( seli && movingObj )
1207 if (seli->getType()==TreeItem::Image)
1209 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1212 // Moved FloatObj. Maybe we need to reposition
1213 QString pold=qpointfToString(movingObj_orgRelPos);
1214 QString pnow=qpointfToString(fio->getRelPos());
1220 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1222 cout << "ME::release mouse\n";
1223 fio->getParObj()->requestReposition();
1224 model->reposition();
1228 BranchItem *bi=model->getSelectedBranch();
1229 if (bi && bi->depth()==0)
1231 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1233 QString pold=qpointfToString(movingObj_orgPos);
1234 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1241 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1245 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1246 { // A branch was moved
1247 LinkableMapObj* lmosel=NULL;
1248 lmosel=((MapItem*)seli)->getLMO();
1250 // save the position in case we link to mapcenter
1251 QPointF savePos=QPointF (lmosel->getAbsPos() );
1253 // Reset the temporary drawn link to the original one
1254 lmosel->unsetParObjTmp();
1256 // For Redo we may need to save original selection
1257 QString preSelStr=model->getSelectString(seli);
1262 // We have a destination, relink to that
1264 BranchObj* bsel=model->getSelectedBranchObj();
1266 QString preParStr=model->getSelectString (bsel->getParObj());
1267 QString preNum=QString::number (seli->num(),10);
1268 QString preDstParStr;
1271 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1273 preDstParStr=model->getSelectString (dst->getParObj());
1274 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1276 if (e->state() & Qt::ControlModifier && dst->getParObj())
1279 preDstParStr=model->getSelectString (dst->getParObj());
1280 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1283 preDstParStr=model->getSelectString(dst);
1284 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1285 if (dsti->depth()==0) bsel->move (savePos);
1289 QString postSelStr=model->getSelectString(lmosel);
1290 QString postNum=QString::number (seli->num(),10);
1292 QString undoCom="relinkTo (\""+
1293 preParStr+ "\"," + preNum +"," +
1294 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1296 QString redoCom="relinkTo (\""+
1297 preDstParStr + "\"," + postNum + "," +
1298 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1303 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1308 // No destination, undo temporary move
1310 if (seli->depth()==1)
1312 // The select string might be different _after_ moving around.
1313 // Therefor reposition and then use string of old selection, too
1314 model->reposition();
1316 QPointF rp(lmosel->getRelPos());
1317 if (rp != movingObj_orgRelPos)
1319 QString ps=qpointfToString(rp);
1321 model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos),
1322 preSelStr, "moveRel "+ps,
1323 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1327 // Draw the original link, before selection was moved around
1328 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1330 lmosel->setRelPos(); // calc relPos first for starting point
1331 QPointF dst=bi->getBranchObj()->getParObj()->getChildPos(); // FIXME-3 check getBO here...
1332 // if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
1334 model->startAnimation(
1336 lmosel->getRelPos(),
1338 // QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
1341 model->reposition();
1344 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1345 // Finally resize scene, if needed
1349 // Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
1350 // model->updateActions(); FIXME-3 neeeded?
1352 // maybe we moved View: set old cursor
1353 setCursor (Qt::ArrowCursor);
1357 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1359 if (model->isSelectionBlocked() )
1365 if (e->button() == Qt::LeftButton )
1367 QPointF p = mapToScene(e->pos());
1368 TreeItem *ti=findMapItem (p, NULL);
1369 if (ti) { // MapObj was found
1370 // First select the MapObj than edit heading
1377 void MapEditor::resizeEvent (QResizeEvent* e)
1379 QGraphicsView::resizeEvent( e );
1382 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1384 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1385 // cerr << event->format(i) << endl;
1387 if (event->mimeData()->hasImage())
1388 event->acceptProposedAction();
1390 if (event->mimeData()->hasUrls())
1391 event->acceptProposedAction();
1394 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1398 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1403 void MapEditor::dropEvent(QDropEvent *event)
1405 BranchItem *selbi=model->getSelectedBranch();
1409 foreach (QString format,event->mimeData()->formats())
1410 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1414 if (event->mimeData()->hasImage())
1416 QVariant imageData = event->mimeData()->imageData();
1417 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1419 if (event->mimeData()->hasUrls())
1420 uris=event->mimeData()->urls();
1428 for (int i=0; i<uris.count();i++)
1430 // Workaround to avoid adding empty branches
1431 if (!uris.at(i).toString().isEmpty())
1433 bi=model->addNewBranch();
1437 s=uris.at(i).toLocalFile();
1440 QString file = QDir::fromNativeSeparators(s);
1441 heading = QFileInfo(file).baseName();
1443 if (file.endsWith(".vym", false))
1444 bi->setVymLink(file);
1446 bi->setURL(uris.at(i).toString());
1449 bo->setURL(uris.at(i).toString());
1453 if (!heading.isEmpty())
1454 bi->setHeading(heading);
1456 bi->setHeading(uris.at(i).toString());
1461 model->reposition();
1464 event->acceptProposedAction();
1467 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1469 // Note: Here we are prepared for multiple selections, though this
1470 // is not yet implemented elsewhere
1472 // Here in MapEditor we can only select Branches and Images
1473 QList <TreeItem*> treeItemsNew;
1474 QList <TreeItem*> treeItemsOld;
1477 foreach (ix,newsel.indexes() )
1479 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1480 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1481 if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
1483 foreach (ix,oldsel.indexes() )
1485 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1486 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
1487 if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
1490 // Trim list of selection rectangles
1491 while (treeItemsNew.count() < selboxList.count() )
1492 delete selboxList.takeFirst();
1494 // Take care to tmp scroll/unscroll
1495 if (!oldsel.isEmpty())
1497 QModelIndex ix=oldsel.indexes().first();
1500 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1503 if (ti->isBranchLikeType() )
1505 // reset tmp scrolled branches
1506 BranchItem *bi=(BranchItem*)ti;
1507 bi->resetTmpUnscroll();
1509 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1510 // Hide link if not needed
1511 ((MapItem*)ti)->getLMO()->updateVisibility();
1516 if (!treeItemsNew.isEmpty())
1518 QModelIndex ix=newsel.indexes().first();
1521 // Temporary unscroll if necessary
1522 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1523 if (ti->isBranchLikeType() )
1525 BranchItem *bi=(BranchItem*)ti;
1526 if (bi->hasScrolledParent(bi) )
1530 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1531 // Show link if needed
1532 ((MapItem*)ti)->getLMO()->updateVisibility();
1536 // Reduce rectangles
1537 while (treeItemsNew.count() < selboxList.count() )
1538 delete selboxList.takeFirst();
1540 // Add additonal rectangles
1541 QGraphicsRectItem *sb;
1542 while (treeItemsNew.count() > selboxList.count() )
1544 sb = mapScene->addRect(
1546 QPen(selectionColor),
1548 sb->setZValue(Z_SELBOX);
1550 selboxList.append (sb);
1555 // Reposition rectangles
1559 LinkableMapObj *lmo;
1560 for (int i=0; i<treeItemsNew.count();++i)
1562 lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
1563 bbox=lmo->getBBox();
1564 sb=selboxList.at(i);
1567 bbox.width(), bbox.height());
1568 sb->setPen (selectionColor);
1569 sb->setBrush (selectionColor);
1576 void MapEditor::updateData (const QModelIndex &sel)
1578 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1581 cout << "ME::updateData\n";
1583 cout << " ti="<<ti<<endl;
1584 cout << " h="<<ti->getHeading().toStdString()<<endl;
1587 if (ti->isBranchLikeType())
1589 // cout << " ->updating...\n";
1590 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1595 void MapEditor::setSelectionColor (QColor col)
1598 QItemSelection sel=model->getSelectionModel()->selection();
1599 updateSelection(sel,sel);
1603 QColor MapEditor::getSelectionColor ()
1605 return selectionColor;