From 3994b148f5ad39860d5cae86db9dc9d16e8c7f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20=C5=A0pirk?= Date: Fri, 19 Jan 2024 17:30:01 +0100 Subject: [PATCH 1/2] enable save result to file --- shvspy/src/mainwindow.cpp | 10 ++++++++-- shvspy/src/texteditdialog.cpp | 31 ++++++++++++++++++++++++++++++- shvspy/src/texteditdialog.h | 3 +++ shvspy/src/texteditdialog.ui | 20 ++++++++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/shvspy/src/mainwindow.cpp b/shvspy/src/mainwindow.cpp index 42cb7a2..186c89f 100644 --- a/shvspy/src/mainwindow.cpp +++ b/shvspy/src/mainwindow.cpp @@ -420,8 +420,14 @@ void MainWindow::displayValue(const shv::chainpack::RpcValue &rv) view->setAttribute(Qt::WA_DeleteOnClose); view->setWindowIconText(tr("Result")); view->setReadOnly(true); - std::pair data = rv.asData(); - view->setText(QString::fromUtf8(std::get<0>(data), std::get<1>(data))); + auto [blob, size] = rv.asData(); + auto data = QByteArray::fromRawData(blob, size); + if (rv.isString()) { + view->setText(QString::fromUtf8(data)); + } + else { + view->setBlob(data); + } view->show(); } else { diff --git a/shvspy/src/texteditdialog.cpp b/shvspy/src/texteditdialog.cpp index 1421b47..2a1ba68 100644 --- a/shvspy/src/texteditdialog.cpp +++ b/shvspy/src/texteditdialog.cpp @@ -3,8 +3,10 @@ #include -#include +#include +#include #include +#include #include namespace cp = shv::chainpack; @@ -20,6 +22,7 @@ TextEditDialog::TextEditDialog(QWidget *parent) ui->lblError->hide(); ui->btFormatCpon->hide(); ui->btCompactCpon->hide(); + ui->btSaveToFile->hide(); setReadOnly(false); QSettings settings; @@ -33,6 +36,7 @@ TextEditDialog::TextEditDialog(QWidget *parent) connect(ui->closeToolButton, &QToolButton::clicked, ui->searchWidget, &QWidget::hide); connect(ui->nextToolButton, &QToolButton::clicked, this, &TextEditDialog::search); connect(ui->prevToolButton, &QToolButton::clicked, this, &TextEditDialog::searchBack); + connect(ui->btSaveToFile, &QPushButton::clicked, this, &TextEditDialog::saveToFile); } TextEditDialog::~TextEditDialog() @@ -47,6 +51,12 @@ void TextEditDialog::setText(const QString &s) ui->plainTextEdit->setPlainText(s); } +void TextEditDialog::setBlob(const QByteArray &s) +{ + m_blobData = s; + ui->plainTextEdit->setPlainText(QString::fromUtf8(s)); +} + QString TextEditDialog::text() const { return ui->plainTextEdit->toPlainText(); @@ -56,6 +66,7 @@ void TextEditDialog::setReadOnly(bool ro) { ui->plainTextEdit->setReadOnly(ro); ui->btSave->setVisible(!ro); + ui->btSaveToFile->setVisible(ro); } bool TextEditDialog::eventFilter(QObject *o, QEvent *e) @@ -108,6 +119,24 @@ void TextEditDialog::searchBack() ui->plainTextEdit->find(ui->searchEdit->text(), QTextDocument::FindFlag::FindBackward); } +void TextEditDialog::saveToFile() +{ + QString file_name = QFileDialog::getSaveFileName(this, tr("Save to file")); + if (!file_name.isEmpty()) { + QFile f(file_name); + if (!f.open(QFile::WriteOnly)) { + QMessageBox::warning(this, tr("Warning"), tr("Cannot open file ") + file_name); + return; + } + if (!m_blobData.isEmpty()) { + f.write(m_blobData); + } + else { + f.write(text().toUtf8()); + } + } +} + //========================================================= // CponEditDialog //========================================================= diff --git a/shvspy/src/texteditdialog.h b/shvspy/src/texteditdialog.h index f5aafcf..ef0eee3 100644 --- a/shvspy/src/texteditdialog.h +++ b/shvspy/src/texteditdialog.h @@ -18,6 +18,7 @@ class TextEditDialog : public QDialog ~TextEditDialog() override; void setText(const QString &s); + void setBlob(const QByteArray &s); QString text() const; void setReadOnly(bool ro); @@ -25,8 +26,10 @@ class TextEditDialog : public QDialog bool eventFilter(QObject *o, QEvent *e) override; void search(); void searchBack(); + void saveToFile(); Ui::TextEditDialog *ui; + QByteArray m_blobData; }; class CponEditDialog : public TextEditDialog diff --git a/shvspy/src/texteditdialog.ui b/shvspy/src/texteditdialog.ui index 9e791e1..024b660 100644 --- a/shvspy/src/texteditdialog.ui +++ b/shvspy/src/texteditdialog.ui @@ -52,6 +52,26 @@ + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Save to file + + + From ed4f7b056c99bfbafa4372c5456a52a410b14677 Mon Sep 17 00:00:00 2001 From: Fanda Vacek Date: Fri, 2 Feb 2024 21:00:53 +0100 Subject: [PATCH 2/2] Clang tidy fixes --- shvspy/src/texteditdialog.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shvspy/src/texteditdialog.cpp b/shvspy/src/texteditdialog.cpp index 2a1ba68..b463467 100644 --- a/shvspy/src/texteditdialog.cpp +++ b/shvspy/src/texteditdialog.cpp @@ -90,16 +90,16 @@ bool TextEditDialog::eventFilter(QObject *o, QEvent *e) search(); return true; } - else if (ke->key() == Qt::Key_Escape && ke->modifiers() == Qt::NoModifier) { + if (ke->key() == Qt::Key_Escape && ke->modifiers() == Qt::NoModifier) { ui->searchWidget->hide(); return true; } - else if (ke->key() == Qt::Key_F3) { + if (ke->key() == Qt::Key_F3) { if (ke->modifiers() == Qt::NoModifier) { search(); return true; } - else if (ke->modifiers() == Qt::SHIFT) { + if (ke->modifiers() == Qt::SHIFT) { searchBack(); return true; }