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";
56 rootItem->setModel (this);
62 // cout << "Destr VymModel\n";
63 autosaveTimer->stop();
64 fileChangedTimer->stop();
68 void VymModel::clear()
70 selModel->clearSelection();
73 while (!mapCenters.isEmpty()) // FIXME-2 VM needs to be in treemodel only...
74 delete mapCenters.takeFirst();
76 QModelIndex ri=index(rootItem);
77 //removeRows (0, rowCount(ri),ri); // FIXME-2 here should be at least a beginRemoveRows...
80 void VymModel::init ()
82 // We should have at least one map center to start with
83 // addMapCenter(); FIXME-2 VM create this in MapEditor as long as model is part of that
88 // Also no scene yet (should not be needed anyway) FIXME-3 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);
154 // addMapCenter(); FIXME-2 VM create this in MapEditor until BO and MCO are independent of scene
158 void VymModel::makeTmpDirectories()
160 // Create unique temporary directories
161 tmpMapDir = tmpVymDir+QString("/model-%1").arg(mapNum);
162 histPath = tmpMapDir+"/history";
168 MapEditor* VymModel::getMapEditor() // FIXME-2 VM better return favourite editor here
173 bool VymModel::isRepositionBlocked()
175 return blockReposition;
178 void VymModel::updateActions() // FIXME-2 maybe don't update if blockReposition is set
180 cout << "VymModel::updateActions \n";
181 // Tell mainwindow to update states of actions
182 mainWindow->updateActions();
187 QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
189 // tmpdir temporary directory to which data will be written
190 // prefix mapname, which will be appended to images etc.
191 // writeflags Only write flags for "real" save of map, not undo
192 // offset offset of bbox of whole map in scene.
193 // Needed for XML export
202 case LinkableMapObj::Line:
205 case LinkableMapObj::Parabel:
208 case LinkableMapObj::PolyLine:
212 ls="StylePolyParabel";
216 QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
218 if (linkcolorhint==LinkableMapObj::HeadingColor)
219 colhint=xml.attribut("linkColorHint","HeadingColor");
221 QString mapAttr=xml.attribut("version",vymVersion);
223 mapAttr+= xml.attribut("author",author) +
224 xml.attribut("comment",comment) +
225 xml.attribut("date",getDate()) +
226 xml.attribut("countBranches", QString().number(countBranches())) +
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 // FIXME-2 this can be done local to vymmodel maybe...
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();// FIXME-2 this can be done local to vymmodel maybe...
244 // Build xml recursivly
245 if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
246 // Save all mapcenters as complete map, if saveSel not set
247 s+=saveTreeToDir(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 QString VymModel::saveTreeToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
277 for (int i=0; i<mapCenters.count(); i++)
278 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
282 void VymModel::setFilePath(QString fpath, QString destname)
284 if (fpath.isEmpty() || fpath=="")
291 filePath=fpath; // becomes absolute path
292 fileName=fpath; // gets stripped of path
293 destPath=destname; // needed for vymlinks and during load to reset fileChangedTime
295 // If fpath is not an absolute path, complete it
296 filePath=QDir(fpath).absPath();
297 fileDir=filePath.left (1+filePath.findRev ("/"));
299 // Set short name, too. Search from behind:
300 int i=fileName.findRev("/");
301 if (i>=0) fileName=fileName.remove (0,i+1);
303 // Forget the .vym (or .xml) for name of map
304 mapName=fileName.left(fileName.findRev(".",-1,true) );
308 void VymModel::setFilePath(QString fpath)
310 setFilePath (fpath,fpath);
313 QString VymModel::getFilePath()
318 QString VymModel::getFileName()
323 QString VymModel::getMapName()
328 QString VymModel::getDestPath()
333 ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
335 ErrorCode err=success;
337 parseBaseHandler *handler;
341 case VymMap: handler=new parseVYMHandler; break;
342 case FreemindMap : handler=new parseFreemindHandler; break;
344 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
345 "Unknown FileType in VymModel::load()");
349 bool zipped_org=zipped;
353 selModel->clearSelection();
354 // FIXME-2 VM not needed??? model->setMapEditor(this);
355 // (map state is set later at end of load...)
358 BranchObj *bo=getSelectedBranch();
359 if (!bo) return aborted;
360 if (lmode==ImportAdd)
361 saveStateChangingPart(
364 QString("addMapInsert (%1)").arg(fname),
365 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
367 saveStateChangingPart(
370 QString("addMapReplace(%1)").arg(fname),
371 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
375 // Create temporary directory for packing
377 QString tmpZipDir=makeTmpDir (ok,"vym-pack");
380 QMessageBox::critical( 0, tr( "Critical Load Error" ),
381 tr("Couldn't create temporary directory before load\n"));
386 err=unzipDir (tmpZipDir,fname);
396 // Look for mapname.xml
397 xmlfile= fname.left(fname.findRev(".",-1,true));
398 xmlfile=xmlfile.section( '/', -1 );
399 QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
400 if (!mfile.exists() )
402 // mapname.xml does not exist, well,
403 // maybe someone renamed the mapname.vym file...
404 // Try to find any .xml in the toplevel
405 // directory of the .vym file
406 QStringList flist=QDir (tmpZipDir).entryList("*.xml");
407 if (flist.count()==1)
409 // Only one entry, take this one
410 xmlfile=tmpZipDir + "/"+flist.first();
413 for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it )
414 *it=tmpZipDir + "/" + *it;
415 // TODO Multiple entries, load all (but only the first one into this ME)
416 //mainWindow->fileLoadFromTmp (flist);
417 //returnCode=1; // Silently forget this attempt to load
418 qWarning ("MainWindow::load (fn) multimap found...");
421 if (flist.isEmpty() )
423 QMessageBox::critical( 0, tr( "Critical Load Error" ),
424 tr("Couldn't find a map (*.xml) in .vym archive.\n"));
427 } //file doesn't exist
429 xmlfile=mfile.name();
432 QFile file( xmlfile);
434 // I am paranoid: file should exist anyway
435 // according to check in mainwindow.
438 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
439 tr(QString("Couldn't open map %1").arg(file.name())));
443 bool blockSaveStateOrg=blockSaveState;
444 blockReposition=true;
446 QXmlInputSource source( file);
447 QXmlSimpleReader reader;
448 reader.setContentHandler( handler );
449 reader.setErrorHandler( handler );
450 handler->setModel ( this);
453 // We need to set the tmpDir in order to load files with rel. path
458 tmpdir=fname.left(fname.findRev("/",-1));
459 handler->setTmpDir (tmpdir);
460 handler->setInputFile (file.name());
461 handler->setLoadMode (lmode);
462 bool ok = reader.parse( source );
463 blockReposition=false;
464 blockSaveState=blockSaveStateOrg;
468 reposition(); // FIXME-2 VM reposition the view instead...
475 autosaveTimer->stop();
478 // Reset timestamp to check for later updates of file
479 fileChangedTime=QFileInfo (destPath).lastModified();
482 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
483 tr( handler->errorProtocol() ) );
485 // Still return "success": the map maybe at least
486 // partially read by the parser
491 removeDir (QDir(tmpZipDir));
493 // Restore original zip state
500 ErrorCode VymModel::save (const SaveMode &savemode)
504 QString safeFilePath;
506 ErrorCode err=success;
510 mapFileName=mapName+".xml";
512 // use name given by user, even if he chooses .doc
513 mapFileName=fileName;
515 // Look, if we should zip the data:
518 QMessageBox mb( vymName,
519 tr("The map %1\ndid not use the compressed "
520 "vym file format.\nWriting it uncompressed will also write images \n"
521 "and flags and thus may overwrite files in the "
522 "given directory\n\nDo you want to write the map").arg(filePath),
523 QMessageBox::Warning,
524 QMessageBox::Yes | QMessageBox::Default,
526 QMessageBox::Cancel | QMessageBox::Escape);
527 mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
528 mb.setButtonText( QMessageBox::No, tr("uncompressed") );
529 mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
532 case QMessageBox::Yes:
533 // save compressed (default file format)
536 case QMessageBox::No:
540 case QMessageBox::Cancel:
547 // First backup existing file, we
548 // don't want to add to old zip archives
552 if ( settings.value ("/mapeditor/writeBackupFile").toBool())
554 QString backupFileName(destPath + "~");
555 QFile backupFile(backupFileName);
556 if (backupFile.exists() && !backupFile.remove())
558 QMessageBox::warning(0, tr("Save Error"),
559 tr("%1\ncould not be removed before saving").arg(backupFileName));
561 else if (!f.rename(backupFileName))
563 QMessageBox::warning(0, tr("Save Error"),
564 tr("%1\ncould not be renamed before saving").arg(destPath));
571 // Create temporary directory for packing
573 tmpZipDir=makeTmpDir (ok,"vym-zip");
576 QMessageBox::critical( 0, tr( "Critical Load Error" ),
577 tr("Couldn't create temporary directory before save\n"));
581 safeFilePath=filePath;
582 setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
585 // Create mapName and fileDir
586 makeSubDirs (fileDir);
589 if (savemode==CompleteMap || selection.isEmpty())
592 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
595 autosaveTimer->stop();
600 if (selectionType()==TreeItem::Image)
603 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),getSelectedBranch());
604 // TODO take care of multiselections
607 if (!saveStringToDisk(fileDir+mapFileName,saveFile))
610 qWarning ("ME::saveStringToDisk failed!");
616 if (err==success) err=zipDir (tmpZipDir,destPath);
619 removeDir (QDir(tmpZipDir));
621 // Restore original filepath outside of tmp zip dir
622 setFilePath (safeFilePath);
626 fileChangedTime=QFileInfo (destPath).lastModified();
630 void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)
632 QString pathDir=path.left(path.findRev("/"));
638 // We need to parse saved XML data
639 parseVYMHandler handler;
640 QXmlInputSource source( file);
641 QXmlSimpleReader reader;
642 reader.setContentHandler( &handler );
643 reader.setErrorHandler( &handler );
644 handler.setModel ( this);
645 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
646 if (undoSel.isEmpty())
650 handler.setLoadMode (NewMap);
654 handler.setLoadMode (ImportReplace);
656 blockReposition=true;
657 bool ok = reader.parse( source );
658 blockReposition=false;
661 // This should never ever happen
662 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
663 handler.errorProtocol());
666 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
669 void VymModel::addMapInsertInt (const QString &path, int pos)
671 BranchObj *sel=getSelectedBranch();
674 QString pathDir=path.left(path.findRev("/"));
680 // We need to parse saved XML data
681 parseVYMHandler handler;
682 QXmlInputSource source( file);
683 QXmlSimpleReader reader;
684 reader.setContentHandler( &handler );
685 reader.setErrorHandler( &handler );
686 handler.setModel (this);
687 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
688 handler.setLoadMode (ImportAdd);
689 blockReposition=true;
690 bool ok = reader.parse( source );
691 blockReposition=false;
694 // This should never ever happen
695 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
696 handler.errorProtocol());
698 if (sel->getDepth()>0)
699 sel->getLastBranch()->linkTo (sel,pos);
701 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
705 FloatImageObj* VymModel::loadFloatImageInt (QString fn)
707 TreeItem *fi=createImage();
710 FloatImageObj *fio= ((FloatImageObj*)fi->getLMO());
718 void VymModel::loadFloatImage ()
720 BranchObj *bo=getSelectedBranch();
724 Q3FileDialog *fd=new Q3FileDialog( NULL);
725 fd->setMode (Q3FileDialog::ExistingFiles);
726 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
727 ImagePreview *p =new ImagePreview (fd);
728 fd->setContentsPreviewEnabled( TRUE );
729 fd->setContentsPreview( p, p );
730 fd->setPreviewMode( Q3FileDialog::Contents );
731 fd->setCaption(vymName+" - " +tr("Load image"));
732 fd->setDir (lastImageDir);
735 if ( fd->exec() == QDialog::Accepted )
737 // TODO loadFIO in QT4 use: lastImageDir=fd->directory();
738 lastImageDir=QDir (fd->dirPath());
741 for (int j=0; j<fd->selectedFiles().count(); j++)
743 s=fd->selectedFiles().at(j);
744 fio=loadFloatImageInt (s);
747 (LinkableMapObj*)fio,
750 QString ("loadImage (%1)").arg(s ),
751 QString("Add image %1 to %2").arg(s).arg(getObjectName(bo))
754 // TODO loadFIO error handling
755 qWarning ("Failed to load "+s);
763 void VymModel::saveFloatImageInt (FloatImageObj *fio, const QString &type, const QString &fn)
768 void VymModel::saveFloatImage ()
770 FloatImageObj *fio=selection.getFloatImage();
773 QFileDialog *fd=new QFileDialog( NULL);
774 fd->setFilters (imageIO.getFilters());
775 fd->setCaption(vymName+" - " +tr("Save image"));
776 fd->setFileMode( QFileDialog::AnyFile );
777 fd->setDirectory (lastImageDir);
778 // fd->setSelection (fio->getOriginalFilename());
782 if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
784 fn=fd->selectedFiles().at(0);
785 if (QFile (fn).exists() )
787 QMessageBox mb( vymName,
788 tr("The file %1 exists already.\n"
789 "Do you want to overwrite it?").arg(fn),
790 QMessageBox::Warning,
791 QMessageBox::Yes | QMessageBox::Default,
792 QMessageBox::Cancel | QMessageBox::Escape,
793 QMessageBox::NoButton );
795 mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
796 mb.setButtonText( QMessageBox::No, tr("Cancel"));
799 case QMessageBox::Yes:
802 case QMessageBox::Cancel:
809 saveFloatImageInt (fio,fd->selectedFilter(),fn );
816 void VymModel::importDirInt(BranchObj *dst, QDir d)
818 BranchObj *bo=getSelectedBranch();
821 // Traverse directories
822 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
823 QFileInfoList list = d.entryInfoList();
826 for (int i = 0; i < list.size(); ++i)
829 if (fi.fileName() != "." && fi.fileName() != ".." )
832 bo=dst->getLastBranch();
833 bo->setHeading (fi.fileName() );
834 bo->setColor (QColor("blue"));
836 if ( !d.cd(fi.fileName()) )
837 QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
840 // Recursively add subdirs
847 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
848 list = d.entryInfoList();
850 for (int i = 0; i < list.size(); ++i)
854 bo=dst->getLastBranch();
855 bo->setHeading (fi.fileName() );
856 bo->setColor (QColor("black"));
857 if (fi.fileName().right(4) == ".vym" )
858 bo->setVymLink (fi.filePath());
863 void VymModel::importDirInt (const QString &s)
865 BranchObj *bo=getSelectedBranch();
868 saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
875 void VymModel::importDir()
877 BranchObj *bo=getSelectedBranch();
881 filters <<"VYM map (*.vym)";
882 QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
883 fd->setMode (QFileDialog::DirectoryOnly);
884 fd->setFilters (filters);
885 fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
889 if ( fd->exec() == QDialog::Accepted )
891 importDirInt (fd->selectedFile() );
893 //FIXME-3 VM needed? scene()->update();
899 void VymModel::autosave()
904 cout << "VymModel::autosave rejected due to missing filePath\n";
907 QDateTime now=QDateTime().currentDateTime();
909 // Disable autosave, while we have gone back in history
910 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
911 if (redosAvail>0) return;
913 // Also disable autosave for new map without filename
914 if (filePath.isEmpty()) return;
917 if (mapUnsaved &&mapChanged && settings.value ("/mainwindow/autosave/use",true).toBool() )
919 if (QFileInfo(filePath).lastModified()<=fileChangedTime)
920 mainWindow->fileSave (this);
923 cout <<" ME::autosave rejected, file on disk is newer than last save.\n";
928 void VymModel::fileChanged()
930 // Check if file on disk has changed meanwhile
931 if (!filePath.isEmpty())
933 QDateTime tmod=QFileInfo (filePath).lastModified();
934 if (tmod>fileChangedTime)
936 // FIXME-2 VM switch to current mapeditor and finish lineedits...
937 QMessageBox mb( vymName,
938 tr("The file of the map on disk has changed:\n\n"
939 " %1\n\nDo you want to reload that map with the new file?").arg(filePath),
940 QMessageBox::Question,
942 QMessageBox::Cancel | QMessageBox::Default,
943 QMessageBox::NoButton );
945 mb.setButtonText( QMessageBox::Yes, tr("Reload"));
946 mb.setButtonText( QMessageBox::No, tr("Ignore"));
949 case QMessageBox::Yes:
951 load (filePath,NewMap,fileType);
952 case QMessageBox::Cancel:
953 fileChangedTime=tmod; // allow autosave to overwrite newer file!
959 bool VymModel::isDefault()
964 void VymModel::makeDefault()
970 bool VymModel::hasChanged()
975 void VymModel::setChanged()
977 cout << "VM::setChanged()\n";
980 autosaveTimer->start(settings.value("/mainwindow/autosave/ms/",300000).toInt());
981 cout <<" timer started with "<<settings.value("/mainwindow/autosave/ms/",300000).toInt()<<endl;
990 QString VymModel::getObjectName (const LinkableMapObj *lmo)
993 if (!lmo) return QString("Error: NULL has no name!");
995 if ((typeid(*lmo) == typeid(BranchObj) ||
996 typeid(*lmo) == typeid(MapCenterObj)))
999 s=(((BranchObj*)lmo)->getHeading());
1000 if (s=="") s="unnamed";
1001 return QString("branch (%1)").arg(s);
1003 if ((typeid(*lmo) == typeid(FloatImageObj) ))
1004 return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
1005 return QString("Unknown type has no name!");
1008 void VymModel::redo()
1010 // Can we undo at all?
1011 if (redosAvail<1) return;
1013 bool blockSaveStateOrg=blockSaveState;
1014 blockSaveState=true;
1018 if (undosAvail<stepsTotal) undosAvail++;
1020 if (curStep>stepsTotal) curStep=1;
1021 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1022 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1023 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1024 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1025 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1026 QString version=undoSet.readEntry ("/history/version");
1028 /* TODO Maybe check for version, if we save the history
1029 if (!checkVersion(version))
1030 QMessageBox::warning(0,tr("Warning"),
1031 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1034 // Find out current undo directory
1035 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1039 cout << "VymModel::redo() begin\n";
1040 cout << " undosAvail="<<undosAvail<<endl;
1041 cout << " redosAvail="<<redosAvail<<endl;
1042 cout << " curStep="<<curStep<<endl;
1043 cout << " ---------------------------"<<endl;
1044 cout << " comment="<<comment.toStdString()<<endl;
1045 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1046 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1047 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1048 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1049 cout << " ---------------------------"<<endl<<endl;
1052 // select object before redo
1053 if (!redoSelection.isEmpty())
1054 select (redoSelection);
1057 parseAtom (redoCommand);
1060 blockSaveState=blockSaveStateOrg;
1062 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1063 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1064 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1065 undoSet.writeSettings(histPath);
1067 mainWindow->updateHistory (undoSet);
1070 /* TODO remove testing
1071 cout << "ME::redo() end\n";
1072 cout << " undosAvail="<<undosAvail<<endl;
1073 cout << " redosAvail="<<redosAvail<<endl;
1074 cout << " curStep="<<curStep<<endl;
1075 cout << " ---------------------------"<<endl<<endl;
1081 bool VymModel::isRedoAvailable()
1083 if (undoSet.readNumEntry("/history/redosAvail",0)>0)
1089 void VymModel::undo()
1091 // Can we undo at all?
1092 if (undosAvail<1) return;
1094 mainWindow->statusMessage (tr("Autosave disabled during undo."));
1096 bool blockSaveStateOrg=blockSaveState;
1097 blockSaveState=true;
1099 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1100 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1101 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1102 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1103 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1104 QString version=undoSet.readEntry ("/history/version");
1106 /* TODO Maybe check for version, if we save the history
1107 if (!checkVersion(version))
1108 QMessageBox::warning(0,tr("Warning"),
1109 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1112 // Find out current undo directory
1113 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1115 // select object before undo
1116 if (!undoSelection.isEmpty())
1117 select (undoSelection);
1121 cout << "VymModel::undo() begin\n";
1122 cout << " undosAvail="<<undosAvail<<endl;
1123 cout << " redosAvail="<<redosAvail<<endl;
1124 cout << " curStep="<<curStep<<endl;
1125 cout << " ---------------------------"<<endl;
1126 cout << " comment="<<comment.toStdString()<<endl;
1127 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1128 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1129 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1130 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1131 cout << " ---------------------------"<<endl<<endl;
1133 parseAtom (undoCommand);
1138 if (curStep<1) curStep=stepsTotal;
1142 blockSaveState=blockSaveStateOrg;
1143 /* TODO remove testing
1144 cout << "VymModel::undo() end\n";
1145 cout << " undosAvail="<<undosAvail<<endl;
1146 cout << " redosAvail="<<redosAvail<<endl;
1147 cout << " curStep="<<curStep<<endl;
1148 cout << " ---------------------------"<<endl<<endl;
1151 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1152 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1153 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1154 undoSet.writeSettings(histPath);
1156 mainWindow->updateHistory (undoSet);
1159 ensureSelectionVisible();
1162 bool VymModel::isUndoAvailable()
1164 if (undoSet.readNumEntry("/history/undosAvail",0)>0)
1170 void VymModel::gotoHistoryStep (int i)
1172 // Restore variables
1173 int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
1174 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
1176 if (i<0) i=undosAvail+redosAvail;
1178 // Clicking above current step makes us undo things
1181 for (int j=0; j<undosAvail-i; j++) undo();
1184 // Clicking below current step makes us redo things
1186 for (int j=undosAvail; j<i; j++)
1188 if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
1192 // And ignore clicking the current row ;-)
1196 QString VymModel::getHistoryPath()
1198 QString histName(QString("history-%1").arg(curStep));
1199 return (tmpMapDir+"/"+histName);
1202 void VymModel::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
1204 sendData(redoCom); //FIXME-3 testing
1209 if (blockSaveState) return;
1211 if (debug) cout << "ME::saveState() for "<<qPrintable (mapName)<<endl;
1213 // Find out current undo directory
1214 if (undosAvail<stepsTotal) undosAvail++;
1216 if (curStep>stepsTotal) curStep=1;
1218 QString backupXML="";
1219 QString histDir=getHistoryPath();
1220 QString bakMapPath=histDir+"/map.xml";
1222 // Create histDir if not available
1225 makeSubDirs (histDir);
1227 // Save depending on how much needs to be saved
1229 backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
1231 QString undoCommand="";
1232 if (savemode==UndoCommand)
1234 undoCommand=undoCom;
1236 else if (savemode==PartOfMap )
1238 undoCommand=undoCom;
1239 undoCommand.replace ("PATH",bakMapPath);
1242 if (!backupXML.isEmpty())
1243 // Write XML Data to disk
1244 saveStringToDisk (bakMapPath,backupXML);
1246 // We would have to save all actions in a tree, to keep track of
1247 // possible redos after a action. Possible, but we are too lazy: forget about redos.
1250 // Write the current state to disk
1251 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1252 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1253 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1254 undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
1255 undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
1256 undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
1257 undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
1258 undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
1259 undoSet.setEntry (QString("/history/version"),vymVersion);
1260 undoSet.writeSettings(histPath);
1264 // TODO remove after testing
1265 //cout << " into="<< histPath.toStdString()<<endl;
1266 cout << " stepsTotal="<<stepsTotal<<
1267 ", undosAvail="<<undosAvail<<
1268 ", redosAvail="<<redosAvail<<
1269 ", curStep="<<curStep<<endl;
1270 cout << " ---------------------------"<<endl;
1271 cout << " comment="<<comment.toStdString()<<endl;
1272 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1273 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1274 cout << " redoCom="<<redoCom.toStdString()<<endl;
1275 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1276 if (saveSel) cout << " saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
1277 cout << " ---------------------------"<<endl;
1280 mainWindow->updateHistory (undoSet);
1286 void VymModel::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
1288 // save the selected part of the map, Undo will replace part of map
1289 QString undoSelection="";
1291 undoSelection=getSelectString(undoSel);
1293 qWarning ("VymModel::saveStateChangingPart no undoSel given!");
1294 QString redoSelection="";
1296 redoSelection=getSelectString(undoSel);
1298 qWarning ("VymModel::saveStateChangingPart no redoSel given!");
1301 saveState (PartOfMap,
1302 undoSelection, "addMapReplace (\"PATH\")",
1308 void VymModel::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
1312 qWarning ("VymModel::saveStateRemovingPart no redoSel given!");
1315 QString undoSelection=getSelectString (redoSel->getParObj());
1316 QString redoSelection=getSelectString(redoSel);
1317 if (typeid(*redoSel) == typeid(BranchObj) )
1319 // save the selected branch of the map, Undo will insert part of map
1320 saveState (PartOfMap,
1321 undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
1322 redoSelection, "delete ()",
1329 void VymModel::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment)
1331 // "Normal" savestate: save commands, selections and comment
1332 // so just save commands for undo and redo
1333 // and use current selection
1335 QString redoSelection="";
1336 if (redoSel) redoSelection=getSelectString(redoSel);
1337 QString undoSelection="";
1338 if (undoSel) undoSelection=getSelectString(undoSel);
1340 saveState (UndoCommand,
1347 void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment)
1349 // "Normal" savestate: save commands, selections and comment
1350 // so just save commands for undo and redo
1351 // and use current selection
1352 saveState (UndoCommand,
1359 void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment)
1361 // "Normal" savestate applied to model (no selection needed):
1362 // save commands and comment
1363 saveState (UndoCommand,
1371 QGraphicsScene* VymModel::getScene ()
1376 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
1378 LinkableMapObj *lmo;
1380 for (int i=0;i<mapCenters.count(); i++)
1382 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
1383 if (lmo) return lmo;
1388 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
1390 LinkableMapObj *lmo;
1396 part=s.section(",",0,0);
1398 num=part.right(part.length() - 3);
1399 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
1400 return mapCenters.at(num.toInt() );
1403 for (int i=0; i<mapCenters.count(); i++)
1405 lmo=mapCenters.at(i)->findObjBySelect(s);
1406 if (lmo) return lmo;
1411 LinkableMapObj* VymModel::findID (const QString &s)
1413 LinkableMapObj *lmo;
1414 for (int i=0; i<mapCenters.count(); i++)
1416 lmo=mapCenters.at(i)->findID (s);
1417 if (lmo) return lmo;
1422 //////////////////////////////////////////////
1424 //////////////////////////////////////////////
1425 void VymModel::setVersion (const QString &s)
1430 void VymModel::setAuthor (const QString &s)
1433 QString ("setMapAuthor (\"%1\")").arg(author),
1434 QString ("setMapAuthor (\"%1\")").arg(s),
1435 QString ("Set author of map to \"%1\"").arg(s)
1441 QString VymModel::getAuthor()
1446 void VymModel::setComment (const QString &s)
1449 QString ("setMapComment (\"%1\")").arg(comment),
1450 QString ("setMapComment (\"%1\")").arg(s),
1451 QString ("Set comment of map")
1457 QString VymModel::getComment ()
1462 QString VymModel::getDate ()
1464 return QDate::currentDate().toString ("yyyy-MM-dd");
1467 int VymModel::countBranches() // FIXME-2 Optimize this: use internal counter instead of going through whole map each time...
1471 TreeItem *prev=NULL;
1483 void VymModel::setHeading(const QString &s)
1485 BranchObj *sel=getSelectedBranch();
1490 "setHeading (\""+sel->getHeading()+"\")",
1492 "setHeading (\""+s+"\")",
1493 QString("Set heading of %1 to \"%2\"").arg(getObjectName(sel)).arg(s) );
1494 sel->setHeading(s );
1495 /* FIXME-3 testing only
1497 TreeItem *ti=getSelectedItem();
1501 //FIXME-3 VM ix is wrong ModelIndex below, ix2 is (hopefully) correct:
1502 //QModelIndex ix=index( ti->row(), ti->column(), index (0,0,QModelIndex()) );
1503 //FIXME-3 VM testing only cout <<"VM::setHeading s="<<s.toStdString()<<" ti="<<ti<<" r,c=("<<ti->row()<<","<<ti->column()<<")"<<endl;
1504 QModelIndex ix2=index (ti);
1505 emit (dataChanged ( ix2,ix2));
1509 cout << "Warning: VM::setHeading ti==NULL\n";
1512 //cout <<" (r,c)=("<<ix2.row()<<","<<ix2.column()<<")"<<endl;
1516 ensureSelectionVisible();
1520 BranchObj* VymModel::findText (QString s, bool cs)
1523 QTextDocument::FindFlags flags=0;
1524 if (cs) flags=QTextDocument::FindCaseSensitively;
1527 { // Nothing found or new find process
1529 // nothing found, start again
1533 next (findCurrent,findPrevious,d);
1535 bool searching=true;
1536 bool foundNote=false;
1537 while (searching && !EOFind)
1541 // Searching in Note
1542 if (findCurrent->getNote().contains(s,cs))
1544 select (findCurrent);
1546 if (getSelectedBranch()!=itFind)
1549 ensureSelectionVisible();
1552 if (textEditor->findText(s,flags))
1558 // Searching in Heading
1559 if (searching && findCurrent->getHeading().contains (s,cs) )
1561 select(findCurrent);
1567 if (!next(findCurrent,findPrevious,d) )
1570 //cout <<"still searching... "<<qPrintable( itFind->getHeading())<<endl;
1573 return getSelectedBranch();
1578 void VymModel::findReset()
1579 { // Necessary if text to find changes during a find process
1587 void VymModel::setScene (QGraphicsScene *s)
1589 mapScene=s; // FIXME-2 VM should not be necessary anymore, move all occurences to MapEditor
1590 //init(); // Here we have a mapScene set,
1591 // which is (still) needed to create MapCenters
1594 void VymModel::setURL(const QString &url)
1596 BranchObj *bo=getSelectedBranch();
1599 QString oldurl=bo->getURL();
1603 QString ("setURL (\"%1\")").arg(oldurl),
1605 QString ("setURL (\"%1\")").arg(url),
1606 QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
1611 ensureSelectionVisible();
1615 QString VymModel::getURL()
1617 BranchObj *bo=getSelectedBranch();
1619 return bo->getURL();
1624 QStringList VymModel::getURLs() // FIXME-1 first, next moved to vymmodel
1626 return QStringList();
1629 BranchObj *bo=getSelectedBranch();
1635 if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
1643 void VymModel::linkFloatImageTo(const QString &dstString)
1645 FloatImageObj *fio=selection.getFloatImage();
1648 BranchObj *dst=(BranchObj*)findObjBySelect(dstString);
1649 if (dst && (typeid(*dst)==typeid (BranchObj) ||
1650 typeid(*dst)==typeid (MapCenterObj)))
1652 LinkableMapObj *dstPar=dst->getParObj();
1653 QString parString=getSelectString(dstPar);
1654 QString fioPreSelectString=getSelectString(fio);
1655 QString fioPreParentSelectString=getSelectString (fio->getParObj());
1656 ((BranchObj*)dst)->addFloatImage (fio);
1657 selection.unselect();
1658 ((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
1659 fio=((BranchObj*)dst)->getLastFloatImage();
1662 selection.select(fio);
1664 getSelectString(fio),
1665 QString("linkTo (\"%1\")").arg(fioPreParentSelectString),
1667 QString ("linkTo (\"%1\")").arg(dstString),
1668 QString ("Link floatimage to %1").arg(getObjectName(dst)));
1674 void VymModel::setFrameType(const FrameObj::FrameType &t)
1676 BranchObj *bo=getSelectedBranch();
1679 QString s=bo->getFrameTypeName();
1680 bo->setFrameType (t);
1681 saveState (bo, QString("setFrameType (\"%1\")").arg(s),
1682 bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
1688 void VymModel::setFrameType(const QString &s)
1690 BranchObj *bo=getSelectedBranch();
1693 saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
1694 bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
1695 bo->setFrameType (s);
1701 void VymModel::setFramePenColor(const QColor &c)
1703 BranchObj *bo=getSelectedBranch();
1706 saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
1707 bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
1708 bo->setFramePenColor (c);
1712 void VymModel::setFrameBrushColor(const QColor &c)
1714 BranchObj *bo=getSelectedBranch();
1717 saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
1718 bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
1719 bo->setFrameBrushColor (c);
1723 void VymModel::setFramePadding (const int &i)
1725 BranchObj *bo=getSelectedBranch();
1728 saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
1729 bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
1730 bo->setFramePadding (i);
1736 void VymModel::setFrameBorderWidth(const int &i)
1738 BranchObj *bo=getSelectedBranch();
1741 saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
1742 bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
1743 bo->setFrameBorderWidth (i);
1749 void VymModel::setIncludeImagesVer(bool b)
1751 BranchObj *bo=getSelectedBranch();
1754 QString u= b ? "false" : "true";
1755 QString r=!b ? "false" : "true";
1759 QString("setIncludeImagesVertically (%1)").arg(u),
1761 QString("setIncludeImagesVertically (%1)").arg(r),
1762 QString("Include images vertically in %1").arg(getObjectName(bo))
1764 bo->setIncludeImagesVer(b);
1769 void VymModel::setIncludeImagesHor(bool b)
1771 BranchObj *bo=getSelectedBranch();
1774 QString u= b ? "false" : "true";
1775 QString r=!b ? "false" : "true";
1779 QString("setIncludeImagesHorizontally (%1)").arg(u),
1781 QString("setIncludeImagesHorizontally (%1)").arg(r),
1782 QString("Include images horizontally in %1").arg(getObjectName(bo))
1784 bo->setIncludeImagesHor(b);
1789 void VymModel::setHideLinkUnselected (bool b)
1791 LinkableMapObj *sel=getSelectedLMO();
1793 (selectionType() == TreeItem::Branch ||
1794 selectionType() == TreeItem::MapCenter ||
1795 selectionType() == TreeItem::Image ))
1797 QString u= b ? "false" : "true";
1798 QString r=!b ? "false" : "true";
1802 QString("setHideLinkUnselected (%1)").arg(u),
1804 QString("setHideLinkUnselected (%1)").arg(r),
1805 QString("Hide link of %1 if unselected").arg(getObjectName(sel))
1807 sel->setHideLinkUnselected(b);
1811 void VymModel::setHideExport(bool b)
1813 BranchObj *bo=getSelectedBranch();
1816 bo->setHideInExport (b);
1817 QString u= b ? "false" : "true";
1818 QString r=!b ? "false" : "true";
1822 QString ("setHideExport (%1)").arg(u),
1824 QString ("setHideExport (%1)").arg(r),
1825 QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bo)).arg (r)
1830 // FIXME-3 VM needed? scene()->update();
1834 void VymModel::toggleHideExport()
1836 BranchObj *bo=getSelectedBranch();
1838 setHideExport ( !bo->hideInExport() );
1842 void VymModel::copy()
1844 LinkableMapObj *sel=getSelectedLMO();
1846 (selectionType() == TreeItem::Branch ||
1847 selectionType() == TreeItem::MapCenter ||
1848 selectionType() == TreeItem::Image ))
1850 if (redosAvail == 0)
1853 QString s=getSelectString(sel);
1854 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel );
1855 curClipboard=curStep;
1858 // Copy also to global clipboard, because we are at last step in history
1859 QString bakMapName(QString("history-%1").arg(curStep));
1860 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1861 copyDir (bakMapDir,clipboardDir );
1863 clipboardEmpty=false;
1869 void VymModel::pasteNoSave(const int &n)
1871 bool old=blockSaveState;
1872 blockSaveState=true;
1873 bool zippedOrg=zipped;
1874 if (redosAvail > 0 || n!=0)
1876 // Use the "historical" buffer
1877 QString bakMapName(QString("history-%1").arg(n));
1878 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1879 load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
1881 // Use the global buffer
1882 load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
1887 void VymModel::paste()
1889 BranchObj *sel=getSelectedBranch();
1892 saveStateChangingPart(
1895 QString ("paste (%1)").arg(curClipboard),
1896 QString("Paste to %1").arg( getObjectName(sel))
1903 void VymModel::cut()
1905 LinkableMapObj *sel=getSelectedLMO();
1906 if ( sel && (selectionType() == TreeItem::Branch ||
1907 selectionType()==TreeItem::MapCenter ||
1908 selectionType()==TreeItem::Image))
1910 /* No savestate! savestate is called in cutNoSave
1911 saveStateChangingPart(
1915 QString("Cut %1").arg(getObjectName(sel ))
1924 void VymModel::moveBranchUp()
1926 BranchObj* bo=getSelectedBranch();
1930 if (!bo->canMoveBranchUp()) return;
1931 par=(BranchObj*)(bo->getParObj());
1932 BranchObj *obo=par->moveBranchUp (bo); // bo will be the one below selection
1933 saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
1935 //FIXME-3 VM needed? scene()->update();
1937 ensureSelectionVisible();
1941 void VymModel::moveBranchDown()
1943 BranchObj* bo=getSelectedBranch();
1947 if (!bo->canMoveBranchDown()) return;
1948 par=(BranchObj*)(bo->getParObj());
1949 BranchObj *obo=par->moveBranchDown(bo); // bo will be the one above selection
1950 saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
1952 //FIXME-3 VM needed? scene()->update();
1954 ensureSelectionVisible();
1958 void VymModel::sortChildren()
1960 BranchObj* bo=getSelectedBranch();
1963 if(bo->countBranches()>1)
1965 saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
1968 ensureSelectionVisible();
1973 void VymModel::createMapCenter()
1975 MapCenterObj *mco=addMapCenter (QPointF (0,0) );
1979 void VymModel::createBranch()
1981 addNewBranchInt (-2);
1985 TreeItem* VymModel::createImage()
1987 BranchObj *bo=getSelectedBranch();
1990 FloatImageObj *newfio=bo->addFloatImage(); // FIXME-1 VM Old model, merge with below
1993 QList<QVariant> cData;
1994 cData << "VM:createImage" << "undef"<<"undef";
1995 TreeItem *parti=bo->getTreeItem();
1996 TreeItem *ti=new TreeItem (cData,parti);
1997 ti->setLMO (newfio);
1998 ti->setType (TreeItem::Image);
1999 parti->appendChild (ti);
2003 newfio->setTreeItem (ti);
2004 select (newfio); // FIXME-2 VM really needed here?
2011 MapCenterObj* VymModel::addMapCenter ()
2013 MapCenterObj *mco=addMapCenter (contextPos);
2014 //FIXME-3 selection.select (mco);
2016 ensureSelectionVisible();
2021 QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
2022 QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
2027 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
2029 MapCenterObj *mapCenter = new MapCenterObj(mapScene,this);
2030 mapCenter->setMapEditor(mapEditor); //FIXME-3 VM needed to get defLinkStyle, mapLinkColorHint ... for later added objects
2031 mapCenter->move (absPos);
2032 mapCenter->setVisibility (true);
2033 mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
2034 mapCenters.append(mapCenter);
2037 QModelIndex parix=index(rootItem);
2039 int n=rootItem->branchCount();
2041 emit (layoutAboutToBeChanged() );
2042 beginInsertRows (parix,n,n+1);
2044 QList<QVariant> cData;
2045 cData << "VM:addMapCenter" << "undef"<<"undef";
2046 TreeItem *ti=new TreeItem (cData,rootItem);
2047 ti->setLMO (mapCenter);
2048 ti->setType (TreeItem::MapCenter);
2049 mapCenter->setTreeItem (ti);
2050 rootItem->appendChild (ti);
2053 emit (newChildObject (parix));
2054 emit (layoutChanged() );
2058 qWarning ("MW::insertRow a");
2059 if (!insertRow(0, parix))
2061 std::cout << " war nix...\n";
2063 qWarning ("MW::insertRow b");
2069 MapCenterObj* VymModel::removeMapCenter(MapCenterObj* mco)
2071 int i=mapCenters.indexOf (mco);
2074 mapCenters.removeAt (i);
2076 if (i>0) return mapCenters.at(i-1); // Return previous MCO
2081 MapCenterObj* VymModel::getLastMapCenter()
2083 if (mapCenters.size()>0)
2084 return mapCenters.last();
2091 BranchObj* VymModel::addNewBranchInt(int num)
2093 // Depending on pos:
2094 // -3 insert in children of parent above selection
2095 // -2 add branch to selection
2096 // -1 insert in children of parent below selection
2097 // 0..n insert in children of parent at pos
2098 BranchObj *newbo=NULL;
2099 BranchObj *bo=getSelectedBranch();
2104 // save scroll state. If scrolled, automatically select
2105 // new branch in order to tmp unscroll parent...
2106 newbo=bo->addBranch();
2109 QList<QVariant> cData;
2110 cData << "new" << "undef"<<"undef";
2112 TreeItem *parti=bo->getTreeItem();
2113 QModelIndex parix=index(parti);
2114 int n=parti->branchCount();
2116 emit (layoutAboutToBeChanged() );
2117 beginInsertRows (parix,n,n+1);
2118 TreeItem *ti=new TreeItem (cData,parti);
2120 ti->setType (TreeItem::Branch);
2122 parti->appendChild (ti);
2124 emit (newChildObject (parix));
2125 emit (layoutChanged() );
2129 newbo->setTreeItem (ti);
2130 select (newbo); // FIXME-2 VM really needed here?
2136 bo=(BranchObj*)bo->getParObj();
2137 if (bo) newbo=bo->insertBranch(num); //FIXME-1 VM still missing
2141 bo=(BranchObj*)bo->getParObj();
2142 if (bo) newbo=bo->insertBranch(num); //FIXME-1 VM still missing
2148 BranchObj* VymModel::addNewBranch(int pos)
2150 // Different meaning than num in addNewBranchInt!
2154 BranchObj *bo = getSelectedBranch();
2155 BranchObj *newbo=NULL;
2159 // FIXME-1 VM do we still need this in model? setCursor (Qt::ArrowCursor);
2161 newbo=addNewBranchInt (pos-2);
2169 QString ("addBranch (%1)").arg(pos),
2170 QString ("Add new branch to %1").arg(getObjectName(bo)));
2173 // selection.update(); FIXME-3
2174 latestSelectionString=getSelectString(newbo);
2175 // In Network mode, the client needs to know where the new branch is,
2176 // so we have to pass on this information via saveState.
2177 // TODO: Get rid of this positioning workaround
2178 QString ps=qpointfToString (newbo->getAbsPos());
2179 sendData ("selectLatestAdded ()");
2180 sendData (QString("move %1").arg(ps));
2188 BranchObj* VymModel::addNewBranchBefore()
2190 BranchObj *newbo=NULL;
2191 BranchObj *bo = getSelectedBranch();
2192 if (bo && selectionType()==TreeItem::Branch)
2193 // We accept no MapCenterObj here, so we _have_ a parent
2195 QPointF p=bo->getRelPos();
2198 BranchObj *parbo=(BranchObj*)(bo->getParObj());
2200 // add below selection
2201 newbo=parbo->insertBranch(bo->getNum()+1); //FIXME-1 VM still missing
2205 newbo->move2RelPos (p);
2207 // Move selection to new branch
2208 bo->linkTo (newbo,-1);
2210 saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()",
2211 QString ("Add branch before %1").arg(getObjectName(bo)));
2214 // selection.update(); FIXME-3
2217 latestSelectionString=selection.getSelectString();
2221 void VymModel::deleteSelection()
2223 BranchObj *bo = getSelectedBranch(); // FIXME-2 VM should not be necessary
2226 QModelIndex ix=getSelectedIndex();
2227 if (!ix.isValid() ) return;
2229 QModelIndex parentIndex=parent(ix);
2230 if (!parentIndex.isValid()) return;
2231 TreeItem *ti=bo->getTreeItem();
2233 if (selectionType()==TreeItem::MapCenter) //FIXME-1 VM still missing
2235 // BranchObj* par=(BranchObj*)(bo->getParObj());
2236 //selection.unselect();
2237 /* FIXME-1 VM Note: does saveStateRemovingPart work for MCO? (No parent!)
2238 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2241 bo=removeMapCenter ((MapCenterObj*)bo);
2244 selection.select (bo);
2245 ensureSelectionVisible();
2252 if (selectionType()==TreeItem::Branch)
2254 int n=ti->branchCount();
2256 BranchObj* par=(BranchObj*)bo->getParObj();
2258 saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
2260 emit (layoutAboutToBeChanged() );
2262 cout << "VM::delete ti="<<ti<<" row="<<ix.row()<<endl;
2263 parentIndex=parent(index(ti));
2264 cout << " valid parentIndex="<<parentIndex.isValid()<<endl;
2265 beginRemoveRows (parentIndex,n,n);
2266 removeRows (ix.row(),1,parentIndex);
2268 par->removeBranch(bo);
2270 ensureSelectionVisible();
2273 emit (layoutChanged() );
2276 FloatImageObj *fio=selection.getFloatImage(); //FIXME-1 VM still missing
2280 BranchObj* par=(BranchObj*)fio->getParObj();
2281 saveStateChangingPart(
2285 QString("Delete %1").arg(getObjectName(fio))
2288 par->removeFloatImage(fio);
2291 ensureSelectionVisible();
2296 void VymModel::deleteKeepChildren() //FIXME-1 VM still missing
2299 BranchObj *bo=getSelectedBranch();
2303 par=(BranchObj*)(bo->getParObj());
2305 // Don't use this on mapcenter
2308 // Check if we have childs at all to keep
2309 if (bo->countBranches()==0)
2315 QPointF p=bo->getRelPos();
2316 saveStateChangingPart(
2319 "deleteKeepChildren ()",
2320 QString("Remove %1 and keep its children").arg(getObjectName(bo))
2323 QString sel=getSelectString(bo);
2325 par->removeBranchHere(bo);
2328 getSelectedBranch()->move2RelPos (p);
2333 void VymModel::deleteChildren() //FIXME-1 VM still missing
2336 BranchObj *bo=getSelectedBranch();
2339 saveStateChangingPart(
2342 "deleteChildren ()",
2343 QString( "Remove children of branch %1").arg(getObjectName(bo))
2345 bo->removeChildren();
2351 bool VymModel::scrollBranch(BranchObj *bo)
2355 if (bo->isScrolled()) return false;
2356 if (bo->countBranches()==0) return false;
2357 if (bo->getDepth()==0) return false;
2363 QString ("%1 ()").arg(u),
2365 QString ("%1 ()").arg(r),
2366 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2370 // FIXME-3 VM needed? scene()->update();
2376 bool VymModel::unscrollBranch(BranchObj *bo)
2380 if (!bo->isScrolled()) return false;
2381 if (bo->countBranches()==0) return false;
2382 if (bo->getDepth()==0) return false;
2388 QString ("%1 ()").arg(u),
2390 QString ("%1 ()").arg(r),
2391 QString ("%1 %2").arg(r).arg(getObjectName(bo))
2395 // FIXME-3 VM needed? scene()->update();
2401 void VymModel::toggleScroll()
2403 BranchObj *bo=getSelectedBranch();
2404 if (selectionType()==TreeItem::Branch )
2406 if (bo->isScrolled())
2407 unscrollBranch (bo);
2413 void VymModel::unscrollChildren() // FIXME-1 first, next moved to vymmodel
2417 BranchObj *bo=getSelectedBranch();
2423 if (bo->isScrolled()) unscrollBranch (bo);
2430 void VymModel::addFloatImage (const QPixmap &img)
2432 BranchObj *bo=getSelectedBranch();
2435 FloatImageObj *fio=bo->addFloatImage();
2437 fio->setOriginalFilename("No original filename (image added by dropevent)");
2438 QString s=getSelectString(bo);
2439 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio );
2440 saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
2442 // FIXME-3 VM needed? scene()->update();
2447 void VymModel::colorBranch (QColor c)
2449 BranchObj *bo=getSelectedBranch();
2454 QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
2456 QString ("colorBranch (\"%1\")").arg(c.name()),
2457 QString("Set color of %1 to %2").arg(getObjectName(bo)).arg(c.name())
2459 bo->setColor(c); // color branch
2463 void VymModel::colorSubtree (QColor c)
2465 BranchObj *bo=getSelectedBranch();
2468 saveStateChangingPart(
2471 QString ("colorSubtree (\"%1\")").arg(c.name()),
2472 QString ("Set color of %1 and children to %2").arg(getObjectName(bo)).arg(c.name())
2474 bo->setColorSubtree (c); // color links, color children
2478 QColor VymModel::getCurrentHeadingColor()
2480 BranchObj *bo=getSelectedBranch();
2481 if (bo) return bo->getColor();
2483 QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
2489 void VymModel::editURL()
2491 BranchObj *bo=getSelectedBranch();
2495 QString text = QInputDialog::getText(
2496 "VYM", tr("Enter URL:"), QLineEdit::Normal,
2497 bo->getURL(), &ok, NULL);
2499 // user entered something and pressed OK
2504 void VymModel::editLocalURL()
2506 BranchObj *bo=getSelectedBranch();
2509 QStringList filters;
2510 filters <<"All files (*)";
2511 filters << tr("Text","Filedialog") + " (*.txt)";
2512 filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
2513 filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
2514 filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
2515 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
2516 fd->setFilters (filters);
2517 fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
2518 fd->setDirectory (lastFileDir);
2519 if (! bo->getVymLink().isEmpty() )
2520 fd->selectFile( bo->getURL() );
2523 if ( fd->exec() == QDialog::Accepted )
2525 lastFileDir=QDir (fd->directory().path());
2526 setURL (fd->selectedFile() );
2532 void VymModel::editHeading2URL()
2534 BranchObj *bo=getSelectedBranch();
2536 setURL (bo->getHeading());
2539 void VymModel::editBugzilla2URL()
2541 BranchObj *bo=getSelectedBranch();
2544 QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
2549 void VymModel::editFATE2URL()
2551 BranchObj *bo=getSelectedBranch();
2554 QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
2557 "setURL (\""+bo->getURL()+"\")",
2559 "setURL (\""+url+"\")",
2560 QString("Use heading of %1 as link to FATE").arg(getObjectName(bo))
2567 void VymModel::editVymLink()
2569 BranchObj *bo=getSelectedBranch();
2572 QStringList filters;
2573 filters <<"VYM map (*.vym)";
2574 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
2575 fd->setFilters (filters);
2576 fd->setCaption(vymName+" - " +tr("Link to another map"));
2577 fd->setDirectory (lastFileDir);
2578 if (! bo->getVymLink().isEmpty() )
2579 fd->selectFile( bo->getVymLink() );
2583 if ( fd->exec() == QDialog::Accepted )
2585 lastFileDir=QDir (fd->directory().path());
2588 "setVymLink (\""+bo->getVymLink()+"\")",
2590 "setVymLink (\""+fd->selectedFile()+"\")",
2591 QString("Set vymlink of %1 to %2").arg(getObjectName(bo)).arg(fd->selectedFile())
2593 setVymLink (fd->selectedFile() ); // FIXME-2 ok?
2598 void VymModel::setVymLink (const QString &s)
2600 // Internal function, no saveState needed
2601 BranchObj *bo=getSelectedBranch();
2608 ensureSelectionVisible();
2612 void VymModel::deleteVymLink()
2614 BranchObj *bo=getSelectedBranch();
2619 "setVymLink (\""+bo->getVymLink()+"\")",
2621 "setVymLink (\"\")",
2622 QString("Unset vymlink of %1").arg(getObjectName(bo))
2624 bo->setVymLink ("" );
2627 // FIXME-3 VM needed? scene()->update();
2631 QString VymModel::getVymLink()
2633 BranchObj *bo=getSelectedBranch();
2635 return bo->getVymLink();
2641 QStringList VymModel::getVymLinks() // FIXME-1 first, next moved to vymmodel
2643 return QStringList();
2646 BranchObj *bo=getSelectedBranch();
2652 if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
2661 void VymModel::followXLink(int i)
2663 BranchObj *bo=getSelectedBranch();
2666 bo=bo->XLinkTargetAt(i);
2669 selection.select(bo);
2670 ensureSelectionVisible();
2675 void VymModel::editXLink(int i) // FIXME-1 VM missing saveState
2677 BranchObj *bo=getSelectedBranch();
2680 XLinkObj *xlo=bo->XLinkAt(i);
2683 EditXLinkDialog dia;
2685 dia.setSelection(bo);
2686 if (dia.exec() == QDialog::Accepted)
2688 if (dia.useSettingsGlobal() )
2690 setMapDefXLinkColor (xlo->getColor() );
2691 setMapDefXLinkWidth (xlo->getWidth() );
2693 if (dia.deleteXLink())
2694 bo->deleteXLinkAt(i);
2704 //////////////////////////////////////////////
2706 //////////////////////////////////////////////
2708 void VymModel::parseAtom(const QString &atom)
2710 BranchObj *selb=getSelectedBranch();
2716 // Split string s into command and parameters
2717 parser.parseAtom (atom);
2718 QString com=parser.getCommand();
2720 // External commands
2721 /////////////////////////////////////////////////////////////////////
2722 if (com=="addBranch")
2724 if (selection.isEmpty())
2726 parser.setError (Aborted,"Nothing selected");
2729 parser.setError (Aborted,"Type of selection is not a branch");
2734 if (parser.checkParCount(pl))
2736 if (parser.parCount()==0)
2740 n=parser.parInt (ok,0);
2741 if (ok ) addNewBranch (n);
2745 /////////////////////////////////////////////////////////////////////
2746 } else if (com=="addBranchBefore")
2748 if (selection.isEmpty())
2750 parser.setError (Aborted,"Nothing selected");
2753 parser.setError (Aborted,"Type of selection is not a branch");
2756 if (parser.parCount()==0)
2758 addNewBranchBefore ();
2761 /////////////////////////////////////////////////////////////////////
2762 } else if (com==QString("addMapCenter"))
2764 if (parser.checkParCount(2))
2766 x=parser.parDouble (ok,0);
2769 y=parser.parDouble (ok,1);
2770 if (ok) addMapCenter (QPointF(x,y));
2773 /////////////////////////////////////////////////////////////////////
2774 } else if (com==QString("addMapReplace"))
2776 if (selection.isEmpty())
2778 parser.setError (Aborted,"Nothing selected");
2781 parser.setError (Aborted,"Type of selection is not a branch");
2782 } else if (parser.checkParCount(1))
2784 //s=parser.parString (ok,0); // selection
2785 t=parser.parString (ok,0); // path to map
2786 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2787 addMapReplaceInt(getSelectString(selb),t);
2789 /////////////////////////////////////////////////////////////////////
2790 } else if (com==QString("addMapInsert"))
2792 if (selection.isEmpty())
2794 parser.setError (Aborted,"Nothing selected");
2797 parser.setError (Aborted,"Type of selection is not a branch");
2800 if (parser.checkParCount(2))
2802 t=parser.parString (ok,0); // path to map
2803 n=parser.parInt(ok,1); // position
2804 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2805 addMapInsertInt(t,n);
2808 /////////////////////////////////////////////////////////////////////
2809 } else if (com=="clearFlags")
2811 if (selection.isEmpty() )
2813 parser.setError (Aborted,"Nothing selected");
2816 parser.setError (Aborted,"Type of selection is not a branch");
2817 } else if (parser.checkParCount(0))
2819 selb->clearStandardFlags();
2820 selb->updateFlagsToolbar();
2822 /////////////////////////////////////////////////////////////////////
2823 } else if (com=="colorBranch")
2825 if (selection.isEmpty())
2827 parser.setError (Aborted,"Nothing selected");
2830 parser.setError (Aborted,"Type of selection is not a branch");
2831 } else if (parser.checkParCount(1))
2833 QColor c=parser.parColor (ok,0);
2834 if (ok) colorBranch (c);
2836 /////////////////////////////////////////////////////////////////////
2837 } else if (com=="colorSubtree")
2839 if (selection.isEmpty())
2841 parser.setError (Aborted,"Nothing selected");
2844 parser.setError (Aborted,"Type of selection is not a branch");
2845 } else if (parser.checkParCount(1))
2847 QColor c=parser.parColor (ok,0);
2848 if (ok) colorSubtree (c);
2850 /////////////////////////////////////////////////////////////////////
2851 } else if (com=="copy")
2853 if (selection.isEmpty())
2855 parser.setError (Aborted,"Nothing selected");
2858 parser.setError (Aborted,"Type of selection is not a branch");
2859 } else if (parser.checkParCount(0))
2861 //FIXME-1 missing action for copy
2863 /////////////////////////////////////////////////////////////////////
2864 } else if (com=="cut")
2866 if (selection.isEmpty())
2868 parser.setError (Aborted,"Nothing selected");
2869 } else if ( selectionType()!=TreeItem::Branch &&
2870 selectionType()!=TreeItem::MapCenter &&
2871 selectionType()!=TreeItem::Image )
2873 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2874 } else if (parser.checkParCount(0))
2878 /////////////////////////////////////////////////////////////////////
2879 } else if (com=="delete")
2881 if (selection.isEmpty())
2883 parser.setError (Aborted,"Nothing selected");
2885 /*else if (selectionType() != TreeItem::Branch && selectionType() != TreeItem::Image )
2887 parser.setError (Aborted,"Type of selection is wrong.");
2890 else if (parser.checkParCount(0))
2894 /////////////////////////////////////////////////////////////////////
2895 } else if (com=="deleteKeepChildren")
2897 if (selection.isEmpty())
2899 parser.setError (Aborted,"Nothing selected");
2902 parser.setError (Aborted,"Type of selection is not a branch");
2903 } else if (parser.checkParCount(0))
2905 deleteKeepChildren();
2907 /////////////////////////////////////////////////////////////////////
2908 } else if (com=="deleteChildren")
2910 if (selection.isEmpty())
2912 parser.setError (Aborted,"Nothing selected");
2915 parser.setError (Aborted,"Type of selection is not a branch");
2916 } else if (parser.checkParCount(0))
2920 /////////////////////////////////////////////////////////////////////
2921 } else if (com=="exportASCII")
2925 if (parser.parCount()>=1)
2926 // Hey, we even have a filename
2927 fname=parser.parString(ok,0);
2930 parser.setError (Aborted,"Could not read filename");
2933 exportASCII (fname,false);
2935 /////////////////////////////////////////////////////////////////////
2936 } else if (com=="exportImage")
2940 if (parser.parCount()>=2)
2941 // Hey, we even have a filename
2942 fname=parser.parString(ok,0);
2945 parser.setError (Aborted,"Could not read filename");
2948 QString format="PNG";
2949 if (parser.parCount()>=2)
2951 format=parser.parString(ok,1);
2953 exportImage (fname,false,format);
2955 /////////////////////////////////////////////////////////////////////
2956 } else if (com=="exportXHTML")
2960 if (parser.parCount()>=2)
2961 // Hey, we even have a filename
2962 fname=parser.parString(ok,1);
2965 parser.setError (Aborted,"Could not read filename");
2968 exportXHTML (fname,false);
2970 /////////////////////////////////////////////////////////////////////
2971 } else if (com=="exportXML")
2975 if (parser.parCount()>=2)
2976 // Hey, we even have a filename
2977 fname=parser.parString(ok,1);
2980 parser.setError (Aborted,"Could not read filename");
2983 exportXML (fname,false);
2985 /////////////////////////////////////////////////////////////////////
2986 } else if (com=="importDir")
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(1))
2996 s=parser.parString(ok,0);
2997 if (ok) importDirInt(s);
2999 /////////////////////////////////////////////////////////////////////
3000 } else if (com=="linkTo")
3002 if (selection.isEmpty())
3004 parser.setError (Aborted,"Nothing selected");
3007 if (parser.checkParCount(4))
3009 // 0 selectstring of parent
3010 // 1 num in parent (for branches)
3011 // 2,3 x,y of mainbranch or mapcenter
3012 s=parser.parString(ok,0);
3013 LinkableMapObj *dst=findObjBySelect (s);
3016 if (typeid(*dst) == typeid(BranchObj) )
3018 // Get number in parent
3019 n=parser.parInt (ok,1);
3022 selb->linkTo ((BranchObj*)(dst),n);
3025 } else if (typeid(*dst) == typeid(MapCenterObj) )
3027 selb->linkTo ((BranchObj*)(dst),-1);
3028 // Get coordinates of mainbranch
3029 x=parser.parDouble(ok,2);
3032 y=parser.parDouble(ok,3);
3042 } else if ( selectionType() == TreeItem::Image)
3044 if (parser.checkParCount(1))
3046 // 0 selectstring of parent
3047 s=parser.parString(ok,0);
3048 LinkableMapObj *dst=findObjBySelect (s);
3051 if (typeid(*dst) == typeid(BranchObj) ||
3052 typeid(*dst) == typeid(MapCenterObj))
3053 linkFloatImageTo (getSelectString(dst));
3055 parser.setError (Aborted,"Destination is not a branch");
3058 parser.setError (Aborted,"Type of selection is not a floatimage or branch");
3059 /////////////////////////////////////////////////////////////////////
3060 } else if (com=="loadImage")
3062 if (selection.isEmpty())
3064 parser.setError (Aborted,"Nothing selected");
3067 parser.setError (Aborted,"Type of selection is not a branch");
3068 } else if (parser.checkParCount(1))
3070 s=parser.parString(ok,0);
3071 if (ok) loadFloatImageInt (s);
3073 /////////////////////////////////////////////////////////////////////
3074 } else if (com=="moveBranchUp")
3076 if (selection.isEmpty() )
3078 parser.setError (Aborted,"Nothing selected");
3081 parser.setError (Aborted,"Type of selection is not a branch");
3082 } else if (parser.checkParCount(0))
3086 /////////////////////////////////////////////////////////////////////
3087 } else if (com=="moveBranchDown")
3089 if (selection.isEmpty() )
3091 parser.setError (Aborted,"Nothing selected");
3094 parser.setError (Aborted,"Type of selection is not a branch");
3095 } else if (parser.checkParCount(0))
3099 /////////////////////////////////////////////////////////////////////
3100 } else if (com=="move")
3102 if (selection.isEmpty() )
3104 parser.setError (Aborted,"Nothing selected");
3105 } else if ( selectionType()!=TreeItem::Branch &&
3106 selectionType()!=TreeItem::MapCenter &&
3107 selectionType()!=TreeItem::Image )
3109 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3110 } else if (parser.checkParCount(2))
3112 x=parser.parDouble (ok,0);
3115 y=parser.parDouble (ok,1);
3119 /////////////////////////////////////////////////////////////////////
3120 } else if (com=="moveRel")
3122 if (selection.isEmpty() )
3124 parser.setError (Aborted,"Nothing selected");
3125 } else if ( selectionType()!=TreeItem::Branch &&
3126 selectionType()!=TreeItem::MapCenter &&
3127 selectionType()!=TreeItem::Image )
3129 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3130 } else if (parser.checkParCount(2))
3132 x=parser.parDouble (ok,0);
3135 y=parser.parDouble (ok,1);
3136 if (ok) moveRel (x,y);
3139 /////////////////////////////////////////////////////////////////////
3140 } else if (com=="nop")
3142 /////////////////////////////////////////////////////////////////////
3143 } else if (com=="paste")
3145 if (selection.isEmpty() )
3147 parser.setError (Aborted,"Nothing selected");
3150 parser.setError (Aborted,"Type of selection is not a branch");
3151 } else if (parser.checkParCount(1))
3153 n=parser.parInt (ok,0);
3154 if (ok) pasteNoSave(n);
3156 /////////////////////////////////////////////////////////////////////
3157 } else if (com=="qa")
3159 if (selection.isEmpty() )
3161 parser.setError (Aborted,"Nothing selected");
3164 parser.setError (Aborted,"Type of selection is not a branch");
3165 } else if (parser.checkParCount(4))
3168 c=parser.parString (ok,0);
3171 parser.setError (Aborted,"No comment given");
3174 s=parser.parString (ok,1);
3177 parser.setError (Aborted,"First parameter is not a string");
3180 t=parser.parString (ok,2);
3183 parser.setError (Aborted,"Condition is not a string");
3186 u=parser.parString (ok,3);
3189 parser.setError (Aborted,"Third parameter is not a string");
3194 parser.setError (Aborted,"Unknown type: "+s);
3199 parser.setError (Aborted,"Unknown operator: "+t);
3204 parser.setError (Aborted,"Type of selection is not a branch");
3207 if (selb->getHeading() == u)
3209 cout << "PASSED: " << qPrintable (c) << endl;
3212 cout << "FAILED: " << qPrintable (c) << endl;
3222 /////////////////////////////////////////////////////////////////////
3223 } else if (com=="saveImage")
3225 FloatImageObj *fio=selection.getFloatImage();
3228 parser.setError (Aborted,"Type of selection is not an image");
3229 } else if (parser.checkParCount(2))
3231 s=parser.parString(ok,0);
3234 t=parser.parString(ok,1);
3235 if (ok) saveFloatImageInt (fio,t,s);
3238 /////////////////////////////////////////////////////////////////////
3239 } else if (com=="scroll")
3241 if (selection.isEmpty() )
3243 parser.setError (Aborted,"Nothing selected");
3246 parser.setError (Aborted,"Type of selection is not a branch");
3247 } else if (parser.checkParCount(0))
3249 if (!scrollBranch (selb))
3250 parser.setError (Aborted,"Could not scroll branch");
3252 /////////////////////////////////////////////////////////////////////
3253 } else if (com=="select")
3255 if (parser.checkParCount(1))
3257 s=parser.parString(ok,0);
3260 /////////////////////////////////////////////////////////////////////
3261 } else if (com=="selectLastBranch")
3263 if (selection.isEmpty() )
3265 parser.setError (Aborted,"Nothing selected");
3268 parser.setError (Aborted,"Type of selection is not a branch");
3269 } else if (parser.checkParCount(0))
3271 BranchObj *bo=selb->getLastBranch();
3273 parser.setError (Aborted,"Could not select last branch");
3277 /////////////////////////////////////////////////////////////////////
3278 } else if (com=="selectLastImage")
3280 if (selection.isEmpty() )
3282 parser.setError (Aborted,"Nothing selected");
3285 parser.setError (Aborted,"Type of selection is not a branch");
3286 } else if (parser.checkParCount(0))
3288 FloatImageObj *fio=selb->getLastFloatImage();
3290 parser.setError (Aborted,"Could not select last image");
3294 /////////////////////////////////////////////////////////////////////
3295 } else if (com=="selectLatestAdded")
3297 if (latestSelectionString.isEmpty() )
3299 parser.setError (Aborted,"No latest added object");
3302 if (!select (latestSelectionString))
3303 parser.setError (Aborted,"Could not select latest added object "+latestSelectionString);
3305 /////////////////////////////////////////////////////////////////////
3306 } else if (com=="setFrameType")
3308 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3310 parser.setError (Aborted,"Type of selection does not allow setting frame type");
3312 else if (parser.checkParCount(1))
3314 s=parser.parString(ok,0);
3315 if (ok) setFrameType (s);
3317 /////////////////////////////////////////////////////////////////////
3318 } else if (com=="setFramePenColor")
3320 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3322 parser.setError (Aborted,"Type of selection does not allow setting of pen color");
3324 else if (parser.checkParCount(1))
3326 QColor c=parser.parColor(ok,0);
3327 if (ok) setFramePenColor (c);
3329 /////////////////////////////////////////////////////////////////////
3330 } else if (com=="setFrameBrushColor")
3332 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3334 parser.setError (Aborted,"Type of selection does not allow setting brush color");
3336 else if (parser.checkParCount(1))
3338 QColor c=parser.parColor(ok,0);
3339 if (ok) setFrameBrushColor (c);
3341 /////////////////////////////////////////////////////////////////////
3342 } else if (com=="setFramePadding")
3344 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3346 parser.setError (Aborted,"Type of selection does not allow setting frame padding");
3348 else if (parser.checkParCount(1))
3350 n=parser.parInt(ok,0);
3351 if (ok) setFramePadding(n);
3353 /////////////////////////////////////////////////////////////////////
3354 } else if (com=="setFrameBorderWidth")
3356 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3358 parser.setError (Aborted,"Type of selection does not allow setting frame border width");
3360 else if (parser.checkParCount(1))
3362 n=parser.parInt(ok,0);
3363 if (ok) setFrameBorderWidth (n);
3365 /////////////////////////////////////////////////////////////////////
3366 } else if (com=="setMapAuthor")
3368 if (parser.checkParCount(1))
3370 s=parser.parString(ok,0);
3371 if (ok) setAuthor (s);
3373 /////////////////////////////////////////////////////////////////////
3374 } else if (com=="setMapComment")
3376 if (parser.checkParCount(1))
3378 s=parser.parString(ok,0);
3379 if (ok) setComment(s);
3381 /////////////////////////////////////////////////////////////////////
3382 } else if (com=="setMapBackgroundColor")
3384 if (selection.isEmpty() )
3386 parser.setError (Aborted,"Nothing selected");
3387 } else if (! getSelectedBranch() )
3389 parser.setError (Aborted,"Type of selection is not a branch");
3390 } else if (parser.checkParCount(1))
3392 QColor c=parser.parColor (ok,0);
3393 if (ok) setMapBackgroundColor (c);
3395 /////////////////////////////////////////////////////////////////////
3396 } else if (com=="setMapDefLinkColor")
3398 if (selection.isEmpty() )
3400 parser.setError (Aborted,"Nothing selected");
3403 parser.setError (Aborted,"Type of selection is not a branch");
3404 } else if (parser.checkParCount(1))
3406 QColor c=parser.parColor (ok,0);
3407 if (ok) setMapDefLinkColor (c);
3409 /////////////////////////////////////////////////////////////////////
3410 } else if (com=="setMapLinkStyle")
3412 if (parser.checkParCount(1))
3414 s=parser.parString (ok,0);
3415 if (ok) setMapLinkStyle(s);
3417 /////////////////////////////////////////////////////////////////////
3418 } else if (com=="setHeading")
3420 if (selection.isEmpty() )
3422 parser.setError (Aborted,"Nothing selected");
3425 parser.setError (Aborted,"Type of selection is not a branch");
3426 } else if (parser.checkParCount(1))
3428 s=parser.parString (ok,0);
3432 /////////////////////////////////////////////////////////////////////
3433 } else if (com=="setHideExport")
3435 if (selection.isEmpty() )
3437 parser.setError (Aborted,"Nothing selected");
3438 } else if (selectionType()!=TreeItem::Branch && selectionType() != TreeItem::MapCenter &&selectionType()!=TreeItem::Image)
3440 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3441 } else if (parser.checkParCount(1))
3443 b=parser.parBool(ok,0);
3444 if (ok) setHideExport (b);
3446 /////////////////////////////////////////////////////////////////////
3447 } else if (com=="setIncludeImagesHorizontally")
3449 if (selection.isEmpty() )
3451 parser.setError (Aborted,"Nothing selected");
3454 parser.setError (Aborted,"Type of selection is not a branch");
3455 } else if (parser.checkParCount(1))
3457 b=parser.parBool(ok,0);
3458 if (ok) setIncludeImagesHor(b);
3460 /////////////////////////////////////////////////////////////////////
3461 } else if (com=="setIncludeImagesVertically")
3463 if (selection.isEmpty() )
3465 parser.setError (Aborted,"Nothing selected");
3468 parser.setError (Aborted,"Type of selection is not a branch");
3469 } else if (parser.checkParCount(1))
3471 b=parser.parBool(ok,0);
3472 if (ok) setIncludeImagesVer(b);
3474 /////////////////////////////////////////////////////////////////////
3475 } else if (com=="setHideLinkUnselected")
3477 if (selection.isEmpty() )
3479 parser.setError (Aborted,"Nothing selected");
3480 } else if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3482 parser.setError (Aborted,"Type of selection does not allow hiding the link");
3483 } else if (parser.checkParCount(1))
3485 b=parser.parBool(ok,0);
3486 if (ok) setHideLinkUnselected(b);
3488 /////////////////////////////////////////////////////////////////////
3489 } else if (com=="setSelectionColor")
3491 if (parser.checkParCount(1))
3493 QColor c=parser.parColor (ok,0);
3494 if (ok) setSelectionColorInt (c);
3496 /////////////////////////////////////////////////////////////////////
3497 } else if (com=="setURL")
3499 if (selection.isEmpty() )
3501 parser.setError (Aborted,"Nothing selected");
3504 parser.setError (Aborted,"Type of selection is not a branch");
3505 } else if (parser.checkParCount(1))
3507 s=parser.parString (ok,0);
3510 /////////////////////////////////////////////////////////////////////
3511 } else if (com=="setVymLink")
3513 if (selection.isEmpty() )
3515 parser.setError (Aborted,"Nothing selected");
3518 parser.setError (Aborted,"Type of selection is not a branch");
3519 } else if (parser.checkParCount(1))
3521 s=parser.parString (ok,0);
3522 if (ok) setVymLink(s);
3525 /////////////////////////////////////////////////////////////////////
3526 else if (com=="setFlag")
3528 if (selection.isEmpty() )
3530 parser.setError (Aborted,"Nothing selected");
3533 parser.setError (Aborted,"Type of selection is not a branch");
3534 } else if (parser.checkParCount(1))
3536 s=parser.parString(ok,0);
3539 selb->activateStandardFlag(s);
3540 selb->updateFlagsToolbar();
3543 /////////////////////////////////////////////////////////////////////
3544 } else if (com=="setFrameType")
3546 if (selection.isEmpty() )
3548 parser.setError (Aborted,"Nothing selected");
3551 parser.setError (Aborted,"Type of selection is not a branch");
3552 } else if (parser.checkParCount(1))
3554 s=parser.parString(ok,0);
3558 /////////////////////////////////////////////////////////////////////
3559 } else if (com=="sortChildren")
3561 if (selection.isEmpty() )
3563 parser.setError (Aborted,"Nothing selected");
3566 parser.setError (Aborted,"Type of selection is not a branch");
3567 } else if (parser.checkParCount(0))
3571 /////////////////////////////////////////////////////////////////////
3572 } else if (com=="toggleFlag")
3574 if (selection.isEmpty() )
3576 parser.setError (Aborted,"Nothing selected");
3579 parser.setError (Aborted,"Type of selection is not a branch");
3580 } else if (parser.checkParCount(1))
3582 s=parser.parString(ok,0);
3585 selb->toggleStandardFlag(s);
3586 selb->updateFlagsToolbar();
3589 /////////////////////////////////////////////////////////////////////
3590 } else if (com=="unscroll")
3592 if (selection.isEmpty() )
3594 parser.setError (Aborted,"Nothing selected");
3597 parser.setError (Aborted,"Type of selection is not a branch");
3598 } else if (parser.checkParCount(0))
3600 if (!unscrollBranch (selb))
3601 parser.setError (Aborted,"Could not unscroll branch");
3603 /////////////////////////////////////////////////////////////////////
3604 } else if (com=="unscrollChildren")
3606 if (selection.isEmpty() )
3608 parser.setError (Aborted,"Nothing selected");
3611 parser.setError (Aborted,"Type of selection is not a branch");
3612 } else if (parser.checkParCount(0))
3614 unscrollChildren ();
3616 /////////////////////////////////////////////////////////////////////
3617 } else if (com=="unsetFlag")
3619 if (selection.isEmpty() )
3621 parser.setError (Aborted,"Nothing selected");
3624 parser.setError (Aborted,"Type of selection is not a branch");
3625 } else if (parser.checkParCount(1))
3627 s=parser.parString(ok,0);
3630 selb->deactivateStandardFlag(s);
3631 selb->updateFlagsToolbar();
3635 parser.setError (Aborted,"Unknown command");
3638 if (parser.errorLevel()==NoError)
3640 // setChanged(); FIXME-2 should not be called e.g. for export?!
3645 // TODO Error handling
3646 qWarning("VymModel::parseAtom: Error!");
3647 qWarning(parser.errorMessage());
3651 void VymModel::runScript (QString script)
3653 parser.setScript (script);
3655 while (parser.next() )
3656 parseAtom(parser.getAtom());
3659 void VymModel::setExportMode (bool b)
3661 // should be called before and after exports
3662 // depending on the settings
3663 if (b && settings.value("/export/useHideExport","true")=="true")
3664 setHideTmpMode (HideExport);
3666 setHideTmpMode (HideNone);
3669 void VymModel::exportImage(QString fname, bool askName, QString format)
3673 fname=getMapName()+".png";
3680 QFileDialog *fd=new QFileDialog (NULL);
3681 fd->setCaption (tr("Export map as image"));
3682 fd->setDirectory (lastImageDir);
3683 fd->setFileMode(QFileDialog::AnyFile);
3684 fd->setFilters (imageIO.getFilters() );
3687 fl=fd->selectedFiles();
3689 format=imageIO.getType(fd->selectedFilter());
3693 setExportMode (true);
3694 QPixmap pix (getPixmap());
3695 pix.save(fname, format);
3696 setExportMode (false);
3700 void VymModel::exportXML(QString dir, bool askForName)
3704 dir=browseDirectory(NULL,tr("Export XML to directory"));
3705 if (dir =="" && !reallyWriteDirectory(dir) )
3709 // Hide stuff during export, if settings want this
3710 setExportMode (true);
3712 // Create subdirectories
3715 // write to directory
3716 QString saveFile=saveToDir (dir,mapName+"-",true,getTotalBBox().topLeft() ,NULL);
3719 file.setName ( dir + "/"+mapName+".xml");
3720 if ( !file.open( QIODevice::WriteOnly ) )
3722 // This should neverever happen
3723 QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
3727 // Write it finally, and write in UTF8, no matter what
3728 QTextStream ts( &file );
3729 ts.setEncoding (QTextStream::UnicodeUTF8);
3733 // Now write image, too
3734 exportImage (dir+"/images/"+mapName+".png",false,"PNG");
3736 setExportMode (false);
3739 void VymModel::exportASCII(QString fname,bool askName)
3744 ex.setFile (mapName+".txt");
3750 //ex.addFilter ("TXT (*.txt)");
3751 ex.setDir(lastImageDir);
3752 //ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
3757 setExportMode(true);
3759 setExportMode(false);
3763 void VymModel::exportXHTML (const QString &dir, bool askForName)
3765 ExportXHTMLDialog dia(NULL);
3766 dia.setFilePath (filePath );
3767 dia.setMapName (mapName );
3769 if (dir!="") dia.setDir (dir);
3775 if (dia.exec()!=QDialog::Accepted)
3779 QDir d (dia.getDir());
3780 // Check, if warnings should be used before overwriting
3781 // the output directory
3782 if (d.exists() && d.count()>0)
3785 warn.showCancelButton (true);
3786 warn.setText(QString(
3787 "The directory %1 is not empty.\n"
3788 "Do you risk to overwrite some of its contents?").arg(d.path() ));
3789 warn.setCaption("Warning: Directory not empty");
3790 warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
3792 if (warn.exec()!=QDialog::Accepted) ok=false;
3799 exportXML (dia.getDir(),false );
3800 dia.doExport(mapName );
3801 //if (dia.hasChanged()) setChanged();
3805 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
3810 if (ex.setConfigFile(cf))
3812 setExportMode (true);
3813 ex.exportPresentation();
3814 setExportMode (false);
3821 //////////////////////////////////////////////
3823 //////////////////////////////////////////////
3825 void VymModel::registerEditor(QWidget *me)
3827 mapEditor=(MapEditor*)me;
3828 for (int i=0; i<mapCenters.count(); i++)
3829 mapCenters.at(i)->setMapEditor(mapEditor);
3832 void VymModel::unregisterEditor(QWidget *)
3837 void VymModel::setContextPos(QPointF p)
3842 void VymModel::unsetContextPos()
3844 contextPos=QPointF();
3847 void VymModel::updateNoteFlag()
3850 cout << "VM::updateNoteFlag()\n";
3851 /* FIXME-1 modify note flag
3852 BranchObj *bo=getSelectedBranch();
3855 bo->updateNoteFlag();
3856 mainWindow->updateActions();
3861 void VymModel::updateRelPositions()
3863 for (int i=0; i<mapCenters.count(); i++)
3864 mapCenters.at(i)->updateRelPositions();
3867 void VymModel::reposition()
3869 for (int i=0;i<mapCenters.count(); i++)
3870 mapCenters.at(i)->reposition(); // for positioning heading
3873 QPolygonF VymModel::shape(BranchObj *bo)
3875 // Creating (arbitrary) shapes
3878 QRectF rb=bo->getBBox();
3879 if (bo->getDepth()==0)
3881 // Just take BBox of this mapCenter
3882 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
3886 // Take union of BBox and TotalBBox
3888 QRectF ra=bo->getTotalBBox();
3889 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
3892 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
3895 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
3897 p <<ra.bottomRight()
3899 <<QPointF (rb.topRight().x(), ra.topRight().y() )
3902 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
3907 void VymModel::moveAway(LinkableMapObj *lmo)
3911 // Move all branches and MapCenters away from lmo
3912 // to avoid collisions
3917 BranchObj *boA=(BranchObj*)lmo;
3919 for (int i=0; i<mapCenters.count(); i++)
3921 boB=mapCenters.at(i);
3924 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
3927 <<" ("<<qPrintable(boA->getHeading() )<<")"
3928 <<" with ("<< qPrintable (boB->getHeading() )
3931 <<" minT="<<r.minTranslation<<endl<<endl;
3935 QPixmap VymModel::getPixmap()
3937 QRectF mapRect=getTotalBBox();
3938 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
3941 pp.setRenderHints(mapEditor->renderHints());
3943 // Don't print the visualisation of selection
3944 selection.unselect();
3946 mapScene->render ( &pp,
3947 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
3948 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
3950 // Restore selection
3951 selection.reselect();
3957 void VymModel::setMapLinkStyle (const QString & s)
3962 case LinkableMapObj::Line :
3965 case LinkableMapObj::Parabel:
3966 snow="StyleParabel";
3968 case LinkableMapObj::PolyLine:
3969 snow="StylePolyLine";
3971 case LinkableMapObj::PolyParabel:
3972 snow="StylePolyParabel";
3975 snow="UndefinedStyle";
3980 QString("setMapLinkStyle (\"%1\")").arg(s),
3981 QString("setMapLinkStyle (\"%1\")").arg(snow),
3982 QString("Set map link style (\"%1\")").arg(s)
3986 linkstyle=LinkableMapObj::Line;
3987 else if (s=="StyleParabel")
3988 linkstyle=LinkableMapObj::Parabel;
3989 else if (s=="StylePolyLine")
3990 linkstyle=LinkableMapObj::PolyLine;
3991 else if (s=="StylePolyParabel")
3992 linkstyle=LinkableMapObj::PolyParabel;
3994 linkstyle=LinkableMapObj::UndefinedStyle;
3997 TreeItem *prev=NULL;
4003 bo=(BranchObj*)(cur->getLMO() );
4004 bo->setLinkStyle(bo->getDefLinkStyle());
4005 cur=next(cur,prev,d);
4010 LinkableMapObj::Style VymModel::getMapLinkStyle ()
4015 void VymModel::setMapDefLinkColor(QColor col)
4017 if ( !col.isValid() ) return;
4019 QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
4020 QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
4021 QString("Set map link color to %1").arg(col.name())
4026 TreeItem *prev=NULL;
4029 cur=next(cur,prev,d);
4032 bo=(BranchObj*)(cur->getLMO() );
4039 void VymModel::setMapLinkColorHintInt()
4041 // called from setMapLinkColorHint(lch) or at end of parse
4043 TreeItem *prev=NULL;
4046 cur=next(cur,prev,d);
4049 bo=(BranchObj*)(cur->getLMO() );
4051 cur=next(cur,prev,d);
4055 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
4058 setMapLinkColorHintInt();
4061 void VymModel::toggleMapLinkColorHint()
4063 if (linkcolorhint==LinkableMapObj::HeadingColor)
4064 linkcolorhint=LinkableMapObj::DefaultColor;
4066 linkcolorhint=LinkableMapObj::HeadingColor;
4068 TreeItem *prev=NULL;
4071 cur=next(cur,prev,d);
4074 bo=(BranchObj*)(cur->getLMO() );
4080 void VymModel::selectMapBackgroundImage () // FIXME-2 move to ME
4082 Q3FileDialog *fd=new Q3FileDialog( NULL);
4083 fd->setMode (Q3FileDialog::ExistingFile);
4084 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
4085 ImagePreview *p =new ImagePreview (fd);
4086 fd->setContentsPreviewEnabled( TRUE );
4087 fd->setContentsPreview( p, p );
4088 fd->setPreviewMode( Q3FileDialog::Contents );
4089 fd->setCaption(vymName+" - " +tr("Load background image"));
4090 fd->setDir (lastImageDir);
4093 if ( fd->exec() == QDialog::Accepted )
4095 // TODO selectMapBackgroundImg in QT4 use: lastImageDir=fd->directory();
4096 lastImageDir=QDir (fd->dirPath());
4097 setMapBackgroundImage (fd->selectedFile());
4101 void VymModel::setMapBackgroundImage (const QString &fn) //FIXME-2 missing savestate
4103 QColor oldcol=mapScene->backgroundBrush().color();
4107 QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
4109 QString ("setMapBackgroundImage (%1)").arg(col.name()),
4110 QString("Set background color of map to %1").arg(col.name()));
4113 brush.setTextureImage (QPixmap (fn));
4114 mapScene->setBackgroundBrush(brush);
4117 void VymModel::selectMapBackgroundColor() // FIXME-1 move to ME
4119 QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
4120 if ( !col.isValid() ) return;
4121 setMapBackgroundColor( col );
4125 void VymModel::setMapBackgroundColor(QColor col) // FIXME-1 move to ME
4127 QColor oldcol=mapScene->backgroundBrush().color();
4129 QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
4130 QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
4131 QString("Set background color of map to %1").arg(col.name()));
4132 mapScene->setBackgroundBrush(col);
4135 QColor VymModel::getMapBackgroundColor() // FIXME-1 move to ME
4137 return mapScene->backgroundBrush().color();
4141 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint() // FIXME-1 move to ME
4143 return linkcolorhint;
4146 QColor VymModel::getMapDefLinkColor() // FIXME-1 move to ME
4148 return defLinkColor;
4151 void VymModel::setMapDefXLinkColor(QColor col) // FIXME-1 move to ME
4156 QColor VymModel::getMapDefXLinkColor() // FIXME-1 move to ME
4158 return defXLinkColor;
4161 void VymModel::setMapDefXLinkWidth (int w) // FIXME-1 move to ME
4166 int VymModel::getMapDefXLinkWidth() // FIXME-1 move to ME
4168 return defXLinkWidth;
4171 void VymModel::move(const double &x, const double &y)
4173 BranchObj *bo = getSelectedBranch();
4175 (selectionType()==TreeItem::Branch ||
4176 selectionType()==TreeItem::MapCenter ||
4177 selectionType()==TreeItem::Image
4180 QPointF ap(bo->getAbsPos());
4184 QString ps=qpointfToString(ap);
4185 QString s=getSelectString();
4188 s, "move "+qpointfToString(to),
4189 QString("Move %1 to %2").arg(getObjectName(bo)).arg(ps));
4197 void VymModel::moveRel (const double &x, const double &y)
4199 BranchObj *bo = getSelectedBranch();
4201 (selectionType()==TreeItem::Branch ||
4202 selectionType()==TreeItem::MapCenter ||
4203 selectionType()==TreeItem::Image
4207 QPointF rp(bo->getRelPos());
4211 QString ps=qpointfToString (bo->getRelPos());
4212 QString s=getSelectString(bo);
4215 s, "moveRel "+qpointfToString(to),
4216 QString("Move %1 to relative position %2").arg(getObjectName(bo)).arg(ps));
4217 ((OrnamentedObj*)bo)->move2RelPos (x,y);
4226 void VymModel::animate()
4228 animationTimer->stop();
4231 while (i<animObjList.size() )
4233 bo=(BranchObj*)animObjList.at(i);
4238 animObjList.removeAt(i);
4245 QItemSelection sel=selModel->selection();
4246 emit (selectionChanged(sel,sel));
4249 if (!animObjList.isEmpty()) animationTimer->start();
4253 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
4255 if (bo && bo->getDepth()>0)
4258 ap.setStart (start);
4260 ap.setTicks (animationTicks);
4261 ap.setAnimated (true);
4262 bo->setAnimation (ap);
4263 animObjList.append( bo );
4264 animationTimer->setSingleShot (true);
4265 animationTimer->start(animationInterval);
4269 void VymModel::stopAnimation (MapObj *mo)
4271 int i=animObjList.indexOf(mo);
4273 animObjList.removeAt (i);
4276 void VymModel::sendSelection()
4278 if (netstate!=Server) return;
4279 sendData (QString("select (\"%1\")").arg(selection.getSelectString()) );
4282 void VymModel::newServer()
4286 tcpServer = new QTcpServer(this);
4287 if (!tcpServer->listen(QHostAddress::Any,port)) {
4288 QMessageBox::critical(NULL, "vym server",
4289 QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
4290 //FIXME-3 needed? we are no widget any longer... close();
4293 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
4295 cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
4298 void VymModel::connectToServer()
4301 server="salam.suse.de";
4303 clientSocket = new QTcpSocket (this);
4304 clientSocket->abort();
4305 clientSocket->connectToHost(server ,port);
4306 connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
4307 connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
4308 this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
4310 cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
4315 void VymModel::newClient()
4317 QTcpSocket *newClient = tcpServer->nextPendingConnection();
4318 connect(newClient, SIGNAL(disconnected()),
4319 newClient, SLOT(deleteLater()));
4321 cout <<"ME::newClient at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
4323 clientList.append (newClient);
4327 void VymModel::sendData(const QString &s)
4329 if (clientList.size()==0) return;
4331 // Create bytearray to send
4333 QDataStream out(&block, QIODevice::WriteOnly);
4334 out.setVersion(QDataStream::Qt_4_0);
4336 // Reserve some space for blocksize
4339 // Write sendCounter
4340 out << sendCounter++;
4345 // Go back and write blocksize so far
4346 out.device()->seek(0);
4347 quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
4351 cout << "ME::sendData bs="<<bs<<" counter="<<sendCounter<<" s="<<qPrintable(s)<<endl;
4353 for (int i=0; i<clientList.size(); ++i)
4355 //cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
4356 clientList.at(i)->write (block);
4360 void VymModel::readData ()
4362 while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
4365 cout <<"readData bytesAvail="<<clientSocket->bytesAvailable();
4369 QDataStream in(clientSocket);
4370 in.setVersion(QDataStream::Qt_4_0);
4378 cout << " t="<<qPrintable (t)<<endl;
4384 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
4386 switch (socketError) {
4387 case QAbstractSocket::RemoteHostClosedError:
4389 case QAbstractSocket::HostNotFoundError:
4390 QMessageBox::information(NULL, vymName +" Network client",
4391 "The host was not found. Please check the "
4392 "host name and port settings.");
4394 case QAbstractSocket::ConnectionRefusedError:
4395 QMessageBox::information(NULL, vymName + " Network client",
4396 "The connection was refused by the peer. "
4397 "Make sure the fortune server is running, "
4398 "and check that the host name and port "
4399 "settings are correct.");
4402 QMessageBox::information(NULL, vymName + " Network client",
4403 QString("The following error occurred: %1.")
4404 .arg(clientSocket->errorString()));
4411 void VymModel::selectMapSelectionColor()
4413 QColor col = QColorDialog::getColor( defLinkColor, NULL);
4414 setSelectionColor (col);
4417 void VymModel::setSelectionColorInt (QColor col)
4419 if ( !col.isValid() ) return;
4421 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4422 QString("setSelectionColor (%1)").arg(col.name()),
4423 QString("Set color of selection box to %1").arg(col.name())
4426 mapEditor->setSelectionColor (col);
4430 void VymModel::changeSelection (const QItemSelection &newsel,const QItemSelection &oldsel)
4432 cout << "VymModel::changeSelection (";
4433 if (!newsel.indexes().isEmpty() )
4434 cout << getItem(newsel.indexes().first() )->getHeading().toStdString();
4436 if (!oldsel.indexes().isEmpty() )
4437 cout << getItem(oldsel.indexes().first() )->getHeading().toStdString();
4442 void VymModel::updateSelection(const QItemSelection &newsel)
4444 emit (selectionChanged(newsel,newsel)); // needed e.g. to update geometry in editor
4445 ensureSelectionVisible();
4449 void VymModel::updateSelection()
4451 QItemSelection newsel=selModel->selection();
4452 updateSelection (newsel);
4455 void VymModel::setSelectionColor(QColor col)
4457 if ( !col.isValid() ) return;
4459 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4460 QString("setSelectionColor (%1)").arg(col.name()),
4461 QString("Set color of selection box to %1").arg(col.name())
4463 setSelectionColorInt (col);
4466 QColor VymModel::getSelectionColor()
4468 return mapEditor->getSelectionColor();
4471 void VymModel::setHideTmpMode (HideTmpMode mode)
4474 for (int i=0;i<mapCenters.count(); i++)
4475 mapCenters.at(i)->setHideTmp (mode);
4477 // FIXME-3 needed? scene()->update();
4481 QRectF VymModel::getTotalBBox()
4484 for (int i=0;i<mapCenters.count(); i++)
4485 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
4489 //////////////////////////////////////////////
4490 // Selection related
4491 //////////////////////////////////////////////
4493 void VymModel::setSelectionModel (QItemSelectionModel *sm)
4498 QItemSelectionModel* VymModel::getSelectionModel()
4503 void VymModel::setSelectionBlocked (bool b)
4508 selection.unblock();
4511 bool VymModel::isSelectionBlocked()
4513 return selection.isBlocked();
4516 bool VymModel::select ()
4518 QModelIndex index=selModel->selectedIndexes().first(); // TODO no multiselections yet
4520 TreeItem *item = getItem (index);
4521 return select (item->getLMO() );
4524 bool VymModel::select (const QString &s)
4526 LinkableMapObj *lmo=findObjBySelect(s);
4528 // Finally select the found object
4538 bool VymModel::select (LinkableMapObj *lmo)
4540 QItemSelection oldsel=selModel->selection();
4543 return select (lmo->getTreeItem() );
4548 bool VymModel::select (TreeItem *ti)
4552 QModelIndex ix=index(ti);
4553 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4554 ti->setLastSelectedBranch();
4555 //updateSelection(oldsel); //FIXME-3 needed?
4561 void VymModel::unselect()
4563 selModel->clearSelection();
4566 void VymModel::reselect()
4568 selection.reselect();
4571 void VymModel::ensureSelectionVisible() //FIXME-3 needed? in vymmodel.h commented...
4573 LinkableMapObj *lmo=getSelectedLMO();
4574 if (lmo &&mapEditor) mapEditor->ensureVisible (lmo->getBBox() );
4577 void VymModel::emitContentHasChanged (TreeItem *ti)
4579 QModelIndex ix=index(ti);
4580 emit (contentHasChanged (ix) );
4583 void VymModel::selectInt (LinkableMapObj *lmo)
4585 if (selection.select(lmo))
4587 //selection.update();
4588 sendSelection (); // FIXME-4 VM use signal
4593 void VymModel::selectNextBranchInt()
4595 // Increase number of branch
4596 LinkableMapObj *sel=getSelectedBranch();
4599 QString s=getSelectString();
4605 part=s.section(",",-1);
4607 num=part.right(part.length() - 3);
4609 s=s.left (s.length() -num.length());
4612 num=QString ("%1").arg(num.toUInt()+1);
4616 // Try to select this one
4617 if (select (s)) return;
4619 // We have no direct successor,
4620 // try to increase the parental number in order to
4621 // find a successor with same depth
4623 int d=getSelectedLMO()->getDepth();
4628 while (!found && d>0)
4630 s=s.section (",",0,d-1);
4631 // replace substring of current depth in s with "1"
4632 part=s.section(",",-1);
4634 num=part.right(part.length() - 3);
4638 // increase number of parent
4639 num=QString ("%1").arg(num.toUInt()+1);
4640 s=s.section (",",0,d-2) + ","+ typ+num;
4643 // Special case, look at orientation
4644 if (getSelectedLMO()->getOrientation()==LinkableMapObj::RightOfCenter)
4645 num=QString ("%1").arg(num.toUInt()+1);
4647 num=QString ("%1").arg(num.toUInt()-1);
4652 // pad to oldDepth, select the first branch for each depth
4653 for (i=d;i<oldDepth;i++)
4658 if ( getSelectedBranch()->countBranches()>0)
4666 // try to select the freshly built string
4674 void VymModel::selectPrevBranchInt()
4676 // Decrease number of branch
4677 if (selectionType()==TreeItem::Branch)
4679 QString s=getSelectString();
4685 part=s.section(",",-1);
4687 num=part.right(part.length() - 3);
4689 s=s.left (s.length() -num.length());
4691 int n=num.toInt()-1;
4694 num=QString ("%1").arg(n);
4697 // Try to select this one
4698 if (n>=0 && select (s)) return;
4700 // We have no direct precessor,
4701 // try to decrease the parental number in order to
4702 // find a precessor with same depth
4704 int d=getSelectedLMO()->getDepth();
4709 while (!found && d>0)
4711 s=s.section (",",0,d-1);
4712 // replace substring of current depth in s with "1"
4713 part=s.section(",",-1);
4715 num=part.right(part.length() - 3);
4719 // decrease number of parent
4720 num=QString ("%1").arg(num.toInt()-1);
4721 s=s.section (",",0,d-2) + ","+ typ+num;
4724 // Special case, look at orientation
4725 if (getSelectedLMO()->getOrientation()==LinkableMapObj::RightOfCenter)
4726 num=QString ("%1").arg(num.toInt()-1);
4728 num=QString ("%1").arg(num.toInt()+1);
4733 // pad to oldDepth, select the last branch for each depth
4734 for (i=d;i<oldDepth;i++)
4738 if ( getSelectedBranch()->countBranches()>0)
4739 s+=",bo:"+ QString ("%1").arg( getSelectedBranch()->countBranches()-1 );
4746 // try to select the freshly built string
4754 void VymModel::selectUpperBranch()
4756 if (selection.isBlocked() ) return;
4758 BranchObj *bo=getSelectedBranch();
4759 if (bo && selectionType()==TreeItem::Branch)
4761 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4762 selectPrevBranchInt();
4764 if (bo->getDepth()==1)
4765 selectNextBranchInt();
4767 selectPrevBranchInt();
4771 void VymModel::selectLowerBranch()
4773 if (selection.isBlocked() ) return;
4775 BranchObj *bo=getSelectedBranch();
4776 if (bo && selectionType()==TreeItem::Branch)
4778 if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
4779 selectNextBranchInt();
4781 if (bo->getDepth()==1)
4782 selectPrevBranchInt();
4784 selectNextBranchInt();
4789 void VymModel::selectLeftBranch()
4791 if (selection.isBlocked() ) return;
4793 QItemSelection oldsel=selModel->selection();
4796 LinkableMapObj *sel=getSelectedBranch();
4799 if (selectionType()== TreeItem::MapCenter)
4801 QModelIndex ix=getSelectedIndex();
4802 selModel->select (index (rowCount(ix)-1,0,ix),QItemSelectionModel::ClearAndSelect );
4805 par=(BranchObj*)(sel->getParObj());
4806 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4809 if (selectionType() == TreeItem::Branch ||
4810 selectionType() == TreeItem::Image)
4812 QModelIndex ix=getSelectedIndex();
4814 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4819 if (selectionType() == TreeItem::Branch )
4821 selectLastSelectedBranch();
4826 updateSelection (oldsel);
4830 void VymModel::selectRightBranch()
4832 if (selection.isBlocked() ) return;
4834 QItemSelection oldsel=selModel->selection();
4837 LinkableMapObj *sel=getSelectedBranch();
4840 if (selectionType()== TreeItem::MapCenter)
4842 QModelIndex ix=getSelectedIndex();
4843 selModel->select (index (0,0,ix),QItemSelectionModel::ClearAndSelect );
4846 par=(BranchObj*)(sel->getParObj());
4847 if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
4850 if (selectionType() == TreeItem::Branch )
4852 selectLastSelectedBranch();
4858 if (selectionType() == TreeItem::Branch ||
4859 selectionType() == TreeItem::Image)
4861 QModelIndex ix=getSelectedIndex();
4863 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4867 updateSelection (oldsel);
4871 void VymModel::selectFirstBranch()
4873 TreeItem *ti=getSelectedBranchItem();
4876 TreeItem *par=ti->parent();
4878 TreeItem *ti2=par->getFirstBranch();
4882 ensureSelectionVisible();
4888 void VymModel::selectLastBranch()
4890 TreeItem *ti=getSelectedBranchItem();
4893 TreeItem *par=ti->parent();
4895 TreeItem *ti2=par->getLastBranch();
4899 ensureSelectionVisible();
4905 void VymModel::selectLastSelectedBranch()
4907 TreeItem *ti=getSelectedBranchItem();
4910 ti=ti->getLastSelectedBranch();
4911 if (ti) select (ti);
4915 void VymModel::selectParent()
4917 TreeItem *ti=getSelectedItem();
4925 ensureSelectionVisible();
4930 TreeItem::Type VymModel::selectionType()
4932 QModelIndexList list=selModel->selectedIndexes();
4933 if (list.isEmpty()) return TreeItem::Undefined;
4934 TreeItem *ti = getItem (list.first() );
4935 return ti->getType();
4939 LinkableMapObj* VymModel::getSelectedLMO()
4941 QModelIndexList list=selModel->selectedIndexes();
4942 if (!list.isEmpty() )
4944 TreeItem *ti = getItem (list.first() );
4945 TreeItem::Type type=ti->getType();
4946 if (type ==TreeItem::Branch || type==TreeItem::MapCenter || type==TreeItem::Image)
4948 return ti->getLMO();
4954 BranchObj* VymModel::getSelectedBranch()
4956 TreeItem *ti = getSelectedBranchItem();
4958 return (BranchObj*)ti->getLMO();
4963 TreeItem* VymModel::getSelectedBranchItem()
4965 QModelIndexList list=selModel->selectedIndexes();
4966 if (!list.isEmpty() )
4968 TreeItem *ti = getItem (list.first() );
4969 TreeItem::Type type=ti->getType();
4970 if (type ==TreeItem::Branch || type==TreeItem::MapCenter)
4976 TreeItem* VymModel::getSelectedItem()
4978 QModelIndexList list=selModel->selectedIndexes();
4979 if (!list.isEmpty() )
4980 return getItem (list.first() );
4985 QModelIndex VymModel::getSelectedIndex()
4987 QModelIndexList list=selModel->selectedIndexes();
4988 if (list.isEmpty() )
4989 return QModelIndex();
4991 return list.first();
4994 FloatImageObj* VymModel::getSelectedFloatImage()
4996 return selection.getFloatImage();
4999 QString VymModel::getSelectString ()
5001 LinkableMapObj *lmo=getSelectedLMO();
5003 return getSelectString(lmo);
5008 QString VymModel::getSelectString (LinkableMapObj *lmo) // FIXME-2 VM needs to use TreeModel
5012 if (typeid(*lmo)==typeid(BranchObj) ||
5013 typeid(*lmo)==typeid(MapCenterObj) )
5015 LinkableMapObj *par=lmo->getParObj();
5018 if (lmo->getDepth() ==1)
5019 // Mainbranch, return
5020 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
5022 // Branch, call myself recursively
5023 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
5027 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
5028 if (i>=0) s=QString("mc:%1").arg(i);