Saturday, November 22, 2014

Wondershaper - a GUI wrapper

I wrote this program just for fun!

Header:

AWS.h

#pragma once
typedef uint32_t t4u;
typedef char const*tSQ;

#include<QtWidgets/QComboBox>
#include<QtWidgets/QLabel>
#include<QtWidgets/QHBoxLayout>
#include<QtWidgets/QMainWindow>
#include<QtWidgets/QSlider>
#include<QtWidgets/QSpinBox>
#include<QtWidgets/QGroupBox>

class tSldr:public QGroupBox{Q_OBJECT
public:
tSldr(Qt::Orientation,QString const&,QHBoxLayout&,QWidget* =0);
int unsigned mNo;

signals:
void valueChanged(int);

public slots:
void setValue(int);
void setMinimum(int);
void setMaximum(int);
void invertAppearance(bool);
void invertKeyBindings(bool);
void onSliderValueChanged(int);

private:
QSpinBox*mSpnBx;
QSlider*mSldr;};
class tBxDrp:public QComboBox{Q_OBJECT
public:
tBxDrp(QHBoxLayout&,QLabel&,QWidget* =0);

signals:
void activated(int);
void currentIndexChanged(QString);};
class tMa:public QMainWindow{Q_OBJECT
public:
explicit tMa(QWidget* =0);

private slots:
void fApply();
void fClear();

private:
tSldr*mDn,*mUp;
QLabel*mLb,*mLbNt;
tBxDrp*mBxCb;};

AWS.cpp

#include<QtWidgets/QApplication>
#include<QtWidgets/QPushButton>
#include<unistd.h>
/*You need to run   moc AWS.h -o AWS.moc  .
Firstly, 'cd' (change directory) to your header file (named "AWS.h"), then run "moc" to generate AWS.moc.
For every change you do to the header file, you need to regenerate this file named "AWS.moc".*/
#include"AWS.moc"

tMa::tMa(QWidget*p):QMainWindow(p){
this->setWindowTitle("wondershaper - GUI wrapper");
this->setGeometry(QRect(100,100,640,100));

{QLabel*l=new QLabel("Down (KiB/s)",this);l->setFixedWidth(180),l->move(20,0),l->show();}
{QLabel*l=new QLabel("Up (KiB/s)",this);l->setFixedWidth(180),l->move(280,0),l->show();}
{QLabel*l=new QLabel("Copyleft R-Man 2014 - I am sorry that it is very simple ... / . \\",this);l->setFixedWidth(560),l->move(0,70),l->show();}
{mLb=new QLabel("Welcome to wondershaper wrapper!",this);mLb->setFixedWidth(320),mLb->move(0,55),mLb->show();}
{mLbNt=new QLabel("wlan1",this);}

QWidget*lWn=new QWidget(this);
lWn->setGeometry(QRect(0,0,640,100));
QHBoxLayout*lLy=new QHBoxLayout();

mDn=new tSldr(Qt::Horizontal,"Dn",*lLy);
mUp=new tSldr(Qt::Horizontal,"Up",*lLy);
mBxCb=new tBxDrp(*lLy,*mLbNt,this);
mBxCb->move(180,0),
mBxCb->addItem("eth0",0x00),mBxCb->addItem("eth1",0x01),
mBxCb->addItem("eth2",0x02),mBxCb->addItem("eth3",0x03),
mBxCb->addItem("wlan0",0x10),mBxCb->addItem("wlan1",0x11),
mBxCb->addItem("wlan2",0x12),mBxCb->addItem("wlan3",0x13),mBxCb->show();
lWn->setLayout(lLy);
lWn->show();

{QPushButton*l=new QPushButton("Clear",this);l->setGeometry(QRect(570,5,60,20)),
QObject::connect(l,SIGNAL(clicked()),this,SLOT(fClear())),l->show();}
{QPushButton*l=new QPushButton("Apply",this);l->setGeometry(QRect(570,75,60,20)),
QObject::connect(l,SIGNAL(clicked()),this,SLOT(fApply())),l->show();}}
static tSQ const lnS1[8]={
"eth0","eth1",
"eth2","eth3",
"wlan0","wlan1",
"wlan2","wlan3"};
static t4u const lnLh[8]={4,4,4,4,5,5,5,5};
void tMa::fClear(){
if(vfork()==0){
char l0[0x20]="wondershaper clear ";
int lIn=mBxCb->itemData(mBxCb->currentIndex()).toInt();
tSQ lTx=lnS1[(lIn&0x03)+(lIn&0x10?4:0)];
strcpy(l0+0x13,lTx);
mLb->setText(l0);
int l=execl("/bin/sh","sh","-c",l0,(char*)NULL);exit(l);}
/*else{Not sure why execution arrives here}*/}
void tMa::fApply(){
if(vfork()==0){
char l1[8],l2[8];t4u L1/*,L2*/;
sprintf(l1,"%d",mDn->mNo);L1=strlen(l1);
sprintf(l2,"%d",mUp->mNo);/*L2=strlen(l2);*/
char l0[48]="wondershaper ";
static t4u const lL=13;
int lIn=mBxCb->itemData(mBxCb->currentIndex()).toInt();
tSQ lTx=lnS1[(lIn&0x03)+(lIn&0x10?4:0)];
t4u L3=lnLh[(lIn&0x01)+(lIn&0x10?4:0)];
strcpy(l0+lL,lTx);
l0[lL+L3]=' ';
strcpy(l0+lL+L3+1,l1);
l0[lL+L3+1+L1]=' ';
strcpy(l0+lL+L3+1+L1+1,l2);
mLb->setText(l0);
int l=execl("/bin/sh","sh","-c",l0,(char*)NULL);exit(l);}
/*else{Not sure why execution arrives here}*/}

tBxDrp::tBxDrp(QHBoxLayout&pLy,QLabel&pLb,QWidget*parent):QComboBox(parent){
QObject::connect(this,SIGNAL(currentIndexChanged(QString)),&pLb,SLOT(setText(QString)));
pLy.addWidget(&pLb),
pLy.addWidget(this);}
tSldr::tSldr(Qt::Orientation,QString const&title,QHBoxLayout&pLy,QWidget*parent):QGroupBox(title, parent){
mNo=1280;
mSpnBx=new QSpinBox;
mSldr=new QSlider(Qt::Horizontal);
mSpnBx->setRange(0,10240);
mSldr->setRange(0,10240);

QObject::connect(mSpnBx,SIGNAL(valueChanged(int)),mSldr,SLOT(setValue(int)));
QObject::connect(mSldr,SIGNAL(valueChanged(int)),mSpnBx,SLOT(setValue(int)));
QObject::connect(mSldr,SIGNAL(valueChanged(int)),SLOT(onSliderValueChanged(int)));
mSpnBx->setValue(mNo);

pLy.addWidget(mSpnBx);
pLy.addWidget(mSldr);}
void tSldr::setValue(int p){mSpnBx->setValue(p),mSldr->setValue(p),mNo=p;}
void tSldr::setMinimum(int p){mSpnBx->setMinimum(p),mSldr->setMinimum(p);}
void tSldr::setMaximum(int p){mSpnBx->setMaximum(p),mSldr->setMaximum(p);}
void tSldr::invertAppearance(bool p){mSldr->setInvertedAppearance(p);}
void tSldr::invertKeyBindings(bool p){mSldr->setInvertedControls(p);}
void tSldr::onSliderValueChanged(int p){mNo=p;}

int main(int argc,char*argv[]){
QApplication lAp(argc,argv);
tMa l;l.show();
return lAp.exec();}
 

AWS.sh  (Extra)


#!/bin/bash
gksu ./AWS $* 


Build:


0. It runs only on Linux, obviously (because this app is merely a wrapper of another app, wondershaper).
1. You need to run  (without the quotes)  "  moc AWS.h -o AWS.moc  ", to generate an important C++ code.
2. Then build normally. Link with libQt5Core , libQt5Gui , and libQt5Widgets . (Use your IDE. Linking is trivial. If you encounter difficulties, please search around the web for tutorial)
3. You must run the application with elevated privilege. On Ubuntu, run the application with gksu or sudo . It may be a security risk, hence do not simply trust the program; Inspect the code.
4. I am sorry for the hard-coded network adapter names: eth0 eth1 eth2 eth3 wlan0 wlan1 wlan2 wlan3 . I am sorry, I don't know better ...   / . \  ...

Screenshots: