ACAV f0ba4b7c9529
Abstract Syntax Tree (AST) visualization tool for C, C++, and Objective-C
Loading...
Searching...
No Matches
DockTitleBar.cpp
Go to the documentation of this file.
1/*$!{
2* Aurora Clang AST Viewer (ACAV)
3*
4* Copyright (c) 2026 Min Liu
5* Copyright (c) 2026 Michael David Adams
6*
7* SPDX-License-Identifier: GPL-2.0-or-later
8*
9* This program is free software; you can redistribute it and/or modify
10* it under the terms of the GNU General Public License as published by
11* the Free Software Foundation; either version 2 of the License, or
12* (at your option) any later version.
13*
14* This program is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17* GNU General Public License for more details.
18*
19* You should have received a copy of the GNU General Public License along
20* with this program; if not, see <https://www.gnu.org/licenses/>.
21}$!*/
22
25#include "ui/DockTitleBar.h"
26
27#include <QFontMetrics>
28#include <QHBoxLayout>
29#include <QPalette>
30
31namespace acav {
32
33DockTitleBar::DockTitleBar(const QString &title, QWidget *parent)
34 : QFrame(parent) {
35 setAutoFillBackground(true);
36 setFrameShape(QFrame::NoFrame);
37
38 auto *layout = new QHBoxLayout(this);
39 layout->setContentsMargins(6, 2, 6, 2);
40 layout->setSpacing(8);
41
42 titleLabel_ = new QLabel(title, this);
43 QFont font = titleLabel_->font();
44 font.setBold(true);
45 titleLabel_->setFont(font);
46
47 subtitleLabel_ = new QLabel(this);
48 subtitleLabel_->setVisible(false);
49 subtitleLabel_->setTextFormat(Qt::PlainText);
50
51 layout->addWidget(titleLabel_);
52 layout->addWidget(subtitleLabel_, 1); // Allow subtitle to take available space
53 layout->addStretch();
54
55 updateAppearance();
56}
57
58void DockTitleBar::setFocused(bool focused) {
59 if (focused_ == focused) {
60 return;
61 }
62 focused_ = focused;
63 updateAppearance();
64}
65
66void DockTitleBar::setSubtitle(const QString &subtitle) {
67 if (!subtitleLabel_) {
68 return;
69 }
70 fullSubtitle_ = subtitle;
71 subtitleLabel_->setToolTip(subtitle);
72 subtitleLabel_->setVisible(!subtitle.isEmpty());
73 updateElidedSubtitle();
74}
75
76void DockTitleBar::resizeEvent(QResizeEvent *event) {
77 QFrame::resizeEvent(event);
78 updateElidedSubtitle();
79}
80
81void DockTitleBar::updateElidedSubtitle() {
82 if (!subtitleLabel_ || fullSubtitle_.isEmpty()) {
83 return;
84 }
85
86 // Calculate available width for subtitle
87 // Reserve space for title, spacing, and margins
88 int titleWidth = titleLabel_ ? titleLabel_->sizeHint().width() : 0;
89 int margins = 6 + 6 + 8; // left + right margin + spacing
90 int availableWidth = width() - titleWidth - margins - 20; // extra padding
91
92 if (availableWidth < 50) {
93 availableWidth = 50; // Minimum width
94 }
95
96 QFontMetrics fm(subtitleLabel_->font());
97 QString elided = fm.elidedText(fullSubtitle_, Qt::ElideMiddle, availableWidth);
98 subtitleLabel_->setText(elided);
99}
100
101void DockTitleBar::updateAppearance() {
102 QPalette pal = palette();
103
104 if (focused_) {
105 // Focused: blue accent bar
106 pal.setColor(QPalette::Window, QColor(0, 120, 212)); // #0078d4
107 pal.setColor(QPalette::WindowText, Qt::white);
108 } else {
109 // Unfocused: default background
110 pal.setColor(QPalette::Window, palette().color(QPalette::Mid));
111 pal.setColor(QPalette::WindowText, palette().color(QPalette::WindowText));
112 }
113
114 setPalette(pal);
115 titleLabel_->setPalette(pal);
116 if (subtitleLabel_) {
117 subtitleLabel_->setPalette(pal);
118 }
119}
120
121} // namespace acav
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).