diff --git a/LICENSE b/LICENSE index 7d45b10..1434e3a 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/custom_widgets/arrow_icon_combo_box.cpp b/custom_widgets/arrow_icon_combo_box.cpp index e3aa580..b1fa752 100644 --- a/custom_widgets/arrow_icon_combo_box.cpp +++ b/custom_widgets/arrow_icon_combo_box.cpp @@ -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(); + } } } } @@ -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; diff --git a/custom_widgets/check_box_widget.cpp b/custom_widgets/check_box_widget.cpp index 1821fce..da8aaa2 100644 --- a/custom_widgets/check_box_widget.cpp +++ b/custom_widgets/check_box_widget.cpp @@ -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; diff --git a/custom_widgets/colored_legend_scene.cpp b/custom_widgets/colored_legend_scene.cpp index 6400a9f..b0ced2b 100644 --- a/custom_widgets/colored_legend_scene.cpp +++ b/custom_widgets/colored_legend_scene.cpp @@ -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) @@ -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); } diff --git a/custom_widgets/donut_pie_widget.cpp b/custom_widgets/donut_pie_widget.cpp index 6977c43..1308a65 100644 --- a/custom_widgets/donut_pie_widget.cpp +++ b/custom_widgets/donut_pie_widget.cpp @@ -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(); @@ -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); } @@ -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(); diff --git a/custom_widgets/double_slider_widget.cpp b/custom_widgets/double_slider_widget.cpp index 446ce4e..7c0b2ed 100644 --- a/custom_widgets/double_slider_widget.cpp +++ b/custom_widgets/double_slider_widget.cpp @@ -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)); } } @@ -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) { diff --git a/custom_widgets/file_loading_widget.cpp b/custom_widgets/file_loading_widget.cpp index bd56e14..a405e0b 100644 --- a/custom_widgets/file_loading_widget.cpp +++ b/custom_widgets/file_loading_widget.cpp @@ -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)); diff --git a/custom_widgets/icon_button.cpp b/custom_widgets/icon_button.cpp index 94307e3..2dabb5a 100644 --- a/custom_widgets/icon_button.cpp +++ b/custom_widgets/icon_button.cpp @@ -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()) { diff --git a/custom_widgets/list_widget.cpp b/custom_widgets/list_widget.cpp index 813bf0a..e434c5c 100644 --- a/custom_widgets/list_widget.cpp +++ b/custom_widgets/list_widget.cpp @@ -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); } diff --git a/custom_widgets/quick_link_button_widget.cpp b/custom_widgets/quick_link_button_widget.cpp index cd7e79b..1998708 100644 --- a/custom_widgets/quick_link_button_widget.cpp +++ b/custom_widgets/quick_link_button_widget.cpp @@ -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. diff --git a/custom_widgets/scaled_check_box.cpp b/custom_widgets/scaled_check_box.cpp index 22ce946..700fdae 100644 --- a/custom_widgets/scaled_check_box.cpp +++ b/custom_widgets/scaled_check_box.cpp @@ -7,6 +7,7 @@ #include "scaled_check_box.h" #include +#include #include "utils/qt_util.h" #include "utils/scaling_manager.h" @@ -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); @@ -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() @@ -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(); + } + } +} diff --git a/custom_widgets/scaled_check_box.h b/custom_widgets/scaled_check_box.h index d66ddbf..86db10d 100644 --- a/custom_widgets/scaled_check_box.h +++ b/custom_widgets/scaled_check_box.h @@ -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_ \ No newline at end of file +#endif // QTCOMMON_CUSTOM_WIDGETS_SCALED_CHECKBOX_H_ diff --git a/custom_widgets/scaled_push_button.cpp b/custom_widgets/scaled_push_button.cpp index f6e2d6f..412a097 100644 --- a/custom_widgets/scaled_push_button.cpp +++ b/custom_widgets/scaled_push_button.cpp @@ -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" @@ -11,6 +11,7 @@ ScaledPushButton::ScaledPushButton(QWidget* parent) : QPushButton(parent) + , base_icon_size_(QSize()) { setSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed); @@ -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, diff --git a/custom_widgets/scaled_push_button.h b/custom_widgets/scaled_push_button.h index 6ec6425..b38d9d9 100644 --- a/custom_widgets/scaled_push_button.h +++ b/custom_widgets/scaled_push_button.h @@ -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 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_ @@ -10,27 +10,37 @@ #include -/// 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_ diff --git a/custom_widgets/tab_bar.cpp b/custom_widgets/tab_bar.cpp index a399a91..7ea2e26 100644 --- a/custom_widgets/tab_bar.cpp +++ b/custom_widgets/tab_bar.cpp @@ -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); diff --git a/custom_widgets/text_search_widget.cpp b/custom_widgets/text_search_widget.cpp index 0291c2e..ed62fba 100644 --- a/custom_widgets/text_search_widget.cpp +++ b/custom_widgets/text_search_widget.cpp @@ -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..."); diff --git a/utils/qt_util.cpp b/utils/qt_util.cpp index 0b81832..49e5f32 100644 --- a/utils/qt_util.cpp +++ b/utils/qt_util.cpp @@ -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) diff --git a/utils/scaling_manager.h b/utils/scaling_manager.h index 3e23756..0402af9 100644 --- a/utils/scaling_manager.h +++ b/utils/scaling_manager.h @@ -102,10 +102,10 @@ class ScalingManager : public QObject /// An example might be a custom painted widget that should be 50x50 pixels when at 100% DPI /// and needs to get scaled larger at higher DPI scaling values; ie: 50*150% = 75x75 pixels at 150% DPI. /// And when going back down to 100% DPI, the original values can be used: ie: 50* 100% = 50x50 pixels at 100%. - ///< An initial DPI Scale of 200% => m_scaleFactor = 2.0; - ///< Changing DPI Scale to 100% => m_scaleFactor = 1.0; - ///< Changing DPI Scale to 200% => m_scaleFactor = 2.0; - ///< Changing DPI Scale to 300% => m_scaleFactor = 3.0; + ///< An initial DPI Scale of 200% => scale_factor_ = 2.0; + ///< Changing DPI Scale to 100% => scale_factor_ = 1.0; + ///< Changing DPI Scale to 200% => scale_factor_ = 2.0; + ///< Changing DPI Scale to 300% => scale_factor_ = 3.0; double scale_factor_; /// UI rescale factor @@ -116,19 +116,19 @@ class ScalingManager : public QObject /// which would change a 10pt font size into a 15pt font. Thus, calling font.pointSize() would now return 15 instead of 10. /// In order to reduce from 150% back down to 100% DPI scaling, the current value off font.pointSize() needs to get scaled back down. /// The same line of code can be used to rescale down from the current value: font.setPointSize(font.pointSize() * rescale_factor_). - ///< An initial DPI scale of 200% => m_rescaleFactor = 1.0. (ie: there is no previous scale, so nothing changes). - ///< Changing DPI scale to 100% => m_rescaleFactor = 0.5 (ie: half of previous scale). - ///< Changing DPI scale to 200% => m_rescaleFactor = 2.0 (ie: double the previous scale). - ///< Changing DPI scale to 300% => m_rescaleFactor = 1.5 (ie: 1.5x the previous scale). + ///< An initial DPI scale of 200% => rescale_factor_ = 1.0. (ie: there is no previous scale, so nothing changes). + ///< Changing DPI scale to 100% => rescale_factor_ = 0.5 (ie: half of previous scale). + ///< Changing DPI scale to 200% => rescale_factor_ = 2.0 (ie: double the previous scale). + ///< Changing DPI scale to 300% => rescale_factor_ = 1.5 (ie: 1.5x the previous scale). double rescale_factor_; /// Font scale factor. QFontMetrics appears to calculate correctly at whatever the initial DPI scale is when the app is opened. /// Font PointSizes thus need to get scaled relative to that initial DPI. - /// If initial DPI Scale is 100%, then m_fontScaleFactor will always be equal to m_scaleFactor. - /// An initial DPI scale of 200% => m_fontScaleFactor = 1.0. - /// Changing DPI scale to 100% => m_fontScaleFactor = 0.5 (ie: half of initial scale). - /// Changing DPI scale to 200% => m_fontScaleFactor = 1.0 (ie: back at initial scale). - /// Changing DPI scale to 300% => m_fontScaleFactor = 1.5 (ie: 1.5x of initial scale). + /// If initial DPI Scale is 100%, then font_scale_factor_ will always be equal to scale_factor_. + /// An initial DPI scale of 200% => font_scale_factor_ = 1.0. + /// Changing DPI scale to 100% => font_scale_factor_ = 0.5 (ie: half of initial scale). + /// Changing DPI scale to 200% => font_scale_factor_ = 1.0 (ie: back at initial scale). + /// Changing DPI scale to 300% => font_scale_factor_ = 1.5 (ie: 1.5x of initial scale). double font_scale_factor_; /// Current screen DPI