1 #include <QApplication>
6 #include "editxlinkdialog.h"
8 #include "exportxhtmldialog.h"
10 #include "geometry.h" // for addBBox
11 #include "mainwindow.h"
12 #include "mapcenterobj.h"
15 #include "selection.h"
18 #include "warningdialog.h"
19 #include "xml-freemind.h"
25 extern Main *mainWindow;
26 extern Settings settings;
27 extern QString tmpVymDir;
29 extern TextEditor *textEditor;
32 extern QString clipboardDir;
33 extern QString clipboardFile;
34 extern bool clipboardEmpty;
36 extern ImageIO imageIO;
38 extern QString vymName;
39 extern QString vymVersion;
40 extern QDir vymBaseDir;
42 extern QDir lastImageDir;
43 extern QDir lastFileDir;
45 extern FlagRowObj *standardFlagsDefault;
47 extern Settings settings;
51 int VymModel::mapNum=0; // make instance
55 // cout << "Const VymModel\n";
62 // cout << "Destr VymModel\n";
63 autosaveTimer->stop();
64 fileChangedTimer->stop();
68 void VymModel::clear()
70 cout << "VymModel::clear rows="<<rowCount(index(rootItem))<<endl;
74 while (!mapCenters.isEmpty()) // FIXME VM needs to be in treemodel only...
75 delete mapCenters.takeFirst();
77 QModelIndex ri=index(rootItem);
78 removeRows (0, rowCount(ri),ri);
81 void VymModel::init ()
83 // We should have at least one map center to start with
84 // addMapCenter(); FIXME VM create this in MapEditor as long as model is part of that
89 // Also no scene yet (should not be needed anyway) FIXME VM
102 stepsTotal=settings.readNumEntry("/history/stepsTotal",100);
103 undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
104 mainWindow->updateHistory (undoSet);
107 makeTmpDirectories();
112 fileName=tr("unnamed");
114 blockReposition=false;
115 blockSaveState=false;
117 autosaveTimer=new QTimer (this);
118 connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
120 fileChangedTimer=new QTimer (this);
121 fileChangedTimer->start(3000);
122 connect(fileChangedTimer, SIGNAL(timeout()), this, SLOT(fileChanged()));
127 selection.setModel (this);
128 selection.unselect();
135 animationUse=settings.readBoolEntry("/animation/use",false);
136 animationTicks=settings.readNumEntry("/animation/ticks",10);
137 animationInterval=settings.readNumEntry("/animation/interval",50);
139 animationTimer=new QTimer (this);
140 connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
143 defLinkColor=QColor (0,0,255);
144 defXLinkColor=QColor (180,180,180);
145 linkcolorhint=LinkableMapObj::DefaultColor;
146 linkstyle=LinkableMapObj::PolyParabel;
148 defXLinkColor=QColor (230,230,230);
158 // addMapCenter(); FIXME VM create this in MapEditor until BO and MCO are independent of scene
162 void VymModel::makeTmpDirectories()
164 // Create unique temporary directories
165 tmpMapDir = tmpVymDir+QString("/model-%1").arg(mapNum);
166 histPath = tmpMapDir+"/history";
172 MapEditor* VymModel::getMapEditor() // FIXME VM better return favourite editor here
177 bool VymModel::isRepositionBlocked()
179 return blockReposition;
182 void VymModel::updateActions() // FIXME maybe don't update if blockReposition is set
184 // Tell mainwindow to update states of actions
185 mainWindow->updateActions();
190 QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
192 // tmpdir temporary directory to which data will be written
193 // prefix mapname, which will be appended to images etc.
194 // writeflags Only write flags for "real" save of map, not undo
195 // offset offset of bbox of whole map in scene.
196 // Needed for XML export
205 case LinkableMapObj::Line:
208 case LinkableMapObj::Parabel:
211 case LinkableMapObj::PolyLine:
215 ls="StylePolyParabel";
219 QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
221 if (linkcolorhint==LinkableMapObj::HeadingColor)
222 colhint=xml.attribut("linkColorHint","HeadingColor");
224 QString mapAttr=xml.attribut("version",vymVersion);
226 mapAttr+= xml.attribut("author",author) +
227 xml.attribut("comment",comment) +
228 xml.attribut("date",getDate()) +
229 xml.attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
230 xml.attribut("selectionColor", mapEditor->getSelectionColor().name() ) +
231 xml.attribut("linkStyle", ls ) +
232 xml.attribut("linkColor", defLinkColor.name() ) +
233 xml.attribut("defXLinkColor", defXLinkColor.name() ) +
234 xml.attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
236 s+=xml.beginElement("vymmap",mapAttr);
239 // Find the used flags while traversing the tree
240 standardFlagsDefault->resetUsedCounter();
242 // Reset the counters before saving
243 // TODO constr. of FIO creates lots of objects, better do this in some other way...
244 FloatImageObj (mapScene).resetSaveCounter();
246 // Build xml recursivly
247 if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
248 // Save complete map, if saveSel not set
249 s+=saveToDir(tmpdir,prefix,writeflags,offset);
252 if ( typeid(*saveSel) == typeid(BranchObj) )
254 s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
255 else if ( typeid(*saveSel) == typeid(FloatImageObj) )
257 s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
260 // Save local settings
261 s+=settings.getDataXML (destPath);
264 if (!selection.isEmpty() && !saveSel )
265 s+=xml.valueElement("select",selection.getSelectString());
268 s+=xml.endElement("vymmap");
271 standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
275 void VymModel::setFilePath(QString fpath, QString destname)
277 if (fpath.isEmpty() || fpath=="")
284 filePath=fpath; // becomes absolute path
285 fileName=fpath; // gets stripped of path
286 destPath=destname; // needed for vymlinks and during load to reset fileChangedTime
288 // If fpath is not an absolute path, complete it
289 filePath=QDir(fpath).absPath();
290 fileDir=filePath.left (1+filePath.findRev ("/"));
292 // Set short name, too. Search from behind:
293 int i=fileName.findRev("/");
294 if (i>=0) fileName=fileName.remove (0,i+1);
296 // Forget the .vym (or .xml) for name of map
297 mapName=fileName.left(fileName.findRev(".",-1,true) );
301 void VymModel::setFilePath(QString fpath)
303 setFilePath (fpath,fpath);
306 QString VymModel::getFilePath()
311 QString VymModel::getFileName()
316 QString VymModel::getMapName()
321 QString VymModel::getDestPath()
326 ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
328 ErrorCode err=success;
330 parseBaseHandler *handler;
334 case VymMap: handler=new parseVYMHandler; break;
335 case FreemindMap : handler=new parseFreemindHandler; break;
337 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
338 "Unknown FileType in VymModel::load()");
344 // FIXME VM not needed??? model->setMapEditor(this);
345 // (map state is set later at end of load...)
348 BranchObj *bo=selection.getBranch();
349 if (!bo) return aborted;
350 if (lmode==ImportAdd)
351 saveStateChangingPart(
354 QString("addMapInsert (%1)").arg(fname),
355 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
357 saveStateChangingPart(
360 QString("addMapReplace(%1)").arg(fname),
361 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
365 // Create temporary directory for packing
367 QString tmpZipDir=makeTmpDir (ok,"vym-pack");
370 QMessageBox::critical( 0, tr( "Critical Load Error" ),
371 tr("Couldn't create temporary directory before load\n"));
376 err=unzipDir (tmpZipDir,fname);
386 // Look for mapname.xml
387 xmlfile= fname.left(fname.findRev(".",-1,true));
388 xmlfile=xmlfile.section( '/', -1 );
389 QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
390 if (!mfile.exists() )
392 // mapname.xml does not exist, well,
393 // maybe someone renamed the mapname.vym file...
394 // Try to find any .xml in the toplevel
395 // directory of the .vym file
396 QStringList flist=QDir (tmpZipDir).entryList("*.xml");
397 if (flist.count()==1)
399 // Only one entry, take this one
400 xmlfile=tmpZipDir + "/"+flist.first();
403 for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it )
404 *it=tmpZipDir + "/" + *it;
405 // TODO Multiple entries, load all (but only the first one into this ME)
406 //mainWindow->fileLoadFromTmp (flist);
407 //returnCode=1; // Silently forget this attempt to load
408 qWarning ("MainWindow::load (fn) multimap found...");
411 if (flist.isEmpty() )
413 QMessageBox::critical( 0, tr( "Critical Load Error" ),
414 tr("Couldn't find a map (*.xml) in .vym archive.\n"));
417 } //file doesn't exist
419 xmlfile=mfile.name();
422 QFile file( xmlfile);
424 // I am paranoid: file should exist anyway
425 // according to check in mainwindow.
428 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
429 tr(QString("Couldn't open map %1").arg(file.name())));
433 bool blockSaveStateOrg=blockSaveState;
434 blockReposition=true;
436 QXmlInputSource source( file);
437 QXmlSimpleReader reader;
438 reader.setContentHandler( handler );
439 reader.setErrorHandler( handler );
440 handler->setModel ( this);
443 // We need to set the tmpDir in order to load files with rel. path
448 tmpdir=fname.left(fname.findRev("/",-1));
449 handler->setTmpDir (tmpdir);
450 handler->setInputFile (file.name());
451 handler->setLoadMode (lmode);
452 bool ok = reader.parse( source );
453 blockReposition=false;
454 blockSaveState=blockSaveStateOrg;
458 reposition(); // FIXME VM reposition the view instead...
465 autosaveTimer->stop();
468 // Reset timestamp to check for later updates of file
469 fileChangedTime=QFileInfo (destPath).lastModified();
472 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
473 tr( handler->errorProtocol() ) );
475 // Still return "success": the map maybe at least
476 // partially read by the parser
481 removeDir (QDir(tmpZipDir));
487 ErrorCode VymModel::save (const SaveMode &savemode)
491 QString safeFilePath;
493 ErrorCode err=success;
497 mapFileName=mapName+".xml";
499 // use name given by user, even if he chooses .doc
500 mapFileName=fileName;
502 // Look, if we should zip the data:
505 QMessageBox mb( vymName,
506 tr("The map %1\ndid not use the compressed "
507 "vym file format.\nWriting it uncompressed will also write images \n"
508 "and flags and thus may overwrite files in the "
509 "given directory\n\nDo you want to write the map").arg(filePath),
510 QMessageBox::Warning,
511 QMessageBox::Yes | QMessageBox::Default,
513 QMessageBox::Cancel | QMessageBox::Escape);
514 mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
515 mb.setButtonText( QMessageBox::No, tr("uncompressed") );
516 mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
519 case QMessageBox::Yes:
520 // save compressed (default file format)
523 case QMessageBox::No:
527 case QMessageBox::Cancel:
534 // First backup existing file, we
535 // don't want to add to old zip archives
539 if ( settings.value ("/mapeditor/writeBackupFile").toBool())
541 QString backupFileName(destPath + "~");
542 QFile backupFile(backupFileName);
543 if (backupFile.exists() && !backupFile.remove())
545 QMessageBox::warning(0, tr("Save Error"),
546 tr("%1\ncould not be removed before saving").arg(backupFileName));
548 else if (!f.rename(backupFileName))
550 QMessageBox::warning(0, tr("Save Error"),
551 tr("%1\ncould not be renamed before saving").arg(destPath));
558 // Create temporary directory for packing
560 tmpZipDir=makeTmpDir (ok,"vym-zip");
563 QMessageBox::critical( 0, tr( "Critical Load Error" ),
564 tr("Couldn't create temporary directory before save\n"));
568 safeFilePath=filePath;
569 setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
572 // Create mapName and fileDir
573 makeSubDirs (fileDir);
576 if (savemode==CompleteMap || selection.isEmpty())
579 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
582 autosaveTimer->stop();
587 if (selection.type()==Selection::FloatImage)
590 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),selection.getBranch());
591 // TODO take care of multiselections
594 if (!saveStringToDisk(fileDir+mapFileName,saveFile))
597 qWarning ("ME::saveStringToDisk failed!");
603 if (err==success) err=zipDir (tmpZipDir,destPath);
606 removeDir (QDir(tmpZipDir));
608 // Restore original filepath outside of tmp zip dir
609 setFilePath (safeFilePath);
613 fileChangedTime=QFileInfo (destPath).lastModified();
617 void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)
619 QString pathDir=path.left(path.findRev("/"));
625 // We need to parse saved XML data
626 parseVYMHandler handler;
627 QXmlInputSource source( file);
628 QXmlSimpleReader reader;
629 reader.setContentHandler( &handler );
630 reader.setErrorHandler( &handler );
631 handler.setModel ( this);
632 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
633 if (undoSel.isEmpty())
637 handler.setLoadMode (NewMap);
641 handler.setLoadMode (ImportReplace);
643 blockReposition=true;
644 bool ok = reader.parse( source );
645 blockReposition=false;
648 // This should never ever happen
649 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
650 handler.errorProtocol());
653 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
656 void VymModel::addMapInsertInt (const QString &path, int pos)
658 BranchObj *sel=selection.getBranch();
661 QString pathDir=path.left(path.findRev("/"));
667 // We need to parse saved XML data
668 parseVYMHandler handler;
669 QXmlInputSource source( file);
670 QXmlSimpleReader reader;
671 reader.setContentHandler( &handler );
672 reader.setErrorHandler( &handler );
673 handler.setModel (this);
674 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
675 handler.setLoadMode (ImportAdd);
676 blockReposition=true;
677 bool ok = reader.parse( source );
678 blockReposition=false;
681 // This should never ever happen
682 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
683 handler.errorProtocol());
685 if (sel->getDepth()>0)
686 sel->getLastBranch()->linkTo (sel,pos);
688 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
692 FloatImageObj* VymModel::loadFloatImageInt (QString fn)
694 BranchObj *bo=selection.getBranch();
699 fio=bo->getLastFloatImage();
702 // FIXME VM needed? scene()->update();
708 void VymModel::loadFloatImage ()
710 BranchObj *bo=selection.getBranch();
714 Q3FileDialog *fd=new Q3FileDialog( NULL);
715 fd->setMode (Q3FileDialog::ExistingFiles);
716 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
717 ImagePreview *p =new ImagePreview (fd);
718 fd->setContentsPreviewEnabled( TRUE );
719 fd->setContentsPreview( p, p );
720 fd->setPreviewMode( Q3FileDialog::Contents );
721 fd->setCaption(vymName+" - " +tr("Load image"));
722 fd->setDir (lastImageDir);
725 if ( fd->exec() == QDialog::Accepted )
727 // TODO loadFIO in QT4 use: lastImageDir=fd->directory();
728 lastImageDir=QDir (fd->dirPath());
731 for (int j=0; j<fd->selectedFiles().count(); j++)
733 s=fd->selectedFiles().at(j);
734 fio=loadFloatImageInt (s);
737 (LinkableMapObj*)fio,
740 QString ("loadImage (%1)").arg(s ),
741 QString("Add image %1 to %2").arg(s).arg(getObjectName(bo))
744 // TODO loadFIO error handling
745 qWarning ("Failed to load "+s);
753 void VymModel::saveFloatImageInt (FloatImageObj *fio, const QString &type, const QString &fn)
758 void VymModel::saveFloatImage ()
760 FloatImageObj *fio=selection.getFloatImage();
763 QFileDialog *fd=new QFileDialog( NULL);
764 fd->setFilters (imageIO.getFilters());
765 fd->setCaption(vymName+" - " +tr("Save image"));
766 fd->setFileMode( QFileDialog::AnyFile );
767 fd->setDirectory (lastImageDir);
768 // fd->setSelection (fio->getOriginalFilename());
772 if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
774 fn=fd->selectedFiles().at(0);
775 if (QFile (fn).exists() )
777 QMessageBox mb( vymName,
778 tr("The file %1 exists already.\n"
779 "Do you want to overwrite it?").arg(fn),
780 QMessageBox::Warning,
781 QMessageBox::Yes | QMessageBox::Default,
782 QMessageBox::Cancel | QMessageBox::Escape,
783 QMessageBox::NoButton );
785 mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
786 mb.setButtonText( QMessageBox::No, tr("Cancel"));
789 case QMessageBox::Yes:
792 case QMessageBox::Cancel:
799 saveFloatImageInt (fio,fd->selectedFilter(),fn );
806 void VymModel::importDirInt(BranchObj *dst, QDir d)
808 BranchObj *bo=selection.getBranch();
811 // Traverse directories
812 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
813 QFileInfoList list = d.entryInfoList();
816 for (int i = 0; i < list.size(); ++i)
819 if (fi.fileName() != "." && fi.fileName() != ".." )
822 bo=dst->getLastBranch();
823 bo->setHeading (fi.fileName() );
824 bo->setColor (QColor("blue"));
826 if ( !d.cd(fi.fileName()) )
827 QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
830 // Recursively add subdirs
837 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
838 list = d.entryInfoList();
840 for (int i = 0; i < list.size(); ++i)
844 bo=dst->getLastBranch();
845 bo->setHeading (fi.fileName() );
846 bo->setColor (QColor("black"));
847 if (fi.fileName().right(4) == ".vym" )
848 bo->setVymLink (fi.filePath());
853 void VymModel::importDirInt (const QString &s)
855 BranchObj *bo=selection.getBranch();
858 saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
865 void VymModel::importDir()
867 BranchObj *bo=selection.getBranch();
871 filters <<"VYM map (*.vym)";
872 QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
873 fd->setMode (QFileDialog::DirectoryOnly);
874 fd->setFilters (filters);
875 fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
879 if ( fd->exec() == QDialog::Accepted )
881 importDirInt (fd->selectedFile() );
883 //FIXME VM needed? scene()->update();
889 void VymModel::autosave()
891 QDateTime now=QDateTime().currentDateTime();
893 // Disable autosave, while we have gone back in history
894 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
895 if (redosAvail>0) return;
897 // Also disable autosave for new map without filename
898 if (filePath.isEmpty()) return;
901 if (mapUnsaved &&mapChanged && settings.value ("/autosave/use",true).toBool() )
903 if (QFileInfo(filePath).lastModified()<=fileChangedTime)
904 mainWindow->fileSave (this);
907 cout <<" ME::autosave rejected, file on disk is newer than last save.\n";
912 void VymModel::fileChanged()
914 // Check if file on disk has changed meanwhile
915 if (!filePath.isEmpty())
917 QDateTime tmod=QFileInfo (filePath).lastModified();
918 if (tmod>fileChangedTime)
920 // FIXME VM switch to current mapeditor and finish lineedits...
921 QMessageBox mb( vymName,
922 tr("The file of the map on disk has changed:\n\n"
923 " %1\n\nDo you want to reload that map with the new file?").arg(filePath),
924 QMessageBox::Question,
926 QMessageBox::Cancel | QMessageBox::Default,
927 QMessageBox::NoButton );
929 mb.setButtonText( QMessageBox::Yes, tr("Reload"));
930 mb.setButtonText( QMessageBox::No, tr("Ignore"));
933 case QMessageBox::Yes:
935 load (filePath,NewMap,fileType);
936 case QMessageBox::Cancel:
937 fileChangedTime=tmod; // allow autosave to overwrite newer file!
943 bool VymModel::isDefault()
948 void VymModel::makeDefault()
954 bool VymModel::hasChanged()
959 void VymModel::setChanged()
962 autosaveTimer->start(settings.value("/autosave/ms/",300000).toInt());
970 QString VymModel::getObjectName (const LinkableMapObj *lmo)
973 if (!lmo) return QString("Error: NULL has no name!");
975 if ((typeid(*lmo) == typeid(BranchObj) ||
976 typeid(*lmo) == typeid(MapCenterObj)))
979 s=(((BranchObj*)lmo)->getHeading());
980 if (s=="") s="unnamed";
981 return QString("branch (%1)").arg(s);
983 if ((typeid(*lmo) == typeid(FloatImageObj) ))
984 return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
985 return QString("Unknown type has no name!");
988 void VymModel::redo()
990 // Can we undo at all?
991 if (redosAvail<1) return;
993 bool blockSaveStateOrg=blockSaveState;
998 if (undosAvail<stepsTotal) undosAvail++;
1000 if (curStep>stepsTotal) curStep=1;
1001 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1002 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1003 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1004 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1005 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1006 QString version=undoSet.readEntry ("/history/version");
1008 /* TODO Maybe check for version, if we save the history
1009 if (!checkVersion(version))
1010 QMessageBox::warning(0,tr("Warning"),
1011 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1014 // Find out current undo directory
1015 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1019 cout << "VymModel::redo() begin\n";
1020 cout << " undosAvail="<<undosAvail<<endl;
1021 cout << " redosAvail="<<redosAvail<<endl;
1022 cout << " curStep="<<curStep<<endl;
1023 cout << " ---------------------------"<<endl;
1024 cout << " comment="<<comment.toStdString()<<endl;
1025 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1026 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1027 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1028 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1029 cout << " ---------------------------"<<endl<<endl;
1032 // select object before redo
1033 if (!redoSelection.isEmpty())
1034 select (redoSelection);
1037 parseAtom (redoCommand);
1040 blockSaveState=blockSaveStateOrg;
1042 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1043 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1044 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1045 undoSet.writeSettings(histPath);
1047 mainWindow->updateHistory (undoSet);
1050 /* TODO remove testing
1051 cout << "ME::redo() end\n";
1052 cout << " undosAvail="<<undosAvail<<endl;
1053 cout << " redosAvail="<<redosAvail<<endl;
1054 cout << " curStep="<<curStep<<endl;
1055 cout << " ---------------------------"<<endl<<endl;
1061 bool VymModel::isRedoAvailable()
1063 if (undoSet.readNumEntry("/history/redosAvail",0)>0)
1069 void VymModel::undo()
1071 // Can we undo at all?
1072 if (undosAvail<1) return;
1074 mainWindow->statusMessage (tr("Autosave disabled during undo."));
1076 bool blockSaveStateOrg=blockSaveState;
1077 blockSaveState=true;
1079 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1080 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1081 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1082 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1083 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1084 QString version=undoSet.readEntry ("/history/version");
1086 /* TODO Maybe check for version, if we save the history
1087 if (!checkVersion(version))
1088 QMessageBox::warning(0,tr("Warning"),
1089 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1092 // Find out current undo directory
1093 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1095 // select object before undo
1096 if (!undoSelection.isEmpty())
1097 select (undoSelection);
1101 cout << "VymModel::undo() begin\n";
1102 cout << " undosAvail="<<undosAvail<<endl;
1103 cout << " redosAvail="<<redosAvail<<endl;
1104 cout << " curStep="<<curStep<<endl;
1105 cout << " ---------------------------"<<endl;
1106 cout << " comment="<<comment.toStdString()<<endl;
1107 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1108 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1109 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1110 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1111 cout << " ---------------------------"<<endl<<endl;
1113 parseAtom (undoCommand);
1118 if (curStep<1) curStep=stepsTotal;
1122 blockSaveState=blockSaveStateOrg;
1123 /* TODO remove testing
1124 cout << "VymModel::undo() end\n";
1125 cout << " undosAvail="<<undosAvail<<endl;
1126 cout << " redosAvail="<<redosAvail<<endl;
1127 cout << " curStep="<<curStep<<endl;
1128 cout << " ---------------------------"<<endl<<endl;
1131 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1132 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1133 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1134 undoSet.writeSettings(histPath);
1136 mainWindow->updateHistory (undoSet);
1139 ensureSelectionVisible();
1142 bool VymModel::isUndoAvailable()
1144 if (undoSet.readNumEntry("/history/undosAvail",0)>0)
1150 void VymModel::gotoHistoryStep (int i)
1152 // Restore variables
1153 int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
1154 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
1156 if (i<0) i=undosAvail+redosAvail;
1158 // Clicking above current step makes us undo things
1161 for (int j=0; j<undosAvail-i; j++) undo();
1164 // Clicking below current step makes us redo things
1166 for (int j=undosAvail; j<i; j++)
1168 if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
1172 // And ignore clicking the current row ;-)
1176 QString VymModel::getHistoryPath()
1178 QString histName(QString("history-%1").arg(curStep));
1179 return (tmpMapDir+"/"+histName);
1182 void VymModel::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
1184 sendData(redoCom); //FIXME testing
1189 if (blockSaveState) return;
1191 if (debug) cout << "ME::saveState() for "<<qPrintable (mapName)<<endl;
1193 // Find out current undo directory
1194 if (undosAvail<stepsTotal) undosAvail++;
1196 if (curStep>stepsTotal) curStep=1;
1198 QString backupXML="";
1199 QString histDir=getHistoryPath();
1200 QString bakMapPath=histDir+"/map.xml";
1202 // Create histDir if not available
1205 makeSubDirs (histDir);
1207 // Save depending on how much needs to be saved
1209 backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
1211 QString undoCommand="";
1212 if (savemode==UndoCommand)
1214 undoCommand=undoCom;
1216 else if (savemode==PartOfMap )
1218 undoCommand=undoCom;
1219 undoCommand.replace ("PATH",bakMapPath);
1222 if (!backupXML.isEmpty())
1223 // Write XML Data to disk
1224 saveStringToDisk (bakMapPath,backupXML);
1226 // We would have to save all actions in a tree, to keep track of
1227 // possible redos after a action. Possible, but we are too lazy: forget about redos.
1230 // Write the current state to disk
1231 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1232 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1233 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1234 undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
1235 undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
1236 undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
1237 undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
1238 undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
1239 undoSet.setEntry (QString("/history/version"),vymVersion);
1240 undoSet.writeSettings(histPath);
1244 // TODO remove after testing
1245 //cout << " into="<< histPath.toStdString()<<endl;
1246 cout << " stepsTotal="<<stepsTotal<<
1247 ", undosAvail="<<undosAvail<<
1248 ", redosAvail="<<redosAvail<<
1249 ", curStep="<<curStep<<endl;
1250 cout << " ---------------------------"<<endl;
1251 cout << " comment="<<comment.toStdString()<<endl;
1252 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1253 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1254 cout << " redoCom="<<redoCom.toStdString()<<endl;
1255 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1256 if (saveSel) cout << " saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
1257 cout << " ---------------------------"<<endl;
1260 mainWindow->updateHistory (undoSet);
1266 void VymModel::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
1268 // save the selected part of the map, Undo will replace part of map
1269 QString undoSelection="";
1271 undoSelection=getSelectString(undoSel);
1273 qWarning ("VymModel::saveStateChangingPart no undoSel given!");
1274 QString redoSelection="";
1276 redoSelection=getSelectString(undoSel);
1278 qWarning ("VymModel::saveStateChangingPart no redoSel given!");
1281 saveState (PartOfMap,
1282 undoSelection, "addMapReplace (\"PATH\")",
1288 void VymModel::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
1292 qWarning ("VymModel::saveStateRemovingPart no redoSel given!");
1295 QString undoSelection=getSelectString (redoSel->getParObj());
1296 QString redoSelection=getSelectString(redoSel);
1297 if (typeid(*redoSel) == typeid(BranchObj) )
1299 // save the selected branch of the map, Undo will insert part of map
1300 saveState (PartOfMap,
1301 undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
1302 redoSelection, "delete ()",
1309 void VymModel::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment)
1311 // "Normal" savestate: save commands, selections and comment
1312 // so just save commands for undo and redo
1313 // and use current selection
1315 QString redoSelection="";
1316 if (redoSel) redoSelection=getSelectString(redoSel);
1317 QString undoSelection="";
1318 if (undoSel) undoSelection=getSelectString(undoSel);
1320 saveState (UndoCommand,
1327 void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment)
1329 // "Normal" savestate: save commands, selections and comment
1330 // so just save commands for undo and redo
1331 // and use current selection
1332 saveState (UndoCommand,
1339 void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment)
1341 // "Normal" savestate applied to model (no selection needed):
1342 // save commands and comment
1343 saveState (UndoCommand,
1351 QGraphicsScene* VymModel::getScene ()
1356 BranchObj* VymModel::first()
1358 if (mapCenters.count()>0)
1359 return mapCenters.first();
1364 BranchObj* VymModel::next(BranchObj *bo_start)
1367 BranchObj *bo=bo_start;
1370 // Try to find next branch in current MapCenter
1372 if (rbo) return rbo;
1374 // Try to find MapCenter of bo
1375 while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
1377 // Try to find next MapCenter
1378 int i=mapCenters.indexOf ((MapCenterObj*)bo);
1379 if (i+2 > mapCenters.count() || i<0) return NULL;
1380 if (mapCenters.at(i+1)!=bo_start)
1381 return mapCenters.at(i+1);
1386 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
1388 LinkableMapObj *lmo;
1390 for (int i=0;i<mapCenters.count(); i++)
1392 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
1393 if (lmo) return lmo;
1398 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
1400 LinkableMapObj *lmo;
1406 part=s.section(",",0,0);
1408 num=part.right(part.length() - 3);
1409 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
1410 return mapCenters.at(num.toInt() );
1413 for (int i=0; i<mapCenters.count(); i++)
1415 lmo=mapCenters.at(i)->findObjBySelect(s);
1416 if (lmo) return lmo;
1421 LinkableMapObj* VymModel::findID (const QString &s)
1423 LinkableMapObj *lmo;
1424 for (int i=0; i<mapCenters.count(); i++)
1426 lmo=mapCenters.at(i)->findID (s);
1427 if (lmo) return lmo;
1432 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
1436 for (int i=0; i<mapCenters.count(); i++)
1437 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
1441 //////////////////////////////////////////////
1443 //////////////////////////////////////////////
1444 void VymModel::setVersion (const QString &s)
1449 void VymModel::setAuthor (const QString &s)
1452 QString ("setMapAuthor (\"%1\")").arg(author),
1453 QString ("setMapAuthor (\"%1\")").arg(s),
1454 QString ("Set author of map to \"%1\"").arg(s)
1460 QString VymModel::getAuthor()
1465 void VymModel::setComment (const QString &s)
1468 QString ("setMapComment (\"%1\")").arg(comment),
1469 QString ("setMapComment (\"%1\")").arg(s),
1470 QString ("Set comment of map")
1476 QString VymModel::getComment ()
1481 QString VymModel::getDate ()
1483 return QDate::currentDate().toString ("yyyy-MM-dd");
1486 void VymModel::setHeading(const QString &s)
1488 BranchObj *sel=selection.getBranch();
1493 "setHeading (\""+sel->getHeading()+"\")",
1495 "setHeading (\""+s+"\")",
1496 QString("Set heading of %1 to \"%2\"").arg(getObjectName(sel)).arg(s) );
1497 sel->setHeading(s );
1498 TreeItem *ti=selection.getBranchItem();
1502 //FIXME VM ix is wrong ModelIndex below, ix2 is (hopefully) correct:
1503 QModelIndex ix=index( ti->row(), ti->column(), index (0,0,QModelIndex()) );
1504 //FIXME VM testing only cout <<"VM::setHeading s="<<s.toStdString()<<" ti="<<ti<<" r,c=("<<ti->row()<<","<<ti->column()<<")"<<endl;
1505 QModelIndex ix2=index (ti);
1506 emit (dataChanged ( ix2,ix2));
1510 cout << "Warning: VM::setHeading ti==NULL\n";
1513 //cout <<" (r,c)=("<<ix2.row()<<","<<ix2.column()<<")"<<endl;
1517 ensureSelectionVisible();
1521 void VymModel::setHeadingInt(const QString &s)
1523 BranchObj *bo=selection.getBranch();
1529 ensureSelectionVisible();
1533 BranchObj* VymModel::findText (QString s, bool cs)
1535 QTextDocument::FindFlags flags=0;
1536 if (cs) flags=QTextDocument::FindCaseSensitively;
1539 { // Nothing found or new find process
1541 // nothing found, start again
1545 bool searching=true;
1546 bool foundNote=false;
1547 while (searching && !EOFind)
1551 // Searching in Note
1552 if (itFind->getNote().contains(s,cs))
1554 if (selection.single()!=itFind)
1556 selection.select(itFind);
1557 ensureSelectionVisible();
1559 if (textEditor->findText(s,flags))
1565 // Searching in Heading
1566 if (searching && itFind->getHeading().contains (s,cs) )
1568 selection.select(itFind);
1569 ensureSelectionVisible();
1575 itFind=next(itFind);
1576 if (!itFind) EOFind=true;
1578 //cout <<"still searching... "<<qPrintable( itFind->getHeading())<<endl;
1581 return selection.getBranch();
1586 void VymModel::findReset()
1587 { // Necessary if text to find changes during a find process
1594 void VymModel::setScene (QGraphicsScene *s)
1596 cout << "VM::setscene scene="<<s<<endl;
1597 mapScene=s; // FIXME VM should not be necessary anymore, move all occurences to MapEditor
1598 //init(); // Here we have a mapScene set,
1599 // which is (still) needed to create MapCenters
1602 void VymModel::setURL(const QString &url)
1604 BranchObj *bo=selection.getBranch();
1607 QString oldurl=bo->getURL();
1611 QString ("setURL (\"%1\")").arg(oldurl),
1613 QString ("setURL (\"%1\")").arg(url),
1614 QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
1619 ensureSelectionVisible();
1623 QString VymModel::getURL()
1625 BranchObj *bo=selection.getBranch();
1627 return bo->getURL();
1632 QStringList VymModel::getURLs()
1635 BranchObj *bo=selection.getBranch();
1641 if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
1648 void VymModel::linkFloatImageTo(const QString &dstString)
1650 FloatImageObj *fio=selection.getFloatImage();
1653 BranchObj *dst=(BranchObj*)findObjBySelect(dstString);
1654 if (dst && (typeid(*dst)==typeid (BranchObj) ||
1655 typeid(*dst)==typeid (MapCenterObj)))
1657 LinkableMapObj *dstPar=dst->getParObj();
1658 QString parString=getSelectString(dstPar);
1659 QString fioPreSelectString=getSelectString(fio);
1660 QString fioPreParentSelectString=getSelectString (fio->getParObj());
1661 ((BranchObj*)dst)->addFloatImage (fio);
1662 selection.unselect();
1663 ((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
1664 fio=((BranchObj*)dst)->getLastFloatImage();
1667 selection.select(fio);
1669 getSelectString(fio),
1670 QString("linkTo (\"%1\")").arg(fioPreParentSelectString),
1672 QString ("linkTo (\"%1\")").arg(dstString),
1673 QString ("Link floatimage to %1").arg(getObjectName(dst)));
1679 void VymModel::setFrameType(const FrameObj::FrameType &t)
1681 BranchObj *bo=selection.getBranch();
1684 QString s=bo->getFrameTypeName();
1685 bo->setFrameType (t);
1686 saveState (bo, QString("setFrameType (\"%1\")").arg(s),
1687 bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
1693 void VymModel::setFrameType(const QString &s)
1695 BranchObj *bo=selection.getBranch();
1698 saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
1699 bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
1700 bo->setFrameType (s);
1706 void VymModel::setFramePenColor(const QColor &c)
1708 BranchObj *bo=selection.getBranch();
1711 saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
1712 bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
1713 bo->setFramePenColor (c);
1717 void VymModel::setFrameBrushColor(const QColor &c)
1719 BranchObj *bo=selection.getBranch();
1722 saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
1723 bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
1724 bo->setFrameBrushColor (c);
1728 void VymModel::setFramePadding (const int &i)
1730 BranchObj *bo=selection.getBranch();
1733 saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
1734 bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
1735 bo->setFramePadding (i);
1741 void VymModel::setFrameBorderWidth(const int &i)
1743 BranchObj *bo=selection.getBranch();
1746 saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
1747 bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
1748 bo->setFrameBorderWidth (i);
1754 void VymModel::setIncludeImagesVer(bool b)
1756 BranchObj *bo=selection.getBranch();
1759 QString u= b ? "false" : "true";
1760 QString r=!b ? "false" : "true";
1764 QString("setIncludeImagesVertically (%1)").arg(u),
1766 QString("setIncludeImagesVertically (%1)").arg(r),
1767 QString("Include images vertically in %1").arg(getObjectName(bo))
1769 bo->setIncludeImagesVer(b);
1774 void VymModel::setIncludeImagesHor(bool b)
1776 BranchObj *bo=selection.getBranch();
1779 QString u= b ? "false" : "true";
1780 QString r=!b ? "false" : "true";
1784 QString("setIncludeImagesHorizontally (%1)").arg(u),
1786 QString("setIncludeImagesHorizontally (%1)").arg(r),
1787 QString("Include images horizontally in %1").arg(getObjectName(bo))
1789 bo->setIncludeImagesHor(b);
1794 void VymModel::setHideLinkUnselected (bool b)
1796 LinkableMapObj *sel=selection.single();
1798 (selection.type() == Selection::Branch ||
1799 selection.type() == Selection::MapCenter ||
1800 selection.type() == Selection::FloatImage ))
1802 QString u= b ? "false" : "true";
1803 QString r=!b ? "false" : "true";
1807 QString("setHideLinkUnselected (%1)").arg(u),
1809 QString("setHideLinkUnselected (%1)").arg(r),
1810 QString("Hide link of %1 if unselected").arg(getObjectName(sel))
1812 sel->setHideLinkUnselected(b);
1816 void VymModel::setHideExport(bool b)
1818 BranchObj *bo=selection.getBranch();
1821 bo->setHideInExport (b);
1822 QString u= b ? "false" : "true";
1823 QString r=!b ? "false" : "true";
1827 QString ("setHideExport (%1)").arg(u),
1829 QString ("setHideExport (%1)").arg(r),
1830 QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bo)).arg (r)
1835 // FIXME VM needed? scene()->update();
1839 void VymModel::toggleHideExport()
1841 BranchObj *bo=selection.getBranch();
1843 setHideExport ( !bo->hideInExport() );
1847 void VymModel::copy()
1849 LinkableMapObj *sel=selection.single();
1852 if (redosAvail == 0)
1855 QString s=getSelectString(sel);
1856 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel );
1857 curClipboard=curStep;
1860 // Copy also to global clipboard, because we are at last step in history
1861 QString bakMapName(QString("history-%1").arg(curStep));
1862 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1863 copyDir (bakMapDir,clipboardDir );
1865 clipboardEmpty=false;
1871 void VymModel::pasteNoSave(const int &n)
1873 bool old=blockSaveState;
1874 blockSaveState=true;
1875 bool zippedOrg=zipped;
1876 if (redosAvail > 0 || n!=0)
1878 // Use the "historical" buffer
1879 QString bakMapName(QString("history-%1").arg(n));
1880 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1881 load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
1883 // Use the global buffer
1884 load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
1889 void VymModel::paste()
1891 BranchObj *sel=selection.getBranch();
1894 saveStateChangingPart(
1897 QString ("paste (%1)").arg(curClipboard),
1898 QString("Paste to %1").arg( getObjectName(sel))
1905 void VymModel::cut()
1907 LinkableMapObj *sel=selection.single();
1908 if ( sel && (selection.type() == Selection::Branch ||
1909 selection.type()==Selection::MapCenter ||
1910 selection.type()==Selection::FloatImage))
1912 /* No savestate! savestate is called in cutNoSave
1913 saveStateChangingPart(
1917 QString("Cut %1").arg(getObjectName(sel ))
1926 void VymModel::moveBranchUp()
1928 BranchObj* bo=selection.getBranch();
1932 if (!bo->canMoveBranchUp()) return;
1933 par=(BranchObj*)(bo->getParObj());
1934 BranchObj *obo=par->moveBranchUp (bo); // bo will be the one below selection
1935 saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
1937 //FIXME VM needed? scene()->update();
1939 ensureSelectionVisible();
1943 void VymModel::moveBranchDown()
1945 BranchObj* bo=selection.getBranch();
1949 if (!bo->canMoveBranchDown()) return;
1950 par=(BranchObj*)(bo->getParObj());
1951 BranchObj *obo=par->moveBranchDown(bo); // bo will be the one above selection
1952 saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
1954 //FIXME VM needed? scene()->update();
1956 ensureSelectionVisible();
1960 void VymModel::sortChildren()
1962 BranchObj* bo=selection.getBranch();
1965 if(bo->countBranches()>1)
1967 saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
1970 ensureSelectionVisible();
1975 void VymModel::createMapCenter()
1977 MapCenterObj *mco=addMapCenter (QPointF (0,0) );
1978 selection.select (mco);
1981 void VymModel::createBranch()
1983 BranchObj *bo=selection.getBranch();
1986 BranchObj *newbo=addNewBranchInt (-2); // FIXME VM Old model, merge with below
1989 QList<QVariant> cData;
1990 cData << "VM:createBranch" << "undef"<<"undef";
1991 TreeItem *parti=bo->getTreeItem();
1992 TreeItem *ti=new TreeItem (cData,parti);
1994 ti->setType (TreeItem::Branch);
1995 parti->appendChild (ti);
1999 newbo->setTreeItem (ti);
2000 selection.select (newbo); // FIXME VM really needed here?
2005 MapCenterObj* VymModel::addMapCenter ()
2007 MapCenterObj *mco=addMapCenter (contextPos);
2008 cout <<"VM::addMCO () mapScene="<<mapScene<<endl;
2009 selection.select (mco);
2011 ensureSelectionVisible();
2016 QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
2017 QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
2022 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
2024 MapCenterObj *mapCenter = new MapCenterObj(mapScene,this);
2025 mapCenter->setMapEditor(mapEditor); //FIXME VM needed to get defLinkStyle, mapLinkColorHint ... for later added objects
2026 mapCenter->move (absPos);
2027 mapCenter->setVisibility (true);
2028 mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
2029 mapCenters.append(mapCenter);
2032 QList<QVariant> cData;
2033 cData << "VM:addMapCenter" << "undef"<<"undef";
2034 TreeItem *ti=new TreeItem (cData,rootItem);
2035 ti->setLMO (mapCenter);
2036 ti->setType (TreeItem::MapCenter);
2037 mapCenter->setTreeItem (ti);
2038 rootItem->appendChild (ti);
2043 MapCenterObj* VymModel::removeMapCenter(MapCenterObj* mco)
2045 int i=mapCenters.indexOf (mco);
2048 mapCenters.removeAt (i);
2050 if (i>0) return mapCenters.at(i-1); // Return previous MCO
2055 MapCenterObj* VymModel::getLastMapCenter()
2057 if (mapCenters.size()>0)
2058 return mapCenters.last();
2065 BranchObj* VymModel::addNewBranchInt(int num)
2067 // Depending on pos:
2068 // -3 insert in children of parent above selection
2069 // -2 add branch to selection
2070 // -1 insert in children of parent below selection
2071 // 0..n insert in children of parent at pos
2072 BranchObj *newbo=NULL;
2073 BranchObj *bo=selection.getBranch();
2078 // save scroll state. If scrolled, automatically select
2079 // new branch in order to tmp unscroll parent...
2080 newbo=bo->addBranch();
2085 bo=(BranchObj*)bo->getParObj();
2086 if (bo) newbo=bo->insertBranch(num);
2090 bo=(BranchObj*)bo->getParObj();
2091 if (bo) newbo=bo->insertBranch(num);
2093 if (!newbo) return NULL;
2098 BranchObj* VymModel::addNewBranch(int pos)
2100 // Different meaning than num in addNewBranchInt!
2104 BranchObj *bo = selection.getBranch();
2105 BranchObj *newbo=NULL;
2109 // FIXME VM do we still need this in model? setCursor (Qt::ArrowCursor);
2111 newbo=addNewBranchInt (pos-2);
2119 QString ("addBranch (%1)").arg(pos),
2120 QString ("Add new branch to %1").arg(getObjectName(bo)));
2124 latestSelectionString=getSelectString(newbo);
2125 // In Network mode, the client needs to know where the new branch is,
2126 // so we have to pass on this information via saveState.
2127 // TODO: Get rid of this positioning workaround
2128 QString ps=qpointfToString (newbo->getAbsPos());
2129 sendData ("selectLatestAdded ()");
2130 sendData (QString("move %1").arg(ps));
2138 BranchObj* VymModel::addNewBranchBefore()
2140 BranchObj *newbo=NULL;
2141 BranchObj *bo = selection.getBranch();
2142 if (bo && selection.type()==Selection::Branch)
2143 // We accept no MapCenterObj here, so we _have_ a parent
2145 QPointF p=bo->getRelPos();
2148 BranchObj *parbo=(BranchObj*)(bo->getParObj());
2150 // add below selection
2151 newbo=parbo->insertBranch(bo->getNum()+1);
2154 newbo->move2RelPos (p);
2156 // Move selection to new branch
2157 bo->linkTo (newbo,-1);
2159 saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()",
2160 QString ("Add branch before %1").arg(getObjectName(bo)));
2166 latestSelectionString=selection.getSelectString();
2170 void VymModel::deleteSelection()
2172 BranchObj *bo = selection.getBranch();
2174 if (bo && selection.type()==Selection::MapCenter)
2176 // BranchObj* par=(BranchObj*)(bo->getParObj());
2177 selection.unselect();
2178 /* FIXME VM Note: does saveStateRemovingPart work for MCO? (No parent!)
2179 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2181 bo=removeMapCenter ((MapCenterObj*)bo);
2184 selection.select (bo);
2185 ensureSelectionVisible();
2191 if (bo && selection.type()==Selection::Branch)
2193 QModelIndex ix=selection.getBranchIndex();
2196 cout << "VM::deleteSelection\n";
2199 BranchObj* par=(BranchObj*)bo->getParObj();
2200 selection.unselect();
2201 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2202 par->removeBranch(bo);
2203 selection.select (par);
2204 ensureSelectionVisible();
2209 FloatImageObj *fio=selection.getFloatImage();
2212 BranchObj* par=(BranchObj*)fio->getParObj();
2213 saveStateChangingPart(
2217 QString("Delete %1").arg(getObjectName(fio))
2219 selection.unselect();
2220 par->removeFloatImage(fio);
2221 selection.select (par);
2224 ensureSelectionVisible();
2229 void VymModel::deleteKeepChildren()
2231 BranchObj *bo=selection.getBranch();
2235 par=(BranchObj*)(bo->getParObj());
2237 // Don't use this on mapcenter
2240 // Check if we have childs at all to keep
2241 if (bo->countBranches()==0)
2247 QPointF p=bo->getRelPos();
2248 saveStateChangingPart(
2251 "deleteKeepChildren ()",
2252 QString("Remove %1 and keep its children").arg(getObjectName(bo))
2255 QString sel=getSelectString(bo);
2257 par->removeBranchHere(bo);
2260 selection.getBranch()->move2RelPos (p);
2265 void VymModel::deleteChildren()
2267 BranchObj *bo=selection.getBranch();
2270 saveStateChangingPart(
2273 "deleteChildren ()",
2274 QString( "Remove children of branch %1").arg(getObjectName(bo))
2276 bo->removeChildren();
2282 bool VymModel::scrollBranch(BranchObj *bo)
2286 if (bo->isScrolled()) return false;
2287 if (bo->countBranches()==0) return false;
2288 if (bo->getDepth()==0) return false;
2294 QString ("%1 ()").arg(u),
2296 QString ("%1 ()").arg(r),
2297 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2301 // FIXME VM needed? scene()->update();
2307 bool VymModel::unscrollBranch(BranchObj *bo)
2311 if (!bo->isScrolled()) return false;
2312 if (bo->countBranches()==0) return false;
2313 if (bo->getDepth()==0) return false;
2319 QString ("%1 ()").arg(u),
2321 QString ("%1 ()").arg(r),
2322 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2326 // FIXME VM needed? scene()->update();
2332 void VymModel::toggleScroll()
2334 BranchObj *bo=selection.getBranch();
2335 if (selection.type()==Selection::Branch )
2337 if (bo->isScrolled())
2338 unscrollBranch (bo);
2344 void VymModel::unscrollChildren()
2346 BranchObj *bo=selection.getBranch();
2352 if (bo->isScrolled()) unscrollBranch (bo);
2357 void VymModel::addFloatImage (const QPixmap &img)
2359 BranchObj *bo=selection.getBranch();
2362 FloatImageObj *fio=bo->addFloatImage();
2364 fio->setOriginalFilename("No original filename (image added by dropevent)");
2365 QString s=getSelectString(bo);
2366 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio );
2367 saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
2369 // FIXME VM needed? scene()->update();
2374 void VymModel::colorBranch (QColor c)
2376 BranchObj *bo=selection.getBranch();
2381 QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
2383 QString ("colorBranch (\"%1\")").arg(c.name()),
2384 QString("Set color of %1 to %2").arg(getObjectName(bo)).arg(c.name())
2386 bo->setColor(c); // color branch
2390 void VymModel::colorSubtree (QColor c)
2392 BranchObj *bo=selection.getBranch();
2395 saveStateChangingPart(
2398 QString ("colorSubtree (\"%1\")").arg(c.name()),
2399 QString ("Set color of %1 and children to %2").arg(getObjectName(bo)).arg(c.name())
2401 bo->setColorSubtree (c); // color links, color children
2405 QColor VymModel::getCurrentHeadingColor()
2407 BranchObj *bo=selection.getBranch();
2408 if (bo) return bo->getColor();
2410 QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
2416 void VymModel::editURL()
2418 BranchObj *bo=selection.getBranch();
2422 QString text = QInputDialog::getText(
2423 "VYM", tr("Enter URL:"), QLineEdit::Normal,
2424 bo->getURL(), &ok, NULL);
2426 // user entered something and pressed OK
2431 void VymModel::editLocalURL()
2433 BranchObj *bo=selection.getBranch();
2436 QStringList filters;
2437 filters <<"All files (*)";
2438 filters << tr("Text","Filedialog") + " (*.txt)";
2439 filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
2440 filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
2441 filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
2442 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
2443 fd->setFilters (filters);
2444 fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
2445 fd->setDirectory (lastFileDir);
2446 if (! bo->getVymLink().isEmpty() )
2447 fd->selectFile( bo->getURL() );
2450 if ( fd->exec() == QDialog::Accepted )
2452 lastFileDir=QDir (fd->directory().path());
2453 setURL (fd->selectedFile() );
2459 void VymModel::editHeading2URL()
2461 BranchObj *bo=selection.getBranch();
2463 setURL (bo->getHeading());
2466 void VymModel::editBugzilla2URL()
2468 BranchObj *bo=selection.getBranch();
2471 QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
2476 void VymModel::editFATE2URL()
2478 BranchObj *bo=selection.getBranch();
2481 QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
2484 "setURL (\""+bo->getURL()+"\")",
2486 "setURL (\""+url+"\")",
2487 QString("Use heading of %1 as link to FATE").arg(getObjectName(bo))
2494 void VymModel::editVymLink()
2496 BranchObj *bo=selection.getBranch();
2499 QStringList filters;
2500 filters <<"VYM map (*.vym)";
2501 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
2502 fd->setFilters (filters);
2503 fd->setCaption(vymName+" - " +tr("Link to another map"));
2504 fd->setDirectory (lastFileDir);
2505 if (! bo->getVymLink().isEmpty() )
2506 fd->selectFile( bo->getVymLink() );
2510 if ( fd->exec() == QDialog::Accepted )
2512 lastFileDir=QDir (fd->directory().path());
2515 "setVymLink (\""+bo->getVymLink()+"\")",
2517 "setVymLink (\""+fd->selectedFile()+"\")",
2518 QString("Set vymlink of %1 to %2").arg(getObjectName(bo)).arg(fd->selectedFile())
2520 setVymLink (fd->selectedFile() ); // FIXME ok?
2525 void VymModel::setVymLink (const QString &s)
2527 // Internal function, no saveState needed
2528 BranchObj *bo=selection.getBranch();
2535 ensureSelectionVisible();
2539 void VymModel::deleteVymLink()
2541 BranchObj *bo=selection.getBranch();
2546 "setVymLink (\""+bo->getVymLink()+"\")",
2548 "setVymLink (\"\")",
2549 QString("Unset vymlink of %1").arg(getObjectName(bo))
2551 bo->setVymLink ("" );
2554 // FIXME VM needed? scene()->update();
2558 QString VymModel::getVymLink()
2560 BranchObj *bo=selection.getBranch();
2562 return bo->getVymLink();
2568 QStringList VymModel::getVymLinks()
2571 BranchObj *bo=selection.getBranch();
2577 if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
2585 void VymModel::followXLink(int i)
2587 BranchObj *bo=selection.getBranch();
2590 bo=bo->XLinkTargetAt(i);
2593 selection.select(bo);
2594 ensureSelectionVisible();
2599 void VymModel::editXLink(int i) // FIXME VM missing saveState
2601 BranchObj *bo=selection.getBranch();
2604 XLinkObj *xlo=bo->XLinkAt(i);
2607 EditXLinkDialog dia;
2609 dia.setSelection(bo);
2610 if (dia.exec() == QDialog::Accepted)
2612 if (dia.useSettingsGlobal() )
2614 setMapDefXLinkColor (xlo->getColor() );
2615 setMapDefXLinkWidth (xlo->getWidth() );
2617 if (dia.deleteXLink())
2618 bo->deleteXLinkAt(i);
2628 //////////////////////////////////////////////
2630 //////////////////////////////////////////////
2632 void VymModel::parseAtom(const QString &atom)
2634 BranchObj *selb=selection.getBranch();
2640 // Split string s into command and parameters
2641 parser.parseAtom (atom);
2642 QString com=parser.getCommand();
2644 // External commands
2645 /////////////////////////////////////////////////////////////////////
2646 if (com=="addBranch")
2648 if (selection.isEmpty())
2650 parser.setError (Aborted,"Nothing selected");
2653 parser.setError (Aborted,"Type of selection is not a branch");
2658 if (parser.checkParCount(pl))
2660 if (parser.parCount()==0)
2664 n=parser.parInt (ok,0);
2665 if (ok ) addNewBranch (n);
2669 /////////////////////////////////////////////////////////////////////
2670 } else if (com=="addBranchBefore")
2672 if (selection.isEmpty())
2674 parser.setError (Aborted,"Nothing selected");
2677 parser.setError (Aborted,"Type of selection is not a branch");
2680 if (parser.parCount()==0)
2682 addNewBranchBefore ();
2685 /////////////////////////////////////////////////////////////////////
2686 } else if (com==QString("addMapCenter"))
2688 if (parser.checkParCount(2))
2690 x=parser.parDouble (ok,0);
2693 y=parser.parDouble (ok,1);
2694 if (ok) addMapCenter (QPointF(x,y));
2697 /////////////////////////////////////////////////////////////////////
2698 } else if (com==QString("addMapReplace"))
2700 if (selection.isEmpty())
2702 parser.setError (Aborted,"Nothing selected");
2705 parser.setError (Aborted,"Type of selection is not a branch");
2706 } else if (parser.checkParCount(1))
2708 //s=parser.parString (ok,0); // selection
2709 t=parser.parString (ok,0); // path to map
2710 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2711 addMapReplaceInt(getSelectString(selb),t);
2713 /////////////////////////////////////////////////////////////////////
2714 } else if (com==QString("addMapInsert"))
2716 if (selection.isEmpty())
2718 parser.setError (Aborted,"Nothing selected");
2721 parser.setError (Aborted,"Type of selection is not a branch");
2724 if (parser.checkParCount(2))
2726 t=parser.parString (ok,0); // path to map
2727 n=parser.parInt(ok,1); // position
2728 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2729 addMapInsertInt(t,n);
2732 /////////////////////////////////////////////////////////////////////
2733 } else if (com=="clearFlags")
2735 if (selection.isEmpty() )
2737 parser.setError (Aborted,"Nothing selected");
2740 parser.setError (Aborted,"Type of selection is not a branch");
2741 } else if (parser.checkParCount(0))
2743 selb->clearStandardFlags();
2744 selb->updateFlagsToolbar();
2746 /////////////////////////////////////////////////////////////////////
2747 } else if (com=="colorBranch")
2749 if (selection.isEmpty())
2751 parser.setError (Aborted,"Nothing selected");
2754 parser.setError (Aborted,"Type of selection is not a branch");
2755 } else if (parser.checkParCount(1))
2757 QColor c=parser.parColor (ok,0);
2758 if (ok) colorBranch (c);
2760 /////////////////////////////////////////////////////////////////////
2761 } else if (com=="colorSubtree")
2763 if (selection.isEmpty())
2765 parser.setError (Aborted,"Nothing selected");
2768 parser.setError (Aborted,"Type of selection is not a branch");
2769 } else if (parser.checkParCount(1))
2771 QColor c=parser.parColor (ok,0);
2772 if (ok) colorSubtree (c);
2774 /////////////////////////////////////////////////////////////////////
2775 } else if (com=="copy")
2777 if (selection.isEmpty())
2779 parser.setError (Aborted,"Nothing selected");
2782 parser.setError (Aborted,"Type of selection is not a branch");
2783 } else if (parser.checkParCount(0))
2785 //FIXME missing action for copy
2787 /////////////////////////////////////////////////////////////////////
2788 } else if (com=="cut")
2790 if (selection.isEmpty())
2792 parser.setError (Aborted,"Nothing selected");
2793 } else if ( selection.type()!=Selection::Branch &&
2794 selection.type()!=Selection::MapCenter &&
2795 selection.type()!=Selection::FloatImage )
2797 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2798 } else if (parser.checkParCount(0))
2802 /////////////////////////////////////////////////////////////////////
2803 } else if (com=="delete")
2805 if (selection.isEmpty())
2807 parser.setError (Aborted,"Nothing selected");
2809 /*else if (selection.type() != Selection::Branch && selection.type() != Selection::FloatImage )
2811 parser.setError (Aborted,"Type of selection is wrong.");
2814 else if (parser.checkParCount(0))
2818 /////////////////////////////////////////////////////////////////////
2819 } else if (com=="deleteKeepChildren")
2821 if (selection.isEmpty())
2823 parser.setError (Aborted,"Nothing selected");
2826 parser.setError (Aborted,"Type of selection is not a branch");
2827 } else if (parser.checkParCount(0))
2829 deleteKeepChildren();
2831 /////////////////////////////////////////////////////////////////////
2832 } else if (com=="deleteChildren")
2834 if (selection.isEmpty())
2836 parser.setError (Aborted,"Nothing selected");
2839 parser.setError (Aborted,"Type of selection is not a branch");
2840 } else if (parser.checkParCount(0))
2844 /////////////////////////////////////////////////////////////////////
2845 } else if (com=="exportASCII")
2849 if (parser.parCount()>=1)
2850 // Hey, we even have a filename
2851 fname=parser.parString(ok,0);
2854 parser.setError (Aborted,"Could not read filename");
2857 exportASCII (fname,false);
2859 /////////////////////////////////////////////////////////////////////
2860 } else if (com=="exportImage")
2864 if (parser.parCount()>=2)
2865 // Hey, we even have a filename
2866 fname=parser.parString(ok,0);
2869 parser.setError (Aborted,"Could not read filename");
2872 QString format="PNG";
2873 if (parser.parCount()>=2)
2875 format=parser.parString(ok,1);
2877 exportImage (fname,false,format);
2879 /////////////////////////////////////////////////////////////////////
2880 } else if (com=="exportXHTML")
2884 if (parser.parCount()>=2)
2885 // Hey, we even have a filename
2886 fname=parser.parString(ok,1);
2889 parser.setError (Aborted,"Could not read filename");
2892 exportXHTML (fname,false);
2894 /////////////////////////////////////////////////////////////////////
2895 } else if (com=="exportXML")
2899 if (parser.parCount()>=2)
2900 // Hey, we even have a filename
2901 fname=parser.parString(ok,1);
2904 parser.setError (Aborted,"Could not read filename");
2907 exportXML (fname,false);
2909 /////////////////////////////////////////////////////////////////////
2910 } else if (com=="importDir")
2912 if (selection.isEmpty())
2914 parser.setError (Aborted,"Nothing selected");
2917 parser.setError (Aborted,"Type of selection is not a branch");
2918 } else if (parser.checkParCount(1))
2920 s=parser.parString(ok,0);
2921 if (ok) importDirInt(s);
2923 /////////////////////////////////////////////////////////////////////
2924 } else if (com=="linkTo")
2926 if (selection.isEmpty())
2928 parser.setError (Aborted,"Nothing selected");
2931 if (parser.checkParCount(4))
2933 // 0 selectstring of parent
2934 // 1 num in parent (for branches)
2935 // 2,3 x,y of mainbranch or mapcenter
2936 s=parser.parString(ok,0);
2937 LinkableMapObj *dst=findObjBySelect (s);
2940 if (typeid(*dst) == typeid(BranchObj) )
2942 // Get number in parent
2943 n=parser.parInt (ok,1);
2946 selb->linkTo ((BranchObj*)(dst),n);
2949 } else if (typeid(*dst) == typeid(MapCenterObj) )
2951 selb->linkTo ((BranchObj*)(dst),-1);
2952 // Get coordinates of mainbranch
2953 x=parser.parDouble(ok,2);
2956 y=parser.parDouble(ok,3);
2966 } else if ( selection.type() == Selection::FloatImage)
2968 if (parser.checkParCount(1))
2970 // 0 selectstring of parent
2971 s=parser.parString(ok,0);
2972 LinkableMapObj *dst=findObjBySelect (s);
2975 if (typeid(*dst) == typeid(BranchObj) ||
2976 typeid(*dst) == typeid(MapCenterObj))
2977 linkFloatImageTo (getSelectString(dst));
2979 parser.setError (Aborted,"Destination is not a branch");
2982 parser.setError (Aborted,"Type of selection is not a floatimage or branch");
2983 /////////////////////////////////////////////////////////////////////
2984 } else if (com=="loadImage")
2986 if (selection.isEmpty())
2988 parser.setError (Aborted,"Nothing selected");
2991 parser.setError (Aborted,"Type of selection is not a branch");
2992 } else if (parser.checkParCount(1))
2994 s=parser.parString(ok,0);
2995 if (ok) loadFloatImageInt (s);
2997 /////////////////////////////////////////////////////////////////////
2998 } else if (com=="moveBranchUp")
3000 if (selection.isEmpty() )
3002 parser.setError (Aborted,"Nothing selected");
3005 parser.setError (Aborted,"Type of selection is not a branch");
3006 } else if (parser.checkParCount(0))
3010 /////////////////////////////////////////////////////////////////////
3011 } else if (com=="moveBranchDown")
3013 if (selection.isEmpty() )
3015 parser.setError (Aborted,"Nothing selected");
3018 parser.setError (Aborted,"Type of selection is not a branch");
3019 } else if (parser.checkParCount(0))
3023 /////////////////////////////////////////////////////////////////////
3024 } else if (com=="move")
3026 if (selection.isEmpty() )
3028 parser.setError (Aborted,"Nothing selected");
3029 } else if ( selection.type()!=Selection::Branch &&
3030 selection.type()!=Selection::MapCenter &&
3031 selection.type()!=Selection::FloatImage )
3033 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3034 } else if (parser.checkParCount(2))
3036 x=parser.parDouble (ok,0);
3039 y=parser.parDouble (ok,1);
3043 /////////////////////////////////////////////////////////////////////
3044 } else if (com=="moveRel")
3046 if (selection.isEmpty() )
3048 parser.setError (Aborted,"Nothing selected");
3049 } else if ( selection.type()!=Selection::Branch &&
3050 selection.type()!=Selection::MapCenter &&
3051 selection.type()!=Selection::FloatImage )
3053 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3054 } else if (parser.checkParCount(2))
3056 x=parser.parDouble (ok,0);
3059 y=parser.parDouble (ok,1);
3060 if (ok) moveRel (x,y);
3063 /////////////////////////////////////////////////////////////////////
3064 } else if (com=="nop")
3066 /////////////////////////////////////////////////////////////////////
3067 } else if (com=="paste")
3069 if (selection.isEmpty() )
3071 parser.setError (Aborted,"Nothing selected");
3074 parser.setError (Aborted,"Type of selection is not a branch");
3075 } else if (parser.checkParCount(1))
3077 n=parser.parInt (ok,0);
3078 if (ok) pasteNoSave(n);
3080 /////////////////////////////////////////////////////////////////////
3081 } else if (com=="qa")
3083 if (selection.isEmpty() )
3085 parser.setError (Aborted,"Nothing selected");
3088 parser.setError (Aborted,"Type of selection is not a branch");
3089 } else if (parser.checkParCount(4))
3092 c=parser.parString (ok,0);
3095 parser.setError (Aborted,"No comment given");
3098 s=parser.parString (ok,1);
3101 parser.setError (Aborted,"First parameter is not a string");
3104 t=parser.parString (ok,2);
3107 parser.setError (Aborted,"Condition is not a string");
3110 u=parser.parString (ok,3);
3113 parser.setError (Aborted,"Third parameter is not a string");
3118 parser.setError (Aborted,"Unknown type: "+s);
3123 parser.setError (Aborted,"Unknown operator: "+t);
3128 parser.setError (Aborted,"Type of selection is not a branch");
3131 if (selb->getHeading() == u)
3133 cout << "PASSED: " << qPrintable (c) << endl;
3136 cout << "FAILED: " << qPrintable (c) << endl;
3146 /////////////////////////////////////////////////////////////////////
3147 } else if (com=="saveImage")
3149 FloatImageObj *fio=selection.getFloatImage();
3152 parser.setError (Aborted,"Type of selection is not an image");
3153 } else if (parser.checkParCount(2))
3155 s=parser.parString(ok,0);
3158 t=parser.parString(ok,1);
3159 if (ok) saveFloatImageInt (fio,t,s);
3162 /////////////////////////////////////////////////////////////////////
3163 } else if (com=="scroll")
3165 if (selection.isEmpty() )
3167 parser.setError (Aborted,"Nothing selected");
3170 parser.setError (Aborted,"Type of selection is not a branch");
3171 } else if (parser.checkParCount(0))
3173 if (!scrollBranch (selb))
3174 parser.setError (Aborted,"Could not scroll branch");
3176 /////////////////////////////////////////////////////////////////////
3177 } else if (com=="select")
3179 if (parser.checkParCount(1))
3181 s=parser.parString(ok,0);
3184 /////////////////////////////////////////////////////////////////////
3185 } else if (com=="selectLastBranch")
3187 if (selection.isEmpty() )
3189 parser.setError (Aborted,"Nothing selected");
3192 parser.setError (Aborted,"Type of selection is not a branch");
3193 } else if (parser.checkParCount(0))
3195 BranchObj *bo=selb->getLastBranch();
3197 parser.setError (Aborted,"Could not select last branch");
3201 /////////////////////////////////////////////////////////////////////
3202 } else if (com=="selectLastImage")
3204 if (selection.isEmpty() )
3206 parser.setError (Aborted,"Nothing selected");
3209 parser.setError (Aborted,"Type of selection is not a branch");
3210 } else if (parser.checkParCount(0))
3212 FloatImageObj *fio=selb->getLastFloatImage();
3214 parser.setError (Aborted,"Could not select last image");
3218 /////////////////////////////////////////////////////////////////////
3219 } else if (com=="selectLatestAdded")
3221 if (latestSelectionString.isEmpty() )
3223 parser.setError (Aborted,"No latest added object");
3226 if (!select (latestSelectionString))
3227 parser.setError (Aborted,"Could not select latest added object "+latestSelectionString);
3229 /////////////////////////////////////////////////////////////////////
3230 } else if (com=="setFrameType")
3232 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3234 parser.setError (Aborted,"Type of selection does not allow setting frame type");
3236 else if (parser.checkParCount(1))
3238 s=parser.parString(ok,0);
3239 if (ok) setFrameType (s);
3241 /////////////////////////////////////////////////////////////////////
3242 } else if (com=="setFramePenColor")
3244 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3246 parser.setError (Aborted,"Type of selection does not allow setting of pen color");
3248 else if (parser.checkParCount(1))
3250 QColor c=parser.parColor(ok,0);
3251 if (ok) setFramePenColor (c);
3253 /////////////////////////////////////////////////////////////////////
3254 } else if (com=="setFrameBrushColor")
3256 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3258 parser.setError (Aborted,"Type of selection does not allow setting brush color");
3260 else if (parser.checkParCount(1))
3262 QColor c=parser.parColor(ok,0);
3263 if (ok) setFrameBrushColor (c);
3265 /////////////////////////////////////////////////////////////////////
3266 } else if (com=="setFramePadding")
3268 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3270 parser.setError (Aborted,"Type of selection does not allow setting frame padding");
3272 else if (parser.checkParCount(1))
3274 n=parser.parInt(ok,0);
3275 if (ok) setFramePadding(n);
3277 /////////////////////////////////////////////////////////////////////
3278 } else if (com=="setFrameBorderWidth")
3280 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3282 parser.setError (Aborted,"Type of selection does not allow setting frame border width");
3284 else if (parser.checkParCount(1))
3286 n=parser.parInt(ok,0);
3287 if (ok) setFrameBorderWidth (n);
3289 /////////////////////////////////////////////////////////////////////
3290 } else if (com=="setMapAuthor")
3292 if (parser.checkParCount(1))
3294 s=parser.parString(ok,0);
3295 if (ok) setAuthor (s);
3297 /////////////////////////////////////////////////////////////////////
3298 } else if (com=="setMapComment")
3300 if (parser.checkParCount(1))
3302 s=parser.parString(ok,0);
3303 if (ok) setComment(s);
3305 /////////////////////////////////////////////////////////////////////
3306 } else if (com=="setMapBackgroundColor")
3308 if (selection.isEmpty() )
3310 parser.setError (Aborted,"Nothing selected");
3311 } else if (! selection.getBranch() )
3313 parser.setError (Aborted,"Type of selection is not a branch");
3314 } else if (parser.checkParCount(1))
3316 QColor c=parser.parColor (ok,0);
3317 if (ok) setMapBackgroundColor (c);
3319 /////////////////////////////////////////////////////////////////////
3320 } else if (com=="setMapDefLinkColor")
3322 if (selection.isEmpty() )
3324 parser.setError (Aborted,"Nothing selected");
3327 parser.setError (Aborted,"Type of selection is not a branch");
3328 } else if (parser.checkParCount(1))
3330 QColor c=parser.parColor (ok,0);
3331 if (ok) setMapDefLinkColor (c);
3333 /////////////////////////////////////////////////////////////////////
3334 } else if (com=="setMapLinkStyle")
3336 if (parser.checkParCount(1))
3338 s=parser.parString (ok,0);
3339 if (ok) setMapLinkStyle(s);
3341 /////////////////////////////////////////////////////////////////////
3342 } else if (com=="setHeading")
3344 if (selection.isEmpty() )
3346 parser.setError (Aborted,"Nothing selected");
3349 parser.setError (Aborted,"Type of selection is not a branch");
3350 } else if (parser.checkParCount(1))
3352 s=parser.parString (ok,0);
3356 /////////////////////////////////////////////////////////////////////
3357 } else if (com=="setHideExport")
3359 if (selection.isEmpty() )
3361 parser.setError (Aborted,"Nothing selected");
3362 } else if (selection.type()!=Selection::Branch && selection.type() != Selection::MapCenter &&selection.type()!=Selection::FloatImage)
3364 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3365 } else if (parser.checkParCount(1))
3367 b=parser.parBool(ok,0);
3368 if (ok) setHideExport (b);
3370 /////////////////////////////////////////////////////////////////////
3371 } else if (com=="setIncludeImagesHorizontally")
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 b=parser.parBool(ok,0);
3382 if (ok) setIncludeImagesHor(b);
3384 /////////////////////////////////////////////////////////////////////
3385 } else if (com=="setIncludeImagesVertically")
3387 if (selection.isEmpty() )
3389 parser.setError (Aborted,"Nothing selected");
3392 parser.setError (Aborted,"Type of selection is not a branch");
3393 } else if (parser.checkParCount(1))
3395 b=parser.parBool(ok,0);
3396 if (ok) setIncludeImagesVer(b);
3398 /////////////////////////////////////////////////////////////////////
3399 } else if (com=="setHideLinkUnselected")
3401 if (selection.isEmpty() )
3403 parser.setError (Aborted,"Nothing selected");
3404 } else if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3406 parser.setError (Aborted,"Type of selection does not allow hiding the link");
3407 } else if (parser.checkParCount(1))
3409 b=parser.parBool(ok,0);
3410 if (ok) setHideLinkUnselected(b);
3412 /////////////////////////////////////////////////////////////////////
3413 } else if (com=="setSelectionColor")
3415 if (parser.checkParCount(1))
3417 QColor c=parser.parColor (ok,0);
3418 if (ok) setSelectionColorInt (c);
3420 /////////////////////////////////////////////////////////////////////
3421 } else if (com=="setURL")
3423 if (selection.isEmpty() )
3425 parser.setError (Aborted,"Nothing selected");
3428 parser.setError (Aborted,"Type of selection is not a branch");
3429 } else if (parser.checkParCount(1))
3431 s=parser.parString (ok,0);
3434 /////////////////////////////////////////////////////////////////////
3435 } else if (com=="setVymLink")
3437 if (selection.isEmpty() )
3439 parser.setError (Aborted,"Nothing selected");
3442 parser.setError (Aborted,"Type of selection is not a branch");
3443 } else if (parser.checkParCount(1))
3445 s=parser.parString (ok,0);
3446 if (ok) setVymLink(s);
3449 /////////////////////////////////////////////////////////////////////
3450 else if (com=="setFlag")
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(1))
3460 s=parser.parString(ok,0);
3463 selb->activateStandardFlag(s);
3464 selb->updateFlagsToolbar();
3467 /////////////////////////////////////////////////////////////////////
3468 } else if (com=="setFrameType")
3470 if (selection.isEmpty() )
3472 parser.setError (Aborted,"Nothing selected");
3475 parser.setError (Aborted,"Type of selection is not a branch");
3476 } else if (parser.checkParCount(1))
3478 s=parser.parString(ok,0);
3482 /////////////////////////////////////////////////////////////////////
3483 } else if (com=="sortChildren")
3485 if (selection.isEmpty() )
3487 parser.setError (Aborted,"Nothing selected");
3490 parser.setError (Aborted,"Type of selection is not a branch");
3491 } else if (parser.checkParCount(0))
3495 /////////////////////////////////////////////////////////////////////
3496 } else if (com=="toggleFlag")
3498 if (selection.isEmpty() )
3500 parser.setError (Aborted,"Nothing selected");
3503 parser.setError (Aborted,"Type of selection is not a branch");
3504 } else if (parser.checkParCount(1))
3506 s=parser.parString(ok,0);
3509 selb->toggleStandardFlag(s);
3510 selb->updateFlagsToolbar();
3513 /////////////////////////////////////////////////////////////////////
3514 } else if (com=="unscroll")
3516 if (selection.isEmpty() )
3518 parser.setError (Aborted,"Nothing selected");
3521 parser.setError (Aborted,"Type of selection is not a branch");
3522 } else if (parser.checkParCount(0))
3524 if (!unscrollBranch (selb))
3525 parser.setError (Aborted,"Could not unscroll branch");
3527 /////////////////////////////////////////////////////////////////////
3528 } else if (com=="unscrollChildren")
3530 if (selection.isEmpty() )
3532 parser.setError (Aborted,"Nothing selected");
3535 parser.setError (Aborted,"Type of selection is not a branch");
3536 } else if (parser.checkParCount(0))
3538 unscrollChildren ();
3540 /////////////////////////////////////////////////////////////////////
3541 } else if (com=="unsetFlag")
3543 if (selection.isEmpty() )
3545 parser.setError (Aborted,"Nothing selected");
3548 parser.setError (Aborted,"Type of selection is not a branch");
3549 } else if (parser.checkParCount(1))
3551 s=parser.parString(ok,0);
3554 selb->deactivateStandardFlag(s);
3555 selb->updateFlagsToolbar();
3559 parser.setError (Aborted,"Unknown command");
3562 if (parser.errorLevel()==NoError)
3564 // setChanged(); FIXME should not be called e.g. for export?!
3569 // TODO Error handling
3570 qWarning("VymModel::parseAtom: Error!");
3571 qWarning(parser.errorMessage());
3575 void VymModel::runScript (QString script)
3577 parser.setScript (script);
3579 while (parser.next() )
3580 parseAtom(parser.getAtom());
3583 void VymModel::setExportMode (bool b)
3585 // should be called before and after exports
3586 // depending on the settings
3587 if (b && settings.value("/export/useHideExport","true")=="true")
3588 setHideTmpMode (HideExport);
3590 setHideTmpMode (HideNone);
3593 void VymModel::exportImage(QString fname, bool askName, QString format)
3597 fname=getMapName()+".png";
3604 QFileDialog *fd=new QFileDialog (NULL);
3605 fd->setCaption (tr("Export map as image"));
3606 fd->setDirectory (lastImageDir);
3607 fd->setFileMode(QFileDialog::AnyFile);
3608 fd->setFilters (imageIO.getFilters() );
3611 fl=fd->selectedFiles();
3613 format=imageIO.getType(fd->selectedFilter());
3617 setExportMode (true);
3618 QPixmap pix (getPixmap());
3619 pix.save(fname, format);
3620 setExportMode (false);
3624 void VymModel::exportXML(QString dir, bool askForName)
3628 dir=browseDirectory(NULL,tr("Export XML to directory"));
3629 if (dir =="" && !reallyWriteDirectory(dir) )
3633 // Hide stuff during export, if settings want this
3634 setExportMode (true);
3636 // Create subdirectories
3639 // write to directory
3640 QString saveFile=saveToDir (dir,mapName+"-",true,getTotalBBox().topLeft() ,NULL);
3643 file.setName ( dir + "/"+mapName+".xml");
3644 if ( !file.open( QIODevice::WriteOnly ) )
3646 // This should neverever happen
3647 QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
3651 // Write it finally, and write in UTF8, no matter what
3652 QTextStream ts( &file );
3653 ts.setEncoding (QTextStream::UnicodeUTF8);
3657 // Now write image, too
3658 exportImage (dir+"/images/"+mapName+".png",false,"PNG");
3660 setExportMode (false);
3663 void VymModel::exportASCII(QString fname,bool askName)
3668 ex.setFile (mapName+".txt");
3674 //ex.addFilter ("TXT (*.txt)");
3675 ex.setDir(lastImageDir);
3676 //ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
3681 setExportMode(true);
3683 setExportMode(false);
3687 void VymModel::exportXHTML (const QString &dir, bool askForName)
3689 ExportXHTMLDialog dia(NULL);
3690 dia.setFilePath (filePath );
3691 dia.setMapName (mapName );
3693 if (dir!="") dia.setDir (dir);
3699 if (dia.exec()!=QDialog::Accepted)
3703 QDir d (dia.getDir());
3704 // Check, if warnings should be used before overwriting
3705 // the output directory
3706 if (d.exists() && d.count()>0)
3709 warn.showCancelButton (true);
3710 warn.setText(QString(
3711 "The directory %1 is not empty.\n"
3712 "Do you risk to overwrite some of its contents?").arg(d.path() ));
3713 warn.setCaption("Warning: Directory not empty");
3714 warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
3716 if (warn.exec()!=QDialog::Accepted) ok=false;
3723 exportXML (dia.getDir(),false );
3724 dia.doExport(mapName );
3725 //if (dia.hasChanged()) setChanged();
3729 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
3734 if (ex.setConfigFile(cf))
3736 setExportMode (true);
3737 ex.exportPresentation();
3738 setExportMode (false);
3745 //////////////////////////////////////////////
3747 //////////////////////////////////////////////
3749 void VymModel::registerEditor(QWidget *me)
3751 mapEditor=(MapEditor*)me;
3752 for (int i=0; i<mapCenters.count(); i++)
3753 mapCenters.at(i)->setMapEditor(mapEditor);
3756 void VymModel::unregisterEditor(QWidget *)
3761 void VymModel::setContextPos(QPointF p)
3766 void VymModel::unsetContextPos()
3768 contextPos=QPointF();
3771 void VymModel::updateNoteFlag()
3774 BranchObj *bo=selection.getBranch();
3777 bo->updateNoteFlag();
3778 mainWindow->updateActions();
3782 void VymModel::updateRelPositions()
3784 for (int i=0; i<mapCenters.count(); i++)
3785 mapCenters.at(i)->updateRelPositions();
3788 void VymModel::reposition()
3790 for (int i=0;i<mapCenters.count(); i++)
3791 mapCenters.at(i)->reposition(); // for positioning heading
3794 QPolygonF VymModel::shape(BranchObj *bo)
3796 // Creating (arbitrary) shapes
3799 QRectF rb=bo->getBBox();
3800 if (bo->getDepth()==0)
3802 // Just take BBox of this mapCenter
3803 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
3807 // Take union of BBox and TotalBBox
3809 QRectF ra=bo->getTotalBBox();
3810 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
3813 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
3816 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
3818 p <<ra.bottomRight()
3820 <<QPointF (rb.topRight().x(), ra.topRight().y() )
3823 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
3828 void VymModel::moveAway(LinkableMapObj *lmo)
3832 // Move all branches and MapCenters away from lmo
3833 // to avoid collisions
3838 BranchObj *boA=(BranchObj*)lmo;
3840 for (int i=0; i<mapCenters.count(); i++)
3842 boB=mapCenters.at(i);
3845 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
3848 <<" ("<<qPrintable(boA->getHeading() )<<")"
3849 <<" with ("<< qPrintable (boB->getHeading() )
3852 <<" minT="<<r.minTranslation<<endl<<endl;
3856 QPixmap VymModel::getPixmap()
3858 QRectF mapRect=getTotalBBox();
3859 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
3862 pp.setRenderHints(mapEditor->renderHints());
3864 // Don't print the visualisation of selection
3865 selection.unselect();
3867 mapScene->render ( &pp,
3868 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
3869 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
3871 // Restore selection
3872 selection.reselect();
3878 void VymModel::setMapLinkStyle (const QString & s)
3883 case LinkableMapObj::Line :
3886 case LinkableMapObj::Parabel:
3887 snow="StyleParabel";
3889 case LinkableMapObj::PolyLine:
3890 snow="StylePolyLine";
3892 case LinkableMapObj::PolyParabel:
3893 snow="StylePolyParabel";
3896 snow="UndefinedStyle";
3901 QString("setMapLinkStyle (\"%1\")").arg(s),
3902 QString("setMapLinkStyle (\"%1\")").arg(snow),
3903 QString("Set map link style (\"%1\")").arg(s)
3907 linkstyle=LinkableMapObj::Line;
3908 else if (s=="StyleParabel")
3909 linkstyle=LinkableMapObj::Parabel;
3910 else if (s=="StylePolyLine")
3911 linkstyle=LinkableMapObj::PolyLine;
3912 else if (s=="StylePolyParabel")
3913 linkstyle=LinkableMapObj::PolyParabel;
3915 linkstyle=LinkableMapObj::UndefinedStyle;
3922 bo->setLinkStyle(bo->getDefLinkStyle());
3928 LinkableMapObj::Style VymModel::getMapLinkStyle ()
3933 void VymModel::setMapDefLinkColor(QColor col)
3935 if ( !col.isValid() ) return;
3937 QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
3938 QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
3939 QString("Set map link color to %1").arg(col.name())
3953 void VymModel::setMapLinkColorHintInt()
3955 // called from setMapLinkColorHint(lch) or at end of parse
3965 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
3968 setMapLinkColorHintInt();
3971 void VymModel::toggleMapLinkColorHint()
3973 if (linkcolorhint==LinkableMapObj::HeadingColor)
3974 linkcolorhint=LinkableMapObj::DefaultColor;
3976 linkcolorhint=LinkableMapObj::HeadingColor;
3986 void VymModel::selectMapBackgroundImage ()
3988 Q3FileDialog *fd=new Q3FileDialog( NULL);
3989 fd->setMode (Q3FileDialog::ExistingFile);
3990 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
3991 ImagePreview *p =new ImagePreview (fd);
3992 fd->setContentsPreviewEnabled( TRUE );
3993 fd->setContentsPreview( p, p );
3994 fd->setPreviewMode( Q3FileDialog::Contents );
3995 fd->setCaption(vymName+" - " +tr("Load background image"));
3996 fd->setDir (lastImageDir);
3999 if ( fd->exec() == QDialog::Accepted )
4001 // TODO selectMapBackgroundImg in QT4 use: lastImageDir=fd->directory();
4002 lastImageDir=QDir (fd->dirPath());
4003 setMapBackgroundImage (fd->selectedFile());
4007 void VymModel::setMapBackgroundImage (const QString &fn) //FIXME missing savestate
4009 QColor oldcol=mapScene->backgroundBrush().color();
4013 QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
4015 QString ("setMapBackgroundImage (%1)").arg(col.name()),
4016 QString("Set background color of map to %1").arg(col.name()));
4019 brush.setTextureImage (QPixmap (fn));
4020 mapScene->setBackgroundBrush(brush);
4023 void VymModel::selectMapBackgroundColor()
4025 QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
4026 if ( !col.isValid() ) return;
4027 setMapBackgroundColor( col );
4031 void VymModel::setMapBackgroundColor(QColor col)
4033 QColor oldcol=mapScene->backgroundBrush().color();
4035 QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
4036 QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
4037 QString("Set background color of map to %1").arg(col.name()));
4038 mapScene->setBackgroundBrush(col);
4041 QColor VymModel::getMapBackgroundColor()
4043 return mapScene->backgroundBrush().color();
4047 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint()
4049 return linkcolorhint;
4052 QColor VymModel::getMapDefLinkColor()
4054 return defLinkColor;
4057 void VymModel::setMapDefXLinkColor(QColor col)
4062 QColor VymModel::getMapDefXLinkColor()
4064 return defXLinkColor;
4067 void VymModel::setMapDefXLinkWidth (int w)
4072 int VymModel::getMapDefXLinkWidth()
4074 return defXLinkWidth;
4077 void VymModel::move(const double &x, const double &y)
4079 LinkableMapObj *sel=selection.single();
4082 QPointF ap(sel->getAbsPos());
4086 QString ps=qpointfToString(ap);
4087 QString s=selection.getSelectString();
4090 s, "move "+qpointfToString(to),
4091 QString("Move %1 to %2").arg(getObjectName(sel)).arg(ps));
4099 void VymModel::moveRel (const double &x, const double &y)
4101 LinkableMapObj *sel=selection.single();
4104 QPointF rp(sel->getRelPos());
4108 QString ps=qpointfToString (sel->getRelPos());
4109 QString s=getSelectString(sel);
4112 s, "moveRel "+qpointfToString(to),
4113 QString("Move %1 to relative position %2").arg(getObjectName(sel)).arg(ps));
4114 ((OrnamentedObj*)sel)->move2RelPos (x,y);
4123 void VymModel::animate()
4125 animationTimer->stop();
4128 while (i<animObjList.size() )
4130 bo=(BranchObj*)animObjList.at(i);
4135 animObjList.removeAt(i);
4142 emit (selectionChanged(selModel->selection(), selModel->selection()) );
4144 if (!animObjList.isEmpty()) animationTimer->start();
4148 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
4150 if (bo && bo->getDepth()>0)
4153 ap.setStart (start);
4155 ap.setTicks (animationTicks);
4156 ap.setAnimated (true);
4157 bo->setAnimation (ap);
4158 animObjList.append( bo );
4159 animationTimer->setSingleShot (true);
4160 animationTimer->start(animationInterval);
4164 void VymModel::stopAnimation (MapObj *mo)
4166 int i=animObjList.indexOf(mo);
4168 animObjList.removeAt (i);
4171 void VymModel::sendSelection()
4173 if (netstate!=Server) return;
4174 sendData (QString("select (\"%1\")").arg(selection.getSelectString()) );
4177 void VymModel::newServer()
4181 tcpServer = new QTcpServer(this);
4182 if (!tcpServer->listen(QHostAddress::Any,port)) {
4183 QMessageBox::critical(NULL, "vym server",
4184 QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
4185 //FIXME needed? we are no widget any longer... close();
4188 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
4190 cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
4193 void VymModel::connectToServer()
4196 server="salam.suse.de";
4198 clientSocket = new QTcpSocket (this);
4199 clientSocket->abort();
4200 clientSocket->connectToHost(server ,port);
4201 connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
4202 connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
4203 this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
4205 cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
4210 void VymModel::newClient()
4212 QTcpSocket *newClient = tcpServer->nextPendingConnection();
4213 connect(newClient, SIGNAL(disconnected()),
4214 newClient, SLOT(deleteLater()));
4216 cout <<"ME::newClient at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
4218 clientList.append (newClient);
4222 void VymModel::sendData(const QString &s)
4224 if (clientList.size()==0) return;
4226 // Create bytearray to send
4228 QDataStream out(&block, QIODevice::WriteOnly);
4229 out.setVersion(QDataStream::Qt_4_0);
4231 // Reserve some space for blocksize
4234 // Write sendCounter
4235 out << sendCounter++;
4240 // Go back and write blocksize so far
4241 out.device()->seek(0);
4242 quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
4246 cout << "ME::sendData bs="<<bs<<" counter="<<sendCounter<<" s="<<qPrintable(s)<<endl;
4248 for (int i=0; i<clientList.size(); ++i)
4250 //cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
4251 clientList.at(i)->write (block);
4255 void VymModel::readData ()
4257 while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
4260 cout <<"readData bytesAvail="<<clientSocket->bytesAvailable();
4264 QDataStream in(clientSocket);
4265 in.setVersion(QDataStream::Qt_4_0);
4273 cout << " t="<<qPrintable (t)<<endl;
4279 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
4281 switch (socketError) {
4282 case QAbstractSocket::RemoteHostClosedError:
4284 case QAbstractSocket::HostNotFoundError:
4285 QMessageBox::information(NULL, vymName +" Network client",
4286 "The host was not found. Please check the "
4287 "host name and port settings.");
4289 case QAbstractSocket::ConnectionRefusedError:
4290 QMessageBox::information(NULL, vymName + " Network client",
4291 "The connection was refused by the peer. "
4292 "Make sure the fortune server is running, "
4293 "and check that the host name and port "
4294 "settings are correct.");
4297 QMessageBox::information(NULL, vymName + " Network client",
4298 QString("The following error occurred: %1.")
4299 .arg(clientSocket->errorString()));
4306 void VymModel::selectMapSelectionColor()
4308 QColor col = QColorDialog::getColor( defLinkColor, NULL);
4309 setSelectionColor (col);
4312 void VymModel::setSelectionColorInt (QColor col)
4314 if ( !col.isValid() ) return;
4316 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4317 QString("setSelectionColor (%1)").arg(col.name()),
4318 QString("Set color of selection box to %1").arg(col.name())
4321 mapEditor->setSelectionColor (col);
4324 void VymModel::updateSelection()
4329 void VymModel::setSelectionColor(QColor col)
4331 if ( !col.isValid() ) return;
4333 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4334 QString("setSelectionColor (%1)").arg(col.name()),
4335 QString("Set color of selection box to %1").arg(col.name())
4337 setSelectionColorInt (col);
4340 QColor VymModel::getSelectionColor()
4342 return mapEditor->getSelectionColor();
4345 void VymModel::setHideTmpMode (HideTmpMode mode)
4348 for (int i=0;i<mapCenters.count(); i++)
4349 mapCenters.at(i)->setHideTmp (mode);
4351 // FIXME needed? scene()->update();
4355 QRectF VymModel::getTotalBBox()
4358 for (int i=0;i<mapCenters.count(); i++)
4359 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
4363 //////////////////////////////////////////////
4364 // Selection related
4365 //////////////////////////////////////////////
4367 void VymModel::setSelectionModel (QItemSelectionModel *sm)
4370 cout << "VM::setSelModel selModel="<<selModel<<endl;
4373 QItemSelectionModel* VymModel::getSelectionModel()
4378 void VymModel::setSelectionBlocked (bool b)
4383 selection.unblock();
4386 bool VymModel::isSelectionBlocked()
4388 return selection.isBlocked();
4391 bool VymModel::select ()
4393 QModelIndex index=selModel->selectedIndexes().first(); // TODO no multiselections yet
4395 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
4396 return select (item->getLMO() );
4399 bool VymModel::select (const QString &s)
4401 if (selection.select(s))
4404 ensureSelectionVisible();
4412 bool VymModel::select (LinkableMapObj *lmo)
4414 if (selection.select(lmo))
4417 ensureSelectionVisible();
4424 void VymModel::unselect()
4426 selection.unselect();
4429 void VymModel::reselect()
4431 selection.reselect();
4434 void VymModel::ensureSelectionVisible()
4436 LinkableMapObj *lmo=selection.single();
4437 if (lmo &&mapEditor) mapEditor->ensureVisible (lmo->getBBox() );
4441 void VymModel::selectInt (LinkableMapObj *lmo)
4443 if (selection.select(lmo))
4451 void VymModel::selectNextBranchInt()
4453 // Increase number of branch
4454 LinkableMapObj *sel=selection.single();
4457 QString s=selection.getSelectString();
4463 part=s.section(",",-1);
4465 num=part.right(part.length() - 3);
4467 s=s.left (s.length() -num.length());
4470 num=QString ("%1").arg(num.toUInt()+1);
4474 // Try to select this one
4475 if (select (s)) return;
4477 // We have no direct successor,
4478 // try to increase the parental number in order to
4479 // find a successor with same depth
4481 int d=selection.single()->getDepth();
4486 while (!found && d>0)
4488 s=s.section (",",0,d-1);
4489 // replace substring of current depth in s with "1"
4490 part=s.section(",",-1);
4492 num=part.right(part.length() - 3);
4496 // increase number of parent
4497 num=QString ("%1").arg(num.toUInt()+1);
4498 s=s.section (",",0,d-2) + ","+ typ+num;
4501 // Special case, look at orientation
4502 if (selection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
4503 num=QString ("%1").arg(num.toUInt()+1);
4505 num=QString ("%1").arg(num.toUInt()-1);
4510 // pad to oldDepth, select the first branch for each depth
4511 for (i=d;i<oldDepth;i++)
4516 if ( selection.getBranch()->countBranches()>0)
4524 // try to select the freshly built string
4532 void VymModel::selectPrevBranchInt()
4534 // Decrease number of branch
4535 BranchObj *bo=selection.getBranch();
4538 QString s=selection.getSelectString();
4544 part=s.section(",",-1);
4546 num=part.right(part.length() - 3);
4548 s=s.left (s.length() -num.length());
4550 int n=num.toInt()-1;
4553 num=QString ("%1").arg(n);
4556 // Try to select this one
4557 if (n>=0 && select (s)) return;
4559 // We have no direct precessor,
4560 // try to decrease the parental number in order to
4561 // find a precessor with same depth
4563 int d=selection.single()->getDepth();
4568 while (!found && d>0)
4570 s=s.section (",",0,d-1);
4571 // replace substring of current depth in s with "1"
4572 part=s.section(",",-1);
4574 num=part.right(part.length() - 3);
4578 // decrease number of parent
4579 num=QString ("%1").arg(num.toInt()-1);
4580 s=s.section (",",0,d-2) + ","+ typ+num;
4583 // Special case, look at orientation
4584 if (selection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
4585 num=QString ("%1").arg(num.toInt()-1);
4587 num=QString ("%1").arg(num.toInt()+1);
4592 // pad to oldDepth, select the last branch for each depth
4593 for (i=d;i<oldDepth;i++)
4597 if ( selection.getBranch()->countBranches()>0)
4598 s+=",bo:"+ QString ("%1").arg( selection.getBranch()->countBranches()-1 );
4605 // try to select the freshly built string
4613 void VymModel::selectUpperBranch()
4615 if (selection.isBlocked() ) return;
4617 BranchObj *bo=selection.getBranch();
4618 if (bo && selection.type()==Selection::Branch)
4620 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4621 selectPrevBranchInt();
4623 if (bo->getDepth()==1)
4624 selectNextBranchInt();
4626 selectPrevBranchInt();
4630 void VymModel::selectLowerBranch()
4632 if (selection.isBlocked() ) return;
4634 BranchObj *bo=selection.getBranch();
4635 if (bo && selection.type()==Selection::Branch)
4637 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4638 selectNextBranchInt();
4640 if (bo->getDepth()==1)
4641 selectPrevBranchInt();
4643 selectNextBranchInt();
4648 void VymModel::selectLeftBranch()
4650 if (selection.isBlocked() ) return;
4654 LinkableMapObj *sel=selection.single();
4657 if (selection.type()== Selection::MapCenter)
4659 par=selection.getBranch();
4660 bo=par->getLastSelectedBranch();
4663 // Workaround for reselecting on left and right side
4664 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4665 bo=par->getLastBranch();
4668 bo=par->getLastBranch();
4669 selection.select(bo);
4671 ensureSelectionVisible();
4677 par=(BranchObj*)(sel->getParObj());
4678 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4680 if (selection.type() == Selection::Branch ||
4681 selection.type() == Selection::FloatImage)
4683 selection.select(par);
4685 ensureSelectionVisible();
4690 if (selection.type() == Selection::Branch )
4692 bo=selection.getBranch()->getLastSelectedBranch();
4695 selection.select(bo);
4697 ensureSelectionVisible();
4706 void VymModel::selectRightBranch()
4708 if (selection.isBlocked() ) return;
4712 LinkableMapObj *sel=selection.single();
4715 if (selection.type()==Selection::MapCenter)
4717 par=selection.getBranch();
4718 bo=par->getLastSelectedBranch();
4721 // Workaround for reselecting on left and right side
4722 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
4723 bo=par->getFirstBranch();
4726 selection.select(bo);
4728 ensureSelectionVisible();
4734 par=(BranchObj*)(selection.single()->getParObj());
4735 if (selection.single()->getOrientation()==LinkableMapObj::LeftOfCenter)
4737 if (selection.type() == Selection::Branch ||
4738 selection.type() == Selection::FloatImage)
4740 selection.select(par);
4742 ensureSelectionVisible();
4747 if (selection.type() == Selection::Branch)
4749 bo=selection.getBranch()->getLastSelectedBranch();
4752 selection.select(bo);
4754 ensureSelectionVisible();
4763 void VymModel::selectFirstBranch()
4765 BranchObj *bo1=selection.getBranch();
4770 par=(BranchObj*)(bo1->getParObj());
4772 bo2=par->getFirstBranch();
4774 selection.select(bo2);
4776 ensureSelectionVisible();
4782 void VymModel::selectLastBranch()
4784 BranchObj *bo1=selection.getBranch();
4789 par=(BranchObj*)(bo1->getParObj());
4791 bo2=par->getLastBranch();
4794 selection.select(bo2);
4796 ensureSelectionVisible();
4802 void VymModel::selectParent()
4804 LinkableMapObj *lmo=selection.first();
4808 par=(BranchObj*)(lmo->getParObj());
4810 selection.select(par);
4812 ensureSelectionVisible();
4817 Selection::Type VymModel::selectionType()
4819 return selection.type();
4822 LinkableMapObj* VymModel::getSelection()
4824 return selection.single();
4826 BranchObj* VymModel::getSelectedBranch()
4828 return selection.getBranch();
4831 FloatImageObj* VymModel::getSelectedFloatImage()
4833 return selection.getFloatImage();
4836 QString VymModel::getSelectString ()
4838 return selection.getSelectString();
4841 QString VymModel::getSelectString (LinkableMapObj *lmo)
4845 if (typeid(*lmo)==typeid(BranchObj) ||
4846 typeid(*lmo)==typeid(MapCenterObj) )
4848 LinkableMapObj *par=lmo->getParObj();
4851 if (lmo->getDepth() ==1)
4852 // Mainbranch, return
4853 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4855 // Branch, call myself recursively
4856 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4860 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
4861 if (i>=0) s=QString("mc:%1").arg(i);