⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configurations.cpp

📁 一个帮助你学习英语的软件~~很不错的咯~~ 对功能又做了改进~大家支持下哈~
💻 CPP
字号:
//PK 2006/10/20 - 2007/03/07

#include "configurations.h"

CConfigurations g_config;

CConfigurations::CConfigurations()
{	//PK 2006/10/20 - 11/09
	_is_dirty = false;
	_top = _left = _height = _width = 0;
}

CConfigurations::~CConfigurations()
{	//PK 2006/10/20
	if (_is_dirty) save();
}
bool CConfigurations::load(const string & fn)
{	//PK 2006/10/20 - 2007/03/07
	_filename = fn;

	TiXmlDocument doc(_filename.c_str());
	if (doc.LoadFile()) {
		TiXmlHandle hDoc(&doc);
		TiXmlElement * root = hDoc.FirstChildElement().Element();
		if (NULL != root) {
			TiXmlHandle h_root = TiXmlHandle(root);
			load_intervals(h_root);
			load_outline(h_root);
			load_fonts(h_root);
			load_colors(h_root);
			load_active_books(h_root);
		}
	} else {
		default_intervals();
		default_outline();
		default_fonts();
		default_colors();
	}
	return true;
}
bool CConfigurations::save(const string & fn)
{	//PK 2006/10/20 - 2007/02/20
	string filename;
	if (fn.empty()) filename = _filename;
	else filename = fn;
	if (filename.empty()) return false;

	TiXmlDocument doc;
	TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
	doc.LinkEndChild( decl );

	TiXmlElement * root = new TiXmlElement("Recite");
	doc.LinkEndChild(root);
	TiXmlComment * comment = new TiXmlComment("Configrations for Recite app");
	root->LinkEndChild(comment);

	save_intervals(root);
	save_outline(root);
	save_fonts(root);
	save_colors(root);
	save_active_books(root);
	
	doc.SaveFile(filename.c_str());

	return true;
}
void CConfigurations::default_intervals()
{	//PK 2006/10/22 - 11/30
	_intervals.push_back(0);
	_intervals.push_back(1);
	_intervals.push_back(2);
	_intervals.push_back(5);
	_intervals.push_back(10);
	_intervals.push_back(16);
	_intervals.push_back(30);
	_intervals.push_back(50);
	_is_dirty = true;
}
bool CConfigurations::load_intervals(TiXmlHandle & parents)
{	//PK 2006/10/22 - 11/30
	TiXmlElement * intervals = parents.FirstChild("intervals").Element();
	if (NULL == intervals) {
		default_intervals();
		return true;
	}
	TiXmlHandle h_intervals = TiXmlHandle(intervals);
	TiXmlElement * interval = h_intervals.FirstChild().Element();
	for (; NULL != interval; interval = interval->NextSiblingElement()) {
		int v;
		interval->QueryIntAttribute("value", &v);
		_intervals.push_back(v);
	}
	return true;
}
bool CConfigurations::save_intervals(TiXmlElement * parents)
{	//PK 2006/10/22 - 2007/03/07
	assert(parents != NULL);
	TiXmlElement * intervals = new TiXmlElement("intervals");
	parents->LinkEndChild(intervals);

	t_intervals::iterator itr = _intervals.begin(), itr_end = _intervals.end();
	for (; itr != itr_end; ++itr) {
		TiXmlElement * interval = new TiXmlElement("interval");
		interval->SetAttribute("value", *itr);
		intervals->LinkEndChild(interval);
	}
	return true;
}
int CConfigurations::get_interval(int times)
{	//PK 2006/10/09 - 2007/03/06
	assert(times > 0);
	if (times < _intervals.size() && times >= 0) return _intervals[times];
	else return _intervals[_intervals.size() - 1];
}
void CConfigurations::default_outline()
{	//PK 2006/11/30
	_top = 50;
	_left = 50;
	_height = 480;
	_width = 640;
	_is_dirty = true;
}
bool CConfigurations::load_outline(TiXmlHandle & parents)
{	//PK 2006/11/06 - 11/30
	TiXmlElement * ol = parents.FirstChild("window-outline").Element();
	if (NULL == ol) {
		default_outline();
		return true;
	}
	ol->QueryIntAttribute("top", &_top);
	ol->QueryIntAttribute("left", &_left);
	ol->QueryIntAttribute("height", &_height);
	ol->QueryIntAttribute("width", &_width);
	return true;
}
bool CConfigurations::save_outline(TiXmlElement * parents)
{	//PK 2006/11/06 - 2007/03/07
	assert(parents != NULL);
	TiXmlElement * ol = new TiXmlElement("window-outline");
	ol->SetAttribute("top", _top);
	ol->SetAttribute("left", _left);
	ol->SetAttribute("height", _height);
	ol->SetAttribute("width", _width);
	parents->LinkEndChild(ol);
	return true;
}
void CConfigurations::default_fonts()
{	//PK 2006/11/09
	_Font font;
	
	font._fontname = "Arial";
	font._size = 15;

	_fonts["contents"] = font;
	_fonts["list_item"] = font;

	_is_dirty = true;
}
bool CConfigurations::load_fonts(TiXmlHandle & parents)
{	//PK 2006/11/09 - 11/30
	TiXmlElement * fts = parents.FirstChild("fonts").Element();
	if (NULL == fts) {
		default_fonts();
		return false;
	}
	TiXmlHandle h_fts = TiXmlHandle(fts);
	TiXmlElement * ft = h_fts.FirstChild().Element();
	for (; NULL != ft; ft = ft->NextSiblingElement()) {
		_Font font;
		string name;
		ft->QueryIntAttribute("size", &font._size);
		name = ft->Attribute("name");
		font._fontname = ft->Attribute("fontname");
		_fonts[name] = font;
	}
	return true;
}
bool CConfigurations::save_fonts(TiXmlElement * parents)
{	//PK 2006/11/09 - 2007/03/07
	assert(parents != NULL);
	TiXmlElement * fts = new TiXmlElement("fonts");
	parents->LinkEndChild(fts);
	t_fonts::iterator itr = _fonts.begin(), itr_end = _fonts.end();
	for (; itr != itr_end; ++itr) {
		TiXmlElement * ft = new TiXmlElement("font");
		ft->SetAttribute("name", itr->first.c_str());
		ft->SetAttribute("fontname", itr->second._fontname.c_str());
		ft->SetAttribute("size", itr->second._size);
		fts->LinkEndChild(ft);
	}
	return true;
}
bool CConfigurations::font(string & name, string & fontname, int & size)
{	//PK 2006/11/30
	t_fonts::iterator itr = _fonts.find(name);
	if (itr == _fonts.end()) return false;
	fontname = itr->second._fontname;
	size = itr->second._size;
	return true;
}
void CConfigurations::default_colors()
{	//PK 2006/11/28 - 12/05
	_colors["high_priority"] = 0xc8c8ff;
	_colors["mid_priority"] = 0xc8ffff;
	_colors["low_priority"] = 0xc8ffc8;
	_colors["select_frame"] = 0xc80000;
	_is_dirty = true;
}
bool CConfigurations::load_colors(TiXmlHandle & parents)
{	//PK 2006/11/28 - 30
	TiXmlElement * clrs = parents.FirstChild("colors").Element();
	if (NULL == clrs) {
		default_colors();
		return true;
	}
	TiXmlHandle h_clrs = TiXmlHandle(clrs);
	TiXmlElement * clr = h_clrs.FirstChild().Element();
	for (; NULL != clr; clr = clr->NextSiblingElement()) {
		int value;
		string name;
		clr->QueryIntAttribute("value", &value);
		name = clr->Attribute("name");
		_colors[name] = value;
	}
	return true;
}
bool CConfigurations::save_colors(TiXmlElement * parents)
{	//PK 2006/11/29 - 2007/03/07
	assert(parents != NULL);
	TiXmlElement * colors = new TiXmlElement("colors");
	parents->LinkEndChild(colors);

	t_colors::iterator itr = _colors.begin(), itr_end = _colors.end();
	for (; itr != itr_end; ++itr) {
		TiXmlElement * clr = new TiXmlElement("color");
		clr->SetAttribute("value", itr->second);
		clr->SetAttribute("name", (itr->first).c_str());
		colors->LinkEndChild(clr);
	}
	return true;
}
bool CConfigurations::color(string & name, unsigned long & color)
{	//PK 2006/11/29 - 30
	t_colors::iterator itr = _colors.find(name);
	if (itr == _colors.end()) return false;
	color = (unsigned long)(itr->second);
	return true;
}
bool CConfigurations::load_active_books(TiXmlHandle & parents)
{	//PK 2007/03/07
	TiXmlElement * books = parents.FirstChild("active_books").Element();
	if (NULL == books) return true;
	TiXmlHandle h_book = TiXmlHandle(books);
	TiXmlElement * book = h_book.FirstChild().Element();
	for (; NULL != book; book = book->NextSiblingElement())
		_active_books.push_back(book->Attribute("id"));
	return true;
}
bool CConfigurations::save_active_books(TiXmlElement * parents)
{	//PK 2007/03/07
	assert(parents != NULL);
	if (_active_books.size() == 0) return true;
	TiXmlElement * books = new TiXmlElement("active_books");
	parents->LinkEndChild(books);
	t_active_books::iterator itr = _active_books.begin(), itr_end = _active_books.end();
	for (; itr != itr_end; ++itr) {
		TiXmlElement * book = new TiXmlElement("book");
		book->SetAttribute("id", (*itr).c_str());
		books->LinkEndChild(book);
	}
	return true;
}
const string & CConfigurations::get_active_book(int index)
{	//PK 2007/03/07
	assert(_active_books.size() > 0);
	assert(index >= 0 && index < _active_books.size());
	if (index < 0 || index >= _active_books.size()) return _no_id;
	return _active_books[index];
}
void CConfigurations::set_active_book(const string & id)
{	//PK 2007/03/07
	t_active_books::iterator itr = find(_active_books.begin(), _active_books.end(), id);
	if (itr == _active_books.end()) {
		_active_books.push_back(id);
		_is_dirty = true;
	}
}
void CConfigurations::move_active_book(const string & id)
{	//PK 2007/03/07
	t_active_books::iterator itr = find(_active_books.begin(), _active_books.end(), id);
	if (itr != _active_books.end()) {
		_active_books.erase(itr);
		_is_dirty = true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -