diff --git a/sources/awesomewidgets/graphicalitem.cpp b/sources/awesomewidgets/graphicalitem.cpp index b48a621..b24e284 100644 --- a/sources/awesomewidgets/graphicalitem.cpp +++ b/sources/awesomewidgets/graphicalitem.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -47,12 +48,20 @@ GraphicalItem::GraphicalItem(QWidget *parent, const QString desktopName, connect(ui->checkBox_custom, SIGNAL(stateChanged(int)), this, SLOT(changeValue(int))); + connect(ui->checkBox_activeCheck, SIGNAL(stateChanged(int)), this, + SLOT(changeColorState(int))); + connect(ui->checkBox_inactiveCheck, SIGNAL(stateChanged(int)), this, + SLOT(changeColorState(int))); connect(ui->comboBox_type, SIGNAL(currentIndexChanged(int)), this, SLOT(changeCountState(int))); connect(ui->pushButton_activeColor, SIGNAL(clicked()), this, SLOT(changeColor())); connect(ui->pushButton_inactiveColor, SIGNAL(clicked()), this, SLOT(changeColor())); + connect(ui->pushButton_activeImage, SIGNAL(clicked()), this, + SLOT(changeImage())); + connect(ui->pushButton_inactiveImage, SIGNAL(clicked()), this, + SLOT(changeImage())); } @@ -73,13 +82,13 @@ GraphicalItem *GraphicalItem::copy(const QString _fileName, const int _number) GraphicalItem *item = new GraphicalItem(static_cast(parent()), _fileName, directories()); copyDefaults(item); - item->setActiveColor(activeColor()); + item->setActiveColor(m_activeColor); item->setBar(m_bar); item->setCount(m_count); item->setCustom(m_custom); item->setDirection(m_direction); item->setHeight(m_height); - item->setInactiveColor(inactiveColor()); + item->setInactiveColor(m_inactiveColor); item->setMaxValue(m_maxValue); item->setMinValue(m_minValue); item->setNumber(_number); @@ -144,13 +153,13 @@ QString GraphicalItem::bar() const QString GraphicalItem::activeColor() const { - return m_helper->colorToString(m_activeColor); + return m_activeColor; } QString GraphicalItem::inactiveColor() const { - return m_helper->colorToString(m_inactiveColor); + return m_inactiveColor; } @@ -266,7 +275,7 @@ void GraphicalItem::setActiveColor(const QString _color) { qCDebug(LOG_LIB) << "Color" << _color; - m_activeColor = m_helper->stringToColor(_color); + m_activeColor = _color; } @@ -292,7 +301,7 @@ void GraphicalItem::setInactiveColor(const QString _color) { qCDebug(LOG_LIB) << "Color" << _color; - m_inactiveColor = m_helper->stringToColor(_color); + m_inactiveColor = _color; } @@ -403,10 +412,10 @@ void GraphicalItem::readConfiguration() setMaxValue(settings.value(QString("X-AW-Max"), m_maxValue).toFloat()); setMinValue(settings.value(QString("X-AW-Min"), m_minValue).toFloat()); setActiveColor( - settings.value(QString("X-AW-ActiveColor"), activeColor()) + settings.value(QString("X-AW-ActiveColor"), m_activeColor) .toString()); setInactiveColor( - settings.value(QString("X-AW-InactiveColor"), inactiveColor()) + settings.value(QString("X-AW-InactiveColor"), m_inactiveColor) .toString()); setStrType(settings.value(QString("X-AW-Type"), strType()).toString()); setStrDirection( @@ -430,13 +439,6 @@ void GraphicalItem::readConfiguration() } -QVariantHash GraphicalItem::run() -{ - // required by abstract class - return QVariantHash(); -} - - int GraphicalItem::showConfiguration(const QVariant args) { qCDebug(LOG_LIB) << "Combobox arguments" << args; @@ -455,8 +457,8 @@ int GraphicalItem::showConfiguration(const QVariant args) ui->doubleSpinBox_max->setValue(m_maxValue); ui->doubleSpinBox_min->setValue(m_minValue); ui->spinBox_count->setValue(m_count); - ui->pushButton_activeColor->setText(activeColor()); - ui->pushButton_inactiveColor->setText(inactiveColor()); + ui->pushButton_activeColor->setText(m_activeColor); + ui->pushButton_inactiveColor->setText(m_inactiveColor); ui->comboBox_type->setCurrentIndex(static_cast(m_type)); ui->comboBox_direction->setCurrentIndex(static_cast(m_direction)); ui->spinBox_height->setValue(m_height); @@ -505,8 +507,8 @@ void GraphicalItem::writeConfiguration() const settings.setValue(QString("X-AW-Custom"), m_custom); settings.setValue(QString("X-AW-Max"), m_maxValue); settings.setValue(QString("X-AW-Min"), m_minValue); - settings.setValue(QString("X-AW-ActiveColor"), activeColor()); - settings.setValue(QString("X-AW-InactiveColor"), inactiveColor()); + settings.setValue(QString("X-AW-ActiveColor"), m_activeColor); + settings.setValue(QString("X-AW-InactiveColor"), m_inactiveColor); settings.setValue(QString("X-AW-Type"), strType()); settings.setValue(QString("X-AW-Direction"), strDirection()); settings.setValue(QString("X-AW-Height"), m_height); @@ -538,6 +540,22 @@ void GraphicalItem::changeColor() } +void GraphicalItem::changeColorState(const int state) +{ + qCDebug(LOG_LIB) << "Current color state is" << state; + + if (sender() == ui->checkBox_activeCheck) { + qCInfo(LOG_LIB) << "Change active color state"; + ui->widget_activeColor->setHidden(state == Qt::Unchecked); + ui->widget_activeImage->setHidden(state != Qt::Unchecked); + } else if (sender() == ui->checkBox_inactiveCheck) { + qCInfo(LOG_LIB) << "Change inactive color state"; + ui->widget_inactiveColor->setHidden(state == Qt::Unchecked); + ui->widget_inactiveImage->setHidden(state != Qt::Unchecked); + } +} + + void GraphicalItem::changeCountState(const int state) { qCDebug(LOG_LIB) << "Current state is" << state; @@ -547,6 +565,20 @@ void GraphicalItem::changeCountState(const int state) } +void GraphicalItem::changeImage() +{ + QString path = static_cast(sender())->text(); + QString directory = QFileInfo(path).absolutePath(); + QString newPath = QFileDialog::getOpenFileName( + this, tr("Select path"), directory, + tr("Images (*.png *.bpm *.jpg);;All files (*.*)")); + + qCInfo(LOG_LIB) << "Selected path" << newPath; + + return static_cast(sender())->setText(newPath); +} + + void GraphicalItem::changeValue(const int state) { qCDebug(LOG_LIB) << "Current state is" << state; @@ -560,10 +592,7 @@ void GraphicalItem::initScene() { // init scene m_scene = new QGraphicsScene(); - if (m_type == Graph) - m_scene->setBackgroundBrush(m_inactiveColor); - else - m_scene->setBackgroundBrush(QBrush(Qt::NoBrush)); + m_scene->setBackgroundBrush(QBrush(Qt::NoBrush)); // init view m_view = new QGraphicsView(m_scene); m_view->setStyleSheet(QString("background: transparent")); @@ -590,8 +619,12 @@ void GraphicalItem::translate() ui->label_customValue->setText(i18n("Value")); ui->label_max->setText(i18n("Max value")); ui->label_min->setText(i18n("Min value")); + ui->checkBox_activeCheck->setText(i18n("Use image for active")); ui->label_activeColor->setText(i18n("Active color")); + ui->label_activeImage->setText(i18n("Active image")); + ui->checkBox_inactiveCheck->setText(i18n("Use image for inactive")); ui->label_inactiveColor->setText(i18n("Inactive color")); + ui->label_inactiveImage->setText(i18n("Inactive image")); ui->label_type->setText(i18n("Type")); ui->label_direction->setText(i18n("Direction")); ui->label_height->setText(i18n("Height")); diff --git a/sources/awesomewidgets/graphicalitem.h b/sources/awesomewidgets/graphicalitem.h index 270eb17..117237c 100644 --- a/sources/awesomewidgets/graphicalitem.h +++ b/sources/awesomewidgets/graphicalitem.h @@ -92,13 +92,15 @@ public: public slots: void readConfiguration(); - QVariantHash run(); + QVariantHash run() { return QVariantHash(); }; int showConfiguration(const QVariant args = QVariant()); void writeConfiguration() const; private slots: void changeColor(); + void changeColorState(const int state); void changeCountState(const int state); + void changeImage(); void changeValue(const int state); private: @@ -112,8 +114,8 @@ private: QString m_bar = QString("cpu"); int m_count = 100; bool m_custom = false; - QColor m_activeColor = QColor(0, 0, 0, 130); - QColor m_inactiveColor = QColor(255, 255, 255, 130); + QString m_activeColor; + QString m_inactiveColor; float m_minValue = 0.0f; float m_maxValue = 100.0f; Type m_type = Horizontal; diff --git a/sources/awesomewidgets/graphicalitem.ui b/sources/awesomewidgets/graphicalitem.ui index 3000f49..5a5e2ae 100644 --- a/sources/awesomewidgets/graphicalitem.ui +++ b/sources/awesomewidgets/graphicalitem.ui @@ -7,7 +7,7 @@ 0 0 416 - 481 + 606 @@ -25,7 +25,7 @@ - Name + &Name Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -48,7 +48,7 @@ - Comment + &Comment Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -100,10 +100,22 @@ + + 0 + + + 0 + + + 0 + + + 0 + - Value + &Value Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -123,6 +135,18 @@ + + 0 + + + 0 + + + 0 + + + 0 + @@ -132,7 +156,7 @@ - Value + Va&lue Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -150,7 +174,7 @@ - Max value + &Max value Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -171,7 +195,7 @@ - Min value + Min val&ue Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -190,10 +214,22 @@ + + 0 + + + 0 + + + 0 + + + 0 + - Points count + &Points count Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -221,53 +257,211 @@ - + - - - Active color + + + Qt::Horizontal - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + 40 + 20 + - + - + + + + 0 + 0 + + - + Use image for active - + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Activ&e color + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + &Active image + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + - - - Inactive color + + + Qt::Horizontal - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + 40 + 20 + - + - + + + + 0 + 0 + + - + Use image for inactive + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + &Inactive color + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Inactive image + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + - Type + &Type Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -305,7 +499,7 @@ - Direction + &Direction Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -333,7 +527,7 @@ - Height + &Height Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -357,7 +551,7 @@ - Width + &Width Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter diff --git a/sources/awesomewidgets/graphicalitemhelper.cpp b/sources/awesomewidgets/graphicalitemhelper.cpp index c7f459e..680d90c 100644 --- a/sources/awesomewidgets/graphicalitemhelper.cpp +++ b/sources/awesomewidgets/graphicalitemhelper.cpp @@ -40,16 +40,30 @@ GraphicalItemHelper::~GraphicalItemHelper() } -void GraphicalItemHelper::setParameters(const QColor active, - const QColor inactive, const int width, +void GraphicalItemHelper::setParameters(const QString active, + const QString inactive, const int width, const int height, const int count) { qCDebug(LOG_LIB) << "Use active color" << active << ", inactive" << inactive << ", width" << width << ", height" << height << ", count" << count; - m_activeColor = active; - m_inactiveColor = inactive; + if (active.startsWith(QString("/"))) { + qCInfo(LOG_LIB) << "Found path, trying to load Pixmap from" << active; + m_activeImage = QPixmap(active); + if (m_activeImage.isNull()) + qCInfo(LOG_LIB) << "Invalid pixmap found" << active; + } else { + m_activeColor = stringToColor(active); + } + if (inactive.startsWith(QString("/"))) { + qCInfo(LOG_LIB) << "Found path, trying to load Pixmap from" << inactive; + m_inactiveImage = QPixmap(inactive); + if (m_inactiveImage.isNull()) + qCInfo(LOG_LIB) << "Invalid pixmap found" << inactive; + } else { + m_inactiveColor = stringToColor(inactive); + } m_width = width; m_height = height; m_count = count; @@ -63,6 +77,8 @@ void GraphicalItemHelper::paintCircle(const float &percent) QPen pen; pen.setWidth(1); QGraphicsEllipseItem *circle; + // 16 is because of qt. From Qt documentation: + // Returns the start angle for an ellipse segment in 16ths of a degree // inactive pen.setColor(m_inactiveColor); @@ -75,7 +91,7 @@ void GraphicalItemHelper::paintCircle(const float &percent) circle = m_scene->addEllipse(0.0, 0.0, m_width, m_height, pen, QBrush(m_activeColor, Qt::SolidPattern)); circle->setSpanAngle(-percent * 360.0f * 16.0f); - circle->setStartAngle(90.0f * 16.0f); + circle->setStartAngle(90 * 16); } @@ -83,6 +99,13 @@ void GraphicalItemHelper::paintGraph(const float &value) { qCDebug(LOG_LIB) << "Paint with value" << value; + // refresh background image + if (m_inactiveImage.isNull()) + m_scene->setBackgroundBrush(QBrush(m_inactiveColor)); + else + m_scene->setBackgroundBrush( + QBrush(m_inactiveImage.scaled(m_width, m_height))); + storeValue(value); QPen pen; pen.setColor(m_activeColor); diff --git a/sources/awesomewidgets/graphicalitemhelper.h b/sources/awesomewidgets/graphicalitemhelper.h index 30c2dc3..69da386 100644 --- a/sources/awesomewidgets/graphicalitemhelper.h +++ b/sources/awesomewidgets/graphicalitemhelper.h @@ -19,6 +19,7 @@ #define GRAPHICALITEMHELPER_H #include +#include #include @@ -31,7 +32,7 @@ public: QGraphicsScene *scene = nullptr); virtual ~GraphicalItemHelper(); // parameters - void setParameters(const QColor active, const QColor inactive, + void setParameters(const QString active, const QString inactive, const int width, const int height, const int count); // paint methods void paintCircle(const float &percent); @@ -47,8 +48,10 @@ private: void storeValue(const float &value); QGraphicsScene *m_scene = nullptr; int m_count = 100; - QColor m_activeColor; - QColor m_inactiveColor; + QColor m_activeColor = QColor(0, 0, 0, 130); + QColor m_inactiveColor = QColor(255, 255, 255, 130); + QPixmap m_activeImage; + QPixmap m_inactiveImage; int m_width = 100; int m_height = 100; // list of values which will be used to store data for graph type only