Override QStyle::subElementRect in fancy tabbar to fix style problems
Something is causing the contents of the tabbar to be stretched from top to bottom with space between icons and text. You can see this on the default Fedora (Gnome) installation. Also fixes the tabbar on macOS where the content was in the middle instead of the top.
This commit is contained in:
@@ -52,6 +52,11 @@
|
|||||||
#include <QLayout>
|
#include <QLayout>
|
||||||
#include <QBoxLayout>
|
#include <QBoxLayout>
|
||||||
#include <QtEvents>
|
#include <QtEvents>
|
||||||
|
#include <QStyle>
|
||||||
|
#include <QCommonStyle>
|
||||||
|
#include <QProxyStyle>
|
||||||
|
#include <QStyleOption>
|
||||||
|
#include <QStyleOptionComplex>
|
||||||
|
|
||||||
#include "fancytabwidget.h"
|
#include "fancytabwidget.h"
|
||||||
#include "core/stylehelper.h"
|
#include "core/stylehelper.h"
|
||||||
@@ -406,7 +411,25 @@ void FancyTabWidget::currentTabChanged(const int idx) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FancyTabWidget::FancyTabWidget(QWidget* parent) : QTabWidget(parent),
|
// Override subElementRect() and use QCommonStyle to fix a problem with certain styles.
|
||||||
|
// Something is causing the contents of the tabbar to be stretched from top to bottom with space between icons and text.
|
||||||
|
// You can see this on the default Fedora (Gnome) installation.
|
||||||
|
// Also fixes the tabbar on macOS where the content was in the middle.
|
||||||
|
|
||||||
|
class FancyTabWidgetProxyStyle : public QProxyStyle {
|
||||||
|
public:
|
||||||
|
explicit FancyTabWidgetProxyStyle(QStyle *style) : QProxyStyle(style), common_style_(new QCommonStyle()) {}
|
||||||
|
~FancyTabWidgetProxyStyle() override { common_style_->deleteLater(); }
|
||||||
|
|
||||||
|
QRect subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget = nullptr) const override {
|
||||||
|
return common_style_->subElementRect(element, option, widget);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
QCommonStyle *common_style_;
|
||||||
|
};
|
||||||
|
|
||||||
|
FancyTabWidget::FancyTabWidget(QWidget *parent) : QTabWidget(parent),
|
||||||
menu_(nullptr),
|
menu_(nullptr),
|
||||||
mode_(Mode_None),
|
mode_(Mode_None),
|
||||||
bottom_widget_(nullptr),
|
bottom_widget_(nullptr),
|
||||||
@@ -422,11 +445,16 @@ FancyTabWidget::FancyTabWidget(QWidget* parent) : QTabWidget(parent),
|
|||||||
setMovable(true);
|
setMovable(true);
|
||||||
setElideMode(Qt::ElideNone);
|
setElideMode(Qt::ElideNone);
|
||||||
setUsesScrollButtons(true);
|
setUsesScrollButtons(true);
|
||||||
|
setStyle(new FancyTabWidgetProxyStyle(style()));
|
||||||
|
|
||||||
connect(tabBar, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
|
connect(tabBar, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FancyTabWidget::~FancyTabWidget() {
|
||||||
|
style()->deleteLater();
|
||||||
|
}
|
||||||
|
|
||||||
void FancyTabWidget::Load(const QString &kSettingsGroup) {
|
void FancyTabWidget::Load(const QString &kSettingsGroup) {
|
||||||
|
|
||||||
QSettings s;
|
QSettings s;
|
||||||
|
|||||||
@@ -47,20 +47,21 @@ class FancyTabWidget : public QTabWidget {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
explicit FancyTabWidget(QWidget *parent = nullptr);
|
explicit FancyTabWidget(QWidget *parent = nullptr);
|
||||||
|
~FancyTabWidget() override;
|
||||||
|
|
||||||
void AddTab(QWidget *widget_view, const QString &name, const QIcon &icon, const QString &label);
|
void AddTab(QWidget *widget_view, const QString &name, const QIcon &icon, const QString &label);
|
||||||
bool EnableTab(QWidget *widget_view);
|
bool EnableTab(QWidget *widget_view);
|
||||||
bool DisableTab(QWidget *widget_view);
|
bool DisableTab(QWidget *widget_view);
|
||||||
int insertTab(const int idx, QWidget *page, const QIcon &icon, const QString &label);
|
int insertTab(const int idx, QWidget *page, const QIcon &icon, const QString &label);
|
||||||
void addBottomWidget(QWidget* widget_view);
|
void addBottomWidget(QWidget* widget_view);
|
||||||
int IndexOfTab(QWidget *widget);
|
int IndexOfTab(QWidget *widget);
|
||||||
|
|
||||||
void setBackgroundPixmap(const QPixmap& pixmap);
|
void setBackgroundPixmap(const QPixmap& pixmap);
|
||||||
void addSpacer();
|
void addSpacer();
|
||||||
|
|
||||||
void Load(const QString &kSettingsGroup);
|
void Load(const QString &kSettingsGroup);
|
||||||
void SaveSettings(const QString &kSettingsGroup);
|
void SaveSettings(const QString &kSettingsGroup);
|
||||||
void ReloadSettings();
|
void ReloadSettings();
|
||||||
|
|
||||||
// Values are persisted - only add to the end
|
// Values are persisted - only add to the end
|
||||||
enum Mode {
|
enum Mode {
|
||||||
@@ -72,47 +73,47 @@ class FancyTabWidget : public QTabWidget {
|
|||||||
Mode_PlainSidebar,
|
Mode_PlainSidebar,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const int TabSize_LargeSidebarMinWidth;
|
static const int TabSize_LargeSidebarMinWidth;
|
||||||
static const int IconSize_LargeSidebar;
|
static const int IconSize_LargeSidebar;
|
||||||
static const int IconSize_SmallSidebar;
|
static const int IconSize_SmallSidebar;
|
||||||
|
|
||||||
Mode mode() const { return mode_; }
|
Mode mode() const { return mode_; }
|
||||||
int iconsize_smallsidebar() const { return iconsize_smallsidebar_; }
|
int iconsize_smallsidebar() const { return iconsize_smallsidebar_; }
|
||||||
int iconsize_largesidebar() const { return iconsize_largesidebar_; }
|
int iconsize_largesidebar() const { return iconsize_largesidebar_; }
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void ModeChanged(FancyTabWidget::Mode mode);
|
void ModeChanged(FancyTabWidget::Mode mode);
|
||||||
void CurrentChanged(int);
|
void CurrentChanged(int);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void setCurrentIndex(int idx);
|
void setCurrentIndex(int idx);
|
||||||
void SetMode(Mode mode);
|
void SetMode(Mode mode);
|
||||||
// Mapper mapped signal needs this convenience function
|
// Mapper mapped signal needs this convenience function
|
||||||
void SetMode(int mode) { SetMode(Mode(mode)); }
|
void SetMode(int mode) { SetMode(Mode(mode)); }
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void tabBarUpdateGeometry();
|
void tabBarUpdateGeometry();
|
||||||
void currentTabChanged(int);
|
void currentTabChanged(int);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void paintEvent(QPaintEvent*) override;
|
void paintEvent(QPaintEvent*) override;
|
||||||
void contextMenuEvent(QContextMenuEvent* e) override;
|
void contextMenuEvent(QContextMenuEvent* e) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void addMenuItem(QActionGroup* group, const QString& text, Mode mode);
|
void addMenuItem(QActionGroup* group, const QString& text, Mode mode);
|
||||||
|
|
||||||
QPixmap background_pixmap_;
|
QPixmap background_pixmap_;
|
||||||
QMenu *menu_;
|
QMenu *menu_;
|
||||||
Mode mode_;
|
Mode mode_;
|
||||||
QWidget *bottom_widget_;
|
QWidget *bottom_widget_;
|
||||||
|
|
||||||
QMap<QWidget*, TabData*> tabs_;
|
QMap<QWidget*, TabData*> tabs_;
|
||||||
|
|
||||||
bool bg_color_system_;
|
bool bg_color_system_;
|
||||||
bool bg_gradient_;
|
bool bg_gradient_;
|
||||||
QColor bg_color_;
|
QColor bg_color_;
|
||||||
int iconsize_smallsidebar_;
|
int iconsize_smallsidebar_;
|
||||||
int iconsize_largesidebar_;
|
int iconsize_largesidebar_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user