Skip to content
Snippets Groups Projects
Commit a0fff3ac authored by Van Herck, Walter's avatar Van Herck, Walter
Browse files

Simplify PropertyRepeater

parent 791e7fac
No related branches found
No related tags found
No related merge requests found
...@@ -112,7 +112,6 @@ void FitComparisonWidget::subscribeToItem() ...@@ -112,7 +112,6 @@ void FitComparisonWidget::subscribeToItem()
if (auto simItem = simulatedDataItem()) { if (auto simItem = simulatedDataItem()) {
simItem->mapper()->setOnValueChange([this]() { calculateRelativeDifference(); }, this); simItem->mapper()->setOnValueChange([this]() { calculateRelativeDifference(); }, this);
} }
calculateRelativeDifference(); calculateRelativeDifference();
m_realDataPlot->setItem(realDataItem()); m_realDataPlot->setItem(realDataItem());
...@@ -149,20 +148,12 @@ void FitComparisonWidget::unsubscribeFromItem() ...@@ -149,20 +148,12 @@ void FitComparisonWidget::unsubscribeFromItem()
void FitComparisonWidget::onResetViewAction() void FitComparisonWidget::onResetViewAction()
{ {
m_comparisonController->setActive(false);
if (auto item = realDataItem()) if (auto item = realDataItem())
item->resetView(); item->resetView();
if (auto item = simulatedDataItem())
item->resetView();
if (diffItem()) { if (diffItem()) {
diffItem()->resetView();
diffItem()->setLowerAndUpperZ(relative_diff_min, relative_diff_max); diffItem()->setLowerAndUpperZ(relative_diff_min, relative_diff_max);
} }
m_comparisonController->setActive(true);
} }
void FitComparisonWidget::calculateRelativeDifference() void FitComparisonWidget::calculateRelativeDifference()
...@@ -181,7 +172,6 @@ void FitComparisonWidget::calculateRelativeDifference() ...@@ -181,7 +172,6 @@ void FitComparisonWidget::calculateRelativeDifference()
diffItem()->yAxisItem()->setItemValue(BasicAxisItem::P_TITLE, simulatedDataItem()->getYaxisTitle()); diffItem()->yAxisItem()->setItemValue(BasicAxisItem::P_TITLE, simulatedDataItem()->getYaxisTitle());
diffItem()->setLowerAndUpperZ(relative_diff_min, relative_diff_max); diffItem()->setLowerAndUpperZ(relative_diff_min, relative_diff_max);
diffItem()->setAxesRangeToData(); diffItem()->setAxesRangeToData();
} }
void FitComparisonWidget::unsubscribeFromChildren() void FitComparisonWidget::unsubscribeFromChildren()
......
...@@ -19,9 +19,7 @@ PropertyRepeater::PropertyRepeater(QObject* parent, bool repeat_child_properties ...@@ -19,9 +19,7 @@ PropertyRepeater::PropertyRepeater(QObject* parent, bool repeat_child_properties
: QObject(parent) : QObject(parent)
, m_block_repeater(false) , m_block_repeater(false)
, m_repeat_child_properties(repeat_child_properties) , m_repeat_child_properties(repeat_child_properties)
{ {}
}
PropertyRepeater::~PropertyRepeater() PropertyRepeater::~PropertyRepeater()
{ {
...@@ -29,16 +27,14 @@ PropertyRepeater::~PropertyRepeater() ...@@ -29,16 +27,14 @@ PropertyRepeater::~PropertyRepeater()
item->mapper()->unsubscribe(this); item->mapper()->unsubscribe(this);
} }
void PropertyRepeater::addItem(SessionItem* sessionItem, const QStringList& activeProperties) void PropertyRepeater::addItem(SessionItem* sessionItem)
{ {
if (!sessionItem || m_dataItems.contains(sessionItem)) if (!sessionItem || m_dataItems.contains(sessionItem))
return; return;
sessionItem->mapper()->setOnItemDestroy([this](SessionItem* item) sessionItem->mapper()->setOnItemDestroy([this](SessionItem* item)
{ {
m_dataItems.removeAll(item); m_dataItems.removeAll(item);
m_itemProperties.remove(item);
}, this); }, this);
sessionItem->mapper()->setOnPropertyChange( sessionItem->mapper()->setOnPropertyChange(
...@@ -50,10 +46,7 @@ void PropertyRepeater::addItem(SessionItem* sessionItem, const QStringList& acti ...@@ -50,10 +46,7 @@ void PropertyRepeater::addItem(SessionItem* sessionItem, const QStringList& acti
setOnChildPropertyChange(item, name); setOnChildPropertyChange(item, name);
}, this); }, this);
} }
m_dataItems.push_back(sessionItem); m_dataItems.push_back(sessionItem);
if (!activeProperties.isEmpty())
m_itemProperties[sessionItem] = activeProperties;
} }
void PropertyRepeater::clear() void PropertyRepeater::clear()
...@@ -62,7 +55,6 @@ void PropertyRepeater::clear() ...@@ -62,7 +55,6 @@ void PropertyRepeater::clear()
item->mapper()->unsubscribe(this); item->mapper()->unsubscribe(this);
m_dataItems.clear(); m_dataItems.clear();
m_itemProperties.clear();
} }
void PropertyRepeater::setActive(bool isActive) void PropertyRepeater::setActive(bool isActive)
...@@ -77,19 +69,13 @@ void PropertyRepeater::onPropertyChanged(SessionItem* item, const QString& prope ...@@ -77,19 +69,13 @@ void PropertyRepeater::onPropertyChanged(SessionItem* item, const QString& prope
m_block_repeater = true; m_block_repeater = true;
if (isPropertyBroadcastAllowed(item, propertyName)) { QVariant value = item->getItemValue(propertyName);
QVariant value = item->getItemValue(propertyName); for (auto target : targetItems(item)) {
target->setItemValue(propertyName, value);
for (auto target : targetItems(item))
if (isPropertyReceiveAllowed(target, propertyName))
target->setItemValue(propertyName, value);
} }
m_block_repeater = false; m_block_repeater = false;
} }
void PropertyRepeater::setOnChildPropertyChange(SessionItem* item, const QString& propertyName) void PropertyRepeater::setOnChildPropertyChange(SessionItem* item, const QString& propertyName)
{ {
if (m_block_repeater) if (m_block_repeater)
...@@ -98,16 +84,11 @@ void PropertyRepeater::setOnChildPropertyChange(SessionItem* item, const QString ...@@ -98,16 +84,11 @@ void PropertyRepeater::setOnChildPropertyChange(SessionItem* item, const QString
m_block_repeater = true; m_block_repeater = true;
SessionItem* sourceItem = item->parent(); SessionItem* sourceItem = item->parent();
QString tag = sourceItem->tagFromItem(item);
if (isPropertyBroadcastAllowed(sourceItem, propertyName)) { QVariant value = item->getItemValue(propertyName);
QString tag = sourceItem->tagFromItem(item); for (auto target : targetItems(sourceItem)) {
QVariant value = item->getItemValue(propertyName); target->getItem(tag)->setItemValue(propertyName, value);
for (auto target : targetItems(sourceItem))
if (isPropertyReceiveAllowed(target, propertyName))
target->getItem(tag)->setItemValue(propertyName, value);
} }
m_block_repeater = false; m_block_repeater = false;
} }
...@@ -119,21 +100,3 @@ QVector<SessionItem*> PropertyRepeater::targetItems(SessionItem* sourceItem) ...@@ -119,21 +100,3 @@ QVector<SessionItem*> PropertyRepeater::targetItems(SessionItem* sourceItem)
result.removeAll(sourceItem); result.removeAll(sourceItem);
return result; return result;
} }
//! Returns true if given item is allowed to send updates about property with given name.
bool PropertyRepeater::isPropertyBroadcastAllowed(SessionItem* item, const QString& propertyName)
{
auto it = m_itemProperties.find(item);
if (it == m_itemProperties.end())
return true; // no special wishes, broadcast is allowed
return it.value().contains(propertyName) ? true : false;
}
bool PropertyRepeater::isPropertyReceiveAllowed(SessionItem* item, const QString& propertyName)
{
// for the moment item which is allowed to send notifications on property update
// is also allowed to received updates from other properties
return isPropertyBroadcastAllowed(item, propertyName);
}
...@@ -32,7 +32,7 @@ public: ...@@ -32,7 +32,7 @@ public:
explicit PropertyRepeater(QObject* parent = nullptr, bool repeat_child_properties = false); explicit PropertyRepeater(QObject* parent = nullptr, bool repeat_child_properties = false);
~PropertyRepeater(); ~PropertyRepeater();
void addItem(SessionItem* sessionItem, const QStringList& activeProperties = QStringList()); void addItem(SessionItem* sessionItem);
void clear(); void clear();
...@@ -41,12 +41,8 @@ private: ...@@ -41,12 +41,8 @@ private:
void onPropertyChanged(SessionItem* item, const QString& propertyName); void onPropertyChanged(SessionItem* item, const QString& propertyName);
void setOnChildPropertyChange(SessionItem* item, const QString& propertyName); void setOnChildPropertyChange(SessionItem* item, const QString& propertyName);
QVector<SessionItem*> targetItems(SessionItem* sourceItem); QVector<SessionItem*> targetItems(SessionItem* sourceItem);
bool isPropertyBroadcastAllowed(SessionItem* item, const QString& propertyName);
bool isPropertyReceiveAllowed(SessionItem* item, const QString& propertyName);
QVector<SessionItem*> m_dataItems; QVector<SessionItem*> m_dataItems;
//! List of properties which item is allowed to report to others and receive updates.
QMap<SessionItem*, QStringList> m_itemProperties;
bool m_block_repeater; bool m_block_repeater;
bool m_repeat_child_properties; bool m_repeat_child_properties;
}; };
......
...@@ -85,70 +85,6 @@ TEST_F(TestPropertyRepeater, test_threeItems) ...@@ -85,70 +85,6 @@ TEST_F(TestPropertyRepeater, test_threeItems)
EXPECT_EQ(item3->getItemValue(BasicAxisItem::P_MAX).toDouble(), 4.0); EXPECT_EQ(item3->getItemValue(BasicAxisItem::P_MAX).toDouble(), 4.0);
} }
//! Checks if item with active properties specified receives updates only for these properties.
TEST_F(TestPropertyRepeater, test_filteredBroadcast)
{
SessionModel model("test");
auto item1 = createAxis(model);
auto item2 = createAxis(model);
auto item3 = createAxis(model);
item1->setItemValue(BasicAxisItem::P_MIN, 0.0);
item2->setItemValue(BasicAxisItem::P_MIN, 0.0);
item3->setItemValue(BasicAxisItem::P_MIN, 0.0);
item1->setItemValue(BasicAxisItem::P_MAX, 1.0);
item2->setItemValue(BasicAxisItem::P_MAX, 1.0);
item3->setItemValue(BasicAxisItem::P_MAX, 1.0);
QStringList activeProperties = QStringList() << BasicAxisItem::P_MAX;
PropertyRepeater repeater;
repeater.addItem(item1);
repeater.addItem(item2, activeProperties);
repeater.addItem(item3);
// changing two properties in item2 and checkig that only P_MAX changed in item1 and item3
item2->setItemValue(BasicAxisItem::P_MIN, 2.0);
item2->setItemValue(BasicAxisItem::P_MAX, 3.0);
EXPECT_EQ(item1->getItemValue(BasicAxisItem::P_MIN).toDouble(), 0.0);
EXPECT_EQ(item3->getItemValue(BasicAxisItem::P_MIN).toDouble(), 0.0);
EXPECT_EQ(item1->getItemValue(BasicAxisItem::P_MAX).toDouble(), 3.0);
EXPECT_EQ(item3->getItemValue(BasicAxisItem::P_MAX).toDouble(), 3.0);
}
TEST_F(TestPropertyRepeater, test_filteredReceive)
{
SessionModel model("test");
auto item1 = createAxis(model);
auto item2 = createAxis(model);
auto item3 = createAxis(model);
item1->setItemValue(BasicAxisItem::P_MIN, 0.0);
item2->setItemValue(BasicAxisItem::P_MIN, 0.0);
item3->setItemValue(BasicAxisItem::P_MIN, 0.0);
item1->setItemValue(BasicAxisItem::P_MAX, 1.0);
item2->setItemValue(BasicAxisItem::P_MAX, 1.0);
item3->setItemValue(BasicAxisItem::P_MAX, 1.0);
QStringList activeProperties = QStringList() << BasicAxisItem::P_MAX;
PropertyRepeater repeater;
repeater.addItem(item1);
repeater.addItem(item2, activeProperties);
repeater.addItem(item3);
// changing two properties in item1 and checking that P_MIN of item2 remain intact
item1->setItemValue(BasicAxisItem::P_MIN, 2.0);
item1->setItemValue(BasicAxisItem::P_MAX, 3.0);
EXPECT_EQ(item2->getItemValue(BasicAxisItem::P_MIN).toDouble(), 0.0);
EXPECT_EQ(item2->getItemValue(BasicAxisItem::P_MAX).toDouble(), 3.0); // remain intact
EXPECT_EQ(item3->getItemValue(BasicAxisItem::P_MIN).toDouble(), 2.0);
EXPECT_EQ(item3->getItemValue(BasicAxisItem::P_MAX).toDouble(), 3.0);
}
//! Checking repeater in "repeat childs properties" mode //! Checking repeater in "repeat childs properties" mode
TEST_F(TestPropertyRepeater, test_repeatAll) TEST_F(TestPropertyRepeater, test_repeatAll)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment