From d190d3c5e394af9c993b6a883291c88924b3c0ed Mon Sep 17 00:00:00 2001 From: Hannah von Reth Date: Wed, 19 Feb 2020 11:50:10 +0100 Subject: [PATCH] [GUI] Dynamically elide actions Fixes: #7744 --- changelog/unreleased/7744 | 6 ++++++ src/gui/settingsdialog.cpp | 28 +++++++++------------------- 2 files changed, 15 insertions(+), 19 deletions(-) create mode 100644 changelog/unreleased/7744 diff --git a/changelog/unreleased/7744 b/changelog/unreleased/7744 new file mode 100644 index 00000000000..2c44d6c1e17 --- /dev/null +++ b/changelog/unreleased/7744 @@ -0,0 +1,6 @@ +Resize the buttons in the settings view dynamically + +Since Qt 5.12 the button text gets elided automatically +if the button text would exceed the button width. + +https://github.com/owncloud/client/issues/7744 diff --git a/src/gui/settingsdialog.cpp b/src/gui/settingsdialog.cpp index 3b18eacd84f..a954207e8ae 100644 --- a/src/gui/settingsdialog.cpp +++ b/src/gui/settingsdialog.cpp @@ -52,13 +52,12 @@ const QString TOOLBAR_CSS() "QToolBar QToolButton:checked { background: %3; color: %4; }"); } -const float buttonSizeRatio = 1.618f; // golden ratio +const float BUTTONSIZERATIO = 1.618f; // golden ratio /** display name with two lines that is displayed in the settings - * If width is bigger than 0, the string will be ellided so it does not exceed that width */ -QString shortDisplayNameForSettings(OCC::Account *account, int width) +QString shortDisplayNameForSettings(OCC::Account *account) { QString user = account->davDisplayName(); if (user.isEmpty()) { @@ -70,12 +69,6 @@ QString shortDisplayNameForSettings(OCC::Account *account, int width) host.append(QLatin1Char(':')); host.append(QString::number(port)); } - if (width > 0) { - QFont f; - QFontMetrics fm(f); - host = fm.elidedText(host, Qt::ElideMiddle, width); - user = fm.elidedText(user, Qt::ElideRight, width); - } return QStringLiteral("%1\n%2").arg(user, host); } } @@ -134,7 +127,7 @@ SettingsDialog::SettingsDialog(ownCloudGui *gui, QWidget *parent) _ui->stack->addWidget(networkSettings); QWidget *spacer = new QWidget(); - spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum); _toolBar->addWidget(spacer); QAction *quitAction = createColorAwareAction(QLatin1String(":/client/resources/quit.png"), tr("Quit %1").arg(qApp->applicationName())); @@ -248,8 +241,6 @@ void SettingsDialog::showIssuesList(const QString &folderAlias) void SettingsDialog::accountAdded(AccountState *s) { - auto height = _toolBar->sizeHint().height(); - bool brandingSingleAccount = !Theme::instance()->multiAccount(); QAction *accountAction; @@ -265,7 +256,7 @@ void SettingsDialog::accountAdded(AccountState *s) if (!brandingSingleAccount) { accountAction->setToolTip(s->account()->displayName()); - accountAction->setIconText(shortDisplayNameForSettings(s->account().data(), height * buttonSizeRatio)); + accountAction->setIconText(shortDisplayNameForSettings(s->account().data())); } _toolBar->insertAction(_toolBar->actions().at(0), accountAction); auto accountSettings = new AccountSettings(s, this); @@ -310,8 +301,7 @@ void SettingsDialog::slotAccountDisplayNameChanged() if (action) { QString displayName = account->displayName(); action->setText(displayName); - auto height = _toolBar->sizeHint().height(); - action->setIconText(shortDisplayNameForSettings(account, height * buttonSizeRatio)); + action->setIconText(shortDisplayNameForSettings(account)); } } } @@ -400,12 +390,12 @@ class ToolButtonAction : public QWidgetAction return 0; } - QToolButton *btn = new QToolButton(parent); + QToolButton *btn = new QToolButton(toolbar); btn->setDefaultAction(this); btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); - btn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); - // btn->setMinimumWidth(qMax(parent->sizeHint().height() * buttonSizeRatio, - // btn->sizeHint().width())); + btn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding); + // icon size is fixed, we can't use the toolbars actual size hint as it might not be defined yet + btn->setMinimumWidth(toolbar->iconSize().height() * BUTTONSIZERATIO); return btn; } };