Skip to content

Commit

Permalink
feat: Use enum values rather than int for recording
Browse files Browse the repository at this point in the history
This makes it easier for a human to read and edit the script after the fact.
Mostly useful for the find API in TextDocument

Fix #177
  • Loading branch information
narnaud committed Nov 29, 2024
1 parent 0168c1d commit c1d6211
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 111 deletions.
16 changes: 8 additions & 8 deletions docs/API/knut/rcdocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ import Knut
|array<[Action](../knut/action.md)> |**[actionsFromMenu](#actionsFromMenu)**(string menuId)|
|array<[Action](../knut/action.md)> |**[actionsFromMenuForLanguage](#actionsFromMenuForLanguage)**(string menuId, string language)|
|array<[Action](../knut/action.md)> |**[actionsFromToolbar](#actionsFromToolbar)**(string toolBarId)|
|void |**[convertActions](#convertActions)**(int flags)|
||**[convertAssets](#convertAssets)**(int flags)|
|void |**[convertActions](#convertActions)**(ConversionFlags flags)|
||**[convertAssets](#convertAssets)**(ConversionFlags flags)|
|string |**[convertLanguageToCode](#convertLanguageToCode)**(string language)|
|[Widget](../knut/widget.md) |**[dialog](#dialog)**(string id, int flags, real scaleX, real scaleY)|
|[Widget](../knut/widget.md) |**[dialog](#dialog)**(string id, ConversionFlags flags, real scaleX, real scaleY)|
|string |**[dialogTitleForLanguage](#dialogTitleForLanguage)**(string language, string dialogId)|
|[Menu](../knut/menu.md) |**[menu](#menu)**(string id)|
|bool |**[mergeAllLanguages](#mergeAllLanguages)**(string language = "[default]")|
Expand All @@ -51,7 +51,7 @@ import Knut
|string |**[stringForLanguage](#stringForLanguage)**(string language, string id)|
|array<[String](../knut/string.md)> |**[stringsForLanguage](#stringsForLanguage)**(string language)|
|[ToolBar](../knut/toolbar.md) |**[toolBar](#toolBar)**(string id)|
|bool |**[writeAssetsToImage](#writeAssetsToImage)**(int flags)|
|bool |**[writeAssetsToImage](#writeAssetsToImage)**(ConversionFlags flags)|
|bool |**[writeAssetsToQrc](#writeAssetsToQrc)**(string fileName)|
|bool |**[writeDialogToUi](#writeDialogToUi)**([Widget](../knut/widget.md) dialog, string fileName)|

Expand Down Expand Up @@ -138,7 +138,7 @@ Returns all actions used in the menu `menuId` for language `language`.

Returns all actions used in the toolbar `toolBarId`.

#### <a name="convertActions"></a>void **convertActions**(int flags)
#### <a name="convertActions"></a>void **convertActions**(ConversionFlags flags)


!!! Warning "Experimental API"
Expand All @@ -153,7 +153,7 @@ The `flags` are used to fill the iconPath of the action:
- `RcDocument.ConvertToPng`: convert BMPs to PNGs, needed if we want to also change the transparency
- `RcDocument.AllFlags`: combination of all above

#### <a name="convertAssets"></a>**convertAssets**(int flags)
#### <a name="convertAssets"></a>**convertAssets**(ConversionFlags flags)

Converts all assets using the `flags`.

Expand All @@ -166,7 +166,7 @@ Converts all assets using the `flags`.

Returns language code as defined by the ISO 639 for language name

#### <a name="dialog"></a>[Widget](../knut/widget.md) **dialog**(string id, int flags, real scaleX, real scaleY)
#### <a name="dialog"></a>[Widget](../knut/widget.md) **dialog**(string id, ConversionFlags flags, real scaleX, real scaleY)

Returns the dialog for the given `id`.

Expand Down Expand Up @@ -235,7 +235,7 @@ Returns translated string for specific `language`.

Returns the toolbar for the given `id`.

#### <a name="writeAssetsToImage"></a>bool **writeAssetsToImage**(int flags)
#### <a name="writeAssetsToImage"></a>bool **writeAssetsToImage**(ConversionFlags flags)

Writes the assets to images, using `flags` for transparency settings. Returns `true` if no issues.

Expand Down
35 changes: 18 additions & 17 deletions docs/API/knut/textdocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Inherited properties: [Document properties](../knut/document.md#properties)
||**[deleteSelection](#deleteSelection)**()|
||**[deleteStartOfLine](#deleteStartOfLine)**()|
||**[deleteStartOfWord](#deleteStartOfWord)**()|
|bool |**[find](#find)**(string text, int options = TextDocument.NoFindFlags)|
|bool |**[findRegexp](#findRegexp)**(string regexp, int options = TextDocument.NoFindFlags)|
|bool |**[find](#find)**(string text, FindFlags options = TextDocument.NoFindFlags)|
|bool |**[findRegexp](#findRegexp)**(string regexp, FindFlags options = TextDocument.NoFindFlags)|
||**[gotoEndOfDocument](#gotoEndOfDocument)**()|
||**[gotoEndOfLine](#gotoEndOfLine)**()|
||**[gotoEndOfWord](#gotoEndOfWord)**()|
Expand All @@ -67,7 +67,7 @@ Inherited properties: [Document properties](../knut/document.md#properties)
||**[insertAtLine](#insertAtLine)**(string text, int line = -1)|
||**[insertAtPosition](#insertAtPosition)**(string text, int pos)|
||**[lineAtPosition](#lineAtPosition)**(int position)|
|bool |**[match](#match)**(string regexp, int options = TextDocument.NoFindFlags)|
|bool |**[match](#match)**(string regexp, FindFlags options = TextDocument.NoFindFlags)|
||**[paste](#paste)**()|
||**[positionAt](#positionAt)**(int line, int col)|
||**[redo](#redo)**(int count)|
Expand All @@ -76,11 +76,11 @@ Inherited properties: [Document properties](../knut/document.md#properties)
||**[replace](#replace)**(int length, string text)|
||**[replace](#replace)**([RangeMark](../knut/rangemark.md) range, string text)|
||**[replace](#replace)**(int from, int to, string text)|
|bool |**[replaceAll](#replaceAll)**(string before, string after, int options = TextDocument.NoFindFlags)|
|bool |**[replaceAllInRange](#replaceAllInRange)**(string before, string after, [RangeMark](../knut/rangemark.md) range, int options = TextDocument.NoFindFlags)|
|bool |**[replaceAllRegexp](#replaceAllRegexp)**(string regexp, string after, int options = TextDocument.NoFindFlags)|
|bool |**[replaceAllRegexpInRange](#replaceAllRegexpInRange)**(string regexp, string after, [RangeMark](../knut/rangemark.md) range, int options = TextDocument.NoFindFlags)|
|bool |**[replaceOne](#replaceOne)**(string before, string after, int options = TextDocument.NoFindFlags)|
|bool |**[replaceAll](#replaceAll)**(string before, string after, FindFlags options = TextDocument.NoFindFlags)|
|bool |**[replaceAllInRange](#replaceAllInRange)**(string before, string after, [RangeMark](../knut/rangemark.md) range, FindFlags options = TextDocument.NoFindFlags)|
|bool |**[replaceAllRegexp](#replaceAllRegexp)**(string regexp, string after, FindFlags options = TextDocument.NoFindFlags)|
|bool |**[replaceAllRegexpInRange](#replaceAllRegexpInRange)**(string regexp, string after, [RangeMark](../knut/rangemark.md) range, FindFlags options = TextDocument.NoFindFlags)|
|bool |**[replaceOne](#replaceOne)**(string before, string after, FindFlags options = TextDocument.NoFindFlags)|
||**[selectAll](#selectAll)**()|
||**[selectEndOfLine](#selectEndOfLine)**()|
||**[selectEndOfWord](#selectEndOfWord)**()|
Expand Down Expand Up @@ -224,7 +224,7 @@ Deletes from the cursor position to the start of the line.

Deletes from the cursor position to the start of the word.

#### <a name="find"></a>bool **find**(string text, int options = TextDocument.NoFindFlags)
#### <a name="find"></a>bool **find**(string text, FindFlags options = TextDocument.NoFindFlags)

Searches the string `text` in the editor. Options could be a combination of:

Expand All @@ -235,7 +235,7 @@ Searches the string `text` in the editor. Options could be a combination of:

Selects the match and returns `true` if a match is found.

#### <a name="findRegexp"></a>bool **findRegexp**(string regexp, int options = TextDocument.NoFindFlags)
#### <a name="findRegexp"></a>bool **findRegexp**(string regexp, FindFlags options = TextDocument.NoFindFlags)

Searches the string `regexp` in the editor using a regular expression. Options could be a combination of:

Expand Down Expand Up @@ -329,7 +329,7 @@ Inserts the string `text` at `pos`.

Returns the line number for the given text cursor `position`. Or -1 if position is invalid

#### <a name="match"></a>bool **match**(string regexp, int options = TextDocument.NoFindFlags)
#### <a name="match"></a>bool **match**(string regexp, FindFlags options = TextDocument.NoFindFlags)

Searches the string `regexp` in the editor using a regular expression. Options could be a combination of:

Expand Down Expand Up @@ -371,7 +371,7 @@ Replaces the text in the range `range` with the string `text`.

Replaces the text from `from` to `to` with the string `text`.

#### <a name="replaceAll"></a>bool **replaceAll**(string before, string after, int options = TextDocument.NoFindFlags)
#### <a name="replaceAll"></a>bool **replaceAll**(string before, string after, FindFlags options = TextDocument.NoFindFlags)

Replaces all occurrences of the string `before` with `after`. Options could be a combination of:

Expand All @@ -391,30 +391,31 @@ the occurrence only.

Returns the number of changes done in the document.

#### <a name="replaceAllInRange"></a>bool **replaceAllInRange**(string before, string after, [RangeMark](../knut/rangemark.md) range, int options = TextDocument.NoFindFlags)
#### <a name="replaceAllInRange"></a>bool **replaceAllInRange**(string before, string after, [RangeMark](../knut/rangemark.md) range, FindFlags options = TextDocument.NoFindFlags)

Replaces all occurrences of the string `before` with `after` in the given `range`. See the
options from `replaceAll`.

Returns the number of changes done in the document.

#### <a name="replaceAllRegexp"></a>bool **replaceAllRegexp**(string regexp, string after, int options = TextDocument.NoFindFlags)
#### <a name="replaceAllRegexp"></a>bool **replaceAllRegexp**(string regexp, string after, FindFlags options = TextDocument.NoFindFlags)

Replaces all occurrences of the matches for the `regexp` with `after`. See the options from `replaceAll`.
Replaces all occurrences of the matches for the `regexp` with `after`. See the options from
`replaceAll`.

The captures coming from the regexp can be used in the replacement text, using `\1`..`\n` or `$1`..`$n`.

Returns the number of changes done in the document.

#### <a name="replaceAllRegexpInRange"></a>bool **replaceAllRegexpInRange**(string regexp, string after, [RangeMark](../knut/rangemark.md) range, int options = TextDocument.NoFindFlags)
#### <a name="replaceAllRegexpInRange"></a>bool **replaceAllRegexpInRange**(string regexp, string after, [RangeMark](../knut/rangemark.md) range, FindFlags options = TextDocument.NoFindFlags)

Replaces all occurrences of the matches for the `regexp` with `after` in the given `range`. See the options from `replaceAll`.

The captures coming from the regexp can be used in the replacement text, using `\1`..`\n` or `$1`..`$n`.

Returns the number of changes done in the document.

#### <a name="replaceOne"></a>bool **replaceOne**(string before, string after, int options = TextDocument.NoFindFlags)
#### <a name="replaceOne"></a>bool **replaceOne**(string before, string after, FindFlags options = TextDocument.NoFindFlags)

Replaces one occurrence of the string `before` with `after`. Options could be a combination of:

Expand Down
31 changes: 6 additions & 25 deletions src/core/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,6 @@ int HistoryModel::columnCount(const QModelIndex &parent) const
return ColumnCount;
}

static QString variantToString(const QVariant &variant)
{
QString text = variant.toString();
if (static_cast<QMetaType::Type>(variant.typeId()) == QMetaType::QString) {
text.replace('\\', R"(\\)");
text.replace('\n', R"(\n)");
text.replace('\t', R"(\t)");
text.replace('"', R"(\")");
text.append('"');
text.prepend('"');
} else if (variant.metaType().flags().testAnyFlag(QMetaType::IsEnumeration)) {
QString className = variant.metaType().metaObject()->className();
className = className.split("::").last();
text = className + '.' + text;
}
return text;
}

QVariant HistoryModel::data(const QModelIndex &index, int role) const
{
Q_ASSERT(checkIndex(index, CheckIndexOption::IndexIsValid));
Expand All @@ -87,7 +69,7 @@ QVariant HistoryModel::data(const QModelIndex &index, int role) const
const auto &params = m_data.at(index.row()).params;
QStringList paramStrings;
for (const auto &param : params) {
QString text = variantToString(param.value);
QString text = param.value;
if (!param.name.isEmpty())
text.prepend(QString("%1: ").arg(param.name));
paramStrings.push_back(text);
Expand Down Expand Up @@ -164,8 +146,7 @@ QString HistoryModel::createScript(int start, int end)
continue;
}

QString text = variantToString(param.value);
paramStrings.push_back(text);
paramStrings.push_back(param.value);
}

if (isProperty) {
Expand Down Expand Up @@ -208,15 +189,15 @@ void HistoryModel::addData(LogData &&data, bool merge)
for (size_t i = 0; i < data.params.size(); ++i) {
const auto &param = data.params[i];
auto &lastParam = lastData.params[i];
switch (static_cast<QMetaType::Type>(param.value.typeId())) {
switch (static_cast<QMetaType::Type>(param.type)) {
case QMetaType::Int:
lastParam.value = lastParam.value.toInt() + param.value.toInt();
lastParam.value = QString::number(lastParam.value.toInt() + param.value.toInt());
break;
case QMetaType::QString:
lastParam.value = lastParam.value.toString() + param.value.toString();
lastParam.value = lastParam.value.chopped(1) + param.value.sliced(1);
break;
case QMetaType::QStringList:
lastParam.value = lastParam.value.toStringList() + param.value.toStringList();
lastParam.value = lastParam.value.chopped(1) + ", " + param.value.sliced(1);
break;
default:
Q_UNREACHABLE();
Expand Down
43 changes: 38 additions & 5 deletions src/core/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,41 @@ concept HasToString = requires(const T &t) { t.toString(); };
template <typename T>
concept HasPointerToString = requires(const T &t) { t->toString(); };

template <class T>
QString flagsToString(const QFlags<T> &flags)
{
const auto metaEnum = QMetaEnum::fromType<T>();
QString className = QMetaType::fromType<T>().metaObject()->className();
// Remove potential namespace, we only want the class name
className = className.split("::").last();
QString text;
for (int i = 0; i < metaEnum.keyCount(); ++i) {
if (flags.testFlag(static_cast<T>(metaEnum.value(i)))) {
if (!text.isEmpty())
text += " | ";
text += QString("%1.%2").arg(className, metaEnum.key(i));
}
}
return text;
}

/**
* @brief toString
* Returns a string for any kind of data you can pass as a parameter.
*/
template <class T>
QString valueToString(const T &data)
QString valueToString(const T &data, bool escape = false)
{
if constexpr (std::is_same_v<std::remove_cvref_t<T>, QString>) {
QString text = data;
text.replace('\n', "\\n");
text.replace('\t', "\\t");
if (escape) {
text.replace('\\', R"(\\)");
text.replace('"', R"(\")");
text.append('"');
text.prepend('"');
}
return text;
} else if constexpr (std::is_same_v<std::remove_cvref_t<T>, bool>)
return data ? "true" : "false";
Expand All @@ -105,7 +129,9 @@ QString valueToString(const T &data)
return data.toString();
else if constexpr (HasPointerToString<T>)
return data->toString();
else
else if constexpr (std::is_same_v<std::remove_cvref_t<T>, QFlags<typename T::enum_type>>) {
return flagsToString(data);
} else
Q_UNREACHABLE();
return {};
}
Expand Down Expand Up @@ -159,6 +185,12 @@ class HistoryModel : public QAbstractTableModel
friend class LoggerObject;

struct Arg
{
QString name;
QString value;
int type;
};
struct ReturnArg
{
QString name;
QVariant value;
Expand All @@ -168,7 +200,7 @@ class HistoryModel : public QAbstractTableModel
{
QString name;
std::vector<Arg> params;
Arg returnArg;
ReturnArg returnArg;
};

void logData(const QString &name);
Expand All @@ -194,9 +226,10 @@ class HistoryModel : public QAbstractTableModel
void fillLogData(LogData &data, T param, Ts... params)
{
if constexpr (std::derived_from<T, LoggerArgBase>)
data.params.push_back({param.argName, QVariant::fromValue(param.value)});
data.params.push_back(
{param.argName, valueToString(param.value, true), qMetaTypeId<decltype(param.value)>()});
else
data.params.push_back({"", QVariant::fromValue(param)});
data.params.push_back({"", valueToString(param, true), qMetaTypeId<T>()});

fillLogData(data, params...);
}
Expand Down
Loading

0 comments on commit c1d6211

Please sign in to comment.