diff options
Diffstat (limited to 'src/plugins/coreplugin/documentmanager.cpp')
-rw-r--r-- | src/plugins/coreplugin/documentmanager.cpp | 138 |
1 files changed, 68 insertions, 70 deletions
diff --git a/src/plugins/coreplugin/documentmanager.cpp b/src/plugins/coreplugin/documentmanager.cpp index 7b52203241..7a31b5bba5 100644 --- a/src/plugins/coreplugin/documentmanager.cpp +++ b/src/plugins/coreplugin/documentmanager.cpp @@ -44,6 +44,7 @@ #include <coreplugin/editormanager/ieditorfactory.h> #include <coreplugin/editormanager/iexternaleditor.h> +#include <utils/fileutils.h> #include <utils/hostosinfo.h> #include <utils/qtcassert.h> #include <utils/pathchooser.h> @@ -103,6 +104,8 @@ static const char projectDirectoryKeyC[] = "Projects"; static const char useProjectDirectoryKeyC[] = "UseProjectsDirectory"; static const char buildDirectoryKeyC[] = "BuildDirectory.Template"; +using namespace Utils; + namespace Core { static void readSettings(); @@ -151,12 +154,11 @@ struct DocumentManagerPrivate QList<DocumentManager::RecentFile> m_recentFiles; static const int m_maxRecentFiles = 7; - QString m_currentFile; - QFileSystemWatcher *m_fileWatcher; // Delayed creation. 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; @@ -168,7 +170,7 @@ struct DocumentManagerPrivate }; static DocumentManager *m_instance; -static Internal::DocumentManagerPrivate *d; +static DocumentManagerPrivate *d; QFileSystemWatcher *DocumentManagerPrivate::fileWatcher() { @@ -182,7 +184,7 @@ QFileSystemWatcher *DocumentManagerPrivate::fileWatcher() QFileSystemWatcher *DocumentManagerPrivate::linkWatcher() { - if (Utils::HostOsInfo::isAnyUnixHost()) { + if (HostOsInfo::isAnyUnixHost()) { if (!m_linkWatcher) { m_linkWatcher = new QFileSystemWatcher(m_instance); m_linkWatcher->setObjectName(QLatin1String("_qt_autotest_force_engine_poller")); @@ -265,8 +267,8 @@ static void addFileInfo(const QString &fileName, IDocument *document, bool isLin (The added file names are guaranteed to be absolute and cleaned.) */ static void addFileInfo(IDocument *document) { - const QString fixedName = DocumentManager::fixFileName(document->filePath(), DocumentManager::KeepLinks); - const QString fixedResolvedName = DocumentManager::fixFileName(document->filePath(), DocumentManager::ResolveLinks); + const QString fixedName = DocumentManager::fixFileName(document->filePath().toString(), DocumentManager::KeepLinks); + const QString fixedResolvedName = DocumentManager::fixFileName(document->filePath().toString(), DocumentManager::ResolveLinks); addFileInfo(fixedResolvedName, document, false); if (fixedName != fixedResolvedName) addFileInfo(fixedName, document, true); @@ -285,7 +287,8 @@ void DocumentManager::addDocuments(const QList<IDocument *> &documents, bool add foreach (IDocument *document, documents) { if (document && !d->m_documentsWithoutWatch.contains(document)) { connect(document, SIGNAL(destroyed(QObject*)), m_instance, SLOT(documentDestroyed(QObject*))); - connect(document, SIGNAL(filePathChanged(QString,QString)), m_instance, SLOT(filePathChanged(QString,QString))); + connect(document, &IDocument::filePathChanged, + m_instance, &DocumentManager::filePathChanged); d->m_documentsWithoutWatch.append(document); } } @@ -296,7 +299,7 @@ void DocumentManager::addDocuments(const QList<IDocument *> &documents, bool add if (document && !d->m_documentsWithWatch.contains(document)) { connect(document, SIGNAL(changed()), m_instance, SLOT(checkForNewFileName())); connect(document, SIGNAL(destroyed(QObject*)), m_instance, SLOT(documentDestroyed(QObject*))); - connect(document, SIGNAL(filePathChanged(QString,QString)), m_instance, SLOT(filePathChanged(QString,QString))); + connect(document, &IDocument::filePathChanged, m_instance, &DocumentManager::filePathChanged); addFileInfo(document); } } @@ -387,20 +390,20 @@ void DocumentManager::renamedFile(const QString &from, const QString &to) foreach (IDocument *document, documentsToRename) { d->m_blockedIDocument = document; removeFileInfo(document); - document->setFilePath(to); + document->setFilePath(FileName::fromString(to)); addFileInfo(document); d->m_blockedIDocument = 0; } emit m_instance->allDocumentsRenamed(from, to); } -void DocumentManager::filePathChanged(const QString &oldName, const QString &newName) +void DocumentManager::filePathChanged(const FileName &oldName, const FileName &newName) { IDocument *doc = qobject_cast<IDocument *>(sender()); QTC_ASSERT(doc, return); if (doc == d->m_blockedIDocument) return; - emit m_instance->documentRenamed(doc, oldName, newName); + emit m_instance->documentRenamed(doc, oldName.toString(), newName.toString()); } /*! @@ -479,7 +482,7 @@ QString DocumentManager::fixFileName(const QString &fileName, FixMode fixmode) s = QDir::cleanPath(s); } s = QDir::toNativeSeparators(s); - if (Utils::HostOsInfo::fileNameCaseSensitivity() == Qt::CaseInsensitive) + if (HostOsInfo::fileNameCaseSensitivity() == Qt::CaseInsensitive) s = s.toLower(); return s; } @@ -564,7 +567,7 @@ static bool saveModifiedFilesHelper(const QList<IDocument *> &documents, foreach (IDocument *document, documents) { if (document && document->isModified()) { - QString name = document->filePath(); + QString name = document->filePath().toString(); if (name.isEmpty()) name = document->suggestedFileName(); @@ -634,7 +637,7 @@ static bool saveModifiedFilesHelper(const QList<IDocument *> &documents, bool DocumentManager::saveDocument(IDocument *document, const QString &fileName, bool *isReadOnly) { bool ret = true; - QString effName = fileName.isEmpty() ? document->filePath() : fileName; + QString effName = fileName.isEmpty() ? document->filePath().toString() : fileName; expectFileChange(effName); // This only matters to other IDocuments which refer to this file bool addWatcher = removeDocument(document); // So that our own IDocument gets no notification at all @@ -721,7 +724,7 @@ QString DocumentManager::getSaveAsFileName(const IDocument *document, const QStr { if (!document) return QLatin1String(""); - QString absoluteFilePath = document->filePath(); + QString absoluteFilePath = document->filePath().toString(); const QFileInfo fi(absoluteFilePath); QString path; QString fileName; @@ -745,7 +748,7 @@ QString DocumentManager::getSaveAsFileName(const IDocument *document, const QStr } absoluteFilePath = getSaveFileName(tr("Save File As"), - path + QDir::separator() + fileName, + path + QLatin1Char('/') + fileName, filterString, selectedFilter); return absoluteFilePath; @@ -876,8 +879,8 @@ QStringList DocumentManager::getOpenFileNames(const QString &filters, { QString path = pathIn; if (path.isEmpty()) { - if (!d->m_currentFile.isEmpty()) - path = QFileInfo(d->m_currentFile).absoluteFilePath(); + if (EditorManager::currentDocument() && !EditorManager::currentDocument()->isTemporary()) + path = EditorManager::currentDocument()->filePath().toString(); if (path.isEmpty() && useProjectsDirectory()) path = projectsDirectory(); } @@ -921,8 +924,8 @@ void DocumentManager::checkForReload() d->m_blockActivated = true; IDocument::ReloadSetting defaultBehavior = EditorManagerPrivate::reloadSetting(); - Utils::ReloadPromptAnswer previousReloadAnswer = Utils::ReloadCurrent; - Utils::FileDeletedPromptAnswer previousDeletedAnswer = Utils::FileDeletedSave; + ReloadPromptAnswer previousReloadAnswer = ReloadCurrent; + FileDeletedPromptAnswer previousDeletedAnswer = FileDeletedSave; QList<IDocument *> documentsToClose; QMap<IDocument*, QString> documentsToSave; @@ -1055,26 +1058,26 @@ void DocumentManager::checkForReload() // IDocument wants us to ask } else if (type == IDocument::TypeContents) { // content change, IDocument wants to ask user - if (previousReloadAnswer == Utils::ReloadNone) { + if (previousReloadAnswer == ReloadNone) { // answer already given, ignore success = document->reload(&errorString, IDocument::FlagIgnore, IDocument::TypeContents); - } else if (previousReloadAnswer == Utils::ReloadAll) { + } else if (previousReloadAnswer == ReloadAll) { // answer already given, reload success = document->reload(&errorString, IDocument::FlagReload, IDocument::TypeContents); } else { // Ask about content change - previousReloadAnswer = Utils::reloadPrompt(document->filePath(), document->isModified(), + previousReloadAnswer = reloadPrompt(document->filePath(), document->isModified(), ICore::dialogParent()); switch (previousReloadAnswer) { - case Utils::ReloadAll: - case Utils::ReloadCurrent: + case ReloadAll: + case ReloadCurrent: success = document->reload(&errorString, IDocument::FlagReload, IDocument::TypeContents); break; - case Utils::ReloadSkipCurrent: - case Utils::ReloadNone: + case ReloadSkipCurrent: + case ReloadNone: success = document->reload(&errorString, IDocument::FlagIgnore, IDocument::TypeContents); break; - case Utils::CloseCurrent: + case CloseCurrent: documentsToClose << document; break; } @@ -1084,18 +1087,18 @@ void DocumentManager::checkForReload() // Ask about removed file bool unhandled = true; while (unhandled) { - if (previousDeletedAnswer != Utils::FileDeletedCloseAll) { + if (previousDeletedAnswer != FileDeletedCloseAll) { previousDeletedAnswer = - Utils::fileDeletedPrompt(document->filePath(), + fileDeletedPrompt(document->filePath().toString(), trigger == IDocument::TriggerExternal, QApplication::activeWindow()); } switch (previousDeletedAnswer) { - case Utils::FileDeletedSave: - documentsToSave.insert(document, document->filePath()); + case FileDeletedSave: + documentsToSave.insert(document, document->filePath().toString()); unhandled = false; break; - case Utils::FileDeletedSaveAs: + case FileDeletedSaveAs: { const QString &saveFileName = getSaveAsFileName(document); if (!saveFileName.isEmpty()) { @@ -1104,8 +1107,8 @@ void DocumentManager::checkForReload() } break; } - case Utils::FileDeletedClose: - case Utils::FileDeletedCloseAll: + case FileDeletedClose: + case FileDeletedCloseAll: documentsToClose << document; unhandled = false; break; @@ -1115,7 +1118,7 @@ void DocumentManager::checkForReload() } if (!success) { if (errorString.isEmpty()) - errorStrings << tr("Cannot reload %1").arg(QDir::toNativeSeparators(document->filePath())); + errorStrings << tr("Cannot reload %1").arg(document->filePath().toUserOutput()); else errorStrings << errorString; } @@ -1189,7 +1192,7 @@ void DocumentManager::saveSettings() recentEditorIds.append(file.second.toString()); } - QSettings *s = Core::ICore::settings(); + QSettings *s = ICore::settings(); s->beginGroup(QLatin1String(settingsGroupC)); s->setValue(QLatin1String(filesKeyC), recentFiles); s->setValue(QLatin1String(editorsKeyC), recentEditorIds); @@ -1203,7 +1206,7 @@ void DocumentManager::saveSettings() void readSettings() { - QSettings *s = Core::ICore::settings(); + QSettings *s = ICore::settings(); d->m_recentFiles.clear(); s->beginGroup(QLatin1String(settingsGroupC)); QStringList recentFiles = s->value(QLatin1String(filesKeyC)).toStringList(); @@ -1226,7 +1229,7 @@ void readSettings() if (!settingsProjectDir.isEmpty() && QFileInfo(settingsProjectDir).isDir()) d->m_projectsDirectory = settingsProjectDir; else - d->m_projectsDirectory = Utils::PathChooser::homePath(); + d->m_projectsDirectory = PathChooser::homePath(); d->m_useProjectsDirectory = s->value(QLatin1String(useProjectDirectoryKeyC), d->m_useProjectsDirectory).toBool(); @@ -1242,45 +1245,40 @@ void readSettings() /*! - The current file is the file currently opened when an editor is active, - or the selected file in case a Project Explorer is active. + Returns the initial directory for a new file dialog. If there is + a current file, uses that, otherwise if there is a default location for + new files, uses that, otherwise uses the last visited directory. - \sa currentFile - */ -void DocumentManager::setCurrentFile(const QString &filePath) + \sa setFileDialogLastVisitedDirectory + \sa setDefaultLocationForNewFiles +*/ + +QString DocumentManager::fileDialogInitialDirectory() { - if (d->m_currentFile == filePath) - return; - d->m_currentFile = filePath; - emit m_instance->currentFileChanged(d->m_currentFile); + 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; } /*! - Returns the absolute path of the current file. - The current file is the file currently opened when an editor is active, - or the selected file in case a Project Explorer is active. + Sets the default location for new files - \sa setCurrentFile - */ -QString DocumentManager::currentFile() + \sa fileDialogInitialDirectory +*/ +QString DocumentManager::defaultLocationForNewFiles() { - return d->m_currentFile; + return d->m_defaultLocationForNewFiles; } /*! - - Returns the initial directory for a new file dialog. If there is - a current file, uses that, otherwise uses the last visited directory. - - \sa setFileDialogLastVisitedDirectory -*/ - -QString DocumentManager::fileDialogInitialDirectory() + Returns the default location for new files + */ +void DocumentManager::setDefaultLocationForNewFiles(const QString &location) { - if (!d->m_currentFile.isEmpty()) - return QFileInfo(d->m_currentFile).absolutePath(); - return d->m_lastVisitedDirectory; + d->m_defaultLocationForNewFiles = location; } /*! @@ -1429,11 +1427,11 @@ void DocumentManager::executeOpenWithMenuAction(QAction *action) if (entry.editorFactory) { // close any open editors that have this file open // remember the views to open new editors in there - QList<Internal::EditorView *> views; + QList<EditorView *> views; QList<IEditor *> editorsOpenForFile = DocumentModel::editorsForFilePath(entry.fileName); foreach (IEditor *openEditor, editorsOpenForFile) { - Internal::EditorView *view = EditorManagerPrivate::viewForEditor(openEditor); + EditorView *view = EditorManagerPrivate::viewForEditor(openEditor); if (view && view->currentEditor() == openEditor) // visible views.append(view); } @@ -1443,12 +1441,12 @@ void DocumentManager::executeOpenWithMenuAction(QAction *action) if (views.isEmpty()) { EditorManager::openEditor(entry.fileName, entry.editorFactory->id()); } else { - if (Internal::EditorView *currentView = EditorManagerPrivate::currentEditorView()) { + if (EditorView *currentView = EditorManagerPrivate::currentEditorView()) { if (views.removeOne(currentView)) views.prepend(currentView); // open editor in current view first } EditorManager::OpenEditorFlags flags; - foreach (Internal::EditorView *view, views) { + foreach (EditorView *view, views) { IEditor *editor = EditorManagerPrivate::openEditor(view, entry.fileName, entry.editorFactory->id(), flags); |