audiomonitor: hide labels if too narrow

This commit is contained in:
itsmattkc
2023-03-20 16:20:58 -07:00
parent 0b0582ad5c
commit 980482b06e
+35 -15
View File
@@ -44,6 +44,8 @@ AudioMonitor::AudioMonitor(QWidget *parent) :
instances_.append(this); instances_.append(this);
values_.resize(kMaximumSmoothness); values_.resize(kMaximumSmoothness);
this->setMinimumWidth(this->fontMetrics().height());
} }
AudioMonitor::~AudioMonitor() AudioMonitor::~AudioMonitor()
@@ -146,25 +148,35 @@ void AudioMonitor::paintGL()
bool horizontal = this->width() > this->height(); bool horizontal = this->width() > this->height();
// Create rect where decibel markings will go on the side int minimum_width = this->fontMetrics().height() * 3;
QRect db_labels_rect = geometry;
// Determine rect where the main meter will go // Determine rect where the main meter will go
QRect full_meter_rect = geometry; QRect full_meter_rect = geometry;
// Create rect where decibel markings will go on the side
QRect db_labels_rect;
bool draw_text;
// Width of each channel in the meter // Width of each channel in the meter
int peaks_pos; int peaks_pos;
int channel_size; int channel_size;
int db_line_length = fm.horizontalAdvance(QStringLiteral("-")); int db_line_length = fm.horizontalAdvance(QStringLiteral("-"));
int db_width = QtUtils::QFontMetricsWidth(p.fontMetrics(), "-00 "); int db_width = QtUtils::QFontMetricsWidth(p.fontMetrics(), "-00 ");
if (horizontal) { if (horizontal) {
// Insert meter rect // Insert peaks area
full_meter_rect.adjust(db_width/2, 0, -font_height, -font_height); full_meter_rect.adjust(0, 0, -font_height, 0);
draw_text = (height() >= minimum_width);
// Derive dB label rect // Derive dB label rect
db_labels_rect = full_meter_rect; if (draw_text) {
db_labels_rect.setTop(db_labels_rect.bottom()); // Insert meter rect
db_labels_rect.setHeight(font_height + db_line_length); full_meter_rect.adjust(db_width/2, 0, 0, -font_height);
db_labels_rect = full_meter_rect;
db_labels_rect.setTop(db_labels_rect.bottom());
db_labels_rect.setHeight(font_height + db_line_length);
}
// Divide height by channel count // Divide height by channel count
channel_size = full_meter_rect.height() / params_.channel_count(); channel_size = full_meter_rect.height() / params_.channel_count();
@@ -172,15 +184,22 @@ void AudioMonitor::paintGL()
// Set peaks Y to right-most // Set peaks Y to right-most
peaks_pos = full_meter_rect.right(); peaks_pos = full_meter_rect.right();
} else { } else {
int db_width_and_line = db_width + db_line_length; // Insert peaks rect
full_meter_rect.adjust(0, font_height, 0, 0);
// Inset meter rect draw_text = (width() >= minimum_width);
full_meter_rect.adjust(db_width_and_line, font_height, 0, -font_height/2);
// Derive dB label rect // Derive dB label rect
db_labels_rect = full_meter_rect; if (draw_text) {
db_labels_rect.setX(0); int db_width_and_line = db_width + db_line_length;
db_labels_rect.setWidth(db_width_and_line);
// Insert meter rect
full_meter_rect.adjust(db_width_and_line, 0, 0, -font_height/2);
db_labels_rect = full_meter_rect;
db_labels_rect.setX(0);
db_labels_rect.setWidth(db_width_and_line);
}
// Divide width by channel count // Divide width by channel count
channel_size = full_meter_rect.width() / params_.channel_count(); channel_size = full_meter_rect.width() / params_.channel_count();
@@ -199,9 +218,10 @@ void AudioMonitor::paintGL()
cached_background_.fill(Qt::transparent); cached_background_.fill(Qt::transparent);
QPainter cached_painter(&cached_background_); QPainter cached_painter(&cached_background_);
cached_painter.setFont(f);
{ if (draw_text) {
cached_painter.setFont(f);
// Draw decibel markings // Draw decibel markings
QRect last_db_marking_rect; QRect last_db_marking_rect;