2015-06-06 09:40:08 +08:00
|
|
|
/*
|
2016-11-28 21:33:38 +08:00
|
|
|
Copyright © 2015 by The qTox Project Contributors
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-02-06 19:28:49 +08:00
|
|
|
#include "gui.h"
|
2016-05-12 05:17:36 +08:00
|
|
|
#include "widget.h"
|
2016-12-19 10:26:26 +08:00
|
|
|
#include "src/nexus.h"
|
2015-02-07 02:01:31 +08:00
|
|
|
#include <assert.h>
|
2016-07-30 17:10:49 +08:00
|
|
|
#include <QApplication>
|
2015-02-06 19:28:49 +08:00
|
|
|
#include <QCoreApplication>
|
2015-02-07 02:01:31 +08:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QDialogButtonBox>
|
2015-02-06 19:28:49 +08:00
|
|
|
#include <QInputDialog>
|
2015-02-07 02:01:31 +08:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QPushButton>
|
2015-02-06 19:28:49 +08:00
|
|
|
#include <QThread>
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @class GUI
|
|
|
|
* @brief Abstracts the GUI from the target backend (DesktopGUI, ...)
|
|
|
|
*
|
|
|
|
* All the functions exposed here are thread-safe.
|
|
|
|
* Prefer calling this class to calling a GUI backend directly.
|
|
|
|
*
|
|
|
|
* @fn void GUI::resized()
|
|
|
|
* @brief Emitted when the GUI is resized on supported platforms.
|
|
|
|
*/
|
2016-07-27 06:20:39 +08:00
|
|
|
|
2015-02-06 19:28:49 +08:00
|
|
|
GUI::GUI(QObject *parent) :
|
|
|
|
QObject(parent)
|
|
|
|
{
|
2015-02-07 02:01:31 +08:00
|
|
|
assert(QThread::currentThread() == qApp->thread());
|
|
|
|
assert(Nexus::getDesktopGUI());
|
|
|
|
connect(Nexus::getDesktopGUI(), &Widget::resized, this, &GUI::resized);
|
2015-02-06 19:28:49 +08:00
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Returns the singleton instance.
|
|
|
|
*/
|
2015-02-06 19:28:49 +08:00
|
|
|
GUI& GUI::getInstance()
|
|
|
|
{
|
|
|
|
static GUI gui;
|
|
|
|
return gui;
|
|
|
|
}
|
|
|
|
|
2015-02-07 02:01:31 +08:00
|
|
|
// Implementation of the public clean interface
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Clear the GUI's contact list.
|
|
|
|
*/
|
2015-06-04 22:36:08 +08:00
|
|
|
void GUI::clearContacts()
|
|
|
|
{
|
|
|
|
if (QThread::currentThread() == qApp->thread())
|
|
|
|
getInstance()._clearContacts();
|
|
|
|
else
|
|
|
|
QMetaObject::invokeMethod(&getInstance(), "_clearContacts", Qt::BlockingQueuedConnection);
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Will enable or disable the GUI.
|
|
|
|
* @note A disabled GUI can't be interacted with by the user.
|
|
|
|
* @param state Enable/disable GUI.
|
|
|
|
*/
|
2015-02-07 02:01:31 +08:00
|
|
|
void GUI::setEnabled(bool state)
|
|
|
|
{
|
|
|
|
if (QThread::currentThread() == qApp->thread())
|
|
|
|
{
|
|
|
|
getInstance()._setEnabled(state);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(&getInstance(), "_setEnabled", Qt::BlockingQueuedConnection,
|
|
|
|
Q_ARG(bool, state));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Change the title of the main window.
|
|
|
|
* @param title Titile to set.
|
|
|
|
*
|
|
|
|
* This is usually always visible to the user.
|
|
|
|
*/
|
2015-02-07 02:01:31 +08:00
|
|
|
void GUI::setWindowTitle(const QString& title)
|
|
|
|
{
|
|
|
|
if (QThread::currentThread() == qApp->thread())
|
|
|
|
{
|
|
|
|
getInstance()._setWindowTitle(title);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(&getInstance(), "_setWindowTitle", Qt::BlockingQueuedConnection,
|
|
|
|
Q_ARG(const QString&, title));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Reloads the application theme and redraw the window.
|
|
|
|
*/
|
2015-02-07 02:01:31 +08:00
|
|
|
void GUI::reloadTheme()
|
|
|
|
{
|
|
|
|
if (QThread::currentThread() == qApp->thread())
|
|
|
|
{
|
|
|
|
getInstance()._reloadTheme();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(&getInstance(), "_reloadTheme", Qt::BlockingQueuedConnection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Optionally switches to a view of the qTox update being downloaded.
|
|
|
|
*/
|
2015-12-11 01:46:07 +08:00
|
|
|
void GUI::showUpdateDownloadProgress()
|
|
|
|
{
|
|
|
|
if (QThread::currentThread() == qApp->thread())
|
|
|
|
{
|
|
|
|
getInstance()._showUpdateDownloadProgress();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(&getInstance(), "_showUpdateDownloadProgress", Qt::BlockingQueuedConnection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Show some text to the user.
|
|
|
|
* @param title Title of information window.
|
|
|
|
* @param msg Text in information window.
|
|
|
|
*/
|
2015-02-07 19:46:55 +08:00
|
|
|
void GUI::showInfo(const QString& title, const QString& msg)
|
|
|
|
{
|
|
|
|
if (QThread::currentThread() == qApp->thread())
|
|
|
|
{
|
|
|
|
getInstance()._showInfo(title, msg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(&getInstance(), "_showInfo", Qt::BlockingQueuedConnection,
|
|
|
|
Q_ARG(const QString&, title), Q_ARG(const QString&, msg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Show a warning to the user
|
|
|
|
* @param title Title of warning window.
|
|
|
|
* @param msg Text in warning window.
|
|
|
|
*/
|
2015-02-07 02:01:31 +08:00
|
|
|
void GUI::showWarning(const QString& title, const QString& msg)
|
|
|
|
{
|
|
|
|
if (QThread::currentThread() == qApp->thread())
|
|
|
|
{
|
|
|
|
getInstance()._showWarning(title, msg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(&getInstance(), "_showWarning", Qt::BlockingQueuedConnection,
|
|
|
|
Q_ARG(const QString&, title), Q_ARG(const QString&, msg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Show an error to the user.
|
|
|
|
* @param title Title of error window.
|
|
|
|
* @param msg Text in error window.
|
|
|
|
*/
|
2015-02-07 19:46:55 +08:00
|
|
|
void GUI::showError(const QString& title, const QString& msg)
|
2015-02-07 02:01:31 +08:00
|
|
|
{
|
|
|
|
if (QThread::currentThread() == qApp->thread())
|
|
|
|
{
|
2015-12-17 18:24:01 +08:00
|
|
|
// If the GUI hasn't started yet and we're on the main thread,
|
|
|
|
// we still want to be able to show error messages
|
|
|
|
if (!Nexus::getDesktopGUI())
|
|
|
|
QMessageBox::critical(nullptr, title, msg);
|
|
|
|
else
|
|
|
|
getInstance()._showError(title, msg);
|
2015-02-07 02:01:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-07 19:46:55 +08:00
|
|
|
QMetaObject::invokeMethod(&getInstance(), "_showError", Qt::BlockingQueuedConnection,
|
2015-02-07 02:01:31 +08:00
|
|
|
Q_ARG(const QString&, title), Q_ARG(const QString&, msg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Asks the user a question with Ok/Cancel or Yes/No buttons.
|
|
|
|
* @param title Title of question window.
|
|
|
|
* @param msg Text in question window.
|
|
|
|
* @param defaultAns If is true, default was positive answer. Negative otherwise.
|
|
|
|
* @param warning If is true, we will use a special warning style.
|
|
|
|
* @param yesno Show "Yes" and "No" buttons.
|
|
|
|
* @return True if the answer is positive, false otherwise.
|
|
|
|
*/
|
2015-02-07 02:01:31 +08:00
|
|
|
bool GUI::askQuestion(const QString& title, const QString& msg,
|
2015-03-15 01:26:31 +08:00
|
|
|
bool defaultAns, bool warning,
|
|
|
|
bool yesno)
|
2015-02-07 02:01:31 +08:00
|
|
|
{
|
|
|
|
if (QThread::currentThread() == qApp->thread())
|
|
|
|
{
|
2015-02-20 23:36:35 +08:00
|
|
|
return getInstance()._askQuestion(title, msg, defaultAns, warning, yesno);
|
2015-02-07 02:01:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
QMetaObject::invokeMethod(&getInstance(), "_askQuestion", Qt::BlockingQueuedConnection,
|
|
|
|
Q_RETURN_ARG(bool, ret),
|
|
|
|
Q_ARG(const QString&, title), Q_ARG(const QString&, msg),
|
2015-02-20 23:36:35 +08:00
|
|
|
Q_ARG(bool, defaultAns), Q_ARG(bool, warning),
|
|
|
|
Q_ARG(bool, yesno));
|
2015-02-07 02:01:31 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Asks the user a question.
|
|
|
|
*
|
|
|
|
* The text for the displayed buttons can be specified.
|
|
|
|
* @param title Title of question window.
|
|
|
|
* @param msg Text in question window.
|
|
|
|
* @param button1 Text of positive button.
|
|
|
|
* @param button2 Text of negative button.
|
|
|
|
* @param defaultAns If is true, default was positive answer. Negative otherwise.
|
|
|
|
* @param warning If is true, we will use a special warning style.
|
|
|
|
* @return True if the answer is positive, false otherwise.
|
|
|
|
*/
|
2015-03-15 01:26:31 +08:00
|
|
|
bool GUI::askQuestion(const QString& title, const QString& msg,
|
|
|
|
const QString& button1, const QString& button2,
|
|
|
|
bool defaultAns, bool warning)
|
|
|
|
{
|
|
|
|
if (QThread::currentThread() == qApp->thread())
|
|
|
|
{
|
|
|
|
return getInstance()._askQuestion(title, msg, button1, button2, defaultAns, warning);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
QMetaObject::invokeMethod(&getInstance(), "_askQuestion", Qt::BlockingQueuedConnection,
|
|
|
|
Q_RETURN_ARG(bool, ret),
|
|
|
|
Q_ARG(const QString&, title), Q_ARG(const QString&, msg),
|
|
|
|
Q_ARG(bool, defaultAns), Q_ARG(bool, warning));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Asks the user to input text and returns the answer.
|
|
|
|
*
|
|
|
|
* The interface is equivalent to QInputDialog::getItem()
|
|
|
|
* @param parent Is the dialog's parent widget
|
|
|
|
* @param title Is the text which is displayed in the title bar of the dialog.
|
|
|
|
* @param label Is the text which is shown to the user (it should say what should be entered).
|
|
|
|
* @param items Is the string list which is inserted into the combobox.
|
|
|
|
* @param current Is the number of the item which should be the current item.
|
|
|
|
* @param editable If is true the user can enter their own text, otherwise the user may only select one of the existing items.
|
|
|
|
* @param ok If is nonnull will be set to true if the user pressed OK and to false if the user pressed Cancel.
|
|
|
|
* @param flags The dialog will uses to widget flags.
|
|
|
|
* @param hints Is the input method hints that will be used if the combobox is editable and an input method is active.
|
|
|
|
* @return This function returns the text of the current item, or if editable is true, the current text of the combobox.
|
|
|
|
*/
|
2017-01-06 19:02:54 +08:00
|
|
|
QString GUI::itemInputDialog(QWidget* parent, const QString& title,
|
|
|
|
const QString& label, const QStringList& items,
|
|
|
|
int current, bool editable, bool* ok,
|
|
|
|
Qt::WindowFlags flags, Qt::InputMethodHints hints)
|
2015-02-06 19:28:49 +08:00
|
|
|
{
|
|
|
|
if (QThread::currentThread() == qApp->thread())
|
|
|
|
{
|
|
|
|
return getInstance()._itemInputDialog(parent, title, label, items, current, editable, ok, flags, hints);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QString r;
|
|
|
|
QMetaObject::invokeMethod(&getInstance(), "_itemInputDialog", Qt::BlockingQueuedConnection,
|
|
|
|
Q_RETURN_ARG(QString, r),
|
|
|
|
Q_ARG(QWidget*, parent), Q_ARG(const QString&, title),
|
|
|
|
Q_ARG(const QString&,label), Q_ARG(const QStringList&, items),
|
|
|
|
Q_ARG(int, current), Q_ARG(bool, editable), Q_ARG(bool*, ok),
|
|
|
|
Q_ARG(Qt::WindowFlags, flags), Q_ARG(Qt::InputMethodHints, hints));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Asks the user to answer a password.
|
|
|
|
* @param cancel Is the text on the cancel button.
|
|
|
|
* @param body Is descriptive text that will be shown to the user.
|
|
|
|
* @return Entered password.
|
|
|
|
*/
|
2015-02-07 02:01:31 +08:00
|
|
|
QString GUI::passwordDialog(const QString& cancel, const QString& body)
|
|
|
|
{
|
|
|
|
if (QThread::currentThread() == qApp->thread())
|
|
|
|
{
|
|
|
|
return getInstance()._passwordDialog(cancel, body);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QString r;
|
|
|
|
QMetaObject::invokeMethod(&getInstance(), "_passwordDialog", Qt::BlockingQueuedConnection,
|
|
|
|
Q_RETURN_ARG(QString, r),
|
|
|
|
Q_ARG(const QString&, cancel), Q_ARG(const QString&, body));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private implementations
|
|
|
|
|
2015-06-04 22:36:08 +08:00
|
|
|
void GUI::_clearContacts()
|
|
|
|
{
|
|
|
|
Nexus::getDesktopGUI()->clearContactsList();
|
|
|
|
}
|
|
|
|
|
2015-02-07 02:01:31 +08:00
|
|
|
void GUI::_setEnabled(bool state)
|
|
|
|
{
|
|
|
|
Nexus::getDesktopGUI()->setEnabled(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::_setWindowTitle(const QString& title)
|
|
|
|
{
|
|
|
|
if (title.isEmpty())
|
|
|
|
getMainWidget()->setWindowTitle("qTox");
|
|
|
|
else
|
2016-07-30 17:10:49 +08:00
|
|
|
getMainWidget()->setWindowTitle("qTox - " + title);
|
2015-02-07 02:01:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::_reloadTheme()
|
|
|
|
{
|
|
|
|
Nexus::getDesktopGUI()->reloadTheme();
|
|
|
|
}
|
|
|
|
|
2015-02-07 19:46:55 +08:00
|
|
|
void GUI::_showInfo(const QString& title, const QString& msg)
|
|
|
|
{
|
2016-07-30 17:10:49 +08:00
|
|
|
QMessageBox messageBox(QMessageBox::Information, title, msg, QMessageBox::Ok, getMainWidget());
|
|
|
|
messageBox.setButtonText(QMessageBox::Ok, QApplication::tr("Ok"));
|
|
|
|
messageBox.exec();
|
2015-02-07 19:46:55 +08:00
|
|
|
}
|
|
|
|
|
2015-02-07 02:01:31 +08:00
|
|
|
void GUI::_showWarning(const QString& title, const QString& msg)
|
|
|
|
{
|
2016-07-30 17:10:49 +08:00
|
|
|
QMessageBox messageBox(QMessageBox::Warning, title, msg, QMessageBox::Ok, getMainWidget());
|
|
|
|
messageBox.setButtonText(QMessageBox::Ok, QApplication::tr("Ok"));
|
|
|
|
messageBox.exec();
|
2015-02-07 02:01:31 +08:00
|
|
|
}
|
|
|
|
|
2015-02-07 19:46:55 +08:00
|
|
|
void GUI::_showError(const QString& title, const QString& msg)
|
2015-02-07 02:01:31 +08:00
|
|
|
{
|
2016-07-30 17:10:49 +08:00
|
|
|
QMessageBox messageBox(QMessageBox::Critical, title, msg, QMessageBox::Ok, getMainWidget());
|
|
|
|
messageBox.setButtonText(QMessageBox::Ok, QApplication::tr("Ok"));
|
|
|
|
messageBox.exec();
|
2015-02-07 02:01:31 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 01:46:07 +08:00
|
|
|
void GUI::_showUpdateDownloadProgress()
|
|
|
|
{
|
|
|
|
Nexus::getDesktopGUI()->showUpdateDownloadProgress();
|
|
|
|
}
|
|
|
|
|
2015-02-07 02:01:31 +08:00
|
|
|
bool GUI::_askQuestion(const QString& title, const QString& msg,
|
2015-03-15 01:26:31 +08:00
|
|
|
bool defaultAns, bool warning,
|
|
|
|
bool yesno)
|
2015-02-07 02:01:31 +08:00
|
|
|
{
|
2016-07-30 17:10:49 +08:00
|
|
|
QString positiveButton = yesno ? QApplication::tr("Yes") : QApplication::tr("Ok");
|
|
|
|
QString negativeButton = yesno ? QApplication::tr("No") : QApplication::tr("Cancel");
|
2015-02-20 23:36:35 +08:00
|
|
|
|
2016-07-30 17:10:49 +08:00
|
|
|
return _askQuestion(title, msg, positiveButton, negativeButton, defaultAns, warning);
|
2015-02-07 02:01:31 +08:00
|
|
|
}
|
|
|
|
|
2015-03-15 01:26:31 +08:00
|
|
|
bool GUI::_askQuestion(const QString& title, const QString& msg,
|
|
|
|
const QString& button1, const QString& button2,
|
|
|
|
bool defaultAns, bool warning)
|
|
|
|
{
|
2016-07-30 17:10:49 +08:00
|
|
|
QMessageBox::Icon icon = warning ? QMessageBox::Warning : QMessageBox::Question;
|
|
|
|
QMessageBox box(icon, title, msg, QMessageBox::NoButton, getMainWidget());
|
2015-03-15 01:26:31 +08:00
|
|
|
QPushButton* pushButton1 = box.addButton(button1, QMessageBox::AcceptRole);
|
|
|
|
QPushButton* pushButton2 = box.addButton(button2, QMessageBox::RejectRole);
|
|
|
|
box.setDefaultButton(defaultAns ? pushButton1 : pushButton2);
|
|
|
|
box.setEscapeButton(pushButton2);
|
|
|
|
|
|
|
|
box.exec();
|
|
|
|
return box.clickedButton() == pushButton1;
|
|
|
|
}
|
|
|
|
|
2017-01-06 19:02:54 +08:00
|
|
|
QString GUI::_itemInputDialog(QWidget* parent, const QString& title,
|
|
|
|
const QString& label, const QStringList& items,
|
|
|
|
int current, bool editable, bool* ok,
|
|
|
|
Qt::WindowFlags flags, Qt::InputMethodHints hints)
|
2015-02-06 19:28:49 +08:00
|
|
|
{
|
|
|
|
return QInputDialog::getItem(parent, title, label, items, current, editable, ok, flags, hints);
|
|
|
|
}
|
2015-02-07 02:01:31 +08:00
|
|
|
|
|
|
|
QString GUI::_passwordDialog(const QString& cancel, const QString& body)
|
|
|
|
{
|
|
|
|
// we use a hack. It is considered that closing the dialog without explicitly clicking
|
|
|
|
// disable history is confusing. But we can't distinguish between clicking the cancel
|
|
|
|
// button and closing the dialog. So instead, we reverse the Ok and Cancel roles,
|
|
|
|
// so that nothing but explicitly clicking disable history closes the dialog
|
|
|
|
QString ret;
|
|
|
|
QInputDialog dialog;
|
|
|
|
dialog.setWindowTitle(tr("Enter your password"));
|
|
|
|
dialog.setOkButtonText(cancel);
|
|
|
|
dialog.setCancelButtonText(tr("Decrypt"));
|
|
|
|
dialog.setInputMode(QInputDialog::TextInput);
|
|
|
|
dialog.setTextEchoMode(QLineEdit::Password);
|
|
|
|
dialog.setLabelText(body);
|
|
|
|
|
|
|
|
// problem with previous hack: the default button is disable history, not decrypt.
|
|
|
|
// use another hack to reverse the default buttons.
|
|
|
|
// http://www.qtcentre.org/threads/49924-Change-property-of-QInputDialog-button
|
|
|
|
QList<QDialogButtonBox*> l = dialog.findChildren<QDialogButtonBox*>();
|
|
|
|
if (!l.isEmpty())
|
|
|
|
{
|
|
|
|
QPushButton* ok = l.first()->button(QDialogButtonBox::Ok);
|
|
|
|
QPushButton* cancel = l.first()->button(QDialogButtonBox::Cancel);
|
|
|
|
if (ok && cancel)
|
|
|
|
{
|
|
|
|
ok->setAutoDefault(false);
|
|
|
|
ok->setDefault(false);
|
2016-07-30 17:10:49 +08:00
|
|
|
ok->setText(QApplication::tr("Ok"));
|
2015-02-07 02:01:31 +08:00
|
|
|
cancel->setAutoDefault(true);
|
|
|
|
cancel->setDefault(true);
|
2016-07-30 17:10:49 +08:00
|
|
|
cancel->setText(QApplication::tr("Cancel"));
|
2015-02-07 02:01:31 +08:00
|
|
|
}
|
|
|
|
else
|
2016-07-30 17:10:49 +08:00
|
|
|
{
|
2015-02-07 02:01:31 +08:00
|
|
|
qWarning() << "PasswordDialog: Missing button!";
|
2016-07-30 17:10:49 +08:00
|
|
|
}
|
2015-02-07 02:01:31 +08:00
|
|
|
}
|
|
|
|
else
|
2016-07-30 17:10:49 +08:00
|
|
|
{
|
2015-02-07 02:01:31 +08:00
|
|
|
qWarning() << "PasswordDialog: No QDialogButtonBox!";
|
2016-07-30 17:10:49 +08:00
|
|
|
}
|
2015-02-07 02:01:31 +08:00
|
|
|
|
|
|
|
// using similar code, set QLabels to wrap
|
|
|
|
for (auto* label : dialog.findChildren<QLabel*>())
|
|
|
|
label->setWordWrap(true);
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int val = dialog.exec();
|
|
|
|
if (val == QDialog::Accepted)
|
|
|
|
return QString();
|
2016-07-30 17:10:49 +08:00
|
|
|
|
|
|
|
ret = dialog.textValue();
|
|
|
|
if (!ret.isEmpty())
|
|
|
|
return ret;
|
|
|
|
|
2015-02-07 02:01:31 +08:00
|
|
|
dialog.setTextValue("");
|
|
|
|
dialog.setLabelText(body + "\n\n" + tr("You must enter a non-empty password:"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Get the main widget.
|
|
|
|
* @return The main QWidget* of the application
|
|
|
|
*/
|
2015-02-07 02:01:31 +08:00
|
|
|
QWidget* GUI::getMainWidget()
|
|
|
|
{
|
|
|
|
QWidget* maingui{nullptr};
|
|
|
|
maingui = Nexus::getDesktopGUI();
|
|
|
|
return maingui;
|
|
|
|
}
|