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()));
133 animationUse=settings.readBoolEntry("/animation/use",false);
134 animationTicks=settings.readNumEntry("/animation/ticks",10);
135 animationInterval=settings.readNumEntry("/animation/interval",50);
137 animationTimer=new QTimer (this);
138 connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
141 defLinkColor=QColor (0,0,255);
142 defXLinkColor=QColor (180,180,180);
143 linkcolorhint=LinkableMapObj::DefaultColor;
144 linkstyle=LinkableMapObj::PolyParabel;
146 defXLinkColor=QColor (230,230,230);
156 // addMapCenter(); FIXME VM create this in MapEditor until BO and MCO are independent of scene
160 void VymModel::makeTmpDirectories()
162 // Create unique temporary directories
163 tmpMapDir = tmpVymDir+QString("/model-%1").arg(mapNum);
164 histPath = tmpMapDir+"/history";
170 MapEditor* VymModel::getMapEditor() // FIXME VM better return favourite editor here
175 bool VymModel::isRepositionBlocked()
177 return blockReposition;
180 void VymModel::updateActions() // FIXME maybe don't update if blockReposition is set
182 // Tell mainwindow to update states of actions
183 mainWindow->updateActions();
188 QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
190 // tmpdir temporary directory to which data will be written
191 // prefix mapname, which will be appended to images etc.
192 // writeflags Only write flags for "real" save of map, not undo
193 // offset offset of bbox of whole map in scene.
194 // Needed for XML export
203 case LinkableMapObj::Line:
206 case LinkableMapObj::Parabel:
209 case LinkableMapObj::PolyLine:
213 ls="StylePolyParabel";
217 QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
219 if (linkcolorhint==LinkableMapObj::HeadingColor)
220 colhint=xml.attribut("linkColorHint","HeadingColor");
222 QString mapAttr=xml.attribut("version",vymVersion);
224 mapAttr+= xml.attribut("author",author) +
225 xml.attribut("comment",comment) +
226 xml.attribut("date",getDate()) +
227 xml.attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
228 xml.attribut("selectionColor", mapEditor->getSelectionColor().name() ) +
229 xml.attribut("linkStyle", ls ) +
230 xml.attribut("linkColor", defLinkColor.name() ) +
231 xml.attribut("defXLinkColor", defXLinkColor.name() ) +
232 xml.attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
234 s+=xml.beginElement("vymmap",mapAttr);
237 // Find the used flags while traversing the tree
238 standardFlagsDefault->resetUsedCounter();
240 // Reset the counters before saving
241 // TODO constr. of FIO creates lots of objects, better do this in some other way...
242 FloatImageObj (mapScene).resetSaveCounter();
244 // Build xml recursivly
245 if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
246 // Save complete map, if saveSel not set
247 s+=saveToDir(tmpdir,prefix,writeflags,offset);
250 if ( typeid(*saveSel) == typeid(BranchObj) )
252 s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
253 else if ( typeid(*saveSel) == typeid(FloatImageObj) )
255 s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
258 // Save local settings
259 s+=settings.getDataXML (destPath);
262 if (!selection.isEmpty() && !saveSel )
263 s+=xml.valueElement("select",selection.getSelectString());
266 s+=xml.endElement("vymmap");
269 standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
273 void VymModel::setFilePath(QString fpath, QString destname)
275 if (fpath.isEmpty() || fpath=="")
282 filePath=fpath; // becomes absolute path
283 fileName=fpath; // gets stripped of path
284 destPath=destname; // needed for vymlinks and during load to reset fileChangedTime
286 // If fpath is not an absolute path, complete it
287 filePath=QDir(fpath).absPath();
288 fileDir=filePath.left (1+filePath.findRev ("/"));
290 // Set short name, too. Search from behind:
291 int i=fileName.findRev("/");
292 if (i>=0) fileName=fileName.remove (0,i+1);
294 // Forget the .vym (or .xml) for name of map
295 mapName=fileName.left(fileName.findRev(".",-1,true) );
299 void VymModel::setFilePath(QString fpath)
301 setFilePath (fpath,fpath);
304 QString VymModel::getFilePath()
309 QString VymModel::getFileName()
314 QString VymModel::getMapName()
319 QString VymModel::getDestPath()
324 ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
326 ErrorCode err=success;
328 parseBaseHandler *handler;
332 case VymMap: handler=new parseVYMHandler; break;
333 case FreemindMap : handler=new parseFreemindHandler; break;
335 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
336 "Unknown FileType in VymModel::load()");
340 bool zipped_org=zipped;
344 selModel->clearSelection();
345 // FIXME VM not needed??? model->setMapEditor(this);
346 // (map state is set later at end of load...)
349 BranchObj *bo=getSelectedBranch();
350 if (!bo) return aborted;
351 if (lmode==ImportAdd)
352 saveStateChangingPart(
355 QString("addMapInsert (%1)").arg(fname),
356 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
358 saveStateChangingPart(
361 QString("addMapReplace(%1)").arg(fname),
362 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
366 // Create temporary directory for packing
368 QString tmpZipDir=makeTmpDir (ok,"vym-pack");
371 QMessageBox::critical( 0, tr( "Critical Load Error" ),
372 tr("Couldn't create temporary directory before load\n"));
377 err=unzipDir (tmpZipDir,fname);
387 // Look for mapname.xml
388 xmlfile= fname.left(fname.findRev(".",-1,true));
389 xmlfile=xmlfile.section( '/', -1 );
390 QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
391 if (!mfile.exists() )
393 // mapname.xml does not exist, well,
394 // maybe someone renamed the mapname.vym file...
395 // Try to find any .xml in the toplevel
396 // directory of the .vym file
397 QStringList flist=QDir (tmpZipDir).entryList("*.xml");
398 if (flist.count()==1)
400 // Only one entry, take this one
401 xmlfile=tmpZipDir + "/"+flist.first();
404 for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it )
405 *it=tmpZipDir + "/" + *it;
406 // TODO Multiple entries, load all (but only the first one into this ME)
407 //mainWindow->fileLoadFromTmp (flist);
408 //returnCode=1; // Silently forget this attempt to load
409 qWarning ("MainWindow::load (fn) multimap found...");
412 if (flist.isEmpty() )
414 QMessageBox::critical( 0, tr( "Critical Load Error" ),
415 tr("Couldn't find a map (*.xml) in .vym archive.\n"));
418 } //file doesn't exist
420 xmlfile=mfile.name();
423 QFile file( xmlfile);
425 // I am paranoid: file should exist anyway
426 // according to check in mainwindow.
429 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
430 tr(QString("Couldn't open map %1").arg(file.name())));
434 bool blockSaveStateOrg=blockSaveState;
435 blockReposition=true;
437 QXmlInputSource source( file);
438 QXmlSimpleReader reader;
439 reader.setContentHandler( handler );
440 reader.setErrorHandler( handler );
441 handler->setModel ( this);
444 // We need to set the tmpDir in order to load files with rel. path
449 tmpdir=fname.left(fname.findRev("/",-1));
450 handler->setTmpDir (tmpdir);
451 handler->setInputFile (file.name());
452 handler->setLoadMode (lmode);
453 bool ok = reader.parse( source );
454 blockReposition=false;
455 blockSaveState=blockSaveStateOrg;
459 reposition(); // FIXME VM reposition the view instead...
466 autosaveTimer->stop();
469 // Reset timestamp to check for later updates of file
470 fileChangedTime=QFileInfo (destPath).lastModified();
473 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
474 tr( handler->errorProtocol() ) );
476 // Still return "success": the map maybe at least
477 // partially read by the parser
482 removeDir (QDir(tmpZipDir));
484 // Restore original zip state
491 ErrorCode VymModel::save (const SaveMode &savemode)
495 QString safeFilePath;
497 ErrorCode err=success;
501 mapFileName=mapName+".xml";
503 // use name given by user, even if he chooses .doc
504 mapFileName=fileName;
506 // Look, if we should zip the data:
509 QMessageBox mb( vymName,
510 tr("The map %1\ndid not use the compressed "
511 "vym file format.\nWriting it uncompressed will also write images \n"
512 "and flags and thus may overwrite files in the "
513 "given directory\n\nDo you want to write the map").arg(filePath),
514 QMessageBox::Warning,
515 QMessageBox::Yes | QMessageBox::Default,
517 QMessageBox::Cancel | QMessageBox::Escape);
518 mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
519 mb.setButtonText( QMessageBox::No, tr("uncompressed") );
520 mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
523 case QMessageBox::Yes:
524 // save compressed (default file format)
527 case QMessageBox::No:
531 case QMessageBox::Cancel:
538 // First backup existing file, we
539 // don't want to add to old zip archives
543 if ( settings.value ("/mapeditor/writeBackupFile").toBool())
545 QString backupFileName(destPath + "~");
546 QFile backupFile(backupFileName);
547 if (backupFile.exists() && !backupFile.remove())
549 QMessageBox::warning(0, tr("Save Error"),
550 tr("%1\ncould not be removed before saving").arg(backupFileName));
552 else if (!f.rename(backupFileName))
554 QMessageBox::warning(0, tr("Save Error"),
555 tr("%1\ncould not be renamed before saving").arg(destPath));
562 // Create temporary directory for packing
564 tmpZipDir=makeTmpDir (ok,"vym-zip");
567 QMessageBox::critical( 0, tr( "Critical Load Error" ),
568 tr("Couldn't create temporary directory before save\n"));
572 safeFilePath=filePath;
573 setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
576 // Create mapName and fileDir
577 makeSubDirs (fileDir);
580 if (savemode==CompleteMap || selection.isEmpty())
583 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
586 autosaveTimer->stop();
591 if (selectionType()==TreeItem::Image)
594 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),getSelectedBranch());
595 // TODO take care of multiselections
598 if (!saveStringToDisk(fileDir+mapFileName,saveFile))
601 qWarning ("ME::saveStringToDisk failed!");
607 if (err==success) err=zipDir (tmpZipDir,destPath);
610 removeDir (QDir(tmpZipDir));
612 // Restore original filepath outside of tmp zip dir
613 setFilePath (safeFilePath);
617 fileChangedTime=QFileInfo (destPath).lastModified();
621 void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)
623 QString pathDir=path.left(path.findRev("/"));
629 // We need to parse saved XML data
630 parseVYMHandler handler;
631 QXmlInputSource source( file);
632 QXmlSimpleReader reader;
633 reader.setContentHandler( &handler );
634 reader.setErrorHandler( &handler );
635 handler.setModel ( this);
636 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
637 if (undoSel.isEmpty())
641 handler.setLoadMode (NewMap);
645 handler.setLoadMode (ImportReplace);
647 blockReposition=true;
648 bool ok = reader.parse( source );
649 blockReposition=false;
652 // This should never ever happen
653 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
654 handler.errorProtocol());
657 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
660 void VymModel::addMapInsertInt (const QString &path, int pos)
662 BranchObj *sel=getSelectedBranch();
665 QString pathDir=path.left(path.findRev("/"));
671 // We need to parse saved XML data
672 parseVYMHandler handler;
673 QXmlInputSource source( file);
674 QXmlSimpleReader reader;
675 reader.setContentHandler( &handler );
676 reader.setErrorHandler( &handler );
677 handler.setModel (this);
678 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
679 handler.setLoadMode (ImportAdd);
680 blockReposition=true;
681 bool ok = reader.parse( source );
682 blockReposition=false;
685 // This should never ever happen
686 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
687 handler.errorProtocol());
689 if (sel->getDepth()>0)
690 sel->getLastBranch()->linkTo (sel,pos);
692 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
696 FloatImageObj* VymModel::loadFloatImageInt (QString fn)
698 TreeItem *fi=createImage();
701 FloatImageObj *fio= ((FloatImageObj*)fi->getLMO());
709 void VymModel::loadFloatImage ()
711 BranchObj *bo=getSelectedBranch();
715 Q3FileDialog *fd=new Q3FileDialog( NULL);
716 fd->setMode (Q3FileDialog::ExistingFiles);
717 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
718 ImagePreview *p =new ImagePreview (fd);
719 fd->setContentsPreviewEnabled( TRUE );
720 fd->setContentsPreview( p, p );
721 fd->setPreviewMode( Q3FileDialog::Contents );
722 fd->setCaption(vymName+" - " +tr("Load image"));
723 fd->setDir (lastImageDir);
726 if ( fd->exec() == QDialog::Accepted )
728 // TODO loadFIO in QT4 use: lastImageDir=fd->directory();
729 lastImageDir=QDir (fd->dirPath());
732 for (int j=0; j<fd->selectedFiles().count(); j++)
734 s=fd->selectedFiles().at(j);
735 fio=loadFloatImageInt (s);
738 (LinkableMapObj*)fio,
741 QString ("loadImage (%1)").arg(s ),
742 QString("Add image %1 to %2").arg(s).arg(getObjectName(bo))
745 // TODO loadFIO error handling
746 qWarning ("Failed to load "+s);
754 void VymModel::saveFloatImageInt (FloatImageObj *fio, const QString &type, const QString &fn)
759 void VymModel::saveFloatImage ()
761 FloatImageObj *fio=selection.getFloatImage();
764 QFileDialog *fd=new QFileDialog( NULL);
765 fd->setFilters (imageIO.getFilters());
766 fd->setCaption(vymName+" - " +tr("Save image"));
767 fd->setFileMode( QFileDialog::AnyFile );
768 fd->setDirectory (lastImageDir);
769 // fd->setSelection (fio->getOriginalFilename());
773 if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
775 fn=fd->selectedFiles().at(0);
776 if (QFile (fn).exists() )
778 QMessageBox mb( vymName,
779 tr("The file %1 exists already.\n"
780 "Do you want to overwrite it?").arg(fn),
781 QMessageBox::Warning,
782 QMessageBox::Yes | QMessageBox::Default,
783 QMessageBox::Cancel | QMessageBox::Escape,
784 QMessageBox::NoButton );
786 mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
787 mb.setButtonText( QMessageBox::No, tr("Cancel"));
790 case QMessageBox::Yes:
793 case QMessageBox::Cancel:
800 saveFloatImageInt (fio,fd->selectedFilter(),fn );
807 void VymModel::importDirInt(BranchObj *dst, QDir d)
809 BranchObj *bo=getSelectedBranch();
812 // Traverse directories
813 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
814 QFileInfoList list = d.entryInfoList();
817 for (int i = 0; i < list.size(); ++i)
820 if (fi.fileName() != "." && fi.fileName() != ".." )
823 bo=dst->getLastBranch();
824 bo->setHeading (fi.fileName() );
825 bo->setColor (QColor("blue"));
827 if ( !d.cd(fi.fileName()) )
828 QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
831 // Recursively add subdirs
838 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
839 list = d.entryInfoList();
841 for (int i = 0; i < list.size(); ++i)
845 bo=dst->getLastBranch();
846 bo->setHeading (fi.fileName() );
847 bo->setColor (QColor("black"));
848 if (fi.fileName().right(4) == ".vym" )
849 bo->setVymLink (fi.filePath());
854 void VymModel::importDirInt (const QString &s)
856 BranchObj *bo=getSelectedBranch();
859 saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
866 void VymModel::importDir()
868 BranchObj *bo=getSelectedBranch();
872 filters <<"VYM map (*.vym)";
873 QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
874 fd->setMode (QFileDialog::DirectoryOnly);
875 fd->setFilters (filters);
876 fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
880 if ( fd->exec() == QDialog::Accepted )
882 importDirInt (fd->selectedFile() );
884 //FIXME VM needed? scene()->update();
890 void VymModel::autosave()
892 QDateTime now=QDateTime().currentDateTime();
894 // Disable autosave, while we have gone back in history
895 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
896 if (redosAvail>0) return;
898 // Also disable autosave for new map without filename
899 if (filePath.isEmpty()) return;
902 if (mapUnsaved &&mapChanged && settings.value ("/autosave/use",true).toBool() )
904 if (QFileInfo(filePath).lastModified()<=fileChangedTime)
905 mainWindow->fileSave (this);
908 cout <<" ME::autosave rejected, file on disk is newer than last save.\n";
913 void VymModel::fileChanged()
915 // Check if file on disk has changed meanwhile
916 if (!filePath.isEmpty())
918 QDateTime tmod=QFileInfo (filePath).lastModified();
919 if (tmod>fileChangedTime)
921 // FIXME VM switch to current mapeditor and finish lineedits...
922 QMessageBox mb( vymName,
923 tr("The file of the map on disk has changed:\n\n"
924 " %1\n\nDo you want to reload that map with the new file?").arg(filePath),
925 QMessageBox::Question,
927 QMessageBox::Cancel | QMessageBox::Default,
928 QMessageBox::NoButton );
930 mb.setButtonText( QMessageBox::Yes, tr("Reload"));
931 mb.setButtonText( QMessageBox::No, tr("Ignore"));
934 case QMessageBox::Yes:
936 load (filePath,NewMap,fileType);
937 case QMessageBox::Cancel:
938 fileChangedTime=tmod; // allow autosave to overwrite newer file!
944 bool VymModel::isDefault()
949 void VymModel::makeDefault()
955 bool VymModel::hasChanged()
960 void VymModel::setChanged()
963 autosaveTimer->start(settings.value("/autosave/ms/",300000).toInt());
971 QString VymModel::getObjectName (const LinkableMapObj *lmo)
974 if (!lmo) return QString("Error: NULL has no name!");
976 if ((typeid(*lmo) == typeid(BranchObj) ||
977 typeid(*lmo) == typeid(MapCenterObj)))
980 s=(((BranchObj*)lmo)->getHeading());
981 if (s=="") s="unnamed";
982 return QString("branch (%1)").arg(s);
984 if ((typeid(*lmo) == typeid(FloatImageObj) ))
985 return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
986 return QString("Unknown type has no name!");
989 void VymModel::redo()
991 // Can we undo at all?
992 if (redosAvail<1) return;
994 bool blockSaveStateOrg=blockSaveState;
999 if (undosAvail<stepsTotal) undosAvail++;
1001 if (curStep>stepsTotal) curStep=1;
1002 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1003 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1004 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1005 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1006 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1007 QString version=undoSet.readEntry ("/history/version");
1009 /* TODO Maybe check for version, if we save the history
1010 if (!checkVersion(version))
1011 QMessageBox::warning(0,tr("Warning"),
1012 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1015 // Find out current undo directory
1016 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1020 cout << "VymModel::redo() begin\n";
1021 cout << " undosAvail="<<undosAvail<<endl;
1022 cout << " redosAvail="<<redosAvail<<endl;
1023 cout << " curStep="<<curStep<<endl;
1024 cout << " ---------------------------"<<endl;
1025 cout << " comment="<<comment.toStdString()<<endl;
1026 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1027 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1028 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1029 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1030 cout << " ---------------------------"<<endl<<endl;
1033 // select object before redo
1034 if (!redoSelection.isEmpty())
1035 select (redoSelection);
1038 parseAtom (redoCommand);
1041 blockSaveState=blockSaveStateOrg;
1043 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1044 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1045 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1046 undoSet.writeSettings(histPath);
1048 mainWindow->updateHistory (undoSet);
1051 /* TODO remove testing
1052 cout << "ME::redo() end\n";
1053 cout << " undosAvail="<<undosAvail<<endl;
1054 cout << " redosAvail="<<redosAvail<<endl;
1055 cout << " curStep="<<curStep<<endl;
1056 cout << " ---------------------------"<<endl<<endl;
1062 bool VymModel::isRedoAvailable()
1064 if (undoSet.readNumEntry("/history/redosAvail",0)>0)
1070 void VymModel::undo()
1072 // Can we undo at all?
1073 if (undosAvail<1) return;
1075 mainWindow->statusMessage (tr("Autosave disabled during undo."));
1077 bool blockSaveStateOrg=blockSaveState;
1078 blockSaveState=true;
1080 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1081 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1082 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1083 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1084 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1085 QString version=undoSet.readEntry ("/history/version");
1087 /* TODO Maybe check for version, if we save the history
1088 if (!checkVersion(version))
1089 QMessageBox::warning(0,tr("Warning"),
1090 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1093 // Find out current undo directory
1094 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1096 // select object before undo
1097 if (!undoSelection.isEmpty())
1098 select (undoSelection);
1102 cout << "VymModel::undo() begin\n";
1103 cout << " undosAvail="<<undosAvail<<endl;
1104 cout << " redosAvail="<<redosAvail<<endl;
1105 cout << " curStep="<<curStep<<endl;
1106 cout << " ---------------------------"<<endl;
1107 cout << " comment="<<comment.toStdString()<<endl;
1108 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1109 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1110 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1111 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1112 cout << " ---------------------------"<<endl<<endl;
1114 parseAtom (undoCommand);
1119 if (curStep<1) curStep=stepsTotal;
1123 blockSaveState=blockSaveStateOrg;
1124 /* TODO remove testing
1125 cout << "VymModel::undo() end\n";
1126 cout << " undosAvail="<<undosAvail<<endl;
1127 cout << " redosAvail="<<redosAvail<<endl;
1128 cout << " curStep="<<curStep<<endl;
1129 cout << " ---------------------------"<<endl<<endl;
1132 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1133 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1134 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1135 undoSet.writeSettings(histPath);
1137 mainWindow->updateHistory (undoSet);
1140 ensureSelectionVisible();
1143 bool VymModel::isUndoAvailable()
1145 if (undoSet.readNumEntry("/history/undosAvail",0)>0)
1151 void VymModel::gotoHistoryStep (int i)
1153 // Restore variables
1154 int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
1155 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
1157 if (i<0) i=undosAvail+redosAvail;
1159 // Clicking above current step makes us undo things
1162 for (int j=0; j<undosAvail-i; j++) undo();
1165 // Clicking below current step makes us redo things
1167 for (int j=undosAvail; j<i; j++)
1169 if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
1173 // And ignore clicking the current row ;-)
1177 QString VymModel::getHistoryPath()
1179 QString histName(QString("history-%1").arg(curStep));
1180 return (tmpMapDir+"/"+histName);
1183 void VymModel::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
1185 sendData(redoCom); //FIXME testing
1190 if (blockSaveState) return;
1192 if (debug) cout << "ME::saveState() for "<<qPrintable (mapName)<<endl;
1194 // Find out current undo directory
1195 if (undosAvail<stepsTotal) undosAvail++;
1197 if (curStep>stepsTotal) curStep=1;
1199 QString backupXML="";
1200 QString histDir=getHistoryPath();
1201 QString bakMapPath=histDir+"/map.xml";
1203 // Create histDir if not available
1206 makeSubDirs (histDir);
1208 // Save depending on how much needs to be saved
1210 backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
1212 QString undoCommand="";
1213 if (savemode==UndoCommand)
1215 undoCommand=undoCom;
1217 else if (savemode==PartOfMap )
1219 undoCommand=undoCom;
1220 undoCommand.replace ("PATH",bakMapPath);
1223 if (!backupXML.isEmpty())
1224 // Write XML Data to disk
1225 saveStringToDisk (bakMapPath,backupXML);
1227 // We would have to save all actions in a tree, to keep track of
1228 // possible redos after a action. Possible, but we are too lazy: forget about redos.
1231 // Write the current state to disk
1232 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1233 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1234 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1235 undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
1236 undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
1237 undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
1238 undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
1239 undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
1240 undoSet.setEntry (QString("/history/version"),vymVersion);
1241 undoSet.writeSettings(histPath);
1245 // TODO remove after testing
1246 //cout << " into="<< histPath.toStdString()<<endl;
1247 cout << " stepsTotal="<<stepsTotal<<
1248 ", undosAvail="<<undosAvail<<
1249 ", redosAvail="<<redosAvail<<
1250 ", curStep="<<curStep<<endl;
1251 cout << " ---------------------------"<<endl;
1252 cout << " comment="<<comment.toStdString()<<endl;
1253 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1254 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1255 cout << " redoCom="<<redoCom.toStdString()<<endl;
1256 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1257 if (saveSel) cout << " saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
1258 cout << " ---------------------------"<<endl;
1261 mainWindow->updateHistory (undoSet);
1267 void VymModel::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
1269 // save the selected part of the map, Undo will replace part of map
1270 QString undoSelection="";
1272 undoSelection=getSelectString(undoSel);
1274 qWarning ("VymModel::saveStateChangingPart no undoSel given!");
1275 QString redoSelection="";
1277 redoSelection=getSelectString(undoSel);
1279 qWarning ("VymModel::saveStateChangingPart no redoSel given!");
1282 saveState (PartOfMap,
1283 undoSelection, "addMapReplace (\"PATH\")",
1289 void VymModel::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
1293 qWarning ("VymModel::saveStateRemovingPart no redoSel given!");
1296 QString undoSelection=getSelectString (redoSel->getParObj());
1297 QString redoSelection=getSelectString(redoSel);
1298 if (typeid(*redoSel) == typeid(BranchObj) )
1300 // save the selected branch of the map, Undo will insert part of map
1301 saveState (PartOfMap,
1302 undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
1303 redoSelection, "delete ()",
1310 void VymModel::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment)
1312 // "Normal" savestate: save commands, selections and comment
1313 // so just save commands for undo and redo
1314 // and use current selection
1316 QString redoSelection="";
1317 if (redoSel) redoSelection=getSelectString(redoSel);
1318 QString undoSelection="";
1319 if (undoSel) undoSelection=getSelectString(undoSel);
1321 saveState (UndoCommand,
1328 void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment)
1330 // "Normal" savestate: save commands, selections and comment
1331 // so just save commands for undo and redo
1332 // and use current selection
1333 saveState (UndoCommand,
1340 void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment)
1342 // "Normal" savestate applied to model (no selection needed):
1343 // save commands and comment
1344 saveState (UndoCommand,
1352 QGraphicsScene* VymModel::getScene ()
1357 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
1359 LinkableMapObj *lmo;
1361 for (int i=0;i<mapCenters.count(); i++)
1363 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
1364 if (lmo) return lmo;
1369 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
1371 LinkableMapObj *lmo;
1377 part=s.section(",",0,0);
1379 num=part.right(part.length() - 3);
1380 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
1381 return mapCenters.at(num.toInt() );
1384 for (int i=0; i<mapCenters.count(); i++)
1386 lmo=mapCenters.at(i)->findObjBySelect(s);
1387 if (lmo) return lmo;
1392 LinkableMapObj* VymModel::findID (const QString &s)
1394 LinkableMapObj *lmo;
1395 for (int i=0; i<mapCenters.count(); i++)
1397 lmo=mapCenters.at(i)->findID (s);
1398 if (lmo) return lmo;
1403 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
1407 for (int i=0; i<mapCenters.count(); i++)
1408 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
1412 //////////////////////////////////////////////
1414 //////////////////////////////////////////////
1415 void VymModel::setVersion (const QString &s)
1420 void VymModel::setAuthor (const QString &s)
1423 QString ("setMapAuthor (\"%1\")").arg(author),
1424 QString ("setMapAuthor (\"%1\")").arg(s),
1425 QString ("Set author of map to \"%1\"").arg(s)
1431 QString VymModel::getAuthor()
1436 void VymModel::setComment (const QString &s)
1439 QString ("setMapComment (\"%1\")").arg(comment),
1440 QString ("setMapComment (\"%1\")").arg(s),
1441 QString ("Set comment of map")
1447 QString VymModel::getComment ()
1452 QString VymModel::getDate ()
1454 return QDate::currentDate().toString ("yyyy-MM-dd");
1457 void VymModel::setHeading(const QString &s)
1459 BranchObj *sel=getSelectedBranch();
1464 "setHeading (\""+sel->getHeading()+"\")",
1466 "setHeading (\""+s+"\")",
1467 QString("Set heading of %1 to \"%2\"").arg(getObjectName(sel)).arg(s) );
1468 sel->setHeading(s );
1469 /* FIXME testing only
1471 TreeItem *ti=getSelectedItem();
1475 //FIXME VM ix is wrong ModelIndex below, ix2 is (hopefully) correct:
1476 //QModelIndex ix=index( ti->row(), ti->column(), index (0,0,QModelIndex()) );
1477 //FIXME VM testing only cout <<"VM::setHeading s="<<s.toStdString()<<" ti="<<ti<<" r,c=("<<ti->row()<<","<<ti->column()<<")"<<endl;
1478 QModelIndex ix2=index (ti);
1479 emit (dataChanged ( ix2,ix2));
1483 cout << "Warning: VM::setHeading ti==NULL\n";
1486 //cout <<" (r,c)=("<<ix2.row()<<","<<ix2.column()<<")"<<endl;
1490 ensureSelectionVisible();
1494 BranchObj* VymModel::findText (QString s, bool cs)
1497 QTextDocument::FindFlags flags=0;
1498 if (cs) flags=QTextDocument::FindCaseSensitively;
1501 { // Nothing found or new find process
1503 // nothing found, start again
1507 next (findCurrent,findPrevious,d);
1509 bool searching=true;
1510 bool foundNote=false;
1511 while (searching && !EOFind)
1515 // Searching in Note
1516 if (findCurrent->getNote().contains(s,cs))
1518 select (findCurrent);
1520 if (getSelectedBranch()!=itFind)
1523 ensureSelectionVisible();
1526 if (textEditor->findText(s,flags))
1532 // Searching in Heading
1533 if (searching && findCurrent->getHeading().contains (s,cs) )
1535 select(findCurrent);
1541 if (!next(findCurrent,findPrevious,d) )
1544 //cout <<"still searching... "<<qPrintable( itFind->getHeading())<<endl;
1547 return getSelectedBranch();
1552 void VymModel::findReset()
1553 { // Necessary if text to find changes during a find process
1561 void VymModel::setScene (QGraphicsScene *s)
1563 cout << "VM::setscene scene="<<s<<endl;
1564 mapScene=s; // FIXME VM should not be necessary anymore, move all occurences to MapEditor
1565 //init(); // Here we have a mapScene set,
1566 // which is (still) needed to create MapCenters
1569 void VymModel::setURL(const QString &url)
1571 BranchObj *bo=getSelectedBranch();
1574 QString oldurl=bo->getURL();
1578 QString ("setURL (\"%1\")").arg(oldurl),
1580 QString ("setURL (\"%1\")").arg(url),
1581 QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
1586 ensureSelectionVisible();
1590 QString VymModel::getURL()
1592 BranchObj *bo=getSelectedBranch();
1594 return bo->getURL();
1599 QStringList VymModel::getURLs()
1602 BranchObj *bo=getSelectedBranch();
1608 if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
1615 void VymModel::linkFloatImageTo(const QString &dstString)
1617 FloatImageObj *fio=selection.getFloatImage();
1620 BranchObj *dst=(BranchObj*)findObjBySelect(dstString);
1621 if (dst && (typeid(*dst)==typeid (BranchObj) ||
1622 typeid(*dst)==typeid (MapCenterObj)))
1624 LinkableMapObj *dstPar=dst->getParObj();
1625 QString parString=getSelectString(dstPar);
1626 QString fioPreSelectString=getSelectString(fio);
1627 QString fioPreParentSelectString=getSelectString (fio->getParObj());
1628 ((BranchObj*)dst)->addFloatImage (fio);
1629 selection.unselect();
1630 ((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
1631 fio=((BranchObj*)dst)->getLastFloatImage();
1634 selection.select(fio);
1636 getSelectString(fio),
1637 QString("linkTo (\"%1\")").arg(fioPreParentSelectString),
1639 QString ("linkTo (\"%1\")").arg(dstString),
1640 QString ("Link floatimage to %1").arg(getObjectName(dst)));
1646 void VymModel::setFrameType(const FrameObj::FrameType &t)
1648 BranchObj *bo=getSelectedBranch();
1651 QString s=bo->getFrameTypeName();
1652 bo->setFrameType (t);
1653 saveState (bo, QString("setFrameType (\"%1\")").arg(s),
1654 bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
1660 void VymModel::setFrameType(const QString &s)
1662 BranchObj *bo=getSelectedBranch();
1665 saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
1666 bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
1667 bo->setFrameType (s);
1673 void VymModel::setFramePenColor(const QColor &c)
1675 BranchObj *bo=getSelectedBranch();
1678 saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
1679 bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
1680 bo->setFramePenColor (c);
1684 void VymModel::setFrameBrushColor(const QColor &c)
1686 BranchObj *bo=getSelectedBranch();
1689 saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
1690 bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
1691 bo->setFrameBrushColor (c);
1695 void VymModel::setFramePadding (const int &i)
1697 BranchObj *bo=getSelectedBranch();
1700 saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
1701 bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
1702 bo->setFramePadding (i);
1708 void VymModel::setFrameBorderWidth(const int &i)
1710 BranchObj *bo=getSelectedBranch();
1713 saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
1714 bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
1715 bo->setFrameBorderWidth (i);
1721 void VymModel::setIncludeImagesVer(bool b)
1723 BranchObj *bo=getSelectedBranch();
1726 QString u= b ? "false" : "true";
1727 QString r=!b ? "false" : "true";
1731 QString("setIncludeImagesVertically (%1)").arg(u),
1733 QString("setIncludeImagesVertically (%1)").arg(r),
1734 QString("Include images vertically in %1").arg(getObjectName(bo))
1736 bo->setIncludeImagesVer(b);
1741 void VymModel::setIncludeImagesHor(bool b)
1743 BranchObj *bo=getSelectedBranch();
1746 QString u= b ? "false" : "true";
1747 QString r=!b ? "false" : "true";
1751 QString("setIncludeImagesHorizontally (%1)").arg(u),
1753 QString("setIncludeImagesHorizontally (%1)").arg(r),
1754 QString("Include images horizontally in %1").arg(getObjectName(bo))
1756 bo->setIncludeImagesHor(b);
1761 void VymModel::setHideLinkUnselected (bool b)
1763 LinkableMapObj *sel=getSelectedLMO();
1765 (selectionType() == TreeItem::Branch ||
1766 selectionType() == TreeItem::MapCenter ||
1767 selectionType() == TreeItem::Image ))
1769 QString u= b ? "false" : "true";
1770 QString r=!b ? "false" : "true";
1774 QString("setHideLinkUnselected (%1)").arg(u),
1776 QString("setHideLinkUnselected (%1)").arg(r),
1777 QString("Hide link of %1 if unselected").arg(getObjectName(sel))
1779 sel->setHideLinkUnselected(b);
1783 void VymModel::setHideExport(bool b)
1785 BranchObj *bo=getSelectedBranch();
1788 bo->setHideInExport (b);
1789 QString u= b ? "false" : "true";
1790 QString r=!b ? "false" : "true";
1794 QString ("setHideExport (%1)").arg(u),
1796 QString ("setHideExport (%1)").arg(r),
1797 QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bo)).arg (r)
1802 // FIXME VM needed? scene()->update();
1806 void VymModel::toggleHideExport()
1808 BranchObj *bo=getSelectedBranch();
1810 setHideExport ( !bo->hideInExport() );
1814 void VymModel::copy()
1816 LinkableMapObj *sel=getSelectedLMO();
1818 (selectionType() == TreeItem::Branch ||
1819 selectionType() == TreeItem::MapCenter ||
1820 selectionType() == TreeItem::Image ))
1822 if (redosAvail == 0)
1825 QString s=getSelectString(sel);
1826 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel );
1827 curClipboard=curStep;
1830 // Copy also to global clipboard, because we are at last step in history
1831 QString bakMapName(QString("history-%1").arg(curStep));
1832 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1833 copyDir (bakMapDir,clipboardDir );
1835 clipboardEmpty=false;
1841 void VymModel::pasteNoSave(const int &n)
1843 bool old=blockSaveState;
1844 blockSaveState=true;
1845 bool zippedOrg=zipped;
1846 if (redosAvail > 0 || n!=0)
1848 // Use the "historical" buffer
1849 QString bakMapName(QString("history-%1").arg(n));
1850 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1851 load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
1853 // Use the global buffer
1854 load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
1859 void VymModel::paste()
1861 BranchObj *sel=getSelectedBranch();
1864 saveStateChangingPart(
1867 QString ("paste (%1)").arg(curClipboard),
1868 QString("Paste to %1").arg( getObjectName(sel))
1875 void VymModel::cut()
1877 LinkableMapObj *sel=getSelectedLMO();
1878 if ( sel && (selectionType() == TreeItem::Branch ||
1879 selectionType()==TreeItem::MapCenter ||
1880 selectionType()==TreeItem::Image))
1882 /* No savestate! savestate is called in cutNoSave
1883 saveStateChangingPart(
1887 QString("Cut %1").arg(getObjectName(sel ))
1896 void VymModel::moveBranchUp()
1898 BranchObj* bo=getSelectedBranch();
1902 if (!bo->canMoveBranchUp()) return;
1903 par=(BranchObj*)(bo->getParObj());
1904 BranchObj *obo=par->moveBranchUp (bo); // bo will be the one below selection
1905 saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
1907 //FIXME VM needed? scene()->update();
1909 ensureSelectionVisible();
1913 void VymModel::moveBranchDown()
1915 BranchObj* bo=getSelectedBranch();
1919 if (!bo->canMoveBranchDown()) return;
1920 par=(BranchObj*)(bo->getParObj());
1921 BranchObj *obo=par->moveBranchDown(bo); // bo will be the one above selection
1922 saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
1924 //FIXME VM needed? scene()->update();
1926 ensureSelectionVisible();
1930 void VymModel::sortChildren()
1932 BranchObj* bo=getSelectedBranch();
1935 if(bo->countBranches()>1)
1937 saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
1940 ensureSelectionVisible();
1945 void VymModel::createMapCenter()
1947 MapCenterObj *mco=addMapCenter (QPointF (0,0) );
1951 void VymModel::createBranch()
1953 addNewBranchInt (-2);
1957 TreeItem* VymModel::createImage()
1959 BranchObj *bo=getSelectedBranch();
1962 FloatImageObj *newfio=bo->addFloatImage(); // FIXME VM Old model, merge with below
1965 QList<QVariant> cData;
1966 cData << "VM:createImage" << "undef"<<"undef";
1967 TreeItem *parti=bo->getTreeItem();
1968 TreeItem *ti=new TreeItem (cData,parti);
1969 ti->setLMO (newfio);
1970 ti->setType (TreeItem::Image);
1971 parti->appendChild (ti);
1975 newfio->setTreeItem (ti);
1976 select (newfio); // FIXME VM really needed here?
1983 MapCenterObj* VymModel::addMapCenter ()
1985 MapCenterObj *mco=addMapCenter (contextPos);
1986 cout <<"VM::addMCO () mapScene="<<mapScene<<endl;
1987 //FIXME selection.select (mco);
1989 ensureSelectionVisible();
1994 QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
1995 QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
2000 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
2002 MapCenterObj *mapCenter = new MapCenterObj(mapScene,this);
2003 mapCenter->setMapEditor(mapEditor); //FIXME VM needed to get defLinkStyle, mapLinkColorHint ... for later added objects
2004 mapCenter->move (absPos);
2005 mapCenter->setVisibility (true);
2006 mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
2007 mapCenters.append(mapCenter);
2010 QList<QVariant> cData;
2011 cData << "VM:addMapCenter" << "undef"<<"undef";
2012 TreeItem *ti=new TreeItem (cData,rootItem);
2013 ti->setLMO (mapCenter);
2014 ti->setType (TreeItem::MapCenter);
2015 mapCenter->setTreeItem (ti);
2016 rootItem->appendChild (ti);
2021 MapCenterObj* VymModel::removeMapCenter(MapCenterObj* mco)
2023 int i=mapCenters.indexOf (mco);
2026 mapCenters.removeAt (i);
2028 if (i>0) return mapCenters.at(i-1); // Return previous MCO
2033 MapCenterObj* VymModel::getLastMapCenter()
2035 if (mapCenters.size()>0)
2036 return mapCenters.last();
2043 BranchObj* VymModel::addNewBranchInt(int num)
2045 // Depending on pos:
2046 // -3 insert in children of parent above selection
2047 // -2 add branch to selection
2048 // -1 insert in children of parent below selection
2049 // 0..n insert in children of parent at pos
2050 BranchObj *newbo=NULL;
2051 BranchObj *bo=getSelectedBranch();
2056 // save scroll state. If scrolled, automatically select
2057 // new branch in order to tmp unscroll parent...
2058 newbo=bo->addBranch();
2061 QList<QVariant> cData;
2062 cData << "VM:createBranch" << "undef"<<"undef";
2063 TreeItem *parti=bo->getTreeItem();
2064 TreeItem *ti=new TreeItem (cData,parti);
2066 ti->setType (TreeItem::Branch);
2067 parti->appendChild (ti);
2071 newbo->setTreeItem (ti);
2072 select (newbo); // FIXME VM really needed here?
2078 bo=(BranchObj*)bo->getParObj();
2079 if (bo) newbo=bo->insertBranch(num);
2083 bo=(BranchObj*)bo->getParObj();
2084 if (bo) newbo=bo->insertBranch(num);
2090 BranchObj* VymModel::addNewBranch(int pos)
2092 // Different meaning than num in addNewBranchInt!
2096 BranchObj *bo = getSelectedBranch();
2097 BranchObj *newbo=NULL;
2101 // FIXME VM do we still need this in model? setCursor (Qt::ArrowCursor);
2103 newbo=addNewBranchInt (pos-2);
2111 QString ("addBranch (%1)").arg(pos),
2112 QString ("Add new branch to %1").arg(getObjectName(bo)));
2115 // selection.update(); FIXME
2116 latestSelectionString=getSelectString(newbo);
2117 // In Network mode, the client needs to know where the new branch is,
2118 // so we have to pass on this information via saveState.
2119 // TODO: Get rid of this positioning workaround
2120 QString ps=qpointfToString (newbo->getAbsPos());
2121 sendData ("selectLatestAdded ()");
2122 sendData (QString("move %1").arg(ps));
2130 BranchObj* VymModel::addNewBranchBefore()
2132 BranchObj *newbo=NULL;
2133 BranchObj *bo = getSelectedBranch();
2134 if (bo && selectionType()==TreeItem::Branch)
2135 // We accept no MapCenterObj here, so we _have_ a parent
2137 QPointF p=bo->getRelPos();
2140 BranchObj *parbo=(BranchObj*)(bo->getParObj());
2142 // add below selection
2143 newbo=parbo->insertBranch(bo->getNum()+1);
2146 newbo->move2RelPos (p);
2148 // Move selection to new branch
2149 bo->linkTo (newbo,-1);
2151 saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()",
2152 QString ("Add branch before %1").arg(getObjectName(bo)));
2155 // selection.update(); FIXME
2158 latestSelectionString=selection.getSelectString();
2162 void VymModel::deleteSelection()
2164 BranchObj *bo = getSelectedBranch();
2166 if (bo && selectionType()==TreeItem::MapCenter)
2168 // BranchObj* par=(BranchObj*)(bo->getParObj());
2169 selection.unselect();
2170 /* FIXME VM Note: does saveStateRemovingPart work for MCO? (No parent!)
2171 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2173 bo=removeMapCenter ((MapCenterObj*)bo);
2176 selection.select (bo);
2177 ensureSelectionVisible();
2183 if (bo && selectionType()==TreeItem::Branch)
2185 QModelIndex ix=getSelectedIndex();
2187 BranchObj* par=(BranchObj*)bo->getParObj();
2189 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2190 par->removeBranch(bo);
2192 ensureSelectionVisible();
2197 FloatImageObj *fio=selection.getFloatImage();
2200 BranchObj* par=(BranchObj*)fio->getParObj();
2201 saveStateChangingPart(
2205 QString("Delete %1").arg(getObjectName(fio))
2208 par->removeFloatImage(fio);
2212 ensureSelectionVisible();
2217 void VymModel::deleteKeepChildren()
2219 BranchObj *bo=getSelectedBranch();
2223 par=(BranchObj*)(bo->getParObj());
2225 // Don't use this on mapcenter
2228 // Check if we have childs at all to keep
2229 if (bo->countBranches()==0)
2235 QPointF p=bo->getRelPos();
2236 saveStateChangingPart(
2239 "deleteKeepChildren ()",
2240 QString("Remove %1 and keep its children").arg(getObjectName(bo))
2243 QString sel=getSelectString(bo);
2245 par->removeBranchHere(bo);
2248 getSelectedBranch()->move2RelPos (p);
2253 void VymModel::deleteChildren()
2255 BranchObj *bo=getSelectedBranch();
2258 saveStateChangingPart(
2261 "deleteChildren ()",
2262 QString( "Remove children of branch %1").arg(getObjectName(bo))
2264 bo->removeChildren();
2270 bool VymModel::scrollBranch(BranchObj *bo)
2274 if (bo->isScrolled()) return false;
2275 if (bo->countBranches()==0) return false;
2276 if (bo->getDepth()==0) return false;
2282 QString ("%1 ()").arg(u),
2284 QString ("%1 ()").arg(r),
2285 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2289 // FIXME VM needed? scene()->update();
2295 bool VymModel::unscrollBranch(BranchObj *bo)
2299 if (!bo->isScrolled()) return false;
2300 if (bo->countBranches()==0) return false;
2301 if (bo->getDepth()==0) return false;
2307 QString ("%1 ()").arg(u),
2309 QString ("%1 ()").arg(r),
2310 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2314 // FIXME VM needed? scene()->update();
2320 void VymModel::toggleScroll()
2322 BranchObj *bo=getSelectedBranch();
2323 if (selectionType()==TreeItem::Branch )
2325 if (bo->isScrolled())
2326 unscrollBranch (bo);
2332 void VymModel::unscrollChildren()
2334 BranchObj *bo=getSelectedBranch();
2340 if (bo->isScrolled()) unscrollBranch (bo);
2345 void VymModel::addFloatImage (const QPixmap &img)
2347 BranchObj *bo=getSelectedBranch();
2350 FloatImageObj *fio=bo->addFloatImage();
2352 fio->setOriginalFilename("No original filename (image added by dropevent)");
2353 QString s=getSelectString(bo);
2354 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio );
2355 saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
2357 // FIXME VM needed? scene()->update();
2362 void VymModel::colorBranch (QColor c)
2364 BranchObj *bo=getSelectedBranch();
2369 QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
2371 QString ("colorBranch (\"%1\")").arg(c.name()),
2372 QString("Set color of %1 to %2").arg(getObjectName(bo)).arg(c.name())
2374 bo->setColor(c); // color branch
2378 void VymModel::colorSubtree (QColor c)
2380 BranchObj *bo=getSelectedBranch();
2383 saveStateChangingPart(
2386 QString ("colorSubtree (\"%1\")").arg(c.name()),
2387 QString ("Set color of %1 and children to %2").arg(getObjectName(bo)).arg(c.name())
2389 bo->setColorSubtree (c); // color links, color children
2393 QColor VymModel::getCurrentHeadingColor()
2395 BranchObj *bo=getSelectedBranch();
2396 if (bo) return bo->getColor();
2398 QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
2404 void VymModel::editURL()
2406 BranchObj *bo=getSelectedBranch();
2410 QString text = QInputDialog::getText(
2411 "VYM", tr("Enter URL:"), QLineEdit::Normal,
2412 bo->getURL(), &ok, NULL);
2414 // user entered something and pressed OK
2419 void VymModel::editLocalURL()
2421 BranchObj *bo=getSelectedBranch();
2424 QStringList filters;
2425 filters <<"All files (*)";
2426 filters << tr("Text","Filedialog") + " (*.txt)";
2427 filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
2428 filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
2429 filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
2430 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
2431 fd->setFilters (filters);
2432 fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
2433 fd->setDirectory (lastFileDir);
2434 if (! bo->getVymLink().isEmpty() )
2435 fd->selectFile( bo->getURL() );
2438 if ( fd->exec() == QDialog::Accepted )
2440 lastFileDir=QDir (fd->directory().path());
2441 setURL (fd->selectedFile() );
2447 void VymModel::editHeading2URL()
2449 BranchObj *bo=getSelectedBranch();
2451 setURL (bo->getHeading());
2454 void VymModel::editBugzilla2URL()
2456 BranchObj *bo=getSelectedBranch();
2459 QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
2464 void VymModel::editFATE2URL()
2466 BranchObj *bo=getSelectedBranch();
2469 QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
2472 "setURL (\""+bo->getURL()+"\")",
2474 "setURL (\""+url+"\")",
2475 QString("Use heading of %1 as link to FATE").arg(getObjectName(bo))
2482 void VymModel::editVymLink()
2484 BranchObj *bo=getSelectedBranch();
2487 QStringList filters;
2488 filters <<"VYM map (*.vym)";
2489 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
2490 fd->setFilters (filters);
2491 fd->setCaption(vymName+" - " +tr("Link to another map"));
2492 fd->setDirectory (lastFileDir);
2493 if (! bo->getVymLink().isEmpty() )
2494 fd->selectFile( bo->getVymLink() );
2498 if ( fd->exec() == QDialog::Accepted )
2500 lastFileDir=QDir (fd->directory().path());
2503 "setVymLink (\""+bo->getVymLink()+"\")",
2505 "setVymLink (\""+fd->selectedFile()+"\")",
2506 QString("Set vymlink of %1 to %2").arg(getObjectName(bo)).arg(fd->selectedFile())
2508 setVymLink (fd->selectedFile() ); // FIXME ok?
2513 void VymModel::setVymLink (const QString &s)
2515 // Internal function, no saveState needed
2516 BranchObj *bo=getSelectedBranch();
2523 ensureSelectionVisible();
2527 void VymModel::deleteVymLink()
2529 BranchObj *bo=getSelectedBranch();
2534 "setVymLink (\""+bo->getVymLink()+"\")",
2536 "setVymLink (\"\")",
2537 QString("Unset vymlink of %1").arg(getObjectName(bo))
2539 bo->setVymLink ("" );
2542 // FIXME VM needed? scene()->update();
2546 QString VymModel::getVymLink()
2548 BranchObj *bo=getSelectedBranch();
2550 return bo->getVymLink();
2556 QStringList VymModel::getVymLinks()
2559 BranchObj *bo=getSelectedBranch();
2565 if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
2573 void VymModel::followXLink(int i)
2575 BranchObj *bo=getSelectedBranch();
2578 bo=bo->XLinkTargetAt(i);
2581 selection.select(bo);
2582 ensureSelectionVisible();
2587 void VymModel::editXLink(int i) // FIXME VM missing saveState
2589 BranchObj *bo=getSelectedBranch();
2592 XLinkObj *xlo=bo->XLinkAt(i);
2595 EditXLinkDialog dia;
2597 dia.setSelection(bo);
2598 if (dia.exec() == QDialog::Accepted)
2600 if (dia.useSettingsGlobal() )
2602 setMapDefXLinkColor (xlo->getColor() );
2603 setMapDefXLinkWidth (xlo->getWidth() );
2605 if (dia.deleteXLink())
2606 bo->deleteXLinkAt(i);
2616 //////////////////////////////////////////////
2618 //////////////////////////////////////////////
2620 void VymModel::parseAtom(const QString &atom)
2622 BranchObj *selb=getSelectedBranch();
2628 // Split string s into command and parameters
2629 parser.parseAtom (atom);
2630 QString com=parser.getCommand();
2632 // External commands
2633 /////////////////////////////////////////////////////////////////////
2634 if (com=="addBranch")
2636 if (selection.isEmpty())
2638 parser.setError (Aborted,"Nothing selected");
2641 parser.setError (Aborted,"Type of selection is not a branch");
2646 if (parser.checkParCount(pl))
2648 if (parser.parCount()==0)
2652 n=parser.parInt (ok,0);
2653 if (ok ) addNewBranch (n);
2657 /////////////////////////////////////////////////////////////////////
2658 } else if (com=="addBranchBefore")
2660 if (selection.isEmpty())
2662 parser.setError (Aborted,"Nothing selected");
2665 parser.setError (Aborted,"Type of selection is not a branch");
2668 if (parser.parCount()==0)
2670 addNewBranchBefore ();
2673 /////////////////////////////////////////////////////////////////////
2674 } else if (com==QString("addMapCenter"))
2676 if (parser.checkParCount(2))
2678 x=parser.parDouble (ok,0);
2681 y=parser.parDouble (ok,1);
2682 if (ok) addMapCenter (QPointF(x,y));
2685 /////////////////////////////////////////////////////////////////////
2686 } else if (com==QString("addMapReplace"))
2688 if (selection.isEmpty())
2690 parser.setError (Aborted,"Nothing selected");
2693 parser.setError (Aborted,"Type of selection is not a branch");
2694 } else if (parser.checkParCount(1))
2696 //s=parser.parString (ok,0); // selection
2697 t=parser.parString (ok,0); // path to map
2698 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2699 addMapReplaceInt(getSelectString(selb),t);
2701 /////////////////////////////////////////////////////////////////////
2702 } else if (com==QString("addMapInsert"))
2704 if (selection.isEmpty())
2706 parser.setError (Aborted,"Nothing selected");
2709 parser.setError (Aborted,"Type of selection is not a branch");
2712 if (parser.checkParCount(2))
2714 t=parser.parString (ok,0); // path to map
2715 n=parser.parInt(ok,1); // position
2716 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2717 addMapInsertInt(t,n);
2720 /////////////////////////////////////////////////////////////////////
2721 } else if (com=="clearFlags")
2723 if (selection.isEmpty() )
2725 parser.setError (Aborted,"Nothing selected");
2728 parser.setError (Aborted,"Type of selection is not a branch");
2729 } else if (parser.checkParCount(0))
2731 selb->clearStandardFlags();
2732 selb->updateFlagsToolbar();
2734 /////////////////////////////////////////////////////////////////////
2735 } else if (com=="colorBranch")
2737 if (selection.isEmpty())
2739 parser.setError (Aborted,"Nothing selected");
2742 parser.setError (Aborted,"Type of selection is not a branch");
2743 } else if (parser.checkParCount(1))
2745 QColor c=parser.parColor (ok,0);
2746 if (ok) colorBranch (c);
2748 /////////////////////////////////////////////////////////////////////
2749 } else if (com=="colorSubtree")
2751 if (selection.isEmpty())
2753 parser.setError (Aborted,"Nothing selected");
2756 parser.setError (Aborted,"Type of selection is not a branch");
2757 } else if (parser.checkParCount(1))
2759 QColor c=parser.parColor (ok,0);
2760 if (ok) colorSubtree (c);
2762 /////////////////////////////////////////////////////////////////////
2763 } else if (com=="copy")
2765 if (selection.isEmpty())
2767 parser.setError (Aborted,"Nothing selected");
2770 parser.setError (Aborted,"Type of selection is not a branch");
2771 } else if (parser.checkParCount(0))
2773 //FIXME missing action for copy
2775 /////////////////////////////////////////////////////////////////////
2776 } else if (com=="cut")
2778 if (selection.isEmpty())
2780 parser.setError (Aborted,"Nothing selected");
2781 } else if ( selectionType()!=TreeItem::Branch &&
2782 selectionType()!=TreeItem::MapCenter &&
2783 selectionType()!=TreeItem::Image )
2785 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2786 } else if (parser.checkParCount(0))
2790 /////////////////////////////////////////////////////////////////////
2791 } else if (com=="delete")
2793 if (selection.isEmpty())
2795 parser.setError (Aborted,"Nothing selected");
2797 /*else if (selectionType() != TreeItem::Branch && selectionType() != TreeItem::Image )
2799 parser.setError (Aborted,"Type of selection is wrong.");
2802 else if (parser.checkParCount(0))
2806 /////////////////////////////////////////////////////////////////////
2807 } else if (com=="deleteKeepChildren")
2809 if (selection.isEmpty())
2811 parser.setError (Aborted,"Nothing selected");
2814 parser.setError (Aborted,"Type of selection is not a branch");
2815 } else if (parser.checkParCount(0))
2817 deleteKeepChildren();
2819 /////////////////////////////////////////////////////////////////////
2820 } else if (com=="deleteChildren")
2822 if (selection.isEmpty())
2824 parser.setError (Aborted,"Nothing selected");
2827 parser.setError (Aborted,"Type of selection is not a branch");
2828 } else if (parser.checkParCount(0))
2832 /////////////////////////////////////////////////////////////////////
2833 } else if (com=="exportASCII")
2837 if (parser.parCount()>=1)
2838 // Hey, we even have a filename
2839 fname=parser.parString(ok,0);
2842 parser.setError (Aborted,"Could not read filename");
2845 exportASCII (fname,false);
2847 /////////////////////////////////////////////////////////////////////
2848 } else if (com=="exportImage")
2852 if (parser.parCount()>=2)
2853 // Hey, we even have a filename
2854 fname=parser.parString(ok,0);
2857 parser.setError (Aborted,"Could not read filename");
2860 QString format="PNG";
2861 if (parser.parCount()>=2)
2863 format=parser.parString(ok,1);
2865 exportImage (fname,false,format);
2867 /////////////////////////////////////////////////////////////////////
2868 } else if (com=="exportXHTML")
2872 if (parser.parCount()>=2)
2873 // Hey, we even have a filename
2874 fname=parser.parString(ok,1);
2877 parser.setError (Aborted,"Could not read filename");
2880 exportXHTML (fname,false);
2882 /////////////////////////////////////////////////////////////////////
2883 } else if (com=="exportXML")
2887 if (parser.parCount()>=2)
2888 // Hey, we even have a filename
2889 fname=parser.parString(ok,1);
2892 parser.setError (Aborted,"Could not read filename");
2895 exportXML (fname,false);
2897 /////////////////////////////////////////////////////////////////////
2898 } else if (com=="importDir")
2900 if (selection.isEmpty())
2902 parser.setError (Aborted,"Nothing selected");
2905 parser.setError (Aborted,"Type of selection is not a branch");
2906 } else if (parser.checkParCount(1))
2908 s=parser.parString(ok,0);
2909 if (ok) importDirInt(s);
2911 /////////////////////////////////////////////////////////////////////
2912 } else if (com=="linkTo")
2914 if (selection.isEmpty())
2916 parser.setError (Aborted,"Nothing selected");
2919 if (parser.checkParCount(4))
2921 // 0 selectstring of parent
2922 // 1 num in parent (for branches)
2923 // 2,3 x,y of mainbranch or mapcenter
2924 s=parser.parString(ok,0);
2925 LinkableMapObj *dst=findObjBySelect (s);
2928 if (typeid(*dst) == typeid(BranchObj) )
2930 // Get number in parent
2931 n=parser.parInt (ok,1);
2934 selb->linkTo ((BranchObj*)(dst),n);
2937 } else if (typeid(*dst) == typeid(MapCenterObj) )
2939 selb->linkTo ((BranchObj*)(dst),-1);
2940 // Get coordinates of mainbranch
2941 x=parser.parDouble(ok,2);
2944 y=parser.parDouble(ok,3);
2954 } else if ( selectionType() == TreeItem::Image)
2956 if (parser.checkParCount(1))
2958 // 0 selectstring of parent
2959 s=parser.parString(ok,0);
2960 LinkableMapObj *dst=findObjBySelect (s);
2963 if (typeid(*dst) == typeid(BranchObj) ||
2964 typeid(*dst) == typeid(MapCenterObj))
2965 linkFloatImageTo (getSelectString(dst));
2967 parser.setError (Aborted,"Destination is not a branch");
2970 parser.setError (Aborted,"Type of selection is not a floatimage or branch");
2971 /////////////////////////////////////////////////////////////////////
2972 } else if (com=="loadImage")
2974 if (selection.isEmpty())
2976 parser.setError (Aborted,"Nothing selected");
2979 parser.setError (Aborted,"Type of selection is not a branch");
2980 } else if (parser.checkParCount(1))
2982 s=parser.parString(ok,0);
2983 if (ok) loadFloatImageInt (s);
2985 /////////////////////////////////////////////////////////////////////
2986 } else if (com=="moveBranchUp")
2988 if (selection.isEmpty() )
2990 parser.setError (Aborted,"Nothing selected");
2993 parser.setError (Aborted,"Type of selection is not a branch");
2994 } else if (parser.checkParCount(0))
2998 /////////////////////////////////////////////////////////////////////
2999 } else if (com=="moveBranchDown")
3001 if (selection.isEmpty() )
3003 parser.setError (Aborted,"Nothing selected");
3006 parser.setError (Aborted,"Type of selection is not a branch");
3007 } else if (parser.checkParCount(0))
3011 /////////////////////////////////////////////////////////////////////
3012 } else if (com=="move")
3014 if (selection.isEmpty() )
3016 parser.setError (Aborted,"Nothing selected");
3017 } else if ( selectionType()!=TreeItem::Branch &&
3018 selectionType()!=TreeItem::MapCenter &&
3019 selectionType()!=TreeItem::Image )
3021 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3022 } else if (parser.checkParCount(2))
3024 x=parser.parDouble (ok,0);
3027 y=parser.parDouble (ok,1);
3031 /////////////////////////////////////////////////////////////////////
3032 } else if (com=="moveRel")
3034 if (selection.isEmpty() )
3036 parser.setError (Aborted,"Nothing selected");
3037 } else if ( selectionType()!=TreeItem::Branch &&
3038 selectionType()!=TreeItem::MapCenter &&
3039 selectionType()!=TreeItem::Image )
3041 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3042 } else if (parser.checkParCount(2))
3044 x=parser.parDouble (ok,0);
3047 y=parser.parDouble (ok,1);
3048 if (ok) moveRel (x,y);
3051 /////////////////////////////////////////////////////////////////////
3052 } else if (com=="nop")
3054 /////////////////////////////////////////////////////////////////////
3055 } else if (com=="paste")
3057 if (selection.isEmpty() )
3059 parser.setError (Aborted,"Nothing selected");
3062 parser.setError (Aborted,"Type of selection is not a branch");
3063 } else if (parser.checkParCount(1))
3065 n=parser.parInt (ok,0);
3066 if (ok) pasteNoSave(n);
3068 /////////////////////////////////////////////////////////////////////
3069 } else if (com=="qa")
3071 if (selection.isEmpty() )
3073 parser.setError (Aborted,"Nothing selected");
3076 parser.setError (Aborted,"Type of selection is not a branch");
3077 } else if (parser.checkParCount(4))
3080 c=parser.parString (ok,0);
3083 parser.setError (Aborted,"No comment given");
3086 s=parser.parString (ok,1);
3089 parser.setError (Aborted,"First parameter is not a string");
3092 t=parser.parString (ok,2);
3095 parser.setError (Aborted,"Condition is not a string");
3098 u=parser.parString (ok,3);
3101 parser.setError (Aborted,"Third parameter is not a string");
3106 parser.setError (Aborted,"Unknown type: "+s);
3111 parser.setError (Aborted,"Unknown operator: "+t);
3116 parser.setError (Aborted,"Type of selection is not a branch");
3119 if (selb->getHeading() == u)
3121 cout << "PASSED: " << qPrintable (c) << endl;
3124 cout << "FAILED: " << qPrintable (c) << endl;
3134 /////////////////////////////////////////////////////////////////////
3135 } else if (com=="saveImage")
3137 FloatImageObj *fio=selection.getFloatImage();
3140 parser.setError (Aborted,"Type of selection is not an image");
3141 } else if (parser.checkParCount(2))
3143 s=parser.parString(ok,0);
3146 t=parser.parString(ok,1);
3147 if (ok) saveFloatImageInt (fio,t,s);
3150 /////////////////////////////////////////////////////////////////////
3151 } else if (com=="scroll")
3153 if (selection.isEmpty() )
3155 parser.setError (Aborted,"Nothing selected");
3158 parser.setError (Aborted,"Type of selection is not a branch");
3159 } else if (parser.checkParCount(0))
3161 if (!scrollBranch (selb))
3162 parser.setError (Aborted,"Could not scroll branch");
3164 /////////////////////////////////////////////////////////////////////
3165 } else if (com=="select")
3167 if (parser.checkParCount(1))
3169 s=parser.parString(ok,0);
3172 /////////////////////////////////////////////////////////////////////
3173 } else if (com=="selectLastBranch")
3175 if (selection.isEmpty() )
3177 parser.setError (Aborted,"Nothing selected");
3180 parser.setError (Aborted,"Type of selection is not a branch");
3181 } else if (parser.checkParCount(0))
3183 BranchObj *bo=selb->getLastBranch();
3185 parser.setError (Aborted,"Could not select last branch");
3189 /////////////////////////////////////////////////////////////////////
3190 } else if (com=="selectLastImage")
3192 if (selection.isEmpty() )
3194 parser.setError (Aborted,"Nothing selected");
3197 parser.setError (Aborted,"Type of selection is not a branch");
3198 } else if (parser.checkParCount(0))
3200 FloatImageObj *fio=selb->getLastFloatImage();
3202 parser.setError (Aborted,"Could not select last image");
3206 /////////////////////////////////////////////////////////////////////
3207 } else if (com=="selectLatestAdded")
3209 if (latestSelectionString.isEmpty() )
3211 parser.setError (Aborted,"No latest added object");
3214 if (!select (latestSelectionString))
3215 parser.setError (Aborted,"Could not select latest added object "+latestSelectionString);
3217 /////////////////////////////////////////////////////////////////////
3218 } else if (com=="setFrameType")
3220 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3222 parser.setError (Aborted,"Type of selection does not allow setting frame type");
3224 else if (parser.checkParCount(1))
3226 s=parser.parString(ok,0);
3227 if (ok) setFrameType (s);
3229 /////////////////////////////////////////////////////////////////////
3230 } else if (com=="setFramePenColor")
3232 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3234 parser.setError (Aborted,"Type of selection does not allow setting of pen color");
3236 else if (parser.checkParCount(1))
3238 QColor c=parser.parColor(ok,0);
3239 if (ok) setFramePenColor (c);
3241 /////////////////////////////////////////////////////////////////////
3242 } else if (com=="setFrameBrushColor")
3244 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3246 parser.setError (Aborted,"Type of selection does not allow setting brush color");
3248 else if (parser.checkParCount(1))
3250 QColor c=parser.parColor(ok,0);
3251 if (ok) setFrameBrushColor (c);
3253 /////////////////////////////////////////////////////////////////////
3254 } else if (com=="setFramePadding")
3256 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3258 parser.setError (Aborted,"Type of selection does not allow setting frame padding");
3260 else if (parser.checkParCount(1))
3262 n=parser.parInt(ok,0);
3263 if (ok) setFramePadding(n);
3265 /////////////////////////////////////////////////////////////////////
3266 } else if (com=="setFrameBorderWidth")
3268 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3270 parser.setError (Aborted,"Type of selection does not allow setting frame border width");
3272 else if (parser.checkParCount(1))
3274 n=parser.parInt(ok,0);
3275 if (ok) setFrameBorderWidth (n);
3277 /////////////////////////////////////////////////////////////////////
3278 } else if (com=="setMapAuthor")
3280 if (parser.checkParCount(1))
3282 s=parser.parString(ok,0);
3283 if (ok) setAuthor (s);
3285 /////////////////////////////////////////////////////////////////////
3286 } else if (com=="setMapComment")
3288 if (parser.checkParCount(1))
3290 s=parser.parString(ok,0);
3291 if (ok) setComment(s);
3293 /////////////////////////////////////////////////////////////////////
3294 } else if (com=="setMapBackgroundColor")
3296 if (selection.isEmpty() )
3298 parser.setError (Aborted,"Nothing selected");
3299 } else if (! getSelectedBranch() )
3301 parser.setError (Aborted,"Type of selection is not a branch");
3302 } else if (parser.checkParCount(1))
3304 QColor c=parser.parColor (ok,0);
3305 if (ok) setMapBackgroundColor (c);
3307 /////////////////////////////////////////////////////////////////////
3308 } else if (com=="setMapDefLinkColor")
3310 if (selection.isEmpty() )
3312 parser.setError (Aborted,"Nothing selected");
3315 parser.setError (Aborted,"Type of selection is not a branch");
3316 } else if (parser.checkParCount(1))
3318 QColor c=parser.parColor (ok,0);
3319 if (ok) setMapDefLinkColor (c);
3321 /////////////////////////////////////////////////////////////////////
3322 } else if (com=="setMapLinkStyle")
3324 if (parser.checkParCount(1))
3326 s=parser.parString (ok,0);
3327 if (ok) setMapLinkStyle(s);
3329 /////////////////////////////////////////////////////////////////////
3330 } else if (com=="setHeading")
3332 if (selection.isEmpty() )
3334 parser.setError (Aborted,"Nothing selected");
3337 parser.setError (Aborted,"Type of selection is not a branch");
3338 } else if (parser.checkParCount(1))
3340 s=parser.parString (ok,0);
3344 /////////////////////////////////////////////////////////////////////
3345 } else if (com=="setHideExport")
3347 if (selection.isEmpty() )
3349 parser.setError (Aborted,"Nothing selected");
3350 } else if (selectionType()!=TreeItem::Branch && selectionType() != TreeItem::MapCenter &&selectionType()!=TreeItem::Image)
3352 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3353 } else if (parser.checkParCount(1))
3355 b=parser.parBool(ok,0);
3356 if (ok) setHideExport (b);
3358 /////////////////////////////////////////////////////////////////////
3359 } else if (com=="setIncludeImagesHorizontally")
3361 if (selection.isEmpty() )
3363 parser.setError (Aborted,"Nothing selected");
3366 parser.setError (Aborted,"Type of selection is not a branch");
3367 } else if (parser.checkParCount(1))
3369 b=parser.parBool(ok,0);
3370 if (ok) setIncludeImagesHor(b);
3372 /////////////////////////////////////////////////////////////////////
3373 } else if (com=="setIncludeImagesVertically")
3375 if (selection.isEmpty() )
3377 parser.setError (Aborted,"Nothing selected");
3380 parser.setError (Aborted,"Type of selection is not a branch");
3381 } else if (parser.checkParCount(1))
3383 b=parser.parBool(ok,0);
3384 if (ok) setIncludeImagesVer(b);
3386 /////////////////////////////////////////////////////////////////////
3387 } else if (com=="setHideLinkUnselected")
3389 if (selection.isEmpty() )
3391 parser.setError (Aborted,"Nothing selected");
3392 } else if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3394 parser.setError (Aborted,"Type of selection does not allow hiding the link");
3395 } else if (parser.checkParCount(1))
3397 b=parser.parBool(ok,0);
3398 if (ok) setHideLinkUnselected(b);
3400 /////////////////////////////////////////////////////////////////////
3401 } else if (com=="setSelectionColor")
3403 if (parser.checkParCount(1))
3405 QColor c=parser.parColor (ok,0);
3406 if (ok) setSelectionColorInt (c);
3408 /////////////////////////////////////////////////////////////////////
3409 } else if (com=="setURL")
3411 if (selection.isEmpty() )
3413 parser.setError (Aborted,"Nothing selected");
3416 parser.setError (Aborted,"Type of selection is not a branch");
3417 } else if (parser.checkParCount(1))
3419 s=parser.parString (ok,0);
3422 /////////////////////////////////////////////////////////////////////
3423 } else if (com=="setVymLink")
3425 if (selection.isEmpty() )
3427 parser.setError (Aborted,"Nothing selected");
3430 parser.setError (Aborted,"Type of selection is not a branch");
3431 } else if (parser.checkParCount(1))
3433 s=parser.parString (ok,0);
3434 if (ok) setVymLink(s);
3437 /////////////////////////////////////////////////////////////////////
3438 else if (com=="setFlag")
3440 if (selection.isEmpty() )
3442 parser.setError (Aborted,"Nothing selected");
3445 parser.setError (Aborted,"Type of selection is not a branch");
3446 } else if (parser.checkParCount(1))
3448 s=parser.parString(ok,0);
3451 selb->activateStandardFlag(s);
3452 selb->updateFlagsToolbar();
3455 /////////////////////////////////////////////////////////////////////
3456 } else if (com=="setFrameType")
3458 if (selection.isEmpty() )
3460 parser.setError (Aborted,"Nothing selected");
3463 parser.setError (Aborted,"Type of selection is not a branch");
3464 } else if (parser.checkParCount(1))
3466 s=parser.parString(ok,0);
3470 /////////////////////////////////////////////////////////////////////
3471 } else if (com=="sortChildren")
3473 if (selection.isEmpty() )
3475 parser.setError (Aborted,"Nothing selected");
3478 parser.setError (Aborted,"Type of selection is not a branch");
3479 } else if (parser.checkParCount(0))
3483 /////////////////////////////////////////////////////////////////////
3484 } else if (com=="toggleFlag")
3486 if (selection.isEmpty() )
3488 parser.setError (Aborted,"Nothing selected");
3491 parser.setError (Aborted,"Type of selection is not a branch");
3492 } else if (parser.checkParCount(1))
3494 s=parser.parString(ok,0);
3497 selb->toggleStandardFlag(s);
3498 selb->updateFlagsToolbar();
3501 /////////////////////////////////////////////////////////////////////
3502 } else if (com=="unscroll")
3504 if (selection.isEmpty() )
3506 parser.setError (Aborted,"Nothing selected");
3509 parser.setError (Aborted,"Type of selection is not a branch");
3510 } else if (parser.checkParCount(0))
3512 if (!unscrollBranch (selb))
3513 parser.setError (Aborted,"Could not unscroll branch");
3515 /////////////////////////////////////////////////////////////////////
3516 } else if (com=="unscrollChildren")
3518 if (selection.isEmpty() )
3520 parser.setError (Aborted,"Nothing selected");
3523 parser.setError (Aborted,"Type of selection is not a branch");
3524 } else if (parser.checkParCount(0))
3526 unscrollChildren ();
3528 /////////////////////////////////////////////////////////////////////
3529 } else if (com=="unsetFlag")
3531 if (selection.isEmpty() )
3533 parser.setError (Aborted,"Nothing selected");
3536 parser.setError (Aborted,"Type of selection is not a branch");
3537 } else if (parser.checkParCount(1))
3539 s=parser.parString(ok,0);
3542 selb->deactivateStandardFlag(s);
3543 selb->updateFlagsToolbar();
3547 parser.setError (Aborted,"Unknown command");
3550 if (parser.errorLevel()==NoError)
3552 // setChanged(); FIXME should not be called e.g. for export?!
3557 // TODO Error handling
3558 qWarning("VymModel::parseAtom: Error!");
3559 qWarning(parser.errorMessage());
3563 void VymModel::runScript (QString script)
3565 parser.setScript (script);
3567 while (parser.next() )
3568 parseAtom(parser.getAtom());
3571 void VymModel::setExportMode (bool b)
3573 // should be called before and after exports
3574 // depending on the settings
3575 if (b && settings.value("/export/useHideExport","true")=="true")
3576 setHideTmpMode (HideExport);
3578 setHideTmpMode (HideNone);
3581 void VymModel::exportImage(QString fname, bool askName, QString format)
3585 fname=getMapName()+".png";
3592 QFileDialog *fd=new QFileDialog (NULL);
3593 fd->setCaption (tr("Export map as image"));
3594 fd->setDirectory (lastImageDir);
3595 fd->setFileMode(QFileDialog::AnyFile);
3596 fd->setFilters (imageIO.getFilters() );
3599 fl=fd->selectedFiles();
3601 format=imageIO.getType(fd->selectedFilter());
3605 setExportMode (true);
3606 QPixmap pix (getPixmap());
3607 pix.save(fname, format);
3608 setExportMode (false);
3612 void VymModel::exportXML(QString dir, bool askForName)
3616 dir=browseDirectory(NULL,tr("Export XML to directory"));
3617 if (dir =="" && !reallyWriteDirectory(dir) )
3621 // Hide stuff during export, if settings want this
3622 setExportMode (true);
3624 // Create subdirectories
3627 // write to directory
3628 QString saveFile=saveToDir (dir,mapName+"-",true,getTotalBBox().topLeft() ,NULL);
3631 file.setName ( dir + "/"+mapName+".xml");
3632 if ( !file.open( QIODevice::WriteOnly ) )
3634 // This should neverever happen
3635 QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
3639 // Write it finally, and write in UTF8, no matter what
3640 QTextStream ts( &file );
3641 ts.setEncoding (QTextStream::UnicodeUTF8);
3645 // Now write image, too
3646 exportImage (dir+"/images/"+mapName+".png",false,"PNG");
3648 setExportMode (false);
3651 void VymModel::exportASCII(QString fname,bool askName)
3656 ex.setFile (mapName+".txt");
3662 //ex.addFilter ("TXT (*.txt)");
3663 ex.setDir(lastImageDir);
3664 //ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
3669 setExportMode(true);
3671 setExportMode(false);
3675 void VymModel::exportXHTML (const QString &dir, bool askForName)
3677 ExportXHTMLDialog dia(NULL);
3678 dia.setFilePath (filePath );
3679 dia.setMapName (mapName );
3681 if (dir!="") dia.setDir (dir);
3687 if (dia.exec()!=QDialog::Accepted)
3691 QDir d (dia.getDir());
3692 // Check, if warnings should be used before overwriting
3693 // the output directory
3694 if (d.exists() && d.count()>0)
3697 warn.showCancelButton (true);
3698 warn.setText(QString(
3699 "The directory %1 is not empty.\n"
3700 "Do you risk to overwrite some of its contents?").arg(d.path() ));
3701 warn.setCaption("Warning: Directory not empty");
3702 warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
3704 if (warn.exec()!=QDialog::Accepted) ok=false;
3711 exportXML (dia.getDir(),false );
3712 dia.doExport(mapName );
3713 //if (dia.hasChanged()) setChanged();
3717 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
3722 if (ex.setConfigFile(cf))
3724 setExportMode (true);
3725 ex.exportPresentation();
3726 setExportMode (false);
3733 //////////////////////////////////////////////
3735 //////////////////////////////////////////////
3737 void VymModel::registerEditor(QWidget *me)
3739 mapEditor=(MapEditor*)me;
3740 for (int i=0; i<mapCenters.count(); i++)
3741 mapCenters.at(i)->setMapEditor(mapEditor);
3744 void VymModel::unregisterEditor(QWidget *)
3749 void VymModel::setContextPos(QPointF p)
3754 void VymModel::unsetContextPos()
3756 contextPos=QPointF();
3759 void VymModel::updateNoteFlag()
3762 BranchObj *bo=getSelectedBranch();
3765 bo->updateNoteFlag();
3766 mainWindow->updateActions();
3770 void VymModel::updateRelPositions()
3772 for (int i=0; i<mapCenters.count(); i++)
3773 mapCenters.at(i)->updateRelPositions();
3776 void VymModel::reposition()
3778 for (int i=0;i<mapCenters.count(); i++)
3779 mapCenters.at(i)->reposition(); // for positioning heading
3782 QPolygonF VymModel::shape(BranchObj *bo)
3784 // Creating (arbitrary) shapes
3787 QRectF rb=bo->getBBox();
3788 if (bo->getDepth()==0)
3790 // Just take BBox of this mapCenter
3791 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
3795 // Take union of BBox and TotalBBox
3797 QRectF ra=bo->getTotalBBox();
3798 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
3801 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
3804 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
3806 p <<ra.bottomRight()
3808 <<QPointF (rb.topRight().x(), ra.topRight().y() )
3811 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
3816 void VymModel::moveAway(LinkableMapObj *lmo)
3820 // Move all branches and MapCenters away from lmo
3821 // to avoid collisions
3826 BranchObj *boA=(BranchObj*)lmo;
3828 for (int i=0; i<mapCenters.count(); i++)
3830 boB=mapCenters.at(i);
3833 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
3836 <<" ("<<qPrintable(boA->getHeading() )<<")"
3837 <<" with ("<< qPrintable (boB->getHeading() )
3840 <<" minT="<<r.minTranslation<<endl<<endl;
3844 QPixmap VymModel::getPixmap()
3846 QRectF mapRect=getTotalBBox();
3847 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
3850 pp.setRenderHints(mapEditor->renderHints());
3852 // Don't print the visualisation of selection
3853 selection.unselect();
3855 mapScene->render ( &pp,
3856 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
3857 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
3859 // Restore selection
3860 selection.reselect();
3866 void VymModel::setMapLinkStyle (const QString & s)
3871 case LinkableMapObj::Line :
3874 case LinkableMapObj::Parabel:
3875 snow="StyleParabel";
3877 case LinkableMapObj::PolyLine:
3878 snow="StylePolyLine";
3880 case LinkableMapObj::PolyParabel:
3881 snow="StylePolyParabel";
3884 snow="UndefinedStyle";
3889 QString("setMapLinkStyle (\"%1\")").arg(s),
3890 QString("setMapLinkStyle (\"%1\")").arg(snow),
3891 QString("Set map link style (\"%1\")").arg(s)
3895 linkstyle=LinkableMapObj::Line;
3896 else if (s=="StyleParabel")
3897 linkstyle=LinkableMapObj::Parabel;
3898 else if (s=="StylePolyLine")
3899 linkstyle=LinkableMapObj::PolyLine;
3900 else if (s=="StylePolyParabel")
3901 linkstyle=LinkableMapObj::PolyParabel;
3903 linkstyle=LinkableMapObj::UndefinedStyle;
3906 TreeItem *prev=NULL;
3912 bo=(BranchObj*)(cur->getLMO() );
3913 bo->setLinkStyle(bo->getDefLinkStyle());
3914 cur=next(cur,prev,d);
3919 LinkableMapObj::Style VymModel::getMapLinkStyle ()
3924 void VymModel::setMapDefLinkColor(QColor col)
3926 if ( !col.isValid() ) return;
3928 QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
3929 QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
3930 QString("Set map link color to %1").arg(col.name())
3935 TreeItem *prev=NULL;
3938 cur=next(cur,prev,d);
3941 bo=(BranchObj*)(cur->getLMO() );
3948 void VymModel::setMapLinkColorHintInt()
3950 // called from setMapLinkColorHint(lch) or at end of parse
3952 TreeItem *prev=NULL;
3955 cur=next(cur,prev,d);
3958 bo=(BranchObj*)(cur->getLMO() );
3960 cur=next(cur,prev,d);
3964 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
3967 setMapLinkColorHintInt();
3970 void VymModel::toggleMapLinkColorHint()
3972 if (linkcolorhint==LinkableMapObj::HeadingColor)
3973 linkcolorhint=LinkableMapObj::DefaultColor;
3975 linkcolorhint=LinkableMapObj::HeadingColor;
3977 TreeItem *prev=NULL;
3980 cur=next(cur,prev,d);
3983 bo=(BranchObj*)(cur->getLMO() );
3989 void VymModel::selectMapBackgroundImage () // FIXME move to ME
3991 Q3FileDialog *fd=new Q3FileDialog( NULL);
3992 fd->setMode (Q3FileDialog::ExistingFile);
3993 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
3994 ImagePreview *p =new ImagePreview (fd);
3995 fd->setContentsPreviewEnabled( TRUE );
3996 fd->setContentsPreview( p, p );
3997 fd->setPreviewMode( Q3FileDialog::Contents );
3998 fd->setCaption(vymName+" - " +tr("Load background image"));
3999 fd->setDir (lastImageDir);
4002 if ( fd->exec() == QDialog::Accepted )
4004 // TODO selectMapBackgroundImg in QT4 use: lastImageDir=fd->directory();
4005 lastImageDir=QDir (fd->dirPath());
4006 setMapBackgroundImage (fd->selectedFile());
4010 void VymModel::setMapBackgroundImage (const QString &fn) //FIXME missing savestate
4012 QColor oldcol=mapScene->backgroundBrush().color();
4016 QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
4018 QString ("setMapBackgroundImage (%1)").arg(col.name()),
4019 QString("Set background color of map to %1").arg(col.name()));
4022 brush.setTextureImage (QPixmap (fn));
4023 mapScene->setBackgroundBrush(brush);
4026 void VymModel::selectMapBackgroundColor() // FIXME move to ME
4028 QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
4029 if ( !col.isValid() ) return;
4030 setMapBackgroundColor( col );
4034 void VymModel::setMapBackgroundColor(QColor col) // FIXME move to ME
4036 QColor oldcol=mapScene->backgroundBrush().color();
4038 QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
4039 QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
4040 QString("Set background color of map to %1").arg(col.name()));
4041 mapScene->setBackgroundBrush(col);
4044 QColor VymModel::getMapBackgroundColor() // FIXME move to ME
4046 return mapScene->backgroundBrush().color();
4050 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint() // FIXME move to ME
4052 return linkcolorhint;
4055 QColor VymModel::getMapDefLinkColor() // FIXME move to ME
4057 return defLinkColor;
4060 void VymModel::setMapDefXLinkColor(QColor col) // FIXME move to ME
4065 QColor VymModel::getMapDefXLinkColor() // FIXME move to ME
4067 return defXLinkColor;
4070 void VymModel::setMapDefXLinkWidth (int w) // FIXME move to ME
4075 int VymModel::getMapDefXLinkWidth() // FIXME move to ME
4077 return defXLinkWidth;
4080 void VymModel::move(const double &x, const double &y)
4082 BranchObj *bo = getSelectedBranch();
4084 (selectionType()==TreeItem::Branch ||
4085 selectionType()==TreeItem::MapCenter ||
4086 selectionType()==TreeItem::Image
4089 QPointF ap(bo->getAbsPos());
4093 QString ps=qpointfToString(ap);
4094 QString s=getSelectString();
4097 s, "move "+qpointfToString(to),
4098 QString("Move %1 to %2").arg(getObjectName(bo)).arg(ps));
4106 void VymModel::moveRel (const double &x, const double &y)
4108 BranchObj *bo = getSelectedBranch();
4110 (selectionType()==TreeItem::Branch ||
4111 selectionType()==TreeItem::MapCenter ||
4112 selectionType()==TreeItem::Image
4116 QPointF rp(bo->getRelPos());
4120 QString ps=qpointfToString (bo->getRelPos());
4121 QString s=getSelectString(bo);
4124 s, "moveRel "+qpointfToString(to),
4125 QString("Move %1 to relative position %2").arg(getObjectName(bo)).arg(ps));
4126 ((OrnamentedObj*)bo)->move2RelPos (x,y);
4135 void VymModel::animate()
4137 animationTimer->stop();
4140 while (i<animObjList.size() )
4142 bo=(BranchObj*)animObjList.at(i);
4147 animObjList.removeAt(i);
4154 QItemSelection sel=selModel->selection();
4155 emit (selectionChanged(sel,sel));
4158 if (!animObjList.isEmpty()) animationTimer->start();
4162 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
4164 if (bo && bo->getDepth()>0)
4167 ap.setStart (start);
4169 ap.setTicks (animationTicks);
4170 ap.setAnimated (true);
4171 bo->setAnimation (ap);
4172 animObjList.append( bo );
4173 animationTimer->setSingleShot (true);
4174 animationTimer->start(animationInterval);
4178 void VymModel::stopAnimation (MapObj *mo)
4180 int i=animObjList.indexOf(mo);
4182 animObjList.removeAt (i);
4185 void VymModel::sendSelection()
4187 if (netstate!=Server) return;
4188 sendData (QString("select (\"%1\")").arg(selection.getSelectString()) );
4191 void VymModel::newServer()
4195 tcpServer = new QTcpServer(this);
4196 if (!tcpServer->listen(QHostAddress::Any,port)) {
4197 QMessageBox::critical(NULL, "vym server",
4198 QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
4199 //FIXME needed? we are no widget any longer... close();
4202 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
4204 cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
4207 void VymModel::connectToServer()
4210 server="salam.suse.de";
4212 clientSocket = new QTcpSocket (this);
4213 clientSocket->abort();
4214 clientSocket->connectToHost(server ,port);
4215 connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
4216 connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
4217 this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
4219 cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
4224 void VymModel::newClient()
4226 QTcpSocket *newClient = tcpServer->nextPendingConnection();
4227 connect(newClient, SIGNAL(disconnected()),
4228 newClient, SLOT(deleteLater()));
4230 cout <<"ME::newClient at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
4232 clientList.append (newClient);
4236 void VymModel::sendData(const QString &s)
4238 if (clientList.size()==0) return;
4240 // Create bytearray to send
4242 QDataStream out(&block, QIODevice::WriteOnly);
4243 out.setVersion(QDataStream::Qt_4_0);
4245 // Reserve some space for blocksize
4248 // Write sendCounter
4249 out << sendCounter++;
4254 // Go back and write blocksize so far
4255 out.device()->seek(0);
4256 quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
4260 cout << "ME::sendData bs="<<bs<<" counter="<<sendCounter<<" s="<<qPrintable(s)<<endl;
4262 for (int i=0; i<clientList.size(); ++i)
4264 //cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
4265 clientList.at(i)->write (block);
4269 void VymModel::readData ()
4271 while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
4274 cout <<"readData bytesAvail="<<clientSocket->bytesAvailable();
4278 QDataStream in(clientSocket);
4279 in.setVersion(QDataStream::Qt_4_0);
4287 cout << " t="<<qPrintable (t)<<endl;
4293 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
4295 switch (socketError) {
4296 case QAbstractSocket::RemoteHostClosedError:
4298 case QAbstractSocket::HostNotFoundError:
4299 QMessageBox::information(NULL, vymName +" Network client",
4300 "The host was not found. Please check the "
4301 "host name and port settings.");
4303 case QAbstractSocket::ConnectionRefusedError:
4304 QMessageBox::information(NULL, vymName + " Network client",
4305 "The connection was refused by the peer. "
4306 "Make sure the fortune server is running, "
4307 "and check that the host name and port "
4308 "settings are correct.");
4311 QMessageBox::information(NULL, vymName + " Network client",
4312 QString("The following error occurred: %1.")
4313 .arg(clientSocket->errorString()));
4320 void VymModel::selectMapSelectionColor()
4322 QColor col = QColorDialog::getColor( defLinkColor, NULL);
4323 setSelectionColor (col);
4326 void VymModel::setSelectionColorInt (QColor col)
4328 if ( !col.isValid() ) return;
4330 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4331 QString("setSelectionColor (%1)").arg(col.name()),
4332 QString("Set color of selection box to %1").arg(col.name())
4335 mapEditor->setSelectionColor (col);
4338 void VymModel::updateSelection()
4340 QItemSelection newsel=selModel->selection();
4341 updateSelection (newsel);
4344 void VymModel::updateSelection(const QItemSelection &)
4346 QItemSelection newsel=selModel->selection();
4348 cout << "VM::updateSelection new=";
4349 if (!newsel.indexes().isEmpty() )
4350 cout << newsel.indexes().first().row()<<"," << newsel.indexes().first().column();
4352 if (!oldsel.indexes().isEmpty() )
4353 cout << oldsel.indexes().first().row()<<"," << oldsel.indexes().first().column();
4356 //emit (selectionChanged(newsel,oldsel)); // FIXME needed?
4357 ensureSelectionVisible();
4361 void VymModel::setSelectionColor(QColor col)
4363 if ( !col.isValid() ) return;
4365 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4366 QString("setSelectionColor (%1)").arg(col.name()),
4367 QString("Set color of selection box to %1").arg(col.name())
4369 setSelectionColorInt (col);
4372 QColor VymModel::getSelectionColor()
4374 return mapEditor->getSelectionColor();
4377 void VymModel::setHideTmpMode (HideTmpMode mode)
4380 for (int i=0;i<mapCenters.count(); i++)
4381 mapCenters.at(i)->setHideTmp (mode);
4383 // FIXME needed? scene()->update();
4387 QRectF VymModel::getTotalBBox()
4390 for (int i=0;i<mapCenters.count(); i++)
4391 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
4395 //////////////////////////////////////////////
4396 // Selection related
4397 //////////////////////////////////////////////
4399 void VymModel::setSelectionModel (QItemSelectionModel *sm)
4402 cout << "VM::setSelModel selModel="<<selModel<<endl;
4405 QItemSelectionModel* VymModel::getSelectionModel()
4410 void VymModel::setSelectionBlocked (bool b)
4415 selection.unblock();
4418 bool VymModel::isSelectionBlocked()
4420 return selection.isBlocked();
4423 bool VymModel::select ()
4425 QModelIndex index=selModel->selectedIndexes().first(); // TODO no multiselections yet
4427 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
4428 return select (item->getLMO() );
4431 bool VymModel::select (const QString &s)
4433 LinkableMapObj *lmo=findObjBySelect(s);
4435 // Finally select the found object
4445 bool VymModel::select (LinkableMapObj *lmo)
4447 QItemSelection oldsel=selModel->selection();
4450 return select (lmo->getTreeItem() );
4455 bool VymModel::select (TreeItem *ti)
4459 QModelIndex ix=index(ti);
4460 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4461 ti->setLastSelectedBranch();
4462 //updateSelection(oldsel); //FIXME needed?
4468 void VymModel::unselect()
4470 selModel->clearSelection();
4473 void VymModel::reselect()
4475 selection.reselect();
4478 void VymModel::ensureSelectionVisible()
4480 LinkableMapObj *lmo=getSelectedLMO();
4481 if (lmo &&mapEditor) mapEditor->ensureVisible (lmo->getBBox() );
4485 void VymModel::selectInt (LinkableMapObj *lmo)
4487 if (selection.select(lmo))
4489 //selection.update();
4490 sendSelection (); // FIXME VM use signal
4495 void VymModel::selectNextBranchInt()
4497 // Increase number of branch
4498 LinkableMapObj *sel=getSelectedBranch();
4501 QString s=getSelectString();
4507 part=s.section(",",-1);
4509 num=part.right(part.length() - 3);
4511 s=s.left (s.length() -num.length());
4514 num=QString ("%1").arg(num.toUInt()+1);
4518 // Try to select this one
4519 if (select (s)) return;
4521 // We have no direct successor,
4522 // try to increase the parental number in order to
4523 // find a successor with same depth
4525 int d=getSelectedLMO()->getDepth();
4530 while (!found && d>0)
4532 s=s.section (",",0,d-1);
4533 // replace substring of current depth in s with "1"
4534 part=s.section(",",-1);
4536 num=part.right(part.length() - 3);
4540 // increase number of parent
4541 num=QString ("%1").arg(num.toUInt()+1);
4542 s=s.section (",",0,d-2) + ","+ typ+num;
4545 // Special case, look at orientation
4546 if (getSelectedLMO()->getOrientation()==LinkableMapObj::RightOfCenter)
4547 num=QString ("%1").arg(num.toUInt()+1);
4549 num=QString ("%1").arg(num.toUInt()-1);
4554 // pad to oldDepth, select the first branch for each depth
4555 for (i=d;i<oldDepth;i++)
4560 if ( getSelectedBranch()->countBranches()>0)
4568 // try to select the freshly built string
4576 void VymModel::selectPrevBranchInt()
4578 // Decrease number of branch
4579 if (selectionType()==TreeItem::Branch)
4581 QString s=getSelectString();
4587 part=s.section(",",-1);
4589 num=part.right(part.length() - 3);
4591 s=s.left (s.length() -num.length());
4593 int n=num.toInt()-1;
4596 num=QString ("%1").arg(n);
4599 cout <<"SP:: s0="<<s.toStdString()<<endl;
4602 // Try to select this one
4603 if (n>=0 && select (s)) return;
4605 // We have no direct precessor,
4606 // try to decrease the parental number in order to
4607 // find a precessor with same depth
4609 int d=getSelectedLMO()->getDepth();
4614 while (!found && d>0)
4616 s=s.section (",",0,d-1);
4617 // replace substring of current depth in s with "1"
4618 part=s.section(",",-1);
4620 num=part.right(part.length() - 3);
4624 // decrease number of parent
4625 num=QString ("%1").arg(num.toInt()-1);
4626 s=s.section (",",0,d-2) + ","+ typ+num;
4629 // Special case, look at orientation
4630 if (getSelectedLMO()->getOrientation()==LinkableMapObj::RightOfCenter)
4631 num=QString ("%1").arg(num.toInt()-1);
4633 num=QString ("%1").arg(num.toInt()+1);
4637 cout <<"SP:: si="<<s.toStdString()<<endl;
4639 // pad to oldDepth, select the last branch for each depth
4640 for (i=d;i<oldDepth;i++)
4644 if ( getSelectedBranch()->countBranches()>0)
4645 s+=",bo:"+ QString ("%1").arg( getSelectedBranch()->countBranches()-1 );
4652 // try to select the freshly built string
4660 void VymModel::selectUpperBranch()
4662 if (selection.isBlocked() ) return;
4664 BranchObj *bo=getSelectedBranch();
4665 if (bo && selectionType()==TreeItem::Branch)
4667 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4668 selectPrevBranchInt();
4670 if (bo->getDepth()==1)
4671 selectNextBranchInt();
4673 selectPrevBranchInt();
4677 void VymModel::selectLowerBranch()
4679 if (selection.isBlocked() ) return;
4681 BranchObj *bo=getSelectedBranch();
4682 if (bo && selectionType()==TreeItem::Branch)
4684 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4685 selectNextBranchInt();
4687 if (bo->getDepth()==1)
4688 selectPrevBranchInt();
4690 selectNextBranchInt();
4695 void VymModel::selectLeftBranch()
4697 if (selection.isBlocked() ) return;
4699 QItemSelection oldsel=selModel->selection();
4702 LinkableMapObj *sel=getSelectedBranch();
4705 if (selectionType()== TreeItem::MapCenter)
4707 QModelIndex ix=getSelectedIndex();
4708 selModel->select (index (rowCount(ix)-1,0,ix),QItemSelectionModel::ClearAndSelect );
4711 par=(BranchObj*)(sel->getParObj());
4712 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4715 if (selectionType() == TreeItem::Branch ||
4716 selectionType() == TreeItem::Image)
4718 QModelIndex ix=getSelectedIndex();
4720 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4725 if (selectionType() == TreeItem::Branch )
4727 selectLastSelectedBranch();
4732 updateSelection (oldsel);
4736 void VymModel::selectRightBranch()
4738 if (selection.isBlocked() ) return;
4740 QItemSelection oldsel=selModel->selection();
4743 LinkableMapObj *sel=getSelectedBranch();
4746 if (selectionType()== TreeItem::MapCenter)
4748 QModelIndex ix=getSelectedIndex();
4749 selModel->select (index (0,0,ix),QItemSelectionModel::ClearAndSelect );
4752 par=(BranchObj*)(sel->getParObj());
4753 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4756 if (selectionType() == TreeItem::Branch )
4758 selectLastSelectedBranch();
4764 if (selectionType() == TreeItem::Branch ||
4765 selectionType() == TreeItem::Image)
4767 QModelIndex ix=getSelectedIndex();
4769 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4773 updateSelection (oldsel);
4777 void VymModel::selectFirstBranch()
4779 TreeItem *ti=getSelectedBranchItem();
4782 TreeItem *par=ti->parent();
4784 TreeItem *ti2=par->getFirstBranch();
4788 ensureSelectionVisible();
4794 void VymModel::selectLastBranch()
4796 TreeItem *ti=getSelectedBranchItem();
4799 TreeItem *par=ti->parent();
4801 TreeItem *ti2=par->getLastBranch();
4805 ensureSelectionVisible();
4811 void VymModel::selectLastSelectedBranch()
4813 TreeItem *ti=getSelectedBranchItem();
4816 ti=ti->getLastSelectedBranch();
4817 if (ti) select (ti);
4821 void VymModel::selectParent()
4823 TreeItem *ti=getSelectedItem();
4831 ensureSelectionVisible();
4836 TreeItem::Type VymModel::selectionType()
4838 QModelIndexList list=selModel->selectedIndexes();
4839 if (list.isEmpty()) return TreeItem::Undefined;
4840 TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
4841 return ti->getType();
4845 LinkableMapObj* VymModel::getSelectedLMO()
4847 QModelIndexList list=selModel->selectedIndexes();
4848 if (!list.isEmpty() )
4850 TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
4851 TreeItem::Type type=ti->getType();
4852 if (type ==TreeItem::Branch || type==TreeItem::MapCenter || type==TreeItem::Image)
4854 return ti->getLMO();
4860 BranchObj* VymModel::getSelectedBranch()
4862 TreeItem *ti = getSelectedBranchItem();
4864 return (BranchObj*)ti->getLMO();
4869 TreeItem* VymModel::getSelectedBranchItem()
4871 QModelIndexList list=selModel->selectedIndexes();
4872 if (!list.isEmpty() )
4874 TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
4875 TreeItem::Type type=ti->getType();
4876 if (type ==TreeItem::Branch || type==TreeItem::MapCenter)
4882 TreeItem* VymModel::getSelectedItem()
4884 // FIXME this may not only be branch, but also float etc...
4885 BranchObj* bo=getSelectedBranch();
4886 if (bo) return bo->getTreeItem(); // FIXME VM get directly from treemodl
4890 QModelIndex VymModel::getSelectedIndex()
4892 QModelIndexList list=selModel->selectedIndexes();
4893 if (list.isEmpty() )
4894 return QModelIndex();
4896 return list.first();
4899 FloatImageObj* VymModel::getSelectedFloatImage()
4901 return selection.getFloatImage();
4904 QString VymModel::getSelectString ()
4906 LinkableMapObj *lmo=getSelectedLMO();
4908 return getSelectString(lmo);
4913 QString VymModel::getSelectString (LinkableMapObj *lmo) // FIXME VM needs to use TreeModel
4917 if (typeid(*lmo)==typeid(BranchObj) ||
4918 typeid(*lmo)==typeid(MapCenterObj) )
4920 LinkableMapObj *par=lmo->getParObj();
4923 if (lmo->getDepth() ==1)
4924 // Mainbranch, return
4925 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4927 // Branch, call myself recursively
4928 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
4932 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
4933 if (i>=0) s=QString("mc:%1").arg(i);