diff --git a/src/common/meshmodel.cpp b/src/common/meshmodel.cpp index 7392eeb56..f94380734 100644 --- a/src/common/meshmodel.cpp +++ b/src/common/meshmodel.cpp @@ -140,24 +140,42 @@ void MeshDocument::setCurrentRaster( int i) template QString NameDisambiguator(QList &elemList, QString meshLabel ) { - QFileInfo info(meshLabel); - QString newName=info.fileName(); + QString newName=meshLabel; typename QList::iterator mmi; + for(mmi=elemList.begin(); mmi!=elemList.end(); ++mmi) { - if((*mmi)->label() == newName) + if((*mmi)->label() == newName) // if duplicated name found { 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(2).toInt(); - if(lastNum) - newName = baseName.left(baseName.length()-(lastNum<10?1:2))+QString::number(lastNum+1); + QString suffix = fi.suffix(); + bool ok; + + // if name ends with a number between parenthesis (XXX), + // it was himself a duplicated name, and we need to + // just increase the number between parenthesis + int numDisamb; + int startDisamb; + int endDisamb; + + startDisamb = baseName.lastIndexOf("("); + endDisamb = baseName.lastIndexOf(")"); + if((startDisamb!=-1)&&(endDisamb!=-1)) + numDisamb = (baseName.mid((startDisamb+1),(endDisamb-startDisamb-1))).toInt(&ok); else - newName = baseName+"_1"; - if (info.suffix() != QString("")) - newName = newName + "." + info.suffix(); + numDisamb = 0; + + if(startDisamb!=-1) + newName = baseName.left(startDisamb)+ "(" + QString::number(numDisamb+1) + ")"; + else + newName = baseName + "(" + QString::number(numDisamb+1) + ")"; + + if (suffix != QString("")) + newName = newName + "." + suffix; + + // now recurse to see if the new name is free + newName = NameDisambiguator(elemList, newName); } } return newName; diff --git a/src/meshlab/mainwindow_RunTime.cpp b/src/meshlab/mainwindow_RunTime.cpp index 59d0b38fa..2bd2261ee 100644 --- a/src/meshlab/mainwindow_RunTime.cpp +++ b/src/meshlab/mainwindow_RunTime.cpp @@ -2065,7 +2065,8 @@ bool MainWindow::importMesh(QString fileName) } int mask = 0; //MeshModel *mm= new MeshModel(gla->meshDoc); - MeshModel *mm=meshDoc()->addNewMesh(qPrintable(fileName),""); + QFileInfo info(fileName); + MeshModel *mm=meshDoc()->addNewMesh(qPrintable(fileName),info.fileName()); qb->show(); QTime t;t.start(); bool open = loadMesh(fileName,pCurrentIOPlugin,mm,mask,&prePar);