Commit 802b21c0 authored by Kai Koehne's avatar Kai Koehne

Save/restore settings of navigation widgets more aggressively

Save/restore the current settings of a navigation widget per position,
every time something changes in the navigation bar setup. Previously,
only settings on exit were stored / restored on startup, which means
that e.g. when you switch from the Outline to the Project Explorer and
back, the settings of the Outline were lost.

Reviewed-by: con
parent b687272a
......@@ -67,6 +67,7 @@
#include <coreplugin/findplaceholder.h>
#include <coreplugin/icorelistener.h>
#include <coreplugin/inavigationwidgetfactory.h>
#include <coreplugin/settingsdatabase.h>
#include <utils/pathchooser.h>
#include <utils/stylehelper.h>
......@@ -332,6 +333,9 @@ void MainWindow::extensionsInitialized()
m_actionManager->initialize();
m_vcsManager->extensionsInitialized();
m_navigationWidget->setFactories(ExtensionSystem::PluginManager::instance()->getObjects<INavigationWidgetFactory>());
readSettings();
updateContext();
......
......@@ -49,6 +49,7 @@
#include <QtGui/QResizeEvent>
#include <QtGui/QToolButton>
#include <QtGui/QShortcut>
#include <QtGui/QStandardItemModel>
Q_DECLARE_METATYPE(Core::INavigationWidgetFactory *)
......@@ -135,17 +136,15 @@ void NavigationWidgetPlaceHolder::currentModeAboutToChange(Core::IMode *mode)
NavigationWidget *NavigationWidget::m_instance = 0;
NavigationWidget::NavigationWidget(QAction *toggleSideBarAction)
: m_shown(true),
m_suppressed(false),
m_width(0),
m_toggleSideBarAction(toggleSideBarAction)
NavigationWidget::NavigationWidget(QAction *toggleSideBarAction) :
m_factoryModel(new QStandardItemModel),
m_shown(true),
m_suppressed(false),
m_width(0),
m_toggleSideBarAction(toggleSideBarAction)
{
connect(ExtensionSystem::PluginManager::instance(), SIGNAL(objectAdded(QObject*)),
this, SLOT(objectAdded(QObject*)));
setOrientation(Qt::Vertical);
insertSubItem(0);
insertSubItem(0, -1); // we don't have any entry to show yet
m_instance = this;
}
......@@ -159,11 +158,42 @@ NavigationWidget *NavigationWidget::instance()
return m_instance;
}
void NavigationWidget::setFactories(const QList<INavigationWidgetFactory *> factories)
{
ICore *core = ICore::instance();
ActionManager *am = core->actionManager();
Context navicontext(Core::Constants::C_NAVIGATION_PANE);
foreach (INavigationWidgetFactory *factory, factories) {
const QString id = factory->id();
QShortcut *shortcut = new QShortcut(this);
shortcut->setWhatsThis(tr("Activate %1 Pane").arg(factory->displayName()));
connect(shortcut, SIGNAL(activated()), this, SLOT(activateSubWidget()));
m_shortcutMap.insert(shortcut, id);
Core::Command *cmd = am->registerShortcut(shortcut,
QLatin1String("QtCreator.Sidebar.") + id, navicontext);
cmd->setDefaultKeySequence(factory->activationSequence());
m_commandMap.insert(id, cmd);
QStandardItem *newRow = new QStandardItem(factory->displayName());
newRow->setData(qVariantFromValue(factory), FactoryObjectRole);
newRow->setData(factory->id(), FactoryIdRole);
m_factoryModel->appendRow(newRow);
}
}
int NavigationWidget::storedWidth()
{
return m_width;
}
QAbstractItemModel *NavigationWidget::factoryModel() const
{
return m_factoryModel;
}
void NavigationWidget::updateToggleText()
{
if (isShown())
......@@ -186,13 +216,18 @@ void NavigationWidget::resizeEvent(QResizeEvent *re)
MiniSplitter::resizeEvent(re);
}
NavigationSubWidget *NavigationWidget::insertSubItem(int position)
NavigationSubWidget *NavigationWidget::insertSubItem(int position,int index)
{
NavigationSubWidget *nsw = new NavigationSubWidget(this);
for (int pos = position + 1; pos < m_subWidgets.size(); ++pos) {
m_subWidgets.at(pos)->setPosition(pos + 1);
}
NavigationSubWidget *nsw = new NavigationSubWidget(this, position, index);
connect(nsw, SIGNAL(splitMe()), this, SLOT(splitSubWidget()));
connect(nsw, SIGNAL(closeMe()), this, SLOT(closeSubWidget()));
insertWidget(position, nsw);
m_subWidgets.insert(position, nsw);
return nsw;
}
......@@ -202,28 +237,32 @@ void NavigationWidget::activateSubWidget()
QShortcut *original = qobject_cast<QShortcut *>(sender());
QString id = m_shortcutMap[original];
foreach (NavigationSubWidget *subWidget, m_subWidgets)
foreach (NavigationSubWidget *subWidget, m_subWidgets) {
if (subWidget->factory()->id() == id) {
subWidget->setFocusWidget();
return;
}
}
m_subWidgets.first()->setFactory(id);
m_subWidgets.first()->setFocusWidget();
int index = factoryIndex(id);
if (index >= 0) {
m_subWidgets.first()->setFactoryIndex(index);
m_subWidgets.first()->setFocusWidget();
}
}
void NavigationWidget::splitSubWidget()
{
NavigationSubWidget *original = qobject_cast<NavigationSubWidget *>(sender());
int pos = indexOf(original) + 1;
NavigationSubWidget *newnsw = insertSubItem(pos);
newnsw->setFactory(original->factory());
insertSubItem(pos, original->factoryIndex());
}
void NavigationWidget::closeSubWidget()
{
if (m_subWidgets.count() != 1) {
NavigationSubWidget *subWidget = qobject_cast<NavigationSubWidget *>(sender());
subWidget->saveSettings();
m_subWidgets.removeOne(subWidget);
subWidget->hide();
subWidget->deleteLater();
......@@ -232,46 +271,44 @@ void NavigationWidget::closeSubWidget()
}
}
void NavigationWidget::saveSettings(QSettings *settings)
{
QStringList views;
QStringList viewIds;
for (int i=0; i<m_subWidgets.count(); ++i) {
views.append(m_subWidgets.at(i)->factory()->id());
m_subWidgets.at(i)->saveSettings();
viewIds.append(m_subWidgets.at(i)->factory()->id());
}
settings->setValue("Navigation/Views", views);
settings->setValue("Navigation/Views", viewIds);
settings->setValue("Navigation/Visible", isShown());
settings->setValue("Navigation/VerticalPosition", saveState());
settings->setValue("Navigation/Width", m_width);
for (int i=0; i<m_subWidgets.count(); ++i)
m_subWidgets.at(i)->saveSettings(i);
}
void NavigationWidget::restoreSettings(QSettings *settings)
{
int version = settings->value("Navigation/Version", 1).toInt();
QStringList views = settings->value("Navigation/Views").toStringList();
QStringList viewIds = settings->value("Navigation/Views").toStringList();
bool restoreSplitterState = true;
if (version == 1) {
if (views.isEmpty())
views += "Projects";
if (!views.contains("Open Documents")) {
views += "Open Documents";
if (viewIds.isEmpty())
viewIds += "Projects";
if (!viewIds.contains("Open Documents")) {
viewIds += "Open Documents";
restoreSplitterState = false;
}
settings->setValue("Navigation/Version", 2);
}
for (int i=0; i<views.count()-1; ++i) {
insertSubItem(0);
}
for (int i=0; i<views.count(); ++i) {
const QString &view = views.at(i);
NavigationSubWidget *nsw = m_subWidgets.at(i);
nsw->setFactory(view);
for (int i=0; i<viewIds.count(); ++i) {
const QString &view = viewIds.at(i);
int index = factoryIndex(view);
if (i >= m_subWidgets.size()) {
insertSubItem(i, index);
} else {
m_subWidgets.at(i)->setFactoryIndex(index);
}
}
if (settings->contains("Navigation/Visible")) {
......@@ -285,7 +322,7 @@ void NavigationWidget::restoreSettings(QSettings *settings)
} else {
QList<int> sizes;
sizes += 256;
for (int i = views.size()-1; i > 0; --i)
for (int i = viewIds.size()-1; i > 0; --i)
sizes.prepend(512);
setSizes(sizes);
}
......@@ -301,9 +338,6 @@ void NavigationWidget::restoreSettings(QSettings *settings)
if (NavigationWidgetPlaceHolder::m_current) {
NavigationWidgetPlaceHolder::m_current->applyStoredSize(m_width);
}
for (int i=0; i<m_subWidgets.count(); ++i)
m_subWidgets.at(i)->restoreSettings(i);
}
void NavigationWidget::setShown(bool b)
......@@ -339,26 +373,14 @@ void NavigationWidget::setSuppressed(bool b)
NavigationWidgetPlaceHolder::m_current->setVisible(m_shown && !m_suppressed);
}
void NavigationWidget::objectAdded(QObject * obj)
int NavigationWidget::factoryIndex(const QString &id)
{
INavigationWidgetFactory *factory = Aggregation::query<INavigationWidgetFactory>(obj);
if (!factory)
return;
ICore *core = ICore::instance();
ActionManager *am = core->actionManager();
Context navicontext(Core::Constants::C_NAVIGATION_PANE);
QString id = factory->id();
QShortcut *shortcut = new QShortcut(this);
shortcut->setWhatsThis(tr("Activate %1 Pane").arg(factory->displayName()));
Core::Command *cmd = am->registerShortcut(shortcut,
QLatin1String("QtCreator.Sidebar.") + id, navicontext);
cmd->setDefaultKeySequence(factory->activationSequence());
connect(shortcut, SIGNAL(activated()), this, SLOT(activateSubWidget()));
m_shortcutMap.insert(shortcut, id);
m_commandMap.insert(id, cmd);
for (int row = 0; row < m_factoryModel->rowCount(); ++row) {
if (m_factoryModel->data(m_factoryModel->index(row, 0), FactoryIdRole).toString() == id) {
return row;
}
}
return -1;
}
////
......@@ -366,18 +388,16 @@ void NavigationWidget::objectAdded(QObject * obj)
////
NavigationSubWidget::NavigationSubWidget(NavigationWidget *parentWidget)
: m_parentWidget(parentWidget)
NavigationSubWidget::NavigationSubWidget(NavigationWidget *parentWidget, int position, int factoryIndex)
: m_parentWidget(parentWidget),
m_position(position),
m_currentIndex(-1)
{
connect(ExtensionSystem::PluginManager::instance(), SIGNAL(objectAdded(QObject*)),
this, SLOT(objectAdded(QObject*)));
connect(ExtensionSystem::PluginManager::instance(), SIGNAL(aboutToRemoveObject(QObject*)),
this, SLOT(aboutToRemoveObject(QObject*)));
m_navigationComboBox = new NavComboBox(this);
m_navigationComboBox->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
m_navigationComboBox->setFocusPolicy(Qt::TabFocus);
m_navigationComboBox->setMinimumContentsLength(0);
m_navigationComboBox->setModel(parentWidget->factoryModel());
m_navigationWidget = 0;
m_toolBar = new Utils::StyledBar(this);
......@@ -405,33 +425,41 @@ NavigationSubWidget::NavigationSubWidget(NavigationWidget *parentWidget)
connect(splitAction, SIGNAL(clicked()), this, SIGNAL(splitMe()));
connect(close, SIGNAL(clicked()), this, SIGNAL(closeMe()));
setFactoryIndex(factoryIndex);
connect(m_navigationComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(setCurrentIndex(int)));
this, SLOT(comboBoxIndexChanged(int)));
foreach (INavigationWidgetFactory *factory, ExtensionSystem::PluginManager::instance()->getObjects<INavigationWidgetFactory>()) {
QVariant v;
v.setValue(factory);
m_navigationComboBox->addItem(factory->displayName(), v);
}
comboBoxIndexChanged(factoryIndex);
}
NavigationSubWidget::~NavigationSubWidget()
{
}
void NavigationSubWidget::setCurrentIndex(int index)
void NavigationSubWidget::comboBoxIndexChanged(int factoryIndex)
{
if (m_currentIndex == factoryIndex)
return;
saveSettings();
m_currentIndex = factoryIndex;
// Remove toolbutton
foreach (QWidget *w, m_additionalToolBarWidgets)
delete w;
// Remove old Widget
delete m_navigationWidget;
if (index == -1)
if (factoryIndex == -1)
return;
// Get new stuff
INavigationWidgetFactory *factory = m_navigationComboBox->itemData(index).value<INavigationWidgetFactory *>();
INavigationWidgetFactory *factory
= m_navigationComboBox->itemData(factoryIndex,
NavigationWidget::FactoryObjectRole).value<INavigationWidgetFactory *>();
NavigationView n = factory->createWidget();
m_navigationWidget = n.widget;
layout()->addWidget(m_navigationWidget);
......@@ -442,6 +470,8 @@ void NavigationSubWidget::setCurrentIndex(int index)
foreach (QToolButton *w, m_additionalToolBarWidgets) {
layout->insertWidget(layout->count()-2, w);
}
restoreSettings();
}
void NavigationSubWidget::setFocusWidget()
......@@ -450,78 +480,56 @@ void NavigationSubWidget::setFocusWidget()
m_navigationWidget->setFocus();
}
void NavigationSubWidget::objectAdded(QObject * obj)
INavigationWidgetFactory *NavigationSubWidget::factory()
{
INavigationWidgetFactory *factory = Aggregation::query<INavigationWidgetFactory>(obj);
if (!factory)
return;
QVariant v;
v.setValue(factory);
m_navigationComboBox->addItem(factory->displayName(), v);
//qDebug()<<"added factory :"<<factory<<m_navigationComboBox->findData(v);
if (m_currentIndex == -1)
return 0;
return m_navigationComboBox->itemData(m_currentIndex,
NavigationWidget::FactoryObjectRole).value<INavigationWidgetFactory *>();
}
void NavigationSubWidget::aboutToRemoveObject(QObject *obj)
void NavigationSubWidget::saveSettings()
{
INavigationWidgetFactory *factory = Aggregation::query<INavigationWidgetFactory>(obj);
if (!factory)
if (!m_navigationWidget || !factory())
return;
QVariant v;
v.setValue(factory);
int index = m_navigationComboBox->findData(v);
if (index == -1) {
qDebug()<<"factory not found not removing" << factory;
return;
}
m_navigationComboBox->removeItem(index);
factory()->saveSettings(position(), m_navigationWidget);
}
void NavigationSubWidget::setFactory(INavigationWidgetFactory *factory)
void NavigationSubWidget::restoreSettings()
{
QVariant v;
v.setValue(factory);
int index = m_navigationComboBox->findData(v);
if (index == -1)
if (!m_navigationWidget || !factory())
return;
m_navigationComboBox->setCurrentIndex(index);
factory()->restoreSettings(position(), m_navigationWidget);
}
void NavigationSubWidget::setFactory(const QString &id)
Core::Command *NavigationSubWidget::command(const QString &title) const
{
for (int i = 0; i < m_navigationComboBox->count(); ++i) {
INavigationWidgetFactory *factory =
m_navigationComboBox->itemData(i).value<INavigationWidgetFactory *>();
if (factory->id() == id)
m_navigationComboBox->setCurrentIndex(i);
}
const QHash<QString, Core::Command*> commandMap = m_parentWidget->commandMap();
QHash<QString, Core::Command*>::const_iterator r = commandMap.find(title);
if (r != commandMap.end())
return r.value();
return 0;
}
INavigationWidgetFactory *NavigationSubWidget::factory()
int NavigationSubWidget::factoryIndex() const
{
int index = m_navigationComboBox->currentIndex();
if (index == -1)
return 0;
return m_navigationComboBox->itemData(index).value<INavigationWidgetFactory *>();
return m_currentIndex;
}
void NavigationSubWidget::saveSettings(int position)
void NavigationSubWidget::setFactoryIndex(int i)
{
factory()->saveSettings(position, m_navigationWidget);
m_navigationComboBox->setCurrentIndex(i);
}
void NavigationSubWidget::restoreSettings(int position)
int NavigationSubWidget::position() const
{
factory()->restoreSettings(position, m_navigationWidget);
return m_position;
}
Core::Command *NavigationSubWidget::command(const QString &title) const
void NavigationSubWidget::setPosition(int position)
{
const QHash<QString, Core::Command*> commandMap = m_parentWidget->commandMap();
QHash<QString, Core::Command*>::const_iterator r = commandMap.find(title);
if (r != commandMap.end())
return r.value();
return 0;
m_position = position;
}
NavComboBox::NavComboBox(NavigationSubWidget *navSubWidget)
......
......@@ -38,6 +38,7 @@ QT_BEGIN_NAMESPACE
class QSettings;
class QShortcut;
class QToolButton;
class QStandardItemModel;
QT_END_NAMESPACE
namespace Utils {
......@@ -77,9 +78,17 @@ class NavigationWidget : public MiniSplitter
{
Q_OBJECT
public:
enum FactoryModelRoles {
FactoryObjectRole = Qt::UserRole,
FactoryIdRole
};
NavigationWidget(QAction *toggleSideBarAction);
~NavigationWidget();
void setFactories(const QList<INavigationWidgetFactory*> factories);
void saveSettings(QSettings *settings);
void restoreSettings(QSettings *settings);
......@@ -97,21 +106,26 @@ public:
void placeHolderChanged(NavigationWidgetPlaceHolder *holder);
QHash<QString, Core::Command*> commandMap() const { return m_commandMap; }
QAbstractItemModel *factoryModel() const;
protected:
void resizeEvent(QResizeEvent *);
private slots:
void objectAdded(QObject*);
void activateSubWidget();
void splitSubWidget();
void closeSubWidget();
private:
void updateToggleText();
NavigationSubWidget *insertSubItem(int position);
NavigationSubWidget *insertSubItem(int position, int index);
int factoryIndex(const QString &id);
QList<NavigationSubWidget *> m_subWidgets;
QHash<QShortcut *, QString> m_shortcutMap;
QHash<QString, Core::Command*> m_commandMap;
QStandardItemModel *m_factoryModel;
bool m_shown;
bool m_suppressed;
int m_width;
......@@ -123,16 +137,21 @@ class NavigationSubWidget : public QWidget
{
Q_OBJECT
public:
NavigationSubWidget(NavigationWidget *parentWidget);
NavigationSubWidget(NavigationWidget *parentWidget, int position, int index);
virtual ~NavigationSubWidget();
INavigationWidgetFactory *factory();
void setFactory(INavigationWidgetFactory *factory);
void setFactory(const QString &id);
int factoryIndex() const;
void setFactoryIndex(int i);
void setFocusWidget();
void saveSettings(int position);
void restoreSettings(int position);
int position() const;
void setPosition(int i);
void saveSettings();
void restoreSettings();
Core::Command *command(const QString &title) const;
......@@ -141,9 +160,7 @@ signals:
void closeMe();
private slots:
void objectAdded(QObject*);
void aboutToRemoveObject(QObject*);
void setCurrentIndex(int);
void comboBoxIndexChanged(int);
private:
NavigationWidget *m_parentWidget;
......@@ -151,6 +168,8 @@ private:
QWidget *m_navigationWidget;
Utils::StyledBar *m_toolBar;
QList<QToolButton *> m_additionalToolBarWidgets;
int m_position;
int m_currentIndex;
};
class NavComboBox : public QComboBox
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment