#6367 Improve the way we automatically name ensembles and cases

This commit is contained in:
Gaute Lindkvist
2020-09-02 15:04:48 +02:00
parent f38d119cc8
commit 7cf24d02ff
17 changed files with 416 additions and 110 deletions

View File

@@ -19,7 +19,11 @@
/////////////////////////////////////////////////////////////////////////////////
#include "RiaFilePathTools.h"
#include "cafAssert.h"
#include <QDir>
#include <set>
//--------------------------------------------------------------------------------------------------
@@ -173,3 +177,170 @@ QString RiaFilePathTools::rootSearchPathFromSearchFilter( const QString& searchF
return pathPartList.join( separator() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList RiaFilePathTools::splitPathIntoComponents( const QString& inputPath, bool splitExtensionIntoSeparateEntry )
{
auto path = QDir::cleanPath( inputPath );
QStringList components;
QDir dir( path );
QFileInfo fileInfo( path );
if ( splitExtensionIntoSeparateEntry )
{
QString extension = fileInfo.completeSuffix();
path = path.replace( QString( ".%1" ).arg( extension ), "" );
components.push_front( extension );
components.push_front( fileInfo.baseName() );
}
else
{
components.push_back( fileInfo.fileName() );
}
while ( dir.cdUp() )
{
components.push_front( dir.dirName() );
}
return components;
}
struct PathNode
{
QString name;
PathNode* parent;
std::list<std::unique_ptr<PathNode>> children;
QString fileName;
PathNode( const QString& name, PathNode* parent )
: name( name )
, parent( parent )
{
}
};
void addToPathTree( PathNode* node, QStringList pathComponents, const QString& fileName )
{
CAF_ASSERT( node );
if ( !pathComponents.empty() )
{
QString pathComponent = pathComponents.front();
pathComponents.pop_front();
for ( auto it = node->children.begin(); it != node->children.end(); ++it )
{
if ( it->get()->name == pathComponent )
{
addToPathTree( it->get(), pathComponents, fileName );
return;
}
}
node->children.push_back( std::make_unique<PathNode>( pathComponent, node ) );
addToPathTree( node->children.back().get(), pathComponents, fileName );
}
else
{
// Reached leaf, just set file name
node->fileName = fileName;
}
}
void trimTree( PathNode* node )
{
if ( node->children.size() == 1u )
{
// Unnecessary level. Remove it.
std::unique_ptr<PathNode> singleChildNode = std::move( node->children.front() );
node->children.clear();
node->children.swap( singleChildNode->children );
node->fileName = singleChildNode->fileName;
// Re-parent children
for ( auto it = node->children.begin(); it != node->children.end(); ++it )
{
it->get()->parent = node;
}
trimTree( node );
}
else
{
for ( auto it = node->children.begin(); it != node->children.end(); ++it )
{
trimTree( it->get() );
}
}
}
void extractLeafNodes( PathNode* node, std::list<PathNode*>* leafNodes )
{
if ( node->children.empty() )
{
leafNodes->push_back( node );
}
else
{
for ( auto it = node->children.begin(); it != node->children.end(); ++it )
{
extractLeafNodes( it->get(), leafNodes );
}
}
}
void pathToNode( PathNode* node, QStringList* path )
{
CAF_ASSERT( path );
if ( node != nullptr )
{
if ( !node->name.isEmpty() ) path->push_front( node->name );
pathToNode( node->parent, path );
}
}
//--------------------------------------------------------------------------------------------------
/// Takes a list of file paths and returns a map with the key components that separate the path
/// from the others.
//--------------------------------------------------------------------------------------------------
std::map<QString, QStringList> RiaFilePathTools::keyPathComponentsForEachFilePath( const QStringList& filePaths )
{
std::map<QString, QStringList> allComponents;
std::multiset<QString> allPathComponents;
for ( auto fileName : filePaths )
{
QStringList pathComponentsForFile = splitPathIntoComponents( fileName, true );
allComponents[fileName] = pathComponentsForFile;
for ( auto pathComponent : pathComponentsForFile )
{
allPathComponents.insert( pathComponent );
}
}
auto topNode = std::make_unique<PathNode>( "", nullptr );
for ( auto keyComponentsPair : allComponents )
{
addToPathTree( topNode.get(), keyComponentsPair.second, keyComponentsPair.first );
}
trimTree( topNode.get() );
std::list<PathNode*> leafNodes;
extractLeafNodes( topNode.get(), &leafNodes );
std::map<QString, QStringList> keyComponents;
for ( PathNode* node : leafNodes )
{
QStringList path;
pathToNode( node, &path );
keyComponents[node->fileName] = path;
}
return keyComponents;
}

View File

@@ -22,6 +22,8 @@
#include <QByteArray>
#include <QString>
#include <QStringList>
#include <map>
#include <string>
//==================================================================================================
@@ -41,4 +43,8 @@ public:
static std::pair<QString, QString> toFolderAndFileName( const QString& absFileName );
static QString removeDuplicatePathSeparators( const QString& path );
static QString rootSearchPathFromSearchFilter( const QString& searchFilter );
static QStringList splitPathIntoComponents( const QString& path, bool splitExtensionIntoSeparateEntry = false );
static std::map<QString, QStringList> keyPathComponentsForEachFilePath( const QStringList& filePaths );
};

View File

@@ -20,6 +20,7 @@
#include <QRegularExpression>
#include <QString>
#include <QStringList>
//--------------------------------------------------------------------------------------------------
///
@@ -55,3 +56,29 @@ QString RiaTextStringTools::trimAndRemoveDoubleSpaces( const QString& s )
return trimmed;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaTextStringTools::findCommonRoot( const QStringList& stringList )
{
QString root = stringList.front();
for ( const auto& item : stringList )
{
if ( root.length() > item.length() )
{
root.truncate( item.length() );
}
for ( int i = 0; i < root.length(); ++i )
{
if ( root[i] != item[i] )
{
root.truncate( i );
break;
}
}
}
return root;
}

View File

@@ -19,6 +19,7 @@
#pragma once
class QString;
class QStringList;
//--------------------------------------------------------------------------------------------------
///
@@ -27,4 +28,5 @@ namespace RiaTextStringTools
{
bool compare( const QString& expected, const QString& actual );
QString trimAndRemoveDoubleSpaces( const QString& s );
QString findCommonRoot( const QStringList& stringList );
} // namespace RiaTextStringTools