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 BranchObj* VymModel::first()
1358 if (mapCenters.count()>0)
1359 return mapCenters.first();
1364 BranchObj* VymModel::next(BranchObj *bo_start)
1367 BranchObj *bo=bo_start;
1370 // Try to find next branch in current MapCenter
1372 if (rbo) return rbo;
1374 // Try to find MapCenter of bo
1375 while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
1377 // Try to find next MapCenter
1378 int i=mapCenters.indexOf ((MapCenterObj*)bo);
1379 if (i+2 > mapCenters.count() || i<0) return NULL;
1380 if (mapCenters.at(i+1)!=bo_start)
1381 return mapCenters.at(i+1);
1386 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
1388 LinkableMapObj *lmo;
1390 for (int i=0;i<mapCenters.count(); i++)
1392 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
1393 if (lmo) return lmo;
1398 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
1400 LinkableMapObj *lmo;
1406 part=s.section(",",0,0);
1408 num=part.right(part.length() - 3);
1409 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
1410 return mapCenters.at(num.toInt() );
1413 for (int i=0; i<mapCenters.count(); i++)
1415 lmo=mapCenters.at(i)->findObjBySelect(s);
1416 if (lmo) return lmo;
1421 LinkableMapObj* VymModel::findID (const QString &s)
1423 LinkableMapObj *lmo;
1424 for (int i=0; i<mapCenters.count(); i++)
1426 lmo=mapCenters.at(i)->findID (s);
1427 if (lmo) return lmo;
1432 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
1436 for (int i=0; i<mapCenters.count(); i++)
1437 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
1441 //////////////////////////////////////////////
1443 //////////////////////////////////////////////
1444 void VymModel::setVersion (const QString &s)
1449 void VymModel::setAuthor (const QString &s)
1452 QString ("setMapAuthor (\"%1\")").arg(author),
1453 QString ("setMapAuthor (\"%1\")").arg(s),
1454 QString ("Set author of map to \"%1\"").arg(s)
1460 QString VymModel::getAuthor()
1465 void VymModel::setComment (const QString &s)
1468 QString ("setMapComment (\"%1\")").arg(comment),
1469 QString ("setMapComment (\"%1\")").arg(s),
1470 QString ("Set comment of map")
1476 QString VymModel::getComment ()
1481 QString VymModel::getDate ()
1483 return QDate::currentDate().toString ("yyyy-MM-dd");
1486 void VymModel::setHeading(const QString &s)
1488 BranchObj *sel=getSelectedBranch();
1493 "setHeading (\""+sel->getHeading()+"\")",
1495 "setHeading (\""+s+"\")",
1496 QString("Set heading of %1 to \"%2\"").arg(getObjectName(sel)).arg(s) );
1497 sel->setHeading(s );
1498 /* FIXME testing only
1500 TreeItem *ti=getSelectedItem();
1504 //FIXME VM ix is wrong ModelIndex below, ix2 is (hopefully) correct:
1505 //QModelIndex ix=index( ti->row(), ti->column(), index (0,0,QModelIndex()) );
1506 //FIXME VM testing only cout <<"VM::setHeading s="<<s.toStdString()<<" ti="<<ti<<" r,c=("<<ti->row()<<","<<ti->column()<<")"<<endl;
1507 QModelIndex ix2=index (ti);
1508 emit (dataChanged ( ix2,ix2));
1512 cout << "Warning: VM::setHeading ti==NULL\n";
1515 //cout <<" (r,c)=("<<ix2.row()<<","<<ix2.column()<<")"<<endl;
1519 ensureSelectionVisible();
1523 BranchObj* VymModel::findText (QString s, bool cs)
1525 QTextDocument::FindFlags flags=0;
1526 if (cs) flags=QTextDocument::FindCaseSensitively;
1529 { // Nothing found or new find process
1531 // nothing found, start again
1535 bool searching=true;
1536 bool foundNote=false;
1537 while (searching && !EOFind)
1541 // Searching in Note
1542 if (itFind->getNote().contains(s,cs))
1544 if (getSelectedBranch()!=itFind)
1547 ensureSelectionVisible();
1549 if (textEditor->findText(s,flags))
1555 // Searching in Heading
1556 if (searching && itFind->getHeading().contains (s,cs) )
1559 ensureSelectionVisible();
1565 itFind=next(itFind);
1566 if (!itFind) EOFind=true;
1568 //cout <<"still searching... "<<qPrintable( itFind->getHeading())<<endl;
1571 return getSelectedBranch();
1576 void VymModel::findReset()
1577 { // Necessary if text to find changes during a find process
1584 void VymModel::setScene (QGraphicsScene *s)
1586 cout << "VM::setscene scene="<<s<<endl;
1587 mapScene=s; // FIXME VM should not be necessary anymore, move all occurences to MapEditor
1588 //init(); // Here we have a mapScene set,
1589 // which is (still) needed to create MapCenters
1592 void VymModel::setURL(const QString &url)
1594 BranchObj *bo=getSelectedBranch();
1597 QString oldurl=bo->getURL();
1601 QString ("setURL (\"%1\")").arg(oldurl),
1603 QString ("setURL (\"%1\")").arg(url),
1604 QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
1609 ensureSelectionVisible();
1613 QString VymModel::getURL()
1615 BranchObj *bo=getSelectedBranch();
1617 return bo->getURL();
1622 QStringList VymModel::getURLs()
1625 BranchObj *bo=getSelectedBranch();
1631 if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
1638 void VymModel::linkFloatImageTo(const QString &dstString)
1640 FloatImageObj *fio=selection.getFloatImage();
1643 BranchObj *dst=(BranchObj*)findObjBySelect(dstString);
1644 if (dst && (typeid(*dst)==typeid (BranchObj) ||
1645 typeid(*dst)==typeid (MapCenterObj)))
1647 LinkableMapObj *dstPar=dst->getParObj();
1648 QString parString=getSelectString(dstPar);
1649 QString fioPreSelectString=getSelectString(fio);
1650 QString fioPreParentSelectString=getSelectString (fio->getParObj());
1651 ((BranchObj*)dst)->addFloatImage (fio);
1652 selection.unselect();
1653 ((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
1654 fio=((BranchObj*)dst)->getLastFloatImage();
1657 selection.select(fio);
1659 getSelectString(fio),
1660 QString("linkTo (\"%1\")").arg(fioPreParentSelectString),
1662 QString ("linkTo (\"%1\")").arg(dstString),
1663 QString ("Link floatimage to %1").arg(getObjectName(dst)));
1669 void VymModel::setFrameType(const FrameObj::FrameType &t)
1671 BranchObj *bo=getSelectedBranch();
1674 QString s=bo->getFrameTypeName();
1675 bo->setFrameType (t);
1676 saveState (bo, QString("setFrameType (\"%1\")").arg(s),
1677 bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
1683 void VymModel::setFrameType(const QString &s)
1685 BranchObj *bo=getSelectedBranch();
1688 saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
1689 bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
1690 bo->setFrameType (s);
1696 void VymModel::setFramePenColor(const QColor &c)
1698 BranchObj *bo=getSelectedBranch();
1701 saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
1702 bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
1703 bo->setFramePenColor (c);
1707 void VymModel::setFrameBrushColor(const QColor &c)
1709 BranchObj *bo=getSelectedBranch();
1712 saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
1713 bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
1714 bo->setFrameBrushColor (c);
1718 void VymModel::setFramePadding (const int &i)
1720 BranchObj *bo=getSelectedBranch();
1723 saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
1724 bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
1725 bo->setFramePadding (i);
1731 void VymModel::setFrameBorderWidth(const int &i)
1733 BranchObj *bo=getSelectedBranch();
1736 saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
1737 bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
1738 bo->setFrameBorderWidth (i);
1744 void VymModel::setIncludeImagesVer(bool b)
1746 BranchObj *bo=getSelectedBranch();
1749 QString u= b ? "false" : "true";
1750 QString r=!b ? "false" : "true";
1754 QString("setIncludeImagesVertically (%1)").arg(u),
1756 QString("setIncludeImagesVertically (%1)").arg(r),
1757 QString("Include images vertically in %1").arg(getObjectName(bo))
1759 bo->setIncludeImagesVer(b);
1764 void VymModel::setIncludeImagesHor(bool b)
1766 BranchObj *bo=getSelectedBranch();
1769 QString u= b ? "false" : "true";
1770 QString r=!b ? "false" : "true";
1774 QString("setIncludeImagesHorizontally (%1)").arg(u),
1776 QString("setIncludeImagesHorizontally (%1)").arg(r),
1777 QString("Include images horizontally in %1").arg(getObjectName(bo))
1779 bo->setIncludeImagesHor(b);
1784 void VymModel::setHideLinkUnselected (bool b)
1786 LinkableMapObj *sel=getSelectedLMO();
1788 (selectionType() == TreeItem::Branch ||
1789 selectionType() == TreeItem::MapCenter ||
1790 selectionType() == TreeItem::Image ))
1792 QString u= b ? "false" : "true";
1793 QString r=!b ? "false" : "true";
1797 QString("setHideLinkUnselected (%1)").arg(u),
1799 QString("setHideLinkUnselected (%1)").arg(r),
1800 QString("Hide link of %1 if unselected").arg(getObjectName(sel))
1802 sel->setHideLinkUnselected(b);
1806 void VymModel::setHideExport(bool b)
1808 BranchObj *bo=getSelectedBranch();
1811 bo->setHideInExport (b);
1812 QString u= b ? "false" : "true";
1813 QString r=!b ? "false" : "true";
1817 QString ("setHideExport (%1)").arg(u),
1819 QString ("setHideExport (%1)").arg(r),
1820 QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bo)).arg (r)
1825 // FIXME VM needed? scene()->update();
1829 void VymModel::toggleHideExport()
1831 BranchObj *bo=getSelectedBranch();
1833 setHideExport ( !bo->hideInExport() );
1837 void VymModel::copy()
1839 LinkableMapObj *sel=getSelectedLMO();
1841 (selectionType() == TreeItem::Branch ||
1842 selectionType() == TreeItem::MapCenter ||
1843 selectionType() == TreeItem::Image ))
1845 if (redosAvail == 0)
1848 QString s=getSelectString(sel);
1849 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel );
1850 curClipboard=curStep;
1853 // Copy also to global clipboard, because we are at last step in history
1854 QString bakMapName(QString("history-%1").arg(curStep));
1855 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1856 copyDir (bakMapDir,clipboardDir );
1858 clipboardEmpty=false;
1864 void VymModel::pasteNoSave(const int &n)
1866 bool old=blockSaveState;
1867 blockSaveState=true;
1868 bool zippedOrg=zipped;
1869 if (redosAvail > 0 || n!=0)
1871 // Use the "historical" buffer
1872 QString bakMapName(QString("history-%1").arg(n));
1873 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1874 load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
1876 // Use the global buffer
1877 load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
1882 void VymModel::paste()
1884 BranchObj *sel=getSelectedBranch();
1887 saveStateChangingPart(
1890 QString ("paste (%1)").arg(curClipboard),
1891 QString("Paste to %1").arg( getObjectName(sel))
1898 void VymModel::cut()
1900 LinkableMapObj *sel=getSelectedLMO();
1901 if ( sel && (selectionType() == TreeItem::Branch ||
1902 selectionType()==TreeItem::MapCenter ||
1903 selectionType()==TreeItem::Image))
1905 /* No savestate! savestate is called in cutNoSave
1906 saveStateChangingPart(
1910 QString("Cut %1").arg(getObjectName(sel ))
1919 void VymModel::moveBranchUp()
1921 BranchObj* bo=getSelectedBranch();
1925 if (!bo->canMoveBranchUp()) return;
1926 par=(BranchObj*)(bo->getParObj());
1927 BranchObj *obo=par->moveBranchUp (bo); // bo will be the one below selection
1928 saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
1930 //FIXME VM needed? scene()->update();
1932 ensureSelectionVisible();
1936 void VymModel::moveBranchDown()
1938 BranchObj* bo=getSelectedBranch();
1942 if (!bo->canMoveBranchDown()) return;
1943 par=(BranchObj*)(bo->getParObj());
1944 BranchObj *obo=par->moveBranchDown(bo); // bo will be the one above selection
1945 saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
1947 //FIXME VM needed? scene()->update();
1949 ensureSelectionVisible();
1953 void VymModel::sortChildren()
1955 BranchObj* bo=getSelectedBranch();
1958 if(bo->countBranches()>1)
1960 saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
1963 ensureSelectionVisible();
1968 void VymModel::createMapCenter()
1970 MapCenterObj *mco=addMapCenter (QPointF (0,0) );
1974 void VymModel::createBranch()
1976 addNewBranchInt (-2);
1980 TreeItem* VymModel::createImage()
1982 BranchObj *bo=getSelectedBranch();
1985 FloatImageObj *newfio=bo->addFloatImage(); // FIXME VM Old model, merge with below
1988 QList<QVariant> cData;
1989 cData << "VM:createImage" << "undef"<<"undef";
1990 TreeItem *parti=bo->getTreeItem();
1991 TreeItem *ti=new TreeItem (cData,parti);
1992 ti->setLMO (newfio);
1993 ti->setType (TreeItem::Image);
1994 parti->appendChild (ti);
1998 newfio->setTreeItem (ti);
1999 select (newfio); // FIXME VM really needed here?
2006 MapCenterObj* VymModel::addMapCenter ()
2008 MapCenterObj *mco=addMapCenter (contextPos);
2009 cout <<"VM::addMCO () mapScene="<<mapScene<<endl;
2010 selection.select (mco);
2012 ensureSelectionVisible();
2017 QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
2018 QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
2023 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
2025 MapCenterObj *mapCenter = new MapCenterObj(mapScene,this);
2026 mapCenter->setMapEditor(mapEditor); //FIXME VM needed to get defLinkStyle, mapLinkColorHint ... for later added objects
2027 mapCenter->move (absPos);
2028 mapCenter->setVisibility (true);
2029 mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
2030 mapCenters.append(mapCenter);
2033 QList<QVariant> cData;
2034 cData << "VM:addMapCenter" << "undef"<<"undef";
2035 TreeItem *ti=new TreeItem (cData,rootItem);
2036 ti->setLMO (mapCenter);
2037 ti->setType (TreeItem::MapCenter);
2038 mapCenter->setTreeItem (ti);
2039 rootItem->appendChild (ti);
2044 MapCenterObj* VymModel::removeMapCenter(MapCenterObj* mco)
2046 int i=mapCenters.indexOf (mco);
2049 mapCenters.removeAt (i);
2051 if (i>0) return mapCenters.at(i-1); // Return previous MCO
2056 MapCenterObj* VymModel::getLastMapCenter()
2058 if (mapCenters.size()>0)
2059 return mapCenters.last();
2066 BranchObj* VymModel::addNewBranchInt(int num)
2068 // Depending on pos:
2069 // -3 insert in children of parent above selection
2070 // -2 add branch to selection
2071 // -1 insert in children of parent below selection
2072 // 0..n insert in children of parent at pos
2073 BranchObj *newbo=NULL;
2074 BranchObj *bo=getSelectedBranch();
2079 // save scroll state. If scrolled, automatically select
2080 // new branch in order to tmp unscroll parent...
2081 newbo=bo->addBranch();
2084 QList<QVariant> cData;
2085 cData << "VM:createBranch" << "undef"<<"undef";
2086 TreeItem *parti=bo->getTreeItem();
2087 TreeItem *ti=new TreeItem (cData,parti);
2089 ti->setType (TreeItem::Branch);
2090 parti->appendChild (ti);
2094 newbo->setTreeItem (ti);
2095 select (newbo); // FIXME VM really needed here?
2101 bo=(BranchObj*)bo->getParObj();
2102 if (bo) newbo=bo->insertBranch(num);
2106 bo=(BranchObj*)bo->getParObj();
2107 if (bo) newbo=bo->insertBranch(num);
2113 BranchObj* VymModel::addNewBranch(int pos)
2115 // Different meaning than num in addNewBranchInt!
2119 BranchObj *bo = getSelectedBranch();
2120 BranchObj *newbo=NULL;
2124 // FIXME VM do we still need this in model? setCursor (Qt::ArrowCursor);
2126 newbo=addNewBranchInt (pos-2);
2134 QString ("addBranch (%1)").arg(pos),
2135 QString ("Add new branch to %1").arg(getObjectName(bo)));
2138 // selection.update(); FIXME
2139 latestSelectionString=getSelectString(newbo);
2140 // In Network mode, the client needs to know where the new branch is,
2141 // so we have to pass on this information via saveState.
2142 // TODO: Get rid of this positioning workaround
2143 QString ps=qpointfToString (newbo->getAbsPos());
2144 sendData ("selectLatestAdded ()");
2145 sendData (QString("move %1").arg(ps));
2153 BranchObj* VymModel::addNewBranchBefore()
2155 BranchObj *newbo=NULL;
2156 BranchObj *bo = getSelectedBranch();
2157 if (bo && selectionType()==TreeItem::Branch)
2158 // We accept no MapCenterObj here, so we _have_ a parent
2160 QPointF p=bo->getRelPos();
2163 BranchObj *parbo=(BranchObj*)(bo->getParObj());
2165 // add below selection
2166 newbo=parbo->insertBranch(bo->getNum()+1);
2169 newbo->move2RelPos (p);
2171 // Move selection to new branch
2172 bo->linkTo (newbo,-1);
2174 saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()",
2175 QString ("Add branch before %1").arg(getObjectName(bo)));
2178 // selection.update(); FIXME
2181 latestSelectionString=selection.getSelectString();
2185 void VymModel::deleteSelection()
2187 BranchObj *bo = getSelectedBranch();
2189 if (bo && selectionType()==TreeItem::MapCenter)
2191 // BranchObj* par=(BranchObj*)(bo->getParObj());
2192 selection.unselect();
2193 /* FIXME VM Note: does saveStateRemovingPart work for MCO? (No parent!)
2194 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2196 bo=removeMapCenter ((MapCenterObj*)bo);
2199 selection.select (bo);
2200 ensureSelectionVisible();
2206 if (bo && selectionType()==TreeItem::Branch)
2208 QModelIndex ix=getSelectedIndex();
2210 BranchObj* par=(BranchObj*)bo->getParObj();
2212 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2213 par->removeBranch(bo);
2215 ensureSelectionVisible();
2220 FloatImageObj *fio=selection.getFloatImage();
2223 BranchObj* par=(BranchObj*)fio->getParObj();
2224 saveStateChangingPart(
2228 QString("Delete %1").arg(getObjectName(fio))
2231 par->removeFloatImage(fio);
2235 ensureSelectionVisible();
2240 void VymModel::deleteKeepChildren()
2242 BranchObj *bo=getSelectedBranch();
2246 par=(BranchObj*)(bo->getParObj());
2248 // Don't use this on mapcenter
2251 // Check if we have childs at all to keep
2252 if (bo->countBranches()==0)
2258 QPointF p=bo->getRelPos();
2259 saveStateChangingPart(
2262 "deleteKeepChildren ()",
2263 QString("Remove %1 and keep its children").arg(getObjectName(bo))
2266 QString sel=getSelectString(bo);
2268 par->removeBranchHere(bo);
2271 getSelectedBranch()->move2RelPos (p);
2276 void VymModel::deleteChildren()
2278 BranchObj *bo=getSelectedBranch();
2281 saveStateChangingPart(
2284 "deleteChildren ()",
2285 QString( "Remove children of branch %1").arg(getObjectName(bo))
2287 bo->removeChildren();
2293 bool VymModel::scrollBranch(BranchObj *bo)
2297 if (bo->isScrolled()) return false;
2298 if (bo->countBranches()==0) return false;
2299 if (bo->getDepth()==0) return false;
2305 QString ("%1 ()").arg(u),
2307 QString ("%1 ()").arg(r),
2308 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2312 // FIXME VM needed? scene()->update();
2318 bool VymModel::unscrollBranch(BranchObj *bo)
2322 if (!bo->isScrolled()) return false;
2323 if (bo->countBranches()==0) return false;
2324 if (bo->getDepth()==0) return false;
2330 QString ("%1 ()").arg(u),
2332 QString ("%1 ()").arg(r),
2333 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2337 // FIXME VM needed? scene()->update();
2343 void VymModel::toggleScroll()
2345 BranchObj *bo=getSelectedBranch();
2346 if (selectionType()==TreeItem::Branch )
2348 if (bo->isScrolled())
2349 unscrollBranch (bo);
2355 void VymModel::unscrollChildren()
2357 BranchObj *bo=getSelectedBranch();
2363 if (bo->isScrolled()) unscrollBranch (bo);
2368 void VymModel::addFloatImage (const QPixmap &img)
2370 BranchObj *bo=getSelectedBranch();
2373 FloatImageObj *fio=bo->addFloatImage();
2375 fio->setOriginalFilename("No original filename (image added by dropevent)");
2376 QString s=getSelectString(bo);
2377 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio );
2378 saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
2380 // FIXME VM needed? scene()->update();
2385 void VymModel::colorBranch (QColor c)
2387 BranchObj *bo=getSelectedBranch();
2392 QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
2394 QString ("colorBranch (\"%1\")").arg(c.name()),
2395 QString("Set color of %1 to %2").arg(getObjectName(bo)).arg(c.name())
2397 bo->setColor(c); // color branch
2401 void VymModel::colorSubtree (QColor c)
2403 BranchObj *bo=getSelectedBranch();
2406 saveStateChangingPart(
2409 QString ("colorSubtree (\"%1\")").arg(c.name()),
2410 QString ("Set color of %1 and children to %2").arg(getObjectName(bo)).arg(c.name())
2412 bo->setColorSubtree (c); // color links, color children
2416 QColor VymModel::getCurrentHeadingColor()
2418 BranchObj *bo=getSelectedBranch();
2419 if (bo) return bo->getColor();
2421 QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
2427 void VymModel::editURL()
2429 BranchObj *bo=getSelectedBranch();
2433 QString text = QInputDialog::getText(
2434 "VYM", tr("Enter URL:"), QLineEdit::Normal,
2435 bo->getURL(), &ok, NULL);
2437 // user entered something and pressed OK
2442 void VymModel::editLocalURL()
2444 BranchObj *bo=getSelectedBranch();
2447 QStringList filters;
2448 filters <<"All files (*)";
2449 filters << tr("Text","Filedialog") + " (*.txt)";
2450 filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
2451 filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
2452 filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
2453 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
2454 fd->setFilters (filters);
2455 fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
2456 fd->setDirectory (lastFileDir);
2457 if (! bo->getVymLink().isEmpty() )
2458 fd->selectFile( bo->getURL() );
2461 if ( fd->exec() == QDialog::Accepted )
2463 lastFileDir=QDir (fd->directory().path());
2464 setURL (fd->selectedFile() );
2470 void VymModel::editHeading2URL()
2472 BranchObj *bo=getSelectedBranch();
2474 setURL (bo->getHeading());
2477 void VymModel::editBugzilla2URL()
2479 BranchObj *bo=getSelectedBranch();
2482 QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
2487 void VymModel::editFATE2URL()
2489 BranchObj *bo=getSelectedBranch();
2492 QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
2495 "setURL (\""+bo->getURL()+"\")",
2497 "setURL (\""+url+"\")",
2498 QString("Use heading of %1 as link to FATE").arg(getObjectName(bo))
2505 void VymModel::editVymLink()
2507 BranchObj *bo=getSelectedBranch();
2510 QStringList filters;
2511 filters <<"VYM map (*.vym)";
2512 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
2513 fd->setFilters (filters);
2514 fd->setCaption(vymName+" - " +tr("Link to another map"));
2515 fd->setDirectory (lastFileDir);
2516 if (! bo->getVymLink().isEmpty() )
2517 fd->selectFile( bo->getVymLink() );
2521 if ( fd->exec() == QDialog::Accepted )
2523 lastFileDir=QDir (fd->directory().path());
2526 "setVymLink (\""+bo->getVymLink()+"\")",
2528 "setVymLink (\""+fd->selectedFile()+"\")",
2529 QString("Set vymlink of %1 to %2").arg(getObjectName(bo)).arg(fd->selectedFile())
2531 setVymLink (fd->selectedFile() ); // FIXME ok?
2536 void VymModel::setVymLink (const QString &s)
2538 // Internal function, no saveState needed
2539 BranchObj *bo=getSelectedBranch();
2546 ensureSelectionVisible();
2550 void VymModel::deleteVymLink()
2552 BranchObj *bo=getSelectedBranch();
2557 "setVymLink (\""+bo->getVymLink()+"\")",
2559 "setVymLink (\"\")",
2560 QString("Unset vymlink of %1").arg(getObjectName(bo))
2562 bo->setVymLink ("" );
2565 // FIXME VM needed? scene()->update();
2569 QString VymModel::getVymLink()
2571 BranchObj *bo=getSelectedBranch();
2573 return bo->getVymLink();
2579 QStringList VymModel::getVymLinks()
2582 BranchObj *bo=getSelectedBranch();
2588 if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
2596 void VymModel::followXLink(int i)
2598 BranchObj *bo=getSelectedBranch();
2601 bo=bo->XLinkTargetAt(i);
2604 selection.select(bo);
2605 ensureSelectionVisible();
2610 void VymModel::editXLink(int i) // FIXME VM missing saveState
2612 BranchObj *bo=getSelectedBranch();
2615 XLinkObj *xlo=bo->XLinkAt(i);
2618 EditXLinkDialog dia;
2620 dia.setSelection(bo);
2621 if (dia.exec() == QDialog::Accepted)
2623 if (dia.useSettingsGlobal() )
2625 setMapDefXLinkColor (xlo->getColor() );
2626 setMapDefXLinkWidth (xlo->getWidth() );
2628 if (dia.deleteXLink())
2629 bo->deleteXLinkAt(i);
2639 //////////////////////////////////////////////
2641 //////////////////////////////////////////////
2643 void VymModel::parseAtom(const QString &atom)
2645 BranchObj *selb=getSelectedBranch();
2651 // Split string s into command and parameters
2652 parser.parseAtom (atom);
2653 QString com=parser.getCommand();
2655 // External commands
2656 /////////////////////////////////////////////////////////////////////
2657 if (com=="addBranch")
2659 if (selection.isEmpty())
2661 parser.setError (Aborted,"Nothing selected");
2664 parser.setError (Aborted,"Type of selection is not a branch");
2669 if (parser.checkParCount(pl))
2671 if (parser.parCount()==0)
2675 n=parser.parInt (ok,0);
2676 if (ok ) addNewBranch (n);
2680 /////////////////////////////////////////////////////////////////////
2681 } else if (com=="addBranchBefore")
2683 if (selection.isEmpty())
2685 parser.setError (Aborted,"Nothing selected");
2688 parser.setError (Aborted,"Type of selection is not a branch");
2691 if (parser.parCount()==0)
2693 addNewBranchBefore ();
2696 /////////////////////////////////////////////////////////////////////
2697 } else if (com==QString("addMapCenter"))
2699 if (parser.checkParCount(2))
2701 x=parser.parDouble (ok,0);
2704 y=parser.parDouble (ok,1);
2705 if (ok) addMapCenter (QPointF(x,y));
2708 /////////////////////////////////////////////////////////////////////
2709 } else if (com==QString("addMapReplace"))
2711 if (selection.isEmpty())
2713 parser.setError (Aborted,"Nothing selected");
2716 parser.setError (Aborted,"Type of selection is not a branch");
2717 } else if (parser.checkParCount(1))
2719 //s=parser.parString (ok,0); // selection
2720 t=parser.parString (ok,0); // path to map
2721 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2722 addMapReplaceInt(getSelectString(selb),t);
2724 /////////////////////////////////////////////////////////////////////
2725 } else if (com==QString("addMapInsert"))
2727 if (selection.isEmpty())
2729 parser.setError (Aborted,"Nothing selected");
2732 parser.setError (Aborted,"Type of selection is not a branch");
2735 if (parser.checkParCount(2))
2737 t=parser.parString (ok,0); // path to map
2738 n=parser.parInt(ok,1); // position
2739 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2740 addMapInsertInt(t,n);
2743 /////////////////////////////////////////////////////////////////////
2744 } else if (com=="clearFlags")
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(0))
2754 selb->clearStandardFlags();
2755 selb->updateFlagsToolbar();
2757 /////////////////////////////////////////////////////////////////////
2758 } else if (com=="colorBranch")
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(1))
2768 QColor c=parser.parColor (ok,0);
2769 if (ok) colorBranch (c);
2771 /////////////////////////////////////////////////////////////////////
2772 } else if (com=="colorSubtree")
2774 if (selection.isEmpty())
2776 parser.setError (Aborted,"Nothing selected");
2779 parser.setError (Aborted,"Type of selection is not a branch");
2780 } else if (parser.checkParCount(1))
2782 QColor c=parser.parColor (ok,0);
2783 if (ok) colorSubtree (c);
2785 /////////////////////////////////////////////////////////////////////
2786 } else if (com=="copy")
2788 if (selection.isEmpty())
2790 parser.setError (Aborted,"Nothing selected");
2793 parser.setError (Aborted,"Type of selection is not a branch");
2794 } else if (parser.checkParCount(0))
2796 //FIXME missing action for copy
2798 /////////////////////////////////////////////////////////////////////
2799 } else if (com=="cut")
2801 if (selection.isEmpty())
2803 parser.setError (Aborted,"Nothing selected");
2804 } else if ( selectionType()!=TreeItem::Branch &&
2805 selectionType()!=TreeItem::MapCenter &&
2806 selectionType()!=TreeItem::Image )
2808 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2809 } else if (parser.checkParCount(0))
2813 /////////////////////////////////////////////////////////////////////
2814 } else if (com=="delete")
2816 if (selection.isEmpty())
2818 parser.setError (Aborted,"Nothing selected");
2820 /*else if (selectionType() != TreeItem::Branch && selectionType() != TreeItem::Image )
2822 parser.setError (Aborted,"Type of selection is wrong.");
2825 else if (parser.checkParCount(0))
2829 /////////////////////////////////////////////////////////////////////
2830 } else if (com=="deleteKeepChildren")
2832 if (selection.isEmpty())
2834 parser.setError (Aborted,"Nothing selected");
2837 parser.setError (Aborted,"Type of selection is not a branch");
2838 } else if (parser.checkParCount(0))
2840 deleteKeepChildren();
2842 /////////////////////////////////////////////////////////////////////
2843 } else if (com=="deleteChildren")
2845 if (selection.isEmpty())
2847 parser.setError (Aborted,"Nothing selected");
2850 parser.setError (Aborted,"Type of selection is not a branch");
2851 } else if (parser.checkParCount(0))
2855 /////////////////////////////////////////////////////////////////////
2856 } else if (com=="exportASCII")
2860 if (parser.parCount()>=1)
2861 // Hey, we even have a filename
2862 fname=parser.parString(ok,0);
2865 parser.setError (Aborted,"Could not read filename");
2868 exportASCII (fname,false);
2870 /////////////////////////////////////////////////////////////////////
2871 } else if (com=="exportImage")
2875 if (parser.parCount()>=2)
2876 // Hey, we even have a filename
2877 fname=parser.parString(ok,0);
2880 parser.setError (Aborted,"Could not read filename");
2883 QString format="PNG";
2884 if (parser.parCount()>=2)
2886 format=parser.parString(ok,1);
2888 exportImage (fname,false,format);
2890 /////////////////////////////////////////////////////////////////////
2891 } else if (com=="exportXHTML")
2895 if (parser.parCount()>=2)
2896 // Hey, we even have a filename
2897 fname=parser.parString(ok,1);
2900 parser.setError (Aborted,"Could not read filename");
2903 exportXHTML (fname,false);
2905 /////////////////////////////////////////////////////////////////////
2906 } else if (com=="exportXML")
2910 if (parser.parCount()>=2)
2911 // Hey, we even have a filename
2912 fname=parser.parString(ok,1);
2915 parser.setError (Aborted,"Could not read filename");
2918 exportXML (fname,false);
2920 /////////////////////////////////////////////////////////////////////
2921 } else if (com=="importDir")
2923 if (selection.isEmpty())
2925 parser.setError (Aborted,"Nothing selected");
2928 parser.setError (Aborted,"Type of selection is not a branch");
2929 } else if (parser.checkParCount(1))
2931 s=parser.parString(ok,0);
2932 if (ok) importDirInt(s);
2934 /////////////////////////////////////////////////////////////////////
2935 } else if (com=="linkTo")
2937 if (selection.isEmpty())
2939 parser.setError (Aborted,"Nothing selected");
2942 if (parser.checkParCount(4))
2944 // 0 selectstring of parent
2945 // 1 num in parent (for branches)
2946 // 2,3 x,y of mainbranch or mapcenter
2947 s=parser.parString(ok,0);
2948 LinkableMapObj *dst=findObjBySelect (s);
2951 if (typeid(*dst) == typeid(BranchObj) )
2953 // Get number in parent
2954 n=parser.parInt (ok,1);
2957 selb->linkTo ((BranchObj*)(dst),n);
2960 } else if (typeid(*dst) == typeid(MapCenterObj) )
2962 selb->linkTo ((BranchObj*)(dst),-1);
2963 // Get coordinates of mainbranch
2964 x=parser.parDouble(ok,2);
2967 y=parser.parDouble(ok,3);
2977 } else if ( selectionType() == TreeItem::Image)
2979 if (parser.checkParCount(1))
2981 // 0 selectstring of parent
2982 s=parser.parString(ok,0);
2983 LinkableMapObj *dst=findObjBySelect (s);
2986 if (typeid(*dst) == typeid(BranchObj) ||
2987 typeid(*dst) == typeid(MapCenterObj))
2988 linkFloatImageTo (getSelectString(dst));
2990 parser.setError (Aborted,"Destination is not a branch");
2993 parser.setError (Aborted,"Type of selection is not a floatimage or branch");
2994 /////////////////////////////////////////////////////////////////////
2995 } else if (com=="loadImage")
2997 if (selection.isEmpty())
2999 parser.setError (Aborted,"Nothing selected");
3002 parser.setError (Aborted,"Type of selection is not a branch");
3003 } else if (parser.checkParCount(1))
3005 s=parser.parString(ok,0);
3006 if (ok) loadFloatImageInt (s);
3008 /////////////////////////////////////////////////////////////////////
3009 } else if (com=="moveBranchUp")
3011 if (selection.isEmpty() )
3013 parser.setError (Aborted,"Nothing selected");
3016 parser.setError (Aborted,"Type of selection is not a branch");
3017 } else if (parser.checkParCount(0))
3021 /////////////////////////////////////////////////////////////////////
3022 } else if (com=="moveBranchDown")
3024 if (selection.isEmpty() )
3026 parser.setError (Aborted,"Nothing selected");
3029 parser.setError (Aborted,"Type of selection is not a branch");
3030 } else if (parser.checkParCount(0))
3034 /////////////////////////////////////////////////////////////////////
3035 } else if (com=="move")
3037 if (selection.isEmpty() )
3039 parser.setError (Aborted,"Nothing selected");
3040 } else if ( selectionType()!=TreeItem::Branch &&
3041 selectionType()!=TreeItem::MapCenter &&
3042 selectionType()!=TreeItem::Image )
3044 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3045 } else if (parser.checkParCount(2))
3047 x=parser.parDouble (ok,0);
3050 y=parser.parDouble (ok,1);
3054 /////////////////////////////////////////////////////////////////////
3055 } else if (com=="moveRel")
3057 if (selection.isEmpty() )
3059 parser.setError (Aborted,"Nothing selected");
3060 } else if ( selectionType()!=TreeItem::Branch &&
3061 selectionType()!=TreeItem::MapCenter &&
3062 selectionType()!=TreeItem::Image )
3064 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3065 } else if (parser.checkParCount(2))
3067 x=parser.parDouble (ok,0);
3070 y=parser.parDouble (ok,1);
3071 if (ok) moveRel (x,y);
3074 /////////////////////////////////////////////////////////////////////
3075 } else if (com=="nop")
3077 /////////////////////////////////////////////////////////////////////
3078 } else if (com=="paste")
3080 if (selection.isEmpty() )
3082 parser.setError (Aborted,"Nothing selected");
3085 parser.setError (Aborted,"Type of selection is not a branch");
3086 } else if (parser.checkParCount(1))
3088 n=parser.parInt (ok,0);
3089 if (ok) pasteNoSave(n);
3091 /////////////////////////////////////////////////////////////////////
3092 } else if (com=="qa")
3094 if (selection.isEmpty() )
3096 parser.setError (Aborted,"Nothing selected");
3099 parser.setError (Aborted,"Type of selection is not a branch");
3100 } else if (parser.checkParCount(4))
3103 c=parser.parString (ok,0);
3106 parser.setError (Aborted,"No comment given");
3109 s=parser.parString (ok,1);
3112 parser.setError (Aborted,"First parameter is not a string");
3115 t=parser.parString (ok,2);
3118 parser.setError (Aborted,"Condition is not a string");
3121 u=parser.parString (ok,3);
3124 parser.setError (Aborted,"Third parameter is not a string");
3129 parser.setError (Aborted,"Unknown type: "+s);
3134 parser.setError (Aborted,"Unknown operator: "+t);
3139 parser.setError (Aborted,"Type of selection is not a branch");
3142 if (selb->getHeading() == u)
3144 cout << "PASSED: " << qPrintable (c) << endl;
3147 cout << "FAILED: " << qPrintable (c) << endl;
3157 /////////////////////////////////////////////////////////////////////
3158 } else if (com=="saveImage")
3160 FloatImageObj *fio=selection.getFloatImage();
3163 parser.setError (Aborted,"Type of selection is not an image");
3164 } else if (parser.checkParCount(2))
3166 s=parser.parString(ok,0);
3169 t=parser.parString(ok,1);
3170 if (ok) saveFloatImageInt (fio,t,s);
3173 /////////////////////////////////////////////////////////////////////
3174 } else if (com=="scroll")
3176 if (selection.isEmpty() )
3178 parser.setError (Aborted,"Nothing selected");
3181 parser.setError (Aborted,"Type of selection is not a branch");
3182 } else if (parser.checkParCount(0))
3184 if (!scrollBranch (selb))
3185 parser.setError (Aborted,"Could not scroll branch");
3187 /////////////////////////////////////////////////////////////////////
3188 } else if (com=="select")
3190 if (parser.checkParCount(1))
3192 s=parser.parString(ok,0);
3195 /////////////////////////////////////////////////////////////////////
3196 } else if (com=="selectLastBranch")
3198 if (selection.isEmpty() )
3200 parser.setError (Aborted,"Nothing selected");
3203 parser.setError (Aborted,"Type of selection is not a branch");
3204 } else if (parser.checkParCount(0))
3206 BranchObj *bo=selb->getLastBranch();
3208 parser.setError (Aborted,"Could not select last branch");
3212 /////////////////////////////////////////////////////////////////////
3213 } else if (com=="selectLastImage")
3215 if (selection.isEmpty() )
3217 parser.setError (Aborted,"Nothing selected");
3220 parser.setError (Aborted,"Type of selection is not a branch");
3221 } else if (parser.checkParCount(0))
3223 FloatImageObj *fio=selb->getLastFloatImage();
3225 parser.setError (Aborted,"Could not select last image");
3229 /////////////////////////////////////////////////////////////////////
3230 } else if (com=="selectLatestAdded")
3232 if (latestSelectionString.isEmpty() )
3234 parser.setError (Aborted,"No latest added object");
3237 if (!select (latestSelectionString))
3238 parser.setError (Aborted,"Could not select latest added object "+latestSelectionString);
3240 /////////////////////////////////////////////////////////////////////
3241 } else if (com=="setFrameType")
3243 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3245 parser.setError (Aborted,"Type of selection does not allow setting frame type");
3247 else if (parser.checkParCount(1))
3249 s=parser.parString(ok,0);
3250 if (ok) setFrameType (s);
3252 /////////////////////////////////////////////////////////////////////
3253 } else if (com=="setFramePenColor")
3255 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3257 parser.setError (Aborted,"Type of selection does not allow setting of pen color");
3259 else if (parser.checkParCount(1))
3261 QColor c=parser.parColor(ok,0);
3262 if (ok) setFramePenColor (c);
3264 /////////////////////////////////////////////////////////////////////
3265 } else if (com=="setFrameBrushColor")
3267 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3269 parser.setError (Aborted,"Type of selection does not allow setting brush color");
3271 else if (parser.checkParCount(1))
3273 QColor c=parser.parColor(ok,0);
3274 if (ok) setFrameBrushColor (c);
3276 /////////////////////////////////////////////////////////////////////
3277 } else if (com=="setFramePadding")
3279 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3281 parser.setError (Aborted,"Type of selection does not allow setting frame padding");
3283 else if (parser.checkParCount(1))
3285 n=parser.parInt(ok,0);
3286 if (ok) setFramePadding(n);
3288 /////////////////////////////////////////////////////////////////////
3289 } else if (com=="setFrameBorderWidth")
3291 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3293 parser.setError (Aborted,"Type of selection does not allow setting frame border width");
3295 else if (parser.checkParCount(1))
3297 n=parser.parInt(ok,0);
3298 if (ok) setFrameBorderWidth (n);
3300 /////////////////////////////////////////////////////////////////////
3301 } else if (com=="setMapAuthor")
3303 if (parser.checkParCount(1))
3305 s=parser.parString(ok,0);
3306 if (ok) setAuthor (s);
3308 /////////////////////////////////////////////////////////////////////
3309 } else if (com=="setMapComment")
3311 if (parser.checkParCount(1))
3313 s=parser.parString(ok,0);
3314 if (ok) setComment(s);
3316 /////////////////////////////////////////////////////////////////////
3317 } else if (com=="setMapBackgroundColor")
3319 if (selection.isEmpty() )
3321 parser.setError (Aborted,"Nothing selected");
3322 } else if (! getSelectedBranch() )
3324 parser.setError (Aborted,"Type of selection is not a branch");
3325 } else if (parser.checkParCount(1))
3327 QColor c=parser.parColor (ok,0);
3328 if (ok) setMapBackgroundColor (c);
3330 /////////////////////////////////////////////////////////////////////
3331 } else if (com=="setMapDefLinkColor")
3333 if (selection.isEmpty() )
3335 parser.setError (Aborted,"Nothing selected");
3338 parser.setError (Aborted,"Type of selection is not a branch");
3339 } else if (parser.checkParCount(1))
3341 QColor c=parser.parColor (ok,0);
3342 if (ok) setMapDefLinkColor (c);
3344 /////////////////////////////////////////////////////////////////////
3345 } else if (com=="setMapLinkStyle")
3347 if (parser.checkParCount(1))
3349 s=parser.parString (ok,0);
3350 if (ok) setMapLinkStyle(s);
3352 /////////////////////////////////////////////////////////////////////
3353 } else if (com=="setHeading")
3355 if (selection.isEmpty() )
3357 parser.setError (Aborted,"Nothing selected");
3360 parser.setError (Aborted,"Type of selection is not a branch");
3361 } else if (parser.checkParCount(1))
3363 s=parser.parString (ok,0);
3367 /////////////////////////////////////////////////////////////////////
3368 } else if (com=="setHideExport")
3370 if (selection.isEmpty() )
3372 parser.setError (Aborted,"Nothing selected");
3373 } else if (selectionType()!=TreeItem::Branch && selectionType() != TreeItem::MapCenter &&selectionType()!=TreeItem::Image)
3375 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3376 } else if (parser.checkParCount(1))
3378 b=parser.parBool(ok,0);
3379 if (ok) setHideExport (b);
3381 /////////////////////////////////////////////////////////////////////
3382 } else if (com=="setIncludeImagesHorizontally")
3384 if (selection.isEmpty() )
3386 parser.setError (Aborted,"Nothing selected");
3389 parser.setError (Aborted,"Type of selection is not a branch");
3390 } else if (parser.checkParCount(1))
3392 b=parser.parBool(ok,0);
3393 if (ok) setIncludeImagesHor(b);
3395 /////////////////////////////////////////////////////////////////////
3396 } else if (com=="setIncludeImagesVertically")
3398 if (selection.isEmpty() )
3400 parser.setError (Aborted,"Nothing selected");
3403 parser.setError (Aborted,"Type of selection is not a branch");
3404 } else if (parser.checkParCount(1))
3406 b=parser.parBool(ok,0);
3407 if (ok) setIncludeImagesVer(b);
3409 /////////////////////////////////////////////////////////////////////
3410 } else if (com=="setHideLinkUnselected")
3412 if (selection.isEmpty() )
3414 parser.setError (Aborted,"Nothing selected");
3415 } else if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3417 parser.setError (Aborted,"Type of selection does not allow hiding the link");
3418 } else if (parser.checkParCount(1))
3420 b=parser.parBool(ok,0);
3421 if (ok) setHideLinkUnselected(b);
3423 /////////////////////////////////////////////////////////////////////
3424 } else if (com=="setSelectionColor")
3426 if (parser.checkParCount(1))
3428 QColor c=parser.parColor (ok,0);
3429 if (ok) setSelectionColorInt (c);
3431 /////////////////////////////////////////////////////////////////////
3432 } else if (com=="setURL")
3434 if (selection.isEmpty() )
3436 parser.setError (Aborted,"Nothing selected");
3439 parser.setError (Aborted,"Type of selection is not a branch");
3440 } else if (parser.checkParCount(1))
3442 s=parser.parString (ok,0);
3445 /////////////////////////////////////////////////////////////////////
3446 } else if (com=="setVymLink")
3448 if (selection.isEmpty() )
3450 parser.setError (Aborted,"Nothing selected");
3453 parser.setError (Aborted,"Type of selection is not a branch");
3454 } else if (parser.checkParCount(1))
3456 s=parser.parString (ok,0);
3457 if (ok) setVymLink(s);
3460 /////////////////////////////////////////////////////////////////////
3461 else if (com=="setFlag")
3463 if (selection.isEmpty() )
3465 parser.setError (Aborted,"Nothing selected");
3468 parser.setError (Aborted,"Type of selection is not a branch");
3469 } else if (parser.checkParCount(1))
3471 s=parser.parString(ok,0);
3474 selb->activateStandardFlag(s);
3475 selb->updateFlagsToolbar();
3478 /////////////////////////////////////////////////////////////////////
3479 } else if (com=="setFrameType")
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);
3493 /////////////////////////////////////////////////////////////////////
3494 } else if (com=="sortChildren")
3496 if (selection.isEmpty() )
3498 parser.setError (Aborted,"Nothing selected");
3501 parser.setError (Aborted,"Type of selection is not a branch");
3502 } else if (parser.checkParCount(0))
3506 /////////////////////////////////////////////////////////////////////
3507 } else if (com=="toggleFlag")
3509 if (selection.isEmpty() )
3511 parser.setError (Aborted,"Nothing selected");
3514 parser.setError (Aborted,"Type of selection is not a branch");
3515 } else if (parser.checkParCount(1))
3517 s=parser.parString(ok,0);
3520 selb->toggleStandardFlag(s);
3521 selb->updateFlagsToolbar();
3524 /////////////////////////////////////////////////////////////////////
3525 } else if (com=="unscroll")
3527 if (selection.isEmpty() )
3529 parser.setError (Aborted,"Nothing selected");
3532 parser.setError (Aborted,"Type of selection is not a branch");
3533 } else if (parser.checkParCount(0))
3535 if (!unscrollBranch (selb))
3536 parser.setError (Aborted,"Could not unscroll branch");
3538 /////////////////////////////////////////////////////////////////////
3539 } else if (com=="unscrollChildren")
3541 if (selection.isEmpty() )
3543 parser.setError (Aborted,"Nothing selected");
3546 parser.setError (Aborted,"Type of selection is not a branch");
3547 } else if (parser.checkParCount(0))
3549 unscrollChildren ();
3551 /////////////////////////////////////////////////////////////////////
3552 } else if (com=="unsetFlag")
3554 if (selection.isEmpty() )
3556 parser.setError (Aborted,"Nothing selected");
3559 parser.setError (Aborted,"Type of selection is not a branch");
3560 } else if (parser.checkParCount(1))
3562 s=parser.parString(ok,0);
3565 selb->deactivateStandardFlag(s);
3566 selb->updateFlagsToolbar();
3570 parser.setError (Aborted,"Unknown command");
3573 if (parser.errorLevel()==NoError)
3575 // setChanged(); FIXME should not be called e.g. for export?!
3580 // TODO Error handling
3581 qWarning("VymModel::parseAtom: Error!");
3582 qWarning(parser.errorMessage());
3586 void VymModel::runScript (QString script)
3588 parser.setScript (script);
3590 while (parser.next() )
3591 parseAtom(parser.getAtom());
3594 void VymModel::setExportMode (bool b)
3596 // should be called before and after exports
3597 // depending on the settings
3598 if (b && settings.value("/export/useHideExport","true")=="true")
3599 setHideTmpMode (HideExport);
3601 setHideTmpMode (HideNone);
3604 void VymModel::exportImage(QString fname, bool askName, QString format)
3608 fname=getMapName()+".png";
3615 QFileDialog *fd=new QFileDialog (NULL);
3616 fd->setCaption (tr("Export map as image"));
3617 fd->setDirectory (lastImageDir);
3618 fd->setFileMode(QFileDialog::AnyFile);
3619 fd->setFilters (imageIO.getFilters() );
3622 fl=fd->selectedFiles();
3624 format=imageIO.getType(fd->selectedFilter());
3628 setExportMode (true);
3629 QPixmap pix (getPixmap());
3630 pix.save(fname, format);
3631 setExportMode (false);
3635 void VymModel::exportXML(QString dir, bool askForName)
3639 dir=browseDirectory(NULL,tr("Export XML to directory"));
3640 if (dir =="" && !reallyWriteDirectory(dir) )
3644 // Hide stuff during export, if settings want this
3645 setExportMode (true);
3647 // Create subdirectories
3650 // write to directory
3651 QString saveFile=saveToDir (dir,mapName+"-",true,getTotalBBox().topLeft() ,NULL);
3654 file.setName ( dir + "/"+mapName+".xml");
3655 if ( !file.open( QIODevice::WriteOnly ) )
3657 // This should neverever happen
3658 QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
3662 // Write it finally, and write in UTF8, no matter what
3663 QTextStream ts( &file );
3664 ts.setEncoding (QTextStream::UnicodeUTF8);
3668 // Now write image, too
3669 exportImage (dir+"/images/"+mapName+".png",false,"PNG");
3671 setExportMode (false);
3674 void VymModel::exportASCII(QString fname,bool askName)
3679 ex.setFile (mapName+".txt");
3685 //ex.addFilter ("TXT (*.txt)");
3686 ex.setDir(lastImageDir);
3687 //ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
3692 setExportMode(true);
3694 setExportMode(false);
3698 void VymModel::exportXHTML (const QString &dir, bool askForName)
3700 ExportXHTMLDialog dia(NULL);
3701 dia.setFilePath (filePath );
3702 dia.setMapName (mapName );
3704 if (dir!="") dia.setDir (dir);
3710 if (dia.exec()!=QDialog::Accepted)
3714 QDir d (dia.getDir());
3715 // Check, if warnings should be used before overwriting
3716 // the output directory
3717 if (d.exists() && d.count()>0)
3720 warn.showCancelButton (true);
3721 warn.setText(QString(
3722 "The directory %1 is not empty.\n"
3723 "Do you risk to overwrite some of its contents?").arg(d.path() ));
3724 warn.setCaption("Warning: Directory not empty");
3725 warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
3727 if (warn.exec()!=QDialog::Accepted) ok=false;
3734 exportXML (dia.getDir(),false );
3735 dia.doExport(mapName );
3736 //if (dia.hasChanged()) setChanged();
3740 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
3745 if (ex.setConfigFile(cf))
3747 setExportMode (true);
3748 ex.exportPresentation();
3749 setExportMode (false);
3756 //////////////////////////////////////////////
3758 //////////////////////////////////////////////
3760 void VymModel::registerEditor(QWidget *me)
3762 mapEditor=(MapEditor*)me;
3763 for (int i=0; i<mapCenters.count(); i++)
3764 mapCenters.at(i)->setMapEditor(mapEditor);
3767 void VymModel::unregisterEditor(QWidget *)
3772 void VymModel::setContextPos(QPointF p)
3777 void VymModel::unsetContextPos()
3779 contextPos=QPointF();
3782 void VymModel::updateNoteFlag()
3785 BranchObj *bo=getSelectedBranch();
3788 bo->updateNoteFlag();
3789 mainWindow->updateActions();
3793 void VymModel::updateRelPositions()
3795 for (int i=0; i<mapCenters.count(); i++)
3796 mapCenters.at(i)->updateRelPositions();
3799 void VymModel::reposition()
3801 for (int i=0;i<mapCenters.count(); i++)
3802 mapCenters.at(i)->reposition(); // for positioning heading
3805 QPolygonF VymModel::shape(BranchObj *bo)
3807 // Creating (arbitrary) shapes
3810 QRectF rb=bo->getBBox();
3811 if (bo->getDepth()==0)
3813 // Just take BBox of this mapCenter
3814 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
3818 // Take union of BBox and TotalBBox
3820 QRectF ra=bo->getTotalBBox();
3821 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
3824 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
3827 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
3829 p <<ra.bottomRight()
3831 <<QPointF (rb.topRight().x(), ra.topRight().y() )
3834 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
3839 void VymModel::moveAway(LinkableMapObj *lmo)
3843 // Move all branches and MapCenters away from lmo
3844 // to avoid collisions
3849 BranchObj *boA=(BranchObj*)lmo;
3851 for (int i=0; i<mapCenters.count(); i++)
3853 boB=mapCenters.at(i);
3856 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
3859 <<" ("<<qPrintable(boA->getHeading() )<<")"
3860 <<" with ("<< qPrintable (boB->getHeading() )
3863 <<" minT="<<r.minTranslation<<endl<<endl;
3867 QPixmap VymModel::getPixmap()
3869 QRectF mapRect=getTotalBBox();
3870 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
3873 pp.setRenderHints(mapEditor->renderHints());
3875 // Don't print the visualisation of selection
3876 selection.unselect();
3878 mapScene->render ( &pp,
3879 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
3880 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
3882 // Restore selection
3883 selection.reselect();
3889 void VymModel::setMapLinkStyle (const QString & s)
3894 case LinkableMapObj::Line :
3897 case LinkableMapObj::Parabel:
3898 snow="StyleParabel";
3900 case LinkableMapObj::PolyLine:
3901 snow="StylePolyLine";
3903 case LinkableMapObj::PolyParabel:
3904 snow="StylePolyParabel";
3907 snow="UndefinedStyle";
3912 QString("setMapLinkStyle (\"%1\")").arg(s),
3913 QString("setMapLinkStyle (\"%1\")").arg(snow),
3914 QString("Set map link style (\"%1\")").arg(s)
3918 linkstyle=LinkableMapObj::Line;
3919 else if (s=="StyleParabel")
3920 linkstyle=LinkableMapObj::Parabel;
3921 else if (s=="StylePolyLine")
3922 linkstyle=LinkableMapObj::PolyLine;
3923 else if (s=="StylePolyParabel")
3924 linkstyle=LinkableMapObj::PolyParabel;
3926 linkstyle=LinkableMapObj::UndefinedStyle;
3933 bo->setLinkStyle(bo->getDefLinkStyle());
3939 LinkableMapObj::Style VymModel::getMapLinkStyle ()
3944 void VymModel::setMapDefLinkColor(QColor col)
3946 if ( !col.isValid() ) return;
3948 QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
3949 QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
3950 QString("Set map link color to %1").arg(col.name())
3964 void VymModel::setMapLinkColorHintInt()
3966 // called from setMapLinkColorHint(lch) or at end of parse
3976 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
3979 setMapLinkColorHintInt();
3982 void VymModel::toggleMapLinkColorHint()
3984 if (linkcolorhint==LinkableMapObj::HeadingColor)
3985 linkcolorhint=LinkableMapObj::DefaultColor;
3987 linkcolorhint=LinkableMapObj::HeadingColor;
3997 void VymModel::selectMapBackgroundImage () // FIXME move to ME
3999 Q3FileDialog *fd=new Q3FileDialog( NULL);
4000 fd->setMode (Q3FileDialog::ExistingFile);
4001 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
4002 ImagePreview *p =new ImagePreview (fd);
4003 fd->setContentsPreviewEnabled( TRUE );
4004 fd->setContentsPreview( p, p );
4005 fd->setPreviewMode( Q3FileDialog::Contents );
4006 fd->setCaption(vymName+" - " +tr("Load background image"));
4007 fd->setDir (lastImageDir);
4010 if ( fd->exec() == QDialog::Accepted )
4012 // TODO selectMapBackgroundImg in QT4 use: lastImageDir=fd->directory();
4013 lastImageDir=QDir (fd->dirPath());
4014 setMapBackgroundImage (fd->selectedFile());
4018 void VymModel::setMapBackgroundImage (const QString &fn) //FIXME missing savestate
4020 QColor oldcol=mapScene->backgroundBrush().color();
4024 QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
4026 QString ("setMapBackgroundImage (%1)").arg(col.name()),
4027 QString("Set background color of map to %1").arg(col.name()));
4030 brush.setTextureImage (QPixmap (fn));
4031 mapScene->setBackgroundBrush(brush);
4034 void VymModel::selectMapBackgroundColor() // FIXME move to ME
4036 QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
4037 if ( !col.isValid() ) return;
4038 setMapBackgroundColor( col );
4042 void VymModel::setMapBackgroundColor(QColor col) // FIXME move to ME
4044 QColor oldcol=mapScene->backgroundBrush().color();
4046 QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
4047 QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
4048 QString("Set background color of map to %1").arg(col.name()));
4049 mapScene->setBackgroundBrush(col);
4052 QColor VymModel::getMapBackgroundColor() // FIXME move to ME
4054 return mapScene->backgroundBrush().color();
4058 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint() // FIXME move to ME
4060 return linkcolorhint;
4063 QColor VymModel::getMapDefLinkColor() // FIXME move to ME
4065 return defLinkColor;
4068 void VymModel::setMapDefXLinkColor(QColor col) // FIXME move to ME
4073 QColor VymModel::getMapDefXLinkColor() // FIXME move to ME
4075 return defXLinkColor;
4078 void VymModel::setMapDefXLinkWidth (int w) // FIXME move to ME
4083 int VymModel::getMapDefXLinkWidth() // FIXME move to ME
4085 return defXLinkWidth;
4088 void VymModel::move(const double &x, const double &y)
4090 BranchObj *bo = getSelectedBranch();
4092 (selectionType()==TreeItem::Branch ||
4093 selectionType()==TreeItem::MapCenter ||
4094 selectionType()==TreeItem::Image
4097 QPointF ap(bo->getAbsPos());
4101 QString ps=qpointfToString(ap);
4102 QString s=getSelectString();
4105 s, "move "+qpointfToString(to),
4106 QString("Move %1 to %2").arg(getObjectName(bo)).arg(ps));
4114 void VymModel::moveRel (const double &x, const double &y)
4116 BranchObj *bo = getSelectedBranch();
4118 (selectionType()==TreeItem::Branch ||
4119 selectionType()==TreeItem::MapCenter ||
4120 selectionType()==TreeItem::Image
4124 QPointF rp(bo->getRelPos());
4128 QString ps=qpointfToString (bo->getRelPos());
4129 QString s=getSelectString(bo);
4132 s, "moveRel "+qpointfToString(to),
4133 QString("Move %1 to relative position %2").arg(getObjectName(bo)).arg(ps));
4134 ((OrnamentedObj*)bo)->move2RelPos (x,y);
4143 void VymModel::animate()
4145 animationTimer->stop();
4148 while (i<animObjList.size() )
4150 bo=(BranchObj*)animObjList.at(i);
4155 animObjList.removeAt(i);
4162 QItemSelection sel=selModel->selection();
4163 emit (selectionChanged(sel,sel));
4166 if (!animObjList.isEmpty()) animationTimer->start();
4170 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
4172 if (bo && bo->getDepth()>0)
4175 ap.setStart (start);
4177 ap.setTicks (animationTicks);
4178 ap.setAnimated (true);
4179 bo->setAnimation (ap);
4180 animObjList.append( bo );
4181 animationTimer->setSingleShot (true);
4182 animationTimer->start(animationInterval);
4186 void VymModel::stopAnimation (MapObj *mo)
4188 int i=animObjList.indexOf(mo);
4190 animObjList.removeAt (i);
4193 void VymModel::sendSelection()
4195 if (netstate!=Server) return;
4196 sendData (QString("select (\"%1\")").arg(selection.getSelectString()) );
4199 void VymModel::newServer()
4203 tcpServer = new QTcpServer(this);
4204 if (!tcpServer->listen(QHostAddress::Any,port)) {
4205 QMessageBox::critical(NULL, "vym server",
4206 QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
4207 //FIXME needed? we are no widget any longer... close();
4210 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
4212 cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
4215 void VymModel::connectToServer()
4218 server="salam.suse.de";
4220 clientSocket = new QTcpSocket (this);
4221 clientSocket->abort();
4222 clientSocket->connectToHost(server ,port);
4223 connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
4224 connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
4225 this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
4227 cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
4232 void VymModel::newClient()
4234 QTcpSocket *newClient = tcpServer->nextPendingConnection();
4235 connect(newClient, SIGNAL(disconnected()),
4236 newClient, SLOT(deleteLater()));
4238 cout <<"ME::newClient at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
4240 clientList.append (newClient);
4244 void VymModel::sendData(const QString &s)
4246 if (clientList.size()==0) return;
4248 // Create bytearray to send
4250 QDataStream out(&block, QIODevice::WriteOnly);
4251 out.setVersion(QDataStream::Qt_4_0);
4253 // Reserve some space for blocksize
4256 // Write sendCounter
4257 out << sendCounter++;
4262 // Go back and write blocksize so far
4263 out.device()->seek(0);
4264 quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
4268 cout << "ME::sendData bs="<<bs<<" counter="<<sendCounter<<" s="<<qPrintable(s)<<endl;
4270 for (int i=0; i<clientList.size(); ++i)
4272 //cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
4273 clientList.at(i)->write (block);
4277 void VymModel::readData ()
4279 while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
4282 cout <<"readData bytesAvail="<<clientSocket->bytesAvailable();
4286 QDataStream in(clientSocket);
4287 in.setVersion(QDataStream::Qt_4_0);
4295 cout << " t="<<qPrintable (t)<<endl;
4301 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
4303 switch (socketError) {
4304 case QAbstractSocket::RemoteHostClosedError:
4306 case QAbstractSocket::HostNotFoundError:
4307 QMessageBox::information(NULL, vymName +" Network client",
4308 "The host was not found. Please check the "
4309 "host name and port settings.");
4311 case QAbstractSocket::ConnectionRefusedError:
4312 QMessageBox::information(NULL, vymName + " Network client",
4313 "The connection was refused by the peer. "
4314 "Make sure the fortune server is running, "
4315 "and check that the host name and port "
4316 "settings are correct.");
4319 QMessageBox::information(NULL, vymName + " Network client",
4320 QString("The following error occurred: %1.")
4321 .arg(clientSocket->errorString()));
4328 void VymModel::selectMapSelectionColor()
4330 QColor col = QColorDialog::getColor( defLinkColor, NULL);
4331 setSelectionColor (col);
4334 void VymModel::setSelectionColorInt (QColor col)
4336 if ( !col.isValid() ) return;
4338 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4339 QString("setSelectionColor (%1)").arg(col.name()),
4340 QString("Set color of selection box to %1").arg(col.name())
4343 mapEditor->setSelectionColor (col);
4346 void VymModel::updateSelection()
4348 QItemSelection newsel=selModel->selection();
4349 updateSelection (newsel);
4352 void VymModel::updateSelection(const QItemSelection &)
4354 QItemSelection newsel=selModel->selection();
4356 cout << "VM::updateSelection new=";
4357 if (!newsel.indexes().isEmpty() )
4358 cout << newsel.indexes().first().row()<<"," << newsel.indexes().first().column();
4360 if (!oldsel.indexes().isEmpty() )
4361 cout << oldsel.indexes().first().row()<<"," << oldsel.indexes().first().column();
4364 //emit (selectionChanged(newsel,oldsel)); // FIXME needed?
4365 ensureSelectionVisible();
4369 void VymModel::setSelectionColor(QColor col)
4371 if ( !col.isValid() ) return;
4373 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4374 QString("setSelectionColor (%1)").arg(col.name()),
4375 QString("Set color of selection box to %1").arg(col.name())
4377 setSelectionColorInt (col);
4380 QColor VymModel::getSelectionColor()
4382 return mapEditor->getSelectionColor();
4385 void VymModel::setHideTmpMode (HideTmpMode mode)
4388 for (int i=0;i<mapCenters.count(); i++)
4389 mapCenters.at(i)->setHideTmp (mode);
4391 // FIXME needed? scene()->update();
4395 QRectF VymModel::getTotalBBox()
4398 for (int i=0;i<mapCenters.count(); i++)
4399 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
4403 //////////////////////////////////////////////
4404 // Selection related
4405 //////////////////////////////////////////////
4407 void VymModel::setSelectionModel (QItemSelectionModel *sm)
4410 cout << "VM::setSelModel selModel="<<selModel<<endl;
4413 QItemSelectionModel* VymModel::getSelectionModel()
4418 void VymModel::setSelectionBlocked (bool b)
4423 selection.unblock();
4426 bool VymModel::isSelectionBlocked()
4428 return selection.isBlocked();
4431 bool VymModel::select ()
4433 QModelIndex index=selModel->selectedIndexes().first(); // TODO no multiselections yet
4435 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
4436 return select (item->getLMO() );
4439 bool VymModel::select (const QString &s)
4441 LinkableMapObj *lmo=findObjBySelect(s);
4443 // Finally select the found object
4453 bool VymModel::select (LinkableMapObj *lmo)
4455 QItemSelection oldsel=selModel->selection();
4458 return select (lmo->getTreeItem() );
4463 bool VymModel::select (TreeItem *ti)
4467 QModelIndex ix=index(ti);
4468 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4469 ti->setLastSelectedBranch();
4470 //updateSelection(oldsel); //FIXME needed?
4476 void VymModel::unselect()
4478 selModel->clearSelection();
4481 void VymModel::reselect()
4483 selection.reselect();
4486 void VymModel::ensureSelectionVisible()
4488 LinkableMapObj *lmo=getSelectedLMO();
4489 if (lmo &&mapEditor) mapEditor->ensureVisible (lmo->getBBox() );
4493 void VymModel::selectInt (LinkableMapObj *lmo)
4495 if (selection.select(lmo))
4497 //selection.update();
4498 sendSelection (); // FIXME VM use signal
4503 void VymModel::selectNextBranchInt()
4505 // Increase number of branch
4506 LinkableMapObj *sel=getSelectedBranch();
4509 QString s=getSelectString();
4515 part=s.section(",",-1);
4517 num=part.right(part.length() - 3);
4519 s=s.left (s.length() -num.length());
4522 num=QString ("%1").arg(num.toUInt()+1);
4526 // Try to select this one
4527 if (select (s)) return;
4529 // We have no direct successor,
4530 // try to increase the parental number in order to
4531 // find a successor with same depth
4533 int d=getSelectedLMO()->getDepth();
4538 while (!found && d>0)
4540 s=s.section (",",0,d-1);
4541 // replace substring of current depth in s with "1"
4542 part=s.section(",",-1);
4544 num=part.right(part.length() - 3);
4548 // increase number of parent
4549 num=QString ("%1").arg(num.toUInt()+1);
4550 s=s.section (",",0,d-2) + ","+ typ+num;
4553 // Special case, look at orientation
4554 if (getSelectedLMO()->getOrientation()==LinkableMapObj::RightOfCenter)
4555 num=QString ("%1").arg(num.toUInt()+1);
4557 num=QString ("%1").arg(num.toUInt()-1);
4562 // pad to oldDepth, select the first branch for each depth
4563 for (i=d;i<oldDepth;i++)
4568 if ( getSelectedBranch()->countBranches()>0)
4576 // try to select the freshly built string
4584 void VymModel::selectPrevBranchInt()
4586 // Decrease number of branch
4587 if (selectionType()==TreeItem::Branch)
4589 QString s=getSelectString();
4595 part=s.section(",",-1);
4597 num=part.right(part.length() - 3);
4599 s=s.left (s.length() -num.length());
4601 int n=num.toInt()-1;
4604 num=QString ("%1").arg(n);
4607 cout <<"SP:: s0="<<s.toStdString()<<endl;
4610 // Try to select this one
4611 if (n>=0 && select (s)) return;
4613 // We have no direct precessor,
4614 // try to decrease the parental number in order to
4615 // find a precessor with same depth
4617 int d=getSelectedLMO()->getDepth();
4622 while (!found && d>0)
4624 s=s.section (",",0,d-1);
4625 // replace substring of current depth in s with "1"
4626 part=s.section(",",-1);
4628 num=part.right(part.length() - 3);
4632 // decrease number of parent
4633 num=QString ("%1").arg(num.toInt()-1);
4634 s=s.section (",",0,d-2) + ","+ typ+num;
4637 // Special case, look at orientation
4638 if (getSelectedLMO()->getOrientation()==LinkableMapObj::RightOfCenter)
4639 num=QString ("%1").arg(num.toInt()-1);
4641 num=QString ("%1").arg(num.toInt()+1);
4645 cout <<"SP:: si="<<s.toStdString()<<endl;
4647 // pad to oldDepth, select the last branch for each depth
4648 for (i=d;i<oldDepth;i++)
4652 if ( getSelectedBranch()->countBranches()>0)
4653 s+=",bo:"+ QString ("%1").arg( getSelectedBranch()->countBranches()-1 );
4660 // try to select the freshly built string
4668 void VymModel::selectUpperBranch()
4670 if (selection.isBlocked() ) return;
4672 BranchObj *bo=getSelectedBranch();
4673 if (bo && selectionType()==TreeItem::Branch)
4675 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4676 selectPrevBranchInt();
4678 if (bo->getDepth()==1)
4679 selectNextBranchInt();
4681 selectPrevBranchInt();
4685 void VymModel::selectLowerBranch()
4687 if (selection.isBlocked() ) return;
4689 BranchObj *bo=getSelectedBranch();
4690 if (bo && selectionType()==TreeItem::Branch)
4692 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4693 selectNextBranchInt();
4695 if (bo->getDepth()==1)
4696 selectPrevBranchInt();
4698 selectNextBranchInt();
4703 void VymModel::selectLeftBranch()
4705 if (selection.isBlocked() ) return;
4707 QItemSelection oldsel=selModel->selection();
4710 LinkableMapObj *sel=getSelectedBranch();
4713 if (selectionType()== TreeItem::MapCenter)
4715 QModelIndex ix=getSelectedIndex();
4716 selModel->select (index (rowCount(ix)-1,0,ix),QItemSelectionModel::ClearAndSelect );
4719 par=(BranchObj*)(sel->getParObj());
4720 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4723 if (selectionType() == TreeItem::Branch ||
4724 selectionType() == TreeItem::Image)
4726 QModelIndex ix=getSelectedIndex();
4728 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4733 if (selectionType() == TreeItem::Branch )
4735 selectLastSelectedBranch();
4740 updateSelection (oldsel);
4744 void VymModel::selectRightBranch()
4746 if (selection.isBlocked() ) return;
4748 QItemSelection oldsel=selModel->selection();
4751 LinkableMapObj *sel=getSelectedBranch();
4754 if (selectionType()== TreeItem::MapCenter)
4756 QModelIndex ix=getSelectedIndex();
4757 selModel->select (index (0,0,ix),QItemSelectionModel::ClearAndSelect );
4760 par=(BranchObj*)(sel->getParObj());
4761 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4764 if (selectionType() == TreeItem::Branch )
4766 selectLastSelectedBranch();
4772 if (selectionType() == TreeItem::Branch ||
4773 selectionType() == TreeItem::Image)
4775 QModelIndex ix=getSelectedIndex();
4777 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4781 updateSelection (oldsel);
4785 void VymModel::selectFirstBranch()
4787 TreeItem *ti=getSelectedBranchItem();
4790 TreeItem *par=ti->parent();
4792 TreeItem *ti2=par->getFirstBranch();
4796 ensureSelectionVisible();
4802 void VymModel::selectLastBranch()
4804 TreeItem *ti=getSelectedBranchItem();
4807 TreeItem *par=ti->parent();
4809 TreeItem *ti2=par->getLastBranch();
4813 ensureSelectionVisible();
4819 void VymModel::selectLastSelectedBranch()
4821 TreeItem *ti=getSelectedBranchItem();
4824 ti=ti->getLastSelectedBranch();
4825 if (ti) select (ti);
4829 void VymModel::selectParent()
4831 TreeItem *ti=getSelectedItem();
4839 ensureSelectionVisible();
4844 TreeItem::Type VymModel::selectionType()
4846 QModelIndexList list=selModel->selectedIndexes();
4847 if (list.isEmpty()) return TreeItem::Undefined;
4848 TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
4849 return ti->getType();
4853 LinkableMapObj* VymModel::getSelectedLMO()
4855 QModelIndexList list=selModel->selectedIndexes();
4856 if (!list.isEmpty() )
4858 TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
4859 TreeItem::Type type=ti->getType();
4860 if (type ==TreeItem::Branch || type==TreeItem::MapCenter || type==TreeItem::Image)
4862 return ti->getLMO();
4868 BranchObj* VymModel::getSelectedBranch()
4870 TreeItem *ti = getSelectedBranchItem();
4872 return (BranchObj*)ti->getLMO();
4877 TreeItem* VymModel::getSelectedBranchItem()
4879 QModelIndexList list=selModel->selectedIndexes();
4880 if (!list.isEmpty() )
4882 TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
4883 TreeItem::Type type=ti->getType();
4884 if (type ==TreeItem::Branch || type==TreeItem::MapCenter)
4890 TreeItem* VymModel::getSelectedItem()
4892 // FIXME this may not only be branch, but also float etc...
4893 BranchObj* bo=getSelectedBranch();
4894 if (bo) return bo->getTreeItem(); // FIXME VM get directly from treemodl
4898 QModelIndex VymModel::getSelectedIndex()
4900 QModelIndexList list=selModel->selectedIndexes();
4901 if (list.isEmpty() )
4902 return QModelIndex();
4904 return list.first();
4907 FloatImageObj* VymModel::getSelectedFloatImage()
4909 return selection.getFloatImage();
4912 QString VymModel::getSelectString ()
4914 LinkableMapObj *lmo=getSelectedLMO();
4916 return getSelectString(lmo);
4921 QString VymModel::getSelectString (LinkableMapObj *lmo) // FIXME VM needs to use TreeModel
4925 if (typeid(*lmo)==typeid(BranchObj) ||
4926 typeid(*lmo)==typeid(MapCenterObj) )
4928 LinkableMapObj *par=lmo->getParObj();
4931 if (lmo->getDepth() ==1)
4932 // Mainbranch, return
4933 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4935 // Branch, call myself recursively
4936 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4940 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
4941 if (i>=0) s=QString("mc:%1").arg(i);