1 #include <QApplication>
6 #include "editxlinkdialog.h"
8 #include "exportxhtmldialog.h"
10 #include "geometry.h" // for addBBox
11 #include "mainwindow.h"
12 #include "mapcenterobj.h"
15 #include "selection.h"
18 #include "warningdialog.h"
19 #include "xml-freemind.h"
25 extern Main *mainWindow;
26 extern Settings settings;
27 extern QString tmpVymDir;
29 extern TextEditor *textEditor;
31 extern QString clipboardDir;
32 extern QString clipboardFile;
33 extern bool clipboardEmpty;
35 extern ImageIO imageIO;
37 extern QString vymName;
38 extern QString vymVersion;
39 extern QDir vymBaseDir;
41 extern QDir lastImageDir;
42 extern QDir lastFileDir;
44 extern FlagRowObj *standardFlagsDefault;
46 extern Settings settings;
50 int VymModel::mapNum=0; // make instance
54 // cout << "Const VymModel\n";
61 // cout << "Destr VymModel\n";
62 autosaveTimer->stop();
63 fileChangedTimer->stop();
67 void VymModel::clear()
69 cout << "VymModel::clear rows="<<rowCount(index(rootItem))<<endl;
70 selModel->clearSelection();
73 while (!mapCenters.isEmpty()) // FIXME VM needs to be in treemodel only...
74 delete mapCenters.takeFirst();
76 QModelIndex ri=index(rootItem);
77 removeRows (0, rowCount(ri),ri);
80 void VymModel::init ()
82 // We should have at least one map center to start with
83 // addMapCenter(); FIXME VM create this in MapEditor as long as model is part of that
88 // Also no scene yet (should not be needed anyway) FIXME VM
101 stepsTotal=settings.readNumEntry("/history/stepsTotal",100);
102 undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
103 mainWindow->updateHistory (undoSet);
106 makeTmpDirectories();
111 fileName=tr("unnamed");
113 blockReposition=false;
114 blockSaveState=false;
116 autosaveTimer=new QTimer (this);
117 connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
119 fileChangedTimer=new QTimer (this);
120 fileChangedTimer->start(3000);
121 connect(fileChangedTimer, SIGNAL(timeout()), this, SLOT(fileChanged()));
132 animationUse=settings.readBoolEntry("/animation/use",false);
133 animationTicks=settings.readNumEntry("/animation/ticks",10);
134 animationInterval=settings.readNumEntry("/animation/interval",50);
136 animationTimer=new QTimer (this);
137 connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
140 defLinkColor=QColor (0,0,255);
141 defXLinkColor=QColor (180,180,180);
142 linkcolorhint=LinkableMapObj::DefaultColor;
143 linkstyle=LinkableMapObj::PolyParabel;
145 defXLinkColor=QColor (230,230,230);
155 // addMapCenter(); FIXME VM create this in MapEditor until BO and MCO are independent of scene
159 void VymModel::makeTmpDirectories()
161 // Create unique temporary directories
162 tmpMapDir = tmpVymDir+QString("/model-%1").arg(mapNum);
163 histPath = tmpMapDir+"/history";
169 MapEditor* VymModel::getMapEditor() // FIXME VM better return favourite editor here
174 bool VymModel::isRepositionBlocked()
176 return blockReposition;
179 void VymModel::updateActions() // FIXME maybe don't update if blockReposition is set
181 // Tell mainwindow to update states of actions
182 mainWindow->updateActions();
187 QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
189 // tmpdir temporary directory to which data will be written
190 // prefix mapname, which will be appended to images etc.
191 // writeflags Only write flags for "real" save of map, not undo
192 // offset offset of bbox of whole map in scene.
193 // Needed for XML export
202 case LinkableMapObj::Line:
205 case LinkableMapObj::Parabel:
208 case LinkableMapObj::PolyLine:
212 ls="StylePolyParabel";
216 QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
218 if (linkcolorhint==LinkableMapObj::HeadingColor)
219 colhint=xml.attribut("linkColorHint","HeadingColor");
221 QString mapAttr=xml.attribut("version",vymVersion);
223 mapAttr+= xml.attribut("author",author) +
224 xml.attribut("comment",comment) +
225 xml.attribut("date",getDate()) +
226 xml.attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
227 xml.attribut("selectionColor", mapEditor->getSelectionColor().name() ) +
228 xml.attribut("linkStyle", ls ) +
229 xml.attribut("linkColor", defLinkColor.name() ) +
230 xml.attribut("defXLinkColor", defXLinkColor.name() ) +
231 xml.attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
233 s+=xml.beginElement("vymmap",mapAttr);
236 // Find the used flags while traversing the tree
237 standardFlagsDefault->resetUsedCounter();
239 // Reset the counters before saving
240 // TODO constr. of FIO creates lots of objects, better do this in some other way...
241 FloatImageObj (mapScene).resetSaveCounter();
243 // Build xml recursivly
244 if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
245 // Save complete map, if saveSel not set
246 s+=saveToDir(tmpdir,prefix,writeflags,offset);
249 if ( typeid(*saveSel) == typeid(BranchObj) )
251 s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
252 else if ( typeid(*saveSel) == typeid(FloatImageObj) )
254 s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
257 // Save local settings
258 s+=settings.getDataXML (destPath);
261 if (!selection.isEmpty() && !saveSel )
262 s+=xml.valueElement("select",selection.getSelectString());
265 s+=xml.endElement("vymmap");
268 standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
272 void VymModel::setFilePath(QString fpath, QString destname)
274 if (fpath.isEmpty() || fpath=="")
281 filePath=fpath; // becomes absolute path
282 fileName=fpath; // gets stripped of path
283 destPath=destname; // needed for vymlinks and during load to reset fileChangedTime
285 // If fpath is not an absolute path, complete it
286 filePath=QDir(fpath).absPath();
287 fileDir=filePath.left (1+filePath.findRev ("/"));
289 // Set short name, too. Search from behind:
290 int i=fileName.findRev("/");
291 if (i>=0) fileName=fileName.remove (0,i+1);
293 // Forget the .vym (or .xml) for name of map
294 mapName=fileName.left(fileName.findRev(".",-1,true) );
298 void VymModel::setFilePath(QString fpath)
300 setFilePath (fpath,fpath);
303 QString VymModel::getFilePath()
308 QString VymModel::getFileName()
313 QString VymModel::getMapName()
318 QString VymModel::getDestPath()
323 ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
325 ErrorCode err=success;
327 parseBaseHandler *handler;
331 case VymMap: handler=new parseVYMHandler; break;
332 case FreemindMap : handler=new parseFreemindHandler; break;
334 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
335 "Unknown FileType in VymModel::load()");
339 bool zipped_org=zipped;
343 selModel->clearSelection();
344 // FIXME VM not needed??? model->setMapEditor(this);
345 // (map state is set later at end of load...)
348 BranchObj *bo=getSelectedBranch();
349 if (!bo) return aborted;
350 if (lmode==ImportAdd)
351 saveStateChangingPart(
354 QString("addMapInsert (%1)").arg(fname),
355 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
357 saveStateChangingPart(
360 QString("addMapReplace(%1)").arg(fname),
361 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
365 // Create temporary directory for packing
367 QString tmpZipDir=makeTmpDir (ok,"vym-pack");
370 QMessageBox::critical( 0, tr( "Critical Load Error" ),
371 tr("Couldn't create temporary directory before load\n"));
376 err=unzipDir (tmpZipDir,fname);
386 // Look for mapname.xml
387 xmlfile= fname.left(fname.findRev(".",-1,true));
388 xmlfile=xmlfile.section( '/', -1 );
389 QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
390 if (!mfile.exists() )
392 // mapname.xml does not exist, well,
393 // maybe someone renamed the mapname.vym file...
394 // Try to find any .xml in the toplevel
395 // directory of the .vym file
396 QStringList flist=QDir (tmpZipDir).entryList("*.xml");
397 if (flist.count()==1)
399 // Only one entry, take this one
400 xmlfile=tmpZipDir + "/"+flist.first();
403 for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it )
404 *it=tmpZipDir + "/" + *it;
405 // TODO Multiple entries, load all (but only the first one into this ME)
406 //mainWindow->fileLoadFromTmp (flist);
407 //returnCode=1; // Silently forget this attempt to load
408 qWarning ("MainWindow::load (fn) multimap found...");
411 if (flist.isEmpty() )
413 QMessageBox::critical( 0, tr( "Critical Load Error" ),
414 tr("Couldn't find a map (*.xml) in .vym archive.\n"));
417 } //file doesn't exist
419 xmlfile=mfile.name();
422 QFile file( xmlfile);
424 // I am paranoid: file should exist anyway
425 // according to check in mainwindow.
428 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
429 tr(QString("Couldn't open map %1").arg(file.name())));
433 bool blockSaveStateOrg=blockSaveState;
434 blockReposition=true;
436 QXmlInputSource source( file);
437 QXmlSimpleReader reader;
438 reader.setContentHandler( handler );
439 reader.setErrorHandler( handler );
440 handler->setModel ( this);
443 // We need to set the tmpDir in order to load files with rel. path
448 tmpdir=fname.left(fname.findRev("/",-1));
449 handler->setTmpDir (tmpdir);
450 handler->setInputFile (file.name());
451 handler->setLoadMode (lmode);
452 bool ok = reader.parse( source );
453 blockReposition=false;
454 blockSaveState=blockSaveStateOrg;
458 reposition(); // FIXME VM reposition the view instead...
465 autosaveTimer->stop();
468 // Reset timestamp to check for later updates of file
469 fileChangedTime=QFileInfo (destPath).lastModified();
472 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
473 tr( handler->errorProtocol() ) );
475 // Still return "success": the map maybe at least
476 // partially read by the parser
481 removeDir (QDir(tmpZipDir));
483 // Restore original zip state
490 ErrorCode VymModel::save (const SaveMode &savemode)
494 QString safeFilePath;
496 ErrorCode err=success;
500 mapFileName=mapName+".xml";
502 // use name given by user, even if he chooses .doc
503 mapFileName=fileName;
505 // Look, if we should zip the data:
508 QMessageBox mb( vymName,
509 tr("The map %1\ndid not use the compressed "
510 "vym file format.\nWriting it uncompressed will also write images \n"
511 "and flags and thus may overwrite files in the "
512 "given directory\n\nDo you want to write the map").arg(filePath),
513 QMessageBox::Warning,
514 QMessageBox::Yes | QMessageBox::Default,
516 QMessageBox::Cancel | QMessageBox::Escape);
517 mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
518 mb.setButtonText( QMessageBox::No, tr("uncompressed") );
519 mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
522 case QMessageBox::Yes:
523 // save compressed (default file format)
526 case QMessageBox::No:
530 case QMessageBox::Cancel:
537 // First backup existing file, we
538 // don't want to add to old zip archives
542 if ( settings.value ("/mapeditor/writeBackupFile").toBool())
544 QString backupFileName(destPath + "~");
545 QFile backupFile(backupFileName);
546 if (backupFile.exists() && !backupFile.remove())
548 QMessageBox::warning(0, tr("Save Error"),
549 tr("%1\ncould not be removed before saving").arg(backupFileName));
551 else if (!f.rename(backupFileName))
553 QMessageBox::warning(0, tr("Save Error"),
554 tr("%1\ncould not be renamed before saving").arg(destPath));
561 // Create temporary directory for packing
563 tmpZipDir=makeTmpDir (ok,"vym-zip");
566 QMessageBox::critical( 0, tr( "Critical Load Error" ),
567 tr("Couldn't create temporary directory before save\n"));
571 safeFilePath=filePath;
572 setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
575 // Create mapName and fileDir
576 makeSubDirs (fileDir);
579 if (savemode==CompleteMap || selection.isEmpty())
582 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
585 autosaveTimer->stop();
590 if (selectionType()==TreeItem::Image)
593 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),getSelectedBranch());
594 // TODO take care of multiselections
597 if (!saveStringToDisk(fileDir+mapFileName,saveFile))
600 qWarning ("ME::saveStringToDisk failed!");
606 if (err==success) err=zipDir (tmpZipDir,destPath);
609 removeDir (QDir(tmpZipDir));
611 // Restore original filepath outside of tmp zip dir
612 setFilePath (safeFilePath);
616 fileChangedTime=QFileInfo (destPath).lastModified();
620 void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)
622 QString pathDir=path.left(path.findRev("/"));
628 // We need to parse saved XML data
629 parseVYMHandler handler;
630 QXmlInputSource source( file);
631 QXmlSimpleReader reader;
632 reader.setContentHandler( &handler );
633 reader.setErrorHandler( &handler );
634 handler.setModel ( this);
635 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
636 if (undoSel.isEmpty())
640 handler.setLoadMode (NewMap);
644 handler.setLoadMode (ImportReplace);
646 blockReposition=true;
647 bool ok = reader.parse( source );
648 blockReposition=false;
651 // This should never ever happen
652 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
653 handler.errorProtocol());
656 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
659 void VymModel::addMapInsertInt (const QString &path, int pos)
661 BranchObj *sel=getSelectedBranch();
664 QString pathDir=path.left(path.findRev("/"));
670 // We need to parse saved XML data
671 parseVYMHandler handler;
672 QXmlInputSource source( file);
673 QXmlSimpleReader reader;
674 reader.setContentHandler( &handler );
675 reader.setErrorHandler( &handler );
676 handler.setModel (this);
677 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
678 handler.setLoadMode (ImportAdd);
679 blockReposition=true;
680 bool ok = reader.parse( source );
681 blockReposition=false;
684 // This should never ever happen
685 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
686 handler.errorProtocol());
688 if (sel->getDepth()>0)
689 sel->getLastBranch()->linkTo (sel,pos);
691 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
695 FloatImageObj* VymModel::loadFloatImageInt (QString fn)
697 TreeItem *fi=createImage();
700 FloatImageObj *fio= ((FloatImageObj*)fi->getLMO());
708 void VymModel::loadFloatImage ()
710 BranchObj *bo=getSelectedBranch();
714 Q3FileDialog *fd=new Q3FileDialog( NULL);
715 fd->setMode (Q3FileDialog::ExistingFiles);
716 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
717 ImagePreview *p =new ImagePreview (fd);
718 fd->setContentsPreviewEnabled( TRUE );
719 fd->setContentsPreview( p, p );
720 fd->setPreviewMode( Q3FileDialog::Contents );
721 fd->setCaption(vymName+" - " +tr("Load image"));
722 fd->setDir (lastImageDir);
725 if ( fd->exec() == QDialog::Accepted )
727 // TODO loadFIO in QT4 use: lastImageDir=fd->directory();
728 lastImageDir=QDir (fd->dirPath());
731 for (int j=0; j<fd->selectedFiles().count(); j++)
733 s=fd->selectedFiles().at(j);
734 fio=loadFloatImageInt (s);
737 (LinkableMapObj*)fio,
740 QString ("loadImage (%1)").arg(s ),
741 QString("Add image %1 to %2").arg(s).arg(getObjectName(bo))
744 // TODO loadFIO error handling
745 qWarning ("Failed to load "+s);
753 void VymModel::saveFloatImageInt (FloatImageObj *fio, const QString &type, const QString &fn)
758 void VymModel::saveFloatImage ()
760 FloatImageObj *fio=selection.getFloatImage();
763 QFileDialog *fd=new QFileDialog( NULL);
764 fd->setFilters (imageIO.getFilters());
765 fd->setCaption(vymName+" - " +tr("Save image"));
766 fd->setFileMode( QFileDialog::AnyFile );
767 fd->setDirectory (lastImageDir);
768 // fd->setSelection (fio->getOriginalFilename());
772 if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
774 fn=fd->selectedFiles().at(0);
775 if (QFile (fn).exists() )
777 QMessageBox mb( vymName,
778 tr("The file %1 exists already.\n"
779 "Do you want to overwrite it?").arg(fn),
780 QMessageBox::Warning,
781 QMessageBox::Yes | QMessageBox::Default,
782 QMessageBox::Cancel | QMessageBox::Escape,
783 QMessageBox::NoButton );
785 mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
786 mb.setButtonText( QMessageBox::No, tr("Cancel"));
789 case QMessageBox::Yes:
792 case QMessageBox::Cancel:
799 saveFloatImageInt (fio,fd->selectedFilter(),fn );
806 void VymModel::importDirInt(BranchObj *dst, QDir d)
808 BranchObj *bo=getSelectedBranch();
811 // Traverse directories
812 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
813 QFileInfoList list = d.entryInfoList();
816 for (int i = 0; i < list.size(); ++i)
819 if (fi.fileName() != "." && fi.fileName() != ".." )
822 bo=dst->getLastBranch();
823 bo->setHeading (fi.fileName() );
824 bo->setColor (QColor("blue"));
826 if ( !d.cd(fi.fileName()) )
827 QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
830 // Recursively add subdirs
837 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
838 list = d.entryInfoList();
840 for (int i = 0; i < list.size(); ++i)
844 bo=dst->getLastBranch();
845 bo->setHeading (fi.fileName() );
846 bo->setColor (QColor("black"));
847 if (fi.fileName().right(4) == ".vym" )
848 bo->setVymLink (fi.filePath());
853 void VymModel::importDirInt (const QString &s)
855 BranchObj *bo=getSelectedBranch();
858 saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
865 void VymModel::importDir()
867 BranchObj *bo=getSelectedBranch();
871 filters <<"VYM map (*.vym)";
872 QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
873 fd->setMode (QFileDialog::DirectoryOnly);
874 fd->setFilters (filters);
875 fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
879 if ( fd->exec() == QDialog::Accepted )
881 importDirInt (fd->selectedFile() );
883 //FIXME VM needed? scene()->update();
889 void VymModel::autosave()
891 QDateTime now=QDateTime().currentDateTime();
893 // Disable autosave, while we have gone back in history
894 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
895 if (redosAvail>0) return;
897 // Also disable autosave for new map without filename
898 if (filePath.isEmpty()) return;
901 if (mapUnsaved &&mapChanged && settings.value ("/autosave/use",true).toBool() )
903 if (QFileInfo(filePath).lastModified()<=fileChangedTime)
904 mainWindow->fileSave (this);
907 cout <<" ME::autosave rejected, file on disk is newer than last save.\n";
912 void VymModel::fileChanged()
914 // Check if file on disk has changed meanwhile
915 if (!filePath.isEmpty())
917 QDateTime tmod=QFileInfo (filePath).lastModified();
918 if (tmod>fileChangedTime)
920 // FIXME VM switch to current mapeditor and finish lineedits...
921 QMessageBox mb( vymName,
922 tr("The file of the map on disk has changed:\n\n"
923 " %1\n\nDo you want to reload that map with the new file?").arg(filePath),
924 QMessageBox::Question,
926 QMessageBox::Cancel | QMessageBox::Default,
927 QMessageBox::NoButton );
929 mb.setButtonText( QMessageBox::Yes, tr("Reload"));
930 mb.setButtonText( QMessageBox::No, tr("Ignore"));
933 case QMessageBox::Yes:
935 load (filePath,NewMap,fileType);
936 case QMessageBox::Cancel:
937 fileChangedTime=tmod; // allow autosave to overwrite newer file!
943 bool VymModel::isDefault()
948 void VymModel::makeDefault()
954 bool VymModel::hasChanged()
959 void VymModel::setChanged()
962 autosaveTimer->start(settings.value("/autosave/ms/",300000).toInt());
970 QString VymModel::getObjectName (const LinkableMapObj *lmo)
973 if (!lmo) return QString("Error: NULL has no name!");
975 if ((typeid(*lmo) == typeid(BranchObj) ||
976 typeid(*lmo) == typeid(MapCenterObj)))
979 s=(((BranchObj*)lmo)->getHeading());
980 if (s=="") s="unnamed";
981 return QString("branch (%1)").arg(s);
983 if ((typeid(*lmo) == typeid(FloatImageObj) ))
984 return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
985 return QString("Unknown type has no name!");
988 void VymModel::redo()
990 // Can we undo at all?
991 if (redosAvail<1) return;
993 bool blockSaveStateOrg=blockSaveState;
998 if (undosAvail<stepsTotal) undosAvail++;
1000 if (curStep>stepsTotal) curStep=1;
1001 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1002 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1003 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1004 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1005 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1006 QString version=undoSet.readEntry ("/history/version");
1008 /* TODO Maybe check for version, if we save the history
1009 if (!checkVersion(version))
1010 QMessageBox::warning(0,tr("Warning"),
1011 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1014 // Find out current undo directory
1015 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1019 cout << "VymModel::redo() begin\n";
1020 cout << " undosAvail="<<undosAvail<<endl;
1021 cout << " redosAvail="<<redosAvail<<endl;
1022 cout << " curStep="<<curStep<<endl;
1023 cout << " ---------------------------"<<endl;
1024 cout << " comment="<<comment.toStdString()<<endl;
1025 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1026 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1027 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1028 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1029 cout << " ---------------------------"<<endl<<endl;
1032 // select object before redo
1033 if (!redoSelection.isEmpty())
1034 select (redoSelection);
1037 parseAtom (redoCommand);
1040 blockSaveState=blockSaveStateOrg;
1042 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1043 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1044 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1045 undoSet.writeSettings(histPath);
1047 mainWindow->updateHistory (undoSet);
1050 /* TODO remove testing
1051 cout << "ME::redo() end\n";
1052 cout << " undosAvail="<<undosAvail<<endl;
1053 cout << " redosAvail="<<redosAvail<<endl;
1054 cout << " curStep="<<curStep<<endl;
1055 cout << " ---------------------------"<<endl<<endl;
1061 bool VymModel::isRedoAvailable()
1063 if (undoSet.readNumEntry("/history/redosAvail",0)>0)
1069 void VymModel::undo()
1071 // Can we undo at all?
1072 if (undosAvail<1) return;
1074 mainWindow->statusMessage (tr("Autosave disabled during undo."));
1076 bool blockSaveStateOrg=blockSaveState;
1077 blockSaveState=true;
1079 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1080 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1081 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1082 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1083 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1084 QString version=undoSet.readEntry ("/history/version");
1086 /* TODO Maybe check for version, if we save the history
1087 if (!checkVersion(version))
1088 QMessageBox::warning(0,tr("Warning"),
1089 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1092 // Find out current undo directory
1093 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1095 // select object before undo
1096 if (!undoSelection.isEmpty())
1097 select (undoSelection);
1101 cout << "VymModel::undo() begin\n";
1102 cout << " undosAvail="<<undosAvail<<endl;
1103 cout << " redosAvail="<<redosAvail<<endl;
1104 cout << " curStep="<<curStep<<endl;
1105 cout << " ---------------------------"<<endl;
1106 cout << " comment="<<comment.toStdString()<<endl;
1107 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1108 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1109 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1110 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1111 cout << " ---------------------------"<<endl<<endl;
1113 parseAtom (undoCommand);
1118 if (curStep<1) curStep=stepsTotal;
1122 blockSaveState=blockSaveStateOrg;
1123 /* TODO remove testing
1124 cout << "VymModel::undo() end\n";
1125 cout << " undosAvail="<<undosAvail<<endl;
1126 cout << " redosAvail="<<redosAvail<<endl;
1127 cout << " curStep="<<curStep<<endl;
1128 cout << " ---------------------------"<<endl<<endl;
1131 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1132 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1133 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1134 undoSet.writeSettings(histPath);
1136 mainWindow->updateHistory (undoSet);
1139 ensureSelectionVisible();
1142 bool VymModel::isUndoAvailable()
1144 if (undoSet.readNumEntry("/history/undosAvail",0)>0)
1150 void VymModel::gotoHistoryStep (int i)
1152 // Restore variables
1153 int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
1154 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
1156 if (i<0) i=undosAvail+redosAvail;
1158 // Clicking above current step makes us undo things
1161 for (int j=0; j<undosAvail-i; j++) undo();
1164 // Clicking below current step makes us redo things
1166 for (int j=undosAvail; j<i; j++)
1168 if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
1172 // And ignore clicking the current row ;-)
1176 QString VymModel::getHistoryPath()
1178 QString histName(QString("history-%1").arg(curStep));
1179 return (tmpMapDir+"/"+histName);
1182 void VymModel::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
1184 sendData(redoCom); //FIXME testing
1189 if (blockSaveState) return;
1191 if (debug) cout << "ME::saveState() for "<<qPrintable (mapName)<<endl;
1193 // Find out current undo directory
1194 if (undosAvail<stepsTotal) undosAvail++;
1196 if (curStep>stepsTotal) curStep=1;
1198 QString backupXML="";
1199 QString histDir=getHistoryPath();
1200 QString bakMapPath=histDir+"/map.xml";
1202 // Create histDir if not available
1205 makeSubDirs (histDir);
1207 // Save depending on how much needs to be saved
1209 backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
1211 QString undoCommand="";
1212 if (savemode==UndoCommand)
1214 undoCommand=undoCom;
1216 else if (savemode==PartOfMap )
1218 undoCommand=undoCom;
1219 undoCommand.replace ("PATH",bakMapPath);
1222 if (!backupXML.isEmpty())
1223 // Write XML Data to disk
1224 saveStringToDisk (bakMapPath,backupXML);
1226 // We would have to save all actions in a tree, to keep track of
1227 // possible redos after a action. Possible, but we are too lazy: forget about redos.
1230 // Write the current state to disk
1231 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1232 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1233 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1234 undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
1235 undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
1236 undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
1237 undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
1238 undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
1239 undoSet.setEntry (QString("/history/version"),vymVersion);
1240 undoSet.writeSettings(histPath);
1244 // TODO remove after testing
1245 //cout << " into="<< histPath.toStdString()<<endl;
1246 cout << " stepsTotal="<<stepsTotal<<
1247 ", undosAvail="<<undosAvail<<
1248 ", redosAvail="<<redosAvail<<
1249 ", curStep="<<curStep<<endl;
1250 cout << " ---------------------------"<<endl;
1251 cout << " comment="<<comment.toStdString()<<endl;
1252 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1253 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1254 cout << " redoCom="<<redoCom.toStdString()<<endl;
1255 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1256 if (saveSel) cout << " saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
1257 cout << " ---------------------------"<<endl;
1260 mainWindow->updateHistory (undoSet);
1266 void VymModel::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
1268 // save the selected part of the map, Undo will replace part of map
1269 QString undoSelection="";
1271 undoSelection=getSelectString(undoSel);
1273 qWarning ("VymModel::saveStateChangingPart no undoSel given!");
1274 QString redoSelection="";
1276 redoSelection=getSelectString(undoSel);
1278 qWarning ("VymModel::saveStateChangingPart no redoSel given!");
1281 saveState (PartOfMap,
1282 undoSelection, "addMapReplace (\"PATH\")",
1288 void VymModel::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
1292 qWarning ("VymModel::saveStateRemovingPart no redoSel given!");
1295 QString undoSelection=getSelectString (redoSel->getParObj());
1296 QString redoSelection=getSelectString(redoSel);
1297 if (typeid(*redoSel) == typeid(BranchObj) )
1299 // save the selected branch of the map, Undo will insert part of map
1300 saveState (PartOfMap,
1301 undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
1302 redoSelection, "delete ()",
1309 void VymModel::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment)
1311 // "Normal" savestate: save commands, selections and comment
1312 // so just save commands for undo and redo
1313 // and use current selection
1315 QString redoSelection="";
1316 if (redoSel) redoSelection=getSelectString(redoSel);
1317 QString undoSelection="";
1318 if (undoSel) undoSelection=getSelectString(undoSel);
1320 saveState (UndoCommand,
1327 void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment)
1329 // "Normal" savestate: save commands, selections and comment
1330 // so just save commands for undo and redo
1331 // and use current selection
1332 saveState (UndoCommand,
1339 void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment)
1341 // "Normal" savestate applied to model (no selection needed):
1342 // save commands and comment
1343 saveState (UndoCommand,
1351 QGraphicsScene* VymModel::getScene ()
1356 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
1358 LinkableMapObj *lmo;
1360 for (int i=0;i<mapCenters.count(); i++)
1362 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
1363 if (lmo) return lmo;
1368 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
1370 LinkableMapObj *lmo;
1376 part=s.section(",",0,0);
1378 num=part.right(part.length() - 3);
1379 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
1380 return mapCenters.at(num.toInt() );
1383 for (int i=0; i<mapCenters.count(); i++)
1385 lmo=mapCenters.at(i)->findObjBySelect(s);
1386 if (lmo) return lmo;
1391 LinkableMapObj* VymModel::findID (const QString &s)
1393 LinkableMapObj *lmo;
1394 for (int i=0; i<mapCenters.count(); i++)
1396 lmo=mapCenters.at(i)->findID (s);
1397 if (lmo) return lmo;
1402 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
1406 for (int i=0; i<mapCenters.count(); i++)
1407 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
1411 //////////////////////////////////////////////
1413 //////////////////////////////////////////////
1414 void VymModel::setVersion (const QString &s)
1419 void VymModel::setAuthor (const QString &s)
1422 QString ("setMapAuthor (\"%1\")").arg(author),
1423 QString ("setMapAuthor (\"%1\")").arg(s),
1424 QString ("Set author of map to \"%1\"").arg(s)
1430 QString VymModel::getAuthor()
1435 void VymModel::setComment (const QString &s)
1438 QString ("setMapComment (\"%1\")").arg(comment),
1439 QString ("setMapComment (\"%1\")").arg(s),
1440 QString ("Set comment of map")
1446 QString VymModel::getComment ()
1451 QString VymModel::getDate ()
1453 return QDate::currentDate().toString ("yyyy-MM-dd");
1456 void VymModel::setHeading(const QString &s)
1458 BranchObj *sel=getSelectedBranch();
1463 "setHeading (\""+sel->getHeading()+"\")",
1465 "setHeading (\""+s+"\")",
1466 QString("Set heading of %1 to \"%2\"").arg(getObjectName(sel)).arg(s) );
1467 sel->setHeading(s );
1468 /* FIXME testing only
1470 TreeItem *ti=getSelectedItem();
1474 //FIXME VM ix is wrong ModelIndex below, ix2 is (hopefully) correct:
1475 //QModelIndex ix=index( ti->row(), ti->column(), index (0,0,QModelIndex()) );
1476 //FIXME VM testing only cout <<"VM::setHeading s="<<s.toStdString()<<" ti="<<ti<<" r,c=("<<ti->row()<<","<<ti->column()<<")"<<endl;
1477 QModelIndex ix2=index (ti);
1478 emit (dataChanged ( ix2,ix2));
1482 cout << "Warning: VM::setHeading ti==NULL\n";
1485 //cout <<" (r,c)=("<<ix2.row()<<","<<ix2.column()<<")"<<endl;
1489 ensureSelectionVisible();
1493 BranchObj* VymModel::findText (QString s, bool cs) // FIXME needs to converted to first() next() next (ti)
1496 QTextDocument::FindFlags flags=0;
1497 if (cs) flags=QTextDocument::FindCaseSensitively;
1500 { // Nothing found or new find process
1502 // nothing found, start again
1506 bool searching=true;
1507 bool foundNote=false;
1508 while (searching && !EOFind)
1512 // Searching in Note
1513 if (itFind->getNote().contains(s,cs))
1515 if (getSelectedBranch()!=itFind)
1518 ensureSelectionVisible();
1520 if (textEditor->findText(s,flags))
1526 // Searching in Heading
1527 if (searching && itFind->getHeading().contains (s,cs) )
1530 ensureSelectionVisible();
1536 itFind=next(itFind);
1537 if (!itFind) EOFind=true;
1539 //cout <<"still searching... "<<qPrintable( itFind->getHeading())<<endl;
1542 return getSelectedBranch();
1548 void VymModel::findReset()
1549 { // Necessary if text to find changes during a find process
1556 void VymModel::setScene (QGraphicsScene *s)
1558 cout << "VM::setscene scene="<<s<<endl;
1559 mapScene=s; // FIXME VM should not be necessary anymore, move all occurences to MapEditor
1560 //init(); // Here we have a mapScene set,
1561 // which is (still) needed to create MapCenters
1564 void VymModel::setURL(const QString &url)
1566 BranchObj *bo=getSelectedBranch();
1569 QString oldurl=bo->getURL();
1573 QString ("setURL (\"%1\")").arg(oldurl),
1575 QString ("setURL (\"%1\")").arg(url),
1576 QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
1581 ensureSelectionVisible();
1585 QString VymModel::getURL()
1587 BranchObj *bo=getSelectedBranch();
1589 return bo->getURL();
1594 QStringList VymModel::getURLs()
1597 BranchObj *bo=getSelectedBranch();
1603 if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
1610 void VymModel::linkFloatImageTo(const QString &dstString)
1612 FloatImageObj *fio=selection.getFloatImage();
1615 BranchObj *dst=(BranchObj*)findObjBySelect(dstString);
1616 if (dst && (typeid(*dst)==typeid (BranchObj) ||
1617 typeid(*dst)==typeid (MapCenterObj)))
1619 LinkableMapObj *dstPar=dst->getParObj();
1620 QString parString=getSelectString(dstPar);
1621 QString fioPreSelectString=getSelectString(fio);
1622 QString fioPreParentSelectString=getSelectString (fio->getParObj());
1623 ((BranchObj*)dst)->addFloatImage (fio);
1624 selection.unselect();
1625 ((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
1626 fio=((BranchObj*)dst)->getLastFloatImage();
1629 selection.select(fio);
1631 getSelectString(fio),
1632 QString("linkTo (\"%1\")").arg(fioPreParentSelectString),
1634 QString ("linkTo (\"%1\")").arg(dstString),
1635 QString ("Link floatimage to %1").arg(getObjectName(dst)));
1641 void VymModel::setFrameType(const FrameObj::FrameType &t)
1643 BranchObj *bo=getSelectedBranch();
1646 QString s=bo->getFrameTypeName();
1647 bo->setFrameType (t);
1648 saveState (bo, QString("setFrameType (\"%1\")").arg(s),
1649 bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
1655 void VymModel::setFrameType(const QString &s)
1657 BranchObj *bo=getSelectedBranch();
1660 saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
1661 bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
1662 bo->setFrameType (s);
1668 void VymModel::setFramePenColor(const QColor &c)
1670 BranchObj *bo=getSelectedBranch();
1673 saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
1674 bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
1675 bo->setFramePenColor (c);
1679 void VymModel::setFrameBrushColor(const QColor &c)
1681 BranchObj *bo=getSelectedBranch();
1684 saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
1685 bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
1686 bo->setFrameBrushColor (c);
1690 void VymModel::setFramePadding (const int &i)
1692 BranchObj *bo=getSelectedBranch();
1695 saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
1696 bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
1697 bo->setFramePadding (i);
1703 void VymModel::setFrameBorderWidth(const int &i)
1705 BranchObj *bo=getSelectedBranch();
1708 saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
1709 bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
1710 bo->setFrameBorderWidth (i);
1716 void VymModel::setIncludeImagesVer(bool b)
1718 BranchObj *bo=getSelectedBranch();
1721 QString u= b ? "false" : "true";
1722 QString r=!b ? "false" : "true";
1726 QString("setIncludeImagesVertically (%1)").arg(u),
1728 QString("setIncludeImagesVertically (%1)").arg(r),
1729 QString("Include images vertically in %1").arg(getObjectName(bo))
1731 bo->setIncludeImagesVer(b);
1736 void VymModel::setIncludeImagesHor(bool b)
1738 BranchObj *bo=getSelectedBranch();
1741 QString u= b ? "false" : "true";
1742 QString r=!b ? "false" : "true";
1746 QString("setIncludeImagesHorizontally (%1)").arg(u),
1748 QString("setIncludeImagesHorizontally (%1)").arg(r),
1749 QString("Include images horizontally in %1").arg(getObjectName(bo))
1751 bo->setIncludeImagesHor(b);
1756 void VymModel::setHideLinkUnselected (bool b)
1758 LinkableMapObj *sel=getSelectedLMO();
1760 (selectionType() == TreeItem::Branch ||
1761 selectionType() == TreeItem::MapCenter ||
1762 selectionType() == TreeItem::Image ))
1764 QString u= b ? "false" : "true";
1765 QString r=!b ? "false" : "true";
1769 QString("setHideLinkUnselected (%1)").arg(u),
1771 QString("setHideLinkUnselected (%1)").arg(r),
1772 QString("Hide link of %1 if unselected").arg(getObjectName(sel))
1774 sel->setHideLinkUnselected(b);
1778 void VymModel::setHideExport(bool b)
1780 BranchObj *bo=getSelectedBranch();
1783 bo->setHideInExport (b);
1784 QString u= b ? "false" : "true";
1785 QString r=!b ? "false" : "true";
1789 QString ("setHideExport (%1)").arg(u),
1791 QString ("setHideExport (%1)").arg(r),
1792 QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bo)).arg (r)
1797 // FIXME VM needed? scene()->update();
1801 void VymModel::toggleHideExport()
1803 BranchObj *bo=getSelectedBranch();
1805 setHideExport ( !bo->hideInExport() );
1809 void VymModel::copy()
1811 LinkableMapObj *sel=getSelectedLMO();
1813 (selectionType() == TreeItem::Branch ||
1814 selectionType() == TreeItem::MapCenter ||
1815 selectionType() == TreeItem::Image ))
1817 if (redosAvail == 0)
1820 QString s=getSelectString(sel);
1821 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel );
1822 curClipboard=curStep;
1825 // Copy also to global clipboard, because we are at last step in history
1826 QString bakMapName(QString("history-%1").arg(curStep));
1827 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1828 copyDir (bakMapDir,clipboardDir );
1830 clipboardEmpty=false;
1836 void VymModel::pasteNoSave(const int &n)
1838 bool old=blockSaveState;
1839 blockSaveState=true;
1840 bool zippedOrg=zipped;
1841 if (redosAvail > 0 || n!=0)
1843 // Use the "historical" buffer
1844 QString bakMapName(QString("history-%1").arg(n));
1845 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1846 load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
1848 // Use the global buffer
1849 load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
1854 void VymModel::paste()
1856 BranchObj *sel=getSelectedBranch();
1859 saveStateChangingPart(
1862 QString ("paste (%1)").arg(curClipboard),
1863 QString("Paste to %1").arg( getObjectName(sel))
1870 void VymModel::cut()
1872 LinkableMapObj *sel=getSelectedLMO();
1873 if ( sel && (selectionType() == TreeItem::Branch ||
1874 selectionType()==TreeItem::MapCenter ||
1875 selectionType()==TreeItem::Image))
1877 /* No savestate! savestate is called in cutNoSave
1878 saveStateChangingPart(
1882 QString("Cut %1").arg(getObjectName(sel ))
1891 void VymModel::moveBranchUp()
1893 BranchObj* bo=getSelectedBranch();
1897 if (!bo->canMoveBranchUp()) return;
1898 par=(BranchObj*)(bo->getParObj());
1899 BranchObj *obo=par->moveBranchUp (bo); // bo will be the one below selection
1900 saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
1902 //FIXME VM needed? scene()->update();
1904 ensureSelectionVisible();
1908 void VymModel::moveBranchDown()
1910 BranchObj* bo=getSelectedBranch();
1914 if (!bo->canMoveBranchDown()) return;
1915 par=(BranchObj*)(bo->getParObj());
1916 BranchObj *obo=par->moveBranchDown(bo); // bo will be the one above selection
1917 saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
1919 //FIXME VM needed? scene()->update();
1921 ensureSelectionVisible();
1925 void VymModel::sortChildren()
1927 BranchObj* bo=getSelectedBranch();
1930 if(bo->countBranches()>1)
1932 saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
1935 ensureSelectionVisible();
1940 void VymModel::createMapCenter()
1942 MapCenterObj *mco=addMapCenter (QPointF (0,0) );
1946 void VymModel::createBranch()
1948 addNewBranchInt (-2);
1952 TreeItem* VymModel::createImage()
1954 BranchObj *bo=getSelectedBranch();
1957 FloatImageObj *newfio=bo->addFloatImage(); // FIXME VM Old model, merge with below
1960 QList<QVariant> cData;
1961 cData << "VM:createImage" << "undef"<<"undef";
1962 TreeItem *parti=bo->getTreeItem();
1963 TreeItem *ti=new TreeItem (cData,parti);
1964 ti->setLMO (newfio);
1965 ti->setType (TreeItem::Image);
1966 parti->appendChild (ti);
1970 newfio->setTreeItem (ti);
1971 select (newfio); // FIXME VM really needed here?
1978 MapCenterObj* VymModel::addMapCenter ()
1980 MapCenterObj *mco=addMapCenter (contextPos);
1981 cout <<"VM::addMCO () mapScene="<<mapScene<<endl;
1982 selection.select (mco);
1984 ensureSelectionVisible();
1989 QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
1990 QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
1995 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
1997 MapCenterObj *mapCenter = new MapCenterObj(mapScene,this);
1998 mapCenter->setMapEditor(mapEditor); //FIXME VM needed to get defLinkStyle, mapLinkColorHint ... for later added objects
1999 mapCenter->move (absPos);
2000 mapCenter->setVisibility (true);
2001 mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
2002 mapCenters.append(mapCenter);
2005 QList<QVariant> cData;
2006 cData << "VM:addMapCenter" << "undef"<<"undef";
2007 TreeItem *ti=new TreeItem (cData,rootItem);
2008 ti->setLMO (mapCenter);
2009 ti->setType (TreeItem::MapCenter);
2010 mapCenter->setTreeItem (ti);
2011 rootItem->appendChild (ti);
2016 MapCenterObj* VymModel::removeMapCenter(MapCenterObj* mco)
2018 int i=mapCenters.indexOf (mco);
2021 mapCenters.removeAt (i);
2023 if (i>0) return mapCenters.at(i-1); // Return previous MCO
2028 MapCenterObj* VymModel::getLastMapCenter()
2030 if (mapCenters.size()>0)
2031 return mapCenters.last();
2038 BranchObj* VymModel::addNewBranchInt(int num)
2040 // Depending on pos:
2041 // -3 insert in children of parent above selection
2042 // -2 add branch to selection
2043 // -1 insert in children of parent below selection
2044 // 0..n insert in children of parent at pos
2045 BranchObj *newbo=NULL;
2046 BranchObj *bo=getSelectedBranch();
2051 // save scroll state. If scrolled, automatically select
2052 // new branch in order to tmp unscroll parent...
2053 newbo=bo->addBranch();
2056 QList<QVariant> cData;
2057 cData << "VM:createBranch" << "undef"<<"undef";
2058 TreeItem *parti=bo->getTreeItem();
2059 TreeItem *ti=new TreeItem (cData,parti);
2061 ti->setType (TreeItem::Branch);
2062 parti->appendChild (ti);
2066 newbo->setTreeItem (ti);
2067 select (newbo); // FIXME VM really needed here?
2073 bo=(BranchObj*)bo->getParObj();
2074 if (bo) newbo=bo->insertBranch(num);
2078 bo=(BranchObj*)bo->getParObj();
2079 if (bo) newbo=bo->insertBranch(num);
2085 BranchObj* VymModel::addNewBranch(int pos)
2087 // Different meaning than num in addNewBranchInt!
2091 BranchObj *bo = getSelectedBranch();
2092 BranchObj *newbo=NULL;
2096 // FIXME VM do we still need this in model? setCursor (Qt::ArrowCursor);
2098 newbo=addNewBranchInt (pos-2);
2106 QString ("addBranch (%1)").arg(pos),
2107 QString ("Add new branch to %1").arg(getObjectName(bo)));
2110 // selection.update(); FIXME
2111 latestSelectionString=getSelectString(newbo);
2112 // In Network mode, the client needs to know where the new branch is,
2113 // so we have to pass on this information via saveState.
2114 // TODO: Get rid of this positioning workaround
2115 QString ps=qpointfToString (newbo->getAbsPos());
2116 sendData ("selectLatestAdded ()");
2117 sendData (QString("move %1").arg(ps));
2125 BranchObj* VymModel::addNewBranchBefore()
2127 BranchObj *newbo=NULL;
2128 BranchObj *bo = getSelectedBranch();
2129 if (bo && selectionType()==TreeItem::Branch)
2130 // We accept no MapCenterObj here, so we _have_ a parent
2132 QPointF p=bo->getRelPos();
2135 BranchObj *parbo=(BranchObj*)(bo->getParObj());
2137 // add below selection
2138 newbo=parbo->insertBranch(bo->getNum()+1);
2141 newbo->move2RelPos (p);
2143 // Move selection to new branch
2144 bo->linkTo (newbo,-1);
2146 saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()",
2147 QString ("Add branch before %1").arg(getObjectName(bo)));
2150 // selection.update(); FIXME
2153 latestSelectionString=selection.getSelectString();
2157 void VymModel::deleteSelection()
2159 BranchObj *bo = getSelectedBranch();
2161 if (bo && selectionType()==TreeItem::MapCenter)
2163 // BranchObj* par=(BranchObj*)(bo->getParObj());
2164 selection.unselect();
2165 /* FIXME VM Note: does saveStateRemovingPart work for MCO? (No parent!)
2166 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2168 bo=removeMapCenter ((MapCenterObj*)bo);
2171 selection.select (bo);
2172 ensureSelectionVisible();
2178 if (bo && selectionType()==TreeItem::Branch)
2180 QModelIndex ix=getSelectedIndex();
2182 BranchObj* par=(BranchObj*)bo->getParObj();
2184 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2185 par->removeBranch(bo);
2187 ensureSelectionVisible();
2192 FloatImageObj *fio=selection.getFloatImage();
2195 BranchObj* par=(BranchObj*)fio->getParObj();
2196 saveStateChangingPart(
2200 QString("Delete %1").arg(getObjectName(fio))
2203 par->removeFloatImage(fio);
2207 ensureSelectionVisible();
2212 void VymModel::deleteKeepChildren()
2214 BranchObj *bo=getSelectedBranch();
2218 par=(BranchObj*)(bo->getParObj());
2220 // Don't use this on mapcenter
2223 // Check if we have childs at all to keep
2224 if (bo->countBranches()==0)
2230 QPointF p=bo->getRelPos();
2231 saveStateChangingPart(
2234 "deleteKeepChildren ()",
2235 QString("Remove %1 and keep its children").arg(getObjectName(bo))
2238 QString sel=getSelectString(bo);
2240 par->removeBranchHere(bo);
2243 getSelectedBranch()->move2RelPos (p);
2248 void VymModel::deleteChildren()
2250 BranchObj *bo=getSelectedBranch();
2253 saveStateChangingPart(
2256 "deleteChildren ()",
2257 QString( "Remove children of branch %1").arg(getObjectName(bo))
2259 bo->removeChildren();
2265 bool VymModel::scrollBranch(BranchObj *bo)
2269 if (bo->isScrolled()) return false;
2270 if (bo->countBranches()==0) return false;
2271 if (bo->getDepth()==0) return false;
2277 QString ("%1 ()").arg(u),
2279 QString ("%1 ()").arg(r),
2280 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2284 // FIXME VM needed? scene()->update();
2290 bool VymModel::unscrollBranch(BranchObj *bo)
2294 if (!bo->isScrolled()) return false;
2295 if (bo->countBranches()==0) return false;
2296 if (bo->getDepth()==0) return false;
2302 QString ("%1 ()").arg(u),
2304 QString ("%1 ()").arg(r),
2305 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2309 // FIXME VM needed? scene()->update();
2315 void VymModel::toggleScroll()
2317 BranchObj *bo=getSelectedBranch();
2318 if (selectionType()==TreeItem::Branch )
2320 if (bo->isScrolled())
2321 unscrollBranch (bo);
2327 void VymModel::unscrollChildren()
2329 BranchObj *bo=getSelectedBranch();
2335 if (bo->isScrolled()) unscrollBranch (bo);
2340 void VymModel::addFloatImage (const QPixmap &img)
2342 BranchObj *bo=getSelectedBranch();
2345 FloatImageObj *fio=bo->addFloatImage();
2347 fio->setOriginalFilename("No original filename (image added by dropevent)");
2348 QString s=getSelectString(bo);
2349 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio );
2350 saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
2352 // FIXME VM needed? scene()->update();
2357 void VymModel::colorBranch (QColor c)
2359 BranchObj *bo=getSelectedBranch();
2364 QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
2366 QString ("colorBranch (\"%1\")").arg(c.name()),
2367 QString("Set color of %1 to %2").arg(getObjectName(bo)).arg(c.name())
2369 bo->setColor(c); // color branch
2373 void VymModel::colorSubtree (QColor c)
2375 BranchObj *bo=getSelectedBranch();
2378 saveStateChangingPart(
2381 QString ("colorSubtree (\"%1\")").arg(c.name()),
2382 QString ("Set color of %1 and children to %2").arg(getObjectName(bo)).arg(c.name())
2384 bo->setColorSubtree (c); // color links, color children
2388 QColor VymModel::getCurrentHeadingColor()
2390 BranchObj *bo=getSelectedBranch();
2391 if (bo) return bo->getColor();
2393 QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
2399 void VymModel::editURL()
2401 BranchObj *bo=getSelectedBranch();
2405 QString text = QInputDialog::getText(
2406 "VYM", tr("Enter URL:"), QLineEdit::Normal,
2407 bo->getURL(), &ok, NULL);
2409 // user entered something and pressed OK
2414 void VymModel::editLocalURL()
2416 BranchObj *bo=getSelectedBranch();
2419 QStringList filters;
2420 filters <<"All files (*)";
2421 filters << tr("Text","Filedialog") + " (*.txt)";
2422 filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
2423 filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
2424 filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
2425 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
2426 fd->setFilters (filters);
2427 fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
2428 fd->setDirectory (lastFileDir);
2429 if (! bo->getVymLink().isEmpty() )
2430 fd->selectFile( bo->getURL() );
2433 if ( fd->exec() == QDialog::Accepted )
2435 lastFileDir=QDir (fd->directory().path());
2436 setURL (fd->selectedFile() );
2442 void VymModel::editHeading2URL()
2444 BranchObj *bo=getSelectedBranch();
2446 setURL (bo->getHeading());
2449 void VymModel::editBugzilla2URL()
2451 BranchObj *bo=getSelectedBranch();
2454 QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
2459 void VymModel::editFATE2URL()
2461 BranchObj *bo=getSelectedBranch();
2464 QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
2467 "setURL (\""+bo->getURL()+"\")",
2469 "setURL (\""+url+"\")",
2470 QString("Use heading of %1 as link to FATE").arg(getObjectName(bo))
2477 void VymModel::editVymLink()
2479 BranchObj *bo=getSelectedBranch();
2482 QStringList filters;
2483 filters <<"VYM map (*.vym)";
2484 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
2485 fd->setFilters (filters);
2486 fd->setCaption(vymName+" - " +tr("Link to another map"));
2487 fd->setDirectory (lastFileDir);
2488 if (! bo->getVymLink().isEmpty() )
2489 fd->selectFile( bo->getVymLink() );
2493 if ( fd->exec() == QDialog::Accepted )
2495 lastFileDir=QDir (fd->directory().path());
2498 "setVymLink (\""+bo->getVymLink()+"\")",
2500 "setVymLink (\""+fd->selectedFile()+"\")",
2501 QString("Set vymlink of %1 to %2").arg(getObjectName(bo)).arg(fd->selectedFile())
2503 setVymLink (fd->selectedFile() ); // FIXME ok?
2508 void VymModel::setVymLink (const QString &s)
2510 // Internal function, no saveState needed
2511 BranchObj *bo=getSelectedBranch();
2518 ensureSelectionVisible();
2522 void VymModel::deleteVymLink()
2524 BranchObj *bo=getSelectedBranch();
2529 "setVymLink (\""+bo->getVymLink()+"\")",
2531 "setVymLink (\"\")",
2532 QString("Unset vymlink of %1").arg(getObjectName(bo))
2534 bo->setVymLink ("" );
2537 // FIXME VM needed? scene()->update();
2541 QString VymModel::getVymLink()
2543 BranchObj *bo=getSelectedBranch();
2545 return bo->getVymLink();
2551 QStringList VymModel::getVymLinks()
2554 BranchObj *bo=getSelectedBranch();
2560 if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
2568 void VymModel::followXLink(int i)
2570 BranchObj *bo=getSelectedBranch();
2573 bo=bo->XLinkTargetAt(i);
2576 selection.select(bo);
2577 ensureSelectionVisible();
2582 void VymModel::editXLink(int i) // FIXME VM missing saveState
2584 BranchObj *bo=getSelectedBranch();
2587 XLinkObj *xlo=bo->XLinkAt(i);
2590 EditXLinkDialog dia;
2592 dia.setSelection(bo);
2593 if (dia.exec() == QDialog::Accepted)
2595 if (dia.useSettingsGlobal() )
2597 setMapDefXLinkColor (xlo->getColor() );
2598 setMapDefXLinkWidth (xlo->getWidth() );
2600 if (dia.deleteXLink())
2601 bo->deleteXLinkAt(i);
2611 //////////////////////////////////////////////
2613 //////////////////////////////////////////////
2615 void VymModel::parseAtom(const QString &atom)
2617 BranchObj *selb=getSelectedBranch();
2623 // Split string s into command and parameters
2624 parser.parseAtom (atom);
2625 QString com=parser.getCommand();
2627 // External commands
2628 /////////////////////////////////////////////////////////////////////
2629 if (com=="addBranch")
2631 if (selection.isEmpty())
2633 parser.setError (Aborted,"Nothing selected");
2636 parser.setError (Aborted,"Type of selection is not a branch");
2641 if (parser.checkParCount(pl))
2643 if (parser.parCount()==0)
2647 n=parser.parInt (ok,0);
2648 if (ok ) addNewBranch (n);
2652 /////////////////////////////////////////////////////////////////////
2653 } else if (com=="addBranchBefore")
2655 if (selection.isEmpty())
2657 parser.setError (Aborted,"Nothing selected");
2660 parser.setError (Aborted,"Type of selection is not a branch");
2663 if (parser.parCount()==0)
2665 addNewBranchBefore ();
2668 /////////////////////////////////////////////////////////////////////
2669 } else if (com==QString("addMapCenter"))
2671 if (parser.checkParCount(2))
2673 x=parser.parDouble (ok,0);
2676 y=parser.parDouble (ok,1);
2677 if (ok) addMapCenter (QPointF(x,y));
2680 /////////////////////////////////////////////////////////////////////
2681 } else if (com==QString("addMapReplace"))
2683 if (selection.isEmpty())
2685 parser.setError (Aborted,"Nothing selected");
2688 parser.setError (Aborted,"Type of selection is not a branch");
2689 } else if (parser.checkParCount(1))
2691 //s=parser.parString (ok,0); // selection
2692 t=parser.parString (ok,0); // path to map
2693 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2694 addMapReplaceInt(getSelectString(selb),t);
2696 /////////////////////////////////////////////////////////////////////
2697 } else if (com==QString("addMapInsert"))
2699 if (selection.isEmpty())
2701 parser.setError (Aborted,"Nothing selected");
2704 parser.setError (Aborted,"Type of selection is not a branch");
2707 if (parser.checkParCount(2))
2709 t=parser.parString (ok,0); // path to map
2710 n=parser.parInt(ok,1); // position
2711 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2712 addMapInsertInt(t,n);
2715 /////////////////////////////////////////////////////////////////////
2716 } else if (com=="clearFlags")
2718 if (selection.isEmpty() )
2720 parser.setError (Aborted,"Nothing selected");
2723 parser.setError (Aborted,"Type of selection is not a branch");
2724 } else if (parser.checkParCount(0))
2726 selb->clearStandardFlags();
2727 selb->updateFlagsToolbar();
2729 /////////////////////////////////////////////////////////////////////
2730 } else if (com=="colorBranch")
2732 if (selection.isEmpty())
2734 parser.setError (Aborted,"Nothing selected");
2737 parser.setError (Aborted,"Type of selection is not a branch");
2738 } else if (parser.checkParCount(1))
2740 QColor c=parser.parColor (ok,0);
2741 if (ok) colorBranch (c);
2743 /////////////////////////////////////////////////////////////////////
2744 } else if (com=="colorSubtree")
2746 if (selection.isEmpty())
2748 parser.setError (Aborted,"Nothing selected");
2751 parser.setError (Aborted,"Type of selection is not a branch");
2752 } else if (parser.checkParCount(1))
2754 QColor c=parser.parColor (ok,0);
2755 if (ok) colorSubtree (c);
2757 /////////////////////////////////////////////////////////////////////
2758 } else if (com=="copy")
2760 if (selection.isEmpty())
2762 parser.setError (Aborted,"Nothing selected");
2765 parser.setError (Aborted,"Type of selection is not a branch");
2766 } else if (parser.checkParCount(0))
2768 //FIXME missing action for copy
2770 /////////////////////////////////////////////////////////////////////
2771 } else if (com=="cut")
2773 if (selection.isEmpty())
2775 parser.setError (Aborted,"Nothing selected");
2776 } else if ( selectionType()!=TreeItem::Branch &&
2777 selectionType()!=TreeItem::MapCenter &&
2778 selectionType()!=TreeItem::Image )
2780 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2781 } else if (parser.checkParCount(0))
2785 /////////////////////////////////////////////////////////////////////
2786 } else if (com=="delete")
2788 if (selection.isEmpty())
2790 parser.setError (Aborted,"Nothing selected");
2792 /*else if (selectionType() != TreeItem::Branch && selectionType() != TreeItem::Image )
2794 parser.setError (Aborted,"Type of selection is wrong.");
2797 else if (parser.checkParCount(0))
2801 /////////////////////////////////////////////////////////////////////
2802 } else if (com=="deleteKeepChildren")
2804 if (selection.isEmpty())
2806 parser.setError (Aborted,"Nothing selected");
2809 parser.setError (Aborted,"Type of selection is not a branch");
2810 } else if (parser.checkParCount(0))
2812 deleteKeepChildren();
2814 /////////////////////////////////////////////////////////////////////
2815 } else if (com=="deleteChildren")
2817 if (selection.isEmpty())
2819 parser.setError (Aborted,"Nothing selected");
2822 parser.setError (Aborted,"Type of selection is not a branch");
2823 } else if (parser.checkParCount(0))
2827 /////////////////////////////////////////////////////////////////////
2828 } else if (com=="exportASCII")
2832 if (parser.parCount()>=1)
2833 // Hey, we even have a filename
2834 fname=parser.parString(ok,0);
2837 parser.setError (Aborted,"Could not read filename");
2840 exportASCII (fname,false);
2842 /////////////////////////////////////////////////////////////////////
2843 } else if (com=="exportImage")
2847 if (parser.parCount()>=2)
2848 // Hey, we even have a filename
2849 fname=parser.parString(ok,0);
2852 parser.setError (Aborted,"Could not read filename");
2855 QString format="PNG";
2856 if (parser.parCount()>=2)
2858 format=parser.parString(ok,1);
2860 exportImage (fname,false,format);
2862 /////////////////////////////////////////////////////////////////////
2863 } else if (com=="exportXHTML")
2867 if (parser.parCount()>=2)
2868 // Hey, we even have a filename
2869 fname=parser.parString(ok,1);
2872 parser.setError (Aborted,"Could not read filename");
2875 exportXHTML (fname,false);
2877 /////////////////////////////////////////////////////////////////////
2878 } else if (com=="exportXML")
2882 if (parser.parCount()>=2)
2883 // Hey, we even have a filename
2884 fname=parser.parString(ok,1);
2887 parser.setError (Aborted,"Could not read filename");
2890 exportXML (fname,false);
2892 /////////////////////////////////////////////////////////////////////
2893 } else if (com=="importDir")
2895 if (selection.isEmpty())
2897 parser.setError (Aborted,"Nothing selected");
2900 parser.setError (Aborted,"Type of selection is not a branch");
2901 } else if (parser.checkParCount(1))
2903 s=parser.parString(ok,0);
2904 if (ok) importDirInt(s);
2906 /////////////////////////////////////////////////////////////////////
2907 } else if (com=="linkTo")
2909 if (selection.isEmpty())
2911 parser.setError (Aborted,"Nothing selected");
2914 if (parser.checkParCount(4))
2916 // 0 selectstring of parent
2917 // 1 num in parent (for branches)
2918 // 2,3 x,y of mainbranch or mapcenter
2919 s=parser.parString(ok,0);
2920 LinkableMapObj *dst=findObjBySelect (s);
2923 if (typeid(*dst) == typeid(BranchObj) )
2925 // Get number in parent
2926 n=parser.parInt (ok,1);
2929 selb->linkTo ((BranchObj*)(dst),n);
2932 } else if (typeid(*dst) == typeid(MapCenterObj) )
2934 selb->linkTo ((BranchObj*)(dst),-1);
2935 // Get coordinates of mainbranch
2936 x=parser.parDouble(ok,2);
2939 y=parser.parDouble(ok,3);
2949 } else if ( selectionType() == TreeItem::Image)
2951 if (parser.checkParCount(1))
2953 // 0 selectstring of parent
2954 s=parser.parString(ok,0);
2955 LinkableMapObj *dst=findObjBySelect (s);
2958 if (typeid(*dst) == typeid(BranchObj) ||
2959 typeid(*dst) == typeid(MapCenterObj))
2960 linkFloatImageTo (getSelectString(dst));
2962 parser.setError (Aborted,"Destination is not a branch");
2965 parser.setError (Aborted,"Type of selection is not a floatimage or branch");
2966 /////////////////////////////////////////////////////////////////////
2967 } else if (com=="loadImage")
2969 if (selection.isEmpty())
2971 parser.setError (Aborted,"Nothing selected");
2974 parser.setError (Aborted,"Type of selection is not a branch");
2975 } else if (parser.checkParCount(1))
2977 s=parser.parString(ok,0);
2978 if (ok) loadFloatImageInt (s);
2980 /////////////////////////////////////////////////////////////////////
2981 } else if (com=="moveBranchUp")
2983 if (selection.isEmpty() )
2985 parser.setError (Aborted,"Nothing selected");
2988 parser.setError (Aborted,"Type of selection is not a branch");
2989 } else if (parser.checkParCount(0))
2993 /////////////////////////////////////////////////////////////////////
2994 } else if (com=="moveBranchDown")
2996 if (selection.isEmpty() )
2998 parser.setError (Aborted,"Nothing selected");
3001 parser.setError (Aborted,"Type of selection is not a branch");
3002 } else if (parser.checkParCount(0))
3006 /////////////////////////////////////////////////////////////////////
3007 } else if (com=="move")
3009 if (selection.isEmpty() )
3011 parser.setError (Aborted,"Nothing selected");
3012 } else if ( selectionType()!=TreeItem::Branch &&
3013 selectionType()!=TreeItem::MapCenter &&
3014 selectionType()!=TreeItem::Image )
3016 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3017 } else if (parser.checkParCount(2))
3019 x=parser.parDouble (ok,0);
3022 y=parser.parDouble (ok,1);
3026 /////////////////////////////////////////////////////////////////////
3027 } else if (com=="moveRel")
3029 if (selection.isEmpty() )
3031 parser.setError (Aborted,"Nothing selected");
3032 } else if ( selectionType()!=TreeItem::Branch &&
3033 selectionType()!=TreeItem::MapCenter &&
3034 selectionType()!=TreeItem::Image )
3036 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3037 } else if (parser.checkParCount(2))
3039 x=parser.parDouble (ok,0);
3042 y=parser.parDouble (ok,1);
3043 if (ok) moveRel (x,y);
3046 /////////////////////////////////////////////////////////////////////
3047 } else if (com=="nop")
3049 /////////////////////////////////////////////////////////////////////
3050 } else if (com=="paste")
3052 if (selection.isEmpty() )
3054 parser.setError (Aborted,"Nothing selected");
3057 parser.setError (Aborted,"Type of selection is not a branch");
3058 } else if (parser.checkParCount(1))
3060 n=parser.parInt (ok,0);
3061 if (ok) pasteNoSave(n);
3063 /////////////////////////////////////////////////////////////////////
3064 } else if (com=="qa")
3066 if (selection.isEmpty() )
3068 parser.setError (Aborted,"Nothing selected");
3071 parser.setError (Aborted,"Type of selection is not a branch");
3072 } else if (parser.checkParCount(4))
3075 c=parser.parString (ok,0);
3078 parser.setError (Aborted,"No comment given");
3081 s=parser.parString (ok,1);
3084 parser.setError (Aborted,"First parameter is not a string");
3087 t=parser.parString (ok,2);
3090 parser.setError (Aborted,"Condition is not a string");
3093 u=parser.parString (ok,3);
3096 parser.setError (Aborted,"Third parameter is not a string");
3101 parser.setError (Aborted,"Unknown type: "+s);
3106 parser.setError (Aborted,"Unknown operator: "+t);
3111 parser.setError (Aborted,"Type of selection is not a branch");
3114 if (selb->getHeading() == u)
3116 cout << "PASSED: " << qPrintable (c) << endl;
3119 cout << "FAILED: " << qPrintable (c) << endl;
3129 /////////////////////////////////////////////////////////////////////
3130 } else if (com=="saveImage")
3132 FloatImageObj *fio=selection.getFloatImage();
3135 parser.setError (Aborted,"Type of selection is not an image");
3136 } else if (parser.checkParCount(2))
3138 s=parser.parString(ok,0);
3141 t=parser.parString(ok,1);
3142 if (ok) saveFloatImageInt (fio,t,s);
3145 /////////////////////////////////////////////////////////////////////
3146 } else if (com=="scroll")
3148 if (selection.isEmpty() )
3150 parser.setError (Aborted,"Nothing selected");
3153 parser.setError (Aborted,"Type of selection is not a branch");
3154 } else if (parser.checkParCount(0))
3156 if (!scrollBranch (selb))
3157 parser.setError (Aborted,"Could not scroll branch");
3159 /////////////////////////////////////////////////////////////////////
3160 } else if (com=="select")
3162 if (parser.checkParCount(1))
3164 s=parser.parString(ok,0);
3167 /////////////////////////////////////////////////////////////////////
3168 } else if (com=="selectLastBranch")
3170 if (selection.isEmpty() )
3172 parser.setError (Aborted,"Nothing selected");
3175 parser.setError (Aborted,"Type of selection is not a branch");
3176 } else if (parser.checkParCount(0))
3178 BranchObj *bo=selb->getLastBranch();
3180 parser.setError (Aborted,"Could not select last branch");
3184 /////////////////////////////////////////////////////////////////////
3185 } else if (com=="selectLastImage")
3187 if (selection.isEmpty() )
3189 parser.setError (Aborted,"Nothing selected");
3192 parser.setError (Aborted,"Type of selection is not a branch");
3193 } else if (parser.checkParCount(0))
3195 FloatImageObj *fio=selb->getLastFloatImage();
3197 parser.setError (Aborted,"Could not select last image");
3201 /////////////////////////////////////////////////////////////////////
3202 } else if (com=="selectLatestAdded")
3204 if (latestSelectionString.isEmpty() )
3206 parser.setError (Aborted,"No latest added object");
3209 if (!select (latestSelectionString))
3210 parser.setError (Aborted,"Could not select latest added object "+latestSelectionString);
3212 /////////////////////////////////////////////////////////////////////
3213 } else if (com=="setFrameType")
3215 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3217 parser.setError (Aborted,"Type of selection does not allow setting frame type");
3219 else if (parser.checkParCount(1))
3221 s=parser.parString(ok,0);
3222 if (ok) setFrameType (s);
3224 /////////////////////////////////////////////////////////////////////
3225 } else if (com=="setFramePenColor")
3227 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3229 parser.setError (Aborted,"Type of selection does not allow setting of pen color");
3231 else if (parser.checkParCount(1))
3233 QColor c=parser.parColor(ok,0);
3234 if (ok) setFramePenColor (c);
3236 /////////////////////////////////////////////////////////////////////
3237 } else if (com=="setFrameBrushColor")
3239 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3241 parser.setError (Aborted,"Type of selection does not allow setting brush color");
3243 else if (parser.checkParCount(1))
3245 QColor c=parser.parColor(ok,0);
3246 if (ok) setFrameBrushColor (c);
3248 /////////////////////////////////////////////////////////////////////
3249 } else if (com=="setFramePadding")
3251 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3253 parser.setError (Aborted,"Type of selection does not allow setting frame padding");
3255 else if (parser.checkParCount(1))
3257 n=parser.parInt(ok,0);
3258 if (ok) setFramePadding(n);
3260 /////////////////////////////////////////////////////////////////////
3261 } else if (com=="setFrameBorderWidth")
3263 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3265 parser.setError (Aborted,"Type of selection does not allow setting frame border width");
3267 else if (parser.checkParCount(1))
3269 n=parser.parInt(ok,0);
3270 if (ok) setFrameBorderWidth (n);
3272 /////////////////////////////////////////////////////////////////////
3273 } else if (com=="setMapAuthor")
3275 if (parser.checkParCount(1))
3277 s=parser.parString(ok,0);
3278 if (ok) setAuthor (s);
3280 /////////////////////////////////////////////////////////////////////
3281 } else if (com=="setMapComment")
3283 if (parser.checkParCount(1))
3285 s=parser.parString(ok,0);
3286 if (ok) setComment(s);
3288 /////////////////////////////////////////////////////////////////////
3289 } else if (com=="setMapBackgroundColor")
3291 if (selection.isEmpty() )
3293 parser.setError (Aborted,"Nothing selected");
3294 } else if (! getSelectedBranch() )
3296 parser.setError (Aborted,"Type of selection is not a branch");
3297 } else if (parser.checkParCount(1))
3299 QColor c=parser.parColor (ok,0);
3300 if (ok) setMapBackgroundColor (c);
3302 /////////////////////////////////////////////////////////////////////
3303 } else if (com=="setMapDefLinkColor")
3305 if (selection.isEmpty() )
3307 parser.setError (Aborted,"Nothing selected");
3310 parser.setError (Aborted,"Type of selection is not a branch");
3311 } else if (parser.checkParCount(1))
3313 QColor c=parser.parColor (ok,0);
3314 if (ok) setMapDefLinkColor (c);
3316 /////////////////////////////////////////////////////////////////////
3317 } else if (com=="setMapLinkStyle")
3319 if (parser.checkParCount(1))
3321 s=parser.parString (ok,0);
3322 if (ok) setMapLinkStyle(s);
3324 /////////////////////////////////////////////////////////////////////
3325 } else if (com=="setHeading")
3327 if (selection.isEmpty() )
3329 parser.setError (Aborted,"Nothing selected");
3332 parser.setError (Aborted,"Type of selection is not a branch");
3333 } else if (parser.checkParCount(1))
3335 s=parser.parString (ok,0);
3339 /////////////////////////////////////////////////////////////////////
3340 } else if (com=="setHideExport")
3342 if (selection.isEmpty() )
3344 parser.setError (Aborted,"Nothing selected");
3345 } else if (selectionType()!=TreeItem::Branch && selectionType() != TreeItem::MapCenter &&selectionType()!=TreeItem::Image)
3347 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3348 } else if (parser.checkParCount(1))
3350 b=parser.parBool(ok,0);
3351 if (ok) setHideExport (b);
3353 /////////////////////////////////////////////////////////////////////
3354 } else if (com=="setIncludeImagesHorizontally")
3356 if (selection.isEmpty() )
3358 parser.setError (Aborted,"Nothing selected");
3361 parser.setError (Aborted,"Type of selection is not a branch");
3362 } else if (parser.checkParCount(1))
3364 b=parser.parBool(ok,0);
3365 if (ok) setIncludeImagesHor(b);
3367 /////////////////////////////////////////////////////////////////////
3368 } else if (com=="setIncludeImagesVertically")
3370 if (selection.isEmpty() )
3372 parser.setError (Aborted,"Nothing selected");
3375 parser.setError (Aborted,"Type of selection is not a branch");
3376 } else if (parser.checkParCount(1))
3378 b=parser.parBool(ok,0);
3379 if (ok) setIncludeImagesVer(b);
3381 /////////////////////////////////////////////////////////////////////
3382 } else if (com=="setHideLinkUnselected")
3384 if (selection.isEmpty() )
3386 parser.setError (Aborted,"Nothing selected");
3387 } else if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3389 parser.setError (Aborted,"Type of selection does not allow hiding the link");
3390 } else if (parser.checkParCount(1))
3392 b=parser.parBool(ok,0);
3393 if (ok) setHideLinkUnselected(b);
3395 /////////////////////////////////////////////////////////////////////
3396 } else if (com=="setSelectionColor")
3398 if (parser.checkParCount(1))
3400 QColor c=parser.parColor (ok,0);
3401 if (ok) setSelectionColorInt (c);
3403 /////////////////////////////////////////////////////////////////////
3404 } else if (com=="setURL")
3406 if (selection.isEmpty() )
3408 parser.setError (Aborted,"Nothing selected");
3411 parser.setError (Aborted,"Type of selection is not a branch");
3412 } else if (parser.checkParCount(1))
3414 s=parser.parString (ok,0);
3417 /////////////////////////////////////////////////////////////////////
3418 } else if (com=="setVymLink")
3420 if (selection.isEmpty() )
3422 parser.setError (Aborted,"Nothing selected");
3425 parser.setError (Aborted,"Type of selection is not a branch");
3426 } else if (parser.checkParCount(1))
3428 s=parser.parString (ok,0);
3429 if (ok) setVymLink(s);
3432 /////////////////////////////////////////////////////////////////////
3433 else if (com=="setFlag")
3435 if (selection.isEmpty() )
3437 parser.setError (Aborted,"Nothing selected");
3440 parser.setError (Aborted,"Type of selection is not a branch");
3441 } else if (parser.checkParCount(1))
3443 s=parser.parString(ok,0);
3446 selb->activateStandardFlag(s);
3447 selb->updateFlagsToolbar();
3450 /////////////////////////////////////////////////////////////////////
3451 } else if (com=="setFrameType")
3453 if (selection.isEmpty() )
3455 parser.setError (Aborted,"Nothing selected");
3458 parser.setError (Aborted,"Type of selection is not a branch");
3459 } else if (parser.checkParCount(1))
3461 s=parser.parString(ok,0);
3465 /////////////////////////////////////////////////////////////////////
3466 } else if (com=="sortChildren")
3468 if (selection.isEmpty() )
3470 parser.setError (Aborted,"Nothing selected");
3473 parser.setError (Aborted,"Type of selection is not a branch");
3474 } else if (parser.checkParCount(0))
3478 /////////////////////////////////////////////////////////////////////
3479 } else if (com=="toggleFlag")
3481 if (selection.isEmpty() )
3483 parser.setError (Aborted,"Nothing selected");
3486 parser.setError (Aborted,"Type of selection is not a branch");
3487 } else if (parser.checkParCount(1))
3489 s=parser.parString(ok,0);
3492 selb->toggleStandardFlag(s);
3493 selb->updateFlagsToolbar();
3496 /////////////////////////////////////////////////////////////////////
3497 } else if (com=="unscroll")
3499 if (selection.isEmpty() )
3501 parser.setError (Aborted,"Nothing selected");
3504 parser.setError (Aborted,"Type of selection is not a branch");
3505 } else if (parser.checkParCount(0))
3507 if (!unscrollBranch (selb))
3508 parser.setError (Aborted,"Could not unscroll branch");
3510 /////////////////////////////////////////////////////////////////////
3511 } else if (com=="unscrollChildren")
3513 if (selection.isEmpty() )
3515 parser.setError (Aborted,"Nothing selected");
3518 parser.setError (Aborted,"Type of selection is not a branch");
3519 } else if (parser.checkParCount(0))
3521 unscrollChildren ();
3523 /////////////////////////////////////////////////////////////////////
3524 } else if (com=="unsetFlag")
3526 if (selection.isEmpty() )
3528 parser.setError (Aborted,"Nothing selected");
3531 parser.setError (Aborted,"Type of selection is not a branch");
3532 } else if (parser.checkParCount(1))
3534 s=parser.parString(ok,0);
3537 selb->deactivateStandardFlag(s);
3538 selb->updateFlagsToolbar();
3542 parser.setError (Aborted,"Unknown command");
3545 if (parser.errorLevel()==NoError)
3547 // setChanged(); FIXME should not be called e.g. for export?!
3552 // TODO Error handling
3553 qWarning("VymModel::parseAtom: Error!");
3554 qWarning(parser.errorMessage());
3558 void VymModel::runScript (QString script)
3560 parser.setScript (script);
3562 while (parser.next() )
3563 parseAtom(parser.getAtom());
3566 void VymModel::setExportMode (bool b)
3568 // should be called before and after exports
3569 // depending on the settings
3570 if (b && settings.value("/export/useHideExport","true")=="true")
3571 setHideTmpMode (HideExport);
3573 setHideTmpMode (HideNone);
3576 void VymModel::exportImage(QString fname, bool askName, QString format)
3580 fname=getMapName()+".png";
3587 QFileDialog *fd=new QFileDialog (NULL);
3588 fd->setCaption (tr("Export map as image"));
3589 fd->setDirectory (lastImageDir);
3590 fd->setFileMode(QFileDialog::AnyFile);
3591 fd->setFilters (imageIO.getFilters() );
3594 fl=fd->selectedFiles();
3596 format=imageIO.getType(fd->selectedFilter());
3600 setExportMode (true);
3601 QPixmap pix (getPixmap());
3602 pix.save(fname, format);
3603 setExportMode (false);
3607 void VymModel::exportXML(QString dir, bool askForName)
3611 dir=browseDirectory(NULL,tr("Export XML to directory"));
3612 if (dir =="" && !reallyWriteDirectory(dir) )
3616 // Hide stuff during export, if settings want this
3617 setExportMode (true);
3619 // Create subdirectories
3622 // write to directory
3623 QString saveFile=saveToDir (dir,mapName+"-",true,getTotalBBox().topLeft() ,NULL);
3626 file.setName ( dir + "/"+mapName+".xml");
3627 if ( !file.open( QIODevice::WriteOnly ) )
3629 // This should neverever happen
3630 QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
3634 // Write it finally, and write in UTF8, no matter what
3635 QTextStream ts( &file );
3636 ts.setEncoding (QTextStream::UnicodeUTF8);
3640 // Now write image, too
3641 exportImage (dir+"/images/"+mapName+".png",false,"PNG");
3643 setExportMode (false);
3646 void VymModel::exportASCII(QString fname,bool askName)
3651 ex.setFile (mapName+".txt");
3657 //ex.addFilter ("TXT (*.txt)");
3658 ex.setDir(lastImageDir);
3659 //ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
3664 setExportMode(true);
3666 setExportMode(false);
3670 void VymModel::exportXHTML (const QString &dir, bool askForName)
3672 ExportXHTMLDialog dia(NULL);
3673 dia.setFilePath (filePath );
3674 dia.setMapName (mapName );
3676 if (dir!="") dia.setDir (dir);
3682 if (dia.exec()!=QDialog::Accepted)
3686 QDir d (dia.getDir());
3687 // Check, if warnings should be used before overwriting
3688 // the output directory
3689 if (d.exists() && d.count()>0)
3692 warn.showCancelButton (true);
3693 warn.setText(QString(
3694 "The directory %1 is not empty.\n"
3695 "Do you risk to overwrite some of its contents?").arg(d.path() ));
3696 warn.setCaption("Warning: Directory not empty");
3697 warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
3699 if (warn.exec()!=QDialog::Accepted) ok=false;
3706 exportXML (dia.getDir(),false );
3707 dia.doExport(mapName );
3708 //if (dia.hasChanged()) setChanged();
3712 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
3717 if (ex.setConfigFile(cf))
3719 setExportMode (true);
3720 ex.exportPresentation();
3721 setExportMode (false);
3728 //////////////////////////////////////////////
3730 //////////////////////////////////////////////
3732 void VymModel::registerEditor(QWidget *me)
3734 mapEditor=(MapEditor*)me;
3735 for (int i=0; i<mapCenters.count(); i++)
3736 mapCenters.at(i)->setMapEditor(mapEditor);
3739 void VymModel::unregisterEditor(QWidget *)
3744 void VymModel::setContextPos(QPointF p)
3749 void VymModel::unsetContextPos()
3751 contextPos=QPointF();
3754 void VymModel::updateNoteFlag()
3757 BranchObj *bo=getSelectedBranch();
3760 bo->updateNoteFlag();
3761 mainWindow->updateActions();
3765 void VymModel::updateRelPositions()
3767 for (int i=0; i<mapCenters.count(); i++)
3768 mapCenters.at(i)->updateRelPositions();
3771 void VymModel::reposition()
3773 for (int i=0;i<mapCenters.count(); i++)
3774 mapCenters.at(i)->reposition(); // for positioning heading
3777 QPolygonF VymModel::shape(BranchObj *bo)
3779 // Creating (arbitrary) shapes
3782 QRectF rb=bo->getBBox();
3783 if (bo->getDepth()==0)
3785 // Just take BBox of this mapCenter
3786 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
3790 // Take union of BBox and TotalBBox
3792 QRectF ra=bo->getTotalBBox();
3793 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
3796 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
3799 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
3801 p <<ra.bottomRight()
3803 <<QPointF (rb.topRight().x(), ra.topRight().y() )
3806 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
3811 void VymModel::moveAway(LinkableMapObj *lmo)
3815 // Move all branches and MapCenters away from lmo
3816 // to avoid collisions
3821 BranchObj *boA=(BranchObj*)lmo;
3823 for (int i=0; i<mapCenters.count(); i++)
3825 boB=mapCenters.at(i);
3828 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
3831 <<" ("<<qPrintable(boA->getHeading() )<<")"
3832 <<" with ("<< qPrintable (boB->getHeading() )
3835 <<" minT="<<r.minTranslation<<endl<<endl;
3839 QPixmap VymModel::getPixmap()
3841 QRectF mapRect=getTotalBBox();
3842 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
3845 pp.setRenderHints(mapEditor->renderHints());
3847 // Don't print the visualisation of selection
3848 selection.unselect();
3850 mapScene->render ( &pp,
3851 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
3852 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
3854 // Restore selection
3855 selection.reselect();
3861 void VymModel::setMapLinkStyle (const QString & s)
3866 case LinkableMapObj::Line :
3869 case LinkableMapObj::Parabel:
3870 snow="StyleParabel";
3872 case LinkableMapObj::PolyLine:
3873 snow="StylePolyLine";
3875 case LinkableMapObj::PolyParabel:
3876 snow="StylePolyParabel";
3879 snow="UndefinedStyle";
3884 QString("setMapLinkStyle (\"%1\")").arg(s),
3885 QString("setMapLinkStyle (\"%1\")").arg(snow),
3886 QString("Set map link style (\"%1\")").arg(s)
3890 linkstyle=LinkableMapObj::Line;
3891 else if (s=="StyleParabel")
3892 linkstyle=LinkableMapObj::Parabel;
3893 else if (s=="StylePolyLine")
3894 linkstyle=LinkableMapObj::PolyLine;
3895 else if (s=="StylePolyParabel")
3896 linkstyle=LinkableMapObj::PolyParabel;
3898 linkstyle=LinkableMapObj::UndefinedStyle;
3900 TreeItem *ti=first();
3904 bo=(BranchObj*)(ti->getLMO() );
3905 bo->setLinkStyle(bo->getDefLinkStyle());
3911 LinkableMapObj::Style VymModel::getMapLinkStyle ()
3916 void VymModel::setMapDefLinkColor(QColor col)
3918 if ( !col.isValid() ) return;
3920 QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
3921 QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
3922 QString("Set map link color to %1").arg(col.name())
3931 bo=(BranchObj*)(ti->getLMO() );
3938 void VymModel::setMapLinkColorHintInt()
3940 // called from setMapLinkColorHint(lch) or at end of parse
3946 bo=(BranchObj*)(ti->getLMO() );
3952 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
3955 setMapLinkColorHintInt();
3958 void VymModel::toggleMapLinkColorHint()
3960 if (linkcolorhint==LinkableMapObj::HeadingColor)
3961 linkcolorhint=LinkableMapObj::DefaultColor;
3963 linkcolorhint=LinkableMapObj::HeadingColor;
3969 bo=(BranchObj*)(ti->getLMO() );
3975 void VymModel::selectMapBackgroundImage () // FIXME move to ME
3977 Q3FileDialog *fd=new Q3FileDialog( NULL);
3978 fd->setMode (Q3FileDialog::ExistingFile);
3979 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
3980 ImagePreview *p =new ImagePreview (fd);
3981 fd->setContentsPreviewEnabled( TRUE );
3982 fd->setContentsPreview( p, p );
3983 fd->setPreviewMode( Q3FileDialog::Contents );
3984 fd->setCaption(vymName+" - " +tr("Load background image"));
3985 fd->setDir (lastImageDir);
3988 if ( fd->exec() == QDialog::Accepted )
3990 // TODO selectMapBackgroundImg in QT4 use: lastImageDir=fd->directory();
3991 lastImageDir=QDir (fd->dirPath());
3992 setMapBackgroundImage (fd->selectedFile());
3996 void VymModel::setMapBackgroundImage (const QString &fn) //FIXME missing savestate
3998 QColor oldcol=mapScene->backgroundBrush().color();
4002 QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
4004 QString ("setMapBackgroundImage (%1)").arg(col.name()),
4005 QString("Set background color of map to %1").arg(col.name()));
4008 brush.setTextureImage (QPixmap (fn));
4009 mapScene->setBackgroundBrush(brush);
4012 void VymModel::selectMapBackgroundColor() // FIXME move to ME
4014 QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
4015 if ( !col.isValid() ) return;
4016 setMapBackgroundColor( col );
4020 void VymModel::setMapBackgroundColor(QColor col) // FIXME move to ME
4022 QColor oldcol=mapScene->backgroundBrush().color();
4024 QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
4025 QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
4026 QString("Set background color of map to %1").arg(col.name()));
4027 mapScene->setBackgroundBrush(col);
4030 QColor VymModel::getMapBackgroundColor() // FIXME move to ME
4032 return mapScene->backgroundBrush().color();
4036 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint() // FIXME move to ME
4038 return linkcolorhint;
4041 QColor VymModel::getMapDefLinkColor() // FIXME move to ME
4043 return defLinkColor;
4046 void VymModel::setMapDefXLinkColor(QColor col) // FIXME move to ME
4051 QColor VymModel::getMapDefXLinkColor() // FIXME move to ME
4053 return defXLinkColor;
4056 void VymModel::setMapDefXLinkWidth (int w) // FIXME move to ME
4061 int VymModel::getMapDefXLinkWidth() // FIXME move to ME
4063 return defXLinkWidth;
4066 void VymModel::move(const double &x, const double &y)
4068 BranchObj *bo = getSelectedBranch();
4070 (selectionType()==TreeItem::Branch ||
4071 selectionType()==TreeItem::MapCenter ||
4072 selectionType()==TreeItem::Image
4075 QPointF ap(bo->getAbsPos());
4079 QString ps=qpointfToString(ap);
4080 QString s=getSelectString();
4083 s, "move "+qpointfToString(to),
4084 QString("Move %1 to %2").arg(getObjectName(bo)).arg(ps));
4092 void VymModel::moveRel (const double &x, const double &y)
4094 BranchObj *bo = getSelectedBranch();
4096 (selectionType()==TreeItem::Branch ||
4097 selectionType()==TreeItem::MapCenter ||
4098 selectionType()==TreeItem::Image
4102 QPointF rp(bo->getRelPos());
4106 QString ps=qpointfToString (bo->getRelPos());
4107 QString s=getSelectString(bo);
4110 s, "moveRel "+qpointfToString(to),
4111 QString("Move %1 to relative position %2").arg(getObjectName(bo)).arg(ps));
4112 ((OrnamentedObj*)bo)->move2RelPos (x,y);
4121 void VymModel::animate()
4123 animationTimer->stop();
4126 while (i<animObjList.size() )
4128 bo=(BranchObj*)animObjList.at(i);
4133 animObjList.removeAt(i);
4140 QItemSelection sel=selModel->selection();
4141 emit (selectionChanged(sel,sel));
4144 if (!animObjList.isEmpty()) animationTimer->start();
4148 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
4150 if (bo && bo->getDepth()>0)
4153 ap.setStart (start);
4155 ap.setTicks (animationTicks);
4156 ap.setAnimated (true);
4157 bo->setAnimation (ap);
4158 animObjList.append( bo );
4159 animationTimer->setSingleShot (true);
4160 animationTimer->start(animationInterval);
4164 void VymModel::stopAnimation (MapObj *mo)
4166 int i=animObjList.indexOf(mo);
4168 animObjList.removeAt (i);
4171 void VymModel::sendSelection()
4173 if (netstate!=Server) return;
4174 sendData (QString("select (\"%1\")").arg(selection.getSelectString()) );
4177 void VymModel::newServer()
4181 tcpServer = new QTcpServer(this);
4182 if (!tcpServer->listen(QHostAddress::Any,port)) {
4183 QMessageBox::critical(NULL, "vym server",
4184 QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
4185 //FIXME needed? we are no widget any longer... close();
4188 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
4190 cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
4193 void VymModel::connectToServer()
4196 server="salam.suse.de";
4198 clientSocket = new QTcpSocket (this);
4199 clientSocket->abort();
4200 clientSocket->connectToHost(server ,port);
4201 connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
4202 connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
4203 this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
4205 cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
4210 void VymModel::newClient()
4212 QTcpSocket *newClient = tcpServer->nextPendingConnection();
4213 connect(newClient, SIGNAL(disconnected()),
4214 newClient, SLOT(deleteLater()));
4216 cout <<"ME::newClient at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
4218 clientList.append (newClient);
4222 void VymModel::sendData(const QString &s)
4224 if (clientList.size()==0) return;
4226 // Create bytearray to send
4228 QDataStream out(&block, QIODevice::WriteOnly);
4229 out.setVersion(QDataStream::Qt_4_0);
4231 // Reserve some space for blocksize
4234 // Write sendCounter
4235 out << sendCounter++;
4240 // Go back and write blocksize so far
4241 out.device()->seek(0);
4242 quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
4246 cout << "ME::sendData bs="<<bs<<" counter="<<sendCounter<<" s="<<qPrintable(s)<<endl;
4248 for (int i=0; i<clientList.size(); ++i)
4250 //cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
4251 clientList.at(i)->write (block);
4255 void VymModel::readData ()
4257 while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
4260 cout <<"readData bytesAvail="<<clientSocket->bytesAvailable();
4264 QDataStream in(clientSocket);
4265 in.setVersion(QDataStream::Qt_4_0);
4273 cout << " t="<<qPrintable (t)<<endl;
4279 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
4281 switch (socketError) {
4282 case QAbstractSocket::RemoteHostClosedError:
4284 case QAbstractSocket::HostNotFoundError:
4285 QMessageBox::information(NULL, vymName +" Network client",
4286 "The host was not found. Please check the "
4287 "host name and port settings.");
4289 case QAbstractSocket::ConnectionRefusedError:
4290 QMessageBox::information(NULL, vymName + " Network client",
4291 "The connection was refused by the peer. "
4292 "Make sure the fortune server is running, "
4293 "and check that the host name and port "
4294 "settings are correct.");
4297 QMessageBox::information(NULL, vymName + " Network client",
4298 QString("The following error occurred: %1.")
4299 .arg(clientSocket->errorString()));
4306 void VymModel::selectMapSelectionColor()
4308 QColor col = QColorDialog::getColor( defLinkColor, NULL);
4309 setSelectionColor (col);
4312 void VymModel::setSelectionColorInt (QColor col)
4314 if ( !col.isValid() ) return;
4316 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4317 QString("setSelectionColor (%1)").arg(col.name()),
4318 QString("Set color of selection box to %1").arg(col.name())
4321 mapEditor->setSelectionColor (col);
4324 void VymModel::updateSelection()
4326 QItemSelection newsel=selModel->selection();
4327 updateSelection (newsel);
4330 void VymModel::updateSelection(const QItemSelection &)
4332 QItemSelection newsel=selModel->selection();
4334 cout << "VM::updateSelection new=";
4335 if (!newsel.indexes().isEmpty() )
4336 cout << newsel.indexes().first().row()<<"," << newsel.indexes().first().column();
4338 if (!oldsel.indexes().isEmpty() )
4339 cout << oldsel.indexes().first().row()<<"," << oldsel.indexes().first().column();
4342 //emit (selectionChanged(newsel,oldsel)); // FIXME needed?
4343 ensureSelectionVisible();
4347 void VymModel::setSelectionColor(QColor col)
4349 if ( !col.isValid() ) return;
4351 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4352 QString("setSelectionColor (%1)").arg(col.name()),
4353 QString("Set color of selection box to %1").arg(col.name())
4355 setSelectionColorInt (col);
4358 QColor VymModel::getSelectionColor()
4360 return mapEditor->getSelectionColor();
4363 void VymModel::setHideTmpMode (HideTmpMode mode)
4366 for (int i=0;i<mapCenters.count(); i++)
4367 mapCenters.at(i)->setHideTmp (mode);
4369 // FIXME needed? scene()->update();
4373 QRectF VymModel::getTotalBBox()
4376 for (int i=0;i<mapCenters.count(); i++)
4377 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
4381 //////////////////////////////////////////////
4382 // Selection related
4383 //////////////////////////////////////////////
4385 void VymModel::setSelectionModel (QItemSelectionModel *sm)
4388 cout << "VM::setSelModel selModel="<<selModel<<endl;
4391 QItemSelectionModel* VymModel::getSelectionModel()
4396 void VymModel::setSelectionBlocked (bool b)
4401 selection.unblock();
4404 bool VymModel::isSelectionBlocked()
4406 return selection.isBlocked();
4409 bool VymModel::select ()
4411 QModelIndex index=selModel->selectedIndexes().first(); // TODO no multiselections yet
4413 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
4414 return select (item->getLMO() );
4417 bool VymModel::select (const QString &s)
4419 LinkableMapObj *lmo=findObjBySelect(s);
4421 // Finally select the found object
4431 bool VymModel::select (LinkableMapObj *lmo)
4433 QItemSelection oldsel=selModel->selection();
4436 return select (lmo->getTreeItem() );
4441 bool VymModel::select (TreeItem *ti)
4445 QModelIndex ix=index(ti);
4446 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4447 ti->setLastSelectedBranch();
4448 //updateSelection(oldsel); //FIXME needed?
4454 void VymModel::unselect()
4456 selModel->clearSelection();
4459 void VymModel::reselect()
4461 selection.reselect();
4464 void VymModel::ensureSelectionVisible()
4466 LinkableMapObj *lmo=getSelectedLMO();
4467 if (lmo &&mapEditor) mapEditor->ensureVisible (lmo->getBBox() );
4471 void VymModel::selectInt (LinkableMapObj *lmo)
4473 if (selection.select(lmo))
4475 //selection.update();
4476 sendSelection (); // FIXME VM use signal
4481 void VymModel::selectNextBranchInt()
4483 // Increase number of branch
4484 LinkableMapObj *sel=getSelectedBranch();
4487 QString s=getSelectString();
4493 part=s.section(",",-1);
4495 num=part.right(part.length() - 3);
4497 s=s.left (s.length() -num.length());
4500 num=QString ("%1").arg(num.toUInt()+1);
4504 // Try to select this one
4505 if (select (s)) return;
4507 // We have no direct successor,
4508 // try to increase the parental number in order to
4509 // find a successor with same depth
4511 int d=getSelectedLMO()->getDepth();
4516 while (!found && d>0)
4518 s=s.section (",",0,d-1);
4519 // replace substring of current depth in s with "1"
4520 part=s.section(",",-1);
4522 num=part.right(part.length() - 3);
4526 // increase number of parent
4527 num=QString ("%1").arg(num.toUInt()+1);
4528 s=s.section (",",0,d-2) + ","+ typ+num;
4531 // Special case, look at orientation
4532 if (getSelectedLMO()->getOrientation()==LinkableMapObj::RightOfCenter)
4533 num=QString ("%1").arg(num.toUInt()+1);
4535 num=QString ("%1").arg(num.toUInt()-1);
4540 // pad to oldDepth, select the first branch for each depth
4541 for (i=d;i<oldDepth;i++)
4546 if ( getSelectedBranch()->countBranches()>0)
4554 // try to select the freshly built string
4562 void VymModel::selectPrevBranchInt()
4564 // Decrease number of branch
4565 if (selectionType()==TreeItem::Branch)
4567 QString s=getSelectString();
4573 part=s.section(",",-1);
4575 num=part.right(part.length() - 3);
4577 s=s.left (s.length() -num.length());
4579 int n=num.toInt()-1;
4582 num=QString ("%1").arg(n);
4585 cout <<"SP:: s0="<<s.toStdString()<<endl;
4588 // Try to select this one
4589 if (n>=0 && select (s)) return;
4591 // We have no direct precessor,
4592 // try to decrease the parental number in order to
4593 // find a precessor with same depth
4595 int d=getSelectedLMO()->getDepth();
4600 while (!found && d>0)
4602 s=s.section (",",0,d-1);
4603 // replace substring of current depth in s with "1"
4604 part=s.section(",",-1);
4606 num=part.right(part.length() - 3);
4610 // decrease number of parent
4611 num=QString ("%1").arg(num.toInt()-1);
4612 s=s.section (",",0,d-2) + ","+ typ+num;
4615 // Special case, look at orientation
4616 if (getSelectedLMO()->getOrientation()==LinkableMapObj::RightOfCenter)
4617 num=QString ("%1").arg(num.toInt()-1);
4619 num=QString ("%1").arg(num.toInt()+1);
4623 cout <<"SP:: si="<<s.toStdString()<<endl;
4625 // pad to oldDepth, select the last branch for each depth
4626 for (i=d;i<oldDepth;i++)
4630 if ( getSelectedBranch()->countBranches()>0)
4631 s+=",bo:"+ QString ("%1").arg( getSelectedBranch()->countBranches()-1 );
4638 // try to select the freshly built string
4646 void VymModel::selectUpperBranch()
4648 if (selection.isBlocked() ) return;
4650 BranchObj *bo=getSelectedBranch();
4651 if (bo && selectionType()==TreeItem::Branch)
4653 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4654 selectPrevBranchInt();
4656 if (bo->getDepth()==1)
4657 selectNextBranchInt();
4659 selectPrevBranchInt();
4663 void VymModel::selectLowerBranch()
4665 if (selection.isBlocked() ) return;
4667 BranchObj *bo=getSelectedBranch();
4668 if (bo && selectionType()==TreeItem::Branch)
4670 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4671 selectNextBranchInt();
4673 if (bo->getDepth()==1)
4674 selectPrevBranchInt();
4676 selectNextBranchInt();
4681 void VymModel::selectLeftBranch()
4683 if (selection.isBlocked() ) return;
4685 QItemSelection oldsel=selModel->selection();
4688 LinkableMapObj *sel=getSelectedBranch();
4691 if (selectionType()== TreeItem::MapCenter)
4693 QModelIndex ix=getSelectedIndex();
4694 selModel->select (index (rowCount(ix)-1,0,ix),QItemSelectionModel::ClearAndSelect );
4697 par=(BranchObj*)(sel->getParObj());
4698 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4701 if (selectionType() == TreeItem::Branch ||
4702 selectionType() == TreeItem::Image)
4704 QModelIndex ix=getSelectedIndex();
4706 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4711 if (selectionType() == TreeItem::Branch )
4713 selectLastSelectedBranch();
4718 updateSelection (oldsel);
4722 void VymModel::selectRightBranch()
4724 if (selection.isBlocked() ) return;
4726 QItemSelection oldsel=selModel->selection();
4729 LinkableMapObj *sel=getSelectedBranch();
4732 if (selectionType()== TreeItem::MapCenter)
4734 QModelIndex ix=getSelectedIndex();
4735 selModel->select (index (0,0,ix),QItemSelectionModel::ClearAndSelect );
4738 par=(BranchObj*)(sel->getParObj());
4739 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4742 if (selectionType() == TreeItem::Branch )
4744 selectLastSelectedBranch();
4750 if (selectionType() == TreeItem::Branch ||
4751 selectionType() == TreeItem::Image)
4753 QModelIndex ix=getSelectedIndex();
4755 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4759 updateSelection (oldsel);
4763 void VymModel::selectFirstBranch()
4765 TreeItem *ti=getSelectedBranchItem();
4768 TreeItem *par=ti->parent();
4770 TreeItem *ti2=par->getFirstBranch();
4774 ensureSelectionVisible();
4780 void VymModel::selectLastBranch()
4782 TreeItem *ti=getSelectedBranchItem();
4785 TreeItem *par=ti->parent();
4787 TreeItem *ti2=par->getLastBranch();
4791 ensureSelectionVisible();
4797 void VymModel::selectLastSelectedBranch()
4799 TreeItem *ti=getSelectedBranchItem();
4802 ti=ti->getLastSelectedBranch();
4803 if (ti) select (ti);
4807 void VymModel::selectParent()
4809 TreeItem *ti=getSelectedItem();
4817 ensureSelectionVisible();
4822 TreeItem::Type VymModel::selectionType()
4824 QModelIndexList list=selModel->selectedIndexes();
4825 if (list.isEmpty()) return TreeItem::Undefined;
4826 TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
4827 return ti->getType();
4831 LinkableMapObj* VymModel::getSelectedLMO()
4833 QModelIndexList list=selModel->selectedIndexes();
4834 if (!list.isEmpty() )
4836 TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
4837 TreeItem::Type type=ti->getType();
4838 if (type ==TreeItem::Branch || type==TreeItem::MapCenter || type==TreeItem::Image)
4840 return ti->getLMO();
4846 BranchObj* VymModel::getSelectedBranch()
4848 TreeItem *ti = getSelectedBranchItem();
4850 return (BranchObj*)ti->getLMO();
4855 TreeItem* VymModel::getSelectedBranchItem()
4857 QModelIndexList list=selModel->selectedIndexes();
4858 if (!list.isEmpty() )
4860 TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
4861 TreeItem::Type type=ti->getType();
4862 if (type ==TreeItem::Branch || type==TreeItem::MapCenter)
4868 TreeItem* VymModel::getSelectedItem()
4870 // FIXME this may not only be branch, but also float etc...
4871 BranchObj* bo=getSelectedBranch();
4872 if (bo) return bo->getTreeItem(); // FIXME VM get directly from treemodl
4876 QModelIndex VymModel::getSelectedIndex()
4878 QModelIndexList list=selModel->selectedIndexes();
4879 if (list.isEmpty() )
4880 return QModelIndex();
4882 return list.first();
4885 FloatImageObj* VymModel::getSelectedFloatImage()
4887 return selection.getFloatImage();
4890 QString VymModel::getSelectString ()
4892 LinkableMapObj *lmo=getSelectedLMO();
4894 return getSelectString(lmo);
4899 QString VymModel::getSelectString (LinkableMapObj *lmo) // FIXME VM needs to use TreeModel
4903 if (typeid(*lmo)==typeid(BranchObj) ||
4904 typeid(*lmo)==typeid(MapCenterObj) )
4906 LinkableMapObj *par=lmo->getParObj();
4909 if (lmo->getDepth() ==1)
4910 // Mainbranch, return
4911 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4913 // Branch, call myself recursively
4914 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4918 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
4919 if (i>=0) s=QString("mc:%1").arg(i);