-
Notifications
You must be signed in to change notification settings - Fork 3
/
baseform.cpp
260 lines (212 loc) · 5.92 KB
/
baseform.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "toolkit.h"
#include "setting.h"
#include "baseform.h"
#include <QShortcut>
#include <QWidget>
#include <QLabel>
#include <QToolButton>
#include <QLineEdit>
#include <QTreeWidget>
#include <QComboBox>
#include <QListWidget>
#include <QPlainTextEdit>
#define PROP_EDIT "edit"
#define PROP_DIRT "dirt"
#define PROP_TARG "targ"
BaseForm::BaseForm(QWidget* p, Qt::WindowFlags f)
:QWidget(p, f),m_cntRecv(0),m_cntSend(0),m_labRecv(0),m_labSend(0),m_cnlist(0),
timer(new QTimer(this))
{
connect(timer, SIGNAL(timeout()), this, SLOT(updateCountLabel()));
}
BaseForm::~BaseForm()
{
if(timer != nullptr)
{
timer->disconnect(this);
delete timer;
}
}
bool BaseForm::init()
{
if (!initForm() || !initHotkeys())
return false;
// initConfig();
m_logger.setProperty(SET_SEC_DIR, property(SET_SEC_DIR).toString());
return true;
}
void BaseForm::initCounter(QLabel* r, QLabel* s)
{
m_labRecv = r;
m_labSend = s;
}
void BaseForm::initLogger(QCheckBox* w, QToolButton* c, QTreeWidget* o, QPlainTextEdit* d
/*---->add a param by davidWu 2014/1/2--->*/, QCheckBox *x)
{
m_logger.init(o, w, d, x);
connect(c, SIGNAL(released()), this, SLOT(clear()));
connect(&m_logger, SIGNAL(clearLog()), this, SLOT(clear()));
bindFocus(o, Qt::Key_F3);
QShortcut* wr = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
QShortcut* cl = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_D), this);
QShortcut* sl = new QShortcut(QKeySequence(Qt::Key_F4), this);
sl->setProperty(PROP_TARG, qVariantFromValue((void*)d));
connect(wr, SIGNAL(activated()), w, SLOT(click()));
connect(sl, SIGNAL(activated()), this, SLOT(hotOutput()));
connect(cl, SIGNAL(activated()), this, SLOT(clear()));
connect(this, SIGNAL(output(const QString&)), &m_logger, SLOT(output(const QString&)));
connect(this, SIGNAL(output(const QString&, const char*, quint32)), &m_logger, SLOT(output(const QString&, const char*, quint32)));
}
void BaseForm::initLister(QToolButton* a, QToolButton* k, QListWidget* l)
{
m_cnlist = l;
QShortcut* sk = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_K), this);
QShortcut* sa = new QShortcut(QKeySequence(Qt::ALT + Qt::Key_A), this);
connect(sk, SIGNAL(activated()), this, SLOT(kill()));
connect(sa, SIGNAL(activated()), m_cnlist, SLOT(selectAll()));
connect(k, SIGNAL(released()), this, SLOT(kill()));
connect(a, SIGNAL(released()), m_cnlist, SLOT(selectAll()));
bindFocus(m_cnlist, Qt::Key_F2);
}
void BaseForm::bindBuffer(qint32 id, QLineEdit* e, QToolButton* s, QComboBox* d)
{
s->setProperty(PROP_EDIT, qVariantFromValue((void*)e));
s->setProperty(PROP_DIRT, qVariantFromValue((void*)d));
connect(s, SIGNAL(released()), this, SLOT(send()));
bindClick(s, Qt::Key_0 + id + Qt::CTRL);
bindFocus(e, Qt::Key_0 + id + Qt::ALT);
bindFocus(d, Qt::Key_0 + id + Qt::CTRL + Qt::SHIFT);
}
void BaseForm::bindFocus(QWidget* w, qint32 k)
{
QShortcut* s = new QShortcut(QKeySequence(k), this);
s->setProperty(PROP_TARG, qVariantFromValue((void*)w));
connect(s, SIGNAL(activated()), this, SLOT(focus()));
}
void BaseForm::bindClick(QAbstractButton* b, qint32 k)
{
QShortcut* s = new QShortcut(QKeySequence(k), this);
connect(s, SIGNAL(activated()), b, SLOT(click()));
}
void BaseForm::bindSelect(QComboBox* b, qint32 i, qint32 k)
{
QShortcut* s = new QShortcut(QKeySequence(k), this);
s->setProperty(PROP_TARG, qVariantFromValue((void*)b));
s->setObjectName(QString::number(i));
connect(s, SIGNAL(activated()), this, SLOT(select()));
}
void BaseForm::focus()
{
QWidget* w = (QWidget*)sender()->property(PROP_TARG).value<void*>();
if (w) w->setFocus(Qt::TabFocusReason);
}
void BaseForm::hotOutput()
{
QPlainTextEdit* t = (QPlainTextEdit*)sender()->property(PROP_TARG).value<void*>();
if (t)
{
t->setFocus(Qt::TabFocusReason);
t->selectAll();
}
}
void BaseForm::select()
{
QComboBox* b = (QComboBox*)sender()->property(PROP_TARG).value<void*>();
if (b && b->isEnabled())
{
qint32 i = sender()->objectName().toInt();
if (i < 0)
{
i = b->currentIndex() + 1;
if (i >= b->count()) i = 0;
}
b->setCurrentIndex(i);
}
}
void BaseForm::countRecv(qint32 bytes)
{
if (bytes < 0)
m_cntRecv = 0;
else
m_cntRecv += bytes;
// m_labRecv->setText(QString::number(m_cntRecv));
}
void BaseForm::countSend(qint32 bytes)
{
if (bytes < 0)
m_cntSend = 0;
else
m_cntSend += bytes;
// m_labSend->setText(QString::number(m_cntSend));
}
void BaseForm::send()
{
QLineEdit* e = (QLineEdit*)sender()->property(PROP_EDIT).value<void*>();
QComboBox* d = (QComboBox*)sender()->property(PROP_DIRT).value<void*>();
if (e)
send(e->text(), (d?d->currentText():""));
}
void BaseForm::clear()
{
m_logger.clear();
lock();
countRecv(-1);
countSend(-1);
unlock();
}
void BaseForm::kill()
{
if (lock(100))
{
QStringList list;
listerSelected(list);
kill(list);
unlock();
}
}
void BaseForm::listerSelected(QStringList& output)
{
qint32 i = m_cnlist->count();
while (i--)
{
QListWidgetItem* itm = m_cnlist->item(i);
if (itm && itm->isSelected())
output << itm->text();
}
}
void BaseForm::listerAdd(const QString& caption)
{
// listerRemove(caption);
//edit by daviwu 2014/2/11
// m_cnlist->addItem(caption);
if(connectsList.indexOf(caption) != -1)
return;
connectsList.push_back(caption);
//end
}
void BaseForm::listerRemove(const QString& caption)
{
// qint32 i = m_cnlist->count();
// while (i--)
// {
// QListWidgetItem* itm = m_cnlist->item(i);
// if (itm && itm->text() == caption)
// delete m_cnlist->takeItem(i);
// }
int index = connectsList.indexOf(caption);
if(index != -1)
{
connectsList.removeAt(index);
}
}
//add by davidWu 2014/2/11
void BaseForm::updateCountLabel()
{
m_labRecv->setText(QString::number(m_cntRecv));
m_labSend->setText(QString::number(m_cntSend));
m_cnlist->clear();
auto iter = connectsList.constBegin();
while(iter != connectsList.constEnd())
m_cnlist->addItem(*iter++);
}
//end