From 4e9291ad0995119927fdd29440de0ae7090b76b4 Mon Sep 17 00:00:00 2001 From: "alexandr.mruchok" <alexandrmruchok@demlabs.net> Date: Thu, 20 Feb 2020 21:06:09 +0200 Subject: [PATCH] [+] CustomPlacementButton_New [*] Changed CustomWidget [+] Properties::HOVER, CHECKED --- auxiliary/defines.h | 7 +- controls/CustomComboBoxPopup.cpp | 2 +- controls/CustomPlacementButton_New.cpp | 176 +++++++++++++++++++++++++ controls/CustomPlacementButton_New.h | 79 +++++++++++ controls/CustomWidget.cpp | 18 ++- controls/CustomWidget.h | 15 ++- controls/controls.pri | 2 + 7 files changed, 289 insertions(+), 10 deletions(-) create mode 100644 controls/CustomPlacementButton_New.cpp create mode 100644 controls/CustomPlacementButton_New.h diff --git a/auxiliary/defines.h b/auxiliary/defines.h index 9d83145..02f8874 100644 --- a/auxiliary/defines.h +++ b/auxiliary/defines.h @@ -4,9 +4,10 @@ #include <QString> namespace Properties { - static const QString TEXT = "text"; - static const QString STATE = "state"; - + static const QString TEXT = "text" ; + static const QString STATE = "state" ; + static const QString HOVER = "hover" ; + static const QString CHECKED = "checked"; } diff --git a/controls/CustomComboBoxPopup.cpp b/controls/CustomComboBoxPopup.cpp index 0413982..c559fe8 100644 --- a/controls/CustomComboBoxPopup.cpp +++ b/controls/CustomComboBoxPopup.cpp @@ -27,7 +27,7 @@ CustomComboBoxPopup::CustomComboBoxPopup(const QString &a_caption,ListModel *mod for(int i = 0; i < m_model->getData().size();i++) { - m_lvwList->setIndexWidget(m_model->index(i),new CustomWidget(i,m_model->getData().at(i),m_lvwList)); + m_lvwList->setIndexWidget(m_model->index(i),new CustomWidget("", "", m_lvwList)); } m_layout = new QVBoxLayout; diff --git a/controls/CustomPlacementButton_New.cpp b/controls/CustomPlacementButton_New.cpp new file mode 100644 index 0000000..d33a243 --- /dev/null +++ b/controls/CustomPlacementButton_New.cpp @@ -0,0 +1,176 @@ +#include "CustomPlacementButton_New.h" +#include "AppStyleSheetHandler.h" +#include "defines.h" + +/** @brief constructor + * @param a_parent object parent + */ +CustomPlacementButton_New::CustomPlacementButton_New(QWidget *a_parent) + :QPushButton (a_parent), + m_layout (new QHBoxLayout(this)), + m_lbLeftSpacing (this), + m_lbImage (this), + m_lbText (this), + m_lbRightSpacing(this) +{ + m_lbLeftSpacing .setObjectName("leftSpacing"); + m_lbImage .setObjectName("image"); + m_lbText .setObjectName("text"); + m_lbRightSpacing.setObjectName("rightSpacing"); + + m_subcontrols.append(&m_lbImage); + m_subcontrols.append(&m_lbText); + + // Set up subcontroll object names: + + //Setup layout + m_layout->setMargin(0); + m_layout->setSpacing(0); + + //Setup spacing setSizePolicy + m_lbLeftSpacing .setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + m_lbRightSpacing.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + + //Adding subcontrols to layout + m_layout->addWidget(&m_lbLeftSpacing); + for (QLabel *subcontrol: m_subcontrols){ + // Set up subcontroll ScaledContents: + subcontrol->setScaledContents(true); + m_layout->addWidget(subcontrol); + } + m_layout->addWidget(&m_lbRightSpacing); + + setLayout(m_layout); + + // on toggled update Appearance + connect(this, &QAbstractButton::toggled, [=](bool a_checked) { + this->setState(this->underMouse(), a_checked); + }); +} + +/** @brief Reimplemented QPushButton::setText method. Sets text property of text subcontrol. + * @param text Text + */ +void CustomPlacementButton_New::setText(const QString &text) +{ + m_lbText.setText(text); +} + +///** @brief Reimplemented QPushButton::setObjectName method. Updates stylesheets. +// * @param text Text +// */ +//void CustomPlacementButton_New::setObjectName(const QString &name) +//{ +// QObject::setObjectName(name); + +// updateStyleSheets(); +//} + + +//void CustomPlacementButton_New::setCheckable(bool checkable) +//{ +// QPushButton::setCheckable(checkable); + +// updateStyleSheets(); +//} + +///** @brief Initialization of image and text stylesheets +// */ +//void CustomPlacementButton_New::updateStyleSheets() +//{ +// for (StyledSubcontrol_New *subcontrol: m_subcontrols){ +// subcontrol->updateStylesheets(); +// } +// updateAppearance(); +//} + +/** @brief Updates appearance of image and text + */ +void CustomPlacementButton_New::setState(bool isHover, bool isChecked) +{ + const char* hoverProperty = qPrintable(Properties::HOVER); + const char* checkedProperty = qPrintable(Properties::CHECKED); + + if (isHover != this->property(hoverProperty) && isChecked != this->property(checkedProperty)) + { + for (QLabel *subcontrol: m_subcontrols) + { + subcontrol->setProperty(hoverProperty , isHover ); + subcontrol->setProperty(checkedProperty, isChecked); + + subcontrol->style()->unpolish(subcontrol); + subcontrol->style()->polish(subcontrol); + } + } +} + +/** @brief add new subcontrol and place it in layout + * @param a_id text id of subcontrol wich is using in CSS file + */ +void CustomPlacementButton_New::addSubcontrol(QString a_id) +{ +// StyledSubcontrol_New *newSubcontrol = new StyledSubcontrol_New(a_id, this); + QLabel *newSubcontrol = new QLabel((QPushButton*)this); + newSubcontrol->setObjectName(a_id); + +// newSubcontrol->widget()->setScaledContents(true); + + //add to list and layout + m_subcontrols.append(newSubcontrol); + m_layout->insertWidget(m_layout->count() - 1, newSubcontrol); +} + +/** @brief Set image position relative to text (left or right) + * @param a_position image position relatife to text (Left/Right) + */ +void CustomPlacementButton_New::setImagePosition(ImagePos a_position /*= ImagePos::Left*/) +{ + int imageIndex = m_layout->indexOf(&m_lbImage); + if (a_position == ImagePos::Left && imageIndex == 2) + { + m_layout->removeWidget(&m_lbImage); + m_layout->insertWidget(1, &m_lbImage); + } + else if (a_position == ImagePos::Right && imageIndex == 1) + { + m_layout->removeWidget(&m_lbImage); + m_layout->insertWidget(2, &m_lbImage); + } +} + + +/** @brief event is sent to the widget when the mouse cursor enters the widget. + * @param event + */ +void CustomPlacementButton_New::enterEvent(QEvent *event) +{ + Q_UNUSED(event); + + if (isEnabled()) + setState(true, isChecked()); +} + +/** @brief A leave event is sent to the widget when the mouse cursor leaves the widget. + * @param event + */ +void CustomPlacementButton_New::leaveEvent(QEvent *event) +{ + Q_UNUSED(event); + + if (isEnabled()) + setState(false, isChecked()); +} + +///** @brief Reimplemented QWidget::changeEvent is sent to the widget when "enabled" changed. +// * @param event +// */ +//void CustomPlacementButton_New::changeEvent(QEvent *event) +//{ +// if (event->type() == QEvent::EnabledChange) { + +// updateAppearance(); + +// return QWidget::changeEvent(event); +// } +// return QWidget::changeEvent(event); +//} diff --git a/controls/CustomPlacementButton_New.h b/controls/CustomPlacementButton_New.h new file mode 100644 index 0000000..df59a30 --- /dev/null +++ b/controls/CustomPlacementButton_New.h @@ -0,0 +1,79 @@ +#ifndef CUSTOMPLACEMENTBUTTON_NEW_H +#define CUSTOMPLACEMENTBUTTON_NEW_H + +#include <QPushButton> +#include <QHBoxLayout> +#include <QStyle> +#include <QDebug> +#include <QEvent> +#include <QLabel> + +enum class ImagePos {Left, Right}; + +/** @brief QPushButton with subControls "text" and "image" + * + * @details Places image and text from left to right in QHBoxLayout. + * Set style in .css file in format + *> #buttonName #leftSpacing { + *> ...; //if max-width==0, left alinment + *> } + *> #buttonName::text { + *> ... + *> } + *> #buttonName::text:hover { + *> ... + *> } + *> #buttonName::text:checked { + *> ... + *> } + *> #buttonName::text:checked:hover { + *> ... + *> } + *> #buttonName::image { + *> ... + *> } + *> #buttonName::image:hover { + *> ... + *> } + *> #buttonName::image:checked { + *> ... + *> } + *> #buttonName::image:checked:hover { + *> ... + *> } + *> #buttonName #rightSpacing { + *> ...//if max-width==0, right alignment + *> } + * @todo Is searching style in comments also! +*/ +class CustomPlacementButton_New : public QPushButton +{ + Q_OBJECT +public: + + explicit CustomPlacementButton_New(QWidget *a_parent = Q_NULLPTR); + + void setText(const QString &text); +// void setObjectName(const QString &name); +// void setCheckable(bool checkable); + + void setState(bool isHover, bool isChecked); + + void addSubcontrol(QString a_id); + void setImagePosition(ImagePos a_position = ImagePos::Left); + +private: +protected: + void enterEvent(QEvent *event); + void leaveEvent(QEvent *event); +// void changeEvent(QEvent * event); + + QHBoxLayout *m_layout; + QLabel m_lbLeftSpacing; ///<label for left spacing + QList<QLabel*> m_subcontrols; + QLabel m_lbImage; ///<label with image + QLabel m_lbText; ///<label with text + QLabel m_lbRightSpacing; ///<label for right spacing +}; + +#endif // CUSTOMPLACEMENTBUTTON_NEW_H diff --git a/controls/CustomWidget.cpp b/controls/CustomWidget.cpp index c9b48d9..9ba78b7 100644 --- a/controls/CustomWidget.cpp +++ b/controls/CustomWidget.cpp @@ -10,12 +10,12 @@ CustomWidget::CustomWidget(QWidget *parent) } -CustomWidget::CustomWidget(int index,DataModel model,QListView *listView, QWidget *parent) +CustomWidget::CustomWidget(QString iconPath/* = ""*/, QString text/* = ""*/, QWidget *parent/* = nullptr*/) : QWidget (parent) { - m_index = index; - m_data = &model; - m_listView = listView; +// m_index = index; +// m_data = &model; +// m_listView = listView; setObjectName("wgtDelegate"); setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed); @@ -98,3 +98,13 @@ void CustomWidget::setStyle(int state) setGraphicsEffect(m_styledshadow); } } + +bool CustomWidget::selected() const +{ + return m_selected; +} + +void CustomWidget::setSelected(bool selected) +{ + m_selected = selected; +} diff --git a/controls/CustomWidget.h b/controls/CustomWidget.h index 6a61787..4bd0f97 100644 --- a/controls/CustomWidget.h +++ b/controls/CustomWidget.h @@ -7,11 +7,15 @@ #include "StyledDropShadowEffect.h" #include "ListModel.h" #include <QListView> -class CustomWidget:public QWidget +class CustomWidget: public QWidget { public: CustomWidget(QWidget *parent = nullptr); - CustomWidget(int index, DataModel model,QListView *listView = nullptr, QWidget *parent = nullptr); + CustomWidget(QString iconPath = "", QString text = "", QWidget *parent = nullptr); + + bool selected() const; + void setSelected(bool selected); + protected: void enterEvent(QEvent *event); void leaveEvent(QEvent *event); @@ -21,6 +25,13 @@ private: //Updates styles on mouseover and on setting activity of an element. void setStyle(int state); + bool m_selected; + + + + + + ///////////////////////////////////////// QLabel *m_lblFlag; QLabel *m_lblText; QLabel *m_lblCheckIcon; diff --git a/controls/controls.pri b/controls/controls.pri index 60ea740..d4e500c 100644 --- a/controls/controls.pri +++ b/controls/controls.pri @@ -6,6 +6,7 @@ SOURCES += \ $$PWD/CustomComboBoxPopup.cpp \ $$PWD/CustomLineHeightLabel.cpp \ $$PWD/CustomPlacementButton.cpp \ + $$PWD/CustomPlacementButton_New.cpp \ $$PWD/CustomWidget.cpp \ $$PWD/ListModel.cpp \ $$PWD/ServersComboBox.cpp \ @@ -22,6 +23,7 @@ HEADERS += \ $$PWD/CustomComboBoxPopup.h \ $$PWD/CustomLineHeightLabel.h \ $$PWD/CustomPlacementButton.h \ + $$PWD/CustomPlacementButton_New.h \ $$PWD/CustomWidget.h \ $$PWD/ListModel.h \ $$PWD/ServersComboBox.h \ -- GitLab