Paste P451

Masterwork From Distant Lands
ActivePublic

Authored by davidedmundson on Sat, Aug 17, 1:39 PM.
/*
Copyright (c) 2019 David Edmundson <davidedmundson@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "processdataprovider.h"
class ProcessAttribute::Private
{
public:
QString m_id;
QString m_name;
QString m_shortName;
QString m_description;
qreal m_min = 0;
qreal m_max = 0;
KSGRD2::utils::Unit m_unit = KSGRD2::utils::UnitInvalid; //Both a format hint and implies data type (i.e double/string)
QHash<KSysGuard::Process *, QVariant> m_data;
bool m_enabled = 0;
};
class ProcessDataProvider::Private
{
public:
QVector<ProcessAttribute *> m_attributes;
bool m_enabled = false;
};
ProcessDataProvider::ProcessDataProvider(QObject *parent, const QVariantList &args)
: QObject(parent)
{
Q_UNUSED(args)
}
ProcessDataProvider::~ProcessDataProvider()
{
}
QVector<ProcessAttribute *> ProcessDataProvider::attributes() const
{
return d->m_attributes;
}
void ProcessDataProvider::addProcessAttribute(ProcessAttribute *attribute)
{
d->m_attributes << attribute;
connect(attribute, &ProcessAttribute::enabledChanged, this, [this](bool enabled) {
if (enabled == d->m_enabled) {
return;
}
bool wasEnabled = d->m_enabled;
d->m_enabled = std::any_of(d->m_attributes.constBegin(), d->m_attributes.constEnd(), [](ProcessAttribute *p) {
return p->enabled();
});
if (d->m_enabled != wasEnabled) {
handleEnabledChanged(d->m_enabled);
}
});
}
ProcessAttribute::ProcessAttribute(const QString &id, QObject *parent)
: QObject(parent),
d(new Private)
{
d->m_id = id;
}
ProcessAttribute::ProcessAttribute(const QString &id, const QString &name, QObject *parent)
: QObject(parent),
d(new Private)
{
d->m_id = id;
d->m_name = name;
}
ProcessAttribute::~ProcessAttribute()
{
}
QString ProcessAttribute::id() const
{
return d->m_id;
}
bool ProcessAttribute::enabled() const
{
return d->m_enabled;
}
void ProcessAttribute::setEnabled(const bool enabled)
{
if (d->m_enabled == enabled) {
return;
}
d->m_enabled = enabled;
emit enabledChanged(enabled);
}
QString ProcessAttribute::name() const
{
return d->m_name;
}
void ProcessAttribute::setName(const QString &name)
{
d->m_name = name;
}
QString ProcessAttribute::shortName() const
{
return d->m_shortName.isEmpty() ? d->m_name : d->m_shortName;
}
QString ProcessAttribute::description() const
{
return d->m_description;
}
void ProcessAttribute::setDescription(const QString &description)
{
d->m_description = description;
}
qreal ProcessAttribute::min() const
{
return d->m_min;
}
void ProcessAttribute::setMin(const qreal min)
{
d->m_min = min;
}
qreal ProcessAttribute::max() const
{
return d->m_max;
}
void ProcessAttribute::setMax(const qreal max)
{
d->m_max = max;
}
void ProcessAttribute::setUnit(KSGRD2::utils::Unit unit)
{
d->m_unit = unit;
}
QVariant ProcessAttribute::data(KSysGuard::Process *process)
{
return d->m_data.value(process);
}
void ProcessAttribute::setData(KSysGuard::Process *process, const QVariant &value)
{
d->m_data[process] = value;
emit dataChanged(process);
}
/*
Copyright (c) 2019 David Edmundson <davidedmundson@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#pragma once
#include <QDateTime>
#include <QDebug>
#include <QObject>
#include <QVariant>
#include "types.h"
class ProcessDataProvider;
namespace KSysGuard {
class Process;
class Processes;
}
class Q_DECL_EXPORT ProcessAttribute : public QObject
{
Q_OBJECT
public:
ProcessAttribute(const QString &id, QObject *parent);
ProcessAttribute(const QString &id, const QString &name, QObject *parent);
~ProcessAttribute() override;
/**
* A unique non-translatable ID for this attribute. For saving in config files
*/
QString id() const;
/**
* Controls whether we should fetch process attributes
*/
bool enabled() const;
void setEnabled(const bool enable);
/**
* A translated user facing name for the attribute.
* e.g "Download Speed"
*/
QString name() const;
void setName(const QString &name);
/**
* A translated shorter version of the name
* for use in table column headers for example
* e.g "D/L"
* If unset, name is returned
*/
QString shortName() const;
void setShortName(const QString &name);
/**
* A translated human readable description of this attribute
*/
QString description() const;
void setDescription(const QString &description);
/**
* The minimum value possible for this sensor
* (i.e to show a CPU is between 0 and 100)
* Set min and max to 0 if not relevant
*/
qreal min() const;
void setMin(const qreal min);
/**
* The maximum value possible for this attribute
*/
qreal max() const;
void setMax(const qreal max);
KSGRD2::utils::Unit unit() const;
void setUnit(KSGRD2::utils::Unit unit);
/**
* The last stored value for a given process
*/
QVariant data(KSysGuard::Process *process);
/**
* Updates the stored value for a given process
* Note process data will be automatically removed when a process closes
*/
void setData(KSysGuard::Process *process, const QVariant &value);
Q_SIGNALS:
void dataChanged(KSysGuard::Process *process);
void enabledChanged(bool enabled);
private:
class Private;
QScopedPointer<Private> d;
};
/**
* Base class for a process plugin data
* Plugins provide a list of additional attributes, which in turn have data about a given process
*/
class Q_DECL_EXPORT ProcessDataProvider : public QObject
{
Q_OBJECT
public:
ProcessDataProvider(QObject *parent, const QVariantList &args);
~ProcessDataProvider() override;
/**
* A list of all process attributes provided by this plugin
* It is expected to remain constant through the lifespan of this class
*/
QVector<ProcessAttribute *> attributes() const;
/**
* Called when processes should be updated if manually polled
* Plugins can however update at any time if enabled
*/
virtual void update()
{}
/**
* True when at least one attribute from this plugin is subscribed
*/
bool enabled() const;
virtual void handleEnabledChanged(bool enabled)
{
Q_UNUSED(enabled)
}
// for any future compatibility
virtual void virtual_hook(int id, void *data)
{
Q_UNUSED(id)
Q_UNUSED(data)
}
protected:
/**
* Register a new process attribute
* Process attributes are expected to be created in the plugin constuctor and live for the duration the plugin
*/
void addProcessAttribute(ProcessAttribute *attribute);
private:
class Private;
QScopedPointer<Private> d;
};
davidedmundson edited the content of this paste. (Show Details)Sat, Aug 17, 1:39 PM
davidedmundson changed the title of this paste from untitled to Masterwork From Distant Lands.