WSF
EditorHelpers.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2019 Infoscitex, a DCS Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11
12#ifndef EDITOR_HELPERS_HPP
13#define EDITOR_HELPERS_HPP
14
15#include <sstream>
16
17#include <QString>
18#include <QStringList>
19#include <QTextCursor>
20
21// Util Includes
22#include "UtCompilerAttributes.hpp"
23class UtTextDocumentRange;
24
25// WKF Includes
26#include "WkfEnvironment.hpp"
27#include "WkfUnitsObject.hpp"
28
29// WSF Includes
30#include "WsfPProxyNode.hpp"
31
32// Wizard Includes
33#include "Editor.hpp"
34#include "ViExport.hpp"
35
36namespace vespa
37{
38
39class VaPosition;
40
41}
42
43namespace wizard
44{
45
46class Editor;
47
48namespace EditorHelpers
49{
50
56QString VI_EXPORT GetInsertWithComments(const UtTextDocumentRange& aEntryRange,
57 const QString& aAttributeName,
58 const QString& aInsert);
59
66CPP17_NODISCARD bool VI_EXPORT ReplaceTextInEditor(const UtTextDocumentRange& aEntryRange,
67 const QString& aAttributeName,
68 const QString& aInsert);
69
73QTextCursor VI_EXPORT FindStartCursor(const WsfPProxyNode& aNode) noexcept;
74
78QTextCursor VI_EXPORT FindEndCursor(const WsfPProxyNode& aNode) noexcept;
79
83int VI_EXPORT GetIndentLevel(const QString& aLine) noexcept;
84
90QString VI_EXPORT ConstructTextToAppendToBlock(const QTextCursor& aStartCursor,
91 const QTextCursor& aEndCursor,
92 const QString& aAttributeName,
93 const QString& aDataValue) noexcept;
94
99void VI_EXPORT AddNewAttributeInText(const WsfPProxyNode& aProxyNode,
100 const QString& aAttributeName,
101 const QString& aDataValue) noexcept;
102
108bool VI_EXPORT MoveToPreviousNonWhitespaceToken(QTextCursor& aCursor,
109 QTextCursor::MoveMode aMoveMode,
110 int aOperationCount = 1) noexcept;
111
115bool VI_EXPORT IsWhitespaceString(const QString& aText) noexcept;
116
121bool VI_EXPORT ReplacePositionText(QStringList& aPositionTokens,
122 int aCommandIndex,
123 const vespa::VaPosition& aNewPosition) noexcept;
124
129bool VI_EXPORT ReplaceMGRS_Text(QStringList& aMGRS_Tokens, int aCommandIndex, const vespa::VaPosition& aNewPosition) noexcept;
130
135template<typename UNITARY_PROPERTY>
136bool ReplaceUnitaryText(QStringList& aUnitaryTokens, int aCommandIndex, UNITARY_PROPERTY& aNewUnitary) noexcept
137{
138 static_assert(std::is_base_of<UtUnitaryValue<typename UNITARY_PROPERTY::UnitType>, UNITARY_PROPERTY>::value,
139 "UNITARY_PROPERTY must derive from UtUnitaryValue");
140
141 // Read the unitary property's value
142 int valueIndex{wizard::Editor::FindNextValidToken(aUnitaryTokens, aCommandIndex + 1)};
143 if (valueIndex >= 0)
144 {
145 // Read the unitary property's unit
146 int unitIndex{wizard::Editor::FindNextValidToken(aUnitaryTokens, valueIndex + 1)};
147 if (unitIndex >= 0)
148 {
149 // Edit the unitary property
150 QString unitaryValueStr;
151 std::string unitaryUnitStr{aUnitaryTokens[unitIndex].toStdString()};
152 if (UtUnitAngle::IsUnitValid(unitaryUnitStr))
153 {
154 // Convert the unitary property to the preferred unit
155 if (aNewUnitary.ConvertToUnit(unitaryUnitStr))
156 {
157 // Convert the unitary property to a string
158 std::ostringstream oss;
159 oss << aNewUnitary;
160 unitaryValueStr = QString::fromStdString(oss.str());
161 // Split the unitary property and unit
162 QStringList unitaryValueStrSplit{unitaryValueStr.split(' ')};
163 if (unitaryValueStrSplit.size() == 2)
164 {
165 // Store the unitary property value
166 unitaryValueStr = unitaryValueStrSplit[0];
167 }
168 }
169 }
170
171 // Replace the old unitary property with the new unitary property
172 aUnitaryTokens[valueIndex] = unitaryValueStr;
173 // The replacement succeeded
174 return true;
175 }
176 }
177 // The replacement failed
178 return false;
179}
180
186void VI_EXPORT InsertTextAndResetCursor(wizard::Editor* aEditorPtr, QTextCursor& aCursor, const QString& aTextToInsert) noexcept;
187
192QString VI_EXPORT GetPositionString(const vespa::VaPosition& aPosition) noexcept;
193
198template<wkf::ValueType UNITARY_TYPE, typename UNITARY_PROPERTY>
199QString GetUnitaryString(UNITARY_PROPERTY& aUnitary) noexcept
200{
201 static_assert(std::is_base_of<UtUnitaryValue<typename UNITARY_PROPERTY::UnitType>, UNITARY_PROPERTY>::value,
202 "UNITARY_PROPERTY must derive from UtUnitaryValue");
203
204 // Get the unitary property's unit preference
205 int unitaryUnit{wkfEnv.GetPreferenceObject<wkf::UnitsObject>()->GetUnit(UNITARY_TYPE)};
206 // Convert the unitary property to the preferred unit
207 aUnitary.ConvertToUnit(unitaryUnit);
208 // Convert the unitary property to a string
209 std::ostringstream oss;
210 oss << aUnitary;
211 // The unitary property string contains the value AND unit strings delimited by a single ' ' character
212 return QString::fromStdString(oss.str());
213}
214
215} // namespace EditorHelpers
216} // namespace wizard
217
218#endif // EDITOR_HELPERS_HPP
#define VI_EXPORT
Definition ViExport.hpp:38
Definition WsfPProxyNode.hpp:60
Definition Editor.hpp:57
static int FindNextValidToken(const QStringList &aList, int aStart)
Definition Editor.cpp:1258
Definition EditorHelpers.hpp:37
bool ReplaceTextInEditor(const UtTextDocumentRange &aEntryRange, const QString &aAttributeName, const QString &aInsert)
Definition EditorHelpers.cpp:113
QTextCursor FindStartCursor(const WsfPProxyNode &aNode) noexcept
Definition EditorHelpers.cpp:167
QString GetInsertWithComments(const UtTextDocumentRange &aEntryRange, const QString &aAttributeName, const QString &aInsert)
Definition EditorHelpers.cpp:41
bool ReplaceUnitaryText(QStringList &aUnitaryTokens, int aCommandIndex, UNITARY_PROPERTY &aNewUnitary) noexcept
Definition EditorHelpers.hpp:136
bool IsWhitespaceString(const QString &aText) noexcept
Definition EditorHelpers.cpp:286
QString ConstructTextToAppendToBlock(const QTextCursor &aStartCursor, const QTextCursor &aEndCursor, const QString &aAttributeName, const QString &aDataValue) noexcept
Definition EditorHelpers.cpp:206
int GetIndentLevel(const QString &aLine) noexcept
Definition EditorHelpers.cpp:194
bool MoveToPreviousNonWhitespaceToken(QTextCursor &aCursor, QTextCursor::MoveMode aMoveMode, int aOperationCount) noexcept
Definition EditorHelpers.cpp:261
bool ReplacePositionText(QStringList &aPositionTokens, int aCommandIndex, const vespa::VaPosition &aNewPosition) noexcept
Definition EditorHelpers.cpp:298
QString GetPositionString(const vespa::VaPosition &aPosition) noexcept
Definition EditorHelpers.cpp:354
QTextCursor FindEndCursor(const WsfPProxyNode &aNode) noexcept
Definition EditorHelpers.cpp:178
QString GetUnitaryString(UNITARY_PROPERTY &aUnitary) noexcept
Definition EditorHelpers.hpp:199
void AddNewAttributeInText(const WsfPProxyNode &aProxyNode, const QString &aAttributeName, const QString &aDataValue) noexcept
Definition EditorHelpers.cpp:228
bool ReplaceMGRS_Text(QStringList &aMGRS_Tokens, int aCommandIndex, const vespa::VaPosition &aNewPosition) noexcept
Definition EditorHelpers.cpp:328
void InsertTextAndResetCursor(wizard::Editor *aEditorPtr, QTextCursor &aCursor, const QString &aTextToInsert) noexcept
Definition EditorHelpers.cpp:347
Definition Runner.hpp:22
Copyrights Multiple, All Rights Reserved