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

qt: Expose branding color #670

Open
wants to merge 1 commit 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
qt: Expose branding color
  • Loading branch information
CarlSchwan committed Sep 29, 2024
commit 8e9ab6f4ae2f917c575ec5cc9330885554ff72bc
102 changes: 102 additions & 0 deletions qt/branding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// SPDX-FileCopyrightText: 2024 Carl Schwan <carl@carlschwan.eu>
// SPDX-License-Identifier: LGPL-2.1-or-later
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the full copyright please, like in all other files? I do like the SPDX strings, but I think it will be easier to convert everything all at once at a future point, rather than having one outlier file.


#include "branding.h"
#include "appstream.h"
#include "chelpers.h"

using namespace AppStream;

class AppStream::BrandingData : public QSharedData
{
public:
BrandingData()
{
brandingp = as_branding_new();
}

BrandingData(AsBranding *branding)
: brandingp(branding)
{
g_object_ref(brandingp);
}

~BrandingData()
{
g_object_unref(brandingp);
}

bool operator==(const BrandingData &rd) const
{
return rd.brandingp == brandingp;
}

AsBranding *brandingp;
};

Branding::Branding()
: d(new BrandingData)
{
}

Branding::Branding(_AsBranding *devp)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

devp should probably be branding

: d(new BrandingData(devp))
{
}

Branding::Branding(const Branding &devp) = default;

Branding::~Branding() = default;

Branding &Branding::operator=(const Branding &devp) = default;

bool Branding::operator==(const Branding &other) const
{
if (this->d == other.d) {
return true;
}
if (this->d && other.d) {
return *(this->d) == *other.d;
}
return false;
}

_AsBranding *Branding::cPtr() const
{
return d->brandingp;
}

QString Branding::colorKindToString(Branding::ColorKind colorKind)
{
return valueWrap(as_color_kind_to_string(static_cast<AsColorKind>(colorKind)));
}

Branding::ColorKind Branding::colorKindfromString(const QString &string)
{
return static_cast<Branding::ColorKind>(as_color_kind_from_string(qPrintable(string)));
}

QString Branding::colorSchemeToString(ColorSchemeKind colorScheme)
{
return valueWrap(as_color_scheme_kind_to_string(static_cast<AsColorSchemeKind>(colorScheme)));
}

Branding::ColorSchemeKind Branding::colorSchemefromString(const QString &string)
{
return static_cast<Branding::ColorSchemeKind>(as_color_scheme_kind_from_string(qPrintable(string)));
}

void Branding::setColor(Branding::ColorKind kind, Branding::ColorSchemeKind scheme, const QString &color)
{
as_branding_set_color(d->brandingp, static_cast<AsColorKind>(kind), static_cast<AsColorSchemeKind>(scheme), qPrintable(color));
}

void Branding::removeColor(Branding::ColorKind kind, Branding::ColorSchemeKind scheme)
{
as_branding_remove_color(d->brandingp, static_cast<AsColorKind>(kind), static_cast<AsColorSchemeKind>(scheme));
}

QString Branding::color(Branding::ColorKind kind, Branding::ColorSchemeKind scheme)
{
return valueWrap(as_branding_get_color(d->brandingp, static_cast<AsColorKind>(kind), static_cast<AsColorSchemeKind>(scheme)));
}
88 changes: 88 additions & 0 deletions qt/branding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// SPDX-FileCopyrightText: 2024 Carl Schwan <carl@carlschwan.eu>
// SPDX-License-Identifier: LGPL-2.1-or-later

#pragma once

#include <QObject>
#include <QSharedDataPointer>
#include "appstreamqt_export.h"

class _AsBranding;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be struct, shouldn't it?


namespace AppStream
{

class BrandingData;

class APPSTREAMQT_EXPORT Branding
{
Q_GADGET

public:
enum class ColorSchemeKind {
Unknown,
Light,
Dark,
};

enum class ColorKind {
Unknow,
Primary,
};

Branding();
Branding(_AsBranding *);
Branding(const Branding &devp);
~Branding();

Branding &operator=(const Branding &devp);
bool operator==(const Branding &r) const;

/**
* Converts the ColorKind enumerated value to an text representation.
*/
static QString colorKindToString(ColorKind colorKind);

/**
* Converts the text representation to an ColorKind enumerated value.
*/
static ColorKind colorKindfromString(const QString &string);

/**
* Converts the ColorScheme enumerated value to an text representation.
*/
static QString colorSchemeToString(ColorSchemeKind colorScheme);

/**
* Converts the text representation to an ColorScheme enumerated value.
*/
static ColorSchemeKind colorSchemefromString(const QString &string);

/**
* Sets a new accent color. If a color of the given kind with the given scheme preference already exists,
* it will be overriden with the new color code.
*/
void setColor(ColorKind kind, ColorSchemeKind scheme, const QString &color);

/**
* Deletes a color that matches the given type and scheme preference.
*/
void removeColor(ColorKind kind, ColorSchemeKind scheme);

/**
* Retrieve a color of the given @kind that matches @scheme_kind.
* If a color has no scheme preference defined, it will be returned for either scheme type,
* unless a more suitable color was found.
*/
QString color(ColorKind kind, ColorSchemeKind scheme);

/**
* \returns the internally stored AsBranding
*/
_AsBranding *cPtr() const;

private:
QSharedDataPointer<BrandingData> d;
};

};
14 changes: 14 additions & 0 deletions qt/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QUrl>
#include <QMap>
#include <QMultiHash>
#include "branding.h"
#include "chelpers.h"
#include "icon.h"
#include "screenshot.h"
Expand Down Expand Up @@ -761,6 +762,19 @@ void Component::setNameVariantSuffix(const QString &variantSuffix, const QString
lang.isEmpty() ? NULL : qPrintable(lang));
}

Branding Component::branding() const
{
auto branding = as_component_get_branding(d->cpt);
if (branding == NULL)
return Branding();
return Branding(branding);
}

void Component::setBranding(const Branding &branding)
{
as_component_set_branding(d->cpt, branding.cPtr());
}

bool Component::hasTag(const QString &ns, const QString &tagName)
{
return as_component_has_tag(d->cpt, qPrintable(ns), qPrintable(tagName));
Expand Down
4 changes: 4 additions & 0 deletions qt/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Icon;
class Screenshot;
class Suggested;
class Developer;
class Branding;
class RelationCheckResult;

class ComponentData;
Expand Down Expand Up @@ -264,6 +265,9 @@ class APPSTREAMQT_EXPORT Component
QString nameVariantSuffix() const;
void setNameVariantSuffix(const QString &variantSuffix, const QString &lang = {});

Branding branding() const;
void setBranding(const Branding &branding);

bool hasTag(const QString &ns, const QString &tagName);
bool addTag(const QString &ns, const QString &tagName);
void removeTag(const QString &ns, const QString &tagName);
Expand Down
2 changes: 2 additions & 0 deletions qt/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ qt_dep = dependency(

asqt_src = [
'bundle.cpp',
'branding.cpp',
'category.cpp',
'component-box.cpp',
'component.cpp',
Expand All @@ -53,6 +54,7 @@ asqt_src = [

asqt_pub_headers = [
'appstreamqt_export.h',
'branding.h',
'bundle.h',
'category.h',
'component-box.h',
Expand Down
Loading