27#include <QFontMetrics>
33DockTitleBar::DockTitleBar(
const QString &title, QWidget *parent)
35 setAutoFillBackground(
true);
36 setFrameShape(QFrame::NoFrame);
38 auto *layout =
new QHBoxLayout(
this);
39 layout->setContentsMargins(6, 2, 6, 2);
40 layout->setSpacing(8);
42 titleLabel_ =
new QLabel(title,
this);
43 QFont font = titleLabel_->font();
45 titleLabel_->setFont(font);
47 subtitleLabel_ =
new QLabel(
this);
48 subtitleLabel_->setVisible(
false);
49 subtitleLabel_->setTextFormat(Qt::PlainText);
51 layout->addWidget(titleLabel_);
52 layout->addWidget(subtitleLabel_, 1);
59 if (focused_ == focused) {
67 if (!subtitleLabel_) {
71 subtitleLabel_->setToolTip(
subtitle);
72 subtitleLabel_->setVisible(!
subtitle.isEmpty());
73 updateElidedSubtitle();
76void DockTitleBar::resizeEvent(QResizeEvent *event) {
77 QFrame::resizeEvent(event);
78 updateElidedSubtitle();
81void DockTitleBar::updateElidedSubtitle() {
82 if (!subtitleLabel_ || fullSubtitle_.isEmpty()) {
88 int titleWidth = titleLabel_ ? titleLabel_->sizeHint().width() : 0;
89 int margins = 6 + 6 + 8;
90 int availableWidth = width() - titleWidth - margins - 20;
92 if (availableWidth < 50) {
96 QFontMetrics fm(subtitleLabel_->font());
97 QString elided = fm.elidedText(fullSubtitle_, Qt::ElideMiddle, availableWidth);
98 subtitleLabel_->setText(elided);
101void DockTitleBar::updateAppearance() {
102 QPalette pal = palette();
106 pal.setColor(QPalette::Window, QColor(0, 120, 212));
107 pal.setColor(QPalette::WindowText, Qt::white);
110 pal.setColor(QPalette::Window, palette().color(QPalette::Mid));
111 pal.setColor(QPalette::WindowText, palette().color(QPalette::WindowText));
115 titleLabel_->setPalette(pal);
116 if (subtitleLabel_) {
117 subtitleLabel_->setPalette(pal);
Custom dock widget title bar with fast focus indication.
void setFocused(bool focused)
Set the focused state. Uses QPalette for fast updates.
void setSubtitle(const QString &subtitle)
Set subtitle text (e.g., file path) displayed below the title.
QString subtitle() const
Get the current subtitle text (full, non-elided).