Set padding-right correctly instead of moving the clear icon

This commit is contained in:
Arnaud Bienner 2014-01-12 20:50:03 +01:00
parent 3838bd7bd7
commit 5c05745985
2 changed files with 21 additions and 18 deletions

View File

@ -81,18 +81,21 @@ void ExtendedEditor::set_reset_button(bool visible) {
void ExtendedEditor::UpdateButtonGeometry() { void ExtendedEditor::UpdateButtonGeometry() {
const int frame_width = widget_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth); const int frame_width = widget_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
const int left = frame_width + 1 + ( int left = frame_width + 1 + (
has_clear_button() ? clear_button_->sizeHint().width() : 0); has_clear_button() ? clear_button_->sizeHint().width() : 0);
const int right = frame_width + 1 + ( int right = frame_width + 1 + (
has_reset_button() ? reset_button_->sizeHint().width() : 0); has_reset_button() ? reset_button_->sizeHint().width() : 0);
if (is_rtl()) {
qSwap(left, right);
}
widget_->setStyleSheet( widget_->setStyleSheet(
QString("QLineEdit { padding-left: %1px; padding-right: %2px; }") QString("QLineEdit { padding-left: %1px; padding-right: %2px; }")
.arg(left).arg(right)); .arg(left).arg(right));
QSize msz = widget_->minimumSizeHint(); QSize msz = widget_->minimumSizeHint();
widget_->setMinimumSize(msz.width() + (clear_button_->sizeHint().width() + frame_width + 1) * 2 + extra_right_padding_, int width = msz.width() + (clear_button_->sizeHint().width() + frame_width + 1) * 2 + extra_right_padding_;
qMax(msz.height(), clear_button_->sizeHint().height() + frame_width * 2 + 2)); int height = qMax(msz.height(), clear_button_->sizeHint().height() + frame_width * 2 + 2);
widget_->setMinimumSize(width, height);
} }
void ExtendedEditor::Paint(QPaintDevice* device) { void ExtendedEditor::Paint(QPaintDevice* device) {
@ -128,12 +131,7 @@ void ExtendedEditor::Resize() {
const int y = (widget_->rect().height() - sz.height()) / 2; const int y = (widget_->rect().height() - sz.height()) / 2;
clear_button_->move(frame_width, y); clear_button_->move(frame_width, y);
reset_button_->move(widget_->width() - frame_width - sz.width() - extra_right_padding_, y);
if (!is_rtl_) {
reset_button_->move(widget_->width() - frame_width - sz.width() - extra_right_padding_, y);
} else {
reset_button_->move((has_clear_button() ? sz.width() + 4 : 0) + frame_width, y);
}
} }
@ -145,6 +143,13 @@ LineEdit::LineEdit(QWidget* parent)
connect(this, SIGNAL(textChanged(QString)), SLOT(text_changed(QString))); connect(this, SIGNAL(textChanged(QString)), SLOT(text_changed(QString)));
} }
void LineEdit::set_rtl(bool rtl) {
if (rtl != is_rtl_) {
is_rtl_ = rtl;
UpdateButtonGeometry();
}
}
void LineEdit::text_changed(const QString& text) { void LineEdit::text_changed(const QString& text) {
if (text.isEmpty()) { if (text.isEmpty()) {
// Consider empty string as LTR // Consider empty string as LTR
@ -154,7 +159,6 @@ void LineEdit::text_changed(const QString& text) {
// compare only the first character // compare only the first character
set_rtl(QString(text.at(0)).isRightToLeft()); set_rtl(QString(text.at(0)).isRightToLeft());
} }
Resize();
} }
void LineEdit::paintEvent(QPaintEvent* e) { void LineEdit::paintEvent(QPaintEvent* e) {

View File

@ -72,8 +72,11 @@ public:
protected: protected:
void Paint(QPaintDevice* device); void Paint(QPaintDevice* device);
void Resize(); void Resize();
// Change the value of the "right to left" (direction) property.
private: // Will recompute whatever is needed (UpdateButtonGeometry, ...) if the value
// changed.
void set_rtl(bool rtl);
bool is_rtl() const { return is_rtl_; }
void UpdateButtonGeometry(); void UpdateButtonGeometry();
protected: protected:
@ -110,10 +113,6 @@ protected:
void paintEvent(QPaintEvent*); void paintEvent(QPaintEvent*);
void resizeEvent(QResizeEvent*); void resizeEvent(QResizeEvent*);
private:
bool is_rtl() const { return is_rtl_; }
void set_rtl(bool rtl) { is_rtl_ = rtl; }
private slots: private slots:
void text_changed(const QString& text); void text_changed(const QString& text);