Changeset - 40acd76815dc
[Not reviewed]
portlist
0 3 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-09-03 15:20:27
hy@ozderya.net
add user entered port to list when opening port (if not already in the list)
3 files changed with 25 insertions and 5 deletions:
0 comments (0 inline, 0 general)
portcontrol.cpp
Show inline comments
 
@@ -208,15 +208,22 @@ void PortControl::togglePort()
 
        serialPort->close();
 
        qDebug() << "Closed port:" << serialPort->portName();
 
        emit portToggled(false);
 
    }
 
    else
 
    {
 
        // port name may contain description
 
        QString portName = ui->cbPortList->currentText().split(" ")[0];
 
        keepPortName(portName);
 
        // 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);
 
        if (portIndex < 0) // not in list, add to model and update the selections
 
        {
 
            portList.appendRow(new PortListItem(portName));
 
            ui->cbPortList->setCurrentIndex(portList.rowCount()-1);
 
            tbPortList.setCurrentIndex(portList.rowCount()-1);
 
        }
 

	
 
        serialPort->setPortName(portName);
 

	
 
        // open port
 
        if (serialPort->open(QIODevice::ReadWrite))
 
        {
portlist.cpp
Show inline comments
 
@@ -55,13 +55,13 @@ void PortListItem::construct(QString nam
 
        text += QString("%1]").arg(pid, 4, 16, QChar('0'));
 
    }
 
    setText(text);
 
    setData(name, PortNameRole);
 
}
 

	
 
QString PortListItem::name()
 
QString PortListItem::portName()
 
{
 
    return data(PortNameRole).toString();
 
}
 

	
 
PortList::PortList(QObject* parent) :
 
    QStandardItemModel(parent)
 
@@ -75,6 +75,18 @@ void PortList::loadPortList()
 

	
 
    for (auto portInfo : QSerialPortInfo::availablePorts())
 
    {
 
        appendRow(new PortListItem(&portInfo));
 
    }
 
}
 

	
 
int PortList::indexOf(QString portName)
 
{
 
    for (int i = 0; i < rowCount(); i++)
 
    {
 
        if (static_cast<PortListItem*>(item(i))->portName() == portName)
 
        {
 
            return i;
 
        }
 
    }
 
    return -1; // not found
 
}
portlist.h
Show inline comments
 
@@ -30,23 +30,24 @@ enum PortListRoles
 
class PortListItem : public QStandardItem
 
{
 
public:
 
    PortListItem(QString name, QString description="", quint16 vid=0, quint16 pid=0);
 
    PortListItem(QSerialPortInfo* portInfo);
 

	
 
    QString name(); // returns only the port name
 
    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
 
{
 
public:
 
    PortList(QObject* parent=0);
 

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

	
 
private:
 
    QList<PortListItem*> userEnteredPorts;
 
};
0 comments (0 inline, 0 general)