From d7975e8f6acb9ca8ecb25ec177f748e69ce7218b Mon Sep 17 00:00:00 2001 From: Roman Vladimirov Date: Sat, 18 Nov 2023 17:14:56 +0300 Subject: [PATCH] + Added new formatter Plain Text --- src/ArdorQuery.pro | 2 ++ src/Formatters/formatterfactory.cpp | 7 ++++ src/Formatters/plaintextformatter.cpp | 33 +++++++++++++++++++ src/Formatters/plaintextformatter.h | 19 +++++++++++ src/ListModels/outputformatslistmodel.cpp | 2 ++ src/ListModels/responsebodylistmodel.cpp | 2 +- src/ViewModels/httprequestresultviewmodel.cpp | 16 +++++++-- src/Views/HttpResultViewer.qml | 9 +---- src/globalconstants.cpp | 1 + src/globalconstants.h | 1 + 10 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 src/Formatters/plaintextformatter.cpp create mode 100644 src/Formatters/plaintextformatter.h diff --git a/src/ArdorQuery.pro b/src/ArdorQuery.pro index 749aae9..50d3133 100644 --- a/src/ArdorQuery.pro +++ b/src/ArdorQuery.pro @@ -13,6 +13,7 @@ SOURCES += \ Formatters/htmlformatter.cpp \ Formatters/jsonformatter.cpp \ Formatters/outputformatter.cpp \ + Formatters/plaintextformatter.cpp \ Formatters/xmlformatter.cpp \ ListModels/addressespalettelistmodel.cpp \ ListModels/globalvariableslistmodel.cpp \ @@ -68,6 +69,7 @@ HEADERS += \ Formatters/htmlformatter.h \ Formatters/jsonformatter.h \ Formatters/outputformatter.h \ + Formatters/plaintextformatter.h \ Formatters/xmlformatter.h \ ListModels/addressespalettelistmodel.h \ ListModels/globalvariableslistmodel.h \ diff --git a/src/Formatters/formatterfactory.cpp b/src/Formatters/formatterfactory.cpp index f490feb..0273639 100644 --- a/src/Formatters/formatterfactory.cpp +++ b/src/Formatters/formatterfactory.cpp @@ -18,6 +18,7 @@ #include "jsonformatter.h" #include "htmlformatter.h" #include "xmlformatter.h" +#include "plaintextformatter.h" FormatterFactory::FormatterFactory() { @@ -46,5 +47,11 @@ OutputFormatter* FormatterFactory::getFormatter(const QString& formatter) return xmlFormatter; } + if (formatter == OutputFormatPlainText) { + auto plainTextFormatter = new PlainTextFormatter(); + m_instanceCache.insert(OutputFormatPlainText, plainTextFormatter); + return plainTextFormatter; + } + return m_nullableFormatter; } diff --git a/src/Formatters/plaintextformatter.cpp b/src/Formatters/plaintextformatter.cpp new file mode 100644 index 0000000..b7c8793 --- /dev/null +++ b/src/Formatters/plaintextformatter.cpp @@ -0,0 +1,33 @@ +#include "plaintextformatter.h" + +PlainTextFormatter::PlainTextFormatter() +{ + +} + +QString PlainTextFormatter::format(const QString &data) +{ + QString result; + + auto lineWidth = 0; + + for(auto character: data) { + auto latinCharacter = character.toLatin1(); + + if (latinCharacter == m_newline || latinCharacter == m_caretBack) { + lineWidth = 0; + result.append(character); + continue; + } + + if (lineWidth >= 200) { + result.append("\n"); + lineWidth = 0; + } else { + lineWidth += 1; + } + result.append(character); + } + + return result; +} diff --git a/src/Formatters/plaintextformatter.h b/src/Formatters/plaintextformatter.h new file mode 100644 index 0000000..b0987c8 --- /dev/null +++ b/src/Formatters/plaintextformatter.h @@ -0,0 +1,19 @@ +#ifndef PLAINTEXTFORMATTER_H +#define PLAINTEXTFORMATTER_H + +#include "outputformatter.h" + +class PlainTextFormatter : public OutputFormatter +{ + +private: + const QString m_newline { "\n" }; + const QString m_caretBack { "\r" }; + +public: + PlainTextFormatter(); + + QString format(const QString& data) override; +}; + +#endif // PLAINTEXTFORMATTER_H diff --git a/src/ListModels/outputformatslistmodel.cpp b/src/ListModels/outputformatslistmodel.cpp index 4012d21..00efce2 100644 --- a/src/ListModels/outputformatslistmodel.cpp +++ b/src/ListModels/outputformatslistmodel.cpp @@ -23,6 +23,7 @@ OutputFormatsListModel::OutputFormatsListModel(QObject *parent) m_outputFormats.append(OutputFormatJson); m_outputFormats.append(OutputFormatXml); m_outputFormats.append(OutputFormatHtml); + m_outputFormats.append(OutputFormatPlainText); m_outputFormats.append(OutputFormatImage); m_outputFormats.append(OutputNeedDownloaded); @@ -32,6 +33,7 @@ OutputFormatsListModel::OutputFormatsListModel(QObject *parent) m_outputFormatTitles.insert(OutputFormatImage, "Image"); m_outputFormatTitles.insert(OutputFormatHtml, "HTML"); m_outputFormatTitles.insert(OutputNeedDownloaded, "Attachment"); + m_outputFormatTitles.insert(OutputFormatPlainText, "Plain Text"); } int OutputFormatsListModel::rowCount(const QModelIndex &parent) const diff --git a/src/ListModels/responsebodylistmodel.cpp b/src/ListModels/responsebodylistmodel.cpp index 8d9f288..66f16ab 100644 --- a/src/ListModels/responsebodylistmodel.cpp +++ b/src/ListModels/responsebodylistmodel.cpp @@ -115,7 +115,7 @@ void ResponseBodyListModel::reformatting(const QString &formatter) noexcept } int currentStart = 0; - int count = line.count(); + int count = line.length(); while (currentStart < count) { int end = currentStart + 100; diff --git a/src/ViewModels/httprequestresultviewmodel.cpp b/src/ViewModels/httprequestresultviewmodel.cpp index c435f63..af7ba06 100644 --- a/src/ViewModels/httprequestresultviewmodel.cpp +++ b/src/ViewModels/httprequestresultviewmodel.cpp @@ -66,7 +66,7 @@ void HttpRequestResultViewModel::setBody(const QByteArray &body) noexcept m_actualFormat = outputFormat; } - m_isFormatting = !outputFormat.isEmpty(); + m_isFormatting = !outputFormat.isEmpty() && outputFormat != OutputFormatPlainText; m_showImage = outputFormat == OutputFormatImage; m_showDownloadFile = outputFormat == OutputNeedDownloaded; @@ -88,9 +88,12 @@ void HttpRequestResultViewModel::setBody(const QByteArray &body) noexcept void HttpRequestResultViewModel::reformatting() noexcept { auto outputFormat = m_outputFormat; - if (outputFormat == OutputFormatAuto) outputFormat = getFormatFromContentType(); + if (outputFormat == OutputFormatAuto) { + outputFormat = getFormatFromContentType(); + m_actualFormat = outputFormat; + } - m_isFormatting = !outputFormat.isEmpty(); + m_isFormatting = !outputFormat.isEmpty() && outputFormat != OutputFormatPlainText; m_showImage = outputFormat == OutputFormatImage; m_showDownloadFile = outputFormat == OutputNeedDownloaded; @@ -99,6 +102,7 @@ void HttpRequestResultViewModel::reformatting() noexcept emit isFormattingChanged(); emit showImageChanged(); emit showDownloadFileChanged(); + emit actualFormatChanged(); } QString HttpRequestResultViewModel::responseTime() const noexcept @@ -282,6 +286,10 @@ void HttpRequestResultViewModel::saveBodyToFile(const QString &fileName) void HttpRequestResultViewModel::reformatBody() { + if (m_outputFormat != OutputFormatAuto) { + m_actualFormat.clear(); + emit actualFormatChanged(); + } reformatting(); } @@ -358,6 +366,8 @@ QString HttpRequestResultViewModel::getFormatFromContentType() noexcept return OutputNeedDownloaded; } + if (contentTypeHeader.contains("text/")) return OutputFormatPlainText; + return ""; } diff --git a/src/Views/HttpResultViewer.qml b/src/Views/HttpResultViewer.qml index c4727b7..b0db4e4 100644 --- a/src/Views/HttpResultViewer.qml +++ b/src/Views/HttpResultViewer.qml @@ -244,7 +244,7 @@ Item { Menu { id: outputFormatMenu - y: -60 + y: -101 modal: true focus: true closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent @@ -353,13 +353,6 @@ Item { } } } - - MouseArea { - anchors.fill: parent - onPressed: { - console.log(mouseX, mouseY, listStrings.contentX, listStrings.contentY); - } - } } } diff --git a/src/globalconstants.cpp b/src/globalconstants.cpp index 2d8d890..b881834 100644 --- a/src/globalconstants.cpp +++ b/src/globalconstants.cpp @@ -42,5 +42,6 @@ QString OutputFormatHtml = "html"; QString OutputFormatXml = "xml"; QString OutputFormatImage = "image"; QString OutputNeedDownloaded = "downloadable"; +QString OutputFormatPlainText = "plaintext"; bool IsPortable = false; diff --git a/src/globalconstants.h b/src/globalconstants.h index 0fe16cb..f4071f4 100644 --- a/src/globalconstants.h +++ b/src/globalconstants.h @@ -42,5 +42,6 @@ extern QString OutputFormatHtml; extern QString OutputFormatXml; extern QString OutputFormatImage; extern QString OutputNeedDownloaded; +extern QString OutputFormatPlainText; extern bool IsPortable;