From 3f81317c89e2f02174bbb0ff0796fcce56bdd75e Mon Sep 17 00:00:00 2001 From: Roman Vladimirov Date: Thu, 3 Oct 2024 21:36:58 +0300 Subject: [PATCH] + Added fields order and timeout --- .../requestscommandpalettelistmodel.cpp | 9 +++ .../requestscommandpalettelistmodel.h | 4 +- src/ViewModels/httpperformerviewmodel.cpp | 3 + src/ViewModels/httprequestviewmodel.cpp | 59 ++++++++++++++++++- src/ViewModels/httprequestviewmodel.h | 6 +- src/ViewModels/requestexternalviewmodel.cpp | 8 +++ ...Palette.qml => HistoryRequestsPalette.qml} | 24 +++++++- src/globalconstants.h | 7 +++ src/globalhelpers.cpp | 1 + src/globalhelpers.h | 1 - src/main.qml | 2 +- src/qml.qrc | 2 +- 12 files changed, 118 insertions(+), 8 deletions(-) rename src/Views/Controls/{CommandPalette.qml => HistoryRequestsPalette.qml} (60%) diff --git a/src/ListModels/requestscommandpalettelistmodel.cpp b/src/ListModels/requestscommandpalettelistmodel.cpp index 5233cfc..eb54cf1 100644 --- a/src/ListModels/requestscommandpalettelistmodel.cpp +++ b/src/ListModels/requestscommandpalettelistmodel.cpp @@ -52,6 +52,9 @@ QVariant RequestsCommandPaletteListModel::data(const QModelIndex &index, int rol case IsSelectedRole: { return QVariant(m_selected == itemIndex); } + case StatusIconRole: { + return QVariant(request->resultModel()->displayStatus()); + } } return QVariant(); @@ -71,6 +74,10 @@ QHash RequestsCommandPaletteListModel::roleNames() const { IsSelectedRole, "isSelected" + }, + { + StatusIconRole, + "displayStatus" } }; } @@ -82,6 +89,7 @@ void RequestsCommandPaletteListModel::selectItem() m_history.insert(0, id); m_selected = 0; emit itemSelected(id); + emit itemIndexSelected(m_selected); } void RequestsCommandPaletteListModel::selectNext() @@ -92,6 +100,7 @@ void RequestsCommandPaletteListModel::selectNext() if (m_selected >= m_requests->count()) m_selected = 0; refresh(); + emit itemIndexSelected(m_selected); } void RequestsCommandPaletteListModel::forceSelectItem(QUuid id) diff --git a/src/ListModels/requestscommandpalettelistmodel.h b/src/ListModels/requestscommandpalettelistmodel.h index 5936e23..d89d358 100644 --- a/src/ListModels/requestscommandpalettelistmodel.h +++ b/src/ListModels/requestscommandpalettelistmodel.h @@ -34,7 +34,8 @@ class RequestsCommandPaletteListModel : public QAbstractListModel enum CommandPaletterRoles { IdentifierRole = Qt::UserRole + 1, TitleRole, - IsSelectedRole + IsSelectedRole, + StatusIconRole }; public: @@ -54,6 +55,7 @@ class RequestsCommandPaletteListModel : public QAbstractListModel signals: void itemSelected(const QUuid& id); + void itemIndexSelected(int index); }; diff --git a/src/ViewModels/httpperformerviewmodel.cpp b/src/ViewModels/httpperformerviewmodel.cpp index 54895b2..019cdf3 100644 --- a/src/ViewModels/httpperformerviewmodel.cpp +++ b/src/ViewModels/httpperformerviewmodel.cpp @@ -334,6 +334,9 @@ bool HttpPerformerViewModel::performSingleRequest(HttpRequestModel *modelRequest resultModel->setPostScript(postScript); } + auto timeout = requestModel->getTimeout(); + if (timeout > 0) request.setTransferTimeout(timeout); + auto method = requestModel->getMethod(); method = m_globalVariable->replaceGlobalVariables(method).toLower(); if (method == "get") { diff --git a/src/ViewModels/httprequestviewmodel.cpp b/src/ViewModels/httprequestviewmodel.cpp index bcf8aac..48c7d0f 100644 --- a/src/ViewModels/httprequestviewmodel.cpp +++ b/src/ViewModels/httprequestviewmodel.cpp @@ -43,6 +43,8 @@ HttpRequestViewModel::HttpRequestViewModel(QObject *parent) m_sortWeight->insert(static_cast(HttpRequestTypes::OptionsType), 50); m_sortWeight->insert(static_cast(HttpRequestTypes::PostScriptType), 51); + m_sortWeight->insert(static_cast(HttpRequestTypes::TimeoutType), 52); + m_sortWeight->insert(static_cast(HttpRequestTypes::OrderType), 53); m_sortWeight->insert(static_cast(HttpRequestTypes::BodyType), 60); m_sortWeight->insert(static_cast(HttpRequestTypes::UnknownType), 1000); @@ -281,6 +283,16 @@ void HttpRequestViewModel::setItemContent(const int position, const QString &con needRefresh = true; } + if (lowerContent.startsWith(TimeoutPrefix) && itemType != HttpRequestTypes::TimeoutType) { + item->setType(static_cast(HttpRequestTypes::TimeoutType)); + needRefresh = true; + } + + if (lowerContent.startsWith(OrderPrefix) && itemType != HttpRequestTypes::OrderType) { + item->setType(static_cast(HttpRequestTypes::OrderType)); + needRefresh = true; + } + if (itemType != HttpRequestTypes::UnknownType && content.isEmpty()) { item->setType(static_cast(HttpRequestTypes::UnknownType)); @@ -576,6 +588,44 @@ QString HttpRequestViewModel::getPostScript() const noexcept return ""; } +int HttpRequestViewModel::getTimeout() const noexcept +{ + auto iterator = std::find_if( + m_items->begin(), + m_items->end(), + [](const HttpRequestItem* item) { + auto type = static_cast(item->type()); + return type == HttpRequestTypes::TimeoutType; + } + ); + if (iterator != m_items->end()) { + auto item = *iterator; + auto text = item->text().replace(TimeoutPrefix, "", Qt::CaseInsensitive); + return text.toInt(); + } + + return 0; +} + +int HttpRequestViewModel::getOrder() const noexcept +{ + auto iterator = std::find_if( + m_items->begin(), + m_items->end(), + [](const HttpRequestItem* item) { + auto type = static_cast(item->type()); + return type == HttpRequestTypes::OrderType; + } + ); + if (iterator != m_items->end()) { + auto item = *iterator; + auto text = item->text().replace(OrderPrefix, "", Qt::CaseInsensitive); + return text.toInt(); + } + + return 0; +} + bool HttpRequestViewModel::isOnlyEmptyFirstItem() const noexcept { return m_items->count() == 1 && m_items->value(0)->text().isEmpty(); @@ -692,8 +742,9 @@ QString HttpRequestViewModel::getTypeColor(int type) const case HttpRequestTypes::RouteType: return "#78D34E24"; case HttpRequestTypes::OptionsType: - return "#78FDB833"; case HttpRequestTypes::PostScriptType: + case HttpRequestTypes::TimeoutType: + case HttpRequestTypes::OrderType: return "#78FDB833"; default: return "#CDCDB4"; @@ -750,6 +801,12 @@ QString HttpRequestViewModel::getItemPrefix(const HttpRequestTypes itemType, con case HttpRequestTypes::PostScriptType: prefix = PostScriptPrefix; break; + case HttpRequestTypes::TimeoutType: + prefix = TimeoutPrefix; + break; + case HttpRequestTypes::OrderType: + prefix = OrderPrefix; + break; default: prefix = ""; } diff --git a/src/ViewModels/httprequestviewmodel.h b/src/ViewModels/httprequestviewmodel.h index 4a8d7e4..d439fb6 100644 --- a/src/ViewModels/httprequestviewmodel.h +++ b/src/ViewModels/httprequestviewmodel.h @@ -46,7 +46,9 @@ class HttpRequestViewModel : public QAbstractListModel PastryType, RouteType, OptionsType, - PostScriptType + PostScriptType, + TimeoutType, + OrderType }; private: @@ -108,6 +110,8 @@ class HttpRequestViewModel : public QAbstractListModel QStringList getOptions() const noexcept; QString getTitle() const noexcept; QString getPostScript() const noexcept; + int getTimeout() const noexcept; + int getOrder() const noexcept; bool isOnlyEmptyFirstItem() const noexcept; int countItems() const noexcept; void sortingFields(const bool descending) noexcept; diff --git a/src/ViewModels/requestexternalviewmodel.cpp b/src/ViewModels/requestexternalviewmodel.cpp index 50e48e8..85e7df6 100644 --- a/src/ViewModels/requestexternalviewmodel.cpp +++ b/src/ViewModels/requestexternalviewmodel.cpp @@ -81,6 +81,14 @@ void RequestExternalViewModel::parseFromString(const QString &input) noexcept type = HttpRequestViewModel::HttpRequestTypes::PostScriptType; isBodyType = false; } + if (line.startsWith(TimeoutPrefix)) { + type = HttpRequestViewModel::HttpRequestTypes::TimeoutType; + isBodyType = false; + } + if (line.startsWith(OrderPrefix)) { + type = HttpRequestViewModel::HttpRequestTypes::OrderType; + isBodyType = false; + } if (m_textAdvisor->isContainsHeader(line)) { type = HttpRequestViewModel::HttpRequestTypes::HeaderType; isBodyType = false; diff --git a/src/Views/Controls/CommandPalette.qml b/src/Views/Controls/HistoryRequestsPalette.qml similarity index 60% rename from src/Views/Controls/CommandPalette.qml rename to src/Views/Controls/HistoryRequestsPalette.qml index 9c225fd..40e3f77 100644 --- a/src/Views/Controls/CommandPalette.qml +++ b/src/Views/Controls/HistoryRequestsPalette.qml @@ -2,7 +2,7 @@ import QtQuick import QtQuick.Controls Item { - width: 280 + width: 320 height: 250 Rectangle { @@ -17,7 +17,9 @@ Item { anchors.fill: parent anchors.margins: 4 model: backend.requestsCommandPaletter + clip: true delegate: Item { + id: itemRoot width: items.width height: 30 @@ -32,15 +34,33 @@ Item { anchors.left: parent.left anchors.leftMargin: 4 anchors.right: parent.right - anchors.rightMargin: 4 + anchors.rightMargin: 36 text: title wrapMode: Text.Wrap elide: Text.ElideRight maximumLineCount: 2 } + + Image { + id: image + anchors.right: parent.right + anchors.rightMargin: 14 + anchors.verticalCenter: parent.verticalCenter + width: 22 + height: 22 + source: storagePaths.icons + displayStatus + ".svg" + mipmap: true + } } ScrollBar.vertical: ScrollBar { active: true } } + + Connections { + target: backend.requestsCommandPaletter + function onItemIndexSelected(id: int) { + items.positionViewAtIndex(id, ListView.Contain); + } + } } diff --git a/src/globalconstants.h b/src/globalconstants.h index 76eee15..ca11911 100644 --- a/src/globalconstants.h +++ b/src/globalconstants.h @@ -13,6 +13,9 @@ along with this program. If not, see . */ +#ifndef GLOBALCONSTANT_H +#define GLOBALCONSTANT_H + #include extern QString UrlPrefix; @@ -32,6 +35,8 @@ extern QString PastryPrefix; extern QString RoutePrefix; extern QString OptionsPrefix; extern QString PostScriptPrefix; +inline QString TimeoutPrefix = "timeout "; +inline QString OrderPrefix = "order "; extern QString NotificationErrorTopic; extern QString NotificationInfoTopic; @@ -46,3 +51,5 @@ extern QString OutputFormatPlainText; extern QString OutputFormatCss; extern bool IsPortable; + +#endif diff --git a/src/globalhelpers.cpp b/src/globalhelpers.cpp index 3d70d69..b10e25a 100644 --- a/src/globalhelpers.cpp +++ b/src/globalhelpers.cpp @@ -1,4 +1,5 @@ #include "globalhelpers.h" +#include "globalconstants.h" QString getCachePath(const QString& filename) noexcept { diff --git a/src/globalhelpers.h b/src/globalhelpers.h index af51a14..38bd7dd 100644 --- a/src/globalhelpers.h +++ b/src/globalhelpers.h @@ -1,7 +1,6 @@ #include #include #include -#include "globalconstants.h" QString getCachePath(const QString& filename) noexcept; diff --git a/src/main.qml b/src/main.qml index 4a11efc..3020784 100644 --- a/src/main.qml +++ b/src/main.qml @@ -51,7 +51,7 @@ ApplicationWindow { } } - CommandPalette { + HistoryRequestsPalette { id: commandPalette anchors.centerIn: parent visible: backend.openedCommandPalette diff --git a/src/qml.qrc b/src/qml.qrc index 8872fa1..9eb63cb 100644 --- a/src/qml.qrc +++ b/src/qml.qrc @@ -33,7 +33,7 @@ Views/Icons/broken.svg Views/Windows/ImageResponseWindow.qml Views/AboutWindow.qml - Views/Controls/CommandPalette.qml + Views/Controls/HistoryRequestsPalette.qml Views/Images/backgroundpattern.png Views/OpenApiExportWindow.qml Views/Controls/CommonTextField.qml