Skip to content

Commit

Permalink
Fixed copying of channel power settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
hmatuschek committed Dec 28, 2024
1 parent f80a108 commit 8eaf2f5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 18 deletions.
19 changes: 18 additions & 1 deletion lib/configcopyvisitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,27 @@ ConfigCloneVisitor::processItem(ConfigItem *item, const ErrorStack &err) {
if (item->is<ConfigObject>())
_map[item->as<ConfigObject>()] = dynamic_cast<ConfigObject*>(obj);

// then traverse item
// then traverse by type ...
if (item->is<Channel>())
return processChannel(item->as<Channel>(), err);

// .. or use default.
return Visitor::processItem(item, err);
}


bool
ConfigCloneVisitor::processChannel(Channel *item, const ErrorStack &err) {
if (! Visitor::processItem(item, err))
return false;

if (item->defaultPower())
qobject_cast<Channel*>(_stack.back())->setDefaultPower();

return true;
}


bool
ConfigCloneVisitor::processList(AbstractConfigObjectList *list, const ErrorStack &err) {
if (ConfigObjectRefList *refs = dynamic_cast<ConfigObjectRefList*>(list)) {
Expand Down
4 changes: 4 additions & 0 deletions lib/configcopyvisitor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "visitor.hh"

class ConfigObject;
class Channel;

/** This visitor traverses the the given configuration and clones it. All references are still
* pointing to the originals.
Expand All @@ -19,6 +20,9 @@ public:
bool processItem(ConfigItem *item, const ErrorStack &err=ErrorStack());
bool processList(AbstractConfigObjectList *list, const ErrorStack &err=ErrorStack());

/** Specialized handler for channels, must traverse the channel object. */
virtual bool processChannel(Channel *item, const ErrorStack &err=ErrorStack());

/** Extracts the cloned item. */
ConfigItem *takeResult(const ErrorStack &err=ErrorStack());

Expand Down
55 changes: 48 additions & 7 deletions test/copytest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,61 @@ CopyTest::CopyTest(QObject *parent)
void
CopyTest::testChannelClone() {
QHash<ConfigObject *, ConfigObject *> map;
ConfigCloneVisitor cloner(map);
Channel *ch = _basicConfig.channelList()->channel(0);

ErrorStack err;
if (! cloner.processItem(ch, err)) {
QFAIL(err.format().toLocal8Bit().constData());
ConfigCloneVisitor cloner(map);

Config config;
if (! config.readYAML(":/data/config_test.yaml"))
QFAIL("Cannot load config.");
auto ch = config.channelList()->channel(0);

{
if (! cloner.processItem(ch, err))
QFAIL(err.format().toLocal8Bit().constData());

ConfigItem *item = cloner.takeResult(err);
QVERIFY(item);
QCOMPARE(ch->compare(*item), 0);
delete item;
}

ConfigItem *item = cloner.takeResult(err);
QVERIFY(item);
{
ch->setPower(Channel::Power::Min);
ch->setTimeout(60);
ch->setVOX(3);

if (! cloner.processItem(ch, err))
QFAIL(err.format().toLocal8Bit().constData());

ConfigItem *item = cloner.takeResult(err);
QVERIFY(item);
QVERIFY(item->is<Channel>());
QCOMPARE(item->as<Channel>()->power(), Channel::Power::Min);
QCOMPARE(item->as<Channel>()->timeout(), 60);
QCOMPARE(item->as<Channel>()->vox(), 3);
delete item;
}

QCOMPARE(ch->compare(*item), 0);
{
ch->setDefaultPower();
ch->setDefaultTimeout();
ch->setVOXDefault();

if (! cloner.processItem(ch, err))
QFAIL(err.format().toLocal8Bit().constData());

ConfigItem *item = cloner.takeResult(err);
QVERIFY(item);
QVERIFY(item->is<Channel>());
QVERIFY(item->as<Channel>()->defaultPower());
QVERIFY(item->as<Channel>()->defaultTimeout());
QVERIFY(item->as<Channel>()->defaultVOX());
delete item;
}
}


void
CopyTest::testConfigClone() {
QHash<ConfigObject *, ConfigObject *> map;
Expand Down
10 changes: 0 additions & 10 deletions test/opengd77_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,6 @@ OpenGD77Test::testChannelPowerSettings() {
QFAIL(err.format().toLocal8Bit().constData());
QCOMPARE(decoded.channelList()->channel(0)->defaultPower(), false);
QCOMPARE(decoded.channelList()->channel(0)->power(), Channel::Power::High);


// Check re-encoding power settings.
OpenGD77Codeplug codeplug;
config.channelList()->channel(0)->setPower(Channel::Power::High);
codeplug.encode(&config, Codeplug::Flags());
config.channelList()->channel(0)->setDefaultPower();
codeplug.encode(&config, Codeplug::Flags());
codeplug.decode(&decoded, err);
QCOMPARE(decoded.channelList()->channel(0)->defaultPower(), true);
}


Expand Down

0 comments on commit 8eaf2f5

Please sign in to comment.