mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-20 21:33:27 -07:00
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:
parent
fd8a2e05a4
commit
de3108e1e5
2 changed files with 36 additions and 14 deletions
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,9 @@ public:
|
||||||
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;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue