1 #include <QApplication>
6 #include "editxlinkdialog.h"
8 #include "exportxhtmldialog.h"
10 #include "geometry.h" // for addBBox
11 #include "mainwindow.h"
12 #include "mapcenterobj.h"
15 #include "selection.h"
18 #include "warningdialog.h"
19 #include "xml-freemind.h"
25 extern Main *mainWindow;
26 extern Settings settings;
27 extern QString tmpVymDir;
29 extern TextEditor *textEditor;
31 extern QString clipboardDir;
32 extern QString clipboardFile;
33 extern bool clipboardEmpty;
35 extern ImageIO imageIO;
37 extern QString vymName;
38 extern QString vymVersion;
39 extern QDir vymBaseDir;
41 extern QDir lastImageDir;
42 extern QDir lastFileDir;
44 extern FlagRowObj *standardFlagsDefault;
46 extern Settings settings;
50 int VymModel::mapNum=0; // make instance
54 // cout << "Const VymModel\n";
61 // cout << "Destr VymModel\n";
62 autosaveTimer->stop();
63 fileChangedTimer->stop();
67 void VymModel::clear()
69 selModel->clearSelection();
72 while (!mapCenters.isEmpty()) // FIXME VM needs to be in treemodel only...
73 delete mapCenters.takeFirst();
75 QModelIndex ri=index(rootItem);
76 removeRows (0, rowCount(ri),ri);
79 void VymModel::init ()
81 // We should have at least one map center to start with
82 // addMapCenter(); FIXME VM create this in MapEditor as long as model is part of that
87 // Also no scene yet (should not be needed anyway) FIXME VM
100 stepsTotal=settings.readNumEntry("/history/stepsTotal",100);
101 undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
102 mainWindow->updateHistory (undoSet);
105 makeTmpDirectories();
110 fileName=tr("unnamed");
112 blockReposition=false;
113 blockSaveState=false;
115 autosaveTimer=new QTimer (this);
116 connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
118 fileChangedTimer=new QTimer (this);
119 fileChangedTimer->start(3000);
120 connect(fileChangedTimer, SIGNAL(timeout()), this, SLOT(fileChanged()));
132 animationUse=settings.readBoolEntry("/animation/use",false);
133 animationTicks=settings.readNumEntry("/animation/ticks",10);
134 animationInterval=settings.readNumEntry("/animation/interval",50);
136 animationTimer=new QTimer (this);
137 connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
140 defLinkColor=QColor (0,0,255);
141 defXLinkColor=QColor (180,180,180);
142 linkcolorhint=LinkableMapObj::DefaultColor;
143 linkstyle=LinkableMapObj::PolyParabel;
145 defXLinkColor=QColor (230,230,230);
153 // addMapCenter(); FIXME VM create this in MapEditor until BO and MCO are independent of scene
157 void VymModel::makeTmpDirectories()
159 // Create unique temporary directories
160 tmpMapDir = tmpVymDir+QString("/model-%1").arg(mapNum);
161 histPath = tmpMapDir+"/history";
167 MapEditor* VymModel::getMapEditor() // FIXME VM better return favourite editor here
172 bool VymModel::isRepositionBlocked()
174 return blockReposition;
177 void VymModel::updateActions() // FIXME maybe don't update if blockReposition is set
179 cout << "VymModel::updateActions \n";
180 // Tell mainwindow to update states of actions
181 mainWindow->updateActions();
186 QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
188 // tmpdir temporary directory to which data will be written
189 // prefix mapname, which will be appended to images etc.
190 // writeflags Only write flags for "real" save of map, not undo
191 // offset offset of bbox of whole map in scene.
192 // Needed for XML export
201 case LinkableMapObj::Line:
204 case LinkableMapObj::Parabel:
207 case LinkableMapObj::PolyLine:
211 ls="StylePolyParabel";
215 QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
217 if (linkcolorhint==LinkableMapObj::HeadingColor)
218 colhint=xml.attribut("linkColorHint","HeadingColor");
220 QString mapAttr=xml.attribut("version",vymVersion);
222 mapAttr+= xml.attribut("author",author) +
223 xml.attribut("comment",comment) +
224 xml.attribut("date",getDate()) +
225 xml.attribut("countBranches", QString().number(countBranches())) +
226 xml.attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
227 xml.attribut("selectionColor", mapEditor->getSelectionColor().name() ) +
228 xml.attribut("linkStyle", ls ) +
229 xml.attribut("linkColor", defLinkColor.name() ) +
230 xml.attribut("defXLinkColor", defXLinkColor.name() ) +
231 xml.attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
233 s+=xml.beginElement("vymmap",mapAttr);
236 // Find the used flags while traversing the tree
237 standardFlagsDefault->resetUsedCounter();
239 // Reset the counters before saving
240 // TODO constr. of FIO creates lots of objects, better do this in some other way...
241 FloatImageObj (mapScene).resetSaveCounter();
243 // Build xml recursivly
244 if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
245 // Save complete map, if saveSel not set
246 s+=saveToDir(tmpdir,prefix,writeflags,offset);
249 if ( typeid(*saveSel) == typeid(BranchObj) )
251 s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
252 else if ( typeid(*saveSel) == typeid(FloatImageObj) )
254 s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
257 // Save local settings
258 s+=settings.getDataXML (destPath);
261 if (!selection.isEmpty() && !saveSel )
262 s+=xml.valueElement("select",selection.getSelectString());
265 s+=xml.endElement("vymmap");
268 standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
272 void VymModel::setFilePath(QString fpath, QString destname)
274 if (fpath.isEmpty() || fpath=="")
281 filePath=fpath; // becomes absolute path
282 fileName=fpath; // gets stripped of path
283 destPath=destname; // needed for vymlinks and during load to reset fileChangedTime
285 // If fpath is not an absolute path, complete it
286 filePath=QDir(fpath).absPath();
287 fileDir=filePath.left (1+filePath.findRev ("/"));
289 // Set short name, too. Search from behind:
290 int i=fileName.findRev("/");
291 if (i>=0) fileName=fileName.remove (0,i+1);
293 // Forget the .vym (or .xml) for name of map
294 mapName=fileName.left(fileName.findRev(".",-1,true) );
298 void VymModel::setFilePath(QString fpath)
300 setFilePath (fpath,fpath);
303 QString VymModel::getFilePath()
308 QString VymModel::getFileName()
313 QString VymModel::getMapName()
318 QString VymModel::getDestPath()
323 ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
325 ErrorCode err=success;
327 parseBaseHandler *handler;
331 case VymMap: handler=new parseVYMHandler; break;
332 case FreemindMap : handler=new parseFreemindHandler; break;
334 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
335 "Unknown FileType in VymModel::load()");
339 bool zipped_org=zipped;
343 selModel->clearSelection();
344 // FIXME VM not needed??? model->setMapEditor(this);
345 // (map state is set later at end of load...)
348 BranchObj *bo=getSelectedBranch();
349 if (!bo) return aborted;
350 if (lmode==ImportAdd)
351 saveStateChangingPart(
354 QString("addMapInsert (%1)").arg(fname),
355 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
357 saveStateChangingPart(
360 QString("addMapReplace(%1)").arg(fname),
361 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
365 // Create temporary directory for packing
367 QString tmpZipDir=makeTmpDir (ok,"vym-pack");
370 QMessageBox::critical( 0, tr( "Critical Load Error" ),
371 tr("Couldn't create temporary directory before load\n"));
376 err=unzipDir (tmpZipDir,fname);
386 // Look for mapname.xml
387 xmlfile= fname.left(fname.findRev(".",-1,true));
388 xmlfile=xmlfile.section( '/', -1 );
389 QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
390 if (!mfile.exists() )
392 // mapname.xml does not exist, well,
393 // maybe someone renamed the mapname.vym file...
394 // Try to find any .xml in the toplevel
395 // directory of the .vym file
396 QStringList flist=QDir (tmpZipDir).entryList("*.xml");
397 if (flist.count()==1)
399 // Only one entry, take this one
400 xmlfile=tmpZipDir + "/"+flist.first();
403 for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it )
404 *it=tmpZipDir + "/" + *it;
405 // TODO Multiple entries, load all (but only the first one into this ME)
406 //mainWindow->fileLoadFromTmp (flist);
407 //returnCode=1; // Silently forget this attempt to load
408 qWarning ("MainWindow::load (fn) multimap found...");
411 if (flist.isEmpty() )
413 QMessageBox::critical( 0, tr( "Critical Load Error" ),
414 tr("Couldn't find a map (*.xml) in .vym archive.\n"));
417 } //file doesn't exist
419 xmlfile=mfile.name();
422 QFile file( xmlfile);
424 // I am paranoid: file should exist anyway
425 // according to check in mainwindow.
428 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
429 tr(QString("Couldn't open map %1").arg(file.name())));
433 bool blockSaveStateOrg=blockSaveState;
434 blockReposition=true;
436 QXmlInputSource source( file);
437 QXmlSimpleReader reader;
438 reader.setContentHandler( handler );
439 reader.setErrorHandler( handler );
440 handler->setModel ( this);
443 // We need to set the tmpDir in order to load files with rel. path
448 tmpdir=fname.left(fname.findRev("/",-1));
449 handler->setTmpDir (tmpdir);
450 handler->setInputFile (file.name());
451 handler->setLoadMode (lmode);
452 bool ok = reader.parse( source );
453 blockReposition=false;
454 blockSaveState=blockSaveStateOrg;
458 reposition(); // FIXME VM reposition the view instead...
465 autosaveTimer->stop();
468 // Reset timestamp to check for later updates of file
469 fileChangedTime=QFileInfo (destPath).lastModified();
472 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
473 tr( handler->errorProtocol() ) );
475 // Still return "success": the map maybe at least
476 // partially read by the parser
481 removeDir (QDir(tmpZipDir));
483 // Restore original zip state
490 ErrorCode VymModel::save (const SaveMode &savemode)
494 QString safeFilePath;
496 ErrorCode err=success;
500 mapFileName=mapName+".xml";
502 // use name given by user, even if he chooses .doc
503 mapFileName=fileName;
505 // Look, if we should zip the data:
508 QMessageBox mb( vymName,
509 tr("The map %1\ndid not use the compressed "
510 "vym file format.\nWriting it uncompressed will also write images \n"
511 "and flags and thus may overwrite files in the "
512 "given directory\n\nDo you want to write the map").arg(filePath),
513 QMessageBox::Warning,
514 QMessageBox::Yes | QMessageBox::Default,
516 QMessageBox::Cancel | QMessageBox::Escape);
517 mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
518 mb.setButtonText( QMessageBox::No, tr("uncompressed") );
519 mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
522 case QMessageBox::Yes:
523 // save compressed (default file format)
526 case QMessageBox::No:
530 case QMessageBox::Cancel:
537 // First backup existing file, we
538 // don't want to add to old zip archives
542 if ( settings.value ("/mapeditor/writeBackupFile").toBool())
544 QString backupFileName(destPath + "~");
545 QFile backupFile(backupFileName);
546 if (backupFile.exists() && !backupFile.remove())
548 QMessageBox::warning(0, tr("Save Error"),
549 tr("%1\ncould not be removed before saving").arg(backupFileName));
551 else if (!f.rename(backupFileName))
553 QMessageBox::warning(0, tr("Save Error"),
554 tr("%1\ncould not be renamed before saving").arg(destPath));
561 // Create temporary directory for packing
563 tmpZipDir=makeTmpDir (ok,"vym-zip");
566 QMessageBox::critical( 0, tr( "Critical Load Error" ),
567 tr("Couldn't create temporary directory before save\n"));
571 safeFilePath=filePath;
572 setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
575 // Create mapName and fileDir
576 makeSubDirs (fileDir);
579 if (savemode==CompleteMap || selection.isEmpty())
582 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
585 autosaveTimer->stop();
590 if (selectionType()==TreeItem::Image)
593 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),getSelectedBranch());
594 // TODO take care of multiselections
597 if (!saveStringToDisk(fileDir+mapFileName,saveFile))
600 qWarning ("ME::saveStringToDisk failed!");
606 if (err==success) err=zipDir (tmpZipDir,destPath);
609 removeDir (QDir(tmpZipDir));
611 // Restore original filepath outside of tmp zip dir
612 setFilePath (safeFilePath);
616 fileChangedTime=QFileInfo (destPath).lastModified();
620 void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)
622 QString pathDir=path.left(path.findRev("/"));
628 // We need to parse saved XML data
629 parseVYMHandler handler;
630 QXmlInputSource source( file);
631 QXmlSimpleReader reader;
632 reader.setContentHandler( &handler );
633 reader.setErrorHandler( &handler );
634 handler.setModel ( this);
635 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
636 if (undoSel.isEmpty())
640 handler.setLoadMode (NewMap);
644 handler.setLoadMode (ImportReplace);
646 blockReposition=true;
647 bool ok = reader.parse( source );
648 blockReposition=false;
651 // This should never ever happen
652 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
653 handler.errorProtocol());
656 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
659 void VymModel::addMapInsertInt (const QString &path, int pos)
661 BranchObj *sel=getSelectedBranch();
664 QString pathDir=path.left(path.findRev("/"));
670 // We need to parse saved XML data
671 parseVYMHandler handler;
672 QXmlInputSource source( file);
673 QXmlSimpleReader reader;
674 reader.setContentHandler( &handler );
675 reader.setErrorHandler( &handler );
676 handler.setModel (this);
677 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
678 handler.setLoadMode (ImportAdd);
679 blockReposition=true;
680 bool ok = reader.parse( source );
681 blockReposition=false;
684 // This should never ever happen
685 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
686 handler.errorProtocol());
688 if (sel->getDepth()>0)
689 sel->getLastBranch()->linkTo (sel,pos);
691 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
695 FloatImageObj* VymModel::loadFloatImageInt (QString fn)
697 TreeItem *fi=createImage();
700 FloatImageObj *fio= ((FloatImageObj*)fi->getLMO());
708 void VymModel::loadFloatImage ()
710 BranchObj *bo=getSelectedBranch();
714 Q3FileDialog *fd=new Q3FileDialog( NULL);
715 fd->setMode (Q3FileDialog::ExistingFiles);
716 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
717 ImagePreview *p =new ImagePreview (fd);
718 fd->setContentsPreviewEnabled( TRUE );
719 fd->setContentsPreview( p, p );
720 fd->setPreviewMode( Q3FileDialog::Contents );
721 fd->setCaption(vymName+" - " +tr("Load image"));
722 fd->setDir (lastImageDir);
725 if ( fd->exec() == QDialog::Accepted )
727 // TODO loadFIO in QT4 use: lastImageDir=fd->directory();
728 lastImageDir=QDir (fd->dirPath());
731 for (int j=0; j<fd->selectedFiles().count(); j++)
733 s=fd->selectedFiles().at(j);
734 fio=loadFloatImageInt (s);
737 (LinkableMapObj*)fio,
740 QString ("loadImage (%1)").arg(s ),
741 QString("Add image %1 to %2").arg(s).arg(getObjectName(bo))
744 // TODO loadFIO error handling
745 qWarning ("Failed to load "+s);
753 void VymModel::saveFloatImageInt (FloatImageObj *fio, const QString &type, const QString &fn)
758 void VymModel::saveFloatImage ()
760 FloatImageObj *fio=selection.getFloatImage();
763 QFileDialog *fd=new QFileDialog( NULL);
764 fd->setFilters (imageIO.getFilters());
765 fd->setCaption(vymName+" - " +tr("Save image"));
766 fd->setFileMode( QFileDialog::AnyFile );
767 fd->setDirectory (lastImageDir);
768 // fd->setSelection (fio->getOriginalFilename());
772 if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
774 fn=fd->selectedFiles().at(0);
775 if (QFile (fn).exists() )
777 QMessageBox mb( vymName,
778 tr("The file %1 exists already.\n"
779 "Do you want to overwrite it?").arg(fn),
780 QMessageBox::Warning,
781 QMessageBox::Yes | QMessageBox::Default,
782 QMessageBox::Cancel | QMessageBox::Escape,
783 QMessageBox::NoButton );
785 mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
786 mb.setButtonText( QMessageBox::No, tr("Cancel"));
789 case QMessageBox::Yes:
792 case QMessageBox::Cancel:
799 saveFloatImageInt (fio,fd->selectedFilter(),fn );
806 void VymModel::importDirInt(BranchObj *dst, QDir d)
808 BranchObj *bo=getSelectedBranch();
811 // Traverse directories
812 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
813 QFileInfoList list = d.entryInfoList();
816 for (int i = 0; i < list.size(); ++i)
819 if (fi.fileName() != "." && fi.fileName() != ".." )
822 bo=dst->getLastBranch();
823 bo->setHeading (fi.fileName() );
824 bo->setColor (QColor("blue"));
826 if ( !d.cd(fi.fileName()) )
827 QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
830 // Recursively add subdirs
837 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
838 list = d.entryInfoList();
840 for (int i = 0; i < list.size(); ++i)
844 bo=dst->getLastBranch();
845 bo->setHeading (fi.fileName() );
846 bo->setColor (QColor("black"));
847 if (fi.fileName().right(4) == ".vym" )
848 bo->setVymLink (fi.filePath());
853 void VymModel::importDirInt (const QString &s)
855 BranchObj *bo=getSelectedBranch();
858 saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
865 void VymModel::importDir()
867 BranchObj *bo=getSelectedBranch();
871 filters <<"VYM map (*.vym)";
872 QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
873 fd->setMode (QFileDialog::DirectoryOnly);
874 fd->setFilters (filters);
875 fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
879 if ( fd->exec() == QDialog::Accepted )
881 importDirInt (fd->selectedFile() );
883 //FIXME VM needed? scene()->update();
889 void VymModel::autosave()
894 cout << "VymModel::autosave rejected due to missing filePath\n";
897 QDateTime now=QDateTime().currentDateTime();
899 // Disable autosave, while we have gone back in history
900 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
901 if (redosAvail>0) return;
903 // Also disable autosave for new map without filename
904 if (filePath.isEmpty()) return;
907 if (mapUnsaved &&mapChanged && settings.value ("/mainwindow/autosave/use",true).toBool() )
909 if (QFileInfo(filePath).lastModified()<=fileChangedTime)
910 mainWindow->fileSave (this);
913 cout <<" ME::autosave rejected, file on disk is newer than last save.\n";
918 void VymModel::fileChanged()
920 // Check if file on disk has changed meanwhile
921 if (!filePath.isEmpty())
923 QDateTime tmod=QFileInfo (filePath).lastModified();
924 if (tmod>fileChangedTime)
926 // FIXME VM switch to current mapeditor and finish lineedits...
927 QMessageBox mb( vymName,
928 tr("The file of the map on disk has changed:\n\n"
929 " %1\n\nDo you want to reload that map with the new file?").arg(filePath),
930 QMessageBox::Question,
932 QMessageBox::Cancel | QMessageBox::Default,
933 QMessageBox::NoButton );
935 mb.setButtonText( QMessageBox::Yes, tr("Reload"));
936 mb.setButtonText( QMessageBox::No, tr("Ignore"));
939 case QMessageBox::Yes:
941 load (filePath,NewMap,fileType);
942 case QMessageBox::Cancel:
943 fileChangedTime=tmod; // allow autosave to overwrite newer file!
949 bool VymModel::isDefault()
954 void VymModel::makeDefault()
960 bool VymModel::hasChanged()
965 void VymModel::setChanged()
967 cout << "VM::setChanged()\n";
970 autosaveTimer->start(settings.value("/mainwindow/autosave/ms/",300000).toInt());
971 cout <<" timer started with "<<settings.value("/mainwindow/autosave/ms/",300000).toInt()<<endl;
980 QString VymModel::getObjectName (const LinkableMapObj *lmo)
983 if (!lmo) return QString("Error: NULL has no name!");
985 if ((typeid(*lmo) == typeid(BranchObj) ||
986 typeid(*lmo) == typeid(MapCenterObj)))
989 s=(((BranchObj*)lmo)->getHeading());
990 if (s=="") s="unnamed";
991 return QString("branch (%1)").arg(s);
993 if ((typeid(*lmo) == typeid(FloatImageObj) ))
994 return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
995 return QString("Unknown type has no name!");
998 void VymModel::redo()
1000 // Can we undo at all?
1001 if (redosAvail<1) return;
1003 bool blockSaveStateOrg=blockSaveState;
1004 blockSaveState=true;
1008 if (undosAvail<stepsTotal) undosAvail++;
1010 if (curStep>stepsTotal) curStep=1;
1011 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1012 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1013 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1014 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1015 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1016 QString version=undoSet.readEntry ("/history/version");
1018 /* TODO Maybe check for version, if we save the history
1019 if (!checkVersion(version))
1020 QMessageBox::warning(0,tr("Warning"),
1021 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1024 // Find out current undo directory
1025 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1029 cout << "VymModel::redo() begin\n";
1030 cout << " undosAvail="<<undosAvail<<endl;
1031 cout << " redosAvail="<<redosAvail<<endl;
1032 cout << " curStep="<<curStep<<endl;
1033 cout << " ---------------------------"<<endl;
1034 cout << " comment="<<comment.toStdString()<<endl;
1035 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1036 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1037 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1038 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1039 cout << " ---------------------------"<<endl<<endl;
1042 // select object before redo
1043 if (!redoSelection.isEmpty())
1044 select (redoSelection);
1047 parseAtom (redoCommand);
1050 blockSaveState=blockSaveStateOrg;
1052 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1053 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1054 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1055 undoSet.writeSettings(histPath);
1057 mainWindow->updateHistory (undoSet);
1060 /* TODO remove testing
1061 cout << "ME::redo() end\n";
1062 cout << " undosAvail="<<undosAvail<<endl;
1063 cout << " redosAvail="<<redosAvail<<endl;
1064 cout << " curStep="<<curStep<<endl;
1065 cout << " ---------------------------"<<endl<<endl;
1071 bool VymModel::isRedoAvailable()
1073 if (undoSet.readNumEntry("/history/redosAvail",0)>0)
1079 void VymModel::undo()
1081 // Can we undo at all?
1082 if (undosAvail<1) return;
1084 mainWindow->statusMessage (tr("Autosave disabled during undo."));
1086 bool blockSaveStateOrg=blockSaveState;
1087 blockSaveState=true;
1089 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1090 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1091 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1092 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1093 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1094 QString version=undoSet.readEntry ("/history/version");
1096 /* TODO Maybe check for version, if we save the history
1097 if (!checkVersion(version))
1098 QMessageBox::warning(0,tr("Warning"),
1099 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1102 // Find out current undo directory
1103 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1105 // select object before undo
1106 if (!undoSelection.isEmpty())
1107 select (undoSelection);
1111 cout << "VymModel::undo() begin\n";
1112 cout << " undosAvail="<<undosAvail<<endl;
1113 cout << " redosAvail="<<redosAvail<<endl;
1114 cout << " curStep="<<curStep<<endl;
1115 cout << " ---------------------------"<<endl;
1116 cout << " comment="<<comment.toStdString()<<endl;
1117 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1118 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1119 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1120 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1121 cout << " ---------------------------"<<endl<<endl;
1123 parseAtom (undoCommand);
1128 if (curStep<1) curStep=stepsTotal;
1132 blockSaveState=blockSaveStateOrg;
1133 /* TODO remove testing
1134 cout << "VymModel::undo() end\n";
1135 cout << " undosAvail="<<undosAvail<<endl;
1136 cout << " redosAvail="<<redosAvail<<endl;
1137 cout << " curStep="<<curStep<<endl;
1138 cout << " ---------------------------"<<endl<<endl;
1141 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1142 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1143 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1144 undoSet.writeSettings(histPath);
1146 mainWindow->updateHistory (undoSet);
1149 ensureSelectionVisible();
1152 bool VymModel::isUndoAvailable()
1154 if (undoSet.readNumEntry("/history/undosAvail",0)>0)
1160 void VymModel::gotoHistoryStep (int i)
1162 // Restore variables
1163 int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
1164 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
1166 if (i<0) i=undosAvail+redosAvail;
1168 // Clicking above current step makes us undo things
1171 for (int j=0; j<undosAvail-i; j++) undo();
1174 // Clicking below current step makes us redo things
1176 for (int j=undosAvail; j<i; j++)
1178 if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
1182 // And ignore clicking the current row ;-)
1186 QString VymModel::getHistoryPath()
1188 QString histName(QString("history-%1").arg(curStep));
1189 return (tmpMapDir+"/"+histName);
1192 void VymModel::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
1194 sendData(redoCom); //FIXME testing
1199 if (blockSaveState) return;
1201 if (debug) cout << "ME::saveState() for "<<qPrintable (mapName)<<endl;
1203 // Find out current undo directory
1204 if (undosAvail<stepsTotal) undosAvail++;
1206 if (curStep>stepsTotal) curStep=1;
1208 QString backupXML="";
1209 QString histDir=getHistoryPath();
1210 QString bakMapPath=histDir+"/map.xml";
1212 // Create histDir if not available
1215 makeSubDirs (histDir);
1217 // Save depending on how much needs to be saved
1219 backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
1221 QString undoCommand="";
1222 if (savemode==UndoCommand)
1224 undoCommand=undoCom;
1226 else if (savemode==PartOfMap )
1228 undoCommand=undoCom;
1229 undoCommand.replace ("PATH",bakMapPath);
1232 if (!backupXML.isEmpty())
1233 // Write XML Data to disk
1234 saveStringToDisk (bakMapPath,backupXML);
1236 // We would have to save all actions in a tree, to keep track of
1237 // possible redos after a action. Possible, but we are too lazy: forget about redos.
1240 // Write the current state to disk
1241 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1242 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1243 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1244 undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
1245 undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
1246 undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
1247 undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
1248 undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
1249 undoSet.setEntry (QString("/history/version"),vymVersion);
1250 undoSet.writeSettings(histPath);
1254 // TODO remove after testing
1255 //cout << " into="<< histPath.toStdString()<<endl;
1256 cout << " stepsTotal="<<stepsTotal<<
1257 ", undosAvail="<<undosAvail<<
1258 ", redosAvail="<<redosAvail<<
1259 ", curStep="<<curStep<<endl;
1260 cout << " ---------------------------"<<endl;
1261 cout << " comment="<<comment.toStdString()<<endl;
1262 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1263 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1264 cout << " redoCom="<<redoCom.toStdString()<<endl;
1265 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1266 if (saveSel) cout << " saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
1267 cout << " ---------------------------"<<endl;
1270 mainWindow->updateHistory (undoSet);
1276 void VymModel::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
1278 // save the selected part of the map, Undo will replace part of map
1279 QString undoSelection="";
1281 undoSelection=getSelectString(undoSel);
1283 qWarning ("VymModel::saveStateChangingPart no undoSel given!");
1284 QString redoSelection="";
1286 redoSelection=getSelectString(undoSel);
1288 qWarning ("VymModel::saveStateChangingPart no redoSel given!");
1291 saveState (PartOfMap,
1292 undoSelection, "addMapReplace (\"PATH\")",
1298 void VymModel::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
1302 qWarning ("VymModel::saveStateRemovingPart no redoSel given!");
1305 QString undoSelection=getSelectString (redoSel->getParObj());
1306 QString redoSelection=getSelectString(redoSel);
1307 if (typeid(*redoSel) == typeid(BranchObj) )
1309 // save the selected branch of the map, Undo will insert part of map
1310 saveState (PartOfMap,
1311 undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
1312 redoSelection, "delete ()",
1319 void VymModel::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment)
1321 // "Normal" savestate: save commands, selections and comment
1322 // so just save commands for undo and redo
1323 // and use current selection
1325 QString redoSelection="";
1326 if (redoSel) redoSelection=getSelectString(redoSel);
1327 QString undoSelection="";
1328 if (undoSel) undoSelection=getSelectString(undoSel);
1330 saveState (UndoCommand,
1337 void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment)
1339 // "Normal" savestate: save commands, selections and comment
1340 // so just save commands for undo and redo
1341 // and use current selection
1342 saveState (UndoCommand,
1349 void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment)
1351 // "Normal" savestate applied to model (no selection needed):
1352 // save commands and comment
1353 saveState (UndoCommand,
1361 QGraphicsScene* VymModel::getScene ()
1366 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
1368 LinkableMapObj *lmo;
1370 for (int i=0;i<mapCenters.count(); i++)
1372 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
1373 if (lmo) return lmo;
1378 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
1380 LinkableMapObj *lmo;
1386 part=s.section(",",0,0);
1388 num=part.right(part.length() - 3);
1389 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
1390 return mapCenters.at(num.toInt() );
1393 for (int i=0; i<mapCenters.count(); i++)
1395 lmo=mapCenters.at(i)->findObjBySelect(s);
1396 if (lmo) return lmo;
1401 LinkableMapObj* VymModel::findID (const QString &s)
1403 LinkableMapObj *lmo;
1404 for (int i=0; i<mapCenters.count(); i++)
1406 lmo=mapCenters.at(i)->findID (s);
1407 if (lmo) return lmo;
1412 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
1416 for (int i=0; i<mapCenters.count(); i++)
1417 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
1421 //////////////////////////////////////////////
1423 //////////////////////////////////////////////
1424 void VymModel::setVersion (const QString &s)
1429 void VymModel::setAuthor (const QString &s)
1432 QString ("setMapAuthor (\"%1\")").arg(author),
1433 QString ("setMapAuthor (\"%1\")").arg(s),
1434 QString ("Set author of map to \"%1\"").arg(s)
1440 QString VymModel::getAuthor()
1445 void VymModel::setComment (const QString &s)
1448 QString ("setMapComment (\"%1\")").arg(comment),
1449 QString ("setMapComment (\"%1\")").arg(s),
1450 QString ("Set comment of map")
1456 QString VymModel::getComment ()
1461 QString VymModel::getDate ()
1463 return QDate::currentDate().toString ("yyyy-MM-dd");
1466 int VymModel::countBranches() // FIXME Optimize this: use internal counter instead of going through whole map each time...
1470 TreeItem *prev=NULL;
1482 void VymModel::setHeading(const QString &s)
1484 BranchObj *sel=getSelectedBranch();
1489 "setHeading (\""+sel->getHeading()+"\")",
1491 "setHeading (\""+s+"\")",
1492 QString("Set heading of %1 to \"%2\"").arg(getObjectName(sel)).arg(s) );
1493 sel->setHeading(s );
1494 /* FIXME testing only
1496 TreeItem *ti=getSelectedItem();
1500 //FIXME VM ix is wrong ModelIndex below, ix2 is (hopefully) correct:
1501 //QModelIndex ix=index( ti->row(), ti->column(), index (0,0,QModelIndex()) );
1502 //FIXME VM testing only cout <<"VM::setHeading s="<<s.toStdString()<<" ti="<<ti<<" r,c=("<<ti->row()<<","<<ti->column()<<")"<<endl;
1503 QModelIndex ix2=index (ti);
1504 emit (dataChanged ( ix2,ix2));
1508 cout << "Warning: VM::setHeading ti==NULL\n";
1511 //cout <<" (r,c)=("<<ix2.row()<<","<<ix2.column()<<")"<<endl;
1515 ensureSelectionVisible();
1519 BranchObj* VymModel::findText (QString s, bool cs)
1522 QTextDocument::FindFlags flags=0;
1523 if (cs) flags=QTextDocument::FindCaseSensitively;
1526 { // Nothing found or new find process
1528 // nothing found, start again
1532 next (findCurrent,findPrevious,d);
1534 bool searching=true;
1535 bool foundNote=false;
1536 while (searching && !EOFind)
1540 // Searching in Note
1541 if (findCurrent->getNote().contains(s,cs))
1543 select (findCurrent);
1545 if (getSelectedBranch()!=itFind)
1548 ensureSelectionVisible();
1551 if (textEditor->findText(s,flags))
1557 // Searching in Heading
1558 if (searching && findCurrent->getHeading().contains (s,cs) )
1560 select(findCurrent);
1566 if (!next(findCurrent,findPrevious,d) )
1569 //cout <<"still searching... "<<qPrintable( itFind->getHeading())<<endl;
1572 return getSelectedBranch();
1577 void VymModel::findReset()
1578 { // Necessary if text to find changes during a find process
1586 void VymModel::setScene (QGraphicsScene *s)
1588 cout << "VM::setscene scene="<<s<<endl;
1589 mapScene=s; // FIXME VM should not be necessary anymore, move all occurences to MapEditor
1590 //init(); // Here we have a mapScene set,
1591 // which is (still) needed to create MapCenters
1594 void VymModel::setURL(const QString &url)
1596 BranchObj *bo=getSelectedBranch();
1599 QString oldurl=bo->getURL();
1603 QString ("setURL (\"%1\")").arg(oldurl),
1605 QString ("setURL (\"%1\")").arg(url),
1606 QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
1611 ensureSelectionVisible();
1615 QString VymModel::getURL()
1617 BranchObj *bo=getSelectedBranch();
1619 return bo->getURL();
1624 QStringList VymModel::getURLs()
1627 BranchObj *bo=getSelectedBranch();
1633 if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
1640 void VymModel::linkFloatImageTo(const QString &dstString)
1642 FloatImageObj *fio=selection.getFloatImage();
1645 BranchObj *dst=(BranchObj*)findObjBySelect(dstString);
1646 if (dst && (typeid(*dst)==typeid (BranchObj) ||
1647 typeid(*dst)==typeid (MapCenterObj)))
1649 LinkableMapObj *dstPar=dst->getParObj();
1650 QString parString=getSelectString(dstPar);
1651 QString fioPreSelectString=getSelectString(fio);
1652 QString fioPreParentSelectString=getSelectString (fio->getParObj());
1653 ((BranchObj*)dst)->addFloatImage (fio);
1654 selection.unselect();
1655 ((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
1656 fio=((BranchObj*)dst)->getLastFloatImage();
1659 selection.select(fio);
1661 getSelectString(fio),
1662 QString("linkTo (\"%1\")").arg(fioPreParentSelectString),
1664 QString ("linkTo (\"%1\")").arg(dstString),
1665 QString ("Link floatimage to %1").arg(getObjectName(dst)));
1671 void VymModel::setFrameType(const FrameObj::FrameType &t)
1673 BranchObj *bo=getSelectedBranch();
1676 QString s=bo->getFrameTypeName();
1677 bo->setFrameType (t);
1678 saveState (bo, QString("setFrameType (\"%1\")").arg(s),
1679 bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
1685 void VymModel::setFrameType(const QString &s)
1687 BranchObj *bo=getSelectedBranch();
1690 saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
1691 bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
1692 bo->setFrameType (s);
1698 void VymModel::setFramePenColor(const QColor &c)
1700 BranchObj *bo=getSelectedBranch();
1703 saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
1704 bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
1705 bo->setFramePenColor (c);
1709 void VymModel::setFrameBrushColor(const QColor &c)
1711 BranchObj *bo=getSelectedBranch();
1714 saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
1715 bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
1716 bo->setFrameBrushColor (c);
1720 void VymModel::setFramePadding (const int &i)
1722 BranchObj *bo=getSelectedBranch();
1725 saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
1726 bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
1727 bo->setFramePadding (i);
1733 void VymModel::setFrameBorderWidth(const int &i)
1735 BranchObj *bo=getSelectedBranch();
1738 saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
1739 bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
1740 bo->setFrameBorderWidth (i);
1746 void VymModel::setIncludeImagesVer(bool b)
1748 BranchObj *bo=getSelectedBranch();
1751 QString u= b ? "false" : "true";
1752 QString r=!b ? "false" : "true";
1756 QString("setIncludeImagesVertically (%1)").arg(u),
1758 QString("setIncludeImagesVertically (%1)").arg(r),
1759 QString("Include images vertically in %1").arg(getObjectName(bo))
1761 bo->setIncludeImagesVer(b);
1766 void VymModel::setIncludeImagesHor(bool b)
1768 BranchObj *bo=getSelectedBranch();
1771 QString u= b ? "false" : "true";
1772 QString r=!b ? "false" : "true";
1776 QString("setIncludeImagesHorizontally (%1)").arg(u),
1778 QString("setIncludeImagesHorizontally (%1)").arg(r),
1779 QString("Include images horizontally in %1").arg(getObjectName(bo))
1781 bo->setIncludeImagesHor(b);
1786 void VymModel::setHideLinkUnselected (bool b)
1788 LinkableMapObj *sel=getSelectedLMO();
1790 (selectionType() == TreeItem::Branch ||
1791 selectionType() == TreeItem::MapCenter ||
1792 selectionType() == TreeItem::Image ))
1794 QString u= b ? "false" : "true";
1795 QString r=!b ? "false" : "true";
1799 QString("setHideLinkUnselected (%1)").arg(u),
1801 QString("setHideLinkUnselected (%1)").arg(r),
1802 QString("Hide link of %1 if unselected").arg(getObjectName(sel))
1804 sel->setHideLinkUnselected(b);
1808 void VymModel::setHideExport(bool b)
1810 BranchObj *bo=getSelectedBranch();
1813 bo->setHideInExport (b);
1814 QString u= b ? "false" : "true";
1815 QString r=!b ? "false" : "true";
1819 QString ("setHideExport (%1)").arg(u),
1821 QString ("setHideExport (%1)").arg(r),
1822 QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bo)).arg (r)
1827 // FIXME VM needed? scene()->update();
1831 void VymModel::toggleHideExport()
1833 BranchObj *bo=getSelectedBranch();
1835 setHideExport ( !bo->hideInExport() );
1839 void VymModel::copy()
1841 LinkableMapObj *sel=getSelectedLMO();
1843 (selectionType() == TreeItem::Branch ||
1844 selectionType() == TreeItem::MapCenter ||
1845 selectionType() == TreeItem::Image ))
1847 if (redosAvail == 0)
1850 QString s=getSelectString(sel);
1851 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel );
1852 curClipboard=curStep;
1855 // Copy also to global clipboard, because we are at last step in history
1856 QString bakMapName(QString("history-%1").arg(curStep));
1857 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1858 copyDir (bakMapDir,clipboardDir );
1860 clipboardEmpty=false;
1866 void VymModel::pasteNoSave(const int &n)
1868 bool old=blockSaveState;
1869 blockSaveState=true;
1870 bool zippedOrg=zipped;
1871 if (redosAvail > 0 || n!=0)
1873 // Use the "historical" buffer
1874 QString bakMapName(QString("history-%1").arg(n));
1875 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1876 load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
1878 // Use the global buffer
1879 load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
1884 void VymModel::paste()
1886 BranchObj *sel=getSelectedBranch();
1889 saveStateChangingPart(
1892 QString ("paste (%1)").arg(curClipboard),
1893 QString("Paste to %1").arg( getObjectName(sel))
1900 void VymModel::cut()
1902 LinkableMapObj *sel=getSelectedLMO();
1903 if ( sel && (selectionType() == TreeItem::Branch ||
1904 selectionType()==TreeItem::MapCenter ||
1905 selectionType()==TreeItem::Image))
1907 /* No savestate! savestate is called in cutNoSave
1908 saveStateChangingPart(
1912 QString("Cut %1").arg(getObjectName(sel ))
1921 void VymModel::moveBranchUp()
1923 BranchObj* bo=getSelectedBranch();
1927 if (!bo->canMoveBranchUp()) return;
1928 par=(BranchObj*)(bo->getParObj());
1929 BranchObj *obo=par->moveBranchUp (bo); // bo will be the one below selection
1930 saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
1932 //FIXME VM needed? scene()->update();
1934 ensureSelectionVisible();
1938 void VymModel::moveBranchDown()
1940 BranchObj* bo=getSelectedBranch();
1944 if (!bo->canMoveBranchDown()) return;
1945 par=(BranchObj*)(bo->getParObj());
1946 BranchObj *obo=par->moveBranchDown(bo); // bo will be the one above selection
1947 saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
1949 //FIXME VM needed? scene()->update();
1951 ensureSelectionVisible();
1955 void VymModel::sortChildren()
1957 BranchObj* bo=getSelectedBranch();
1960 if(bo->countBranches()>1)
1962 saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
1965 ensureSelectionVisible();
1970 void VymModel::createMapCenter()
1972 MapCenterObj *mco=addMapCenter (QPointF (0,0) );
1976 void VymModel::createBranch()
1978 addNewBranchInt (-2);
1982 TreeItem* VymModel::createImage()
1984 BranchObj *bo=getSelectedBranch();
1987 FloatImageObj *newfio=bo->addFloatImage(); // FIXME VM Old model, merge with below
1990 QList<QVariant> cData;
1991 cData << "VM:createImage" << "undef"<<"undef";
1992 TreeItem *parti=bo->getTreeItem();
1993 TreeItem *ti=new TreeItem (cData,parti);
1994 ti->setLMO (newfio);
1995 ti->setType (TreeItem::Image);
1996 parti->appendChild (ti);
2000 newfio->setTreeItem (ti);
2001 select (newfio); // FIXME VM really needed here?
2008 MapCenterObj* VymModel::addMapCenter ()
2010 MapCenterObj *mco=addMapCenter (contextPos);
2011 cout <<"VM::addMCO () mapScene="<<mapScene<<endl;
2012 //FIXME selection.select (mco);
2014 ensureSelectionVisible();
2019 QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
2020 QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
2025 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
2027 MapCenterObj *mapCenter = new MapCenterObj(mapScene,this);
2028 mapCenter->setMapEditor(mapEditor); //FIXME VM needed to get defLinkStyle, mapLinkColorHint ... for later added objects
2029 mapCenter->move (absPos);
2030 mapCenter->setVisibility (true);
2031 mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
2032 mapCenters.append(mapCenter);
2035 QList<QVariant> cData;
2036 cData << "VM:addMapCenter" << "undef"<<"undef";
2037 TreeItem *ti=new TreeItem (cData,rootItem);
2038 ti->setLMO (mapCenter);
2039 ti->setType (TreeItem::MapCenter);
2040 mapCenter->setTreeItem (ti);
2041 rootItem->appendChild (ti);
2046 MapCenterObj* VymModel::removeMapCenter(MapCenterObj* mco)
2048 int i=mapCenters.indexOf (mco);
2051 mapCenters.removeAt (i);
2053 if (i>0) return mapCenters.at(i-1); // Return previous MCO
2058 MapCenterObj* VymModel::getLastMapCenter()
2060 if (mapCenters.size()>0)
2061 return mapCenters.last();
2068 BranchObj* VymModel::addNewBranchInt(int num)
2070 // Depending on pos:
2071 // -3 insert in children of parent above selection
2072 // -2 add branch to selection
2073 // -1 insert in children of parent below selection
2074 // 0..n insert in children of parent at pos
2075 BranchObj *newbo=NULL;
2076 BranchObj *bo=getSelectedBranch();
2081 // save scroll state. If scrolled, automatically select
2082 // new branch in order to tmp unscroll parent...
2083 newbo=bo->addBranch();
2086 QList<QVariant> cData;
2087 cData << "VM:createBranch" << "undef"<<"undef";
2088 TreeItem *parti=bo->getTreeItem();
2089 TreeItem *ti=new TreeItem (cData,parti);
2091 ti->setType (TreeItem::Branch);
2092 parti->appendChild (ti);
2096 newbo->setTreeItem (ti);
2097 select (newbo); // FIXME VM really needed here?
2103 bo=(BranchObj*)bo->getParObj();
2104 if (bo) newbo=bo->insertBranch(num);
2108 bo=(BranchObj*)bo->getParObj();
2109 if (bo) newbo=bo->insertBranch(num);
2115 BranchObj* VymModel::addNewBranch(int pos)
2117 // Different meaning than num in addNewBranchInt!
2121 BranchObj *bo = getSelectedBranch();
2122 BranchObj *newbo=NULL;
2126 // FIXME VM do we still need this in model? setCursor (Qt::ArrowCursor);
2128 newbo=addNewBranchInt (pos-2);
2136 QString ("addBranch (%1)").arg(pos),
2137 QString ("Add new branch to %1").arg(getObjectName(bo)));
2140 // selection.update(); FIXME
2141 latestSelectionString=getSelectString(newbo);
2142 // In Network mode, the client needs to know where the new branch is,
2143 // so we have to pass on this information via saveState.
2144 // TODO: Get rid of this positioning workaround
2145 QString ps=qpointfToString (newbo->getAbsPos());
2146 sendData ("selectLatestAdded ()");
2147 sendData (QString("move %1").arg(ps));
2155 BranchObj* VymModel::addNewBranchBefore()
2157 BranchObj *newbo=NULL;
2158 BranchObj *bo = getSelectedBranch();
2159 if (bo && selectionType()==TreeItem::Branch)
2160 // We accept no MapCenterObj here, so we _have_ a parent
2162 QPointF p=bo->getRelPos();
2165 BranchObj *parbo=(BranchObj*)(bo->getParObj());
2167 // add below selection
2168 newbo=parbo->insertBranch(bo->getNum()+1);
2171 newbo->move2RelPos (p);
2173 // Move selection to new branch
2174 bo->linkTo (newbo,-1);
2176 saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()",
2177 QString ("Add branch before %1").arg(getObjectName(bo)));
2180 // selection.update(); FIXME
2183 latestSelectionString=selection.getSelectString();
2187 void VymModel::deleteSelection()
2189 BranchObj *bo = getSelectedBranch();
2191 if (bo && selectionType()==TreeItem::MapCenter)
2193 // BranchObj* par=(BranchObj*)(bo->getParObj());
2194 selection.unselect();
2195 /* FIXME VM Note: does saveStateRemovingPart work for MCO? (No parent!)
2196 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2198 bo=removeMapCenter ((MapCenterObj*)bo);
2201 selection.select (bo);
2202 ensureSelectionVisible();
2208 if (bo && selectionType()==TreeItem::Branch)
2210 QModelIndex ix=getSelectedIndex();
2212 BranchObj* par=(BranchObj*)bo->getParObj();
2214 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2215 par->removeBranch(bo);
2217 ensureSelectionVisible();
2222 FloatImageObj *fio=selection.getFloatImage();
2225 BranchObj* par=(BranchObj*)fio->getParObj();
2226 saveStateChangingPart(
2230 QString("Delete %1").arg(getObjectName(fio))
2233 par->removeFloatImage(fio);
2237 ensureSelectionVisible();
2242 void VymModel::deleteKeepChildren()
2244 BranchObj *bo=getSelectedBranch();
2248 par=(BranchObj*)(bo->getParObj());
2250 // Don't use this on mapcenter
2253 // Check if we have childs at all to keep
2254 if (bo->countBranches()==0)
2260 QPointF p=bo->getRelPos();
2261 saveStateChangingPart(
2264 "deleteKeepChildren ()",
2265 QString("Remove %1 and keep its children").arg(getObjectName(bo))
2268 QString sel=getSelectString(bo);
2270 par->removeBranchHere(bo);
2273 getSelectedBranch()->move2RelPos (p);
2278 void VymModel::deleteChildren()
2280 BranchObj *bo=getSelectedBranch();
2283 saveStateChangingPart(
2286 "deleteChildren ()",
2287 QString( "Remove children of branch %1").arg(getObjectName(bo))
2289 bo->removeChildren();
2295 bool VymModel::scrollBranch(BranchObj *bo)
2299 if (bo->isScrolled()) return false;
2300 if (bo->countBranches()==0) return false;
2301 if (bo->getDepth()==0) return false;
2307 QString ("%1 ()").arg(u),
2309 QString ("%1 ()").arg(r),
2310 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2314 // FIXME VM needed? scene()->update();
2320 bool VymModel::unscrollBranch(BranchObj *bo)
2324 if (!bo->isScrolled()) return false;
2325 if (bo->countBranches()==0) return false;
2326 if (bo->getDepth()==0) return false;
2332 QString ("%1 ()").arg(u),
2334 QString ("%1 ()").arg(r),
2335 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2339 // FIXME VM needed? scene()->update();
2345 void VymModel::toggleScroll()
2347 BranchObj *bo=getSelectedBranch();
2348 if (selectionType()==TreeItem::Branch )
2350 if (bo->isScrolled())
2351 unscrollBranch (bo);
2357 void VymModel::unscrollChildren()
2359 BranchObj *bo=getSelectedBranch();
2365 if (bo->isScrolled()) unscrollBranch (bo);
2370 void VymModel::addFloatImage (const QPixmap &img)
2372 BranchObj *bo=getSelectedBranch();
2375 FloatImageObj *fio=bo->addFloatImage();
2377 fio->setOriginalFilename("No original filename (image added by dropevent)");
2378 QString s=getSelectString(bo);
2379 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio );
2380 saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
2382 // FIXME VM needed? scene()->update();
2387 void VymModel::colorBranch (QColor c)
2389 BranchObj *bo=getSelectedBranch();
2394 QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
2396 QString ("colorBranch (\"%1\")").arg(c.name()),
2397 QString("Set color of %1 to %2").arg(getObjectName(bo)).arg(c.name())
2399 bo->setColor(c); // color branch
2403 void VymModel::colorSubtree (QColor c)
2405 BranchObj *bo=getSelectedBranch();
2408 saveStateChangingPart(
2411 QString ("colorSubtree (\"%1\")").arg(c.name()),
2412 QString ("Set color of %1 and children to %2").arg(getObjectName(bo)).arg(c.name())
2414 bo->setColorSubtree (c); // color links, color children
2418 QColor VymModel::getCurrentHeadingColor()
2420 BranchObj *bo=getSelectedBranch();
2421 if (bo) return bo->getColor();
2423 QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
2429 void VymModel::editURL()
2431 BranchObj *bo=getSelectedBranch();
2435 QString text = QInputDialog::getText(
2436 "VYM", tr("Enter URL:"), QLineEdit::Normal,
2437 bo->getURL(), &ok, NULL);
2439 // user entered something and pressed OK
2444 void VymModel::editLocalURL()
2446 BranchObj *bo=getSelectedBranch();
2449 QStringList filters;
2450 filters <<"All files (*)";
2451 filters << tr("Text","Filedialog") + " (*.txt)";
2452 filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
2453 filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
2454 filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
2455 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
2456 fd->setFilters (filters);
2457 fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
2458 fd->setDirectory (lastFileDir);
2459 if (! bo->getVymLink().isEmpty() )
2460 fd->selectFile( bo->getURL() );
2463 if ( fd->exec() == QDialog::Accepted )
2465 lastFileDir=QDir (fd->directory().path());
2466 setURL (fd->selectedFile() );
2472 void VymModel::editHeading2URL()
2474 BranchObj *bo=getSelectedBranch();
2476 setURL (bo->getHeading());
2479 void VymModel::editBugzilla2URL()
2481 BranchObj *bo=getSelectedBranch();
2484 QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
2489 void VymModel::editFATE2URL()
2491 BranchObj *bo=getSelectedBranch();
2494 QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
2497 "setURL (\""+bo->getURL()+"\")",
2499 "setURL (\""+url+"\")",
2500 QString("Use heading of %1 as link to FATE").arg(getObjectName(bo))
2507 void VymModel::editVymLink()
2509 BranchObj *bo=getSelectedBranch();
2512 QStringList filters;
2513 filters <<"VYM map (*.vym)";
2514 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
2515 fd->setFilters (filters);
2516 fd->setCaption(vymName+" - " +tr("Link to another map"));
2517 fd->setDirectory (lastFileDir);
2518 if (! bo->getVymLink().isEmpty() )
2519 fd->selectFile( bo->getVymLink() );
2523 if ( fd->exec() == QDialog::Accepted )
2525 lastFileDir=QDir (fd->directory().path());
2528 "setVymLink (\""+bo->getVymLink()+"\")",
2530 "setVymLink (\""+fd->selectedFile()+"\")",
2531 QString("Set vymlink of %1 to %2").arg(getObjectName(bo)).arg(fd->selectedFile())
2533 setVymLink (fd->selectedFile() ); // FIXME ok?
2538 void VymModel::setVymLink (const QString &s)
2540 // Internal function, no saveState needed
2541 BranchObj *bo=getSelectedBranch();
2548 ensureSelectionVisible();
2552 void VymModel::deleteVymLink()
2554 BranchObj *bo=getSelectedBranch();
2559 "setVymLink (\""+bo->getVymLink()+"\")",
2561 "setVymLink (\"\")",
2562 QString("Unset vymlink of %1").arg(getObjectName(bo))
2564 bo->setVymLink ("" );
2567 // FIXME VM needed? scene()->update();
2571 QString VymModel::getVymLink()
2573 BranchObj *bo=getSelectedBranch();
2575 return bo->getVymLink();
2581 QStringList VymModel::getVymLinks()
2584 BranchObj *bo=getSelectedBranch();
2590 if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
2598 void VymModel::followXLink(int i)
2600 BranchObj *bo=getSelectedBranch();
2603 bo=bo->XLinkTargetAt(i);
2606 selection.select(bo);
2607 ensureSelectionVisible();
2612 void VymModel::editXLink(int i) // FIXME VM missing saveState
2614 BranchObj *bo=getSelectedBranch();
2617 XLinkObj *xlo=bo->XLinkAt(i);
2620 EditXLinkDialog dia;
2622 dia.setSelection(bo);
2623 if (dia.exec() == QDialog::Accepted)
2625 if (dia.useSettingsGlobal() )
2627 setMapDefXLinkColor (xlo->getColor() );
2628 setMapDefXLinkWidth (xlo->getWidth() );
2630 if (dia.deleteXLink())
2631 bo->deleteXLinkAt(i);
2641 //////////////////////////////////////////////
2643 //////////////////////////////////////////////
2645 void VymModel::parseAtom(const QString &atom)
2647 BranchObj *selb=getSelectedBranch();
2653 // Split string s into command and parameters
2654 parser.parseAtom (atom);
2655 QString com=parser.getCommand();
2657 // External commands
2658 /////////////////////////////////////////////////////////////////////
2659 if (com=="addBranch")
2661 if (selection.isEmpty())
2663 parser.setError (Aborted,"Nothing selected");
2666 parser.setError (Aborted,"Type of selection is not a branch");
2671 if (parser.checkParCount(pl))
2673 if (parser.parCount()==0)
2677 n=parser.parInt (ok,0);
2678 if (ok ) addNewBranch (n);
2682 /////////////////////////////////////////////////////////////////////
2683 } else if (com=="addBranchBefore")
2685 if (selection.isEmpty())
2687 parser.setError (Aborted,"Nothing selected");
2690 parser.setError (Aborted,"Type of selection is not a branch");
2693 if (parser.parCount()==0)
2695 addNewBranchBefore ();
2698 /////////////////////////////////////////////////////////////////////
2699 } else if (com==QString("addMapCenter"))
2701 if (parser.checkParCount(2))
2703 x=parser.parDouble (ok,0);
2706 y=parser.parDouble (ok,1);
2707 if (ok) addMapCenter (QPointF(x,y));
2710 /////////////////////////////////////////////////////////////////////
2711 } else if (com==QString("addMapReplace"))
2713 if (selection.isEmpty())
2715 parser.setError (Aborted,"Nothing selected");
2718 parser.setError (Aborted,"Type of selection is not a branch");
2719 } else if (parser.checkParCount(1))
2721 //s=parser.parString (ok,0); // selection
2722 t=parser.parString (ok,0); // path to map
2723 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2724 addMapReplaceInt(getSelectString(selb),t);
2726 /////////////////////////////////////////////////////////////////////
2727 } else if (com==QString("addMapInsert"))
2729 if (selection.isEmpty())
2731 parser.setError (Aborted,"Nothing selected");
2734 parser.setError (Aborted,"Type of selection is not a branch");
2737 if (parser.checkParCount(2))
2739 t=parser.parString (ok,0); // path to map
2740 n=parser.parInt(ok,1); // position
2741 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2742 addMapInsertInt(t,n);
2745 /////////////////////////////////////////////////////////////////////
2746 } else if (com=="clearFlags")
2748 if (selection.isEmpty() )
2750 parser.setError (Aborted,"Nothing selected");
2753 parser.setError (Aborted,"Type of selection is not a branch");
2754 } else if (parser.checkParCount(0))
2756 selb->clearStandardFlags();
2757 selb->updateFlagsToolbar();
2759 /////////////////////////////////////////////////////////////////////
2760 } else if (com=="colorBranch")
2762 if (selection.isEmpty())
2764 parser.setError (Aborted,"Nothing selected");
2767 parser.setError (Aborted,"Type of selection is not a branch");
2768 } else if (parser.checkParCount(1))
2770 QColor c=parser.parColor (ok,0);
2771 if (ok) colorBranch (c);
2773 /////////////////////////////////////////////////////////////////////
2774 } else if (com=="colorSubtree")
2776 if (selection.isEmpty())
2778 parser.setError (Aborted,"Nothing selected");
2781 parser.setError (Aborted,"Type of selection is not a branch");
2782 } else if (parser.checkParCount(1))
2784 QColor c=parser.parColor (ok,0);
2785 if (ok) colorSubtree (c);
2787 /////////////////////////////////////////////////////////////////////
2788 } else if (com=="copy")
2790 if (selection.isEmpty())
2792 parser.setError (Aborted,"Nothing selected");
2795 parser.setError (Aborted,"Type of selection is not a branch");
2796 } else if (parser.checkParCount(0))
2798 //FIXME missing action for copy
2800 /////////////////////////////////////////////////////////////////////
2801 } else if (com=="cut")
2803 if (selection.isEmpty())
2805 parser.setError (Aborted,"Nothing selected");
2806 } else if ( selectionType()!=TreeItem::Branch &&
2807 selectionType()!=TreeItem::MapCenter &&
2808 selectionType()!=TreeItem::Image )
2810 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2811 } else if (parser.checkParCount(0))
2815 /////////////////////////////////////////////////////////////////////
2816 } else if (com=="delete")
2818 if (selection.isEmpty())
2820 parser.setError (Aborted,"Nothing selected");
2822 /*else if (selectionType() != TreeItem::Branch && selectionType() != TreeItem::Image )
2824 parser.setError (Aborted,"Type of selection is wrong.");
2827 else if (parser.checkParCount(0))
2831 /////////////////////////////////////////////////////////////////////
2832 } else if (com=="deleteKeepChildren")
2834 if (selection.isEmpty())
2836 parser.setError (Aborted,"Nothing selected");
2839 parser.setError (Aborted,"Type of selection is not a branch");
2840 } else if (parser.checkParCount(0))
2842 deleteKeepChildren();
2844 /////////////////////////////////////////////////////////////////////
2845 } else if (com=="deleteChildren")
2847 if (selection.isEmpty())
2849 parser.setError (Aborted,"Nothing selected");
2852 parser.setError (Aborted,"Type of selection is not a branch");
2853 } else if (parser.checkParCount(0))
2857 /////////////////////////////////////////////////////////////////////
2858 } else if (com=="exportASCII")
2862 if (parser.parCount()>=1)
2863 // Hey, we even have a filename
2864 fname=parser.parString(ok,0);
2867 parser.setError (Aborted,"Could not read filename");
2870 exportASCII (fname,false);
2872 /////////////////////////////////////////////////////////////////////
2873 } else if (com=="exportImage")
2877 if (parser.parCount()>=2)
2878 // Hey, we even have a filename
2879 fname=parser.parString(ok,0);
2882 parser.setError (Aborted,"Could not read filename");
2885 QString format="PNG";
2886 if (parser.parCount()>=2)
2888 format=parser.parString(ok,1);
2890 exportImage (fname,false,format);
2892 /////////////////////////////////////////////////////////////////////
2893 } else if (com=="exportXHTML")
2897 if (parser.parCount()>=2)
2898 // Hey, we even have a filename
2899 fname=parser.parString(ok,1);
2902 parser.setError (Aborted,"Could not read filename");
2905 exportXHTML (fname,false);
2907 /////////////////////////////////////////////////////////////////////
2908 } else if (com=="exportXML")
2912 if (parser.parCount()>=2)
2913 // Hey, we even have a filename
2914 fname=parser.parString(ok,1);
2917 parser.setError (Aborted,"Could not read filename");
2920 exportXML (fname,false);
2922 /////////////////////////////////////////////////////////////////////
2923 } else if (com=="importDir")
2925 if (selection.isEmpty())
2927 parser.setError (Aborted,"Nothing selected");
2930 parser.setError (Aborted,"Type of selection is not a branch");
2931 } else if (parser.checkParCount(1))
2933 s=parser.parString(ok,0);
2934 if (ok) importDirInt(s);
2936 /////////////////////////////////////////////////////////////////////
2937 } else if (com=="linkTo")
2939 if (selection.isEmpty())
2941 parser.setError (Aborted,"Nothing selected");
2944 if (parser.checkParCount(4))
2946 // 0 selectstring of parent
2947 // 1 num in parent (for branches)
2948 // 2,3 x,y of mainbranch or mapcenter
2949 s=parser.parString(ok,0);
2950 LinkableMapObj *dst=findObjBySelect (s);
2953 if (typeid(*dst) == typeid(BranchObj) )
2955 // Get number in parent
2956 n=parser.parInt (ok,1);
2959 selb->linkTo ((BranchObj*)(dst),n);
2962 } else if (typeid(*dst) == typeid(MapCenterObj) )
2964 selb->linkTo ((BranchObj*)(dst),-1);
2965 // Get coordinates of mainbranch
2966 x=parser.parDouble(ok,2);
2969 y=parser.parDouble(ok,3);
2979 } else if ( selectionType() == TreeItem::Image)
2981 if (parser.checkParCount(1))
2983 // 0 selectstring of parent
2984 s=parser.parString(ok,0);
2985 LinkableMapObj *dst=findObjBySelect (s);
2988 if (typeid(*dst) == typeid(BranchObj) ||
2989 typeid(*dst) == typeid(MapCenterObj))
2990 linkFloatImageTo (getSelectString(dst));
2992 parser.setError (Aborted,"Destination is not a branch");
2995 parser.setError (Aborted,"Type of selection is not a floatimage or branch");
2996 /////////////////////////////////////////////////////////////////////
2997 } else if (com=="loadImage")
2999 if (selection.isEmpty())
3001 parser.setError (Aborted,"Nothing selected");
3004 parser.setError (Aborted,"Type of selection is not a branch");
3005 } else if (parser.checkParCount(1))
3007 s=parser.parString(ok,0);
3008 if (ok) loadFloatImageInt (s);
3010 /////////////////////////////////////////////////////////////////////
3011 } else if (com=="moveBranchUp")
3013 if (selection.isEmpty() )
3015 parser.setError (Aborted,"Nothing selected");
3018 parser.setError (Aborted,"Type of selection is not a branch");
3019 } else if (parser.checkParCount(0))
3023 /////////////////////////////////////////////////////////////////////
3024 } else if (com=="moveBranchDown")
3026 if (selection.isEmpty() )
3028 parser.setError (Aborted,"Nothing selected");
3031 parser.setError (Aborted,"Type of selection is not a branch");
3032 } else if (parser.checkParCount(0))
3036 /////////////////////////////////////////////////////////////////////
3037 } else if (com=="move")
3039 if (selection.isEmpty() )
3041 parser.setError (Aborted,"Nothing selected");
3042 } else if ( selectionType()!=TreeItem::Branch &&
3043 selectionType()!=TreeItem::MapCenter &&
3044 selectionType()!=TreeItem::Image )
3046 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3047 } else if (parser.checkParCount(2))
3049 x=parser.parDouble (ok,0);
3052 y=parser.parDouble (ok,1);
3056 /////////////////////////////////////////////////////////////////////
3057 } else if (com=="moveRel")
3059 if (selection.isEmpty() )
3061 parser.setError (Aborted,"Nothing selected");
3062 } else if ( selectionType()!=TreeItem::Branch &&
3063 selectionType()!=TreeItem::MapCenter &&
3064 selectionType()!=TreeItem::Image )
3066 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3067 } else if (parser.checkParCount(2))
3069 x=parser.parDouble (ok,0);
3072 y=parser.parDouble (ok,1);
3073 if (ok) moveRel (x,y);
3076 /////////////////////////////////////////////////////////////////////
3077 } else if (com=="nop")
3079 /////////////////////////////////////////////////////////////////////
3080 } else if (com=="paste")
3082 if (selection.isEmpty() )
3084 parser.setError (Aborted,"Nothing selected");
3087 parser.setError (Aborted,"Type of selection is not a branch");
3088 } else if (parser.checkParCount(1))
3090 n=parser.parInt (ok,0);
3091 if (ok) pasteNoSave(n);
3093 /////////////////////////////////////////////////////////////////////
3094 } else if (com=="qa")
3096 if (selection.isEmpty() )
3098 parser.setError (Aborted,"Nothing selected");
3101 parser.setError (Aborted,"Type of selection is not a branch");
3102 } else if (parser.checkParCount(4))
3105 c=parser.parString (ok,0);
3108 parser.setError (Aborted,"No comment given");
3111 s=parser.parString (ok,1);
3114 parser.setError (Aborted,"First parameter is not a string");
3117 t=parser.parString (ok,2);
3120 parser.setError (Aborted,"Condition is not a string");
3123 u=parser.parString (ok,3);
3126 parser.setError (Aborted,"Third parameter is not a string");
3131 parser.setError (Aborted,"Unknown type: "+s);
3136 parser.setError (Aborted,"Unknown operator: "+t);
3141 parser.setError (Aborted,"Type of selection is not a branch");
3144 if (selb->getHeading() == u)
3146 cout << "PASSED: " << qPrintable (c) << endl;
3149 cout << "FAILED: " << qPrintable (c) << endl;
3159 /////////////////////////////////////////////////////////////////////
3160 } else if (com=="saveImage")
3162 FloatImageObj *fio=selection.getFloatImage();
3165 parser.setError (Aborted,"Type of selection is not an image");
3166 } else if (parser.checkParCount(2))
3168 s=parser.parString(ok,0);
3171 t=parser.parString(ok,1);
3172 if (ok) saveFloatImageInt (fio,t,s);
3175 /////////////////////////////////////////////////////////////////////
3176 } else if (com=="scroll")
3178 if (selection.isEmpty() )
3180 parser.setError (Aborted,"Nothing selected");
3183 parser.setError (Aborted,"Type of selection is not a branch");
3184 } else if (parser.checkParCount(0))
3186 if (!scrollBranch (selb))
3187 parser.setError (Aborted,"Could not scroll branch");
3189 /////////////////////////////////////////////////////////////////////
3190 } else if (com=="select")
3192 if (parser.checkParCount(1))
3194 s=parser.parString(ok,0);
3197 /////////////////////////////////////////////////////////////////////
3198 } else if (com=="selectLastBranch")
3200 if (selection.isEmpty() )
3202 parser.setError (Aborted,"Nothing selected");
3205 parser.setError (Aborted,"Type of selection is not a branch");
3206 } else if (parser.checkParCount(0))
3208 BranchObj *bo=selb->getLastBranch();
3210 parser.setError (Aborted,"Could not select last branch");
3214 /////////////////////////////////////////////////////////////////////
3215 } else if (com=="selectLastImage")
3217 if (selection.isEmpty() )
3219 parser.setError (Aborted,"Nothing selected");
3222 parser.setError (Aborted,"Type of selection is not a branch");
3223 } else if (parser.checkParCount(0))
3225 FloatImageObj *fio=selb->getLastFloatImage();
3227 parser.setError (Aborted,"Could not select last image");
3231 /////////////////////////////////////////////////////////////////////
3232 } else if (com=="selectLatestAdded")
3234 if (latestSelectionString.isEmpty() )
3236 parser.setError (Aborted,"No latest added object");
3239 if (!select (latestSelectionString))
3240 parser.setError (Aborted,"Could not select latest added object "+latestSelectionString);
3242 /////////////////////////////////////////////////////////////////////
3243 } else if (com=="setFrameType")
3245 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3247 parser.setError (Aborted,"Type of selection does not allow setting frame type");
3249 else if (parser.checkParCount(1))
3251 s=parser.parString(ok,0);
3252 if (ok) setFrameType (s);
3254 /////////////////////////////////////////////////////////////////////
3255 } else if (com=="setFramePenColor")
3257 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3259 parser.setError (Aborted,"Type of selection does not allow setting of pen color");
3261 else if (parser.checkParCount(1))
3263 QColor c=parser.parColor(ok,0);
3264 if (ok) setFramePenColor (c);
3266 /////////////////////////////////////////////////////////////////////
3267 } else if (com=="setFrameBrushColor")
3269 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3271 parser.setError (Aborted,"Type of selection does not allow setting brush color");
3273 else if (parser.checkParCount(1))
3275 QColor c=parser.parColor(ok,0);
3276 if (ok) setFrameBrushColor (c);
3278 /////////////////////////////////////////////////////////////////////
3279 } else if (com=="setFramePadding")
3281 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3283 parser.setError (Aborted,"Type of selection does not allow setting frame padding");
3285 else if (parser.checkParCount(1))
3287 n=parser.parInt(ok,0);
3288 if (ok) setFramePadding(n);
3290 /////////////////////////////////////////////////////////////////////
3291 } else if (com=="setFrameBorderWidth")
3293 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3295 parser.setError (Aborted,"Type of selection does not allow setting frame border width");
3297 else if (parser.checkParCount(1))
3299 n=parser.parInt(ok,0);
3300 if (ok) setFrameBorderWidth (n);
3302 /////////////////////////////////////////////////////////////////////
3303 } else if (com=="setMapAuthor")
3305 if (parser.checkParCount(1))
3307 s=parser.parString(ok,0);
3308 if (ok) setAuthor (s);
3310 /////////////////////////////////////////////////////////////////////
3311 } else if (com=="setMapComment")
3313 if (parser.checkParCount(1))
3315 s=parser.parString(ok,0);
3316 if (ok) setComment(s);
3318 /////////////////////////////////////////////////////////////////////
3319 } else if (com=="setMapBackgroundColor")
3321 if (selection.isEmpty() )
3323 parser.setError (Aborted,"Nothing selected");
3324 } else if (! getSelectedBranch() )
3326 parser.setError (Aborted,"Type of selection is not a branch");
3327 } else if (parser.checkParCount(1))
3329 QColor c=parser.parColor (ok,0);
3330 if (ok) setMapBackgroundColor (c);
3332 /////////////////////////////////////////////////////////////////////
3333 } else if (com=="setMapDefLinkColor")
3335 if (selection.isEmpty() )
3337 parser.setError (Aborted,"Nothing selected");
3340 parser.setError (Aborted,"Type of selection is not a branch");
3341 } else if (parser.checkParCount(1))
3343 QColor c=parser.parColor (ok,0);
3344 if (ok) setMapDefLinkColor (c);
3346 /////////////////////////////////////////////////////////////////////
3347 } else if (com=="setMapLinkStyle")
3349 if (parser.checkParCount(1))
3351 s=parser.parString (ok,0);
3352 if (ok) setMapLinkStyle(s);
3354 /////////////////////////////////////////////////////////////////////
3355 } else if (com=="setHeading")
3357 if (selection.isEmpty() )
3359 parser.setError (Aborted,"Nothing selected");
3362 parser.setError (Aborted,"Type of selection is not a branch");
3363 } else if (parser.checkParCount(1))
3365 s=parser.parString (ok,0);
3369 /////////////////////////////////////////////////////////////////////
3370 } else if (com=="setHideExport")
3372 if (selection.isEmpty() )
3374 parser.setError (Aborted,"Nothing selected");
3375 } else if (selectionType()!=TreeItem::Branch && selectionType() != TreeItem::MapCenter &&selectionType()!=TreeItem::Image)
3377 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3378 } else if (parser.checkParCount(1))
3380 b=parser.parBool(ok,0);
3381 if (ok) setHideExport (b);
3383 /////////////////////////////////////////////////////////////////////
3384 } else if (com=="setIncludeImagesHorizontally")
3386 if (selection.isEmpty() )
3388 parser.setError (Aborted,"Nothing selected");
3391 parser.setError (Aborted,"Type of selection is not a branch");
3392 } else if (parser.checkParCount(1))
3394 b=parser.parBool(ok,0);
3395 if (ok) setIncludeImagesHor(b);
3397 /////////////////////////////////////////////////////////////////////
3398 } else if (com=="setIncludeImagesVertically")
3400 if (selection.isEmpty() )
3402 parser.setError (Aborted,"Nothing selected");
3405 parser.setError (Aborted,"Type of selection is not a branch");
3406 } else if (parser.checkParCount(1))
3408 b=parser.parBool(ok,0);
3409 if (ok) setIncludeImagesVer(b);
3411 /////////////////////////////////////////////////////////////////////
3412 } else if (com=="setHideLinkUnselected")
3414 if (selection.isEmpty() )
3416 parser.setError (Aborted,"Nothing selected");
3417 } else if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3419 parser.setError (Aborted,"Type of selection does not allow hiding the link");
3420 } else if (parser.checkParCount(1))
3422 b=parser.parBool(ok,0);
3423 if (ok) setHideLinkUnselected(b);
3425 /////////////////////////////////////////////////////////////////////
3426 } else if (com=="setSelectionColor")
3428 if (parser.checkParCount(1))
3430 QColor c=parser.parColor (ok,0);
3431 if (ok) setSelectionColorInt (c);
3433 /////////////////////////////////////////////////////////////////////
3434 } else if (com=="setURL")
3436 if (selection.isEmpty() )
3438 parser.setError (Aborted,"Nothing selected");
3441 parser.setError (Aborted,"Type of selection is not a branch");
3442 } else if (parser.checkParCount(1))
3444 s=parser.parString (ok,0);
3447 /////////////////////////////////////////////////////////////////////
3448 } else if (com=="setVymLink")
3450 if (selection.isEmpty() )
3452 parser.setError (Aborted,"Nothing selected");
3455 parser.setError (Aborted,"Type of selection is not a branch");
3456 } else if (parser.checkParCount(1))
3458 s=parser.parString (ok,0);
3459 if (ok) setVymLink(s);
3462 /////////////////////////////////////////////////////////////////////
3463 else if (com=="setFlag")
3465 if (selection.isEmpty() )
3467 parser.setError (Aborted,"Nothing selected");
3470 parser.setError (Aborted,"Type of selection is not a branch");
3471 } else if (parser.checkParCount(1))
3473 s=parser.parString(ok,0);
3476 selb->activateStandardFlag(s);
3477 selb->updateFlagsToolbar();
3480 /////////////////////////////////////////////////////////////////////
3481 } else if (com=="setFrameType")
3483 if (selection.isEmpty() )
3485 parser.setError (Aborted,"Nothing selected");
3488 parser.setError (Aborted,"Type of selection is not a branch");
3489 } else if (parser.checkParCount(1))
3491 s=parser.parString(ok,0);
3495 /////////////////////////////////////////////////////////////////////
3496 } else if (com=="sortChildren")
3498 if (selection.isEmpty() )
3500 parser.setError (Aborted,"Nothing selected");
3503 parser.setError (Aborted,"Type of selection is not a branch");
3504 } else if (parser.checkParCount(0))
3508 /////////////////////////////////////////////////////////////////////
3509 } else if (com=="toggleFlag")
3511 if (selection.isEmpty() )
3513 parser.setError (Aborted,"Nothing selected");
3516 parser.setError (Aborted,"Type of selection is not a branch");
3517 } else if (parser.checkParCount(1))
3519 s=parser.parString(ok,0);
3522 selb->toggleStandardFlag(s);
3523 selb->updateFlagsToolbar();
3526 /////////////////////////////////////////////////////////////////////
3527 } else if (com=="unscroll")
3529 if (selection.isEmpty() )
3531 parser.setError (Aborted,"Nothing selected");
3534 parser.setError (Aborted,"Type of selection is not a branch");
3535 } else if (parser.checkParCount(0))
3537 if (!unscrollBranch (selb))
3538 parser.setError (Aborted,"Could not unscroll branch");
3540 /////////////////////////////////////////////////////////////////////
3541 } else if (com=="unscrollChildren")
3543 if (selection.isEmpty() )
3545 parser.setError (Aborted,"Nothing selected");
3548 parser.setError (Aborted,"Type of selection is not a branch");
3549 } else if (parser.checkParCount(0))
3551 unscrollChildren ();
3553 /////////////////////////////////////////////////////////////////////
3554 } else if (com=="unsetFlag")
3556 if (selection.isEmpty() )
3558 parser.setError (Aborted,"Nothing selected");
3561 parser.setError (Aborted,"Type of selection is not a branch");
3562 } else if (parser.checkParCount(1))
3564 s=parser.parString(ok,0);
3567 selb->deactivateStandardFlag(s);
3568 selb->updateFlagsToolbar();
3572 parser.setError (Aborted,"Unknown command");
3575 if (parser.errorLevel()==NoError)
3577 // setChanged(); FIXME should not be called e.g. for export?!
3582 // TODO Error handling
3583 qWarning("VymModel::parseAtom: Error!");
3584 qWarning(parser.errorMessage());
3588 void VymModel::runScript (QString script)
3590 parser.setScript (script);
3592 while (parser.next() )
3593 parseAtom(parser.getAtom());
3596 void VymModel::setExportMode (bool b)
3598 // should be called before and after exports
3599 // depending on the settings
3600 if (b && settings.value("/export/useHideExport","true")=="true")
3601 setHideTmpMode (HideExport);
3603 setHideTmpMode (HideNone);
3606 void VymModel::exportImage(QString fname, bool askName, QString format)
3610 fname=getMapName()+".png";
3617 QFileDialog *fd=new QFileDialog (NULL);
3618 fd->setCaption (tr("Export map as image"));
3619 fd->setDirectory (lastImageDir);
3620 fd->setFileMode(QFileDialog::AnyFile);
3621 fd->setFilters (imageIO.getFilters() );
3624 fl=fd->selectedFiles();
3626 format=imageIO.getType(fd->selectedFilter());
3630 setExportMode (true);
3631 QPixmap pix (getPixmap());
3632 pix.save(fname, format);
3633 setExportMode (false);
3637 void VymModel::exportXML(QString dir, bool askForName)
3641 dir=browseDirectory(NULL,tr("Export XML to directory"));
3642 if (dir =="" && !reallyWriteDirectory(dir) )
3646 // Hide stuff during export, if settings want this
3647 setExportMode (true);
3649 // Create subdirectories
3652 // write to directory
3653 QString saveFile=saveToDir (dir,mapName+"-",true,getTotalBBox().topLeft() ,NULL);
3656 file.setName ( dir + "/"+mapName+".xml");
3657 if ( !file.open( QIODevice::WriteOnly ) )
3659 // This should neverever happen
3660 QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
3664 // Write it finally, and write in UTF8, no matter what
3665 QTextStream ts( &file );
3666 ts.setEncoding (QTextStream::UnicodeUTF8);
3670 // Now write image, too
3671 exportImage (dir+"/images/"+mapName+".png",false,"PNG");
3673 setExportMode (false);
3676 void VymModel::exportASCII(QString fname,bool askName)
3681 ex.setFile (mapName+".txt");
3687 //ex.addFilter ("TXT (*.txt)");
3688 ex.setDir(lastImageDir);
3689 //ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
3694 setExportMode(true);
3696 setExportMode(false);
3700 void VymModel::exportXHTML (const QString &dir, bool askForName)
3702 ExportXHTMLDialog dia(NULL);
3703 dia.setFilePath (filePath );
3704 dia.setMapName (mapName );
3706 if (dir!="") dia.setDir (dir);
3712 if (dia.exec()!=QDialog::Accepted)
3716 QDir d (dia.getDir());
3717 // Check, if warnings should be used before overwriting
3718 // the output directory
3719 if (d.exists() && d.count()>0)
3722 warn.showCancelButton (true);
3723 warn.setText(QString(
3724 "The directory %1 is not empty.\n"
3725 "Do you risk to overwrite some of its contents?").arg(d.path() ));
3726 warn.setCaption("Warning: Directory not empty");
3727 warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
3729 if (warn.exec()!=QDialog::Accepted) ok=false;
3736 exportXML (dia.getDir(),false );
3737 dia.doExport(mapName );
3738 //if (dia.hasChanged()) setChanged();
3742 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
3747 if (ex.setConfigFile(cf))
3749 setExportMode (true);
3750 ex.exportPresentation();
3751 setExportMode (false);
3758 //////////////////////////////////////////////
3760 //////////////////////////////////////////////
3762 void VymModel::registerEditor(QWidget *me)
3764 mapEditor=(MapEditor*)me;
3765 for (int i=0; i<mapCenters.count(); i++)
3766 mapCenters.at(i)->setMapEditor(mapEditor);
3769 void VymModel::unregisterEditor(QWidget *)
3774 void VymModel::setContextPos(QPointF p)
3779 void VymModel::unsetContextPos()
3781 contextPos=QPointF();
3784 void VymModel::updateNoteFlag()
3787 BranchObj *bo=getSelectedBranch();
3790 bo->updateNoteFlag();
3791 mainWindow->updateActions();
3795 void VymModel::updateRelPositions()
3797 for (int i=0; i<mapCenters.count(); i++)
3798 mapCenters.at(i)->updateRelPositions();
3801 void VymModel::reposition()
3803 for (int i=0;i<mapCenters.count(); i++)
3804 mapCenters.at(i)->reposition(); // for positioning heading
3807 QPolygonF VymModel::shape(BranchObj *bo)
3809 // Creating (arbitrary) shapes
3812 QRectF rb=bo->getBBox();
3813 if (bo->getDepth()==0)
3815 // Just take BBox of this mapCenter
3816 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
3820 // Take union of BBox and TotalBBox
3822 QRectF ra=bo->getTotalBBox();
3823 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
3826 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
3829 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
3831 p <<ra.bottomRight()
3833 <<QPointF (rb.topRight().x(), ra.topRight().y() )
3836 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
3841 void VymModel::moveAway(LinkableMapObj *lmo)
3845 // Move all branches and MapCenters away from lmo
3846 // to avoid collisions
3851 BranchObj *boA=(BranchObj*)lmo;
3853 for (int i=0; i<mapCenters.count(); i++)
3855 boB=mapCenters.at(i);
3858 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
3861 <<" ("<<qPrintable(boA->getHeading() )<<")"
3862 <<" with ("<< qPrintable (boB->getHeading() )
3865 <<" minT="<<r.minTranslation<<endl<<endl;
3869 QPixmap VymModel::getPixmap()
3871 QRectF mapRect=getTotalBBox();
3872 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
3875 pp.setRenderHints(mapEditor->renderHints());
3877 // Don't print the visualisation of selection
3878 selection.unselect();
3880 mapScene->render ( &pp,
3881 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
3882 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
3884 // Restore selection
3885 selection.reselect();
3891 void VymModel::setMapLinkStyle (const QString & s)
3896 case LinkableMapObj::Line :
3899 case LinkableMapObj::Parabel:
3900 snow="StyleParabel";
3902 case LinkableMapObj::PolyLine:
3903 snow="StylePolyLine";
3905 case LinkableMapObj::PolyParabel:
3906 snow="StylePolyParabel";
3909 snow="UndefinedStyle";
3914 QString("setMapLinkStyle (\"%1\")").arg(s),
3915 QString("setMapLinkStyle (\"%1\")").arg(snow),
3916 QString("Set map link style (\"%1\")").arg(s)
3920 linkstyle=LinkableMapObj::Line;
3921 else if (s=="StyleParabel")
3922 linkstyle=LinkableMapObj::Parabel;
3923 else if (s=="StylePolyLine")
3924 linkstyle=LinkableMapObj::PolyLine;
3925 else if (s=="StylePolyParabel")
3926 linkstyle=LinkableMapObj::PolyParabel;
3928 linkstyle=LinkableMapObj::UndefinedStyle;
3931 TreeItem *prev=NULL;
3937 bo=(BranchObj*)(cur->getLMO() );
3938 bo->setLinkStyle(bo->getDefLinkStyle());
3939 cur=next(cur,prev,d);
3944 LinkableMapObj::Style VymModel::getMapLinkStyle ()
3949 void VymModel::setMapDefLinkColor(QColor col)
3951 if ( !col.isValid() ) return;
3953 QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
3954 QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
3955 QString("Set map link color to %1").arg(col.name())
3960 TreeItem *prev=NULL;
3963 cur=next(cur,prev,d);
3966 bo=(BranchObj*)(cur->getLMO() );
3973 void VymModel::setMapLinkColorHintInt()
3975 // called from setMapLinkColorHint(lch) or at end of parse
3977 TreeItem *prev=NULL;
3980 cur=next(cur,prev,d);
3983 bo=(BranchObj*)(cur->getLMO() );
3985 cur=next(cur,prev,d);
3989 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
3992 setMapLinkColorHintInt();
3995 void VymModel::toggleMapLinkColorHint()
3997 if (linkcolorhint==LinkableMapObj::HeadingColor)
3998 linkcolorhint=LinkableMapObj::DefaultColor;
4000 linkcolorhint=LinkableMapObj::HeadingColor;
4002 TreeItem *prev=NULL;
4005 cur=next(cur,prev,d);
4008 bo=(BranchObj*)(cur->getLMO() );
4014 void VymModel::selectMapBackgroundImage () // FIXME move to ME
4016 Q3FileDialog *fd=new Q3FileDialog( NULL);
4017 fd->setMode (Q3FileDialog::ExistingFile);
4018 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
4019 ImagePreview *p =new ImagePreview (fd);
4020 fd->setContentsPreviewEnabled( TRUE );
4021 fd->setContentsPreview( p, p );
4022 fd->setPreviewMode( Q3FileDialog::Contents );
4023 fd->setCaption(vymName+" - " +tr("Load background image"));
4024 fd->setDir (lastImageDir);
4027 if ( fd->exec() == QDialog::Accepted )
4029 // TODO selectMapBackgroundImg in QT4 use: lastImageDir=fd->directory();
4030 lastImageDir=QDir (fd->dirPath());
4031 setMapBackgroundImage (fd->selectedFile());
4035 void VymModel::setMapBackgroundImage (const QString &fn) //FIXME missing savestate
4037 QColor oldcol=mapScene->backgroundBrush().color();
4041 QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
4043 QString ("setMapBackgroundImage (%1)").arg(col.name()),
4044 QString("Set background color of map to %1").arg(col.name()));
4047 brush.setTextureImage (QPixmap (fn));
4048 mapScene->setBackgroundBrush(brush);
4051 void VymModel::selectMapBackgroundColor() // FIXME move to ME
4053 QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
4054 if ( !col.isValid() ) return;
4055 setMapBackgroundColor( col );
4059 void VymModel::setMapBackgroundColor(QColor col) // FIXME move to ME
4061 QColor oldcol=mapScene->backgroundBrush().color();
4063 QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
4064 QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
4065 QString("Set background color of map to %1").arg(col.name()));
4066 mapScene->setBackgroundBrush(col);
4069 QColor VymModel::getMapBackgroundColor() // FIXME move to ME
4071 return mapScene->backgroundBrush().color();
4075 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint() // FIXME move to ME
4077 return linkcolorhint;
4080 QColor VymModel::getMapDefLinkColor() // FIXME move to ME
4082 return defLinkColor;
4085 void VymModel::setMapDefXLinkColor(QColor col) // FIXME move to ME
4090 QColor VymModel::getMapDefXLinkColor() // FIXME move to ME
4092 return defXLinkColor;
4095 void VymModel::setMapDefXLinkWidth (int w) // FIXME move to ME
4100 int VymModel::getMapDefXLinkWidth() // FIXME move to ME
4102 return defXLinkWidth;
4105 void VymModel::move(const double &x, const double &y)
4107 BranchObj *bo = getSelectedBranch();
4109 (selectionType()==TreeItem::Branch ||
4110 selectionType()==TreeItem::MapCenter ||
4111 selectionType()==TreeItem::Image
4114 QPointF ap(bo->getAbsPos());
4118 QString ps=qpointfToString(ap);
4119 QString s=getSelectString();
4122 s, "move "+qpointfToString(to),
4123 QString("Move %1 to %2").arg(getObjectName(bo)).arg(ps));
4131 void VymModel::moveRel (const double &x, const double &y)
4133 BranchObj *bo = getSelectedBranch();
4135 (selectionType()==TreeItem::Branch ||
4136 selectionType()==TreeItem::MapCenter ||
4137 selectionType()==TreeItem::Image
4141 QPointF rp(bo->getRelPos());
4145 QString ps=qpointfToString (bo->getRelPos());
4146 QString s=getSelectString(bo);
4149 s, "moveRel "+qpointfToString(to),
4150 QString("Move %1 to relative position %2").arg(getObjectName(bo)).arg(ps));
4151 ((OrnamentedObj*)bo)->move2RelPos (x,y);
4160 void VymModel::animate()
4162 animationTimer->stop();
4165 while (i<animObjList.size() )
4167 bo=(BranchObj*)animObjList.at(i);
4172 animObjList.removeAt(i);
4179 QItemSelection sel=selModel->selection();
4180 emit (selectionChanged(sel,sel));
4183 if (!animObjList.isEmpty()) animationTimer->start();
4187 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
4189 if (bo && bo->getDepth()>0)
4192 ap.setStart (start);
4194 ap.setTicks (animationTicks);
4195 ap.setAnimated (true);
4196 bo->setAnimation (ap);
4197 animObjList.append( bo );
4198 animationTimer->setSingleShot (true);
4199 animationTimer->start(animationInterval);
4203 void VymModel::stopAnimation (MapObj *mo)
4205 int i=animObjList.indexOf(mo);
4207 animObjList.removeAt (i);
4210 void VymModel::sendSelection()
4212 if (netstate!=Server) return;
4213 sendData (QString("select (\"%1\")").arg(selection.getSelectString()) );
4216 void VymModel::newServer()
4220 tcpServer = new QTcpServer(this);
4221 if (!tcpServer->listen(QHostAddress::Any,port)) {
4222 QMessageBox::critical(NULL, "vym server",
4223 QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
4224 //FIXME needed? we are no widget any longer... close();
4227 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
4229 cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
4232 void VymModel::connectToServer()
4235 server="salam.suse.de";
4237 clientSocket = new QTcpSocket (this);
4238 clientSocket->abort();
4239 clientSocket->connectToHost(server ,port);
4240 connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
4241 connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
4242 this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
4244 cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
4249 void VymModel::newClient()
4251 QTcpSocket *newClient = tcpServer->nextPendingConnection();
4252 connect(newClient, SIGNAL(disconnected()),
4253 newClient, SLOT(deleteLater()));
4255 cout <<"ME::newClient at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
4257 clientList.append (newClient);
4261 void VymModel::sendData(const QString &s)
4263 if (clientList.size()==0) return;
4265 // Create bytearray to send
4267 QDataStream out(&block, QIODevice::WriteOnly);
4268 out.setVersion(QDataStream::Qt_4_0);
4270 // Reserve some space for blocksize
4273 // Write sendCounter
4274 out << sendCounter++;
4279 // Go back and write blocksize so far
4280 out.device()->seek(0);
4281 quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
4285 cout << "ME::sendData bs="<<bs<<" counter="<<sendCounter<<" s="<<qPrintable(s)<<endl;
4287 for (int i=0; i<clientList.size(); ++i)
4289 //cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
4290 clientList.at(i)->write (block);
4294 void VymModel::readData ()
4296 while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
4299 cout <<"readData bytesAvail="<<clientSocket->bytesAvailable();
4303 QDataStream in(clientSocket);
4304 in.setVersion(QDataStream::Qt_4_0);
4312 cout << " t="<<qPrintable (t)<<endl;
4318 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
4320 switch (socketError) {
4321 case QAbstractSocket::RemoteHostClosedError:
4323 case QAbstractSocket::HostNotFoundError:
4324 QMessageBox::information(NULL, vymName +" Network client",
4325 "The host was not found. Please check the "
4326 "host name and port settings.");
4328 case QAbstractSocket::ConnectionRefusedError:
4329 QMessageBox::information(NULL, vymName + " Network client",
4330 "The connection was refused by the peer. "
4331 "Make sure the fortune server is running, "
4332 "and check that the host name and port "
4333 "settings are correct.");
4336 QMessageBox::information(NULL, vymName + " Network client",
4337 QString("The following error occurred: %1.")
4338 .arg(clientSocket->errorString()));
4345 void VymModel::selectMapSelectionColor()
4347 QColor col = QColorDialog::getColor( defLinkColor, NULL);
4348 setSelectionColor (col);
4351 void VymModel::setSelectionColorInt (QColor col)
4353 if ( !col.isValid() ) return;
4355 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4356 QString("setSelectionColor (%1)").arg(col.name()),
4357 QString("Set color of selection box to %1").arg(col.name())
4360 mapEditor->setSelectionColor (col);
4363 void VymModel::updateSelection()
4365 QItemSelection newsel=selModel->selection();
4366 updateSelection (newsel);
4369 void VymModel::updateSelection(const QItemSelection &newsel)
4371 emit (selectionChanged(newsel,newsel)); // needed e.g. to update geometry in editor
4372 ensureSelectionVisible();
4376 void VymModel::setSelectionColor(QColor col)
4378 if ( !col.isValid() ) return;
4380 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4381 QString("setSelectionColor (%1)").arg(col.name()),
4382 QString("Set color of selection box to %1").arg(col.name())
4384 setSelectionColorInt (col);
4387 QColor VymModel::getSelectionColor()
4389 return mapEditor->getSelectionColor();
4392 void VymModel::setHideTmpMode (HideTmpMode mode)
4395 for (int i=0;i<mapCenters.count(); i++)
4396 mapCenters.at(i)->setHideTmp (mode);
4398 // FIXME needed? scene()->update();
4402 QRectF VymModel::getTotalBBox()
4405 for (int i=0;i<mapCenters.count(); i++)
4406 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
4410 //////////////////////////////////////////////
4411 // Selection related
4412 //////////////////////////////////////////////
4414 void VymModel::setSelectionModel (QItemSelectionModel *sm)
4417 cout << "VM::setSelModel selModel="<<selModel<<endl;
4420 QItemSelectionModel* VymModel::getSelectionModel()
4425 void VymModel::setSelectionBlocked (bool b)
4430 selection.unblock();
4433 bool VymModel::isSelectionBlocked()
4435 return selection.isBlocked();
4438 bool VymModel::select ()
4440 QModelIndex index=selModel->selectedIndexes().first(); // TODO no multiselections yet
4442 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
4443 return select (item->getLMO() );
4446 bool VymModel::select (const QString &s)
4448 LinkableMapObj *lmo=findObjBySelect(s);
4450 // Finally select the found object
4460 bool VymModel::select (LinkableMapObj *lmo)
4462 QItemSelection oldsel=selModel->selection();
4465 return select (lmo->getTreeItem() );
4470 bool VymModel::select (TreeItem *ti)
4474 QModelIndex ix=index(ti);
4475 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4476 ti->setLastSelectedBranch();
4477 //updateSelection(oldsel); //FIXME needed?
4483 void VymModel::unselect()
4485 selModel->clearSelection();
4488 void VymModel::reselect()
4490 selection.reselect();
4493 void VymModel::ensureSelectionVisible()
4495 LinkableMapObj *lmo=getSelectedLMO();
4496 if (lmo &&mapEditor) mapEditor->ensureVisible (lmo->getBBox() );
4500 void VymModel::selectInt (LinkableMapObj *lmo)
4502 if (selection.select(lmo))
4504 //selection.update();
4505 sendSelection (); // FIXME VM use signal
4510 void VymModel::selectNextBranchInt()
4512 // Increase number of branch
4513 LinkableMapObj *sel=getSelectedBranch();
4516 QString s=getSelectString();
4522 part=s.section(",",-1);
4524 num=part.right(part.length() - 3);
4526 s=s.left (s.length() -num.length());
4529 num=QString ("%1").arg(num.toUInt()+1);
4533 // Try to select this one
4534 if (select (s)) return;
4536 // We have no direct successor,
4537 // try to increase the parental number in order to
4538 // find a successor with same depth
4540 int d=getSelectedLMO()->getDepth();
4545 while (!found && d>0)
4547 s=s.section (",",0,d-1);
4548 // replace substring of current depth in s with "1"
4549 part=s.section(",",-1);
4551 num=part.right(part.length() - 3);
4555 // increase number of parent
4556 num=QString ("%1").arg(num.toUInt()+1);
4557 s=s.section (",",0,d-2) + ","+ typ+num;
4560 // Special case, look at orientation
4561 if (getSelectedLMO()->getOrientation()==LinkableMapObj::RightOfCenter)
4562 num=QString ("%1").arg(num.toUInt()+1);
4564 num=QString ("%1").arg(num.toUInt()-1);
4569 // pad to oldDepth, select the first branch for each depth
4570 for (i=d;i<oldDepth;i++)
4575 if ( getSelectedBranch()->countBranches()>0)
4583 // try to select the freshly built string
4591 void VymModel::selectPrevBranchInt()
4593 // Decrease number of branch
4594 if (selectionType()==TreeItem::Branch)
4596 QString s=getSelectString();
4602 part=s.section(",",-1);
4604 num=part.right(part.length() - 3);
4606 s=s.left (s.length() -num.length());
4608 int n=num.toInt()-1;
4611 num=QString ("%1").arg(n);
4614 // Try to select this one
4615 if (n>=0 && select (s)) return;
4617 // We have no direct precessor,
4618 // try to decrease the parental number in order to
4619 // find a precessor with same depth
4621 int d=getSelectedLMO()->getDepth();
4626 while (!found && d>0)
4628 s=s.section (",",0,d-1);
4629 // replace substring of current depth in s with "1"
4630 part=s.section(",",-1);
4632 num=part.right(part.length() - 3);
4636 // decrease number of parent
4637 num=QString ("%1").arg(num.toInt()-1);
4638 s=s.section (",",0,d-2) + ","+ typ+num;
4641 // Special case, look at orientation
4642 if (getSelectedLMO()->getOrientation()==LinkableMapObj::RightOfCenter)
4643 num=QString ("%1").arg(num.toInt()-1);
4645 num=QString ("%1").arg(num.toInt()+1);
4650 // pad to oldDepth, select the last branch for each depth
4651 for (i=d;i<oldDepth;i++)
4655 if ( getSelectedBranch()->countBranches()>0)
4656 s+=",bo:"+ QString ("%1").arg( getSelectedBranch()->countBranches()-1 );
4663 // try to select the freshly built string
4671 void VymModel::selectUpperBranch()
4673 if (selection.isBlocked() ) return;
4675 BranchObj *bo=getSelectedBranch();
4676 if (bo && selectionType()==TreeItem::Branch)
4678 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4679 selectPrevBranchInt();
4681 if (bo->getDepth()==1)
4682 selectNextBranchInt();
4684 selectPrevBranchInt();
4688 void VymModel::selectLowerBranch()
4690 if (selection.isBlocked() ) return;
4692 BranchObj *bo=getSelectedBranch();
4693 if (bo && selectionType()==TreeItem::Branch)
4695 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4696 selectNextBranchInt();
4698 if (bo->getDepth()==1)
4699 selectPrevBranchInt();
4701 selectNextBranchInt();
4706 void VymModel::selectLeftBranch()
4708 if (selection.isBlocked() ) return;
4710 QItemSelection oldsel=selModel->selection();
4713 LinkableMapObj *sel=getSelectedBranch();
4716 if (selectionType()== TreeItem::MapCenter)
4718 QModelIndex ix=getSelectedIndex();
4719 selModel->select (index (rowCount(ix)-1,0,ix),QItemSelectionModel::ClearAndSelect );
4722 par=(BranchObj*)(sel->getParObj());
4723 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4726 if (selectionType() == TreeItem::Branch ||
4727 selectionType() == TreeItem::Image)
4729 QModelIndex ix=getSelectedIndex();
4731 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4736 if (selectionType() == TreeItem::Branch )
4738 selectLastSelectedBranch();
4743 updateSelection (oldsel);
4747 void VymModel::selectRightBranch()
4749 if (selection.isBlocked() ) return;
4751 QItemSelection oldsel=selModel->selection();
4754 LinkableMapObj *sel=getSelectedBranch();
4757 if (selectionType()== TreeItem::MapCenter)
4759 QModelIndex ix=getSelectedIndex();
4760 selModel->select (index (0,0,ix),QItemSelectionModel::ClearAndSelect );
4763 par=(BranchObj*)(sel->getParObj());
4764 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4767 if (selectionType() == TreeItem::Branch )
4769 selectLastSelectedBranch();
4775 if (selectionType() == TreeItem::Branch ||
4776 selectionType() == TreeItem::Image)
4778 QModelIndex ix=getSelectedIndex();
4780 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4784 updateSelection (oldsel);
4788 void VymModel::selectFirstBranch()
4790 TreeItem *ti=getSelectedBranchItem();
4793 TreeItem *par=ti->parent();
4795 TreeItem *ti2=par->getFirstBranch();
4799 ensureSelectionVisible();
4805 void VymModel::selectLastBranch()
4807 TreeItem *ti=getSelectedBranchItem();
4810 TreeItem *par=ti->parent();
4812 TreeItem *ti2=par->getLastBranch();
4816 ensureSelectionVisible();
4822 void VymModel::selectLastSelectedBranch()
4824 TreeItem *ti=getSelectedBranchItem();
4827 ti=ti->getLastSelectedBranch();
4828 if (ti) select (ti);
4832 void VymModel::selectParent()
4834 TreeItem *ti=getSelectedItem();
4842 ensureSelectionVisible();
4847 TreeItem::Type VymModel::selectionType()
4849 QModelIndexList list=selModel->selectedIndexes();
4850 if (list.isEmpty()) return TreeItem::Undefined;
4851 TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
4852 return ti->getType();
4856 LinkableMapObj* VymModel::getSelectedLMO()
4858 QModelIndexList list=selModel->selectedIndexes();
4859 if (!list.isEmpty() )
4861 TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
4862 TreeItem::Type type=ti->getType();
4863 if (type ==TreeItem::Branch || type==TreeItem::MapCenter || type==TreeItem::Image)
4865 return ti->getLMO();
4871 BranchObj* VymModel::getSelectedBranch()
4873 TreeItem *ti = getSelectedBranchItem();
4875 return (BranchObj*)ti->getLMO();
4880 TreeItem* VymModel::getSelectedBranchItem()
4882 QModelIndexList list=selModel->selectedIndexes();
4883 if (!list.isEmpty() )
4885 TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
4886 TreeItem::Type type=ti->getType();
4887 if (type ==TreeItem::Branch || type==TreeItem::MapCenter)
4893 TreeItem* VymModel::getSelectedItem()
4895 // FIXME this may not only be branch, but also float etc...
4896 BranchObj* bo=getSelectedBranch();
4897 if (bo) return bo->getTreeItem(); // FIXME VM get directly from treemodl
4901 QModelIndex VymModel::getSelectedIndex()
4903 QModelIndexList list=selModel->selectedIndexes();
4904 if (list.isEmpty() )
4905 return QModelIndex();
4907 return list.first();
4910 FloatImageObj* VymModel::getSelectedFloatImage()
4912 return selection.getFloatImage();
4915 QString VymModel::getSelectString ()
4917 LinkableMapObj *lmo=getSelectedLMO();
4919 return getSelectString(lmo);
4924 QString VymModel::getSelectString (LinkableMapObj *lmo) // FIXME VM needs to use TreeModel
4928 if (typeid(*lmo)==typeid(BranchObj) ||
4929 typeid(*lmo)==typeid(MapCenterObj) )
4931 LinkableMapObj *par=lmo->getParObj();
4934 if (lmo->getDepth() ==1)
4935 // Mainbranch, return
4936 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4938 // Branch, call myself recursively
4939 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4943 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
4944 if (i>=0) s=QString("mc:%1").arg(i);