instAdmin/Cpp/GisDes/SaRibbon/SARibbonBar/SARibbonTabBar.cpp
2024-10-29 22:24:50 +08:00

54 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "SARibbonTabBar.h"
#include <QStyleOptionTab>
#include <QFontMetrics>
SARibbonTabBar::SARibbonTabBar(QWidget* parent) : QTabBar(parent), m_tabMargin(6, 0, 0, 0)
{
setExpanding(false);
}
const QMargins& SARibbonTabBar::tabMargin() const
{
return (m_tabMargin);
}
void SARibbonTabBar::setTabMargin(const QMargins& tabMargin)
{
m_tabMargin = tabMargin;
}
/**
* @brief tab的尺寸预估
*
* 有别于系统默认的tabbarSARibbonTabBar的tab高度和tabbar高度一致且不考虑纵向分布情况
* @param index
* @return
*/
QSize SARibbonTabBar::tabSizeHint(int index) const
{
if (index < 0) {
return QSize();
}
QStyleOptionTab opt;
initStyleOption(&opt, index);
int hframe = style()->pixelMetric(QStyle::PM_TabBarTabHSpace, &opt, this);
const QFontMetrics fm = fontMetrics();
int widgetWidth = 0;
int padding = 0;
if (!opt.leftButtonSize.isEmpty()) {
padding += 4;
widgetWidth += opt.leftButtonSize.width();
}
if (!opt.rightButtonSize.isEmpty()) {
padding += 4;
widgetWidth += opt.rightButtonSize.width();
}
if (!opt.icon.isNull()) {
padding += 4;
}
const int textWidth = fm.size(Qt::TextShowMnemonic, opt.text).width();
QSize csz = QSize(textWidth + opt.iconSize.width() + hframe + widgetWidth + padding, height());
return style()->sizeFromContents(QStyle::CT_TabBarTab, &opt, csz, this);
}