cleanup tici networking + remove pagination (#20528)

* remove wifi pagination

* cleanup

* little more cleanup

* fix tethering button

Co-authored-by: Comma Device <device@comma.ai>
pull/20463/head
Adeeb Shihadeh 2021-03-29 19:11:27 -07:00 committed by GitHub
parent ed5a6722af
commit bd12a787ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 110 deletions

View File

@ -2,14 +2,8 @@
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
#include <QPixmap> #include <QPixmap>
#include <QPushButton>
#include <QRandomGenerator>
#include <algorithm>
#include "common/params.h"
#include "hardware/hw.h"
#include "networking.hpp" #include "networking.hpp"
#include "util.h"
void clearLayout(QLayout* layout) { void clearLayout(QLayout* layout) {
while (QLayoutItem* item = layout->takeAt(0)) { while (QLayoutItem* item = layout->takeAt(0)) {
@ -23,12 +17,6 @@ void clearLayout(QLayout* layout) {
} }
} }
QWidget* layoutToWidget(QLayout* l, QWidget* parent){
QWidget* q = new QWidget(parent);
q->setLayout(l);
return q;
}
// Networking functions // Networking functions
Networking::Networking(QWidget* parent, bool show_advanced) : QWidget(parent), show_advanced(show_advanced){ Networking::Networking(QWidget* parent, bool show_advanced) : QWidget(parent), show_advanced(show_advanced){
@ -61,9 +49,9 @@ void Networking::attemptInitialization(){
if (show_advanced) { if (show_advanced) {
QPushButton* advancedSettings = new QPushButton("Advanced"); QPushButton* advancedSettings = new QPushButton("Advanced");
advancedSettings->setStyleSheet(R"(margin-right: 30px)"); advancedSettings->setStyleSheet("margin-right: 30px;");
advancedSettings->setFixedSize(350, 100); advancedSettings->setFixedSize(350, 100);
connect(advancedSettings, &QPushButton::released, [=](){s->setCurrentWidget(an);}); connect(advancedSettings, &QPushButton::released, [=](){ s->setCurrentWidget(an); });
vlayout->addSpacing(10); vlayout->addSpacing(10);
vlayout->addWidget(advancedSettings, 0, Qt::AlignRight); vlayout->addWidget(advancedSettings, 0, Qt::AlignRight);
vlayout->addSpacing(10); vlayout->addSpacing(10);
@ -73,7 +61,8 @@ void Networking::attemptInitialization(){
connect(wifiWidget, SIGNAL(connectToNetwork(Network)), this, SLOT(connectToNetwork(Network))); connect(wifiWidget, SIGNAL(connectToNetwork(Network)), this, SLOT(connectToNetwork(Network)));
vlayout->addWidget(wifiWidget, 1); vlayout->addWidget(wifiWidget, 1);
wifiScreen = layoutToWidget(vlayout, this); QWidget* wifiScreen = new QWidget(this);
wifiScreen->setLayout(vlayout);
s->addWidget(wifiScreen); s->addWidget(wifiScreen);
an = new AdvancedNetworking(this, wifi); an = new AdvancedNetworking(this, wifi);
@ -138,6 +127,7 @@ AdvancedNetworking::AdvancedNetworking(QWidget* parent, WifiManager* wifi): QWid
QVBoxLayout* vlayout = new QVBoxLayout; QVBoxLayout* vlayout = new QVBoxLayout;
vlayout->setMargin(40); vlayout->setMargin(40);
vlayout->setSpacing(20);
// Back button // Back button
QPushButton* back = new QPushButton("Back"); QPushButton* back = new QPushButton("Back");
@ -146,41 +136,24 @@ AdvancedNetworking::AdvancedNetworking(QWidget* parent, WifiManager* wifi): QWid
vlayout->addWidget(back, 0, Qt::AlignLeft); vlayout->addWidget(back, 0, Qt::AlignLeft);
// Enable tethering layout // Enable tethering layout
QHBoxLayout* tetheringToggleLayout = new QHBoxLayout; ToggleControl *tetheringToggle = new ToggleControl("Enable Tethering", "", "", wifi->tetheringEnabled());
tetheringToggleLayout->addWidget(new QLabel("Enable tethering")); vlayout->addWidget(tetheringToggle);
Toggle* toggle_switch = new Toggle; QObject::connect(tetheringToggle, SIGNAL(toggleFlipped(bool)), this, SLOT(toggleTethering(bool)));
toggle_switch->setFixedSize(150, 100);
tetheringToggleLayout->addWidget(toggle_switch);
tetheringToggleLayout->addSpacing(40);
if (wifi->tetheringEnabled()) {
toggle_switch->togglePosition();
}
QObject::connect(toggle_switch, SIGNAL(stateChanged(bool)), this, SLOT(toggleTethering(bool)));
vlayout->addWidget(layoutToWidget(tetheringToggleLayout, this), 0);
vlayout->addWidget(horizontal_line(), 0); vlayout->addWidget(horizontal_line(), 0);
// Change tethering password // Change tethering password
QHBoxLayout *tetheringPassword = new QHBoxLayout; editPasswordButton = new ButtonControl("Tethering Password", "EDIT", "", [=](){
tetheringPassword->addWidget(new QLabel("Edit tethering password"), 1);
editPasswordButton = new QPushButton("EDIT");
editPasswordButton->setFixedWidth(500);
connect(editPasswordButton, &QPushButton::released, [=](){
QString pass = InputDialog::getText("Enter new tethering password", 8); QString pass = InputDialog::getText("Enter new tethering password", 8);
if (pass.size()) { if (pass.size()) {
wifi->changeTetheringPassword(pass); wifi->changeTetheringPassword(pass);
} }
}); });
tetheringPassword->addWidget(editPasswordButton, 1, Qt::AlignRight); vlayout->addWidget(editPasswordButton, 0);
vlayout->addWidget(layoutToWidget(tetheringPassword, this), 0);
vlayout->addWidget(horizontal_line(), 0); vlayout->addWidget(horizontal_line(), 0);
// IP adress // IP address
QHBoxLayout* IPlayout = new QHBoxLayout; ipLabel = new LabelControl("IP Address", wifi->ipv4_address);
IPlayout->addWidget(new QLabel("IP address"), 0); vlayout->addWidget(ipLabel, 0);
ipLabel = new QLabel(wifi->ipv4_address);
ipLabel->setStyleSheet("color: #aaaaaa");
IPlayout->addWidget(ipLabel, 0, Qt::AlignRight);
vlayout->addWidget(layoutToWidget(IPlayout, this), 0);
vlayout->addWidget(horizontal_line(), 0); vlayout->addWidget(horizontal_line(), 0);
// SSH keys // SSH keys
@ -188,6 +161,7 @@ AdvancedNetworking::AdvancedNetworking(QWidget* parent, WifiManager* wifi): QWid
vlayout->addWidget(horizontal_line(), 0); vlayout->addWidget(horizontal_line(), 0);
vlayout->addWidget(new SshControl()); vlayout->addWidget(new SshControl());
vlayout->addStretch(1);
setLayout(vlayout); setLayout(vlayout);
} }
@ -218,7 +192,6 @@ WifiUI::WifiUI(QWidget *parent, WifiManager* wifi) : QWidget(parent), wifi(wifi)
vlayout->setSpacing(25); vlayout->setSpacing(25);
setLayout(vlayout); setLayout(vlayout);
page = 0;
} }
void WifiUI::refresh() { void WifiUI::refresh() {
@ -229,70 +202,40 @@ void WifiUI::refresh() {
connectButtons = new QButtonGroup(this); // TODO check if this is a leak connectButtons = new QButtonGroup(this); // TODO check if this is a leak
QObject::connect(connectButtons, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(handleButton(QAbstractButton*))); QObject::connect(connectButtons, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(handleButton(QAbstractButton*)));
int networks_per_page = height() / 180;
int i = 0; int i = 0;
int pageCount = (wifi->seen_networks.size() - 1) / networks_per_page;
page = std::max(0, std::min(page, pageCount));
for (Network &network : wifi->seen_networks) { for (Network &network : wifi->seen_networks) {
QHBoxLayout *hlayout = new QHBoxLayout; QHBoxLayout *hlayout = new QHBoxLayout;
if (page * networks_per_page <= i && i < (page + 1) * networks_per_page) { hlayout->addSpacing(50);
// SSID
hlayout->addSpacing(50);
QString ssid = QString::fromUtf8(network.ssid);
if(ssid.length() > 20){
ssid = ssid.left(20 - 3) + "";
}
QLabel *ssid_label = new QLabel(ssid); QLabel *ssid_label = new QLabel(QString::fromUtf8(network.ssid));
ssid_label->setStyleSheet(R"( ssid_label->setStyleSheet("font-size: 55px;");
font-size: 55px; hlayout->addWidget(ssid_label, 1, Qt::AlignLeft);
)");
ssid_label->setFixedWidth(this->width()*0.5);
hlayout->addWidget(ssid_label, 0, Qt::AlignLeft);
// TODO: don't use images for this // TODO: don't use images for this
// strength indicator // strength indicator
unsigned int strength_scale = network.strength / 17; unsigned int strength_scale = network.strength / 17;
QPixmap pix("../assets/images/network_" + QString::number(strength_scale) + ".png"); QPixmap pix("../assets/images/network_" + QString::number(strength_scale) + ".png");
QLabel *icon = new QLabel(); QLabel *icon = new QLabel();
icon->setPixmap(pix.scaledToWidth(100, Qt::SmoothTransformation)); icon->setPixmap(pix.scaledToWidth(100, Qt::SmoothTransformation));
icon->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); icon->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
hlayout->addWidget(icon, 0, Qt::AlignRight); hlayout->addWidget(icon, 0, Qt::AlignRight);
// connect button // connect button
QPushButton* btn = new QPushButton(network.security_type == SecurityType::UNSUPPORTED ? "Unsupported" : (network.connected == ConnectedType::CONNECTED ? "Connected" : (network.connected == ConnectedType::CONNECTING ? "Connecting" : "Connect"))); QPushButton* btn = new QPushButton(network.security_type == SecurityType::UNSUPPORTED ? "Unsupported" : (network.connected == ConnectedType::CONNECTED ? "Connected" : (network.connected == ConnectedType::CONNECTING ? "Connecting" : "Connect")));
btn->setDisabled(network.connected == ConnectedType::CONNECTED || network.connected == ConnectedType::CONNECTING || network.security_type == SecurityType::UNSUPPORTED); btn->setDisabled(network.connected == ConnectedType::CONNECTED || network.connected == ConnectedType::CONNECTING || network.security_type == SecurityType::UNSUPPORTED);
btn->setFixedWidth(350); btn->setFixedWidth(350);
hlayout->addWidget(btn, 0, Qt::AlignRight); hlayout->addWidget(btn, 0, Qt::AlignRight);
connectButtons->addButton(btn, i); connectButtons->addButton(btn, i);
vlayout->addLayout(hlayout, 1); vlayout->addLayout(hlayout, 1);
// Don't add the last horizontal line // Don't add the last horizontal line
if (page * networks_per_page <= i+1 && i+1 < (page + 1) * networks_per_page && i+1 < wifi->seen_networks.size()) { if (i+1 < wifi->seen_networks.size()) {
vlayout->addWidget(horizontal_line(), 0); vlayout->addWidget(horizontal_line(), 0);
}
} }
i++; i++;
} }
vlayout->addStretch(3); vlayout->addStretch(3);
// Setup buttons for pagination
QHBoxLayout *prev_next_buttons = new QHBoxLayout;
QPushButton* prev = new QPushButton("Previous");
prev->setEnabled(page);
QObject::connect(prev, SIGNAL(released()), this, SLOT(prevPage()));
prev_next_buttons->addWidget(prev);
QPushButton* next = new QPushButton("Next");
next->setEnabled(wifi->seen_networks.size() > (page + 1) * networks_per_page);
QObject::connect(next, SIGNAL(released()), this, SLOT(nextPage()));
prev_next_buttons->addWidget(next);
vlayout->addLayout(prev_next_buttons, 2);
} }
void WifiUI::handleButton(QAbstractButton* button) { void WifiUI::handleButton(QAbstractButton* button) {
@ -300,13 +243,3 @@ void WifiUI::handleButton(QAbstractButton* button) {
Network n = wifi->seen_networks[connectButtons->id(btn)]; Network n = wifi->seen_networks[connectButtons->id(btn)];
emit connectToNetwork(n); emit connectToNetwork(n);
} }
void WifiUI::prevPage() {
page--;
refresh();
}
void WifiUI::nextPage() {
page++;
refresh();
}

View File

@ -15,7 +15,6 @@ class WifiUI : public QWidget {
Q_OBJECT Q_OBJECT
public: public:
int page;
explicit WifiUI(QWidget *parent = 0, WifiManager* wifi = 0); explicit WifiUI(QWidget *parent = 0, WifiManager* wifi = 0);
private: private:
@ -29,10 +28,8 @@ signals:
void connectToNetwork(Network n); void connectToNetwork(Network n);
public slots: public slots:
void handleButton(QAbstractButton* m_button);
void refresh(); void refresh();
void prevPage(); void handleButton(QAbstractButton* m_button);
void nextPage();
}; };
class AdvancedNetworking : public QWidget { class AdvancedNetworking : public QWidget {
@ -41,8 +38,8 @@ public:
explicit AdvancedNetworking(QWidget* parent = 0, WifiManager* wifi = 0); explicit AdvancedNetworking(QWidget* parent = 0, WifiManager* wifi = 0);
private: private:
QLabel* ipLabel; LabelControl* ipLabel;
QPushButton* editPasswordButton; ButtonControl* editPasswordButton;
WifiManager* wifi = nullptr; WifiManager* wifi = nullptr;
signals: signals:
@ -60,7 +57,7 @@ public:
explicit Networking(QWidget* parent = 0, bool show_advanced = true); explicit Networking(QWidget* parent = 0, bool show_advanced = true);
private: private:
QStackedLayout* s = nullptr; // nm_warning, keyboard, wifiScreen, advanced QStackedLayout* s = nullptr; // nm_warning, wifiScreen, advanced
QWidget* wifiScreen = nullptr; QWidget* wifiScreen = nullptr;
AdvancedNetworking* an = nullptr; AdvancedNetworking* an = nullptr;
bool ui_setup_complete = false; bool ui_setup_complete = false;