From d3846dc4d5c962b1bed5d068cd191c81ae8f9233 Mon Sep 17 00:00:00 2001 From: "konstantin.kukharenko" <konstantin.kukharenko@demlabs.net> Date: Mon, 24 Feb 2020 17:27:33 +0300 Subject: [PATCH] [*]Edit popup window --- auxiliary/defines.h | 2 +- controls/CustomComboBoxPopup.cpp | 78 +++++++++++------- controls/CustomComboBoxPopup.h | 29 +++++-- controls/CustomPlacementButton_New.cpp | 78 ++++++++---------- controls/CustomPlacementButton_New.h | 38 ++++++--- controls/CustomWidget.cpp | 110 ------------------------- controls/CustomWidget.h | 46 ----------- controls/ListModel.cpp | 54 ------------ controls/ListModel.h | 43 ---------- controls/controls.pri | 4 - 10 files changed, 137 insertions(+), 345 deletions(-) delete mode 100644 controls/CustomWidget.cpp delete mode 100644 controls/CustomWidget.h delete mode 100644 controls/ListModel.cpp delete mode 100644 controls/ListModel.h diff --git a/auxiliary/defines.h b/auxiliary/defines.h index 02f8874..da71b60 100644 --- a/auxiliary/defines.h +++ b/auxiliary/defines.h @@ -6,7 +6,7 @@ namespace Properties { static const QString TEXT = "text" ; static const QString STATE = "state" ; - static const QString HOVER = "hover" ; + static const char* HOVER = "hover" ; static const QString CHECKED = "checked"; } diff --git a/controls/CustomComboBoxPopup.cpp b/controls/CustomComboBoxPopup.cpp index c559fe8..2387dfe 100644 --- a/controls/CustomComboBoxPopup.cpp +++ b/controls/CustomComboBoxPopup.cpp @@ -1,45 +1,67 @@ #include "CustomComboBoxPopup.h" -CustomComboBoxPopup::CustomComboBoxPopup(const QString &a_caption,ListModel *model, QWidget *parent) - : QWidget (parent) + +CustomComboBoxPopup::CustomComboBoxPopup(QWidget *parent) + : QWidget (parent), + m_layout(new QVBoxLayout(this)), + m_layoutScroll(new QVBoxLayout(this)), + m_lblCaption(new QLabel(this)), + m_scaList(new QScrollArea(this)), + m_listButton(new QList<CustomPlacementButton_New*>()) { setFixedSize(parent->width(),parent->height()); setObjectName("wgtComboBoxPopup"); + m_scaList->setObjectName("scaList"); - if(model != nullptr) - { - m_model = model; - } - else - { - m_model = new ListModel(this); - } + m_scaList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + m_scaList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - 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("", "", m_lvwList)); - } + m_lblCaption->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed); - m_layout = new QVBoxLayout; m_layout->setSpacing(0); m_layout->setMargin(0); m_layout->addWidget(m_lblCaption); - m_layout->addWidget(m_lvwList); + m_layout->addWidget(m_scaList); m_layout->setAlignment(m_lblCaption,Qt::AlignHCenter); - m_layout->setAlignment(m_lvwList,Qt::AlignHCenter); + m_layout->setAlignment(m_scaList,Qt::AlignHCenter); + this->setLayout(m_layout); - connect(m_lvwList,&QListView::clicked,[=]{ - setVisible(false); - }); + m_layoutScroll->setSpacing(0); + m_layoutScroll->setMargin(0); + + m_scaList->setLayout(m_layoutScroll); + +} + +void CustomComboBoxPopup::setModel(QList<DataModel> *model) +{ + for(int i=0; i<model->size();i++) + { + // CustomPlacementButton_New tmp; + m_listButton->append(new CustomPlacementButton_New()); + m_listButton->last()->setIcon(model->at(i).iconPath); + m_listButton->last()->setText(model->at(i).text); + m_listButton->last()->setObjectName("btnServer"); + m_listButton->last()->addSubcontrol("arrow"); + + m_layoutScroll->addWidget(m_listButton->last()); + m_layoutScroll->setAlignment(m_listButton->last(),Qt::AlignHCenter); + QWidget *tmpWidget = new QWidget(); + if(i<model->size()-1) + { + tmpWidget->setProperty("state","sizeFixed"); //This setting is for fixing gaps between widgets other than the last. + } + + m_layoutScroll->addWidget(tmpWidget); + + } +} + + +void CustomComboBoxPopup::setTextCapture(const QString &text) +{ + m_lblCaption->setText(text); } diff --git a/controls/CustomComboBoxPopup.h b/controls/CustomComboBoxPopup.h index d8a6737..68a4fad 100644 --- a/controls/CustomComboBoxPopup.h +++ b/controls/CustomComboBoxPopup.h @@ -1,22 +1,39 @@ #ifndef CUSTOMCOMBOBOXPOPUP_H #define CUSTOCOMBOBOXPOPUP_H -#include <QMdiArea> #include <QLabel> #include <QListView> #include <QVBoxLayout> -#include "ListModel.h" -#include "CustomWidget.h" +#include <QStandardItemModel> +#include <QStandardItem> +#include <QScrollArea> +#include "CustomPlacementButton_New.h" + + +struct DataModel +{ + QString iconPath; + QString text; +}; class CustomComboBoxPopup: public QWidget { public: - CustomComboBoxPopup(const QString &a_Caption,ListModel *model = nullptr, QWidget *parent = nullptr); + CustomComboBoxPopup( QWidget *parent = nullptr); + /// Set model. + /// @param model Model date for button. + void setModel(QList<DataModel> *model); + /// Set capture name. + /// @param text Name. + void setTextCapture(const QString &text); private: + + QVBoxLayout *m_layout; + QVBoxLayout *m_layoutScroll; QLabel *m_lblCaption; - QListView *m_lvwList; - ListModel *m_model; + QScrollArea *m_scaList; + QList<CustomPlacementButton_New*> *m_listButton; }; #endif // CUSTOMCOMBOBOXPOPUP_H diff --git a/controls/CustomPlacementButton_New.cpp b/controls/CustomPlacementButton_New.cpp index d33a243..f32974a 100644 --- a/controls/CustomPlacementButton_New.cpp +++ b/controls/CustomPlacementButton_New.cpp @@ -18,6 +18,9 @@ CustomPlacementButton_New::CustomPlacementButton_New(QWidget *a_parent) m_lbText .setObjectName("text"); m_lbRightSpacing.setObjectName("rightSpacing"); + m_lbRightSpacing.setVisible(false); + m_lbLeftSpacing.setVisible(false); + m_subcontrols.append(&m_lbImage); m_subcontrols.append(&m_lbText); @@ -37,6 +40,8 @@ CustomPlacementButton_New::CustomPlacementButton_New(QWidget *a_parent) // Set up subcontroll ScaledContents: subcontrol->setScaledContents(true); m_layout->addWidget(subcontrol); + subcontrol->setProperty("hover", false ); + subcontrol->setProperty(qPrintable(Properties::CHECKED), false); } m_layout->addWidget(&m_lbRightSpacing); @@ -46,6 +51,9 @@ CustomPlacementButton_New::CustomPlacementButton_New(QWidget *a_parent) connect(this, &QAbstractButton::toggled, [=](bool a_checked) { this->setState(this->underMouse(), a_checked); }); + + m_styledshadow = new StyledDropShadowEffect(this); + } /** @brief Reimplemented QPushButton::setText method. Sets text property of text subcontrol. @@ -56,33 +64,11 @@ 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(); -//} +void CustomPlacementButton_New::setIcon(const QString &path) +{ + QPixmap icon(path); + m_lbImage.setPixmap(icon); +} /** @brief Updates appearance of image and text */ @@ -91,8 +77,11 @@ 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)) - { + m_lbText.setProperty(hoverProperty , isHover ); + m_lbText.setProperty(checkedProperty, isChecked); + m_lbText.style()->unpolish(&m_lbText); + m_lbText.style()->polish(&m_lbText); + for (QLabel *subcontrol: m_subcontrols) { subcontrol->setProperty(hoverProperty , isHover ); @@ -101,7 +90,6 @@ void CustomPlacementButton_New::setState(bool isHover, bool isChecked) subcontrol->style()->unpolish(subcontrol); subcontrol->style()->polish(subcontrol); } - } } /** @brief add new subcontrol and place it in layout @@ -114,7 +102,8 @@ void CustomPlacementButton_New::addSubcontrol(QString a_id) newSubcontrol->setObjectName(a_id); // newSubcontrol->widget()->setScaledContents(true); - + newSubcontrol->setProperty("hover", false ); +// newSubcontrol->setProperty(qPrintable(Properties::CHECKED), false); //add to list and layout m_subcontrols.append(newSubcontrol); m_layout->insertWidget(m_layout->count() - 1, newSubcontrol); @@ -148,6 +137,9 @@ void CustomPlacementButton_New::enterEvent(QEvent *event) if (isEnabled()) setState(true, isChecked()); + + m_styledshadow->updateStyle(HOVER_SHADOW); + setGraphicsEffect(m_styledshadow); } /** @brief A leave event is sent to the widget when the mouse cursor leaves the widget. @@ -159,18 +151,18 @@ void CustomPlacementButton_New::leaveEvent(QEvent *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) { + m_styledshadow->updateStyle(DEFAULT_SHADOW); + setGraphicsEffect(m_styledshadow); +} -// updateAppearance(); +/** @brief Reimplemented QPushButton::setObjectName method. Updates stylesheets. + * @param name Name + */ +void CustomPlacementButton_New::setObjectName(const QString &name) +{ + QPushButton::setObjectName(name); + m_styledshadow->updateStyleProperties(); + setGraphicsEffect(m_styledshadow); -// return QWidget::changeEvent(event); -// } -// return QWidget::changeEvent(event); -//} +} diff --git a/controls/CustomPlacementButton_New.h b/controls/CustomPlacementButton_New.h index df59a30..5e0f5da 100644 --- a/controls/CustomPlacementButton_New.h +++ b/controls/CustomPlacementButton_New.h @@ -7,6 +7,8 @@ #include <QDebug> #include <QEvent> #include <QLabel> +#include "StyledDropShadowEffect.h" +#include <QFontMetrics> enum class ImagePos {Left, Right}; @@ -20,10 +22,10 @@ enum class ImagePos {Left, Right}; *> #buttonName::text { *> ... *> } - *> #buttonName::text:hover { + *> #buttonName::text[hover="true"] { *> ... *> } - *> #buttonName::text:checked { + *> #buttonName::text[checked = "true"] { *> ... *> } *> #buttonName::text:checked:hover { @@ -32,10 +34,10 @@ enum class ImagePos {Left, Right}; *> #buttonName::image { *> ... *> } - *> #buttonName::image:hover { + *> #buttonName::image[hover="true"]{ *> ... *> } - *> #buttonName::image:checked { + *> #buttonName::image[checked = "true"]{ *> ... *> } *> #buttonName::image:checked:hover { @@ -52,21 +54,37 @@ class CustomPlacementButton_New : public QPushButton public: explicit CustomPlacementButton_New(QWidget *a_parent = Q_NULLPTR); - + /// Set text button. + /// @param text Set text. void setText(const QString &text); -// void setObjectName(const QString &name); -// void setCheckable(bool checkable); - + /// Form initialization. + /// @param path Path to Image. + void setIcon(const QString &path); + /// Set object name.. + /// @param name Object name. + void setObjectName(const QString &name); + /// Sets the state of the button. + /// @param isHover + /// @param isChecked void setState(bool isHover, bool isChecked); - + /// . + /// @param a_id Window GUI widget. void addSubcontrol(QString a_id); + /// Image Position on button. + /// @param a_position Enum ImagePos Right or Left. void setImagePosition(ImagePos a_position = ImagePos::Left); + private: + ///For effect. + StyledDropShadowEffect *m_styledshadow; protected: + /// Cursor in. + /// @param event Signal source. void enterEvent(QEvent *event); + /// Cursor out. + /// @param event Signal source. void leaveEvent(QEvent *event); -// void changeEvent(QEvent * event); QHBoxLayout *m_layout; QLabel m_lbLeftSpacing; ///<label for left spacing diff --git a/controls/CustomWidget.cpp b/controls/CustomWidget.cpp deleted file mode 100644 index 9ba78b7..0000000 --- a/controls/CustomWidget.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include "CustomWidget.h" -#include <QDebug> -#include <QPixmap> -#include <QStyle> -#include <QModelIndex> - -CustomWidget::CustomWidget(QWidget *parent) - : QWidget (parent) -{ - -} - -CustomWidget::CustomWidget(QString iconPath/* = ""*/, QString text/* = ""*/, QWidget *parent/* = nullptr*/) - : 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); - } -} - -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 deleted file mode 100644 index 4bd0f97..0000000 --- a/controls/CustomWidget.h +++ /dev/null @@ -1,46 +0,0 @@ -#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(QString iconPath = "", QString text = "", QWidget *parent = nullptr); - - bool selected() const; - void setSelected(bool selected); - -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); - - bool m_selected; - - - - - - ///////////////////////////////////////// - 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 deleted file mode 100644 index d1fb892..0000000 --- a/controls/ListModel.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#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 deleted file mode 100644 index d6959d4..0000000 --- a/controls/ListModel.h +++ /dev/null @@ -1,43 +0,0 @@ -#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 94d2eb4..d2546db 100644 --- a/controls/controls.pri +++ b/controls/controls.pri @@ -8,8 +8,6 @@ SOURCES += \ $$PWD/CustomLineHeightTextEdit.cpp \ $$PWD/CustomPlacementButton.cpp \ $$PWD/CustomPlacementButton_New.cpp \ - $$PWD/CustomWidget.cpp \ - $$PWD/ListModel.cpp \ $$PWD/ServersComboBox.cpp \ $$PWD/AnimatedLineEdit.cpp \ $$PWD/ScreenOverlaying.cpp \ @@ -26,8 +24,6 @@ HEADERS += \ $$PWD/CustomLineHeightTextEdit.h \ $$PWD/CustomPlacementButton.h \ $$PWD/CustomPlacementButton_New.h \ - $$PWD/CustomWidget.h \ - $$PWD/ListModel.h \ $$PWD/ServersComboBox.h \ $$PWD/AnimatedLineEdit.h \ $$PWD/ScreenOverlaying.h \ -- GitLab