Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / autoexpandabledialog.cpp
blob1d98f9d85be28676ad3ddf834417749f2506b51d
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/path.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);
41 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
42 connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
45 AutoExpandableDialog::~AutoExpandableDialog()
47 delete m_ui;
50 QString AutoExpandableDialog::getText(QWidget *parent, const QString &title, const QString &label,
51 QLineEdit::EchoMode mode, const QString &text,
52 bool *ok, const bool excludeExtension, Qt::InputMethodHints inputMethodHints)
54 AutoExpandableDialog d(parent);
55 d.setWindowTitle(title);
56 d.m_ui->textLabel->setText(label);
57 d.m_ui->textEdit->setText(text);
58 d.m_ui->textEdit->setEchoMode(mode);
59 d.m_ui->textEdit->setInputMethodHints(inputMethodHints);
61 d.m_ui->textEdit->selectAll();
62 if (excludeExtension)
64 const QString extension = Path(text).extension();
65 if (!extension.isEmpty())
66 d.m_ui->textEdit->setSelection(0, (text.length() - extension.length()));
69 bool res = d.exec();
70 if (ok)
71 *ok = res;
73 if (!res) return {};
75 return d.m_ui->textEdit->text();
78 void AutoExpandableDialog::showEvent(QShowEvent *e)
80 // Overriding showEvent is required for consistent UI with fixed size under custom DPI
81 QDialog::showEvent(e);
83 // Show dialog and resize textbox to fit the text
84 // NOTE: For unknown reason QFontMetrics gets more accurate when called from showEvent.
85 int wd = m_ui->textEdit->fontMetrics().horizontalAdvance(m_ui->textEdit->text()) + 4;
87 if (!windowTitle().isEmpty())
89 // not really the font metrics in window title, so we enlarge it a bit,
90 // including the small icon and close button width
91 int w = fontMetrics().horizontalAdvance(windowTitle()) * 1.8;
92 wd = std::max(wd, w);
95 if (!m_ui->textLabel->text().isEmpty())
97 int w = m_ui->textLabel->fontMetrics().horizontalAdvance(m_ui->textLabel->text());
98 wd = std::max(wd, w);
101 // Now resize the dialog to fit the contents
102 // max width of text from either of: label, title, textedit
103 // If the value is less than dialog default size, default size is used
104 if (wd > width())
106 QSize size = {width() - m_ui->verticalLayout->sizeHint().width() + wd, height()};
107 resize(size);