File: GraphTableWidget.cpp

package info (click to toggle)
tulip 3.7.0dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 39,428 kB
  • sloc: cpp: 231,403; php: 11,023; python: 1,128; sh: 671; yacc: 522; makefile: 315; xml: 63; lex: 55
file content (179 lines) | stat: -rwxr-xr-x 5,425 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "GraphTableWidget.h"
#include <tulip/PropertyCreationDialog.h>
#include <tulip/CopyPropertyDialog.h>
#include <tulip/BooleanProperty.h>

#include <QtGui/QHeaderView>
#include <QtGui/QMenu>
#include <QtGui/QVBoxLayout>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QKeyEvent>

#include "GraphTableModel.h"
#include "TulipItemDelegate.h"
#include "TulipFilterProxyModel.h"

using namespace tlp;
using namespace std;
GraphTableWidget::GraphTableWidget(QWidget* parent):QTableView(parent),_graph(NULL),_type(NODE),_tulipTableModel(NULL),_filterModel(NULL) {
  horizontalHeader()->setMovable(true);
  setItemDelegate(new TulipItemDelegate(this));
}

void GraphTableWidget::setGraph(Graph* graph,ElementType element) {
  _graph = graph;
  _type = element;
  GraphTableModel* oldModel = _tulipTableModel;

  _tulipTableModel = new GraphTableModel(graph,element,this);

  if(_filterModel) {
    _filterModel->setGraphTableModel(_tulipTableModel);
    QTableView::setModel(_filterModel);
  }
  else {
    QTableView::setModel(_tulipTableModel);
  }

  if(oldModel) {
    oldModel->deleteLater();
  }
}


GraphTableWidget::SelectionStatus GraphTableWidget::selectionStatus(const QModelIndexList& elementsIndexes)const {
  BooleanProperty* selectionProperty = _graph->getProperty<BooleanProperty>("viewSelection");
  set<unsigned int> elements = indexListToIds(elementsIndexes);
  bool allSelected = true;
  bool allUnselected = true;

  for(set<unsigned int>::iterator it = elements.begin(); it != elements.end(); ++it) {
    if(_type == NODE) {
      if(selectionProperty->getNodeValue(node(*it))) {
        allUnselected = false;
      }
      else {
        allSelected = false;
      }
    }
    else {
      if(selectionProperty->getEdgeValue(edge(*it))) {
        allUnselected = false;
      }
      else {
        allSelected = false;

      }
    }
  }

  return allSelected?Selected:(allUnselected?Unselected:PartiallySelected);
}

set<unsigned int> GraphTableWidget::indexListToIds(const QModelIndexList& elementsIndexes)const {
  set<unsigned int> elements;

  for(QModelIndexList::const_iterator it = elementsIndexes.begin(); it != elementsIndexes.end(); ++it) {
    elements.insert(_tulipTableModel->idForIndex(*it));
  }

  return elements;
}


void GraphTableWidget::highlightElements(const std::set<unsigned int>& elementsToHighligh) {
  QItemSelectionModel *itemSelectionModel = new QItemSelectionModel(_tulipTableModel);

  for(int i = 0 ; i < _tulipTableModel->rowCount() ; ++i) {
    if(elementsToHighligh.find(_tulipTableModel->idForIndex(i))!=elementsToHighligh.end()) {
      itemSelectionModel->select(_tulipTableModel->index(i,0),QItemSelectionModel::Select| QItemSelectionModel::Rows);
    }
  }

  QItemSelectionModel *oldSelectionModel = selectionModel();
  setSelectionModel(itemSelectionModel);
  oldSelectionModel->deleteLater();
}

void GraphTableWidget::scrollToFirstOf(const QModelIndexList& indexes) {
  int xMin=INT_MAX;
  int yMin=INT_MAX;
  int rowMin = -1;
  int colMin = -1;

  for(QModelIndexList::const_iterator it = indexes.begin(); it != indexes.end(); ++it) {
    QRect indexRect = visualRect(*it);

    if(indexRect.left() < xMin) {
      xMin = indexRect.left();
      rowMin =( *it).row();
    }

    if(indexRect.top() < yMin) {
      yMin = indexRect.top();
      colMin = (*it).column();
    }
  }

  if(rowMin != -1 && colMin != -1) {
    QTableView::scrollTo(_tulipTableModel->index(rowMin,colMin),QAbstractItemView::PositionAtTop);
  }
}

void GraphTableWidget::highlightAndDisplayElements(const std::set<unsigned int>& elements) {
  highlightElements(elements);
  scrollToFirstOf(selectionModel()->selectedIndexes());
}
void GraphTableWidget::highlightAllElements() {
  selectionModel()->select(QItemSelection(model()->index(0,0),model()->index(model()->rowCount()-1,model()->columnCount()-1)),QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}

QModelIndexList GraphTableWidget::selectedRows(int columns)const {
  QModelIndexList modelSelected = selectionModel()->selectedRows(columns);
  QModelIndexList viewSelected;

  for (int i = 0; i < modelSelected.count(); ++i) {
    QModelIndex index = modelSelected.at(i);
    viewSelected.append(_filterModel == NULL?index:_filterModel->mapToSource(index));
  }

  return viewSelected;
}

void GraphTableWidget::setRowFilter(QRegExp regExp,bool showOnlySelectedElements,int columnFilter) {
  //Unset the filter
  if(regExp.isEmpty() && !showOnlySelectedElements) {
    unsetRowFilter();
  }
  else {
    if(_filterModel == NULL) {
      _filterModel = new TulipFilterProxyModel(this);
      _filterModel->setDynamicSortFilter(true);
      _filterModel->setGraphTableModel(_tulipTableModel);
      _filterModel->setShowOnlySelectedElement(showOnlySelectedElements);
      _filterModel->setFilterKeyColumn(columnFilter);
      _filterModel->setFilterRegExp(regExp);
      QTableView::setModel(_filterModel);
    }
    else {
      _filterModel->setShowOnlySelectedElement(showOnlySelectedElements);
      _filterModel->setFilterKeyColumn(columnFilter);
      _filterModel->setFilterRegExp(regExp);
    }
  }
}

void GraphTableWidget::unsetRowFilter() {
  if(_filterModel != NULL) {
    QTableView::setModel(_tulipTableModel);
    _filterModel->deleteLater();
    _filterModel = NULL;
  }
}

void GraphTableWidget::update() {
  _tulipTableModel->update();

  if(_filterModel)
    _filterModel->update();
}