forked from qt-creator/qt-creator
QmlDesigner: Refactor the code for adding assets
Change-Id: I25ddfb6df40494eee92d2d90faf3e90c15a82c3e Reviewed-by: Samuel Ghinet <samuel.ghinet@qt.io> Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
This commit is contained in:
@@ -560,45 +560,31 @@ void ItemLibraryWidget::addImportForItem(const QString &importUrl)
|
|||||||
|
|
||||||
void ItemLibraryWidget::addResources(const QStringList &files)
|
void ItemLibraryWidget::addResources(const QStringList &files)
|
||||||
{
|
{
|
||||||
auto document = QmlDesignerPlugin::instance()->currentDesignDocument();
|
DesignDocument *document = QmlDesignerPlugin::instance()->currentDesignDocument();
|
||||||
|
|
||||||
QTC_ASSERT(document, return);
|
QTC_ASSERT(document, return);
|
||||||
|
|
||||||
QList<AddResourceHandler> handlers = QmlDesignerPlugin::instance()->viewManager().designerActionManager().addResourceHandler();
|
const QList<AddResourceHandler> handlers = QmlDesignerPlugin::instance()->viewManager()
|
||||||
|
.designerActionManager().addResourceHandler();
|
||||||
QMultiMap<QString, QString> map;
|
|
||||||
for (const AddResourceHandler &handler : handlers) {
|
|
||||||
map.insert(handler.category, handler.filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
QMap<QString, QString> reverseMap;
|
|
||||||
for (const AddResourceHandler &handler : handlers) {
|
|
||||||
reverseMap.insert(handler.filter, handler.category);
|
|
||||||
}
|
|
||||||
|
|
||||||
QMap<QString, int> priorities;
|
|
||||||
for (const AddResourceHandler &handler : handlers) {
|
|
||||||
priorities.insert(handler.category, handler.piority);
|
|
||||||
}
|
|
||||||
|
|
||||||
QStringList sortedKeys = map.uniqueKeys();
|
|
||||||
Utils::sort(sortedKeys, [&priorities](const QString &first,
|
|
||||||
const QString &second){
|
|
||||||
return priorities.value(first) < priorities.value(second);
|
|
||||||
});
|
|
||||||
|
|
||||||
QStringList fileNames = files;
|
QStringList fileNames = files;
|
||||||
if (fileNames.isEmpty()) {
|
if (fileNames.isEmpty()) { // if no files, show the "add assets" dialog
|
||||||
QStringList filters;
|
QMultiMap<QString, QString> map;
|
||||||
|
QHash<QString, int> priorities;
|
||||||
for (const QString &key : qAsConst(sortedKeys)) {
|
for (const AddResourceHandler &handler : handlers) {
|
||||||
QString str = key + " (";
|
map.insert(handler.category, handler.filter);
|
||||||
str.append(map.values(key).join(" "));
|
priorities.insert(handler.category, handler.piority);
|
||||||
str.append(")");
|
|
||||||
filters.append(str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
filters.prepend(tr("All Files (%1)").arg(map.values().join(" ")));
|
QStringList sortedKeys = map.uniqueKeys();
|
||||||
|
Utils::sort(sortedKeys, [&priorities](const QString &first, const QString &second) {
|
||||||
|
return priorities.value(first) < priorities.value(second);
|
||||||
|
});
|
||||||
|
|
||||||
|
QStringList filters { tr("All Files (%1)").arg(map.values().join(' ')) };
|
||||||
|
QString filterTemplate = "%1 (%2)";
|
||||||
|
for (const QString &key : qAsConst(sortedKeys))
|
||||||
|
filters.append(filterTemplate.arg(key, map.values(key).join(' ')));
|
||||||
|
|
||||||
static QString lastDir;
|
static QString lastDir;
|
||||||
const QString currentDir = lastDir.isEmpty() ? document->fileName().parentDir().toString() : lastDir;
|
const QString currentDir = lastDir.isEmpty() ? document->fileName().parentDir().toString() : lastDir;
|
||||||
@@ -616,24 +602,27 @@ void ItemLibraryWidget::addResources(const QStringList &files)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QMultiMap<QString, QString> partitionedFileNames;
|
QHash<QString, QString> filterToCategory;
|
||||||
|
QHash<QString, AddResourceOperation> categoryToOperation;
|
||||||
|
for (const AddResourceHandler &handler : handlers) {
|
||||||
|
filterToCategory.insert(handler.filter, handler.category);
|
||||||
|
categoryToOperation.insert(handler.category, handler.operation);
|
||||||
|
}
|
||||||
|
|
||||||
|
QMultiMap<QString, QString> categoryFileNames; // filenames grouped by category
|
||||||
|
|
||||||
for (const QString &fileName : qAsConst(fileNames)) {
|
for (const QString &fileName : qAsConst(fileNames)) {
|
||||||
const QString suffix = "*." + QFileInfo(fileName).suffix().toLower();
|
const QString suffix = "*." + QFileInfo(fileName).suffix().toLower();
|
||||||
const QString category = reverseMap.value(suffix);
|
const QString category = filterToCategory.value(suffix);
|
||||||
partitionedFileNames.insert(category, fileName);
|
categoryFileNames.insert(category, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const QString &category : partitionedFileNames.uniqueKeys()) {
|
for (const QString &category : categoryFileNames.uniqueKeys()) {
|
||||||
for (const AddResourceHandler &handler : handlers) {
|
QStringList fileNames = categoryFileNames.values(category);
|
||||||
QStringList fileNames = partitionedFileNames.values(category);
|
AddResourceOperation operation = categoryToOperation.value(category);
|
||||||
if (handler.category == category) {
|
QmlDesignerPlugin::emitUsageStatistics(Constants::EVENT_RESOURCE_IMPORTED + category);
|
||||||
QmlDesignerPlugin::emitUsageStatistics(Constants::EVENT_RESOURCE_IMPORTED + category);
|
if (!operation(fileNames, document->fileName().parentDir().toString()))
|
||||||
if (!handler.operation(fileNames, document->fileName().parentDir().toString()))
|
Core::AsynchronousMessageBox::warning(tr("Failed to Add Files"), tr("Could not add %1 to project.").arg(fileNames.join(' ')));
|
||||||
Core::AsynchronousMessageBox::warning(tr("Failed to Add Files"), tr("Could not add %1 to project.").arg(fileNames.join(" ")));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user