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

Implemented Number Input UI with Actual Data. #426

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
49 changes: 49 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/AdaptiveCardContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,54 @@ namespace AdaptiveCardQmlEngine
m_lang = lang;
}

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 mCardConfig = this->getCardConfig()->getCardConfig();

int height = 0;

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

return height;
}

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

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


void AdaptiveCardQmlEngine::AdaptiveCardContext::addToRequiredInputElementsIdList(const std::string& elementId)
{
m_RequiredInputElementsIdList.push_back(elementId);
}

const std::vector<std::string>& AdaptiveCardQmlEngine::AdaptiveCardContext::getRequiredInputElementsIdList()
{
return m_RequiredInputElementsIdList;
}

} // namespace AdaptiveCardQmlEngine

22 changes: 21 additions & 1 deletion source/qml_v2/AdaptiveCardQmlEngine/AdaptiveCardContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "AdaptiveCardLightThemeConfig.h"

#include "AdaptiveRenderArgs.h"
#include "AdaptiveWarning.h"

#include "HostConfig.h"
#include "Formatter.h"
Expand Down Expand Up @@ -42,6 +43,19 @@ namespace AdaptiveCardQmlEngine
std::string getLang();
void setLang(const std::string& lang);

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

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

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

void addToRequiredInputElementsIdList(const std::string& elementId);
const std::vector<std::string>& getRequiredInputElementsIdList();


private:
AdaptiveCardContext();
~AdaptiveCardContext();
Expand All @@ -54,6 +68,12 @@ namespace AdaptiveCardQmlEngine
std::shared_ptr<AdaptiveCards::HostConfig> mHostConfig;
std::shared_ptr<AdaptiveCardConfig> mCardConfig;
AdaptiveCardEnums::AdaptiveCardTheme mAdaptiveCardTheme;
std::string m_lang;
std::string m_lang;
std::vector<std::string> m_RequiredInputElementsIdList;


std::vector<AdaptiveWarning> m_warnings;

int m_HeightEstimate{0};
};
}
6 changes: 6 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/AdaptiveCardQmlTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ 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/ImageRender.qml"), "AdaptiveCardQmlEngine", 1, 0, "ImageRender");
qmlRegisterType(QUrl("qrc:qml/RichTextBlockRender.qml"), "AdaptiveCardQmlEngine", 1, 0, "RichTextBlockRender");
qmlRegisterType(QUrl("qrc:qml/NumberInputRender.qml"), "AdaptiveCardQmlEngine", 1, 0, "NumberInputRender");
qmlRegisterType(QUrl("qrc:qml/InputFieldClearIcon.qml"), "AdaptiveCardQmlEngine", 1, 0, "InputFieldClearIcon");
qmlRegisterType(QUrl("qrc:qml/InputLabel.qml"), "AdaptiveCardQmlEngine", 1, 0, "InputLabel");
qmlRegisterType(QUrl("qrc:qml/InputErrorMessage.qml"), "AdaptiveCardQmlEngine", 1, 0, "InputErrorMessage");
}
} // namespace RendererQml
12 changes: 8 additions & 4 deletions source/qml_v2/AdaptiveCardQmlEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ file(GLOB_RECURSE SOURCES
"TextBlockModel.cpp"
"ImageModel.cpp"
"RichTextBlockModel.cpp"
"NumberInputModel.cpp"
"AdaptiveWarning.cpp"


"AdaptiveCardQmlTypes.h"
"AdaptiveCardUtils.cpp"
Expand All @@ -74,10 +77,11 @@ file(GLOB_RECURSE SOURCES
"AdaptiveCardModel.h"
"CollectionItemModel.h"

"TextBlockModel.h"
"ImageModel.h"
"TextBlockModel.h"
"RichTextBlockModel.h"
"TextBlockModel.h"
"ImageModel.h"
"RichTextBlockModel.h"
"NumberInputModel.h"
"AdaptiveWarning.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 "NumberInputModel.h"
#include "AdaptiveCardEnums.h"

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

cardListModel[FillHeightRole] = "fillHeightRole";

return cardListModel;
Expand All @@ -64,6 +67,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::NumberInput:
populateNumberInputModel(std::dynamic_pointer_cast<AdaptiveCards::NumberInput>(element), rowContent);
break;
default:
break;
}
Expand All @@ -86,3 +92,8 @@ 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::populateNumberInputModel(std::shared_ptr<AdaptiveCards::NumberInput> numberInput, RowContent& rowContent)
{
rowContent[CollectionModelRole::DelegateType] = QVariant::fromValue(AdaptiveCardEnums::CardElementType::NumberInput);
rowContent[CollectionModelRole::NumberInputRole] = QVariant::fromValue(new NumberInputModel(numberInput, nullptr));
}
13 changes: 7 additions & 6 deletions source/qml_v2/AdaptiveCardQmlEngine/models/CollectionItemModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

#include <QAbstractListModel>
#include <SharedAdaptiveCard.h>

#include <TextBlock.h>
#include <Image.h>
#include "Enums.h"

class TextBlockModel;
class ImageModel;
#include "RichTextBlock.h"

#include <NumberInput.h>
#include "Enums.h"


class TextBlockModel;
class ImageModel;
class RichTextBlockModel;
class NumberInputModel;


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

Expand All @@ -48,4 +48,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 populateNumberInputModel(std::shared_ptr<AdaptiveCards::NumberInput> numberInput, RowContent& rowContent);
};
1 change: 1 addition & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/models/ImageModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


ImageModel::ImageModel(std::shared_ptr<AdaptiveCards::Image> image, QObject* parent)

: QObject(parent),mImage(image)
{
setImageLayoutProperties();
Expand Down
2 changes: 2 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/models/ImageModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ImageModel : public QObject
Q_PROPERTY(QString anchorCenter MEMBER mAnchorCenter CONSTANT);
Q_PROPERTY(QString anchorRight MEMBER mAnchorRight CONSTANT);
Q_PROPERTY(QString anchorLeft MEMBER mAnchorLeft CONSTANT);

Q_PROPERTY(QString actionType MEMBER mActionType CONSTANT);
Q_PROPERTY(QString submitData MEMBER mSubmitJSON CONSTANT);
Q_PROPERTY(QString openUrl MEMBER mOpenUrl CONSTANT);
Expand All @@ -39,6 +40,7 @@ class ImageModel : public QObject
private:
QString GetImagePath(const std::string url);


void setImageLayoutProperties();
void setImageVisualProperties();
void setImageActionProperties();
Expand Down
76 changes: 76 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/models/NumberInputModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "NumberInputModel.h"
#include "SharedAdaptiveCard.h"
#include <QDebug.h>
#include "Utils.h"
#include "MarkDownParser.h"

NumberInputModel::NumberInputModel(std::shared_ptr<AdaptiveCards::NumberInput> numberInput, QObject* parent) :
QObject(parent), mInput(numberInput)

{
initialize();
createInputLabel();
createErrorMessage();
}

void NumberInputModel::initialize()
{
const auto hostConfig = AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().getHostConfig();

mVisible = mInput->GetIsVisible();
mPlaceholder = QString::fromStdString(AdaptiveCardQmlEngine::Utils::getBackQuoteEscapedString(mInput->GetPlaceholder()));

if (mInput->GetValue().has_value())
{
mValue = mInput->GetValue().value();
mDefaultValue = "true";
}
mMinValue = mInput->GetMin().value_or(-DBL_MAX);
mMaxValue = mInput->GetMax().value_or(DBL_MAX);

if (mInput->GetIsRequired() || mInput->GetMin().has_value() || mInput->GetMax().has_value())
{
AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().addToRequiredInputElementsIdList(mInput->GetId());
mIsRequired = mInput->GetIsRequired();
mValidationRequired = mInput->GetIsRequired();
}
}

void NumberInputModel::createInputLabel()
{
if (!mInput->GetLabel().empty())
{
AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().addHeightEstimate(
AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().getEstimatedTextHeight(mInput->GetLabel()));
mEscapedLabelString =
QString::fromStdString(AdaptiveCardQmlEngine::Utils::getBackQuoteEscapedString(mInput->GetLabel()));
}
else
{
if (mInput->GetIsRequired())
{
AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().AddWarning(AdaptiveCardQmlEngine::AdaptiveWarning(
AdaptiveCardQmlEngine::Code::RenderException, "isRequired is not supported without labels"));
}
}
}

void NumberInputModel::createErrorMessage()
{
if (!mInput->GetErrorMessage().empty())
{
// mNumberInputQmlElement->Property("_mEscapedErrorString",RendererQml::Formatter() << "String.raw`"<< RendererQml::Utils::getBackQuoteEscapedString(mInput->GetErrorMessage()) << "`");
mEscapedErrorString = QString::fromStdString(AdaptiveCardQmlEngine::Utils::getBackQuoteEscapedString(mInput->GetErrorMessage()));
}
else
{
if (mInput->GetIsRequired())
{
AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().AddWarning(AdaptiveCardQmlEngine::AdaptiveWarning(AdaptiveCardQmlEngine::Code::RenderException, "isRequired is not supported without error message"));
}
}
}

NumberInputModel::~NumberInputModel()
{
}
Loading