PIVX Core  5.6.99
P2P Digital Currency
qtutils.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 "qtutils.h"
6 
7 #include "guiconstants.h"
8 #include "qrencode.h"
9 #include "snackbar.h"
10 
11 #include <QFile>
12 #include <QGraphicsDropShadowEffect>
13 #include <QListView>
14 
15 Qt::Modifier SHORT_KEY
16 #ifdef Q_OS_MAC
17  = Qt::CTRL;
18 #else
19  = Qt::ALT;
20 #endif
21 
22 // Open dialog at the bottom
23 bool openDialog(QDialog* widget, QWidget* gui)
24 {
25  widget->setWindowFlags(Qt::CustomizeWindowHint);
26  widget->setAttribute(Qt::WA_TranslucentBackground, true);
27  QPropertyAnimation* animation = new QPropertyAnimation(widget, "pos");
28  animation->setDuration(300);
29  animation->setStartValue(QPoint(0, gui->height()));
30  animation->setEndValue(QPoint(0, gui->height() - widget->height()));
31  animation->setEasingCurve(QEasingCurve::OutQuad);
32  animation->start(QAbstractAnimation::DeleteWhenStopped);
33  widget->activateWindow();
34  widget->raise();
35  return widget->exec();
36 }
37 
38 void closeDialog(QDialog* widget, PIVXGUI* gui)
39 {
40  widget->setWindowFlags(Qt::CustomizeWindowHint);
41  widget->setAttribute(Qt::WA_TranslucentBackground, true);
42  QPropertyAnimation* animation = new QPropertyAnimation(widget, "pos");
43  animation->setDuration(300);
44  animation->setStartValue(widget->pos());
45  animation->setEndValue(QPoint(0, gui->height() + 100));
46  animation->setEasingCurve(QEasingCurve::OutQuad);
47  animation->start(QAbstractAnimation::DeleteWhenStopped);
48 }
49 
50 void openDialogFullScreen(QWidget* parent, QWidget* dialog)
51 {
52  dialog->setWindowFlags(Qt::CustomizeWindowHint);
53  dialog->move(0, 0);
54  dialog->show();
55  dialog->activateWindow();
56  dialog->resize(parent->width(), parent->height());
57 }
58 
59 bool openDialogWithOpaqueBackgroundY(QDialog* widget, PIVXGUI* gui, double posX, int posY, bool hideOpaqueBackground)
60 {
61  widget->setWindowFlags(Qt::CustomizeWindowHint);
62  widget->setAttribute(Qt::WA_TranslucentBackground, true);
63  QPropertyAnimation* animation = new QPropertyAnimation(widget, "pos");
64  animation->setDuration(300);
65  int xPos = gui->width() / posX;
66  animation->setStartValue(QPoint(xPos, gui->height()));
67  animation->setEndValue(QPoint(xPos, gui->height() / posY)); //- (gui->height()) / posY ));
68  animation->setEasingCurve(QEasingCurve::OutQuad);
69  animation->start(QAbstractAnimation::DeleteWhenStopped);
70  widget->activateWindow();
71  bool res = widget->exec();
72  if (hideOpaqueBackground) gui->showHide(false);
73  return res;
74 }
75 
76 bool openDialogWithOpaqueBackground(QDialog* widget, PIVXGUI* gui, double posX)
77 {
78  return openDialogWithOpaqueBackgroundY(widget, gui, posX, 5);
79 }
80 
82 {
83  widget->setWindowFlags(Qt::CustomizeWindowHint);
84  widget->setAttribute(Qt::WA_TranslucentBackground, true);
85 
86  widget->activateWindow();
87  widget->resize(gui->width(), gui->height());
88 
89  QPropertyAnimation* animation = new QPropertyAnimation(widget, "pos");
90  animation->setDuration(300);
91  int xPos = 0;
92  animation->setStartValue(QPoint(xPos, gui->height()));
93  animation->setEndValue(QPoint(xPos, 0));
94  animation->setEasingCurve(QEasingCurve::OutQuad);
95  animation->start(QAbstractAnimation::DeleteWhenStopped);
96  widget->activateWindow();
97  bool res = widget->exec();
98  gui->showHide(false);
99  return res;
100 }
101 
102 QPixmap encodeToQr(const QString& str, QString& errorStr, const QColor& qrColor)
103 {
104  if (!str.isEmpty()) {
105  // limit URI length
106  if (str.length() > MAX_URI_LENGTH) {
107  errorStr = "Resulting URI too long, try to reduce the text for label / message.";
108  return QPixmap();
109  } else {
110  QRcode* code = QRcode_encodeString(str.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
111  if (!code) {
112  errorStr = "Error encoding URI into QR Code.";
113  return QPixmap();
114  }
115  QImage myImage = QImage(code->width + 2, code->width + 2, QImage::Format_RGB32);
116  myImage.fill(0xffffff);
117  unsigned char* p = code->data;
118  for (int y = 0; y < code->width; y++) {
119  for (int x = 0; x < code->width; x++) {
120  myImage.setPixel(x + 1, y + 1, ((*p & 1) ? qrColor.rgb() : 0xffffff));
121  p++;
122  }
123  }
124  QRcode_free(code);
125  return QPixmap::fromImage(myImage);
126  }
127  }
128  return QPixmap();
129 }
130 
131 void setFilterAddressBook(QComboBox* filter, SortEdit* lineEdit)
132 {
133  initComboBox(filter, lineEdit);
134  filter->addItem(QObject::tr("All"), "");
135  filter->addItem(QObject::tr("Receiving"), AddressTableModel::Receive);
136  filter->addItem(QObject::tr("Contacts"), AddressTableModel::Send);
137  filter->addItem(QObject::tr("Cold Staking"), AddressTableModel::ColdStaking);
138  filter->addItem(QObject::tr("Delegator"), AddressTableModel::Delegator);
139  filter->addItem(QObject::tr("Delegable"), AddressTableModel::Delegable);
140  filter->addItem(QObject::tr("Staking Contacts"), AddressTableModel::ColdStakingSend);
141  filter->addItem(QObject::tr("Shielded Recv"), AddressTableModel::ShieldedReceive);
142  filter->addItem(QObject::tr("Shielded Contact"), AddressTableModel::ShieldedSend);
143 }
144 
145 void setSortTx(QComboBox* filter, SortEdit* lineEdit)
146 {
147  // Sort Transactions
148  initComboBox(filter, lineEdit);
149  filter->addItem(QObject::tr("Date desc"), SortTx::DATE_DESC);
150  filter->addItem(QObject::tr("Date asc"), SortTx::DATE_ASC);
151  filter->addItem(QObject::tr("Amount desc"), SortTx::AMOUNT_ASC);
152  filter->addItem(QObject::tr("Amount asc"), SortTx::AMOUNT_DESC);
153 }
154 
155 void setSortTxTypeFilter(QComboBox* filter, SortEdit* lineEditType)
156 {
157  initComboBox(filter, lineEditType);
158  filter->addItem(QObject::tr("All"), TransactionFilterProxy::ALL_TYPES);
159  filter->addItem(QObject::tr("Received"),
164  filter->addItem(QObject::tr("Sent"),
169  filter->addItem(QObject::tr("Shield"),
176  filter->addItem(QObject::tr("Mined"), TransactionFilterProxy::TYPE(TransactionRecord::Generated));
177  filter->addItem(QObject::tr("Minted"), TransactionFilterProxy::TYPE(TransactionRecord::StakeMint));
178  filter->addItem(QObject::tr("MN reward"), TransactionFilterProxy::TYPE(TransactionRecord::MNReward));
179  filter->addItem(QObject::tr("To yourself"), TransactionFilterProxy::TYPE(TransactionRecord::SendToSelf) |
183  filter->addItem(QObject::tr("Cold stakes"), TransactionFilterProxy::TYPE(TransactionRecord::StakeDelegated));
184  filter->addItem(QObject::tr("Hot stakes"), TransactionFilterProxy::TYPE(TransactionRecord::StakeHot));
186  filter->addItem(QObject::tr("Delegations"), TransactionFilterProxy::TYPE(TransactionRecord::P2CSDelegation));
187  filter->addItem(QObject::tr("DAO payment"), TransactionFilterProxy::TYPE(TransactionRecord::BudgetPayment));
188 }
189 
190 void setupSettings(QSettings* settings)
191 {
192  if (!settings->contains("lightTheme")) {
193  settings->setValue("lightTheme", true);
194  }
195 }
196 
197 QSettings* settings = nullptr;
198 
199 QSettings* getSettings()
200 {
201  if (!settings) {
202  settings = new QSettings();
203  // Setup initial values if them are not there
205  }
206 
207  return settings;
208 }
209 
211 {
212  return getSettings()->value("lightTheme", true).toBool();
213 }
214 
215 void setTheme(bool isLight)
216 {
217  QSettings* settings = getSettings();
218  settings->setValue("theme", isLight ? "default" : "default-dark");
219  settings->setValue("lightTheme", isLight);
220 }
221 
222 
223 // Style
224 
225 void updateStyle(QWidget* widget)
226 {
227  widget->style()->unpolish(widget);
228  widget->style()->polish(widget);
229  widget->update();
230 }
231 
232 
233 QColor getRowColor(bool isLightTheme, bool isHovered, bool isSelected)
234 {
235  if (isSelected) {
236  return QColor("#25b088ff");
237  } else if (isHovered) {
238  return QColor("#25bababa");
239  } else {
240  return isLightTheme ? QColor("#ffffff") : QColor("#0f0b16");
241  }
242 }
243 
244 void initComboBox(QComboBox* combo, QLineEdit* lineEdit, QString cssClass, bool setView)
245 {
246  setCssProperty(combo, std::move(cssClass));
247  combo->setEditable(true);
248  if (lineEdit) {
249  lineEdit->setReadOnly(true);
250  lineEdit->setAlignment(Qt::AlignRight);
251  combo->setLineEdit(lineEdit);
252  }
253  combo->setStyleSheet("selection-background-color:transparent;");
254  if (setView) combo->setView(new QListView());
255 }
256 
257 void fillAddressSortControls(SortEdit* seType, SortEdit* seOrder, QComboBox* boxType, QComboBox* boxOrder)
258 {
259  // Sort Type
260  initComboBox(boxType, seType, "btn-combo-small");
261  boxType->addItem(QObject::tr("by Label"), AddressTableModel::Label);
262  boxType->addItem(QObject::tr("by Address"), AddressTableModel::Address);
263  boxType->addItem(QObject::tr("by Date"), AddressTableModel::Date);
264  boxType->setCurrentIndex(0);
265  // Sort Order
266  initComboBox(boxOrder, seOrder, "btn-combo-small");
267  boxOrder->addItem("asc", Qt::AscendingOrder);
268  boxOrder->addItem("desc", Qt::DescendingOrder);
269  boxOrder->setCurrentIndex(0);
270 }
271 
272 void initCssEditLine(QLineEdit* edit, bool isDialog)
273 {
274  if (isDialog)
275  setCssEditLineDialog(edit, true, false);
276  else
277  setCssEditLine(edit, true, false);
278  setShadow(edit);
279  edit->setAttribute(Qt::WA_MacShowFocusRect, false);
280 }
281 
282 void setCssEditLine(QLineEdit* edit, bool isValid, bool forceUpdate)
283 {
284  setCssProperty(edit, isValid ? "edit-primary" : "edit-primary-error", forceUpdate);
285 }
286 
287 void setCssEditLineDialog(QLineEdit* edit, bool isValid, bool forceUpdate)
288 {
289  setCssProperty(edit, isValid ? "edit-primary-dialog" : "edit-primary-dialog-error", forceUpdate);
290 }
291 
292 void setShadow(QWidget* edit)
293 {
294  QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect();
295  shadowEffect->setColor(QColor(0, 0, 0, 22));
296  shadowEffect->setXOffset(0);
297  shadowEffect->setYOffset(3);
298  shadowEffect->setBlurRadius(6);
299  edit->setGraphicsEffect(shadowEffect);
300 }
301 
302 void setCssBtnPrimary(QPushButton* btn, bool forceUpdate)
303 {
304  setCssProperty(btn, "btn-primary", forceUpdate);
305 }
306 
307 void setCssBtnSecondary(QPushButton* btn, bool forceUpdate)
308 {
309  setCssProperty(btn, "btn-secundary", forceUpdate);
310 }
311 
312 void setCssTextBodyDialog(std::initializer_list<QWidget*> args)
313 {
314  for (QWidget* w : args) {
316  }
317 }
318 
319 void setCssTextBodyDialog(QWidget* widget)
320 {
321  setCssProperty(widget, "text-body1-dialog", false);
322 }
323 
324 void setCssTitleScreen(QLabel* label)
325 {
326  setCssProperty(label, "text-title-screen", false);
327 }
328 
329 void setCssSubtitleScreen(QWidget* wid)
330 {
331  setCssProperty(wid, "text-subtitle", false);
332 }
333 
334 void setCssProperty(std::initializer_list<QWidget*> args, const QString& value)
335 {
336  for (QWidget* w : args) {
337  setCssProperty(w, value);
338  }
339 }
340 
341 void setCssProperty(QWidget* wid, const QString& value, bool forceUpdate)
342 {
343  if (wid->property("cssClass") == value) return;
344  wid->setProperty("cssClass", value);
345  forceUpdateStyle(wid, forceUpdate);
346 }
347 
348 void forceUpdateStyle(QWidget* widget, bool forceUpdate)
349 {
350  if (forceUpdate)
351  updateStyle(widget);
352 }
353 
354 void forceUpdateStyle(std::initializer_list<QWidget*> args)
355 {
356  for (QWidget* w : args) {
357  forceUpdateStyle(w, true);
358  }
359 }
static const QString Delegator
Specifies cold staking addresses which delegated tokens to this wallet and ARE being staked.
static const QString ColdStakingSend
Specifies send cold staking addresses (simil 'contacts')
static const QString ShieldedSend
Specifies shielded receive address.
@ Address
Bitcoin address.
@ Label
User specified label.
@ Date
Address creation date.
static const QString ShieldedReceive
Specifies shielded send address.
static const QString Send
Specifies send address.
static const QString Delegable
Specifies cold staking addresses which delegated tokens to this wallet.
static const QString Receive
Specifies receive address.
static const QString ColdStaking
Specifies cold staking own addresses.
PIVX GUI main class.
Definition: pivxgui.h:46
void showHide(bool show)
Definition: pivxgui.cpp:579
static const quint32 ALL_TYPES
Type filter bit field (all types)
static quint32 TYPE(int type)
@ AMOUNT_ASC
@ DATE_DESC
@ DATE_ASC
@ AMOUNT_DESC
QSettings * settings
Definition: qtutils.cpp:197
void forceUpdateStyle(QWidget *widget, bool forceUpdate)
Definition: qtutils.cpp:348
void initComboBox(QComboBox *combo, QLineEdit *lineEdit, QString cssClass, bool setView)
Definition: qtutils.cpp:244
void setFilterAddressBook(QComboBox *filter, SortEdit *lineEdit)
Definition: qtutils.cpp:131
bool isLightTheme()
Definition: qtutils.cpp:210
void setupSettings(QSettings *settings)
Definition: qtutils.cpp:190
void openDialogFullScreen(QWidget *parent, QWidget *dialog)
Definition: qtutils.cpp:50
void closeDialog(QDialog *widget, PIVXGUI *gui)
Definition: qtutils.cpp:38
QSettings * getSettings()
Definition: qtutils.cpp:199
void setCssTextBodyDialog(std::initializer_list< QWidget * > args)
Definition: qtutils.cpp:312
bool openDialogWithOpaqueBackgroundFullScreen(QDialog *widget, PIVXGUI *gui)
Definition: qtutils.cpp:81
void setCssEditLineDialog(QLineEdit *edit, bool isValid, bool forceUpdate)
Definition: qtutils.cpp:287
bool openDialogWithOpaqueBackgroundY(QDialog *widget, PIVXGUI *gui, double posX, int posY, bool hideOpaqueBackground)
Definition: qtutils.cpp:59
void setCssTitleScreen(QLabel *label)
Definition: qtutils.cpp:324
Qt::Modifier SHORT_KEY
Definition: qtutils.cpp:19
void initCssEditLine(QLineEdit *edit, bool isDialog)
Definition: qtutils.cpp:272
bool openDialogWithOpaqueBackground(QDialog *widget, PIVXGUI *gui, double posX)
Definition: qtutils.cpp:76
void setSortTxTypeFilter(QComboBox *filter, SortEdit *lineEditType)
Definition: qtutils.cpp:155
void setShadow(QWidget *edit)
Definition: qtutils.cpp:292
void setCssProperty(std::initializer_list< QWidget * > args, const QString &value)
Definition: qtutils.cpp:334
QColor getRowColor(bool isLightTheme, bool isHovered, bool isSelected)
Definition: qtutils.cpp:233
void setCssSubtitleScreen(QWidget *wid)
Definition: qtutils.cpp:329
QPixmap encodeToQr(const QString &str, QString &errorStr, const QColor &qrColor)
Definition: qtutils.cpp:102
void setCssBtnPrimary(QPushButton *btn, bool forceUpdate)
Definition: qtutils.cpp:302
void setSortTx(QComboBox *filter, SortEdit *lineEdit)
Definition: qtutils.cpp:145
bool openDialog(QDialog *widget, QWidget *gui)
Definition: qtutils.cpp:23
void setCssEditLine(QLineEdit *edit, bool isValid, bool forceUpdate)
Definition: qtutils.cpp:282
void setCssBtnSecondary(QPushButton *btn, bool forceUpdate)
Definition: qtutils.cpp:307
void fillAddressSortControls(SortEdit *seType, SortEdit *seOrder, QComboBox *boxType, QComboBox *boxOrder)
Definition: qtutils.cpp:257
void updateStyle(QWidget *widget)
Definition: qtutils.cpp:225
void setTheme(bool isLight)
Definition: qtutils.cpp:215