From a085d99b64cb4ef8f601532bb515a296e12bd1ee Mon Sep 17 00:00:00 2001 From: mgrojo Date: Mon, 23 Dec 2019 12:35:13 +0100 Subject: [PATCH] Enhancement: save to temporary file, open external application and reload A new button has been added in "Edit DB Cell" dock, which saves the cell data to a temporary file with extension according to detected data and opens default external application for the file type. It then asks user to reload the data in a dialog when they have finished editing the data. It can be also used for viewing if the user cancels the reload. See related issues #1791 and #1746 --- src/EditDialog.cpp | 74 +++++++++++++++++++++++++++++++++++ src/EditDialog.h | 1 + src/EditDialog.ui | 13 ++++++ src/FileDialog.h | 4 ++ src/icons/application_go.png | Bin 0 -> 634 bytes src/icons/icons.qrc | 1 + 6 files changed, 93 insertions(+) create mode 100644 src/icons/application_go.png diff --git a/src/EditDialog.cpp b/src/EditDialog.cpp index 4ce3f3ad..23ffb318 100644 --- a/src/EditDialog.cpp +++ b/src/EditDialog.cpp @@ -88,6 +88,7 @@ EditDialog::EditDialog(QWidget* parent) return; } }); + connect(ui->actionOpenInExternal, &QAction::triggered, this, &EditDialog::openDataWithExternal); mustIndentAndCompact = Settings::getValue("databrowser", "indent_compact").toBool(); ui->actionIndent->setChecked(mustIndentAndCompact); @@ -1177,3 +1178,76 @@ void EditDialog::setWordWrapping(bool value) sciEdit->setWrapMode(value ? QsciScintilla::WrapWord : QsciScintilla::WrapNone); ui->qtEdit->setWordWrapMode(value ? QTextOption::WrapAtWordBoundaryOrAnywhere : QTextOption::NoWrap); } + +void EditDialog::openDataWithExternal() +{ + QString extension; + QStringList filters; + switch (dataType) { + case Image: { + // Images get special treatment. + // Determine the likely filename extension. + QByteArray cellData = hexEdit->data(); + QBuffer imageBuffer(&cellData); + QImageReader imageReader(&imageBuffer); + extension = imageReader.format().toLower().prepend("."); + break; + } + case Binary: + extension = FILE_EXT_BIN_DEFAULT; + break; + case RtlText: + case Text: + if (ui->comboMode->currentIndex() == XmlEditor) + extension = FILE_EXT_XML_DEFAULT; + else + extension = FILE_EXT_TXT_DEFAULT; + break; + case JSON: + extension = FILE_EXT_JSON_DEFAULT; + break; + case SVG: + extension = FILE_EXT_SVG_DEFAULT; + break; + case XML: + extension = FILE_EXT_XML_DEFAULT; + break; + case Null: + return; + } + QTemporaryFile file (QDir::tempPath() + QString("/DB4S-XXXXXX") + extension); + + if(file.open()) + { + switch (dataSource) { + case HexBuffer: + file.write(hexEdit->data()); + break; + case SciBuffer: + // Data source is the Scintilla buffer + file.write(sciEdit->text().toUtf8()); + break; + case QtBuffer: + // Data source is the text buffer + file.write(ui->qtEdit->toPlainText().toUtf8()); + break; + } + file.close(); + + emit requestUrlOrFileOpen(file.fileName()); + + QMessageBox::StandardButton reply = QMessageBox::information + (nullptr, + QApplication::applicationName(), + tr("The data has been saved to a temporary file and has been opened with the default application." + "You can edit now the file and when your are ready, you can apply the saved new data to the cell or cancel any changes."), + QMessageBox::Apply | QMessageBox::Cancel); + + QFile readFile(file.fileName()); + if(reply == QMessageBox::Apply && readFile.open(QIODevice::ReadOnly)){ + QByteArray d = readFile.readAll(); + loadData(d); + readFile.close(); + } + } +} diff --git a/src/EditDialog.h b/src/EditDialog.h index 35d6264a..7144a6f0 100644 --- a/src/EditDialog.h +++ b/src/EditDialog.h @@ -100,6 +100,7 @@ private: bool promptInvalidData(const QString& data_type, const QString& errorString); void setDataInBuffer(const QByteArray& bArrdata, DataSources source); void setStackCurrentIndex(int editMode); + void openDataWithExternal(); }; #endif diff --git a/src/EditDialog.ui b/src/EditDialog.ui index 466fda33..a1aab0fe 100644 --- a/src/EditDialog.ui +++ b/src/EditDialog.ui @@ -124,6 +124,7 @@ + @@ -448,6 +449,18 @@ Errors are indicated with a red squiggle underline. Opens a file dialog used to import any kind of data to this database cell. + + + + :/icons/open_data_in_app:/icons/open_data_in_app + + + Open in external application + + + Open in external application + + comboMode diff --git a/src/FileDialog.h b/src/FileDialog.h index bc26935a..885f9580 100644 --- a/src/FileDialog.h +++ b/src/FileDialog.h @@ -25,6 +25,7 @@ static const QString FILE_FILTER_ALL(QObject::tr("All Files (*)")); // Text Files Extensions Filter static const QString FILE_FILTER_TXT(QObject::tr("Text Files (*.txt)")); +static const QString FILE_EXT_TXT_DEFAULT(".txt"); // Comma,Tab,or Delimiter-Separated Values File Extensions Filter static const QString FILE_FILTER_CSV(QObject::tr("Comma-Separated Values Files (*.csv)")); @@ -38,12 +39,15 @@ static const QString FILE_EXT_JSON_DEFAULT(".json"); // XML File Extensions Filter static const QString FILE_FILTER_XML(QObject::tr("XML Files (*.xml)")); +static const QString FILE_EXT_XML_DEFAULT(".xml"); // Binary File Extensions Filter static const QString FILE_FILTER_BIN(QObject::tr("Binary Files (*.bin *.dat)")); +static const QString FILE_EXT_BIN_DEFAULT(".bin"); // Scalar Vector Graphics File Extensions Filter static const QString FILE_FILTER_SVG(QObject::tr("SVG Files (*.svg)")); +static const QString FILE_EXT_SVG_DEFAULT(".svg"); // Hex-Dump File Extension Filter static const QString FILE_FILTER_HEX(QObject::tr("Hex Dump Files (*.dat *.bin)")); diff --git a/src/icons/application_go.png b/src/icons/application_go.png new file mode 100644 index 0000000000000000000000000000000000000000..5cc2b0dd36978513f3ca009c68ebc4150976fc80 GIT binary patch literal 634 zcmV-=0)_pFP)cA1hmXWM zDZ#H?v3PJ5$Hq5OmbDN{wJ%9%wAR zT-Qu9!vIN`896F;t~rD&@Nfe0`Nv1rEgogE>hi36i1p_V%rE6ZqX5JdGmz-z3Rm#{ z+Z*c0z*?bg!mefM79{Zs`zK3~u)rkEuD#mHG}dlY@$@R6?<^o~D%1C9AK Udps;mZ~y=R07*qoM6N<$f`qvmMgRZ+ literal 0 HcmV?d00001 diff --git a/src/icons/icons.qrc b/src/icons/icons.qrc index fb409427..b77a8b14 100644 --- a/src/icons/icons.qrc +++ b/src/icons/icons.qrc @@ -100,5 +100,6 @@ style_add.png application_link.png document-link.png + application_go.png