forked from qt-creator/qt-creator
Recent files should remember the editor type as well.
Task-number: QTCREATORBUG-1785
This commit is contained in:
@@ -1111,7 +1111,8 @@ void EditorManager::addEditor(IEditor *editor, bool isDuplicate)
|
|||||||
const bool addWatcher = !isTemporary;
|
const bool addWatcher = !isTemporary;
|
||||||
m_d->m_core->fileManager()->addFile(editor->file(), addWatcher);
|
m_d->m_core->fileManager()->addFile(editor->file(), addWatcher);
|
||||||
if (!isTemporary)
|
if (!isTemporary)
|
||||||
m_d->m_core->fileManager()->addToRecentFiles(editor->file()->fileName());
|
m_d->m_core->fileManager()->addToRecentFiles(editor->file()->fileName(),
|
||||||
|
editor->id());
|
||||||
}
|
}
|
||||||
emit editorOpened(editor);
|
emit editorOpened(editor);
|
||||||
}
|
}
|
||||||
@@ -1473,15 +1474,17 @@ bool EditorManager::saveFileAs(IFile *fileParam)
|
|||||||
void EditorManager::addFileToRecentFiles(IFile *file)
|
void EditorManager::addFileToRecentFiles(IFile *file)
|
||||||
{
|
{
|
||||||
bool isTemporary = true;
|
bool isTemporary = true;
|
||||||
|
QString editorId;
|
||||||
QList<IEditor *> editors = editorsForFile(file);
|
QList<IEditor *> editors = editorsForFile(file);
|
||||||
foreach (IEditor *editor, editors) {
|
foreach (IEditor *editor, editors) {
|
||||||
if (!editor->isTemporary()) {
|
if (!editor->isTemporary()) {
|
||||||
|
editorId = editor->id();
|
||||||
isTemporary = false;
|
isTemporary = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isTemporary)
|
if (!isTemporary)
|
||||||
m_d->m_core->fileManager()->addToRecentFiles(file->fileName());
|
m_d->m_core->fileManager()->addToRecentFiles(file->fileName(), editorId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditorManager::gotoNextDocHistory()
|
void EditorManager::gotoNextDocHistory()
|
||||||
|
|||||||
@@ -84,12 +84,13 @@
|
|||||||
(see addToRecentFiles() and recentFiles()).
|
(see addToRecentFiles() and recentFiles()).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static const char settingsGroupC[] = "RecentFiles";
|
static const char * const settingsGroupC = "RecentFiles";
|
||||||
static const char filesKeyC[] = "Files";
|
static const char * const filesKeyC = "Files";
|
||||||
|
static const char * const editorsKeyC = "EditorIds";
|
||||||
|
|
||||||
static const char directoryGroupC[] = "Directories";
|
static const char * const directoryGroupC = "Directories";
|
||||||
static const char projectDirectoryKeyC[] = "Projects";
|
static const char * const projectDirectoryKeyC = "Projects";
|
||||||
static const char useProjectDirectoryKeyC[] = "UseProjectsDirectory";
|
static const char * const useProjectDirectoryKeyC = "UseProjectsDirectory";
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
@@ -116,7 +117,7 @@ struct FileManagerPrivate {
|
|||||||
QList<IFile *> m_filesWithoutWatch;
|
QList<IFile *> m_filesWithoutWatch;
|
||||||
QMap<IFile *, QStringList> m_filesWithWatch;
|
QMap<IFile *, QStringList> m_filesWithWatch;
|
||||||
|
|
||||||
QStringList m_recentFiles;
|
QList<FileManager::RecentFile> m_recentFiles;
|
||||||
static const int m_maxRecentFiles = 7;
|
static const int m_maxRecentFiles = 7;
|
||||||
|
|
||||||
QString m_currentFile;
|
QString m_currentFile;
|
||||||
@@ -1016,24 +1017,28 @@ void FileManager::syncWithEditor(Core::IContext *context)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn void FileManager::addToRecentFiles(const QString &fileName)
|
\fn void FileManager::addToRecentFiles(const QString &fileName, , const QString &editorId)
|
||||||
|
|
||||||
Adds the \a fileName to the list of recent files.
|
Adds the \a fileName to the list of recent files. Associates the file to
|
||||||
|
be reopened with an editor of the given \a editorId, if possible.
|
||||||
|
\a editorId defaults to the empty id, which means to let the system figure out
|
||||||
|
the best editor itself.
|
||||||
*/
|
*/
|
||||||
void FileManager::addToRecentFiles(const QString &fileName)
|
void FileManager::addToRecentFiles(const QString &fileName, const QString &editorId)
|
||||||
{
|
{
|
||||||
if (fileName.isEmpty())
|
if (fileName.isEmpty())
|
||||||
return;
|
return;
|
||||||
QString unifiedForm(fixFileName(fileName, KeepLinks));
|
QString unifiedForm(fixFileName(fileName, KeepLinks));
|
||||||
QMutableStringListIterator it(d->m_recentFiles);
|
QMutableListIterator<RecentFile > it(d->m_recentFiles);
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
QString recentUnifiedForm(fixFileName(it.next(), KeepLinks));
|
RecentFile file = it.next();
|
||||||
|
QString recentUnifiedForm(fixFileName(file.first, KeepLinks));
|
||||||
if (unifiedForm == recentUnifiedForm)
|
if (unifiedForm == recentUnifiedForm)
|
||||||
it.remove();
|
it.remove();
|
||||||
}
|
}
|
||||||
if (d->m_recentFiles.count() > d->m_maxRecentFiles)
|
if (d->m_recentFiles.count() > d->m_maxRecentFiles)
|
||||||
d->m_recentFiles.removeLast();
|
d->m_recentFiles.removeLast();
|
||||||
d->m_recentFiles.prepend(fileName);
|
d->m_recentFiles.prepend(RecentFile(fileName, editorId));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -1041,16 +1046,24 @@ void FileManager::addToRecentFiles(const QString &fileName)
|
|||||||
|
|
||||||
Returns the list of recent files.
|
Returns the list of recent files.
|
||||||
*/
|
*/
|
||||||
QStringList FileManager::recentFiles() const
|
QList<FileManager::RecentFile> FileManager::recentFiles() const
|
||||||
{
|
{
|
||||||
return d->m_recentFiles;
|
return d->m_recentFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileManager::saveSettings()
|
void FileManager::saveSettings()
|
||||||
{
|
{
|
||||||
|
QStringList recentFiles;
|
||||||
|
QStringList recentEditorIds;
|
||||||
|
foreach (const RecentFile &file, d->m_recentFiles) {
|
||||||
|
recentFiles.append(file.first);
|
||||||
|
recentEditorIds.append(file.second);
|
||||||
|
}
|
||||||
|
|
||||||
QSettings *s = Core::ICore::instance()->settings();
|
QSettings *s = Core::ICore::instance()->settings();
|
||||||
s->beginGroup(QLatin1String(settingsGroupC));
|
s->beginGroup(QLatin1String(settingsGroupC));
|
||||||
s->setValue(QLatin1String(filesKeyC), d->m_recentFiles);
|
s->setValue(QLatin1String(filesKeyC), recentFiles);
|
||||||
|
s->setValue(QLatin1String(editorsKeyC), recentEditorIds);
|
||||||
s->endGroup();
|
s->endGroup();
|
||||||
s->beginGroup(QLatin1String(directoryGroupC));
|
s->beginGroup(QLatin1String(directoryGroupC));
|
||||||
s->setValue(QLatin1String(projectDirectoryKeyC), d->m_projectsDirectory);
|
s->setValue(QLatin1String(projectDirectoryKeyC), d->m_projectsDirectory);
|
||||||
@@ -1060,25 +1073,34 @@ void FileManager::saveSettings()
|
|||||||
|
|
||||||
void FileManager::readSettings()
|
void FileManager::readSettings()
|
||||||
{
|
{
|
||||||
const QSettings *s = Core::ICore::instance()->settings();
|
QSettings *s = Core::ICore::instance()->settings();
|
||||||
d->m_recentFiles.clear();
|
d->m_recentFiles.clear();
|
||||||
QStringList recentFiles = s->value(QLatin1String(settingsGroupC) + QLatin1Char('/') + QLatin1String(filesKeyC), QStringList()).toStringList();
|
s->beginGroup(QLatin1String(settingsGroupC));
|
||||||
|
QStringList recentFiles = s->value(QLatin1String(filesKeyC)).toStringList();
|
||||||
|
QStringList recentEditorIds = s->value(QLatin1String(editorsKeyC)).toStringList();
|
||||||
|
s->endGroup();
|
||||||
// clean non-existing files
|
// clean non-existing files
|
||||||
foreach (const QString &file, recentFiles) {
|
QStringListIterator ids(recentEditorIds);
|
||||||
if (QFileInfo(file).isFile())
|
foreach (const QString &fileName, recentFiles) {
|
||||||
d->m_recentFiles.append(QDir::fromNativeSeparators(file)); // from native to guard against old settings
|
QString editorId;
|
||||||
|
if (ids.hasNext()) // guard against old or weird settings
|
||||||
|
editorId = ids.next();
|
||||||
|
if (QFileInfo(fileName).isFile())
|
||||||
|
d->m_recentFiles.append(RecentFile(QDir::fromNativeSeparators(fileName), // from native to guard against old settings
|
||||||
|
editorId));
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString directoryGroup = QLatin1String(directoryGroupC) + QLatin1Char('/');
|
s->beginGroup(QLatin1String(directoryGroupC));
|
||||||
const QString settingsProjectDir = s->value(directoryGroup + QLatin1String(projectDirectoryKeyC),
|
const QString settingsProjectDir = s->value(QLatin1String(projectDirectoryKeyC),
|
||||||
QString()).toString();
|
QString()).toString();
|
||||||
if (!settingsProjectDir.isEmpty() && QFileInfo(settingsProjectDir).isDir()) {
|
if (!settingsProjectDir.isEmpty() && QFileInfo(settingsProjectDir).isDir()) {
|
||||||
d->m_projectsDirectory = settingsProjectDir;
|
d->m_projectsDirectory = settingsProjectDir;
|
||||||
} else {
|
} else {
|
||||||
d->m_projectsDirectory = Utils::PathChooser::homePath();
|
d->m_projectsDirectory = Utils::PathChooser::homePath();
|
||||||
}
|
}
|
||||||
d->m_useProjectsDirectory = s->value(directoryGroup + QLatin1String(useProjectDirectoryKeyC),
|
d->m_useProjectsDirectory = s->value(QLatin1String(useProjectDirectoryKeyC),
|
||||||
d->m_useProjectsDirectory).toBool();
|
d->m_useProjectsDirectory).toBool();
|
||||||
|
s->endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|||||||
@@ -34,6 +34,8 @@
|
|||||||
|
|
||||||
#include <QtCore/QObject>
|
#include <QtCore/QObject>
|
||||||
#include <QtCore/QStringList>
|
#include <QtCore/QStringList>
|
||||||
|
#include <QtCore/QPair>
|
||||||
|
#include <QtCore/QVariant>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QMainWindow;
|
class QMainWindow;
|
||||||
@@ -59,6 +61,8 @@ public:
|
|||||||
KeepLinks
|
KeepLinks
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef QPair<QString, QString> RecentFile;
|
||||||
|
|
||||||
explicit FileManager(QMainWindow *ew);
|
explicit FileManager(QMainWindow *ew);
|
||||||
virtual ~FileManager();
|
virtual ~FileManager();
|
||||||
|
|
||||||
@@ -79,8 +83,8 @@ public:
|
|||||||
void unexpectFileChange(const QString &fileName);
|
void unexpectFileChange(const QString &fileName);
|
||||||
|
|
||||||
// recent files
|
// recent files
|
||||||
void addToRecentFiles(const QString &fileName);
|
void addToRecentFiles(const QString &fileName, const QString &editorId = QString());
|
||||||
QStringList recentFiles() const;
|
QList<RecentFile> recentFiles() const;
|
||||||
void saveSettings();
|
void saveSettings();
|
||||||
|
|
||||||
// current file
|
// current file
|
||||||
@@ -183,4 +187,6 @@ private:
|
|||||||
|
|
||||||
} // namespace Core
|
} // namespace Core
|
||||||
|
|
||||||
|
Q_DECLARE_METATYPE(Core::FileManager::RecentFile)
|
||||||
|
|
||||||
#endif // FILEMANAGER_H
|
#endif // FILEMANAGER_H
|
||||||
|
|||||||
@@ -1278,11 +1278,11 @@ void MainWindow::aboutToShowRecentFiles()
|
|||||||
aci->menu()->clear();
|
aci->menu()->clear();
|
||||||
|
|
||||||
bool hasRecentFiles = false;
|
bool hasRecentFiles = false;
|
||||||
foreach (const QString &fileName, m_fileManager->recentFiles()) {
|
foreach (const FileManager::RecentFile &file, m_fileManager->recentFiles()) {
|
||||||
hasRecentFiles = true;
|
hasRecentFiles = true;
|
||||||
QAction *action = aci->menu()->addAction(
|
QAction *action = aci->menu()->addAction(
|
||||||
QDir::toNativeSeparators(Utils::withTildeHomePath(fileName)));
|
QDir::toNativeSeparators(Utils::withTildeHomePath(file.first)));
|
||||||
action->setData(fileName);
|
action->setData(qVariantFromValue(file));
|
||||||
connect(action, SIGNAL(triggered()), this, SLOT(openRecentFile()));
|
connect(action, SIGNAL(triggered()), this, SLOT(openRecentFile()));
|
||||||
}
|
}
|
||||||
aci->menu()->setEnabled(hasRecentFiles);
|
aci->menu()->setEnabled(hasRecentFiles);
|
||||||
@@ -1291,9 +1291,8 @@ void MainWindow::aboutToShowRecentFiles()
|
|||||||
void MainWindow::openRecentFile()
|
void MainWindow::openRecentFile()
|
||||||
{
|
{
|
||||||
if (const QAction *action = qobject_cast<const QAction*>(sender())) {
|
if (const QAction *action = qobject_cast<const QAction*>(sender())) {
|
||||||
const QString fileName = action->data().toString();
|
const FileManager::RecentFile file = action->data().value<FileManager::RecentFile>();
|
||||||
if (!fileName.isEmpty())
|
editorManager()->openEditor(file.first, file.second, Core::EditorManager::ModeSwitch);
|
||||||
editorManager()->openEditor(fileName, QString(), Core::EditorManager::ModeSwitch);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user