Skip to content

Commit

Permalink
> Few fixes for focus handling
Browse files Browse the repository at this point in the history
+ Added supporting downloading files
  • Loading branch information
trueromanus committed Nov 11, 2023
1 parent 47d1a3e commit 795497d
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 37 deletions.
2 changes: 2 additions & 0 deletions src/ListModels/outputformatslistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ OutputFormatsListModel::OutputFormatsListModel(QObject *parent)
m_outputFormats.append(OutputFormatXml);
m_outputFormats.append(OutputFormatHtml);
m_outputFormats.append(OutputFormatImage);
m_outputFormats.append(OutputNeedDownloaded);

m_outputFormatTitles.insert(OutputFormatAuto, "Auto");
m_outputFormatTitles.insert(OutputFormatJson, "JSON");
m_outputFormatTitles.insert(OutputFormatXml, "XML document");
m_outputFormatTitles.insert(OutputFormatImage, "Image");
m_outputFormatTitles.insert(OutputFormatHtml, "HTML");
m_outputFormatTitles.insert(OutputNeedDownloaded, "Attachment");
}

int OutputFormatsListModel::rowCount(const QModelIndex &parent) const
Expand Down
7 changes: 6 additions & 1 deletion src/ViewModels/backendviewmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void BackendViewModel::addNewRequest()

auto request = model->requestModel();
request->setTextAdvisor(m_textAdviser);
request->setSelectedItem(0); // select first empty field for new request

m_requests->addItem(model);
}
Expand Down Expand Up @@ -67,7 +68,6 @@ bool BackendViewModel::shortcutHandler(const QString &shortcut) noexcept
} else if (command == m_performQueriesMultipleCommand) {
m_requestPerformer->performAllRequest();
} else if (command == m_performQueryCommand) {
m_requestPerformer->performAllRequest();
auto request = m_requests->getSelectedRequest();
m_requestPerformer->performOneRequest(request);
} else if (command == m_cancelQueryCommand) {
Expand Down Expand Up @@ -259,6 +259,11 @@ void BackendViewModel::importFromOpenApi(int index) noexcept
m_requests->selectItem(createdIndex);
}

void BackendViewModel::saveDownloadedFile(const QString &fileName) noexcept
{
m_requests->selectedItem()->resultModel()->saveBodyToFile(removeProtocol(fileName));
}

void BackendViewModel::deleteCurrentRequest() noexcept
{
if (m_requests->singleRequest()) addNewRequest();
Expand Down
1 change: 1 addition & 0 deletions src/ViewModels/backendviewmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class BackendViewModel : public QObject
Q_INVOKABLE void generateImage(const QString& filePath) noexcept;
Q_INVOKABLE void generateImageToClipboard() noexcept;
Q_INVOKABLE void importFromOpenApi(int index) noexcept;
Q_INVOKABLE void saveDownloadedFile(const QString& fileName) noexcept;

void deleteCurrentRequest() noexcept;

Expand Down
25 changes: 16 additions & 9 deletions src/ViewModels/httpperformerviewmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,18 @@ void HttpPerformerViewModel::cancelRequest()

void HttpPerformerViewModel::performOneRequest(HttpRequestModel *request)
{
if (m_runningRequests->contains(request->requestId().toString())) return;
if (m_runningRequests.contains(request->requestId().toString())) return;

if (performSingleRequest(request)) {
addToCounter(1);
emit countRequestsChanged();
if (m_countFinishedRequests < m_countRequests) {
addToCounter(1);
emit countRequestsChanged();
} else {
m_countFinishedRequests = 0;
m_countRequests = 0;
emit countFinishedRequestsChanged();
emit countRequestsChanged();
}
}
}

Expand Down Expand Up @@ -249,7 +256,7 @@ void HttpPerformerViewModel::startTrackRequest(QNetworkReply *reply, const QUuid

reply->setProperty("id", id.toString());

m_runningRequests->insert(id.toString(), resultModel);
m_runningRequests.insert(id.toString(), resultModel);
}

void HttpPerformerViewModel::adjustOptions(QStringList options, QNetworkRequest &request)
Expand Down Expand Up @@ -280,7 +287,7 @@ bool HttpPerformerViewModel::performSingleRequest(HttpRequestModel *modelRequest
auto id = modelRequest->requestId();

//if request already performing don't need make something
if (m_runningRequests->contains(id.toString())) return false;
if (m_runningRequests.contains(id.toString())) return false;

auto resultModel = modelRequest->resultModel();
auto requestModel = modelRequest->requestModel();
Expand Down Expand Up @@ -378,7 +385,7 @@ bool HttpPerformerViewModel::performSingleRequest(HttpRequestModel *modelRequest

void HttpPerformerViewModel::addToCounter(int number) noexcept
{
if (m_countRequests == m_countFinishedRequests) {
if (m_countRequests > 0 && m_countRequests == m_countFinishedRequests) {
m_countRequests = 0;
m_countFinishedRequests = 0;
}
Expand Down Expand Up @@ -421,13 +428,13 @@ void HttpPerformerViewModel::runPostScript(const QString &script, QObject* prope
void HttpPerformerViewModel::requestFinished(QNetworkReply *reply)
{
auto id = reply->property("id").toString();
if (!m_runningRequests->contains(id)) return;
if (!m_runningRequests.contains(id)) return;

auto result = m_runningRequests->value(id);
auto result = m_runningRequests.value(id);
if (result == nullptr) return;

result->untrackRequestTime();
m_runningRequests->remove(id);
m_runningRequests.remove(id);
reduceFromCounter();

result->setNetworkError("");
Expand Down
2 changes: 1 addition & 1 deletion src/ViewModels/httpperformerviewmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class HttpPerformerViewModel : public QObject
private:
QScopedPointer<QNetworkAccessManager> m_networkManager { new QNetworkAccessManager() };
QScopedPointer<QMap<QString, QString>> m_rawHeaders { new QMap<QString, QString>() };
QScopedPointer<QMap<QString, HttpRequestResultViewModel*>> m_runningRequests { new QMap<QString, HttpRequestResultViewModel*>() };
QMap<QString, HttpRequestResultViewModel*> m_runningRequests { QMap<QString, HttpRequestResultViewModel*>() };
QSharedPointer<QList<HttpRequestModel*>> m_requests { nullptr };
int m_countRequests { 0 };
int m_countFinishedRequests { 0 };
Expand Down
37 changes: 34 additions & 3 deletions src/ViewModels/httprequestresultviewmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <QClipboard>
#include <QGuiApplication>
#include <QPainter>
#include <QFile>
#include "httprequestresultviewmodel.h"

HttpRequestResultViewModel::HttpRequestResultViewModel(QObject *parent)
Expand Down Expand Up @@ -53,6 +54,7 @@ void HttpRequestResultViewModel::setBody(const QByteArray &body) noexcept
m_bodyModel->setBody("", "");
m_isFormatting = false;
m_showImage = false;
m_showDownloadFile = false;
emit isFormattingChanged();
return;
}
Expand All @@ -66,6 +68,7 @@ void HttpRequestResultViewModel::setBody(const QByteArray &body) noexcept

m_isFormatting = !outputFormat.isEmpty();
m_showImage = outputFormat == OutputFormatImage;
m_showDownloadFile = outputFormat == OutputNeedDownloaded;

m_bodyModel->setBody(body, outputFormat);

Expand All @@ -78,6 +81,7 @@ void HttpRequestResultViewModel::setBody(const QByteArray &body) noexcept
emit responseSizeChanged();
emit isFormattingChanged();
emit showImageChanged();
emit showDownloadFileChanged();
emit actualFormatChanged();
}

Expand All @@ -88,11 +92,13 @@ void HttpRequestResultViewModel::reformatting() noexcept

m_isFormatting = !outputFormat.isEmpty();
m_showImage = outputFormat == OutputFormatImage;
m_showDownloadFile = outputFormat == OutputNeedDownloaded;

m_bodyModel->reformatBody(outputFormat);

emit isFormattingChanged();
emit showImageChanged();
emit showDownloadFileChanged();
}

QString HttpRequestResultViewModel::responseTime() const noexcept
Expand Down Expand Up @@ -263,6 +269,17 @@ void HttpRequestResultViewModel::copyBodyToClipboard()
}
}

void HttpRequestResultViewModel::saveBodyToFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return;

auto content = m_bodyModel->getFullBody();
file.write(content.toUtf8());

file.close();
}

void HttpRequestResultViewModel::reformatBody()
{
reformatting();
Expand All @@ -284,14 +301,19 @@ QString HttpRequestResultViewModel::getReadableSize(uint64_t size) const noexcep
return result;
}

QString HttpRequestResultViewModel::getFormatFromContentType() const noexcept
QString HttpRequestResultViewModel::getFormatFromContentType() noexcept
{
QString contentTypeHeader = "";
QString contentDisposition = "";
foreach (auto header, m_headers) {
if (header.contains("content-type:", Qt::CaseInsensitive)) {
contentTypeHeader = header.toLower();
break;
}
if (header.contains("content-disposition:", Qt::CaseInsensitive)) {
contentDisposition = header.toLower();
break;
}
}
if (contentTypeHeader.isEmpty()) return "";

Expand All @@ -310,6 +332,15 @@ QString HttpRequestResultViewModel::getFormatFromContentType() const noexcept
return OutputFormatImage;
}

if (!contentDisposition.isEmpty()) {
m_defaultDownloadFile = "";
auto value = contentDisposition.toLower().replace("content-disposition: ", "");
if (!value.contains("attachment", Qt::CaseInsensitive)) return "";
auto fileNameIndex = value.indexOf("filename=", Qt::CaseInsensitive);
if (fileNameIndex > -1) m_defaultDownloadFile = value.mid(fileNameIndex + 10);
return OutputNeedDownloaded;
}

return "";
}

Expand Down Expand Up @@ -346,7 +377,7 @@ QStringList HttpRequestResultViewModel::getHeaderLines()
.replace("</font>", "")
.replace("\n", "")
.replace("\r", "");
if (clearedHeader.count() > lineCount) {
if (clearedHeader.size() > lineCount) {
int position = 0;
QString content;
bool isFull = false;
Expand All @@ -356,7 +387,7 @@ QStringList HttpRequestResultViewModel::getHeaderLines()
if (content.isEmpty()) break;
lines.append(content);
position += lineCount;
isFull = content.count() == lineCount;
isFull = content.size() == lineCount;
} while (isFull);
} else {
lines.append(clearedHeader);
Expand Down
9 changes: 8 additions & 1 deletion src/ViewModels/httprequestresultviewmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class HttpRequestResultViewModel : public QObject
Q_PROPERTY(bool isFormatting READ isFormatting NOTIFY isFormattingChanged)
Q_PROPERTY(bool showImage READ showImage NOTIFY showImageChanged)
Q_PROPERTY(bool hasError READ hasError NOTIFY hasErrorChanged)
Q_PROPERTY(bool showDownloadFile READ showDownloadFile NOTIFY showDownloadFileChanged)

private:
int m_statusCode { 0 };
Expand All @@ -60,6 +61,8 @@ class HttpRequestResultViewModel : public QObject
QString m_actualFormat { "" };
bool m_customErrorResult { false };
QString m_postScript { "" };
bool m_showDownloadFile { false };
QString m_defaultDownloadFile { "" };

public:
explicit HttpRequestResultViewModel(QObject *parent = nullptr);
Expand Down Expand Up @@ -107,6 +110,8 @@ class HttpRequestResultViewModel : public QObject

bool hasError() const noexcept { return m_customErrorResult || !m_networkError.isEmpty(); }

bool showDownloadFile() const noexcept { return m_showDownloadFile; }

void setCustomErrorResult(bool hasErrors, const QString& errorMessage);

uint64_t originResponseSize() const noexcept { return m_responseSize; }
Expand All @@ -117,11 +122,12 @@ class HttpRequestResultViewModel : public QObject

Q_INVOKABLE void copyHeadersToClipboard();
Q_INVOKABLE void copyBodyToClipboard();
Q_INVOKABLE void saveBodyToFile(const QString& fileName);
Q_INVOKABLE void reformatBody();

private:
QString getReadableSize(uint64_t size) const noexcept;
QString getFormatFromContentType() const noexcept;
QString getFormatFromContentType() noexcept;
void setBoldTextToPainter(QPainter& painter) noexcept;
void setNormalTextToPainter(QPainter& painter) noexcept;
void paintText(QPainter& painter, const QImage& image, int& currentLine, int& lineHeight, const QString& text, bool bold) noexcept;
Expand All @@ -144,6 +150,7 @@ class HttpRequestResultViewModel : public QObject
void actualFormatChanged();
void errorSavingGeneratedFile();
void hasErrorChanged();
void showDownloadFileChanged();

};

Expand Down
2 changes: 1 addition & 1 deletion src/Views/AboutWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ ApplicationWindow {
anchors.top: opensourceInfo.bottom
anchors.leftMargin: 20
anchors.topMargin: 12
text: "Used Qt version 6.2.3<br>If you want to get sources of Qt please <a href='mailto:[email protected]'>email this address</a>."
text: "Used <a href='https://www.qt.io/'>Qt</a><br>If you want to get sources of Qt please <a href='mailto:[email protected]'>email this address</a>."
font.pointSize: 10
onLinkActivated: function (link) {
Qt.openUrlExternally(link);
Expand Down
4 changes: 4 additions & 0 deletions src/Views/GlobalVariablesWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ ApplicationWindow {
if (handled) globalEventHandler.setHandledLastSession();
}
}

onActiveFocusItemChanged: {
if (!root.activeFocusItem) globalEventHandler.clear();
}
}
15 changes: 9 additions & 6 deletions src/Views/HttpRequestEditor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Item {

ListView {
id: listView
focus: true
anchors.fill: parent
clip: true
boundsBehavior: ListView.StopAtBounds
Expand All @@ -39,9 +40,8 @@ Item {

onIsNeedFocusedChanged: {
if (isNeedFocused) {
listView.forceActiveFocus();
textArea.forceActiveFocus();
} else {
textArea.focus = false;
}
}

Expand All @@ -65,14 +65,17 @@ Item {
onPressed: {
if (listView.model.selectedItem !== currentIndex) listView.model.selectedItem = currentIndex;
}
onActiveFocusChanged: {
if (backend.tabs.currentTab !== 'Request') return; //dirty hack but I don't know how to resolve it

if (isNeedFocused && !textArea.activeFocus) {
textArea.forceActiveFocus();
}
}
}
}
ScrollBar.vertical: ScrollBar {
active: true
}
}

Component.onCompleted: {
viewModel.selectedItem = 0; //WORKAROUND: fix loosing focus after start application
}
}
Loading

0 comments on commit 795497d

Please sign in to comment.