From 1728fee16adb345622efe05760b547e4c1c79db2 Mon Sep 17 00:00:00 2001 From: Xavier Rodriguez Date: Sat, 7 Feb 2015 11:54:34 +0100 Subject: [PATCH] [4] Dynamic selection of the spied block OK --- block/spy/src/SpyBlock.cpp | 34 +++++++++++++++++++++ block/spy/src/SpyBlock.hpp | 50 ++++++++++++++++++++++++------- block/spy/src/SpyWidget.cpp | 26 ++++++++++------ block/spy/src/SpyWidget.hpp | 8 ++--- block/spy/src/SpyWidgetFooter.cpp | 38 ++++++++++++++++------- block/spy/src/SpyWidgetFooter.hpp | 42 ++++++++++++++------------ block/spy/src/SpyWidgetHeader.cpp | 36 +++++++++++++++++----- block/spy/src/SpyWidgetHeader.hpp | 50 +++++++------------------------ core/src/block/BotBlock.hpp | 11 ++++++- tutorial/01_spies/script.js | 4 +-- 10 files changed, 196 insertions(+), 103 deletions(-) diff --git a/block/spy/src/SpyBlock.cpp b/block/spy/src/SpyBlock.cpp index 94f45dd..921a4b7 100644 --- a/block/spy/src/SpyBlock.cpp +++ b/block/spy/src/SpyBlock.cpp @@ -1,3 +1,22 @@ +//! +//! \file SpyBlock.cpp +//! +// Copyright 2015 MakingBot +// This file is part of BotJs. +// +// BotJs is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// BotJs is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with BotJs. If not, see . + #include /* ============================================================================ @@ -71,6 +90,21 @@ bool SpyBlock::connect(BotBlock* block, bool master) } } +/* ============================================================================ + * + * */ +bool SpyBlock::disconnectAll() +{ + // Clear connection + _spiedBlock.clear(); + + // Log + beglog() << "Remove all connection" << endlog(); + + // Alert the view + emit spiedBlockChanged(); +} + /* ============================================================================ * * */ diff --git a/block/spy/src/SpyBlock.hpp b/block/spy/src/SpyBlock.hpp index 1e052ad..6ce26cc 100644 --- a/block/spy/src/SpyBlock.hpp +++ b/block/spy/src/SpyBlock.hpp @@ -1,5 +1,23 @@ #ifndef SPYBLOCK_HPP #define SPYBLOCK_HPP +//! +//! \file SpyBlock.hpp +//! +// Copyright 2015 MakingBot +// This file is part of BotJs. +// +// BotJs is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// BotJs is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with BotJs. If not, see . #include @@ -39,7 +57,6 @@ class SpyBlock : public BotBlock else { return 0; } } - //! //! Create the widget if it is not //! @@ -63,11 +80,14 @@ class SpyBlock : public BotBlock if(visible) { show(); } else { hide(); } } - - QWeakPointer getSpiedBlock() { return _spiedBlock; } //! - //! Pointer on the spied block + //! Weak pointer on the spied block + //! + QWeakPointer getWeakSpiedBlock() { return _spiedBlock; } + + //! + //! Shared pointer on the spied block //! QSharedPointer getSharedSpiedBlock() { @@ -81,11 +101,11 @@ class SpyBlock : public BotBlock const QString getSpiedBlockName() { QSharedPointer spied = getSharedSpiedBlock(); - if(spied) { return spied->getBlockName(); } - return QString(); + if(spied) { return spied->getBlockName(); } return QString(); } signals: + //! Emitted when the spied block has changed void spiedBlockChanged(); @@ -94,15 +114,25 @@ public slots: //! FROM BotBlock virtual bool connect(BotBlock* block, bool master=true); + //! FROM BotBlock + virtual bool disconnectAll(); + //! Widget visibility setter virtual void show() { createWidgetIfRequired(); - _widget->show(); _visible = true; emit blockiPropertyValuesChanged(); + _widget->show(); + _visible = true; + emit blockiPropertyValuesChanged(); } //! Widget visibility setter - virtual void hide() { _widget->hide(); _visible = false; emit blockiPropertyValuesChanged(); } + virtual void hide() + { + _widget->hide(); + _visible = false; + emit blockiPropertyValuesChanged(); + } protected: //! Widget visibility @@ -111,8 +141,8 @@ public slots: //! Pointer on the spied block QWeakPointer _spiedBlock; - //! Widget - QSharedPointer _widget; + //! Widget for the view + QSharedPointer _widget; }; #endif // SPYBLOCK_HPP diff --git a/block/spy/src/SpyWidget.cpp b/block/spy/src/SpyWidget.cpp index b031b56..0df9cd0 100644 --- a/block/spy/src/SpyWidget.cpp +++ b/block/spy/src/SpyWidget.cpp @@ -15,7 +15,7 @@ SpyWidget::SpyWidget(QWeakPointer spy_block, QWidget* parent) : QWidget(parent) , _spyblock (spy_block) - , _header (new SpyWidgetHeader()) + , _header (spy_block) , _body (new SpyWidgetBody() ) , _footer (spy_block) { @@ -32,8 +32,8 @@ SpyWidget::SpyWidget(QWeakPointer spy_block, QWidget* parent) ((QGridLayout*)layout())->setSpacing(0); ((QGridLayout*)layout())->setContentsMargins(0,0,0,0); - ((QGridLayout*)layout())->addWidget(_header, 0, 0, Qt::AlignTop); - ((QGridLayout*)layout())->addWidget(_body , 1, 0); + ((QGridLayout*)layout())->addWidget(&_header, 0, 0, Qt::AlignTop); + ((QGridLayout*)layout())->addWidget(_body , 1, 0); ((QGridLayout*)layout())->addWidget(&_footer, 2, 0, Qt::AlignBottom); // Connect update signals @@ -45,9 +45,7 @@ SpyWidget::SpyWidget(QWeakPointer spy_block, QWidget* parent) * */ QSharedPointer SpyWidget::getSharedSpiedBlock() { - QSharedPointer spy = getSharedSpyBlock(); - if(!spy) { throw std::runtime_error("Spy not found!"); } - return spy->getSharedSpiedBlock(); + return getSharedSpyBlock()->getSharedSpiedBlock(); } /* ============================================================================ @@ -55,14 +53,17 @@ QSharedPointer SpyWidget::getSharedSpiedBlock() * */ void SpyWidget::onSpiedBlockChange() { + _header.onSpiedBlockChange(); + _footer.onSpiedBlockChange(); + + // Build the new one QSharedPointer spied = getSharedSpiedBlock(); if(spied) { - _header->setSpiedBlock(spied->getBlockWeakFromThis()); + _body ->setSpiedBlock(spied->getBlockWeakFromThis()); - _footer.setSpiedBlock(spied->getBlockWeakFromThis()); - + // Connect events // connect( spied.data(), SIGNAL(propertyValuesChanged ()), _body, SLOT(updateValues ()) ); // connect( spied.data(), SIGNAL(propertyStructureChanged()), _body, SLOT(updateStructure()) ); @@ -73,6 +74,13 @@ void SpyWidget::onSpiedBlockChange() //updateBasicInformation(); // } + else + { + // _header->setSpiedBlock(QWeakPointer(0)); + // _body ->setSpiedBlock(QWeakPointer(0)); + // _footer.setSpiedBlock (QWeakPointer(0)); + + } } /* ============================================================================ diff --git a/block/spy/src/SpyWidget.hpp b/block/spy/src/SpyWidget.hpp index 43a0233..d1837e2 100644 --- a/block/spy/src/SpyWidget.hpp +++ b/block/spy/src/SpyWidget.hpp @@ -24,14 +24,14 @@ class SpyWidget : public QWidget //! explicit SpyWidget(QWeakPointer spy_block, QWidget *parent = 0); - //! //! Pointer on the parent spy block //! QSharedPointer getSharedSpyBlock() { if(_spyblock) { return _spyblock.toStrongRef(); } - else { return QSharedPointer(); } + else { throw std::runtime_error("This widget need a parent spy block"); } + //return QSharedPointer(); } //! @@ -60,10 +60,10 @@ public slots: QWeakPointer _spyblock; //! Information header - SpyWidgetHeader* _header; + SpyWidgetHeader _header; //! Information body - SpyWidgetBody* _body; + SpyWidgetBody* _body; //! Information footer SpyWidgetFooter _footer; diff --git a/block/spy/src/SpyWidgetFooter.cpp b/block/spy/src/SpyWidgetFooter.cpp index cdc5a99..1731b6c 100644 --- a/block/spy/src/SpyWidgetFooter.cpp +++ b/block/spy/src/SpyWidgetFooter.cpp @@ -36,6 +36,14 @@ SpyWidgetFooter::SpyWidgetFooter(QWeakPointer spy_block, QWidget *pare lay->addWidget(&_buttonKill); } +/* ============================================================================ + * + * */ +void SpyWidgetFooter::onSpiedBlockChange() +{ + updateStructure(); + updateValues(); +} /* ============================================================================ @@ -43,7 +51,8 @@ SpyWidgetFooter::SpyWidgetFooter(QWeakPointer spy_block, QWidget *pare * */ void SpyWidgetFooter::updateValues() { - QSharedPointer spied = getSharedSpiedBlock(); + // Check the spied block + QSharedPointer spied = getSharedSpyBlock()->getSharedSpiedBlock(); if(!spied) { return; } // Get block father chain @@ -59,11 +68,11 @@ void SpyWidgetFooter::updateValues() * */ void SpyWidgetFooter::updateStructure() { - QSharedPointer spy = getSharedSpyBlock(); - if(!spy) { return; } + // Disconnect + disconnect( &_cbSpiedBlock, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(onCBSpiedChange(const QString&)) ); // Get chains - QStringList chains = spy->getBlockEngine()->getAllFatherChains(); + QStringList chains = getSharedSpyBlock()->getBlockEngine()->getAllFatherChains(); // Clear combobox _cbSpiedBlock.clear(); @@ -73,7 +82,7 @@ void SpyWidgetFooter::updateStructure() _cbSpiedBlock.addItems( chains ); // Connect combobox - connect( &_cbSpiedBlock, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(onCBSpiedChange(const QString&)) ); + connect ( &_cbSpiedBlock, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(onCBSpiedChange(const QString&)) ); } /* ============================================================================ @@ -99,28 +108,35 @@ void SpyWidgetFooter::onCBSpiedChange( const QString & chain ) { // Get spy QSharedPointer spy = getSharedSpyBlock(); - if(!spy) { return; } + + // If use want to stop spying + if( chain.compare("DO NOT SPY") == 0 ) + { + // Disconnect if it were connected + spy->disconnectAll(); + + return; + } // Special check if a block is already spied - QSharedPointer spied = getSharedSpiedBlock(); + QSharedPointer spied = getSharedSpyBlock()->getSharedSpiedBlock(); if(spied) { if( spied->getBlockFathersChain().compare(chain) == 0 ) { - return; // Cannot spy if its is already spied + // Cannot spy if its is already spied + spy->beglog() << "Try to spy the block that is already spied" << spy->endlog(); + return; } } // Get the block from its father chain BotBlock* block = spy->getBlockFromFathersChain( chain ); - // Try to connect to block if( !spy->connect(block) ) { // restore if fail _cbSpiedBlock.setCurrentIndex(_currentValidSelection); } - - std::cerr << block->getBlockName().toStdString() << std::endl; } diff --git a/block/spy/src/SpyWidgetFooter.hpp b/block/spy/src/SpyWidgetFooter.hpp index c0ff879..ae693ee 100644 --- a/block/spy/src/SpyWidgetFooter.hpp +++ b/block/spy/src/SpyWidgetFooter.hpp @@ -1,5 +1,23 @@ #ifndef SPYWIDGETFOOTER_HPP #define SPYWIDGETFOOTER_HPP +//! +//! \file SpyWidgetFooter.hpp +//! +// Copyright 2015 MakingBot +// This file is part of BotJs. +// +// BotJs is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// BotJs is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with BotJs. If not, see . #include #include @@ -25,12 +43,7 @@ class SpyWidgetFooter : public QWidget explicit SpyWidgetFooter(QWeakPointer spy_block, QWidget *parent = 0); //! - //! To update block information - //! - void setSpiedBlock(QWeakPointer block) { _block = block; updateStructure(); updateValues(); } - - //! - //! To get a shared pointer the parent spy block (0 if no spied block) + //! Pointer on the parent spy block //! QSharedPointer getSharedSpyBlock() { @@ -38,17 +51,11 @@ class SpyWidgetFooter : public QWidget else { return QSharedPointer(0); } } - //! - //! To get a shared pointer the spied block (0 if no spied block) - //! - QSharedPointer getSharedSpiedBlock() - { - if(_block) { return _block.toStrongRef(); } - else { return QSharedPointer(0); } - } - public slots: + //! When the spied change + void onSpiedBlockChange(); + //! Update values of the widget with data from the spied block void updateValues(); @@ -72,14 +79,11 @@ public slots: QPushButton _buttonCreate; // Combobox to select an other spied block - QComboBox _cbSpiedBlock; + QComboBox _cbSpiedBlock; //! Current selection of the combo box int _currentValidSelection; - //! The spied block - QWeakPointer _block; - //! Parent block QWeakPointer _spyblock; }; diff --git a/block/spy/src/SpyWidgetHeader.cpp b/block/spy/src/SpyWidgetHeader.cpp index 681e958..d52a1f3 100644 --- a/block/spy/src/SpyWidgetHeader.cpp +++ b/block/spy/src/SpyWidgetHeader.cpp @@ -8,7 +8,7 @@ /* ============================================================================ * * */ -SpyWidgetHeader::SpyWidgetHeader(QWidget *parent) +SpyWidgetHeader::SpyWidgetHeader(QWeakPointer spy_block, QWidget *parent) : QWidget(parent) , _labelTypName ( "type" ) , _labelVersion ( "version") @@ -16,6 +16,7 @@ SpyWidgetHeader::SpyWidgetHeader(QWidget *parent) , _labelFather ( "father" ) , _labelSonsNub ( "sons" ) , _labelConnNub ( "connections" ) + , _spyblock ( spy_block ) { // Widget geometrie const int height_name = 96; @@ -31,7 +32,6 @@ SpyWidgetHeader::SpyWidgetHeader(QWidget *parent) "}"); // Label name properties - setLabelNameProperties(); _labelName.setMinimumHeight(height_name); _labelName.setMaximumHeight(height_name); _labelName.setAlignment(Qt::AlignBottom); @@ -68,15 +68,27 @@ void SpyWidgetHeader::paintEvent(QPaintEvent *event) /* ============================================================================ * * */ -void SpyWidgetHeader::updateValues() +void SpyWidgetHeader::onSpiedBlockChange() { - // Lanel name has its own function - setLabelNameProperties(); + updateValues(); +} - if(_block) +/* ============================================================================ + * + * */ +void SpyWidgetHeader::updateValues() +{ + QSharedPointer block = getSharedSpyBlock()->getSharedSpiedBlock(); + if(block) { - QSharedPointer block = _block.toStrongRef(); - + _labelName.setText(block->getBlockName()); + _labelName.setStyleSheet( + "background-color:" + BotBlock::BlockRoleToColor(block->getBlockRole()) + " ;"\ + "color: #FFFFFF;"\ + "font: 34px Roboto;"\ + "padding: 10px;"\ + ); + _labelTypName.setValue(block->getBlockTypeName()); _labelVersion.setValue(QString::number(block->getBlockVersion())); _labelRolName.setValue(BotBlock::BlockRoleToString(block->getBlockRole())); @@ -95,6 +107,14 @@ void SpyWidgetHeader::updateValues() } else { + _labelName.setText("No block spied"); + _labelName.setStyleSheet( + "background-color:" + BotBlock::BlockRoleToColor((BotBlock::BlockRole)0xFFFF) + " ;"\ + "color: #FFFFFF;"\ + "font: 34px Roboto;"\ + "padding: 10px;"\ + ); + _labelTypName.setValue(""); _labelVersion.setValue(""); _labelRolName.setValue(""); diff --git a/block/spy/src/SpyWidgetHeader.hpp b/block/spy/src/SpyWidgetHeader.hpp index d4dd5a8..78225b6 100644 --- a/block/spy/src/SpyWidgetHeader.hpp +++ b/block/spy/src/SpyWidgetHeader.hpp @@ -19,26 +19,25 @@ class SpyWidgetHeader : public QWidget //! //! Default constructor //! - explicit SpyWidgetHeader(QWidget *parent = 0); + explicit SpyWidgetHeader(QWeakPointer spy_block, QWidget *parent = 0); //! FROM QWidget void paintEvent(QPaintEvent *event); //! - //! To update block information + //! Pointer on the parent spy block //! - void setSpiedBlock(QWeakPointer block) { _block = block; updateValues(); } - - //! - //! To get a shared pointer the spied block (0 if no spied block) - //! - QSharedPointer getSharedSpiedBlock() + QSharedPointer getSharedSpyBlock() { - if(_block) { return _block.toStrongRef(); } - else { return QSharedPointer(0); } + if(_spyblock) { return _spyblock.toStrongRef(); } + else { return QSharedPointer(0); } } public slots: + + //! When the spied change + void onSpiedBlockChange(); + //! Update property values void updateValues(); @@ -75,36 +74,9 @@ public slots: - //! The spied block - QWeakPointer _block; + //! Parent block + QWeakPointer _spyblock; - //! - //! Set properties of the label name - //! - void setLabelNameProperties() - { - QSharedPointer block = getSharedSpiedBlock(); - if(block) - { - _labelName.setText(block->getBlockName()); - _labelName.setStyleSheet( - "background-color:" + BotBlock::BlockRoleToColor(block->getBlockRole()) + " ;"\ - "color: #FFFFFF;"\ - "font: 34px Roboto;"\ - "padding: 10px;"\ - ); - } - else - { - _labelName.setText("No block spied"); - _labelName.setStyleSheet( - "background-color:" + BotBlock::BlockRoleToColor((BotBlock::BlockRole)0xFFFF) + " ;"\ - "color: #FFFFFF;"\ - "font: 34px Roboto;"\ - "padding: 10px;"\ - ); - } - } }; diff --git a/core/src/block/BotBlock.hpp b/core/src/block/BotBlock.hpp index 68d2cac..a90633b 100644 --- a/core/src/block/BotBlock.hpp +++ b/core/src/block/BotBlock.hpp @@ -264,7 +264,7 @@ public slots: { // Basic checks if(!block) { beglog() << "Connection to null block failure" << endlog(); return false; } - if(block == this) { std::cerr << "-- BotBlock::connectBlock => unable to connect to itself" << std::endl; return false; } + if(block == this) { beglog() << "Connection to itself refused" << endlog(); return false; } // This block ask for a connection if(master) @@ -285,6 +285,15 @@ public slots: return true; } + //! + //! To delete all connections + //! + virtual bool disconnectAll() + { + return true; + } + + //! //! Create a block as a child of this one //! diff --git a/tutorial/01_spies/script.js b/tutorial/01_spies/script.js index 9c5c528..0add5f5 100644 --- a/tutorial/01_spies/script.js +++ b/tutorial/01_spies/script.js @@ -5,11 +5,11 @@ // Create spies core.create("spy", "agent007"); -// // core.create("spy", "agent008"); +core.create("spy", "agent008"); // // core.create("spy", "agent009"); -agent007.logEnable = true agent007.logTalking = true +agent008.logTalking = true // Connect spies to robot agent007.connect(core);