move graph data store to graphical item helper

It is required by custom graphs. X-AW-Count parameter (which is only
    recognized by Graph item type) has been introduced
This commit is contained in:
Evgenii Alekseev 2016-03-14 12:04:38 +03:00
parent 7f665fef77
commit 52b1255d3f
6 changed files with 114 additions and 32 deletions

View File

@ -312,11 +312,6 @@ QString AWKeys::parsePattern(QString pattern) const
// bars // bars
for (auto bar : m_foundBars) { for (auto bar : m_foundBars) {
GraphicalItem *item = keyOperator->giByKey(bar); GraphicalItem *item = keyOperator->giByKey(bar);
if (item->type() == GraphicalItem::Graph) {
pattern.replace(QString("$%1").arg(bar),
item->image(QVariant::fromValue<QList<float>>(
dataAggregator->getData(item->bar()))));
} else {
if (item->isCustom()) if (item->isCustom())
pattern.replace( pattern.replace(
QString("$%1").arg(bar), QString("$%1").arg(bar),
@ -326,7 +321,6 @@ QString AWKeys::parsePattern(QString pattern) const
pattern.replace(QString("$%1").arg(bar), pattern.replace(QString("$%1").arg(bar),
item->image(values[item->bar()])); item->image(values[item->bar()]));
} }
}
// prepare strings // prepare strings
pattern.replace(QString(0x1d), QString("$")); pattern.replace(QString(0x1d), QString("$"));

View File

@ -47,6 +47,8 @@ GraphicalItem::GraphicalItem(QWidget *parent, const QString desktopName,
connect(ui->checkBox_custom, SIGNAL(stateChanged(int)), this, connect(ui->checkBox_custom, SIGNAL(stateChanged(int)), this,
SLOT(changeValue(int))); SLOT(changeValue(int)));
connect(ui->comboBox_type, SIGNAL(currentIndexChanged(int)), this,
SLOT(changeCountState(int)));
connect(ui->pushButton_activeColor, SIGNAL(clicked()), this, connect(ui->pushButton_activeColor, SIGNAL(clicked()), this,
SLOT(changeColor())); SLOT(changeColor()));
connect(ui->pushButton_inactiveColor, SIGNAL(clicked()), this, connect(ui->pushButton_inactiveColor, SIGNAL(clicked()), this,
@ -73,6 +75,7 @@ GraphicalItem *GraphicalItem::copy(const QString _fileName, const int _number)
copyDefaults(item); copyDefaults(item);
item->setActiveColor(activeColor()); item->setActiveColor(activeColor());
item->setBar(m_bar); item->setBar(m_bar);
item->setCount(m_count);
item->setCustom(m_custom); item->setCustom(m_custom);
item->setDirection(m_direction); item->setDirection(m_direction);
item->setHeight(m_height); item->setHeight(m_height);
@ -93,29 +96,28 @@ QString GraphicalItem::image(const QVariant &value)
m_scene->clear(); m_scene->clear();
int scale[2] = {1, 1}; int scale[2] = {1, 1};
float converted
= m_helper->getPercents(value.toFloat(), m_minValue, m_maxValue);
// paint // paint
switch (m_type) { switch (m_type) {
case Vertical: case Vertical:
m_helper->paintVertical( m_helper->paintVertical(converted);
m_helper->getPercents(value.toFloat(), m_minValue, m_maxValue));
// scale // scale
scale[1] = -2 * static_cast<int>(m_direction) + 1; scale[1] = -2 * static_cast<int>(m_direction) + 1;
break; break;
case Circle: case Circle:
m_helper->paintCircle( m_helper->paintCircle(converted);
m_helper->getPercents(value.toFloat(), m_minValue, m_maxValue));
// scale // scale
scale[0] = -2 * static_cast<int>(m_direction) + 1; scale[0] = -2 * static_cast<int>(m_direction) + 1;
break; break;
case Graph: case Graph:
m_helper->paintGraph(value.value<QList<float>>()); m_helper->paintGraph(converted);
// direction option is not recognized by this GI type // direction option is not recognized by this GI type
break; break;
case Horizontal: case Horizontal:
default: default:
m_helper->paintHorizontal( m_helper->paintHorizontal(converted);
m_helper->getPercents(value.toFloat(), m_minValue, m_maxValue));
// scale // scale
scale[0] = -2 * static_cast<int>(m_direction) + 1; scale[0] = -2 * static_cast<int>(m_direction) + 1;
break; break;
@ -152,6 +154,12 @@ QString GraphicalItem::inactiveColor() const
} }
int GraphicalItem::count() const
{
return m_count;
}
bool GraphicalItem::isCustom() const bool GraphicalItem::isCustom() const
{ {
return m_custom; return m_custom;
@ -262,6 +270,16 @@ void GraphicalItem::setActiveColor(const QString _color)
} }
void GraphicalItem::setCount(const int _count)
{
qCDebug(LOG_LIB) << "Count" << _count;
if (_count <= 1)
return;
m_count = _count;
}
void GraphicalItem::setCustom(const bool _custom) void GraphicalItem::setCustom(const bool _custom)
{ {
qCDebug(LOG_LIB) << "Use custom tag" << _custom; qCDebug(LOG_LIB) << "Use custom tag" << _custom;
@ -379,6 +397,7 @@ void GraphicalItem::readConfiguration()
QSettings::IniFormat); QSettings::IniFormat);
settings.beginGroup(QString("Desktop Entry")); settings.beginGroup(QString("Desktop Entry"));
setCount(settings.value(QString("X-AW-Count"), m_count).toInt());
setCustom(settings.value(QString("X-AW-Custom"), m_custom).toBool()); setCustom(settings.value(QString("X-AW-Custom"), m_custom).toBool());
setBar(settings.value(QString("X-AW-Value"), m_bar).toString()); setBar(settings.value(QString("X-AW-Value"), m_bar).toString());
setMaxValue(settings.value(QString("X-AW-Max"), m_maxValue).toFloat()); setMaxValue(settings.value(QString("X-AW-Max"), m_maxValue).toFloat());
@ -435,6 +454,7 @@ int GraphicalItem::showConfiguration(const QVariant args)
} }
ui->doubleSpinBox_max->setValue(m_maxValue); ui->doubleSpinBox_max->setValue(m_maxValue);
ui->doubleSpinBox_min->setValue(m_minValue); ui->doubleSpinBox_min->setValue(m_minValue);
ui->spinBox_count->setValue(m_count);
ui->pushButton_activeColor->setText(activeColor()); ui->pushButton_activeColor->setText(activeColor());
ui->pushButton_inactiveColor->setText(inactiveColor()); ui->pushButton_inactiveColor->setText(inactiveColor());
ui->comboBox_type->setCurrentIndex(static_cast<int>(m_type)); ui->comboBox_type->setCurrentIndex(static_cast<int>(m_type));
@ -442,12 +462,17 @@ int GraphicalItem::showConfiguration(const QVariant args)
ui->spinBox_height->setValue(m_height); ui->spinBox_height->setValue(m_height);
ui->spinBox_width->setValue(m_width); ui->spinBox_width->setValue(m_width);
// update UI
changeCountState(ui->comboBox_type->currentIndex());
changeValue(ui->checkBox_custom->checkState());
int ret = exec(); int ret = exec();
if (ret != 1) if (ret != 1)
return ret; return ret;
setName(ui->lineEdit_name->text()); setName(ui->lineEdit_name->text());
setComment(ui->lineEdit_comment->text()); setComment(ui->lineEdit_comment->text());
setApiVersion(AWGIAPI); setApiVersion(AWGIAPI);
setCount(ui->spinBox_count->value());
setCustom(ui->checkBox_custom->isChecked()); setCustom(ui->checkBox_custom->isChecked());
setBar(m_custom ? ui->lineEdit_customValue->text() setBar(m_custom ? ui->lineEdit_customValue->text()
: ui->comboBox_value->currentText()); : ui->comboBox_value->currentText());
@ -476,6 +501,7 @@ void GraphicalItem::writeConfiguration() const
settings.beginGroup(QString("Desktop Entry")); settings.beginGroup(QString("Desktop Entry"));
settings.setValue(QString("X-AW-Value"), m_bar); settings.setValue(QString("X-AW-Value"), m_bar);
settings.setValue(QString("X-AW-Count"), m_count);
settings.setValue(QString("X-AW-Custom"), m_custom); settings.setValue(QString("X-AW-Custom"), m_custom);
settings.setValue(QString("X-AW-Max"), m_maxValue); settings.setValue(QString("X-AW-Max"), m_maxValue);
settings.setValue(QString("X-AW-Min"), m_minValue); settings.setValue(QString("X-AW-Min"), m_minValue);
@ -512,6 +538,15 @@ void GraphicalItem::changeColor()
} }
void GraphicalItem::changeCountState(const int state)
{
qCDebug(LOG_LIB) << "Current state is" << state;
// 3 is magic number. Actually 3 is Graph mode
ui->widget_count->setHidden(state != 3);
}
void GraphicalItem::changeValue(const int state) void GraphicalItem::changeValue(const int state)
{ {
qCDebug(LOG_LIB) << "Current state is" << state; qCDebug(LOG_LIB) << "Current state is" << state;
@ -540,7 +575,8 @@ void GraphicalItem::initScene()
// init helper // init helper
m_helper = new GraphicalItemHelper(this, m_scene); m_helper = new GraphicalItemHelper(this, m_scene);
m_helper->setParameters(m_activeColor, m_inactiveColor, m_width, m_height); m_helper->setParameters(m_activeColor, m_inactiveColor, m_width, m_height,
m_count);
} }
@ -548,6 +584,7 @@ void GraphicalItem::translate()
{ {
ui->label_name->setText(i18n("Name")); ui->label_name->setText(i18n("Name"));
ui->label_comment->setText(i18n("Comment")); ui->label_comment->setText(i18n("Comment"));
ui->label_count->setText(i18n("Points count"));
ui->checkBox_custom->setText(i18n("Use custom formula")); ui->checkBox_custom->setText(i18n("Use custom formula"));
ui->label_value->setText(i18n("Value")); ui->label_value->setText(i18n("Value"));
ui->label_customValue->setText(i18n("Value")); ui->label_customValue->setText(i18n("Value"));

View File

@ -37,6 +37,7 @@ class GraphicalItem : public AbstractExtItem
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString bar READ bar WRITE setBar) Q_PROPERTY(QString bar READ bar WRITE setBar)
Q_PROPERTY(QString activeColor READ activeColor WRITE setActiveColor) Q_PROPERTY(QString activeColor READ activeColor WRITE setActiveColor)
Q_PROPERTY(int count READ count WRITE setCount)
Q_PROPERTY(bool custom READ isCustom WRITE setCustom) Q_PROPERTY(bool custom READ isCustom WRITE setCustom)
Q_PROPERTY(QString inactiveColor READ inactiveColor WRITE setInactiveColor) Q_PROPERTY(QString inactiveColor READ inactiveColor WRITE setInactiveColor)
Q_PROPERTY(Type type READ type WRITE setType) Q_PROPERTY(Type type READ type WRITE setType)
@ -61,6 +62,7 @@ public:
QString bar() const; QString bar() const;
QString activeColor() const; QString activeColor() const;
QString inactiveColor() const; QString inactiveColor() const;
int count() const;
bool isCustom() const; bool isCustom() const;
float minValue() const; float minValue() const;
float maxValue() const; float maxValue() const;
@ -75,6 +77,7 @@ public:
// set methods // set methods
void setBar(const QString _bar = QString("cpu")); void setBar(const QString _bar = QString("cpu"));
void setActiveColor(const QString _color = QString("0,0,0,130")); void setActiveColor(const QString _color = QString("0,0,0,130"));
void setCount(const int _count = 100);
void setCustom(const bool _custom = false); void setCustom(const bool _custom = false);
void setInactiveColor(const QString _color = QString("255,255,255,130")); void setInactiveColor(const QString _color = QString("255,255,255,130"));
void setMinValue(const float _value = 0.0); void setMinValue(const float _value = 0.0);
@ -95,6 +98,7 @@ public slots:
private slots: private slots:
void changeColor(); void changeColor();
void changeCountState(const int state);
void changeValue(const int state); void changeValue(const int state);
private: private:
@ -106,6 +110,7 @@ private:
void translate(); void translate();
// properties // properties
QString m_bar = QString("cpu"); QString m_bar = QString("cpu");
int m_count = 100;
bool m_custom = false; bool m_custom = false;
QColor m_activeColor = QColor(0, 0, 0, 130); QColor m_activeColor = QColor(0, 0, 0, 130);
QColor m_inactiveColor = QColor(255, 255, 255, 130); QColor m_inactiveColor = QColor(255, 255, 255, 130);

View File

@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>416</width> <width>416</width>
<height>461</height> <height>481</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -187,6 +187,32 @@
</item> </item>
</layout> </layout>
</item> </item>
<item>
<widget class="QWidget" name="widget_count" native="true">
<layout class="QHBoxLayout" name="layout_count">
<item>
<widget class="QLabel" name="label_count">
<property name="text">
<string>Points count</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinBox_count">
<property name="maximum">
<number>2000</number>
</property>
<property name="singleStep">
<number>25</number>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item> <item>
<widget class="Line" name="line"> <widget class="Line" name="line">
<property name="orientation"> <property name="orientation">

View File

@ -42,15 +42,17 @@ GraphicalItemHelper::~GraphicalItemHelper()
void GraphicalItemHelper::setParameters(const QColor active, void GraphicalItemHelper::setParameters(const QColor active,
const QColor inactive, const int width, const QColor inactive, const int width,
const int height) const int height, const int count)
{ {
qCDebug(LOG_LIB) << "Use active color" << active << ", inactive" << inactive qCDebug(LOG_LIB) << "Use active color" << active << ", inactive" << inactive
<< ", width" << width << ", height" << height; << ", width" << width << ", height" << height << ", count"
<< count;
m_activeColor = active; m_activeColor = active;
m_inactiveColor = inactive; m_inactiveColor = inactive;
m_width = width; m_width = width;
m_height = height; m_height = height;
m_count = count;
} }
@ -77,24 +79,25 @@ void GraphicalItemHelper::paintCircle(const float &percent)
} }
void GraphicalItemHelper::paintGraph(const QList<float> &value) void GraphicalItemHelper::paintGraph(const float &value)
{ {
qCDebug(LOG_LIB) << "Paint with value" << value; qCDebug(LOG_LIB) << "Paint with value" << value;
storeValue(value);
QPen pen; QPen pen;
pen.setColor(m_activeColor); pen.setColor(m_activeColor);
// default norms // default norms
float normX float normX
= static_cast<float>(m_width) / static_cast<float>(value.count()); = static_cast<float>(m_width) / static_cast<float>(m_values.count());
float normY = static_cast<float>(m_height) / (1.5f * 100.0f); float normY = static_cast<float>(m_height) / 1.5f;
// paint graph // paint graph
for (int i = 0; i < value.count() - 1; i++) { for (int i = 0; i < m_values.count() - 1; i++) {
// some magic here // some magic here
float x1 = i * normX; float x1 = i * normX;
float y1 = -fabs(value.at(i)) * normY + 5.0f; float y1 = -fabs(m_values.at(i)) * normY + 5.0f;
float x2 = (i + 1) * normX; float x2 = (i + 1) * normX;
float y2 = -fabs(value.at(i + 1)) * normY + 5.0f; float y2 = -fabs(m_values.at(i + 1)) * normY + 5.0f;
m_scene->addLine(x1, y1, x2, y2, pen); m_scene->addLine(x1, y1, x2, y2, pen);
} }
} }
@ -173,3 +176,16 @@ QColor GraphicalItemHelper::stringToColor(const QString &color)
return qColor; return qColor;
} }
void GraphicalItemHelper::storeValue(const float &value)
{
qCDebug(LOG_LIB) << "Save value to array" << value;
if (m_values.count() == 0)
m_values.append(0.0);
else if (m_values.count() > m_count)
m_values.removeFirst();
m_values.append(value);
}

View File

@ -32,10 +32,10 @@ public:
virtual ~GraphicalItemHelper(); virtual ~GraphicalItemHelper();
// parameters // parameters
void setParameters(const QColor active, const QColor inactive, void setParameters(const QColor active, const QColor inactive,
const int width, const int height); const int width, const int height, const int count);
// paint methods // paint methods
void paintCircle(const float &percent); void paintCircle(const float &percent);
void paintGraph(const QList<float> &value); void paintGraph(const float &value);
void paintHorizontal(const float &percent); void paintHorizontal(const float &percent);
void paintVertical(const float &percent); void paintVertical(const float &percent);
// additional conversion methods // additional conversion methods
@ -44,11 +44,15 @@ public:
QColor stringToColor(const QString &color); QColor stringToColor(const QString &color);
private: private:
void storeValue(const float &value);
QGraphicsScene *m_scene = nullptr; QGraphicsScene *m_scene = nullptr;
int m_count;
QColor m_activeColor; QColor m_activeColor;
QColor m_inactiveColor; QColor m_inactiveColor;
int m_width; int m_width;
int m_height; int m_height;
// list of values which will be used to store data for graph type only
QList<float> m_values;
}; };