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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/**
* Copyright (C) 2018 Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301, USA.
**/
#include "biometricdeviceswidget.h"
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
#include <QDebug>
#include <QAbstractItemView>
#include <QStyledItemDelegate>
BiometricDevicesWidget::BiometricDevicesWidget(BiometricProxy *proxy, int uid,QWidget *parent)
: QWidget(parent),
proxy(proxy),
m_uid(uid)
{
initUI();
if(proxy && proxy->isValid())
{
connect(proxy, &BiometricProxy::USBDeviceHotPlug,
this, &BiometricDevicesWidget::onUSBDeviceHotPlug);
updateDevice();
}
resize(500, 500);
}
void BiometricDevicesWidget::initUI()
{
lblPrompt = new QLabel(this);
lblPrompt->setObjectName(QStringLiteral("lblBioetricDevicesPrompt"));
lblPrompt->setText(tr("Please select the biometric device"));
lblPrompt->setAlignment(Qt::AlignHCenter);
lblDeviceType = new QLabel(this);
lblDeviceType->setObjectName(QStringLiteral("lblDeviceType"));
lblDeviceType->setText(tr("Device type:"));
lblDeviceType->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
QStyledItemDelegate* itemDelegate = new QStyledItemDelegate();
cmbDeviceType = new QComboBox(this);
cmbDeviceType->view()->parentWidget()->setWindowFlags(Qt::Popup|Qt::FramelessWindowHint);
cmbDeviceType->view()->parentWidget()->setAttribute(Qt::WA_TranslucentBackground);
cmbDeviceType->setObjectName(QStringLiteral("cmbDeviceType"));
cmbDeviceType->setMaxVisibleItems(5);
cmbDeviceType->setItemDelegate(itemDelegate);
connect(cmbDeviceType, SIGNAL(currentIndexChanged(int)),
this, SLOT(onCmbDeviceTypeCurrentIndexChanged(int)));
lblDeviceName = new QLabel(this);
lblDeviceName->setObjectName(QStringLiteral("lblDeviceName"));
lblDeviceName->setText(tr("Device name:"));
lblDeviceName->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
cmbDeviceName = new QComboBox(this);
cmbDeviceName->view()->parentWidget()->setWindowFlags(Qt::Popup|Qt::FramelessWindowHint);
cmbDeviceName->view()->parentWidget()->setAttribute(Qt::WA_TranslucentBackground);
cmbDeviceName->setObjectName(QStringLiteral("cmbDeviceName"));
cmbDeviceName->setMaxVisibleItems(5);
cmbDeviceName->setItemDelegate(itemDelegate);
btnOK = new QPushButton(tr("OK"), this);
btnOK->setObjectName(QStringLiteral("OKButton"));
btnOK->setCursor(Qt::PointingHandCursor);
connect(btnOK, &QPushButton::clicked,
this, &BiometricDevicesWidget::onOKButtonClicked);
}
void BiometricDevicesWidget::setUser(int user)
{
m_uid = user;
}
void BiometricDevicesWidget::resizeEvent(QResizeEvent */*event*/)
{
lblPrompt->setGeometry(0, 0, width(), 40);
lblDeviceType->setGeometry(100, lblPrompt->geometry().bottom() + 40,
120, 20);
cmbDeviceType->setGeometry(100, lblDeviceType->geometry().bottom() + 15,
300, 40);
lblDeviceName->setGeometry(100, cmbDeviceType->geometry().bottom() + 80,
120, 20);
cmbDeviceName->setGeometry(100, lblDeviceName->geometry().bottom() + 15,
300, 40);
btnOK->setGeometry(100, cmbDeviceName->geometry().bottom() + 80, 140, 38);
}
void BiometricDevicesWidget::updateDevice()
{
deviceMap.clear();
DeviceList deviceList = proxy->GetDevList();
for(auto pDeviceInfo : deviceList)
{
qDebug() << *pDeviceInfo;
if(proxy->GetUserDevFeatureCount(m_uid,pDeviceInfo->id) > 0)
deviceMap[pDeviceInfo->deviceType].push_back(pDeviceInfo);
}
cmbDeviceType->clear();
for(int type : deviceMap.keys())
{
QString iconPath = QString(UKUI_BIOMETRIC_IMAGES_PATH"icon/%1.png")
.arg(DeviceType::getDeviceType(type));
qDebug() << iconPath;
cmbDeviceType->addItem(QIcon(iconPath), DeviceType::getDeviceType_tr(type), type);
}
if(deviceMap.size() > 0)
{
int index = deviceMap.keys().at(0);
setCurrentDevice(deviceMap[index].at(0));
}
}
void BiometricDevicesWidget::setCurrentDevice(int drvid)
{
DeviceInfoPtr pDeviceInfo = findDeviceById(drvid);
if(pDeviceInfo)
{
setCurrentDevice(pDeviceInfo);
}
}
void BiometricDevicesWidget::setCurrentDevice(const QString &deviceName)
{
DeviceInfoPtr pDeviceInfo = findDeviceByName(deviceName);
if(pDeviceInfo)
{
setCurrentDevice(pDeviceInfo);
}
}
void BiometricDevicesWidget::setCurrentDevice(const DeviceInfoPtr &pDeviceInfo)
{
this->currentDevice = pDeviceInfo;
cmbDeviceType->setCurrentText(DeviceType::getDeviceType_tr(pDeviceInfo->deviceType));
cmbDeviceName->setCurrentText(pDeviceInfo->shortName);
}
bool BiometricDevicesWidget::deviceExists(int drvid)
{
return (findDeviceById(drvid) != nullptr);
}
bool BiometricDevicesWidget::deviceExists(const QString &deviceName)
{
return (findDeviceByName(deviceName) != nullptr);
}
DeviceInfoPtr BiometricDevicesWidget::findDeviceById(int drvid)
{
for(int type : deviceMap.keys())
{
DeviceList &deviceList = deviceMap[type];
auto iter = std::find_if(deviceList.begin(), deviceList.end(),
[&](DeviceInfoPtr ptr){
return ptr->id == drvid;
});
if(iter != deviceList.end())
{
return *iter;
}
}
return DeviceInfoPtr();
}
DeviceInfoPtr BiometricDevicesWidget::findDeviceByName(const QString &name)
{
for(int type : deviceMap.keys())
{
DeviceList &deviceList = deviceMap[type];
auto iter = std::find_if(deviceList.begin(), deviceList.end(),
[&](DeviceInfoPtr ptr){
return ptr->shortName == name;
});
if(iter != deviceList.end())
{
return *iter;
}
}
return DeviceInfoPtr();
}
void BiometricDevicesWidget::onCmbDeviceTypeCurrentIndexChanged(int index)
{
if(index < 0 || index >= deviceMap.keys().size())
{
return;
}
int type = cmbDeviceType->itemData(index).toInt();
cmbDeviceName->clear();
for(auto &deviceInfo : deviceMap.value(type))
{
cmbDeviceName->addItem(deviceInfo->shortName);
}
}
void BiometricDevicesWidget::onOKButtonClicked()
{
int type = cmbDeviceType->currentData().toInt();
int index = cmbDeviceName->currentIndex();
qDebug() << type << index;
DeviceInfoPtr deviceInfo = deviceMap.value(type).at(index);
Q_EMIT deviceChanged(deviceInfo);
hide();
}
void BiometricDevicesWidget::onUSBDeviceHotPlug(int drvid, int action, int /*devNum*/)
{
int savedDeviceId = currentDevice->id;
int savedCount = 0;
for(int type : deviceMap.keys())
savedCount += deviceMap.value(type).count();
switch(action)
{
case ACTION_ATTACHED:
{
//插入设备后,需要更新设备列表
deviceMap.clear();
updateDevice();
setCurrentDevice(savedDeviceId);
break;
}
case ACTION_DETACHED:
{
DeviceInfoPtr pDeviceInfo = findDeviceById(drvid);
if(pDeviceInfo)
{
int type = pDeviceInfo->deviceType;
deviceMap[type].removeOne(pDeviceInfo);
int index = cmbDeviceName->findText(pDeviceInfo->shortName);
cmbDeviceName->removeItem(index);
//如果该类型的设备全被移除,删除该类型相关的列表
if(deviceMap[type].isEmpty())
{
deviceMap.remove(type);
index = cmbDeviceType->findData(type);
cmbDeviceType->removeItem(index);
}
}
if(savedDeviceId != drvid)
{
setCurrentDevice(savedDeviceId);
}
break;
}
}
int count = 0;
for(int type : deviceMap.keys())
count += deviceMap.value(type).count();
//设备数量发生了变化
if(count != savedCount)
{
Q_EMIT deviceCountChanged(count);
}
}
|