Changeset - 420befa2304c
[Not reviewed]
default
0 2 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-05-26 06:57:24
hy@ozderya.net
keep user entered port names in the port list
2 files changed with 25 insertions and 0 deletions:
0 comments (0 inline, 0 general)
portcontrol.cpp
Show inline comments
 
@@ -39,12 +39,16 @@ PortControl::PortControl(QSerialPort* po
 
                     this, &PortControl::togglePort);
 

	
 
    QObject::connect(ui->cbPortList,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &PortControl::selectPort);
 

	
 
    QObject::connect(ui->cbPortList,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &PortControl::onPortNameChanged);
 

	
 
    QObject::connect(ui->cbBaudRate,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &PortControl::selectBaudRate);
 

	
 
    // setup parity selection buttons
 
    parityButtons.addButton(ui->rbNoParity, (int) QSerialPort::NoParity);
 
@@ -102,17 +106,21 @@ PortControl::~PortControl()
 
void PortControl::loadPortList()
 
{
 
    QString currentSelection = ui->cbPortList->currentText();
 

	
 
    ui->cbPortList->clear();
 

	
 
    discoveredPorts.clear();
 
    for (auto port : QSerialPortInfo::availablePorts())
 
    {
 
        ui->cbPortList->addItem(port.portName());
 
        discoveredPorts << port.portName();
 
    }
 

	
 
    ui->cbPortList->addItems(userEnteredPorts);
 

	
 
    // find current selection in the new list, maybe it doesn't exist anymore?
 
    int currentSelectionIndex = ui->cbPortList->findText(currentSelection);
 
    if (currentSelectionIndex >= 0)
 
    {
 
        ui->cbPortList->setCurrentIndex(currentSelectionIndex);
 
    }
 
@@ -242,6 +250,16 @@ void PortControl::selectPort(QString por
 
}
 

	
 
void PortControl::enableSkipByte(bool enabled)
 
{
 
    ui->pbSkipByte->setDisabled(enabled);
 
}
 

	
 
void PortControl::onPortNameChanged(QString portName)
 
{
 
    // was this a user entered name?
 
    if(!discoveredPorts.contains(portName) &&
 
       !userEnteredPorts.contains(portName))
 
    {
 
        userEnteredPorts << portName;
 
    }
 
}
portcontrol.h
Show inline comments
 
@@ -20,12 +20,13 @@
 
#ifndef PORTCONTROL_H
 
#define PORTCONTROL_H
 

	
 
#include <QWidget>
 
#include <QButtonGroup>
 
#include <QSerialPort>
 
#include <QStringList>
 

	
 
namespace Ui {
 
class PortControl;
 
}
 

	
 
class PortControl : public QWidget
 
@@ -43,12 +44,15 @@ private:
 

	
 
    QButtonGroup parityButtons;
 
    QButtonGroup dataBitsButtons;
 
    QButtonGroup stopBitsButtons;
 
    QButtonGroup flowControlButtons;
 

	
 
    QStringList discoveredPorts; // list of port names returned by availablePorts
 
    QStringList userEnteredPorts; // list of port names entered by user
 

	
 
public slots:
 
    void loadPortList();
 
    void loadBaudRateList();
 
    void togglePort();
 
    void selectPort(QString portName);
 
    void enableSkipByte(bool enabled = true);
 
@@ -56,12 +60,15 @@ public slots:
 
    void selectBaudRate(QString baudRate);
 
    void selectParity(int parity); // parity must be one of QSerialPort::Parity
 
    void selectDataBits(int dataBits); // bits must be one of QSerialPort::DataBits
 
    void selectStopBits(int stopBits); // stopBits must be one of QSerialPort::StopBits
 
    void selectFlowControl(int flowControl); // flowControl must be one of QSerialPort::FlowControl
 

	
 
private slots:
 
    void onPortNameChanged(QString portName);
 

	
 
signals:
 
    void skipByteRequested();
 
    void portToggled(bool open);
 
};
 

	
 
#endif // PORTCONTROL_H
0 comments (0 inline, 0 general)