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 void MapEditor::cursorUp()
560 model->selectUpperBranch();
563 void MapEditor::cursorDown()
566 model->selectLowerBranch();
569 void MapEditor::cursorLeft()
571 model->selectLeftBranch();
574 void MapEditor::cursorRight()
576 model->selectRightBranch();
579 void MapEditor::cursorFirst()
581 model->selectFirstBranch();
584 void MapEditor::cursorLast()
586 model->selectLastBranch();
590 void MapEditor::editHeading()
594 editHeadingFinished();
597 BranchObj *bo=model->getSelectedBranchObj();
598 BranchItem *bi=model->getSelectedBranchItem();
601 model->setSelectionBlocked(true);
603 lineEdit->setText (bi->getHeading());
604 QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
605 lineEdit->setGeometry(p.x(),p.y(),230,25);
606 lineEdit->selectAll();
608 lineEdit->setFocus();
609 lineEdit->grabKeyboard();
614 void MapEditor::editHeadingFinished()
616 editingHeading=false;
617 lineEdit->releaseKeyboard();
618 model->setHeading (lineEdit->text() );
619 model->setSelectionBlocked(false);
622 // Maybe reselect previous branch
623 mainWindow->editHeadingFinished (model);
627 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
629 // Lineedits are already closed by preceding
630 // mouseEvent, we don't need to close here.
632 QPointF p = mapToScene(e->pos());
633 TreeItem *ti=findMapItem (p, NULL);
634 LinkableMapObj* lmo=NULL;
635 if (ti) lmo=((MapItem*)ti)->getLMO();
638 { // MapObj was found
639 if (model->getSelectedLMO() != lmo)
645 if (model->getSelectedBranchObj() )
647 // Context Menu on branch or mapcenter
648 //FIXME-3 model->updateActions(); needed?
649 branchContextMenu->popup(e->globalPos() );
652 if (model->getSelectedImageItem() )
654 // Context Menu on floatimage
655 // model->updateActions(); FIXME-3 needed?
656 floatimageContextMenu->popup(e->globalPos() );
660 { // No MapObj found, we are on the Canvas itself
661 // Context Menu on scene
662 // model->updateActions(); FIXME-3 needed?
664 // Open context menu synchronously to position new mapcenter
665 model->setContextPos (p);
666 canvasContextMenu->exec(e->globalPos() );
667 model->unsetContextPos ();
672 void MapEditor::keyPressEvent(QKeyEvent* e)
674 if (e->modifiers() & Qt::ControlModifier)
676 switch (mainWindow->getModMode())
678 case Main::ModModeColor:
679 setCursor (PickColorCursor);
681 case Main::ModModeCopy:
682 setCursor (CopyCursor);
684 case Main::ModModeXLink:
685 setCursor (XLinkCursor);
688 setCursor (Qt::ArrowCursor);
694 void MapEditor::keyReleaseEvent(QKeyEvent* e)
696 if (!(e->modifiers() & Qt::ControlModifier))
697 setCursor (Qt::ArrowCursor);
700 void MapEditor::mousePressEvent(QMouseEvent* e)
702 // Ignore right clicks, these will go to context menus
703 if (e->button() == Qt::RightButton )
709 //Ignore clicks while editing heading
710 if (model->isSelectionBlocked() )
716 QPointF p = mapToScene(e->pos());
717 TreeItem *ti=findMapItem (p, NULL);
718 LinkableMapObj* lmo=NULL;
719 if (ti) lmo=((MapItem*)ti)->getLMO();
723 //Take care of system flags _or_ modifier modes
725 if (lmo && ti->isBranchLikeType() )
727 QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
728 if (!foname.isEmpty())
730 // systemFlag clicked
731 model->select (lmo); // FIXME-3 was selectInt
734 if (e->state() & Qt::ControlModifier)
735 mainWindow->editOpenURLTab();
737 mainWindow->editOpenURL();
739 else if (foname=="system-vymLink")
741 mainWindow->editOpenVymLink();
742 // tabWidget may change, better return now
743 // before segfaulting...
744 } else if (foname=="note")
745 mainWindow->windowToggleNoteEditor();
746 else if (foname=="hideInExport")
747 model->toggleHideExport();
748 // FIXME-3 needed? xelection.update();
752 // No system flag clicked, take care of modmodes (CTRL-Click)
753 if (e->state() & Qt::ControlModifier)
755 if (mainWindow->getModMode()==Main::ModModeColor)
758 setCursor (PickColorCursor);
761 if (mainWindow->getModMode()==Main::ModModeXLink)
763 BranchObj *bo_begin=NULL;
765 bo_begin=(BranchObj*)(lmo);
767 bo_begin=model->getSelectedBranchObj();
771 linkingObj_src=bo_begin;
772 tmpXLink=new XLinkObj (mapScene);
773 tmpXLink->setBegin (bo_begin);
774 tmpXLink->setEnd (p);
775 tmpXLink->setColor(model->getMapDefXLinkColor());
776 tmpXLink->setWidth(model->getMapDefXLinkWidth());
777 tmpXLink->updateXLink();
778 tmpXLink->setVisibility (true);
787 cout << "ME::mouse pressed\n";
788 cout << " lmo="<<lmo<<endl;
789 cout << " ti="<<ti->getHeadingStd()<<endl;
791 // Select the clicked object
796 // Left Button Move Branches
797 if (e->button() == Qt::LeftButton )
799 movingObj_start.setX( p.x() - lmo->x() );
800 movingObj_start.setY( p.y() - lmo->y() );
801 movingObj_orgPos.setX (lmo->x() );
802 movingObj_orgPos.setY (lmo->y() );
804 movingObj_orgRelPos=lmo->getRelPos();
806 // If modMode==copy, then we want to "move" the _new_ object around
807 // then we need the offset from p to the _old_ selection, because of tmp
808 if (mainWindow->getModMode()==Main::ModModeCopy &&
809 e->state() & Qt::ControlModifier)
811 BranchItem *bi=model->getSelectedBranchItem();
815 //FIXME-2 TreeItem::addBranch (BranchItem still missing)
816 //bi->addBranch (model->getSelectedBranchItem());
818 model->select(bi->getLastBranch());
823 movingObj=model->getSelectedLMO();
825 // Middle Button Toggle Scroll
826 // (On Mac OS X this won't work, but we still have
827 // a button in the toolbar)
828 if (e->button() == Qt::MidButton )
829 model->toggleScroll();
830 // model->updateActions(); FIXME-3 needed?
831 // FIXME-3 needed? xelection.update();
833 { // No MapObj found, we are on the scene itself
834 // Left Button move Pos of sceneView
835 if (e->button() == Qt::LeftButton )
837 movingObj=NULL; // move Content not Obj
838 movingObj_start=e->globalPos();
839 movingCont_start=QPointF (
840 horizontalScrollBar()->value(),
841 verticalScrollBar()->value());
842 movingVec=QPointF(0,0);
843 setCursor(HandOpenCursor);
848 void MapEditor::mouseMoveEvent(QMouseEvent* e)
850 QPointF p = mapToScene(e->pos());
851 TreeItem *seli=model->getSelectedItem();
852 LinkableMapObj* lmosel=NULL;
853 if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
854 lmosel=((MapItem*)seli)->getLMO();
856 // Move the selected MapObj
857 if ( lmosel && movingObj)
859 // reset cursor if we are moving and don't copy
860 if (mainWindow->getModMode()!=Main::ModModeCopy)
861 setCursor (Qt::ArrowCursor);
863 // To avoid jumping of the sceneView, only
864 // show selection, if not tmp linked
865 if (!lmosel->hasParObjTmp())
866 model->emitShowSelection();
868 // Now move the selection, but add relative position
869 // (movingObj_start) where selection was chosen with
870 // mousepointer. (This avoids flickering resp. jumping
871 // of selection back to absPos)
873 // Check if we could link
874 TreeItem *ti=findMapItem (p, seli);
875 BranchItem *dsti=NULL;
876 LinkableMapObj* dst=NULL;
877 if (ti && ti!=seli && ti->isBranchLikeType())
879 dsti=(BranchItem*)ti;
885 if (lmosel && seli->getType()==TreeItem::Image)
887 FloatObj *fio=(FloatImageObj*)lmosel;
888 fio->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
890 fio->updateLinkGeometry(); //no need for reposition, if we update link here
891 model->emitSelectionChanged(); // position has changed
893 // Relink float to new mapcenter or branch, if shift is pressed
894 // Only relink, if selection really has a new parent
895 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
897 // Also save the move which was done so far
898 QString pold=qpointfToString(movingObj_orgRelPos);
899 QString pnow=qpointfToString(fio->getRelPos());
905 QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
906 fio->getParObj()->requestReposition();
909 model->relinkImage ((ImageItem*) seli,dsti);
910 model->select (seli);
911 //movingObj=lmosel; //FIXME-3
912 //movingObj_orgRelPos=lmosel->getRelPos();
917 { // selection != a FloatObj
918 if (seli->depth()==0) //FIXME-1 also moved mapcenters could be linked, but not working here...
921 if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier)
922 ((BranchObj*)lmosel)->moveBy(
923 QPointF(p.x() -movingObj_start.x(),
924 p.y()-movingObj_start.y()) );
926 lmosel->move (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
927 model->updateRelPositions();
930 if (seli->depth()==1)
933 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
937 // Move ordinary branch
938 if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
939 // Add width of bbox here, otherwise alignRelTo will cause jumping around
940 lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(),
941 p.y()-movingObj_start.y() +lmosel->getTopPad() );
943 lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
947 // Maybe we can relink temporary?
950 if (e->modifiers()==Qt::ControlModifier)
952 // Special case: CTRL to link below dst
953 lmosel->setParObjTmp (dst,p,+1);
955 else if (e->modifiers()==Qt::ShiftModifier)
956 lmosel->setParObjTmp (dst,p,-1);
958 lmosel->setParObjTmp (dst,p,0);
961 lmosel->unsetParObjTmp();
963 // reposition subbranch
964 lmosel->reposition();
967 QItemSelection sel=model->getSelectionModel()->selection();
968 updateSelection(sel,sel); // position has changed
970 } // no FloatImageObj
974 } // selection && moving_obj
976 // Draw a link from one branch to another
979 tmpXLink->setEnd (p);
980 tmpXLink->updateXLink();
984 if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton )
986 QPointF p=e->globalPos();
987 movingVec.setX(-p.x() + movingObj_start.x() );
988 movingVec.setY(-p.y() + movingObj_start.y() );
989 horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
990 verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
995 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
997 QPointF p = mapToScene(e->pos());
998 TreeItem *seli=model->getSelectedItem();
1000 TreeItem *dsti=NULL;
1001 if (seli) dsti=findMapItem(p, seli);
1002 LinkableMapObj* dst=NULL;
1003 if (dsti && dsti->isBranchLikeType ())
1004 dst=((MapItem*)dsti)->getLMO();
1009 // Have we been picking color?
1013 setCursor (Qt::ArrowCursor);
1014 // Check if we are over another branch
1017 if (e->state() & Qt::ShiftModifier)
1018 model->colorBranch (((BranchObj*)dst)->getColor());
1020 model->colorSubtree (((BranchObj*)dst)->getColor());
1025 // Have we been drawing a link?
1029 // Check if we are over another branch
1032 tmpXLink->setEnd ( ((BranchObj*)(dst)) );
1033 tmpXLink->updateXLink();
1034 tmpXLink->activate(); //FIXME-2 savestate missing
1035 //model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );
1044 // Have we been moving something?
1045 if ( seli && movingObj )
1047 if (seli->getType()==TreeItem::Image)
1049 FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
1052 // Moved FloatObj. Maybe we need to reposition
1053 QString pold=qpointfToString(movingObj_orgRelPos);
1054 QString pnow=qpointfToString(fio->getRelPos());
1060 QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
1062 fio->getParObj()->requestReposition();
1063 model->reposition();
1067 BranchItem *bi=model->getSelectedBranchItem();
1068 if (bi && bi->depth()==0)
1070 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1072 QString pold=qpointfToString(movingObj_orgPos);
1073 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1080 QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
1084 if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
1085 { // A branch was moved
1086 LinkableMapObj* lmosel=NULL;
1087 lmosel=((MapItem*)seli)->getLMO();
1089 // save the position in case we link to mapcenter
1090 QPointF savePos=QPointF (lmosel->getAbsPos() );
1092 // Reset the temporary drawn link to the original one
1093 lmosel->unsetParObjTmp();
1095 // For Redo we may need to save original selection
1096 QString preSelStr=model->getSelectString(seli);
1101 // We have a destination, relink to that
1103 BranchObj* bsel=model->getSelectedBranchObj();
1105 QString preParStr=model->getSelectString (bsel->getParObj());
1106 QString preNum=QString::number (seli->num(),10);
1107 QString preDstParStr;
1110 if (e->state() & Qt::ShiftModifier && dst->getParObj())
1112 preDstParStr=model->getSelectString (dst->getParObj());
1113 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
1115 if (e->state() & Qt::ControlModifier && dst->getParObj())
1118 preDstParStr=model->getSelectString (dst->getParObj());
1119 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
1122 preDstParStr=model->getSelectString(dst);
1123 relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
1124 if (dsti->depth()==0) bsel->move (savePos);
1128 QString postSelStr=model->getSelectString(lmosel);
1129 QString postNum=QString::number (seli->num(),10);
1131 QString undoCom="relinkTo (\""+
1132 preParStr+ "\"," + preNum +"," +
1133 QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
1135 QString redoCom="relinkTo (\""+
1136 preDstParStr + "\"," + postNum + "," +
1137 QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
1142 QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
1147 // No destination, undo temporary move
1149 if (seli->depth()==1)
1151 // The select string might be different _after_ moving around.
1152 // Therefor reposition and then use string of old selection, too
1153 model->reposition();
1155 QPointF rp(lmosel->getRelPos());
1156 if (rp != movingObj_orgRelPos)
1158 QString ps=qpointfToString(rp);
1160 model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos),
1161 preSelStr, "moveRel "+ps,
1162 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1166 // Draw the original link, before selection was moved around
1167 if (settings.value("/animation/use",false).toBool() && seli->depth()>1)
1169 lmosel->setRelPos(); // calc relPos first for starting point
1170 QPointF dst=bi->getBranchObj()->getParObj()->getChildPos(); // FIXME-3 check getBO here...
1171 // if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
1173 model->startAnimation(
1175 lmosel->getRelPos(),
1177 // QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
1180 model->reposition();
1183 model->emitSelectionChanged(); //FIXME-3 needed? at least not after pos of selection has changed...
1184 // Finally resize scene, if needed
1188 // Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
1189 // model->updateActions(); FIXME-3 neeeded?
1191 // maybe we moved View: set old cursor
1192 setCursor (Qt::ArrowCursor);
1196 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
1198 if (model->isSelectionBlocked() )
1204 if (e->button() == Qt::LeftButton )
1206 QPointF p = mapToScene(e->pos());
1207 TreeItem *ti=findMapItem (p, NULL);
1208 if (ti) { // MapObj was found
1209 // First select the MapObj than edit heading
1216 void MapEditor::resizeEvent (QResizeEvent* e)
1218 QGraphicsView::resizeEvent( e );
1221 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
1223 //for (unsigned int i=0;event->format(i);i++) // Debug mime type
1224 // cerr << event->format(i) << endl;
1226 if (event->mimeData()->hasImage())
1227 event->acceptProposedAction();
1229 if (event->mimeData()->hasUrls())
1230 event->acceptProposedAction();
1233 void MapEditor::dragMoveEvent(QDragMoveEvent *)
1237 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
1242 void MapEditor::dropEvent(QDropEvent *event)
1244 BranchItem *selbi=model->getSelectedBranchItem();
1248 foreach (QString format,event->mimeData()->formats())
1249 cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
1253 if (event->mimeData()->hasImage())
1255 QVariant imageData = event->mimeData()->imageData();
1256 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
1258 if (event->mimeData()->hasUrls())
1259 uris=event->mimeData()->urls();
1267 for (int i=0; i<uris.count();i++)
1269 // Workaround to avoid adding empty branches
1270 if (!uris.at(i).toString().isEmpty())
1272 bi=model->addNewBranch();
1276 s=uris.at(i).toLocalFile();
1279 QString file = QDir::fromNativeSeparators(s);
1280 heading = QFileInfo(file).baseName();
1282 if (file.endsWith(".vym", false))
1283 bi->setVymLink(file);
1285 bi->setURL(uris.at(i).toString());
1288 bo->setURL(uris.at(i).toString());
1292 if (!heading.isEmpty())
1293 bi->setHeading(heading);
1295 bi->setHeading(uris.at(i).toString());
1300 model->reposition();
1303 event->acceptProposedAction();
1306 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
1308 // Note: Here we are prepared for multiple selections, though this
1309 // is not yet implemented elsewhere
1311 // Here in MapEditor we can only select Branches and Images
1313 foreach (ix,newsel.indexes() )
1315 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1316 if (ti->getType()!=TreeItem::Branch && ti->getType()!=TreeItem::Image )
1317 newsel.indexes().removeOne (ix);
1320 // Trim list of selection rectangles
1321 while (newsel.indexes().count() < selboxList.count() )
1322 delete selboxList.takeFirst();
1324 // Take care to tmp scroll/unscroll
1325 if (!oldsel.isEmpty())
1327 QModelIndex ix=oldsel.indexes().first();
1330 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1333 if (ti->isBranchLikeType() )
1335 // reset tmp scrolled branches
1336 BranchItem *bi=(BranchItem*)ti;
1337 bi->resetTmpUnscroll();
1339 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1340 // Hide link if not needed
1341 ((MapItem*)ti)->getLMO()->updateVisibility();
1346 if (!newsel.isEmpty())
1348 QModelIndex ix=newsel.indexes().first();
1351 // Temporary unscroll if necessary
1352 TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
1353 if (ti->isBranchLikeType() )
1355 BranchItem *bi=(BranchItem*)ti;
1356 if (bi->hasScrolledParent(bi) )
1360 if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
1361 // Show link if needed
1362 ((MapItem*)ti)->getLMO()->updateVisibility();
1366 // Reduce rectangles
1367 while (newsel.indexes().count() < selboxList.count() )
1368 delete selboxList.takeFirst();
1370 // Add additonal rectangles
1371 QGraphicsRectItem *sb;
1372 while (newsel.indexes().count() > selboxList.count() )
1374 sb = mapScene->addRect(
1376 QPen(selectionColor),
1378 sb->setZValue(Z_SELBOX);
1380 selboxList.append (sb);
1383 // Reposition rectangles
1389 LinkableMapObj *lmo;
1390 foreach (sb,selboxList)
1392 index=newsel.indexes().at(i);
1393 ti= static_cast<TreeItem*>(index.internalPointer());
1394 lmo=((MapItem*)ti)->getLMO();
1395 bbox=lmo->getBBox();
1398 bbox.width(), bbox.height());
1399 sb->setPen (selectionColor);
1400 sb->setBrush (selectionColor);
1407 void MapEditor::updateData (const QModelIndex &sel)
1409 TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
1412 cout << "ME::updateData\n";
1414 cout << " ti="<<ti<<endl;
1415 cout << " h="<<ti->getHeading().toStdString()<<endl;
1418 if (ti->isBranchLikeType())
1420 BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
1426 void MapEditor::setSelectionColor (QColor col)
1429 QItemSelection sel=model->getSelectionModel()->selection();
1430 updateSelection(sel,sel);
1434 QColor MapEditor::getSelectionColor ()
1436 return selectionColor;