tuchulcha  0.10.1
Graphical Workflow Configuration Editor
NodeTreeView.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011 Jonathan Wuest
2 
3  This file is part of Tuchulcha.
4 
5  Tuchulcha is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Tuchulcha is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with Tuchulcha. If not, see <http://www.gnu.org/licenses/>.
17 */
24 #include "NodeTreeView.h"
25 #include "FileManager.h"
26 #include "MetaData.h"
27 #include "ui_NodeTreeView.h"
28 #include <QTreeView>
29 #include <QStandardItemModel>
30 #include <QSortFilterProxyModel>
31 #include <QVBoxLayout>
32 #include <QDebug>
33 
35 class MySortFilterProxy : public QSortFilterProxyModel {
36 public:
38  MySortFilterProxy(QObject* pp=0) : QSortFilterProxyModel(pp) {}
39 
40 protected:
42  virtual bool filterAcceptsRow(
43  int source_row, const QModelIndex& source_parent) const {
44  return !source_parent.isValid() ||
45  QSortFilterProxyModel::filterAcceptsRow(source_row,source_parent);
46  }
47 };
48 
50 class NoEmptyGroupsProxy : public QSortFilterProxyModel {
51 public:
53  NoEmptyGroupsProxy(QObject* pp=0) : QSortFilterProxyModel(pp) {}
54 
55 protected:
57  virtual bool filterAcceptsRow(
58  int source_row, const QModelIndex& source_parent) const {
59  QModelIndex cur = sourceModel()->index(source_row,0,source_parent);
60  return source_parent.isValid() || sourceModel()->hasChildren(cur);
61  }
62 };
63 
64 NodeTreeView::NodeTreeView(QWidget* pp) : QWidget(pp) {
65  _ui = new Ui::NodeTreeView;
66  _ui->setupUi(this);
67  _model = new QStandardItemModel(1,1,this);
68  _filter = new MySortFilterProxy(this);
70  _filter->setSourceModel(_model);
71  _hideEmtptyGroups->setSourceModel(_filter);
72  _filter->setFilterCaseSensitivity(Qt::CaseInsensitive);
73  _filter->setSortCaseSensitivity(Qt::CaseInsensitive);
74  _ui->treeView->setModel(_hideEmtptyGroups);
75  reload();
76 }
77 
78 NodeTreeView::~NodeTreeView() {
79  delete _ui;
80 }
81 
83  // reset content
84  _model->clear();
85  QStandardItem* root = new QStandardItem(tr("All Modules"));
86  root->setDragEnabled(false);
87  root->setEditable(false);
88  root->setSelectable(false);
89  int rootIndex = 0 ;
90  _model->setItem(rootIndex++,0,root);
91 
92  QStandardItem* untaggedRoot = new QStandardItem(tr("Untagged"));
93  untaggedRoot->setDragEnabled(false);
94  untaggedRoot->setEditable(false);
95  untaggedRoot->setSelectable(false);
96  _model->setItem(rootIndex++,0,untaggedRoot);
97 
98  QStringList labels;
99  labels << tr("Names");
100  _model->setHorizontalHeaderLabels(labels);
101 
102  QMap<QString,QStandardItem*> allTags ;
103 
104  // load modules
105  MetaData md(FileManager::instance().classesFile());
106  const QStringList& classes = md.getClasses();
107  QStringListIterator classIter(classes);
108  while (classIter.hasNext()) {
109  const QString cur = classIter.next();
110  // new node to be added
111  QStandardItem* node = new QStandardItem(cur);
112  node->setEditable(false);
113  node->setDragEnabled(true);
114  root->appendRow(node);
115 
116  // get list of tags for this module
117  QStringList tags = md.getTags(cur) ;
118 
119  //put module in the untagged list if it has no tags
120  if (tags.isEmpty()) {
121  QStandardItem* cnode = new QStandardItem(cur);
122  cnode->setEditable(false);
123  cnode->setDragEnabled(true);
124  untaggedRoot->appendRow(cnode);
125  }
126 
127  QStringListIterator tagIter(tags);
128  while (tagIter.hasNext()) {
129  const QString tag = tagIter.next();
130  int subIndex = 0 ; // index of current "/"
131  QString parentTag = "" ;
132  // split tag at each "/" and create subnodes
133  do {
134  QStandardItem* cnode = new QStandardItem(cur);
135  cnode->setEditable(false);
136  cnode->setDragEnabled(true);
137 
138  subIndex = tag.indexOf("/",subIndex+1) ;
139  QString subtag = tag.mid(0,subIndex) ;
140 
141  if(!allTags.contains(subtag))
142  {
143  //current innermost tag
144  QString subsubtag = subtag.mid(subtag.lastIndexOf("/")+1,-1) ;
145  QStandardItem* newRoot = new QStandardItem(subsubtag) ;
146  newRoot->setDragEnabled(false);
147  newRoot->setEditable(false);
148  newRoot->setSelectable(false);
149  if(parentTag.isEmpty()) //handle root nodes differently
150  _model->setItem(rootIndex++,0,newRoot);
151  else
152  allTags[parentTag]->appendRow(newRoot) ;
153  allTags.insert(subtag,newRoot) ;
154  }
155  parentTag = subtag ;
156  allTags[subtag]->appendRow(cnode) ;
157  } while(subIndex != -1) ;
158  }
159  }
160  _model->sort(0,Qt::AscendingOrder);
161  _filter->invalidate();
162  _hideEmtptyGroups->invalidate();
163 
164  // update view
165  _ui->treeView->sortByColumn(0,Qt::AscendingOrder);
166  _ui->treeView->setExpanded(_ui->treeView->model()->index(0,0), true);
167 }
168 
169 void NodeTreeView::on_treeView_clicked(const QModelIndex& cur) {
170  if (cur.parent().isValid()) {
171  emit showClassDoc(cur.model()->data(cur).toString());
172  }
173 }
174 
175 void NodeTreeView::on_editFilter_textChanged(const QString& text) {
176  _filter->setFilterWildcard(text);
177  _hideEmtptyGroups->invalidate();
178  _ui->treeView->sortByColumn(0,Qt::AscendingOrder);
179  _ui->treeView->setExpanded(_ui->treeView->model()->index(0,0), true);
180  _ui->treeView->scrollToTop();
181  _ui->editFilter->setFrame(_ui->treeView->model()->rowCount());
182 }
183 
184 void NodeTreeView::on_treeView_doubleClicked(const QModelIndex& idx) {
185  if (idx.parent().isValid()) {
186  emit addNode(idx.model()->data(idx).toString());
187  }
188 }
NodeTreeView(QWidget *parent=0)
default constructor
NoEmptyGroupsProxy(QObject *pp=0)
default constructor
Declaration of class FileManager.
void showClassDoc(const QString &className)
show documentation page
static const FileManager & instance()
get a reference to the file Manager instance
Definition: FileManager.cpp:70
Implementation of class ParameterFileModel.
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
hide top-level entities without valid children
Handle metadata management for ParameterFileModel classes.
Definition: MetaData.h:31
filer proxy to hide top-level entities without children
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
include top-level entities in the model
Declaration of class NodeTreeView.
filter proxy subclass keeping all top-level entities
void on_treeView_doubleClicked(const QModelIndex &)
handle double click events
QStandardItemModel * _model
the model of the view
Definition: NodeTreeView.h:77
void addNode(QString type)
initiate adding a new instance
Ui::NodeTreeView * _ui
designer gui
Definition: NodeTreeView.h:75
void on_treeView_clicked(const QModelIndex &index)
handle documentation display on selection change
QStringList getTags(QString className) const
Get the tags of a given object.
Definition: MetaData.cpp:137
MySortFilterProxy(QObject *pp=0)
default constructor
void reload()
loads nodes/modules from the classesFile
void on_editFilter_textChanged(const QString &text)
handle filter changes
QStringList getClasses() const
get classes contained in this metadata file
Definition: MetaData.cpp:55
QSortFilterProxyModel * _hideEmtptyGroups
hide emtpy groups proxy
Definition: NodeTreeView.h:81
QSortFilterProxyModel * _filter
filter wildcard proxy
Definition: NodeTreeView.h:79