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
This commit is contained in:
Nick Tiskov 2013-07-22 17:11:54 +04:00
commit de3108e1e5
2 changed files with 36 additions and 14 deletions

View file

@ -52,18 +52,36 @@ QString AutoExpandableDialog::getText(QWidget *parent, const QString &title, con
d.ui->textEdit->setEchoMode(mode); d.ui->textEdit->setEchoMode(mode);
d.ui->textEdit->setInputMethodHints(inputMethodHints); 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 screenW = QApplication::desktop()->width() / 4;
int wd = textW; int wd = textW;
if (!title.isEmpty()) { if (!windowTitle().isEmpty()) {
int _w = d.fontMetrics().width(title); int _w = fontMetrics().width(windowTitle());
if (_w > wd) if (_w > wd)
wd = _w; wd = _w;
} }
if (!label.isEmpty()) { if (!ui->textLabel->text().isEmpty()) {
int _w = d.ui->textLabel->fontMetrics().width(label); int _w = ui->textLabel->fontMetrics().width(ui->textLabel->text());
if (_w > wd) if (_w > wd)
wd = _w; 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 // 2. max width of text from either of: label, title, textedit
// If the value is less than dialog default size default size is used // If the value is less than dialog default size default size is used
wd = textW < screenW ? textW : screenW; wd = textW < screenW ? textW : screenW;
if (wd > d.width()) if (wd > width())
d.resize(d.width() - d.ui->horizontalLayout->sizeHint().width() + wd, d.height()); resize(width() - ui->horizontalLayout->sizeHint().width() + wd, height());
bool res = d.exec(); // Use old dialog behavior: prohibit resizing the dialog
if (ok) setFixedHeight(height());
*ok = res;
if (!res) // Update geometry: center on screen
return QString(); int sx = QApplication::desktop()->width();
int sy = QApplication::desktop()->height();
return d.ui->textEdit->text(); QRect geom = geometry();
geom.moveCenter(QPoint(sx / 2, sy / 2));
setGeometry(geom);
} }

View file

@ -49,6 +49,9 @@ public:
static QString getText(QWidget *parent, const QString& title, const QString& label, static QString getText(QWidget *parent, const QString& title, const QString& label,
QLineEdit::EchoMode mode = QLineEdit::Normal, const QString & text = QString(), QLineEdit::EchoMode mode = QLineEdit::Normal, const QString & text = QString(),
bool * ok = 0, Qt::InputMethodHints inputMethodHints = Qt::ImhNone); bool * ok = 0, Qt::InputMethodHints inputMethodHints = Qt::ImhNone);
protected:
void showEvent(QShowEvent *e);
private: private:
Ui::AutoExpandableDialog *ui; Ui::AutoExpandableDialog *ui;