Skip to content

Commit

Permalink
+ Added new formatter Plain Text
Browse files Browse the repository at this point in the history
  • Loading branch information
trueromanus committed Nov 18, 2023
1 parent fd707a1 commit d7975e8
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/ArdorQuery.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
7 changes: 7 additions & 0 deletions src/Formatters/formatterfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "jsonformatter.h"
#include "htmlformatter.h"
#include "xmlformatter.h"
#include "plaintextformatter.h"

FormatterFactory::FormatterFactory()
{
Expand Down Expand Up @@ -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;
}
33 changes: 33 additions & 0 deletions src/Formatters/plaintextformatter.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 19 additions & 0 deletions src/Formatters/plaintextformatter.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions src/ListModels/outputformatslistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ListModels/responsebodylistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 13 additions & 3 deletions src/ViewModels/httprequestresultviewmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -99,6 +102,7 @@ void HttpRequestResultViewModel::reformatting() noexcept
emit isFormattingChanged();
emit showImageChanged();
emit showDownloadFileChanged();
emit actualFormatChanged();
}

QString HttpRequestResultViewModel::responseTime() const noexcept
Expand Down Expand Up @@ -282,6 +286,10 @@ void HttpRequestResultViewModel::saveBodyToFile(const QString &fileName)

void HttpRequestResultViewModel::reformatBody()
{
if (m_outputFormat != OutputFormatAuto) {
m_actualFormat.clear();
emit actualFormatChanged();
}
reformatting();
}

Expand Down Expand Up @@ -358,6 +366,8 @@ QString HttpRequestResultViewModel::getFormatFromContentType() noexcept
return OutputNeedDownloaded;
}

if (contentTypeHeader.contains("text/")) return OutputFormatPlainText;

return "";
}

Expand Down
9 changes: 1 addition & 8 deletions src/Views/HttpResultViewer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ Item {

Menu {
id: outputFormatMenu
y: -60
y: -101
modal: true
focus: true
closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent
Expand Down Expand Up @@ -353,13 +353,6 @@ Item {
}
}
}

MouseArea {
anchors.fill: parent
onPressed: {
console.log(mouseX, mouseY, listStrings.contentX, listStrings.contentY);
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/globalconstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ QString OutputFormatHtml = "html";
QString OutputFormatXml = "xml";
QString OutputFormatImage = "image";
QString OutputNeedDownloaded = "downloadable";
QString OutputFormatPlainText = "plaintext";

bool IsPortable = false;
1 change: 1 addition & 0 deletions src/globalconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ extern QString OutputFormatHtml;
extern QString OutputFormatXml;
extern QString OutputFormatImage;
extern QString OutputNeedDownloaded;
extern QString OutputFormatPlainText;

extern bool IsPortable;

0 comments on commit d7975e8

Please sign in to comment.