# HG changeset patch # User Hasan Yavuz ÖZDERYA # Date 2016-09-02 02:19:42 # Node ID 2903e12a8854f96b929192a4410c0147ea68609f # Parent a6437a54c018608a276499204139877eec381aaa save/load selected port name diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -425,6 +425,9 @@ void MainWindow::saveSettings(QSettings* settings->setValue("size", size()); settings->setValue("pos", pos()); settings->endGroup(); + + // save port settings + portControl.saveSettings(settings); } void MainWindow::loadSettings(QSettings* settings) @@ -434,6 +437,9 @@ void MainWindow::loadSettings(QSettings* resize(settings->value("size", size()).toSize()); move(settings->value("pos", pos()).toPoint()); settings->endGroup(); + + // load port settings + portControl.loadSettings(settings); } void MainWindow::onSaveSettings() diff --git a/src/portcontrol.cpp b/src/portcontrol.cpp --- a/src/portcontrol.cpp +++ b/src/portcontrol.cpp @@ -1,5 +1,5 @@ /* - Copyright © 2015 Hasan Yavuz Özderya + Copyright © 2016 Hasan Yavuz Özderya This file is part of serialplot. @@ -273,6 +273,22 @@ void PortControl::selectPort(QString por } } +QString PortControl::selectedPortName() +{ + QString portText = ui->cbPortList->currentText(); + int portIndex = portList.indexOf(portText); + if (portIndex < 0) // not in the list yet + { + // return the displayed name as port name + return portText; + } + else + { + // get the port name from the 'port list' + return static_cast(portList.item(portIndex))->portName(); + } +} + QToolBar* PortControl::toolBar() { return &portToolBar; @@ -292,3 +308,25 @@ void PortControl::onTbPortListActivated( { ui->cbPortList->setCurrentIndex(index); } + +void PortControl::saveSettings(QSettings* settings) +{ + settings->beginGroup("Port"); + settings->setValue("selectedPort", selectedPortName()); + settings->endGroup(); +} + +void PortControl::loadSettings(QSettings* settings) +{ + settings->beginGroup("Port"); + + // set port name if it exists in the current list otherwise ignore + QString portName = settings->value("selectedPort", QString()).toString(); + if (!portName.isEmpty()) + { + int index = portList.indexOfName(portName); + if (index > -1) ui->cbPortList->setCurrentIndex(index); + } + + settings->endGroup(); +} diff --git a/src/portcontrol.h b/src/portcontrol.h --- a/src/portcontrol.h +++ b/src/portcontrol.h @@ -1,5 +1,5 @@ /* - Copyright © 2015 Hasan Yavuz Özderya + Copyright © 2016 Hasan Yavuz Özderya This file is part of serialplot. @@ -27,6 +27,7 @@ #include #include #include +#include #include "portlist.h" @@ -43,8 +44,12 @@ public: ~PortControl(); QSerialPort* serialPort; + QToolBar* toolBar(); - QToolBar* toolBar(); + /// Stores port settings into a `QSettings` + void saveSettings(QSettings* settings); + /// Loads port settings from a `QSettings` + void loadSettings(QSettings* settings); private: Ui::PortControl *ui; @@ -60,6 +65,9 @@ private: QComboBox tbPortList; PortList portList; + /// Returns the currently selected (entered) "portName" in the UI + QString selectedPortName(); + public slots: void loadPortList(); void loadBaudRateList(); diff --git a/src/portlist.cpp b/src/portlist.cpp --- a/src/portlist.cpp +++ b/src/portlist.cpp @@ -1,5 +1,5 @@ /* - Copyright © 2015 Hasan Yavuz Özderya + Copyright © 2016 Hasan Yavuz Özderya This file is part of serialplot. @@ -112,6 +112,18 @@ int PortList::indexOf(QString portText) return -1; // not found } +int PortList::indexOfName(QString portName) +{ + for (int i = 0; i < rowCount(); i++) + { + if (item(i)->data(PortNameRole) == portName) + { + return i; + } + } + return -1; // not found +} + void PortList::onRowsInserted(QModelIndex parent, int start, int end) { PortListItem* newItem = static_cast(item(start)); diff --git a/src/portlist.h b/src/portlist.h --- a/src/portlist.h +++ b/src/portlist.h @@ -1,5 +1,5 @@ /* - Copyright © 2015 Hasan Yavuz Özderya + Copyright © 2016 Hasan Yavuz Özderya This file is part of serialplot. @@ -50,7 +50,10 @@ public: PortList(QObject* parent=0); void loadPortList(); + /// Search for displayed text of the port int indexOf(QString portText); // return -1 if not found + /// Search for the actual port name + int indexOfName(QString portName); // return -1 if not found private: QStringList userEnteredPorts;