Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single line text input #424

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
39 changes: 39 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/AdaptiveCardContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ namespace AdaptiveCardQmlEngine
return mCardConfig;
}

void AdaptiveCardContext::addHeightEstimate(const int height)
{
m_HeightEstimate += height;
}

void AdaptiveCardContext::setHeightEstimate(const int height)
{
m_HeightEstimate = height;
}

const int AdaptiveCardContext::getHeightEstimate()
{
return m_HeightEstimate;
}

const int AdaptiveCardQmlEngine::AdaptiveCardContext::getEstimatedTextHeight(const std::string text)
{
auto cardConfig = this->getCardConfig()->getCardConfig();

int height = 0;

height += ((((int(text.size()) * cardConfig.averageCharWidth) / cardConfig.cardWidth) + 1) * cardConfig.averageCharHeight);
height += (int(std::count(text.begin(), text.end(), '\n')) * cardConfig.averageCharHeight);
height += cardConfig.averageSpacing;

return height;
}


QString AdaptiveCardContext::getColor(AdaptiveCards::ForegroundColor color, bool isSubtle, bool highlight, bool isQml)
{
AdaptiveCards::ColorConfig colorConfig;
Expand Down Expand Up @@ -98,5 +127,15 @@ namespace AdaptiveCardQmlEngine
m_lang = lang;
}

const std::vector<AdaptiveWarning>& AdaptiveCardContext::GetWarnings()
{
return m_warnings;
}

void AdaptiveCardContext::AddWarning(const AdaptiveWarning& warning)
{
m_warnings.push_back(warning);
}

} // namespace AdaptiveCardQmlEngine

15 changes: 14 additions & 1 deletion source/qml_v2/AdaptiveCardQmlEngine/AdaptiveCardContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Formatter.h"
#include "utils/Utils.h"
#include "utils/AdaptiveCardEnums.h"
#include "AdaptiveWarning.h"

namespace AdaptiveCardQmlEngine
{
Expand All @@ -37,11 +38,21 @@ namespace AdaptiveCardQmlEngine
std::shared_ptr<AdaptiveCards::HostConfig> getHostConfig();
std::shared_ptr<AdaptiveCardConfig> getCardConfig();

void addHeightEstimate(const int height);
void setHeightEstimate(const int height);
const int getHeightEstimate();

const int getEstimatedTextHeight(const std::string text);

QString getColor(AdaptiveCards::ForegroundColor color, bool isSubtle, bool highlight, bool isQml = true);

std::string getLang();
void setLang(const std::string& lang);

const std::vector<AdaptiveWarning>& GetWarnings();
void AddWarning(const AdaptiveWarning& warning);


private:
AdaptiveCardContext();
~AdaptiveCardContext();
Expand All @@ -54,6 +65,8 @@ namespace AdaptiveCardQmlEngine
std::shared_ptr<AdaptiveCards::HostConfig> mHostConfig;
std::shared_ptr<AdaptiveCardConfig> mCardConfig;
AdaptiveCardEnums::AdaptiveCardTheme mAdaptiveCardTheme;
std::string m_lang;
std::string m_lang;
int m_HeightEstimate{0};
std::vector<AdaptiveWarning> m_warnings;
};
}
5 changes: 5 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/AdaptiveCardQmlTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ namespace AdaptiveCardQmlEngine
qmlRegisterType(QUrl("qrc:qml/CardConstants.qml"), "AdaptiveCardQmlEngine", 1, 0, "CardConstants");

qmlRegisterType(QUrl("qrc:qml/TextBlockRender.qml"), "AdaptiveCardQmlEngine", 1, 0, "TextBlockRender");
qmlRegisterType(QUrl("qrc:qml/SingleLineTextInputRender.qml"), "AdaptiveCardQmlEngine", 1, 0, "SingleLineTextInputRender");
qmlRegisterType(QUrl("qrc:qml/InputLabel.qml"), "AdaptiveCardQmlEngine", 1, 0, "InputLabel");
qmlRegisterType(QUrl("qrc:qml/InputErrorMessage.qml"), "AdaptiveCardQmlEngine", 1, 0, "InputErrorMessage");
qmlRegisterType(QUrl("qrc:qml/InputFieldClearIcon.qml"), "AdaptiveCardQmlEngine", 1, 0, "InputFieldClearIcon");

}
} // namespace RendererQml
17 changes: 10 additions & 7 deletions source/qml_v2/AdaptiveCardQmlEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ file(GLOB_RECURSE SOURCES
"AdaptiveCardController.cpp"
"AdaptiveCardModel.cpp"
"CollectionItemModel.cpp"
"AdaptiveWarning.cpp"

"TextBlockModel.cpp"
"ImageModel.cpp"
"RichTextBlockModel.cpp"
"TextInputModel.cpp"

"AdaptiveCardQmlTypes.h"
"AdaptiveCardUtils.cpp"
Expand All @@ -69,15 +71,16 @@ file(GLOB_RECURSE SOURCES
"Formatter.h"
"Utils.h"
"stdafx.h"

"AdaptiveCardController.h"
"AdaptiveCardModel.h"
"CollectionItemModel.h"
"AdaptiveWarning.h"

"AdaptiveCardController.h"
"AdaptiveCardModel.h"
"CollectionItemModel.h"

"TextBlockModel.h"
"TextBlockModel.h"
"RichTextBlockModel.h"
"TextInputModel.h"
"ImageModel.h"
"TextBlockModel.h"
"RichTextBlockModel.h"
)

# Setup Library
Expand Down
24 changes: 24 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/models/AdaptiveWarning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "pch.h"

#include "AdaptiveWarning.h"

namespace AdaptiveCardQmlEngine
{

AdaptiveWarning::AdaptiveWarning(Code code, const std::string& message) :
m_code(code), m_message(message)
{

}

Code AdaptiveWarning::GetStatusCode() const
{
return m_code;
}

const std::string& AdaptiveWarning::GetReason() const
{
return m_message;
}

}
25 changes: 25 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/models/AdaptiveWarning.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "pch.h"

namespace AdaptiveCardQmlEngine
{

enum class Code
{
RenderException = 1
};

class AdaptiveWarning
{
public:
AdaptiveWarning(Code code, const std::string& message);
Code GetStatusCode() const;
const std::string& GetReason() const;

private:
const Code m_code;
const std::string m_message;
};

}
11 changes: 11 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/models/CollectionItemModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "TextBlockModel.h"
#include "ImageModel.h"
#include "RichTextBlockModel.h"
#include "TextInputModel.h"
#include "AdaptiveCardEnums.h"

CollectionItemModel::CollectionItemModel(std::vector<std::shared_ptr<AdaptiveCards::BaseCardElement>> elements, QObject* parent)
Expand Down Expand Up @@ -43,6 +44,7 @@ QHash<int, QByteArray> CollectionItemModel::roleNames() const
cardListModel[TextBlockRole] = "textBlockRole";
cardListModel[ImageRole] = "imageRole";
cardListModel[RichTextBlockRole] = "richTextBlockRole";
cardListModel[TextInputRole] = "textInputRole";
cardListModel[FillHeightRole] = "fillHeightRole";

return cardListModel;
Expand All @@ -64,6 +66,9 @@ void CollectionItemModel::populateRowData(std::shared_ptr<AdaptiveCards::BaseCar
case AdaptiveCards::CardElementType::RichTextBlock:
populateRichTextBlockModel(std::dynamic_pointer_cast<AdaptiveCards::RichTextBlock>(element), rowContent);
break;
case AdaptiveCards::CardElementType::TextInput:
populateTextInputModel(std::dynamic_pointer_cast<AdaptiveCards::TextInput>(element), rowContent);
break;
default:
break;
}
Expand All @@ -86,3 +91,9 @@ void CollectionItemModel::populateRichTextBlockModel(std::shared_ptr<AdaptiveCar
rowContent[CollectionModelRole::DelegateType] = QVariant::fromValue(AdaptiveCardEnums::CardElementType::RichTextBlock);
rowContent[CollectionModelRole::RichTextBlockRole] = QVariant::fromValue(new RichTextBlockModel(richTextBlock, nullptr));
}

void CollectionItemModel::populateTextInputModel(std::shared_ptr<AdaptiveCards::TextInput> input, RowContent& rowContent)
{
rowContent[CollectionModelRole::DelegateType] = QVariant::fromValue(AdaptiveCardEnums::CardElementType::TextInput);
rowContent[CollectionModelRole::TextInputRole] = QVariant::fromValue(new TextInputModel(input, nullptr));
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
class TextBlockModel;
class ImageModel;
#include "RichTextBlock.h"
#include <TextInput.h>

#include "Enums.h"

class TextBlockModel;
class RichTextBlockModel;
class TextIputModel;

class CollectionItemModel : public QAbstractListModel
{
Expand All @@ -26,6 +28,7 @@ class CollectionItemModel : public QAbstractListModel
TextBlockRole,
ImageRole,
RichTextBlockRole,
TextInputRole,
FillHeightRole
};

Expand All @@ -48,4 +51,5 @@ class CollectionItemModel : public QAbstractListModel
void populateTextBlockModel(std::shared_ptr<AdaptiveCards::TextBlock> textBlock, RowContent& rowContent);
void populateImageModel(std::shared_ptr<AdaptiveCards::Image> image, RowContent& rowContent);
void populateRichTextBlockModel(std::shared_ptr<AdaptiveCards::RichTextBlock> rightTextBlock, RowContent& rowContent);
void populateTextInputModel(std::shared_ptr<AdaptiveCards::TextInput> input, RowContent& rowContent);
};
52 changes: 52 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/models/TextInputModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "TextInputModel.h"
#include "SharedAdaptiveCard.h"
#include <QDebug.h>
#include "Utils.h"
#include "MarkDownParser.h"
#include "AdaptiveWarning.h"

TextInputModel::TextInputModel(std::shared_ptr<AdaptiveCards::TextInput> input, QObject* parent) : QObject(parent)
{
const auto hostConfig = AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().getHostConfig();
const auto rendererConfig = AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().getCardConfig();

const auto textConfig = AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().getCardConfig()->getInputTextConfig();
AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().addHeightEstimate(AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().getEstimatedTextHeight(input->GetLabel()));

std::string mOriginalElementId = input->GetId();

mIsVisible = input->GetIsVisible() ? true : false;
mIsRequired = input->GetIsRequired() == true ? true : false;

mLabel = QString::fromStdString(AdaptiveCardQmlEngine::Utils::getBackQuoteEscapedString(input->GetLabel()));
mErrorMessage = QString::fromStdString(AdaptiveCardQmlEngine::Utils::getBackQuoteEscapedString(input->GetErrorMessage()));

mPlaceHolder = QString::fromStdString(AdaptiveCardQmlEngine::Utils::getBackQuoteEscapedString(input->GetPlaceholder()));
mValue = QString::fromStdString(AdaptiveCardQmlEngine::Utils::getBackQuoteEscapedString(input->GetValue()));

auto spacing = AdaptiveCardQmlEngine::Utils::GetSpacing(AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().getHostConfig()->GetSpacing(), AdaptiveCards::Spacing::Small);
QString color = AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().getColor(AdaptiveCards::ForegroundColor::Default, false, false);

if (input->GetRegex() != "")
{
mRegex = QString::fromStdString(input->GetRegex());
}

mMaxLength = input->GetMaxLength();

if (input->GetLabel().empty())
{
if (input->GetIsRequired())
{
AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().AddWarning(AdaptiveCardQmlEngine::AdaptiveWarning(AdaptiveCardQmlEngine::Code::RenderException, "isRequired is not supported without labels"));
}
}

if (input->GetHeight() == AdaptiveCards::HeightType::Stretch)
{
mHeightStreched = input->GetHeight() == AdaptiveCards::HeightType::Stretch ? "true" : "false";
}
}

TextInputModel::~TextInputModel()
{}
42 changes: 42 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/models/TextInputModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <QObject>
#include <QColor>
#include <QFont>

#include "AdaptiveCardContext.h"
#include "TextInput.h"

class TextInputModel : public QObject
{
Q_OBJECT

Q_PROPERTY(QString label MEMBER mLabel CONSTANT)
Q_PROPERTY(QString errorMessage MEMBER mErrorMessage CONSTANT)
Q_PROPERTY(QString placeholder MEMBER mPlaceHolder CONSTANT)
Q_PROPERTY(QString value MEMBER mValue CONSTANT)
Q_PROPERTY(QString regex MEMBER mRegex CONSTANT)

Q_PROPERTY(bool isVisible MEMBER mIsVisible CONSTANT)
Q_PROPERTY(bool isRequired MEMBER mIsRequired CONSTANT)
Q_PROPERTY(bool heightStreched MEMBER mHeightStreched CONSTANT)

Q_PROPERTY(int maxLength MEMBER mMaxLength CONSTANT)

public:
explicit TextInputModel(std::shared_ptr<AdaptiveCards::TextInput> input, QObject* parent = nullptr);
~TextInputModel();

private:
QString mLabel;
QString mErrorMessage;
QString mPlaceHolder;
QString mValue;
QString mRegex;

bool mIsVisible;
bool mIsRequired;
bool mHeightStreched;

int mMaxLength;
};
4 changes: 2 additions & 2 deletions source/qml_v2/AdaptiveCardQmlEngine/qml/CardConstants.qml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "AdaptiveCardUtils.js" as AdaptiveCardUtils
import "JSUtils/AdaptiveCardUtils.js" as AdaptiveCardUtils
import QtQuick 2.15
pragma Singleton
//pragma Singleton

QtObject {
property bool isDarkTheme: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ Loader {
source = "ImageRender.qml"
else if (model.delegateType == AdaptiveCardEnums.CardElementType.RichTextBlock)
source = "RichTextBlockRender.qml";
else if (model.delegateType == AdaptiveCardEnums.CardElementType.TextInput)
source = "TextInputRender.qml"
}
}
Loading