forked from ciphrex/mSIGNA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewaccountdialog.cpp
170 lines (142 loc) · 5.04 KB
/
newaccountdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
///////////////////////////////////////////////////////////////////////////////
//
// mSIGNA
//
// newaccountdialog.cpp
//
// Copyright (c) 2013 Eric Lombrozo
//
// All Rights Reserved.
#include "newaccountdialog.h"
#include <QtAlgorithms>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QListWidget>
#include <QCheckBox>
#include <QComboBox>
#include <QLabel>
#include <QDateTimeEdit>
#include <QCalendarWidget>
#include <stdexcept>
NewAccountDialog::NewAccountDialog(const QList<QString>& allKeychains, const QList<QString>& selectedKeychains, QWidget* parent)
: QDialog(parent)
{
if (allKeychains.isEmpty()) {
throw std::runtime_error(tr("You must first create at least one keychain.").toStdString());
}
keychainSet = selectedKeychains.toSet();
QList<QString> keychains = allKeychains;
qSort(keychains.begin(), keychains.end());
// Buttons
QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);
okButton = buttonBox->button(QDialogButtonBox::Ok);
okButton->setEnabled(false);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
// Account Name
QLabel* nameLabel = new QLabel();
nameLabel->setText(tr("Account Name:"));
nameEdit = new QLineEdit();
connect(nameEdit, &QLineEdit::textChanged, [this](const QString& /*text*/) { updateEnabled(); });
QHBoxLayout* nameLayout = new QHBoxLayout();
nameLayout->setSizeConstraint(QLayout::SetNoConstraint);
nameLayout->addWidget(nameLabel);
nameLayout->addWidget(nameEdit);
// Keychain List Widget
QLabel* selectionLabel = new QLabel(tr("Select keychains:"));
keychainListWidget = new QListWidget();
for (auto& keychain: keychains)
{
QCheckBox* checkBox = new QCheckBox(keychain);
checkBox->setCheckState(selectedKeychains.count(keychain) ? Qt::Checked : Qt::Unchecked);
connect(checkBox, &QCheckBox::stateChanged, [=](int state) { updateSelection(keychain, state); });
QListWidgetItem* item = new QListWidgetItem();
keychainListWidget->addItem(item);
keychainListWidget->setItemWidget(item, checkBox);
}
// Minimum Signatures
QLabel *minSigLabel = new QLabel();
minSigLabel->setText(tr("Minimum Signatures:"));
minSigComboBox = new QComboBox();
minSigLineEdit = new QLineEdit();
minSigLineEdit->setAlignment(Qt::AlignRight);
minSigComboBox->setLineEdit(minSigLineEdit);
QHBoxLayout* minSigLayout = new QHBoxLayout();
minSigLayout->setSizeConstraint(QLayout::SetNoConstraint);
minSigLayout->addWidget(minSigLabel);
minSigLayout->addWidget(minSigComboBox);
updateMinSigs();
// Creation Time
QDateTime localDateTime = QDateTime::currentDateTime();
QLabel* creationTimeLabel = new QLabel(tr("Creation Time ") + "(" + localDateTime.timeZoneAbbreviation() + "):");
creationTimeEdit = new QDateTimeEdit(QDateTime::currentDateTime());
creationTimeEdit->setDisplayFormat("yyyy.MM.dd hh:mm:ss");
creationTimeEdit->setCalendarPopup(true);
calendarWidget = new QCalendarWidget(this);
creationTimeEdit->setCalendarWidget(calendarWidget);
QHBoxLayout* creationTimeLayout = new QHBoxLayout();
creationTimeLayout->setSizeConstraint(QLayout::SetNoConstraint);
creationTimeLayout->addWidget(creationTimeLabel);
creationTimeLayout->addWidget(creationTimeEdit);
// Main Layout
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->setSizeConstraint(QLayout::SetNoConstraint);
mainLayout->addLayout(nameLayout);
mainLayout->addWidget(selectionLabel);
mainLayout->addWidget(keychainListWidget);
mainLayout->addLayout(minSigLayout);
mainLayout->addLayout(creationTimeLayout);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
}
NewAccountDialog::~NewAccountDialog()
{
delete calendarWidget;
}
QString NewAccountDialog::getName() const
{
return nameEdit->text();
}
int NewAccountDialog::getMinSigs() const
{
return minSigComboBox->currentIndex() + 1;
}
qint64 NewAccountDialog::getCreationTime() const
{
return creationTimeEdit->dateTime().toMSecsSinceEpoch();
}
void NewAccountDialog::updateSelection(const QString& keychain, int state)
{
if (state == Qt::Checked)
{
keychainSet.insert(keychain);
}
else
{
keychainSet.remove(keychain);
}
updateMinSigs();
updateEnabled();
}
void NewAccountDialog::updateMinSigs()
{
int maxSigs = minSigComboBox->count();
// Increase maximum
for (int i = maxSigs + 1; i <= keychainSet.size(); i++)
{
minSigComboBox->addItem(QString::number(i));
}
// Decrease maximum
while (minSigComboBox->count() > keychainSet.size())
{
minSigComboBox->removeItem(minSigComboBox->count() - 1);
}
}
void NewAccountDialog::updateEnabled()
{
okButton->setEnabled(!nameEdit->text().isEmpty() && keychainSet.size() > 0);
}