1
0
mirror of https://github.com/KDE/latte-dock.git synced 2025-03-20 18:50:15 +03:00

add distorted flag to backgrounds tracking

This commit is contained in:
Michail Vourlakos 2019-01-04 21:10:50 +02:00
parent 60a96bd157
commit 9229ca07fb
4 changed files with 170 additions and 61 deletions

View File

@ -40,6 +40,11 @@ BackgroundTracker::~BackgroundTracker()
{
}
bool BackgroundTracker::isDistorted() const
{
return m_distorted;
}
int BackgroundTracker::location() const
{
return m_location;
@ -109,8 +114,10 @@ void BackgroundTracker::update()
}
m_brightness = m_cache->brightnessFor(m_activity, m_screenName, m_location);
m_distorted = m_cache->distortedFor(m_activity, m_screenName, m_location);
emit currentBrightnessChanged();
emit isDistortedChanged();
}
}

View File

@ -33,6 +33,8 @@ class BackgroundTracker: public QObject
{
Q_OBJECT
Q_PROPERTY(bool isDistorted READ isDistorted NOTIFY isDistortedChanged)
Q_PROPERTY(int location READ location WRITE setLocation NOTIFY locationChanged)
Q_PROPERTY(float currentBrightness READ currentBrightness NOTIFY currentBrightnessChanged)
@ -44,6 +46,8 @@ public:
BackgroundTracker(QObject *parent = nullptr);
virtual ~BackgroundTracker();
bool isDistorted() const;
int location() const;
void setLocation(int location);
@ -58,6 +62,7 @@ public:
signals:
void activityChanged();
void currentBrightnessChanged();
void isDistortedChanged();
void locationChanged();
void screenNameChanged();
@ -67,6 +72,7 @@ private slots:
private:
// local
bool m_distorted{false};
float m_brightness{-1000};
PlasmaExtended::BackgroundCache *m_cache{nullptr};

View File

@ -26,6 +26,7 @@
#include <QDebug>
#include <QImage>
#include <QRgb>
#include <QtMath>
// Plasma
#include <Plasma>
@ -46,8 +47,8 @@ BackgroundCache::BackgroundCache(QObject *parent)
m_plasmaConfig(KSharedConfig::openConfig(PLASMACONFIG))
{
const auto configFile = QStandardPaths::writableLocation(
QStandardPaths::GenericConfigLocation) +
QLatin1Char('/') + PLASMACONFIG;
QStandardPaths::GenericConfigLocation) +
QLatin1Char('/') + PLASMACONFIG;
KDirWatch::self()->addFile(configFile);
@ -175,66 +176,33 @@ QString BackgroundCache::background(QString activity, QString screen)
}
}
bool BackgroundCache::distortedFor(QString activity, QString screen, Plasma::Types::Location location)
{
QString assignedBackground = background(activity, screen);
if (!assignedBackground.isEmpty()) {
return distortedForFile(assignedBackground, location);
}
return false;
}
float BackgroundCache::brightnessFor(QString activity, QString screen, Plasma::Types::Location location)
{
QString assignedBackground = background(activity, screen);
if (!assignedBackground.isEmpty()) {
return brightnessFromFile(assignedBackground, location);
return brightnessForFile(assignedBackground, location);
}
return -1000;
}
float BackgroundCache::brightnessFromFile(QString imageFile, Plasma::Types::Location location)
float BackgroundCache::brightnessFromArea(QImage &image, int firstRow, int firstColumn, int endRow, int endColumn)
{
if (m_brightnessCache.keys().contains(imageFile)) {
if (m_brightnessCache[imageFile].keys().contains(location)) {
return m_brightnessCache[imageFile].value(location);
}
}
//! if it is a color
if (imageFile.startsWith("#")) {
return Latte::colorBrightness(QColor(imageFile));
}
//! if it is a local image
QImage image(imageFile);
float areaBrightness = -1000;
if (image.format() != QImage::Format_Invalid) {
int maskHeight = (0.08 * image.height());
int maskWidth = (0.05 * image.width());
float areaBrightness = -1000;
int firstRow = 0;
int firstColumn = 0;
int endRow = 0;
int endColumn = 0;
if (location == Plasma::Types::TopEdge) {
firstRow = 0;
endRow = maskHeight;
firstColumn = 0;
endColumn = image.width() - 1;
} else if (location == Plasma::Types::BottomEdge) {
firstRow = image.height() - maskHeight - 1;
endRow = image.height() - 1;
firstColumn = 0;
endColumn = image.width() - 1;
} else if (location == Plasma::Types::LeftEdge) {
firstRow = 0;
endRow = image.height() - 1;
firstColumn = 0;
endColumn = maskWidth;
} else if (location == Plasma::Types::RightEdge) {
firstRow = 0;
endRow = image.height() - 1;
firstColumn = image.width() - 1 - maskWidth;
endColumn = image.width() - 1;
}
for (int row = firstRow; row < endRow; ++row) {
QRgb *line = (QRgb *)image.scanLine(row);
@ -248,19 +216,135 @@ float BackgroundCache::brightnessFromFile(QString imageFile, Plasma::Types::Loca
float areaSize = (endRow - firstRow) * (endColumn - firstColumn);
areaBrightness = areaBrightness / areaSize;
if (!m_brightnessCache.keys().contains(imageFile)) {
m_brightnessCache[imageFile] = EdgesHash();
}
m_brightnessCache[imageFile].insert(location, areaBrightness);
return areaBrightness;
}
//! didn't find anything
return areaBrightness;
}
bool BackgroundCache::areaIsDistorted(float bright1, float bright2, float bright3)
{
int distortedStep{30};
return (qFabs(bright1-bright2)>=distortedStep
|| qFabs(bright2-bright3)>=distortedStep
|| qFabs(bright1-bright3)>=distortedStep);
}
void BackgroundCache::updateImageCalculations(QString imageFile, Plasma::Types::Location location)
{
//! if it is a local image
QImage image(imageFile);
if (image.format() != QImage::Format_Invalid) {
int maskHeight = (0.08 * image.height());
int maskWidth = (0.05 * image.width());
float areaBrightness = -1000;
float area1Brightness = -1000; float area2Brightness = -1000; float area3Brightness = -1000;
float area1p = 0.25; float area2p=0.35; float area3p=0.4;
int firstRow = 0; int firstColumn = 0; int endRow = 0; int endColumn = 0;
//! horizontal mask calculations
if (location == Plasma::Types::TopEdge) {
firstRow = 0; endRow = maskHeight;
} else if (location == Plasma::Types::BottomEdge) {
firstRow = image.height() - maskHeight - 1; endRow = image.height() - 1;
}
if (location == Plasma::Types::TopEdge || location == Plasma::Types::BottomEdge) {
firstColumn = 0; endColumn = (area1p*image.width()) - 1;
area1Brightness = brightnessFromArea(image, firstRow, firstColumn, endRow, endColumn);
firstColumn = endColumn+1; endColumn = ((area1p+area2p)*image.width()) - 1;
area2Brightness = brightnessFromArea(image, firstRow, firstColumn, endRow, endColumn);
firstColumn = endColumn+1; endColumn = (image.width() - 1);
area3Brightness = brightnessFromArea(image, firstRow, firstColumn, endRow, endColumn);
}
//! vertical mask calculations
if (location == Plasma::Types::LeftEdge) {
firstColumn = 0; endColumn = maskWidth;
} else if (location == Plasma::Types::RightEdge) {
firstColumn = image.width() - 1 - maskWidth; endColumn = image.width() - 1;
}
if (location == Plasma::Types::LeftEdge || location == Plasma::Types::RightEdge) {
firstRow = 0; endRow = image.height() - 1;
area1Brightness = brightnessFromArea(image, firstRow, firstColumn, endRow, endColumn);
firstRow = endRow+1; endRow = ((area1p+area2p)*image.height()) - 1;
area2Brightness = brightnessFromArea(image, firstRow, firstColumn, endRow, endColumn);
firstRow = endRow+1; endRow = (image.height() - 1);
area3Brightness = brightnessFromArea(image, firstRow, firstColumn, endRow, endColumn);
}
//! compute total brightness for this area
areaBrightness = (area1Brightness + area2Brightness + area3Brightness) / 3;
bool areaDistorted = areaIsDistorted(area1Brightness, area2Brightness, area3Brightness);
if (!m_hintsCache.keys().contains(imageFile)) {
m_hintsCache[imageFile] = EdgesHash();
}
if (!m_hintsCache[imageFile].contains(location)) {
imageHints iHints;
iHints.brightness = areaBrightness;
iHints.distorted =areaDistorted;
m_hintsCache[imageFile].insert(location, iHints);
} else {
m_hintsCache[imageFile][location].brightness = areaBrightness;
m_hintsCache[imageFile][location].distorted = areaDistorted;
}
}
}
float BackgroundCache::brightnessForFile(QString imageFile, Plasma::Types::Location location)
{
if (m_hintsCache.keys().contains(imageFile)) {
if (m_hintsCache[imageFile].keys().contains(location)) {
return m_hintsCache[imageFile][location].brightness;
}
}
//! if it is a color
if (imageFile.startsWith("#")) {
return Latte::colorBrightness(QColor(imageFile));
}
updateImageCalculations(imageFile, location);
if (m_hintsCache.keys().contains(imageFile)) {
return m_hintsCache[imageFile][location].brightness;
}
return -1000;
}
bool BackgroundCache::distortedForFile(QString imageFile, Plasma::Types::Location location)
{
if (m_hintsCache.keys().contains(imageFile)) {
if (m_hintsCache[imageFile].keys().contains(location)) {
return m_hintsCache[imageFile][location].distorted;
}
}
//! if it is a color
if (imageFile.startsWith("#")) {
return false;
}
updateImageCalculations(imageFile, location);
if (m_hintsCache.keys().contains(imageFile)) {
return m_hintsCache[imageFile][location].distorted;
}
return false;
}
}
}

View File

@ -34,7 +34,12 @@
#include <KConfigGroup>
#include <KSharedConfig>
typedef QHash<Plasma::Types::Location, float> EdgesHash;
struct imageHints {
bool distorted{false};
float brightness{-1000};
};
typedef QHash<Plasma::Types::Location, imageHints> EdgesHash;
namespace Latte {
namespace PlasmaExtended {
@ -47,6 +52,7 @@ public:
static BackgroundCache *self();
~BackgroundCache() override;
bool distortedFor(QString activity, QString screen, Plasma::Types::Location location);
float brightnessFor(QString activity, QString screen, Plasma::Types::Location location);
QString background(QString activity, QString screen);
@ -61,10 +67,16 @@ private slots:
private:
BackgroundCache(QObject *parent = nullptr);
bool areaIsDistorted(float bright1, float bright2, float bright3);
bool distortedForFile(QString imageFile, Plasma::Types::Location location);
bool isDesktopContainment(const KConfigGroup &containment) const;
float brightnessFromFile(QString imageFile, Plasma::Types::Location location);
float brightnessForFile(QString imageFile, Plasma::Types::Location location);
float brightnessFromArea(QImage &image, int firstRow, int firstColumn, int endRow, int endColumn);
QString backgroundFromConfig(const KConfigGroup &config) const;
void updateImageCalculations(QString imageFile, Plasma::Types::Location location);
private:
bool m_initialized{false};
@ -73,7 +85,7 @@ private:
//! screen aware backgrounds: activity id, screen name, backgroundfile
QHash<QString, QHash<QString, QString>> m_backgrounds;
//! image file and brightness per edge
QHash<QString, EdgesHash> m_brightnessCache;
QHash<QString, EdgesHash> m_hintsCache;
KSharedConfig::Ptr m_plasmaConfig;
};