Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. classe TreeItem;
  2. class TreeModel : public QAbstractItemModel
  3. {
  4. Q_OBJECT
  5.  
  6. public:
  7. explicit TreeModel(const QString &data, QObject *parent = 0);
  8. ~TreeModel();
  9.  
  10. QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
  11. Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
  12. QVariant headerData(int section, Qt::Orientation orientation,
  13. int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
  14. QModelIndex index(int row, int column,
  15. const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  16. QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
  17. int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  18. int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  19. QHash<int, QByteArray> roleNames() const;
  20.  
  21.  
  22. enum TreeRoles {
  23.  
  24. titleRole,
  25. summaryRole
  26. };
  27.  
  28. private:
  29. void setupModelData(const QStringList &lines, TreeItem *parent);
  30.  
  31. TreeItem *rootItem;
  32. };
  33. //! [0]
  34.  
  35. TreeModel::TreeModel(const QString &data, QObject *parent)
  36. : QAbstractItemModel(parent)
  37. {
  38. QList<QVariant> rootData;
  39. rootData << "Title" << "Summary";
  40. rootItem = new TreeItem(rootData);
  41. setupModelData(data.split(QString("n")), rootItem);
  42. }
  43. //! [0]
  44.  
  45. //! [1]
  46. TreeModel::~TreeModel()
  47. {
  48. delete rootItem;
  49. }
  50. //! [1]
  51.  
  52. //! [2]
  53. int TreeModel::columnCount(const QModelIndex &parent) const
  54. {
  55. if (parent.isValid())
  56. return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
  57. else
  58. return rootItem->columnCount();
  59. }
  60. //! [2]
  61.  
  62. //! [3]
  63.  
  64. //! [3]
  65.  
  66. //! [4]
  67. Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
  68. {
  69. if (!index.isValid())
  70. return 0;
  71.  
  72. return QAbstractItemModel::flags(index);
  73. }
  74. //! [4]
  75.  
  76. //! [5]
  77. QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
  78. int role) const
  79. {
  80. if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
  81. return rootItem->data(section);
  82.  
  83. return QVariant();
  84. }
  85. //! [5]
  86.  
  87. //! [6]
  88. QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
  89. const
  90. {
  91. if (!hasIndex(row, column, parent))
  92. return QModelIndex();
  93.  
  94. TreeItem *parentItem;
  95.  
  96. if (!parent.isValid())
  97. parentItem = rootItem;
  98. else
  99. parentItem = static_cast<TreeItem*>(parent.internalPointer());
  100.  
  101. TreeItem *childItem = parentItem->child(row);
  102. if (childItem)
  103. return createIndex(row, column, childItem);
  104. else
  105. return QModelIndex();
  106. }
  107. //! [6]
  108.  
  109. //! [7]
  110. QModelIndex TreeModel::parent(const QModelIndex &index) const
  111. {
  112. if (!index.isValid())
  113. return QModelIndex();
  114.  
  115. TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
  116. TreeItem *parentItem = childItem->parentItem();
  117.  
  118. if (parentItem == rootItem)
  119. return QModelIndex();
  120.  
  121. return createIndex(parentItem->row(), 0, parentItem);
  122. }
  123. //! [7]
  124.  
  125. //! [8]
  126. int TreeModel::rowCount(const QModelIndex &parent) const
  127. {
  128. TreeItem *parentItem;
  129. if (parent.column() > 0)
  130. return 0;
  131.  
  132. if (!parent.isValid())
  133. parentItem = rootItem;
  134. else
  135. parentItem = static_cast<TreeItem*>(parent.internalPointer());
  136.  
  137. return parentItem->childCount();
  138. }
  139. //! [8]
  140.  
  141. void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
  142. {
  143. QList<TreeItem*> parents;
  144. QList<int> indentations;
  145. parents << parent;
  146. indentations << 0;
  147.  
  148. int number = 0;
  149.  
  150. while (number < lines.count()) {
  151. int position = 0;
  152. while (position < lines[number].length()) {
  153. if (lines[number].at(position) != ' ')
  154. break;
  155. position++;
  156. }
  157.  
  158. QString lineData = lines[number].mid(position).trimmed();
  159.  
  160. if (!lineData.isEmpty()) {
  161. // Read the column data from the rest of the line.
  162. QStringList columnStrings = lineData.split("t", QString::SkipEmptyParts);
  163. QList<QVariant> columnData;
  164. for (int column = 0; column < columnStrings.count(); ++column)
  165. columnData << columnStrings[column];
  166.  
  167. if (position > indentations.last()) {
  168. // The last child of the current parent is now the new parent
  169. // unless the current parent has no children.
  170.  
  171. if (parents.last()->childCount() > 0) {
  172. parents << parents.last()->child(parents.last()->childCount()-1);
  173. indentations << position;
  174. }
  175. } else {
  176. while (position < indentations.last() && parents.count() > 0) {
  177. parents.pop_back();
  178. indentations.pop_back();
  179. }
  180. }
  181.  
  182. // Append a new item to the current parent's list of children.
  183. parents.last()->appendChild(new TreeItem(columnData, parents.last()));
  184. }
  185.  
  186. ++number;
  187. }
  188. }
  189.  
  190. QVariant TreeModel::data(const QModelIndex & index, int role) const {
  191. if (!index.isValid())
  192. return QVariant();
  193. TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
  194. if (index.isValid()) {
  195. switch (role) {
  196. case titleRole:
  197. return item->data(0);
  198. case summaryRole:
  199. return item->data(0);
  200. }
  201. }
  202. return QVariant();
  203. }
  204.  
  205. QHash<int, QByteArray> TreeModel::roleNames() const {
  206. QHash<int, QByteArray> roles;
  207.  
  208. roles[titleRole] = "title";
  209. roles[summaryRole] = "summary";
  210.  
  211. return roles;
  212. }
  213.  
  214. class TreeItem
  215. {
  216. public:
  217. explicit TreeItem(const QList<QVariant> &data, TreeItem *parentItem = 0);
  218. ~TreeItem();
  219.  
  220. void appendChild(TreeItem *child);
  221.  
  222. TreeItem *child(int row);
  223. int childCount() const;
  224. int columnCount() const;
  225. QVariant data(int column) const;
  226. int row() const;
  227. TreeItem *parentItem();
  228.  
  229.  
  230. private:
  231. QList<TreeItem*> m_childItems;
  232. QList<QVariant> m_itemData;
  233. TreeItem *m_parentItem;
  234. };
  235.  
  236. //! [0]
  237. TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
  238. {
  239. m_parentItem = parent;
  240. m_itemData = data;
  241. }
  242. //! [0]
  243.  
  244. //! [1]
  245. TreeItem::~TreeItem()
  246. {
  247. qDeleteAll(m_childItems);
  248. }
  249. //! [1]
  250.  
  251. //! [2]
  252. void TreeItem::appendChild(TreeItem *item)
  253. {
  254. m_childItems.append(item);
  255. }
  256. //! [2]
  257.  
  258. //! [3]
  259. TreeItem *TreeItem::child(int row)
  260. {
  261. return m_childItems.value(row);
  262. }
  263. //! [3]
  264.  
  265. //! [4]
  266. int TreeItem::childCount() const
  267. {
  268. return m_childItems.count();
  269. }
  270. //! [4]
  271.  
  272. //! [5]
  273. int TreeItem::columnCount() const
  274. {
  275. return m_itemData.count();
  276. }
  277. //! [5]
  278.  
  279. //! [6]
  280. QVariant TreeItem::data(int column) const
  281. {
  282. return m_itemData.value(column);
  283. }
  284. //! [6]
  285.  
  286. //! [7]
  287. TreeItem *TreeItem::parentItem()
  288. {
  289. return m_parentItem;
  290. }
  291. //! [7]
  292.  
  293. //! [8]
  294. int TreeItem::row() const
  295. {
  296. if (m_parentItem)
  297. return m_parentItem->m_childItems.indexOf(const_cast<TreeItem*>(this));
  298.  
  299. return 0;
  300. }
  301. //! [8]
  302.  
  303. #include "mainwindow.h"
  304. #include "ui_mainwindow.h"
  305. #include <QtQml>
  306. #include <QQuickView>
  307. #include "QQuickWidget"
  308. #include <QFile>
  309.  
  310. #include "treemodel.h"
  311.  
  312.  
  313. MainWindow::MainWindow(QWidget *parent) :
  314. QMainWindow(parent),
  315. ui(new Ui::MainWindow)
  316. {
  317. ui->setupUi(this);
  318.  
  319. QQuickWidget *view = new QQuickWidget;
  320. QQmlContext *ctxt = view->rootContext();
  321.  
  322.  
  323. QFile file("default.txt");
  324. file.open(QIODevice::ReadOnly);
  325. TreeModel model(file.readAll());
  326. file.close();
  327. ctxt->setContextProperty("myModel", &model);
  328. view->setSource(QUrl::fromLocalFile("main.qml"));
  329. view->setGeometry(0, 200, 600, 400);
  330. view->setResizeMode(QQuickWidget::SizeRootObjectToView);
  331. ui->dockWidget->setWidget(view);
  332. }
  333.  
  334. MainWindow::~MainWindow()
  335. {
  336. delete ui;
  337. }
  338.  
  339. import QtQuick 2.0
  340. import QtQml.Models 2.2
  341. import QtQuick.Controls 1.5
  342.  
  343. Rectangle {
  344. id : rootRectangle
  345. visible: true
  346. color:"red"
  347.  
  348.  
  349. TreeView {
  350. id: view
  351. anchors.fill: parent
  352. model: myModel
  353.  
  354. TableViewColumn {
  355. title: "title"
  356. role: "title"
  357. resizable: true
  358. }
  359.  
  360. TableViewColumn {
  361. title: "summary"
  362. role: "summary"
  363. resizable: true
  364. horizontalAlignment : Text.AlignRight
  365. width: 70
  366. }
  367.  
  368. }
  369.  
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement