From de3108e1e5e3560f715e1ddd344b28a9e66cc0c7 Mon Sep 17 00:00:00 2001 From: Nick Tiskov Date: Mon, 22 Jul 2013 17:11:54 +0400 Subject: [PATCH] Move textBox geometry updates into showEvent: 1. Makes QFontMetrics more accurate (~50%) for custom DPI systems 2. Makes it possible to have fixed dialog size yet again (like in old dialog box) and still allow to autoexpand the textBox --- src/autoexpandabledialog.cpp | 47 +++++++++++++++++++++++++----------- src/autoexpandabledialog.h | 3 +++ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/autoexpandabledialog.cpp b/src/autoexpandabledialog.cpp index 30e39416a..2190b67a3 100644 --- a/src/autoexpandabledialog.cpp +++ b/src/autoexpandabledialog.cpp @@ -52,18 +52,36 @@ QString AutoExpandableDialog::getText(QWidget *parent, const QString &title, con d.ui->textEdit->setEchoMode(mode); d.ui->textEdit->setInputMethodHints(inputMethodHints); - int textW = d.ui->textEdit->fontMetrics().width(text) + 4; + bool res = d.exec(); + if (ok) + *ok = res; + + if (!res) + return QString(); + + return d.ui->textEdit->text(); +} + +void AutoExpandableDialog::showEvent(QShowEvent *e) { + // Overriding showEvent is required for consistent UI with fixed size under custom DPI + // Show dialog + QDialog::showEvent(e); + // and resize textbox to fit the text + + // NOTE: For some strange reason QFontMetrics gets more accurate + // when called from showEvent. Only 6 symbols off instead of 11 symbols off. + int textW = ui->textEdit->fontMetrics().width(ui->textEdit->text()) + 4; int screenW = QApplication::desktop()->width() / 4; int wd = textW; - if (!title.isEmpty()) { - int _w = d.fontMetrics().width(title); + if (!windowTitle().isEmpty()) { + int _w = fontMetrics().width(windowTitle()); if (_w > wd) wd = _w; } - if (!label.isEmpty()) { - int _w = d.ui->textLabel->fontMetrics().width(label); + if (!ui->textLabel->text().isEmpty()) { + int _w = ui->textLabel->fontMetrics().width(ui->textLabel->text()); if (_w > wd) wd = _w; } @@ -75,15 +93,16 @@ QString AutoExpandableDialog::getText(QWidget *parent, const QString &title, con // 2. max width of text from either of: label, title, textedit // If the value is less than dialog default size default size is used wd = textW < screenW ? textW : screenW; - if (wd > d.width()) - d.resize(d.width() - d.ui->horizontalLayout->sizeHint().width() + wd, d.height()); + if (wd > width()) + resize(width() - ui->horizontalLayout->sizeHint().width() + wd, height()); - bool res = d.exec(); - if (ok) - *ok = res; + // Use old dialog behavior: prohibit resizing the dialog + setFixedHeight(height()); - if (!res) - return QString(); - - return d.ui->textEdit->text(); + // Update geometry: center on screen + int sx = QApplication::desktop()->width(); + int sy = QApplication::desktop()->height(); + QRect geom = geometry(); + geom.moveCenter(QPoint(sx / 2, sy / 2)); + setGeometry(geom); } diff --git a/src/autoexpandabledialog.h b/src/autoexpandabledialog.h index 87863d1f8..6391418e5 100644 --- a/src/autoexpandabledialog.h +++ b/src/autoexpandabledialog.h @@ -49,6 +49,9 @@ public: static QString getText(QWidget *parent, const QString& title, const QString& label, QLineEdit::EchoMode mode = QLineEdit::Normal, const QString & text = QString(), bool * ok = 0, Qt::InputMethodHints inputMethodHints = Qt::ImhNone); + +protected: + void showEvent(QShowEvent *e); private: Ui::AutoExpandableDialog *ui;