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

Base Adaptive Card property #422

Open
wants to merge 4 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
37 changes: 24 additions & 13 deletions source/qml_v2/AdaptiveCardQmlEngine/AdaptiveCardContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

namespace AdaptiveCardQmlEngine
{
AdaptiveCardContext::AdaptiveCardContext()
: mHostConfig(nullptr)
AdaptiveCardContext::AdaptiveCardContext() : mHostConfig(nullptr)
{
}

Expand All @@ -14,7 +13,7 @@ namespace AdaptiveCardQmlEngine

void AdaptiveCardContext::initAdaptiveCardContext()
{
// Initializing Host config and Card config
// Initializing Host config and Card config
mCardConfig = std::make_shared<AdaptiveCardQmlEngine::AdaptiveCardConfig>(true);
mHostConfig = std::make_shared<AdaptiveCards::HostConfig>(AdaptiveCards::HostConfig::DeserializeFromString(DarkConfig::darkConfig));
}
Expand All @@ -23,20 +22,21 @@ namespace AdaptiveCardQmlEngine
{
mAdaptiveCardTheme = theme;

// ReInitializing AdaptiveCard and Host config
// ReInitializing AdaptiveCard and Host config
mCardConfig = std::make_shared<AdaptiveCardConfig>(mAdaptiveCardTheme == AdaptiveCardEnums::AdaptiveCardTheme::DarkTheme ? true : false);

if (mAdaptiveCardTheme == AdaptiveCardEnums::AdaptiveCardTheme::DarkTheme)
{
mHostConfig = std::make_shared<AdaptiveCards::HostConfig>(AdaptiveCards::HostConfig::DeserializeFromString(DarkConfig::darkConfig));
}
mHostConfig =
std::make_shared<AdaptiveCards::HostConfig>(AdaptiveCards::HostConfig::DeserializeFromString(DarkConfig::darkConfig));
}
else
{
mHostConfig = std::make_shared<AdaptiveCards::HostConfig>(AdaptiveCards::HostConfig::DeserializeFromString(LightConfig::lightConfig));
}
mHostConfig = std::make_shared<AdaptiveCards::HostConfig>(
AdaptiveCards::HostConfig::DeserializeFromString(LightConfig::lightConfig));
}
}


std::shared_ptr<AdaptiveCards::HostConfig> AdaptiveCardContext::getHostConfig()
{
return mHostConfig;
Expand All @@ -49,7 +49,7 @@ namespace AdaptiveCardQmlEngine

QString AdaptiveCardContext::getColor(AdaptiveCards::ForegroundColor color, bool isSubtle, bool highlight, bool isQml)
{
AdaptiveCards::ColorConfig colorConfig;
AdaptiveCards::ColorConfig colorConfig;
switch (color)
{
case AdaptiveCards::ForegroundColor::Accent:
Expand All @@ -73,8 +73,8 @@ namespace AdaptiveCardQmlEngine
default:
if (mAdaptiveCardTheme == AdaptiveCardEnums::AdaptiveCardTheme::DarkTheme)
{
colorConfig = mRenderArgs.GetForegroundColors().light;
}
colorConfig = mRenderArgs.GetForegroundColors().light;
}
break;
}

Expand All @@ -88,6 +88,17 @@ namespace AdaptiveCardQmlEngine
const auto color = isSubtle ? colorConfig.subtleColor : colorConfig.defaultColor;
return QString::fromStdString(color);
}
}
}

std::string AdaptiveCardContext::getLang()
{
return m_lang;
}

void AdaptiveCardContext::setLang(const std::string& lang)
{
m_lang = lang;
}

} // namespace AdaptiveCardQmlEngine

4 changes: 4 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/AdaptiveCardContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ namespace AdaptiveCardQmlEngine

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

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

private:
AdaptiveCardContext();
~AdaptiveCardContext();
Expand All @@ -51,5 +54,6 @@ namespace AdaptiveCardQmlEngine
std::shared_ptr<AdaptiveCards::HostConfig> mHostConfig;
std::shared_ptr<AdaptiveCardConfig> mCardConfig;
AdaptiveCardEnums::AdaptiveCardTheme mAdaptiveCardTheme;
std::string m_lang;
};
}
4 changes: 3 additions & 1 deletion source/qml_v2/AdaptiveCardQmlEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ file(GLOB_RECURSE SOURCES
"CollectionItemModel.cpp"

"TextBlockModel.cpp"
"RichTextBlockModel.cpp"

"AdaptiveCardQmlTypes.h"
"AdaptiveCardUtils.cpp"
Expand All @@ -72,7 +73,8 @@ file(GLOB_RECURSE SOURCES
"AdaptiveCardModel.h"
"CollectionItemModel.h"

"TextBlockModel.h"
"TextBlockModel.h"
"RichTextBlockModel.h"
)

# Setup Library
Expand Down
Binary file added source/qml_v2/AdaptiveCardQmlEngine/images/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 69 additions & 4 deletions source/qml_v2/AdaptiveCardQmlEngine/models/AdaptiveCardModel.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "AdaptiveCardModel.h"

#include "Enums.h"

AdaptiveCardModel::AdaptiveCardModel(std::shared_ptr<AdaptiveCards::AdaptiveCard> mainCard, QObject *parent)
: QObject(parent)
, mMainCard(mainCard)
, mCardBody(nullptr)
, mHasBackgroundImage(false)
{
populateCardBody();
}
Expand Down Expand Up @@ -41,7 +42,71 @@ void AdaptiveCardModel::populateCardBody()
break;
}

// Card Background color
auto hostConfig = AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().getHostConfig();
mBackgroundColor = QString::fromStdString(hostConfig->GetContainerStyles().defaultPalette.backgroundColor);
setupBaseCardProperties();
}

void AdaptiveCardModel::setupBaseCardProperties()
{
auto hostConfig = AdaptiveCardQmlEngine::AdaptiveCardContext::getInstance().getHostConfig();

mBackgroundColor = QString::fromStdString(hostConfig->GetContainerStyles().defaultPalette.backgroundColor);

mHasBackgroundImage = false;
mBackgroundImageSource = "";

if (mMainCard->GetBackgroundImage() != nullptr && mMainCard->GetBackgroundImage()->GetUrl() != "")
{
mHasBackgroundImage = true;
mBackgroundImageSource = getImagePath(mMainCard->GetBackgroundImage()->GetUrl());

mImageHorizontalAlignment = QString::fromStdString(AdaptiveCards::EnumHelpers::getHorizontalAlignmentEnum().toString(mMainCard->GetBackgroundImage()->GetHorizontalAlignment()));
mImageVerticalAlignment = QString::fromStdString(AdaptiveCards::EnumHelpers::getVerticalAlignmentEnum().toString(mMainCard->GetBackgroundImage()->GetVerticalAlignment()));

const auto fillMode = mMainCard->GetBackgroundImage()->GetFillMode();
switch (fillMode)
{
case AdaptiveCards::ImageFillMode::Cover:
{
mFillMode = "PreserveAspectCrop";
break;
}
case AdaptiveCards::ImageFillMode::RepeatHorizontally:
{
mFillMode = "TileHorizontally";
break;
}
case AdaptiveCards::ImageFillMode::RepeatVertically:
{
mFillMode = "TileVertically";
break;
}
case AdaptiveCards::ImageFillMode::Repeat:
{
mFillMode = "Tile";
break;
}
default:
break;
}

};
}

QString AdaptiveCardModel::getImagePath(const std::string url)
{
// To Do: Need to download the file and save it in a local forder called "images" first.
// For now images are saved manually prior to render the image from local path.

// Extracting the file name from the url
QString newUrl = QString::fromStdString(url);
const auto imageName = newUrl.split("://").at(1).split("/").last().split(".").first() + ".jpg";

// Setting up the local path for the image
QString file_path = __FILE__;
QString dir_path = file_path.left(file_path.lastIndexOf("\\models"));
dir_path.append("\\images\\" + imageName);
std::replace(dir_path.begin(), dir_path.end(), '\\', '/');
dir_path = "file:/" + dir_path;

return dir_path;
}
17 changes: 15 additions & 2 deletions source/qml_v2/AdaptiveCardQmlEngine/models/AdaptiveCardModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,32 @@ class AdaptiveCardModel : public QObject
Q_PROPERTY(int minHeight MEMBER mMinHeight CONSTANT)
Q_PROPERTY(Qt::AlignmentFlag verticalAlignment MEMBER mVerticalAlignment CONSTANT)
Q_PROPERTY(QString backgroundColor MEMBER mBackgroundColor CONSTANT)
Q_PROPERTY(bool hasBackgroundImage MEMBER mHasBackgroundImage CONSTANT)
Q_PROPERTY(QString backgroundImageSource MEMBER mBackgroundImageSource CONSTANT)
Q_PROPERTY(CollectionItemModel* cardBodyModel MEMBER mCardBody CONSTANT)


Q_PROPERTY(QString fillMode MEMBER mFillMode CONSTANT)
Q_PROPERTY(QString imageHorizontalAlignment MEMBER mImageHorizontalAlignment CONSTANT)
Q_PROPERTY(QString imageVerticalAlignment MEMBER mImageVerticalAlignment CONSTANT)

public:
explicit AdaptiveCardModel(std::shared_ptr<AdaptiveCards::AdaptiveCard> mainCard, QObject* parent = nullptr);
~AdaptiveCardModel();

private:
void populateCardBody();
void setupBaseCardProperties();

QString AdaptiveCardModel::getImagePath(const std::string url);

private:
int mMinHeight;

QString mBackgroundColor;
QString mBackgroundImageSource;
bool mHasBackgroundImage;
QString mFillMode;
QString mImageHorizontalAlignment;
QString mImageVerticalAlignment;

Qt::AlignmentFlag mVerticalAlignment;
std::shared_ptr<AdaptiveCards::AdaptiveCard> mMainCard;
Expand Down
11 changes: 11 additions & 0 deletions source/qml_v2/AdaptiveCardQmlEngine/models/CollectionItemModel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "CollectionItemModel.h"
#include "TextBlockModel.h"
#include "RichTextBlockModel.h"
#include "AdaptiveCardEnums.h"

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

return cardListModel;
Expand All @@ -54,6 +56,9 @@ void CollectionItemModel::populateRowData(std::shared_ptr<AdaptiveCards::BaseCar
case AdaptiveCards::CardElementType::TextBlock:
populateTextBlockModel(std::dynamic_pointer_cast<AdaptiveCards::TextBlock>(element), rowContent);
break;
case AdaptiveCards::CardElementType::RichTextBlock:
populateRichTextBlockModel(std::dynamic_pointer_cast<AdaptiveCards::RichTextBlock>(element), rowContent);
break;
default:
break;
}
Expand All @@ -66,3 +71,9 @@ void CollectionItemModel::populateTextBlockModel(std::shared_ptr<AdaptiveCards::
rowContent[CollectionModelRole::DelegateType] = QVariant::fromValue(AdaptiveCardEnums::CardElementType::TextBlock);
rowContent[CollectionModelRole::TextBlockRole] = QVariant::fromValue(new TextBlockModel(textBlock, nullptr));
}

void CollectionItemModel::populateRichTextBlockModel(std::shared_ptr<AdaptiveCards::RichTextBlock> richTextBlock, RowContent& rowContent)
{
rowContent[CollectionModelRole::DelegateType] = QVariant::fromValue(AdaptiveCardEnums::CardElementType::RichTextBlock);
rowContent[CollectionModelRole::RichTextBlockRole] = QVariant::fromValue(new RichTextBlockModel(richTextBlock, nullptr));
}
13 changes: 9 additions & 4 deletions source/qml_v2/AdaptiveCardQmlEngine/models/CollectionItemModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
#include <SharedAdaptiveCard.h>

#include <TextBlock.h>
#include "RichTextBlock.h"

#include "Enums.h"

class TextBlockModel;
class RichTextBlockModel;

class CollectionItemModel : public QAbstractListModel
class CollectionItemModel : public QAbstractListModel
{
Q_OBJECT
Q_OBJECT

enum CollectionModelRole
enum CollectionModelRole
{
DelegateType = Qt::UserRole + 1,
TextBlockRole,
RichTextBlockRole,
FillHeightRole
};

public:
using RowContent = std::unordered_map<int, QVariant>;
using RowContent = std::unordered_map<int, QVariant>;

explicit CollectionItemModel(std::vector<std::shared_ptr<AdaptiveCards::BaseCardElement>> elements, QObject* parent = nullptr);
~CollectionItemModel();
Expand All @@ -36,4 +40,5 @@ class CollectionItemModel : public QAbstractListModel
private:
void populateRowData(std::shared_ptr<AdaptiveCards::BaseCardElement> element);
void populateTextBlockModel(std::shared_ptr<AdaptiveCards::TextBlock> textBlock, RowContent& rowContent);
void populateRichTextBlockModel(std::shared_ptr<AdaptiveCards::RichTextBlock> rightTextBlock, RowContent& rowContent);
};
Loading