File indexing completed on 2024-05-19 04:58:37

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "proxystyle.h"
0019 #include "combotabbar.h"
0020 
0021 #include <QPainter>
0022 #include <QStyleOption>
0023 
0024 ProxyStyle::ProxyStyle()
0025     : QProxyStyle()
0026     , m_TabBarTabHSpace(-1)
0027 {
0028 }
0029 
0030 int ProxyStyle::styleHint(StyleHint hint, const QStyleOption* option, const QWidget* widget, QStyleHintReturn* returnData) const
0031 {
0032     switch (hint) {
0033     case QStyle::SH_Menu_Scrollable:
0034         return int(true);
0035 
0036     case QStyle::SH_TabBar_Alignment:
0037         return Qt::AlignLeft;
0038 
0039     case QStyle::SH_TabBar_CloseButtonPosition:
0040         if (qobject_cast<const TabBarHelper*>(widget)) {
0041             return QTabBar::RightSide;
0042         }
0043         break;
0044 
0045     default:
0046         break;
0047     }
0048 
0049     return QProxyStyle::styleHint(hint, option, widget, returnData);
0050 }
0051 
0052 int ProxyStyle::pixelMetric(PixelMetric metric, const QStyleOption* option, const QWidget* widget) const
0053 {
0054     switch (metric) {
0055     case PM_TabBarTabHSpace:
0056         if (m_TabBarTabHSpace == -1) {
0057             m_TabBarTabHSpace = qMin(QProxyStyle::pixelMetric(PM_TabBarTabHSpace, option, widget), 14);
0058         }
0059         return m_TabBarTabHSpace;
0060 
0061     default:
0062         return QProxyStyle::pixelMetric(metric, option, widget);
0063     }
0064 }
0065 
0066 void ProxyStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
0067 {
0068     if (element == PE_FrameTabBarBase) {
0069         auto *tabBar = qobject_cast<TabBarHelper*>(option->styleObject);
0070         if (tabBar && tabBar->baseColor().isValid()) {
0071             painter->setPen(QPen(tabBar->baseColor(), 0));
0072             painter->drawLine(option->rect.topLeft(), option->rect.topRight());
0073             return;
0074         }
0075     }
0076 
0077     QProxyStyle::drawPrimitive(element, option, painter, widget);
0078 }
0079 
0080 QString ProxyStyle::name() const
0081 {
0082     return baseStyle()->objectName();
0083 }