Changeset - cf2136c2b501
[Not reviewed]
portlist
0 3 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-09-05 01:35:49
hy@ozderya.net
use full port text (not just name) for selections
3 files changed with 14 insertions and 6 deletions:
0 comments (0 inline, 0 general)
portcontrol.cpp
Show inline comments
 
@@ -199,55 +199,63 @@ void PortControl::selectStopBits(int sto
 

	
 
void PortControl::selectFlowControl(int flowControl)
 
{
 
    if (serialPort->isOpen())
 
    {
 
        if(!serialPort->setFlowControl((QSerialPort::FlowControl) flowControl))
 
        {
 
            qCritical() << "Can't set flow control option!";
 
        }
 
    }
 
}
 

	
 
void PortControl::togglePort()
 
{
 
    if (serialPort->isOpen())
 
    {
 
        serialPort->close();
 
        qDebug() << "Closed port:" << serialPort->portName();
 
        emit portToggled(false);
 
    }
 
    else
 
    {
 
        // we get the port name from the edit text, which may not be
 
        // in the portList if user hasn't pressed Enter
 
        QString portName = ui->cbPortList->currentText();
 
        int portIndex = portList.indexOf(portName);
 
        // Also note that, portText may not be the `portName`
 
        QString portText = ui->cbPortList->currentText();
 
        QString portName;
 
        int portIndex = portList.indexOf(portText);
 
        if (portIndex < 0) // not in list, add to model and update the selections
 
        {
 
            portList.appendRow(new PortListItem(portName));
 
            portList.appendRow(new PortListItem(portText));
 
            ui->cbPortList->setCurrentIndex(portList.rowCount()-1);
 
            tbPortList.setCurrentIndex(portList.rowCount()-1);
 
            portName = portText;
 
        }
 
        else
 
        {
 
            // get the port name from the data field
 
            portName = static_cast<PortListItem*>(portList.item(portIndex))->portName();
 
        }
 

	
 
        serialPort->setPortName(ui->cbPortList->currentData(PortNameRole).toString());
 

	
 
        // open port
 
        if (serialPort->open(QIODevice::ReadWrite))
 
        {
 
            // set port settings
 
            selectBaudRate(ui->cbBaudRate->currentText());
 
            selectParity((QSerialPort::Parity) parityButtons.checkedId());
 
            selectDataBits((QSerialPort::DataBits) dataBitsButtons.checkedId());
 
            selectStopBits((QSerialPort::StopBits) stopBitsButtons.checkedId());
 
            selectFlowControl((QSerialPort::FlowControl) flowControlButtons.checkedId());
 

	
 
            qDebug() << "Opened port:" << serialPort->portName();
 
            emit portToggled(true);
 
        }
 
    }
 
    openAction.setChecked(serialPort->isOpen());
 
}
 

	
 
void PortControl::selectPort(QString portName)
 
{
 
    // portName may be coming from combobox
portlist.cpp
Show inline comments
 
@@ -69,43 +69,43 @@ PortList::PortList(QObject* parent) :
 
    loadPortList();
 

	
 
    // we have to use macro based notation to be able to disconnect
 
    QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
 
                     this, SLOT(onRowsInserted(QModelIndex, int, int)));
 
}
 

	
 
void PortList::loadPortList()
 
{
 
    clear();
 

	
 
    disconnect(this, SLOT(onRowsInserted(QModelIndex,int,int)));
 
    for (auto portInfo : QSerialPortInfo::availablePorts())
 
    {
 
        appendRow(new PortListItem(&portInfo));
 
    }
 
    for (auto portName : userEnteredPorts)
 
    {
 
        appendRow(new PortListItem(portName));
 
    }
 
    QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
 
                     this, SLOT(onRowsInserted(QModelIndex, int, int)));
 
}
 

	
 
int PortList::indexOf(QString portName)
 
int PortList::indexOf(QString portText)
 
{
 
    for (int i = 0; i < rowCount(); i++)
 
    {
 
        if (static_cast<PortListItem*>(item(i))->portName() == portName)
 
        if (item(i)->text() == portText)
 
        {
 
            return i;
 
        }
 
    }
 
    return -1; // not found
 
}
 

	
 
void PortList::onRowsInserted(QModelIndex parent, int start, int end)
 
{
 
    PortListItem* newItem = static_cast<PortListItem*>(item(start));
 
    QString portName = newItem->text();
 
    newItem->setData(portName, PortNameRole);
 
    userEnteredPorts << portName;
 
}
portlist.h
Show inline comments
 
@@ -29,34 +29,34 @@ enum PortListRoles
 
{
 
    PortNameRole = Qt::UserRole+1  // portName as QString
 
};
 

	
 
class PortListItem : public QStandardItem
 
{
 
public:
 
    PortListItem(QString name, QString description="", quint16 vid=0, quint16 pid=0);
 
    PortListItem(QSerialPortInfo* portInfo);
 

	
 
    QString portName(); // returns only the port name
 

	
 
private:
 
    // common constructor
 
    void construct(QString name, QString description="", quint16 vid=0, quint16 pid=0);
 
};
 

	
 
class PortList : public QStandardItemModel
 
{
 
    Q_OBJECT
 
public:
 
    PortList(QObject* parent=0);
 

	
 
    void loadPortList();
 
    int indexOf(QString portName); // return -1 if not found
 
    int indexOf(QString portText); // return -1 if not found
 

	
 
private:
 
    QStringList userEnteredPorts;
 

	
 
private slots:
 
    void onRowsInserted(QModelIndex parent, int start, int end);
 
};
 

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