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 @@
#include <QStylePainter>
#include <QPropertyAnimation>
#include <QStyle>
#include <QWindow>
/*!
\class Utils::FancyLineEdit
......@@ -91,7 +92,6 @@ public:
HistoryCompleter *m_historyCompleter = 0;
FancyLineEdit::ValidationFunction m_validationFunction = &FancyLineEdit::validateWithValidator;
QString m_oldText;
QPixmap m_pixmap[2];
QMenu *m_menu[2];
FancyLineEdit::State m_state = FancyLineEdit::Invalid;
bool m_menuTabFocusTrigger[2];
......@@ -256,17 +256,17 @@ void FancyLineEdit::resizeEvent(QResizeEvent *)
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();
updateButtonPositions();
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)
......@@ -360,9 +360,9 @@ void FancyLineEdit::setFiltering(bool on)
QLatin1String("edit-clear-locationbar-rtl") :
QLatin1String("edit-clear-locationbar-ltr"),
QIcon::fromTheme(QLatin1String("edit-clear"),
Icons::EDIT_CLEAR.pixmap()));
Icons::EDIT_CLEAR.icon()));
setButtonPixmap(Right, icon.pixmap(16));
setButtonIcon(Right, icon);
setButtonVisible(Right, true);
setPlaceholderText(tr("Filter"));
setButtonToolTip(Right, tr("Clear text"));
......@@ -517,15 +517,16 @@ IconButton::IconButton(QWidget *parent)
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);
QRect pixmapRect = QRect(0, 0, m_pixmap.width()/pixmapRatio, m_pixmap.height()/pixmapRatio);
QRect pixmapRect(QPoint(), iconPixmap.size() / window->devicePixelRatio());
pixmapRect.moveCenter(rect().center());
if (m_autoHide)
painter.setOpacity(m_iconOpacity);
painter.drawPixmap(pixmapRect, m_pixmap);
painter.drawPixmap(pixmapRect, iconPixmap);
if (hasFocus()) {
QStyleOptionFocusRect focusOption;
......@@ -550,8 +551,8 @@ void IconButton::animateShow(bool visible)
QSize IconButton::sizeHint() const
{
const qreal pixmapRatio = m_pixmap.devicePixelRatio();
return QSize(m_pixmap.width()/pixmapRatio, m_pixmap.height()/pixmapRatio);
QWindow *window = this->window()->windowHandle();
return icon().actualSize(window, QSize(32, 16)); // Find flags icon can be wider than 16px
}
void IconButton::keyPressEvent(QKeyEvent *ke)
......
......@@ -45,12 +45,9 @@ class QTCREATOR_UTILS_EXPORT IconButton: public QAbstractButton
Q_OBJECT
Q_PROPERTY(float iconOpacity READ iconOpacity WRITE setIconOpacity)
Q_PROPERTY(bool autoHide READ hasAutoHide WRITE setAutoHide)
Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap)
public:
explicit IconButton(QWidget *parent = 0);
void paintEvent(QPaintEvent *event);
void setPixmap(const QPixmap &pixmap) { m_pixmap = pixmap; update(); }
QPixmap pixmap() const { return m_pixmap; }
float iconOpacity() { return m_iconOpacity; }
void setIconOpacity(float value) { m_iconOpacity = value; update(); }
void animateShow(bool visible);
......@@ -67,7 +64,7 @@ protected:
private:
float m_iconOpacity;
bool m_autoHide;
QPixmap m_pixmap;
QIcon m_icon;
};
class QTCREATOR_UTILS_EXPORT FancyLineEdit : public CompletingLineEdit
......@@ -86,8 +83,8 @@ public:
explicit FancyLineEdit(QWidget *parent = 0);
~FancyLineEdit();
QPixmap buttonPixmap(Side side) const;
void setButtonPixmap(Side side, const QPixmap &pixmap);
QIcon buttonIcon(Side side) const;
void setButtonIcon(Side side, const QIcon &icon);
QMenu *buttonMenu(Side side) const;
void setButtonMenu(Side side, QMenu *menu);
......
......@@ -666,10 +666,10 @@ void FindToolBar::updateIcons()
bool regexp = effectiveFlags & FindRegularExpression;
bool preserveCase = effectiveFlags & FindPreserveCase;
if (!casesensitive && !wholewords && !regexp && !preserveCase) {
const QPixmap pixmap = Utils::Icons::MAGNIFIER.pixmap();
m_ui.findEdit->setButtonPixmap(Utils::FancyLineEdit::Left, pixmap);
const QIcon icon = Utils::Icons::MAGNIFIER.icon();
m_ui.findEdit->setButtonIcon(Utils::FancyLineEdit::Left, icon);
} else {
m_ui.findEdit->setButtonPixmap(Utils::FancyLineEdit::Left,
m_ui.findEdit->setButtonIcon(Utils::FancyLineEdit::Left,
IFindFilter::pixmapForFindFlags(effectiveFlags));
}
}
......
......@@ -539,9 +539,9 @@ LocatorWidget::LocatorWidget(Locator *locator) :
layout->setMargin(0);
layout->addWidget(m_fileLineEdit);
const QPixmap pixmap = Utils::Icons::MAGNIFIER.pixmap();
const QIcon icon = Utils::Icons::MAGNIFIER.icon();
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->setFocusPolicy(Qt::ClickFocus);
m_fileLineEdit->setButtonVisible(Utils::FancyLineEdit::Left, true);
......
......@@ -107,7 +107,7 @@ public:
void createIconButton()
{
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->hide();
connect(m_iconButton.data(), static_cast<void(QAbstractButton::*)(bool)>(&QAbstractButton::clicked),
......@@ -457,11 +457,7 @@ void VariableChooserPrivate::updateDescription(const QModelIndex &index)
*/
int VariableChooserPrivate::buttonMargin() const
{
int margin = m_iconButton->pixmap().width() + 8;
if (q->style()->inherits("OxygenStyle"))
margin = qMax(24, margin);
return margin;
return 24;
}
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