1 #include <QApplication>
7 #include "branchitem.h"
8 #include "editxlinkdialog.h"
10 #include "exportxhtmldialog.h"
12 #include "geometry.h" // for addBBox
13 #include "mainwindow.h"
17 #include "warningdialog.h"
18 #include "xml-freemind.h"
24 extern Main *mainWindow;
25 extern Settings settings;
26 extern QString tmpVymDir;
28 extern TextEditor *textEditor;
29 extern FlagRow *standardFlagsMaster;
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 Settings settings;
48 int VymModel::mapNum=0; // make instance
52 // cout << "Const VymModel\n";
54 rootItem->setModel (this);
60 cout << "Destr VymModel\n";
61 autosaveTimer->stop();
62 fileChangedTimer->stop();
66 void VymModel::clear()
68 selModel->clearSelection();
70 //QModelIndex ri=index(rootItem);
71 //removeRows (0, rowCount(ri),ri); // FIXME-3 here should be at least a beginRemoveRows...
74 void VymModel::init ()
76 // We should have at least one map center to start with
77 // addMapCenter(); FIXME-2 VM create this in MapEditor as long as model is part of that
82 // Also no scene yet (should not be needed anyway) FIXME-3 VM
95 stepsTotal=settings.readNumEntry("/history/stepsTotal",100);
96 undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
97 mainWindow->updateHistory (undoSet);
100 makeTmpDirectories();
105 fileName=tr("unnamed");
107 blockReposition=false;
108 blockSaveState=false;
110 autosaveTimer=new QTimer (this);
111 connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
113 fileChangedTimer=new QTimer (this);
114 fileChangedTimer->start(3000);
115 connect(fileChangedTimer, SIGNAL(timeout()), this, SLOT(fileChanged()));
120 selectionBlocked=false;
128 animationUse=settings.readBoolEntry("/animation/use",false);
129 animationTicks=settings.readNumEntry("/animation/ticks",10);
130 animationInterval=settings.readNumEntry("/animation/interval",50);
132 animationTimer=new QTimer (this);
133 connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
136 defLinkColor=QColor (0,0,255);
137 defXLinkColor=QColor (180,180,180);
138 linkcolorhint=LinkableMapObj::DefaultColor;
139 linkstyle=LinkableMapObj::PolyParabel;
141 defXLinkColor=QColor (230,230,230);
143 hidemode=TreeItem::HideNone;
149 // addMapCenter(); FIXME-2 VM create this in MapEditor until BO and MCO are independent of scene
153 void VymModel::makeTmpDirectories()
155 // Create unique temporary directories
156 tmpMapDir = tmpVymDir+QString("/model-%1").arg(mapNum);
157 histPath = tmpMapDir+"/history";
163 MapEditor* VymModel::getMapEditor() // FIXME-2 VM better return favourite editor here
168 bool VymModel::isRepositionBlocked()
170 return blockReposition;
173 void VymModel::updateActions() // FIXME-2 maybe don't update if blockReposition is set
175 //cout << "VM::updateActions \n";
176 // Tell mainwindow to update states of actions
177 mainWindow->updateActions();
182 QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, TreeItem *saveSel)
184 // tmpdir temporary directory to which data will be written
185 // prefix mapname, which will be appended to images etc.
186 // writeflags Only write flags for "real" save of map, not undo
187 // offset offset of bbox of whole map in scene.
188 // Needed for XML export
197 case LinkableMapObj::Line:
200 case LinkableMapObj::Parabel:
203 case LinkableMapObj::PolyLine:
207 ls="StylePolyParabel";
211 QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
213 if (linkcolorhint==LinkableMapObj::HeadingColor)
214 colhint=xml.attribut("linkColorHint","HeadingColor");
216 QString mapAttr=xml.attribut("version",vymVersion);
218 mapAttr+= xml.attribut("author",author) +
219 xml.attribut("comment",comment) +
220 xml.attribut("date",getDate()) +
221 xml.attribut("branchCount", QString().number(branchCount())) +
222 xml.attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
223 xml.attribut("selectionColor", mapEditor->getSelectionColor().name() ) +
224 xml.attribut("linkStyle", ls ) +
225 xml.attribut("linkColor", defLinkColor.name() ) +
226 xml.attribut("defXLinkColor", defXLinkColor.name() ) +
227 xml.attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
229 s+=xml.beginElement("vymmap",mapAttr);
232 // Find the used flags while traversing the tree // FIXME-2 this can be done local to vymmodel maybe...
233 standardFlagsMaster->resetUsedCounter();
235 // Reset the counters before saving
236 // TODO constr. of FIO creates lots of objects, better do this in some other way...
237 ImageItem().resetSaveCounter();// FIXME-2 this can be done local to vymmodel maybe...
239 // Build xml recursivly
240 if (!saveSel || saveSel->getType()==TreeItem::MapCenter)
241 // Save all mapcenters as complete map, if saveSel not set
242 s+=saveTreeToDir(tmpdir,prefix,writeflags,offset);
245 if (saveSel->isBranchLikeType())
247 s+=((BranchItem*)saveSel)->saveToDir(tmpdir,prefix,offset);
248 //FIXME-2 else if (saveSel->getType()==TreeItem::Image)
250 //s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
253 // Save local settings
254 s+=settings.getDataXML (destPath);
257 if (getSelectedItem() && !saveSel )
258 s+=xml.valueElement("select",getSelectString());
261 s+=xml.endElement("vymmap");
263 //cout << s.toStdString() << endl;
265 if (writeflags) standardFlagsMaster->saveToDir (tmpdir+"/flags/","",writeflags);
269 QString VymModel::saveTreeToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset) // FIXME-4 verbose not needed (used to write icons)
273 for (int i=0; i<rootItem->branchCount(); i++)
274 s+=rootItem->getBranchNum(i)->saveToDir (tmpdir,prefix,offset);
278 void VymModel::setFilePath(QString fpath, QString destname)
280 if (fpath.isEmpty() || fpath=="")
287 filePath=fpath; // becomes absolute path
288 fileName=fpath; // gets stripped of path
289 destPath=destname; // needed for vymlinks and during load to reset fileChangedTime
291 // If fpath is not an absolute path, complete it
292 filePath=QDir(fpath).absPath();
293 fileDir=filePath.left (1+filePath.findRev ("/"));
295 // Set short name, too. Search from behind:
296 int i=fileName.findRev("/");
297 if (i>=0) fileName=fileName.remove (0,i+1);
299 // Forget the .vym (or .xml) for name of map
300 mapName=fileName.left(fileName.findRev(".",-1,true) );
304 void VymModel::setFilePath(QString fpath)
306 setFilePath (fpath,fpath);
309 QString VymModel::getFilePath()
314 QString VymModel::getFileName()
319 QString VymModel::getMapName()
324 QString VymModel::getDestPath()
329 ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
331 ErrorCode err=success;
333 parseBaseHandler *handler;
337 case VymMap: handler=new parseVYMHandler; break;
338 case FreemindMap : handler=new parseFreemindHandler; break;
340 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
341 "Unknown FileType in VymModel::load()");
345 bool zipped_org=zipped;
349 selModel->clearSelection();
350 // FIXME-2 VM not needed??? model->setMapEditor(this);
351 // (map state is set later at end of load...)
354 BranchItem *bi=getSelectedBranchItem();
355 if (!bi) return aborted;
356 if (lmode==ImportAdd)
357 saveStateChangingPart(
360 QString("addMapInsert (%1)").arg(fname),
361 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bi)));
363 saveStateChangingPart(
366 QString("addMapReplace(%1)").arg(fname),
367 QString("Add map %1 to %2").arg(fname).arg(getObjectName(bi)));
371 // Create temporary directory for packing
373 QString tmpZipDir=makeTmpDir (ok,"vym-pack");
376 QMessageBox::critical( 0, tr( "Critical Load Error" ),
377 tr("Couldn't create temporary directory before load\n"));
382 err=unzipDir (tmpZipDir,fname);
392 // Look for mapname.xml
393 xmlfile= fname.left(fname.findRev(".",-1,true));
394 xmlfile=xmlfile.section( '/', -1 );
395 QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
396 if (!mfile.exists() )
398 // mapname.xml does not exist, well,
399 // maybe someone renamed the mapname.vym file...
400 // Try to find any .xml in the toplevel
401 // directory of the .vym file
402 QStringList flist=QDir (tmpZipDir).entryList("*.xml");
403 if (flist.count()==1)
405 // Only one entry, take this one
406 xmlfile=tmpZipDir + "/"+flist.first();
409 for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it )
410 *it=tmpZipDir + "/" + *it;
411 // TODO Multiple entries, load all (but only the first one into this ME)
412 //mainWindow->fileLoadFromTmp (flist);
413 //returnCode=1; // Silently forget this attempt to load
414 qWarning ("MainWindow::load (fn) multimap found...");
417 if (flist.isEmpty() )
419 QMessageBox::critical( 0, tr( "Critical Load Error" ),
420 tr("Couldn't find a map (*.xml) in .vym archive.\n"));
423 } //file doesn't exist
425 xmlfile=mfile.name();
428 QFile file( xmlfile);
430 // I am paranoid: file should exist anyway
431 // according to check in mainwindow.
434 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
435 tr(QString("Couldn't open map %1").arg(file.name())));
439 bool blockSaveStateOrg=blockSaveState;
440 blockReposition=true;
442 mapEditor->setViewportUpdateMode (QGraphicsView::NoViewportUpdate);
443 QXmlInputSource source( file);
444 QXmlSimpleReader reader;
445 reader.setContentHandler( handler );
446 reader.setErrorHandler( handler );
447 handler->setModel ( this);
450 // We need to set the tmpDir in order to load files with rel. path
455 tmpdir=fname.left(fname.findRev("/",-1));
456 handler->setTmpDir (tmpdir);
457 handler->setInputFile (file.name());
458 handler->setLoadMode (lmode);
459 bool ok = reader.parse( source );
460 blockReposition=false;
461 blockSaveState=blockSaveStateOrg;
462 mapEditor->setViewportUpdateMode (QGraphicsView::MinimalViewportUpdate);
466 reposition(); // FIXME-2 VM reposition the view instead...
467 emitSelectionChanged();
473 autosaveTimer->stop();
476 // Reset timestamp to check for later updates of file
477 fileChangedTime=QFileInfo (destPath).lastModified();
480 QMessageBox::critical( 0, tr( "Critical Parse Error" ),
481 tr( handler->errorProtocol() ) );
483 // Still return "success": the map maybe at least
484 // partially read by the parser
489 removeDir (QDir(tmpZipDir));
491 // Restore original zip state
498 ErrorCode VymModel::save (const SaveMode &savemode)
502 QString safeFilePath;
504 ErrorCode err=success;
508 mapFileName=mapName+".xml";
510 // use name given by user, even if he chooses .doc
511 mapFileName=fileName;
513 // Look, if we should zip the data:
516 QMessageBox mb( vymName,
517 tr("The map %1\ndid not use the compressed "
518 "vym file format.\nWriting it uncompressed will also write images \n"
519 "and flags and thus may overwrite files in the "
520 "given directory\n\nDo you want to write the map").arg(filePath),
521 QMessageBox::Warning,
522 QMessageBox::Yes | QMessageBox::Default,
524 QMessageBox::Cancel | QMessageBox::Escape);
525 mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
526 mb.setButtonText( QMessageBox::No, tr("uncompressed") );
527 mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
530 case QMessageBox::Yes:
531 // save compressed (default file format)
534 case QMessageBox::No:
538 case QMessageBox::Cancel:
545 // First backup existing file, we
546 // don't want to add to old zip archives
550 if ( settings.value ("/mapeditor/writeBackupFile").toBool())
552 QString backupFileName(destPath + "~");
553 QFile backupFile(backupFileName);
554 if (backupFile.exists() && !backupFile.remove())
556 QMessageBox::warning(0, tr("Save Error"),
557 tr("%1\ncould not be removed before saving").arg(backupFileName));
559 else if (!f.rename(backupFileName))
561 QMessageBox::warning(0, tr("Save Error"),
562 tr("%1\ncould not be renamed before saving").arg(destPath));
569 // Create temporary directory for packing
571 tmpZipDir=makeTmpDir (ok,"vym-zip");
574 QMessageBox::critical( 0, tr( "Critical Load Error" ),
575 tr("Couldn't create temporary directory before save\n"));
579 safeFilePath=filePath;
580 setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
583 // Create mapName and fileDir
584 makeSubDirs (fileDir);
587 if (savemode==CompleteMap || selModel->selection().isEmpty())
590 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
593 autosaveTimer->stop();
598 if (selectionType()==TreeItem::Image)
601 saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),getSelectedBranchItem());
602 // TODO take care of multiselections
605 if (!saveStringToDisk(fileDir+mapFileName,saveFile))
608 qWarning ("ME::saveStringToDisk failed!");
614 if (err==success) err=zipDir (tmpZipDir,destPath);
617 removeDir (QDir(tmpZipDir));
619 // Restore original filepath outside of tmp zip dir
620 setFilePath (safeFilePath);
624 fileChangedTime=QFileInfo (destPath).lastModified();
628 void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path) // FIXME-1 test e.g. with undo color subtree
630 QString pathDir=path.left(path.findRev("/"));
636 // We need to parse saved XML data
637 parseVYMHandler handler;
638 QXmlInputSource source( file);
639 QXmlSimpleReader reader;
640 reader.setContentHandler( &handler );
641 reader.setErrorHandler( &handler );
642 handler.setModel ( this);
643 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
644 if (undoSel.isEmpty())
648 handler.setLoadMode (NewMap);
652 handler.setLoadMode (ImportReplace);
654 blockReposition=true;
655 bool ok = reader.parse( source );
656 blockReposition=false;
659 // This should never ever happen
660 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
661 handler.errorProtocol());
664 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
667 void VymModel::addMapInsertInt (const QString &path, int pos)
669 /* FIXME-2 addMapInsertInt not ported yet
670 BranchObj *sel=getSelectedBranch();
673 QString pathDir=path.left(path.findRev("/"));
679 // We need to parse saved XML data
680 parseVYMHandler handler;
681 QXmlInputSource source( file);
682 QXmlSimpleReader reader;
683 reader.setContentHandler( &handler );
684 reader.setErrorHandler( &handler );
685 handler.setModel (this);
686 handler.setTmpDir ( pathDir ); // needed to load files with rel. path
687 handler.setLoadMode (ImportAdd);
688 blockReposition=true;
689 bool ok = reader.parse( source );
690 blockReposition=false;
693 // This should never ever happen
694 QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
695 handler.errorProtocol());
697 if (sel->getDepth()>0)
698 sel->getLastBranch()->linkTo (sel,pos);
700 QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
705 ImageItem* VymModel::loadFloatImageInt (BranchItem *dst,QString fn)
707 ImageItem *ii=createImage(dst);
717 void VymModel::loadFloatImage ()
719 BranchItem *selbi=getSelectedBranchItem();
723 Q3FileDialog *fd=new Q3FileDialog( NULL); // FIXME-4 get rid of Q3FileDialog
724 fd->setMode (Q3FileDialog::ExistingFiles);
725 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
726 ImagePreview *p =new ImagePreview (fd);
727 fd->setContentsPreviewEnabled( TRUE );
728 fd->setContentsPreview( p, p );
729 fd->setPreviewMode( Q3FileDialog::Contents );
730 fd->setCaption(vymName+" - " +tr("Load image"));
731 fd->setDir (lastImageDir);
734 if ( fd->exec() == QDialog::Accepted )
736 // TODO loadFIO in QT4 use: lastImageDir=fd->directory();
737 lastImageDir=QDir (fd->dirPath());
740 for (int j=0; j<fd->selectedFiles().count(); j++)
742 s=fd->selectedFiles().at(j);
743 ii=loadFloatImageInt (selbi,s);
744 //FIXME-3 check savestate for loadImage
750 QString ("loadImage (%1)").arg(s ),
751 QString("Add image %1 to %2").arg(s).arg(getObjectName(selbi))
754 // TODO loadFIO error handling
755 qWarning ("Failed to load "+s);
763 void VymModel::saveFloatImageInt (ImageItem *ii, const QString &type, const QString &fn)
768 void VymModel::saveFloatImage ()
770 ImageItem *ii=getSelectedImageItem();
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 (ii,fd->selectedFilter(),fn );
816 void VymModel::importDirInt(BranchObj *dst, QDir d)
818 /* FIXME-2 importDirInt not ported yet
819 BranchObj *bo=getSelectedBranch();
822 // Traverse directories
823 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
824 QFileInfoList list = d.entryInfoList();
827 for (int i = 0; i < list.size(); ++i)
830 if (fi.fileName() != "." && fi.fileName() != ".." )
833 bo=dst->getLastBranch();
834 BranchItem *bi=(BranchItem*)(bo->getTreeItem());
835 bi->setHeading (fi.fileName() ); // FIXME-3 check this
836 bo->setColor (QColor("blue"));
838 if ( !d.cd(fi.fileName()) )
839 QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
842 // Recursively add subdirs
849 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
850 list = d.entryInfoList();
852 for (int i = 0; i < list.size(); ++i)
856 bo=dst->getLastBranch();
857 bo->setHeading (fi.fileName() );
858 bo->setColor (QColor("black"));
859 if (fi.fileName().right(4) == ".vym" )
860 bo->setVymLink (fi.filePath());
866 void VymModel::importDirInt (const QString &s) // FIXME-2
869 BranchObj *bo=getSelectedBranch();
872 saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
880 void VymModel::importDir() //FIXME-2
883 BranchObj *bo=getSelectedBranch();
887 filters <<"VYM map (*.vym)";
888 QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
889 fd->setMode (QFileDialog::DirectoryOnly);
890 fd->setFilters (filters);
891 fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
895 if ( fd->exec() == QDialog::Accepted )
897 importDirInt (fd->selectedFile() );
899 //FIXME-3 VM needed? scene()->update();
906 void VymModel::autosave()
911 cout << "VymModel::autosave rejected due to missing filePath\n";
914 QDateTime now=QDateTime().currentDateTime();
916 // Disable autosave, while we have gone back in history
917 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
918 if (redosAvail>0) return;
920 // Also disable autosave for new map without filename
921 if (filePath.isEmpty()) return;
924 if (mapUnsaved &&mapChanged && settings.value ("/mainwindow/autosave/use",true).toBool() )
926 if (QFileInfo(filePath).lastModified()<=fileChangedTime)
927 mainWindow->fileSave (this);
930 cout <<" ME::autosave rejected, file on disk is newer than last save.\n";
935 void VymModel::fileChanged()
937 // Check if file on disk has changed meanwhile
938 if (!filePath.isEmpty())
940 QDateTime tmod=QFileInfo (filePath).lastModified();
941 if (tmod>fileChangedTime)
943 // FIXME-2 VM switch to current mapeditor and finish lineedits...
944 QMessageBox mb( vymName,
945 tr("The file of the map on disk has changed:\n\n"
946 " %1\n\nDo you want to reload that map with the new file?").arg(filePath),
947 QMessageBox::Question,
949 QMessageBox::Cancel | QMessageBox::Default,
950 QMessageBox::NoButton );
952 mb.setButtonText( QMessageBox::Yes, tr("Reload"));
953 mb.setButtonText( QMessageBox::No, tr("Ignore"));
956 case QMessageBox::Yes:
958 load (filePath,NewMap,fileType);
959 case QMessageBox::Cancel:
960 fileChangedTime=tmod; // allow autosave to overwrite newer file!
966 bool VymModel::isDefault()
971 void VymModel::makeDefault()
977 bool VymModel::hasChanged()
982 void VymModel::setChanged()
985 autosaveTimer->start(settings.value("/mainwindow/autosave/ms/",300000).toInt());
989 latestAddedItem=NULL;
993 QString VymModel::getObjectName (LinkableMapObj *lmo) // FIXME-3 should be obsolete
995 if (!lmo || !lmo->getTreeItem() ) return QString();
996 return getObjectName (lmo->getTreeItem() );
1000 QString VymModel::getObjectName (TreeItem *ti)
1003 if (!ti) return QString("Error: NULL has no name!");
1005 if (s=="") s="unnamed";
1007 if (ti->isBranchLikeType() ) //FIXME-3 shouldnt there also be mapcenter??
1008 return QString("branch (%1)").arg(s);
1009 if (ti->getType()==TreeItem::Image)
1010 return QString ("image (%1)").arg(ti->getHeading());
1011 return QString("Unknown type has no name!");
1014 void VymModel::redo()
1016 // Can we undo at all?
1017 if (redosAvail<1) return;
1019 bool blockSaveStateOrg=blockSaveState;
1020 blockSaveState=true;
1024 if (undosAvail<stepsTotal) undosAvail++;
1026 if (curStep>stepsTotal) curStep=1;
1027 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1028 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1029 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1030 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1031 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1032 QString version=undoSet.readEntry ("/history/version");
1034 /* TODO Maybe check for version, if we save the history
1035 if (!checkVersion(version))
1036 QMessageBox::warning(0,tr("Warning"),
1037 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1040 // Find out current undo directory
1041 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1045 cout << "VymModel::redo() begin\n";
1046 cout << " undosAvail="<<undosAvail<<endl;
1047 cout << " redosAvail="<<redosAvail<<endl;
1048 cout << " curStep="<<curStep<<endl;
1049 cout << " ---------------------------"<<endl;
1050 cout << " comment="<<comment.toStdString()<<endl;
1051 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1052 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1053 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1054 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1055 cout << " ---------------------------"<<endl<<endl;
1058 // select object before redo
1059 if (!redoSelection.isEmpty())
1060 select (redoSelection);
1063 parseAtom (redoCommand);
1066 blockSaveState=blockSaveStateOrg;
1068 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1069 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1070 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1071 undoSet.writeSettings(histPath);
1073 mainWindow->updateHistory (undoSet);
1076 /* TODO remove testing
1077 cout << "ME::redo() end\n";
1078 cout << " undosAvail="<<undosAvail<<endl;
1079 cout << " redosAvail="<<redosAvail<<endl;
1080 cout << " curStep="<<curStep<<endl;
1081 cout << " ---------------------------"<<endl<<endl;
1087 bool VymModel::isRedoAvailable()
1089 if (undoSet.readNumEntry("/history/redosAvail",0)>0)
1095 void VymModel::undo()
1097 // Can we undo at all?
1098 if (undosAvail<1) return;
1100 mainWindow->statusMessage (tr("Autosave disabled during undo."));
1102 bool blockSaveStateOrg=blockSaveState;
1103 blockSaveState=true;
1105 QString undoCommand= undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
1106 QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
1107 QString redoCommand= undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
1108 QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
1109 QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
1110 QString version=undoSet.readEntry ("/history/version");
1112 /* TODO Maybe check for version, if we save the history
1113 if (!checkVersion(version))
1114 QMessageBox::warning(0,tr("Warning"),
1115 tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
1118 // Find out current undo directory
1119 QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
1121 // select object before undo
1122 if (!undoSelection.isEmpty())
1123 select (undoSelection);
1127 cout << "VymModel::undo() begin\n";
1128 cout << " undosAvail="<<undosAvail<<endl;
1129 cout << " redosAvail="<<redosAvail<<endl;
1130 cout << " curStep="<<curStep<<endl;
1131 cout << " ---------------------------"<<endl;
1132 cout << " comment="<<comment.toStdString()<<endl;
1133 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1134 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1135 cout << " redoCom="<<redoCommand.toStdString()<<endl;
1136 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1137 cout << " ---------------------------"<<endl<<endl;
1139 parseAtom (undoCommand);
1144 if (curStep<1) curStep=stepsTotal;
1148 blockSaveState=blockSaveStateOrg;
1149 /* TODO remove testing
1150 cout << "VymModel::undo() end\n";
1151 cout << " undosAvail="<<undosAvail<<endl;
1152 cout << " redosAvail="<<redosAvail<<endl;
1153 cout << " curStep="<<curStep<<endl;
1154 cout << " ---------------------------"<<endl<<endl;
1157 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1158 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1159 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1160 undoSet.writeSettings(histPath);
1162 mainWindow->updateHistory (undoSet);
1164 emitSelectionChanged();
1167 bool VymModel::isUndoAvailable()
1169 if (undoSet.readNumEntry("/history/undosAvail",0)>0)
1175 void VymModel::gotoHistoryStep (int i)
1177 // Restore variables
1178 int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
1179 int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
1181 if (i<0) i=undosAvail+redosAvail;
1183 // Clicking above current step makes us undo things
1186 for (int j=0; j<undosAvail-i; j++) undo();
1189 // Clicking below current step makes us redo things
1191 for (int j=undosAvail; j<i; j++)
1193 if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
1197 // And ignore clicking the current row ;-)
1201 QString VymModel::getHistoryPath()
1203 QString histName(QString("history-%1").arg(curStep));
1204 return (tmpMapDir+"/"+histName);
1207 void VymModel::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, TreeItem *saveSel)
1209 sendData(redoCom); //FIXME-3 testing
1214 if (blockSaveState) return;
1216 if (debug) cout << "VM::saveState() for "<<qPrintable (mapName)<<endl;
1218 // Find out current undo directory
1219 if (undosAvail<stepsTotal) undosAvail++;
1221 if (curStep>stepsTotal) curStep=1;
1223 QString backupXML="";
1224 QString histDir=getHistoryPath();
1225 QString bakMapPath=histDir+"/map.xml";
1227 // Create histDir if not available
1230 makeSubDirs (histDir);
1232 // Save depending on how much needs to be saved
1234 backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
1236 QString undoCommand="";
1237 if (savemode==UndoCommand)
1239 undoCommand=undoCom;
1241 else if (savemode==PartOfMap )
1243 undoCommand=undoCom;
1244 undoCommand.replace ("PATH",bakMapPath);
1248 if (!backupXML.isEmpty())
1249 // Write XML Data to disk
1250 saveStringToDisk (bakMapPath,backupXML);
1252 // We would have to save all actions in a tree, to keep track of
1253 // possible redos after a action. Possible, but we are too lazy: forget about redos.
1256 // Write the current state to disk
1257 undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
1258 undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
1259 undoSet.setEntry ("/history/curStep",QString::number(curStep));
1260 undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
1261 undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
1262 undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
1263 undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
1264 undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
1265 undoSet.setEntry (QString("/history/version"),vymVersion);
1266 undoSet.writeSettings(histPath);
1270 // TODO remove after testing
1271 //cout << " into="<< histPath.toStdString()<<endl;
1272 cout << " stepsTotal="<<stepsTotal<<
1273 ", undosAvail="<<undosAvail<<
1274 ", redosAvail="<<redosAvail<<
1275 ", curStep="<<curStep<<endl;
1276 cout << " ---------------------------"<<endl;
1277 cout << " comment="<<comment.toStdString()<<endl;
1278 cout << " undoCom="<<undoCommand.toStdString()<<endl;
1279 cout << " undoSel="<<undoSelection.toStdString()<<endl;
1280 cout << " redoCom="<<redoCom.toStdString()<<endl;
1281 cout << " redoSel="<<redoSelection.toStdString()<<endl;
1282 if (saveSel) cout << " saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
1283 cout << " ---------------------------"<<endl;
1286 mainWindow->updateHistory (undoSet);
1292 void VymModel::saveStateChangingPart(TreeItem *undoSel, TreeItem* redoSel, const QString &rc, const QString &comment)
1294 // save the selected part of the map, Undo will replace part of map
1295 QString undoSelection="";
1297 undoSelection=getSelectString(undoSel);
1299 qWarning ("VymModel::saveStateChangingPart no undoSel given!");
1300 QString redoSelection="";
1302 redoSelection=getSelectString(undoSel);
1304 qWarning ("VymModel::saveStateChangingPart no redoSel given!");
1307 saveState (PartOfMap,
1308 undoSelection, "addMapReplace (\"PATH\")",
1314 void VymModel::saveStateRemovingPart(TreeItem* redoSel, const QString &comment)
1318 qWarning ("VymModel::saveStateRemovingPart no redoSel given!");
1321 QString undoSelection=getSelectString (redoSel->parent() );
1322 QString redoSelection=getSelectString(redoSel);
1323 if (redoSel->getType()==TreeItem::Branch)
1325 // save the selected branch of the map, Undo will insert part of map
1326 saveState (PartOfMap,
1327 undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(redoSel->num()),
1328 redoSelection, "delete ()",
1335 void VymModel::saveState(TreeItem *undoSel, const QString &uc, TreeItem *redoSel, const QString &rc, const QString &comment)
1337 // "Normal" savestate: save commands, selections and comment
1338 // so just save commands for undo and redo
1339 // and use current selection
1341 QString redoSelection="";
1342 if (redoSel) redoSelection=getSelectString(redoSel);
1343 QString undoSelection="";
1344 if (undoSel) undoSelection=getSelectString(undoSel);
1346 saveState (UndoCommand,
1353 void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment)
1355 // "Normal" savestate: save commands, selections and comment
1356 // so just save commands for undo and redo
1357 // and use current selection
1358 saveState (UndoCommand,
1365 void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment)
1367 // "Normal" savestate applied to model (no selection needed):
1368 // save commands and comment
1369 saveState (UndoCommand,
1377 QGraphicsScene* VymModel::getScene ()
1382 TreeItem* VymModel::findBySelectString(QString s)
1384 if (s.isEmpty() ) return NULL;
1386 // Old maps don't have multiple mapcenters and don't save full path
1387 if (s.left(2) !="mc")
1390 QStringList parts=s.split (",");
1393 TreeItem *ti=rootItem;
1395 while (!parts.isEmpty() )
1397 typ=parts.first().left(2);
1398 n=parts.first().right(parts.first().length() - 3).toInt();
1399 parts.removeFirst();
1400 if (typ=="mc" || typ=="bo")
1401 ti=ti->getBranchNum (n);
1405 ti=ti->getImageNum (n);
1411 TreeItem* VymModel::findID (const QString &s)
1414 for (int i=0; i<rootItem->branchCount(); i++)
1416 ti=rootItem->getBranchNum(i)->findID (s);
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)
1440 QString VymModel::getAuthor()
1445 void VymModel::setComment (const QString &s)
1448 QString ("setMapComment (\"%1\")").arg(comment),
1449 QString ("setMapComment (\"%1\")").arg(s),
1450 QString ("Set comment of map")
1455 QString VymModel::getComment ()
1460 QString VymModel::getDate ()
1462 return QDate::currentDate().toString ("yyyy-MM-dd");
1465 int VymModel::branchCount() // FIXME-4 Optimize this: use internal counter instead of going through whole map each time...
1468 BranchItem *cur=NULL;
1469 BranchItem *prev=NULL;
1479 void VymModel::setHeading(const QString &s)
1481 BranchItem *selbi=getSelectedBranchItem();
1486 "setHeading (\""+selbi->getHeading()+"\")",
1488 "setHeading (\""+s+"\")",
1489 QString("Set heading of %1 to \"%2\"").arg(getObjectName(selbi)).arg(s) );
1490 selbi->setHeading(s );
1491 emitDataHasChanged ( selbi); //FIXME-3 maybe emit signal from TreeItem?
1494 emitSelectionChanged();
1498 BranchItem* VymModel::findText (QString s, bool cs)
1500 QTextDocument::FindFlags flags=0;
1501 if (cs) flags=QTextDocument::FindCaseSensitively;
1504 { // Nothing found or new find process
1506 // nothing found, start again
1510 next (findCurrent,findPrevious);
1512 bool searching=true;
1513 bool foundNote=false;
1514 while (searching && !EOFind)
1518 // Searching in Note
1519 if (findCurrent->getNote().contains(s,cs))
1521 select (findCurrent);
1523 if (getSelectedBranch()!=itFind)
1526 emitShowSelection();
1529 if (textEditor->findText(s,flags))
1535 // Searching in Heading
1536 if (searching && findCurrent->getHeading().contains (s,cs) )
1538 select(findCurrent);
1544 if (!next(findCurrent,findPrevious) )
1547 //cout <<"still searching... "<<qPrintable( itFind->getHeading())<<endl;
1550 return getSelectedBranchItem();
1555 void VymModel::findReset()
1556 { // Necessary if text to find changes during a find process
1562 void VymModel::setScene (QGraphicsScene *s)
1564 mapScene=s; // FIXME-2 VM should not be necessary anymore, move all occurences to MapEditor
1565 //init(); // Here we have a mapScene set,
1566 // which is (still) needed to create MapCenters
1569 void VymModel::setURL(const QString &url)
1571 TreeItem *selti=getSelectedItem();
1574 QString oldurl=selti->getURL();
1575 selti->setURL (url);
1578 QString ("setURL (\"%1\")").arg(oldurl),
1580 QString ("setURL (\"%1\")").arg(url),
1581 QString ("set URL of %1 to %2").arg(getObjectName(selti)).arg(url)
1584 emitDataHasChanged (selti);
1585 emitShowSelection();
1589 QString VymModel::getURL()
1591 TreeItem *selti=getSelectedItem();
1593 return selti->getURL();
1598 QStringList VymModel::getURLs()
1601 BranchItem *selbi=getSelectedBranchItem();
1602 BranchItem *cur=selbi;
1603 BranchItem *prev=NULL;
1606 if (!cur->getURL().isEmpty()) urls.append( cur->getURL());
1607 cur=next (cur,prev,selbi);
1613 void VymModel::setFrameType(const FrameObj::FrameType &t) //FIXME-4 not saved if there is no LMO
1615 BranchItem *bi=getSelectedBranchItem();
1618 BranchObj *bo=(BranchObj*)(bi->getLMO());
1621 QString s=bo->getFrameTypeName();
1622 bo->setFrameType (t);
1623 saveState (bi, QString("setFrameType (\"%1\")").arg(s),
1624 bi, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
1631 void VymModel::setFrameType(const QString &s) //FIXME-4 not saved if there is no LMO
1633 BranchItem *bi=getSelectedBranchItem();
1636 BranchObj *bo=(BranchObj*)(bi->getLMO());
1639 saveState (bi, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
1640 bi, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
1641 bo->setFrameType (s);
1648 void VymModel::setFramePenColor(const QColor &c) //FIXME-4 not saved if there is no LMO
1651 BranchItem *bi=getSelectedBranchItem();
1654 BranchObj *bo=(BranchObj*)(bi->getLMO());
1657 saveState (bi, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
1658 bi, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
1659 bo->setFramePenColor (c);
1664 void VymModel::setFrameBrushColor(const QColor &c) //FIXME-4 not saved if there is no LMO
1666 BranchItem *bi=getSelectedBranchItem();
1669 BranchObj *bo=(BranchObj*)(bi->getLMO());
1672 saveState (bi, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
1673 bi, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
1674 bo->setFrameBrushColor (c);
1679 void VymModel::setFramePadding (const int &i) //FIXME-4 not saved if there is no LMO
1681 BranchItem *bi=getSelectedBranchItem();
1684 BranchObj *bo=(BranchObj*)(bi->getLMO());
1687 saveState (bi, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
1688 bi, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
1689 bo->setFramePadding (i);
1696 void VymModel::setFrameBorderWidth(const int &i) //FIXME-4 not saved if there is no LMO
1698 BranchItem *bi=getSelectedBranchItem();
1701 BranchObj *bo=(BranchObj*)(bi->getLMO());
1704 saveState (bi, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
1705 bi, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
1706 bo->setFrameBorderWidth (i);
1713 void VymModel::setIncludeImagesVer(bool b) //FIXME-2
1716 BranchItem *bi=getSelectedBranchItem();
1719 BranchObj *bo=bi->getLMO();
1722 QString u= b ? "false" : "true";
1723 QString r=!b ? "false" : "true";
1727 QString("setIncludeImagesVertically (%1)").arg(u),
1729 QString("setIncludeImagesVertically (%1)").arg(r),
1730 QString("Include images vertically in %1").arg(getObjectName(bo))
1732 bo->setIncludeImagesVer(b);
1738 void VymModel::setIncludeImagesHor(bool b) //FIXME-2
1741 TreeItem *bi=getSelectedBranchItem();
1744 BranchObj *bo=bi->getLMO();
1747 QString u= b ? "false" : "true";
1748 QString r=!b ? "false" : "true";
1752 QString("setIncludeImagesHorizontally (%1)").arg(u),
1754 QString("setIncludeImagesHorizontally (%1)").arg(r),
1755 QString("Include images horizontally in %1").arg(getObjectName(bo))
1757 bo->setIncludeImagesHor(b);
1764 void VymModel::setHideLinkUnselected (bool b)//FIXME-2
1767 LinkableMapObj *sel=getSelectedLMO();
1769 (selectionType() == TreeItem::Branch ||
1770 selectionType() == TreeItem::MapCenter ||
1771 selectionType() == TreeItem::Image ))
1773 QString u= b ? "false" : "true";
1774 QString r=!b ? "false" : "true";
1778 QString("setHideLinkUnselected (%1)").arg(u),
1780 QString("setHideLinkUnselected (%1)").arg(r),
1781 QString("Hide link of %1 if unselected").arg(getObjectName(sel))
1783 sel->setHideLinkUnselected(b);
1788 void VymModel::setHideExport(bool b)
1790 TreeItem *ti=getSelectedItem();
1792 (ti->getType()==TreeItem::Image ||ti->isBranchLikeType()))
1794 ti->setHideInExport (b);
1795 QString u= b ? "false" : "true";
1796 QString r=!b ? "false" : "true";
1800 QString ("setHideExport (%1)").arg(u),
1802 QString ("setHideExport (%1)").arg(r),
1803 QString ("Set HideExport flag of %1 to %2").arg(getObjectName(ti)).arg (r)
1805 emitDataHasChanged(ti);
1806 emitSelectionChanged();
1809 // emitSelectionChanged();
1810 // FIXME-3 VM needed? scene()->update();
1814 void VymModel::toggleHideExport()
1816 TreeItem *selti=getSelectedItem();
1818 setHideExport ( !selti->hideInExport() );
1822 void VymModel::copy()
1824 TreeItem *selti=getSelectedItem();
1826 (selti->getType() == TreeItem::Branch ||
1827 selti->getType() == TreeItem::MapCenter ||
1828 selti->getType() == TreeItem::Image ))
1830 if (redosAvail == 0)
1833 QString s=getSelectString(selti);
1834 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",selti );
1835 curClipboard=curStep;
1838 // Copy also to global clipboard, because we are at last step in history
1839 QString bakMapName(QString("history-%1").arg(curStep));
1840 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1841 copyDir (bakMapDir,clipboardDir );
1843 clipboardEmpty=false;
1849 void VymModel::pasteNoSave(const int &n)
1851 bool old=blockSaveState;
1852 blockSaveState=true;
1853 bool zippedOrg=zipped;
1854 if (redosAvail > 0 || n!=0)
1856 // Use the "historical" buffer
1857 QString bakMapName(QString("history-%1").arg(n));
1858 QString bakMapDir(tmpMapDir +"/"+bakMapName);
1859 load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
1861 // Use the global buffer
1862 load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
1867 void VymModel::paste()
1869 BranchItem *selbi=getSelectedBranchItem();
1872 saveStateChangingPart(
1875 QString ("paste (%1)").arg(curClipboard),
1876 QString("Paste to %1").arg( getObjectName(selbi))
1883 void VymModel::cut()
1885 TreeItem *selti=getSelectedItem();
1886 if ( selti && (selti->isBranchLikeType() ||selti->getType()==TreeItem::Image))
1894 void VymModel::moveUp()
1896 BranchItem *selbi=getSelectedBranchItem();
1899 if (!selbi->canMoveUp()) return;
1900 QString oldsel=getSelectString();
1901 if (relinkBranch (selbi,(BranchItem*)selbi->parent(),selbi->num()-1) )
1903 saveState (getSelectString(),"moveDown ()",oldsel,"moveUp ()",QString("Move up %1").arg(getObjectName(selbi)));
1907 void VymModel::moveDown()
1909 BranchItem *selbi=getSelectedBranchItem();
1912 if (!selbi->canMoveDown()) return;
1913 QString oldsel=getSelectString();
1914 if ( relinkBranch (selbi,(BranchItem*)selbi->parent(),selbi->num()+1) )
1916 saveState (getSelectString(),"moveUp ()",oldsel,"moveDown ()",QString("Move down %1").arg(getObjectName(selbi)));
1920 void VymModel::sortChildren() // FIXME-2 not implemented yet
1923 BranchObj* bo=getSelectedBranch();
1926 if(treeItem->branchCount()>1)
1928 saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
1931 emitShowSelection();
1937 BranchItem* VymModel::createMapCenter()
1939 BranchItem *newbi=addMapCenter (QPointF (0,0) );
1943 BranchItem* VymModel::createBranch(BranchItem *dst)
1946 return addNewBranchInt (dst,-2);
1951 ImageItem* VymModel::createImage(BranchItem *dst)
1958 QList<QVariant> cData;
1959 cData << "new" << "undef"<<"undef";
1961 ImageItem *newii=new ImageItem(cData) ;
1962 //newii->setHeading (QApplication::translate("Heading of new image in map", "new image"));
1964 emit (layoutAboutToBeChanged() );
1967 n=dst->getRowNumAppend(newii);
1968 beginInsertRows (parix,n,n+1);
1969 dst->appendChild (newii);
1972 emit (layoutChanged() );
1974 // save scroll state. If scrolled, automatically select
1975 // new branch in order to tmp unscroll parent...
1976 newii->createMapObj(mapScene);
1983 BranchItem* VymModel::addMapCenter ()
1985 BranchItem *bi=addMapCenter (contextPos);
1987 emitShowSelection();
1992 QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
1993 QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
1998 BranchItem* VymModel::addMapCenter(QPointF absPos) //FIXME-2 absPos not used in context menu?!
1999 // createMapCenter could then probably be merged with createBranch
2003 QModelIndex parix=index(rootItem);
2005 int n=rootItem->branchCount();
2007 emit (layoutAboutToBeChanged() );
2008 beginInsertRows (parix,n,n+1);
2010 QList<QVariant> cData;
2011 cData << "VM:addMapCenter" << "undef"<<"undef";
2012 BranchItem *newbi=new BranchItem (cData);
2013 newbi->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
2014 rootItem->appendChild (newbi);
2017 emit (layoutChanged() );
2020 newbi->setPositionMode (MapItem::Absolute);
2021 newbi->createMapObj(mapScene);
2026 BranchItem* VymModel::addNewBranchInt(BranchItem *dst,int num)
2028 // Depending on pos:
2029 // -3 insert in children of parent above selection
2030 // -2 add branch to selection
2031 // -1 insert in children of parent below selection
2032 // 0..n insert in children of parent at pos
2035 QList<QVariant> cData;
2036 cData << "new" << "undef"<<"undef";
2041 BranchItem *newbi=new BranchItem (cData);
2042 newbi->setHeading (QApplication::translate("Heading of new branch in map", "new"));
2044 emit (layoutAboutToBeChanged() );
2050 n=parbi->getRowNumAppend (newbi);
2051 beginInsertRows (parix,n,n+1);
2052 parbi->appendChild (newbi);
2054 }else if (num==-1 || num==-3)
2056 // insert below selection
2057 parbi=(BranchItem*)dst->parent();
2060 n=dst->childNumber() + (3+num)/2; //-1 |-> 1;-3 |-> 0
2061 beginInsertRows (parix,n,n);
2062 parbi->insertBranch(n,newbi);
2065 emit (layoutChanged() );
2067 // save scroll state. If scrolled, automatically select
2068 // new branch in order to tmp unscroll parent...
2069 newbi->createMapObj(mapScene);
2074 BranchItem* VymModel::addNewBranch(int pos)
2076 // Different meaning than num in addNewBranchInt!
2080 BranchItem *newbi=NULL;
2081 BranchItem *selbi=getSelectedBranchItem();
2085 // FIXME-3 setCursor (Qt::ArrowCursor); //Still needed?
2087 newbi=addNewBranchInt (selbi,pos-2);
2095 QString ("addBranch (%1)").arg(pos),
2096 QString ("Add new branch to %1").arg(getObjectName(selbi)));
2099 // emitSelectionChanged(); FIXME-3
2100 latestAddedItem=newbi;
2101 // In Network mode, the client needs to know where the new branch is,
2102 // so we have to pass on this information via saveState.
2103 // TODO: Get rid of this positioning workaround
2104 /* FIXME-4 network problem: QString ps=qpointfToString (newbo->getAbsPos());
2105 sendData ("selectLatestAdded ()");
2106 sendData (QString("move %1").arg(ps));
2115 BranchItem* VymModel::addNewBranchBefore()
2117 BranchItem *newbi=NULL;
2118 BranchItem *selbi=getSelectedBranchItem();
2119 if (selbi && selbi->getType()==TreeItem::Branch)
2120 // We accept no MapCenter here, so we _have_ a parent
2122 //QPointF p=bo->getRelPos();
2125 // add below selection
2126 newbi=addNewBranchInt (selbi,-1);
2130 //newbi->move2RelPos (p);
2132 // Move selection to new branch
2133 relinkBranch (selbi,newbi,0);
2135 saveState (newbi, "deleteKeepChildren ()", newbi, "addBranchBefore ()",
2136 QString ("Add branch before %1").arg(getObjectName(selbi)));
2138 // FIXME-3 needed? reposition();
2139 // emitSelectionChanged(); FIXME-3
2145 bool VymModel::relinkBranch (BranchItem *branch, BranchItem *dst, int pos)
2149 if (branch->depth()==0)
2151 cout <<"VM::relinkBranch d=0 for "<<branch->getHeadingStd()<<endl;
2153 emit (layoutAboutToBeChanged() );
2154 BranchItem *branchpi=(BranchItem*)branch->parent();
2155 // Remove at current position
2156 int n=branch->childNum();
2157 beginRemoveRows (index(branchpi),n,n);
2158 branchpi->removeChild (n);
2161 if (pos<0 ||pos>dst->branchCount() ) pos=dst->branchCount();
2163 // Append as last branch to dst
2164 if (dst->branchCount()==0)
2167 n=dst->getFirstBranch()->childNumber();
2168 beginInsertRows (index(dst),n+pos,n+pos);
2169 dst->insertBranch (pos,branch);
2172 // reset parObj, fonts, frame, etc in related LMO or other view-objects
2173 branch->updateStyles();
2175 emit (layoutChanged() );
2176 reposition(); // both for moveUp/Down and relinking
2183 bool VymModel::relinkImage (ImageItem *image, BranchItem *dst)
2187 emit (layoutAboutToBeChanged() );
2189 BranchItem *pi=(BranchItem*)(image->parent());
2190 QString oldParString=getSelectString (pi);
2191 // Remove at current position
2192 int n=image->childNum();
2193 beginRemoveRows (index(pi),n,n);
2194 pi->removeChild (n);
2198 QModelIndex dstix=index(dst);
2199 n=dst->getRowNumAppend (image);
2200 beginInsertRows (dstix,n,n+1);
2201 dst->appendChild (image);
2204 // Set new parent also for lmo
2205 if (image->getLMO() && dst->getLMO() )
2206 image->getLMO()->setParObj (dst->getLMO() );
2208 emit (layoutChanged() );
2211 QString("relinkTo (\"%1\")").arg(oldParString),
2213 QString ("relinkTo (\"%1\")").arg(getSelectString (dst)),
2214 QString ("Relink floatimage to %1").arg(getObjectName(dst)));
2220 void VymModel::deleteSelection()
2222 BranchItem *selbi=getSelectedBranchItem();
2224 if (selbi && selbi->isBranchLikeType() )
2227 saveStateRemovingPart (selbi, QString ("Delete %1").arg(getObjectName(selbi)));
2229 TreeItem *pi=deleteItem (selbi);
2233 emitShowSelection();
2237 ImageItem *ii=getSelectedImageItem();
2240 BranchItem *pi=(BranchItem*)(ii->parent());
2241 saveStateChangingPart(
2245 QString("Delete %1").arg(getObjectName(ii))
2251 emitShowSelection();
2256 void VymModel::deleteKeepChildren() //FIXME-2 does not work yet for mapcenters
2259 BranchItem *selbi=getSelectedBranchItem();
2263 // Don't use this on mapcenter
2264 if (selbi->depth()<2) return;
2266 pi=(BranchItem*)(selbi->parent());
2267 // Check if we have childs at all to keep
2268 if (selbi->branchCount()==0)
2275 if (selbi->getLMO()) p=selbi->getLMO()->getRelPos();
2276 saveStateChangingPart(
2279 "deleteKeepChildren ()",
2280 QString("Remove %1 and keep its children").arg(getObjectName(selbi))
2283 QString sel=getSelectString(selbi);
2285 int pos=selbi->num();
2286 BranchItem *bi=selbi->getFirstBranch();
2289 relinkBranch (bi,pi,pos);
2290 bi=selbi->getFirstBranch();
2296 BranchObj *bo=getSelectedBranchObj();
2299 bo->move2RelPos (p);
2305 void VymModel::deleteChildren()
2308 BranchItem *selbi=getSelectedBranchItem();
2311 saveStateChangingPart(
2314 "deleteChildren ()",
2315 QString( "Remove children of branch %1").arg(getObjectName(selbi))
2317 emit (layoutAboutToBeChanged() );
2319 QModelIndex ix=index (selbi);
2320 int n=selbi->branchCount()-1;
2321 beginRemoveRows (ix,0,n);
2322 removeRows (0,n+1,ix);
2324 emit (layoutChanged() );
2329 TreeItem* VymModel::deleteItem (TreeItem *ti)
2333 TreeItem *pi=ti->parent();
2334 QModelIndex parentIndex=index(pi);
2336 emit (layoutAboutToBeChanged() );
2338 int n=ti->childNum();
2339 beginRemoveRows (parentIndex,n,n);
2340 removeRows (n,1,parentIndex);
2344 emit (layoutChanged() );
2345 if (pi->depth()>0) return pi;
2350 bool VymModel::scrollBranch(BranchItem *bi)
2354 if (bi->isScrolled()) return false;
2355 if (bi->branchCount()==0) return false;
2356 if (bi->depth()==0) return false;
2357 if (bi->toggleScroll())
2364 QString ("%1 ()").arg(u),
2366 QString ("%1 ()").arg(r),
2367 QString ("%1 %2").arg(r).arg(getObjectName(bi))
2369 emitDataHasChanged(bi);
2370 emitSelectionChanged();
2371 mapScene->update(); //Needed for _quick_ update, even in 1.13.x //FIXME-3 force update via signal...
2378 bool VymModel::unscrollBranch(BranchItem *bi)
2382 if (!bi->isScrolled()) return false;
2383 if (bi->branchCount()==0) return false;
2384 if (bi->depth()==0) return false;
2391 QString ("%1 ()").arg(u),
2393 QString ("%1 ()").arg(r),
2394 QString ("%1 %2").arg(r).arg(getObjectName(bi))
2397 emitDataHasChanged(bi);
2398 emitSelectionChanged();
2400 mapScene->update(); //Needed for _quick_ update, even in 1.13.x //FIXME-3 force update via signal...
2406 void VymModel::toggleScroll()
2408 BranchItem *bi=(BranchItem*)getSelectedBranchItem();
2409 if (bi && bi->isBranchLikeType() )
2411 if (bi->isScrolled())
2412 unscrollBranch (bi);
2416 // saveState is called in above functions
2419 void VymModel::unscrollChildren() // FIXME-2 first, next moved to vymmodel
2423 BranchObj *bo=getSelectedBranch();
2429 if (bo->isScrolled()) unscrollBranch (bo);
2436 void VymModel::emitExpandAll()
2438 emit (expandAll() );
2441 void VymModel::toggleStandardFlag (const QString &name, FlagRow *master)
2443 BranchItem *bi=getSelectedBranchItem();
2447 if (bi->isActiveStandardFlag(name))
2459 QString("%1 (\"%2\")").arg(u).arg(name),
2461 QString("%1 (\"%2\")").arg(r).arg(name),
2462 QString("Toggling standard flag \"%1\" of %2").arg(name).arg(getObjectName(bi)));
2463 bi->toggleStandardFlag (name, master);
2465 emitSelectionChanged();
2469 void VymModel::addFloatImage (const QPixmap &img) //FIXME-2 drag & drop
2472 BranchObj *bo=getSelectedBranch();
2475 FloatImageObj *fio=bo->addFloatImage();
2477 fio->setOriginalFilename("No original filename (image added by dropevent)");
2478 QString s=getSelectString(bo);
2479 saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio );
2480 saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
2482 // FIXME-3 VM needed? scene()->update();
2488 void VymModel::colorBranch (QColor c)
2490 BranchItem *selbi=getSelectedBranchItem();
2495 QString ("colorBranch (\"%1\")").arg(selbi->getHeadingColor().name()),
2497 QString ("colorBranch (\"%1\")").arg(c.name()),
2498 QString("Set color of %1 to %2").arg(getObjectName(selbi)).arg(c.name())
2500 selbi->setHeadingColor(c); // color branch
2504 void VymModel::colorSubtree (QColor c)
2506 BranchItem *selbi=getSelectedBranchItem();
2509 saveStateChangingPart(
2512 QString ("colorSubtree (\"%1\")").arg(c.name()),
2513 QString ("Set color of %1 and children to %2").arg(getObjectName(selbi)).arg(c.name())
2515 BranchItem *prev=NULL;
2516 BranchItem *cur=selbi;
2519 cur->setHeadingColor(c); // color links, color children
2520 cur=next (cur,prev,selbi);
2526 QColor VymModel::getCurrentHeadingColor()
2528 BranchItem *selbi=getSelectedBranchItem();
2529 if (selbi) return selbi->getHeadingColor();
2531 QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
2537 void VymModel::editURL()
2539 TreeItem *selti=getSelectedItem();
2543 QString text = QInputDialog::getText(
2544 "VYM", tr("Enter URL:"), QLineEdit::Normal,
2545 selti->getURL(), &ok, NULL);
2547 // user entered something and pressed OK
2552 void VymModel::editLocalURL()
2554 TreeItem *selti=getSelectedItem();
2557 QStringList filters;
2558 filters <<"All files (*)";
2559 filters << tr("Text","Filedialog") + " (*.txt)";
2560 filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
2561 filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
2562 filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
2563 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
2564 fd->setFilters (filters);
2565 fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
2566 fd->setDirectory (lastFileDir);
2567 if (! selti->getVymLink().isEmpty() )
2568 fd->selectFile( selti->getURL() );
2571 if ( fd->exec() == QDialog::Accepted )
2573 lastFileDir=QDir (fd->directory().path());
2574 setURL (fd->selectedFile() );
2580 void VymModel::editHeading2URL()
2582 TreeItem *selti=getSelectedItem();
2584 setURL (selti->getHeading());
2587 void VymModel::editBugzilla2URL()
2589 TreeItem *selti=getSelectedItem();
2592 QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+selti->getHeading();
2597 void VymModel::editFATE2URL()
2599 TreeItem *selti=getSelectedItem();
2602 QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+selti->getHeading();
2605 "setURL (\""+selti->getURL()+"\")",
2607 "setURL (\""+url+"\")",
2608 QString("Use heading of %1 as link to FATE").arg(getObjectName(selti))
2610 selti->setURL (url);
2611 // FIXME-4 updateActions();
2615 void VymModel::editVymLink()
2617 BranchItem *bi=getSelectedBranchItem();
2620 QStringList filters;
2621 filters <<"VYM map (*.vym)";
2622 QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
2623 fd->setFilters (filters);
2624 fd->setCaption(vymName+" - " +tr("Link to another map"));
2625 fd->setDirectory (lastFileDir);
2626 if (! bi->getVymLink().isEmpty() )
2627 fd->selectFile( bi->getVymLink() );
2631 if ( fd->exec() == QDialog::Accepted )
2633 lastFileDir=QDir (fd->directory().path());
2636 "setVymLink (\""+bi->getVymLink()+"\")",
2638 "setVymLink (\""+fd->selectedFile()+"\")",
2639 QString("Set vymlink of %1 to %2").arg(getObjectName(bi)).arg(fd->selectedFile())
2641 setVymLink (fd->selectedFile() ); // FIXME-2 ok?
2646 void VymModel::setVymLink (const QString &s) // FIXME-3 no savestate?
2648 // Internal function, no saveState needed
2649 BranchItem *bi=getSelectedBranchItem();
2655 //emitSelectionChanged();
2659 void VymModel::deleteVymLink()
2661 BranchItem *bi=getSelectedBranchItem();
2666 "setVymLink (\""+bi->getVymLink()+"\")",
2668 "setVymLink (\"\")",
2669 QString("Unset vymlink of %1").arg(getObjectName(bi))
2671 bi->setVymLink ("" );
2677 QString VymModel::getVymLink()
2679 BranchItem *bi=getSelectedBranchItem();
2681 return bi->getVymLink();
2687 QStringList VymModel::getVymLinks()
2690 BranchItem *selbi=getSelectedBranchItem();
2691 BranchItem *cur=selbi;
2692 BranchItem *prev=NULL;
2695 if (!cur->getVymLink().isEmpty()) links.append( cur->getVymLink());
2696 cur=next (cur,prev,selbi);
2702 void VymModel::followXLink(int i) // FIXME-2
2706 BranchObj *bo=getSelectedBranch();
2709 bo=bo->XLinkTargetAt(i);
2712 selection.select(bo);
2713 emitShowSelection();
2719 void VymModel::editXLink(int i) // FIXME-2 VM missing saveState
2723 BranchObj *bo=getSelectedBranch();
2726 XLinkObj *xlo=bo->XLinkAt(i);
2729 EditXLinkDialog dia;
2731 dia.setSelection(bo);
2732 if (dia.exec() == QDialog::Accepted)
2734 if (dia.useSettingsGlobal() )
2736 setMapDefXLinkColor (xlo->getColor() );
2737 setMapDefXLinkWidth (xlo->getWidth() );
2739 if (dia.deleteXLink())
2740 bo->deleteXLinkAt(i);
2751 //////////////////////////////////////////////
2753 //////////////////////////////////////////////
2755 void VymModel::parseAtom(const QString &atom)
2757 TreeItem* selti=getSelectedItem();
2758 BranchItem *selbi=getSelectedBranchItem();
2764 // Split string s into command and parameters
2765 parser.parseAtom (atom);
2766 QString com=parser.getCommand();
2768 // External commands
2769 /////////////////////////////////////////////////////////////////////
2770 if (com=="addBranch")
2774 parser.setError (Aborted,"Nothing selected");
2775 } else if (! selbi )
2777 parser.setError (Aborted,"Type of selection is not a branch");
2782 if (parser.checkParCount(pl))
2784 if (parser.parCount()==0)
2788 n=parser.parInt (ok,0);
2789 if (ok ) addNewBranch (n);
2793 /////////////////////////////////////////////////////////////////////
2794 } else if (com=="addBranchBefore")
2798 parser.setError (Aborted,"Nothing selected");
2799 } else if (! selbi )
2801 parser.setError (Aborted,"Type of selection is not a branch");
2804 if (parser.parCount()==0)
2806 addNewBranchBefore ();
2809 /////////////////////////////////////////////////////////////////////
2810 } else if (com==QString("addMapCenter"))
2812 if (parser.checkParCount(2))
2814 x=parser.parDouble (ok,0);
2817 y=parser.parDouble (ok,1);
2818 if (ok) addMapCenter (QPointF(x,y));
2821 /////////////////////////////////////////////////////////////////////
2822 } else if (com==QString("addMapReplace"))
2826 parser.setError (Aborted,"Nothing selected");
2827 } else if (! selbi )
2829 parser.setError (Aborted,"Type of selection is not a branch");
2830 } else if (parser.checkParCount(1))
2832 //s=parser.parString (ok,0); // selection
2833 t=parser.parString (ok,0); // path to map
2834 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2835 addMapReplaceInt(getSelectString(selbi),t);
2837 /////////////////////////////////////////////////////////////////////
2838 } else if (com==QString("addMapInsert"))
2842 parser.setError (Aborted,"Nothing selected");
2843 } else if (! selbi )
2845 parser.setError (Aborted,"Type of selection is not a branch");
2848 if (parser.checkParCount(2))
2850 t=parser.parString (ok,0); // path to map
2851 n=parser.parInt(ok,1); // position
2852 if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
2853 addMapInsertInt(t,n);
2856 /////////////////////////////////////////////////////////////////////
2857 } else if (com=="clearFlags")
2861 parser.setError (Aborted,"Nothing selected");
2862 } else if (! selbi )
2864 parser.setError (Aborted,"Type of selection is not a branch");
2865 } else if (parser.checkParCount(0))
2867 selbi->deactivateAllStandardFlags();
2869 /////////////////////////////////////////////////////////////////////
2870 } else if (com=="colorBranch")
2874 parser.setError (Aborted,"Nothing selected");
2875 } else if (! selbi )
2877 parser.setError (Aborted,"Type of selection is not a branch");
2878 } else if (parser.checkParCount(1))
2880 QColor c=parser.parColor (ok,0);
2881 if (ok) colorBranch (c);
2883 /////////////////////////////////////////////////////////////////////
2884 } else if (com=="colorSubtree")
2888 parser.setError (Aborted,"Nothing selected");
2889 } else if (! selbi )
2891 parser.setError (Aborted,"Type of selection is not a branch");
2892 } else if (parser.checkParCount(1))
2894 QColor c=parser.parColor (ok,0);
2895 if (ok) colorSubtree (c);
2897 /////////////////////////////////////////////////////////////////////
2898 } else if (com=="copy")
2902 parser.setError (Aborted,"Nothing selected");
2903 } else if ( selectionType()!=TreeItem::Branch &&
2904 selectionType()!=TreeItem::MapCenter &&
2905 selectionType()!=TreeItem::Image )
2907 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2908 } else if (parser.checkParCount(0))
2912 /////////////////////////////////////////////////////////////////////
2913 } else if (com=="cut")
2917 parser.setError (Aborted,"Nothing selected");
2918 } else if ( selectionType()!=TreeItem::Branch &&
2919 selectionType()!=TreeItem::MapCenter &&
2920 selectionType()!=TreeItem::Image )
2922 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
2923 } else if (parser.checkParCount(0))
2927 /////////////////////////////////////////////////////////////////////
2928 } else if (com=="delete")
2932 parser.setError (Aborted,"Nothing selected");
2934 /*else if (selectionType() != TreeItem::Branch && selectionType() != TreeItem::Image )
2936 parser.setError (Aborted,"Type of selection is wrong.");
2939 else if (parser.checkParCount(0))
2943 /////////////////////////////////////////////////////////////////////
2944 } else if (com=="deleteKeepChildren")
2948 parser.setError (Aborted,"Nothing selected");
2949 } else if (! selbi )
2951 parser.setError (Aborted,"Type of selection is not a branch");
2952 } else if (parser.checkParCount(0))
2954 deleteKeepChildren();
2956 /////////////////////////////////////////////////////////////////////
2957 } else if (com=="deleteChildren")
2961 parser.setError (Aborted,"Nothing selected");
2964 parser.setError (Aborted,"Type of selection is not a branch");
2965 } else if (parser.checkParCount(0))
2969 /////////////////////////////////////////////////////////////////////
2970 } else if (com=="exportASCII")
2974 if (parser.parCount()>=1)
2975 // Hey, we even have a filename
2976 fname=parser.parString(ok,0);
2979 parser.setError (Aborted,"Could not read filename");
2982 exportASCII (fname,false);
2984 /////////////////////////////////////////////////////////////////////
2985 } else if (com=="exportImage")
2989 if (parser.parCount()>=2)
2990 // Hey, we even have a filename
2991 fname=parser.parString(ok,0);
2994 parser.setError (Aborted,"Could not read filename");
2997 QString format="PNG";
2998 if (parser.parCount()>=2)
3000 format=parser.parString(ok,1);
3002 exportImage (fname,false,format);
3004 /////////////////////////////////////////////////////////////////////
3005 } else if (com=="exportXHTML")
3009 if (parser.parCount()>=2)
3010 // Hey, we even have a filename
3011 fname=parser.parString(ok,1);
3014 parser.setError (Aborted,"Could not read filename");
3017 exportXHTML (fname,false);
3019 /////////////////////////////////////////////////////////////////////
3020 } else if (com=="exportXML")
3024 if (parser.parCount()>=2)
3025 // Hey, we even have a filename
3026 fname=parser.parString(ok,1);
3029 parser.setError (Aborted,"Could not read filename");
3032 exportXML (fname,false);
3034 /////////////////////////////////////////////////////////////////////
3035 } else if (com=="importDir")
3039 parser.setError (Aborted,"Nothing selected");
3040 } else if (! selbi )
3042 parser.setError (Aborted,"Type of selection is not a branch");
3043 } else if (parser.checkParCount(1))
3045 s=parser.parString(ok,0);
3046 if (ok) importDirInt(s);
3048 /////////////////////////////////////////////////////////////////////
3049 } else if (com=="relinkTo")
3053 parser.setError (Aborted,"Nothing selected");
3056 if (parser.checkParCount(4))
3058 // 0 selectstring of parent
3059 // 1 num in parent (for branches)
3060 // 2,3 x,y of mainbranch or mapcenter
3061 s=parser.parString(ok,0);
3062 TreeItem *dst=findBySelectString (s);
3065 if (dst->getType()==TreeItem::Branch)
3067 // Get number in parent
3068 n=parser.parInt (ok,1);
3071 relinkBranch (selbi,(BranchItem*)dst,n);
3072 emitSelectionChanged();
3074 } else if (dst->getType()==TreeItem::MapCenter)
3076 relinkBranch (selbi,(BranchItem*)dst);
3077 // Get coordinates of mainbranch
3078 x=parser.parDouble(ok,2);
3081 y=parser.parDouble(ok,3);
3084 if (selbi->getLMO()) selbi->getLMO()->move (x,y);
3085 emitSelectionChanged();
3091 } else if ( selti->getType() == TreeItem::Image)
3093 if (parser.checkParCount(1))
3095 // 0 selectstring of parent
3096 s=parser.parString(ok,0);
3097 TreeItem *dst=findBySelectString (s);
3100 if (dst->isBranchLikeType())
3101 relinkImage ( ((ImageItem*)selti),(BranchItem*)dst);
3103 parser.setError (Aborted,"Destination is not a branch");
3106 parser.setError (Aborted,"Type of selection is not a floatimage or branch");
3107 /////////////////////////////////////////////////////////////////////
3108 } else if (com=="loadImage")
3112 parser.setError (Aborted,"Nothing selected");
3113 } else if (! selbi )
3115 parser.setError (Aborted,"Type of selection is not a branch");
3116 } else if (parser.checkParCount(1))
3118 s=parser.parString(ok,0);
3119 if (ok) loadFloatImageInt (selbi,s);
3121 /////////////////////////////////////////////////////////////////////
3122 } else if (com=="moveUp")
3126 parser.setError (Aborted,"Nothing selected");
3127 } else if (! selbi )
3129 parser.setError (Aborted,"Type of selection is not a branch");
3130 } else if (parser.checkParCount(0))
3134 /////////////////////////////////////////////////////////////////////
3135 } else if (com=="moveDown")
3139 parser.setError (Aborted,"Nothing selected");
3140 } else if (! selbi )
3142 parser.setError (Aborted,"Type of selection is not a branch");
3143 } else if (parser.checkParCount(0))
3147 /////////////////////////////////////////////////////////////////////
3148 } else if (com=="move")
3152 parser.setError (Aborted,"Nothing selected");
3153 } else if ( selectionType()!=TreeItem::Branch &&
3154 selectionType()!=TreeItem::MapCenter &&
3155 selectionType()!=TreeItem::Image )
3157 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3158 } else if (parser.checkParCount(2))
3160 x=parser.parDouble (ok,0);
3163 y=parser.parDouble (ok,1);
3167 /////////////////////////////////////////////////////////////////////
3168 } else if (com=="moveRel")
3172 parser.setError (Aborted,"Nothing selected");
3173 } else if ( selectionType()!=TreeItem::Branch &&
3174 selectionType()!=TreeItem::MapCenter &&
3175 selectionType()!=TreeItem::Image )
3177 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3178 } else if (parser.checkParCount(2))
3180 x=parser.parDouble (ok,0);
3183 y=parser.parDouble (ok,1);
3184 if (ok) moveRel (x,y);
3187 /////////////////////////////////////////////////////////////////////
3188 } else if (com=="nop")
3190 /////////////////////////////////////////////////////////////////////
3191 } else if (com=="paste")
3195 parser.setError (Aborted,"Nothing selected");
3196 } else if (! selbi )
3198 parser.setError (Aborted,"Type of selection is not a branch");
3199 } else if (parser.checkParCount(1))
3201 n=parser.parInt (ok,0);
3202 if (ok) pasteNoSave(n);
3204 /////////////////////////////////////////////////////////////////////
3205 } else if (com=="qa")
3209 parser.setError (Aborted,"Nothing selected");
3210 } else if (! selbi )
3212 parser.setError (Aborted,"Type of selection is not a branch");
3213 } else if (parser.checkParCount(4))
3216 c=parser.parString (ok,0);
3219 parser.setError (Aborted,"No comment given");
3222 s=parser.parString (ok,1);
3225 parser.setError (Aborted,"First parameter is not a string");
3228 t=parser.parString (ok,2);
3231 parser.setError (Aborted,"Condition is not a string");
3234 u=parser.parString (ok,3);
3237 parser.setError (Aborted,"Third parameter is not a string");
3242 parser.setError (Aborted,"Unknown type: "+s);
3247 parser.setError (Aborted,"Unknown operator: "+t);
3252 parser.setError (Aborted,"Type of selection is not a branch");
3255 if (selbi->getHeading() == u)
3257 cout << "PASSED: " << qPrintable (c) << endl;
3260 cout << "FAILED: " << qPrintable (c) << endl;
3270 /////////////////////////////////////////////////////////////////////
3271 } else if (com=="saveImage")
3273 ImageItem *ii=getSelectedImageItem();
3276 parser.setError (Aborted,"No image selected");
3277 } else if (parser.checkParCount(2))
3279 s=parser.parString(ok,0);
3282 t=parser.parString(ok,1);
3283 if (ok) saveFloatImageInt (ii,t,s);
3286 /////////////////////////////////////////////////////////////////////
3287 } else if (com=="scroll")
3291 parser.setError (Aborted,"Nothing selected");
3292 } else if (! selbi )
3294 parser.setError (Aborted,"Type of selection is not a branch");
3295 } else if (parser.checkParCount(0))
3297 if (!scrollBranch (selbi))
3298 parser.setError (Aborted,"Could not scroll branch");
3300 /////////////////////////////////////////////////////////////////////
3301 } else if (com=="select")
3303 if (parser.checkParCount(1))
3305 s=parser.parString(ok,0);
3308 /////////////////////////////////////////////////////////////////////
3309 } else if (com=="selectLastBranch")
3313 parser.setError (Aborted,"Nothing selected");
3314 } else if (! selbi )
3316 parser.setError (Aborted,"Type of selection is not a branch");
3317 } else if (parser.checkParCount(0))
3319 BranchItem *bi=selbi->getLastBranch();
3321 parser.setError (Aborted,"Could not select last branch");
3322 select (bi); // FIXME-3 was selectInt
3325 /////////////////////////////////////////////////////////////////////
3326 } else /* FIXME-2 if (com=="selectLastImage")
3330 parser.setError (Aborted,"Nothing selected");
3331 } else if (! selbi )
3333 parser.setError (Aborted,"Type of selection is not a branch");
3334 } else if (parser.checkParCount(0))
3336 FloatImageObj *fio=selb->getLastFloatImage();
3338 parser.setError (Aborted,"Could not select last image");
3339 select (fio); // FIXME-3 was selectInt
3342 /////////////////////////////////////////////////////////////////////
3343 } else */ if (com=="selectLatestAdded")
3345 if (!latestAddedItem)
3347 parser.setError (Aborted,"No latest added object");
3350 if (!select (latestAddedItem))
3351 parser.setError (Aborted,"Could not select latest added object ");
3353 /////////////////////////////////////////////////////////////////////
3354 } else if (com=="setFrameType")
3356 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3358 parser.setError (Aborted,"Type of selection does not allow setting frame type");
3360 else if (parser.checkParCount(1))
3362 s=parser.parString(ok,0);
3363 if (ok) setFrameType (s);
3365 /////////////////////////////////////////////////////////////////////
3366 } else if (com=="setFramePenColor")
3368 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3370 parser.setError (Aborted,"Type of selection does not allow setting of pen color");
3372 else if (parser.checkParCount(1))
3374 QColor c=parser.parColor(ok,0);
3375 if (ok) setFramePenColor (c);
3377 /////////////////////////////////////////////////////////////////////
3378 } else if (com=="setFrameBrushColor")
3380 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3382 parser.setError (Aborted,"Type of selection does not allow setting brush color");
3384 else if (parser.checkParCount(1))
3386 QColor c=parser.parColor(ok,0);
3387 if (ok) setFrameBrushColor (c);
3389 /////////////////////////////////////////////////////////////////////
3390 } else if (com=="setFramePadding")
3392 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3394 parser.setError (Aborted,"Type of selection does not allow setting frame padding");
3396 else if (parser.checkParCount(1))
3398 n=parser.parInt(ok,0);
3399 if (ok) setFramePadding(n);
3401 /////////////////////////////////////////////////////////////////////
3402 } else if (com=="setFrameBorderWidth")
3404 if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3406 parser.setError (Aborted,"Type of selection does not allow setting frame border width");
3408 else if (parser.checkParCount(1))
3410 n=parser.parInt(ok,0);
3411 if (ok) setFrameBorderWidth (n);
3413 /////////////////////////////////////////////////////////////////////
3414 } else if (com=="setMapAuthor")
3416 if (parser.checkParCount(1))
3418 s=parser.parString(ok,0);
3419 if (ok) setAuthor (s);
3421 /////////////////////////////////////////////////////////////////////
3422 } else if (com=="setMapComment")
3424 if (parser.checkParCount(1))
3426 s=parser.parString(ok,0);
3427 if (ok) setComment(s);
3429 /////////////////////////////////////////////////////////////////////
3430 } else if (com=="setMapBackgroundColor")
3434 parser.setError (Aborted,"Nothing selected");
3435 } else if (! selbi )
3437 parser.setError (Aborted,"Type of selection is not a branch");
3438 } else if (parser.checkParCount(1))
3440 QColor c=parser.parColor (ok,0);
3441 if (ok) setMapBackgroundColor (c);
3443 /////////////////////////////////////////////////////////////////////
3444 } else if (com=="setMapDefLinkColor")
3448 parser.setError (Aborted,"Nothing selected");
3449 } else if (! selbi )
3451 parser.setError (Aborted,"Type of selection is not a branch");
3452 } else if (parser.checkParCount(1))
3454 QColor c=parser.parColor (ok,0);
3455 if (ok) setMapDefLinkColor (c);
3457 /////////////////////////////////////////////////////////////////////
3458 } else if (com=="setMapLinkStyle")
3460 if (parser.checkParCount(1))
3462 s=parser.parString (ok,0);
3463 if (ok) setMapLinkStyle(s);
3465 /////////////////////////////////////////////////////////////////////
3466 } else if (com=="setHeading")
3470 parser.setError (Aborted,"Nothing selected");
3471 } else if (! selbi )
3473 parser.setError (Aborted,"Type of selection is not a branch");
3474 } else if (parser.checkParCount(1))
3476 s=parser.parString (ok,0);
3480 /////////////////////////////////////////////////////////////////////
3481 } else if (com=="setHideExport")
3485 parser.setError (Aborted,"Nothing selected");
3486 } else if (selectionType()!=TreeItem::Branch && selectionType() != TreeItem::MapCenter &&selectionType()!=TreeItem::Image)
3488 parser.setError (Aborted,"Type of selection is not a branch or floatimage");
3489 } else if (parser.checkParCount(1))
3491 b=parser.parBool(ok,0);
3492 if (ok) setHideExport (b);
3494 /////////////////////////////////////////////////////////////////////
3495 } else if (com=="setIncludeImagesHorizontally")
3499 parser.setError (Aborted,"Nothing selected");
3502 parser.setError (Aborted,"Type of selection is not a branch");
3503 } else if (parser.checkParCount(1))
3505 b=parser.parBool(ok,0);
3506 if (ok) setIncludeImagesHor(b);
3508 /////////////////////////////////////////////////////////////////////
3509 } else if (com=="setIncludeImagesVertically")
3513 parser.setError (Aborted,"Nothing selected");
3516 parser.setError (Aborted,"Type of selection is not a branch");
3517 } else if (parser.checkParCount(1))
3519 b=parser.parBool(ok,0);
3520 if (ok) setIncludeImagesVer(b);
3522 /////////////////////////////////////////////////////////////////////
3523 } else if (com=="setHideLinkUnselected")
3527 parser.setError (Aborted,"Nothing selected");
3528 } else if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
3530 parser.setError (Aborted,"Type of selection does not allow hiding the link");
3531 } else if (parser.checkParCount(1))
3533 b=parser.parBool(ok,0);
3534 if (ok) setHideLinkUnselected(b);
3536 /////////////////////////////////////////////////////////////////////
3537 } else if (com=="setSelectionColor")
3539 if (parser.checkParCount(1))
3541 QColor c=parser.parColor (ok,0);
3542 if (ok) setSelectionColorInt (c);
3544 /////////////////////////////////////////////////////////////////////
3545 } else if (com=="setURL")
3549 parser.setError (Aborted,"Nothing selected");
3550 } else if (! selbi )
3552 parser.setError (Aborted,"Type of selection is not a branch");
3553 } else if (parser.checkParCount(1))
3555 s=parser.parString (ok,0);
3558 /////////////////////////////////////////////////////////////////////
3559 } else if (com=="setVymLink")
3563 parser.setError (Aborted,"Nothing selected");
3564 } else if (! selbi )
3566 parser.setError (Aborted,"Type of selection is not a branch");
3567 } else if (parser.checkParCount(1))
3569 s=parser.parString (ok,0);
3570 if (ok) setVymLink(s);
3573 /////////////////////////////////////////////////////////////////////
3574 else if (com=="setFlag")
3578 parser.setError (Aborted,"Nothing selected");
3579 } else if (! selbi )
3581 parser.setError (Aborted,"Type of selection is not a branch");
3582 } else if (parser.checkParCount(1))
3584 s=parser.parString(ok,0);
3586 selbi->activateStandardFlag(s);
3588 /////////////////////////////////////////////////////////////////////
3589 } else /* FIXME-2 if (com=="setFrameType")
3593 parser.setError (Aborted,"Nothing selected");
3596 parser.setError (Aborted,"Type of selection is not a branch");
3597 } else if (parser.checkParCount(1))
3599 s=parser.parString(ok,0);
3603 /////////////////////////////////////////////////////////////////////
3604 } else*/ if (com=="sortChildren")
3608 parser.setError (Aborted,"Nothing selected");
3609 } else if (! selbi )
3611 parser.setError (Aborted,"Type of selection is not a branch");
3612 } else if (parser.checkParCount(0))
3616 /////////////////////////////////////////////////////////////////////
3617 } else if (com=="toggleFlag")
3621 parser.setError (Aborted,"Nothing selected");
3622 } else if (! selbi )
3624 parser.setError (Aborted,"Type of selection is not a branch");
3625 } else if (parser.checkParCount(1))
3627 s=parser.parString(ok,0);
3629 selbi->toggleStandardFlag(s);
3631 /////////////////////////////////////////////////////////////////////
3632 } else if (com=="unscroll")
3636 parser.setError (Aborted,"Nothing selected");
3637 } else if (! selbi )
3639 parser.setError (Aborted,"Type of selection is not a branch");
3640 } else if (parser.checkParCount(0))
3642 if (!unscrollBranch (selbi))
3643 parser.setError (Aborted,"Could not unscroll branch");
3645 /////////////////////////////////////////////////////////////////////
3646 } else if (com=="unscrollChildren")
3650 parser.setError (Aborted,"Nothing selected");
3651 } else if (! selbi )
3653 parser.setError (Aborted,"Type of selection is not a branch");
3654 } else if (parser.checkParCount(0))
3656 unscrollChildren ();
3658 /////////////////////////////////////////////////////////////////////
3659 } else if (com=="unsetFlag")
3663 parser.setError (Aborted,"Nothing selected");
3664 } else if (! selbi )
3666 parser.setError (Aborted,"Type of selection is not a branch");
3667 } else if (parser.checkParCount(1))
3669 s=parser.parString(ok,0);
3671 selbi->deactivateStandardFlag(s);
3674 parser.setError (Aborted,"Unknown command");
3677 if (parser.errorLevel()==NoError)
3679 // setChanged(); FIXME-2 should not be called e.g. for export?!
3684 // TODO Error handling
3685 qWarning("VymModel::parseAtom: Error!");
3686 qWarning(parser.errorMessage());
3690 void VymModel::runScript (QString script)
3692 parser.setScript (script);
3694 while (parser.next() )
3695 parseAtom(parser.getAtom());
3698 void VymModel::setExportMode (bool b)
3700 // should be called before and after exports
3701 // depending on the settings
3702 if (b && settings.value("/export/useHideExport","true")=="true")
3703 setHideTmpMode (TreeItem::HideExport);
3705 setHideTmpMode (TreeItem::HideNone);
3708 void VymModel::exportImage(QString fname, bool askName, QString format)
3712 fname=getMapName()+".png";
3719 QFileDialog *fd=new QFileDialog (NULL);
3720 fd->setCaption (tr("Export map as image"));
3721 fd->setDirectory (lastImageDir);
3722 fd->setFileMode(QFileDialog::AnyFile);
3723 fd->setFilters (imageIO.getFilters() );
3726 fl=fd->selectedFiles();
3728 format=imageIO.getType(fd->selectedFilter());
3732 setExportMode (true);
3733 QPixmap pix (getPixmap());
3734 pix.save(fname, format);
3735 setExportMode (false);
3739 void VymModel::exportXML(QString dir, bool askForName)
3743 dir=browseDirectory(NULL,tr("Export XML to directory"));
3744 if (dir =="" && !reallyWriteDirectory(dir) )
3748 // Hide stuff during export, if settings want this
3749 setExportMode (true);
3751 // Create subdirectories
3754 // write to directory
3755 QString saveFile=saveToDir (dir,mapName+"-",true,getTotalBBox().topLeft() ,NULL);
3758 file.setName ( dir + "/"+mapName+".xml");
3759 if ( !file.open( QIODevice::WriteOnly ) )
3761 // This should neverever happen
3762 QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
3766 // Write it finally, and write in UTF8, no matter what
3767 QTextStream ts( &file );
3768 ts.setEncoding (QTextStream::UnicodeUTF8);
3772 // Now write image, too
3773 exportImage (dir+"/images/"+mapName+".png",false,"PNG");
3775 setExportMode (false);
3778 void VymModel::exportASCII(QString fname,bool askName)
3783 ex.setFile (mapName+".txt");
3789 //ex.addFilter ("TXT (*.txt)");
3790 ex.setDir(lastImageDir);
3791 //ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
3796 setExportMode(true);
3798 setExportMode(false);
3802 void VymModel::exportXHTML (const QString &dir, bool askForName)
3804 ExportXHTMLDialog dia(NULL);
3805 dia.setFilePath (filePath );
3806 dia.setMapName (mapName );
3808 if (dir!="") dia.setDir (dir);
3814 if (dia.exec()!=QDialog::Accepted)
3818 QDir d (dia.getDir());
3819 // Check, if warnings should be used before overwriting
3820 // the output directory
3821 if (d.exists() && d.count()>0)
3824 warn.showCancelButton (true);
3825 warn.setText(QString(
3826 "The directory %1 is not empty.\n"
3827 "Do you risk to overwrite some of its contents?").arg(d.path() ));
3828 warn.setCaption("Warning: Directory not empty");
3829 warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
3831 if (warn.exec()!=QDialog::Accepted) ok=false;
3838 exportXML (dia.getDir(),false );
3839 dia.doExport(mapName );
3840 //if (dia.hasChanged()) setChanged();
3844 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
3849 if (ex.setConfigFile(cf))
3851 setExportMode (true);
3852 ex.exportPresentation();
3853 setExportMode (false);
3860 //////////////////////////////////////////////
3862 //////////////////////////////////////////////
3864 void VymModel::registerEditor(QWidget *me)
3866 mapEditor=(MapEditor*)me;
3869 void VymModel::unregisterEditor(QWidget *)
3874 void VymModel::setContextPos(QPointF p)
3879 void VymModel::unsetContextPos()
3881 contextPos=QPointF();
3884 void VymModel::updateNoteFlag()
3887 TreeItem *selti=getSelectedItem();
3890 if (textEditor->isEmpty())
3893 selti->setNote (textEditor->getText());
3894 emitDataHasChanged(selti);
3895 emitSelectionChanged();
3900 void VymModel::updateRelPositions() //FIXME-3 VM should have no need to updateRelPos
3903 for (int i=0; i<rootItem->branchCount(); i++)
3904 ((MapCenterObj*)rootItem->getBranchObjNum(i))->updateRelPositions();
3907 void MapCenterObj::updateRelPositions()
3909 if (repositionRequest) unsetAllRepositionRequests();
3911 // update relative Positions of branches and floats
3912 for (int i=0; i<treeItem->branchCount(); ++i)
3914 treeItem->getBranchObjNum(i)->setRelPos();
3915 treeItem->getBranchObjNum(i)->setOrientation();
3918 for (int i=0; i<floatimage.size(); ++i)
3919 floatimage.at(i)->setRelPos();
3921 if (repositionRequest) reposition();
3926 void VymModel::reposition() //FIXME-3 VM should have no need to reposition, this is done in views???
3928 //cout << "VM::reposition blocked="<<blockReposition<<endl;
3929 if (blockReposition) return;
3931 for (int i=0;i<rootItem->branchCount(); i++)
3932 rootItem->getBranchObjNum(i)->reposition(); // for positioning heading
3936 QPolygonF VymModel::shape(BranchObj *bo) //FIXME-4
3938 return QPolygonF ();
3939 // Creating (arbitrary) shapes
3942 QRectF rb=bo->getBBox();
3943 if (bo->getDepth()==0)
3945 // Just take BBox of this mapCenter
3946 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
3950 // Take union of BBox and TotalBBox
3952 QRectF ra=bo->getTotalBBox();
3953 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
3956 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
3959 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
3961 p <<ra.bottomRight()
3963 <<QPointF (rb.topRight().x(), ra.topRight().y() )
3966 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
3972 void VymModel::moveAway(LinkableMapObj *lmo) //FIXME-5
3976 // Move all branches and MapCenters away from lmo
3977 // to avoid collisions
3982 BranchObj *boA=(BranchObj*)lmo;
3984 for (int i=0; i<rootItem->branchCount(); i++)
3986 boB=rootItem->getBranchNum(i);
3989 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
3992 <<" ("<<qPrintable(boA->getHeading() )<<")"
3993 <<" with ("<< qPrintable (boB->getHeading() )
3996 <<" minT="<<r.minTranslation<<endl<<endl;
4001 QPixmap VymModel::getPixmap()
4003 QRectF mapRect=getTotalBBox();
4004 QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
4007 pp.setRenderHints(mapEditor->renderHints());
4009 // Don't print the visualisation of selection
4012 mapScene->render ( &pp,
4013 QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
4014 QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
4016 // Restore selection
4023 void VymModel::setMapLinkStyle (const QString & s)
4028 case LinkableMapObj::Line :
4031 case LinkableMapObj::Parabel:
4032 snow="StyleParabel";
4034 case LinkableMapObj::PolyLine:
4035 snow="StylePolyLine";
4037 case LinkableMapObj::PolyParabel:
4038 snow="StylePolyParabel";
4041 snow="UndefinedStyle";
4046 QString("setMapLinkStyle (\"%1\")").arg(s),
4047 QString("setMapLinkStyle (\"%1\")").arg(snow),
4048 QString("Set map link style (\"%1\")").arg(s)
4052 linkstyle=LinkableMapObj::Line;
4053 else if (s=="StyleParabel")
4054 linkstyle=LinkableMapObj::Parabel;
4055 else if (s=="StylePolyLine")
4056 linkstyle=LinkableMapObj::PolyLine;
4057 else if (s=="StylePolyParabel")
4058 linkstyle=LinkableMapObj::PolyParabel;
4060 linkstyle=LinkableMapObj::UndefinedStyle;
4062 BranchItem *cur=NULL;
4063 BranchItem *prev=NULL;
4068 bo=(BranchObj*)(cur->getLMO() );
4069 bo->setLinkStyle(bo->getDefLinkStyle());
4075 LinkableMapObj::Style VymModel::getMapLinkStyle ()
4080 void VymModel::setMapDefLinkColor(QColor col)
4082 if ( !col.isValid() ) return;
4084 QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
4085 QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
4086 QString("Set map link color to %1").arg(col.name())
4090 BranchItem *cur=NULL;
4091 BranchItem *prev=NULL;
4096 bo=(BranchObj*)(cur->getLMO() );
4103 void VymModel::setMapLinkColorHintInt()
4105 // called from setMapLinkColorHint(lch) or at end of parse
4106 BranchItem *cur=NULL;
4107 BranchItem *prev=NULL;
4112 bo=(BranchObj*)(cur->getLMO() );
4118 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
4121 setMapLinkColorHintInt();
4124 void VymModel::toggleMapLinkColorHint()
4126 if (linkcolorhint==LinkableMapObj::HeadingColor)
4127 linkcolorhint=LinkableMapObj::DefaultColor;
4129 linkcolorhint=LinkableMapObj::HeadingColor;
4130 BranchItem *cur=NULL;
4131 BranchItem *prev=NULL;
4136 bo=(BranchObj*)(cur->getLMO() );
4142 void VymModel::selectMapBackgroundImage () // FIXME-2 move to ME
4144 Q3FileDialog *fd=new Q3FileDialog( NULL);
4145 fd->setMode (Q3FileDialog::ExistingFile);
4146 fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
4147 ImagePreview *p =new ImagePreview (fd);
4148 fd->setContentsPreviewEnabled( TRUE );
4149 fd->setContentsPreview( p, p );
4150 fd->setPreviewMode( Q3FileDialog::Contents );
4151 fd->setCaption(vymName+" - " +tr("Load background image"));
4152 fd->setDir (lastImageDir);
4155 if ( fd->exec() == QDialog::Accepted )
4157 // TODO selectMapBackgroundImg in QT4 use: lastImageDir=fd->directory();
4158 lastImageDir=QDir (fd->dirPath());
4159 setMapBackgroundImage (fd->selectedFile());
4163 void VymModel::setMapBackgroundImage (const QString &fn) //FIXME-2 missing savestate
4165 QColor oldcol=mapScene->backgroundBrush().color();
4169 QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
4171 QString ("setMapBackgroundImage (%1)").arg(col.name()),
4172 QString("Set background color of map to %1").arg(col.name()));
4175 brush.setTextureImage (QPixmap (fn));
4176 mapScene->setBackgroundBrush(brush);
4179 void VymModel::selectMapBackgroundColor() // FIXME-3 move to ME
4181 QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
4182 if ( !col.isValid() ) return;
4183 setMapBackgroundColor( col );
4187 void VymModel::setMapBackgroundColor(QColor col) // FIXME-3 move to ME
4189 QColor oldcol=mapScene->backgroundBrush().color();
4191 QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
4192 QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
4193 QString("Set background color of map to %1").arg(col.name()));
4194 mapScene->setBackgroundBrush(col);
4197 QColor VymModel::getMapBackgroundColor() // FIXME-3 move to ME
4199 return mapScene->backgroundBrush().color();
4203 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint() // FIXME-3 move to ME
4205 return linkcolorhint;
4208 QColor VymModel::getMapDefLinkColor() // FIXME-3 move to ME
4210 return defLinkColor;
4213 void VymModel::setMapDefXLinkColor(QColor col) // FIXME-3 move to ME
4218 QColor VymModel::getMapDefXLinkColor() // FIXME-3 move to ME
4220 return defXLinkColor;
4223 void VymModel::setMapDefXLinkWidth (int w) // FIXME-3 move to ME
4228 int VymModel::getMapDefXLinkWidth() // FIXME-3 move to ME
4230 return defXLinkWidth;
4233 void VymModel::move(const double &x, const double &y) // FIXME-3
4237 BranchObj *bo = getSelectedBranch();
4239 (selectionType()==TreeItem::Branch ||
4240 selectionType()==TreeItem::MapCenter ||
4241 selectionType()==TreeItem::Image
4244 QPointF ap(bo->getAbsPos());
4248 QString ps=qpointfToString(ap);
4249 QString s=getSelectString();
4252 s, "move "+qpointfToString(to),
4253 QString("Move %1 to %2").arg(getObjectName(bo)).arg(ps));
4256 emitSelectionChanged();
4262 void VymModel::moveRel (const double &x, const double &y) // FIXME-3
4266 BranchObj *bo = getSelectedBranch();
4268 (selectionType()==TreeItem::Branch ||
4269 selectionType()==TreeItem::MapCenter ||
4270 selectionType()==TreeItem::Image
4274 QPointF rp(bo->getRelPos());
4278 QString ps=qpointfToString (bo->getRelPos());
4279 QString s=getSelectString(bo);
4282 s, "moveRel "+qpointfToString(to),
4283 QString("Move %1 to relative position %2").arg(getObjectName(bo)).arg(ps));
4284 ((OrnamentedObj*)bo)->move2RelPos (x,y);
4287 emitSelectionChanged();
4294 void VymModel::animate()
4296 animationTimer->stop();
4299 while (i<animObjList.size() )
4301 bo=(BranchObj*)animObjList.at(i);
4306 animObjList.removeAt(i);
4313 QItemSelection sel=selModel->selection();
4314 emit (selectionChanged(sel,sel));
4317 if (!animObjList.isEmpty()) animationTimer->start();
4321 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
4323 if (bo && bo->getTreeItem()->depth()>0)
4326 ap.setStart (start);
4328 ap.setTicks (animationTicks);
4329 ap.setAnimated (true);
4330 bo->setAnimation (ap);
4331 animObjList.append( bo );
4332 animationTimer->setSingleShot (true);
4333 animationTimer->start(animationInterval);
4337 void VymModel::stopAnimation (MapObj *mo)
4339 int i=animObjList.indexOf(mo);
4341 animObjList.removeAt (i);
4344 void VymModel::sendSelection()
4346 if (netstate!=Server) return;
4347 sendData (QString("select (\"%1\")").arg(getSelectString()) );
4350 void VymModel::newServer()
4354 tcpServer = new QTcpServer(this);
4355 if (!tcpServer->listen(QHostAddress::Any,port)) {
4356 QMessageBox::critical(NULL, "vym server",
4357 QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
4358 //FIXME-3 needed? we are no widget any longer... close();
4361 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
4363 cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
4366 void VymModel::connectToServer()
4369 server="salam.suse.de";
4371 clientSocket = new QTcpSocket (this);
4372 clientSocket->abort();
4373 clientSocket->connectToHost(server ,port);
4374 connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
4375 connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
4376 this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
4378 cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
4383 void VymModel::newClient()
4385 QTcpSocket *newClient = tcpServer->nextPendingConnection();
4386 connect(newClient, SIGNAL(disconnected()),
4387 newClient, SLOT(deleteLater()));
4389 cout <<"ME::newClient at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
4391 clientList.append (newClient);
4395 void VymModel::sendData(const QString &s)
4397 if (clientList.size()==0) return;
4399 // Create bytearray to send
4401 QDataStream out(&block, QIODevice::WriteOnly);
4402 out.setVersion(QDataStream::Qt_4_0);
4404 // Reserve some space for blocksize
4407 // Write sendCounter
4408 out << sendCounter++;
4413 // Go back and write blocksize so far
4414 out.device()->seek(0);
4415 quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
4419 cout << "ME::sendData bs="<<bs<<" counter="<<sendCounter<<" s="<<qPrintable(s)<<endl;
4421 for (int i=0; i<clientList.size(); ++i)
4423 //cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
4424 clientList.at(i)->write (block);
4428 void VymModel::readData ()
4430 while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
4433 cout <<"readData bytesAvail="<<clientSocket->bytesAvailable();
4437 QDataStream in(clientSocket);
4438 in.setVersion(QDataStream::Qt_4_0);
4446 cout << " t="<<qPrintable (t)<<endl;
4452 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
4454 switch (socketError) {
4455 case QAbstractSocket::RemoteHostClosedError:
4457 case QAbstractSocket::HostNotFoundError:
4458 QMessageBox::information(NULL, vymName +" Network client",
4459 "The host was not found. Please check the "
4460 "host name and port settings.");
4462 case QAbstractSocket::ConnectionRefusedError:
4463 QMessageBox::information(NULL, vymName + " Network client",
4464 "The connection was refused by the peer. "
4465 "Make sure the fortune server is running, "
4466 "and check that the host name and port "
4467 "settings are correct.");
4470 QMessageBox::information(NULL, vymName + " Network client",
4471 QString("The following error occurred: %1.")
4472 .arg(clientSocket->errorString()));
4479 void VymModel::selectMapSelectionColor()
4481 QColor col = QColorDialog::getColor( defLinkColor, NULL);
4482 setSelectionColor (col);
4485 void VymModel::setSelectionColorInt (QColor col)
4487 if ( !col.isValid() ) return;
4489 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4490 QString("setSelectionColor (%1)").arg(col.name()),
4491 QString("Set color of selection box to %1").arg(col.name())
4494 mapEditor->setSelectionColor (col);
4498 void VymModel::changeSelection (const QItemSelection &newsel,const QItemSelection &oldsel)
4500 cout << "VymModel::changeSelection (";
4501 if (!newsel.indexes().isEmpty() )
4502 cout << getItem(newsel.indexes().first() )->getHeading().toStdString();
4504 if (!oldsel.indexes().isEmpty() )
4505 cout << getItem(oldsel.indexes().first() )->getHeading().toStdString();
4510 void VymModel::updateSelection (const QItemSelection &newsel,const QItemSelection &oldsel) //FIXME-4 connected but not used so far
4514 void VymModel::emitSelectionChanged(const QItemSelection &newsel)
4516 emit (selectionChanged(newsel,newsel)); // needed e.g. to update geometry in editor
4517 emitShowSelection();
4521 void VymModel::emitSelectionChanged()
4523 QItemSelection newsel=selModel->selection();
4524 emitSelectionChanged (newsel);
4527 void VymModel::setSelectionColor(QColor col)
4529 if ( !col.isValid() ) return;
4531 QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
4532 QString("setSelectionColor (%1)").arg(col.name()),
4533 QString("Set color of selection box to %1").arg(col.name())
4535 setSelectionColorInt (col);
4538 QColor VymModel::getSelectionColor()
4540 return mapEditor->getSelectionColor();
4543 void VymModel::setHideTmpMode (TreeItem::HideTmpMode mode)
4546 for (int i=0;i<rootItem->childCount();i++)
4547 rootItem->child(i)->setHideTmp (mode);
4549 // FIXME-3 needed? scene()->update();
4553 QRectF VymModel::getTotalBBox() //FIXME-2
4557 for (int i=0;i<rootItem->branchCount(); i++)
4558 r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
4563 //////////////////////////////////////////////
4564 // Selection related
4565 //////////////////////////////////////////////
4567 void VymModel::setSelectionModel (QItemSelectionModel *sm)
4572 QItemSelectionModel* VymModel::getSelectionModel()
4577 void VymModel::setSelectionBlocked (bool b)
4582 bool VymModel::isSelectionBlocked()
4584 return selectionBlocked;
4587 bool VymModel::select ()
4589 return select (selModel->selectedIndexes().first()); // TODO no multiselections yet
4592 bool VymModel::select (const QString &s)
4594 TreeItem *ti=findBySelectString(s);
4604 bool VymModel::select (LinkableMapObj *lmo)
4606 QItemSelection oldsel=selModel->selection();
4609 return select (lmo->getTreeItem() );
4614 bool VymModel::select (TreeItem *ti)
4616 if (ti) return select (index(ti));
4620 bool VymModel::select (const QModelIndex &index)
4622 if (index.isValid() )
4624 selModel->select (index,QItemSelectionModel::ClearAndSelect );
4625 getSelectedItem()->setLastSelectedBranch();
4631 void VymModel::unselect()
4633 lastSelectString=getSelectString();
4634 selModel->clearSelection();
4637 void VymModel::reselect()
4639 select (lastSelectString);
4642 void VymModel::emitShowSelection()
4644 if (!blockReposition)
4645 emit (showSelection() );
4648 void VymModel::emitNoteHasChanged (TreeItem *ti)
4650 QModelIndex ix=index(ti);
4651 emit (noteHasChanged (ix) );
4654 void VymModel::emitDataHasChanged (TreeItem *ti)
4656 QModelIndex ix=index(ti);
4657 emit (dataChanged (ix,ix) );
4661 //void VymModel::selectInt (LinkableMapObj *lmo) // FIXME-3 still needed?
4664 if (selection.select(lmo))
4666 //emitSelectionChanged();
4670 void VymModel::selectInt (TreeItem *ti)
4672 if (selection.select(lmo))
4674 //emitSelectionChanged();
4679 void VymModel::selectNextBranchInt()
4681 BranchItem *bi=getSelectedBranchItem();
4684 TreeItem *pi=bi->parent();
4688 if (i<pi->branchCount() )
4690 // select previous branch with same parent
4692 select (pi->getBranchNum(i));
4700 void VymModel::selectPrevBranchInt()
4703 BranchItem *bi=getSelectedBranchItem();
4706 BranchItem *pi=(BranchItem*)bi->parent();
4712 // select previous branch with same parent
4713 bi=pi->getBranchNum(i-1);
4718 while (bi->branchCount() >0)
4719 bi=bi->getLastBranch();
4722 // Try to select last branch in parent pi2 previous to own parent pi
4724 TreeItem *pi2=pi->parent();
4735 void VymModel::selectAboveBranchInt()
4737 BranchItem *bi=getSelectedBranchItem();
4740 BranchItem *newbi=NULL;
4741 BranchItem *pi=(BranchItem*)bi->parent();
4745 // goto previous branch with same parent
4746 newbi=pi->getBranchNum(i-1);
4747 while (newbi->branchCount() >0 )
4748 newbi=newbi->getLastBranch();
4752 if (newbi==rootItem)
4753 // already at top branch (resp. mapcenter)
4759 void VymModel::selectBelowBranchInt()
4761 BranchItem *bi=getSelectedBranchItem();
4764 BranchItem *newbi=NULL;
4766 if (bi->branchCount() >0)
4767 newbi=bi->getFirstBranch();
4774 pi=(BranchItem*)bi->parent();
4776 if (pi->branchCount()-1 > i)
4778 newbi=(BranchItem*)pi->getBranchNum(i+1);
4783 // look for siblings of myself
4784 // or parent, or parent of parent...
4795 void VymModel::selectUpperBranch()
4797 BranchItem *bi=getSelectedBranchItem();
4798 if (bi && bi->isBranchLikeType())
4799 selectAboveBranchInt();
4802 void VymModel::selectLowerBranch()
4804 BranchItem *bi=getSelectedBranchItem();
4805 if (bi && bi->isBranchLikeType())
4806 selectBelowBranchInt();
4810 void VymModel::selectLeftBranch()
4812 QItemSelection oldsel=selModel->selection();
4815 BranchItem *selbi=getSelectedBranchItem();
4816 TreeItem::Type type=selbi->getType();
4819 if (type == TreeItem::MapCenter)
4821 QModelIndex ix=index(selbi);
4822 selModel->select (index (rowCount(ix)-1,0,ix),QItemSelectionModel::ClearAndSelect );
4825 par=(BranchItem*)selbi->parent();
4826 if (selbi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter) //FIXME-3 check getBO...
4829 if (type == TreeItem::Branch ||
4830 type == TreeItem::Image)
4832 QModelIndex ix=index (selbi->parent());
4833 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4838 if (type == TreeItem::Branch )
4840 selectLastSelectedBranch();
4848 void VymModel::selectRightBranch()
4850 QItemSelection oldsel=selModel->selection();
4853 BranchItem *selbi=getSelectedBranchItem();
4854 TreeItem::Type type=selbi->getType();
4857 if (type==TreeItem::MapCenter)
4859 QModelIndex ix=index(selbi);
4860 selModel->select (index (0,0,ix),QItemSelectionModel::ClearAndSelect );
4863 par=(BranchItem*)selbi->parent();
4864 if (selbi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter) //FIXME-3 check getBO
4867 if ( type== TreeItem::Branch )
4869 selectLastSelectedBranch();
4875 if (type == TreeItem::Branch ||
4876 type == TreeItem::Image)
4878 QModelIndex ix=index(selbi->parent());
4879 selModel->select (ix,QItemSelectionModel::ClearAndSelect );
4886 void VymModel::selectFirstBranch()
4888 TreeItem *ti=getSelectedBranchItem();
4891 TreeItem *par=ti->parent();
4893 TreeItem *ti2=par->getFirstBranch();
4896 emitSelectionChanged();
4901 void VymModel::selectLastBranch()
4903 TreeItem *ti=getSelectedBranchItem();
4906 TreeItem *par=ti->parent();
4908 TreeItem *ti2=par->getLastBranch();
4911 emitSelectionChanged();
4916 void VymModel::selectLastSelectedBranch()
4918 TreeItem *ti=getSelectedBranchItem();
4921 ti=ti->getLastSelectedBranch();
4922 if (ti) select (ti);
4926 void VymModel::selectParent()
4928 TreeItem *ti=getSelectedItem();
4935 emitSelectionChanged();
4939 TreeItem::Type VymModel::selectionType()
4941 QModelIndexList list=selModel->selectedIndexes();
4942 if (list.isEmpty()) return TreeItem::Undefined;
4943 TreeItem *ti = getItem (list.first() );
4944 return ti->getType();
4948 LinkableMapObj* VymModel::getSelectedLMO()
4950 QModelIndexList list=selModel->selectedIndexes();
4951 if (!list.isEmpty() )
4953 TreeItem *ti = getItem (list.first() );
4954 TreeItem::Type type=ti->getType();
4955 if (type ==TreeItem::Branch || type==TreeItem::MapCenter || type==TreeItem::Image)
4957 return ti->getLMO();
4963 BranchObj* VymModel::getSelectedBranchObj() // FIXME-3 this should not be needed in the end!!!
4965 TreeItem *ti = getSelectedBranchItem();
4967 return (BranchObj*)(ti->getLMO());
4972 BranchItem* VymModel::getSelectedBranchItem()
4974 QModelIndexList list=selModel->selectedIndexes();
4975 if (!list.isEmpty() )
4977 TreeItem *ti = getItem (list.first() );
4978 TreeItem::Type type=ti->getType();
4979 if (type ==TreeItem::Branch || type==TreeItem::MapCenter)
4980 return (BranchItem*)ti;
4985 TreeItem* VymModel::getSelectedItem()
4987 QModelIndexList list=selModel->selectedIndexes();
4988 if (!list.isEmpty() )
4989 return getItem (list.first() );
4994 QModelIndex VymModel::getSelectedIndex()
4996 QModelIndexList list=selModel->selectedIndexes();
4997 if (list.isEmpty() )
4998 return QModelIndex();
5000 return list.first();
5003 ImageItem* VymModel::getSelectedImageItem()
5005 QModelIndexList list=selModel->selectedIndexes();
5006 if (!list.isEmpty())
5008 TreeItem *ti=getItem (list.first());
5009 if (ti && ti->getType()==TreeItem::Image)
5010 return (ImageItem*)ti;
5015 QString VymModel::getSelectString ()
5017 return getSelectString (getSelectedItem());
5020 QString VymModel::getSelectString (LinkableMapObj *lmo) // FIXME-2 VM needs to use TreeModel. Port all calls to this funtion to the one using TreeItem below...
5022 if (!lmo) return QString();
5023 return getSelectString (lmo->getTreeItem() );
5026 QString VymModel::getSelectString (TreeItem *ti) //FIXME-1 does not return "mc:"
5030 switch (ti->getType())
5032 case TreeItem::MapCenter: s="mc:"; break;
5033 case TreeItem::Branch: s="bo:";break;
5034 case TreeItem::Image: s="fi:";break;
5037 s= s + QString("%1").arg(ti->num());
5039 // call myself recursively
5040 s= getSelectString(ti->parent()) +","+s;