From f72cb03f0bba74812ab7ec6408c476e8a1f6f458 Mon Sep 17 00:00:00 2001 From: johanneshilden Date: Thu, 12 Oct 2017 09:51:53 +0300 Subject: [PATCH] Update painter code to take into account text alignment --- components/qtmaterialflatbutton.cpp | 8 ++++++-- examples/flatbuttonsettingseditor.cpp | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/components/qtmaterialflatbutton.cpp b/components/qtmaterialflatbutton.cpp index a03f098..b9d84d7 100644 --- a/components/qtmaterialflatbutton.cpp +++ b/components/qtmaterialflatbutton.cpp @@ -694,7 +694,11 @@ void QtMaterialFlatButton::paintForeground(QPainter *painter) } if (icon().isNull()) { - painter->drawText(rect(), Qt::AlignCenter, text()); + if (Qt::AlignLeft == d->textAlignment) { + painter->drawText(rect().adjusted(12, 0, 0, 0), Qt::AlignLeft | Qt::AlignVCenter, text()); + } else { + painter->drawText(rect(), Qt::AlignCenter, text()); + } return; } @@ -702,7 +706,7 @@ void QtMaterialFlatButton::paintForeground(QPainter *painter) QSize base(size()-textSize); const int iw = iconSize().width() + IconPadding; - QPoint pos((base.width()-iw)/2, 0); + QPoint pos(Qt::AlignLeft == d->textAlignment ? 12 : (base.width()-iw)/2, 0); QRect textGeometry(pos + QPoint(0, base.height()/2), textSize); QRect iconGeometry(pos + QPoint(0, (height()-iconSize().height())/2), iconSize()); diff --git a/examples/flatbuttonsettingseditor.cpp b/examples/flatbuttonsettingseditor.cpp index 43fee7d..20e6516 100644 --- a/examples/flatbuttonsettingseditor.cpp +++ b/examples/flatbuttonsettingseditor.cpp @@ -80,6 +80,15 @@ void FlatButtonSettingsEditor::setupForm() break; } + switch (ui->textAlignmentComboBox->currentIndex()) + { + case Qt::AlignLeft: + ui->textAlignmentComboBox->setCurrentIndex(0); + break; + default: + ui->textAlignmentComboBox->setCurrentIndex(1); + } + ui->checkedCheckBox->setEnabled(m_button->isCheckable()); ui->disabledCheckBox->setChecked(!m_button->isEnabled()); @@ -155,6 +164,17 @@ void FlatButtonSettingsEditor::updateWidget() break; } + switch (ui->textAlignmentComboBox->currentIndex()) + { + case 0: + m_button->setTextAlignment(Qt::AlignLeft); + break; + case 1: + default: + m_button->setTextAlignment(Qt::AlignHCenter); + break; + } + m_button->setDisabled(ui->disabledCheckBox->isChecked()); m_button->setCheckable(ui->checkableCheckBox->isChecked()); m_button->setChecked(ui->checkedCheckBox->isChecked()); @@ -213,6 +233,7 @@ void FlatButtonSettingsEditor::applyDefaultPreset() m_button->setHaloVisible(true); m_button->setCheckable(false); m_button->setEnabled(true); + m_button->setTextAlignment(Qt::AlignHCenter); m_button->applyPreset(Material::FlatPreset); setupForm(); } @@ -231,6 +252,7 @@ void FlatButtonSettingsEditor::applyCheckablePreset() m_button->setHaloVisible(true); m_button->setCheckable(true); m_button->setEnabled(true); + m_button->setTextAlignment(Qt::AlignHCenter); m_button->applyPreset(Material::CheckablePreset); setupForm(); } @@ -269,6 +291,7 @@ void FlatButtonSettingsEditor::init() connect(ui->rippleStyleComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateWidget())); connect(ui->hoverStyleComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateWidget())); connect(ui->iconPlacementComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateWidget())); + connect(ui->textAlignmentComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateWidget())); connect(ui->cornerRadiusSpinBox, SIGNAL(valueChanged(int)), this, SLOT(updateWidget())); connect(ui->overlayOpacityDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateWidget())); connect(ui->iconSizeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(updateWidget()));