Various fixes, also from 1.12. branch
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()
72 while (!mapCenters.isEmpty())
73 delete mapCenters.takeFirst();
76 void VymModel::init ()
78 // We should have at least one map center to start with
79 // addMapCenter(); FIXME create this in MapEditor as long as model is part of that
91 stepsTotal=settings.readNumEntry("/history/stepsTotal",100);
92 undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
93 mainWindow->updateHistory (undoSet);
101 fileName=tr("unnamed");
103 blockReposition=false;
104 blockSaveState=false;
106 autosaveTimer=new QTimer (this);
107 connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
109 fileChangedTimer=new QTimer (this);
110 fileChangedTimer->start(3000);
111 connect(fileChangedTimer, SIGNAL(timeout()), this, SLOT(fileChanged()));
115 selection.setModel (this);
116 selection.unselect();
123 animationUse=settings.readBoolEntry("/animation/use",false);
124 animationTicks=settings.readNumEntry("/animation/ticks",10);
125 animationInterval=settings.readNumEntry("/animation/interval",50);
127 animationTimer=new QTimer (this);
128 connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
131 defLinkColor=QColor (0,0,255);
132 defXLinkColor=QColor (180,180,180);
133 linkcolorhint=LinkableMapObj::DefaultColor;
134 linkstyle=LinkableMapObj::PolyParabel;
136 defXLinkColor=QColor (230,230,230);
146 // addMapCenter(); FIXME create this in MapEditor until BO and MCO are independent of scene
150 void VymModel::makeTmpDirectories()
152 // Create unique temporary directories
153 tmpMapDir = tmpVymDir+QString("/model-%1").arg(mapNum);
154 histPath = tmpMapDir+"/history";
160 MapEditor* VymModel::getMapEditor() // FIXME better return favourite editor here
165 bool VymModel::isRepositionBlocked()
167 return blockReposition;
170 void VymModel::updateActions() // FIXME maybe don't update if blockReposition is set
172 // Tell mainwindow to update states of actions
173 mainWindow->updateActions();
178 QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
180 // tmpdir temporary directory to which data will be written
181 // prefix mapname, which will be appended to images etc.
182 // writeflags Only write flags for "real" save of map, not undo
183 // offset offset of bbox of whole map in scene.
184 // Needed for XML export
193 case LinkableMapObj::Line:
196 case LinkableMapObj::Parabel:
199 case LinkableMapObj::PolyLine:
203 ls="StylePolyParabel";
207 QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
209 if (linkcolorhint==LinkableMapObj::HeadingColor)
210 colhint=xml.attribut("linkColorHint","HeadingColor");
212 QString mapAttr=xml.attribut("version",vymVersion);
214 mapAttr+= xml.attribut("author",author) +
215 xml.attribut("comment",comment) +
216 xml.attribut("date",getDate()) +
217 xml.attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
218 xml.attribut("selectionColor", selection.getColor().name() ) +
219 xml.attribut("linkStyle", ls ) +
220 xml.attribut("linkColor", defLinkColor.name() ) +
221 xml.attribut("defXLinkColor", defXLinkColor.name() ) +
222 xml.attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
224 s+=xml.beginElement("vymmap",mapAttr);
227 // Find the used flags while traversing the tree
228 standardFlagsDefault->resetUsedCounter();
230 // Reset the counters before saving
231 // TODO constr. of FIO creates lots of objects, better do this in some other way...
232 FloatImageObj (mapScene).resetSaveCounter();
234 // Build xml recursivly
235 if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
236 // Save complete map, if saveSel not set
237 s+=saveToDir(tmpdir,prefix,writeflags,offset);
240 if ( typeid(*saveSel) == typeid(BranchObj) )
242 s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
243 else if ( typeid(*saveSel) == typeid(FloatImageObj) )
245 s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
248 // Save local settings
249 s+=settings.getDataXML (destPath);
252 if (!selection.isEmpty() && !saveSel )
253 s+=xml.valueElement("select",selection.getSelectString());
256 s+=xml.endElement("vymmap");
259 standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
263 void VymModel::setFilePath(QString fpath, QString destname)
265 if (fpath.isEmpty() || fpath=="")
272 filePath=fpath; // becomes absolute path
273 fileName=fpath; // gets stripped of path
274 destPath=destname; // needed for vymlinks and during load to reset fileChangedTime
276 // If fpath is not an absolute path, complete it
277 filePath=QDir(fpath).absPath();
278 fileDir=filePath.left (1+filePath.findRev ("/"));
280 // Set short name, too. Search from behind:
281 int i=fileName.findRev("/");
282 if (i>=0) fileName=fileName.remove (0,i+1);
284 // Forget the .vym (or .xml) for name of map
285 mapName=fileName.left(fileName.findRev(".",-1,true) );
289 void VymModel::setFilePath(QString fpath)
291 setFilePath (fpath,fpath);
294 QString VymModel::getFilePath()
299 QString VymModel::getFileName()
304 QString VymModel::getMapName()
309 QString VymModel::getDestPath()
314 ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
316 ErrorCode err=success;
318 parseBaseHandler *handler;
322 case VymMap: handler=new parseVYMHandler; break;
323 case FreemindMap : handler=new parseFreemindHandler; break;
325 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
326 "Unknown FileType in VymModel::load()");
332 // FIXME not needed??? model->setMapEditor(this);
333 // (map state is set later at end of load...)
336 BranchObj *bo=selection.getBranch();
337 if (!bo) return aborted;
338 if (lmode==ImportAdd)
339 saveStateChangingPart(
342 QString("addMapInsert (%1)").arg(fname),
343 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
345 saveStateChangingPart(
348 QString("addMapReplace(%1)").arg(fname),
349 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
353 // Create temporary directory for packing
355 QString tmpZipDir=makeTmpDir (ok,"vym-pack");
358 QMessageBox::critical( 0, tr( "Critical Load Error" ),
359 tr("Couldn't create temporary directory before load\n"));
364 err=unzipDir (tmpZipDir,fname);
374 // Look for mapname.xml
375 xmlfile= fname.left(fname.findRev(".",-1,true));
376 xmlfile=xmlfile.section( '/', -1 );
377 QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
378 if (!mfile.exists() )
380 // mapname.xml does not exist, well,
381 // maybe someone renamed the mapname.vym file...
382 // Try to find any .xml in the toplevel
383 // directory of the .vym file
384 QStringList flist=QDir (tmpZipDir).entryList("*.xml");
385 if (flist.count()==1)
387 // Only one entry, take this one
388 xmlfile=tmpZipDir + "/"+flist.first();
391 for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it )
392 *it=tmpZipDir + "/" + *it;
393 // TODO Multiple entries, load all (but only the first one into this ME)
394 //mainWindow->fileLoadFromTmp (flist);
395 //returnCode=1; // Silently forget this attempt to load
396 qWarning ("MainWindow::load (fn) multimap found...");
399 if (flist.isEmpty() )
401 QMessageBox::critical( 0, tr( "Critical Load Error" ),
402 tr("Couldn't find a map (*.xml) in .vym archive.\n"));
405 } //file doesn't exist
407 xmlfile=mfile.name();
410 QFile file( xmlfile);
412 // I am paranoid: file should exist anyway
413 // according to check in mainwindow.
416 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
417 tr(QString("Couldn't open map %1").arg(file.name())));
421 bool blockSaveStateOrg=blockSaveState;
422 blockReposition=true;
424 QXmlInputSource source( file);
425 QXmlSimpleReader reader;
426 reader.setContentHandler( handler );
427 reader.setErrorHandler( handler );
428 handler->setModel ( this);
431 // We need to set the tmpDir in order to load files with rel. path
436 tmpdir=fname.left(fname.findRev("/",-1));
437 handler->setTmpDir (tmpdir);
438 handler->setInputFile (file.name());
439 handler->setLoadMode (lmode);
440 bool ok = reader.parse( source );
441 blockReposition=false;
442 blockSaveState=blockSaveStateOrg;
446 reposition(); // FIXME reposition the view instead...
453 autosaveTimer->stop();
456 // Reset timestamp to check for later updates of file
457 fileChangedTime=QFileInfo (destPath).lastModified();
460 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
461 tr( handler->errorProtocol() ) );
463 // Still return "success": the map maybe at least
464 // partially read by the parser
469 removeDir (QDir(tmpZipDir));
476 ErrorCode VymModel::save (const SaveMode &savemode)
480 QString safeFilePath;
482 ErrorCode err=success;
486 mapFileName=mapName+".xml";
488 // use name given by user, even if he chooses .doc
489 mapFileName=fileName;
491 // Look, if we should zip the data:
494 QMessageBox mb( vymName,
495 tr("The map %1\ndid not use the compressed "
496 "vym file format.\nWriting it uncompressed will also write images \n"
497 "and flags and thus may overwrite files in the "
498 "given directory\n\nDo you want to write the map").arg(filePath),
499 QMessageBox::Warning,
500 QMessageBox::Yes | QMessageBox::Default,
502 QMessageBox::Cancel | QMessageBox::Escape);
503 mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
504 mb.setButtonText( QMessageBox::No, tr("uncompressed") );
505 mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
508 case QMessageBox::Yes:
509 // save compressed (default file format)
512 case QMessageBox::No:
516 case QMessageBox::Cancel:
523 // First backup existing file, we
524 // don't want to add to old zip archives
528 if ( settings.value ("/mapeditor/writeBackupFile").toBool())
530 QString backupFileName(destPath + "~");
531 QFile backupFile(backupFileName);
532 if (backupFile.exists() && !backupFile.remove())
534 QMessageBox::warning(0, tr("Save Error"),
535 tr("%1\ncould not be removed before saving").arg(backupFileName));
537 else if (!f.rename(backupFileName))
539 QMessageBox::warning(0, tr("Save Error"),
540 tr("%1\ncould not be renamed before saving").arg(destPath));
547 // Create temporary directory for packing
549 tmpZipDir=makeTmpDir (ok,"vym-zip");
552 QMessageBox::critical( 0, tr( "Critical Load Error" ),
553 tr("Couldn't create temporary directory before save\n"));
557 safeFilePath=filePath;
558 setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
561 // Create mapName and fileDir
562 makeSubDirs (fileDir);
565 if (savemode==CompleteMap || selection.isEmpty())
568 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
571 autosaveTimer->stop();
576 if (selection.type()==Selection::FloatImage)
579 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),selection.getBranch());
580 // TODO take care of multiselections
583 if (!saveStringToDisk(fileDir+mapFileName,saveFile))
586 qWarning ("ME::saveStringToDisk failed!");
592 if (err==success) err=zipDir (tmpZipDir,destPath);
595 removeDir (QDir(tmpZipDir));
597 // Restore original filepath outside of tmp zip dir
598 setFilePath (safeFilePath);
602 fileChangedTime=QFileInfo (destPath).lastModified();
606 void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)
608 QString pathDir=path.left(path.findRev("/"));
614 // We need to parse saved XML data
615 parseVYMHandler handler;
616 QXmlInputSource source( file);
617 QXmlSimpleReader reader;
618 reader.setContentHandler( &handler );
619 reader.setErrorHandler( &handler );
620 handler.setModel ( this);
621 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
622 if (undoSel.isEmpty())
626 handler.setLoadMode (NewMap);
630 handler.setLoadMode (ImportReplace);
632 blockReposition=true;
633 bool ok = reader.parse( source );
634 blockReposition=false;
637 // This should never ever happen
638 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
639 handler.errorProtocol());
642 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
645 void VymModel::addMapInsertInt (const QString &path, int pos)
647 BranchObj *sel=selection.getBranch();
650 QString pathDir=path.left(path.findRev("/"));
656 // We need to parse saved XML data
657 parseVYMHandler handler;
658 QXmlInputSource source( file);
659 QXmlSimpleReader reader;
660 reader.setContentHandler( &handler );
661 reader.setErrorHandler( &handler );
662 handler.setModel (this);
663 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
664 handler.setLoadMode (ImportAdd);
665 blockReposition=true;
666 bool ok = reader.parse( source );
667 blockReposition=false;
670 // This should never ever happen
671 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
672 handler.errorProtocol());
674 if (sel->getDepth()>0)
675 sel->getLastBranch()->linkTo (sel,pos);
677 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
681 FloatImageObj* VymModel::loadFloatImageInt (QString fn)
683 BranchObj *bo=selection.getBranch();
688 fio=bo->getLastFloatImage();
691 // FIXME needed? scene()->update();
697 void VymModel::loadFloatImage ()
699 BranchObj *bo=selection.getBranch();
703 Q3FileDialog *fd=new Q3FileDialog( NULL);
704 fd->setMode (Q3FileDialog::ExistingFiles);
705 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
706 ImagePreview *p =new ImagePreview (fd);
707 fd->setContentsPreviewEnabled( TRUE );
708 fd->setContentsPreview( p, p );
709 fd->setPreviewMode( Q3FileDialog::Contents );
710 fd->setCaption(vymName+" - " +tr("Load image"));
711 fd->setDir (lastImageDir);
714 if ( fd->exec() == QDialog::Accepted )
716 // TODO loadFIO in QT4 use: lastImageDir=fd->directory();
717 lastImageDir=QDir (fd->dirPath());
720 for (int j=0; j<fd->selectedFiles().count(); j++)
722 s=fd->selectedFiles().at(j);
723 fio=loadFloatImageInt (s);
726 (LinkableMapObj*)fio,
729 QString ("loadImage (%1)").arg(s ),
730 QString("Add image %1 to %2").arg(s).arg(getObjectName(bo))
733 // TODO loadFIO error handling
734 qWarning ("Failed to load "+s);
742 void VymModel::saveFloatImageInt (FloatImageObj *fio, const QString &type, const QString &fn)
747 void VymModel::saveFloatImage ()
749 FloatImageObj *fio=selection.getFloatImage();
752 QFileDialog *fd=new QFileDialog( NULL);
753 fd->setFilters (imageIO.getFilters());
754 fd->setCaption(vymName+" - " +tr("Save image"));
755 fd->setFileMode( QFileDialog::AnyFile );
756 fd->setDirectory (lastImageDir);
757 // fd->setSelection (fio->getOriginalFilename());
761 if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
763 fn=fd->selectedFiles().at(0);
764 if (QFile (fn).exists() )
766 QMessageBox mb( vymName,
767 tr("The file %1 exists already.\n"
768 "Do you want to overwrite it?").arg(fn),
769 QMessageBox::Warning,
770 QMessageBox::Yes | QMessageBox::Default,
771 QMessageBox::Cancel | QMessageBox::Escape,
772 QMessageBox::NoButton );
774 mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
775 mb.setButtonText( QMessageBox::No, tr("Cancel"));
778 case QMessageBox::Yes:
781 case QMessageBox::Cancel:
788 saveFloatImageInt (fio,fd->selectedFilter(),fn );
795 void VymModel::importDirInt(BranchObj *dst, QDir d)
797 BranchObj *bo=selection.getBranch();
800 // Traverse directories
801 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
802 QFileInfoList list = d.entryInfoList();
805 for (int i = 0; i < list.size(); ++i)
808 if (fi.fileName() != "." && fi.fileName() != ".." )
811 bo=dst->getLastBranch();
812 bo->setHeading (fi.fileName() );
813 bo->setColor (QColor("blue"));
815 if ( !d.cd(fi.fileName()) )
816 QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
819 // Recursively add subdirs
826 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
827 list = d.entryInfoList();
829 for (int i = 0; i < list.size(); ++i)
833 bo=dst->getLastBranch();
834 bo->setHeading (fi.fileName() );
835 bo->setColor (QColor("black"));
836 if (fi.fileName().right(4) == ".vym" )
837 bo->setVymLink (fi.filePath());
842 void VymModel::importDirInt (const QString &s)
844 BranchObj *bo=selection.getBranch();
847 saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
854 void VymModel::importDir()
856 BranchObj *bo=selection.getBranch();
860 filters <<"VYM map (*.vym)";
861 QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
862 fd->setMode (QFileDialog::DirectoryOnly);
863 fd->setFilters (filters);
864 fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
868 if ( fd->exec() == QDialog::Accepted )
870 importDirInt (fd->selectedFile() );
872 //FIXME needed? scene()->update();
878 void VymModel::autosave()
880 QDateTime now=QDateTime().currentDateTime();
882 // Disable autosave, while we have gone back in history
883 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
884 if (redosAvail>0) return;
886 // Also disable autosave for new map without filename
887 if (filePath.isEmpty()) return;
890 if (mapUnsaved &&mapChanged && settings.value ("/autosave/use",true).toBool() )
892 if (QFileInfo(filePath).lastModified()<=fileChangedTime)
893 mainWindow->fileSave (this);
896 cout <<" ME::autosave rejected, file on disk is newer than last save.\n";
901 void VymModel::fileChanged()
903 // Check if file on disk has changed meanwhile
904 if (!filePath.isEmpty())
906 QDateTime tmod=QFileInfo (filePath).lastModified();
907 if (tmod>fileChangedTime)
909 // FIXME switch to current mapeditor and finish lineedits...
910 QMessageBox mb( vymName,
911 tr("The file of the map on disk has changed:\n\n"
912 " %1\n\nDo you want to reload that map with the new file?").arg(filePath),
913 QMessageBox::Question,
915 QMessageBox::Cancel | QMessageBox::Default,
916 QMessageBox::NoButton );
918 mb.setButtonText( QMessageBox::Yes, tr("Reload"));
919 mb.setButtonText( QMessageBox::No, tr("Ignore"));
922 case QMessageBox::Yes:
924 load (filePath,NewMap,fileType);
925 case QMessageBox::Cancel:
926 fileChangedTime=tmod; // allow autosave to overwrite newer file!
932 bool VymModel::isDefault()
937 void VymModel::makeDefault()
943 bool VymModel::hasChanged()
948 void VymModel::setChanged()
951 autosaveTimer->start(settings.value("/autosave/ms/",300000).toInt());
959 QString VymModel::getObjectName (const LinkableMapObj *lmo)
962 if (!lmo) return QString("Error: NULL has no name!");
964 if ((typeid(*lmo) == typeid(BranchObj) ||
965 typeid(*lmo) == typeid(MapCenterObj)))
968 s=(((BranchObj*)lmo)->getHeading());
969 if (s=="") s="unnamed";
970 return QString("branch (%1)").arg(s);
972 if ((typeid(*lmo) == typeid(FloatImageObj) ))
973 return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
974 return QString("Unknown type has no name!");
977 void VymModel::redo()
979 // Can we undo at all?
980 if (redosAvail<1) return;
982 bool blockSaveStateOrg=blockSaveState;
987 if (undosAvail<stepsTotal) undosAvail++;
989 if (curStep>stepsTotal) curStep=1;
990 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
991 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
992 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
993 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
994 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
995 QString version=undoSet.readEntry ("/history/version");
997 /* TODO Maybe check for version, if we save the history
998 if (!checkVersion(version))
999 QMessageBox::warning(0,tr("Warning"),
1000 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1003 // Find out current undo directory
1004 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1008 cout << "VymModel::redo() begin\n";
1009 cout << " undosAvail="<<undosAvail<<endl;
1010 cout << " redosAvail="<<redosAvail<<endl;
1011 cout << " curStep="<<curStep<<endl;
1012 cout << " ---------------------------"<<endl;
1013 cout << " comment="<<comment.toStdString()<<endl;
1014 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1015 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1016 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1017 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1018 cout << " ---------------------------"<<endl<<endl;
1021 // select object before redo
1022 if (!redoSelection.isEmpty())
1023 select (redoSelection);
1026 parseAtom (redoCommand);
1029 blockSaveState=blockSaveStateOrg;
1031 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1032 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1033 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1034 undoSet.writeSettings(histPath);
1036 mainWindow->updateHistory (undoSet);
1039 /* TODO remove testing
1040 cout << "ME::redo() end\n";
1041 cout << " undosAvail="<<undosAvail<<endl;
1042 cout << " redosAvail="<<redosAvail<<endl;
1043 cout << " curStep="<<curStep<<endl;
1044 cout << " ---------------------------"<<endl<<endl;
1050 bool VymModel::isRedoAvailable()
1052 if (undoSet.readNumEntry("/history/redosAvail",0)>0)
1058 void VymModel::undo()
1060 // Can we undo at all?
1061 if (undosAvail<1) return;
1063 mainWindow->statusMessage (tr("Autosave disabled during undo."));
1065 bool blockSaveStateOrg=blockSaveState;
1066 blockSaveState=true;
1068 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1069 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1070 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1071 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1072 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1073 QString version=undoSet.readEntry ("/history/version");
1075 /* TODO Maybe check for version, if we save the history
1076 if (!checkVersion(version))
1077 QMessageBox::warning(0,tr("Warning"),
1078 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1081 // Find out current undo directory
1082 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1084 // select object before undo
1085 if (!undoSelection.isEmpty())
1086 select (undoSelection);
1090 cout << "VymModel::undo() begin\n";
1091 cout << " undosAvail="<<undosAvail<<endl;
1092 cout << " redosAvail="<<redosAvail<<endl;
1093 cout << " curStep="<<curStep<<endl;
1094 cout << " ---------------------------"<<endl;
1095 cout << " comment="<<comment.toStdString()<<endl;
1096 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1097 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1098 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1099 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1100 cout << " ---------------------------"<<endl<<endl;
1102 parseAtom (undoCommand);
1107 if (curStep<1) curStep=stepsTotal;
1111 blockSaveState=blockSaveStateOrg;
1112 /* TODO remove testing
1113 cout << "VymModel::undo() end\n";
1114 cout << " undosAvail="<<undosAvail<<endl;
1115 cout << " redosAvail="<<redosAvail<<endl;
1116 cout << " curStep="<<curStep<<endl;
1117 cout << " ---------------------------"<<endl<<endl;
1120 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1121 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1122 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1123 undoSet.writeSettings(histPath);
1125 mainWindow->updateHistory (undoSet);
1128 ensureSelectionVisible();
1131 bool VymModel::isUndoAvailable()
1133 if (undoSet.readNumEntry("/history/undosAvail",0)>0)
1139 void VymModel::gotoHistoryStep (int i)
1141 // Restore variables
1142 int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
1143 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
1145 if (i<0) i=undosAvail+redosAvail;
1147 // Clicking above current step makes us undo things
1150 for (int j=0; j<undosAvail-i; j++) undo();
1153 // Clicking below current step makes us redo things
1155 for (int j=undosAvail; j<i; j++)
1157 if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
1161 // And ignore clicking the current row ;-)
1165 QString VymModel::getHistoryPath()
1167 QString histName(QString("history-%1").arg(curStep));
1168 return (tmpMapDir+"/"+histName);
1171 void VymModel::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
1173 sendData(redoCom); //FIXME testing
1178 if (blockSaveState) return;
1180 if (debug) cout << "ME::saveState() for "<<qPrintable (mapName)<<endl;
1182 // Find out current undo directory
1183 if (undosAvail<stepsTotal) undosAvail++;
1185 if (curStep>stepsTotal) curStep=1;
1187 QString backupXML="";
1188 QString histDir=getHistoryPath();
1189 QString bakMapPath=histDir+"/map.xml";
1191 // Create histDir if not available
1194 makeSubDirs (histDir);
1196 // Save depending on how much needs to be saved
1198 backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
1200 QString undoCommand="";
1201 if (savemode==UndoCommand)
1203 undoCommand=undoCom;
1205 else if (savemode==PartOfMap )
1207 undoCommand=undoCom;
1208 undoCommand.replace ("PATH",bakMapPath);
1211 if (!backupXML.isEmpty())
1212 // Write XML Data to disk
1213 saveStringToDisk (bakMapPath,backupXML);
1215 // We would have to save all actions in a tree, to keep track of
1216 // possible redos after a action. Possible, but we are too lazy: forget about redos.
1219 // Write the current state to disk
1220 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1221 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1222 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1223 undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
1224 undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
1225 undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
1226 undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
1227 undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
1228 undoSet.setEntry (QString("/history/version"),vymVersion);
1229 undoSet.writeSettings(histPath);
1233 // TODO remove after testing
1234 //cout << " into="<< histPath.toStdString()<<endl;
1235 cout << " stepsTotal="<<stepsTotal<<
1236 ", undosAvail="<<undosAvail<<
1237 ", redosAvail="<<redosAvail<<
1238 ", curStep="<<curStep<<endl;
1239 cout << " ---------------------------"<<endl;
1240 cout << " comment="<<comment.toStdString()<<endl;
1241 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1242 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1243 cout << " redoCom="<<redoCom.toStdString()<<endl;
1244 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1245 if (saveSel) cout << " saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
1246 cout << " ---------------------------"<<endl;
1249 mainWindow->updateHistory (undoSet);
1255 void VymModel::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
1257 // save the selected part of the map, Undo will replace part of map
1258 QString undoSelection="";
1260 undoSelection=getSelectString(undoSel);
1262 qWarning ("VymModel::saveStateChangingPart no undoSel given!");
1263 QString redoSelection="";
1265 redoSelection=getSelectString(undoSel);
1267 qWarning ("VymModel::saveStateChangingPart no redoSel given!");
1270 saveState (PartOfMap,
1271 undoSelection, "addMapReplace (\"PATH\")",
1277 void VymModel::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
1281 qWarning ("VymModel::saveStateRemovingPart no redoSel given!");
1284 QString undoSelection=getSelectString (redoSel->getParObj());
1285 QString redoSelection=getSelectString(redoSel);
1286 if (typeid(*redoSel) == typeid(BranchObj) )
1288 // save the selected branch of the map, Undo will insert part of map
1289 saveState (PartOfMap,
1290 undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
1291 redoSelection, "delete ()",
1298 void VymModel::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment)
1300 // "Normal" savestate: save commands, selections and comment
1301 // so just save commands for undo and redo
1302 // and use current selection
1304 QString redoSelection="";
1305 if (redoSel) redoSelection=getSelectString(redoSel);
1306 QString undoSelection="";
1307 if (undoSel) undoSelection=getSelectString(undoSel);
1309 saveState (UndoCommand,
1316 void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment)
1318 // "Normal" savestate: save commands, selections and comment
1319 // so just save commands for undo and redo
1320 // and use current selection
1321 saveState (UndoCommand,
1328 void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment)
1330 // "Normal" savestate applied to model (no selection needed):
1331 // save commands and comment
1332 saveState (UndoCommand,
1340 QGraphicsScene* VymModel::getScene ()
1345 BranchObj* VymModel::first()
1347 if (mapCenters.count()>0)
1348 return mapCenters.first();
1353 BranchObj* VymModel::next(BranchObj *bo_start)
1356 BranchObj *bo=bo_start;
1359 // Try to find next branch in current MapCenter
1361 if (rbo) return rbo;
1363 // Try to find MapCenter of bo
1364 while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
1366 // Try to find next MapCenter
1367 int i=mapCenters.indexOf ((MapCenterObj*)bo);
1368 if (i+2 > mapCenters.count() || i<0) return NULL;
1369 if (mapCenters.at(i+1)!=bo_start)
1370 return mapCenters.at(i+1);
1375 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
1377 LinkableMapObj *lmo;
1379 for (int i=0;i<mapCenters.count(); i++)
1381 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
1382 if (lmo) return lmo;
1387 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
1389 LinkableMapObj *lmo;
1395 part=s.section(",",0,0);
1397 num=part.right(part.length() - 3);
1398 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
1399 return mapCenters.at(num.toInt() );
1402 for (int i=0; i<mapCenters.count(); i++)
1404 lmo=mapCenters.at(i)->findObjBySelect(s);
1405 if (lmo) return lmo;
1410 LinkableMapObj* VymModel::findID (const QString &s)
1412 LinkableMapObj *lmo;
1413 for (int i=0; i<mapCenters.count(); i++)
1415 lmo=mapCenters.at(i)->findID (s);
1416 if (lmo) return lmo;
1421 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
1425 for (int i=0; i<mapCenters.count(); i++)
1426 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
1430 //////////////////////////////////////////////
1432 //////////////////////////////////////////////
1433 void VymModel::setVersion (const QString &s)
1438 void VymModel::setAuthor (const QString &s)
1441 QString ("setMapAuthor (\"%1\")").arg(author),
1442 QString ("setMapAuthor (\"%1\")").arg(s),
1443 QString ("Set author of map to \"%1\"").arg(s)
1449 QString VymModel::getAuthor()
1454 void VymModel::setComment (const QString &s)
1457 QString ("setMapComment (\"%1\")").arg(comment),
1458 QString ("setMapComment (\"%1\")").arg(s),
1459 QString ("Set comment of map")
1465 QString VymModel::getComment ()
1470 QString VymModel::getDate ()
1472 return QDate::currentDate().toString ("yyyy-MM-dd");
1475 void VymModel::setHeading(const QString &s)
1477 BranchObj *sel=selection.getBranch();
1482 "setHeading (\""+sel->getHeading()+"\")",
1484 "setHeading (\""+s+"\")",
1485 QString("Set heading of %1 to \"%2\"").arg(getObjectName(sel)).arg(s) );
1486 sel->setHeading(s );
1487 TreeItem *ti=selection.getBranchItem();
1488 if (ti) ti->setHeading (s);
1489 //FIXME ix is wrong ModelIndex below, ix2 is (hopefully) correct:
1490 QModelIndex ix=index( ti->row(), ti->column(), index (0,0,QModelIndex()) );
1491 cout <<"VM::setHeading s="<<s.toStdString()<<" ti="<<ti<<" r,c=("<<ti->row()<<","<<ti->column()<<")"<<endl;
1492 QModelIndex ix2=index (ti);
1493 emit (dataChanged ( ix2,ix2));
1494 cout <<" (r,c)=("<<ix2.row()<<","<<ix2.column()<<")"<<endl;
1496 // FIXME playing around with selections:
1497 // but somehow not all mco are visible in tree then???
1502 QItemSelectionModel *selectionModel=mapEditor->selectionModel();
1503 selectionModel->clear();
1504 QItemSelection sel (ix2,ix2);
1505 selectionModel->select (sel, QItemSelectionModel::Select);
1510 ensureSelectionVisible();
1514 void VymModel::setHeadingInt(const QString &s)
1516 BranchObj *bo=selection.getBranch();
1522 ensureSelectionVisible();
1526 BranchObj* VymModel::findText (QString s, bool cs)
1528 QTextDocument::FindFlags flags=0;
1529 if (cs) flags=QTextDocument::FindCaseSensitively;
1532 { // Nothing found or new find process
1534 // nothing found, start again
1538 bool searching=true;
1539 bool foundNote=false;
1540 while (searching && !EOFind)
1544 // Searching in Note
1545 if (itFind->getNote().contains(s,cs))
1547 if (selection.single()!=itFind)
1549 selection.select(itFind);
1550 ensureSelectionVisible();
1552 if (textEditor->findText(s,flags))
1558 // Searching in Heading
1559 if (searching && itFind->getHeading().contains (s,cs) )
1561 selection.select(itFind);
1562 ensureSelectionVisible();
1568 itFind=next(itFind);
1569 if (!itFind) EOFind=true;
1571 //cout <<"still searching... "<<qPrintable( itFind->getHeading())<<endl;
1574 return selection.getBranch();
1579 void VymModel::findReset()
1580 { // Necessary if text to find changes during a find process
1587 void VymModel::setScene (QGraphicsScene *s)
1590 init(); // Here we have a mapScene set,
1591 // which is (still) needed to create MapCenters
1594 void VymModel::setURL(const QString &url)
1596 BranchObj *bo=selection.getBranch();
1599 QString oldurl=bo->getURL();
1603 QString ("setURL (\"%1\")").arg(oldurl),
1605 QString ("setURL (\"%1\")").arg(url),
1606 QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
1611 ensureSelectionVisible();
1615 QString VymModel::getURL()
1617 BranchObj *bo=selection.getBranch();
1619 return bo->getURL();
1624 QStringList VymModel::getURLs()
1627 BranchObj *bo=selection.getBranch();
1633 if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
1640 void VymModel::linkFloatImageTo(const QString &dstString)
1642 FloatImageObj *fio=selection.getFloatImage();
1645 BranchObj *dst=(BranchObj*)findObjBySelect(dstString);
1646 if (dst && (typeid(*dst)==typeid (BranchObj) ||
1647 typeid(*dst)==typeid (MapCenterObj)))
1649 LinkableMapObj *dstPar=dst->getParObj();
1650 QString parString=getSelectString(dstPar);
1651 QString fioPreSelectString=getSelectString(fio);
1652 QString fioPreParentSelectString=getSelectString (fio->getParObj());
1653 ((BranchObj*)dst)->addFloatImage (fio);
1654 selection.unselect();
1655 ((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
1656 fio=((BranchObj*)dst)->getLastFloatImage();
1659 selection.select(fio);
1661 getSelectString(fio),
1662 QString("linkTo (\"%1\")").arg(fioPreParentSelectString),
1664 QString ("linkTo (\"%1\")").arg(dstString),
1665 QString ("Link floatimage to %1").arg(getObjectName(dst)));
1671 void VymModel::setFrameType(const FrameObj::FrameType &t)
1673 BranchObj *bo=selection.getBranch();
1676 QString s=bo->getFrameTypeName();
1677 bo->setFrameType (t);
1678 saveState (bo, QString("setFrameType (\"%1\")").arg(s),
1679 bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
1685 void VymModel::setFrameType(const QString &s)
1687 BranchObj *bo=selection.getBranch();
1690 saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
1691 bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
1692 bo->setFrameType (s);
1698 void VymModel::setFramePenColor(const QColor &c)
1700 BranchObj *bo=selection.getBranch();
1703 saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
1704 bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
1705 bo->setFramePenColor (c);
1709 void VymModel::setFrameBrushColor(const QColor &c)
1711 BranchObj *bo=selection.getBranch();
1714 saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
1715 bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
1716 bo->setFrameBrushColor (c);
1720 void VymModel::setFramePadding (const int &i)
1722 BranchObj *bo=selection.getBranch();
1725 saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
1726 bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
1727 bo->setFramePadding (i);
1733 void VymModel::setFrameBorderWidth(const int &i)
1735 BranchObj *bo=selection.getBranch();
1738 saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
1739 bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
1740 bo->setFrameBorderWidth (i);
1746 void VymModel::setIncludeImagesVer(bool b)
1748 BranchObj *bo=selection.getBranch();
1751 QString u= b ? "false" : "true";
1752 QString r=!b ? "false" : "true";
1756 QString("setIncludeImagesVertically (%1)").arg(u),
1758 QString("setIncludeImagesVertically (%1)").arg(r),
1759 QString("Include images vertically in %1").arg(getObjectName(bo))
1761 bo->setIncludeImagesVer(b);
1766 void VymModel::setIncludeImagesHor(bool b)
1768 BranchObj *bo=selection.getBranch();
1771 QString u= b ? "false" : "true";
1772 QString r=!b ? "false" : "true";
1776 QString("setIncludeImagesHorizontally (%1)").arg(u),
1778 QString("setIncludeImagesHorizontally (%1)").arg(r),
1779 QString("Include images horizontally in %1").arg(getObjectName(bo))
1781 bo->setIncludeImagesHor(b);
1786 void VymModel::setHideLinkUnselected (bool b)
1788 LinkableMapObj *sel=selection.single();
1790 (selection.type() == Selection::Branch ||
1791 selection.type() == Selection::MapCenter ||
1792 selection.type() == Selection::FloatImage ))
1794 QString u= b ? "false" : "true";
1795 QString r=!b ? "false" : "true";
1799 QString("setHideLinkUnselected (%1)").arg(u),
1801 QString("setHideLinkUnselected (%1)").arg(r),
1802 QString("Hide link of %1 if unselected").arg(getObjectName(sel))
1804 sel->setHideLinkUnselected(b);
1808 void VymModel::setHideExport(bool b)
1810 BranchObj *bo=selection.getBranch();
1813 bo->setHideInExport (b);
1814 QString u= b ? "false" : "true";
1815 QString r=!b ? "false" : "true";
1819 QString ("setHideExport (%1)").arg(u),
1821 QString ("setHideExport (%1)").arg(r),
1822 QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bo)).arg (r)
1827 // FIXME needed? scene()->update();
1831 void VymModel::toggleHideExport()
1833 BranchObj *bo=selection.getBranch();
1835 setHideExport ( !bo->hideInExport() );
1839 void VymModel::copy()
1841 LinkableMapObj *sel=selection.single();
1844 if (redosAvail == 0)
1847 QString s=getSelectString(sel);
1848 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel );
1849 curClipboard=curStep;
1852 // Copy also to global clipboard, because we are at last step in history
1853 QString bakMapName(QString("history-%1").arg(curStep));
1854 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1855 copyDir (bakMapDir,clipboardDir );
1857 clipboardEmpty=false;
1863 void VymModel::pasteNoSave(const int &n)
1865 bool old=blockSaveState;
1866 blockSaveState=true;
1867 bool zippedOrg=zipped;
1868 if (redosAvail > 0 || n!=0)
1870 // Use the "historical" buffer
1871 QString bakMapName(QString("history-%1").arg(n));
1872 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1873 load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
1875 // Use the global buffer
1876 load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
1881 void VymModel::paste()
1883 BranchObj *sel=selection.getBranch();
1886 saveStateChangingPart(
1889 QString ("paste (%1)").arg(curClipboard),
1890 QString("Paste to %1").arg( getObjectName(sel))
1897 void VymModel::cut()
1899 LinkableMapObj *sel=selection.single();
1900 if ( sel && (selection.type() == Selection::Branch ||
1901 selection.type()==Selection::MapCenter ||
1902 selection.type()==Selection::FloatImage))
1904 /* No savestate! savestate is called in cutNoSave
1905 saveStateChangingPart(
1909 QString("Cut %1").arg(getObjectName(sel ))
1918 void VymModel::moveBranchUp()
1920 BranchObj* bo=selection.getBranch();
1924 if (!bo->canMoveBranchUp()) return;
1925 par=(BranchObj*)(bo->getParObj());
1926 BranchObj *obo=par->moveBranchUp (bo); // bo will be the one below selection
1927 saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
1929 //FIXME needed? scene()->update();
1931 ensureSelectionVisible();
1935 void VymModel::moveBranchDown()
1937 BranchObj* bo=selection.getBranch();
1941 if (!bo->canMoveBranchDown()) return;
1942 par=(BranchObj*)(bo->getParObj());
1943 BranchObj *obo=par->moveBranchDown(bo); // bo will be the one above selection
1944 saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
1946 //FIXME needed? scene()->update();
1948 ensureSelectionVisible();
1952 void VymModel::sortChildren()
1954 BranchObj* bo=selection.getBranch();
1957 if(bo->countBranches()>1)
1959 saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
1962 ensureSelectionVisible();
1967 void VymModel::createMapCenter()
1969 MapCenterObj *mco=addMapCenter (QPointF (0,0) );
1970 selection.select (mco);
1973 void VymModel::createBranch()
1975 BranchObj *bo=selection.getBranch();
1978 BranchObj *newbo=addNewBranchInt (-2); // FIXME Old model, merge with below
1981 QList<QVariant> cData;
1982 cData << "VM:createBranch" << "undef"<<"undef";
1983 TreeItem *parti=bo->getTreeItem();
1984 TreeItem *ti=new TreeItem (cData,parti);
1986 ti->setType (TreeItem::Branch);
1987 parti->appendChild (ti);
1991 newbo->setTreeItem (ti);
1992 selection.select (newbo);
1997 MapCenterObj* VymModel::addMapCenter ()
1999 MapCenterObj *mco=addMapCenter (contextPos);
2000 selection.select (mco);
2002 ensureSelectionVisible();
2007 QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
2008 QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
2013 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
2015 MapCenterObj *mapCenter = new MapCenterObj(mapScene);
2016 mapCenter->move (absPos);
2017 mapCenter->setVisibility (true);
2018 mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
2019 mapCenter->setMapEditor(mapEditor); //FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
2020 mapCenters.append(mapCenter);
2023 QList<QVariant> cData;
2024 cData << "VM:addMapCenter" << "undef"<<"undef";
2025 TreeItem *ti=new TreeItem (cData,rootItem);
2026 ti->setLMO (mapCenter);
2027 ti->setType (TreeItem::MapCenter);
2028 mapCenter->setTreeItem (ti);
2029 rootItem->appendChild (ti);
2034 MapCenterObj* VymModel::removeMapCenter(MapCenterObj* mco)
2036 int i=mapCenters.indexOf (mco);
2039 mapCenters.removeAt (i);
2041 if (i>0) return mapCenters.at(i-1); // Return previous MCO
2046 MapCenterObj* VymModel::getLastMapCenter()
2048 if (mapCenters.size()>0)
2049 return mapCenters.last();
2056 BranchObj* VymModel::addNewBranchInt(int num)
2058 // Depending on pos:
2059 // -3 insert in children of parent above selection
2060 // -2 add branch to selection
2061 // -1 insert in children of parent below selection
2062 // 0..n insert in children of parent at pos
2063 BranchObj *newbo=NULL;
2064 BranchObj *bo=selection.getBranch();
2069 // save scroll state. If scrolled, automatically select
2070 // new branch in order to tmp unscroll parent...
2071 newbo=bo->addBranch();
2076 bo=(BranchObj*)bo->getParObj();
2077 if (bo) newbo=bo->insertBranch(num);
2081 bo=(BranchObj*)bo->getParObj();
2082 if (bo) newbo=bo->insertBranch(num);
2084 if (!newbo) return NULL;
2089 BranchObj* VymModel::addNewBranch(int pos)
2091 // Different meaning than num in addNewBranchInt!
2095 BranchObj *bo = selection.getBranch();
2096 BranchObj *newbo=NULL;
2100 // FIXME do we still need this in model? setCursor (Qt::ArrowCursor);
2102 newbo=addNewBranchInt (pos-2);
2110 QString ("addBranch (%1)").arg(pos),
2111 QString ("Add new branch to %1").arg(getObjectName(bo)));
2115 latestSelectionString=getSelectString(newbo);
2116 // In Network mode, the client needs to know where the new branch is,
2117 // so we have to pass on this information via saveState.
2118 // TODO: Get rid of this positioning workaround
2119 QString ps=qpointfToString (newbo->getAbsPos());
2120 sendData ("selectLatestAdded ()");
2121 sendData (QString("move %1").arg(ps));
2129 BranchObj* VymModel::addNewBranchBefore()
2131 BranchObj *newbo=NULL;
2132 BranchObj *bo = selection.getBranch();
2133 if (bo && selection.type()==Selection::Branch)
2134 // We accept no MapCenterObj here, so we _have_ a parent
2136 QPointF p=bo->getRelPos();
2139 BranchObj *parbo=(BranchObj*)(bo->getParObj());
2141 // add below selection
2142 newbo=parbo->insertBranch(bo->getNum()+1);
2145 newbo->move2RelPos (p);
2147 // Move selection to new branch
2148 bo->linkTo (newbo,-1);
2150 saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()",
2151 QString ("Add branch before %1").arg(getObjectName(bo)));
2157 latestSelectionString=selection.getSelectString();
2161 void VymModel::deleteSelection()
2163 BranchObj *bo = selection.getBranch();
2164 if (bo && selection.type()==Selection::MapCenter)
2166 // BranchObj* par=(BranchObj*)(bo->getParObj());
2167 selection.unselect();
2168 /* FIXME Note: does saveStateRemovingPart work for MCO? (No parent!)
2169 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2171 bo=removeMapCenter ((MapCenterObj*)bo);
2174 selection.select (bo);
2175 ensureSelectionVisible();
2181 if (bo && selection.type()==Selection::Branch)
2183 //FIXME need to check if any animObj is part of Branch, remove from animated obj list then
2188 BranchObj* par=(BranchObj*)bo->getParObj();
2189 selection.unselect();
2190 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2191 par->removeBranch(bo);
2192 selection.select (par);
2193 ensureSelectionVisible();
2198 FloatImageObj *fio=selection.getFloatImage();
2201 BranchObj* par=(BranchObj*)fio->getParObj();
2202 saveStateChangingPart(
2206 QString("Delete %1").arg(getObjectName(fio))
2208 selection.unselect();
2209 par->removeFloatImage(fio);
2210 selection.select (par);
2213 ensureSelectionVisible();
2218 void VymModel::deleteKeepChildren()
2220 BranchObj *bo=selection.getBranch();
2224 par=(BranchObj*)(bo->getParObj());
2226 // Don't use this on mapcenter
2229 // Check if we have childs at all to keep
2230 if (bo->countBranches()==0)
2236 QPointF p=bo->getRelPos();
2237 saveStateChangingPart(
2240 "deleteKeepChildren ()",
2241 QString("Remove %1 and keep its children").arg(getObjectName(bo))
2244 QString sel=getSelectString(bo);
2246 par->removeBranchHere(bo);
2249 selection.getBranch()->move2RelPos (p);
2254 void VymModel::deleteChildren()
2256 BranchObj *bo=selection.getBranch();
2259 saveStateChangingPart(
2262 "deleteChildren ()",
2263 QString( "Remove children of branch %1").arg(getObjectName(bo))
2265 bo->removeChildren();
2271 bool VymModel::scrollBranch(BranchObj *bo)
2275 if (bo->isScrolled()) return false;
2276 if (bo->countBranches()==0) return false;
2277 if (bo->getDepth()==0) return false;
2283 QString ("%1 ()").arg(u),
2285 QString ("%1 ()").arg(r),
2286 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2290 // FIXME needed? scene()->update();
2296 bool VymModel::unscrollBranch(BranchObj *bo)
2300 if (!bo->isScrolled()) return false;
2301 if (bo->countBranches()==0) return false;
2302 if (bo->getDepth()==0) return false;
2308 QString ("%1 ()").arg(u),
2310 QString ("%1 ()").arg(r),
2311 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2315 // FIXME needed? scene()->update();
2321 void VymModel::toggleScroll()
2323 BranchObj *bo=selection.getBranch();
2324 if (selection.type()==Selection::Branch )
2326 if (bo->isScrolled())
2327 unscrollBranch (bo);
2333 void VymModel::unscrollChildren()
2335 BranchObj *bo=selection.getBranch();
2341 if (bo->isScrolled()) unscrollBranch (bo);
2346 void VymModel::addFloatImage (const QPixmap &img)
2348 BranchObj *bo=selection.getBranch();
2351 FloatImageObj *fio=bo->addFloatImage();
2353 fio->setOriginalFilename("No original filename (image added by dropevent)");
2354 QString s=getSelectString(bo);
2355 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio );
2356 saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
2358 // FIXME needed? scene()->update();
2363 void VymModel::colorBranch (QColor c)
2365 BranchObj *bo=selection.getBranch();
2370 QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
2372 QString ("colorBranch (\"%1\")").arg(c.name()),
2373 QString("Set color of %1 to %2").arg(getObjectName(bo)).arg(c.name())
2375 bo->setColor(c); // color branch
2379 void VymModel::colorSubtree (QColor c)
2381 BranchObj *bo=selection.getBranch();
2384 saveStateChangingPart(
2387 QString ("colorSubtree (\"%1\")").arg(c.name()),
2388 QString ("Set color of %1 and children to %2").arg(getObjectName(bo)).arg(c.name())
2390 bo->setColorSubtree (c); // color links, color children
2394 QColor VymModel::getCurrentHeadingColor()
2396 BranchObj *bo=selection.getBranch();
2397 if (bo) return bo->getColor();
2399 QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
2405 void VymModel::editURL()
2407 BranchObj *bo=selection.getBranch();
2411 QString text = QInputDialog::getText(
2412 "VYM", tr("Enter URL:"), QLineEdit::Normal,
2413 bo->getURL(), &ok, NULL);
2415 // user entered something and pressed OK
2420 void VymModel::editLocalURL()
2422 BranchObj *bo=selection.getBranch();
2425 QStringList filters;
2426 filters <<"All files (*)";
2427 filters << tr("Text","Filedialog") + " (*.txt)";
2428 filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
2429 filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
2430 filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
2431 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
2432 fd->setFilters (filters);
2433 fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
2434 fd->setDirectory (lastFileDir);
2435 if (! bo->getVymLink().isEmpty() )
2436 fd->selectFile( bo->getURL() );
2439 if ( fd->exec() == QDialog::Accepted )
2441 lastFileDir=QDir (fd->directory().path());
2442 setURL (fd->selectedFile() );
2448 void VymModel::editHeading2URL()
2450 BranchObj *bo=selection.getBranch();
2452 setURL (bo->getHeading());
2455 void VymModel::editBugzilla2URL()
2457 BranchObj *bo=selection.getBranch();
2460 QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
2465 void VymModel::editFATE2URL()
2467 BranchObj *bo=selection.getBranch();
2470 QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
2473 "setURL (\""+bo->getURL()+"\")",
2475 "setURL (\""+url+"\")",
2476 QString("Use heading of %1 as link to FATE").arg(getObjectName(bo))
2483 void VymModel::editVymLink()
2485 BranchObj *bo=selection.getBranch();
2488 QStringList filters;
2489 filters <<"VYM map (*.vym)";
2490 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
2491 fd->setFilters (filters);
2492 fd->setCaption(vymName+" - " +tr("Link to another map"));
2493 fd->setDirectory (lastFileDir);
2494 if (! bo->getVymLink().isEmpty() )
2495 fd->selectFile( bo->getVymLink() );
2499 if ( fd->exec() == QDialog::Accepted )
2501 lastFileDir=QDir (fd->directory().path());
2504 "setVymLink (\""+bo->getVymLink()+"\")",
2506 "setVymLink (\""+fd->selectedFile()+"\")",
2507 QString("Set vymlink of %1 to %2").arg(getObjectName(bo)).arg(fd->selectedFile())
2509 setVymLink (fd->selectedFile() ); // FIXME ok?
2514 void VymModel::setVymLink (const QString &s)
2516 // Internal function, no saveState needed
2517 BranchObj *bo=selection.getBranch();
2524 ensureSelectionVisible();
2528 void VymModel::deleteVymLink()
2530 BranchObj *bo=selection.getBranch();
2535 "setVymLink (\""+bo->getVymLink()+"\")",
2537 "setVymLink (\"\")",
2538 QString("Unset vymlink of %1").arg(getObjectName(bo))
2540 bo->setVymLink ("" );
2543 // FIXME needed? scene()->update();
2547 QString VymModel::getVymLink()
2549 BranchObj *bo=selection.getBranch();
2551 return bo->getVymLink();
2557 QStringList VymModel::getVymLinks()
2560 BranchObj *bo=selection.getBranch();
2566 if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
2574 void VymModel::followXLink(int i)
2576 BranchObj *bo=selection.getBranch();
2579 bo=bo->XLinkTargetAt(i);
2582 selection.select(bo);
2583 ensureSelectionVisible();
2588 void VymModel::editXLink(int i) // FIXME missing saveState
2590 BranchObj *bo=selection.getBranch();
2593 XLinkObj *xlo=bo->XLinkAt(i);
2596 EditXLinkDialog dia;
2598 dia.setSelection(bo);
2599 if (dia.exec() == QDialog::Accepted)
2601 if (dia.useSettingsGlobal() )
2603 setMapDefXLinkColor (xlo->getColor() );
2604 setMapDefXLinkWidth (xlo->getWidth() );
2606 if (dia.deleteXLink())
2607 bo->deleteXLinkAt(i);
2617 //////////////////////////////////////////////
2619 //////////////////////////////////////////////
2621 void VymModel::parseAtom(const QString &atom)
2623 BranchObj *selb=selection.getBranch();
2629 // Split string s into command and parameters
2630 parser.parseAtom (atom);
2631 QString com=parser.getCommand();
2633 // External commands
2634 /////////////////////////////////////////////////////////////////////
2635 if (com=="addBranch")
2637 if (selection.isEmpty())
2639 parser.setError (Aborted,"Nothing selected");
2642 parser.setError (Aborted,"Type of selection is not a branch");
2647 if (parser.checkParCount(pl))
2649 if (parser.parCount()==0)
2653 n=parser.parInt (ok,0);
2654 if (ok ) addNewBranch (n);
2658 /////////////////////////////////////////////////////////////////////
2659 } else if (com=="addBranchBefore")
2661 if (selection.isEmpty())
2663 parser.setError (Aborted,"Nothing selected");
2666 parser.setError (Aborted,"Type of selection is not a branch");
2669 if (parser.parCount()==0)
2671 addNewBranchBefore ();
2674 /////////////////////////////////////////////////////////////////////
2675 } else if (com==QString("addMapCenter"))
2677 if (parser.checkParCount(2))
2679 x=parser.parDouble (ok,0);
2682 y=parser.parDouble (ok,1);
2683 if (ok) addMapCenter (QPointF(x,y));
2686 /////////////////////////////////////////////////////////////////////
2687 } else if (com==QString("addMapReplace"))
2689 if (selection.isEmpty())
2691 parser.setError (Aborted,"Nothing selected");
2694 parser.setError (Aborted,"Type of selection is not a branch");
2695 } else if (parser.checkParCount(1))
2697 //s=parser.parString (ok,0); // selection
2698 t=parser.parString (ok,0); // path to map
2699 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2700 addMapReplaceInt(getSelectString(selb),t);
2702 /////////////////////////////////////////////////////////////////////
2703 } else if (com==QString("addMapInsert"))
2705 if (selection.isEmpty())
2707 parser.setError (Aborted,"Nothing selected");
2710 parser.setError (Aborted,"Type of selection is not a branch");
2713 if (parser.checkParCount(2))
2715 t=parser.parString (ok,0); // path to map
2716 n=parser.parInt(ok,1); // position
2717 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2718 addMapInsertInt(t,n);
2721 /////////////////////////////////////////////////////////////////////
2722 } else if (com=="clearFlags")
2724 if (selection.isEmpty() )
2726 parser.setError (Aborted,"Nothing selected");
2729 parser.setError (Aborted,"Type of selection is not a branch");
2730 } else if (parser.checkParCount(0))
2732 selb->clearStandardFlags();
2733 selb->updateFlagsToolbar();
2735 /////////////////////////////////////////////////////////////////////
2736 } else if (com=="colorBranch")
2738 if (selection.isEmpty())
2740 parser.setError (Aborted,"Nothing selected");
2743 parser.setError (Aborted,"Type of selection is not a branch");
2744 } else if (parser.checkParCount(1))
2746 QColor c=parser.parColor (ok,0);
2747 if (ok) colorBranch (c);
2749 /////////////////////////////////////////////////////////////////////
2750 } else if (com=="colorSubtree")
2752 if (selection.isEmpty())
2754 parser.setError (Aborted,"Nothing selected");
2757 parser.setError (Aborted,"Type of selection is not a branch");
2758 } else if (parser.checkParCount(1))
2760 QColor c=parser.parColor (ok,0);
2761 if (ok) colorSubtree (c);
2763 /////////////////////////////////////////////////////////////////////
2764 } else if (com=="copy")
2766 if (selection.isEmpty())
2768 parser.setError (Aborted,"Nothing selected");
2771 parser.setError (Aborted,"Type of selection is not a branch");
2772 } else if (parser.checkParCount(0))
2774 //FIXME missing action for copy
2776 /////////////////////////////////////////////////////////////////////
2777 } else if (com=="cut")
2779 if (selection.isEmpty())
2781 parser.setError (Aborted,"Nothing selected");
2782 } else if ( selection.type()!=Selection::Branch &&
2783 selection.type()!=Selection::MapCenter &&
2784 selection.type()!=Selection::FloatImage )
2786 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2787 } else if (parser.checkParCount(0))
2791 /////////////////////////////////////////////////////////////////////
2792 } else if (com=="delete")
2794 if (selection.isEmpty())
2796 parser.setError (Aborted,"Nothing selected");
2798 /*else if (selection.type() != Selection::Branch && selection.type() != Selection::FloatImage )
2800 parser.setError (Aborted,"Type of selection is wrong.");
2803 else if (parser.checkParCount(0))
2807 /////////////////////////////////////////////////////////////////////
2808 } else if (com=="deleteKeepChildren")
2810 if (selection.isEmpty())
2812 parser.setError (Aborted,"Nothing selected");
2815 parser.setError (Aborted,"Type of selection is not a branch");
2816 } else if (parser.checkParCount(0))
2818 deleteKeepChildren();
2820 /////////////////////////////////////////////////////////////////////
2821 } else if (com=="deleteChildren")
2823 if (selection.isEmpty())
2825 parser.setError (Aborted,"Nothing selected");
2828 parser.setError (Aborted,"Type of selection is not a branch");
2829 } else if (parser.checkParCount(0))
2833 /////////////////////////////////////////////////////////////////////
2834 } else if (com=="exportASCII")
2838 if (parser.parCount()>=1)
2839 // Hey, we even have a filename
2840 fname=parser.parString(ok,0);
2843 parser.setError (Aborted,"Could not read filename");
2846 exportASCII (fname,false);
2848 /////////////////////////////////////////////////////////////////////
2849 } else if (com=="exportImage")
2853 if (parser.parCount()>=2)
2854 // Hey, we even have a filename
2855 fname=parser.parString(ok,0);
2858 parser.setError (Aborted,"Could not read filename");
2861 QString format="PNG";
2862 if (parser.parCount()>=2)
2864 format=parser.parString(ok,1);
2866 exportImage (fname,false,format);
2868 /////////////////////////////////////////////////////////////////////
2869 } else if (com=="exportXHTML")
2873 if (parser.parCount()>=2)
2874 // Hey, we even have a filename
2875 fname=parser.parString(ok,1);
2878 parser.setError (Aborted,"Could not read filename");
2881 exportXHTML (fname,false);
2883 /////////////////////////////////////////////////////////////////////
2884 } else if (com=="exportXML")
2888 if (parser.parCount()>=2)
2889 // Hey, we even have a filename
2890 fname=parser.parString(ok,1);
2893 parser.setError (Aborted,"Could not read filename");
2896 exportXML (fname,false);
2898 /////////////////////////////////////////////////////////////////////
2899 } else if (com=="importDir")
2901 if (selection.isEmpty())
2903 parser.setError (Aborted,"Nothing selected");
2906 parser.setError (Aborted,"Type of selection is not a branch");
2907 } else if (parser.checkParCount(1))
2909 s=parser.parString(ok,0);
2910 if (ok) importDirInt(s);
2912 /////////////////////////////////////////////////////////////////////
2913 } else if (com=="linkTo")
2915 if (selection.isEmpty())
2917 parser.setError (Aborted,"Nothing selected");
2920 if (parser.checkParCount(4))
2922 // 0 selectstring of parent
2923 // 1 num in parent (for branches)
2924 // 2,3 x,y of mainbranch or mapcenter
2925 s=parser.parString(ok,0);
2926 LinkableMapObj *dst=findObjBySelect (s);
2929 if (typeid(*dst) == typeid(BranchObj) )
2931 // Get number in parent
2932 n=parser.parInt (ok,1);
2935 selb->linkTo ((BranchObj*)(dst),n);
2938 } else if (typeid(*dst) == typeid(MapCenterObj) )
2940 selb->linkTo ((BranchObj*)(dst),-1);
2941 // Get coordinates of mainbranch
2942 x=parser.parDouble(ok,2);
2945 y=parser.parDouble(ok,3);
2955 } else if ( selection.type() == Selection::FloatImage)
2957 if (parser.checkParCount(1))
2959 // 0 selectstring of parent
2960 s=parser.parString(ok,0);
2961 LinkableMapObj *dst=findObjBySelect (s);
2964 if (typeid(*dst) == typeid(BranchObj) ||
2965 typeid(*dst) == typeid(MapCenterObj))
2966 linkFloatImageTo (getSelectString(dst));
2968 parser.setError (Aborted,"Destination is not a branch");
2971 parser.setError (Aborted,"Type of selection is not a floatimage or branch");
2972 /////////////////////////////////////////////////////////////////////
2973 } else if (com=="loadImage")
2975 if (selection.isEmpty())
2977 parser.setError (Aborted,"Nothing selected");
2980 parser.setError (Aborted,"Type of selection is not a branch");
2981 } else if (parser.checkParCount(1))
2983 s=parser.parString(ok,0);
2984 if (ok) loadFloatImageInt (s);
2986 /////////////////////////////////////////////////////////////////////
2987 } else if (com=="moveBranchUp")
2989 if (selection.isEmpty() )
2991 parser.setError (Aborted,"Nothing selected");
2994 parser.setError (Aborted,"Type of selection is not a branch");
2995 } else if (parser.checkParCount(0))
2999 /////////////////////////////////////////////////////////////////////
3000 } else if (com=="moveBranchDown")
3002 if (selection.isEmpty() )
3004 parser.setError (Aborted,"Nothing selected");
3007 parser.setError (Aborted,"Type of selection is not a branch");
3008 } else if (parser.checkParCount(0))
3012 /////////////////////////////////////////////////////////////////////
3013 } else if (com=="move")
3015 if (selection.isEmpty() )
3017 parser.setError (Aborted,"Nothing selected");
3018 } else if ( selection.type()!=Selection::Branch &&
3019 selection.type()!=Selection::MapCenter &&
3020 selection.type()!=Selection::FloatImage )
3022 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3023 } else if (parser.checkParCount(2))
3025 x=parser.parDouble (ok,0);
3028 y=parser.parDouble (ok,1);
3032 /////////////////////////////////////////////////////////////////////
3033 } else if (com=="moveRel")
3035 if (selection.isEmpty() )
3037 parser.setError (Aborted,"Nothing selected");
3038 } else if ( selection.type()!=Selection::Branch &&
3039 selection.type()!=Selection::MapCenter &&
3040 selection.type()!=Selection::FloatImage )
3042 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3043 } else if (parser.checkParCount(2))
3045 x=parser.parDouble (ok,0);
3048 y=parser.parDouble (ok,1);
3049 if (ok) moveRel (x,y);
3052 /////////////////////////////////////////////////////////////////////
3053 } else if (com=="nop")
3055 /////////////////////////////////////////////////////////////////////
3056 } else if (com=="paste")
3058 if (selection.isEmpty() )
3060 parser.setError (Aborted,"Nothing selected");
3063 parser.setError (Aborted,"Type of selection is not a branch");
3064 } else if (parser.checkParCount(1))
3066 n=parser.parInt (ok,0);
3067 if (ok) pasteNoSave(n);
3069 /////////////////////////////////////////////////////////////////////
3070 } else if (com=="qa")
3072 if (selection.isEmpty() )
3074 parser.setError (Aborted,"Nothing selected");
3077 parser.setError (Aborted,"Type of selection is not a branch");
3078 } else if (parser.checkParCount(4))
3081 c=parser.parString (ok,0);
3084 parser.setError (Aborted,"No comment given");
3087 s=parser.parString (ok,1);
3090 parser.setError (Aborted,"First parameter is not a string");
3093 t=parser.parString (ok,2);
3096 parser.setError (Aborted,"Condition is not a string");
3099 u=parser.parString (ok,3);
3102 parser.setError (Aborted,"Third parameter is not a string");
3107 parser.setError (Aborted,"Unknown type: "+s);
3112 parser.setError (Aborted,"Unknown operator: "+t);
3117 parser.setError (Aborted,"Type of selection is not a branch");
3120 if (selb->getHeading() == u)
3122 cout << "PASSED: " << qPrintable (c) << endl;
3125 cout << "FAILED: " << qPrintable (c) << endl;
3135 /////////////////////////////////////////////////////////////////////
3136 } else if (com=="saveImage")
3138 FloatImageObj *fio=selection.getFloatImage();
3141 parser.setError (Aborted,"Type of selection is not an image");
3142 } else if (parser.checkParCount(2))
3144 s=parser.parString(ok,0);
3147 t=parser.parString(ok,1);
3148 if (ok) saveFloatImageInt (fio,t,s);
3151 /////////////////////////////////////////////////////////////////////
3152 } else if (com=="scroll")
3154 if (selection.isEmpty() )
3156 parser.setError (Aborted,"Nothing selected");
3159 parser.setError (Aborted,"Type of selection is not a branch");
3160 } else if (parser.checkParCount(0))
3162 if (!scrollBranch (selb))
3163 parser.setError (Aborted,"Could not scroll branch");
3165 /////////////////////////////////////////////////////////////////////
3166 } else if (com=="select")
3168 if (parser.checkParCount(1))
3170 s=parser.parString(ok,0);
3173 /////////////////////////////////////////////////////////////////////
3174 } else if (com=="selectLastBranch")
3176 if (selection.isEmpty() )
3178 parser.setError (Aborted,"Nothing selected");
3181 parser.setError (Aborted,"Type of selection is not a branch");
3182 } else if (parser.checkParCount(0))
3184 BranchObj *bo=selb->getLastBranch();
3186 parser.setError (Aborted,"Could not select last branch");
3190 /////////////////////////////////////////////////////////////////////
3191 } else if (com=="selectLastImage")
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 FloatImageObj *fio=selb->getLastFloatImage();
3203 parser.setError (Aborted,"Could not select last image");
3207 /////////////////////////////////////////////////////////////////////
3208 } else if (com=="selectLatestAdded")
3210 if (latestSelectionString.isEmpty() )
3212 parser.setError (Aborted,"No latest added object");
3215 if (!select (latestSelectionString))
3216 parser.setError (Aborted,"Could not select latest added object "+latestSelectionString);
3218 /////////////////////////////////////////////////////////////////////
3219 } else if (com=="setFrameType")
3221 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3223 parser.setError (Aborted,"Type of selection does not allow setting frame type");
3225 else if (parser.checkParCount(1))
3227 s=parser.parString(ok,0);
3228 if (ok) setFrameType (s);
3230 /////////////////////////////////////////////////////////////////////
3231 } else if (com=="setFramePenColor")
3233 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3235 parser.setError (Aborted,"Type of selection does not allow setting of pen color");
3237 else if (parser.checkParCount(1))
3239 QColor c=parser.parColor(ok,0);
3240 if (ok) setFramePenColor (c);
3242 /////////////////////////////////////////////////////////////////////
3243 } else if (com=="setFrameBrushColor")
3245 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3247 parser.setError (Aborted,"Type of selection does not allow setting brush color");
3249 else if (parser.checkParCount(1))
3251 QColor c=parser.parColor(ok,0);
3252 if (ok) setFrameBrushColor (c);
3254 /////////////////////////////////////////////////////////////////////
3255 } else if (com=="setFramePadding")
3257 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3259 parser.setError (Aborted,"Type of selection does not allow setting frame padding");
3261 else if (parser.checkParCount(1))
3263 n=parser.parInt(ok,0);
3264 if (ok) setFramePadding(n);
3266 /////////////////////////////////////////////////////////////////////
3267 } else if (com=="setFrameBorderWidth")
3269 if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3271 parser.setError (Aborted,"Type of selection does not allow setting frame border width");
3273 else if (parser.checkParCount(1))
3275 n=parser.parInt(ok,0);
3276 if (ok) setFrameBorderWidth (n);
3278 /////////////////////////////////////////////////////////////////////
3279 } else if (com=="setMapAuthor")
3281 if (parser.checkParCount(1))
3283 s=parser.parString(ok,0);
3284 if (ok) setAuthor (s);
3286 /////////////////////////////////////////////////////////////////////
3287 } else if (com=="setMapComment")
3289 if (parser.checkParCount(1))
3291 s=parser.parString(ok,0);
3292 if (ok) setComment(s);
3294 /////////////////////////////////////////////////////////////////////
3295 } else if (com=="setMapBackgroundColor")
3297 if (selection.isEmpty() )
3299 parser.setError (Aborted,"Nothing selected");
3300 } else if (! selection.getBranch() )
3302 parser.setError (Aborted,"Type of selection is not a branch");
3303 } else if (parser.checkParCount(1))
3305 QColor c=parser.parColor (ok,0);
3306 if (ok) setMapBackgroundColor (c);
3308 /////////////////////////////////////////////////////////////////////
3309 } else if (com=="setMapDefLinkColor")
3311 if (selection.isEmpty() )
3313 parser.setError (Aborted,"Nothing selected");
3316 parser.setError (Aborted,"Type of selection is not a branch");
3317 } else if (parser.checkParCount(1))
3319 QColor c=parser.parColor (ok,0);
3320 if (ok) setMapDefLinkColor (c);
3322 /////////////////////////////////////////////////////////////////////
3323 } else if (com=="setMapLinkStyle")
3325 if (parser.checkParCount(1))
3327 s=parser.parString (ok,0);
3328 if (ok) setMapLinkStyle(s);
3330 /////////////////////////////////////////////////////////////////////
3331 } else if (com=="setHeading")
3333 if (selection.isEmpty() )
3335 parser.setError (Aborted,"Nothing selected");
3338 parser.setError (Aborted,"Type of selection is not a branch");
3339 } else if (parser.checkParCount(1))
3341 s=parser.parString (ok,0);
3345 /////////////////////////////////////////////////////////////////////
3346 } else if (com=="setHideExport")
3348 if (selection.isEmpty() )
3350 parser.setError (Aborted,"Nothing selected");
3351 } else if (selection.type()!=Selection::Branch && selection.type() != Selection::MapCenter &&selection.type()!=Selection::FloatImage)
3353 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3354 } else if (parser.checkParCount(1))
3356 b=parser.parBool(ok,0);
3357 if (ok) setHideExport (b);
3359 /////////////////////////////////////////////////////////////////////
3360 } else if (com=="setIncludeImagesHorizontally")
3362 if (selection.isEmpty() )
3364 parser.setError (Aborted,"Nothing selected");
3367 parser.setError (Aborted,"Type of selection is not a branch");
3368 } else if (parser.checkParCount(1))
3370 b=parser.parBool(ok,0);
3371 if (ok) setIncludeImagesHor(b);
3373 /////////////////////////////////////////////////////////////////////
3374 } else if (com=="setIncludeImagesVertically")
3376 if (selection.isEmpty() )
3378 parser.setError (Aborted,"Nothing selected");
3381 parser.setError (Aborted,"Type of selection is not a branch");
3382 } else if (parser.checkParCount(1))
3384 b=parser.parBool(ok,0);
3385 if (ok) setIncludeImagesVer(b);
3387 /////////////////////////////////////////////////////////////////////
3388 } else if (com=="setHideLinkUnselected")
3390 if (selection.isEmpty() )
3392 parser.setError (Aborted,"Nothing selected");
3393 } else if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
3395 parser.setError (Aborted,"Type of selection does not allow hiding the link");
3396 } else if (parser.checkParCount(1))
3398 b=parser.parBool(ok,0);
3399 if (ok) setHideLinkUnselected(b);
3401 /////////////////////////////////////////////////////////////////////
3402 } else if (com=="setSelectionColor")
3404 if (parser.checkParCount(1))
3406 QColor c=parser.parColor (ok,0);
3407 if (ok) setSelectionColorInt (c);
3409 /////////////////////////////////////////////////////////////////////
3410 } else if (com=="setURL")
3412 if (selection.isEmpty() )
3414 parser.setError (Aborted,"Nothing selected");
3417 parser.setError (Aborted,"Type of selection is not a branch");
3418 } else if (parser.checkParCount(1))
3420 s=parser.parString (ok,0);
3423 /////////////////////////////////////////////////////////////////////
3424 } else if (com=="setVymLink")
3426 if (selection.isEmpty() )
3428 parser.setError (Aborted,"Nothing selected");
3431 parser.setError (Aborted,"Type of selection is not a branch");
3432 } else if (parser.checkParCount(1))
3434 s=parser.parString (ok,0);
3435 if (ok) setVymLink(s);
3438 /////////////////////////////////////////////////////////////////////
3439 else if (com=="setFlag")
3441 if (selection.isEmpty() )
3443 parser.setError (Aborted,"Nothing selected");
3446 parser.setError (Aborted,"Type of selection is not a branch");
3447 } else if (parser.checkParCount(1))
3449 s=parser.parString(ok,0);
3452 selb->activateStandardFlag(s);
3453 selb->updateFlagsToolbar();
3456 /////////////////////////////////////////////////////////////////////
3457 } else if (com=="setFrameType")
3459 if (selection.isEmpty() )
3461 parser.setError (Aborted,"Nothing selected");
3464 parser.setError (Aborted,"Type of selection is not a branch");
3465 } else if (parser.checkParCount(1))
3467 s=parser.parString(ok,0);
3471 /////////////////////////////////////////////////////////////////////
3472 } else if (com=="sortChildren")
3474 if (selection.isEmpty() )
3476 parser.setError (Aborted,"Nothing selected");
3479 parser.setError (Aborted,"Type of selection is not a branch");
3480 } else if (parser.checkParCount(0))
3484 /////////////////////////////////////////////////////////////////////
3485 } else if (com=="toggleFlag")
3487 if (selection.isEmpty() )
3489 parser.setError (Aborted,"Nothing selected");
3492 parser.setError (Aborted,"Type of selection is not a branch");
3493 } else if (parser.checkParCount(1))
3495 s=parser.parString(ok,0);
3498 selb->toggleStandardFlag(s);
3499 selb->updateFlagsToolbar();
3502 /////////////////////////////////////////////////////////////////////
3503 } else if (com=="unscroll")
3505 if (selection.isEmpty() )
3507 parser.setError (Aborted,"Nothing selected");
3510 parser.setError (Aborted,"Type of selection is not a branch");
3511 } else if (parser.checkParCount(0))
3513 if (!unscrollBranch (selb))
3514 parser.setError (Aborted,"Could not unscroll branch");
3516 /////////////////////////////////////////////////////////////////////
3517 } else if (com=="unscrollChildren")
3519 if (selection.isEmpty() )
3521 parser.setError (Aborted,"Nothing selected");
3524 parser.setError (Aborted,"Type of selection is not a branch");
3525 } else if (parser.checkParCount(0))
3527 unscrollChildren ();
3529 /////////////////////////////////////////////////////////////////////
3530 } else if (com=="unsetFlag")
3532 if (selection.isEmpty() )
3534 parser.setError (Aborted,"Nothing selected");
3537 parser.setError (Aborted,"Type of selection is not a branch");
3538 } else if (parser.checkParCount(1))
3540 s=parser.parString(ok,0);
3543 selb->deactivateStandardFlag(s);
3544 selb->updateFlagsToolbar();
3548 parser.setError (Aborted,"Unknown command");
3551 if (parser.errorLevel()==NoError)
3553 // setChanged(); FIXME should not be called e.g. for export?!
3558 // TODO Error handling
3559 qWarning("VymModel::parseAtom: Error!");
3560 qWarning(parser.errorMessage());
3564 void VymModel::runScript (QString script)
3566 parser.setScript (script);
3568 while (parser.next() )
3569 parseAtom(parser.getAtom());
3572 void VymModel::setExportMode (bool b)
3574 // should be called before and after exports
3575 // depending on the settings
3576 if (b && settings.value("/export/useHideExport","true")=="true")
3577 setHideTmpMode (HideExport);
3579 setHideTmpMode (HideNone);
3582 void VymModel::exportImage(QString fname, bool askName, QString format)
3586 fname=getMapName()+".png";
3593 QFileDialog *fd=new QFileDialog (NULL);
3594 fd->setCaption (tr("Export map as image"));
3595 fd->setDirectory (lastImageDir);
3596 fd->setFileMode(QFileDialog::AnyFile);
3597 fd->setFilters (imageIO.getFilters() );
3600 fl=fd->selectedFiles();
3602 format=imageIO.getType(fd->selectedFilter());
3606 setExportMode (true);
3607 QPixmap pix (getPixmap());
3608 pix.save(fname, format);
3609 setExportMode (false);
3613 void VymModel::exportXML(QString dir, bool askForName)
3617 dir=browseDirectory(NULL,tr("Export XML to directory"));
3618 if (dir =="" && !reallyWriteDirectory(dir) )
3622 // Hide stuff during export, if settings want this
3623 setExportMode (true);
3625 // Create subdirectories
3628 // write to directory
3629 QString saveFile=saveToDir (dir,mapName+"-",true,getTotalBBox().topLeft() ,NULL);
3632 file.setName ( dir + "/"+mapName+".xml");
3633 if ( !file.open( QIODevice::WriteOnly ) )
3635 // This should neverever happen
3636 QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
3640 // Write it finally, and write in UTF8, no matter what
3641 QTextStream ts( &file );
3642 ts.setEncoding (QTextStream::UnicodeUTF8);
3646 // Now write image, too
3647 exportImage (dir+"/images/"+mapName+".png",false,"PNG");
3649 setExportMode (false);
3652 void VymModel::exportASCII(QString fname,bool askName)
3657 ex.setFile (mapName+".txt");
3663 //ex.addFilter ("TXT (*.txt)");
3664 ex.setDir(lastImageDir);
3665 //ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
3670 setExportMode(true);
3672 setExportMode(false);
3676 void VymModel::exportXHTML (const QString &dir, bool askForName)
3678 ExportXHTMLDialog dia(NULL);
3679 dia.setFilePath (filePath );
3680 dia.setMapName (mapName );
3682 if (dir!="") dia.setDir (dir);
3688 if (dia.exec()!=QDialog::Accepted)
3692 QDir d (dia.getDir());
3693 // Check, if warnings should be used before overwriting
3694 // the output directory
3695 if (d.exists() && d.count()>0)
3698 warn.showCancelButton (true);
3699 warn.setText(QString(
3700 "The directory %1 is not empty.\n"
3701 "Do you risk to overwrite some of its contents?").arg(d.path() ));
3702 warn.setCaption("Warning: Directory not empty");
3703 warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
3705 if (warn.exec()!=QDialog::Accepted) ok=false;
3712 exportXML (dia.getDir(),false );
3713 dia.doExport(mapName );
3714 //if (dia.hasChanged()) setChanged();
3718 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
3723 if (ex.setConfigFile(cf))
3725 setExportMode (true);
3726 ex.exportPresentation();
3727 setExportMode (false);
3734 //////////////////////////////////////////////
3736 //////////////////////////////////////////////
3738 void VymModel::registerEditor(QWidget *me)
3740 mapEditor=(MapEditor*)me;
3741 for (int i=0; i<mapCenters.count(); i++)
3742 mapCenters.at(i)->setMapEditor(mapEditor);
3745 void VymModel::unregisterEditor(QWidget *)
3750 void VymModel::setContextPos(QPointF p)
3755 void VymModel::unsetContextPos()
3757 contextPos=QPointF();
3760 void VymModel::updateNoteFlag()
3763 BranchObj *bo=selection.getBranch();
3766 bo->updateNoteFlag();
3767 mainWindow->updateActions();
3771 void VymModel::updateRelPositions()
3773 for (int i=0; i<mapCenters.count(); i++)
3774 mapCenters.at(i)->updateRelPositions();
3777 void VymModel::reposition()
3779 for (int i=0;i<mapCenters.count(); i++)
3780 mapCenters.at(i)->reposition(); // for positioning heading
3783 QPolygonF VymModel::shape(BranchObj *bo)
3785 // Creating (arbitrary) shapes
3788 QRectF rb=bo->getBBox();
3789 if (bo->getDepth()==0)
3791 // Just take BBox of this mapCenter
3792 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
3796 // Take union of BBox and TotalBBox
3798 QRectF ra=bo->getTotalBBox();
3799 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
3802 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
3805 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
3807 p <<ra.bottomRight()
3809 <<QPointF (rb.topRight().x(), ra.topRight().y() )
3812 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
3817 void VymModel::moveAway(LinkableMapObj *lmo)
3821 // Move all branches and MapCenters away from lmo
3822 // to avoid collisions
3827 BranchObj *boA=(BranchObj*)lmo;
3829 for (int i=0; i<mapCenters.count(); i++)
3831 boB=mapCenters.at(i);
3834 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
3837 <<" ("<<qPrintable(boA->getHeading() )<<")"
3838 <<" with ("<< qPrintable (boB->getHeading() )
3841 <<" minT="<<r.minTranslation<<endl<<endl;
3845 QPixmap VymModel::getPixmap()
3847 QRectF mapRect=getTotalBBox();
3848 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
3851 pp.setRenderHints(mapEditor->renderHints());
3853 // Don't print the visualisation of selection
3854 selection.unselect();
3856 mapScene->render ( &pp,
3857 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
3858 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
3860 // Restore selection
3861 selection.reselect();
3867 void VymModel::setMapLinkStyle (const QString & s)
3870 if (linkstyle==LinkableMapObj::Line)
3872 else if (linkstyle==LinkableMapObj::Parabel)
3873 snow="StyleParabel";
3874 else if (linkstyle==LinkableMapObj::PolyLine)
3875 snow="StylePolyLine";
3876 else if (linkstyle==LinkableMapObj::PolyParabel)
3877 snow="StyleParabel";
3880 QString("setMapLinkStyle (\"%1\")").arg(s),
3881 QString("setMapLinkStyle (\"%1\")").arg(snow),
3882 QString("Set map link style (\"%1\")").arg(s)
3886 linkstyle=LinkableMapObj::Line;
3887 else if (s=="StyleParabel")
3888 linkstyle=LinkableMapObj::Parabel;
3889 else if (s=="StylePolyLine")
3890 linkstyle=LinkableMapObj::PolyLine;
3892 linkstyle=LinkableMapObj::PolyParabel;
3899 bo->setLinkStyle(bo->getDefLinkStyle());
3905 LinkableMapObj::Style VymModel::getMapLinkStyle ()
3910 void VymModel::setMapDefLinkColor(QColor col)
3912 if ( !col.isValid() ) return;
3914 QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
3915 QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
3916 QString("Set map link color to %1").arg(col.name())
3930 void VymModel::setMapLinkColorHintInt()
3932 // called from setMapLinkColorHint(lch) or at end of parse
3942 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
3945 setMapLinkColorHintInt();
3948 void VymModel::toggleMapLinkColorHint()
3950 if (linkcolorhint==LinkableMapObj::HeadingColor)
3951 linkcolorhint=LinkableMapObj::DefaultColor;
3953 linkcolorhint=LinkableMapObj::HeadingColor;
3963 void VymModel::selectMapBackgroundImage ()
3965 Q3FileDialog *fd=new Q3FileDialog( NULL);
3966 fd->setMode (Q3FileDialog::ExistingFile);
3967 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
3968 ImagePreview *p =new ImagePreview (fd);
3969 fd->setContentsPreviewEnabled( TRUE );
3970 fd->setContentsPreview( p, p );
3971 fd->setPreviewMode( Q3FileDialog::Contents );
3972 fd->setCaption(vymName+" - " +tr("Load background image"));
3973 fd->setDir (lastImageDir);
3976 if ( fd->exec() == QDialog::Accepted )
3978 // TODO selectMapBackgroundImg in QT4 use: lastImageDir=fd->directory();
3979 lastImageDir=QDir (fd->dirPath());
3980 setMapBackgroundImage (fd->selectedFile());
3984 void VymModel::setMapBackgroundImage (const QString &fn) //FIXME missing savestate
3986 QColor oldcol=mapScene->backgroundBrush().color();
3990 QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
3992 QString ("setMapBackgroundImage (%1)").arg(col.name()),
3993 QString("Set background color of map to %1").arg(col.name()));
3996 brush.setTextureImage (QPixmap (fn));
3997 mapScene->setBackgroundBrush(brush);
4000 void VymModel::selectMapBackgroundColor()
4002 QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
4003 if ( !col.isValid() ) return;
4004 setMapBackgroundColor( col );
4008 void VymModel::setMapBackgroundColor(QColor col)
4010 QColor oldcol=mapScene->backgroundBrush().color();
4012 QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
4013 QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
4014 QString("Set background color of map to %1").arg(col.name()));
4015 mapScene->setBackgroundBrush(col);
4018 QColor VymModel::getMapBackgroundColor()
4020 return mapScene->backgroundBrush().color();
4024 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint()
4026 return linkcolorhint;
4029 QColor VymModel::getMapDefLinkColor()
4031 return defLinkColor;
4034 void VymModel::setMapDefXLinkColor(QColor col)
4039 QColor VymModel::getMapDefXLinkColor()
4041 return defXLinkColor;
4044 void VymModel::setMapDefXLinkWidth (int w)
4049 int VymModel::getMapDefXLinkWidth()
4051 return defXLinkWidth;
4054 void VymModel::move(const double &x, const double &y)
4056 LinkableMapObj *sel=selection.single();
4059 QPointF ap(sel->getAbsPos());
4063 QString ps=qpointfToString(ap);
4064 QString s=selection.getSelectString();
4067 s, "move "+qpointfToString(to),
4068 QString("Move %1 to %2").arg(getObjectName(sel)).arg(ps));
4076 void VymModel::moveRel (const double &x, const double &y)
4078 LinkableMapObj *sel=selection.single();
4081 QPointF rp(sel->getRelPos());
4085 QString ps=qpointfToString (sel->getRelPos());
4086 QString s=getSelectString(sel);
4089 s, "moveRel "+qpointfToString(to),
4090 QString("Move %1 to relative position %2").arg(getObjectName(sel)).arg(ps));
4091 ((OrnamentedObj*)sel)->move2RelPos (x,y);
4100 void VymModel::animate()
4102 animationTimer->stop();
4105 while (i<animObjList.size() )
4107 bo=(BranchObj*)animObjList.at(i);
4112 animObjList.removeAt(i);
4119 mapEditor->updateSelection();
4121 if (!animObjList.isEmpty()) animationTimer->start();
4125 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
4127 if (bo && bo->getDepth()>0)
4130 ap.setStart (start);
4132 ap.setTicks (animationTicks);
4133 ap.setAnimated (true);
4134 bo->setAnimation (ap);
4135 animObjList.append( bo );
4136 animationTimer->setSingleShot (true);
4137 animationTimer->start(animationInterval);
4141 void VymModel::stopAnimation (MapObj *mo)
4143 int i=animObjList.indexOf(mo);
4145 animObjList.removeAt (i);
4148 void VymModel::sendSelection()
4150 if (netstate!=Server) return;
4151 sendData (QString("select (\"%1\")").arg(selection.getSelectString()) );
4154 void VymModel::newServer()
4158 tcpServer = new QTcpServer(this);
4159 if (!tcpServer->listen(QHostAddress::Any,port)) {
4160 QMessageBox::critical(NULL, "vym server",
4161 QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
4162 //FIXME needed? we are no widget any longer... close();
4165 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
4167 cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
4170 void VymModel::connectToServer()
4173 server="salam.suse.de";
4175 clientSocket = new QTcpSocket (this);
4176 clientSocket->abort();
4177 clientSocket->connectToHost(server ,port);
4178 connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
4179 connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
4180 this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
4182 cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
4187 void VymModel::newClient()
4189 QTcpSocket *newClient = tcpServer->nextPendingConnection();
4190 connect(newClient, SIGNAL(disconnected()),
4191 newClient, SLOT(deleteLater()));
4193 cout <<"ME::newClient at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
4195 clientList.append (newClient);
4199 void VymModel::sendData(const QString &s)
4201 if (clientList.size()==0) return;
4203 // Create bytearray to send
4205 QDataStream out(&block, QIODevice::WriteOnly);
4206 out.setVersion(QDataStream::Qt_4_0);
4208 // Reserve some space for blocksize
4211 // Write sendCounter
4212 out << sendCounter++;
4217 // Go back and write blocksize so far
4218 out.device()->seek(0);
4219 quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
4223 cout << "ME::sendData bs="<<bs<<" counter="<<sendCounter<<" s="<<qPrintable(s)<<endl;
4225 for (int i=0; i<clientList.size(); ++i)
4227 //cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
4228 clientList.at(i)->write (block);
4232 void VymModel::readData ()
4234 while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
4237 cout <<"readData bytesAvail="<<clientSocket->bytesAvailable();
4241 QDataStream in(clientSocket);
4242 in.setVersion(QDataStream::Qt_4_0);
4250 cout << " t="<<qPrintable (t)<<endl;
4256 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
4258 switch (socketError) {
4259 case QAbstractSocket::RemoteHostClosedError:
4261 case QAbstractSocket::HostNotFoundError:
4262 QMessageBox::information(NULL, vymName +" Network client",
4263 "The host was not found. Please check the "
4264 "host name and port settings.");
4266 case QAbstractSocket::ConnectionRefusedError:
4267 QMessageBox::information(NULL, vymName + " Network client",
4268 "The connection was refused by the peer. "
4269 "Make sure the fortune server is running, "
4270 "and check that the host name and port "
4271 "settings are correct.");
4274 QMessageBox::information(NULL, vymName + " Network client",
4275 QString("The following error occurred: %1.")
4276 .arg(clientSocket->errorString()));
4283 void VymModel::selectMapSelectionColor()
4285 QColor col = QColorDialog::getColor( defLinkColor, NULL);
4286 setSelectionColor (col);
4289 void VymModel::setSelectionColorInt (QColor col)
4291 if ( !col.isValid() ) return;
4293 QString("setSelectionColor (%1)").arg(selection.getColor().name()),
4294 QString("setSelectionColor (%1)").arg(col.name()),
4295 QString("Set color of selection box to %1").arg(col.name())
4298 selection.setColor (col);
4301 void VymModel::updateSelection()
4306 void VymModel::setSelectionColor(QColor col)
4308 if ( !col.isValid() ) return;
4310 QString("setSelectionColor (%1)").arg(selection.getColor().name()),
4311 QString("setSelectionColor (%1)").arg(col.name()),
4312 QString("Set color of selection box to %1").arg(col.name())
4314 setSelectionColorInt (col);
4317 QColor VymModel::getSelectionColor()
4319 return selection.getColor();
4322 void VymModel::setHideTmpMode (HideTmpMode mode)
4325 for (int i=0;i<mapCenters.count(); i++)
4326 mapCenters.at(i)->setHideTmp (mode);
4328 // FIXME needed? scene()->update();
4332 QRectF VymModel::getTotalBBox()
4335 for (int i=0;i<mapCenters.count(); i++)
4336 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
4340 //////////////////////////////////////////////
4341 // Selection related
4342 //////////////////////////////////////////////
4344 void VymModel::setSelectionBlocked (bool b)
4349 selection.unblock();
4352 bool VymModel::isSelectionBlocked()
4354 return selection.isBlocked();
4357 bool VymModel::select (const QString &s)
4359 if (selection.select(s))
4362 ensureSelectionVisible();
4370 bool VymModel::select (LinkableMapObj *lmo)
4372 if (selection.select(lmo))
4375 ensureSelectionVisible();
4382 void VymModel::unselect()
4384 selection.unselect();
4387 void VymModel::reselect()
4389 selection.reselect();
4392 void VymModel::ensureSelectionVisible()
4394 LinkableMapObj *lmo=selection.single();
4395 if (lmo &&mapEditor) mapEditor->ensureVisible (lmo->getBBox() );
4399 void VymModel::selectInt (LinkableMapObj *lmo)
4401 if (selection.select(lmo))
4409 void VymModel::selectNextBranchInt()
4411 // Increase number of branch
4412 LinkableMapObj *sel=selection.single();
4415 QString s=selection.getSelectString();
4421 part=s.section(",",-1);
4423 num=part.right(part.length() - 3);
4425 s=s.left (s.length() -num.length());
4428 num=QString ("%1").arg(num.toUInt()+1);
4432 // Try to select this one
4433 if (select (s)) return;
4435 // We have no direct successor,
4436 // try to increase the parental number in order to
4437 // find a successor with same depth
4439 int d=selection.single()->getDepth();
4444 while (!found && d>0)
4446 s=s.section (",",0,d-1);
4447 // replace substring of current depth in s with "1"
4448 part=s.section(",",-1);
4450 num=part.right(part.length() - 3);
4454 // increase number of parent
4455 num=QString ("%1").arg(num.toUInt()+1);
4456 s=s.section (",",0,d-2) + ","+ typ+num;
4459 // Special case, look at orientation
4460 if (selection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
4461 num=QString ("%1").arg(num.toUInt()+1);
4463 num=QString ("%1").arg(num.toUInt()-1);
4468 // pad to oldDepth, select the first branch for each depth
4469 for (i=d;i<oldDepth;i++)
4474 if ( selection.getBranch()->countBranches()>0)
4482 // try to select the freshly built string
4490 void VymModel::selectPrevBranchInt()
4492 // Decrease number of branch
4493 BranchObj *bo=selection.getBranch();
4496 QString s=selection.getSelectString();
4502 part=s.section(",",-1);
4504 num=part.right(part.length() - 3);
4506 s=s.left (s.length() -num.length());
4508 int n=num.toInt()-1;
4511 num=QString ("%1").arg(n);
4514 // Try to select this one
4515 if (n>=0 && select (s)) return;
4517 // We have no direct precessor,
4518 // try to decrease the parental number in order to
4519 // find a precessor with same depth
4521 int d=selection.single()->getDepth();
4526 while (!found && d>0)
4528 s=s.section (",",0,d-1);
4529 // replace substring of current depth in s with "1"
4530 part=s.section(",",-1);
4532 num=part.right(part.length() - 3);
4536 // decrease number of parent
4537 num=QString ("%1").arg(num.toInt()-1);
4538 s=s.section (",",0,d-2) + ","+ typ+num;
4541 // Special case, look at orientation
4542 if (selection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
4543 num=QString ("%1").arg(num.toInt()-1);
4545 num=QString ("%1").arg(num.toInt()+1);
4550 // pad to oldDepth, select the last branch for each depth
4551 for (i=d;i<oldDepth;i++)
4555 if ( selection.getBranch()->countBranches()>0)
4556 s+=",bo:"+ QString ("%1").arg( selection.getBranch()->countBranches()-1 );
4563 // try to select the freshly built string
4571 void VymModel::selectUpperBranch()
4573 if (selection.isBlocked() ) return;
4575 BranchObj *bo=selection.getBranch();
4576 if (bo && selection.type()==Selection::Branch)
4578 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4579 selectPrevBranchInt();
4581 if (bo->getDepth()==1)
4582 selectNextBranchInt();
4584 selectPrevBranchInt();
4588 void VymModel::selectLowerBranch()
4590 if (selection.isBlocked() ) return;
4592 BranchObj *bo=selection.getBranch();
4593 if (bo && selection.type()==Selection::Branch)
4595 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4596 selectNextBranchInt();
4598 if (bo->getDepth()==1)
4599 selectPrevBranchInt();
4601 selectNextBranchInt();
4606 void VymModel::selectLeftBranch()
4608 if (selection.isBlocked() ) return;
4612 LinkableMapObj *sel=selection.single();
4615 if (selection.type()== Selection::MapCenter)
4617 par=selection.getBranch();
4618 bo=par->getLastSelectedBranch();
4621 // Workaround for reselecting on left and right side
4622 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4623 bo=par->getLastBranch();
4626 bo=par->getLastBranch();
4627 selection.select(bo);
4629 ensureSelectionVisible();
4635 par=(BranchObj*)(sel->getParObj());
4636 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4638 if (selection.type() == Selection::Branch ||
4639 selection.type() == Selection::FloatImage)
4641 selection.select(par);
4643 ensureSelectionVisible();
4648 if (selection.type() == Selection::Branch )
4650 bo=selection.getBranch()->getLastSelectedBranch();
4653 selection.select(bo);
4655 ensureSelectionVisible();
4664 void VymModel::selectRightBranch()
4666 if (selection.isBlocked() ) return;
4670 LinkableMapObj *sel=selection.single();
4673 if (selection.type()==Selection::MapCenter)
4675 par=selection.getBranch();
4676 bo=par->getLastSelectedBranch();
4679 // Workaround for reselecting on left and right side
4680 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
4681 bo=par->getFirstBranch();
4684 selection.select(bo);
4686 ensureSelectionVisible();
4692 par=(BranchObj*)(selection.single()->getParObj());
4693 if (selection.single()->getOrientation()==LinkableMapObj::LeftOfCenter)
4695 if (selection.type() == Selection::Branch ||
4696 selection.type() == Selection::FloatImage)
4698 selection.select(par);
4700 ensureSelectionVisible();
4705 if (selection.type() == Selection::Branch)
4707 bo=selection.getBranch()->getLastSelectedBranch();
4710 selection.select(bo);
4712 ensureSelectionVisible();
4721 void VymModel::selectFirstBranch()
4723 BranchObj *bo1=selection.getBranch();
4728 par=(BranchObj*)(bo1->getParObj());
4730 bo2=par->getFirstBranch();
4732 selection.select(bo2);
4734 ensureSelectionVisible();
4740 void VymModel::selectLastBranch()
4742 BranchObj *bo1=selection.getBranch();
4747 par=(BranchObj*)(bo1->getParObj());
4749 bo2=par->getLastBranch();
4752 selection.select(bo2);
4754 ensureSelectionVisible();
4760 void VymModel::selectParent()
4762 LinkableMapObj *lmo=selection.first();
4766 par=(BranchObj*)(lmo->getParObj());
4768 selection.select(par);
4770 ensureSelectionVisible();
4775 Selection::Type VymModel::selectionType()
4777 return selection.type();
4780 LinkableMapObj* VymModel::getSelection()
4782 return selection.single();
4784 BranchObj* VymModel::getSelectedBranch()
4786 return selection.getBranch();
4789 FloatImageObj* VymModel::getSelectedFloatImage()
4791 return selection.getFloatImage();
4794 QString VymModel::getSelectString ()
4796 return selection.getSelectString();
4799 QString VymModel::getSelectString (LinkableMapObj *lmo)
4803 if (typeid(*lmo)==typeid(BranchObj) ||
4804 typeid(*lmo)==typeid(MapCenterObj) )
4806 LinkableMapObj *par=lmo->getParObj();
4809 if (lmo->getDepth() ==1)
4810 // Mainbranch, return
4811 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4813 // Branch, call myself recursively
4814 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4818 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
4819 if (i>=0) s=QString("mc:%1").arg(i);