Commit 663a4d7f authored by Karel Slaný's avatar Karel Slaný
Browse files

Introducing PasteInputDialogue.

parent 4353b319
......@@ -108,7 +108,8 @@ SOURCES += \
src/net/isds_wrapper.cpp \
src/net/isds_session.cpp \
src/net/db_wrapper.cpp \
src/setwrapper.cpp
src/setwrapper.cpp \
src/widgets/paste_input_dialogue.cpp
HEADERS += \
src/accounts.h \
......@@ -136,7 +137,8 @@ HEADERS += \
src/net/isds_wrapper.h \
src/net/isds_session.h \
src/net/db_wrapper.h \
src/setwrapper.h
src/setwrapper.h \
src/widgets/paste_input_dialogue.h
RESOURCES += \
res/qml.qrc \
......
/*
* Copyright (C) 2014-2016 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.
*/
#include <QClipboard>
#include <QDebug>
#include <QDialogButtonBox>
#include <QGuiApplication>
#include <QPlainTextEdit>
#include <QPushButton>
#include "src/widgets/paste_input_dialogue.h"
PasteInputDialogue::PasteInputDialogue(QWidget *parent, Qt::WindowFlags flags)
: QInputDialog(parent, flags),
m_timer(this),
m_pasteButton(Q_NULLPTR)
{
}
PasteInputDialogue::~PasteInputDialogue(void)
{
m_timer.stop();
}
QString PasteInputDialogue::getText(QWidget *parent, const QString &title,
const QString &label, QLineEdit::EchoMode mode, const QString &text,
bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
{
PasteInputDialogue dialogue(parent, flags);
dialogue.setWindowTitle(title);
dialogue.setLabelText(label);
dialogue.setTextValue(text);
dialogue.setTextEchoMode(mode);
dialogue.setInputMethodHints(inputMethodHints);
#if defined Q_OS_ANDROID
//#if 1
dialogue.setVisible(true); /* Constructs the dialogue. */
dialogue.extendDialoguePaste();
#endif /* defined Q_OS_ANDROID */
int ret = dialogue.exec();
if (ok) {
*ok = !!ret;
}
if (ret) {
return dialogue.textValue();
} else {
return QString();
}
}
void PasteInputDialogue::activatePasteButton(void)
{
if (m_pasteButton == Q_NULLPTR) {
return;
}
QClipboard *clipboard = QGuiApplication::clipboard();
if (clipboard == Q_NULLPTR) {
return;
}
/* Activate button only when clipboard contains text. */
m_pasteButton->setEnabled(!clipboard->text().isEmpty());
}
void PasteInputDialogue::paste(void)
{
QLineEdit *lineEdit = Q_NULLPTR;
QPlainTextEdit *plainTextEdit = Q_NULLPTR;
{
QList<QLineEdit *> lineEdits(findChildren<QLineEdit *>());
if (lineEdits.size() == 1) {
lineEdit = lineEdits.at(0);
}
QList<QPlainTextEdit *> plainTextEdits(findChildren<QPlainTextEdit *>());
if (plainTextEdits.size() == 1) {
plainTextEdit = plainTextEdits.at(0);
}
}
if ((lineEdit != Q_NULLPTR) && (plainTextEdit == Q_NULLPTR)) {
lineEdit->paste();
} else if ((lineEdit == Q_NULLPTR) && (plainTextEdit != Q_NULLPTR)) {
plainTextEdit->paste();
}
}
void PasteInputDialogue::extendDialoguePaste(void)
{
QList<QDialogButtonBox *> buttonBoxes(
findChildren<QDialogButtonBox *>());
/* There should be only one button box. */
if (buttonBoxes.size() != 1) {
return;
}
QDialogButtonBox *buttonBox = buttonBoxes.at(0);
if (buttonBox == Q_NULLPTR) {
return;
}
m_pasteButton = buttonBox->addButton(
tr("Paste"), QDialogButtonBox::ActionRole);
if (m_pasteButton == Q_NULLPTR) {
return;
}
m_pasteButton->setEnabled(false);
connect(m_pasteButton, SIGNAL(clicked()), this, SLOT(paste()));
connect(&m_timer, SIGNAL(timeout()), this, SLOT(activatePasteButton()));
m_timer.setInterval(500);
m_timer.start();
}
/*
* Copyright (C) 2014-2016 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.
*/
#ifndef _PASTE_INPUT_DIALOGUE_H_
#define _PASTE_INPUT_DIALOGUE_H_
#include <QInputDialog>
#include <QLineEdit>
#include <QString>
#include <QTimer>
/*!
* @brief Text input dialogue with paste button.
*
* @note This class is a nasty hack to explicitly allow pasting text from
* clipboard on Android devices.
*/
class PasteInputDialogue : public QInputDialog {
Q_OBJECT
private:
/*!
* @brief Private constructor.
*
* @param[in] parent Parent widget.
* @param[in] flags Flags s used in QInputDialog.
*/
PasteInputDialogue(QWidget *parent = Q_NULLPTR,
Qt::WindowFlags flags = Qt::WindowFlags());
public:
/*!
* @brief Destructor.
*/
~PasteInputDialogue(void);
/*!
* @brief Generates a text edit dialogue. Paste button is added on
* Android devices.
*/
static QString getText(QWidget *parent, const QString &title,
const QString &label, QLineEdit::EchoMode mode = QLineEdit::Normal,
const QString &text = QString(), bool *ok = Q_NULLPTR,
Qt::WindowFlags flags = Qt::WindowFlags(),
Qt::InputMethodHints inputMethodHints = Qt::ImhNone);
private slots:
/*!
* @brief Activates pasting button according to clipboard content.
*/
void activatePasteButton(void);
/*!
* @brief Pastes clipboard content into text field.
*/
void paste(void);
private:
/*!
* @brief This method adds the paste button.
*
* @note Must be called after the whole dialogue is constructed.
*/
void extendDialoguePaste(void);
QTimer m_timer; /*!< Timer used to check clipboard and activate button. */
QPushButton *m_pasteButton; /*!< Convenience pointer to paste button. */
};
#endif /* _PASTE_INPUT_DIALOGUE_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment