Editor toolbar integration

This commit is contained in:
Lasse Holmstedt
2010-03-09 17:40:55 +01:00
parent 9534f6bc50
commit a6a049852b
9 changed files with 266 additions and 311 deletions

View File

@@ -43,6 +43,7 @@
#include <utils/parameteraction.h>
#include <utils/qtcassert.h>
#include <utils/styledbar.h>
#include <QtCore/QSettings>
#include <QtCore/QEvent>
@@ -69,61 +70,57 @@ namespace Core {
/*!
Mimic the look of the text editor toolbar as defined in e.g. EditorView::EditorView
*/
DesignModeToolBar::DesignModeToolBar(QWidget *parent) :
QWidget(parent),
m_editorList(new QComboBox),
m_centerToolBar(0),
m_rightToolBar(new QToolBar),
EditorToolBar::EditorToolBar(QWidget *parent) :
Utils::StyledBar(parent),
m_editorList(new QComboBox(this)),
m_rightToolBar(new QToolBar(this)),
m_closeButton(new QToolButton),
m_lockButton(new QToolButton),
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_editor(0)
m_activeToolBar(0),
m_toolBarPlaceholder(new QWidget),
m_defaultToolBar(new QWidget(this)),
m_ignoreEditorToolbar(false)
{
ICore *core = ICore::instance();
QHBoxLayout *toolBarLayout = new QHBoxLayout(this);
toolBarLayout->setMargin(0);
toolBarLayout->setSpacing(0);
toolBarLayout->addWidget(m_defaultToolBar);
m_toolBarPlaceholder->setLayout(toolBarLayout);
m_toolBarPlaceholder->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
//setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
m_editorsListModel = core->editorManager()->openedEditorsModel();
m_defaultToolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
m_activeToolBar = m_defaultToolBar;
m_editorsListModel = EditorManager::instance()->openedEditorsModel();
connect(m_goBackAction, SIGNAL(triggered()), this, SIGNAL(goBackClicked()));
connect(m_goForwardAction, SIGNAL(triggered()), this, SIGNAL(goForwardClicked()));
// copied from EditorView::EditorView
m_editorList->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_editorList->setMinimumContentsLength(20);
m_editorList->setModel(m_editorsListModel);
m_editorList->setMaxVisibleItems(40);
m_editorList->setContextMenuPolicy(Qt::CustomContextMenu);
QToolBar *editorListToolBar = new QToolBar;
editorListToolBar->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
editorListToolBar->addWidget(m_editorList);
m_lockButton->setAutoRaise(true);
m_lockButton->setProperty("type", QLatin1String("dockbutton"));
m_lockButton->setVisible(false);
m_closeButton->setAutoRaise(true);
m_closeButton->setIcon(QIcon(":/core/images/closebutton.png"));
m_closeButton->setProperty("type", QLatin1String("dockbutton"));
m_closeButton->setEnabled(false);
// ActionManager *am = core->actionManager();
// EditorManager *em = core->editorManager();
m_toolBarPlaceholder->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
// TODO back/FW buttons disabled for the time being, as the implementation would require changing editormanager.
// QToolBar *backFwToolBar = new QToolBar;
// backFwToolBar->addAction(m_goBackAction);
// backFwToolBar->addAction(m_goForwardAction);
// Command *cmd = am->registerAction(m_goBackAction, Constants::GO_BACK, editor->context());
//#ifdef Q_WS_MAC
// cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+Left")));
//#else
// cmd->setDefaultKeySequence(QKeySequence(tr("Alt+Left")));
//#endif
// connect(m_goBackAction, SIGNAL(triggered()), em, SLOT(goBackInNavigationHistory()));
// cmd = am->registerAction(m_goForwardAction, Constants::GO_FORWARD, editor->context());
//#ifdef Q_WS_MAC
// cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+Right")));
//#else
// cmd->setDefaultKeySequence(QKeySequence(tr("Alt+Right")));
//#endif
// connect(m_goForwardAction, SIGNAL(triggered()), em, SLOT(goForwardInNavigationHistory()));
m_backButton = new QToolButton(this);
m_backButton->setDefaultAction(m_goBackAction);
m_forwardButton= new QToolButton(this);
m_forwardButton->setDefaultAction(m_goForwardAction);
m_rightToolBar->setLayoutDirection(Qt::RightToLeft);
m_rightToolBar->addWidget(m_closeButton);
@@ -132,55 +129,123 @@ DesignModeToolBar::DesignModeToolBar(QWidget *parent) :
QHBoxLayout *toplayout = new QHBoxLayout(this);
toplayout->setSpacing(0);
toplayout->setMargin(0);
toplayout->setContentsMargins(0,0,0,0);
toplayout->addWidget(editorListToolBar);
toplayout->addWidget(m_backButton);
toplayout->addWidget(m_forwardButton);
toplayout->addWidget(m_editorList);
toplayout->addWidget(m_toolBarPlaceholder, 1); // Custom toolbar stretches
toplayout->addWidget(m_rightToolBar);
setLayout(toplayout);
connect(m_editorList, SIGNAL(activated(int)), this, SLOT(listSelectionActivated(int)));
connect(m_editorList, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(listContextMenu(QPoint)));
connect(m_lockButton, SIGNAL(clicked()), this, SLOT(makeEditorWritable()));
connect(m_closeButton, SIGNAL(clicked()), this, SIGNAL(closeClicked()));
connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeView()), Qt::QueuedConnection);
connect(core->editorManager(), SIGNAL(currentEditorChanged(IEditor*)), SLOT(updateEditorListSelection(IEditor*)));
EditorManager *em = EditorManager::instance();
connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)), SLOT(updateEditorListSelection(Core::IEditor*)));
ActionManager *am = ICore::instance()->actionManager();
connect(am->command(Constants::CLOSE), SIGNAL(keySequenceChanged()),
this, SLOT(updateActionShortcuts()));
connect(am->command(Constants::GO_BACK), SIGNAL(keySequenceChanged()),
this, SLOT(updateActionShortcuts()));
connect(am->command(Constants::GO_FORWARD), SIGNAL(keySequenceChanged()),
this, SLOT(updateActionShortcuts()));
updateEditorStatus();
}
void DesignModeToolBar::setCenterToolBar(QWidget *toolBar)
void EditorToolBar::removeToolbarForEditor(IEditor *editor)
{
if (toolBar) {
layout()->removeWidget(m_rightToolBar);
layout()->addWidget(toolBar);
disconnect(editor, SIGNAL(changed()), this, SLOT(checkEditorStatus()));
QWidget *toolBar = editor->toolBar();
if (toolBar != 0) {
if (m_activeToolBar == toolBar) {
m_activeToolBar = m_defaultToolBar;
m_activeToolBar->setVisible(true);
}
m_toolBarPlaceholder->layout()->removeWidget(toolBar);
toolBar->setVisible(false);
toolBar->setParent(0);
}
layout()->addWidget(m_rightToolBar);
}
void DesignModeToolBar::setEditor(IEditor *editor)
void EditorToolBar::closeView()
{
m_editor = editor;
m_editorList->setCurrentIndex(m_editorsListModel->indexOf(m_editor).row());
connect(m_editor, SIGNAL(changed()), this, SLOT(updateEditorStatus()));
if (!currentEditor())
return;
EditorManager *em = ICore::instance()->editorManager();
if (IEditor *editor = currentEditor()) {
em->closeDuplicate(editor);
}
emit closeClicked();
}
void DesignModeToolBar::updateEditorListSelection(IEditor *newSelection)
void EditorToolBar::addEditor(IEditor *editor, ToolbarCreationFlags flags)
{
connect(editor, SIGNAL(changed()), this, SLOT(checkEditorStatus()));
QWidget *toolBar = editor->toolBar();
m_ignoreEditorToolbar = flags & FlagsIgnoreIEditorToolBar;
if (toolBar && !m_ignoreEditorToolbar)
addCenterToolBar(toolBar);
updateEditorStatus(editor);
}
void EditorToolBar::addCenterToolBar(QWidget *toolBar)
{
toolBar->setVisible(false); // will be made visible in setCurrentEditor
m_toolBarPlaceholder->layout()->addWidget(toolBar);
updateToolBar(toolBar);
}
void EditorToolBar::updateToolBar(QWidget *toolBar)
{
if (!toolBar)
toolBar = m_defaultToolBar;
if (m_activeToolBar == toolBar)
return;
toolBar->setVisible(true);
m_activeToolBar->setVisible(false);
m_activeToolBar = toolBar;
}
void EditorToolBar::setCurrentEditor(IEditor *editor)
{
m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
// If we never added the toolbar from the editor, we will never change
// the editor, so there's no need to update the toolbar either.
if (!m_ignoreEditorToolbar)
updateToolBar(editor->toolBar());
updateEditorStatus(editor);
}
void EditorToolBar::updateEditorListSelection(IEditor *newSelection)
{
if (newSelection) {
m_editorList->setCurrentIndex(m_editorsListModel->indexOf(newSelection).row());
}
}
void DesignModeToolBar::listSelectionActivated(int row)
void EditorToolBar::listSelectionActivated(int row)
{
EditorManager *em = ICore::instance()->editorManager();
QAbstractItemModel *model = m_editorList->model();
const QModelIndex modelIndex = model->index(row, 0);
IEditor *editor = model->data(modelIndex, Qt::UserRole).value<IEditor*>();
if (editor) {
if (editor != em->currentEditor())
em->activateEditor(editor, EditorManager::NoModeSwitch);
} else {
//em->activateEditor(model->index(index, 0), this);
QString fileName = model->data(modelIndex, Qt::UserRole + 1).toString();
QByteArray kind = model->data(modelIndex, Qt::UserRole + 2).toByteArray();
editor = em->openEditor(fileName, kind, EditorManager::NoModeSwitch);
@@ -190,7 +255,7 @@ void DesignModeToolBar::listSelectionActivated(int row)
}
}
void DesignModeToolBar::listContextMenu(QPoint pos)
void EditorToolBar::listContextMenu(QPoint pos)
{
QModelIndex index = m_editorsListModel->index(m_editorList->currentIndex(), 0);
QString fileName = m_editorsListModel->data(index, Qt::UserRole + 1).toString();
@@ -203,31 +268,80 @@ void DesignModeToolBar::listContextMenu(QPoint pos)
}
}
void DesignModeToolBar::makeEditorWritable()
void EditorToolBar::makeEditorWritable()
{
if (m_editor)
ICore::instance()->editorManager()->makeEditorWritable(m_editor);
if (currentEditor())
ICore::instance()->editorManager()->makeEditorWritable(currentEditor());
}
void DesignModeToolBar::updateEditorStatus()
void EditorToolBar::setCanGoBack(bool canGoBack)
{
if (!m_editor || !m_editor->file())
return;
m_goBackAction->setEnabled(canGoBack);
}
if (m_editor->file()->isReadOnly()) {
m_lockButton->setIcon(m_editorsListModel->lockedIcon());
m_lockButton->setEnabled(!m_editor->file()->fileName().isEmpty());
void EditorToolBar::setCanGoForward(bool canGoForward)
{
m_goForwardAction->setEnabled(canGoForward);
}
void EditorToolBar::updateActionShortcuts()
{
ActionManager *am = ICore::instance()->actionManager();
m_closeButton->setToolTip(am->command(Constants::CLOSE)->stringWithAppendedShortcut(EditorManager::tr("Close")));
m_goBackAction->setToolTip(am->command(Constants::GO_BACK)->action()->toolTip());
m_goForwardAction->setToolTip(am->command(Constants::GO_FORWARD)->action()->toolTip());
}
IEditor *EditorToolBar::currentEditor() const
{
return ICore::instance()->editorManager()->currentEditor();
}
void EditorToolBar::checkEditorStatus()
{
IEditor *editor = qobject_cast<IEditor *>(sender());
IEditor *current = currentEditor();
if (current == editor)
updateEditorStatus(editor);
}
void EditorToolBar::updateEditorStatus(IEditor *editor)
{
m_lockButton->setVisible(editor != 0);
m_closeButton->setEnabled(editor != 0);
if (!editor || !editor->file()) {
m_editorList->setToolTip(QString());
return;
}
m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
if (editor->file()->isReadOnly()) {
m_lockButton->setIcon(QIcon(m_editorsListModel->lockedIcon()));
m_lockButton->setEnabled(!editor->file()->fileName().isEmpty());
m_lockButton->setToolTip(tr("Make writable"));
} else {
m_lockButton->setIcon(m_editorsListModel->unlockedIcon());
m_lockButton->setIcon(QIcon(m_editorsListModel->unlockedIcon()));
m_lockButton->setEnabled(false);
m_lockButton->setToolTip(tr("File is writable"));
}
m_editorList->setToolTip(
m_editor->file()->fileName().isEmpty()
? m_editor->displayName()
: QDir::toNativeSeparators(m_editor->file()->fileName())
);
if (editor == currentEditor())
m_editorList->setToolTip(
currentEditor()->file()->fileName().isEmpty()
? currentEditor()->displayName()
: QDir::toNativeSeparators(editor->file()->fileName())
);
}
void EditorToolBar::setNavigationVisible(bool isVisible)
{
m_goBackAction->setVisible(isVisible);
m_goForwardAction->setVisible(isVisible);
m_backButton->setVisible(isVisible);
m_forwardButton->setVisible(isVisible);
}
} // Core