PIVX Core  5.6.99
P2P Digital Currency
contactsdropdown.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2021 The PIVX Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "contactsdropdown.h"
6 
7 #include "addresslabelrow.h"
8 #include "addresstablemodel.h"
9 #include "contactdropdownrow.h"
10 #include "furlistrow.h"
11 #include "qtutils.h"
12 #include "walletmodel.h"
13 #include <QPainter>
14 #include <QSizePolicy>
15 
16 #define DECORATION_SIZE 70
17 #define NUM_ITEMS 2
18 
19 class ContViewHolder : public FurListRow<QWidget*>
20 {
21 public:
23 
24  explicit ContViewHolder(bool _isLightTheme) : FurListRow(), isLightTheme(_isLightTheme){}
25 
26  ContactDropdownRow* createHolder(int pos) override{
27  if (!row) row = new ContactDropdownRow();
28  row->init(true, false);
29  return row;
30  }
31 
32  void init(QWidget* holder,const QModelIndex &index, bool isHovered, bool isSelected) const override{
33  ContactDropdownRow* row = static_cast<ContactDropdownRow*>(holder);
34  row->update(isLightTheme, isHovered, isSelected);
35  QString address = index.data(Qt::DisplayRole).toString();
36  QModelIndex sibling = index.sibling(index.row(), AddressTableModel::Label);
37  QString label = sibling.data(Qt::DisplayRole).toString();
38  row->setData(address, label);
39  }
40 
41  QColor rectColor(bool isHovered, bool isSelected) override{
42  return getRowColor(isLightTheme, isHovered, isSelected);
43  }
44 
45  ~ContViewHolder() override{}
46 
48  ContactDropdownRow* row = nullptr;
49 };
50 
51 ContactsDropdown::ContactsDropdown(int minWidth, int minHeight, PIVXGUI* _window, QWidget* _parent) : PWidget(_window, _parent)
52 {
53  this->setStyleSheet(_window->styleSheet());
54  init(minWidth, minHeight);
55 }
56 
57 ContactsDropdown::ContactsDropdown(int minWidth, int minHeight, PWidget* parent) : PWidget(parent)
58 {
59  this->setStyleSheet(parent->styleSheet());
60  init(minWidth, minHeight);
61 }
62 
63 void ContactsDropdown::init(int minWidth, int minHeight)
64 {
68  this
69  );
70 
71  setMinimumWidth(minWidth);
72  setMinimumHeight(minHeight);
73  setContentsMargins(0,0,0,0);
74  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
75 
76  frameList = new QFrame(this);
77  frameList->setProperty("cssClass", "container-border-light");
78  frameList->setContentsMargins(10,10,10,10);
79  frameList->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
80  list = new QListView(frameList);
81  list->setMinimumWidth(minWidth);
82  list->setProperty("cssClass", "container-border-light");
83  list->setItemDelegate(delegate);
84  list->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE));
85  list->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2));
86  list->setAttribute(Qt::WA_MacShowFocusRect, false);
87  list->setSelectionBehavior(QAbstractItemView::SelectRows);
88 
89  connect(list, &QListView::clicked, this, &ContactsDropdown::handleClick);
90 }
91 
92 void ContactsDropdown::setWalletModel(WalletModel* _model, const QStringList& type){
93  if (!model) {
94  model = _model->getAddressTableModel();
95  this->filter = new AddressFilterProxyModel(type, this);
96  this->filter->setSourceModel(model);
97  this->filter->sort(AddressTableModel::Label, Qt::AscendingOrder);
98  list->setModel(this->filter);
99  list->setModelColumn(AddressTableModel::Address);
100  } else {
101  setType(type);
102  }
103 }
104 
105 void ContactsDropdown::setType(const QStringList& type) {
106  if (filter)
107  filter->setType(type);
108 }
109 
110 void ContactsDropdown::resizeList(int minWidth, int mintHeight){
111  list->setMinimumWidth(minWidth);
112  setMinimumWidth(minWidth);
113  setMinimumHeight(mintHeight);
114  frameList->setMinimumHeight(mintHeight);
115  frameList->setMinimumWidth(minWidth);
116  list->setMinimumHeight(mintHeight);
117  list->resize(mintHeight,mintHeight);
118  list->adjustSize();
119  frameList->resize(minWidth, mintHeight);
120  resize(minWidth, mintHeight);
121  adjustSize();
122  update();
123 }
124 
125 void ContactsDropdown::handleClick(const QModelIndex &index){
126  QModelIndex rIndex = (filter) ? filter->mapToSource(index) : index;
127  QString address = rIndex.data(Qt::DisplayRole).toString();
128  QModelIndex sibling = rIndex.sibling(rIndex.row(), AddressTableModel::Label);
129  QString label = sibling.data(Qt::DisplayRole).toString();
130  Q_EMIT contactSelected(address, label);
131  close();
132 }
133 
134 void ContactsDropdown::changeTheme(bool isLightTheme, QString& theme){
135  static_cast<ContViewHolder*>(this->delegate->getRowFactory())->isLightTheme = isLightTheme;
136 }
void setType(const QString &type)
@ Address
Bitcoin address.
@ Label
User specified label.
ContactDropdownRow * createHolder(int pos) override
QColor rectColor(bool isHovered, bool isSelected) override
~ContViewHolder() override
ContViewHolder(bool _isLightTheme)
void init(QWidget *holder, const QModelIndex &index, bool isHovered, bool isSelected) const override
ContactDropdownRow * row
void update(bool isLightTheme, bool isHover, bool isSelected)
void init(bool isLightTheme, bool isHover)
void setData(QString address, QString label)
AddressFilterProxyModel * filter
FurAbstractListItemDelegate * delegate
ContactsDropdown(int minWidth, int minHeight, PWidget *parent=nullptr)
void handleClick(const QModelIndex &index)
void resizeList(int minWidth, int mintHeight)
void changeTheme(bool isLightTheme, QString &theme) override
void contactSelected(QString address, QString label)
void setType(const QStringList &type)
AddressTableModel * model
void setWalletModel(WalletModel *_model, const QStringList &type)
PIVX GUI main class.
Definition: pivxgui.h:46
void init()
Definition: pwidget.cpp:15
Interface to PIVX wallet from Qt view code.
Definition: walletmodel.h:109
AddressTableModel * getAddressTableModel()
#define DECORATION_SIZE
#define NUM_ITEMS
bool isLightTheme()
Definition: qtutils.cpp:210
QColor getRowColor(bool isLightTheme, bool isHovered, bool isSelected)
Definition: qtutils.cpp:233