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"
24 extern Main *mainWindow;
25 extern Settings settings;
26 extern QString tmpVymDir;
28 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()
71 while (!mapCenters.isEmpty())
72 delete mapCenters.takeFirst();
75 void VymModel::init ()
77 // We should have at least one map center to start with
78 // addMapCenter(); FIXME create this in MapEditor as long as model is part of that
90 stepsTotal=settings.readNumEntry("/history/stepsTotal",100);
91 undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
92 mainWindow->updateHistory (undoSet);
100 fileName=tr("unnamed");
102 blockReposition=false;
103 blockSaveState=false;
105 autosaveTimer=new QTimer (this);
106 connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
108 fileChangedTimer=new QTimer (this);
109 fileChangedTimer->start(3000);
110 connect(fileChangedTimer, SIGNAL(timeout()), this, SLOT(fileChanged()));
114 selection.setModel (this);
115 selection.unselect();
122 treeModel=new TreeModel;
125 animationUse=settings.readBoolEntry("/animation/use",false);
126 animationTicks=settings.readNumEntry("/animation/ticks",10);
127 animationInterval=settings.readNumEntry("/animation/interval",50);
129 animationTimer=new QTimer (this);
130 connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
133 defLinkColor=QColor (0,0,255);
134 defXLinkColor=QColor (180,180,180);
135 linkcolorhint=LinkableMapObj::DefaultColor;
136 linkstyle=LinkableMapObj::PolyParabel;
138 defXLinkColor=QColor (230,230,230);
148 // addMapCenter(); FIXME create this in MapEditor until BO and MCO are independent of scene
152 void VymModel::makeTmpDirectories()
154 // Create unique temporary directories
155 tmpMapDir = tmpVymDir+QString("/model-%1").arg(mapNum);
156 histPath = tmpMapDir+"/history";
162 MapEditor* VymModel::getMapEditor() // FIXME better return favourite editor here
167 TreeModel* VymModel::getTreeModel()
172 bool VymModel::isRepositionBlocked()
174 return blockReposition;
177 void VymModel::updateActions() // FIXME maybe don't update if blockReposition is set
179 // Tell mainwindow to update states of actions
180 mainWindow->updateActions();
185 QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
187 // tmpdir temporary directory to which data will be written
188 // prefix mapname, which will be appended to images etc.
189 // writeflags Only write flags for "real" save of map, not undo
190 // offset offset of bbox of whole map in scene.
191 // Needed for XML export
197 case LinkableMapObj::Line:
200 case LinkableMapObj::Parabel:
203 case LinkableMapObj::PolyLine:
207 ls="StylePolyParabel";
211 QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
213 if (linkcolorhint==LinkableMapObj::HeadingColor)
214 colhint=attribut("linkColorHint","HeadingColor");
216 QString mapAttr=attribut("version",vymVersion);
218 mapAttr+= attribut("author",author) +
219 attribut("comment",comment) +
220 attribut("date",getDate()) +
221 attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
222 attribut("selectionColor", selection.getColor().name() ) +
223 attribut("linkStyle", ls ) +
224 attribut("linkColor", defLinkColor.name() ) +
225 attribut("defXLinkColor", defXLinkColor.name() ) +
226 attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
228 s+=beginElement("vymmap",mapAttr);
231 // Find the used flags while traversing the tree
232 standardFlagsDefault->resetUsedCounter();
234 // Reset the counters before saving
235 // TODO constr. of FIO creates lots of objects, better do this in some other way...
236 FloatImageObj (mapScene).resetSaveCounter();
238 // Build xml recursivly
239 if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
240 // Save complete map, if saveSel not set
241 s+=saveToDir(tmpdir,prefix,writeflags,offset);
244 if ( typeid(*saveSel) == typeid(BranchObj) )
246 s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
247 else if ( typeid(*saveSel) == typeid(FloatImageObj) )
249 s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
252 // Save local settings
253 s+=settings.getDataXML (destPath);
256 if (!selection.isEmpty() && !saveSel )
257 s+=valueElement("select",selection.getSelectString());
260 s+=endElement("vymmap");
263 standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
267 void VymModel::setFilePath(QString fpath, QString destname)
269 if (fpath.isEmpty() || fpath=="")
276 filePath=fpath; // becomes absolute path
277 fileName=fpath; // gets stripped of path
278 destPath=destname; // needed for vymlinks and during load to reset fileChangedTime
280 // If fpath is not an absolute path, complete it
281 filePath=QDir(fpath).absPath();
282 fileDir=filePath.left (1+filePath.findRev ("/"));
284 // Set short name, too. Search from behind:
285 int i=fileName.findRev("/");
286 if (i>=0) fileName=fileName.remove (0,i+1);
288 // Forget the .vym (or .xml) for name of map
289 mapName=fileName.left(fileName.findRev(".",-1,true) );
293 void VymModel::setFilePath(QString fpath)
295 setFilePath (fpath,fpath);
298 QString VymModel::getFilePath()
303 QString VymModel::getFileName()
308 QString VymModel::getMapName()
313 QString VymModel::getDestPath()
318 ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
320 ErrorCode err=success;
322 parseBaseHandler *handler;
326 case VymMap: handler=new parseVYMHandler; break;
327 case FreemindMap : handler=new parseFreemindHandler; break;
329 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
330 "Unknown FileType in VymModel::load()");
336 // FIXME not needed??? model->setMapEditor(this);
337 // (map state is set later at end of load...)
340 BranchObj *bo=selection.getBranch();
341 if (!bo) return aborted;
342 if (lmode==ImportAdd)
343 saveStateChangingPart(
346 QString("addMapInsert (%1)").arg(fname),
347 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
349 saveStateChangingPart(
352 QString("addMapReplace(%1)").arg(fname),
353 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
357 // Create temporary directory for packing
359 QString tmpZipDir=makeTmpDir (ok,"vym-pack");
362 QMessageBox::critical( 0, tr( "Critical Load Error" ),
363 tr("Couldn't create temporary directory before load\n"));
368 err=unzipDir (tmpZipDir,fname);
378 // Look for mapname.xml
379 xmlfile= fname.left(fname.findRev(".",-1,true));
380 xmlfile=xmlfile.section( '/', -1 );
381 QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
382 if (!mfile.exists() )
384 // mapname.xml does not exist, well,
385 // maybe someone renamed the mapname.vym file...
386 // Try to find any .xml in the toplevel
387 // directory of the .vym file
388 QStringList flist=QDir (tmpZipDir).entryList("*.xml");
389 if (flist.count()==1)
391 // Only one entry, take this one
392 xmlfile=tmpZipDir + "/"+flist.first();
395 for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it )
396 *it=tmpZipDir + "/" + *it;
397 // TODO Multiple entries, load all (but only the first one into this ME)
398 //mainWindow->fileLoadFromTmp (flist);
399 //returnCode=1; // Silently forget this attempt to load
400 qWarning ("MainWindow::load (fn) multimap found...");
403 if (flist.isEmpty() )
405 QMessageBox::critical( 0, tr( "Critical Load Error" ),
406 tr("Couldn't find a map (*.xml) in .vym archive.\n"));
409 } //file doesn't exist
411 xmlfile=mfile.name();
414 QFile file( xmlfile);
416 // I am paranoid: file should exist anyway
417 // according to check in mainwindow.
420 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
421 tr(QString("Couldn't open map %1").arg(file.name())));
425 bool blockSaveStateOrg=blockSaveState;
426 blockReposition=true;
428 QXmlInputSource source( file);
429 QXmlSimpleReader reader;
430 reader.setContentHandler( handler );
431 reader.setErrorHandler( handler );
432 handler->setModel ( this);
435 // We need to set the tmpDir in order to load files with rel. path
440 tmpdir=fname.left(fname.findRev("/",-1));
441 handler->setTmpDir (tmpdir);
442 handler->setInputFile (file.name());
443 handler->setLoadMode (lmode);
444 bool ok = reader.parse( source );
445 blockReposition=false;
446 blockSaveState=blockSaveStateOrg;
450 reposition(); // FIXME reposition the view instead...
457 autosaveTimer->stop();
460 // Reset timestamp to check for later updates of file
461 fileChangedTime=QFileInfo (destPath).lastModified();
464 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
465 tr( handler->errorProtocol() ) );
467 // Still return "success": the map maybe at least
468 // partially read by the parser
473 removeDir (QDir(tmpZipDir));
480 ErrorCode VymModel::save (const SaveMode &savemode)
484 QString safeFilePath;
486 ErrorCode err=success;
490 mapFileName=mapName+".xml";
492 // use name given by user, even if he chooses .doc
493 mapFileName=fileName;
495 // Look, if we should zip the data:
498 QMessageBox mb( vymName,
499 tr("The map %1\ndid not use the compressed "
500 "vym file format.\nWriting it uncompressed will also write images \n"
501 "and flags and thus may overwrite files in the "
502 "given directory\n\nDo you want to write the map").arg(filePath),
503 QMessageBox::Warning,
504 QMessageBox::Yes | QMessageBox::Default,
506 QMessageBox::Cancel | QMessageBox::Escape);
507 mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
508 mb.setButtonText( QMessageBox::No, tr("uncompressed") );
509 mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
512 case QMessageBox::Yes:
513 // save compressed (default file format)
516 case QMessageBox::No:
520 case QMessageBox::Cancel:
527 // First backup existing file, we
528 // don't want to add to old zip archives
532 if ( settings.value ("/mapeditor/writeBackupFile").toBool())
534 QString backupFileName(destPath + "~");
535 QFile backupFile(backupFileName);
536 if (backupFile.exists() && !backupFile.remove())
538 QMessageBox::warning(0, tr("Save Error"),
539 tr("%1\ncould not be removed before saving").arg(backupFileName));
541 else if (!f.rename(backupFileName))
543 QMessageBox::warning(0, tr("Save Error"),
544 tr("%1\ncould not be renamed before saving").arg(destPath));
551 // Create temporary directory for packing
553 tmpZipDir=makeTmpDir (ok,"vym-zip");
556 QMessageBox::critical( 0, tr( "Critical Load Error" ),
557 tr("Couldn't create temporary directory before save\n"));
561 safeFilePath=filePath;
562 setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
565 // Create mapName and fileDir
566 makeSubDirs (fileDir);
569 if (savemode==CompleteMap || selection.isEmpty())
572 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
575 autosaveTimer->stop();
580 if (selection.type()==Selection::FloatImage)
583 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),selection.getBranch());
584 // TODO take care of multiselections
587 if (!saveStringToDisk(fileDir+mapFileName,saveFile))
590 qWarning ("ME::saveStringToDisk failed!");
596 if (err==success) err=zipDir (tmpZipDir,destPath);
599 removeDir (QDir(tmpZipDir));
601 // Restore original filepath outside of tmp zip dir
602 setFilePath (safeFilePath);
606 fileChangedTime=QFileInfo (destPath).lastModified();
610 void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)
612 QString pathDir=path.left(path.findRev("/"));
618 // We need to parse saved XML data
619 parseVYMHandler handler;
620 QXmlInputSource source( file);
621 QXmlSimpleReader reader;
622 reader.setContentHandler( &handler );
623 reader.setErrorHandler( &handler );
624 handler.setModel ( this);
625 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
626 if (undoSel.isEmpty())
630 handler.setLoadMode (NewMap);
634 handler.setLoadMode (ImportReplace);
636 blockReposition=true;
637 bool ok = reader.parse( source );
638 blockReposition=false;
641 // This should never ever happen
642 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
643 handler.errorProtocol());
646 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
649 void VymModel::addMapInsertInt (const QString &path, int pos)
651 BranchObj *sel=selection.getBranch();
654 QString pathDir=path.left(path.findRev("/"));
660 // We need to parse saved XML data
661 parseVYMHandler handler;
662 QXmlInputSource source( file);
663 QXmlSimpleReader reader;
664 reader.setContentHandler( &handler );
665 reader.setErrorHandler( &handler );
666 handler.setModel (this);
667 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
668 handler.setLoadMode (ImportAdd);
669 blockReposition=true;
670 bool ok = reader.parse( source );
671 blockReposition=false;
674 // This should never ever happen
675 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
676 handler.errorProtocol());
678 if (sel->getDepth()>0)
679 sel->getLastBranch()->linkTo (sel,pos);
681 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
685 FloatImageObj* VymModel::loadFloatImageInt (QString fn)
687 BranchObj *bo=selection.getBranch();
692 fio=bo->getLastFloatImage();
695 // FIXME needed? scene()->update();
701 void VymModel::loadFloatImage ()
703 BranchObj *bo=selection.getBranch();
707 Q3FileDialog *fd=new Q3FileDialog( NULL);
708 fd->setMode (Q3FileDialog::ExistingFiles);
709 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
710 ImagePreview *p =new ImagePreview (fd);
711 fd->setContentsPreviewEnabled( TRUE );
712 fd->setContentsPreview( p, p );
713 fd->setPreviewMode( Q3FileDialog::Contents );
714 fd->setCaption(vymName+" - " +tr("Load image"));
715 fd->setDir (lastImageDir);
718 if ( fd->exec() == QDialog::Accepted )
720 // TODO loadFIO in QT4 use: lastImageDir=fd->directory();
721 lastImageDir=QDir (fd->dirPath());
724 for (int j=0; j<fd->selectedFiles().count(); j++)
726 s=fd->selectedFiles().at(j);
727 fio=loadFloatImageInt (s);
730 (LinkableMapObj*)fio,
733 QString ("loadImage (%1)").arg(s ),
734 QString("Add image %1 to %2").arg(s).arg(getObjectName(bo))
737 // TODO loadFIO error handling
738 qWarning ("Failed to load "+s);
746 void VymModel::saveFloatImageInt (FloatImageObj *fio, const QString &type, const QString &fn)
751 void VymModel::saveFloatImage ()
753 FloatImageObj *fio=selection.getFloatImage();
756 QFileDialog *fd=new QFileDialog( NULL);
757 fd->setFilters (imageIO.getFilters());
758 fd->setCaption(vymName+" - " +tr("Save image"));
759 fd->setFileMode( QFileDialog::AnyFile );
760 fd->setDirectory (lastImageDir);
761 // fd->setSelection (fio->getOriginalFilename());
765 if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
767 fn=fd->selectedFiles().at(0);
768 if (QFile (fn).exists() )
770 QMessageBox mb( vymName,
771 tr("The file %1 exists already.\n"
772 "Do you want to overwrite it?").arg(fn),
773 QMessageBox::Warning,
774 QMessageBox::Yes | QMessageBox::Default,
775 QMessageBox::Cancel | QMessageBox::Escape,
776 QMessageBox::NoButton );
778 mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
779 mb.setButtonText( QMessageBox::No, tr("Cancel"));
782 case QMessageBox::Yes:
785 case QMessageBox::Cancel:
792 saveFloatImageInt (fio,fd->selectedFilter(),fn );
799 void VymModel::importDirInt(BranchObj *dst, QDir d)
801 BranchObj *bo=selection.getBranch();
804 // Traverse directories
805 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
806 QFileInfoList list = d.entryInfoList();
809 for (int i = 0; i < list.size(); ++i)
812 if (fi.fileName() != "." && fi.fileName() != ".." )
815 bo=dst->getLastBranch();
816 bo->setHeading (fi.fileName() );
817 bo->setColor (QColor("blue"));
819 if ( !d.cd(fi.fileName()) )
820 QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
823 // Recursively add subdirs
830 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
831 list = d.entryInfoList();
833 for (int i = 0; i < list.size(); ++i)
837 bo=dst->getLastBranch();
838 bo->setHeading (fi.fileName() );
839 bo->setColor (QColor("black"));
840 if (fi.fileName().right(4) == ".vym" )
841 bo->setVymLink (fi.filePath());
846 void VymModel::importDirInt (const QString &s)
848 BranchObj *bo=selection.getBranch();
851 saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
858 void VymModel::importDir()
860 BranchObj *bo=selection.getBranch();
864 filters <<"VYM map (*.vym)";
865 QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
866 fd->setMode (QFileDialog::DirectoryOnly);
867 fd->setFilters (filters);
868 fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
872 if ( fd->exec() == QDialog::Accepted )
874 importDirInt (fd->selectedFile() );
876 //FIXME needed? scene()->update();
882 void VymModel::autosave()
884 QDateTime now=QDateTime().currentDateTime();
886 // Disable autosave, while we have gone back in history
887 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
888 if (redosAvail>0) return;
890 // Also disable autosave for new map without filename
891 if (filePath.isEmpty()) return;
894 if (mapUnsaved &&mapChanged && settings.value ("/autosave/use",true).toBool() )
896 if (QFileInfo(filePath).lastModified()<=fileChangedTime)
897 mainWindow->fileSave (this);
900 cout <<" ME::autosave rejected, file on disk is newer than last save.\n";
905 void VymModel::fileChanged()
907 // Check if file on disk has changed meanwhile
908 if (!filePath.isEmpty())
910 QDateTime tmod=QFileInfo (filePath).lastModified();
911 if (tmod>fileChangedTime)
913 // FIXME switch to current mapeditor and finish lineedits...
914 QMessageBox mb( vymName,
915 tr("The file of the map on disk has changed:\n\n"
916 " %1\n\nDo you want to reload that map with the new file?").arg(filePath),
917 QMessageBox::Question,
919 QMessageBox::Cancel | QMessageBox::Default,
920 QMessageBox::NoButton );
922 mb.setButtonText( QMessageBox::Yes, tr("Reload"));
923 mb.setButtonText( QMessageBox::No, tr("Ignore"));
926 case QMessageBox::Yes:
928 load (filePath,NewMap,fileType);
929 case QMessageBox::Cancel:
930 fileChangedTime=tmod; // allow autosave to overwrite newer file!
936 bool VymModel::isDefault()
941 void VymModel::makeDefault()
947 bool VymModel::hasChanged()
952 void VymModel::setChanged()
955 autosaveTimer->start(settings.value("/autosave/ms/",300000).toInt());
963 QString VymModel::getObjectName (const LinkableMapObj *lmo)
966 if (!lmo) return QString("Error: NULL has no name!");
968 if ((typeid(*lmo) == typeid(BranchObj) ||
969 typeid(*lmo) == typeid(MapCenterObj)))
972 s=(((BranchObj*)lmo)->getHeading());
973 if (s=="") s="unnamed";
974 return QString("branch (%1)").arg(s);
976 if ((typeid(*lmo) == typeid(FloatImageObj) ))
977 return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
978 return QString("Unknown type has no name!");
981 void VymModel::redo()
983 // Can we undo at all?
984 if (redosAvail<1) return;
986 bool blockSaveStateOrg=blockSaveState;
991 if (undosAvail<stepsTotal) undosAvail++;
993 if (curStep>stepsTotal) curStep=1;
994 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
995 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
996 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
997 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
998 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
999 QString version=undoSet.readEntry ("/history/version");
1001 /* TODO Maybe check for version, if we save the history
1002 if (!checkVersion(version))
1003 QMessageBox::warning(0,tr("Warning"),
1004 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1007 // Find out current undo directory
1008 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1012 cout << "VymModel::redo() begin\n";
1013 cout << " undosAvail="<<undosAvail<<endl;
1014 cout << " redosAvail="<<redosAvail<<endl;
1015 cout << " curStep="<<curStep<<endl;
1016 cout << " ---------------------------"<<endl;
1017 cout << " comment="<<comment.toStdString()<<endl;
1018 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1019 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1020 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1021 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1022 cout << " ---------------------------"<<endl<<endl;
1025 // select object before redo
1026 if (!redoSelection.isEmpty())
1027 select (redoSelection);
1030 parseAtom (redoCommand);
1033 blockSaveState=blockSaveStateOrg;
1035 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1036 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1037 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1038 undoSet.writeSettings(histPath);
1040 mainWindow->updateHistory (undoSet);
1043 /* TODO remove testing
1044 cout << "ME::redo() end\n";
1045 cout << " undosAvail="<<undosAvail<<endl;
1046 cout << " redosAvail="<<redosAvail<<endl;
1047 cout << " curStep="<<curStep<<endl;
1048 cout << " ---------------------------"<<endl<<endl;
1054 bool VymModel::isRedoAvailable()
1056 if (undoSet.readNumEntry("/history/redosAvail",0)>0)
1062 void VymModel::undo()
1064 // Can we undo at all?
1065 if (undosAvail<1) return;
1067 mainWindow->statusMessage (tr("Autosave disabled during undo."));
1069 bool blockSaveStateOrg=blockSaveState;
1070 blockSaveState=true;
1072 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1073 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1074 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1075 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1076 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1077 QString version=undoSet.readEntry ("/history/version");
1079 /* TODO Maybe check for version, if we save the history
1080 if (!checkVersion(version))
1081 QMessageBox::warning(0,tr("Warning"),
1082 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1085 // Find out current undo directory
1086 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1088 // select object before undo
1089 if (!undoSelection.isEmpty())
1090 select (undoSelection);
1094 cout << "VymModel::undo() begin\n";
1095 cout << " undosAvail="<<undosAvail<<endl;
1096 cout << " redosAvail="<<redosAvail<<endl;
1097 cout << " curStep="<<curStep<<endl;
1098 cout << " ---------------------------"<<endl;
1099 cout << " comment="<<comment.toStdString()<<endl;
1100 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1101 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1102 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1103 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1104 cout << " ---------------------------"<<endl<<endl;
1106 parseAtom (undoCommand);
1111 if (curStep<1) curStep=stepsTotal;
1115 blockSaveState=blockSaveStateOrg;
1116 /* TODO remove testing
1117 cout << "VymModel::undo() end\n";
1118 cout << " undosAvail="<<undosAvail<<endl;
1119 cout << " redosAvail="<<redosAvail<<endl;
1120 cout << " curStep="<<curStep<<endl;
1121 cout << " ---------------------------"<<endl<<endl;
1124 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1125 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1126 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1127 undoSet.writeSettings(histPath);
1129 mainWindow->updateHistory (undoSet);
1132 ensureSelectionVisible();
1135 bool VymModel::isUndoAvailable()
1137 if (undoSet.readNumEntry("/history/undosAvail",0)>0)
1143 void VymModel::gotoHistoryStep (int i)
1145 // Restore variables
1146 int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
1147 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
1149 if (i<0) i=undosAvail+redosAvail;
1151 // Clicking above current step makes us undo things
1154 for (int j=0; j<undosAvail-i; j++) undo();
1157 // Clicking below current step makes us redo things
1159 for (int j=undosAvail; j<i; j++)
1161 if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
1165 // And ignore clicking the current row ;-)
1169 QString VymModel::getHistoryPath()
1171 QString histName(QString("history-%1").arg(curStep));
1172 return (tmpMapDir+"/"+histName);
1175 void VymModel::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
1177 sendData(redoCom); //FIXME testing
1182 if (blockSaveState) return;
1184 if (debug) cout << "ME::saveState() for "<<qPrintable (mapName)<<endl;
1186 // Find out current undo directory
1187 if (undosAvail<stepsTotal) undosAvail++;
1189 if (curStep>stepsTotal) curStep=1;
1191 QString backupXML="";
1192 QString histDir=getHistoryPath();
1193 QString bakMapPath=histDir+"/map.xml";
1195 // Create histDir if not available
1198 makeSubDirs (histDir);
1200 // Save depending on how much needs to be saved
1202 backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
1204 QString undoCommand="";
1205 if (savemode==UndoCommand)
1207 undoCommand=undoCom;
1209 else if (savemode==PartOfMap )
1211 undoCommand=undoCom;
1212 undoCommand.replace ("PATH",bakMapPath);
1215 if (!backupXML.isEmpty())
1216 // Write XML Data to disk
1217 saveStringToDisk (bakMapPath,backupXML);
1219 // We would have to save all actions in a tree, to keep track of
1220 // possible redos after a action. Possible, but we are too lazy: forget about redos.
1223 // Write the current state to disk
1224 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1225 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1226 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1227 undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
1228 undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
1229 undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
1230 undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
1231 undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
1232 undoSet.setEntry (QString("/history/version"),vymVersion);
1233 undoSet.writeSettings(histPath);
1237 // TODO remove after testing
1238 //cout << " into="<< histPath.toStdString()<<endl;
1239 cout << " stepsTotal="<<stepsTotal<<
1240 ", undosAvail="<<undosAvail<<
1241 ", redosAvail="<<redosAvail<<
1242 ", curStep="<<curStep<<endl;
1243 cout << " ---------------------------"<<endl;
1244 cout << " comment="<<comment.toStdString()<<endl;
1245 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1246 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1247 cout << " redoCom="<<redoCom.toStdString()<<endl;
1248 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1249 if (saveSel) cout << " saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
1250 cout << " ---------------------------"<<endl;
1253 mainWindow->updateHistory (undoSet);
1259 void VymModel::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
1261 // save the selected part of the map, Undo will replace part of map
1262 QString undoSelection="";
1264 undoSelection=getSelectString(undoSel);
1266 qWarning ("VymModel::saveStateChangingPart no undoSel given!");
1267 QString redoSelection="";
1269 redoSelection=getSelectString(undoSel);
1271 qWarning ("VymModel::saveStateChangingPart no redoSel given!");
1274 saveState (PartOfMap,
1275 undoSelection, "addMapReplace (\"PATH\")",
1281 void VymModel::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
1285 qWarning ("VymModel::saveStateRemovingPart no redoSel given!");
1288 QString undoSelection=getSelectString (redoSel->getParObj());
1289 QString redoSelection=getSelectString(redoSel);
1290 if (typeid(*redoSel) == typeid(BranchObj) )
1292 // save the selected branch of the map, Undo will insert part of map
1293 saveState (PartOfMap,
1294 undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
1295 redoSelection, "delete ()",
1302 void VymModel::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment)
1304 // "Normal" savestate: save commands, selections and comment
1305 // so just save commands for undo and redo
1306 // and use current selection
1308 QString redoSelection="";
1309 if (redoSel) redoSelection=getSelectString(redoSel);
1310 QString undoSelection="";
1311 if (undoSel) undoSelection=getSelectString(undoSel);
1313 saveState (UndoCommand,
1320 void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment)
1322 // "Normal" savestate: save commands, selections and comment
1323 // so just save commands for undo and redo
1324 // and use current selection
1325 saveState (UndoCommand,
1332 void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment)
1334 // "Normal" savestate applied to model (no selection needed):
1335 // save commands and comment
1336 saveState (UndoCommand,
1344 QGraphicsScene* VymModel::getScene ()
1349 BranchObj* VymModel::first()
1351 if (mapCenters.count()>0)
1352 return mapCenters.first();
1357 BranchObj* VymModel::next(BranchObj *bo_start)
1360 BranchObj *bo=bo_start;
1363 // Try to find next branch in current MapCenter
1365 if (rbo) return rbo;
1367 // Try to find MapCenter of bo
1368 while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
1370 // Try to find next MapCenter
1371 int i=mapCenters.indexOf ((MapCenterObj*)bo);
1372 if (i+2 > mapCenters.count() || i<0) return NULL;
1373 if (mapCenters.at(i+1)!=bo_start)
1374 return mapCenters.at(i+1);
1379 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
1381 LinkableMapObj *lmo;
1383 for (int i=0;i<mapCenters.count(); i++)
1385 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
1386 if (lmo) return lmo;
1391 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
1393 LinkableMapObj *lmo;
1399 part=s.section(",",0,0);
1401 num=part.right(part.length() - 3);
1402 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
1403 return mapCenters.at(num.toInt() );
1406 for (int i=0; i<mapCenters.count(); i++)
1408 lmo=mapCenters.at(i)->findObjBySelect(s);
1409 if (lmo) return lmo;
1414 LinkableMapObj* VymModel::findID (const QString &s)
1416 LinkableMapObj *lmo;
1417 for (int i=0; i<mapCenters.count(); i++)
1419 lmo=mapCenters.at(i)->findID (s);
1420 if (lmo) return lmo;
1425 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
1429 for (int i=0; i<mapCenters.count(); i++)
1430 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
1434 //////////////////////////////////////////////
1436 //////////////////////////////////////////////
1437 void VymModel::setVersion (const QString &s)
1442 void VymModel::setAuthor (const QString &s)
1445 QString ("setMapAuthor (\"%1\")").arg(author),
1446 QString ("setMapAuthor (\"%1\")").arg(s),
1447 QString ("Set author of map to \"%1\"").arg(s)
1453 QString VymModel::getAuthor()
1458 void VymModel::setComment (const QString &s)
1461 QString ("setMapComment (\"%1\")").arg(comment),
1462 QString ("setMapComment (\"%1\")").arg(s),
1463 QString ("Set comment of map")
1469 QString VymModel::getComment ()
1474 QString VymModel::getDate ()
1476 return QDate::currentDate().toString ("yyyy-MM-dd");
1479 void VymModel::setHeading(const QString &s)
1481 BranchObj *sel=selection.getBranch();
1486 "setHeading (\""+sel->getHeading()+"\")",
1488 "setHeading (\""+s+"\")",
1489 QString("Set heading of %1 to \"%2\"").arg(getObjectName(sel)).arg(s) );
1490 sel->setHeading(s );
1493 ensureSelectionVisible();
1497 /* FIXME delete this
1498 QString VymModel::getHeading(bool &ok, QPoint &p)
1500 BranchObj *bo=selection.getBranch();
1504 //p=scene->mapFromScene(bo->getAbsPos()); // FIXME this is view-dependant!!!
1505 return bo->getHeading();
1512 void VymModel::setHeadingInt(const QString &s)
1514 BranchObj *bo=selection.getBranch();
1520 ensureSelectionVisible();
1524 BranchObj* VymModel::findText (QString s, bool cs)
1526 QTextDocument::FindFlags flags=0;
1527 if (cs) flags=QTextDocument::FindCaseSensitively;
1530 { // Nothing found or new find process
1532 // nothing found, start again
1536 bool searching=true;
1537 bool foundNote=false;
1538 while (searching && !EOFind)
1542 // Searching in Note
1543 if (itFind->getNote().contains(s,cs))
1545 if (selection.single()!=itFind)
1547 selection.select(itFind);
1548 ensureSelectionVisible();
1550 if (textEditor->findText(s,flags))
1556 // Searching in Heading
1557 if (searching && itFind->getHeading().contains (s,cs) )
1559 selection.select(itFind);
1560 ensureSelectionVisible();
1566 itFind=next(itFind);
1567 if (!itFind) EOFind=true;
1569 //cout <<"still searching... "<<qPrintable( itFind->getHeading())<<endl;
1572 return selection.getBranch();
1577 void VymModel::findReset()
1578 { // Necessary if text to find changes during a find process
1585 void VymModel::setScene (QGraphicsScene *s)
1588 init(); // Here we have a mapScene set,
1589 // which is (still) needed to create MapCenters
1592 void VymModel::setURL(const QString &url)
1594 BranchObj *bo=selection.getBranch();
1597 QString oldurl=bo->getURL();
1601 QString ("setURL (\"%1\")").arg(oldurl),
1603 QString ("setURL (\"%1\")").arg(url),
1604 QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
1609 ensureSelectionVisible();
1613 QString VymModel::getURL()
1615 BranchObj *bo=selection.getBranch();
1617 return bo->getURL();
1622 QStringList VymModel::getURLs()
1625 BranchObj *bo=selection.getBranch();
1631 if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
1638 void VymModel::linkFloatImageTo(const QString &dstString)
1640 FloatImageObj *fio=selection.getFloatImage();
1643 BranchObj *dst=(BranchObj*)findObjBySelect(dstString);
1644 if (dst && (typeid(*dst)==typeid (BranchObj) ||
1645 typeid(*dst)==typeid (MapCenterObj)))
1647 LinkableMapObj *dstPar=dst->getParObj();
1648 QString parString=getSelectString(dstPar);
1649 QString fioPreSelectString=getSelectString(fio);
1650 QString fioPreParentSelectString=getSelectString (fio->getParObj());
1651 ((BranchObj*)dst)->addFloatImage (fio);
1652 selection.unselect();
1653 ((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
1654 fio=((BranchObj*)dst)->getLastFloatImage();
1657 selection.select(fio);
1659 getSelectString(fio),
1660 QString("linkTo (\"%1\")").arg(fioPreParentSelectString),
1662 QString ("linkTo (\"%1\")").arg(dstString),
1663 QString ("Link floatimage to %1").arg(getObjectName(dst)));
1669 void VymModel::setFrameType(const FrameObj::FrameType &t)
1671 BranchObj *bo=selection.getBranch();
1674 QString s=bo->getFrameTypeName();
1675 bo->setFrameType (t);
1676 saveState (bo, QString("setFrameType (\"%1\")").arg(s),
1677 bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
1683 void VymModel::setFrameType(const QString &s)
1685 BranchObj *bo=selection.getBranch();
1688 saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
1689 bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
1690 bo->setFrameType (s);
1696 void VymModel::setFramePenColor(const QColor &c)
1698 BranchObj *bo=selection.getBranch();
1701 saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
1702 bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
1703 bo->setFramePenColor (c);
1707 void VymModel::setFrameBrushColor(const QColor &c)
1709 BranchObj *bo=selection.getBranch();
1712 saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
1713 bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
1714 bo->setFrameBrushColor (c);
1718 void VymModel::setFramePadding (const int &i)
1720 BranchObj *bo=selection.getBranch();
1723 saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
1724 bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
1725 bo->setFramePadding (i);
1731 void VymModel::setFrameBorderWidth(const int &i)
1733 BranchObj *bo=selection.getBranch();
1736 saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
1737 bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
1738 bo->setFrameBorderWidth (i);
1744 void VymModel::setIncludeImagesVer(bool b)
1746 BranchObj *bo=selection.getBranch();
1749 QString u= b ? "false" : "true";
1750 QString r=!b ? "false" : "true";
1754 QString("setIncludeImagesVertically (%1)").arg(u),
1756 QString("setIncludeImagesVertically (%1)").arg(r),
1757 QString("Include images vertically in %1").arg(getObjectName(bo))
1759 bo->setIncludeImagesVer(b);
1764 void VymModel::setIncludeImagesHor(bool b)
1766 BranchObj *bo=selection.getBranch();
1769 QString u= b ? "false" : "true";
1770 QString r=!b ? "false" : "true";
1774 QString("setIncludeImagesHorizontally (%1)").arg(u),
1776 QString("setIncludeImagesHorizontally (%1)").arg(r),
1777 QString("Include images horizontally in %1").arg(getObjectName(bo))
1779 bo->setIncludeImagesHor(b);
1784 void VymModel::setHideLinkUnselected (bool b)
1786 LinkableMapObj *sel=selection.single();
1788 (selection.type() == Selection::Branch ||
1789 selection.type() == Selection::MapCenter ||
1790 selection.type() == Selection::FloatImage ))
1792 QString u= b ? "false" : "true";
1793 QString r=!b ? "false" : "true";
1797 QString("setHideLinkUnselected (%1)").arg(u),
1799 QString("setHideLinkUnselected (%1)").arg(r),
1800 QString("Hide link of %1 if unselected").arg(getObjectName(sel))
1802 sel->setHideLinkUnselected(b);
1806 void VymModel::setHideExport(bool b)
1808 BranchObj *bo=selection.getBranch();
1811 bo->setHideInExport (b);
1812 QString u= b ? "false" : "true";
1813 QString r=!b ? "false" : "true";
1817 QString ("setHideExport (%1)").arg(u),
1819 QString ("setHideExport (%1)").arg(r),
1820 QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bo)).arg (r)
1825 // FIXME needed? scene()->update();
1829 void VymModel::toggleHideExport()
1831 BranchObj *bo=selection.getBranch();
1833 setHideExport ( !bo->hideInExport() );
1837 void VymModel::copy()
1839 LinkableMapObj *sel=selection.single();
1842 if (redosAvail == 0)
1845 QString s=getSelectString(sel);
1846 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel );
1847 curClipboard=curStep;
1850 // Copy also to global clipboard, because we are at last step in history
1851 QString bakMapName(QString("history-%1").arg(curStep));
1852 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1853 copyDir (bakMapDir,clipboardDir );
1855 clipboardEmpty=false;
1861 void VymModel::pasteNoSave(const int &n)
1863 bool old=blockSaveState;
1864 blockSaveState=true;
1865 bool zippedOrg=zipped;
1866 if (redosAvail > 0 || n!=0)
1868 // Use the "historical" buffer
1869 QString bakMapName(QString("history-%1").arg(n));
1870 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1871 load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
1873 // Use the global buffer
1874 load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
1879 void VymModel::paste()
1881 BranchObj *sel=selection.getBranch();
1884 saveStateChangingPart(
1887 QString ("paste (%1)").arg(curClipboard),
1888 QString("Paste to %1").arg( getObjectName(sel))
1895 void VymModel::cut()
1897 LinkableMapObj *sel=selection.single();
1898 if ( sel && (selection.type() == Selection::Branch ||
1899 selection.type()==Selection::MapCenter ||
1900 selection.type()==Selection::FloatImage))
1902 /* No savestate! savestate is called in cutNoSave
1903 saveStateChangingPart(
1907 QString("Cut %1").arg(getObjectName(sel ))
1916 void VymModel::moveBranchUp()
1918 BranchObj* bo=selection.getBranch();
1922 if (!bo->canMoveBranchUp()) return;
1923 par=(BranchObj*)(bo->getParObj());
1924 BranchObj *obo=par->moveBranchUp (bo); // bo will be the one below selection
1925 saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
1927 //FIXME needed? scene()->update();
1929 ensureSelectionVisible();
1933 void VymModel::moveBranchDown()
1935 BranchObj* bo=selection.getBranch();
1939 if (!bo->canMoveBranchDown()) return;
1940 par=(BranchObj*)(bo->getParObj());
1941 BranchObj *obo=par->moveBranchDown(bo); // bo will be the one above selection
1942 saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
1944 //FIXME needed? scene()->update();
1946 ensureSelectionVisible();
1950 void VymModel::sortChildren()
1952 BranchObj* bo=selection.getBranch();
1955 if(bo->countBranches()>1)
1957 saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
1960 ensureSelectionVisible();
1965 MapCenterObj* VymModel::addMapCenter ()
1967 MapCenterObj *mco=addMapCenter (contextPos);
1968 selection.select (mco);
1970 ensureSelectionVisible();
1975 QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
1976 QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
1981 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
1983 MapCenterObj *mapCenter = new MapCenterObj(mapScene);
1984 mapCenter->move (absPos);
1985 mapCenter->setVisibility (true);
1986 mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
1987 mapCenter->setMapEditor(mapEditor); //FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
1988 mapCenters.append(mapCenter);
1992 MapCenterObj* VymModel::removeMapCenter(MapCenterObj* mco)
1994 int i=mapCenters.indexOf (mco);
1997 mapCenters.removeAt (i);
1999 if (i>0) return mapCenters.at(i-1); // Return previous MCO
2004 MapCenterObj* VymModel::getLastMapCenter()
2006 if (mapCenters.size()>0)
2007 return mapCenters.last();
2014 BranchObj* VymModel::addNewBranchInt(int num)
2016 // Depending on pos:
2017 // -3 insert in children of parent above selection
2018 // -2 add branch to selection
2019 // -1 insert in children of parent below selection
2020 // 0..n insert in children of parent at pos
2021 BranchObj *newbo=NULL;
2022 BranchObj *bo=selection.getBranch();
2027 // save scroll state. If scrolled, automatically select
2028 // new branch in order to tmp unscroll parent...
2029 newbo=bo->addBranch();
2034 bo=(BranchObj*)bo->getParObj();
2035 if (bo) newbo=bo->insertBranch(num);
2039 bo=(BranchObj*)bo->getParObj();
2040 if (bo) newbo=bo->insertBranch(num);
2042 if (!newbo) return NULL;
2047 BranchObj* VymModel::addNewBranch(int pos)
2049 // Different meaning than num in addNewBranchInt!
2053 BranchObj *bo = selection.getBranch();
2054 BranchObj *newbo=NULL;
2058 // FIXME do we still need this in model? setCursor (Qt::ArrowCursor);
2060 newbo=addNewBranchInt (pos-2);
2068 QString ("addBranch (%1)").arg(pos),
2069 QString ("Add new branch to %1").arg(getObjectName(bo)));
2073 latestSelectionString=getSelectString(newbo);
2074 // In Network mode, the client needs to know where the new branch is,
2075 // so we have to pass on this information via saveState.
2076 // TODO: Get rid of this positioning workaround
2077 QString ps=qpointfToString (newbo->getAbsPos());
2078 sendData ("selectLatestAdded ()");
2079 sendData (QString("move %1").arg(ps));
2087 BranchObj* VymModel::addNewBranchBefore()
2089 BranchObj *newbo=NULL;
2090 BranchObj *bo = selection.getBranch();
2091 if (bo && selection.type()==Selection::Branch)
2092 // We accept no MapCenterObj here, so we _have_ a parent
2094 QPointF p=bo->getRelPos();
2097 BranchObj *parbo=(BranchObj*)(bo->getParObj());
2099 // add below selection
2100 newbo=parbo->insertBranch(bo->getNum()+1);
2103 newbo->move2RelPos (p);
2105 // Move selection to new branch
2106 bo->linkTo (newbo,-1);
2108 saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()",
2109 QString ("Add branch before %1").arg(getObjectName(bo)));
2115 latestSelectionString=selection.getSelectString();
2119 void VymModel::deleteSelection()
2121 BranchObj *bo = selection.getBranch();
2122 if (bo && selection.type()==Selection::MapCenter)
2124 // BranchObj* par=(BranchObj*)(bo->getParObj());
2125 selection.unselect();
2126 /* FIXME Note: does saveStateRemovingPart work for MCO? (No parent!)
2127 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2129 bo=removeMapCenter ((MapCenterObj*)bo);
2132 selection.select (bo);
2133 ensureSelectionVisible();
2139 if (bo && selection.type()==Selection::Branch)
2141 //FIXME need to check if any animObj is part of Branch, remove from animated obj list then
2146 BranchObj* par=(BranchObj*)bo->getParObj();
2147 selection.unselect();
2148 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2149 par->removeBranch(bo);
2150 selection.select (par);
2151 ensureSelectionVisible();
2156 FloatImageObj *fio=selection.getFloatImage();
2159 BranchObj* par=(BranchObj*)fio->getParObj();
2160 saveStateChangingPart(
2164 QString("Delete %1").arg(getObjectName(fio))
2166 selection.unselect();
2167 par->removeFloatImage(fio);
2168 selection.select (par);
2171 ensureSelectionVisible();
2176 void VymModel::deleteKeepChildren()
2178 BranchObj *bo=selection.getBranch();
2182 par=(BranchObj*)(bo->getParObj());
2183 QPointF p=bo->getRelPos();
2184 saveStateChangingPart(
2187 "deleteKeepChildren ()",
2188 QString("Remove %1 and keep its children").arg(getObjectName(bo))
2191 QString sel=getSelectString(bo);
2193 par->removeBranchHere(bo);
2196 selection.getBranch()->move2RelPos (p);
2201 void VymModel::deleteChildren()
2203 BranchObj *bo=selection.getBranch();
2206 saveStateChangingPart(
2209 "deleteChildren ()",
2210 QString( "Remove children of branch %1").arg(getObjectName(bo))
2212 bo->removeChildren();
2218 bool VymModel::scrollBranch(BranchObj *bo)
2222 if (bo->isScrolled()) return false;
2223 if (bo->countBranches()==0) return false;
2224 if (bo->getDepth()==0) return false;
2230 QString ("%1 ()").arg(u),
2232 QString ("%1 ()").arg(r),
2233 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2237 // FIXME needed? scene()->update();
2243 bool VymModel::unscrollBranch(BranchObj *bo)
2247 if (!bo->isScrolled()) return false;
2248 if (bo->countBranches()==0) return false;
2249 if (bo->getDepth()==0) return false;
2255 QString ("%1 ()").arg(u),
2257 QString ("%1 ()").arg(r),
2258 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2262 // FIXME needed? scene()->update();
2268 void VymModel::toggleScroll()
2270 BranchObj *bo=selection.getBranch();
2271 if (selection.type()==Selection::Branch )
2273 if (bo->isScrolled())
2274 unscrollBranch (bo);
2280 void VymModel::unscrollChildren()
2282 BranchObj *bo=selection.getBranch();
2288 if (bo->isScrolled()) unscrollBranch (bo);
2293 void VymModel::addFloatImage (const QPixmap &img)
2295 BranchObj *bo=selection.getBranch();
2298 FloatImageObj *fio=bo->addFloatImage();
2300 fio->setOriginalFilename("No original filename (image added by dropevent)");
2301 QString s=getSelectString(bo);
2302 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio );
2303 saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
2305 // FIXME needed? scene()->update();
2310 void VymModel::colorBranch (QColor c)
2312 BranchObj *bo=selection.getBranch();
2317 QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
2319 QString ("colorBranch (\"%1\")").arg(c.name()),
2320 QString("Set color of %1 to %2").arg(getObjectName(bo)).arg(c.name())
2322 bo->setColor(c); // color branch
2326 void VymModel::colorSubtree (QColor c)
2328 BranchObj *bo=selection.getBranch();
2331 saveStateChangingPart(
2334 QString ("colorSubtree (\"%1\")").arg(c.name()),
2335 QString ("Set color of %1 and children to %2").arg(getObjectName(bo)).arg(c.name())
2337 bo->setColorSubtree (c); // color links, color children
2341 QColor VymModel::getCurrentHeadingColor()
2343 BranchObj *bo=selection.getBranch();
2344 if (bo) return bo->getColor();
2346 QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
2352 void VymModel::editURL()
2354 BranchObj *bo=selection.getBranch();
2358 QString text = QInputDialog::getText(
2359 "VYM", tr("Enter URL:"), QLineEdit::Normal,
2360 bo->getURL(), &ok, NULL);
2362 // user entered something and pressed OK
2367 void VymModel::editLocalURL()
2369 BranchObj *bo=selection.getBranch();
2372 QStringList filters;
2373 filters <<"All files (*)";
2374 filters << tr("Text","Filedialog") + " (*.txt)";
2375 filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
2376 filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
2377 filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
2378 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
2379 fd->setFilters (filters);
2380 fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
2381 fd->setDirectory (lastFileDir);
2382 if (! bo->getVymLink().isEmpty() )
2383 fd->selectFile( bo->getURL() );
2386 if ( fd->exec() == QDialog::Accepted )
2388 lastFileDir=QDir (fd->directory().path());
2389 setURL (fd->selectedFile() );
2395 void VymModel::editHeading2URL()
2397 BranchObj *bo=selection.getBranch();
2399 setURL (bo->getHeading());
2402 void VymModel::editBugzilla2URL()
2404 BranchObj *bo=selection.getBranch();
2407 QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
2412 void VymModel::editFATE2URL()
2414 BranchObj *bo=selection.getBranch();
2417 QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
2420 "setURL (\""+bo->getURL()+"\")",
2422 "setURL (\""+url+"\")",
2423 QString("Use heading of %1 as link to FATE").arg(getObjectName(bo))
2430 void VymModel::editVymLink()
2432 BranchObj *bo=selection.getBranch();
2435 QStringList filters;
2436 filters <<"VYM map (*.vym)";
2437 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
2438 fd->setFilters (filters);
2439 fd->setCaption(vymName+" - " +tr("Link to another map"));
2440 fd->setDirectory (lastFileDir);
2441 if (! bo->getVymLink().isEmpty() )
2442 fd->selectFile( bo->getVymLink() );
2446 if ( fd->exec() == QDialog::Accepted )
2448 lastFileDir=QDir (fd->directory().path());
2451 "setVymLink (\""+bo->getVymLink()+"\")",
2453 "setVymLink (\""+fd->selectedFile()+"\")",
2454 QString("Set vymlink of %1 to %2").arg(getObjectName(bo)).arg(fd->selectedFile())
2456 setVymLink (fd->selectedFile() ); // FIXME ok?
2461 void VymModel::setVymLink (const QString &s)
2463 // Internal function, no saveState needed
2464 BranchObj *bo=selection.getBranch();
2471 ensureSelectionVisible();
2475 void VymModel::deleteVymLink()
2477 BranchObj *bo=selection.getBranch();
2482 "setVymLink (\""+bo->getVymLink()+"\")",
2484 "setVymLink (\"\")",
2485 QString("Unset vymlink of %1").arg(getObjectName(bo))
2487 bo->setVymLink ("" );
2490 // FIXME needed? scene()->update();
2494 QString VymModel::getVymLink()
2496 BranchObj *bo=selection.getBranch();
2498 return bo->getVymLink();
2504 QStringList VymModel::getVymLinks()
2507 BranchObj *bo=selection.getBranch();
2513 if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
2521 void VymModel::followXLink(int i)
2523 BranchObj *bo=selection.getBranch();
2526 bo=bo->XLinkTargetAt(i);
2529 selection.select(bo);
2530 ensureSelectionVisible();
2535 void VymModel::editXLink(int i) // FIXME missing saveState
2537 BranchObj *bo=selection.getBranch();
2540 XLinkObj *xlo=bo->XLinkAt(i);
2543 EditXLinkDialog dia;
2545 dia.setSelection(bo);
2546 if (dia.exec() == QDialog::Accepted)
2548 if (dia.useSettingsGlobal() )
2550 setMapDefXLinkColor (xlo->getColor() );
2551 setMapDefXLinkWidth (xlo->getWidth() );
2553 if (dia.deleteXLink())
2554 bo->deleteXLinkAt(i);
2564 //////////////////////////////////////////////
2566 //////////////////////////////////////////////
2568 void VymModel::parseAtom(const QString &atom)
2570 BranchObj *selb=selection.getBranch();
2576 // Split string s into command and parameters
2577 parser.parseAtom (atom);
2578 QString com=parser.getCommand();
2580 // External commands
2581 /////////////////////////////////////////////////////////////////////
2582 if (com=="addBranch")
2584 if (selection.isEmpty())
2586 parser.setError (Aborted,"Nothing selected");
2589 parser.setError (Aborted,"Type of selection is not a branch");
2594 if (parser.checkParCount(pl))
2596 if (parser.parCount()==0)
2600 n=parser.parInt (ok,0);
2601 if (ok ) addNewBranch (n);
2605 /////////////////////////////////////////////////////////////////////
2606 } else if (com=="addBranchBefore")
2608 if (selection.isEmpty())
2610 parser.setError (Aborted,"Nothing selected");
2613 parser.setError (Aborted,"Type of selection is not a branch");
2616 if (parser.parCount()==0)
2618 addNewBranchBefore ();
2621 /////////////////////////////////////////////////////////////////////
2622 } else if (com==QString("addMapCenter"))
2624 if (parser.checkParCount(2))
2626 x=parser.parDouble (ok,0);
2629 y=parser.parDouble (ok,1);
2630 if (ok) addMapCenter (QPointF(x,y));
2633 /////////////////////////////////////////////////////////////////////
2634 } else if (com==QString("addMapReplace"))
2636 if (selection.isEmpty())
2638 parser.setError (Aborted,"Nothing selected");
2641 parser.setError (Aborted,"Type of selection is not a branch");
2642 } else if (parser.checkParCount(1))
2644 //s=parser.parString (ok,0); // selection
2645 t=parser.parString (ok,0); // path to map
2646 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2647 addMapReplaceInt(getSelectString(selb),t);
2649 /////////////////////////////////////////////////////////////////////
2650 } else if (com==QString("addMapInsert"))
2652 if (selection.isEmpty())
2654 parser.setError (Aborted,"Nothing selected");
2657 parser.setError (Aborted,"Type of selection is not a branch");
2660 if (parser.checkParCount(2))
2662 t=parser.parString (ok,0); // path to map
2663 n=parser.parInt(ok,1); // position
2664 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2665 addMapInsertInt(t,n);
2668 /////////////////////////////////////////////////////////////////////
2669 } else if (com=="clearFlags")
2671 if (selection.isEmpty() )
2673 parser.setError (Aborted,"Nothing selected");
2676 parser.setError (Aborted,"Type of selection is not a branch");
2677 } else if (parser.checkParCount(0))
2679 selb->clearStandardFlags();
2680 selb->updateFlagsToolbar();
2682 /////////////////////////////////////////////////////////////////////
2683 } else if (com=="colorBranch")
2685 if (selection.isEmpty())
2687 parser.setError (Aborted,"Nothing selected");
2690 parser.setError (Aborted,"Type of selection is not a branch");
2691 } else if (parser.checkParCount(1))
2693 QColor c=parser.parColor (ok,0);
2694 if (ok) colorBranch (c);
2696 /////////////////////////////////////////////////////////////////////
2697 } else if (com=="colorSubtree")
2699 if (selection.isEmpty())
2701 parser.setError (Aborted,"Nothing selected");
2704 parser.setError (Aborted,"Type of selection is not a branch");
2705 } else if (parser.checkParCount(1))
2707 QColor c=parser.parColor (ok,0);
2708 if (ok) colorSubtree (c);
2710 /////////////////////////////////////////////////////////////////////
2711 } else if (com=="copy")
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(0))
2721 //FIXME missing action for copy
2723 /////////////////////////////////////////////////////////////////////
2724 } else if (com=="cut")
2726 if (selection.isEmpty())
2728 parser.setError (Aborted,"Nothing selected");
2729 } else if ( selection.type()!=Selection::Branch &&
2730 selection.type()!=Selection::MapCenter &&
2731 selection.type()!=Selection::FloatImage )
2733 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2734 } else if (parser.checkParCount(0))
2738 /////////////////////////////////////////////////////////////////////
2739 } else if (com=="delete")
2741 if (selection.isEmpty())
2743 parser.setError (Aborted,"Nothing selected");
2745 /*else if (selection.type() != Selection::Branch && selection.type() != Selection::FloatImage )
2747 parser.setError (Aborted,"Type of selection is wrong.");
2750 else if (parser.checkParCount(0))
2754 /////////////////////////////////////////////////////////////////////
2755 } else if (com=="deleteKeepChildren")
2757 if (selection.isEmpty())
2759 parser.setError (Aborted,"Nothing selected");
2762 parser.setError (Aborted,"Type of selection is not a branch");
2763 } else if (parser.checkParCount(0))
2765 deleteKeepChildren();
2767 /////////////////////////////////////////////////////////////////////
2768 } else if (com=="deleteChildren")
2770 if (selection.isEmpty())
2772 parser.setError (Aborted,"Nothing selected");
2775 parser.setError (Aborted,"Type of selection is not a branch");
2776 } else if (parser.checkParCount(0))
2780 /////////////////////////////////////////////////////////////////////
2781 } else if (com=="exportASCII")
2785 if (parser.parCount()>=1)
2786 // Hey, we even have a filename
2787 fname=parser.parString(ok,0);
2790 parser.setError (Aborted,"Could not read filename");
2793 exportASCII (fname,false);
2795 /////////////////////////////////////////////////////////////////////
2796 } else if (com=="exportImage")
2800 if (parser.parCount()>=2)
2801 // Hey, we even have a filename
2802 fname=parser.parString(ok,0);
2805 parser.setError (Aborted,"Could not read filename");
2808 QString format="PNG";
2809 if (parser.parCount()>=2)
2811 format=parser.parString(ok,1);
2813 exportImage (fname,false,format);
2815 /////////////////////////////////////////////////////////////////////
2816 } else if (com=="exportXHTML")
2820 if (parser.parCount()>=2)
2821 // Hey, we even have a filename
2822 fname=parser.parString(ok,1);
2825 parser.setError (Aborted,"Could not read filename");
2828 exportXHTML (fname,false);
2830 /////////////////////////////////////////////////////////////////////
2831 } else if (com=="exportXML")
2835 if (parser.parCount()>=2)
2836 // Hey, we even have a filename
2837 fname=parser.parString(ok,1);
2840 parser.setError (Aborted,"Could not read filename");
2843 exportXML (fname,false);
2845 /////////////////////////////////////////////////////////////////////
2846 } else if (com=="importDir")
2848 if (selection.isEmpty())
2850 parser.setError (Aborted,"Nothing selected");
2853 parser.setError (Aborted,"Type of selection is not a branch");
2854 } else if (parser.checkParCount(1))
2856 s=parser.parString(ok,0);
2857 if (ok) importDirInt(s);
2859 /////////////////////////////////////////////////////////////////////
2860 } else if (com=="linkTo")
2862 if (selection.isEmpty())
2864 parser.setError (Aborted,"Nothing selected");
2867 if (parser.checkParCount(4))
2869 // 0 selectstring of parent
2870 // 1 num in parent (for branches)
2871 // 2,3 x,y of mainbranch or mapcenter
2872 s=parser.parString(ok,0);
2873 LinkableMapObj *dst=findObjBySelect (s);
2876 if (typeid(*dst) == typeid(BranchObj) )
2878 // Get number in parent
2879 n=parser.parInt (ok,1);
2882 selb->linkTo ((BranchObj*)(dst),n);
2885 } else if (typeid(*dst) == typeid(MapCenterObj) )
2887 selb->linkTo ((BranchObj*)(dst),-1);
2888 // Get coordinates of mainbranch
2889 x=parser.parDouble(ok,2);
2892 y=parser.parDouble(ok,3);
2902 } else if ( selection.type() == Selection::FloatImage)
2904 if (parser.checkParCount(1))
2906 // 0 selectstring of parent
2907 s=parser.parString(ok,0);
2908 LinkableMapObj *dst=findObjBySelect (s);
2911 if (typeid(*dst) == typeid(BranchObj) ||
2912 typeid(*dst) == typeid(MapCenterObj))
2913 linkFloatImageTo (getSelectString(dst));
2915 parser.setError (Aborted,"Destination is not a branch");
2918 parser.setError (Aborted,"Type of selection is not a floatimage or branch");
2919 /////////////////////////////////////////////////////////////////////
2920 } else if (com=="loadImage")
2922 if (selection.isEmpty())
2924 parser.setError (Aborted,"Nothing selected");
2927 parser.setError (Aborted,"Type of selection is not a branch");
2928 } else if (parser.checkParCount(1))
2930 s=parser.parString(ok,0);
2931 if (ok) loadFloatImageInt (s);
2933 /////////////////////////////////////////////////////////////////////
2934 } else if (com=="moveBranchUp")
2936 if (selection.isEmpty() )
2938 parser.setError (Aborted,"Nothing selected");
2941 parser.setError (Aborted,"Type of selection is not a branch");
2942 } else if (parser.checkParCount(0))
2946 /////////////////////////////////////////////////////////////////////
2947 } else if (com=="moveBranchDown")
2949 if (selection.isEmpty() )
2951 parser.setError (Aborted,"Nothing selected");
2954 parser.setError (Aborted,"Type of selection is not a branch");
2955 } else if (parser.checkParCount(0))
2959 /////////////////////////////////////////////////////////////////////
2960 } else if (com=="move")
2962 if (selection.isEmpty() )
2964 parser.setError (Aborted,"Nothing selected");
2965 } else if ( selection.type()!=Selection::Branch &&
2966 selection.type()!=Selection::MapCenter &&
2967 selection.type()!=Selection::FloatImage )
2969 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2970 } else if (parser.checkParCount(2))
2972 x=parser.parDouble (ok,0);
2975 y=parser.parDouble (ok,1);
2979 /////////////////////////////////////////////////////////////////////
2980 } else if (com=="moveRel")
2982 if (selection.isEmpty() )
2984 parser.setError (Aborted,"Nothing selected");
2985 } else if ( selection.type()!=Selection::Branch &&
2986 selection.type()!=Selection::MapCenter &&
2987 selection.type()!=Selection::FloatImage )
2989 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2990 } else if (parser.checkParCount(2))
2992 x=parser.parDouble (ok,0);
2995 y=parser.parDouble (ok,1);
2996 if (ok) moveRel (x,y);
2999 /////////////////////////////////////////////////////////////////////
3000 } else if (com=="nop")
3002 /////////////////////////////////////////////////////////////////////
3003 } else if (com=="paste")
3005 if (selection.isEmpty() )
3007 parser.setError (Aborted,"Nothing selected");
3010 parser.setError (Aborted,"Type of selection is not a branch");
3011 } else if (parser.checkParCount(1))
3013 n=parser.parInt (ok,0);
3014 if (ok) pasteNoSave(n);
3016 /////////////////////////////////////////////////////////////////////
3017 } else if (com=="qa")
3019 if (selection.isEmpty() )
3021 parser.setError (Aborted,"Nothing selected");
3024 parser.setError (Aborted,"Type of selection is not a branch");
3025 } else if (parser.checkParCount(4))
3028 c=parser.parString (ok,0);
3031 parser.setError (Aborted,"No comment given");
3034 s=parser.parString (ok,1);
3037 parser.setError (Aborted,"First parameter is not a string");
3040 t=parser.parString (ok,2);
3043 parser.setError (Aborted,"Condition is not a string");
3046 u=parser.parString (ok,3);
3049 parser.setError (Aborted,"Third parameter is not a string");
3054 parser.setError (Aborted,"Unknown type: "+s);
3059 parser.setError (Aborted,"Unknown operator: "+t);
3064 parser.setError (Aborted,"Type of selection is not a branch");
3067 if (selb->getHeading() == u)
3069 cout << "PASSED: " << qPrintable (c) << endl;
3072 cout << "FAILED: " << qPrintable (c) << endl;
3082 /////////////////////////////////////////////////////////////////////
3083 } else if (com=="saveImage")
3085 FloatImageObj *fio=selection.getFloatImage();
3088 parser.setError (Aborted,"Type of selection is not an image");
3089 } else if (parser.checkParCount(2))
3091 s=parser.parString(ok,0);
3094 t=parser.parString(ok,1);
3095 if (ok) saveFloatImageInt (fio,t,s);
3098 /////////////////////////////////////////////////////////////////////
3099 } else if (com=="scroll")
3101 if (selection.isEmpty() )
3103 parser.setError (Aborted,"Nothing selected");
3106 parser.setError (Aborted,"Type of selection is not a branch");
3107 } else if (parser.checkParCount(0))
3109 if (!scrollBranch (selb))
3110 parser.setError (Aborted,"Could not scroll branch");
3112 /////////////////////////////////////////////////////////////////////
3113 } else if (com=="select")
3115 if (parser.checkParCount(1))
3117 s=parser.parString(ok,0);
3120 /////////////////////////////////////////////////////////////////////
3121 } else if (com=="selectLastBranch")
3123 if (selection.isEmpty() )
3125 parser.setError (Aborted,"Nothing selected");
3128 parser.setError (Aborted,"Type of selection is not a branch");
3129 } else if (parser.checkParCount(0))
3131 BranchObj *bo=selb->getLastBranch();
3133 parser.setError (Aborted,"Could not select last branch");
3137 /////////////////////////////////////////////////////////////////////
3138 } else if (com=="selectLastImage")
3140 if (selection.isEmpty() )
3142 parser.setError (Aborted,"Nothing selected");
3145 parser.setError (Aborted,"Type of selection is not a branch");
3146 } else if (parser.checkParCount(0))
3148 FloatImageObj *fio=selb->getLastFloatImage();
3150 parser.setError (Aborted,"Could not select last image");
3154 /////////////////////////////////////////////////////////////////////
3155 } else if (com=="selectLatestAdded")
3157 if (latestSelectionString.isEmpty() )
3159 parser.setError (Aborted,"No latest added object");
3162 if (!select (latestSelectionString))
3163 parser.setError (Aborted,"Could not select latest added object "+latestSelectionString);
3165 /////////////////////////////////////////////////////////////////////
3166 } else if (com=="setFrameType")
3168 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3170 parser.setError (Aborted,"Type of selection does not allow setting frame type");
3172 else if (parser.checkParCount(1))
3174 s=parser.parString(ok,0);
3175 if (ok) setFrameType (s);
3177 /////////////////////////////////////////////////////////////////////
3178 } else if (com=="setFramePenColor")
3180 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3182 parser.setError (Aborted,"Type of selection does not allow setting of pen color");
3184 else if (parser.checkParCount(1))
3186 QColor c=parser.parColor(ok,0);
3187 if (ok) setFramePenColor (c);
3189 /////////////////////////////////////////////////////////////////////
3190 } else if (com=="setFrameBrushColor")
3192 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3194 parser.setError (Aborted,"Type of selection does not allow setting brush color");
3196 else if (parser.checkParCount(1))
3198 QColor c=parser.parColor(ok,0);
3199 if (ok) setFrameBrushColor (c);
3201 /////////////////////////////////////////////////////////////////////
3202 } else if (com=="setFramePadding")
3204 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3206 parser.setError (Aborted,"Type of selection does not allow setting frame padding");
3208 else if (parser.checkParCount(1))
3210 n=parser.parInt(ok,0);
3211 if (ok) setFramePadding(n);
3213 /////////////////////////////////////////////////////////////////////
3214 } else if (com=="setFrameBorderWidth")
3216 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3218 parser.setError (Aborted,"Type of selection does not allow setting frame border width");
3220 else if (parser.checkParCount(1))
3222 n=parser.parInt(ok,0);
3223 if (ok) setFrameBorderWidth (n);
3225 /////////////////////////////////////////////////////////////////////
3226 } else if (com=="setMapAuthor")
3228 if (parser.checkParCount(1))
3230 s=parser.parString(ok,0);
3231 if (ok) setAuthor (s);
3233 /////////////////////////////////////////////////////////////////////
3234 } else if (com=="setMapComment")
3236 if (parser.checkParCount(1))
3238 s=parser.parString(ok,0);
3239 if (ok) setComment(s);
3241 /////////////////////////////////////////////////////////////////////
3242 } else if (com=="setMapBackgroundColor")
3244 if (selection.isEmpty() )
3246 parser.setError (Aborted,"Nothing selected");
3247 } else if (! selection.getBranch() )
3249 parser.setError (Aborted,"Type of selection is not a branch");
3250 } else if (parser.checkParCount(1))
3252 QColor c=parser.parColor (ok,0);
3253 if (ok) setMapBackgroundColor (c);
3255 /////////////////////////////////////////////////////////////////////
3256 } else if (com=="setMapDefLinkColor")
3258 if (selection.isEmpty() )
3260 parser.setError (Aborted,"Nothing selected");
3263 parser.setError (Aborted,"Type of selection is not a branch");
3264 } else if (parser.checkParCount(1))
3266 QColor c=parser.parColor (ok,0);
3267 if (ok) setMapDefLinkColor (c);
3269 /////////////////////////////////////////////////////////////////////
3270 } else if (com=="setMapLinkStyle")
3272 if (parser.checkParCount(1))
3274 s=parser.parString (ok,0);
3275 if (ok) setMapLinkStyle(s);
3277 /////////////////////////////////////////////////////////////////////
3278 } else if (com=="setHeading")
3280 if (selection.isEmpty() )
3282 parser.setError (Aborted,"Nothing selected");
3285 parser.setError (Aborted,"Type of selection is not a branch");
3286 } else if (parser.checkParCount(1))
3288 s=parser.parString (ok,0);
3292 /////////////////////////////////////////////////////////////////////
3293 } else if (com=="setHideExport")
3295 if (selection.isEmpty() )
3297 parser.setError (Aborted,"Nothing selected");
3298 } else if (selection.type()!=Selection::Branch && selection.type() != Selection::MapCenter &&selection.type()!=Selection::FloatImage)
3300 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3301 } else if (parser.checkParCount(1))
3303 b=parser.parBool(ok,0);
3304 if (ok) setHideExport (b);
3306 /////////////////////////////////////////////////////////////////////
3307 } else if (com=="setIncludeImagesHorizontally")
3309 if (selection.isEmpty() )
3311 parser.setError (Aborted,"Nothing selected");
3314 parser.setError (Aborted,"Type of selection is not a branch");
3315 } else if (parser.checkParCount(1))
3317 b=parser.parBool(ok,0);
3318 if (ok) setIncludeImagesHor(b);
3320 /////////////////////////////////////////////////////////////////////
3321 } else if (com=="setIncludeImagesVertically")
3323 if (selection.isEmpty() )
3325 parser.setError (Aborted,"Nothing selected");
3328 parser.setError (Aborted,"Type of selection is not a branch");
3329 } else if (parser.checkParCount(1))
3331 b=parser.parBool(ok,0);
3332 if (ok) setIncludeImagesVer(b);
3334 /////////////////////////////////////////////////////////////////////
3335 } else if (com=="setHideLinkUnselected")
3337 if (selection.isEmpty() )
3339 parser.setError (Aborted,"Nothing selected");
3340 } else if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3342 parser.setError (Aborted,"Type of selection does not allow hiding the link");
3343 } else if (parser.checkParCount(1))
3345 b=parser.parBool(ok,0);
3346 if (ok) setHideLinkUnselected(b);
3348 /////////////////////////////////////////////////////////////////////
3349 } else if (com=="setSelectionColor")
3351 if (parser.checkParCount(1))
3353 QColor c=parser.parColor (ok,0);
3354 if (ok) setSelectionColorInt (c);
3356 /////////////////////////////////////////////////////////////////////
3357 } else if (com=="setURL")
3359 if (selection.isEmpty() )
3361 parser.setError (Aborted,"Nothing selected");
3364 parser.setError (Aborted,"Type of selection is not a branch");
3365 } else if (parser.checkParCount(1))
3367 s=parser.parString (ok,0);
3370 /////////////////////////////////////////////////////////////////////
3371 } else if (com=="setVymLink")
3373 if (selection.isEmpty() )
3375 parser.setError (Aborted,"Nothing selected");
3378 parser.setError (Aborted,"Type of selection is not a branch");
3379 } else if (parser.checkParCount(1))
3381 s=parser.parString (ok,0);
3382 if (ok) setVymLink(s);
3385 /////////////////////////////////////////////////////////////////////
3386 else if (com=="setFlag")
3388 if (selection.isEmpty() )
3390 parser.setError (Aborted,"Nothing selected");
3393 parser.setError (Aborted,"Type of selection is not a branch");
3394 } else if (parser.checkParCount(1))
3396 s=parser.parString(ok,0);
3399 selb->activateStandardFlag(s);
3400 selb->updateFlagsToolbar();
3403 /////////////////////////////////////////////////////////////////////
3404 } else if (com=="setFrameType")
3406 if (selection.isEmpty() )
3408 parser.setError (Aborted,"Nothing selected");
3411 parser.setError (Aborted,"Type of selection is not a branch");
3412 } else if (parser.checkParCount(1))
3414 s=parser.parString(ok,0);
3418 /////////////////////////////////////////////////////////////////////
3419 } else if (com=="sortChildren")
3421 if (selection.isEmpty() )
3423 parser.setError (Aborted,"Nothing selected");
3426 parser.setError (Aborted,"Type of selection is not a branch");
3427 } else if (parser.checkParCount(0))
3431 /////////////////////////////////////////////////////////////////////
3432 } else if (com=="toggleFlag")
3434 if (selection.isEmpty() )
3436 parser.setError (Aborted,"Nothing selected");
3439 parser.setError (Aborted,"Type of selection is not a branch");
3440 } else if (parser.checkParCount(1))
3442 s=parser.parString(ok,0);
3445 selb->toggleStandardFlag(s);
3446 selb->updateFlagsToolbar();
3449 /////////////////////////////////////////////////////////////////////
3450 } else if (com=="unscroll")
3452 if (selection.isEmpty() )
3454 parser.setError (Aborted,"Nothing selected");
3457 parser.setError (Aborted,"Type of selection is not a branch");
3458 } else if (parser.checkParCount(0))
3460 if (!unscrollBranch (selb))
3461 parser.setError (Aborted,"Could not unscroll branch");
3463 /////////////////////////////////////////////////////////////////////
3464 } else if (com=="unscrollChildren")
3466 if (selection.isEmpty() )
3468 parser.setError (Aborted,"Nothing selected");
3471 parser.setError (Aborted,"Type of selection is not a branch");
3472 } else if (parser.checkParCount(0))
3474 unscrollChildren ();
3476 /////////////////////////////////////////////////////////////////////
3477 } else if (com=="unsetFlag")
3479 if (selection.isEmpty() )
3481 parser.setError (Aborted,"Nothing selected");
3484 parser.setError (Aborted,"Type of selection is not a branch");
3485 } else if (parser.checkParCount(1))
3487 s=parser.parString(ok,0);
3490 selb->deactivateStandardFlag(s);
3491 selb->updateFlagsToolbar();
3495 parser.setError (Aborted,"Unknown command");
3498 if (parser.errorLevel()==NoError)
3500 // setChanged(); FIXME should not be called e.g. for export?!
3505 // TODO Error handling
3506 qWarning("VymModel::parseAtom: Error!");
3507 qWarning(parser.errorMessage());
3511 void VymModel::runScript (QString script)
3513 parser.setScript (script);
3515 while (parser.next() )
3516 parseAtom(parser.getAtom());
3519 void VymModel::setExportMode (bool b)
3521 // should be called before and after exports
3522 // depending on the settings
3523 if (b && settings.value("/export/useHideExport","true")=="true")
3524 setHideTmpMode (HideExport);
3526 setHideTmpMode (HideNone);
3529 void VymModel::exportImage(QString fname, bool askName, QString format)
3533 fname=getMapName()+".png";
3540 QFileDialog *fd=new QFileDialog (NULL);
3541 fd->setCaption (tr("Export map as image"));
3542 fd->setDirectory (lastImageDir);
3543 fd->setFileMode(QFileDialog::AnyFile);
3544 fd->setFilters (imageIO.getFilters() );
3547 fl=fd->selectedFiles();
3549 format=imageIO.getType(fd->selectedFilter());
3553 setExportMode (true);
3554 QPixmap pix (getPixmap());
3555 pix.save(fname, format);
3556 setExportMode (false);
3560 void VymModel::exportXML(QString dir, bool askForName)
3564 dir=browseDirectory(NULL,tr("Export XML to directory"));
3565 if (dir =="" && !reallyWriteDirectory(dir) )
3569 // Hide stuff during export, if settings want this
3570 setExportMode (true);
3572 // Create subdirectories
3575 // write to directory
3576 QString saveFile=saveToDir (dir,mapName+"-",true,getTotalBBox().topLeft() ,NULL);
3579 file.setName ( dir + "/"+mapName+".xml");
3580 if ( !file.open( QIODevice::WriteOnly ) )
3582 // This should neverever happen
3583 QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
3587 // Write it finally, and write in UTF8, no matter what
3588 QTextStream ts( &file );
3589 ts.setEncoding (QTextStream::UnicodeUTF8);
3593 // Now write image, too
3594 exportImage (dir+"/images/"+mapName+".png",false,"PNG");
3596 setExportMode (false);
3599 void VymModel::exportASCII(QString fname,bool askName)
3604 ex.setFile (mapName+".txt");
3610 //ex.addFilter ("TXT (*.txt)");
3611 ex.setDir(lastImageDir);
3612 //ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
3617 setExportMode(true);
3619 setExportMode(false);
3623 void VymModel::exportXHTML (const QString &dir, bool askForName)
3625 ExportXHTMLDialog dia(NULL);
3626 dia.setFilePath (filePath );
3627 dia.setMapName (mapName );
3629 if (dir!="") dia.setDir (dir);
3635 if (dia.exec()!=QDialog::Accepted)
3639 QDir d (dia.getDir());
3640 // Check, if warnings should be used before overwriting
3641 // the output directory
3642 if (d.exists() && d.count()>0)
3645 warn.showCancelButton (true);
3646 warn.setText(QString(
3647 "The directory %1 is not empty.\n"
3648 "Do you risk to overwrite some of its contents?").arg(d.path() ));
3649 warn.setCaption("Warning: Directory not empty");
3650 warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
3652 if (warn.exec()!=QDialog::Accepted) ok=false;
3659 exportXML (dia.getDir(),false );
3660 dia.doExport(mapName );
3661 //if (dia.hasChanged()) setChanged();
3665 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
3670 if (ex.setConfigFile(cf))
3672 setExportMode (true);
3673 ex.exportPresentation();
3674 setExportMode (false);
3681 //////////////////////////////////////////////
3683 //////////////////////////////////////////////
3685 void VymModel::registerEditor(QWidget *me)
3687 mapEditor=(MapEditor*)me;
3688 for (int i=0; i<mapCenters.count(); i++)
3689 mapCenters.at(i)->setMapEditor(mapEditor);
3692 void VymModel::unregisterEditor(QWidget *)
3697 void VymModel::setContextPos(QPointF p)
3702 void VymModel::unsetContextPos()
3704 contextPos=QPointF();
3707 void VymModel::updateNoteFlag()
3710 BranchObj *bo=selection.getBranch();
3713 bo->updateNoteFlag();
3714 mainWindow->updateActions();
3718 void VymModel::updateRelPositions()
3720 for (int i=0; i<mapCenters.count(); i++)
3721 mapCenters.at(i)->updateRelPositions();
3724 void VymModel::reposition()
3726 for (int i=0;i<mapCenters.count(); i++)
3727 mapCenters.at(i)->reposition(); // for positioning heading
3730 QPolygonF VymModel::shape(BranchObj *bo)
3732 // Creating (arbitrary) shapes
3735 QRectF rb=bo->getBBox();
3736 if (bo->getDepth()==0)
3738 // Just take BBox of this mapCenter
3739 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
3743 // Take union of BBox and TotalBBox
3745 QRectF ra=bo->getTotalBBox();
3746 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
3749 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
3752 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
3754 p <<ra.bottomRight()
3756 <<QPointF (rb.topRight().x(), ra.topRight().y() )
3759 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
3764 void VymModel::moveAway(LinkableMapObj *lmo)
3768 // Move all branches and MapCenters away from lmo
3769 // to avoid collisions
3774 BranchObj *boA=(BranchObj*)lmo;
3776 for (int i=0; i<mapCenters.count(); i++)
3778 boB=mapCenters.at(i);
3781 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
3784 <<" ("<<qPrintable(boA->getHeading() )<<")"
3785 <<" with ("<< qPrintable (boB->getHeading() )
3788 <<" minT="<<r.minTranslation<<endl<<endl;
3792 QPixmap VymModel::getPixmap()
3794 QRectF mapRect=getTotalBBox();
3795 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
3798 pp.setRenderHints(mapEditor->renderHints());
3800 // Don't print the visualisation of selection
3801 selection.unselect();
3803 mapScene->render ( &pp,
3804 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
3805 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
3807 // Restore selection
3808 selection.reselect();
3814 void VymModel::setMapLinkStyle (const QString & s)
3817 if (linkstyle==LinkableMapObj::Line)
3819 else if (linkstyle==LinkableMapObj::Parabel)
3820 snow="StyleParabel";
3821 else if (linkstyle==LinkableMapObj::PolyLine)
3822 snow="StylePolyLine";
3823 else if (linkstyle==LinkableMapObj::PolyParabel)
3824 snow="StyleParabel";
3827 QString("setMapLinkStyle (\"%1\")").arg(s),
3828 QString("setMapLinkStyle (\"%1\")").arg(snow),
3829 QString("Set map link style (\"%1\")").arg(s)
3833 linkstyle=LinkableMapObj::Line;
3834 else if (s=="StyleParabel")
3835 linkstyle=LinkableMapObj::Parabel;
3836 else if (s=="StylePolyLine")
3837 linkstyle=LinkableMapObj::PolyLine;
3839 linkstyle=LinkableMapObj::PolyParabel;
3846 bo->setLinkStyle(bo->getDefLinkStyle());
3852 LinkableMapObj::Style VymModel::getMapLinkStyle ()
3857 void VymModel::setMapDefLinkColor(QColor col)
3859 if ( !col.isValid() ) return;
3861 QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
3862 QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
3863 QString("Set map link color to %1").arg(col.name())
3877 void VymModel::setMapLinkColorHintInt()
3879 // called from setMapLinkColorHint(lch) or at end of parse
3889 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
3892 setMapLinkColorHintInt();
3895 void VymModel::toggleMapLinkColorHint()
3897 if (linkcolorhint==LinkableMapObj::HeadingColor)
3898 linkcolorhint=LinkableMapObj::DefaultColor;
3900 linkcolorhint=LinkableMapObj::HeadingColor;
3910 void VymModel::selectMapBackgroundImage ()
3912 Q3FileDialog *fd=new Q3FileDialog( NULL);
3913 fd->setMode (Q3FileDialog::ExistingFile);
3914 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
3915 ImagePreview *p =new ImagePreview (fd);
3916 fd->setContentsPreviewEnabled( TRUE );
3917 fd->setContentsPreview( p, p );
3918 fd->setPreviewMode( Q3FileDialog::Contents );
3919 fd->setCaption(vymName+" - " +tr("Load background image"));
3920 fd->setDir (lastImageDir);
3923 if ( fd->exec() == QDialog::Accepted )
3925 // TODO selectMapBackgroundImg in QT4 use: lastImageDir=fd->directory();
3926 lastImageDir=QDir (fd->dirPath());
3927 setMapBackgroundImage (fd->selectedFile());
3931 void VymModel::setMapBackgroundImage (const QString &fn) //FIXME missing savestate
3933 QColor oldcol=mapScene->backgroundBrush().color();
3937 QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
3939 QString ("setMapBackgroundImage (%1)").arg(col.name()),
3940 QString("Set background color of map to %1").arg(col.name()));
3943 brush.setTextureImage (QPixmap (fn));
3944 mapScene->setBackgroundBrush(brush);
3947 void VymModel::selectMapBackgroundColor()
3949 QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
3950 if ( !col.isValid() ) return;
3951 setMapBackgroundColor( col );
3955 void VymModel::setMapBackgroundColor(QColor col)
3957 QColor oldcol=mapScene->backgroundBrush().color();
3959 QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
3960 QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
3961 QString("Set background color of map to %1").arg(col.name()));
3962 mapScene->setBackgroundBrush(col);
3965 QColor VymModel::getMapBackgroundColor()
3967 return mapScene->backgroundBrush().color();
3971 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint()
3973 return linkcolorhint;
3976 QColor VymModel::getMapDefLinkColor()
3978 return defLinkColor;
3981 void VymModel::setMapDefXLinkColor(QColor col)
3986 QColor VymModel::getMapDefXLinkColor()
3988 return defXLinkColor;
3991 void VymModel::setMapDefXLinkWidth (int w)
3996 int VymModel::getMapDefXLinkWidth()
3998 return defXLinkWidth;
4001 void VymModel::move(const double &x, const double &y)
4003 LinkableMapObj *sel=selection.single();
4006 QPointF ap(sel->getAbsPos());
4010 QString ps=qpointfToString(ap);
4011 QString s=selection.getSelectString();
4014 s, "move "+qpointfToString(to),
4015 QString("Move %1 to %2").arg(getObjectName(sel)).arg(ps));
4023 void VymModel::moveRel (const double &x, const double &y)
4025 LinkableMapObj *sel=selection.single();
4028 QPointF rp(sel->getRelPos());
4032 QString ps=qpointfToString (sel->getRelPos());
4033 QString s=getSelectString(sel);
4036 s, "moveRel "+qpointfToString(to),
4037 QString("Move %1 to relative position %2").arg(getObjectName(sel)).arg(ps));
4038 ((OrnamentedObj*)sel)->move2RelPos (x,y);
4047 void VymModel::animate()
4049 animationTimer->stop();
4052 while (i<animObjList.size() )
4054 bo=(BranchObj*)animObjList.at(i);
4059 animObjList.removeAt(i);
4066 mapEditor->updateSelection();
4068 if (!animObjList.isEmpty()) animationTimer->start();
4072 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
4074 if (bo && bo->getDepth()>0)
4077 ap.setStart (start);
4079 ap.setTicks (animationTicks);
4080 ap.setAnimated (true);
4081 bo->setAnimation (ap);
4082 animObjList.append( bo );
4083 animationTimer->setSingleShot (true);
4084 animationTimer->start(animationInterval);
4088 void VymModel::stopAnimation (MapObj *mo)
4090 int i=animObjList.indexOf(mo);
4092 animObjList.removeAt (i);
4095 void VymModel::sendSelection()
4097 if (netstate!=Server) return;
4098 sendData (QString("select (\"%1\")").arg(selection.getSelectString()) );
4101 void VymModel::newServer()
4105 tcpServer = new QTcpServer(this);
4106 if (!tcpServer->listen(QHostAddress::Any,port)) {
4107 QMessageBox::critical(NULL, "vym server",
4108 QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
4109 //FIXME needed? we are no widget any longer... close();
4112 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
4114 cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
4117 void VymModel::connectToServer()
4120 server="salam.suse.de";
4122 clientSocket = new QTcpSocket (this);
4123 clientSocket->abort();
4124 clientSocket->connectToHost(server ,port);
4125 connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
4126 connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
4127 this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
4129 cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
4134 void VymModel::newClient()
4136 QTcpSocket *newClient = tcpServer->nextPendingConnection();
4137 connect(newClient, SIGNAL(disconnected()),
4138 newClient, SLOT(deleteLater()));
4140 cout <<"ME::newClient at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
4142 clientList.append (newClient);
4146 void VymModel::sendData(const QString &s)
4148 if (clientList.size()==0) return;
4150 // Create bytearray to send
4152 QDataStream out(&block, QIODevice::WriteOnly);
4153 out.setVersion(QDataStream::Qt_4_0);
4155 // Reserve some space for blocksize
4158 // Write sendCounter
4159 out << sendCounter++;
4164 // Go back and write blocksize so far
4165 out.device()->seek(0);
4166 quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
4170 cout << "ME::sendData bs="<<bs<<" counter="<<sendCounter<<" s="<<qPrintable(s)<<endl;
4172 for (int i=0; i<clientList.size(); ++i)
4174 //cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
4175 clientList.at(i)->write (block);
4179 void VymModel::readData ()
4181 while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
4184 cout <<"readData bytesAvail="<<clientSocket->bytesAvailable();
4188 QDataStream in(clientSocket);
4189 in.setVersion(QDataStream::Qt_4_0);
4197 cout << " t="<<qPrintable (t)<<endl;
4203 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
4205 switch (socketError) {
4206 case QAbstractSocket::RemoteHostClosedError:
4208 case QAbstractSocket::HostNotFoundError:
4209 QMessageBox::information(NULL, vymName +" Network client",
4210 "The host was not found. Please check the "
4211 "host name and port settings.");
4213 case QAbstractSocket::ConnectionRefusedError:
4214 QMessageBox::information(NULL, vymName + " Network client",
4215 "The connection was refused by the peer. "
4216 "Make sure the fortune server is running, "
4217 "and check that the host name and port "
4218 "settings are correct.");
4221 QMessageBox::information(NULL, vymName + " Network client",
4222 QString("The following error occurred: %1.")
4223 .arg(clientSocket->errorString()));
4230 void VymModel::selectMapSelectionColor()
4232 QColor col = QColorDialog::getColor( defLinkColor, NULL);
4233 setSelectionColor (col);
4236 void VymModel::setSelectionColorInt (QColor col)
4238 if ( !col.isValid() ) return;
4240 QString("setSelectionColor (%1)").arg(selection.getColor().name()),
4241 QString("setSelectionColor (%1)").arg(col.name()),
4242 QString("Set color of selection box to %1").arg(col.name())
4245 selection.setColor (col);
4248 void VymModel::updateSelection()
4253 void VymModel::setSelectionColor(QColor col)
4255 if ( !col.isValid() ) return;
4257 QString("setSelectionColor (%1)").arg(selection.getColor().name()),
4258 QString("setSelectionColor (%1)").arg(col.name()),
4259 QString("Set color of selection box to %1").arg(col.name())
4261 setSelectionColorInt (col);
4264 QColor VymModel::getSelectionColor()
4266 return selection.getColor();
4269 void VymModel::setHideTmpMode (HideTmpMode mode)
4272 for (int i=0;i<mapCenters.count(); i++)
4273 mapCenters.at(i)->setHideTmp (mode);
4275 // FIXME needed? scene()->update();
4279 QRectF VymModel::getTotalBBox()
4282 for (int i=0;i<mapCenters.count(); i++)
4283 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
4287 //////////////////////////////////////////////
4288 // Selection related
4289 //////////////////////////////////////////////
4291 void VymModel::setSelectionBlocked (bool b)
4296 selection.unblock();
4299 bool VymModel::isSelectionBlocked()
4301 return selection.isBlocked();
4304 bool VymModel::select (const QString &s)
4306 if (selection.select(s))
4309 ensureSelectionVisible();
4317 bool VymModel::select (LinkableMapObj *lmo)
4319 if (selection.select(lmo))
4322 ensureSelectionVisible();
4329 void VymModel::unselect()
4331 selection.unselect();
4334 void VymModel::reselect()
4336 selection.reselect();
4339 void VymModel::ensureSelectionVisible()
4341 LinkableMapObj *lmo=selection.single();
4342 if (lmo &&mapEditor) mapEditor->ensureVisible (lmo->getBBox() );
4346 void VymModel::selectInt (LinkableMapObj *lmo)
4348 if (selection.select(lmo))
4356 void VymModel::selectNextBranchInt()
4358 // Increase number of branch
4359 LinkableMapObj *sel=selection.single();
4362 QString s=selection.getSelectString();
4368 part=s.section(",",-1);
4370 num=part.right(part.length() - 3);
4372 s=s.left (s.length() -num.length());
4375 num=QString ("%1").arg(num.toUInt()+1);
4379 // Try to select this one
4380 if (select (s)) return;
4382 // We have no direct successor,
4383 // try to increase the parental number in order to
4384 // find a successor with same depth
4386 int d=selection.single()->getDepth();
4391 while (!found && d>0)
4393 s=s.section (",",0,d-1);
4394 // replace substring of current depth in s with "1"
4395 part=s.section(",",-1);
4397 num=part.right(part.length() - 3);
4401 // increase number of parent
4402 num=QString ("%1").arg(num.toUInt()+1);
4403 s=s.section (",",0,d-2) + ","+ typ+num;
4406 // Special case, look at orientation
4407 if (selection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
4408 num=QString ("%1").arg(num.toUInt()+1);
4410 num=QString ("%1").arg(num.toUInt()-1);
4415 // pad to oldDepth, select the first branch for each depth
4416 for (i=d;i<oldDepth;i++)
4421 if ( selection.getBranch()->countBranches()>0)
4429 // try to select the freshly built string
4437 void VymModel::selectPrevBranchInt()
4439 // Decrease number of branch
4440 BranchObj *bo=selection.getBranch();
4443 QString s=selection.getSelectString();
4449 part=s.section(",",-1);
4451 num=part.right(part.length() - 3);
4453 s=s.left (s.length() -num.length());
4455 int n=num.toInt()-1;
4458 num=QString ("%1").arg(n);
4461 // Try to select this one
4462 if (n>=0 && select (s)) return;
4464 // We have no direct precessor,
4465 // try to decrease the parental number in order to
4466 // find a precessor with same depth
4468 int d=selection.single()->getDepth();
4473 while (!found && d>0)
4475 s=s.section (",",0,d-1);
4476 // replace substring of current depth in s with "1"
4477 part=s.section(",",-1);
4479 num=part.right(part.length() - 3);
4483 // decrease number of parent
4484 num=QString ("%1").arg(num.toInt()-1);
4485 s=s.section (",",0,d-2) + ","+ typ+num;
4488 // Special case, look at orientation
4489 if (selection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
4490 num=QString ("%1").arg(num.toInt()-1);
4492 num=QString ("%1").arg(num.toInt()+1);
4497 // pad to oldDepth, select the last branch for each depth
4498 for (i=d;i<oldDepth;i++)
4502 if ( selection.getBranch()->countBranches()>0)
4503 s+=",bo:"+ QString ("%1").arg( selection.getBranch()->countBranches()-1 );
4510 // try to select the freshly built string
4518 void VymModel::selectUpperBranch()
4520 if (selection.isBlocked() ) return;
4522 BranchObj *bo=selection.getBranch();
4523 if (bo && selection.type()==Selection::Branch)
4525 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4526 selectPrevBranchInt();
4528 if (bo->getDepth()==1)
4529 selectNextBranchInt();
4531 selectPrevBranchInt();
4535 void VymModel::selectLowerBranch()
4537 if (selection.isBlocked() ) return;
4539 BranchObj *bo=selection.getBranch();
4540 if (bo && selection.type()==Selection::Branch)
4542 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4543 selectNextBranchInt();
4545 if (bo->getDepth()==1)
4546 selectPrevBranchInt();
4548 selectNextBranchInt();
4553 void VymModel::selectLeftBranch()
4555 if (selection.isBlocked() ) return;
4559 LinkableMapObj *sel=selection.single();
4562 if (selection.type()== Selection::MapCenter)
4564 par=selection.getBranch();
4565 bo=par->getLastSelectedBranch();
4568 // Workaround for reselecting on left and right side
4569 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4570 bo=par->getLastBranch();
4573 bo=par->getLastBranch();
4574 selection.select(bo);
4576 ensureSelectionVisible();
4582 par=(BranchObj*)(sel->getParObj());
4583 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4585 if (selection.type() == Selection::Branch ||
4586 selection.type() == Selection::FloatImage)
4588 selection.select(par);
4590 ensureSelectionVisible();
4595 if (selection.type() == Selection::Branch )
4597 bo=selection.getBranch()->getLastSelectedBranch();
4600 selection.select(bo);
4602 ensureSelectionVisible();
4611 void VymModel::selectRightBranch()
4613 if (selection.isBlocked() ) return;
4617 LinkableMapObj *sel=selection.single();
4620 if (selection.type()==Selection::MapCenter)
4622 par=selection.getBranch();
4623 bo=par->getLastSelectedBranch();
4626 // Workaround for reselecting on left and right side
4627 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
4628 bo=par->getFirstBranch();
4631 selection.select(bo);
4633 ensureSelectionVisible();
4639 par=(BranchObj*)(selection.single()->getParObj());
4640 if (selection.single()->getOrientation()==LinkableMapObj::LeftOfCenter)
4642 if (selection.type() == Selection::Branch ||
4643 selection.type() == Selection::FloatImage)
4645 selection.select(par);
4647 ensureSelectionVisible();
4652 if (selection.type() == Selection::Branch)
4654 bo=selection.getBranch()->getLastSelectedBranch();
4657 selection.select(bo);
4659 ensureSelectionVisible();
4668 void VymModel::selectFirstBranch()
4670 BranchObj *bo1=selection.getBranch();
4675 par=(BranchObj*)(bo1->getParObj());
4677 bo2=par->getFirstBranch();
4679 selection.select(bo2);
4681 ensureSelectionVisible();
4687 void VymModel::selectLastBranch()
4689 BranchObj *bo1=selection.getBranch();
4694 par=(BranchObj*)(bo1->getParObj());
4696 bo2=par->getLastBranch();
4699 selection.select(bo2);
4701 ensureSelectionVisible();
4707 Selection::Type VymModel::selectionType()
4709 return selection.type();
4712 LinkableMapObj* VymModel::getSelection()
4714 return selection.single();
4716 BranchObj* VymModel::getSelectedBranch()
4718 return selection.getBranch();
4721 FloatImageObj* VymModel::getSelectedFloatImage()
4723 return selection.getFloatImage();
4726 QString VymModel::getSelectString ()
4728 return selection.getSelectString();
4731 QString VymModel::getSelectString (LinkableMapObj *lmo)
4735 if (typeid(*lmo)==typeid(BranchObj) ||
4736 typeid(*lmo)==typeid(MapCenterObj) )
4738 LinkableMapObj *par=lmo->getParObj();
4741 if (lmo->getDepth() ==1)
4742 // Mainbranch, return
4743 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4745 // Branch, call myself recursively
4746 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4750 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
4751 if (i>=0) s=QString("mc:%1").arg(i);