mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Add GncOptionUIItem manipulation to GncOptionDB.
This commit is contained in:
parent
6ccb9dbb9e
commit
4146251cc7
@ -78,6 +78,50 @@ GncOptionDB::get_default_section() const noexcept
|
||||
return &(m_default_section.get());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionDB::set_ui_item(const char* section, const char* name, void* ui_item)
|
||||
{
|
||||
auto option = find_option(section, name);
|
||||
if (!option) return;
|
||||
option->set_ui_item(ui_item);
|
||||
}
|
||||
|
||||
void* const
|
||||
GncOptionDB::get_ui_item(const char* section, const char* name)
|
||||
{
|
||||
auto option = find_option(section, name);
|
||||
if (!option) return nullptr;
|
||||
return option->get_ui_item();
|
||||
}
|
||||
|
||||
GncOptionUIType
|
||||
GncOptionDB::get_ui_type(const char* section, const char* name)
|
||||
{
|
||||
auto option = find_option(section, name);
|
||||
if (!option) return GncOptionUIType::INTERNAL;
|
||||
return option->get_ui_type();
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionDB::set_ui_from_option(const char* section, const char* name,
|
||||
std::function<void(GncOption&)> func)
|
||||
{
|
||||
auto option = find_option(section, name);
|
||||
if (!option) return;
|
||||
func(option.get());
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionDB::set_option_from_ui(const char* section, const char* name,
|
||||
std::function<void(GncOption&)> func)
|
||||
{
|
||||
auto option = find_option(section, name);
|
||||
if (!option) return;
|
||||
func(option.get());
|
||||
}
|
||||
|
||||
|
||||
boost::optional<GncOptionSection&>
|
||||
GncOptionDB::find_section(const char* section)
|
||||
{
|
||||
@ -145,14 +189,14 @@ GncOptionDB::commit()
|
||||
{
|
||||
}
|
||||
|
||||
GncOptionDB*
|
||||
GncOptionDBPtr
|
||||
gnc_option_db_new(void)
|
||||
{
|
||||
return new GncOptionDB;
|
||||
return GncOptionDBPtr{new GncOptionDB};
|
||||
}
|
||||
|
||||
void
|
||||
gnc_register_string_option(GncOptionDB* db, const char* section,
|
||||
gnc_register_string_option(const GncOptionDBPtr& db, const char* section,
|
||||
const char* name, const char* key,
|
||||
const char* doc_string, std::string value)
|
||||
{
|
||||
@ -162,7 +206,7 @@ gnc_register_string_option(GncOptionDB* db, const char* section,
|
||||
}
|
||||
|
||||
void
|
||||
gnc_register_text_option(GncOptionDB* db, const char* section, const char* name,
|
||||
gnc_register_text_option(const GncOptionDBPtr& db, const char* section, const char* name,
|
||||
const char* key, const char* doc_string,
|
||||
std::string value)
|
||||
{
|
||||
@ -173,7 +217,7 @@ gnc_register_text_option(GncOptionDB* db, const char* section, const char* name,
|
||||
}
|
||||
|
||||
void
|
||||
gnc_register_budget_option(GncOptionDB* db, const char* section,
|
||||
gnc_register_budget_option(const GncOptionDBPtr& db, const char* section,
|
||||
const char* name, const char* key,
|
||||
const char* doc_string, GncBudget *value)
|
||||
{
|
||||
@ -183,7 +227,7 @@ gnc_register_budget_option(GncOptionDB* db, const char* section,
|
||||
}
|
||||
|
||||
void
|
||||
gnc_register_commodity_option(GncOptionDB* db, const char* section,
|
||||
gnc_register_commodity_option(const GncOptionDBPtr& db, const char* section,
|
||||
const char* name, const char* key,
|
||||
const char* doc_string, gnc_commodity *value)
|
||||
{
|
||||
@ -194,7 +238,7 @@ gnc_register_commodity_option(GncOptionDB* db, const char* section,
|
||||
|
||||
|
||||
void
|
||||
gnc_register_currency_option(GncOptionDB* db, const char* section,
|
||||
gnc_register_currency_option(const GncOptionDBPtr& db, const char* section,
|
||||
const char* name, const char* key,
|
||||
const char* doc_string, gnc_commodity *value)
|
||||
{
|
||||
|
@ -46,6 +46,14 @@ public:
|
||||
void unregister_option(const char* section, const char* name);
|
||||
void set_default_section(const char* section);
|
||||
const GncOptionSection* const get_default_section() const noexcept;
|
||||
void set_ui_item(const char* section, const char* name, void* ui_item);
|
||||
void* const get_ui_item(const char* section, const char* name);
|
||||
GncOptionUIType get_ui_type(const char* section, const char* name);
|
||||
void set_ui_from_option(const char* section, const char* name,
|
||||
std::function<void(GncOption&)> func);
|
||||
void set_option_from_ui(const char* section, const char* name,
|
||||
std::function<void(GncOption&)> func);
|
||||
SCM lookup_option(const char* section, const char* name);
|
||||
std::string lookup_string_option(const char* section,
|
||||
const char* name);
|
||||
bool set_option(const char* section, const char* name, SCM value);
|
||||
|
@ -68,3 +68,62 @@ TEST_F(GncOptionDBTest, test_register_string_option)
|
||||
std::string{"waldo"});
|
||||
EXPECT_STREQ("waldo", m_db->lookup_string_option("foo", "bar").c_str());
|
||||
}
|
||||
|
||||
class GncUIType
|
||||
{
|
||||
public:
|
||||
void set_value(const std::string& value) { m_value = value; }
|
||||
const std::string& get_value() const { return m_value; }
|
||||
private:
|
||||
std::string m_value;
|
||||
};
|
||||
|
||||
class GncOptionDBUITest : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
GncOptionDBUITest() : m_db{gnc_option_db_new()}
|
||||
{
|
||||
gnc_register_string_option(m_db, "foo", "bar", "baz", "Phony Option",
|
||||
std::string{"waldo"});
|
||||
gnc_register_text_option(m_db, "foo", "sausage", "links",
|
||||
"Phony Option", std::string{"waldo"});
|
||||
gnc_register_string_option(m_db, "qux", "grault", "baz", "Phony Option",
|
||||
std::string{""});
|
||||
gnc_register_text_option(m_db, "qux", "garply", "fred",
|
||||
"Phony Option", std::string{"waldo"});
|
||||
}
|
||||
|
||||
GncOptionDBPtr m_db;
|
||||
};
|
||||
|
||||
TEST_F(GncOptionDBUITest, test_set_ui_item)
|
||||
{
|
||||
GncUIType entry;
|
||||
m_db->set_ui_item("foo", "bar", &entry);
|
||||
EXPECT_EQ(&entry, static_cast<GncUIType*>(m_db->get_ui_item("foo", "bar")));
|
||||
}
|
||||
|
||||
TEST_F(GncOptionDBUITest, test_ui_value_from_option)
|
||||
{
|
||||
GncUIType entry;
|
||||
const char* value{"waldo"};
|
||||
m_db->set_ui_item("foo", "bar", &entry);
|
||||
m_db->set_ui_from_option("foo", "bar", [](GncOption& option){
|
||||
auto ui_item = static_cast<GncUIType* const>(option.get_ui_item());
|
||||
ui_item->set_value(option.get_value<std::string>());
|
||||
});
|
||||
EXPECT_STREQ(value, entry.get_value().c_str());
|
||||
}
|
||||
|
||||
TEST_F(GncOptionDBUITest, test_option_value_from_ui)
|
||||
{
|
||||
GncUIType entry;
|
||||
const char* value{"pepper"};
|
||||
m_db->set_ui_item("foo", "bar", &entry);
|
||||
entry.set_value(value);
|
||||
m_db->set_option_from_ui("foo", "bar", [](GncOption& option){
|
||||
auto ui_item = static_cast<GncUIType* const>(option.get_ui_item());
|
||||
option.set_value(ui_item->get_value());
|
||||
});
|
||||
EXPECT_STREQ(value, m_db->lookup_string_option("foo", "bar").c_str());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user