Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / gui / autoexpandabledialog.cpp
blobaf54db1fba116bfdc4b60df9f9e707fb5ecbcb09
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2013 Nick Tiskov <daymansmail@gmail.com>
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.
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.
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.
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.
29 #include "autoexpandabledialog.h"
31 #include "base/utils/fs.h"
32 #include "ui_autoexpandabledialog.h"
33 #include "utils.h"
35 AutoExpandableDialog::AutoExpandableDialog(QWidget *parent)
36 : QDialog(parent)
37 , m_ui(new Ui::AutoExpandableDialog)
39 m_ui->setupUi(this);
42 AutoExpandableDialog::~AutoExpandableDialog()
44 delete m_ui;
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)
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);
58 d.m_ui->textEdit->selectAll();
59 if (excludeExtension)
61 const QString extension = Utils::Fs::fileExtension(text);
62 if (!extension.isEmpty())
63 d.m_ui->textEdit->setSelection(0, (text.length() - extension.length() - 1));
66 bool res = d.exec();
67 if (ok)
68 *ok = res;
70 if (!res) return {};
72 return d.m_ui->textEdit->text();
75 void AutoExpandableDialog::showEvent(QShowEvent *e)
77 // Overriding showEvent is required for consistent UI with fixed size under custom DPI
78 QDialog::showEvent(e);
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;
84 if (!windowTitle().isEmpty())
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);
92 if (!m_ui->textLabel->text().isEmpty())
94 int w = m_ui->textLabel->fontMetrics().horizontalAdvance(m_ui->textLabel->text());
95 wd = std::max(wd, w);
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())
103 QSize size = {width() - m_ui->verticalLayout->sizeHint().width() + wd, height()};
104 Utils::Gui::resize(this, size);