Skip to content

Commit

Permalink
QtCommon v3.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosier committed Jul 20, 2021
1 parent 581c9cb commit db1e003
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 63 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016-2020 Advanced Micro Devices, Inc. All Rights Reserved.
Copyright (c) 2016-2021 Advanced Micro Devices, Inc. All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 7 additions & 6 deletions custom_widgets/arrow_icon_combo_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,15 +635,16 @@ bool ArrowIconComboBox::eventFilter(QObject* object, QEvent* event)
{
Q_UNUSED(object);

Q_ASSERT(item_list_ != nullptr);

if (event != nullptr && item_list_ != nullptr)
if (event != nullptr)
{
if (event->type() == QEvent::MouseButtonPress)
{
if ((this->underMouse() == false) && (item_list_->underMouse() == false))
if (item_list_ != nullptr)
{
CloseMenu();
if ((this->underMouse() == false) && (item_list_->underMouse() == false))
{
CloseMenu();
}
}
}
}
Expand Down Expand Up @@ -793,7 +794,7 @@ void ArrowIconComboBox::paintEvent(QPaintEvent* event)
painter.save();

// Paint background
painter.fillRect(rect(), palette().background());
painter.fillRect(rect(), palette().window());

// Set properties for the lines
QPen pen;
Expand Down
7 changes: 5 additions & 2 deletions custom_widgets/check_box_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ QSize CheckBoxWidget::sizeHint() const

// Calculate max length of the on and off text strings,
// using TextShowMnemonic so that keyboard shortcuts indicated by '&' are taken into account.
int on_text_width = fontMetrics().width(on_text_, -1, Qt::TextShowMnemonic);
int off_text_width = fontMetrics().width(off_text_, -1, Qt::TextShowMnemonic);
const QRect& on_rect = fontMetrics().boundingRect(on_text_);
const QRect& off_rect = fontMetrics().boundingRect(off_text_);

int on_text_width = fontMetrics().boundingRect(on_rect, Qt::TextShowMnemonic, on_text_).width();
int off_text_width = fontMetrics().boundingRect(off_rect, Qt::TextShowMnemonic, off_text_).width();
int max_text_width = std::max(on_text_width, off_text_width);

QSize size;
Expand Down
6 changes: 3 additions & 3 deletions custom_widgets/colored_legend_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ void ColoredLegendScene::Update()
item.rect_item_->setRect(0, 0, scaled_base_height, scaled_base_height);
item.text_item_->setPos(x_pos + scaled_base_height, y_pos_top - kVerticalSpacingAroundText);
item.text_item_->setFont(text_font);
int textWidth = font_metrics.width(item.text_item_->toPlainText());
const int text_width = font_metrics.boundingRect(item.text_item_->toPlainText()).width();

x_pos += scaled_base_height + textWidth + kHorizontalSpacingAfterText;
x_pos += scaled_base_height + text_width + kHorizontalSpacingAfterText;
}
}
else if (legend_mode_ == LegendMode::kText)
Expand All @@ -144,7 +144,7 @@ void ColoredLegendScene::Update()
{
text_legends_[i]->setPos(x_pos, y_pos_top);
text_legends_[i]->setFont(text_font);
int text_width = font_metrics.width(text_legends_[i]->toPlainText());
const int text_width = font_metrics.boundingRect(text_legends_[i]->toPlainText()).width();

x_pos += text_width + ScalingManager::Get().Scaled(kHorizontalSpacingAfterText);
}
Expand Down
6 changes: 3 additions & 3 deletions custom_widgets/donut_pie_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void DonutPieWidget::paintEvent(QPaintEvent* paint_event)
QPainter painter(this);

painter.setRenderHint(QPainter::Antialiasing);
painter.fillRect(rect(), palette().background());
painter.fillRect(rect(), palette().window());

const int width = rect().width();
const int height = rect().height();
Expand All @@ -56,7 +56,7 @@ void DonutPieWidget::paintEvent(QPaintEvent* paint_event)
const QFontMetrics& font_metrics = ScalingManager::Get().ScaledFontMetrics(font());
for (unsigned int index = 0; index < num_segments_; ++index)
{
const int text_width = font_metrics.width(slices_[index].slice_text_);
const int text_width = font_metrics.boundingRect(slices_[index].slice_text_).width();

max_width = std::max(max_width, text_width);
}
Expand Down Expand Up @@ -131,7 +131,7 @@ void DonutPieWidget::paintEvent(QPaintEvent* paint_event)
}

// draw the text labels on the arcs
painter.setPen(palette().foreground().color());
painter.setPen(palette().windowText().color());
for (unsigned int loop = 0; loop < num_segments_; loop++)
{
const QPoint label_pos = label_positions.dequeue();
Expand Down
10 changes: 5 additions & 5 deletions custom_widgets/double_slider_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ void DoubleSliderWidget::SetupPainter(QPainter* painter, Qt::Orientation orienta
{
QColor highlight = palette().color(QPalette::Highlight);
QLinearGradient gradient(start_x, start_y, end_x, end_y);
gradient.setColorAt(0, highlight.dark(120));
gradient.setColorAt(1, highlight.light(108));
gradient.setColorAt(0, highlight.darker(120));
gradient.setColorAt(1, highlight.lighter(108));
painter->setBrush(gradient);

if (orientation == Qt::Horizontal)
{
painter->setPen(QPen(highlight.dark(130), 0));
painter->setPen(QPen(highlight.darker(130), 0));
}
else
{
painter->setPen(QPen(highlight.dark(150), 0));
painter->setPen(QPen(highlight.darker(150), 0));
}
}

Expand All @@ -193,7 +193,7 @@ void DoubleSliderWidget::DrawSpan(QStylePainter* painter, const QRect& span_area
groove_rect.setHeight(1);

// pen & brush
painter->setPen(QPen(double_slider->palette().color(QPalette::Dark).light(), 0));
painter->setPen(QPen(double_slider->palette().color(QPalette::Dark).lighter(), 0));

if (option.orientation == Qt::Horizontal)
{
Expand Down
4 changes: 2 additions & 2 deletions custom_widgets/file_loading_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ void FileLoadingWidget::paintEvent(QPaintEvent* event)
painter.setRenderHint(QPainter::Antialiasing);

// Draw background of widget
painter.setBrush(palette().background());
painter.setBrush(palette().window());
painter.setPen(Qt::NoPen);
painter.drawRect(0, 0, width(), height());

painter.setBrush(palette().foreground());
painter.setBrush(palette().windowText());
double x_offset = (contentsRect().width() / 2) - 1;
painter.translate(contentsRect().center() - QPoint(x_offset, 0));

Expand Down
2 changes: 1 addition & 1 deletion custom_widgets/icon_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void IconButton::paintEvent(QPaintEvent* event)
Q_UNUSED(event);

QStylePainter painter(this);
painter.setPen(QPen(palette().color(QPalette::Background)));
painter.setPen(QPen(palette().color(QPalette::Window)));
painter.drawLine(rect().topLeft(), rect().topRight());
if (!isEnabled())
{
Expand Down
4 changes: 2 additions & 2 deletions custom_widgets/list_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ ListWidget::ListWidget(QWidget* parent, ArrowIconComboBox* button, bool hide)
, parent_(parent)
, show_list_above_button_(false)
{
connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(FocusChanged(QWidget*, QWidget*)));
connect(qApp, &QApplication::focusChanged, this, &ListWidget::FocusChanged);
connect(&ScalingManager::Get(), &ScalingManager::ScaleFactorChanged, this, &ListWidget::OnScaleFactorChanged);
}

ListWidget::~ListWidget()
{
disconnect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(FocusChanged(QWidget*, QWidget*)));
disconnect(qApp, &QApplication::focusChanged, this, &ListWidget::FocusChanged);
disconnect(&ScalingManager::Get(), &ScalingManager::ScaleFactorChanged, this, &ListWidget::OnScaleFactorChanged);
}

Expand Down
4 changes: 2 additions & 2 deletions custom_widgets/quick_link_button_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ QSize QuickLinkButtonWidget::sizeHint() const
QFontMetrics desc_font_metrics(tmp_font);

// The width is defined by the width of the widest line of text, based on the font they will be drawn with.
int width = std::max(std::max(title_font_metrics.width(title_string_), desc_font_metrics.width(description_line_one_)),
desc_font_metrics.width(description_line_two_));
int width = std::max(std::max(title_font_metrics.horizontalAdvance(title_string_), desc_font_metrics.horizontalAdvance(description_line_one_)),
desc_font_metrics.boundingRect(description_line_two_).width());
width += (contentsMargins().left() + contentsMargins().right());

// The height is defined by the height of the title plus two lines of description plus two line spacing between the text.
Expand Down
25 changes: 23 additions & 2 deletions custom_widgets/scaled_check_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "scaled_check_box.h"

#include <QStyle>
#include <QEvent>

#include "utils/qt_util.h"
#include "utils/scaling_manager.h"
Expand Down Expand Up @@ -40,7 +41,8 @@ QSize ScaledCheckBox::sizeHint() const

// Calculate length of the text string,
// using TextShowMnemonic so that keyboard shortcuts indicated by '&' are taken into account.
int text_width = fontMetrics().width(text(), -1, Qt::TextShowMnemonic);
const QRect& rect = fontMetrics().boundingRect(text());
int text_width = fontMetrics().boundingRect(rect, Qt::TextShowMnemonic, text()).width();

QSize size;
size.setWidth(icon_width + spacing + text_width);
Expand All @@ -52,7 +54,7 @@ QSize ScaledCheckBox::sizeHint() const
void ScaledCheckBox::UpdateIndicatorSize()
{
// It appears the only way (or at least easiest way) to set the indicator size is through the stylesheet.
this->setStyleSheet(kIndicatorSizeStylesheet_.arg(fontMetrics().height()));
this->setStyleSheet(kIndicatorSizeStylesheet_.arg(fontMetrics().ascent()));
}

void ScaledCheckBox::OnScaleFactorChanged()
Expand All @@ -68,3 +70,22 @@ void ScaledCheckBox::OnScaleFactorChanged()
updateGeometry();
update();
}

void ScaledCheckBox::changeEvent(QEvent* event)
{
QCheckBox::changeEvent(event);

if (!invalidating_font_metrics_)
{
if (event->type() == QEvent::FontChange)
{
invalidating_font_metrics_ = true;
QtCommon::QtUtils::InvalidateFontMetrics(this);
invalidating_font_metrics_ = false;

UpdateIndicatorSize();

updateGeometry();
}
}
}
11 changes: 10 additions & 1 deletion custom_widgets/scaled_check_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,19 @@ class ScaledCheckBox : public QCheckBox
/// and assumes a 1:1 width:height ratio of the indicator icons.
const QString kIndicatorSizeStylesheet_ = "ScaledCheckBox::indicator { width: %1px; height: %1px; }";

protected:
/// \brief Reimplement changeEvent to handle resizing on fontChange.
/// \param event The event that caused the change.
virtual void changeEvent(QEvent* event) override;

private slots:
/// Handler for ScaleFactorChanged signals.
/// Invalidates font sizes and resizes the checkbox indicator.
void OnScaleFactorChanged();

private:
/// Track if invalidating font metrics so the changeEvent does not become recursive.
bool invalidating_font_metrics_;
};

#endif // QTCOMMON_CUSTOM_WIDGETS_SCALED_CHECKBOX_H_
#endif // QTCOMMON_CUSTOM_WIDGETS_SCALED_CHECKBOX_H_
22 changes: 18 additions & 4 deletions custom_widgets/scaled_push_button.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//=============================================================================
/// Copyright (c) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief Scaling wrapper for QPushButton objects.
/// Copyright (c) 2019-2021 Advanced Micro Devices, Inc. All rights reserved.
/// @author AMD Developer Tools Team
/// @file
/// @brief Scaling wrapper for QPushButton objects.
//=============================================================================
#include "scaled_push_button.h"

Expand All @@ -11,6 +11,7 @@

ScaledPushButton::ScaledPushButton(QWidget* parent)
: QPushButton(parent)
, base_icon_size_(QSize())
{
setSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);

Expand All @@ -30,10 +31,23 @@ ScaledPushButton::~ScaledPushButton()
disconnect(&ScalingManager::Get(), &ScalingManager::ScaleFactorChanged, this, &ScaledPushButton::OnScaleFactorChanged);
}

void ScaledPushButton::setIconSize(const QSize& size)
{
QPushButton::setIconSize(size);

base_icon_size_ = size;
}

void ScaledPushButton::OnScaleFactorChanged()
{
QtCommon::QtUtils::InvalidateFontMetrics(this);

if (!icon().isNull() && base_icon_size_.isValid())
{
ScalingManager& sm = ScalingManager::Get();
QPushButton::setIconSize(QSize(sm.Scaled(base_icon_size_.width()), sm.Scaled(base_icon_size_.height())));
}

if (parentWidget() != nullptr && parentWidget()->layout() != nullptr)
{
// If there is a parent widget and the parent has a layout,
Expand Down
34 changes: 22 additions & 12 deletions custom_widgets/scaled_push_button.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
//=============================================================================
/// Copyright (c) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief Scaling wrapper for QPushButton object.
/// Copyright (c) 2019-2021 Advanced Micro Devices, Inc. All rights reserved.
/// @author AMD Developer Tools Team
/// @file
/// @brief Scaling wrapper for QPushButton object.
//=============================================================================

#ifndef QTCOMMON_CUSTOM_WIDGETS_SCALED_PUSHBUTTON_H_
#define QTCOMMON_CUSTOM_WIDGETS_SCALED_PUSHBUTTON_H_

#include <QPushButton>

/// Wraps the QPushButton and fixes DPI scaling.
/// @brief Wraps the QPushButton and fixes DPI scaling.
class ScaledPushButton : public QPushButton
{
Q_OBJECT

public:
/// Constructor.
/// \param parent The parent object.
/// @brief Constructor.
///
/// @param parent The parent object.
ScaledPushButton(QWidget* parent = nullptr);

/// Constructor.
/// \param text The button text.
/// \param parent The parent object.
/// @brief Constructor.
///
/// @param text The button text.
/// @param parent The parent object.
ScaledPushButton(const QString& text, QWidget* parent = nullptr);

/// Destructor.
/// @brief Destructor.
virtual ~ScaledPushButton();

/// @brief Overridden setIconSize for the QAbstractButton.
///
/// @param size Maximum size for the icon. Smaller icons will not be scaled up.
void setIconSize(const QSize& size);

private slots:
/// Callback for when the DPI scale factor changes.
/// @brief Callback for when the DPI scale factor changes.
void OnScaleFactorChanged();

private:
QSize base_icon_size_; ///< Base icon size DPI scaling will be calculated from.
};

#endif // QTCOMMON_CUSTOM_WIDGETS_SCALED_PUSHBUTTON_H_
4 changes: 2 additions & 2 deletions custom_widgets/tab_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ QSize TabBar::tabSizeHint(int index) const
// a call to tabwidget->adjustSize() may be needed in a showEvent
// to force the tabwidget to update the tab-bar
//
int tab_widget_width = tab_widget->width();
int x = QTabBar::tabRect(index).x();
int tab_widget_width = tab_widget->geometry().width();
int x = this->mapFromGlobal(QPoint(QTabBar::tabRect(index).x(), 0)).x();
int remaining = qAbs(tab_widget_width - x);

return QSize(remaining, height);
Expand Down
2 changes: 1 addition & 1 deletion custom_widgets/text_search_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TextSearchWidget::TextSearchWidget(QWidget* parent)
setStyleSheet(kTextSearchStylesheet);

// connect text changed signal/slot
connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(HandleTextChanged(const QString&)));
connect(this, &TextSearchWidget::textChanged, this, &TextSearchWidget::HandleTextChanged);

// Set place holder text to search boxes
setPlaceholderText("Search...");
Expand Down
2 changes: 1 addition & 1 deletion utils/qt_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ namespace QtCommon

int QtUtils::GetTextWidth(const QFont& font, const QString& str)
{
return ScalingManager::Get().ScaledFontMetrics(font).width(str);
return ScalingManager::Get().ScaledFontMetrics(font).boundingRect(str).width();
}

int QtUtils::GetPainterTextWidth(QPainter* pPainter, const QString& str)
Expand Down
Loading

0 comments on commit db1e003

Please sign in to comment.