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

📄 staticsettings.cc

📁 c++的guiQt做的开发
💻 CC
字号:
/** @file StaticSettings - class handling static settings stored in one file. Format of the file is almost identical to "ini file", but the format is enhanced to allow indentation and comments to be put into the file and also the encoding of the file is always assumed to be utf-8. It can read almost all files that QSettings can read, but not all files this can read can be read by QSettings @author Martin Petricek*/#include "staticsettings.h"#include <QFile>#include <QRegExp>#include <QStringList>#include <QTextCodec>namespace gui {/** Default constructor*/StaticSettings::StaticSettings() { //Empty}/** Default destructor*/StaticSettings::~StaticSettings() { //Empty}/** Convenience overloaded function providing file parameter separated to file and directory name \see tryLoad(const QString &) @param dirName directory name @param fileName file name*/bool StaticSettings::tryLoad(const QString &dirName,const QString &fileName) { return tryLoad(dirName+"/"+fileName);}/** Load configuration from given text stream. Lines are loaded from the stream until end of stream is reached @param cfg QTextStream used to load configuration*/void StaticSettings::load(QTextStream &cfg) { QString line; QString mainKey=""; QRegExp nextHead("\\[(.*)(\\]|#|;)"); nextHead.setMinimal(true); set.clear(); for(;;) {  //Read till we get EOF  line=cfg.readLine();  if (line.isNull()) return;  line=line.trimmed();  if (line=="") continue;		//Empty line  if (line[0]=='#' || line[0]==';') continue;	//Comment line  if (line[0]=='[') {   //Possibly heading with group name   int pos=nextHead.indexIn(line);   if (pos<0) continue;			//Bad/invalid heading   if (nextHead.cap(2)!="]") continue;	//Bad/invalid heading   mainKey=nextHead.cap(1)+"/";   continue;  }  /** \todo check for extra comment at end or something like that */  QString key=line.section('=',0,0).trimmed();  QString value=line.section('=',1);  if (value.isNull()) continue; //Bad line format  value=value.trimmed();  set.insert(mainKey+key,value); }}/** Try to load settings from given file in given directory. If succesful, return true and any settings loaded so far are reset and replaced by settings from given file If unsuccessful, return false @param fileName file name*/bool StaticSettings::tryLoad(const QString &fileName) { QFile f(fileName); if (!f.open(QIODevice::ReadOnly)) return false; //File can't be opened QTextStream cfg(&f); cfg.setCodec(QTextCodec::codecForName("UTF-8")); load(cfg); return true;}/** Return list of all keys starting with given prefix followed by "/" Warning: this function is not efficient, as it traverse all the settings to find the result @param prefix Prefix of items @return list of keys with this prefix*/QStringList StaticSettings::entryList(const QString &prefix) { QStringList ret; QMap<QString,QString>::Iterator it; QString pr=prefix+"/"; int len=pr.length(); for (it=set.begin();it!=set.end();++it) {  QString key=it.key();  if (key.startsWith(pr)) {   ret+=key.mid(len);  } } return ret;}/** Read settings with given key from configuration file and return as QString @param key Key to read from settings @param defValue default value to use if key not found in settings. @return Value of given setting*/QString StaticSettings::readEntry(const QString &key,const QString &defValue/*=QString::null*/) { if (!set.contains(key)) return defValue; return set[key];}} // namespace gui

⌨️ 快捷键说明

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