From 1ffeb943a13205b1d71fd1acea37bc14b57232e5 Mon Sep 17 00:00:00 2001
From: "konstantin.kukharenko" <konstantin.kukharenko@demlabs.net>
Date: Mon, 17 Feb 2020 09:58:56 +0000
Subject: [PATCH] [+]Add signal

---
 controls/CustomComboBox.cpp      |   6 +-
 controls/CustomComboBox.h        |   3 +
 controls/CustomComboBoxPopup.cpp |  45 ++++++++++++++
 controls/CustomComboBoxPopup.h   |  22 +++++++
 controls/CustomWidget.cpp        | 100 +++++++++++++++++++++++++++++++
 controls/CustomWidget.h          |  35 +++++++++++
 controls/ListModel.cpp           |  54 +++++++++++++++++
 controls/ListModel.h             |  43 +++++++++++++
 controls/controls.pri            |   6 ++
 9 files changed, 313 insertions(+), 1 deletion(-)
 create mode 100644 controls/CustomComboBoxPopup.cpp
 create mode 100644 controls/CustomComboBoxPopup.h
 create mode 100644 controls/CustomWidget.cpp
 create mode 100644 controls/CustomWidget.h
 create mode 100644 controls/ListModel.cpp
 create mode 100644 controls/ListModel.h

diff --git a/controls/CustomComboBox.cpp b/controls/CustomComboBox.cpp
index b35f4f7..1e28470 100644
--- a/controls/CustomComboBox.cpp
+++ b/controls/CustomComboBox.cpp
@@ -1,5 +1,5 @@
 #include "CustomComboBox.h"
-
+#include <QDebug>
 /** @brief constructor
  *  @param a_parent object parent
  */
@@ -19,6 +19,10 @@ void CustomComboBox::setObjectName(const QString &name)
     setGraphicsEffect(m_styledshadow);
 }
 
+void CustomComboBox::showPopup()
+{
+    emit showCustomWindow();
+}
 /** @brief Reimplemented QComboBox::enterEvent is sent to the widget when the mouse cursor enters the widget.
  *  @param event
  */
diff --git a/controls/CustomComboBox.h b/controls/CustomComboBox.h
index 5d7ad71..a67319d 100644
--- a/controls/CustomComboBox.h
+++ b/controls/CustomComboBox.h
@@ -22,6 +22,9 @@ class CustomComboBox : public QComboBox
 public:
     CustomComboBox(QWidget *parent = Q_NULLPTR);
     void setObjectName(const QString &name);
+    void showPopup();
+signals:
+    void showCustomWindow();
 protected:
     void enterEvent(QEvent *event);
     void leaveEvent(QEvent *event);
diff --git a/controls/CustomComboBoxPopup.cpp b/controls/CustomComboBoxPopup.cpp
new file mode 100644
index 0000000..0413982
--- /dev/null
+++ b/controls/CustomComboBoxPopup.cpp
@@ -0,0 +1,45 @@
+#include "CustomComboBoxPopup.h"
+
+CustomComboBoxPopup::CustomComboBoxPopup(const QString &a_caption,ListModel *model, QWidget *parent)
+    : QWidget (parent)
+{
+    setFixedSize(parent->width(),parent->height());
+    setObjectName("wgtComboBoxPopup");
+
+    if(model != nullptr)
+    {
+        m_model = model;
+    }
+    else
+    {
+        m_model = new ListModel(this);
+    }
+
+    m_lblCaption = new QLabel(this);
+    m_lblCaption->setObjectName("lblCaption");
+    m_lblCaption->setText(a_caption);
+    m_lblCaption->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
+
+    m_lvwList = new QListView(this);
+    m_lvwList->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
+    m_lvwList->setObjectName("lvwList");
+    m_lvwList->setModel(m_model);
+
+    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_layout = new QVBoxLayout;
+    m_layout->setSpacing(0);
+    m_layout->setMargin(0);
+    m_layout->addWidget(m_lblCaption);
+    m_layout->addWidget(m_lvwList);
+    m_layout->setAlignment(m_lblCaption,Qt::AlignHCenter);
+    m_layout->setAlignment(m_lvwList,Qt::AlignHCenter);
+    this->setLayout(m_layout);
+    connect(m_lvwList,&QListView::clicked,[=]{
+        setVisible(false);
+    });
+
+}
diff --git a/controls/CustomComboBoxPopup.h b/controls/CustomComboBoxPopup.h
new file mode 100644
index 0000000..d8a6737
--- /dev/null
+++ b/controls/CustomComboBoxPopup.h
@@ -0,0 +1,22 @@
+#ifndef CUSTOMCOMBOBOXPOPUP_H
+#define CUSTOCOMBOBOXPOPUP_H
+
+#include <QMdiArea>
+#include <QLabel>
+#include <QListView>
+#include <QVBoxLayout>
+#include "ListModel.h"
+#include "CustomWidget.h"
+
+class CustomComboBoxPopup: public QWidget
+{
+public:
+    CustomComboBoxPopup(const QString &a_Caption,ListModel *model = nullptr, QWidget *parent = nullptr);
+private:
+    QVBoxLayout *m_layout;
+    QLabel *m_lblCaption;
+    QListView *m_lvwList;
+    ListModel *m_model;
+};
+
+#endif // CUSTOMCOMBOBOXPOPUP_H
diff --git a/controls/CustomWidget.cpp b/controls/CustomWidget.cpp
new file mode 100644
index 0000000..c9b48d9
--- /dev/null
+++ b/controls/CustomWidget.cpp
@@ -0,0 +1,100 @@
+#include "CustomWidget.h"
+#include <QDebug>
+#include <QPixmap>
+#include <QStyle>
+#include <QModelIndex>
+
+CustomWidget::CustomWidget(QWidget *parent)
+    : QWidget (parent)
+{
+
+}
+
+CustomWidget::CustomWidget(int index,DataModel model,QListView *listView, QWidget *parent)
+    : QWidget (parent)
+{
+    m_index = index;
+    m_data = &model;
+    m_listView = listView;
+
+    setObjectName("wgtDelegate");
+    setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
+
+    m_lblFlag = new QLabel(this);
+    m_lblFlag->setObjectName("lblFlag");
+    m_lblFlag->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
+
+    QPixmap pixmap(m_data->iconPath);
+    m_lblFlag->setPixmap(pixmap);
+
+    m_lblText = new QLabel(this);
+    m_lblText->setObjectName("lblText");
+    m_lblText->setText(m_data->text);
+    m_lblText->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
+
+    m_lblCheckIcon = new QLabel(this);
+    m_lblCheckIcon->setObjectName("lblCheckIcon");
+
+    m_lblCheckIcon->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
+    m_mainLayout = new QHBoxLayout();
+
+    m_mainLayout->setSpacing(0);
+    m_mainLayout->setMargin(0);
+
+    m_mainLayout->addWidget(m_lblFlag);
+    m_mainLayout->addWidget(m_lblText);
+    m_mainLayout->addWidget(m_lblCheckIcon);
+    this->setLayout(m_mainLayout);
+
+    m_styledshadow = new StyledDropShadowEffect(this);
+
+    show();
+}
+
+void CustomWidget::enterEvent(QEvent *event)
+{
+    Q_UNUSED(event)
+    setStyle(1);
+}
+
+void CustomWidget::leaveEvent(QEvent *event)
+{
+    Q_UNUSED(event)
+    if(m_listView->currentIndex().row()!=m_index)
+        setStyle(0);
+}
+
+void CustomWidget::hideEvent(QHideEvent *event)
+{
+    Q_UNUSED(event)
+    if(m_listView->currentIndex().row()==m_index)
+    {
+        setStyle(1);
+    }
+    else
+        setStyle(0);
+}
+
+void CustomWidget::setStyle(int state)
+{
+    setProperty("state",state);
+    style()->unpolish(this);
+    style()->polish(this);
+    m_lblCheckIcon->setProperty("state",state);
+    style()->unpolish(m_lblCheckIcon);
+    style()->polish(m_lblCheckIcon);
+    m_lblText->setProperty("state",state);
+    style()->unpolish(m_lblText);
+    style()->polish(m_lblText);
+    update();
+    if(!state)
+    {
+        m_styledshadow->updateStyle(DEFAULT_SHADOW);
+        setGraphicsEffect(m_styledshadow);
+    }
+    else
+    {
+        m_styledshadow->updateStyle(HOVER_SHADOW);
+        setGraphicsEffect(m_styledshadow);
+    }
+}
diff --git a/controls/CustomWidget.h b/controls/CustomWidget.h
new file mode 100644
index 0000000..6a61787
--- /dev/null
+++ b/controls/CustomWidget.h
@@ -0,0 +1,35 @@
+#ifndef CUSTOMWIDGET_H
+#define CUSTOMWIDGET_H
+
+#include <QWidget>
+#include <QLabel>
+#include <QHBoxLayout>
+#include "StyledDropShadowEffect.h"
+#include "ListModel.h"
+#include <QListView>
+class CustomWidget:public QWidget
+{
+public:
+    CustomWidget(QWidget *parent = nullptr);
+    CustomWidget(int index, DataModel model,QListView *listView = nullptr, QWidget *parent = nullptr);
+protected:
+    void enterEvent(QEvent *event);
+    void leaveEvent(QEvent *event);
+    void hideEvent(QHideEvent *event);
+
+private:
+    //Updates styles on mouseover and on setting activity of an element.
+    void setStyle(int state);
+
+    QLabel *m_lblFlag;
+    QLabel *m_lblText;
+    QLabel *m_lblCheckIcon;
+    QHBoxLayout *m_mainLayout;
+    StyledDropShadowEffect *m_styledshadow;
+    DataModel *m_data;
+    int m_index;
+    QListView *m_listView;
+
+};
+
+#endif // CUSTOMWIDGET_H
diff --git a/controls/ListModel.cpp b/controls/ListModel.cpp
new file mode 100644
index 0000000..d1fb892
--- /dev/null
+++ b/controls/ListModel.cpp
@@ -0,0 +1,54 @@
+#include "ListModel.h"
+
+
+ListModel::ListModel(QObject *parent):QAbstractListModel(parent)
+{
+
+    DataModel tmpModel;
+    for(int i = 0;i<6;i++)
+    {
+        tmpModel.text = "kelvin-testnet.Cellframe";
+        tmpModel.iconPath ="img1";
+        m_data.append(tmpModel);
+    }
+
+}
+ListModel::ListModel(QList<DataModel> *list_model,QObject *parent)
+{
+    Q_UNUSED(parent)
+
+    for(int i = 0;i<list_model->size();i++)
+        m_data.append(list_model->at(i));
+}
+
+
+int ListModel::rowCount(const QModelIndex &parent) const
+{
+    if (parent.isValid()) {
+        return 0;
+    }
+    return m_data.size();
+}
+
+QVariant ListModel::data(const QModelIndex &index, int role) const
+{
+    if (!index.isValid()) {
+        return QVariant();
+    }
+
+        return QVariant();
+}
+
+QHash<int, QByteArray> ListModel::roleNames() const
+{
+    QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
+
+    return roles;
+}
+
+QList<DataModel> ListModel::getData()
+{
+    return m_data;
+}
+
+
diff --git a/controls/ListModel.h b/controls/ListModel.h
new file mode 100644
index 0000000..d6959d4
--- /dev/null
+++ b/controls/ListModel.h
@@ -0,0 +1,43 @@
+#ifndef LISTMODEL_H
+#define LISTMODEL_H
+
+#include <QAbstractListModel>
+#include <QDebug>
+#include <QString>
+#include <QStringList>
+//class ListModel
+//{
+//public:
+//    ListModel();
+//};
+
+struct DataModel
+{
+    QString iconPath;
+    QString text;
+};
+
+
+class ListModel : public QAbstractListModel
+{
+    Q_OBJECT
+
+public:
+
+    ListModel(QObject *parent = nullptr);
+    ListModel(QList<DataModel> *list_model,QObject *parent = nullptr);
+
+
+    virtual int rowCount(const QModelIndex &parent) const;
+    virtual QVariant data(const QModelIndex &index, int role) const;
+    virtual QHash<int, QByteArray> roleNames() const;
+
+    QList<DataModel> getData();
+
+private:
+    QList<DataModel> m_data;
+};
+
+
+
+#endif // LISTMODEL_H
diff --git a/controls/controls.pri b/controls/controls.pri
index 5f309b6..f435d17 100644
--- a/controls/controls.pri
+++ b/controls/controls.pri
@@ -2,8 +2,11 @@ SOURCES += \
     $$PWD/AnimationChangingWidget.cpp \
     $$PWD/ComboBoxDelegate.cpp \
     $$PWD/CustomComboBox.cpp \
+    $$PWD/CustomComboBoxPopup.cpp \
     $$PWD/CustomLineHeightLabel.cpp \
     $$PWD/CustomPlacementButton.cpp \
+    $$PWD/CustomWidget.cpp \
+    $$PWD/ListModel.cpp \
     $$PWD/ServersComboBox.cpp \
     $$PWD/AnimatedLineEdit.cpp \
     $$PWD/ScreenOverlaying.cpp \
@@ -14,8 +17,11 @@ HEADERS += \
     $$PWD/AnimationChangingWidget.h \
     $$PWD/ComboBoxDelegate.h \
     $$PWD/CustomComboBox.h \
+    $$PWD/CustomComboBoxPopup.h \
     $$PWD/CustomLineHeightLabel.h \
     $$PWD/CustomPlacementButton.h \
+    $$PWD/CustomWidget.h \
+    $$PWD/ListModel.h \
     $$PWD/ServersComboBox.h \
     $$PWD/AnimatedLineEdit.h \
     $$PWD/ScreenOverlaying.h \
-- 
GitLab