Skip to content

Commit

Permalink
+ Added restore cursor on position after inserted global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
trueromanus committed Mar 10, 2024
1 parent 06771f7 commit a0eff52
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/ListModels/globalvariableslistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ void GlobalVariablesListModel::setSelectedTab(const QString &selectedTab) noexce
emit selectedTabChanged();
}

void GlobalVariablesListModel::refreshVariableNames() noexcept
{
emit variableNamesChanged();
}

bool GlobalVariablesListModel::shortcutHandler(const QString &shortcut) noexcept
{
if (!m_shortcutCommandMapping.contains(shortcut)) return false;
Expand Down
1 change: 1 addition & 0 deletions src/ListModels/globalvariableslistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class GlobalVariablesListModel : public QAbstractListModel
void setSelectedTab(const QString& selectedTab) noexcept;

QStringList variableNames() const noexcept { return m_variables.keys(); }
void refreshVariableNames() noexcept;

Q_INVOKABLE bool shortcutHandler(const QString& shortcut) noexcept;
Q_INVOKABLE void fillLines();
Expand Down
8 changes: 7 additions & 1 deletion src/ViewModels/backendviewmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,18 @@ bool BackendViewModel::shortcutHandler(const QString &shortcut) noexcept
}

if (shortcut == "enter") {
auto lastPosition = 0;
if (m_selectedGlobalVariableIndex > -1) {
auto variable = m_globalVariables->variableNames().value(m_selectedGlobalVariableIndex);
m_requests->selectedItem()->requestModel()->insertGlobalVariableToCursor(variable);
lastPosition = m_requests->selectedItem()->requestModel()->insertGlobalVariableToCursor(variable);
m_globalVariables->refreshVariableNames();
}
m_selectedGlobalVariableIndex = -1;
m_selectedGlobalVariable = "";
m_showGlobalVariablesPopup = false;
emit showGlobalVariablesPopupChanged();
emit selectedGlobalVariableChanged();
emit globalVariableSelected(lastPosition);
return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/ViewModels/backendviewmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class BackendViewModel : public QObject
void focusedHelpTextFieldChanged();
void showGlobalVariablesPopupChanged();
void selectedGlobalVariableChanged();
void globalVariableSelected(int lastPosition);

private slots:
void errorNotification(const QString& message, const QString& title);
Expand Down
12 changes: 11 additions & 1 deletion src/ViewModels/httprequestviewmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ QVariant HttpRequestViewModel::data(const QModelIndex &index, int role) const
case IsFocusedRole: {
return QVariant(rowIndex == m_selectedItem);
}
case LastCursorPositionRole: {
return QVariant(item->cursor());
}
}

return QVariant();
Expand Down Expand Up @@ -113,6 +116,10 @@ QHash<int, QByteArray> HttpRequestViewModel::roleNames() const
{
IsFocusedRole,
"isNeedFocused"
},
{
LastCursorPositionRole,
"lastCursorPosition"
}
};
}
Expand Down Expand Up @@ -616,7 +623,7 @@ QStringList HttpRequestViewModel::getAllFieldsAsList() const noexcept
return result;
}

void HttpRequestViewModel::insertGlobalVariableToCursor(const QString &variable) noexcept
int HttpRequestViewModel::insertGlobalVariableToCursor(const QString &variable) noexcept
{
auto item = m_items->value(m_selectedItem);
auto content = item->text();
Expand All @@ -629,8 +636,11 @@ void HttpRequestViewModel::insertGlobalVariableToCursor(const QString &variable)
}

item->setText(content);
item->setCursor(cursorIndex + insertedVariable.size());

emit dataChanged(index(m_selectedItem, 0), index(m_selectedItem, 0));

return cursorIndex + insertedVariable.size();
}

QString HttpRequestViewModel::getTypeColor(int type) const
Expand Down
7 changes: 4 additions & 3 deletions src/ViewModels/httprequestviewmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class HttpRequestViewModel : public QAbstractListModel
PastryType,
RouteType,
OptionsType,
PostScriptType,
PostScriptType
};

private:
Expand All @@ -63,7 +63,8 @@ class HttpRequestViewModel : public QAbstractListModel
TextRole,
IsActiveRole,
IndexRole,
IsFocusedRole
IsFocusedRole,
LastCursorPositionRole
};

public:
Expand Down Expand Up @@ -108,7 +109,7 @@ class HttpRequestViewModel : public QAbstractListModel
void sortingFields(const bool descending) noexcept;
QString getAllFields() const noexcept;
QStringList getAllFieldsAsList() const noexcept;
void insertGlobalVariableToCursor(const QString& variable) noexcept;
int insertGlobalVariableToCursor(const QString& variable) noexcept;

private:
QString getTypeColor(int type) const;
Expand Down
16 changes: 16 additions & 0 deletions src/Views/HttpRequestEditor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Item {
required property bool isActive
required property string textContent
required property int currentIndex
property int lastCursorPosition

onIsNeedFocusedChanged: {
if (isNeedFocused) {
Expand Down Expand Up @@ -152,6 +153,21 @@ Item {
}
}

Connections {
target: backend
function onGlobalVariableSelected(lastPosition) {
let activeItem;
for (const item of listView.contentItem.children) {
if (item.isActive) activeItem = item;
}

if (!activeItem) return;

const textField = activeItem.children[0];
textField.cursorPosition = lastPosition;
}
}

Text {
id: emptyText
}
Expand Down

0 comments on commit a0eff52

Please sign in to comment.