diff --git a/plugins/filetemplates/filetemplatesplugin.cpp b/plugins/filetemplates/filetemplatesplugin.cpp --- a/plugins/filetemplates/filetemplatesplugin.cpp +++ b/plugins/filetemplates/filetemplatesplugin.cpp @@ -6,10 +6,13 @@ #include #include #include +#include #include #include #include #include +#include +#include #include #include @@ -198,6 +201,32 @@ baseUrl = doc->url().adjusted(QUrl::RemoveFilename); } } + if (!baseUrl.isValid()) { + // fall-back to currently selected project's or item's base directory + ProjectItemContext* projectContext = dynamic_cast(ICore::self()->selectionController()->currentSelection()); + if (projectContext) { + const QList items = projectContext->items(); + if (items.size() == 1) { + ProjectBaseItem* item = items.at(0); + if (item->folder()) { + baseUrl = item->path().toUrl(); + } else if (item->target()) { + baseUrl = item->parent()->path().toUrl(); + } + } + } + } + if (!baseUrl.isValid()) { + // fall back to base directory of currently open project, if there is only one + const QList projects = ICore::self()->projectController()->projects(); + if (projects.size() == 1) { + baseUrl = projects.at(0)->path().toUrl(); + } + } + if (!baseUrl.isValid()) { + // last resort: home path + baseUrl = QUrl::fromLocalFile(QDir::homePath()); + } TemplateClassAssistant* assistant = new TemplateClassAssistant(QApplication::activeWindow(), baseUrl); assistant->setAttribute(Qt::WA_DeleteOnClose); assistant->show();