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
 
@@ -64,22 +64,33 @@ QString PortListItem::portName()
 
}
 

	
 
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)
 
{
 
    for (int i = 0; i < rowCount(); i++)
 
    {
 
@@ -87,6 +98,14 @@ int PortList::indexOf(QString portName)
 
        {
 
            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
 
@@ -14,20 +14,23 @@
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef PORTLIST_H
 
#define PORTLIST_H
 

	
 
#include <QStandardItemModel>
 
#include <QStandardItem>
 
#include <QList>
 
#include <QSerialPortInfo>
 

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

	
 
class PortListItem : public QStandardItem
 
{
 
public:
 
    PortListItem(QString name, QString description="", quint16 vid=0, quint16 pid=0);
 
@@ -39,15 +42,21 @@ 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
 

	
 
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)