Device Emulation (contd.): Implemented new/edit/delete actions for device groups in the GUI

This commit is contained in:
Srivats P 2016-02-22 18:47:51 +05:30
parent c63528ebae
commit 6d9327c9d4
7 changed files with 187 additions and 10 deletions

View File

@ -165,6 +165,56 @@ bool DeviceGroupModel::setData(
return false; return false;
} }
bool DeviceGroupModel::insertRows(
int row,
int count,
const QModelIndex &parent)
{
int c = 0;
Q_ASSERT(!parent.isValid());
beginInsertRows(parent, row, row+count-1);
for (int i = 0; i < count; i++) {
if (port_->newDeviceGroupAt(row))
c++;
}
endInsertRows();
if (c != count) {
qWarning("failed to insert rows in DeviceGroupModel at row %d; "
"requested = %d, actual = %d", row, count, c);
return false;
}
return true;
}
bool DeviceGroupModel::removeRows(
int row,
int count,
const QModelIndex &parent)
{
int c = 0;
Q_ASSERT(!parent.isValid());
beginRemoveRows(parent, row, row+count-1);
for (int i = 0; i < count; i++) {
if (port_->deleteDeviceGroupAt(row))
c++;
}
endRemoveRows();
if (c != count) {
qWarning("failed to delete rows in DeviceGroupModel at row %d; "
"requested = %d, actual = %d", row, count, c);
return false;
}
return true;
}
void DeviceGroupModel::setPort(Port *port) void DeviceGroupModel::setPort(Port *port)
{ {
port_ = port; port_ = port;

View File

@ -42,6 +42,10 @@ public:
QVariant data(const QModelIndex &index, int role) const; QVariant data(const QModelIndex &index, int role) const;
bool setData(const QModelIndex &index, const QVariant &value, bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole); int role = Qt::EditRole);
bool insertRows (int row, int count,
const QModelIndex &parent = QModelIndex());
bool removeRows (int row, int count,
const QModelIndex &parent = QModelIndex());
void setPort(Port *port); void setPort(Port *port);

View File

@ -31,6 +31,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
extern QMainWindow *mainWindow; extern QMainWindow *mainWindow;
uint Port::mAllocStreamId = 0; uint Port::mAllocStreamId = 0;
uint Port::allocDeviceGroupId_ = 1;
static const int kEthOverhead = 20; static const int kEthOverhead = 20;
@ -608,6 +609,11 @@ _exit:
// ------------ Device Group ----------- // // ------------ Device Group ----------- //
uint Port::newDeviceGroupId()
{
return allocDeviceGroupId_++;
}
int Port::numDeviceGroups() int Port::numDeviceGroups()
{ {
return deviceGroups_.size(); return deviceGroups_.size();
@ -615,22 +621,62 @@ int Port::numDeviceGroups()
OstProto::DeviceGroup* Port::deviceGroupByIndex(int index) OstProto::DeviceGroup* Port::deviceGroupByIndex(int index)
{ {
// FIXME: do we need to index? can't we use an iterator instead? if ((index < 0) || (index >= numDeviceGroups())) {
if ((index < 0) || (index >= numDeviceGroups())) {
qWarning("%s: index %d out of range (0 - %d)", __FUNCTION__, qWarning("%s: index %d out of range (0 - %d)", __FUNCTION__,
index, numDeviceGroups() - 1); index, numDeviceGroups() - 1);
return NULL; return NULL;
} }
// Sort List by 'id', get the id at 'index' and then corresponding devGrp return deviceGroups_.at(index);
return deviceGroups_.value(deviceGroups_.uniqueKeys().value(index)); }
OstProto::DeviceGroup* Port::deviceGroupById(uint deviceGroupId)
{
for (int i = 0; i < deviceGroups_.size(); i++) {
OstProto::DeviceGroup *devGrp = deviceGroups_.at(i);
if (devGrp->device_group_id().id() == deviceGroupId)
return devGrp;
}
return NULL;
}
bool Port::newDeviceGroupAt(int index, const OstProto::DeviceGroup *deviceGroup)
{
if (index < 0 || index > numDeviceGroups())
return false;
OstProto::DeviceGroup *devGrp = new OstProto::DeviceGroup;
if (!devGrp) {
qWarning("failed allocating a new device group");
return false;
}
if (deviceGroup)
devGrp->CopyFrom(*deviceGroup);
devGrp->mutable_device_group_id()->set_id(newDeviceGroupId());
deviceGroups_.insert(index, devGrp);
return true;
}
bool Port::deleteDeviceGroupAt(int index)
{
if (index < 0 || index >= deviceGroups_.size())
return false;
delete deviceGroups_.takeAt(index);
return true;
} }
bool Port::insertDeviceGroup(uint deviceGroupId) bool Port::insertDeviceGroup(uint deviceGroupId)
{ {
OstProto::DeviceGroup *devGrp; OstProto::DeviceGroup *devGrp;
if (deviceGroups_.contains(deviceGroupId)) { if (deviceGroupById(deviceGroupId)) {
qDebug("%s: deviceGroup id %u already exists", __FUNCTION__, qDebug("%s: deviceGroup id %u already exists", __FUNCTION__,
deviceGroupId); deviceGroupId);
return false; return false;
@ -638,7 +684,7 @@ bool Port::insertDeviceGroup(uint deviceGroupId)
devGrp = new OstProto::DeviceGroup; devGrp = new OstProto::DeviceGroup;
devGrp->mutable_device_group_id()->set_id(deviceGroupId); devGrp->mutable_device_group_id()->set_id(deviceGroupId);
deviceGroups_.insert(deviceGroupId, devGrp); deviceGroups_.append(devGrp);
return true; return true;
} }
@ -646,7 +692,7 @@ bool Port::updateDeviceGroup(
uint deviceGroupId, uint deviceGroupId,
OstProto::DeviceGroup *deviceGroup) OstProto::DeviceGroup *deviceGroup)
{ {
OstProto::DeviceGroup *devGrp = deviceGroups_.value(deviceGroupId); OstProto::DeviceGroup *devGrp = deviceGroupById(deviceGroupId);
if (!devGrp) { if (!devGrp) {
qDebug("%s: deviceGroup id %u does not exist", __FUNCTION__, qDebug("%s: deviceGroup id %u does not exist", __FUNCTION__,

View File

@ -21,7 +21,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#define _PORT_H #define _PORT_H
#include <QDir> #include <QDir>
#include <QHash>
#include <QList> #include <QList>
#include <QString> #include <QString>
#include <QTemporaryFile> #include <QTemporaryFile>
@ -35,6 +34,7 @@ class Port : public QObject {
Q_OBJECT Q_OBJECT
static uint mAllocStreamId; static uint mAllocStreamId;
static uint allocDeviceGroupId_;
OstProto::Port d; OstProto::Port d;
OstProto::PortStats stats; OstProto::PortStats stats;
@ -52,7 +52,7 @@ class Port : public QObject {
QList<quint32> mLastSyncStreamList; QList<quint32> mLastSyncStreamList;
QList<Stream*> mStreams; // sorted by stream's ordinal value QList<Stream*> mStreams; // sorted by stream's ordinal value
QHash<uint, OstProto::DeviceGroup*> deviceGroups_; QList<OstProto::DeviceGroup*> deviceGroups_;
uint newStreamId(); uint newStreamId();
void updateStreamOrdinalsFromIndex(); void updateStreamOrdinalsFromIndex();
@ -150,8 +150,17 @@ public:
// ------------ Device Group ----------- // // ------------ Device Group ----------- //
uint newDeviceGroupId();
int numDeviceGroups(); int numDeviceGroups();
OstProto::DeviceGroup* deviceGroupByIndex(int index); OstProto::DeviceGroup* deviceGroupByIndex(int index);
OstProto::DeviceGroup* deviceGroupById(uint deviceGroupId);
//! Used by StreamModel
//@{
bool newDeviceGroupAt(int index,
const OstProto::DeviceGroup *deviceGroup = NULL);
bool deleteDeviceGroupAt(int index);
//@}
//! Used by MyService::Stub to update from config received from server //! Used by MyService::Stub to update from config received from server
//@{ //@{

View File

@ -77,12 +77,22 @@ PortsWindow::PortsWindow(PortGroupList *pgl, QWidget *parent)
tvStreamList->addAction(actionOpen_Streams); tvStreamList->addAction(actionOpen_Streams);
tvStreamList->addAction(actionSave_Streams); tvStreamList->addAction(actionSave_Streams);
// PortList and StreamList actions combined make this window's actions // Populate DeviceGroup Context Menu Actions
deviceGroupList->addAction(actionNewDeviceGroup);
deviceGroupList->addAction(actionEditDeviceGroup);
deviceGroupList->addAction(actionDeleteDeviceGroup);
// PortList, StreamList, DeviceGroupList actions combined
// make this window's actions
addActions(tvPortList->actions()); addActions(tvPortList->actions());
sep = new QAction(this); sep = new QAction(this);
sep->setSeparator(true); sep->setSeparator(true);
addAction(sep); addAction(sep);
addActions(tvStreamList->actions()); addActions(tvStreamList->actions());
sep = new QAction(this);
sep->setSeparator(true);
addAction(sep);
addActions(deviceGroupList->actions());
tvStreamList->setModel(plm->getStreamModel()); tvStreamList->setModel(plm->getStreamModel());
deviceGroupList->setModel(plm->getDeviceGroupModel()); deviceGroupList->setModel(plm->getDeviceGroupModel());
@ -818,6 +828,46 @@ _exit:
// //
// DeviceGroup slots // DeviceGroup slots
// //
void PortsWindow::on_actionNewDeviceGroup_triggered()
{
// In case nothing is selected, insert 1 row at the top
int row = 0, count = 1;
QItemSelection selection = deviceGroupList->selectionModel()->selection();
// In case we have a single range selected; insert as many rows as
// in the singe selected range before the top of the selected range
if (selection.size() == 1)
{
row = selection.at(0).top();
count = selection.at(0).height();
}
plm->getDeviceGroupModel()->insertRows(row, count);
}
void PortsWindow::on_actionDeleteDeviceGroup_triggered()
{
QModelIndex index;
if (deviceGroupList->selectionModel()->hasSelection())
{
while(deviceGroupList->selectionModel()->selectedRows().size())
{
index = deviceGroupList->selectionModel()->selectedRows().at(0);
plm->getDeviceGroupModel()->removeRows(index.row(), 1);
}
}
}
void PortsWindow::on_actionEditDeviceGroup_triggered()
{
QItemSelection selection = deviceGroupList->selectionModel()->selection();
// Ensure we have only one range selected which contains only one row
if ((selection.size() == 1) && (selection.at(0).height() == 1))
on_deviceGroupList_activated(selection.at(0).topLeft());
}
void PortsWindow::on_deviceGroupList_activated(const QModelIndex &index) void PortsWindow::on_deviceGroupList_activated(const QModelIndex &index)
{ {
if (!index.isValid()) if (!index.isValid())

View File

@ -81,6 +81,9 @@ private slots:
void streamModelDataChanged(); void streamModelDataChanged();
void on_actionNewDeviceGroup_triggered();
void on_actionDeleteDeviceGroup_triggered();
void on_actionEditDeviceGroup_triggered();
void on_deviceGroupList_activated(const QModelIndex &index); void on_deviceGroupList_activated(const QModelIndex &index);
}; };

View File

@ -312,6 +312,21 @@
<string>Duplicate Stream</string> <string>Duplicate Stream</string>
</property> </property>
</action> </action>
<action name="actionNewDeviceGroup" >
<property name="text" >
<string>New Device Group</string>
</property>
</action>
<action name="actionDeleteDeviceGroup" >
<property name="text" >
<string>Delete Device Group</string>
</property>
</action>
<action name="actionEditDeviceGroup" >
<property name="text" >
<string>Edit Device Group</string>
</property>
</action>
</widget> </widget>
<resources> <resources>
<include location="ostinato.qrc" /> <include location="ostinato.qrc" />