dialogues.cpp 6.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (C) 2014-2017 CZ.NIC
 *
 * This program is free 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.
 *
 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

#if !defined(Q_OS_IOS)
Karel Slaný's avatar
Karel Slaný committed
25
//#define USE_QML_DIALOGUES 1
26 27
#endif /* !defined(Q_OS_IOS) */

28
#include <QQmlEngine> /* qmlRegisterType */
29 30 31 32
#include <QWindow>

#include "src/dialogues/dialogues.h"
#include "src/dialogues/qml_input_dialogue.h"
33
#include "src/dialogues/qml_message_dialogue.h"
34 35 36 37 38 39 40
#include "src/dialogues/widget_input_dialogue.h"
#include "src/dialogues/widget_message_dialogue.h"

#if !defined(USE_QML_DIALOGUES)
/*!
 * @brief Converts echo mode from helper class definition.
 *
41
 * @param[in] echoMode Echo mode as defined in Dialogues.
42 43 44
 * @return Echo mode as defined in QLineEdit.
 */
static
45
QLineEdit::EchoMode toLineEditEchoMode(enum Dialogues::EchoMode echoMode)
46 47
{
	switch (echoMode) {
48
	case Dialogues::EM_NORMAL:
49 50
		return QLineEdit::Normal;
		break;
51
	case Dialogues::EM_PWD:
52
		return QLineEdit::Password;
53
		break;
54
	case Dialogues::EM_NOECHO:
55
		return QLineEdit::NoEcho;
56
		break;
57
	case Dialogues::EM_PWD_ECHOONEDIT:
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
		return QLineEdit::PasswordEchoOnEdit;
		break;
	default:
		Q_ASSERT(0);
		return QLineEdit::Normal;
		break;
	}
}
#endif /* !defined(USE_QML_DIALOGUES) */

/*!
 * @brief Converts dialogue icon from helper class definition.
 *
 * @param[in] icon Icon as used in Dialogues.
 * @return Icon as used in WidgetMessageDialogue.
 */
static
enum QMessageBox::Icon toMessageBoxIcon(enum Dialogues::Icon icon)
{
	switch (icon) {
	case Dialogues::NO_ICON:
		return QMessageBox::NoIcon;
		break;
	case Dialogues::QUESTION:
		return QMessageBox::Question;
		break;
	case Dialogues::INFORMATION:
		return QMessageBox::Information;
		break;
	case Dialogues::WARNING:
		return QMessageBox::Warning;
		break;
	case Dialogues::CRITICAL:
		return QMessageBox::Critical;
		break;
	default:
		Q_ASSERT(0);
		return QMessageBox::NoIcon;
		break;
	}
}

/*!
 * @brief Converts dialogue button from helper class definition.
 *
 * @param[in] button Button as used in Dialogues.
 * @return Button as used in WidgetMessageDialogue.
 */
static
enum QMessageBox::StandardButton toMessageBoxButton(
    enum Dialogues::Button button)
{
	switch (button) {
	case Dialogues::OK:
		return QMessageBox::Ok;
		break;
	case Dialogues::CANCEL:
		return QMessageBox::Cancel;
		break;
	case Dialogues::YES:
		return QMessageBox::Yes;
		break;
	case Dialogues::NO:
		return QMessageBox::No;
		break;
	case Dialogues::NO_BUTTON:
		return QMessageBox::NoButton;
		break;
	default:
		Q_ASSERT(0);
		return QMessageBox::NoButton;
		break;
	}
}

/*!
 * @brief Concerts return value from message dialogue.
 *
 * @param[in] retVal Return value as returned by message dialogue.
 * @return Converted value.
 */
static
int toDialogueRetVal(int retVal)
{
	switch (retVal) {
	case -1:
		return -1;
		break;
	case QMessageBox::Ok:
		return Dialogues::OK;
		break;
	case QMessageBox::Cancel:
		return Dialogues::CANCEL;
		break;
	case QMessageBox::Yes:
		return Dialogues::YES;
		break;
	case QMessageBox::No:
		return Dialogues::NO;
		break;
	case QMessageBox::NoButton:
		return Dialogues::NO_BUTTON;
		break;
	default:
		Q_ASSERT(0);
		return retVal;
		break;
	}
}

/*!
 * @brief Converts dialogue button from helper class definition.
 *
 * @param[in] button Button as used in Dialogues.
 * @return Button as used in WidgetMessageDialogue.
 */
static
QMessageBox::StandardButtons toMessageBoxButtons(Dialogues::Buttons buttons)
{
	QMessageBox::StandardButtons outButtons = QMessageBox::NoButton;

	outButtons |= (buttons & Dialogues::OK) ? QMessageBox::Ok : QMessageBox::NoButton;
	outButtons |= (buttons & Dialogues::CANCEL) ? QMessageBox::Cancel : QMessageBox::NoButton;
	outButtons |= (buttons & Dialogues::YES) ? QMessageBox::Yes : QMessageBox::NoButton;
	outButtons |= (buttons & Dialogues::NO) ? QMessageBox::No : QMessageBox::NoButton;

	return outButtons;
}

187 188 189 190 191 192 193 194
void Dialogues::declareQML(void)
{
	qmlRegisterType<Dialogues>("cz.nic.mobileDatovka.dialogues", 1, 0, "Dialogues");
	qRegisterMetaType<Dialogues::EchoMode>();
	qRegisterMetaType<Dialogues::Icon>();
	qRegisterMetaType<Dialogues::Button>();
}

195
QString Dialogues::getText(QObject *parent, const QString &title,
196 197
    const QString &message, enum EchoMode echoMode, const QString &text,
    const QString &placeholderText, bool *ok,
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    Qt::InputMethodHints inputMethodHints)
{
#if defined(USE_QML_DIALOGUES)
	return QmlInputDialogue::getText(qobject_cast<QWindow *>(parent),
	    title, message, echoMode, text, placeholderText, ok,
	    inputMethodHints);
#else /* !defined(USE_QML_DIALOGUES) */
	Q_UNUSED(placeholderText);
	return WidgetInputDialogue::getText(qobject_cast<QWidget *>(parent),
	    title, message, toLineEditEchoMode(echoMode), text, ok,
	    Qt::Dialog, inputMethodHints);
#endif /* defined(USE_QML_DIALOGUES) */
}

int Dialogues::message(enum Icon icon, const QString &title,
    const QString &text, const QString &infoText,
    Buttons buttons, enum Button dfltButton,
    const QList< QPair<QString, int> > &customButtons, int *selected)
{
	return toDialogueRetVal(
	    WidgetMessageDialogue::message(toMessageBoxIcon(icon), title,
	        text, infoText, toMessageBoxButtons(buttons),
	        toMessageBoxButton(dfltButton), customButtons, selected));
}

void Dialogues::errorMessage(enum Icon icon, const QString &title,
    const QString &text, const QString &infoText)
{
226
#if defined(USE_QML_DIALOGUES)
227 228
	QmlMessageDialogue::errorMessage(Q_NULLPTR, icon, title, text,
	    infoText);
229
#else /* !defined(USE_QML_DIALOGUES) */
230 231
	WidgetMessageDialogue::errorMessage(toMessageBoxIcon(icon), title,
	    text, infoText);
232
#endif /* defined(USE_QML_DIALOGUES) */
233
}