Commit 7ba6fc88 authored by Lasse Holmstedt's avatar Lasse Holmstedt
Browse files

Fixed localization issues with SideBar

When changing the language, the sidebar items would break because
their localized window titles were used for storing the settings. Now,
id's are used.

Reviewed-by: con
parent 3fd04c23
......@@ -33,7 +33,6 @@
#include "actionmanager/actionmanager.h"
#include "actionmanager/command.h"
#include <QtCore/QDebug>
#include <QtCore/QEvent>
#include <QtCore/QSettings>
#include <QtGui/QLayout>
......@@ -55,16 +54,16 @@ SideBar::SideBar(QList<SideBarItem*> itemList,
{
setOrientation(Qt::Vertical);
foreach (SideBarItem *item, itemList) {
const QString title = item->widget()->windowTitle();
m_itemMap.insert(title, item);
m_itemMap.insert(item->id(), item);
m_availableItemIds.append(item->id());
m_availableItemTitles.append(item->title());
}
foreach (SideBarItem *item, defaultVisible) {
if (!itemList.contains(item))
continue;
m_defaultVisible.append(item->widget()->windowTitle());
m_defaultVisible.append(item->id());
}
m_availableItems = m_itemMap.keys();
}
SideBar::~SideBar()
......@@ -77,14 +76,30 @@ SideBar::~SideBar()
}
}
QStringList SideBar::availableItems() const
QString SideBar::idForTitle(const QString &title) const
{
QMapIterator<QString, QWeakPointer<SideBarItem> > iter(m_itemMap);
while(iter.hasNext()) {
iter.next();
if (iter.value().data()->title() == title)
return iter.key();
}
return QString();
}
QStringList SideBar::availableItemIds() const
{
return m_availableItemIds;
}
QStringList SideBar::availableItemTitles() const
{
return m_availableItems;
return m_availableItemTitles;
}
QStringList SideBar::unavailableItems() const
QStringList SideBar::unavailableItemIds() const
{
return m_unavailableItems;
return m_unavailableItemIds;
}
bool SideBar::closeWhenEmpty() const
......@@ -101,9 +116,10 @@ void SideBar::makeItemAvailable(SideBarItem *item)
QMap<QString, QWeakPointer<SideBarItem> >::const_iterator it = m_itemMap.constBegin();
while (it != m_itemMap.constEnd()) {
if (it.value().data() == item) {
m_availableItems.append(it.key());
m_unavailableItems.removeAll(it.key());
qSort(m_availableItems);
m_availableItemIds.append(it.key());
m_availableItemTitles.append(it.value().data()->title());
m_unavailableItemIds.removeAll(it.key());
qSort(m_availableItemTitles);
emit availableItemsChanged();
//updateWidgets();
break;
......@@ -114,40 +130,44 @@ void SideBar::makeItemAvailable(SideBarItem *item)
// sets a list of externally used, unavailable items. For example,
// another sidebar could set
void SideBar::setUnavailableItems(const QStringList &itemTitles)
void SideBar::setUnavailableItemIds(const QStringList &itemIds)
{
// re-enable previous items
foreach(const QString &title, m_unavailableItems)
m_availableItems.append(title);
foreach(const QString &id, m_unavailableItemIds) {
m_availableItemIds.append(id);
m_availableItemTitles.append(m_itemMap.value(id).data()->title());
}
m_unavailableItems.clear();
m_unavailableItemIds.clear();
foreach (const QString &title, itemTitles) {
if (!m_unavailableItems.contains(title))
m_unavailableItems.append(title);
m_availableItems.removeAll(title);
foreach (const QString &id, itemIds) {
if (!m_unavailableItemIds.contains(id))
m_unavailableItemIds.append(id);
m_availableItemIds.removeAll(id);
m_availableItemTitles.removeAll(m_itemMap.value(id).data()->title());
}
qSort(m_availableItems);
qSort(m_availableItemTitles);
updateWidgets();
}
SideBarItem *SideBar::item(const QString &title)
SideBarItem *SideBar::item(const QString &id)
{
if (m_itemMap.contains(title)) {
m_availableItems.removeAll(title);
if (m_itemMap.contains(id)) {
m_availableItemIds.removeAll(id);
m_availableItemTitles.removeAll(m_itemMap.value(id).data()->title());
if (!m_unavailableItems.contains(title))
m_unavailableItems.append(title);
if (!m_unavailableItemIds.contains(id))
m_unavailableItemIds.append(id);
emit availableItemsChanged();
return m_itemMap.value(title).data();
return m_itemMap.value(id).data();
}
return 0;
}
SideBarWidget *SideBar::insertSideBarWidget(int position, const QString &title)
SideBarWidget *SideBar::insertSideBarWidget(int position, const QString &id)
{
SideBarWidget *item = new SideBarWidget(this, title);
SideBarWidget *item = new SideBarWidget(this, id);
connect(item, SIGNAL(splitMe()), this, SLOT(splitSubWidget()));
connect(item, SIGNAL(closeMe()), this, SLOT(closeSubWidget()));
connect(item, SIGNAL(currentWidgetChanged()), this, SLOT(updateWidgets()));
......@@ -199,7 +219,8 @@ void SideBar::saveSettings(QSettings *settings, const QString &name)
QStringList views;
for (int i = 0; i < m_widgets.count(); ++i)
views.append(m_widgets.at(i)->currentItemTitle());
views.append(m_widgets.at(i)->currentItemId());
settings->setValue(prefix + "Views", views);
settings->setValue(prefix + "Visible", true);//isVisible());
settings->setValue(prefix + "VerticalPosition", saveState());
......@@ -221,14 +242,14 @@ void SideBar::readSettings(QSettings *settings, const QString &name)
if (settings->contains(prefix + "Views")) {
QStringList views = settings->value(prefix + "Views").toStringList();
if (views.count()) {
foreach (const QString &title, views)
insertSideBarWidget(m_widgets.count(), title);
foreach (const QString &id, views)
insertSideBarWidget(m_widgets.count(), id);
} else {
insertSideBarWidget(0);
}
} else {
foreach (const QString &title, m_defaultVisible)
insertSideBarWidget(m_widgets.count(), title);
foreach (const QString &id, m_defaultVisible)
insertSideBarWidget(m_widgets.count(), id);
}
if (settings->contains(prefix + "Visible"))
......@@ -247,27 +268,27 @@ void SideBar::readSettings(QSettings *settings, const QString &name)
void SideBar::activateItem(SideBarItem *item)
{
QMap<QString, QWeakPointer<SideBarItem> >::const_iterator it = m_itemMap.constBegin();
QString title;
QString id;
while (it != m_itemMap.constEnd()) {
if (it.value().data() == item) {
title = it.key();
id = it.key();
break;
}
++it;
}
if (title.isEmpty())
if (id.isEmpty())
return;
for (int i = 0; i < m_widgets.count(); ++i) {
if (m_widgets.at(i)->currentItemTitle() == title) {
if (m_widgets.at(i)->currentItemId() == id) {
item->widget()->setFocus();
return;
}
}
SideBarWidget *widget = m_widgets.first();
widget->setCurrentItem(title);
widget->setCurrentItem(id);
updateWidgets();
item->widget()->setFocus();
}
......@@ -282,7 +303,7 @@ QMap<QString, Core::Command*> SideBar::shortcutMap() const
return m_shortcutMap;
}
SideBarWidget::SideBarWidget(SideBar *sideBar, const QString &title)
SideBarWidget::SideBarWidget(SideBar *sideBar, const QString &id)
: m_currentItem(0)
, m_sideBar(sideBar)
{
......@@ -316,13 +337,16 @@ SideBarWidget::SideBarWidget(SideBar *sideBar, const QString &title)
setLayout(lay);
lay->addWidget(m_toolbar);
const QStringList lst = m_sideBar->availableItems();
QString t = title;
if (lst.count()) {
m_comboBox->addItems(lst);
QStringList titleList = m_sideBar->availableItemTitles();
qSort(titleList);
QString t = id;
if (titleList.count()) {
foreach(const QString &itemTitle, titleList)
m_comboBox->addItem(itemTitle, m_sideBar->idForTitle(itemTitle));
m_comboBox->setCurrentIndex(0);
if (t.isEmpty())
t = m_comboBox->currentText();
t = m_comboBox->itemData(0, ComboBox::IdRole).toString();
}
setCurrentItem(t);
......@@ -339,18 +363,25 @@ QString SideBarWidget::currentItemTitle() const
return m_comboBox->currentText();
}
void SideBarWidget::setCurrentItem(const QString &title)
QString SideBarWidget::currentItemId() const
{
return m_currentItem->id();
}
void SideBarWidget::setCurrentItem(const QString &id)
{
if (!title.isEmpty()) {
int idx = m_comboBox->findText(title);
if (!id.isEmpty()) {
int idx = m_comboBox->findData(QVariant(id), ComboBox::IdRole);
if (idx < 0)
idx = 0;
bool blocked = m_comboBox->blockSignals(true);
m_comboBox->setCurrentIndex(idx);
m_comboBox->blockSignals(blocked);
}
SideBarItem *item = m_sideBar->item(title);
SideBarItem *item = m_sideBar->item(id);
if (!item)
return;
removeCurrentItem();
......@@ -366,18 +397,23 @@ void SideBarWidget::setCurrentItem(const QString &title)
void SideBarWidget::updateAvailableItems()
{
bool blocked = m_comboBox->blockSignals(true);
QString current = m_comboBox->currentText();
QString currentTitle = m_comboBox->currentText();
m_comboBox->clear();
QStringList itms = m_sideBar->availableItems();
if (!current.isEmpty() && !itms.contains(current))
itms.append(current);
qSort(itms);
m_comboBox->addItems(itms);
int idx = m_comboBox->findText(current);
QStringList titleList = m_sideBar->availableItemTitles();
if (!currentTitle.isEmpty() && !titleList.contains(currentTitle))
titleList.append(currentTitle);
qSort(titleList);
foreach(const QString &itemTitle, titleList)
m_comboBox->addItem(itemTitle, m_sideBar->idForTitle(itemTitle));
int idx = m_comboBox->findText(currentTitle);
if (idx < 0)
idx = 0;
m_comboBox->setCurrentIndex(idx);
m_splitButton->setEnabled(itms.count() > 1);
m_splitButton->setEnabled(titleList.count() > 1);
m_comboBox->blockSignals(blocked);
}
......@@ -400,14 +436,15 @@ void SideBarWidget::removeCurrentItem()
void SideBarWidget::setCurrentIndex(int)
{
setCurrentItem(m_comboBox->currentText());
setCurrentItem(m_comboBox->itemData(m_comboBox->currentIndex(),
ComboBox::IdRole).toString());
emit currentWidgetChanged();
}
Core::Command *SideBarWidget::command(const QString &title) const
Core::Command *SideBarWidget::command(const QString &id) const
{
const QMap<QString, Core::Command*> shortcutMap = m_sideBar->shortcutMap();
QMap<QString, Core::Command*>::const_iterator r = shortcutMap.find(title);
QMap<QString, Core::Command*>::const_iterator r = shortcutMap.find(id);
if (r != shortcutMap.end())
return r.value();
return 0;
......
......@@ -64,13 +64,15 @@ class CORE_EXPORT SideBarItem : public QObject
{
Q_OBJECT
public:
SideBarItem(QWidget *widget)
: m_widget(widget)
// id is non-localized string of the item that's used to store the settings.
SideBarItem(QWidget *widget, const QString &id)
: m_widget(widget), m_id(id)
{}
virtual ~SideBarItem();
QWidget *widget() { return m_widget; }
QString id() const { return m_id; }
QString title() const { return m_widget->windowTitle(); }
/* Should always return a new set of tool buttons.
*
......@@ -85,6 +87,7 @@ public:
private:
QWidget *m_widget;
QString m_id;
};
class CORE_EXPORT SideBar : public MiniSplitter
......@@ -92,16 +95,20 @@ class CORE_EXPORT SideBar : public MiniSplitter
Q_OBJECT
public:
/*
* The SideBar takes ownership of the SideBarItems.
* The SideBar takes explicit ownership of the SideBarItems
* if you have one SideBar, or shared ownership in case
* of multiple SideBars.
*/
SideBar(QList< SideBarItem*> widgetList,
QList< SideBarItem*> defaultVisible);
~SideBar();
QStringList availableItems() const;
QStringList unavailableItems() const;
QStringList availableItemIds() const;
QStringList availableItemTitles() const;
QStringList unavailableItemIds() const;
void makeItemAvailable(SideBarItem *item);
void setUnavailableItems(const QStringList &itemTitles);
void setUnavailableItemIds(const QStringList &itemTitles);
QString idForTitle(const QString &itemId) const;
SideBarItem *item(const QString &title);
......@@ -131,8 +138,9 @@ private:
QList<Internal::SideBarWidget*> m_widgets;
QMap<QString, QWeakPointer<SideBarItem> > m_itemMap;
QStringList m_availableItems;
QStringList m_unavailableItems;
QStringList m_availableItemIds;
QStringList m_availableItemTitles;
QStringList m_unavailableItemIds;
QStringList m_defaultVisible;
QMap<QString, Core::Command*> m_shortcutMap;
bool m_closeWhenEmpty;
......@@ -147,13 +155,14 @@ public:
SideBarWidget(SideBar *sideBar, const QString &title);
~SideBarWidget();
QString currentItemId() const;
QString currentItemTitle() const;
void setCurrentItem(const QString &title);
void setCurrentItem(const QString &id);
void updateAvailableItems();
void removeCurrentItem();
Core::Command *command(const QString &title) const;
Core::Command *command(const QString &id) const;
signals:
void splitMe();
......@@ -179,6 +188,10 @@ class ComboBox : public QComboBox
Q_OBJECT
public:
enum DataRoles {
IdRole = Qt::UserRole
};
ComboBox(SideBarWidget *sideBarWidget);
protected:
......
......@@ -84,6 +84,12 @@ using namespace Core::Constants;
using namespace Help;
using namespace Help::Internal;
const char * const SB_INDEX = "Index";
const char * const SB_CONTENTS = "Contents";
const char * const SB_BOOKMARKS = "Bookmarks";
const char * const SB_SEARCH = "Search";
const char * const SB_OPENPAGES = "OpenPages";
#define IMAGEPATH ":/help/images/"
#if defined(Q_OS_MAC)
# define DOCPATH "/../Resources/doc/"
......@@ -373,7 +379,7 @@ void HelpPlugin::setupUi()
IndexWindow *indexWindow = new IndexWindow();
indexWindow->setWindowTitle(tr("Index"));
m_indexItem = new Core::SideBarItem(indexWindow);
m_indexItem = new Core::SideBarItem(indexWindow, QLatin1String(SB_INDEX));
connect(indexWindow, SIGNAL(linkActivated(QUrl)), m_centralWidget,
SLOT(setSource(QUrl)));
......@@ -387,11 +393,11 @@ void HelpPlugin::setupUi()
QLatin1String("Help.IndexShortcut"), modecontext);
cmd->setDefaultKeySequence(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_I));
connect(shortcut, SIGNAL(activated()), this, SLOT(activateIndex()));
shortcutMap.insert(indexWindow->windowTitle(), cmd);
shortcutMap.insert(QLatin1String(SB_INDEX), cmd);
ContentWindow *contentWindow = new ContentWindow();
contentWindow->setWindowTitle(tr("Contents"));
m_contentItem = new Core::SideBarItem(contentWindow);
m_contentItem = new Core::SideBarItem(contentWindow, QLatin1String(SB_CONTENTS));
connect(contentWindow, SIGNAL(linkActivated(QUrl)), m_centralWidget,
SLOT(setSource(QUrl)));
......@@ -401,11 +407,11 @@ void HelpPlugin::setupUi()
modecontext);
cmd->setDefaultKeySequence(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C));
connect(shortcut, SIGNAL(activated()), this, SLOT(activateContents()));
shortcutMap.insert(contentWindow->windowTitle(), cmd);
shortcutMap.insert(QLatin1String(SB_CONTENTS), cmd);
SearchWidget *searchWidget = new SearchWidget();
searchWidget->setWindowTitle(tr("Search"));
m_searchItem = new Core::SideBarItem(searchWidget);
m_searchItem = new Core::SideBarItem(searchWidget, "Search");
connect(searchWidget, SIGNAL(linkActivated(QUrl)), m_centralWidget,
SLOT(setSourceFromSearch(QUrl)));
......@@ -416,12 +422,12 @@ void HelpPlugin::setupUi()
// modecontext);
// cmd->setDefaultKeySequence(QKeySequence(Qt::CTRL + Qt::Key_S));
// connect(shortcut, SIGNAL(activated()), this, SLOT(activateSearch()));
// shortcutMap.insert(searchWidget->windowTitle(), cmd);
// shortcutMap.insert("Search", cmd);
BookmarkManager *manager = &HelpManager::bookmarkManager();
BookmarkWidget *bookmarkWidget = new BookmarkWidget(manager, 0, false);
bookmarkWidget->setWindowTitle(tr("Bookmarks"));
m_bookmarkItem = new Core::SideBarItem(bookmarkWidget);
m_bookmarkItem = new Core::SideBarItem(bookmarkWidget, QLatin1String(SB_BOOKMARKS));
connect(bookmarkWidget, SIGNAL(linkActivated(QUrl)), m_centralWidget,
SLOT(setSource(QUrl)));
......@@ -432,11 +438,11 @@ void HelpPlugin::setupUi()
// modecontext);
// cmd->setDefaultKeySequence(QKeySequence(Qt::CTRL + Qt::Key_B));
// connect(shortcut, SIGNAL(activated()), this, SLOT(activateBookmarks()));
// shortcutMap.insert(bookmarkWidget->windowTitle(), cmd);
// shortcutMap.insert("Bookmarks", cmd);
QWidget *openPagesWidget = OpenPagesManager::instance().openPagesWidget();
openPagesWidget->setWindowTitle(tr("Open Pages"));
m_openPagesItem = new Core::SideBarItem(openPagesWidget);
m_openPagesItem = new Core::SideBarItem(openPagesWidget, QLatin1String(SB_OPENPAGES));
shortcut = new QShortcut(m_splitter);
shortcut->setWhatsThis(tr("Activate Open Pages in Help mode"));
......@@ -444,7 +450,7 @@ void HelpPlugin::setupUi()
modecontext);
cmd->setDefaultKeySequence(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_O));
connect(shortcut, SIGNAL(activated()), this, SLOT(activateOpenPages()));
shortcutMap.insert(openPagesWidget->windowTitle(), cmd);
shortcutMap.insert(QLatin1String(SB_OPENPAGES), cmd);
QList<Core::SideBarItem*> itemList;
itemList << m_contentItem << m_indexItem << m_searchItem << m_bookmarkItem
......
......@@ -77,6 +77,13 @@ enum {
debug = false
};
const char * const SB_NAVIGATOR = "Navigator";
const char * const SB_LIBRARY = "Library";
const char * const SB_PROPERTIES = "Properties";
const char * const SB_PROJECTS = "Projects";
const char * const SB_FILESYSTEM = "FileSystem";
const char * const SB_OPENDOCUMENTS = "OpenDocuments";
namespace QmlDesigner {
namespace Internal {
......@@ -584,17 +591,15 @@ void DesignModeWidget::setup()
{
Core::NavigationView navigationView;
navigationView.widget = 0;
if (factory->displayName() == QLatin1String("Projects")) {
if (factory->id() == QLatin1String("Projects")) {
navigationView = factory->createWidget();
projectsExplorer = navigationView.widget;
projectsExplorer->setWindowTitle(tr("Projects"));
}
if (factory->displayName() == QLatin1String("File System")) {
} else if (factory->id() == QLatin1String("File System")) {
navigationView = factory->createWidget();
fileSystemExplorer = navigationView.widget;
fileSystemExplorer->setWindowTitle(tr("File System"));
}
if (factory->displayName() == QLatin1String("Open Documents")) {
} else if (factory->id() == QLatin1String("Open Documents")) {
navigationView = factory->createWidget();
openDocumentsWidget = navigationView.widget;
openDocumentsWidget->setWindowTitle(tr("Open Documents"));
......@@ -632,25 +637,25 @@ void DesignModeWidget::setup()
m_warningWidget = new DocumentWarningWidget(this);
m_warningWidget->setVisible(false);
Core::SideBarItem *navigatorItem = new Core::SideBarItem(m_navigator->widget());
Core::SideBarItem *libraryItem = new Core::SideBarItem(m_itemLibrary.data());
Core::SideBarItem *propertiesItem = new Core::SideBarItem(m_allPropertiesBox.data());
Core::SideBarItem *navigatorItem = new Core::SideBarItem(m_navigator->widget(), QLatin1String(SB_NAVIGATOR));
Core::SideBarItem *libraryItem = new Core::SideBarItem(m_itemLibrary.data(), QLatin1String(SB_LIBRARY));
Core::SideBarItem *propertiesItem = new Core::SideBarItem(m_allPropertiesBox.data(), QLatin1String(SB_PROPERTIES));
// default items
m_sideBarItems << navigatorItem << libraryItem << propertiesItem;
if (projectsExplorer) {
Core::SideBarItem *projectExplorerItem = new Core::SideBarItem(projectsExplorer);
Core::SideBarItem *projectExplorerItem = new Core::SideBarItem(projectsExplorer, QLatin1String(SB_PROJECTS));
m_sideBarItems << projectExplorerItem;
}
if (fileSystemExplorer) {
Core::SideBarItem *fileSystemExplorerItem = new Core::SideBarItem(fileSystemExplorer);
Core::SideBarItem *fileSystemExplorerItem = new Core::SideBarItem(fileSystemExplorer, QLatin1String(SB_FILESYSTEM));
m_sideBarItems << fileSystemExplorerItem;
}
if (openDocumentsWidget) {
Core::SideBarItem *openDocumentsItem = new Core::SideBarItem(openDocumentsWidget);
Core::SideBarItem *openDocumentsItem = new Core::SideBarItem(openDocumentsWidget, QLatin1String(SB_OPENDOCUMENTS));
m_sideBarItems << openDocumentsItem;
}
......@@ -717,13 +722,13 @@ void DesignModeWidget::setup()
void DesignModeWidget::updateAvailableSidebarItemsRight()
{
// event comes from m_leftSidebar, so update right side.
m_rightSideBar->setUnavailableItems(m_leftSideBar->unavailableItems());
m_rightSideBar->setUnavailableItemIds(m_leftSideBar->unavailableItemIds());
}
void DesignModeWidget::updateAvailableSidebarItemsLeft()
{
// event comes from m_rightSidebar, so update left side.
m_leftSideBar->setUnavailableItems(m_rightSideBar->unavailableItems());
m_leftSideBar->setUnavailableItemIds(m_rightSideBar->unavailableItemIds());
}
void DesignModeWidget::resizeEvent(QResizeEvent *event)
......
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