Thank you for the response and generous help!
I implemented it and it does change the color for a specific QGraphicsRectItem with its connection status(thank you), however when it updates the status correctly it also add new QGraphicsRectItems(see attachment),a repeat of QGraphicsRectItems that was on the scene but with an updated status. I called the addRect function on the update statement like this:
void GatewayWidget
::receivedTitlesEx(const QString &_rStrDescription,
const CORE
::Time &_rSimTime,
const QList<std
::shared_ptr<DM
::Object> >
&_rTitles,
int _eType
) {
if (_rStrDescription == "LINK.*.STATE")
{
foreach (const std::shared_ptr<DM::Object> &objPtr, _rTitles)
{
if (CORE::checkPtrType<C2DM::LinkState>(objPtr) == true)
{
if (_eType == QSubscription::NEW_TITLES)
{
C2DM::LinkState *pState = (C2DM::LinkState*)objPtr->_clone();
addRect(pState);// this is the first call of the method that adds the QGraphicsRectItem(nodes) on the scene
}
else if (_eType == QSubscription::UPDATED_TITLES)
{
C2DM::LinkState *pState = (C2DM::LinkState*)objPtr->_clone();
addRect(pState);//on updated status i call the addRect() again. When it updates the link status and change the color, it also adds the new QGraphicsRectItem when it is was already added on the first call of addRect() above.
}
void GatewayWidget::receivedTitlesEx(const QString &_rStrDescription, const CORE::Time &_rSimTime, const QList<std::shared_ptr<DM::Object> > &_rTitles, int _eType)
{
if (_rStrDescription == "LINK.*.STATE")
{
foreach (const std::shared_ptr<DM::Object> &objPtr, _rTitles)
{
if (CORE::checkPtrType<C2DM::LinkState>(objPtr) == true)
{
if (_eType == QSubscription::NEW_TITLES)
{
C2DM::LinkState *pState = (C2DM::LinkState*)objPtr->_clone();
addRect(pState);// this is the first call of the method that adds the QGraphicsRectItem(nodes) on the scene
}
else if (_eType == QSubscription::UPDATED_TITLES)
{
C2DM::LinkState *pState = (C2DM::LinkState*)objPtr->_clone();
addRect(pState);//on updated status i call the addRect() again. When it updates the link status and change the color, it also adds the new QGraphicsRectItem when it is was already added on the first call of addRect() above.
}
To copy to clipboard, switch view to plain text mode
my understanding was that I am supposed to call addRect() again on the UPDATED_TITLES as seen on the else if clause above, am I correct?
I think that on the addRect() function, I always add QGraphicsRectItems on the scene through this lines:
rectangleColor->setData(0, int(pState->m_eState));
QGraphicsTextItem *textColor = scene->addText(QString::fromStdString(pState->m_strLinkId));
QGraphicsRectItem *rectangleColor = new QGraphicsRectItem(QRectF(0, 0, 50, 50), textColor);
rectangleColor->setData(0, int(pState->m_eState));
To copy to clipboard, switch view to plain text mode
then whenever I call addRect() it creates another QGraphicsRectItem with the updated status. my question is am I supposed to write another statement on addRect() that will check if the QGraphicsRectItems are already added on the scene, and if they are, then just change color only and not add a new QGraphicsRectItem?
on your statement"You will need to add code to change the value stored in the QGraphicsRectItem whenever its LinkState changes." do you mean I should change the value int Rectstate when _eType changes here :
}
else if (_eType == QSubscription::UPDATED_TITLES) //this is where _eType changes(which is the LinkState),is this where I am supposed to write the code? how do I access the value [I]int rectState[/I] on this function?
{
C2DM::LinkState *pState = (C2DM::LinkState*)objPtr->_clone();
addRect(pState);
}
else if (_eType == QSubscription::UPDATED_TITLES) //this is where _eType changes(which is the LinkState),is this where I am supposed to write the code? how do I access the value [I]int rectState[/I] on this function?
{
C2DM::LinkState *pState = (C2DM::LinkState*)objPtr->_clone();
addRect(pState);
To copy to clipboard, switch view to plain text mode
function addRect() I updated it as you advised:
void GatewayWidget::addRect(DM::Object *_pTitle)
{
if (CORE::checkPtrType<C2DM::LinkState>(_pTitle) == true)
{
C2DM::LinkState *pState = (C2DM::LinkState*)_pTitle;
int x = (qrand() % 4);
int pos = (x * 75) - 75 / 2;
textColor->setPos(400, pos + 200);
rectangleColor->setData(0, int(pState->m_eState));
rectangleColor->setPos(0, 25);
getTitleDescriptionColor(pState, m_backgroundColor, rectangleColor);
}
void GatewayWidget::addRect(DM::Object *_pTitle)
{
if (CORE::checkPtrType<C2DM::LinkState>(_pTitle) == true)
{
C2DM::LinkState *pState = (C2DM::LinkState*)_pTitle;
QGraphicsTextItem *textColor = scene->addText(QString::fromStdString(pState->m_strLinkId));
int x = (qrand() % 4);
int pos = (x * 75) - 75 / 2;
textColor->setPos(400, pos + 200);
textColor->setFlag(QGraphicsItem::ItemIsMovable);
textColor->setFlag(QGraphicsItem::ItemIsSelectable);
QGraphicsRectItem *rectangleColor = new QGraphicsRectItem(QRectF(0, 0, 50, 50), textColor);
rectangleColor->setData(0, int(pState->m_eState));
rectangleColor->setPos(0, 25);
rectangleColor->setFlag(QGraphicsItem::ItemIsSelectable);
getTitleDescriptionColor(pState, m_backgroundColor, rectangleColor);
}
To copy to clipboard, switch view to plain text mode
Function getTitleDescription() as follows:
{
QColor ret
= _colorBackground;
if (CORE::checkPtrType<C2DM::LinkState>(_pTitle) == true)
{
C2DM::LinkState *pState = (C2DM::LinkState*)_pTitle;
// If the state type passed in does not match the state type stored in the rect, then do nothing
int stateType = int(pState->m_eState);
int rectState = _row->data(0).toInt();
if (stateType != rectState)
{
return ret;
}
else
{
if (pState->m_eState == C2DM::LinkState::EIS_GOOD)
{
ret
= QColor(Qt
::green).
darker(120);
_row->setBrush(ret);
}
else if (pState->m_eState == C2DM::LinkState::EIS_BUSY)
{
ret
= QColor(Qt
::yellow).
darker(120);
_row->setBrush(ret);
}
else
{
ret
= QColor(Qt
::red).
darker(120);
_row->setBrush(ret);
}
}
}
QColor getTitleDescriptionColor(const DM::Object *_pTitle, const QColor &_colorBackground, QGraphicsRectItem *_row)
{
QColor ret = _colorBackground;
if (CORE::checkPtrType<C2DM::LinkState>(_pTitle) == true)
{
C2DM::LinkState *pState = (C2DM::LinkState*)_pTitle;
// If the state type passed in does not match the state type stored in the rect, then do nothing
int stateType = int(pState->m_eState);
int rectState = _row->data(0).toInt();
if (stateType != rectState)
{
return ret;
}
else
{
if (pState->m_eState == C2DM::LinkState::EIS_GOOD)
{
ret = QColor(Qt::green).darker(120);
_row->setBrush(ret);
}
else if (pState->m_eState == C2DM::LinkState::EIS_BUSY)
{
ret = QColor(Qt::yellow).darker(120);
_row->setBrush(ret);
}
else
{
ret = QColor(Qt::red).darker(120);
_row->setBrush(ret);
}
}
}
To copy to clipboard, switch view to plain text mode
Please pardon me for slow understanding, please help me on the way forward.Thank you for your help in advance.
Bookmarks