Changeset - cb8b69a0516c
[Not reviewed]
portlist
0 2 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-09-04 18:00:21
hy@ozderya.net
remember user entered port names on reload
2 files changed with 30 insertions and 2 deletions:
0 comments (0 inline, 0 general)
portlist.cpp
Show inline comments
 
@@ -67,16 +67,27 @@ PortList::PortList(QObject* parent) :
 
    QStandardItemModel(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)
 
@@ -90,3 +101,11 @@ int PortList::indexOf(QString portName)
 
    }
 
    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
 
@@ -17,6 +17,9 @@
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef PORTLIST_H
 
#define PORTLIST_H
 

	
 
#include <QStandardItemModel>
 
#include <QStandardItem>
 
#include <QList>
 
@@ -24,7 +27,7 @@
 

	
 
enum PortListRoles
 
{
 
    PortNameRole = Qt::UserRole  // portName as QString
 
    PortNameRole = Qt::UserRole+1  // portName as QString
 
};
 

	
 
class PortListItem : public QStandardItem
 
@@ -42,6 +45,7 @@ private:
 

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

	
 
@@ -49,5 +53,10 @@ public:
 
    int indexOf(QString portName); // return -1 if not found
 

	
 
private:
 
    QList<PortListItem*> userEnteredPorts;
 
    QStringList userEnteredPorts;
 

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

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