Skip to content

Commit

Permalink
Fix settings tab name in KDE/Plasma
Browse files Browse the repository at this point in the history
With Plasma Breeze theme, accelerators are automatically set if a tab in
QTabBar doesn't have any. This means that it's not possible to use tab
label to store tab name.
  • Loading branch information
hluk committed Aug 27, 2017
1 parent 829a8fc commit 9c581e8
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 47 deletions.
12 changes: 6 additions & 6 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ int MainWindow::findTabIndexExactMatch(const QString &name)
TabWidget *w = ui->tabWidget;

for( int i = 0; i < w->count(); ++i ) {
if ( name == w->tabText(i) )
if ( name == w->tabName(i) )
return i;
}

Expand Down Expand Up @@ -2044,7 +2044,7 @@ int MainWindow::findTabIndex(const QString &name)
// Ignore key hints ('&').
if ( !hasKeyHint(name) ) {
for( int i = 0; i < w->count(); ++i ) {
QString tabName = w->tabText(i);
QString tabName = w->tabName(i);
if ( name == removeKeyHint(&tabName) )
return i;
}
Expand Down Expand Up @@ -3433,9 +3433,9 @@ Action *MainWindow::action(const QVariantMap &data, const Command &cmd, const QM
QStringList tabs;
tabs.reserve( w->count() );
for( int i = 0; i < w->count(); ++i )
tabs << w->tabText(i);
tabs << w->tabName(i);
if ( outputTab.isEmpty() && w->currentIndex() > 0 )
outputTab = w->tabText( w->currentIndex() );
outputTab = w->tabName( w->currentIndex() );
actionDialog->setOutputTabs(tabs, outputTab);

actionDialog->show();
Expand Down Expand Up @@ -3533,7 +3533,7 @@ void MainWindow::renameTab(const QString &name, int tabIndex)
if (placeholder) {
updateTabIcon(name, placeholder->tabName());
placeholder->setTabName(name);
ui->tabWidget->setTabText(tabIndex, name);
ui->tabWidget->setTabName(tabIndex, name);
saveTabPositions();
}
}
Expand Down Expand Up @@ -3592,7 +3592,7 @@ void MainWindow::removeTab(bool ask, int tabIndex)
this,
tr("Remove Tab?"),
tr("Do you want to remove tab <strong>%1</strong>?"
).arg( w->tabText(tabIndex).remove('&') ),
).arg( w->tabName(tabIndex).remove('&') ),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);
}
Expand Down
26 changes: 15 additions & 11 deletions src/gui/tabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ int dropItemsTabIndex(const QDropEvent &event, const QTabBar &parent)
return canDropToTab(event) ? parent.tabAt( event.pos() ) : -1;
}

int tabIndex(const QString &tabName, const QTabBar &parent)
int tabIndex(const QString &tabName, const TabBar &parent)
{
for (int i = 0; i < parent.count(); ++i) {
if (parent.tabText(i) == tabName)
if (parent.tabName(i) == tabName)
return i;
}

return -1;
}

void updateTabIcon(int i, QTabBar *parent)
void updateTabIcon(int i, TabBar *parent)
{
const QIcon icon = getIconForTabName(parent->tabText(i));
const QIcon icon = getIconForTabName(parent->tabName(i));
parent->setTabIcon(i, icon);
}

Expand All @@ -73,14 +73,17 @@ QString TabBar::getCurrentTabPath() const
return QString();
}

QString TabBar::tabText(int tabIndex) const
QString TabBar::tabName(int tabIndex) const
{
return QTabBar::tabText(tabIndex);
return QTabBar::tabData(tabIndex).toString();
}

void TabBar::setTabText(int tabIndex, const QString &tabText)
void TabBar::setTabName(int tabIndex, const QString &tabName)
{
QTabBar::setTabText(tabIndex, tabText);
// Use tab data to store tab name because tab text can change externally,
// e.g. Breeze theme adds missing accelerators (renames "test" to "&test).
setTabData(tabIndex, tabName);
QTabBar::setTabText(tabIndex, tabName);
}

void TabBar::setTabItemCount(const QString &tabName, const QString &itemCount)
Expand Down Expand Up @@ -120,9 +123,10 @@ void TabBar::updateTabIcon(const QString &tabName)
::updateTabIcon(i, this);
}

void TabBar::insertTab(int index, const QString &text)
void TabBar::insertTab(int index, const QString &tabName)
{
QTabBar::insertTab(index, text);
const int i = QTabBar::insertTab(index, tabName);
setTabData(i, tabName);
}

void TabBar::removeTab(int index)
Expand Down Expand Up @@ -202,7 +206,7 @@ void TabBar::dropEvent(QDropEvent *event)
int tabIndex = dropItemsTabIndex(*event, *this);

if ( tabIndex != -1 )
emit dropItems( tabText(tabIndex), event->mimeData() );
emit dropItems( tabName(tabIndex), event->mimeData() );
else
QTabBar::dropEvent(event);
}
Expand Down
6 changes: 3 additions & 3 deletions src/gui/tabbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class TabBar : public QTabBar, public TabsWidgetInterface

bool isTabGroup(const QString &) const override { return false; }

QString tabText(int tabIndex) const override;
void setTabText(int tabIndex, const QString &tabText) override;
QString tabName(int tabIndex) const override;
void setTabName(int tabIndex, const QString &tabName) override;

void setTabItemCount(const QString &tabName, const QString &itemCount) override;

void updateTabIcon(const QString &tabName) override;

void insertTab(int index, const QString &text) override;
void insertTab(int index, const QString &tabName) override;
void removeTab(int index) override;
void moveTab(int from, int to) override;

Expand Down
6 changes: 3 additions & 3 deletions src/gui/tabswidgetinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class TabsWidgetInterface {
virtual bool isTabGroup(const QString &tab) const = 0;

/** Return path of tab in tree or label in tab bar. */
virtual QString tabText(int tabIndex) const = 0;
virtual QString tabName(int tabIndex) const = 0;

virtual void setTabText(int tabIndex, const QString &tabName) = 0;
virtual void setTabName(int tabIndex, const QString &tabName) = 0;

virtual void setTabItemCount(const QString &tabName, const QString &itemCount) = 0;

virtual void updateTabIcon(const QString &tabName) = 0;

virtual void insertTab(int tabIndex, const QString &tabText) = 0;
virtual void insertTab(int tabIndex, const QString &tabName) = 0;

/** Remove tab with given @a index. */
virtual void removeTab(int index) = 0;
Expand Down
12 changes: 6 additions & 6 deletions src/gui/tabtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,21 +291,21 @@ bool TabTree::isTabGroup(const QString &tab) const
return isTabGroup( findTreeItem(tab) );
}

QString TabTree::tabText(int tabIndex) const
QString TabTree::tabName(int tabIndex) const
{
return getTabPath( findTreeItem(tabIndex) );
}

void TabTree::setTabText(int tabIndex, const QString &tabText)
void TabTree::setTabName(int tabIndex, const QString &tabName)
{
QTreeWidgetItem *item = findTreeItem(tabIndex);
Q_ASSERT(item);

if (getTabPath(item) == tabText)
if (getTabPath(item) == tabName)
return;

const QString itemCount = item->data(0, DataItemCount).toString();
insertTab(tabIndex, tabText);
insertTab(tabIndex, tabName);
if (item == currentItem())
setCurrentTab(tabIndex);

Expand All @@ -316,10 +316,10 @@ void TabTree::setTabText(int tabIndex, const QString &tabText)

item = findTreeItem(tabIndex);
Q_ASSERT(item);
Q_ASSERT(getTabPath(item) == tabText);
Q_ASSERT(getTabPath(item) == tabName);

if ( !itemCount.isEmpty() )
setTabItemCount(tabText, itemCount);
setTabItemCount(tabName, itemCount);

updateItemSize(item);
updateSize();
Expand Down
4 changes: 2 additions & 2 deletions src/gui/tabtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class TabTree : public QTreeWidget, public TabsWidgetInterface

bool isTabGroup(const QString &tab) const override;

QString tabText(int tabIndex) const override;
void setTabText(int tabIndex, const QString &tabText) override;
QString tabName(int tabIndex) const override;
void setTabName(int tabIndex, const QString &tabName) override;

void setTabItemCount(const QString &tabName, const QString &itemCount) override;

Expand Down
24 changes: 12 additions & 12 deletions src/gui/tabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,18 @@ int TabWidget::count() const
return m_stackedWidget->count();
}

QString TabWidget::tabText(int tabIndex) const
QString TabWidget::tabName(int tabIndex) const
{
return m_tabs->tabText(tabIndex);
return m_tabs->tabName(tabIndex);
}

void TabWidget::setTabText(int tabIndex, const QString &tabName)
void TabWidget::setTabName(int tabIndex, const QString &tabName)
{
const QString oldTabName = tabText(tabIndex);
const QString oldTabName = this->tabName(tabIndex);
if ( m_tabItemCounters.contains(oldTabName) )
m_tabItemCounters.insert( tabName, m_tabItemCounters.take(oldTabName) );

m_tabs->setTabText(tabIndex, tabName);
m_tabs->setTabName(tabIndex, tabName);

m_tabs->adjustSize();
}
Expand All @@ -138,7 +138,7 @@ void TabWidget::setTabItemCountVisible(bool visible)
{
m_showTabItemCount = visible;
for (int i = 0; i < count(); ++i)
updateTabItemCount( tabText(i) );
updateTabItemCount( tabName(i) );

m_tabs->adjustSize();
}
Expand All @@ -148,18 +148,18 @@ void TabWidget::updateTabIcon(const QString &tabName)
m_tabs->updateTabIcon(tabName);
}

void TabWidget::insertTab(int tabIndex, QWidget *widget, const QString &tabText)
void TabWidget::insertTab(int tabIndex, QWidget *widget, const QString &tabName)
{
const bool firstTab = count() == 0;
m_stackedWidget->insertWidget(tabIndex, widget);

m_tabs->insertTab(tabIndex, tabText);
m_tabs->insertTab(tabIndex, tabName);
m_toolBarCurrent->layout()->setSizeConstraint(QLayout::SetMinAndMaxSize);

if (firstTab)
emit currentChanged(0, -1);

updateTabItemCount(tabText);
updateTabItemCount(tabName);
updateToolBar();
}

Expand All @@ -168,7 +168,7 @@ void TabWidget::removeTab(int tabIndex)
if (tabIndex == currentIndex())
setCurrentIndex(tabIndex == 0 ? 1 : 0);

const QString tabName = tabText(tabIndex);
const QString tabName = this->tabName(tabIndex);
m_tabItemCounters.remove(tabName);

// Item count must be updated If tab is removed but tab group remains.
Expand All @@ -188,7 +188,7 @@ QStringList TabWidget::tabs() const
QStringList tabs;
tabs.reserve( count() );
for( int i = 0; i < count(); ++i )
tabs.append( tabText(i) );
tabs.append( tabName(i) );

return tabs;
}
Expand Down Expand Up @@ -221,7 +221,7 @@ void TabWidget::saveTabInfo()
QStringList tabs;
tabs.reserve( count() );
for ( int i = 0; i < count(); ++i )
tabs.append( tabText(i) );
tabs.append( tabName(i) );

QSettings settings(getTabWidgetConfigurationFilePath(), QSettings::IniFormat);

Expand Down
8 changes: 4 additions & 4 deletions src/gui/tabwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ class TabWidget : public QWidget
int count() const;

/** Return path of tab in tree or label in tab bar. */
QString tabText(int tabIndex) const;
QString tabName(int tabIndex) const;

void setTabText(int tabIndex, const QString &tabName);
void setTabName(int tabIndex, const QString &tabName);

void setTabItemCountVisible(bool visible);

void updateTabIcon(const QString &tabName);

void insertTab(int tabIndex, QWidget *widget, const QString &tabText);
void insertTab(int tabIndex, QWidget *widget, const QString &tabName);

void addTab(QWidget *widget, const QString &tabText) { insertTab( count(), widget, tabText); }
void addTab(QWidget *widget, const QString &tabName) { insertTab( count(), widget, tabName); }

void removeTab(int tabIndex);

Expand Down

0 comments on commit 9c581e8

Please sign in to comment.