File: PreferencePage.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (163 lines) | stat: -rw-r--r-- 3,996 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "PreferencePage.h"

#include <stdexcept>
#include "i18n.h"
#include "itextstream.h"
#include "string/split.h"
#include "string/join.h"
#include <fmt/format.h>

#include "PreferenceItems.h"

namespace settings
{

PreferencePage::PreferencePage(const std::string& name, const PreferencePagePtr& parentPage) :
	_name(name)
{
	_title = fmt::format(_("{0} Settings"), _name);

	// Construct the _path member value
	if (parentPage && !parentPage->getPath().empty())
	{
		_path = parentPage->getPath() + "/" + _name;
	}
	else
	{
		_path = _name;
	}
}

const std::string& PreferencePage::getTitle() const
{
	return _title;
}

void PreferencePage::setTitle(const std::string& title)
{
	_title = title;
}

const std::string& PreferencePage::getPath() const
{
	return _path;
}

const std::string& PreferencePage::getName() const
{
	return _name;
}

PreferencePage& PreferencePage::createOrFindPage(const std::string& path)
{
	// Split the path into parts
	std::list<std::string> parts;
	string::split(parts, path, "/");

	if (parts.empty())
	{
		rError() << "Cannot resolve empty preference path: " << path << std::endl;
		throw std::logic_error("Cannot resolve empty preference path.");
	}

	PreferencePagePtr child;

	// Try to lookup the page in the child list
	for (const PreferencePagePtr& candidate : _children)
	{
		if (candidate->getName() == parts.front())
		{
			child = candidate;
			break;
		}
	}

	if (!child)
	{
		// No child found, create a new page and add it to the list
		child = std::make_shared<PreferencePage>(parts.front(), shared_from_this());
		_children.push_back(child);
	}

	// We now have a child with this name, do we have a leaf?
	if (parts.size() > 1) 
	{
		// We have still more parts, split off the first part
		parts.pop_front();
		std::string subPath = string::join(parts, "/");

		// Pass the call to the child
		return child->createOrFindPage(subPath);
	}
	else 
	{
		// We have found a leaf, return the child page
		return *child;
	}
}

void PreferencePage::foreachChildPage(const std::function<void(IPreferencePage&)>& functor)
{
	for (const PreferencePagePtr& child : _children)
	{
		// Visit this instance
		functor(*child);

		// Pass the visitor recursively
		child->foreachChildPage(functor);
	}
}

void PreferencePage::foreachItem(const std::function<void(const IPreferenceItemBase::Ptr&)>& functor) const
{
	for (const IPreferenceItemBase::Ptr& item : _items)
	{
		functor(item);
	}
}

bool PreferencePage::isEmpty() const
{
	return _items.empty();
}

void PreferencePage::appendCheckBox(const std::string& label, const std::string& registryKey)
{
	_items.push_back(std::make_shared<PreferenceCheckbox>(label, registryKey));
}

void PreferencePage::appendSlider(const std::string& name, const std::string& registryKey,
	double lower, double upper, double stepIncrement, double pageIncrement)
{
	_items.push_back(std::make_shared<PreferenceSlider>(name, registryKey, 
		lower, upper, stepIncrement, pageIncrement));
}

void PreferencePage::appendCombo(const std::string& name, const std::string& registryKey,
	const ComboBoxValueList& valueList, bool storeValueNotIndex)
{
	_items.push_back(std::make_shared<PreferenceCombobox>(name, registryKey, valueList, storeValueNotIndex));
}

void PreferencePage::appendEntry(const std::string& name, const std::string& registryKey)
{
	_items.push_back(std::make_shared<PreferenceEntry>(name, registryKey));
}

void PreferencePage::appendLabel(const std::string& caption)
{
	_items.push_back(std::make_shared<PreferenceLabel>(caption));
}

void PreferencePage::appendPathEntry(const std::string& name, const std::string& registryKey, bool browseDirectories)
{
	_items.push_back(std::make_shared<PreferencePathEntry>(name, registryKey, browseDirectories));
}

void PreferencePage::appendSpinner(const std::string& name, const std::string& registryKey,
	double lower, double upper, int fraction)
{
	_items.push_back(std::make_shared<PreferenceSpinner>(name, registryKey, lower, upper, fraction));
}

} // namespace