Commit 6b0b8c24 authored by Alessandro Portale's avatar Alessandro Portale

Utils: Let FancyLineEdit have QIcons instead of QPixmaps

A QIcon contains variants for different resolutions. This change
makes sure that in a Multi-screen/mixed-scaling-mode scenario,
the right of those resolutions is chosen, depending on which screen
the line edit currently is.

Task-number: QTCREATORBUG-18869
Change-Id: I8775e9e2c9b15c2863e9415751e1306f9e43c9aa
Reviewed-by: Thomas Hartmann's avatarThomas Hartmann <thomas.hartmann@qt.io>
Reviewed-by: Eike Ziller's avatarEike Ziller <eike.ziller@qt.io>
parent f4ff4200
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include <QStylePainter> #include <QStylePainter>
#include <QPropertyAnimation> #include <QPropertyAnimation>
#include <QStyle> #include <QStyle>
#include <QWindow>
/*! /*!
\class Utils::FancyLineEdit \class Utils::FancyLineEdit
...@@ -91,7 +92,6 @@ public: ...@@ -91,7 +92,6 @@ public:
HistoryCompleter *m_historyCompleter = 0; HistoryCompleter *m_historyCompleter = 0;
FancyLineEdit::ValidationFunction m_validationFunction = &FancyLineEdit::validateWithValidator; FancyLineEdit::ValidationFunction m_validationFunction = &FancyLineEdit::validateWithValidator;
QString m_oldText; QString m_oldText;
QPixmap m_pixmap[2];
QMenu *m_menu[2]; QMenu *m_menu[2];
FancyLineEdit::State m_state = FancyLineEdit::Invalid; FancyLineEdit::State m_state = FancyLineEdit::Invalid;
bool m_menuTabFocusTrigger[2]; bool m_menuTabFocusTrigger[2];
...@@ -256,17 +256,17 @@ void FancyLineEdit::resizeEvent(QResizeEvent *) ...@@ -256,17 +256,17 @@ void FancyLineEdit::resizeEvent(QResizeEvent *)
updateButtonPositions(); updateButtonPositions();
} }
void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap) void FancyLineEdit::setButtonIcon(Side side, const QIcon &icon)
{ {
d->m_iconbutton[side]->setPixmap(buttonPixmap); d->m_iconbutton[side]->setIcon(icon);
updateMargins(); updateMargins();
updateButtonPositions(); updateButtonPositions();
update(); update();
} }
QPixmap FancyLineEdit::buttonPixmap(Side side) const QIcon FancyLineEdit::buttonIcon(Side side) const
{ {
return d->m_pixmap[side]; return d->m_iconbutton[side]->icon();
} }
void FancyLineEdit::setButtonMenu(Side side, QMenu *buttonMenu) void FancyLineEdit::setButtonMenu(Side side, QMenu *buttonMenu)
...@@ -360,9 +360,9 @@ void FancyLineEdit::setFiltering(bool on) ...@@ -360,9 +360,9 @@ void FancyLineEdit::setFiltering(bool on)
QLatin1String("edit-clear-locationbar-rtl") : QLatin1String("edit-clear-locationbar-rtl") :
QLatin1String("edit-clear-locationbar-ltr"), QLatin1String("edit-clear-locationbar-ltr"),
QIcon::fromTheme(QLatin1String("edit-clear"), QIcon::fromTheme(QLatin1String("edit-clear"),
Icons::EDIT_CLEAR.pixmap())); Icons::EDIT_CLEAR.icon()));
setButtonPixmap(Right, icon.pixmap(16)); setButtonIcon(Right, icon);
setButtonVisible(Right, true); setButtonVisible(Right, true);
setPlaceholderText(tr("Filter")); setPlaceholderText(tr("Filter"));
setButtonToolTip(Right, tr("Clear text")); setButtonToolTip(Right, tr("Clear text"));
...@@ -517,15 +517,16 @@ IconButton::IconButton(QWidget *parent) ...@@ -517,15 +517,16 @@ IconButton::IconButton(QWidget *parent)
void IconButton::paintEvent(QPaintEvent *) void IconButton::paintEvent(QPaintEvent *)
{ {
const qreal pixmapRatio = m_pixmap.devicePixelRatio(); QWindow *window = this->window()->windowHandle();
const QPixmap iconPixmap = icon().pixmap(window, sizeHint());
QStylePainter painter(this); QStylePainter painter(this);
QRect pixmapRect = QRect(0, 0, m_pixmap.width()/pixmapRatio, m_pixmap.height()/pixmapRatio); QRect pixmapRect(QPoint(), iconPixmap.size() / window->devicePixelRatio());
pixmapRect.moveCenter(rect().center()); pixmapRect.moveCenter(rect().center());
if (m_autoHide) if (m_autoHide)
painter.setOpacity(m_iconOpacity); painter.setOpacity(m_iconOpacity);
painter.drawPixmap(pixmapRect, m_pixmap); painter.drawPixmap(pixmapRect, iconPixmap);
if (hasFocus()) { if (hasFocus()) {
QStyleOptionFocusRect focusOption; QStyleOptionFocusRect focusOption;
...@@ -550,8 +551,8 @@ void IconButton::animateShow(bool visible) ...@@ -550,8 +551,8 @@ void IconButton::animateShow(bool visible)
QSize IconButton::sizeHint() const QSize IconButton::sizeHint() const
{ {
const qreal pixmapRatio = m_pixmap.devicePixelRatio(); QWindow *window = this->window()->windowHandle();
return QSize(m_pixmap.width()/pixmapRatio, m_pixmap.height()/pixmapRatio); return icon().actualSize(window, QSize(32, 16)); // Find flags icon can be wider than 16px
} }
void IconButton::keyPressEvent(QKeyEvent *ke) void IconButton::keyPressEvent(QKeyEvent *ke)
......
...@@ -45,12 +45,9 @@ class QTCREATOR_UTILS_EXPORT IconButton: public QAbstractButton ...@@ -45,12 +45,9 @@ class QTCREATOR_UTILS_EXPORT IconButton: public QAbstractButton
Q_OBJECT Q_OBJECT
Q_PROPERTY(float iconOpacity READ iconOpacity WRITE setIconOpacity) Q_PROPERTY(float iconOpacity READ iconOpacity WRITE setIconOpacity)
Q_PROPERTY(bool autoHide READ hasAutoHide WRITE setAutoHide) Q_PROPERTY(bool autoHide READ hasAutoHide WRITE setAutoHide)
Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap)
public: public:
explicit IconButton(QWidget *parent = 0); explicit IconButton(QWidget *parent = 0);
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event);
void setPixmap(const QPixmap &pixmap) { m_pixmap = pixmap; update(); }
QPixmap pixmap() const { return m_pixmap; }
float iconOpacity() { return m_iconOpacity; } float iconOpacity() { return m_iconOpacity; }
void setIconOpacity(float value) { m_iconOpacity = value; update(); } void setIconOpacity(float value) { m_iconOpacity = value; update(); }
void animateShow(bool visible); void animateShow(bool visible);
...@@ -67,7 +64,7 @@ protected: ...@@ -67,7 +64,7 @@ protected:
private: private:
float m_iconOpacity; float m_iconOpacity;
bool m_autoHide; bool m_autoHide;
QPixmap m_pixmap; QIcon m_icon;
}; };
class QTCREATOR_UTILS_EXPORT FancyLineEdit : public CompletingLineEdit class QTCREATOR_UTILS_EXPORT FancyLineEdit : public CompletingLineEdit
...@@ -86,8 +83,8 @@ public: ...@@ -86,8 +83,8 @@ public:
explicit FancyLineEdit(QWidget *parent = 0); explicit FancyLineEdit(QWidget *parent = 0);
~FancyLineEdit(); ~FancyLineEdit();
QPixmap buttonPixmap(Side side) const; QIcon buttonIcon(Side side) const;
void setButtonPixmap(Side side, const QPixmap &pixmap); void setButtonIcon(Side side, const QIcon &icon);
QMenu *buttonMenu(Side side) const; QMenu *buttonMenu(Side side) const;
void setButtonMenu(Side side, QMenu *menu); void setButtonMenu(Side side, QMenu *menu);
......
...@@ -666,11 +666,11 @@ void FindToolBar::updateIcons() ...@@ -666,11 +666,11 @@ void FindToolBar::updateIcons()
bool regexp = effectiveFlags & FindRegularExpression; bool regexp = effectiveFlags & FindRegularExpression;
bool preserveCase = effectiveFlags & FindPreserveCase; bool preserveCase = effectiveFlags & FindPreserveCase;
if (!casesensitive && !wholewords && !regexp && !preserveCase) { if (!casesensitive && !wholewords && !regexp && !preserveCase) {
const QPixmap pixmap = Utils::Icons::MAGNIFIER.pixmap(); const QIcon icon = Utils::Icons::MAGNIFIER.icon();
m_ui.findEdit->setButtonPixmap(Utils::FancyLineEdit::Left, pixmap); m_ui.findEdit->setButtonIcon(Utils::FancyLineEdit::Left, icon);
} else { } else {
m_ui.findEdit->setButtonPixmap(Utils::FancyLineEdit::Left, m_ui.findEdit->setButtonIcon(Utils::FancyLineEdit::Left,
IFindFilter::pixmapForFindFlags(effectiveFlags)); IFindFilter::pixmapForFindFlags(effectiveFlags));
} }
} }
......
...@@ -539,9 +539,9 @@ LocatorWidget::LocatorWidget(Locator *locator) : ...@@ -539,9 +539,9 @@ LocatorWidget::LocatorWidget(Locator *locator) :
layout->setMargin(0); layout->setMargin(0);
layout->addWidget(m_fileLineEdit); layout->addWidget(m_fileLineEdit);
const QPixmap pixmap = Utils::Icons::MAGNIFIER.pixmap(); const QIcon icon = Utils::Icons::MAGNIFIER.icon();
m_fileLineEdit->setFiltering(true); m_fileLineEdit->setFiltering(true);
m_fileLineEdit->setButtonPixmap(Utils::FancyLineEdit::Left, pixmap); m_fileLineEdit->setButtonIcon(Utils::FancyLineEdit::Left, icon);
m_fileLineEdit->setButtonToolTip(Utils::FancyLineEdit::Left, tr("Options")); m_fileLineEdit->setButtonToolTip(Utils::FancyLineEdit::Left, tr("Options"));
m_fileLineEdit->setFocusPolicy(Qt::ClickFocus); m_fileLineEdit->setFocusPolicy(Qt::ClickFocus);
m_fileLineEdit->setButtonVisible(Utils::FancyLineEdit::Left, true); m_fileLineEdit->setButtonVisible(Utils::FancyLineEdit::Left, true);
......
...@@ -107,7 +107,7 @@ public: ...@@ -107,7 +107,7 @@ public:
void createIconButton() void createIconButton()
{ {
m_iconButton = new IconButton; m_iconButton = new IconButton;
m_iconButton->setPixmap(Utils::Icons::REPLACE.pixmap()); m_iconButton->setIcon(Utils::Icons::REPLACE.icon());
m_iconButton->setToolTip(VariableChooser::tr("Insert Variable")); m_iconButton->setToolTip(VariableChooser::tr("Insert Variable"));
m_iconButton->hide(); m_iconButton->hide();
connect(m_iconButton.data(), static_cast<void(QAbstractButton::*)(bool)>(&QAbstractButton::clicked), connect(m_iconButton.data(), static_cast<void(QAbstractButton::*)(bool)>(&QAbstractButton::clicked),
...@@ -457,11 +457,7 @@ void VariableChooserPrivate::updateDescription(const QModelIndex &index) ...@@ -457,11 +457,7 @@ void VariableChooserPrivate::updateDescription(const QModelIndex &index)
*/ */
int VariableChooserPrivate::buttonMargin() const int VariableChooserPrivate::buttonMargin() const
{ {
int margin = m_iconButton->pixmap().width() + 8; return 24;
if (q->style()->inherits("OxygenStyle"))
margin = qMax(24, margin);
return margin;
} }
void VariableChooserPrivate::updateButtonGeometry() void VariableChooserPrivate::updateButtonGeometry()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment