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=model->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 void MapEditor::setAntiAlias (bool b)
391 setRenderHint(QPainter::Antialiasing,b);
394 void MapEditor::setSmoothPixmap(bool b)
396 setRenderHint(QPainter::SmoothPixmapTransform,b);
399 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
401 // Start with mapcenter, no images allowed at rootItem
403 BranchItem *bi=model->getRootItem()->getFirstBranch();
404 TreeItem *found=NULL;
407 found=bi->findMapItem (p, exclude);
408 if (found) return found;
410 bi=model->getRootItem()->getBranchNum(i);
415 AttributeTable* MapEditor::attributeTable()
420 void MapEditor::testFunction1()
423 // Code copied from Qt sources
424 QRectF rect=model->getSelectedBranchObj()->getBBox();
428 qreal width = viewport()->width();
429 qreal height = viewport()->height();
430 QRectF viewRect = matrix().mapRect(rect);
432 qreal left = horizontalScrollBar()->value();
433 qreal right = left + width;
434 qreal top = verticalScrollBar()->value();
435 qreal bottom = top + height;
437 if (viewRect.left() <= left + xmargin) {
438 // need to scroll from the left
439 // if (!d->leftIndent)
440 horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
442 if (viewRect.right() >= right - xmargin) {
443 // need to scroll from the right
444 // if (!d->leftIndent)
445 horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
447 if (viewRect.top() <= top + ymargin) {
448 // need to scroll from the top
449 // if (!d->topIndent)
450 verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
452 if (viewRect.bottom() >= bottom - ymargin) {
453 // need to scroll from the bottom
454 // if (!d->topIndent)
455 verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
457 cout << "test1: hor="<<horizontalScrollBar()->value()<<endl;
458 cout << "test1: ver="<<verticalScrollBar()->value()<<endl;
461 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
462 animation->setDuration(5000);
463 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
464 animation->setEasingCurve ( QtEasingCurve::OutQuint);
465 animation->setStartValue(sceneRect() );
466 animation->setEndValue(QRectF(50, 50, 1000, 1000));
471 QDialog *dia= new QDialog (this);
472 dia->setGeometry (50,50,10,10);
477 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
478 animation->setDuration(1000);
479 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
480 animation->setEasingCurve ( QtEasingCurve::OutQuint);
481 animation->setStartValue(QRect(50, 50, 10, 10));
482 animation->setEndValue(QRect(250, 250, 100, 100));
487 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
488 if (hidemode==HideNone)
490 setHideTmpMode (HideExport);
491 mapCenter->calcBBoxSizeWithChilds();
492 QRectF totalBBox=mapCenter->getTotalBBox();
493 QRectF mapRect=totalBBox;
494 QCanvasRectangle *frame=NULL;
496 cout << " map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
498 mapRect.setRect (totalBBox.x(), totalBBox.y(),
499 totalBBox.width(), totalBBox.height());
500 frame=new QCanvasRectangle (mapRect,mapScene);
501 frame->setBrush (QColor(white));
502 frame->setPen (QColor(black));
508 setHideTmpMode (HideNone);
510 cout <<" hidemode="<<hidemode<<endl;
513 void MapEditor::testFunction2()
518 if (hidemode==HideExport)
519 setHideTmpMode (HideNone);
521 setHideTmpMode (HideExport);
525 void MapEditor::cursorUp()
527 model->selectUpperBranch();
530 void MapEditor::cursorDown()
533 model->selectLowerBranch();
536 void MapEditor::cursorLeft()
538 model->selectLeftBranch();
541 void MapEditor::cursorRight()
543 model->selectRightBranch();
546 void MapEditor::cursorFirst()
548 model->selectFirstBranch();
551 void MapEditor::cursorLast()
553 model->selectLastBranch();
557 void MapEditor::editHeading()
561 editHeadingFinished();
564 BranchObj *bo=model->getSelectedBranchObj();
565 BranchItem *bi=model->getSelectedBranchItem();
568 model->setSelectionBlocked(true);
570 lineEdit->setText (bi->getHeading());
571 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
572 lineEdit->setGeometry(p.x(),p.y(),230,25);
573 lineEdit->selectAll();
575 lineEdit->setFocus();
576 lineEdit->grabKeyboard();
581 void MapEditor::editHeadingFinished()
583 editingHeading=false;
584 lineEdit->releaseKeyboard();
585 model->setHeading (lineEdit->text() );
586 model->setSelectionBlocked(false);
589 // Maybe reselect previous branch
590 mainWindow->editHeadingFinished (model);
594 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
596 // Lineedits are already closed by preceding
597 // mouseEvent, we don't need to close here.
599 QPointF p = mapToScene(e->pos());
600 TreeItem *ti=findMapItem (p, NULL);
601 LinkableMapObj* lmo=NULL;
602 if (ti) lmo=((MapItem*)ti)->getLMO();
605 { // MapObj was found
606 if (model->getSelectedLMO() != lmo)
612 if (model->getSelectedBranchObj() )
614 // Context Menu on branch or mapcenter
615 //FIXME-3 model->updateActions(); needed?
616 branchContextMenu->popup(e->globalPos() );
619 if (model->getSelectedImageItem() )
621 // Context Menu on floatimage
622 // model->updateActions(); FIXME-3 needed?
623 floatimageContextMenu->popup(e->globalPos() );
627 { // No MapObj found, we are on the Canvas itself
628 // Context Menu on scene
629 // model->updateActions(); FIXME-3 needed?
631 // Open context menu synchronously to position new mapcenter
632 model->setContextPos (p);
633 canvasContextMenu->exec(e->globalPos() );
634 model->unsetContextPos ();
639 void MapEditor::keyPressEvent(QKeyEvent* e)
641 if (e->modifiers() & Qt::ControlModifier)
643 switch (mainWindow->getModMode())
645 case Main::ModModeColor:
646 setCursor (PickColorCursor);
648 case Main::ModModeCopy:
649 setCursor (CopyCursor);
651 case Main::ModModeXLink:
652 setCursor (XLinkCursor);
655 setCursor (Qt::ArrowCursor);
661 void MapEditor::keyReleaseEvent(QKeyEvent* e)
663 if (!(e->modifiers() & Qt::ControlModifier))
664 setCursor (Qt::ArrowCursor);
667 void MapEditor::mousePressEvent(QMouseEvent* e)
669 // Ignore right clicks, these will go to context menus
670 if (e->button() == Qt::RightButton )
676 //Ignore clicks while editing heading
677 if (model->isSelectionBlocked() )
683 QPointF p = mapToScene(e->pos());
684 TreeItem *ti=findMapItem (p, NULL);
685 LinkableMapObj* lmo=NULL;
686 if (ti) lmo=((MapItem*)ti)->getLMO();
690 //Take care of system flags _or_ modifier modes
692 if (lmo && ti->isBranchLikeType() )
694 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
695 if (!foname.isEmpty())
697 // systemFlag clicked
698 model->select (lmo); // FIXME-3 was selectInt
701 if (e->state() & Qt::ControlModifier)
702 mainWindow->editOpenURLTab();
704 mainWindow->editOpenURL();
706 else if (foname=="vymLink")
708 mainWindow->editOpenVymLink();
709 // tabWidget may change, better return now
710 // before segfaulting...
711 } else if (foname=="note")
712 mainWindow->windowToggleNoteEditor();
713 else if (foname=="hideInExport")
714 model->toggleHideExport();
715 // FIXME-3 needed? xelection.update();
719 // No system flag clicked, take care of modmodes (CTRL-Click)
720 if (e->state() & Qt::ControlModifier)
722 if (mainWindow->getModMode()==Main::ModModeColor)
725 setCursor (PickColorCursor);
728 if (mainWindow->getModMode()==Main::ModModeXLink)
730 BranchObj *bo_begin=NULL;
732 bo_begin=(BranchObj*)(lmo);
734 bo_begin=model->getSelectedBranchObj();
738 linkingObj_src=bo_begin;
739 tmpXLink=new XLinkObj (mapScene);
740 tmpXLink->setBegin (bo_begin);
741 tmpXLink->setEnd (p);
742 tmpXLink->setColor(model->getMapDefXLinkColor());
743 tmpXLink->setWidth(model->getMapDefXLinkWidth());
744 tmpXLink->updateXLink();
745 tmpXLink->setVisibility (true);
754 cout << "ME::mouse pressed\n";
755 cout << " lmo="<<lmo<<endl;
756 cout << " ti="<<ti->getHeadingStd()<<endl;
758 // Select the clicked object
763 // Left Button Move Branches
764 if (e->button() == Qt::LeftButton )
766 //movingObj_start.setX( p.x() - selection->x() );// TODO replaced selection->lmo here
767 //movingObj_start.setY( p.y() - selection->y() );
768 movingObj_start.setX( p.x() - lmo->x() );
769 movingObj_start.setY( p.y() - lmo->y() );
770 movingObj_orgPos.setX (lmo->x() );
771 movingObj_orgPos.setY (lmo->y() );
773 movingObj_orgRelPos=lmo->getRelPos();
775 // If modMode==copy, then we want to "move" the _new_ object around
776 // then we need the offset from p to the _old_ selection, because of tmp
777 if (mainWindow->getModMode()==Main::ModModeCopy &&
778 e->state() & Qt::ControlModifier)
780 BranchItem *bi=model->getSelectedBranchItem();
784 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
785 //bi->addBranch (model->getSelectedBranchItem());
787 model->select(bi->getLastBranch());
792 movingObj=model->getSelectedLMO();
794 // Middle Button Toggle Scroll
795 // (On Mac OS X this won't work, but we still have
796 // a button in the toolbar)
797 if (e->button() == Qt::MidButton )
798 model->toggleScroll();
799 // model->updateActions(); FIXME-3 needed?
800 // FIXME-3 needed? xelection.update();
802 { // No MapObj found, we are on the scene itself
803 // Left Button move Pos of sceneView
804 if (e->button() == Qt::LeftButton )
806 movingObj=NULL; // move Content not Obj
807 movingObj_start=e->globalPos();
808 movingCont_start=QPointF (
809 horizontalScrollBar()->value(),
810 verticalScrollBar()->value());
811 movingVec=QPointF(0,0);
812 setCursor(HandOpenCursor);
817 void MapEditor::mouseMoveEvent(QMouseEvent* e)
819 QPointF p = mapToScene(e->pos());
820 TreeItem *seli=model->getSelectedItem();
821 LinkableMapObj* lmosel=NULL; //FIXME-2 get rid of lmosel
822 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
823 lmosel=((MapItem*)seli)->getLMO();
825 // Move the selected MapObj
826 if ( lmosel && movingObj)
828 // reset cursor if we are moving and don't copy
829 if (mainWindow->getModMode()!=Main::ModModeCopy)
830 setCursor (Qt::ArrowCursor);
832 // To avoid jumping of the sceneView, only
833 // show selection, if not tmp linked
834 if (!lmosel->hasParObjTmp())
835 model->emitShowSelection();
837 // Now move the selection, but add relative position
838 // (movingObj_start) where selection was chosen with
839 // mousepointer. (This avoids flickering resp. jumping
840 // of selection back to absPos)
842 // Check if we could link
843 TreeItem *dsti=findMapItem (p, seli);
844 LinkableMapObj* dst=NULL;
845 if (dsti && dsti!=seli && dsti->isBranchLikeType())
846 dst=((MapItem*)dsti)->getLMO();
851 if (lmosel && seli->getType()==TreeItem::Image)
853 FloatObj *fio=(FloatImageObj*)lmosel;
854 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
856 fio->updateLink(); //no need for reposition, if we update link here
857 model->emitSelectionChanged(); // position has changed
859 // Relink float to new mapcenter or branch, if shift is pressed
860 // Only relink, if selection really has a new parent
861 if ( (e->modifiers()==Qt::ShiftModifier) && dst && ( dst != fio->getParObj())
864 // Also save the move which was done so far
865 QString pold=qpointfToString(movingObj_orgRelPos);
866 QString pnow=qpointfToString(fio->getRelPos());
868 fio->getTreeItem(), // FIXME-3
872 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
873 fio->getParObj()->requestReposition();
876 model->linkFloatImageTo (model->getSelectString(dst));
878 //movingObj_orgRelPos=lmosel->getRelPos();
883 { // selection != a FloatObj
884 if (seli->depth()==0) //FIXME-1 also moved mapcenters could be linked, but not working here...
887 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
888 ((BranchObj*)lmosel)->moveBy(
889 QPointF(p.x() -movingObj_start.x(),
890 p.y()-movingObj_start.y()) );
892 lmosel->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
893 model->updateRelPositions();
896 if (seli->depth()==1)
899 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
903 // Move ordinary branch
904 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
905 // Add width of bbox here, otherwise alignRelTo will cause jumping around
906 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
907 p.y()-movingObj_start.y() +lmosel->getTopPad() );
909 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
913 // Maybe we can relink temporary?
916 if (e->modifiers()==Qt::ControlModifier)
918 // Special case: CTRL to link below dst
919 lmosel->setParObjTmp (dst,p,+1);
921 else if (e->modifiers()==Qt::ShiftModifier)
922 lmosel->setParObjTmp (dst,p,-1);
924 lmosel->setParObjTmp (dst,p,0);
927 lmosel->unsetParObjTmp();
929 // reposition subbranch
930 lmosel->reposition();
933 QItemSelection sel=model->getSelectionModel()->selection();
934 updateSelection(sel,sel); // position has changed
936 } // no FloatImageObj
940 } // selection && moving_obj
942 // Draw a link from one branch to another
945 tmpXLink->setEnd (p);
946 tmpXLink->updateXLink();
950 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
952 QPointF p=e->globalPos();
953 movingVec.setX(-p.x() + movingObj_start.x() );
954 movingVec.setY(-p.y() + movingObj_start.y() );
955 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
956 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
961 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
963 QPointF p = mapToScene(e->pos());
964 TreeItem *seli=model->getSelectedItem();
967 if (seli) dsti=findMapItem(p, seli);
968 LinkableMapObj* dst=NULL;
969 if (dsti && dsti->isBranchLikeType ())
970 dst=((MapItem*)dsti)->getLMO();
975 // Have we been picking color?
979 setCursor (Qt::ArrowCursor);
980 // Check if we are over another branch
983 if (e->state() & Qt::ShiftModifier)
984 model->colorBranch (((BranchObj*)dst)->getColor());
986 model->colorSubtree (((BranchObj*)dst)->getColor());
991 // Have we been drawing a link?
995 // Check if we are over another branch
998 tmpXLink->setEnd ( ((BranchObj*)(dst)) );
999 tmpXLink->updateXLink();
1000 tmpXLink->activate(); //FIXME-2 savestate missing
1001 //model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );
1010 // Have we been moving something?
1011 if ( seli && movingObj )
1013 if (seli->getType()==TreeItem::Image)
1015 FloatImageObj *fio=(FloatImageObj*)(((MapItem*)seli)->getLMO());
1018 // Moved FloatObj. Maybe we need to reposition
1019 QString pold=qpointfToString(movingObj_orgRelPos);
1020 QString pnow=qpointfToString(fio->getRelPos());
1026 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1028 fio->getParObj()->requestReposition();
1029 model->reposition();
1033 BranchItem *bi=model->getSelectedBranchItem();
1034 if (bi && bi->depth()==0)
1036 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1038 QString pold=qpointfToString(movingObj_orgPos);
1039 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1046 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1050 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1051 { // A branch was moved
1052 LinkableMapObj* lmosel=NULL; //FIXME-2 get rid of lmosel
1053 lmosel=((MapItem*)seli)->getLMO();
1055 // save the position in case we link to mapcenter
1056 QPointF savePos=QPointF (lmosel->getAbsPos() );
1058 // Reset the temporary drawn link to the original one
1059 lmosel->unsetParObjTmp();
1061 // For Redo we may need to save original selection
1062 QString preSelStr=model->getSelectString(seli);
1067 // We have a destination, relink to that
1069 BranchObj* bsel=model->getSelectedBranchObj();
1071 QString preParStr=model->getSelectString (bsel->getParObj());
1072 QString preNum=QString::number (seli->num(),10);
1073 QString preDstParStr;
1076 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1078 preDstParStr=model->getSelectString (dst->getParObj());
1079 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1081 if (e->state() & Qt::ControlModifier && dst->getParObj())
1084 preDstParStr=model->getSelectString (dst->getParObj());
1085 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1088 preDstParStr=model->getSelectString(dst);
1089 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1090 if (dsti->depth()==0) bsel->move (savePos);
1094 QString postSelStr=model->getSelectString(lmosel);
1095 QString postNum=QString::number (seli->num(),10);
1097 QString undoCom="relinkTo (\""+
1098 preParStr+ "\"," + preNum +"," +
1099 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1101 QString redoCom="relinkTo (\""+
1102 preDstParStr + "\"," + postNum + "," +
1103 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1108 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1113 // No destination, undo temporary move
1115 if (seli->depth()==1)
1117 // The select string might be different _after_ moving around.
1118 // Therefor reposition and then use string of old selection, too
1119 model->reposition();
1121 QPointF rp(lmosel->getRelPos());
1122 if (rp != movingObj_orgRelPos)
1124 QString ps=qpointfToString(rp);
1126 model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos),
1127 preSelStr, "moveRel "+ps,
1128 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1132 // Draw the original link, before selection was moved around
1133 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1135 lmosel->setRelPos(); // calc relPos first for starting point
1136 QPointF dst=bi->getBranchObj()->getParObj()->getChildPos(); // FIXME-3 check getBO here...
1137 // if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
1139 model->startAnimation(
1141 lmosel->getRelPos(),
1143 // QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
1146 model->reposition();
1149 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1150 // Finally resize scene, if needed
1154 // Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
1155 // model->updateActions(); FIXME-3 neeeded?
1157 // maybe we moved View: set old cursor
1158 setCursor (Qt::ArrowCursor);
1162 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1164 if (model->isSelectionBlocked() )
1170 if (e->button() == Qt::LeftButton )
1172 QPointF p = mapToScene(e->pos());
1173 TreeItem *ti=findMapItem (p, NULL);
1174 if (ti) { // MapObj was found
1175 // First select the MapObj than edit heading
1182 void MapEditor::resizeEvent (QResizeEvent* e)
1184 QGraphicsView::resizeEvent( e );
1187 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1189 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1190 // cerr << event->format(i) << endl;
1192 if (event->mimeData()->hasImage())
1193 event->acceptProposedAction();
1195 if (event->mimeData()->hasUrls())
1196 event->acceptProposedAction();
1199 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1203 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1208 void MapEditor::dropEvent(QDropEvent *event)
1210 BranchItem *selbi=model->getSelectedBranchItem();
1214 foreach (QString format,event->mimeData()->formats())
1215 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1219 if (event->mimeData()->hasImage())
1221 QVariant imageData = event->mimeData()->imageData();
1222 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1224 if (event->mimeData()->hasUrls())
1225 uris=event->mimeData()->urls();
1233 for (int i=0; i<uris.count();i++)
1235 // Workaround to avoid adding empty branches
1236 if (!uris.at(i).toString().isEmpty())
1238 bi=model->addNewBranch();
1242 s=uris.at(i).toLocalFile();
1245 QString file = QDir::fromNativeSeparators(s);
1246 heading = QFileInfo(file).baseName();
1248 if (file.endsWith(".vym", false))
1249 bi->setVymLink(file);
1251 bi->setURL(uris.at(i).toString());
1254 bo->setURL(uris.at(i).toString());
1258 if (!heading.isEmpty())
1259 bi->setHeading(heading);
1261 bi->setHeading(uris.at(i).toString());
1266 model->reposition();
1269 event->acceptProposedAction();
1272 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1274 // Here in MapEditor we can only select Branches and Images
1276 foreach (ix,newsel.indexes() )
1278 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1279 if (ti->getType()!=TreeItem::Branch && ti->getType()!=TreeItem::Image )
1280 newsel.indexes().removeOne (ix);
1283 while (newsel.indexes().count() < selboxList.count() )
1284 delete selboxList.takeFirst();
1286 // Take care to tmp scroll/unscroll
1287 if (!oldsel.isEmpty())
1289 QModelIndex ix=oldsel.indexes().first();
1292 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1293 if (ti && ti->isBranchLikeType() )
1295 BranchItem *bi=(BranchItem*)ti;
1296 bi->resetTmpUnscroll();
1301 if (!newsel.isEmpty())
1303 QModelIndex ix=newsel.indexes().first();
1306 // Temporary unscroll if necessary
1307 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1308 if (ti->isBranchLikeType() )
1310 BranchItem *bi=(BranchItem*)ti;
1311 if (bi->hasScrolledParent(bi) )
1318 // Reduce rectangles
1319 while (newsel.indexes().count() < selboxList.count() )
1320 delete selboxList.takeFirst();
1322 // Add additonal rectangles
1323 QGraphicsRectItem *sb;
1324 while (newsel.indexes().count() > selboxList.count() )
1326 sb = mapScene->addRect(
1328 QPen(selectionColor),
1330 sb->setZValue(Z_SELBOX);
1332 selboxList.append (sb);
1335 // Reposition rectangles
1341 LinkableMapObj *lmo;
1342 foreach (sb,selboxList)
1344 index=newsel.indexes().at(i);
1345 ti= static_cast<TreeItem*>(index.internalPointer());
1346 lmo=((MapItem*)ti)->getLMO();
1347 bbox=lmo->getBBox();
1350 bbox.width(), bbox.height());
1351 sb->setPen (selectionColor);
1352 sb->setBrush (selectionColor);
1359 void MapEditor::updateData (const QModelIndex &sel)
1361 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1364 cout << "ME::updateData\n";
1366 cout << " ti="<<ti<<endl;
1367 cout << " h="<<ti->getHeading().toStdString()<<endl;
1370 if (ti->isBranchLikeType())
1372 BranchObj *bo=(BranchObj*) ((MapItem*)ti)->getLMO();
1378 void MapEditor::setSelectionColor (QColor col)
1381 QItemSelection sel=model->getSelectionModel()->selection();
1382 updateSelection(sel,sel);
1386 QColor MapEditor::getSelectionColor ()
1388 return selectionColor;