Skip to content

Commit

Permalink
实现设备插拔监控【QtAdb 已正式可用,将开始功能添加与细节优化】
Browse files Browse the repository at this point in the history
  • Loading branch information
LapplandSP committed Aug 17, 2022
1 parent 33fd2a6 commit 169b175
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 36 deletions.
3 changes: 3 additions & 0 deletions QtAdb/QtAdb.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
QT += core gui
QT += serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11
Expand Down Expand Up @@ -38,6 +39,7 @@ SOURCES += \
textexplainer.cpp \
threads/adbthread.cpp \
threads/thread_createpage.cpp \
usb_listener.cpp \
welcomepage.cpp

HEADERS += \
Expand Down Expand Up @@ -70,6 +72,7 @@ HEADERS += \
textexplainer.h \
threads/adbthread.h \
threads/thread_createpage.h \
usb_listener.h \
welcomepage.h

FORMS += \
Expand Down
6 changes: 4 additions & 2 deletions QtAdb/about.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ about::about(QWidget *parent) :
basePage(parent),
ui(new Ui::about)
{
ui->setupUi(this);

self_castrate();
ui->setupUi(this);

QGraphicsDropShadowEffect *shadowEffect_btn_website = new QGraphicsDropShadowEffect(this);
shadowEffect_btn_website->setOffset(0,0);
Expand Down Expand Up @@ -58,6 +57,9 @@ about::about(QWidget *parent) :
ui->btn_money->hide();
ui->btn_money_3->hide();
ui->btn_money_4->hide();

this->setStyleSheet("QToolTip{border:1px solid #BDBDBD; background-color: #ffffff; color:rgba(117, 117, 117, 0.9);font-family: MiSans Medium; font-size:12px;border-radius:4px;}");

}

about::~about()
Expand Down
2 changes: 1 addition & 1 deletion QtAdb/about.ui
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ QPushButton:pressed{background-color:rgba(255,255,255,0.6);}</string>
</font>
</property>
<property name="text">
<string>测试版本:beta-v0.5</string>
<string>测试版本:beta-v0.6</string>
</property>
</widget>
</item>
Expand Down
12 changes: 6 additions & 6 deletions QtAdb/animationwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ void animationWidget::playLoadAnimation()
qDebug() << "wgtHeight = " << wgtHeight;
if(parent->width() <= 600)
{
animation->setEndValue(QRect(301, 117, 600,wgtHeight));
animation->setEndValue(QRect(301, 117 - 6, 600,wgtHeight));
}
else
{
animation->setEndValue(QRect(301, 117, parent->width(),wgtHeight));
animation->setEndValue(QRect(301, 117 - 6, parent->width(),wgtHeight));
}
//animation->setStartValue(QPoint(301 + 50,117));
animation->setStartValue(QRect(301 + 50, 117, parent->width()-50,wgtHeight));
animation->setStartValue(QRect(301 + 50, 117 - 6, parent->width()-50,wgtHeight));

animation->setEasingCurve(QEasingCurve::OutQuart);

Expand All @@ -38,14 +38,14 @@ void animationWidget::playLoadAnimation(int h)
//qDebug() << "wgtHeight = " << wgtHeight;
if(parent->width() <= 600)
{
animation->setEndValue(QRect(301, 117, 600,h));
animation->setEndValue(QRect(301, 117 - 6, 600,h));
}
else
{
animation->setEndValue(QRect(301, 117, parent->width(),h));
animation->setEndValue(QRect(301, 117 - 6, parent->width(),h));
}
//animation->setStartValue(QPoint(301 + 50,117));
animation->setStartValue(QRect(301 + 50, 117, parent->width()-50,h));
animation->setStartValue(QRect(301 + 50, 117 - 6, parent->width()-50,h));

animation->setEasingCurve(QEasingCurve::OutQuart);

Expand Down
182 changes: 178 additions & 4 deletions QtAdb/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,22 @@ MainWindow::MainWindow(QWidget *parent)
addIndexItems(); //设置左侧目录
setStyles(); //设置样式


/*初始化对象*/
process = new adbProcess();
explainer = new textExplainer();
maker = new pageMaker();
listener = new usb_listener();

//connect(listener, SIGNAL(DevicePlugIn()),this,SLOT(DevicePlugIn()));
//connect(listener, SIGNAL(DevicePlugOut()),this,SLOT(DevicePlugOut()));
connect(listener, SIGNAL(DeviceChanged()),this,SLOT(refreshDevListLater()));
connect(this, SIGNAL(adbDeviceChanged()),this,SLOT(DeviceChanged()));
qApp->installNativeEventFilter(listener);

//listener->EmitMySignal();
this->setAttribute(Qt::WA_NativeWindow);

/*启动监听*/

/*启动ADB,将延长页面创建时间,在此期间显示启动界面,显示了个勾八*/
process->run("adb server");
Expand Down Expand Up @@ -75,9 +86,97 @@ void MainWindow::initEnvironmentPATH() //方法:设置环境变

void MainWindow::refreshDevList() //方法:刷新设备列表
{
ui->comboBox->clear();
//ui->comboBox->clear();
/*DEBUG*/
bool changed = false;
qDebug() << "******************一次调用*********************";
qDebug() << "refreshDevList devList is empty? " << devList.isEmpty() << QTime::currentTime();
qDebug() << "devList.size()" << devList.size();

for(int i = 0; i < devList.size();i++)
{
qDebug() << "devList[" << i << "] is :" << devList[i].addr;
}

/*DEBUG_END*/
qDebug() << "shit 0";
bool devList_is_empty = devList.isEmpty();

QList<device> tmpList;

/*给tmpList赋值*/
if(!devList_is_empty)
{
for(int i = 0; i < devList.size();i++)
{
qDebug() << "shit 3.1";
device dev;
dev.device_debug = devList[i].device_debug;
dev.device_product = devList[i].device_product;
dev.addr = devList[i].addr;
dev.model = devList[i].model;
dev.state = devList[i].state;
dev.transport_id = devList[i].transport_id;
tmpList.append(dev);
}
}
qDebug() << "tmpList.size()" << tmpList.size();

//ui->comboBox->clear();
devList.clear(); //清空设备列表
devList = explainer->getDevList_windows(process->run("adb devices -l")); //重新赋值
qDebug() << "devList after explain:" << devList.isEmpty();

/*
qDebug() << "shit 1";
for(int i = 0; i < tmpList.size();i++)
{
qDebug() << "shit 2";
qDebug() << "tmpList[" << i << "] is :" << devList[i].addr;
}
for(int i = 0; i < devList.size();i++)
{
qDebug() << "shit 3";
qDebug() << "devList new[" << i << "] is :" << devList[i].addr;
}
qDebug() << "shit finish";*/


/*判断列表内容是否变化,若变化则发送 adbDeviceChanged 信号*/
if(!devList_is_empty)
{
if(devList.size() == tmpList.size())
{
for(int i = 0; i < devList.size() && i < tmpList.size() ; i++)
{
if(devList[i].addr != tmpList[i].addr)
{
emit adbDeviceChanged();
changed = true;
break;
}
}
}
else
{
emit adbDeviceChanged();
changed = true;
}
}
else if(!devList.isEmpty() && tmpList.isEmpty())
{
emit adbDeviceChanged();
changed = true;
}
else if(devList.isEmpty() && !tmpList.isEmpty())
{
emit adbDeviceChanged();
changed = true;
}
else
{
qDebug() << "else:" << devList.isEmpty() << tmpList.isEmpty() << devList_is_empty;
}

QList<int> off; //未响应设备索引

Expand All @@ -89,6 +188,7 @@ void MainWindow::refreshDevList() //方法:刷新设备列
{
QString devItem = devList[i].state + " " + explainer->get_words_after(devList[i].model, ":") + " " + devList[i].addr;
l.append(devItem);
qDebug() << "l[" << i << "] = " << l[i];
if(devList[i].state == "[未响应]")
{
off.append(i);
Expand All @@ -99,7 +199,21 @@ void MainWindow::refreshDevList() //方法:刷新设备列
}
}

ui->comboBox->addItems(l);
if(changed)
{
qDebug() <<"changed so clear";
ui->comboBox->clear();
ui->comboBox->addItems(l);
}

if(!liangYi)
{
qDebug() <<"liangYi so clear";
ui->comboBox->clear();
ui->comboBox->addItems(l);
}

liangYi = false;

for(int i = 0; i < off.count();i++)
{
Expand All @@ -115,6 +229,10 @@ void MainWindow::refreshDevList() //方法:刷新设备列
{
current_device = 0;
}

liangYi = false;

qDebug() << "******************一次调用结束*********************";
}

void MainWindow::on_refreshButton_clicked() //槽:按下刷新按钮
Expand All @@ -130,7 +248,6 @@ void MainWindow::on_refreshButton_clicked() //槽:按下刷新按钮
}*/

initBasePage(6);
qDebug() <<"8";
}

void MainWindow::setCurrentDevice(int index) //槽:改变所选设备
Expand Down Expand Up @@ -540,3 +657,60 @@ void MainWindow::on_cmdBtn_clicked()
batProcess.start(batPath);
batProcess.waitForFinished();
}

void MainWindow::DevicePlugIn()
{
qDebug() << "DevicePlugIn";
}

void MainWindow::DevicePlugOut()
{
qDebug() << "DevicePlugout";
}

void MainWindow::DeviceChanged()
{
if(firstBoot == true)
{
firstBoot = false;
}
else
{
QMessageBox::StandardButton result=QMessageBox::warning(NULL, "检测到USB热插拔","您可能插入或拔出了一台ADB设备\n"
"将对设备列表进行刷新\n"
"(⚠️这会中断当前正在执行的任务)");

//msgBox->setStyleSheet("background-color:rgba(255,255,255,1);border:0px; border-radius:0px;");
//QMessageBox::StandardButton result=QMessageBox::warning(this, "Title","text");
//qDebug() << "result =" << result;
if(result)
{
//这里要delete adbprocess
on_refreshButton_clicked();
}
else
{

}
//qDebug() << "DeviceChanged";
}
}

void MainWindow::slot_refreshDevList()
{
//qDebug() << "slot_refreshDevList";
liangYi = true;
refreshDevList();
}


void MainWindow::refreshDevListLater()
{

//qDebug() << ">>>>>>>>>>>>>>>>>>>>>>refreshDevListLater";
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(slot_refreshDevList()));
timer->setSingleShot(true);
timer->start(1000);
//timer->deleteLater();
}
26 changes: 23 additions & 3 deletions QtAdb/mainwindow.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
/*
#define 布尔 bool
#define 为 =
#define 真 true*/

#include <QMainWindow>
#include <QDir>
Expand All @@ -14,8 +18,8 @@
#include "indexlistitem.h"
#include "basepage.h"
#include "pagemaker.h"
#include "threads/thread_createpage.h"
#include "about.h"
#include "usb_listener.h"

#include <QCoreApplication>
#include <QStringList>
Expand All @@ -33,6 +37,7 @@ class MainWindow : public QMainWindow
{
Q_OBJECT
signals:
void adbDeviceChanged();

public:
MainWindow(QWidget *parent = nullptr);
Expand All @@ -49,7 +54,6 @@ class MainWindow : public QMainWindow
QLineEdit * le_port;
QLineEdit * le_code;


int current_device;
QList<device> devList;
indexListItem listItems[10];
Expand Down Expand Up @@ -81,12 +85,28 @@ private slots:
void addIndexItems();
void setStyles();

bool taiChi = true;
bool taiChi = true; //界面锁定定时器
bool liangYi = false; //判断刷新按钮槽函数是被谁调用:false:click()调用 ; true: 自行connect()
bool bagua = true; //忘了是干啥的了

//布尔 太极 为 真;


bool firstBoot = true;
QTimer *taiChiTimer;
QTimer *baguaTimer;

basePage * currentPage;
animationWidget * tmpPage;
about * WCMPage2;
usb_listener *listener;

public slots:
void DevicePlugIn();
void DevicePlugOut();
void DeviceChanged();
void slot_refreshDevList();
void refreshDevListLater();
};

#endif // MAINWINDOW_H
Loading

0 comments on commit 169b175

Please sign in to comment.