Skip to content

Commit

Permalink
Attempt to fix error when pinning active tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitozz committed Mar 2, 2019
1 parent abc9bb9 commit 36e4e6c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/widgets/psitabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ PsiTabWidget::PsiTabWidget(QWidget *parent)
barLayout_->addWidget(tabBar_, 2);
barLayout_->setAlignment(Qt::AlignLeft);

int buttonwidth = qMax(tabBar_->style()->pixelMetric(QStyle::PM_TabBarScrollButtonWidth, 0, tabBar_),
int buttonwidth = qMax(tabBar_->style()->pixelMetric(QStyle::PM_TabBarScrollButtonWidth, nullptr, tabBar_),
QApplication::globalStrut().width());

downButton_ = new QToolButton(this);
Expand Down Expand Up @@ -145,7 +145,7 @@ int PsiTabWidget::count() {
*/
QWidget *PsiTabWidget::currentPage() {
if (currentPageIndex() == -1)
return 0;
return nullptr;
return widgets_[currentPageIndex()];
}

Expand Down Expand Up @@ -288,8 +288,8 @@ void PsiTabWidget::setTabPosition(QTabWidget::TabPosition pos) {
layout_->removeItem(stacked_);

// addLayout sets parent and complains if it's already set
barLayout_->setParent(0);
stacked_->setParent(0);
barLayout_->setParent(nullptr);
stacked_->setParent(nullptr);
if (tabsPosition_ == QTabWidget::North) {
layout_->addLayout(barLayout_);
layout_->addLayout(stacked_);
Expand Down
39 changes: 22 additions & 17 deletions src/widgets/tabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
#include <QMimeData>
//#include <QDebug>

#define PINNED_CHARS 4
#define PINNED_CHARS 6

// Do not count invisible &
//#define PINNED_TEXT(text) text.left(text.left(PINNED_CHARS).contains("&") ? (PINNED_CHARS + 1) : PINNED_CHARS)
#define PINNED_TEXT(text) text.left(text.left(PINNED_CHARS).contains("&") ? (PINNED_CHARS + 1) : PINNED_CHARS)

class CloseButton : public QAbstractButton
{
Expand Down Expand Up @@ -158,7 +158,7 @@ LayoutSf possibleLayouts2(const QList<int> &tabs, int barWidth, int rows, double
pLayout += rows;
}

int *pGoodLayout = 0;
int *pGoodLayout = nullptr;
double minDSf = 10000.; // Just huge number

for (pPrevLayout = &layouts[0]; pPrevLayout < pLayout; pPrevLayout += rows) {
Expand All @@ -183,8 +183,8 @@ LayoutSf possibleLayouts2(const QList<int> &tabs, int barWidth, int rows, double
minDSf = sf;
}
else {
break;
addRow = false;
break;
}
}
}
Expand Down Expand Up @@ -263,7 +263,7 @@ void TabBar::Private::layoutTabs()
pinnedTabs = qMin(pinnedTabs, q->count());
hackedTabs.clear();

QTabBar::ButtonPosition closeSide = (QTabBar::ButtonPosition)q->style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, q);
QTabBar::ButtonPosition closeSide = static_cast<QTabBar::ButtonPosition>(q->style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, nullptr, q));
// Tabs maybe 0 width in all-in-one mode
int barWidth = qMax(q->width(), 1);
int tabsWidthHint = 0;
Expand Down Expand Up @@ -301,10 +301,10 @@ void TabBar::Private::layoutTabs()

tab.rect.setSize(tabSizeHint(tab));
// Make pinned tab if need
//if (i < pinnedTabs){
// tab.text = PINNED_TEXT(tab.text);
// tab.rect.setWidth(pinnedTabWidth);
//}
if (i < pinnedTabs){
tab.text = PINNED_TEXT(tab.text);
tab.rect.setWidth(pinnedTabWidth);
}
hackedTabs << tab;
}

Expand Down Expand Up @@ -466,7 +466,7 @@ int TabBar::Private::pinnedTabWidthHint() const
q->initStyleOption(&opt, 0);
opt.leftButtonSize = QSize();
opt.rightButtonSize = QSize();
// opt.text = "XXX";
opt.text = "XXX";
opt.text = QString(PINNED_CHARS, 'X');
QSize iconSize = opt.iconSize;
int hframe = q->style()->pixelMetric(QStyle::PM_TabBarTabHSpace, &opt, q);
Expand Down Expand Up @@ -736,12 +736,12 @@ QRect TabBar::tabRect(int index) const

QWidget *TabBar::tabButton(int index, ButtonPosition position) const
{
ButtonPosition closeButtonPos = static_cast<ButtonPosition>(style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, this));
ButtonPosition closeButtonPos = static_cast<ButtonPosition>(style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, nullptr, this));
if (!d->multiRow || position != closeButtonPos)
return QTabBar::tabButton(index, position);

Q_ASSERT(index < d->closeButtons.size());
return index < d->closeButtons.size() ? d->closeButtons[index] : 0;
return index < d->closeButtons.size() ? d->closeButtons[index] : nullptr;
}

int TabBar::tabAt(const QPoint &position) const
Expand Down Expand Up @@ -798,8 +798,13 @@ void TabBar::setDragsEnabled(bool enabled)
void TabBar::setTabPinned(int index, bool pinned)
{
int newPos = d->pinnedTabs + (pinned ? 0 : -1);
if (index != newPos)
int tempPos = (index+1) < d->hackedTabs.count() ? index + 1 : index - 1; //get nearby tab index
if (index != newPos) {
if(index == TabBar::currentIndex()) { //Check is this tab active
setCurrentIndex(tempPos); // Dirty hack. Set nearby tab as active
}
moveTab(index, newPos);
}

d->pinnedTabs += pinned ? +1 : -1;

Expand Down Expand Up @@ -1029,7 +1034,7 @@ void TabBar::paintEvent(QPaintEvent *event)
}
}

ButtonPosition closeSide = (QTabBar::ButtonPosition)style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, this);
ButtonPosition closeSide = static_cast<QTabBar::ButtonPosition>(style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, nullptr, this));
QStyle::SubElement se = (closeSide == LeftSide ? QStyle::SE_TabBarTabLeftButton : QStyle::SE_TabBarTabRightButton);
if (d->tabsClosable) {
for (int i = 0; i < tabs.size(); i++) {
Expand Down Expand Up @@ -1281,8 +1286,8 @@ CloseButton::CloseButton(QWidget *parent)
QSize CloseButton::sizeHint() const
{
ensurePolished();
int width = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, this);
int height = style()->pixelMetric(QStyle::PM_TabCloseIndicatorHeight, 0, this);
int width = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, nullptr, this);
int height = style()->pixelMetric(QStyle::PM_TabCloseIndicatorHeight, nullptr, this);
return QSize(width, height);
}

Expand Down Expand Up @@ -1316,7 +1321,7 @@ void CloseButton::paintEvent(QPaintEvent *)
if (const TabBar *tb = qobject_cast<const TabBar*>(parent())) {
int index = tb->currentIndex();
if (index >= 0) {
QTabBar::ButtonPosition position = static_cast<QTabBar::ButtonPosition>(style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, tb));
QTabBar::ButtonPosition position = static_cast<QTabBar::ButtonPosition>(style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, nullptr, tb));
if (tb->tabButton(index, position) == this)
opt.state |= QStyle::State_Selected;
}
Expand Down

0 comments on commit 36e4e6c

Please sign in to comment.