Browse Source

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
adaptive-webui-19844
Nick Tiskov 11 years ago
parent
commit
de3108e1e5
  1. 47
      src/autoexpandabledialog.cpp
  2. 3
      src/autoexpandabledialog.h

47
src/autoexpandabledialog.cpp

@ -52,18 +52,36 @@ QString AutoExpandableDialog::getText(QWidget *parent, const QString &title, con @@ -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 @@ -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);
}

3
src/autoexpandabledialog.h

@ -49,6 +49,9 @@ public: @@ -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;

Loading…
Cancel
Save