Guest User

Untitled

a guest
Nov 14th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. #include <QTextStream>
  5.  
  6. MainWindow::MainWindow(QWidget *parent) :
  7. QMainWindow(parent),
  8. ui(new Ui::MainWindow)
  9. {
  10. ui->setupUi(this);
  11. }
  12.  
  13. void MainWindow::setConfig(libconfig::Config *config)
  14. {
  15. tm = std::make_unique<TreeModel>(config, this);
  16. ui->treeView->setModel(tm.get());
  17. }
  18.  
  19. MainWindow::~MainWindow()
  20. {
  21. delete ui;
  22. }
  23.  
  24. /*
  25. void MainWindow::on_treeView_activated(const QModelIndex &index)
  26. {
  27. QString val = ui->treeView->model()->data(index).toString();
  28. ui->lineEdit->setText(val);
  29. }
  30.  
  31. */
  32.  
  33. void MainWindow::on_pushButton_clicked()
  34. {
  35. tm->saveSettings();
  36. }
  37.  
  38. #include "treeitem.h"
  39. #include <libconfig.h++>
  40.  
  41. static QVariant getIndexOrName(const libconfig::Setting &setting)
  42. {
  43. if (!setting.isRoot()) {
  44. const auto &parent = setting.getParent();
  45. if (parent.isArray() || parent.isList())
  46. return setting.getIndex();
  47. }
  48.  
  49. return setting.getName();
  50. }
  51.  
  52. static QVariant getValueOrSize(const libconfig::Setting &setting)
  53. {
  54. using namespace libconfig;
  55. switch (setting.getType()) {
  56. // scalar types
  57. case Setting::TypeInt:
  58. return setting.operator int();
  59.  
  60. case Setting::TypeInt64:
  61. return setting.operator long long();
  62.  
  63. case Setting::TypeFloat:
  64. return setting.operator double();
  65.  
  66. case Setting::TypeString:
  67. return setting.c_str();
  68.  
  69. case Setting::TypeBoolean:
  70. return setting.operator bool();
  71.  
  72.  
  73. // aggregate types
  74. case Setting::TypeGroup:
  75. return QString{"Group. Size: %1"}.arg(setting.getLength());
  76.  
  77. case Setting::TypeArray:
  78. return QString{"Array. Size: %1"}.arg(setting.getLength());
  79.  
  80. case Setting::TypeList:
  81. return QString{"List. Size: %1"}.arg(setting.getLength());
  82.  
  83.  
  84. // not used
  85. case Setting::TypeNone:
  86. break;
  87. }
  88.  
  89. return QVariant{};
  90. }
  91.  
  92. static bool setValue(libconfig::Setting &setting, const QVariant &value)
  93. {
  94. using namespace libconfig;
  95. switch (setting.getType()) {
  96. // scalar types
  97. case Setting::TypeInt:
  98. if (value.canConvert(QVariant::Int)) {
  99. setting = value.toInt();
  100. return true;
  101. }
  102. case Setting::TypeInt64:
  103. if (value.canConvert(QVariant::LongLong)) {
  104. setting = value.toLongLong();
  105. return true;
  106. }
  107. case Setting::TypeFloat:
  108. if (value.canConvert(QVariant::Double)) {
  109. setting = value.toFloat();
  110. return true;
  111. }
  112. case Setting::TypeString:
  113. if (value.canConvert(QVariant::String)) {
  114. setting = value.toString().toStdString();
  115. return true;
  116. }
  117. case Setting::TypeBoolean:
  118. if (value.canConvert(QVariant::Bool)) {
  119. setting = value.toBool();
  120. return true;
  121. }
  122. default:
  123. break;
  124. }
  125.  
  126. return false;
  127. }
  128.  
  129. TreeItem::TreeItem(libconfig::Setting *setting, TreeItem *parentItem)
  130. : m_setting{setting}, m_parentItem{parentItem}
  131. {
  132. if (setting->isAggregate()) {
  133. for (auto &setting : *setting) {
  134. m_subSettings.push_back(new TreeItem(&setting, this));
  135. }
  136. }
  137. }
  138.  
  139. TreeItem::~TreeItem() { qDeleteAll(m_subSettings); }
  140.  
  141. TreeItem *TreeItem::child(int row) { return m_subSettings.at(row); }
  142.  
  143. int TreeItem::childCount() const { return m_subSettings.size(); }
  144.  
  145. int TreeItem::columnCount() const { return 2; }
  146.  
  147. QVariant TreeItem::data(int column) const
  148. {
  149. switch (column) {
  150. case 0:
  151. return getIndexOrName(*m_setting);
  152. case 1:
  153. return getValueOrSize(*m_setting);
  154. default:
  155. return QVariant{};
  156. }
  157. }
  158.  
  159. bool TreeItem::setData(const QVariant &value)
  160. {
  161. if (m_setting->isAggregate())
  162. return false;
  163.  
  164. return setValue(*m_setting, value);
  165. }
  166.  
  167. int TreeItem::row() const
  168. {
  169. if (!m_parentItem)
  170. return 0;
  171.  
  172. return m_parentItem->m_subSettings.indexOf(const_cast<TreeItem *>(this));
  173. }
  174.  
  175. TreeItem *TreeItem::parentItem() { return m_parentItem; }
  176.  
  177. #include "treemodel.h"
  178. #include "treeitem.h"
  179.  
  180. #include <QFile>
  181. #include <libconfig.h++>
  182. #include <QDateTime>
  183.  
  184. TreeModel::TreeModel(libconfig::Config *config, QObject *parent)
  185. : QAbstractItemModel{parent}, m_rootSetting{std::make_unique<TreeItem>(
  186. &(config->getRoot()), nullptr)}, m_config{config}
  187. {
  188. }
  189.  
  190. TreeModel::~TreeModel()
  191. {
  192. }
  193.  
  194. QVariant TreeModel::data(const QModelIndex &index, int role) const
  195. {
  196. if (!index.isValid())
  197. return QVariant();
  198.  
  199. if (role != Qt::DisplayRole)
  200. return QVariant();
  201.  
  202. TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
  203.  
  204. return item->data(index.column());
  205. }
  206.  
  207. bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
  208. {
  209. if (!index.isValid())
  210. return false;
  211.  
  212. if (role != Qt::EditRole)
  213. return false;
  214.  
  215. TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
  216. return item->setData(value);
  217. }
  218.  
  219. Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
  220. {
  221.  
  222. if (!index.isValid())
  223. //return Qt::NoItemFlags;
  224. return 0;
  225.  
  226. //return QAbstractItemModel::flags(index);
  227. return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
  228. }
  229.  
  230. QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
  231. int role) const
  232. {
  233. if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
  234. return QVariant{};
  235.  
  236. switch (section) {
  237. case 0:
  238. return "Name";
  239. case 1:
  240. return "Value";
  241. default:
  242. return QVariant{};
  243. }
  244. }
  245.  
  246. QModelIndex TreeModel::index(int row, int column,
  247. const QModelIndex &parent) const
  248. {
  249. if (!hasIndex(row, column, parent))
  250. return QModelIndex();
  251.  
  252. TreeItem *parentItem;
  253.  
  254. if (!parent.isValid())
  255. parentItem = m_rootSetting.get();
  256. else
  257. parentItem = static_cast<TreeItem*>(parent.internalPointer());
  258.  
  259. TreeItem *childItem = parentItem->child(row);
  260. if (childItem)
  261. return createIndex(row, column, childItem);
  262. else
  263. return QModelIndex();
  264. }
  265.  
  266. QModelIndex TreeModel::parent(const QModelIndex &index) const
  267. {
  268. if (!index.isValid())
  269. return QModelIndex();
  270.  
  271. TreeItem *childItem = static_cast<TreeItem *>(index.internalPointer());
  272. TreeItem *parentItem = childItem->parentItem();
  273.  
  274. if (parentItem == m_rootSetting.get())
  275. return QModelIndex();
  276.  
  277. return createIndex(parentItem->row(), 0, parentItem);
  278. }
  279.  
  280. int TreeModel::rowCount(const QModelIndex &parent) const
  281. {
  282. TreeItem *parentItem;
  283. if (parent.column() > 0)
  284. return 0;
  285.  
  286. if (!parent.isValid())
  287. parentItem = m_rootSetting.get();
  288. else
  289. parentItem = static_cast<TreeItem*>(parent.internalPointer());
  290.  
  291. return parentItem->childCount();
  292. }
  293.  
  294. int TreeModel::columnCount(const QModelIndex &parent) const
  295. {
  296. if (parent.isValid())
  297. return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
  298. else
  299. return m_rootSetting->columnCount();
  300. }
  301.  
  302. bool TreeModel::saveSettings(QString filename) const
  303. {
  304. if (filename.isEmpty())
  305. filename = QString::fromLocal8Bit(m_config->getRoot().getSourceFile());
  306. QString today = QDateTime::currentDateTime().toString("_yyyy.MM.dd_hh:mm");
  307. QFile::copy(filename, filename+today+".backup");
  308. try {
  309. m_config->writeFile(filename.toLocal8Bit().constData());
  310. } catch (...) {
  311. return false;
  312. }
  313.  
  314. return true;
  315. }
Add Comment
Please, Sign In to add comment