qBittorrent
autoexpandabledialog.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2013 Nick Tiskov <[email protected]>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL". If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #include "autoexpandabledialog.h"
30 
31 #include "base/utils/fs.h"
32 #include "ui_autoexpandabledialog.h"
33 #include "utils.h"
34 
36  : QDialog(parent)
37  , m_ui(new Ui::AutoExpandableDialog)
38 {
39  m_ui->setupUi(this);
40 }
41 
43 {
44  delete m_ui;
45 }
46 
47 QString AutoExpandableDialog::getText(QWidget *parent, const QString &title, const QString &label,
48  QLineEdit::EchoMode mode, const QString &text,
49  bool *ok, const bool excludeExtension, Qt::InputMethodHints inputMethodHints)
50 {
51  AutoExpandableDialog d(parent);
52  d.setWindowTitle(title);
53  d.m_ui->textLabel->setText(label);
54  d.m_ui->textEdit->setText(text);
55  d.m_ui->textEdit->setEchoMode(mode);
56  d.m_ui->textEdit->setInputMethodHints(inputMethodHints);
57 
58  d.m_ui->textEdit->selectAll();
59  if (excludeExtension)
60  {
61  const QString extension = Utils::Fs::fileExtension(text);
62  if (!extension.isEmpty())
63  d.m_ui->textEdit->setSelection(0, (text.length() - extension.length() - 1));
64  }
65 
66  bool res = d.exec();
67  if (ok)
68  *ok = res;
69 
70  if (!res) return {};
71 
72  return d.m_ui->textEdit->text();
73 }
74 
76 {
77  // Overriding showEvent is required for consistent UI with fixed size under custom DPI
78  QDialog::showEvent(e);
79 
80  // Show dialog and resize textbox to fit the text
81  // NOTE: For unknown reason QFontMetrics gets more accurate when called from showEvent.
82  int wd = m_ui->textEdit->fontMetrics().horizontalAdvance(m_ui->textEdit->text()) + 4;
83 
84  if (!windowTitle().isEmpty())
85  {
86  // not really the font metrics in window title, so we enlarge it a bit,
87  // including the small icon and close button width
88  int w = fontMetrics().horizontalAdvance(windowTitle()) * 1.8;
89  wd = std::max(wd, w);
90  }
91 
92  if (!m_ui->textLabel->text().isEmpty())
93  {
94  int w = m_ui->textLabel->fontMetrics().horizontalAdvance(m_ui->textLabel->text());
95  wd = std::max(wd, w);
96  }
97 
98  // Now resize the dialog to fit the contents
99  // max width of text from either of: label, title, textedit
100  // If the value is less than dialog default size, default size is used
101  if (wd > width())
102  {
103  QSize size = {width() - m_ui->verticalLayout->sizeHint().width() + wd, height()};
104  Utils::Gui::resize(this, size);
105  }
106 }
void showEvent(QShowEvent *e) override
AutoExpandableDialog(QWidget *parent)
static QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode=QLineEdit::Normal, const QString &text={}, bool *ok=nullptr, bool excludeExtension=false, Qt::InputMethodHints inputMethodHints=Qt::ImhNone)
Ui::AutoExpandableDialog * m_ui
QString fileExtension(const QString &filename)
Definition: fs.cpp:82
void resize(QWidget *widget, const QSize &newSize={})
Definition: utils.cpp:54