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()");
342 bool zipped_org=zipped;
347 // FIXME VM not needed??? model->setMapEditor(this);
348 // (map state is set later at end of load...)
351 BranchObj *bo=selection.getBranch();
352 if (!bo) return aborted;
353 if (lmode==ImportAdd)
354 saveStateChangingPart(
357 QString("addMapInsert (%1)").arg(fname),
358 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
360 saveStateChangingPart(
363 QString("addMapReplace(%1)").arg(fname),
364 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
368 // Create temporary directory for packing
370 QString tmpZipDir=makeTmpDir (ok,"vym-pack");
373 QMessageBox::critical( 0, tr( "Critical Load Error" ),
374 tr("Couldn't create temporary directory before load\n"));
379 err=unzipDir (tmpZipDir,fname);
389 // Look for mapname.xml
390 xmlfile= fname.left(fname.findRev(".",-1,true));
391 xmlfile=xmlfile.section( '/', -1 );
392 QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
393 if (!mfile.exists() )
395 // mapname.xml does not exist, well,
396 // maybe someone renamed the mapname.vym file...
397 // Try to find any .xml in the toplevel
398 // directory of the .vym file
399 QStringList flist=QDir (tmpZipDir).entryList("*.xml");
400 if (flist.count()==1)
402 // Only one entry, take this one
403 xmlfile=tmpZipDir + "/"+flist.first();
406 for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it )
407 *it=tmpZipDir + "/" + *it;
408 // TODO Multiple entries, load all (but only the first one into this ME)
409 //mainWindow->fileLoadFromTmp (flist);
410 //returnCode=1; // Silently forget this attempt to load
411 qWarning ("MainWindow::load (fn) multimap found...");
414 if (flist.isEmpty() )
416 QMessageBox::critical( 0, tr( "Critical Load Error" ),
417 tr("Couldn't find a map (*.xml) in .vym archive.\n"));
420 } //file doesn't exist
422 xmlfile=mfile.name();
425 QFile file( xmlfile);
427 // I am paranoid: file should exist anyway
428 // according to check in mainwindow.
431 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
432 tr(QString("Couldn't open map %1").arg(file.name())));
436 bool blockSaveStateOrg=blockSaveState;
437 blockReposition=true;
439 QXmlInputSource source( file);
440 QXmlSimpleReader reader;
441 reader.setContentHandler( handler );
442 reader.setErrorHandler( handler );
443 handler->setModel ( this);
446 // We need to set the tmpDir in order to load files with rel. path
451 tmpdir=fname.left(fname.findRev("/",-1));
452 handler->setTmpDir (tmpdir);
453 handler->setInputFile (file.name());
454 handler->setLoadMode (lmode);
455 bool ok = reader.parse( source );
456 blockReposition=false;
457 blockSaveState=blockSaveStateOrg;
461 reposition(); // FIXME VM reposition the view instead...
468 autosaveTimer->stop();
471 // Reset timestamp to check for later updates of file
472 fileChangedTime=QFileInfo (destPath).lastModified();
475 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
476 tr( handler->errorProtocol() ) );
478 // Still return "success": the map maybe at least
479 // partially read by the parser
484 removeDir (QDir(tmpZipDir));
486 // Restore original zip state
493 ErrorCode VymModel::save (const SaveMode &savemode)
497 QString safeFilePath;
499 ErrorCode err=success;
503 mapFileName=mapName+".xml";
505 // use name given by user, even if he chooses .doc
506 mapFileName=fileName;
508 // Look, if we should zip the data:
511 QMessageBox mb( vymName,
512 tr("The map %1\ndid not use the compressed "
513 "vym file format.\nWriting it uncompressed will also write images \n"
514 "and flags and thus may overwrite files in the "
515 "given directory\n\nDo you want to write the map").arg(filePath),
516 QMessageBox::Warning,
517 QMessageBox::Yes | QMessageBox::Default,
519 QMessageBox::Cancel | QMessageBox::Escape);
520 mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
521 mb.setButtonText( QMessageBox::No, tr("uncompressed") );
522 mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
525 case QMessageBox::Yes:
526 // save compressed (default file format)
529 case QMessageBox::No:
533 case QMessageBox::Cancel:
540 // First backup existing file, we
541 // don't want to add to old zip archives
545 if ( settings.value ("/mapeditor/writeBackupFile").toBool())
547 QString backupFileName(destPath + "~");
548 QFile backupFile(backupFileName);
549 if (backupFile.exists() && !backupFile.remove())
551 QMessageBox::warning(0, tr("Save Error"),
552 tr("%1\ncould not be removed before saving").arg(backupFileName));
554 else if (!f.rename(backupFileName))
556 QMessageBox::warning(0, tr("Save Error"),
557 tr("%1\ncould not be renamed before saving").arg(destPath));
564 // Create temporary directory for packing
566 tmpZipDir=makeTmpDir (ok,"vym-zip");
569 QMessageBox::critical( 0, tr( "Critical Load Error" ),
570 tr("Couldn't create temporary directory before save\n"));
574 safeFilePath=filePath;
575 setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
578 // Create mapName and fileDir
579 makeSubDirs (fileDir);
582 if (savemode==CompleteMap || selection.isEmpty())
585 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
588 autosaveTimer->stop();
593 if (selection.type()==Selection::FloatImage)
596 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),selection.getBranch());
597 // TODO take care of multiselections
600 if (!saveStringToDisk(fileDir+mapFileName,saveFile))
603 qWarning ("ME::saveStringToDisk failed!");
609 if (err==success) err=zipDir (tmpZipDir,destPath);
612 removeDir (QDir(tmpZipDir));
614 // Restore original filepath outside of tmp zip dir
615 setFilePath (safeFilePath);
619 fileChangedTime=QFileInfo (destPath).lastModified();
623 void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)
625 QString pathDir=path.left(path.findRev("/"));
631 // We need to parse saved XML data
632 parseVYMHandler handler;
633 QXmlInputSource source( file);
634 QXmlSimpleReader reader;
635 reader.setContentHandler( &handler );
636 reader.setErrorHandler( &handler );
637 handler.setModel ( this);
638 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
639 if (undoSel.isEmpty())
643 handler.setLoadMode (NewMap);
647 handler.setLoadMode (ImportReplace);
649 blockReposition=true;
650 bool ok = reader.parse( source );
651 blockReposition=false;
654 // This should never ever happen
655 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
656 handler.errorProtocol());
659 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
662 void VymModel::addMapInsertInt (const QString &path, int pos)
664 BranchObj *sel=selection.getBranch();
667 QString pathDir=path.left(path.findRev("/"));
673 // We need to parse saved XML data
674 parseVYMHandler handler;
675 QXmlInputSource source( file);
676 QXmlSimpleReader reader;
677 reader.setContentHandler( &handler );
678 reader.setErrorHandler( &handler );
679 handler.setModel (this);
680 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
681 handler.setLoadMode (ImportAdd);
682 blockReposition=true;
683 bool ok = reader.parse( source );
684 blockReposition=false;
687 // This should never ever happen
688 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
689 handler.errorProtocol());
691 if (sel->getDepth()>0)
692 sel->getLastBranch()->linkTo (sel,pos);
694 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
698 FloatImageObj* VymModel::loadFloatImageInt (QString fn)
700 BranchObj *bo=selection.getBranch();
705 fio=bo->getLastFloatImage();
708 // FIXME VM needed? scene()->update();
714 void VymModel::loadFloatImage ()
716 BranchObj *bo=selection.getBranch();
720 Q3FileDialog *fd=new Q3FileDialog( NULL);
721 fd->setMode (Q3FileDialog::ExistingFiles);
722 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
723 ImagePreview *p =new ImagePreview (fd);
724 fd->setContentsPreviewEnabled( TRUE );
725 fd->setContentsPreview( p, p );
726 fd->setPreviewMode( Q3FileDialog::Contents );
727 fd->setCaption(vymName+" - " +tr("Load image"));
728 fd->setDir (lastImageDir);
731 if ( fd->exec() == QDialog::Accepted )
733 // TODO loadFIO in QT4 use: lastImageDir=fd->directory();
734 lastImageDir=QDir (fd->dirPath());
737 for (int j=0; j<fd->selectedFiles().count(); j++)
739 s=fd->selectedFiles().at(j);
740 fio=loadFloatImageInt (s);
743 (LinkableMapObj*)fio,
746 QString ("loadImage (%1)").arg(s ),
747 QString("Add image %1 to %2").arg(s).arg(getObjectName(bo))
750 // TODO loadFIO error handling
751 qWarning ("Failed to load "+s);
759 void VymModel::saveFloatImageInt (FloatImageObj *fio, const QString &type, const QString &fn)
764 void VymModel::saveFloatImage ()
766 FloatImageObj *fio=selection.getFloatImage();
769 QFileDialog *fd=new QFileDialog( NULL);
770 fd->setFilters (imageIO.getFilters());
771 fd->setCaption(vymName+" - " +tr("Save image"));
772 fd->setFileMode( QFileDialog::AnyFile );
773 fd->setDirectory (lastImageDir);
774 // fd->setSelection (fio->getOriginalFilename());
778 if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
780 fn=fd->selectedFiles().at(0);
781 if (QFile (fn).exists() )
783 QMessageBox mb( vymName,
784 tr("The file %1 exists already.\n"
785 "Do you want to overwrite it?").arg(fn),
786 QMessageBox::Warning,
787 QMessageBox::Yes | QMessageBox::Default,
788 QMessageBox::Cancel | QMessageBox::Escape,
789 QMessageBox::NoButton );
791 mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
792 mb.setButtonText( QMessageBox::No, tr("Cancel"));
795 case QMessageBox::Yes:
798 case QMessageBox::Cancel:
805 saveFloatImageInt (fio,fd->selectedFilter(),fn );
812 void VymModel::importDirInt(BranchObj *dst, QDir d)
814 BranchObj *bo=selection.getBranch();
817 // Traverse directories
818 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
819 QFileInfoList list = d.entryInfoList();
822 for (int i = 0; i < list.size(); ++i)
825 if (fi.fileName() != "." && fi.fileName() != ".." )
828 bo=dst->getLastBranch();
829 bo->setHeading (fi.fileName() );
830 bo->setColor (QColor("blue"));
832 if ( !d.cd(fi.fileName()) )
833 QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
836 // Recursively add subdirs
843 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
844 list = d.entryInfoList();
846 for (int i = 0; i < list.size(); ++i)
850 bo=dst->getLastBranch();
851 bo->setHeading (fi.fileName() );
852 bo->setColor (QColor("black"));
853 if (fi.fileName().right(4) == ".vym" )
854 bo->setVymLink (fi.filePath());
859 void VymModel::importDirInt (const QString &s)
861 BranchObj *bo=selection.getBranch();
864 saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
871 void VymModel::importDir()
873 BranchObj *bo=selection.getBranch();
877 filters <<"VYM map (*.vym)";
878 QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
879 fd->setMode (QFileDialog::DirectoryOnly);
880 fd->setFilters (filters);
881 fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
885 if ( fd->exec() == QDialog::Accepted )
887 importDirInt (fd->selectedFile() );
889 //FIXME VM needed? scene()->update();
895 void VymModel::autosave()
897 QDateTime now=QDateTime().currentDateTime();
899 // Disable autosave, while we have gone back in history
900 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
901 if (redosAvail>0) return;
903 // Also disable autosave for new map without filename
904 if (filePath.isEmpty()) return;
907 if (mapUnsaved &&mapChanged && settings.value ("/autosave/use",true).toBool() )
909 if (QFileInfo(filePath).lastModified()<=fileChangedTime)
910 mainWindow->fileSave (this);
913 cout <<" ME::autosave rejected, file on disk is newer than last save.\n";
918 void VymModel::fileChanged()
920 // Check if file on disk has changed meanwhile
921 if (!filePath.isEmpty())
923 QDateTime tmod=QFileInfo (filePath).lastModified();
924 if (tmod>fileChangedTime)
926 // FIXME VM switch to current mapeditor and finish lineedits...
927 QMessageBox mb( vymName,
928 tr("The file of the map on disk has changed:\n\n"
929 " %1\n\nDo you want to reload that map with the new file?").arg(filePath),
930 QMessageBox::Question,
932 QMessageBox::Cancel | QMessageBox::Default,
933 QMessageBox::NoButton );
935 mb.setButtonText( QMessageBox::Yes, tr("Reload"));
936 mb.setButtonText( QMessageBox::No, tr("Ignore"));
939 case QMessageBox::Yes:
941 load (filePath,NewMap,fileType);
942 case QMessageBox::Cancel:
943 fileChangedTime=tmod; // allow autosave to overwrite newer file!
949 bool VymModel::isDefault()
954 void VymModel::makeDefault()
960 bool VymModel::hasChanged()
965 void VymModel::setChanged()
968 autosaveTimer->start(settings.value("/autosave/ms/",300000).toInt());
976 QString VymModel::getObjectName (const LinkableMapObj *lmo)
979 if (!lmo) return QString("Error: NULL has no name!");
981 if ((typeid(*lmo) == typeid(BranchObj) ||
982 typeid(*lmo) == typeid(MapCenterObj)))
985 s=(((BranchObj*)lmo)->getHeading());
986 if (s=="") s="unnamed";
987 return QString("branch (%1)").arg(s);
989 if ((typeid(*lmo) == typeid(FloatImageObj) ))
990 return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
991 return QString("Unknown type has no name!");
994 void VymModel::redo()
996 // Can we undo at all?
997 if (redosAvail<1) return;
999 bool blockSaveStateOrg=blockSaveState;
1000 blockSaveState=true;
1004 if (undosAvail<stepsTotal) undosAvail++;
1006 if (curStep>stepsTotal) curStep=1;
1007 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1008 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1009 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1010 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1011 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1012 QString version=undoSet.readEntry ("/history/version");
1014 /* TODO Maybe check for version, if we save the history
1015 if (!checkVersion(version))
1016 QMessageBox::warning(0,tr("Warning"),
1017 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1020 // Find out current undo directory
1021 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1025 cout << "VymModel::redo() begin\n";
1026 cout << " undosAvail="<<undosAvail<<endl;
1027 cout << " redosAvail="<<redosAvail<<endl;
1028 cout << " curStep="<<curStep<<endl;
1029 cout << " ---------------------------"<<endl;
1030 cout << " comment="<<comment.toStdString()<<endl;
1031 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1032 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1033 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1034 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1035 cout << " ---------------------------"<<endl<<endl;
1038 // select object before redo
1039 if (!redoSelection.isEmpty())
1040 select (redoSelection);
1043 parseAtom (redoCommand);
1046 blockSaveState=blockSaveStateOrg;
1048 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1049 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1050 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1051 undoSet.writeSettings(histPath);
1053 mainWindow->updateHistory (undoSet);
1056 /* TODO remove testing
1057 cout << "ME::redo() end\n";
1058 cout << " undosAvail="<<undosAvail<<endl;
1059 cout << " redosAvail="<<redosAvail<<endl;
1060 cout << " curStep="<<curStep<<endl;
1061 cout << " ---------------------------"<<endl<<endl;
1067 bool VymModel::isRedoAvailable()
1069 if (undoSet.readNumEntry("/history/redosAvail",0)>0)
1075 void VymModel::undo()
1077 // Can we undo at all?
1078 if (undosAvail<1) return;
1080 mainWindow->statusMessage (tr("Autosave disabled during undo."));
1082 bool blockSaveStateOrg=blockSaveState;
1083 blockSaveState=true;
1085 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1086 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1087 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1088 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1089 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1090 QString version=undoSet.readEntry ("/history/version");
1092 /* TODO Maybe check for version, if we save the history
1093 if (!checkVersion(version))
1094 QMessageBox::warning(0,tr("Warning"),
1095 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1098 // Find out current undo directory
1099 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1101 // select object before undo
1102 if (!undoSelection.isEmpty())
1103 select (undoSelection);
1107 cout << "VymModel::undo() begin\n";
1108 cout << " undosAvail="<<undosAvail<<endl;
1109 cout << " redosAvail="<<redosAvail<<endl;
1110 cout << " curStep="<<curStep<<endl;
1111 cout << " ---------------------------"<<endl;
1112 cout << " comment="<<comment.toStdString()<<endl;
1113 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1114 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1115 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1116 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1117 cout << " ---------------------------"<<endl<<endl;
1119 parseAtom (undoCommand);
1124 if (curStep<1) curStep=stepsTotal;
1128 blockSaveState=blockSaveStateOrg;
1129 /* TODO remove testing
1130 cout << "VymModel::undo() end\n";
1131 cout << " undosAvail="<<undosAvail<<endl;
1132 cout << " redosAvail="<<redosAvail<<endl;
1133 cout << " curStep="<<curStep<<endl;
1134 cout << " ---------------------------"<<endl<<endl;
1137 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1138 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1139 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1140 undoSet.writeSettings(histPath);
1142 mainWindow->updateHistory (undoSet);
1145 ensureSelectionVisible();
1148 bool VymModel::isUndoAvailable()
1150 if (undoSet.readNumEntry("/history/undosAvail",0)>0)
1156 void VymModel::gotoHistoryStep (int i)
1158 // Restore variables
1159 int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
1160 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
1162 if (i<0) i=undosAvail+redosAvail;
1164 // Clicking above current step makes us undo things
1167 for (int j=0; j<undosAvail-i; j++) undo();
1170 // Clicking below current step makes us redo things
1172 for (int j=undosAvail; j<i; j++)
1174 if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
1178 // And ignore clicking the current row ;-)
1182 QString VymModel::getHistoryPath()
1184 QString histName(QString("history-%1").arg(curStep));
1185 return (tmpMapDir+"/"+histName);
1188 void VymModel::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
1190 sendData(redoCom); //FIXME testing
1195 if (blockSaveState) return;
1197 if (debug) cout << "ME::saveState() for "<<qPrintable (mapName)<<endl;
1199 // Find out current undo directory
1200 if (undosAvail<stepsTotal) undosAvail++;
1202 if (curStep>stepsTotal) curStep=1;
1204 QString backupXML="";
1205 QString histDir=getHistoryPath();
1206 QString bakMapPath=histDir+"/map.xml";
1208 // Create histDir if not available
1211 makeSubDirs (histDir);
1213 // Save depending on how much needs to be saved
1215 backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
1217 QString undoCommand="";
1218 if (savemode==UndoCommand)
1220 undoCommand=undoCom;
1222 else if (savemode==PartOfMap )
1224 undoCommand=undoCom;
1225 undoCommand.replace ("PATH",bakMapPath);
1228 if (!backupXML.isEmpty())
1229 // Write XML Data to disk
1230 saveStringToDisk (bakMapPath,backupXML);
1232 // We would have to save all actions in a tree, to keep track of
1233 // possible redos after a action. Possible, but we are too lazy: forget about redos.
1236 // Write the current state to disk
1237 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1238 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1239 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1240 undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
1241 undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
1242 undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
1243 undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
1244 undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
1245 undoSet.setEntry (QString("/history/version"),vymVersion);
1246 undoSet.writeSettings(histPath);
1250 // TODO remove after testing
1251 //cout << " into="<< histPath.toStdString()<<endl;
1252 cout << " stepsTotal="<<stepsTotal<<
1253 ", undosAvail="<<undosAvail<<
1254 ", redosAvail="<<redosAvail<<
1255 ", curStep="<<curStep<<endl;
1256 cout << " ---------------------------"<<endl;
1257 cout << " comment="<<comment.toStdString()<<endl;
1258 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1259 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1260 cout << " redoCom="<<redoCom.toStdString()<<endl;
1261 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1262 if (saveSel) cout << " saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
1263 cout << " ---------------------------"<<endl;
1266 mainWindow->updateHistory (undoSet);
1272 void VymModel::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
1274 // save the selected part of the map, Undo will replace part of map
1275 QString undoSelection="";
1277 undoSelection=getSelectString(undoSel);
1279 qWarning ("VymModel::saveStateChangingPart no undoSel given!");
1280 QString redoSelection="";
1282 redoSelection=getSelectString(undoSel);
1284 qWarning ("VymModel::saveStateChangingPart no redoSel given!");
1287 saveState (PartOfMap,
1288 undoSelection, "addMapReplace (\"PATH\")",
1294 void VymModel::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
1298 qWarning ("VymModel::saveStateRemovingPart no redoSel given!");
1301 QString undoSelection=getSelectString (redoSel->getParObj());
1302 QString redoSelection=getSelectString(redoSel);
1303 if (typeid(*redoSel) == typeid(BranchObj) )
1305 // save the selected branch of the map, Undo will insert part of map
1306 saveState (PartOfMap,
1307 undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
1308 redoSelection, "delete ()",
1315 void VymModel::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment)
1317 // "Normal" savestate: save commands, selections and comment
1318 // so just save commands for undo and redo
1319 // and use current selection
1321 QString redoSelection="";
1322 if (redoSel) redoSelection=getSelectString(redoSel);
1323 QString undoSelection="";
1324 if (undoSel) undoSelection=getSelectString(undoSel);
1326 saveState (UndoCommand,
1333 void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment)
1335 // "Normal" savestate: save commands, selections and comment
1336 // so just save commands for undo and redo
1337 // and use current selection
1338 saveState (UndoCommand,
1345 void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment)
1347 // "Normal" savestate applied to model (no selection needed):
1348 // save commands and comment
1349 saveState (UndoCommand,
1357 QGraphicsScene* VymModel::getScene ()
1362 BranchObj* VymModel::first()
1364 if (mapCenters.count()>0)
1365 return mapCenters.first();
1370 BranchObj* VymModel::next(BranchObj *bo_start)
1373 BranchObj *bo=bo_start;
1376 // Try to find next branch in current MapCenter
1378 if (rbo) return rbo;
1380 // Try to find MapCenter of bo
1381 while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
1383 // Try to find next MapCenter
1384 int i=mapCenters.indexOf ((MapCenterObj*)bo);
1385 if (i+2 > mapCenters.count() || i<0) return NULL;
1386 if (mapCenters.at(i+1)!=bo_start)
1387 return mapCenters.at(i+1);
1392 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
1394 LinkableMapObj *lmo;
1396 for (int i=0;i<mapCenters.count(); i++)
1398 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
1399 if (lmo) return lmo;
1404 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
1406 LinkableMapObj *lmo;
1412 part=s.section(",",0,0);
1414 num=part.right(part.length() - 3);
1415 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
1416 return mapCenters.at(num.toInt() );
1419 for (int i=0; i<mapCenters.count(); i++)
1421 lmo=mapCenters.at(i)->findObjBySelect(s);
1422 if (lmo) return lmo;
1427 LinkableMapObj* VymModel::findID (const QString &s)
1429 LinkableMapObj *lmo;
1430 for (int i=0; i<mapCenters.count(); i++)
1432 lmo=mapCenters.at(i)->findID (s);
1433 if (lmo) return lmo;
1438 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
1442 for (int i=0; i<mapCenters.count(); i++)
1443 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
1447 //////////////////////////////////////////////
1449 //////////////////////////////////////////////
1450 void VymModel::setVersion (const QString &s)
1455 void VymModel::setAuthor (const QString &s)
1458 QString ("setMapAuthor (\"%1\")").arg(author),
1459 QString ("setMapAuthor (\"%1\")").arg(s),
1460 QString ("Set author of map to \"%1\"").arg(s)
1466 QString VymModel::getAuthor()
1471 void VymModel::setComment (const QString &s)
1474 QString ("setMapComment (\"%1\")").arg(comment),
1475 QString ("setMapComment (\"%1\")").arg(s),
1476 QString ("Set comment of map")
1482 QString VymModel::getComment ()
1487 QString VymModel::getDate ()
1489 return QDate::currentDate().toString ("yyyy-MM-dd");
1492 void VymModel::setHeading(const QString &s)
1494 BranchObj *sel=selection.getBranch();
1499 "setHeading (\""+sel->getHeading()+"\")",
1501 "setHeading (\""+s+"\")",
1502 QString("Set heading of %1 to \"%2\"").arg(getObjectName(sel)).arg(s) );
1503 sel->setHeading(s );
1504 TreeItem *ti=selection.getBranchItem();
1508 //FIXME VM ix is wrong ModelIndex below, ix2 is (hopefully) correct:
1509 QModelIndex ix=index( ti->row(), ti->column(), index (0,0,QModelIndex()) );
1510 //FIXME VM testing only cout <<"VM::setHeading s="<<s.toStdString()<<" ti="<<ti<<" r,c=("<<ti->row()<<","<<ti->column()<<")"<<endl;
1511 QModelIndex ix2=index (ti);
1512 emit (dataChanged ( ix2,ix2));
1516 cout << "Warning: VM::setHeading ti==NULL\n";
1519 //cout <<" (r,c)=("<<ix2.row()<<","<<ix2.column()<<")"<<endl;
1523 ensureSelectionVisible();
1527 void VymModel::setHeadingInt(const QString &s)
1529 BranchObj *bo=selection.getBranch();
1535 ensureSelectionVisible();
1539 BranchObj* VymModel::findText (QString s, bool cs)
1541 QTextDocument::FindFlags flags=0;
1542 if (cs) flags=QTextDocument::FindCaseSensitively;
1545 { // Nothing found or new find process
1547 // nothing found, start again
1551 bool searching=true;
1552 bool foundNote=false;
1553 while (searching && !EOFind)
1557 // Searching in Note
1558 if (itFind->getNote().contains(s,cs))
1560 if (selection.single()!=itFind)
1562 selection.select(itFind);
1563 ensureSelectionVisible();
1565 if (textEditor->findText(s,flags))
1571 // Searching in Heading
1572 if (searching && itFind->getHeading().contains (s,cs) )
1574 selection.select(itFind);
1575 ensureSelectionVisible();
1581 itFind=next(itFind);
1582 if (!itFind) EOFind=true;
1584 //cout <<"still searching... "<<qPrintable( itFind->getHeading())<<endl;
1587 return selection.getBranch();
1592 void VymModel::findReset()
1593 { // Necessary if text to find changes during a find process
1600 void VymModel::setScene (QGraphicsScene *s)
1602 cout << "VM::setscene scene="<<s<<endl;
1603 mapScene=s; // FIXME VM should not be necessary anymore, move all occurences to MapEditor
1604 //init(); // Here we have a mapScene set,
1605 // which is (still) needed to create MapCenters
1608 void VymModel::setURL(const QString &url)
1610 BranchObj *bo=selection.getBranch();
1613 QString oldurl=bo->getURL();
1617 QString ("setURL (\"%1\")").arg(oldurl),
1619 QString ("setURL (\"%1\")").arg(url),
1620 QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
1625 ensureSelectionVisible();
1629 QString VymModel::getURL()
1631 BranchObj *bo=selection.getBranch();
1633 return bo->getURL();
1638 QStringList VymModel::getURLs()
1641 BranchObj *bo=selection.getBranch();
1647 if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
1654 void VymModel::linkFloatImageTo(const QString &dstString)
1656 FloatImageObj *fio=selection.getFloatImage();
1659 BranchObj *dst=(BranchObj*)findObjBySelect(dstString);
1660 if (dst && (typeid(*dst)==typeid (BranchObj) ||
1661 typeid(*dst)==typeid (MapCenterObj)))
1663 LinkableMapObj *dstPar=dst->getParObj();
1664 QString parString=getSelectString(dstPar);
1665 QString fioPreSelectString=getSelectString(fio);
1666 QString fioPreParentSelectString=getSelectString (fio->getParObj());
1667 ((BranchObj*)dst)->addFloatImage (fio);
1668 selection.unselect();
1669 ((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
1670 fio=((BranchObj*)dst)->getLastFloatImage();
1673 selection.select(fio);
1675 getSelectString(fio),
1676 QString("linkTo (\"%1\")").arg(fioPreParentSelectString),
1678 QString ("linkTo (\"%1\")").arg(dstString),
1679 QString ("Link floatimage to %1").arg(getObjectName(dst)));
1685 void VymModel::setFrameType(const FrameObj::FrameType &t)
1687 BranchObj *bo=selection.getBranch();
1690 QString s=bo->getFrameTypeName();
1691 bo->setFrameType (t);
1692 saveState (bo, QString("setFrameType (\"%1\")").arg(s),
1693 bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
1699 void VymModel::setFrameType(const QString &s)
1701 BranchObj *bo=selection.getBranch();
1704 saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
1705 bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
1706 bo->setFrameType (s);
1712 void VymModel::setFramePenColor(const QColor &c)
1714 BranchObj *bo=selection.getBranch();
1717 saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
1718 bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
1719 bo->setFramePenColor (c);
1723 void VymModel::setFrameBrushColor(const QColor &c)
1725 BranchObj *bo=selection.getBranch();
1728 saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
1729 bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
1730 bo->setFrameBrushColor (c);
1734 void VymModel::setFramePadding (const int &i)
1736 BranchObj *bo=selection.getBranch();
1739 saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
1740 bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
1741 bo->setFramePadding (i);
1747 void VymModel::setFrameBorderWidth(const int &i)
1749 BranchObj *bo=selection.getBranch();
1752 saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
1753 bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
1754 bo->setFrameBorderWidth (i);
1760 void VymModel::setIncludeImagesVer(bool b)
1762 BranchObj *bo=selection.getBranch();
1765 QString u= b ? "false" : "true";
1766 QString r=!b ? "false" : "true";
1770 QString("setIncludeImagesVertically (%1)").arg(u),
1772 QString("setIncludeImagesVertically (%1)").arg(r),
1773 QString("Include images vertically in %1").arg(getObjectName(bo))
1775 bo->setIncludeImagesVer(b);
1780 void VymModel::setIncludeImagesHor(bool b)
1782 BranchObj *bo=selection.getBranch();
1785 QString u= b ? "false" : "true";
1786 QString r=!b ? "false" : "true";
1790 QString("setIncludeImagesHorizontally (%1)").arg(u),
1792 QString("setIncludeImagesHorizontally (%1)").arg(r),
1793 QString("Include images horizontally in %1").arg(getObjectName(bo))
1795 bo->setIncludeImagesHor(b);
1800 void VymModel::setHideLinkUnselected (bool b)
1802 LinkableMapObj *sel=selection.single();
1804 (selection.type() == Selection::Branch ||
1805 selection.type() == Selection::MapCenter ||
1806 selection.type() == Selection::FloatImage ))
1808 QString u= b ? "false" : "true";
1809 QString r=!b ? "false" : "true";
1813 QString("setHideLinkUnselected (%1)").arg(u),
1815 QString("setHideLinkUnselected (%1)").arg(r),
1816 QString("Hide link of %1 if unselected").arg(getObjectName(sel))
1818 sel->setHideLinkUnselected(b);
1822 void VymModel::setHideExport(bool b)
1824 BranchObj *bo=selection.getBranch();
1827 bo->setHideInExport (b);
1828 QString u= b ? "false" : "true";
1829 QString r=!b ? "false" : "true";
1833 QString ("setHideExport (%1)").arg(u),
1835 QString ("setHideExport (%1)").arg(r),
1836 QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bo)).arg (r)
1841 // FIXME VM needed? scene()->update();
1845 void VymModel::toggleHideExport()
1847 BranchObj *bo=selection.getBranch();
1849 setHideExport ( !bo->hideInExport() );
1853 void VymModel::copy()
1855 LinkableMapObj *sel=selection.single();
1858 if (redosAvail == 0)
1861 QString s=getSelectString(sel);
1862 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel );
1863 curClipboard=curStep;
1866 // Copy also to global clipboard, because we are at last step in history
1867 QString bakMapName(QString("history-%1").arg(curStep));
1868 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1869 copyDir (bakMapDir,clipboardDir );
1871 clipboardEmpty=false;
1877 void VymModel::pasteNoSave(const int &n)
1879 bool old=blockSaveState;
1880 blockSaveState=true;
1881 bool zippedOrg=zipped;
1882 if (redosAvail > 0 || n!=0)
1884 // Use the "historical" buffer
1885 QString bakMapName(QString("history-%1").arg(n));
1886 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1887 load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
1889 // Use the global buffer
1890 load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
1895 void VymModel::paste()
1897 BranchObj *sel=selection.getBranch();
1900 saveStateChangingPart(
1903 QString ("paste (%1)").arg(curClipboard),
1904 QString("Paste to %1").arg( getObjectName(sel))
1911 void VymModel::cut()
1913 LinkableMapObj *sel=selection.single();
1914 if ( sel && (selection.type() == Selection::Branch ||
1915 selection.type()==Selection::MapCenter ||
1916 selection.type()==Selection::FloatImage))
1918 /* No savestate! savestate is called in cutNoSave
1919 saveStateChangingPart(
1923 QString("Cut %1").arg(getObjectName(sel ))
1932 void VymModel::moveBranchUp()
1934 BranchObj* bo=selection.getBranch();
1938 if (!bo->canMoveBranchUp()) return;
1939 par=(BranchObj*)(bo->getParObj());
1940 BranchObj *obo=par->moveBranchUp (bo); // bo will be the one below selection
1941 saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
1943 //FIXME VM needed? scene()->update();
1945 ensureSelectionVisible();
1949 void VymModel::moveBranchDown()
1951 BranchObj* bo=selection.getBranch();
1955 if (!bo->canMoveBranchDown()) return;
1956 par=(BranchObj*)(bo->getParObj());
1957 BranchObj *obo=par->moveBranchDown(bo); // bo will be the one above selection
1958 saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
1960 //FIXME VM needed? scene()->update();
1962 ensureSelectionVisible();
1966 void VymModel::sortChildren()
1968 BranchObj* bo=selection.getBranch();
1971 if(bo->countBranches()>1)
1973 saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
1976 ensureSelectionVisible();
1981 void VymModel::createMapCenter()
1983 MapCenterObj *mco=addMapCenter (QPointF (0,0) );
1984 selection.select (mco);
1987 void VymModel::createBranch()
1989 BranchObj *bo=selection.getBranch();
1992 BranchObj *newbo=addNewBranchInt (-2); // FIXME VM Old model, merge with below
1995 QList<QVariant> cData;
1996 cData << "VM:createBranch" << "undef"<<"undef";
1997 TreeItem *parti=bo->getTreeItem();
1998 TreeItem *ti=new TreeItem (cData,parti);
2000 ti->setType (TreeItem::Branch);
2001 parti->appendChild (ti);
2005 newbo->setTreeItem (ti);
2006 selection.select (newbo); // FIXME VM really needed here?
2011 MapCenterObj* VymModel::addMapCenter ()
2013 MapCenterObj *mco=addMapCenter (contextPos);
2014 cout <<"VM::addMCO () mapScene="<<mapScene<<endl;
2015 selection.select (mco);
2017 ensureSelectionVisible();
2022 QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
2023 QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
2028 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
2030 MapCenterObj *mapCenter = new MapCenterObj(mapScene,this);
2031 mapCenter->setMapEditor(mapEditor); //FIXME VM needed to get defLinkStyle, mapLinkColorHint ... for later added objects
2032 mapCenter->move (absPos);
2033 mapCenter->setVisibility (true);
2034 mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
2035 mapCenters.append(mapCenter);
2038 QList<QVariant> cData;
2039 cData << "VM:addMapCenter" << "undef"<<"undef";
2040 TreeItem *ti=new TreeItem (cData,rootItem);
2041 ti->setLMO (mapCenter);
2042 ti->setType (TreeItem::MapCenter);
2043 mapCenter->setTreeItem (ti);
2044 rootItem->appendChild (ti);
2049 MapCenterObj* VymModel::removeMapCenter(MapCenterObj* mco)
2051 int i=mapCenters.indexOf (mco);
2054 mapCenters.removeAt (i);
2056 if (i>0) return mapCenters.at(i-1); // Return previous MCO
2061 MapCenterObj* VymModel::getLastMapCenter()
2063 if (mapCenters.size()>0)
2064 return mapCenters.last();
2071 BranchObj* VymModel::addNewBranchInt(int num)
2073 // Depending on pos:
2074 // -3 insert in children of parent above selection
2075 // -2 add branch to selection
2076 // -1 insert in children of parent below selection
2077 // 0..n insert in children of parent at pos
2078 BranchObj *newbo=NULL;
2079 BranchObj *bo=selection.getBranch();
2084 // save scroll state. If scrolled, automatically select
2085 // new branch in order to tmp unscroll parent...
2086 newbo=bo->addBranch();
2091 bo=(BranchObj*)bo->getParObj();
2092 if (bo) newbo=bo->insertBranch(num);
2096 bo=(BranchObj*)bo->getParObj();
2097 if (bo) newbo=bo->insertBranch(num);
2099 if (!newbo) return NULL;
2104 BranchObj* VymModel::addNewBranch(int pos)
2106 // Different meaning than num in addNewBranchInt!
2110 BranchObj *bo = selection.getBranch();
2111 BranchObj *newbo=NULL;
2115 // FIXME VM do we still need this in model? setCursor (Qt::ArrowCursor);
2117 newbo=addNewBranchInt (pos-2);
2125 QString ("addBranch (%1)").arg(pos),
2126 QString ("Add new branch to %1").arg(getObjectName(bo)));
2130 latestSelectionString=getSelectString(newbo);
2131 // In Network mode, the client needs to know where the new branch is,
2132 // so we have to pass on this information via saveState.
2133 // TODO: Get rid of this positioning workaround
2134 QString ps=qpointfToString (newbo->getAbsPos());
2135 sendData ("selectLatestAdded ()");
2136 sendData (QString("move %1").arg(ps));
2144 BranchObj* VymModel::addNewBranchBefore()
2146 BranchObj *newbo=NULL;
2147 BranchObj *bo = selection.getBranch();
2148 if (bo && selection.type()==Selection::Branch)
2149 // We accept no MapCenterObj here, so we _have_ a parent
2151 QPointF p=bo->getRelPos();
2154 BranchObj *parbo=(BranchObj*)(bo->getParObj());
2156 // add below selection
2157 newbo=parbo->insertBranch(bo->getNum()+1);
2160 newbo->move2RelPos (p);
2162 // Move selection to new branch
2163 bo->linkTo (newbo,-1);
2165 saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()",
2166 QString ("Add branch before %1").arg(getObjectName(bo)));
2172 latestSelectionString=selection.getSelectString();
2176 void VymModel::deleteSelection()
2178 BranchObj *bo = selection.getBranch();
2180 if (bo && selection.type()==Selection::MapCenter)
2182 // BranchObj* par=(BranchObj*)(bo->getParObj());
2183 selection.unselect();
2184 /* FIXME VM Note: does saveStateRemovingPart work for MCO? (No parent!)
2185 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2187 bo=removeMapCenter ((MapCenterObj*)bo);
2190 selection.select (bo);
2191 ensureSelectionVisible();
2197 if (bo && selection.type()==Selection::Branch)
2199 QModelIndex ix=selection.getBranchIndex();
2202 cout << "VM::deleteSelection\n";
2205 BranchObj* par=(BranchObj*)bo->getParObj();
2206 selection.unselect();
2207 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2208 par->removeBranch(bo);
2209 selection.select (par);
2210 ensureSelectionVisible();
2215 FloatImageObj *fio=selection.getFloatImage();
2218 BranchObj* par=(BranchObj*)fio->getParObj();
2219 saveStateChangingPart(
2223 QString("Delete %1").arg(getObjectName(fio))
2225 selection.unselect();
2226 par->removeFloatImage(fio);
2227 selection.select (par);
2230 ensureSelectionVisible();
2235 void VymModel::deleteKeepChildren()
2237 BranchObj *bo=selection.getBranch();
2241 par=(BranchObj*)(bo->getParObj());
2243 // Don't use this on mapcenter
2246 // Check if we have childs at all to keep
2247 if (bo->countBranches()==0)
2253 QPointF p=bo->getRelPos();
2254 saveStateChangingPart(
2257 "deleteKeepChildren ()",
2258 QString("Remove %1 and keep its children").arg(getObjectName(bo))
2261 QString sel=getSelectString(bo);
2263 par->removeBranchHere(bo);
2266 selection.getBranch()->move2RelPos (p);
2271 void VymModel::deleteChildren()
2273 BranchObj *bo=selection.getBranch();
2276 saveStateChangingPart(
2279 "deleteChildren ()",
2280 QString( "Remove children of branch %1").arg(getObjectName(bo))
2282 bo->removeChildren();
2288 bool VymModel::scrollBranch(BranchObj *bo)
2292 if (bo->isScrolled()) return false;
2293 if (bo->countBranches()==0) return false;
2294 if (bo->getDepth()==0) return false;
2300 QString ("%1 ()").arg(u),
2302 QString ("%1 ()").arg(r),
2303 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2307 // FIXME VM needed? scene()->update();
2313 bool VymModel::unscrollBranch(BranchObj *bo)
2317 if (!bo->isScrolled()) return false;
2318 if (bo->countBranches()==0) return false;
2319 if (bo->getDepth()==0) return false;
2325 QString ("%1 ()").arg(u),
2327 QString ("%1 ()").arg(r),
2328 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2332 // FIXME VM needed? scene()->update();
2338 void VymModel::toggleScroll()
2340 BranchObj *bo=selection.getBranch();
2341 if (selection.type()==Selection::Branch )
2343 if (bo->isScrolled())
2344 unscrollBranch (bo);
2350 void VymModel::unscrollChildren()
2352 BranchObj *bo=selection.getBranch();
2358 if (bo->isScrolled()) unscrollBranch (bo);
2363 void VymModel::addFloatImage (const QPixmap &img)
2365 BranchObj *bo=selection.getBranch();
2368 FloatImageObj *fio=bo->addFloatImage();
2370 fio->setOriginalFilename("No original filename (image added by dropevent)");
2371 QString s=getSelectString(bo);
2372 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio );
2373 saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
2375 // FIXME VM needed? scene()->update();
2380 void VymModel::colorBranch (QColor c)
2382 BranchObj *bo=selection.getBranch();
2387 QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
2389 QString ("colorBranch (\"%1\")").arg(c.name()),
2390 QString("Set color of %1 to %2").arg(getObjectName(bo)).arg(c.name())
2392 bo->setColor(c); // color branch
2396 void VymModel::colorSubtree (QColor c)
2398 BranchObj *bo=selection.getBranch();
2401 saveStateChangingPart(
2404 QString ("colorSubtree (\"%1\")").arg(c.name()),
2405 QString ("Set color of %1 and children to %2").arg(getObjectName(bo)).arg(c.name())
2407 bo->setColorSubtree (c); // color links, color children
2411 QColor VymModel::getCurrentHeadingColor()
2413 BranchObj *bo=selection.getBranch();
2414 if (bo) return bo->getColor();
2416 QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
2422 void VymModel::editURL()
2424 BranchObj *bo=selection.getBranch();
2428 QString text = QInputDialog::getText(
2429 "VYM", tr("Enter URL:"), QLineEdit::Normal,
2430 bo->getURL(), &ok, NULL);
2432 // user entered something and pressed OK
2437 void VymModel::editLocalURL()
2439 BranchObj *bo=selection.getBranch();
2442 QStringList filters;
2443 filters <<"All files (*)";
2444 filters << tr("Text","Filedialog") + " (*.txt)";
2445 filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
2446 filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
2447 filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
2448 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
2449 fd->setFilters (filters);
2450 fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
2451 fd->setDirectory (lastFileDir);
2452 if (! bo->getVymLink().isEmpty() )
2453 fd->selectFile( bo->getURL() );
2456 if ( fd->exec() == QDialog::Accepted )
2458 lastFileDir=QDir (fd->directory().path());
2459 setURL (fd->selectedFile() );
2465 void VymModel::editHeading2URL()
2467 BranchObj *bo=selection.getBranch();
2469 setURL (bo->getHeading());
2472 void VymModel::editBugzilla2URL()
2474 BranchObj *bo=selection.getBranch();
2477 QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
2482 void VymModel::editFATE2URL()
2484 BranchObj *bo=selection.getBranch();
2487 QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
2490 "setURL (\""+bo->getURL()+"\")",
2492 "setURL (\""+url+"\")",
2493 QString("Use heading of %1 as link to FATE").arg(getObjectName(bo))
2500 void VymModel::editVymLink()
2502 BranchObj *bo=selection.getBranch();
2505 QStringList filters;
2506 filters <<"VYM map (*.vym)";
2507 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
2508 fd->setFilters (filters);
2509 fd->setCaption(vymName+" - " +tr("Link to another map"));
2510 fd->setDirectory (lastFileDir);
2511 if (! bo->getVymLink().isEmpty() )
2512 fd->selectFile( bo->getVymLink() );
2516 if ( fd->exec() == QDialog::Accepted )
2518 lastFileDir=QDir (fd->directory().path());
2521 "setVymLink (\""+bo->getVymLink()+"\")",
2523 "setVymLink (\""+fd->selectedFile()+"\")",
2524 QString("Set vymlink of %1 to %2").arg(getObjectName(bo)).arg(fd->selectedFile())
2526 setVymLink (fd->selectedFile() ); // FIXME ok?
2531 void VymModel::setVymLink (const QString &s)
2533 // Internal function, no saveState needed
2534 BranchObj *bo=selection.getBranch();
2541 ensureSelectionVisible();
2545 void VymModel::deleteVymLink()
2547 BranchObj *bo=selection.getBranch();
2552 "setVymLink (\""+bo->getVymLink()+"\")",
2554 "setVymLink (\"\")",
2555 QString("Unset vymlink of %1").arg(getObjectName(bo))
2557 bo->setVymLink ("" );
2560 // FIXME VM needed? scene()->update();
2564 QString VymModel::getVymLink()
2566 BranchObj *bo=selection.getBranch();
2568 return bo->getVymLink();
2574 QStringList VymModel::getVymLinks()
2577 BranchObj *bo=selection.getBranch();
2583 if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
2591 void VymModel::followXLink(int i)
2593 BranchObj *bo=selection.getBranch();
2596 bo=bo->XLinkTargetAt(i);
2599 selection.select(bo);
2600 ensureSelectionVisible();
2605 void VymModel::editXLink(int i) // FIXME VM missing saveState
2607 BranchObj *bo=selection.getBranch();
2610 XLinkObj *xlo=bo->XLinkAt(i);
2613 EditXLinkDialog dia;
2615 dia.setSelection(bo);
2616 if (dia.exec() == QDialog::Accepted)
2618 if (dia.useSettingsGlobal() )
2620 setMapDefXLinkColor (xlo->getColor() );
2621 setMapDefXLinkWidth (xlo->getWidth() );
2623 if (dia.deleteXLink())
2624 bo->deleteXLinkAt(i);
2634 //////////////////////////////////////////////
2636 //////////////////////////////////////////////
2638 void VymModel::parseAtom(const QString &atom)
2640 BranchObj *selb=selection.getBranch();
2646 // Split string s into command and parameters
2647 parser.parseAtom (atom);
2648 QString com=parser.getCommand();
2650 // External commands
2651 /////////////////////////////////////////////////////////////////////
2652 if (com=="addBranch")
2654 if (selection.isEmpty())
2656 parser.setError (Aborted,"Nothing selected");
2659 parser.setError (Aborted,"Type of selection is not a branch");
2664 if (parser.checkParCount(pl))
2666 if (parser.parCount()==0)
2670 n=parser.parInt (ok,0);
2671 if (ok ) addNewBranch (n);
2675 /////////////////////////////////////////////////////////////////////
2676 } else if (com=="addBranchBefore")
2678 if (selection.isEmpty())
2680 parser.setError (Aborted,"Nothing selected");
2683 parser.setError (Aborted,"Type of selection is not a branch");
2686 if (parser.parCount()==0)
2688 addNewBranchBefore ();
2691 /////////////////////////////////////////////////////////////////////
2692 } else if (com==QString("addMapCenter"))
2694 if (parser.checkParCount(2))
2696 x=parser.parDouble (ok,0);
2699 y=parser.parDouble (ok,1);
2700 if (ok) addMapCenter (QPointF(x,y));
2703 /////////////////////////////////////////////////////////////////////
2704 } else if (com==QString("addMapReplace"))
2706 if (selection.isEmpty())
2708 parser.setError (Aborted,"Nothing selected");
2711 parser.setError (Aborted,"Type of selection is not a branch");
2712 } else if (parser.checkParCount(1))
2714 //s=parser.parString (ok,0); // selection
2715 t=parser.parString (ok,0); // path to map
2716 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2717 addMapReplaceInt(getSelectString(selb),t);
2719 /////////////////////////////////////////////////////////////////////
2720 } else if (com==QString("addMapInsert"))
2722 if (selection.isEmpty())
2724 parser.setError (Aborted,"Nothing selected");
2727 parser.setError (Aborted,"Type of selection is not a branch");
2730 if (parser.checkParCount(2))
2732 t=parser.parString (ok,0); // path to map
2733 n=parser.parInt(ok,1); // position
2734 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2735 addMapInsertInt(t,n);
2738 /////////////////////////////////////////////////////////////////////
2739 } else if (com=="clearFlags")
2741 if (selection.isEmpty() )
2743 parser.setError (Aborted,"Nothing selected");
2746 parser.setError (Aborted,"Type of selection is not a branch");
2747 } else if (parser.checkParCount(0))
2749 selb->clearStandardFlags();
2750 selb->updateFlagsToolbar();
2752 /////////////////////////////////////////////////////////////////////
2753 } else if (com=="colorBranch")
2755 if (selection.isEmpty())
2757 parser.setError (Aborted,"Nothing selected");
2760 parser.setError (Aborted,"Type of selection is not a branch");
2761 } else if (parser.checkParCount(1))
2763 QColor c=parser.parColor (ok,0);
2764 if (ok) colorBranch (c);
2766 /////////////////////////////////////////////////////////////////////
2767 } else if (com=="colorSubtree")
2769 if (selection.isEmpty())
2771 parser.setError (Aborted,"Nothing selected");
2774 parser.setError (Aborted,"Type of selection is not a branch");
2775 } else if (parser.checkParCount(1))
2777 QColor c=parser.parColor (ok,0);
2778 if (ok) colorSubtree (c);
2780 /////////////////////////////////////////////////////////////////////
2781 } else if (com=="copy")
2783 if (selection.isEmpty())
2785 parser.setError (Aborted,"Nothing selected");
2788 parser.setError (Aborted,"Type of selection is not a branch");
2789 } else if (parser.checkParCount(0))
2791 //FIXME missing action for copy
2793 /////////////////////////////////////////////////////////////////////
2794 } else if (com=="cut")
2796 if (selection.isEmpty())
2798 parser.setError (Aborted,"Nothing selected");
2799 } else if ( selection.type()!=Selection::Branch &&
2800 selection.type()!=Selection::MapCenter &&
2801 selection.type()!=Selection::FloatImage )
2803 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2804 } else if (parser.checkParCount(0))
2808 /////////////////////////////////////////////////////////////////////
2809 } else if (com=="delete")
2811 if (selection.isEmpty())
2813 parser.setError (Aborted,"Nothing selected");
2815 /*else if (selection.type() != Selection::Branch && selection.type() != Selection::FloatImage )
2817 parser.setError (Aborted,"Type of selection is wrong.");
2820 else if (parser.checkParCount(0))
2824 /////////////////////////////////////////////////////////////////////
2825 } else if (com=="deleteKeepChildren")
2827 if (selection.isEmpty())
2829 parser.setError (Aborted,"Nothing selected");
2832 parser.setError (Aborted,"Type of selection is not a branch");
2833 } else if (parser.checkParCount(0))
2835 deleteKeepChildren();
2837 /////////////////////////////////////////////////////////////////////
2838 } else if (com=="deleteChildren")
2840 if (selection.isEmpty())
2842 parser.setError (Aborted,"Nothing selected");
2845 parser.setError (Aborted,"Type of selection is not a branch");
2846 } else if (parser.checkParCount(0))
2850 /////////////////////////////////////////////////////////////////////
2851 } else if (com=="exportASCII")
2855 if (parser.parCount()>=1)
2856 // Hey, we even have a filename
2857 fname=parser.parString(ok,0);
2860 parser.setError (Aborted,"Could not read filename");
2863 exportASCII (fname,false);
2865 /////////////////////////////////////////////////////////////////////
2866 } else if (com=="exportImage")
2870 if (parser.parCount()>=2)
2871 // Hey, we even have a filename
2872 fname=parser.parString(ok,0);
2875 parser.setError (Aborted,"Could not read filename");
2878 QString format="PNG";
2879 if (parser.parCount()>=2)
2881 format=parser.parString(ok,1);
2883 exportImage (fname,false,format);
2885 /////////////////////////////////////////////////////////////////////
2886 } else if (com=="exportXHTML")
2890 if (parser.parCount()>=2)
2891 // Hey, we even have a filename
2892 fname=parser.parString(ok,1);
2895 parser.setError (Aborted,"Could not read filename");
2898 exportXHTML (fname,false);
2900 /////////////////////////////////////////////////////////////////////
2901 } else if (com=="exportXML")
2905 if (parser.parCount()>=2)
2906 // Hey, we even have a filename
2907 fname=parser.parString(ok,1);
2910 parser.setError (Aborted,"Could not read filename");
2913 exportXML (fname,false);
2915 /////////////////////////////////////////////////////////////////////
2916 } else if (com=="importDir")
2918 if (selection.isEmpty())
2920 parser.setError (Aborted,"Nothing selected");
2923 parser.setError (Aborted,"Type of selection is not a branch");
2924 } else if (parser.checkParCount(1))
2926 s=parser.parString(ok,0);
2927 if (ok) importDirInt(s);
2929 /////////////////////////////////////////////////////////////////////
2930 } else if (com=="linkTo")
2932 if (selection.isEmpty())
2934 parser.setError (Aborted,"Nothing selected");
2937 if (parser.checkParCount(4))
2939 // 0 selectstring of parent
2940 // 1 num in parent (for branches)
2941 // 2,3 x,y of mainbranch or mapcenter
2942 s=parser.parString(ok,0);
2943 LinkableMapObj *dst=findObjBySelect (s);
2946 if (typeid(*dst) == typeid(BranchObj) )
2948 // Get number in parent
2949 n=parser.parInt (ok,1);
2952 selb->linkTo ((BranchObj*)(dst),n);
2955 } else if (typeid(*dst) == typeid(MapCenterObj) )
2957 selb->linkTo ((BranchObj*)(dst),-1);
2958 // Get coordinates of mainbranch
2959 x=parser.parDouble(ok,2);
2962 y=parser.parDouble(ok,3);
2972 } else if ( selection.type() == Selection::FloatImage)
2974 if (parser.checkParCount(1))
2976 // 0 selectstring of parent
2977 s=parser.parString(ok,0);
2978 LinkableMapObj *dst=findObjBySelect (s);
2981 if (typeid(*dst) == typeid(BranchObj) ||
2982 typeid(*dst) == typeid(MapCenterObj))
2983 linkFloatImageTo (getSelectString(dst));
2985 parser.setError (Aborted,"Destination is not a branch");
2988 parser.setError (Aborted,"Type of selection is not a floatimage or branch");
2989 /////////////////////////////////////////////////////////////////////
2990 } else if (com=="loadImage")
2992 if (selection.isEmpty())
2994 parser.setError (Aborted,"Nothing selected");
2997 parser.setError (Aborted,"Type of selection is not a branch");
2998 } else if (parser.checkParCount(1))
3000 s=parser.parString(ok,0);
3001 if (ok) loadFloatImageInt (s);
3003 /////////////////////////////////////////////////////////////////////
3004 } else if (com=="moveBranchUp")
3006 if (selection.isEmpty() )
3008 parser.setError (Aborted,"Nothing selected");
3011 parser.setError (Aborted,"Type of selection is not a branch");
3012 } else if (parser.checkParCount(0))
3016 /////////////////////////////////////////////////////////////////////
3017 } else if (com=="moveBranchDown")
3019 if (selection.isEmpty() )
3021 parser.setError (Aborted,"Nothing selected");
3024 parser.setError (Aborted,"Type of selection is not a branch");
3025 } else if (parser.checkParCount(0))
3029 /////////////////////////////////////////////////////////////////////
3030 } else if (com=="move")
3032 if (selection.isEmpty() )
3034 parser.setError (Aborted,"Nothing selected");
3035 } else if ( selection.type()!=Selection::Branch &&
3036 selection.type()!=Selection::MapCenter &&
3037 selection.type()!=Selection::FloatImage )
3039 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3040 } else if (parser.checkParCount(2))
3042 x=parser.parDouble (ok,0);
3045 y=parser.parDouble (ok,1);
3049 /////////////////////////////////////////////////////////////////////
3050 } else if (com=="moveRel")
3052 if (selection.isEmpty() )
3054 parser.setError (Aborted,"Nothing selected");
3055 } else if ( selection.type()!=Selection::Branch &&
3056 selection.type()!=Selection::MapCenter &&
3057 selection.type()!=Selection::FloatImage )
3059 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3060 } else if (parser.checkParCount(2))
3062 x=parser.parDouble (ok,0);
3065 y=parser.parDouble (ok,1);
3066 if (ok) moveRel (x,y);
3069 /////////////////////////////////////////////////////////////////////
3070 } else if (com=="nop")
3072 /////////////////////////////////////////////////////////////////////
3073 } else if (com=="paste")
3075 if (selection.isEmpty() )
3077 parser.setError (Aborted,"Nothing selected");
3080 parser.setError (Aborted,"Type of selection is not a branch");
3081 } else if (parser.checkParCount(1))
3083 n=parser.parInt (ok,0);
3084 if (ok) pasteNoSave(n);
3086 /////////////////////////////////////////////////////////////////////
3087 } else if (com=="qa")
3089 if (selection.isEmpty() )
3091 parser.setError (Aborted,"Nothing selected");
3094 parser.setError (Aborted,"Type of selection is not a branch");
3095 } else if (parser.checkParCount(4))
3098 c=parser.parString (ok,0);
3101 parser.setError (Aborted,"No comment given");
3104 s=parser.parString (ok,1);
3107 parser.setError (Aborted,"First parameter is not a string");
3110 t=parser.parString (ok,2);
3113 parser.setError (Aborted,"Condition is not a string");
3116 u=parser.parString (ok,3);
3119 parser.setError (Aborted,"Third parameter is not a string");
3124 parser.setError (Aborted,"Unknown type: "+s);
3129 parser.setError (Aborted,"Unknown operator: "+t);
3134 parser.setError (Aborted,"Type of selection is not a branch");
3137 if (selb->getHeading() == u)
3139 cout << "PASSED: " << qPrintable (c) << endl;
3142 cout << "FAILED: " << qPrintable (c) << endl;
3152 /////////////////////////////////////////////////////////////////////
3153 } else if (com=="saveImage")
3155 FloatImageObj *fio=selection.getFloatImage();
3158 parser.setError (Aborted,"Type of selection is not an image");
3159 } else if (parser.checkParCount(2))
3161 s=parser.parString(ok,0);
3164 t=parser.parString(ok,1);
3165 if (ok) saveFloatImageInt (fio,t,s);
3168 /////////////////////////////////////////////////////////////////////
3169 } else if (com=="scroll")
3171 if (selection.isEmpty() )
3173 parser.setError (Aborted,"Nothing selected");
3176 parser.setError (Aborted,"Type of selection is not a branch");
3177 } else if (parser.checkParCount(0))
3179 if (!scrollBranch (selb))
3180 parser.setError (Aborted,"Could not scroll branch");
3182 /////////////////////////////////////////////////////////////////////
3183 } else if (com=="select")
3185 if (parser.checkParCount(1))
3187 s=parser.parString(ok,0);
3190 /////////////////////////////////////////////////////////////////////
3191 } else if (com=="selectLastBranch")
3193 if (selection.isEmpty() )
3195 parser.setError (Aborted,"Nothing selected");
3198 parser.setError (Aborted,"Type of selection is not a branch");
3199 } else if (parser.checkParCount(0))
3201 BranchObj *bo=selb->getLastBranch();
3203 parser.setError (Aborted,"Could not select last branch");
3207 /////////////////////////////////////////////////////////////////////
3208 } else if (com=="selectLastImage")
3210 if (selection.isEmpty() )
3212 parser.setError (Aborted,"Nothing selected");
3215 parser.setError (Aborted,"Type of selection is not a branch");
3216 } else if (parser.checkParCount(0))
3218 FloatImageObj *fio=selb->getLastFloatImage();
3220 parser.setError (Aborted,"Could not select last image");
3224 /////////////////////////////////////////////////////////////////////
3225 } else if (com=="selectLatestAdded")
3227 if (latestSelectionString.isEmpty() )
3229 parser.setError (Aborted,"No latest added object");
3232 if (!select (latestSelectionString))
3233 parser.setError (Aborted,"Could not select latest added object "+latestSelectionString);
3235 /////////////////////////////////////////////////////////////////////
3236 } else if (com=="setFrameType")
3238 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3240 parser.setError (Aborted,"Type of selection does not allow setting frame type");
3242 else if (parser.checkParCount(1))
3244 s=parser.parString(ok,0);
3245 if (ok) setFrameType (s);
3247 /////////////////////////////////////////////////////////////////////
3248 } else if (com=="setFramePenColor")
3250 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3252 parser.setError (Aborted,"Type of selection does not allow setting of pen color");
3254 else if (parser.checkParCount(1))
3256 QColor c=parser.parColor(ok,0);
3257 if (ok) setFramePenColor (c);
3259 /////////////////////////////////////////////////////////////////////
3260 } else if (com=="setFrameBrushColor")
3262 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3264 parser.setError (Aborted,"Type of selection does not allow setting brush color");
3266 else if (parser.checkParCount(1))
3268 QColor c=parser.parColor(ok,0);
3269 if (ok) setFrameBrushColor (c);
3271 /////////////////////////////////////////////////////////////////////
3272 } else if (com=="setFramePadding")
3274 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3276 parser.setError (Aborted,"Type of selection does not allow setting frame padding");
3278 else if (parser.checkParCount(1))
3280 n=parser.parInt(ok,0);
3281 if (ok) setFramePadding(n);
3283 /////////////////////////////////////////////////////////////////////
3284 } else if (com=="setFrameBorderWidth")
3286 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3288 parser.setError (Aborted,"Type of selection does not allow setting frame border width");
3290 else if (parser.checkParCount(1))
3292 n=parser.parInt(ok,0);
3293 if (ok) setFrameBorderWidth (n);
3295 /////////////////////////////////////////////////////////////////////
3296 } else if (com=="setMapAuthor")
3298 if (parser.checkParCount(1))
3300 s=parser.parString(ok,0);
3301 if (ok) setAuthor (s);
3303 /////////////////////////////////////////////////////////////////////
3304 } else if (com=="setMapComment")
3306 if (parser.checkParCount(1))
3308 s=parser.parString(ok,0);
3309 if (ok) setComment(s);
3311 /////////////////////////////////////////////////////////////////////
3312 } else if (com=="setMapBackgroundColor")
3314 if (selection.isEmpty() )
3316 parser.setError (Aborted,"Nothing selected");
3317 } else if (! selection.getBranch() )
3319 parser.setError (Aborted,"Type of selection is not a branch");
3320 } else if (parser.checkParCount(1))
3322 QColor c=parser.parColor (ok,0);
3323 if (ok) setMapBackgroundColor (c);
3325 /////////////////////////////////////////////////////////////////////
3326 } else if (com=="setMapDefLinkColor")
3328 if (selection.isEmpty() )
3330 parser.setError (Aborted,"Nothing selected");
3333 parser.setError (Aborted,"Type of selection is not a branch");
3334 } else if (parser.checkParCount(1))
3336 QColor c=parser.parColor (ok,0);
3337 if (ok) setMapDefLinkColor (c);
3339 /////////////////////////////////////////////////////////////////////
3340 } else if (com=="setMapLinkStyle")
3342 if (parser.checkParCount(1))
3344 s=parser.parString (ok,0);
3345 if (ok) setMapLinkStyle(s);
3347 /////////////////////////////////////////////////////////////////////
3348 } else if (com=="setHeading")
3350 if (selection.isEmpty() )
3352 parser.setError (Aborted,"Nothing selected");
3355 parser.setError (Aborted,"Type of selection is not a branch");
3356 } else if (parser.checkParCount(1))
3358 s=parser.parString (ok,0);
3362 /////////////////////////////////////////////////////////////////////
3363 } else if (com=="setHideExport")
3365 if (selection.isEmpty() )
3367 parser.setError (Aborted,"Nothing selected");
3368 } else if (selection.type()!=Selection::Branch && selection.type() != Selection::MapCenter &&selection.type()!=Selection::FloatImage)
3370 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3371 } else if (parser.checkParCount(1))
3373 b=parser.parBool(ok,0);
3374 if (ok) setHideExport (b);
3376 /////////////////////////////////////////////////////////////////////
3377 } else if (com=="setIncludeImagesHorizontally")
3379 if (selection.isEmpty() )
3381 parser.setError (Aborted,"Nothing selected");
3384 parser.setError (Aborted,"Type of selection is not a branch");
3385 } else if (parser.checkParCount(1))
3387 b=parser.parBool(ok,0);
3388 if (ok) setIncludeImagesHor(b);
3390 /////////////////////////////////////////////////////////////////////
3391 } else if (com=="setIncludeImagesVertically")
3393 if (selection.isEmpty() )
3395 parser.setError (Aborted,"Nothing selected");
3398 parser.setError (Aborted,"Type of selection is not a branch");
3399 } else if (parser.checkParCount(1))
3401 b=parser.parBool(ok,0);
3402 if (ok) setIncludeImagesVer(b);
3404 /////////////////////////////////////////////////////////////////////
3405 } else if (com=="setHideLinkUnselected")
3407 if (selection.isEmpty() )
3409 parser.setError (Aborted,"Nothing selected");
3410 } else if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3412 parser.setError (Aborted,"Type of selection does not allow hiding the link");
3413 } else if (parser.checkParCount(1))
3415 b=parser.parBool(ok,0);
3416 if (ok) setHideLinkUnselected(b);
3418 /////////////////////////////////////////////////////////////////////
3419 } else if (com=="setSelectionColor")
3421 if (parser.checkParCount(1))
3423 QColor c=parser.parColor (ok,0);
3424 if (ok) setSelectionColorInt (c);
3426 /////////////////////////////////////////////////////////////////////
3427 } else if (com=="setURL")
3429 if (selection.isEmpty() )
3431 parser.setError (Aborted,"Nothing selected");
3434 parser.setError (Aborted,"Type of selection is not a branch");
3435 } else if (parser.checkParCount(1))
3437 s=parser.parString (ok,0);
3440 /////////////////////////////////////////////////////////////////////
3441 } else if (com=="setVymLink")
3443 if (selection.isEmpty() )
3445 parser.setError (Aborted,"Nothing selected");
3448 parser.setError (Aborted,"Type of selection is not a branch");
3449 } else if (parser.checkParCount(1))
3451 s=parser.parString (ok,0);
3452 if (ok) setVymLink(s);
3455 /////////////////////////////////////////////////////////////////////
3456 else if (com=="setFlag")
3458 if (selection.isEmpty() )
3460 parser.setError (Aborted,"Nothing selected");
3463 parser.setError (Aborted,"Type of selection is not a branch");
3464 } else if (parser.checkParCount(1))
3466 s=parser.parString(ok,0);
3469 selb->activateStandardFlag(s);
3470 selb->updateFlagsToolbar();
3473 /////////////////////////////////////////////////////////////////////
3474 } else if (com=="setFrameType")
3476 if (selection.isEmpty() )
3478 parser.setError (Aborted,"Nothing selected");
3481 parser.setError (Aborted,"Type of selection is not a branch");
3482 } else if (parser.checkParCount(1))
3484 s=parser.parString(ok,0);
3488 /////////////////////////////////////////////////////////////////////
3489 } else if (com=="sortChildren")
3491 if (selection.isEmpty() )
3493 parser.setError (Aborted,"Nothing selected");
3496 parser.setError (Aborted,"Type of selection is not a branch");
3497 } else if (parser.checkParCount(0))
3501 /////////////////////////////////////////////////////////////////////
3502 } else if (com=="toggleFlag")
3504 if (selection.isEmpty() )
3506 parser.setError (Aborted,"Nothing selected");
3509 parser.setError (Aborted,"Type of selection is not a branch");
3510 } else if (parser.checkParCount(1))
3512 s=parser.parString(ok,0);
3515 selb->toggleStandardFlag(s);
3516 selb->updateFlagsToolbar();
3519 /////////////////////////////////////////////////////////////////////
3520 } else if (com=="unscroll")
3522 if (selection.isEmpty() )
3524 parser.setError (Aborted,"Nothing selected");
3527 parser.setError (Aborted,"Type of selection is not a branch");
3528 } else if (parser.checkParCount(0))
3530 if (!unscrollBranch (selb))
3531 parser.setError (Aborted,"Could not unscroll branch");
3533 /////////////////////////////////////////////////////////////////////
3534 } else if (com=="unscrollChildren")
3536 if (selection.isEmpty() )
3538 parser.setError (Aborted,"Nothing selected");
3541 parser.setError (Aborted,"Type of selection is not a branch");
3542 } else if (parser.checkParCount(0))
3544 unscrollChildren ();
3546 /////////////////////////////////////////////////////////////////////
3547 } else if (com=="unsetFlag")
3549 if (selection.isEmpty() )
3551 parser.setError (Aborted,"Nothing selected");
3554 parser.setError (Aborted,"Type of selection is not a branch");
3555 } else if (parser.checkParCount(1))
3557 s=parser.parString(ok,0);
3560 selb->deactivateStandardFlag(s);
3561 selb->updateFlagsToolbar();
3565 parser.setError (Aborted,"Unknown command");
3568 if (parser.errorLevel()==NoError)
3570 // setChanged(); FIXME should not be called e.g. for export?!
3575 // TODO Error handling
3576 qWarning("VymModel::parseAtom: Error!");
3577 qWarning(parser.errorMessage());
3581 void VymModel::runScript (QString script)
3583 parser.setScript (script);
3585 while (parser.next() )
3586 parseAtom(parser.getAtom());
3589 void VymModel::setExportMode (bool b)
3591 // should be called before and after exports
3592 // depending on the settings
3593 if (b && settings.value("/export/useHideExport","true")=="true")
3594 setHideTmpMode (HideExport);
3596 setHideTmpMode (HideNone);
3599 void VymModel::exportImage(QString fname, bool askName, QString format)
3603 fname=getMapName()+".png";
3610 QFileDialog *fd=new QFileDialog (NULL);
3611 fd->setCaption (tr("Export map as image"));
3612 fd->setDirectory (lastImageDir);
3613 fd->setFileMode(QFileDialog::AnyFile);
3614 fd->setFilters (imageIO.getFilters() );
3617 fl=fd->selectedFiles();
3619 format=imageIO.getType(fd->selectedFilter());
3623 setExportMode (true);
3624 QPixmap pix (getPixmap());
3625 pix.save(fname, format);
3626 setExportMode (false);
3630 void VymModel::exportXML(QString dir, bool askForName)
3634 dir=browseDirectory(NULL,tr("Export XML to directory"));
3635 if (dir =="" && !reallyWriteDirectory(dir) )
3639 // Hide stuff during export, if settings want this
3640 setExportMode (true);
3642 // Create subdirectories
3645 // write to directory
3646 QString saveFile=saveToDir (dir,mapName+"-",true,getTotalBBox().topLeft() ,NULL);
3649 file.setName ( dir + "/"+mapName+".xml");
3650 if ( !file.open( QIODevice::WriteOnly ) )
3652 // This should neverever happen
3653 QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
3657 // Write it finally, and write in UTF8, no matter what
3658 QTextStream ts( &file );
3659 ts.setEncoding (QTextStream::UnicodeUTF8);
3663 // Now write image, too
3664 exportImage (dir+"/images/"+mapName+".png",false,"PNG");
3666 setExportMode (false);
3669 void VymModel::exportASCII(QString fname,bool askName)
3674 ex.setFile (mapName+".txt");
3680 //ex.addFilter ("TXT (*.txt)");
3681 ex.setDir(lastImageDir);
3682 //ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
3687 setExportMode(true);
3689 setExportMode(false);
3693 void VymModel::exportXHTML (const QString &dir, bool askForName)
3695 ExportXHTMLDialog dia(NULL);
3696 dia.setFilePath (filePath );
3697 dia.setMapName (mapName );
3699 if (dir!="") dia.setDir (dir);
3705 if (dia.exec()!=QDialog::Accepted)
3709 QDir d (dia.getDir());
3710 // Check, if warnings should be used before overwriting
3711 // the output directory
3712 if (d.exists() && d.count()>0)
3715 warn.showCancelButton (true);
3716 warn.setText(QString(
3717 "The directory %1 is not empty.\n"
3718 "Do you risk to overwrite some of its contents?").arg(d.path() ));
3719 warn.setCaption("Warning: Directory not empty");
3720 warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
3722 if (warn.exec()!=QDialog::Accepted) ok=false;
3729 exportXML (dia.getDir(),false );
3730 dia.doExport(mapName );
3731 //if (dia.hasChanged()) setChanged();
3735 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
3740 if (ex.setConfigFile(cf))
3742 setExportMode (true);
3743 ex.exportPresentation();
3744 setExportMode (false);
3751 //////////////////////////////////////////////
3753 //////////////////////////////////////////////
3755 void VymModel::registerEditor(QWidget *me)
3757 mapEditor=(MapEditor*)me;
3758 for (int i=0; i<mapCenters.count(); i++)
3759 mapCenters.at(i)->setMapEditor(mapEditor);
3762 void VymModel::unregisterEditor(QWidget *)
3767 void VymModel::setContextPos(QPointF p)
3772 void VymModel::unsetContextPos()
3774 contextPos=QPointF();
3777 void VymModel::updateNoteFlag()
3780 BranchObj *bo=selection.getBranch();
3783 bo->updateNoteFlag();
3784 mainWindow->updateActions();
3788 void VymModel::updateRelPositions()
3790 for (int i=0; i<mapCenters.count(); i++)
3791 mapCenters.at(i)->updateRelPositions();
3794 void VymModel::reposition()
3796 for (int i=0;i<mapCenters.count(); i++)
3797 mapCenters.at(i)->reposition(); // for positioning heading
3800 QPolygonF VymModel::shape(BranchObj *bo)
3802 // Creating (arbitrary) shapes
3805 QRectF rb=bo->getBBox();
3806 if (bo->getDepth()==0)
3808 // Just take BBox of this mapCenter
3809 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
3813 // Take union of BBox and TotalBBox
3815 QRectF ra=bo->getTotalBBox();
3816 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
3819 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
3822 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
3824 p <<ra.bottomRight()
3826 <<QPointF (rb.topRight().x(), ra.topRight().y() )
3829 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
3834 void VymModel::moveAway(LinkableMapObj *lmo)
3838 // Move all branches and MapCenters away from lmo
3839 // to avoid collisions
3844 BranchObj *boA=(BranchObj*)lmo;
3846 for (int i=0; i<mapCenters.count(); i++)
3848 boB=mapCenters.at(i);
3851 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
3854 <<" ("<<qPrintable(boA->getHeading() )<<")"
3855 <<" with ("<< qPrintable (boB->getHeading() )
3858 <<" minT="<<r.minTranslation<<endl<<endl;
3862 QPixmap VymModel::getPixmap()
3864 QRectF mapRect=getTotalBBox();
3865 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
3868 pp.setRenderHints(mapEditor->renderHints());
3870 // Don't print the visualisation of selection
3871 selection.unselect();
3873 mapScene->render ( &pp,
3874 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
3875 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
3877 // Restore selection
3878 selection.reselect();
3884 void VymModel::setMapLinkStyle (const QString & s)
3889 case LinkableMapObj::Line :
3892 case LinkableMapObj::Parabel:
3893 snow="StyleParabel";
3895 case LinkableMapObj::PolyLine:
3896 snow="StylePolyLine";
3898 case LinkableMapObj::PolyParabel:
3899 snow="StylePolyParabel";
3902 snow="UndefinedStyle";
3907 QString("setMapLinkStyle (\"%1\")").arg(s),
3908 QString("setMapLinkStyle (\"%1\")").arg(snow),
3909 QString("Set map link style (\"%1\")").arg(s)
3913 linkstyle=LinkableMapObj::Line;
3914 else if (s=="StyleParabel")
3915 linkstyle=LinkableMapObj::Parabel;
3916 else if (s=="StylePolyLine")
3917 linkstyle=LinkableMapObj::PolyLine;
3918 else if (s=="StylePolyParabel")
3919 linkstyle=LinkableMapObj::PolyParabel;
3921 linkstyle=LinkableMapObj::UndefinedStyle;
3928 bo->setLinkStyle(bo->getDefLinkStyle());
3934 LinkableMapObj::Style VymModel::getMapLinkStyle ()
3939 void VymModel::setMapDefLinkColor(QColor col)
3941 if ( !col.isValid() ) return;
3943 QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
3944 QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
3945 QString("Set map link color to %1").arg(col.name())
3959 void VymModel::setMapLinkColorHintInt()
3961 // called from setMapLinkColorHint(lch) or at end of parse
3971 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
3974 setMapLinkColorHintInt();
3977 void VymModel::toggleMapLinkColorHint()
3979 if (linkcolorhint==LinkableMapObj::HeadingColor)
3980 linkcolorhint=LinkableMapObj::DefaultColor;
3982 linkcolorhint=LinkableMapObj::HeadingColor;
3992 void VymModel::selectMapBackgroundImage ()
3994 Q3FileDialog *fd=new Q3FileDialog( NULL);
3995 fd->setMode (Q3FileDialog::ExistingFile);
3996 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
3997 ImagePreview *p =new ImagePreview (fd);
3998 fd->setContentsPreviewEnabled( TRUE );
3999 fd->setContentsPreview( p, p );
4000 fd->setPreviewMode( Q3FileDialog::Contents );
4001 fd->setCaption(vymName+" - " +tr("Load background image"));
4002 fd->setDir (lastImageDir);
4005 if ( fd->exec() == QDialog::Accepted )
4007 // TODO selectMapBackgroundImg in QT4 use: lastImageDir=fd->directory();
4008 lastImageDir=QDir (fd->dirPath());
4009 setMapBackgroundImage (fd->selectedFile());
4013 void VymModel::setMapBackgroundImage (const QString &fn) //FIXME missing savestate
4015 QColor oldcol=mapScene->backgroundBrush().color();
4019 QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
4021 QString ("setMapBackgroundImage (%1)").arg(col.name()),
4022 QString("Set background color of map to %1").arg(col.name()));
4025 brush.setTextureImage (QPixmap (fn));
4026 mapScene->setBackgroundBrush(brush);
4029 void VymModel::selectMapBackgroundColor()
4031 QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
4032 if ( !col.isValid() ) return;
4033 setMapBackgroundColor( col );
4037 void VymModel::setMapBackgroundColor(QColor col)
4039 QColor oldcol=mapScene->backgroundBrush().color();
4041 QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
4042 QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
4043 QString("Set background color of map to %1").arg(col.name()));
4044 mapScene->setBackgroundBrush(col);
4047 QColor VymModel::getMapBackgroundColor()
4049 return mapScene->backgroundBrush().color();
4053 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint()
4055 return linkcolorhint;
4058 QColor VymModel::getMapDefLinkColor()
4060 return defLinkColor;
4063 void VymModel::setMapDefXLinkColor(QColor col)
4068 QColor VymModel::getMapDefXLinkColor()
4070 return defXLinkColor;
4073 void VymModel::setMapDefXLinkWidth (int w)
4078 int VymModel::getMapDefXLinkWidth()
4080 return defXLinkWidth;
4083 void VymModel::move(const double &x, const double &y)
4085 LinkableMapObj *sel=selection.single();
4088 QPointF ap(sel->getAbsPos());
4092 QString ps=qpointfToString(ap);
4093 QString s=selection.getSelectString();
4096 s, "move "+qpointfToString(to),
4097 QString("Move %1 to %2").arg(getObjectName(sel)).arg(ps));
4105 void VymModel::moveRel (const double &x, const double &y)
4107 LinkableMapObj *sel=selection.single();
4110 QPointF rp(sel->getRelPos());
4114 QString ps=qpointfToString (sel->getRelPos());
4115 QString s=getSelectString(sel);
4118 s, "moveRel "+qpointfToString(to),
4119 QString("Move %1 to relative position %2").arg(getObjectName(sel)).arg(ps));
4120 ((OrnamentedObj*)sel)->move2RelPos (x,y);
4129 void VymModel::animate()
4131 animationTimer->stop();
4134 while (i<animObjList.size() )
4136 bo=(BranchObj*)animObjList.at(i);
4141 animObjList.removeAt(i);
4148 emit (selectionChanged(selModel->selection(), selModel->selection()) );
4150 if (!animObjList.isEmpty()) animationTimer->start();
4154 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
4156 if (bo && bo->getDepth()>0)
4159 ap.setStart (start);
4161 ap.setTicks (animationTicks);
4162 ap.setAnimated (true);
4163 bo->setAnimation (ap);
4164 animObjList.append( bo );
4165 animationTimer->setSingleShot (true);
4166 animationTimer->start(animationInterval);
4170 void VymModel::stopAnimation (MapObj *mo)
4172 int i=animObjList.indexOf(mo);
4174 animObjList.removeAt (i);
4177 void VymModel::sendSelection()
4179 if (netstate!=Server) return;
4180 sendData (QString("select (\"%1\")").arg(selection.getSelectString()) );
4183 void VymModel::newServer()
4187 tcpServer = new QTcpServer(this);
4188 if (!tcpServer->listen(QHostAddress::Any,port)) {
4189 QMessageBox::critical(NULL, "vym server",
4190 QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
4191 //FIXME needed? we are no widget any longer... close();
4194 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
4196 cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
4199 void VymModel::connectToServer()
4202 server="salam.suse.de";
4204 clientSocket = new QTcpSocket (this);
4205 clientSocket->abort();
4206 clientSocket->connectToHost(server ,port);
4207 connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
4208 connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
4209 this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
4211 cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
4216 void VymModel::newClient()
4218 QTcpSocket *newClient = tcpServer->nextPendingConnection();
4219 connect(newClient, SIGNAL(disconnected()),
4220 newClient, SLOT(deleteLater()));
4222 cout <<"ME::newClient at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
4224 clientList.append (newClient);
4228 void VymModel::sendData(const QString &s)
4230 if (clientList.size()==0) return;
4232 // Create bytearray to send
4234 QDataStream out(&block, QIODevice::WriteOnly);
4235 out.setVersion(QDataStream::Qt_4_0);
4237 // Reserve some space for blocksize
4240 // Write sendCounter
4241 out << sendCounter++;
4246 // Go back and write blocksize so far
4247 out.device()->seek(0);
4248 quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
4252 cout << "ME::sendData bs="<<bs<<" counter="<<sendCounter<<" s="<<qPrintable(s)<<endl;
4254 for (int i=0; i<clientList.size(); ++i)
4256 //cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
4257 clientList.at(i)->write (block);
4261 void VymModel::readData ()
4263 while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
4266 cout <<"readData bytesAvail="<<clientSocket->bytesAvailable();
4270 QDataStream in(clientSocket);
4271 in.setVersion(QDataStream::Qt_4_0);
4279 cout << " t="<<qPrintable (t)<<endl;
4285 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
4287 switch (socketError) {
4288 case QAbstractSocket::RemoteHostClosedError:
4290 case QAbstractSocket::HostNotFoundError:
4291 QMessageBox::information(NULL, vymName +" Network client",
4292 "The host was not found. Please check the "
4293 "host name and port settings.");
4295 case QAbstractSocket::ConnectionRefusedError:
4296 QMessageBox::information(NULL, vymName + " Network client",
4297 "The connection was refused by the peer. "
4298 "Make sure the fortune server is running, "
4299 "and check that the host name and port "
4300 "settings are correct.");
4303 QMessageBox::information(NULL, vymName + " Network client",
4304 QString("The following error occurred: %1.")
4305 .arg(clientSocket->errorString()));
4312 void VymModel::selectMapSelectionColor()
4314 QColor col = QColorDialog::getColor( defLinkColor, NULL);
4315 setSelectionColor (col);
4318 void VymModel::setSelectionColorInt (QColor col)
4320 if ( !col.isValid() ) return;
4322 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4323 QString("setSelectionColor (%1)").arg(col.name()),
4324 QString("Set color of selection box to %1").arg(col.name())
4327 mapEditor->setSelectionColor (col);
4330 void VymModel::updateSelection()
4335 void VymModel::setSelectionColor(QColor col)
4337 if ( !col.isValid() ) return;
4339 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4340 QString("setSelectionColor (%1)").arg(col.name()),
4341 QString("Set color of selection box to %1").arg(col.name())
4343 setSelectionColorInt (col);
4346 QColor VymModel::getSelectionColor()
4348 return mapEditor->getSelectionColor();
4351 void VymModel::setHideTmpMode (HideTmpMode mode)
4354 for (int i=0;i<mapCenters.count(); i++)
4355 mapCenters.at(i)->setHideTmp (mode);
4357 // FIXME needed? scene()->update();
4361 QRectF VymModel::getTotalBBox()
4364 for (int i=0;i<mapCenters.count(); i++)
4365 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
4369 //////////////////////////////////////////////
4370 // Selection related
4371 //////////////////////////////////////////////
4373 void VymModel::setSelectionModel (QItemSelectionModel *sm)
4376 cout << "VM::setSelModel selModel="<<selModel<<endl;
4379 QItemSelectionModel* VymModel::getSelectionModel()
4384 void VymModel::setSelectionBlocked (bool b)
4389 selection.unblock();
4392 bool VymModel::isSelectionBlocked()
4394 return selection.isBlocked();
4397 bool VymModel::select ()
4399 QModelIndex index=selModel->selectedIndexes().first(); // TODO no multiselections yet
4401 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
4402 return select (item->getLMO() );
4405 bool VymModel::select (const QString &s)
4407 if (selection.select(s))
4410 ensureSelectionVisible();
4418 bool VymModel::select (LinkableMapObj *lmo)
4420 if (selection.select(lmo))
4423 ensureSelectionVisible();
4430 void VymModel::unselect()
4432 selection.unselect();
4435 void VymModel::reselect()
4437 selection.reselect();
4440 void VymModel::ensureSelectionVisible()
4442 LinkableMapObj *lmo=selection.single();
4443 if (lmo &&mapEditor) mapEditor->ensureVisible (lmo->getBBox() );
4447 void VymModel::selectInt (LinkableMapObj *lmo)
4449 if (selection.select(lmo))
4451 //selection.update();
4452 sendSelection (); // FIXME VM use signal
4457 void VymModel::selectNextBranchInt()
4459 // Increase number of branch
4460 LinkableMapObj *sel=selection.single();
4463 QString s=selection.getSelectString();
4469 part=s.section(",",-1);
4471 num=part.right(part.length() - 3);
4473 s=s.left (s.length() -num.length());
4476 num=QString ("%1").arg(num.toUInt()+1);
4480 // Try to select this one
4481 if (select (s)) return;
4483 // We have no direct successor,
4484 // try to increase the parental number in order to
4485 // find a successor with same depth
4487 int d=selection.single()->getDepth();
4492 while (!found && d>0)
4494 s=s.section (",",0,d-1);
4495 // replace substring of current depth in s with "1"
4496 part=s.section(",",-1);
4498 num=part.right(part.length() - 3);
4502 // increase number of parent
4503 num=QString ("%1").arg(num.toUInt()+1);
4504 s=s.section (",",0,d-2) + ","+ typ+num;
4507 // Special case, look at orientation
4508 if (selection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
4509 num=QString ("%1").arg(num.toUInt()+1);
4511 num=QString ("%1").arg(num.toUInt()-1);
4516 // pad to oldDepth, select the first branch for each depth
4517 for (i=d;i<oldDepth;i++)
4522 if ( selection.getBranch()->countBranches()>0)
4530 // try to select the freshly built string
4538 void VymModel::selectPrevBranchInt()
4540 // Decrease number of branch
4541 BranchObj *bo=selection.getBranch();
4544 QString s=selection.getSelectString();
4550 part=s.section(",",-1);
4552 num=part.right(part.length() - 3);
4554 s=s.left (s.length() -num.length());
4556 int n=num.toInt()-1;
4559 num=QString ("%1").arg(n);
4562 // Try to select this one
4563 if (n>=0 && select (s)) return;
4565 // We have no direct precessor,
4566 // try to decrease the parental number in order to
4567 // find a precessor with same depth
4569 int d=selection.single()->getDepth();
4574 while (!found && d>0)
4576 s=s.section (",",0,d-1);
4577 // replace substring of current depth in s with "1"
4578 part=s.section(",",-1);
4580 num=part.right(part.length() - 3);
4584 // decrease number of parent
4585 num=QString ("%1").arg(num.toInt()-1);
4586 s=s.section (",",0,d-2) + ","+ typ+num;
4589 // Special case, look at orientation
4590 if (selection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
4591 num=QString ("%1").arg(num.toInt()-1);
4593 num=QString ("%1").arg(num.toInt()+1);
4598 // pad to oldDepth, select the last branch for each depth
4599 for (i=d;i<oldDepth;i++)
4603 if ( selection.getBranch()->countBranches()>0)
4604 s+=",bo:"+ QString ("%1").arg( selection.getBranch()->countBranches()-1 );
4611 // try to select the freshly built string
4619 void VymModel::selectUpperBranch()
4621 if (selection.isBlocked() ) return;
4623 BranchObj *bo=selection.getBranch();
4624 if (bo && selection.type()==Selection::Branch)
4626 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4627 selectPrevBranchInt();
4629 if (bo->getDepth()==1)
4630 selectNextBranchInt();
4632 selectPrevBranchInt();
4636 void VymModel::selectLowerBranch()
4638 if (selection.isBlocked() ) return;
4640 BranchObj *bo=selection.getBranch();
4641 if (bo && selection.type()==Selection::Branch)
4643 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4644 selectNextBranchInt();
4646 if (bo->getDepth()==1)
4647 selectPrevBranchInt();
4649 selectNextBranchInt();
4654 void VymModel::selectLeftBranch()
4656 if (selection.isBlocked() ) return;
4660 LinkableMapObj *sel=selection.single();
4663 if (selection.type()== Selection::MapCenter)
4665 par=selection.getBranch();
4666 bo=par->getLastSelectedBranch();
4669 // Workaround for reselecting on left and right side
4670 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4671 bo=par->getLastBranch();
4674 bo=par->getLastBranch();
4675 selection.select(bo);
4677 ensureSelectionVisible();
4683 par=(BranchObj*)(sel->getParObj());
4684 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4686 if (selection.type() == Selection::Branch ||
4687 selection.type() == Selection::FloatImage)
4689 selection.select(par);
4691 ensureSelectionVisible();
4696 if (selection.type() == Selection::Branch )
4698 bo=selection.getBranch()->getLastSelectedBranch();
4701 selection.select(bo);
4703 ensureSelectionVisible();
4712 void VymModel::selectRightBranch()
4714 if (selection.isBlocked() ) return;
4718 LinkableMapObj *sel=selection.single();
4721 if (selection.type()==Selection::MapCenter)
4723 par=selection.getBranch();
4724 bo=par->getLastSelectedBranch();
4727 // Workaround for reselecting on left and right side
4728 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
4729 bo=par->getFirstBranch();
4732 selection.select(bo);
4734 ensureSelectionVisible();
4740 par=(BranchObj*)(selection.single()->getParObj());
4741 if (selection.single()->getOrientation()==LinkableMapObj::LeftOfCenter)
4743 if (selection.type() == Selection::Branch ||
4744 selection.type() == Selection::FloatImage)
4746 selection.select(par);
4748 ensureSelectionVisible();
4753 if (selection.type() == Selection::Branch)
4755 bo=selection.getBranch()->getLastSelectedBranch();
4758 selection.select(bo);
4760 ensureSelectionVisible();
4769 void VymModel::selectFirstBranch()
4771 BranchObj *bo1=selection.getBranch();
4776 par=(BranchObj*)(bo1->getParObj());
4778 bo2=par->getFirstBranch();
4780 selection.select(bo2);
4782 ensureSelectionVisible();
4788 void VymModel::selectLastBranch()
4790 BranchObj *bo1=selection.getBranch();
4795 par=(BranchObj*)(bo1->getParObj());
4797 bo2=par->getLastBranch();
4800 selection.select(bo2);
4802 ensureSelectionVisible();
4808 void VymModel::selectParent()
4810 LinkableMapObj *lmo=selection.first();
4814 par=(BranchObj*)(lmo->getParObj());
4816 selection.select(par);
4818 ensureSelectionVisible();
4823 Selection::Type VymModel::selectionType()
4825 return selection.type();
4828 LinkableMapObj* VymModel::getSelection()
4830 return selection.single();
4832 BranchObj* VymModel::getSelectedBranch()
4834 return selection.getBranch();
4837 FloatImageObj* VymModel::getSelectedFloatImage()
4839 return selection.getFloatImage();
4842 QString VymModel::getSelectString ()
4844 return selection.getSelectString();
4847 QString VymModel::getSelectString (LinkableMapObj *lmo) // FIXME VM needs to use TreeModel
4851 if (typeid(*lmo)==typeid(BranchObj) ||
4852 typeid(*lmo)==typeid(MapCenterObj) )
4854 LinkableMapObj *par=lmo->getParObj();
4857 if (lmo->getDepth() ==1)
4858 // Mainbranch, return
4859 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4862 //cout << "VM::getSelectString lmo="<<lmo<<" d="<<lmo->getDepth()<<" s="<<s.toStdString()<<" h="<<((BranchObj*)lmo)->getHeading().toStdString()<<endl;
4863 // Branch, call myself recursively
4864 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4869 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
4870 if (i>=0) s=QString("mc:%1").arg(i);