aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorxiphon <xiphon@protonmail.com>2021-01-06 15:40:47 +0000
committerxiphon <xiphon@protonmail.com>2021-01-11 01:35:54 +0000
commite9b894da1601473eca7706acdb2f8cbb8a7fb12b (patch)
treecb9b2cc56d15e8f560f137066c0e962f2e175100 /src/main
parent9399839d96d3b359a939475ce9cfd23fb28df4ec (diff)
downloadmonzero-gui-e9b894da1601473eca7706acdb2f8cbb8a7fb12b.tar.gz
monzero-gui-e9b894da1601473eca7706acdb2f8cbb8a7fb12b.tar.xz
monzero-gui-e9b894da1601473eca7706acdb2f8cbb8a7fb12b.zip
Transfer: implement 'Grab QR code from screen' functionality
Diffstat (limited to 'src/main')
-rw-r--r--src/main/oshelper.cpp60
-rw-r--r--src/main/oshelper.h3
2 files changed, 63 insertions, 0 deletions
diff --git a/src/main/oshelper.cpp b/src/main/oshelper.cpp
index 7584732e..7037e21e 100644
--- a/src/main/oshelper.cpp
+++ b/src/main/oshelper.cpp
@@ -27,10 +27,16 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "oshelper.h"
+
+#include <unordered_set>
+
#include <QCoreApplication>
+#include <QGuiApplication>
#include <QFileDialog>
+#include <QScreen>
#include <QStandardPaths>
#include <QTemporaryFile>
+#include <QWindow>
#include <QDir>
#include <QDebug>
#include <QDesktopServices>
@@ -55,6 +61,40 @@
#include "qt/utils.h"
#endif
+#include "QR-Code-scanner/Decoder.h"
+#include "qt/ScopeGuard.h"
+
+namespace
+{
+
+QPixmap screenshot()
+{
+#ifdef Q_OS_MAC
+ return MacOSHelper::screenshot();
+#else
+ std::unordered_set<QWindow *> hidden;
+ const QWindowList windows = QGuiApplication::allWindows();
+ for (QWindow *window : windows)
+ {
+ if (window->isVisible())
+ {
+ hidden.emplace(window);
+ window->hide();
+ }
+ }
+ const auto unhide = sg::make_scope_guard([&hidden]() {
+ for (QWindow *window : hidden)
+ {
+ window->show();
+ }
+ });
+
+ return QGuiApplication::primaryScreen()->grabWindow(0);
+#endif
+}
+
+} // namespace
+
#if defined(Q_OS_WIN)
bool openFolderAndSelectItem(const QString &filePath)
{
@@ -99,6 +139,26 @@ QString OSHelper::downloadLocation() const
return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
}
+QList<QString> OSHelper::grabQrCodesFromScreen() const
+{
+ QList<QString> codes;
+
+ try
+ {
+ const QImage image = screenshot().toImage();
+ const std::vector<std::string> decoded = QrDecoder().decode(image);
+ std::for_each(decoded.begin(), decoded.end(), [&codes](const std::string &code) {
+ codes.push_back(QString::fromStdString(code));
+ });
+ }
+ catch (const std::exception &e)
+ {
+ qWarning() << e.what();
+ }
+
+ return codes;
+}
+
bool OSHelper::openContainingFolder(const QString &filePath) const
{
QString canonicalFilePath = QFileInfo(filePath).canonicalFilePath();
diff --git a/src/main/oshelper.h b/src/main/oshelper.h
index 96f58073..f4168071 100644
--- a/src/main/oshelper.h
+++ b/src/main/oshelper.h
@@ -29,7 +29,9 @@
#ifndef OSHELPER_H
#define OSHELPER_H
+#include <QList>
#include <QObject>
+#include <QString>
/**
* @brief The OSHelper class - exports to QML some OS-related functions
*/
@@ -43,6 +45,7 @@ public:
Q_INVOKABLE void createDesktopEntry() const;
Q_INVOKABLE QString downloadLocation() const;
+ Q_INVOKABLE QList<QString> grabQrCodesFromScreen() const;
Q_INVOKABLE bool openContainingFolder(const QString &filePath) const;
Q_INVOKABLE QString openSaveFileDialog(const QString &title, const QString &folder, const QString &filename) const;
Q_INVOKABLE QString temporaryFilename() const;