Commit 44bafd32 authored by dt's avatar dt
Browse files

Merge branch 'master' of git@scm.dev.nokia.troll.no:creator/mainline

parents abeb4504 21e05c62
......@@ -132,13 +132,9 @@ EditorManagerPlaceHolder* EditorManagerPlaceHolder::current()
// ---------------- EditorManager
namespace Core {
struct EditorManagerPrivate {
struct EditLocation {
QPointer<IEditor> editor;
QString fileName;
QString kind;
QVariant state;
};
explicit EditorManagerPrivate(ICore *core, QWidget *parent);
~EditorManagerPrivate();
Internal::EditorView *m_view;
......@@ -167,13 +163,6 @@ struct EditorManagerPrivate {
QAction *m_removeAllSplitsAction;
QAction *m_gotoOtherSplitAction;
QList<IEditor *> m_editorHistory;
QList<EditLocation *> m_navigationHistory;
void clearNavigationHistory() {
qDeleteAll(m_navigationHistory);
m_navigationHistory.clear();
}
int currentNavigationHistoryPosition;
Internal::OpenEditorsWindow *m_windowPopup;
Core::BaseView *m_openEditorsView;
Internal::EditorClosingCoreListener *m_coreListener;
......@@ -201,12 +190,11 @@ EditorManagerPrivate::EditorManagerPrivate(ICore *core, QWidget *parent) :
m_closeCurrentEditorAction(new QAction(EditorManager::tr("Close"), parent)),
m_closeAllEditorsAction(new QAction(EditorManager::tr("Close All"), parent)),
m_closeOtherEditorsAction(new QAction(EditorManager::tr("Close Others"), parent)),
m_gotoNextDocHistoryAction(new QAction(EditorManager::tr("Next Document in History"), parent)),
m_gotoPreviousDocHistoryAction(new QAction(EditorManager::tr("Previous Document in History"), parent)),
m_goBackAction(new QAction(EditorManager::tr("Go Back"), parent)),
m_goForwardAction(new QAction(EditorManager::tr("Go Forward"), parent)),
m_gotoNextDocHistoryAction(new QAction(EditorManager::tr("Next Open Document in History"), parent)),
m_gotoPreviousDocHistoryAction(new QAction(EditorManager::tr("Previous Open Document in History"), parent)),
m_goBackAction(new QAction(QIcon(QLatin1String(":/help/images/previous.png")), EditorManager::tr("Go Back"), parent)),
m_goForwardAction(new QAction(QIcon(QLatin1String(":/help/images/next.png")), EditorManager::tr("Go Forward"), parent)),
m_openInExternalEditorAction(new QAction(EditorManager::tr("Open in External Editor"), parent)),
currentNavigationHistoryPosition(0),
m_windowPopup(0),
m_coreListener(0),
m_reloadBehavior(IFile::AskForReload)
......@@ -216,7 +204,7 @@ EditorManagerPrivate::EditorManagerPrivate(ICore *core, QWidget *parent) :
EditorManagerPrivate::~EditorManagerPrivate()
{
clearNavigationHistory();
// clearNavigationHistory();
}
EditorManager *EditorManager::m_instance = 0;
......@@ -461,15 +449,6 @@ QString EditorManager::defaultExternalEditor() const
#endif
}
void EditorManager::updateEditorHistory()
{
IEditor *editor = currentEditor();
if (!editor)
return;
m_d->m_editorHistory.removeAll(editor);
m_d->m_editorHistory.prepend(editor);
}
void EditorManager::removeEditor(IEditor *editor)
{
bool isDuplicate = m_d->m_editorModel->isDuplicate(editor);
......@@ -477,9 +456,7 @@ void EditorManager::removeEditor(IEditor *editor)
if (!isDuplicate) {
m_d->m_core->fileManager()->removeFile(editor->file());
}
m_d->m_editorHistory.removeAll(editor);
m_d->m_core->removeContextObject(editor);
}
void EditorManager::handleContextChange(Core::IContext *context)
......@@ -508,9 +485,9 @@ void EditorManager::setCurrentEditor(IEditor *editor, bool ignoreNavigationHisto
if (editor) {
if (SplitterOrView *splitterOrView = m_d->m_splitter->findView(editor))
splitterOrView->view()->setCurrentEditor(editor);
m_d->m_view->updateEditorHistory(editor); // the global view should have a complete history
}
updateActions();
updateEditorHistory();
emit currentEditorChanged(editor);
}
......@@ -539,6 +516,8 @@ Core::Internal::SplitterOrView *EditorManager::currentView() const
view = m_d->m_currentEditor?
m_d->m_splitter->findView(m_d->m_currentEditor):
m_d->m_splitter->findFirstView();
if (!view)
return m_d->m_splitter;
return view;
}
......@@ -553,6 +532,16 @@ QList<IEditor *> EditorManager::editorsForFileName(const QString &filename) cons
return found;
}
QList<IEditor *> EditorManager::editorsForFile(IFile *file) const
{
QList<IEditor *> found;
foreach (IEditor *editor, openedEditors()) {
if (editor->file() == file)
found << editor;
}
return found;
}
IEditor *EditorManager::currentEditor() const
{
return m_d->m_currentEditor;
......@@ -612,7 +601,6 @@ void EditorManager::closeView(Core::Internal::EditorView *view)
else
setCurrentView(newCurrent);
}
updateEditorHistory();
}
QList<IEditor*>
......@@ -647,7 +635,7 @@ bool EditorManager::closeAllEditors(bool askAboutModifiedEditors)
{
m_d->m_editorModel->removeAllRestoredEditors();
if (closeEditors(openedEditors(), askAboutModifiedEditors)) {
m_d->clearNavigationHistory();
// m_d->clearNavigationHistory();
return true;
}
return false;
......@@ -836,7 +824,7 @@ void EditorManager::closeDuplicate(Core::IEditor *editor)
Core::IEditor *EditorManager::pickUnusedEditor() const
{
foreach (IEditor *editor, m_d->m_editorHistory) {
foreach (IEditor *editor, openedEditors()) {
SplitterOrView *view = m_d->m_splitter->findView(editor);
if (!view || view->editor() != editor)
return editor;
......@@ -844,24 +832,25 @@ Core::IEditor *EditorManager::pickUnusedEditor() const
return 0;
}
Core::IEditor *EditorManager::activateEditor(const QModelIndex &index, Internal::EditorView *view, OpenEditorFlags flags)
{
IEditor *editor = index.data(Qt::UserRole).value<IEditor*>();
if (editor) {
return activateEditor(view, editor, flags);
}
if (view)
setCurrentView(m_d->m_splitter->findView(view));
QString fileName = index.data(Qt::UserRole + 1).toString();
QByteArray kind = index.data(Qt::UserRole + 2).toByteArray();
return openEditor(fileName, kind, flags);
return openEditor(view, fileName, kind, flags);
}
Core::IEditor *EditorManager::placeEditor(Core::Internal::EditorView *view, Core::IEditor *editor)
{
Q_ASSERT(view && editor) ;
Q_ASSERT(view && editor);
if (view->currentEditor() && view->currentEditor()->file() == editor->file())
editor = view->currentEditor();
if (!view->hasEditor(editor)) {
bool duplicateSupported = editor->duplicateSupported();
if (SplitterOrView *sourceView = m_d->m_splitter->findView(editor)) {
......@@ -914,6 +903,13 @@ Core::IEditor *EditorManager::activateEditor(Core::Internal::EditorView *view, C
return editor;
}
Core::IEditor *EditorManager::activateEditor(Core::Internal::EditorView *view, Core::IFile *file, OpenEditorFlags flags)
{
QList<IEditor*> editors = editorsForFile(file);
Q_ASSERT(!editors.isEmpty());
return activateEditor(view, editors.first(), flags);
}
/* For something that has a 'QStringList mimeTypes' (IEditorFactory
* or IExternalEditor), find the one best matching the mimetype passed in.
* Recurse over the parent classes of the mimetype to find them. */
......@@ -1034,10 +1030,6 @@ void EditorManager::addEditor(IEditor *editor, bool isDuplicate)
m_d->m_core->fileManager()->addToRecentFiles(editor->file()->fileName());
}
}
m_d->m_editorHistory.removeAll(editor);
m_d->m_editorHistory.prepend(editor);
emit editorOpened(editor);
}
......@@ -1118,6 +1110,12 @@ static QString formatFileFilters(const Core::ICore *core, QString *selectedFilte
IEditor *EditorManager::openEditor(const QString &fileName, const QString &editorKind,
EditorManager::OpenEditorFlags flags)
{
return openEditor(0, fileName, editorKind, flags);
}
IEditor *EditorManager::openEditor(Core::Internal::EditorView *view, const QString &fileName,
const QString &editorKind, EditorManager::OpenEditorFlags flags)
{
if (debugEditorManager)
qDebug() << Q_FUNC_INFO << fileName << editorKind;
......@@ -1127,7 +1125,7 @@ IEditor *EditorManager::openEditor(const QString &fileName, const QString &edito
const QList<IEditor *> editors = editorsForFileName(fileName);
if (!editors.isEmpty()) {
return activateEditor(editors.first(), flags);
return activateEditor(view, editors.first(), flags);
}
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
IEditor *editor = createEditor(editorKind, fileName);
......@@ -1142,7 +1140,7 @@ IEditor *EditorManager::openEditor(const QString &fileName, const QString &edito
restoreEditorState(editor);
QApplication::restoreOverrideCursor();
return activateEditor(editor, flags);
return activateEditor(view, editor, flags);
}
bool EditorManager::openExternalEditor(const QString &fileName, const QString &editorKind)
......@@ -1407,7 +1405,8 @@ void EditorManager::gotoNextDocHistory()
if (dialog->isVisible()) {
dialog->selectNextEditor();
} else {
dialog->setEditors(m_d->m_editorHistory, m_d->m_currentEditor, m_d->m_editorModel);
EditorView *view = currentView()->view();
dialog->setEditors(m_d->m_view, view, m_d->m_editorModel);
dialog->selectNextEditor();
showWindowPopup();
}
......@@ -1419,7 +1418,8 @@ void EditorManager::gotoPreviousDocHistory()
if (dialog->isVisible()) {
dialog->selectPreviousEditor();
} else {
dialog->setEditors(m_d->m_editorHistory, m_d->m_currentEditor, m_d->m_editorModel);
EditorView *view = currentView()->view();
dialog->setEditors(m_d->m_view, view, m_d->m_editorModel);
dialog->selectPreviousEditor();
showWindowPopup();
}
......@@ -1474,10 +1474,11 @@ void EditorManager::updateActions()
m_d->m_closeOtherEditorsAction->setEnabled(openedCount > 1);
m_d->m_closeOtherEditorsAction->setText((openedCount > 1 ? tr("Close All Except %1").arg(quotedName) : tr("Close Others")));
m_d->m_gotoNextDocHistoryAction->setEnabled(m_d->m_editorHistory.count() > 0);
m_d->m_gotoPreviousDocHistoryAction->setEnabled(m_d->m_editorHistory.count() > 0);
m_d->m_goBackAction->setEnabled(m_d->currentNavigationHistoryPosition > 0);
m_d->m_goForwardAction->setEnabled(m_d->currentNavigationHistoryPosition < m_d->m_navigationHistory.size()-1);
m_d->m_gotoNextDocHistoryAction->setEnabled(m_d->m_editorModel->rowCount() != 0);
m_d->m_gotoPreviousDocHistoryAction->setEnabled(m_d->m_editorModel->rowCount() != 0);
EditorView *view = currentEditorView();
m_d->m_goBackAction->setEnabled(view->canGoBack());
m_d->m_goForwardAction->setEnabled(view->canGoForward());
bool hasSplitter = m_d->m_splitter->isSplitter();
m_d->m_removeCurrentSplitAction->setEnabled(hasSplitter);
......@@ -1497,121 +1498,23 @@ OpenEditorsModel *EditorManager::openedEditorsModel() const
return m_d->m_editorModel;
}
QList<IEditor*> EditorManager::editorHistory() const
{
return m_d->m_editorHistory;
}
void EditorManager::addCurrentPositionToNavigationHistory(const QByteArray &saveState, bool compress)
void EditorManager::addCurrentPositionToNavigationHistory(IEditor *editor, const QByteArray &saveState)
{
IEditor *editor = currentEditor();
if (!editor)
return;
if (!editor->file())
return;
QString fileName = editor->file()->fileName();
QByteArray state;
if (saveState.isNull()) {
state = editor->saveState();
} else {
state = saveState;
}
// cut existing
int firstIndexToRemove;
if (compress && m_d->currentNavigationHistoryPosition > 0) {
EditorManagerPrivate::EditLocation *previousLocation =
m_d->m_navigationHistory.at(m_d->currentNavigationHistoryPosition-1);
if ((previousLocation->editor && editor == previousLocation->editor)
|| (!fileName.isEmpty() && previousLocation->fileName == fileName)) {
firstIndexToRemove = m_d->currentNavigationHistoryPosition-1;
} else {
firstIndexToRemove = m_d->currentNavigationHistoryPosition;
}
} else {
firstIndexToRemove = m_d->currentNavigationHistoryPosition;
}
if (firstIndexToRemove >= 0) {
for (int i = m_d->m_navigationHistory.size()-1; i >= firstIndexToRemove; --i) {
delete m_d->m_navigationHistory.takeLast();
}
}
while (m_d->m_navigationHistory.size() >= 30) {
delete m_d->m_navigationHistory.takeFirst();
}
EditorManagerPrivate::EditLocation *location = new EditorManagerPrivate::EditLocation;
location->editor = editor;
location->fileName = editor->file()->fileName();
location->kind = editor->kind();
location->state = QVariant(state);
m_d->m_navigationHistory.append(location);
m_d->currentNavigationHistoryPosition = m_d->m_navigationHistory.size();
currentView()->view()->addCurrentPositionToNavigationHistory(editor, saveState);
updateActions();
}
void EditorManager::updateCurrentPositionInNavigationHistory()
{
if (!m_d->m_currentEditor || !m_d->m_currentEditor->file())
return;
EditorManagerPrivate::EditLocation *location;
if (m_d->currentNavigationHistoryPosition < m_d->m_navigationHistory.size()) {
location = m_d->m_navigationHistory[m_d->currentNavigationHistoryPosition];
} else {
location = new EditorManagerPrivate::EditLocation;
m_d->m_navigationHistory.append(location);
}
location->editor = m_d->m_currentEditor;
location->fileName = m_d->m_currentEditor->file()->fileName();
location->kind = m_d->m_currentEditor->kind();
location->state = QVariant(m_d->m_currentEditor->saveState());
}
void EditorManager::goBackInNavigationHistory()
{
updateCurrentPositionInNavigationHistory();
while (m_d->currentNavigationHistoryPosition > 0) {
--m_d->currentNavigationHistoryPosition;
EditorManagerPrivate::EditLocation *location = m_d->m_navigationHistory.at(m_d->currentNavigationHistoryPosition);
IEditor *editor;
if (location->editor) {
editor = location->editor;
activateEditor(location->editor, IgnoreNavigationHistory);
} else {
editor = openEditor(location->fileName, location->kind, IgnoreNavigationHistory);
if (!editor) {
delete m_d->m_navigationHistory.takeAt(m_d->currentNavigationHistoryPosition);
continue;
}
}
editor->restoreState(location->state.toByteArray());
updateActions();
ensureEditorManagerVisible();
return;
}
currentView()->view()->goBackInNavigationHistory();
updateActions();
ensureEditorManagerVisible();
return;
}
void EditorManager::goForwardInNavigationHistory()
{
updateCurrentPositionInNavigationHistory();
if (m_d->currentNavigationHistoryPosition >= m_d->m_navigationHistory.size()-1)
return;
++m_d->currentNavigationHistoryPosition;
EditorManagerPrivate::EditLocation *location = m_d->m_navigationHistory.at(m_d->currentNavigationHistoryPosition);
IEditor *editor;
if (location->editor) {
editor = location->editor;
activateEditor(location->editor, IgnoreNavigationHistory);
} else {
editor = openEditor(location->fileName, location->kind, IgnoreNavigationHistory);
if (!editor) {
//TODO
qDebug() << Q_FUNC_INFO << "can't open file" << location->fileName;
return;
}
}
editor->restoreState(location->state.toByteArray());
currentView()->view()->goForwardInNavigationHistory();
updateActions();
ensureEditorManagerVisible();
}
......@@ -1951,7 +1854,6 @@ void EditorManager::split(Qt::Orientation orientation)
: m_d->m_splitter->findFirstView();
if (view && !view->splitter()) {
view->split(orientation);
updateEditorHistory();
}
updateActions();
}
......@@ -2006,7 +1908,7 @@ void EditorManager::gotoOtherSplit()
view = m_d->m_splitter->findFirstView();
if (view) {
if (IEditor *editor = view->editor()) {
setCurrentEditor(editor);
setCurrentEditor(editor, true);
editor->widget()->setFocus();
} else {
setCurrentView(view);
......
......@@ -36,6 +36,7 @@
#include <QtGui/QWidget>
#include <QtCore/QList>
#include <QtCore/QPointer>
QT_BEGIN_NAMESPACE
class QSettings;
......@@ -72,6 +73,8 @@ class SplitterOrView;
class EditorClosingCoreListener;
class OpenEditorsViewFactory;
} // namespace Internal
class CORE_EXPORT EditorManagerPlaceHolder : public QWidget
......@@ -122,21 +125,22 @@ public:
const QString &contents = QString());
bool hasEditor(const QString &fileName) const;
QList<IEditor *> editorsForFileName(const QString &filename) const;
QList<IEditor *> editorsForFile(IFile *file) const;
IEditor *currentEditor() const;
IEditor *activateEditor(IEditor *editor, OpenEditorFlags flags = 0);
IEditor *activateEditor(const QModelIndex &index, Internal::EditorView *view = 0, OpenEditorFlags = 0);
IEditor *activateEditor(Core::Internal::EditorView *view, Core::IFile*file, OpenEditorFlags flags = 0);
QList<IEditor*> openedEditors() const;
OpenEditorsModel *openedEditorsModel() const;
IEditor *activateEditor(const QModelIndex &index, Internal::EditorView *view = 0, OpenEditorFlags = 0);
void closeEditor(const QModelIndex &index);
void closeOtherEditors(IEditor *editor);
QList<IEditor*> editorsForFiles(QList<IFile*> files) const;
//QList<EditorGroup *> editorGroups() const;
QList<IEditor*> editorHistory() const;
void addCurrentPositionToNavigationHistory(const QByteArray &saveState = QByteArray(), bool compress = false);
void addCurrentPositionToNavigationHistory(IEditor *editor = 0, const QByteArray &saveState = QByteArray());
bool saveEditor(IEditor *editor);
......@@ -209,7 +213,6 @@ private slots:
void gotoNextDocHistory();
void gotoPreviousDocHistory();
void handleContextChange(Core::IContext *context);
void updateEditorHistory();
void updateActions();
void revertToSaved();
void makeCurrentEditorWritable();
......@@ -238,7 +241,11 @@ private:
void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false);
void setCurrentView(Core::Internal::SplitterOrView *view);
IEditor *activateEditor(Core::Internal::EditorView *view, Core::IEditor *editor, OpenEditorFlags flags = 0);
IEditor *openEditor(Core::Internal::EditorView *view, const QString &fileName,
const QString &editorKind = QString(),
OpenEditorFlags flags = 0);
Core::Internal::SplitterOrView *currentView() const;
void closeEditor(Core::IEditor *editor);
void closeDuplicate(Core::IEditor *editor);
void closeView(Core::Internal::EditorView *view);
......@@ -246,7 +253,6 @@ private:
Core::Internal::EditorView *currentEditorView();
IEditor *pickUnusedEditor() const;
void updateCurrentPositionInNavigationHistory();
static EditorManager *m_instance;
EditorManagerPrivate *m_d;
......
......@@ -78,7 +78,8 @@ EditorView::EditorView(OpenEditorsModel *model, QWidget *parent) :
m_infoWidget(new QFrame(this)),
m_editorForInfoWidget(0),
m_statusHLine(new QFrame(this)),
m_statusWidget(new QFrame(this))
m_statusWidget(new QFrame(this)),
m_currentNavigationHistoryPosition(0)
{
QVBoxLayout *tl = new QVBoxLayout(this);
tl->setSpacing(0);
......@@ -322,6 +323,7 @@ void EditorView::setCurrentEditor(IEditor *editor)
m_editorList->setCurrentIndex(m_model->indexOf(editor).row());
updateEditorStatus(editor);
updateToolBar(editor);
updateEditorHistory(editor);
// FIXME: this keeps the editor hidden if switching from A to B and back
if (editor != m_editorForInfoWidget) {
......@@ -408,10 +410,155 @@ void EditorView::listContextMenu(QPoint pos)
QMenu menu;
menu.addAction(tr("Copy full path to clipboard"));
if (menu.exec(m_editorList->mapToGlobal(pos))) {
QApplication::clipboard()->setText(fileName);
QApplication::clipboard()->setText(QDir::toNativeSeparators(fileName));
}
}
void EditorView::updateEditorHistory(IEditor *editor)
{
if (!editor)
return;
IFile *file = editor->file();
if (!file)
return;
QString fileName = file->fileName();
QByteArray state = editor->saveState();
EditLocation location;
location.file = file;
location.fileName = file->fileName();
location.kind = editor->kind();
location.state = QVariant(state);
for(int i = 0; i < m_editorHistory.size(); ++i) {
if (m_editorHistory.at(i).file == 0
|| m_editorHistory.at(i).file == file
){
m_editorHistory.removeAt(i--);
continue;
}
}
m_editorHistory.prepend(location);
}
void EditorView::addCurrentPositionToNavigationHistory(IEditor *editor, const QByteArray &saveState)
{
if (editor && editor != currentEditor()) {
qDebug() << Q_FUNC_INFO << "this should not happen!";
return;
}
if (!editor)
editor = currentEditor();
if (!editor)
return;
IFile *file = editor->file();
if (!file)
return;
QString fileName = file->fileName();
QByteArray state;
if (saveState.isNull()) {
state = editor->saveState();
} else {
state = saveState;
}
EditLocation location;
location.file = file;
location.fileName = file->fileName();
location.kind = editor->kind();
location.state = QVariant(state);
m_currentNavigationHistoryPosition = qMin(m_currentNavigationHistoryPosition, m_navigationHistory.size()); // paranoia
m_navigationHistory.insert(m_currentNavigationHistoryPosition, location);
++m_currentNavigationHistoryPosition;
while (m_navigationHistory.size() >= 30) {
if (m_currentNavigationHistoryPosition > 15) {
m_navigationHistory.takeFirst();
--m_currentNavigationHistoryPosition;
} else {
m_navigationHistory.takeLast();
}
}
}
void EditorView::copyNavigationHistoryFrom(EditorView* other)
{
if (!other)
return;
m_currentNavigationHistoryPosition = other->m_currentNavigationHistoryPosition;
m_navigationHistory = other->m_navigationHistory;
m_editorHistory = other->m_editorHistory;
}
void EditorView::updateCurrentPositionInNavigationHistory()
{
IEditor *editor = currentEditor();
if (!editor || !editor->file())
return;
IFile *file = editor->file();
EditLocation *