File: TulipFilterProxyModel.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 (190 lines) | stat: -rwxr-xr-x 5,385 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
180
181
182
183
184
185
186
187
188
189
190
#include "TulipFilterProxyModel.h"
#include "GraphTableModel.h"

#include <tulip/BooleanProperty.h>
#include <tulip/Graph.h>
using namespace tlp;
using namespace std;
TulipFilterProxyModel::TulipFilterProxyModel(QObject* parent):QSortFilterProxyModel(parent),_tableModel(NULL),_showOnlyVisibleElements(false),_selectionProperty(NULL),_reloadSelectionProperty(false),_needToReload(false) {
}

void TulipFilterProxyModel::sort(int column, Qt::SortOrder order) {
  //Disable sorting
  if(_tableModel) {
    _tableModel->sort(column,order);
  }
}

void TulipFilterProxyModel::setSourceModel ( QAbstractItemModel * sourceModel ) {
  QSortFilterProxyModel::setSourceModel(sourceModel);
}

void TulipFilterProxyModel::setGraphTableModel(GraphTableModel* tableModel) {
  _tableModel = tableModel;
  setSourceModel(tableModel);
}

void TulipFilterProxyModel::setShowOnlySelectedElement(bool showOnlySelectedElements) {
  if(_showOnlyVisibleElements != showOnlySelectedElements) {
    //Stop listening viewSelection
    if(_showOnlyVisibleElements) {
      _selectionProperty->removeListener(this);
      _tableModel->graph()->removeListener(this);
    }
    else {
      //Begin listening view selection property
      if(_tableModel->graph()->existProperty("viewSelection")) {
        _selectionProperty = _tableModel->graph()->getProperty<BooleanProperty>("viewSelection");
        _selectionProperty->addListener(const_cast<TulipFilterProxyModel*>(this));
      }

      _tableModel->graph()->addListener(const_cast<TulipFilterProxyModel*>(this));
    }

    _showOnlyVisibleElements = showOnlySelectedElements;
  }

}

bool TulipFilterProxyModel::filterAcceptsRow ( int source_row, const QModelIndex &) const {
  //Avoid another invalidate filter when calling update
  _needToReload = false;

  //Need to reload selection property
  if (_reloadSelectionProperty) {
    assert(_selectionProperty == NULL);

    if(_tableModel->graph()->existProperty("viewSelection")) {
      _selectionProperty = _tableModel->graph()->getProperty<BooleanProperty>("viewSelection");
      _selectionProperty->addListener(const_cast<TulipFilterProxyModel*>(this));
      _tableModel->graph()->addListener(const_cast<TulipFilterProxyModel*>(this));
    }

    _reloadSelectionProperty = false;
  }

  unsigned int id =_tableModel->idForIndex(source_row);
  bool display = true;

  if(_showOnlyVisibleElements && _selectionProperty!=NULL) {
    display =  _tableModel->elementType() == NODE ? _selectionProperty->getNodeValue(node(id)):_selectionProperty->getEdgeValue(edge(id));
  }

  bool match = false;
  QRegExp regExp= filterRegExp();

  if(regExp.isEmpty() ) {
    match = true;
  }
  else {
    //Search in a specific column
    if(filterKeyColumn()!= -1) {
      match |= regExp.exactMatch(_tableModel->data(_tableModel->index(source_row,filterKeyColumn())).toString());
    }
    else {
      //Search in all columns
      bool foundPattern = false;

      for(int j =0 ; j < _tableModel->columnCount(); ++j) {
        //Filter only visible columns.
        //                        if(!ui->tableView->isColumnHidden(j)){
        foundPattern = regExp.exactMatch(_tableModel->data(_tableModel->index(source_row,j)).toString());

        //If we find the pattern stop the search
        if(foundPattern)
          break;

        //                        }
      }

      match |= foundPattern;
    }
  }

  return (display && match);
}

bool TulipFilterProxyModel::filterAcceptsColumn ( int, const QModelIndex &) const {
  return true;
}

void TulipFilterProxyModel::treatEvent(const Event &ev) {
  const PropertyEvent* propEvt = dynamic_cast<const PropertyEvent*>(&ev);

  if (propEvt) {
    switch(propEvt->getType()) {
    case PropertyEvent::TLP_AFTER_SET_NODE_VALUE:

      if(_tableModel->elementType() == NODE) {
        _needToReload = true;
      }

      break;

    case PropertyEvent::TLP_AFTER_SET_ALL_NODE_VALUE:

      if(_tableModel->elementType() == NODE) {
        _needToReload = true;
      }

      break;

    case PropertyEvent::TLP_AFTER_SET_ALL_EDGE_VALUE:

      if(_tableModel->elementType() == EDGE) {
        _needToReload = true;
      }

      break;

    case PropertyEvent::TLP_AFTER_SET_EDGE_VALUE:

      if(_tableModel->elementType() == EDGE) {
        _needToReload = true;
      }

      break;

    default:
      break;
    }
  }
  else {
    const GraphEvent* graphEvt = dynamic_cast<const GraphEvent*>(&ev);

    if(graphEvt) {
      switch(graphEvt->getType()) {
      case GraphEvent::TLP_ADD_LOCAL_PROPERTY:
      case GraphEvent::TLP_ADD_INHERITED_PROPERTY:
      case GraphEvent::TLP_BEFORE_DEL_INHERITED_PROPERTY:
      case GraphEvent::TLP_BEFORE_DEL_LOCAL_PROPERTY: {
        string propertyName = graphEvt->getPropertyName();

        //A new selection property was created or the current one will be destructed.
        if(propertyName.compare("viewSelection")==0) {
          _tableModel->graph()->removeListener(this);

          if(_selectionProperty != NULL) {
            _selectionProperty->removeListener(this);
            _selectionProperty = NULL;
          }

          _reloadSelectionProperty = true;
          _needToReload = true;
        }
      }
      break;

      default:
        break;
      }
    }
  }
}

void TulipFilterProxyModel::update() {
  if(_needToReload) {
    invalidateFilter();
    _needToReload = false;
  }
}