Skip to content

Commit

Permalink
[4] Dynamic selection of the spied block OK
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ committed Feb 7, 2015
1 parent d72438c commit 1728fee
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 103 deletions.
34 changes: 34 additions & 0 deletions block/spy/src/SpyBlock.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

#include <SpyBlock.hpp>

/* ============================================================================
Expand Down Expand Up @@ -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();
}

/* ============================================================================
*
* */
Expand Down
50 changes: 40 additions & 10 deletions block/spy/src/SpyBlock.hpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

#include <SpyWidget.hpp>

Expand Down Expand Up @@ -39,7 +57,6 @@ class SpyBlock : public BotBlock
else { return 0; }
}


//!
//! Create the widget if it is not
//!
Expand All @@ -63,11 +80,14 @@ class SpyBlock : public BotBlock
if(visible) { show(); }
else { hide(); }
}

QWeakPointer<BotBlock> getSpiedBlock() { return _spiedBlock; }

//!
//! Pointer on the spied block
//! Weak pointer on the spied block
//!
QWeakPointer<BotBlock> getWeakSpiedBlock() { return _spiedBlock; }

//!
//! Shared pointer on the spied block
//!
QSharedPointer<BotBlock> getSharedSpiedBlock()
{
Expand All @@ -81,11 +101,11 @@ class SpyBlock : public BotBlock
const QString getSpiedBlockName()
{
QSharedPointer<BotBlock> 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();

Expand All @@ -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
Expand All @@ -111,8 +141,8 @@ public slots:
//! Pointer on the spied block
QWeakPointer<BotBlock> _spiedBlock;

//! Widget
QSharedPointer<SpyWidget> _widget;
//! Widget for the view
QSharedPointer<SpyWidget> _widget;
};

#endif // SPYBLOCK_HPP
26 changes: 17 additions & 9 deletions block/spy/src/SpyWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
SpyWidget::SpyWidget(QWeakPointer<SpyBlock> spy_block, QWidget* parent)
: QWidget(parent)
, _spyblock (spy_block)
, _header (new SpyWidgetHeader())
, _header (spy_block)
, _body (new SpyWidgetBody() )
, _footer (spy_block)
{
Expand All @@ -32,8 +32,8 @@ SpyWidget::SpyWidget(QWeakPointer<SpyBlock> 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
Expand All @@ -45,24 +45,25 @@ SpyWidget::SpyWidget(QWeakPointer<SpyBlock> spy_block, QWidget* parent)
* */
QSharedPointer<BotBlock> SpyWidget::getSharedSpiedBlock()
{
QSharedPointer<SpyBlock> spy = getSharedSpyBlock();
if(!spy) { throw std::runtime_error("Spy not found!"); }
return spy->getSharedSpiedBlock();
return getSharedSpyBlock()->getSharedSpiedBlock();
}

/* ============================================================================
*
* */
void SpyWidget::onSpiedBlockChange()
{
_header.onSpiedBlockChange();
_footer.onSpiedBlockChange();


// Build the new one
QSharedPointer<BotBlock> 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()) );
Expand All @@ -73,6 +74,13 @@ void SpyWidget::onSpiedBlockChange()
//updateBasicInformation();
//
}
else
{
// _header->setSpiedBlock(QWeakPointer<BotBlock>(0));
// _body ->setSpiedBlock(QWeakPointer<BotBlock>(0));
// _footer.setSpiedBlock (QWeakPointer<BotBlock>(0));

}
}

/* ============================================================================
Expand Down
8 changes: 4 additions & 4 deletions block/spy/src/SpyWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class SpyWidget : public QWidget
//!
explicit SpyWidget(QWeakPointer<SpyBlock> spy_block, QWidget *parent = 0);


//!
//! Pointer on the parent spy block
//!
QSharedPointer<SpyBlock> getSharedSpyBlock()
{
if(_spyblock) { return _spyblock.toStrongRef(); }
else { return QSharedPointer<SpyBlock>(); }
else { throw std::runtime_error("This widget need a parent spy block"); }
//return QSharedPointer<SpyBlock>();
}

//!
Expand Down Expand Up @@ -60,10 +60,10 @@ public slots:
QWeakPointer<SpyBlock> _spyblock;

//! Information header
SpyWidgetHeader* _header;
SpyWidgetHeader _header;

//! Information body
SpyWidgetBody* _body;
SpyWidgetBody* _body;

//! Information footer
SpyWidgetFooter _footer;
Expand Down
38 changes: 27 additions & 11 deletions block/spy/src/SpyWidgetFooter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,23 @@ SpyWidgetFooter::SpyWidgetFooter(QWeakPointer<SpyBlock> spy_block, QWidget *pare
lay->addWidget(&_buttonKill);
}

/* ============================================================================
*
* */
void SpyWidgetFooter::onSpiedBlockChange()
{
updateStructure();
updateValues();
}


/* ============================================================================
*
* */
void SpyWidgetFooter::updateValues()
{
QSharedPointer<BotBlock> spied = getSharedSpiedBlock();
// Check the spied block
QSharedPointer<BotBlock> spied = getSharedSpyBlock()->getSharedSpiedBlock();
if(!spied) { return; }

// Get block father chain
Expand All @@ -59,11 +68,11 @@ void SpyWidgetFooter::updateValues()
* */
void SpyWidgetFooter::updateStructure()
{
QSharedPointer<SpyBlock> 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();
Expand All @@ -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&)) );
}

/* ============================================================================
Expand All @@ -99,28 +108,35 @@ void SpyWidgetFooter::onCBSpiedChange( const QString & chain )
{
// Get spy
QSharedPointer<SpyBlock> 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<BotBlock> spied = getSharedSpiedBlock();
QSharedPointer<BotBlock> 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;
}
Loading

0 comments on commit 1728fee

Please sign in to comment.