Skip to content

Commit

Permalink
Merge pull request #42 from silicon-heaven/save-result-to-file
Browse files Browse the repository at this point in the history
enable save result to file
  • Loading branch information
fvacek authored Feb 2, 2024
2 parents 156dab6 + ed4f7b0 commit fa91bbe
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
10 changes: 8 additions & 2 deletions shvspy/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char *, size_t> 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 {
Expand Down
37 changes: 33 additions & 4 deletions shvspy/src/texteditdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

#include <shv/chainpack/rpcvalue.h>

#include <QSettings>
#include <QFileDialog>
#include <QMessageBox>
#include <QPushButton>
#include <QSettings>
#include <QTimer>

namespace cp = shv::chainpack;
Expand All @@ -20,6 +22,7 @@ TextEditDialog::TextEditDialog(QWidget *parent)
ui->lblError->hide();
ui->btFormatCpon->hide();
ui->btCompactCpon->hide();
ui->btSaveToFile->hide();
setReadOnly(false);

QSettings settings;
Expand All @@ -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()
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -79,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;
}
Expand All @@ -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
//=========================================================
Expand Down
3 changes: 3 additions & 0 deletions shvspy/src/texteditdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ class TextEditDialog : public QDialog
~TextEditDialog() override;

void setText(const QString &s);
void setBlob(const QByteArray &s);
QString text() const;

void setReadOnly(bool ro);
protected:
bool eventFilter(QObject *o, QEvent *e) override;
void search();
void searchBack();
void saveToFile();

Ui::TextEditDialog *ui;
QByteArray m_blobData;
};

class CponEditDialog : public TextEditDialog
Expand Down
20 changes: 20 additions & 0 deletions shvspy/src/texteditdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btSaveToFile">
<property name="text">
<string>Save to file</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
Expand Down

0 comments on commit fa91bbe

Please sign in to comment.