From fee09e56741c508f492818efe449c8cc22dba14d Mon Sep 17 00:00:00 2001 From: Paolo Cignoni cignoni Date: Wed, 17 Nov 2010 15:54:18 +0000 Subject: [PATCH] Cleaned up the interface for name and paths for mesh and raster --- src/common/meshmodel.cpp | 120 +++++++++++------------------ src/common/meshmodel.h | 31 +++++--- src/meshlab/layerDialog.cpp | 2 +- src/meshlab/mainwindow_RunTime.cpp | 4 +- 4 files changed, 65 insertions(+), 92 deletions(-) diff --git a/src/common/meshmodel.cpp b/src/common/meshmodel.cpp index b198ca434..5ba7677a5 100644 --- a/src/common/meshmodel.cpp +++ b/src/common/meshmodel.cpp @@ -55,7 +55,7 @@ MeshModel *MeshDocument::getMesh(int i) return 0; } -MeshModel *MeshDocument::getMesh(const char *name) +MeshModel *MeshDocument::getMesh(QString name) { foreach(MeshModel *mmp, meshList) { @@ -114,29 +114,35 @@ void MeshDocument::setCurrentRaster( int i) return; } - -MeshModel * MeshDocument::addNewMesh(const char *meshLabel, MeshModel *newMesh, bool setAsCurrent) +template +QString NameDisambiguator(QList &elemList, QString meshLabel ) { QFileInfo info(meshLabel); - QString newName=info.fileName(); - for(QList::iterator mmi=meshList.begin();mmi!=meshList.end();++mmi) - { - if((*mmi)->fullName() == newName) - { - QFileInfo fi((*mmi)->fullName()); - QString baseName = fi.baseName(); - int lastNum = baseName.right(1).toInt(); - if( baseName.right(2).toInt() >= 10) - lastNum = baseName.right(1).toInt(); - if(lastNum) - newName = baseName.left(baseName.length()-1)+QString::number(lastNum+1); - else - newName = baseName+"_1"; - if (info.suffix() != QString("")) - newName = newName + "." + info.suffix(); - } - } + QString newName=info.fileName(); + typename QList::iterator mmi; + for(mmi=elemList.begin(); mmi!=elemList.end(); ++mmi) + { + if((*mmi)->label() == newName) + { + QFileInfo fi((*mmi)->label()); + QString baseName = fi.baseName(); // all characters in the file up to the first '.' Eg "/tmp/archive.tar.gz" -> "archive" + int lastNum = baseName.right(1).toInt(); + if( baseName.right(2).toInt() >= 10) + lastNum = baseName.right(1).toInt(); + if(lastNum) + newName = baseName.left(baseName.length()-1)+QString::number(lastNum+1); + else + newName = baseName+"_1"; + if (info.suffix() != QString("")) + newName = newName + "." + info.suffix(); + } + } + return newName; +} +MeshModel * MeshDocument::addNewMesh(QString meshLabel, MeshModel *newMesh, bool setAsCurrent) +{ + QString newName = NameDisambiguator(this->meshList,meshLabel); if(newMesh==0) newMesh=new MeshModel(this,qPrintable(newName)); else @@ -154,72 +160,32 @@ MeshModel * MeshDocument::addNewMesh(const char *meshLabel, MeshModel *newMesh, bool MeshDocument::delMesh(MeshModel *mmToDel) { if(meshList.size()==1) return false; + if(!meshList.removeOne(mmToDel)) return false; + if(currentMesh == mmToDel) + setCurrentMesh(this->meshList.at(0)->id()); - QMutableListIterator i(meshList); - - while (i.hasNext()) - { - MeshModel *md = i.next(); - - if (md==mmToDel) - { - i.remove(); - delete mmToDel; - } - } - - if(currentMesh == mmToDel) - { - if (!meshList.isEmpty()) - setCurrentMesh(this->meshList.at(0)->id()); - else - { - this->Log.Logf(GLLogStream::SYSTEM,"Empty MeshDocument: should never happened!"); - } - } - - emit meshSetChanged(); + delete mmToDel; + emit meshSetChanged(); return true; } -RasterModel * MeshDocument::addNewRaster(const char *rasterName, RasterModel *newRaster) +RasterModel * MeshDocument::addNewRaster(QString fullPathFilename) { - QFileInfo info(rasterName); - QString newName=info.fileName(); - for(QList::iterator ri=rasterList.begin();ri!=rasterList.end();++ri) - { - if((*ri)->getName() == newName) - { - QFileInfo fi((*ri)->getName()); - QString baseName = fi.baseName(); - int lastNum = baseName.right(1).toInt(); - if( baseName.right(2).toInt() >= 10) - lastNum = baseName.right(1).toInt(); - if(lastNum) - newName = baseName.left(baseName.length()-1)+QString::number(lastNum+1); - else - newName = baseName+"_1"; - if (info.suffix() != QString("")) - newName = newName + "." + info.suffix(); - } - } - - if(newRaster==0) - newRaster=new RasterModel(this,qPrintable(newName)); - else - newRaster->setRasterName(newName); + QFileInfo info(fullPathFilename); + QString newLabel=info.fileName(); + QString newName = NameDisambiguator(this->rasterList, newLabel); + RasterModel *newRaster=new RasterModel(this, newLabel); rasterList.push_back(newRaster); - emit rasterSetChanged(); - //Add new plane - Plane *plane = new Plane(newRaster, rasterName, QString()); + Plane *plane = new Plane(newRaster, fullPathFilename, QString()); newRaster->addPlane(plane); this->setCurrentRaster(newRaster->id()); + emit rasterSetChanged(); return newRaster; } @@ -261,7 +227,7 @@ void MeshDocument::removeTag(int id){ } } -MeshModel::MeshModel(MeshDocument *parent, const char *meshName) { +MeshModel::MeshModel(MeshDocument *parent, QString meshName) { glw.m=&cm; _id=parent->newMeshId(); // These data are always active on the mesh @@ -273,7 +239,7 @@ MeshModel::MeshModel(MeshDocument *parent, const char *meshName) { cm.Tr.SetIdentity(); cm.sfn=0; cm.svn=0; - if(meshName) + if(!meshName.isEmpty()) fullPathFileName=meshName; //parent->addNewMesh(qPrintable(fullPathFileName),this); } @@ -389,9 +355,9 @@ Plane::Plane(RasterModel *_parent, const QString pathName, const QString _semant image = QImage(pathName); } -RasterModel::RasterModel(MeshDocument *parent, const char *_rasterName) { +RasterModel::RasterModel(MeshDocument *parent, QString _rasterName) { _id=parent->newRasterId(); - rasterName= _rasterName; + this->_label= _rasterName; visible=false; } diff --git a/src/common/meshmodel.h b/src/common/meshmodel.h index 9f5d504f5..1765d11a0 100644 --- a/src/common/meshmodel.h +++ b/src/common/meshmodel.h @@ -252,14 +252,14 @@ public: const QString suffixName() const {QFileInfo fi(fullName()); return fi.suffix();} /// the relative path with respect to the current project - const QString relativeName() const {QFileInfo fi(fullName()); return fi.suffix();} + const QString relativeName() const {QFileInfo fi(fullName()); assert(0); return fi.suffix();} void setFileName(QString newFileName) {fullPathFileName = newFileName;} public: bool visible; // used in rendering; Needed for toggling on and off the meshes - MeshModel(MeshDocument *parent, const char *meshName=0); + MeshModel(MeshDocument *parent, QString meshName=QString()); bool Render(vcg::GLW::DrawMode _dm, vcg::GLW::ColorMode _cm, vcg::GLW::TextureMode _tm); bool RenderSelectedFace(); bool RenderSelectedVert(); @@ -292,6 +292,11 @@ public: QString fullPathFileName; QImage image; + /// The whole full path name of the mesh + const QString fullName() const {return fullPathFileName;} + /// just the name of the file + const QString shortName() const { return QFileInfo(fullPathFileName).fileName(); } + Plane(RasterModel *parent, const QString pathName, const QString _semantic); }; //end class Plane @@ -307,8 +312,7 @@ class RasterModel public: vcg::Shotf shot; - QString rasterName; - bool visible; // used in rendering; Needed for switching from mesh view mode to raster view mode and vice versa. + bool visible; // used in rendering; Needed for switching from mesh view mode to raster view mode and vice versa. ///The list of the registered images QList planeList; @@ -316,17 +320,22 @@ public: private: int _id; + QString _label; public: inline int id() const {return _id;} - RasterModel(MeshDocument *parent, const char *_rasterName=0); + RasterModel(MeshDocument *parent, QString _rasterName=QString()); - void setRasterName(QString newFileName) {rasterName = newFileName;}; - const QString getName() const {return rasterName;} + void setLabel(QString newLabel) {_label = newLabel;}; + + const QString label() const { + if(!_label.isEmpty()) return _label; + if(!planeList.empty()) return planeList.first()->shortName(); + return "Error!"; + } void addPlane(Plane * plane); - };// end class RasterModel class MeshDocument; @@ -412,7 +421,7 @@ public: //returns the mesh ata given position in the list MeshModel *getMesh(int i); - MeshModel *getMesh(const char *name); + MeshModel *getMesh(QString name); @@ -474,13 +483,13 @@ public: QList getMeshTags(int meshId); ///add a new mesh with the given name - MeshModel *addNewMesh(const char *meshLabel, MeshModel *newMesh=0, bool setAsCurrent=true); + MeshModel *addNewMesh(QString meshLabel, MeshModel *newMesh=0, bool setAsCurrent=true); ///remove the mesh from the list and delete it from memory bool delMesh(MeshModel *mmToDel); ///add a new raster model - RasterModel *addNewRaster(const char *rasterName,RasterModel *newRaster=0); + RasterModel *addNewRaster(QString rasterName); ///remove the raster from the list and delete it from memory bool delRaster(RasterModel *rasterToDel); diff --git a/src/meshlab/layerDialog.cpp b/src/meshlab/layerDialog.cpp index fcfb50bdf..856535113 100644 --- a/src/meshlab/layerDialog.cpp +++ b/src/meshlab/layerDialog.cpp @@ -476,7 +476,7 @@ RasterTreeWidgetItem::RasterTreeWidgetItem(RasterModel *rasterModel) setText(1, QString::number(rasterModel->id())); - QString rasterName = rasterModel->getName(); + QString rasterName = rasterModel->label(); setText(2, rasterName); r=rasterModel; diff --git a/src/meshlab/mainwindow_RunTime.cpp b/src/meshlab/mainwindow_RunTime.cpp index a518f2c75..be134b380 100644 --- a/src/meshlab/mainwindow_RunTime.cpp +++ b/src/meshlab/mainwindow_RunTime.cpp @@ -1242,10 +1242,8 @@ bool MainWindow::open(QString fileName, GLArea *gla) if(!GLA()) newDocument(); - RasterModel *rm= new RasterModel(GLA()->meshDoc); - GLA()->meshDoc->setBusy(true); - GLA()->meshDoc->addNewRaster(qPrintable(fileName),rm); + RasterModel *rm= GLA()->meshDoc->addNewRaster(qPrintable(fileName)); meshDoc()->setBusy(false); if(mdiarea->isVisible()) GLA()->mvc->showMaximized();