From 230be99474adbe868c61885563ad18454dffbdd8 Mon Sep 17 00:00:00 2001 From: Daniel Teske Date: Fri, 16 Jan 2015 17:47:11 +0100 Subject: [PATCH] Fix default wrong directory if no files are open in the editor Task-number: QTCREATORBUG-13831 Change-Id: Iaeb11014ed16bc9f08d7c12d81c1b7b8a4163d21 Reviewed-by: Eike Ziller --- src/plugins/coreplugin/documentmanager.cpp | 26 ++++++++++++++++++++- src/plugins/coreplugin/documentmanager.h | 3 +++ src/plugins/projectexplorer/projecttree.cpp | 19 +++++++++++++++ src/plugins/projectexplorer/projecttree.h | 1 + 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/plugins/coreplugin/documentmanager.cpp b/src/plugins/coreplugin/documentmanager.cpp index 067b4cfd5ae..87641980c8f 100644 --- a/src/plugins/coreplugin/documentmanager.cpp +++ b/src/plugins/coreplugin/documentmanager.cpp @@ -156,6 +156,7 @@ struct DocumentManagerPrivate QFileSystemWatcher *m_linkWatcher; // Delayed creation (only UNIX/if a link is seen). bool m_blockActivated; QString m_lastVisitedDirectory; + QString m_defaultLocationForNewFiles; QString m_projectsDirectory; bool m_useProjectsDirectory; QString m_buildDirectory; @@ -1243,18 +1244,41 @@ void readSettings() /*! Returns the initial directory for a new file dialog. If there is - a current file, uses that, otherwise uses the last visited directory. + a current file, uses that, otherwise if there is a default location for + new files, uses that, otherwise uses the last visited directory. \sa setFileDialogLastVisitedDirectory + \sa setDefaultLocationForNewFiles */ QString DocumentManager::fileDialogInitialDirectory() { if (EditorManager::currentDocument() && !EditorManager::currentDocument()->isTemporary()) return QFileInfo(EditorManager::currentDocument()->filePath().toString()).absolutePath(); + if (!d->m_defaultLocationForNewFiles.isEmpty()) + return d->m_defaultLocationForNewFiles; return d->m_lastVisitedDirectory; } +/*! + + Sets the default location for new files + + \sa fileDialogInitialDirectory +*/ +QString DocumentManager::defaultLocationForNewFiles() +{ + return d->m_defaultLocationForNewFiles; +} + +/*! + Returns the default location for new files + */ +void DocumentManager::setDefaultLocationForNewFiles(const QString &location) +{ + d->m_defaultLocationForNewFiles = location; +} + /*! Returns the directory for projects. Defaults to HOME. diff --git a/src/plugins/coreplugin/documentmanager.h b/src/plugins/coreplugin/documentmanager.h index 8fab9a05061..e5d66566dc3 100644 --- a/src/plugins/coreplugin/documentmanager.h +++ b/src/plugins/coreplugin/documentmanager.h @@ -125,6 +125,9 @@ public: static QString fileDialogInitialDirectory(); + static QString defaultLocationForNewFiles(); + static void setDefaultLocationForNewFiles(const QString &location); + static bool useProjectsDirectory(); static void setUseProjectsDirectory(bool); diff --git a/src/plugins/projectexplorer/projecttree.cpp b/src/plugins/projectexplorer/projecttree.cpp index 53210312c77..95f95aab2f6 100644 --- a/src/plugins/projectexplorer/projecttree.cpp +++ b/src/plugins/projectexplorer/projecttree.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -72,6 +73,13 @@ ProjectTree::ProjectTree(QObject *parent) connect(qApp, &QApplication::focusChanged, this, &ProjectTree::focusChanged); + + connect(SessionManager::instance(), &SessionManager::projectAdded, + this, &ProjectTree::updateDefaultLocationForNewFiles); + connect(SessionManager::instance(), &SessionManager::projectRemoved, + this, &ProjectTree::updateDefaultLocationForNewFiles); + connect(SessionManager::instance(), &SessionManager::startupProjectChanged, + this, &ProjectTree::updateDefaultLocationForNewFiles); } void ProjectTree::aboutToShutDown() @@ -215,9 +223,20 @@ void ProjectTree::update(Node *node, Project *project) emit currentProjectChanged(m_currentProject); + updateDefaultLocationForNewFiles(); updateContext(); } +void ProjectTree::updateDefaultLocationForNewFiles() +{ + if (m_currentProject) + Core::DocumentManager::setDefaultLocationForNewFiles(m_currentProject->projectDirectory().toString()); + else if (SessionManager::startupProject()) + Core::DocumentManager::setDefaultLocationForNewFiles(SessionManager::startupProject()->projectDirectory().toString()); + else + Core::DocumentManager::setDefaultLocationForNewFiles(QString()); +} + void ProjectTree::updateContext() { Core::Context oldContext; diff --git a/src/plugins/projectexplorer/projecttree.h b/src/plugins/projectexplorer/projecttree.h index fbaef27bd4d..642458221e3 100644 --- a/src/plugins/projectexplorer/projecttree.h +++ b/src/plugins/projectexplorer/projecttree.h @@ -130,6 +130,7 @@ public: // for nodes to emit signals, do not call unless you are a node void collapseAll(); private: + void updateDefaultLocationForNewFiles(); void focusChanged(); void updateFromProjectTreeWidget(Internal::ProjectTreeWidget *widget); void documentManagerCurrentFileChanged();