Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ArdorQuery.pro
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ SOURCES += \
Models/shortcutsection.cpp \
QuickControls/backendimage.cpp \
ViewModels/backendviewmodel.cpp \
ViewModels/globalmouseviewmodel.cpp \
ViewModels/httpperformerviewmodel.cpp \
ViewModels/httprequestresultviewmodel.cpp \
ViewModels/httprequestviewmodel.cpp \
Expand Down Expand Up @@ -88,6 +89,7 @@ HEADERS += \
Models/shortcutsection.h \
QuickControls/backendimage.h \
ViewModels/backendviewmodel.h \
ViewModels/globalmouseviewmodel.h \
ViewModels/httpperformerviewmodel.h \
ViewModels/httprequestresultviewmodel.h \
ViewModels/httprequestviewmodel.h \
Expand Down
82 changes: 81 additions & 1 deletion src/ListModels/responsebodylistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
ResponseBodyListModel::ResponseBodyListModel(QObject *parent)
: QAbstractListModel{parent}
{

}

int ResponseBodyListModel::rowCount(const QModelIndex &parent) const
Expand All @@ -39,6 +38,18 @@ QVariant ResponseBodyListModel::data(const QModelIndex &index, int role) const

switch (role) {
case CurrentLineRole: {
if (currentIndex == m_startSelectLine && currentIndex != m_endSelectLine) {
//TODO: this line is start for more lines
auto highlightedLine = selectAsStartLine(line);
return QVariant(highlightedLine);
}
if (currentIndex != m_startSelectLine && currentIndex == m_endSelectLine) {
//TODO: this line is end for more lines
return QVariant(selectAsEndLine(line));
}
if (currentIndex == m_startSelectLine && currentIndex == m_endSelectLine) {
return QVariant(selectAsOneLine(line));
}
return QVariant(line);
}
case IndexRole: {
Expand Down Expand Up @@ -206,6 +217,25 @@ void ResponseBodyListModel::clear() noexcept
m_originalBody.clear();
}

QString ResponseBodyListModel::selectAsStartLine(const QString &line) const
{
auto start = m_startSelectLine > m_endSelectLine ? 0 : m_startSelectPosition;
auto end = m_startSelectLine > m_endSelectLine ? m_startSelectPosition : 0;
auto formatted = QString(line);

return formatted;
}

QString ResponseBodyListModel::selectAsEndLine(const QString &line) const
{

}

QString ResponseBodyListModel::selectAsOneLine(const QString &line) const
{

}

void ResponseBodyListModel::searchText(const QString &filter) noexcept
{
if (m_previousFilter == filter) return;
Expand Down Expand Up @@ -251,6 +281,56 @@ void ResponseBodyListModel::searchText(const QString &filter) noexcept
emit countFindedLinesTextChanged();
}

void ResponseBodyListModel::selectLine(int elementIndex, int positionX) noexcept
{
if (m_startSelectLine == -1 && m_endSelectLine == -1) {
m_startSelectLine = elementIndex;
m_endSelectLine = elementIndex;
m_startSelectPosition = positionX;
m_endSelectPosition = positionX;
emit startSelectLineChanged();
emit startSelectPositionChanged();
emit endSelectLineChanged();
emit endSelectPositionChanged();
return;
}


m_endSelectLine = elementIndex;
m_endSelectPosition = positionX;
emit endSelectLineChanged();
emit endSelectPositionChanged();

if (m_startSelectLine != m_endSelectLine || m_startSelectPosition != m_endSelectPosition) {
auto startLine = m_startSelectLine;
auto endLine = m_endSelectLine;
if (m_startSelectLine > m_endSelectLine) {
startLine = m_endSelectLine;
endLine = m_startSelectLine;
}
emit dataChanged(index(startLine,0), index(endLine,0));
}

qDebug() << m_startSelectLine << " " << m_endSelectLine << " " << m_startSelectPosition << " " << m_endSelectPosition;
}

void ResponseBodyListModel::resetSelected() noexcept
{
beginResetModel();

m_startSelectLine = -1;
m_endSelectLine = -1;
m_startSelectPosition = -1;
m_endSelectPosition = -1;

emit startSelectLineChanged();
emit endSelectLineChanged();
emit startSelectPositionChanged();
emit endSelectPositionChanged();

endResetModel();
}

QString & ResponseBodyListModel::cleanLineFromTags(QString &line) noexcept
{
return line.replace("</font>", "").replace("&lt;", "<").replace("&gt;", "<").replace(m_fontTagStartRegExp, "");
Expand Down
27 changes: 27 additions & 0 deletions src/ListModels/responsebodylistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <QImage>
#include <QMap>
#include <QList>
#include <QFont>
#include <QFontMetrics>
#include <QRegularExpression>
#include "../Formatters/formatterfactory.h"
#include "../globalconstants.h"
Expand All @@ -33,6 +35,10 @@ class ResponseBodyListModel : public QAbstractListModel
Q_PROPERTY(int bodyImageHeight READ bodyImageHeight NOTIFY bodyImageHeightChanged)
Q_PROPERTY(int countFindedLines READ countFindedLines NOTIFY countFindedLinesChanged)
Q_PROPERTY(QString countFindedLinesText READ countFindedLinesText NOTIFY countFindedLinesTextChanged)
Q_PROPERTY(int startSelectLine READ startSelectLine NOTIFY startSelectLineChanged)
Q_PROPERTY(int endSelectLine READ endSelectLine NOTIFY endSelectLineChanged)
Q_PROPERTY(int startSelectPosition READ startSelectPosition NOTIFY startSelectPositionChanged)
Q_PROPERTY(int endSelectPosition READ endSelectPosition NOTIFY endSelectPositionChanged)

private:
QStringList m_lines { QStringList() };
Expand All @@ -46,6 +52,12 @@ class ResponseBodyListModel : public QAbstractListModel
QRegularExpression m_fontTagStartRegExp { R"a(<font color=\"\#[A-Za-z0-9]{1,6}\">)a" };
bool m_notFounded { false };
QString m_previousFilter { "" };
int m_startSelectLine { -1 };
int m_endSelectLine { -1 };
int m_startSelectPosition { -1 };
int m_endSelectPosition { -1 };
QFont m_font { QFont() };
QFontMetrics m_fontMetrics { QFontMetrics(m_font) };

enum ResponseBodyRoles {
CurrentLineRole = Qt::UserRole + 1,
Expand Down Expand Up @@ -76,7 +88,18 @@ class ResponseBodyListModel : public QAbstractListModel
int getCurrentFindedLine() noexcept;
void clear() noexcept;

int startSelectLine() const noexcept { return m_startSelectLine; }
int endSelectLine() const noexcept { return m_endSelectLine; }
int startSelectPosition() const noexcept { return m_startSelectPosition; }
int endSelectPosition() const noexcept { return m_endSelectPosition; }

QString selectAsStartLine(const QString& line) const;
QString selectAsEndLine(const QString& line) const;
QString selectAsOneLine(const QString& line) const;

Q_INVOKABLE void searchText(const QString& filter) noexcept;
Q_INVOKABLE void selectLine(int elementIndex, int positionX) noexcept;
Q_INVOKABLE void resetSelected() noexcept;

private:
QString& cleanLineFromTags(QString& line) noexcept;
Expand All @@ -88,6 +111,10 @@ class ResponseBodyListModel : public QAbstractListModel
void bodyImageHeightChanged();
void countFindedLinesChanged();
void countFindedLinesTextChanged();
void startSelectLineChanged();
void endSelectLineChanged();
void startSelectPositionChanged();
void endSelectPositionChanged();

};

Expand Down
61 changes: 61 additions & 0 deletions src/ViewModels/backendviewmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,67 @@ void BackendViewModel::importFromOpenApi(int index) noexcept
m_requests->selectItem(createdIndex);
}

void BackendViewModel::setFontFamily(const QString &family) noexcept
{
m_fontFamily = family;
m_font = QFont(family, m_fontPointSize);
m_fontMetrics = QFontMetrics(m_font);
m_fontHeight = m_fontMetrics.boundingRect(QString('A')).height();

auto characters = QString("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789 !@#$%^&*()_+-={}[];:\\|/?><'\"");

foreach (auto character, characters) {
m_characterWidths[character] = m_fontMetrics.boundingRect(QString(character == ' ' ? '<' : character)).width();
}

emit fontFamilyChanged();
}

int BackendViewModel::getPositionInText(const QString &line, int positionX, int positionY, int width, bool formatted) noexcept
{
if (formatted) {
auto replacedLine = QString(line).replace("&nbsp;", " ").replace("&lt;", "[").replace("&quot;", "\"").replace("&qt;", "]");
auto tagStarted = false;
auto characterWidth = 4;
auto characterPosition = 0;
auto characterLine = 0;
auto lineInside = positionY > m_fontHeight ? positionY / m_fontHeight : 0;
foreach (auto character, replacedLine) {
if (characterWidth >= width) characterLine += 1;
if (character == '<' && !tagStarted) {
tagStarted = true;
continue;
}

if (tagStarted) {
if (character == '>') tagStarted = false;
continue;
}

if (lineInside <= characterLine) {
if (!m_characterWidths.contains(character)) m_characterWidths[character] = m_fontMetrics.boundingRect(QString(character)).width();
auto oldCharacterWidth = characterWidth;
characterWidth += m_characterWidths[character];

if (oldCharacterWidth < positionX && positionX <= characterWidth) return characterPosition;
}
if (characterLine > lineInside) break;

characterPosition++;
}
} else {
auto characterWidth = 0;
auto characterPosition = 0;
foreach (auto character, line) {
characterWidth += m_characterWidths[character];
if (characterWidth <= positionX) return characterPosition;
characterPosition++;
}
}

return 0;
}

void BackendViewModel::deleteCurrentRequest() noexcept
{
if (m_requests->singleRequest()) addNewRequest();
Expand Down
17 changes: 17 additions & 0 deletions src/ViewModels/backendviewmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#define BACKENDVIEWMODEL_H

#include <QObject>
#include <QFont>
#include <QFontMetrics>
#include "../ViewModels/httpperformerviewmodel.h"
#include "../ViewModels/textadvisorviewmodel.h"
#include "../ListModels/httprequestslistmodel.h"
Expand All @@ -43,6 +45,8 @@ class BackendViewModel : public QObject
Q_PROPERTY(bool openedCommandPalette READ openedCommandPalette NOTIFY openedCommandPaletteChanged)
Q_PROPERTY(OpenApiExporterViewModel* openApiExporter READ openApiExporter NOTIFY openApiExporterChanged)
Q_PROPERTY(GlobalVariablesListModel* globalVariables READ globalVariables NOTIFY globalVariablesChanged)
Q_PROPERTY(QString fontFamily READ fontFamily NOTIFY fontFamilyChanged)
Q_PROPERTY(int fontPointSize READ fontPointSize NOTIFY fontPointSizeChanged)

private:
HttpPerformerViewModel* m_requestPerformer { new HttpPerformerViewModel(this) };
Expand All @@ -58,6 +62,12 @@ class BackendViewModel : public QObject
OpenApiExporterViewModel* m_openApiExporter { new OpenApiExporterViewModel(this) };
GlobalVariablesListModel* m_globalVariables { new GlobalVariablesListModel(this) };
bool m_openApiHelpVisible { false };
QString m_fontFamily { "" };
int m_fontPointSize { 9 };
QFont m_font;
QFontMetrics m_fontMetrics { QFontMetrics(QFont()) };
int m_fontHeight { 0 };
QMap<QChar,int> m_characterWidths { QMap<QChar,int>() };

public:
explicit BackendViewModel(QObject *parent = nullptr);
Expand All @@ -83,6 +93,8 @@ 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 setFontFamily(const QString& family) noexcept;
Q_INVOKABLE int getPositionInText(const QString& line, int positionX, int positionY, int width, bool formatted) noexcept;

void deleteCurrentRequest() noexcept;

Expand All @@ -91,6 +103,9 @@ class BackendViewModel : public QObject

bool openApiHelpVisible() const noexcept { return m_openApiHelpVisible; }

QString fontFamily() const noexcept { return m_fontFamily; }
int fontPointSize() const noexcept { return m_fontPointSize; }

private:
QString removeProtocol(const QString& filePath) noexcept;
void fillAuthorizationSecurity(const QString& key, HttpRequestViewModel* request, const OpenApiRoutesOptions& options);
Expand All @@ -115,6 +130,8 @@ class BackendViewModel : public QObject
void openApiHelpVisibleChanged();
void globalVariablesChanged();
void needGlobalVariablesWindow();
void fontFamilyChanged();
void fontPointSizeChanged();

private slots:
void errorNotification(const QString& message, const QString& title);
Expand Down
49 changes: 49 additions & 0 deletions src/ViewModels/globalmouseviewmodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <QGuiApplication>
#include <QMouseEvent>
#include "globalmouseviewmodel.h"

GlobalMouseViewModel::GlobalMouseViewModel(QObject *parent)
: QObject{parent}
{
QGuiApplication::instance()->installEventFilter(this);
}

bool GlobalMouseViewModel::eventFilter(QObject* watched, QEvent* event)
{
QEvent::Type t = event->type();
if (t == QEvent::MouseMove && m_moveTracking && event->spontaneous()) {
auto mouseEvent = static_cast<QMouseEvent*>(event);
auto position = mouseEvent->position();
m_xCoordinate = position.x();
m_yCoordinate = position.y();
if (m_xCoordinate >= m_leftEdge && m_yCoordinate >= m_topEdge) {
emit mouseMoved(m_xCoordinate, m_yCoordinate);
}
}

return QObject::eventFilter(watched, event);
}

void GlobalMouseViewModel::setLeftEdge(int leftEdge) noexcept
{
if (m_leftEdge == leftEdge) return;

m_leftEdge = leftEdge;
emit leftEdgeChanged();
}

void GlobalMouseViewModel::setTopEdge(int topEdge) noexcept
{
if (m_topEdge == topEdge) return;

m_topEdge = topEdge;
emit topEdgeChanged();
}

void GlobalMouseViewModel::setMoveTracking(bool moveTracking) noexcept
{
if (m_moveTracking == moveTracking) return;

m_moveTracking = moveTracking;
emit moveTrackingChanged();
}
Loading