From fe396e64ff2740132a354ac5247f192b656bcca8 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Mon, 1 Jan 2018 14:50:36 +0100 Subject: [PATCH] ComboTabBar: Add tabPixmap method to render single tab --- src/lib/tabwidget/combotabbar.cpp | 41 ++++++++++++++++++++++++++++++- src/lib/tabwidget/combotabbar.h | 5 +++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/lib/tabwidget/combotabbar.cpp b/src/lib/tabwidget/combotabbar.cpp index 78e83e03c..608f7efb8 100644 --- a/src/lib/tabwidget/combotabbar.cpp +++ b/src/lib/tabwidget/combotabbar.cpp @@ -1,7 +1,7 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2013-2014 S. Razi Alavizadeh -* Copyright (C) 2014-2017 David Rosca +* Copyright (C) 2014-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -218,6 +218,11 @@ QRect ComboTabBar::tabRect(int index) const return rect; } +QPixmap ComboTabBar::tabPixmap(int index) const +{ + return localTabBar(index)->tabPixmap(toLocalIndex(index)); +} + int ComboTabBar::tabAt(const QPoint &pos) const { QWidget* w = QApplication::widgetAt(mapToGlobal(pos)); @@ -1007,6 +1012,40 @@ QSize TabBarHelper::baseClassTabSizeHint(int index) const return QTabBar::tabSizeHint(index); } +QPixmap TabBarHelper::tabPixmap(int index) const +{ + QStyleOptionTab tab; + initStyleOption(&tab, index); + + tab.state &= ~QStyle::State_MouseOver; + tab.leftButtonSize = QSize(); + tab.rightButtonSize = QSize(); + + QWidget *iconButton = tabButton(index, m_comboTabBar->iconButtonPosition()); + QWidget *closeButton = tabButton(index, m_comboTabBar->closeButtonPosition()); + + if (iconButton) { + const QPixmap pix = iconButton->grab(); + tab.icon = pix; + tab.iconSize = pix.size(); + } + + if (closeButton) { + const int width = tab.fontMetrics.width(tab.text) + closeButton->width(); + tab.text = tab.fontMetrics.elidedText(tabText(index), Qt::ElideRight, width); + } + + QPixmap out(tab.rect.size()); + out.fill(Qt::transparent); + tab.rect = QRect(QPoint(0, 0), out.size()); + + QPainter p(&out); + style()->drawControl(QStyle::CE_TabBarTab, &tab, &p, this); + p.end(); + + return out; +} + bool TabBarHelper::isActiveTabBar() { return m_activeTabBar; diff --git a/src/lib/tabwidget/combotabbar.h b/src/lib/tabwidget/combotabbar.h index cdb2aa695..eca146390 100644 --- a/src/lib/tabwidget/combotabbar.h +++ b/src/lib/tabwidget/combotabbar.h @@ -1,7 +1,7 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2013-2014 S. Razi Alavizadeh -* Copyright (C) 2014-2017 David Rosca +* Copyright (C) 2014-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -75,6 +75,7 @@ public: void setTabTextColor(int index, const QColor &color); QRect tabRect(int index) const; + QPixmap tabPixmap(int index) const; // Returns tab index at pos, or -1 int tabAt(const QPoint &pos) const; @@ -234,6 +235,8 @@ public: QSize tabSizeHint(int index) const; QSize baseClassTabSizeHint(int index) const; + QPixmap tabPixmap(int index) const; + bool isActiveTabBar(); void setActiveTabBar(bool activate);