From 3bd9e9856b1b3c5875db0db90258f45107e03977 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 15 Apr 2020 16:15:39 +0200 Subject: [PATCH] GUI: Adding GenerateWindow class --- gui/CMakeLists.txt | 1 + gui/GenerateWindow.cpp | 116 +++++++++++++++++++++++++++++++++++++++++ gui/GenerateWindow.hpp | 62 ++++++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 gui/GenerateWindow.cpp create mode 100644 gui/GenerateWindow.hpp diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt index 7dba68c..7e37389 100644 --- a/gui/CMakeLists.txt +++ b/gui/CMakeLists.txt @@ -14,6 +14,7 @@ set( PROGRAM_EXE ${COMPONENT_NAME} ) set( PROGRAM_SRC main.cpp + GenerateWindow.cpp SearchWindow.cpp MultiSelect.cpp helpers.cpp diff --git a/gui/GenerateWindow.cpp b/gui/GenerateWindow.cpp new file mode 100644 index 0000000..8ab00bb --- /dev/null +++ b/gui/GenerateWindow.cpp @@ -0,0 +1,116 @@ +/** + * MIT License + * + * Copyright (c) 2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include +#include +#include +#include +#include +#include +#include +#include "GenerateWindow.hpp" + +void _initKeyWidget(QLineEdit& w) { + w.setFixedWidth(450); + w.setReadOnly(true); +} + +void _initKeyCopyButton(QPushButton& btn, const QIcon& icon) { + btn.setFixedWidth(32); + btn.setIcon(icon); +} + +GenerateWindow::GenerateWindow(QWidget *parent) : +QWidget (parent), +m_btn_gen ("Generate"), +m_btn_copy_both ("Copy keys") +{ + QIcon copy_icon = QIcon::fromTheme("edit-copy"); + QGridLayout* layout; + + _initKeyWidget(m_pub); + _initKeyWidget(m_priv); + + _initKeyCopyButton(m_btn_copy_pub, copy_icon); + _initKeyCopyButton(m_btn_copy_priv, copy_icon); + + // Layout + layout = new QGridLayout(); + layout->setAlignment(Qt::AlignCenter); + + // Public key row + + layout->addWidget(new QLabel("Public:"), 0, 0); + layout->addWidget(&m_pub, 0, 1); + layout->addWidget(&m_btn_copy_pub, 0, 2); + + // Private key row + + layout->addWidget(new QLabel("Private:"), 1, 0); + layout->addWidget(&m_priv, 1, 1); + layout->addWidget(&m_btn_copy_priv, 1, 2); + + // Bottom row + + m_btn_copy_both.setFixedWidth(80); + + layout->addWidget(&m_btn_gen, 2, 1); + layout->addWidget(&m_btn_copy_both, 2, 2); + + setLayout(layout); + + // Connections + connect(&m_btn_gen, SIGNAL(released()), this, SLOT(generate_key())); + connect(&m_btn_copy_both, SIGNAL(released()), this, SLOT(copy_both_keys())); + connect(&m_btn_copy_pub, SIGNAL(released()), this, SLOT(copy_pub_key())); + connect(&m_btn_copy_priv, SIGNAL(released()), this, SLOT(copy_priv_key())); +} + +void GenerateWindow::generate_key() +{ + std::string pubstr, privstr; + struct libeosio::ec_keypair pair; + + libeosio::ec_generate_key(&pair); + + m_pub.setText(QString::fromStdString(libeosio::wif_pub_encode(pair.pub))); + m_priv.setText(QString::fromStdString(libeosio::wif_priv_encode(pair.secret))); +} + +void GenerateWindow::copy_both_keys() +{ + QClipboard *clipboard = QGuiApplication::clipboard(); + clipboard->setText(m_pub.text() + "\n" + m_priv.text()); +} + +void GenerateWindow::copy_pub_key() +{ + QClipboard *clipboard = QGuiApplication::clipboard(); + clipboard->setText(m_pub.text()); +} + +void GenerateWindow::copy_priv_key() +{ + QClipboard *clipboard = QGuiApplication::clipboard(); + clipboard->setText(m_priv.text()); +} diff --git a/gui/GenerateWindow.hpp b/gui/GenerateWindow.hpp new file mode 100644 index 0000000..da2a110 --- /dev/null +++ b/gui/GenerateWindow.hpp @@ -0,0 +1,62 @@ +/** + * MIT License + * + * Copyright (c) 2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef GENERATE_WINDOW_H +#define GENERATE_WINDOW_H + +#include +#include +#include + +class GenerateWindow : public QWidget +{ + Q_OBJECT +public: + GenerateWindow(QWidget *parent = 0); + +public slots: + + // Genereate a new key. + void generate_key(); + + // Copy both keys to clipboard + void copy_both_keys(); + + // copy public key to clipboard + void copy_pub_key(); + + // copy private key to clipboard + void copy_priv_key(); + +protected: + + QLineEdit m_pub; + QLineEdit m_priv; + + QPushButton m_btn_gen; + QPushButton m_btn_copy_both; + QPushButton m_btn_copy_priv; + QPushButton m_btn_copy_pub; +}; + +#endif /* GENERATE_WINDOW_H */