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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
#ifndef GRAPHTABLEMODEL_H
#define GRAPHTABLEMODEL_H
#include <QtCore/QAbstractItemModel>
#include <tulip/Graph.h>
#include <deque>
struct GraphTableModelIndex {
GraphTableModelIndex(unsigned int element,tlp::PropertyInterface* property);
bool isValid()const {
return _element!=UINT_MAX && _property != NULL;
}
unsigned int element()const {
return _element;
}
tlp::PropertyInterface* property()const {
return _property;
}
private:
unsigned int _element;
tlp::PropertyInterface* _property;
};
/**
* @brief The GraphTableModel class allows to visualize and edit a Tulip Graph object with Qt's model/view framework. You can only use this model only with TulipItemDelegate class instead you cannot see double values and histogram.
**/
class GraphTableModel : public QAbstractTableModel, public tlp::Observable , public tlp::GraphObserver, public tlp::PropertyObserver {
Q_OBJECT
public:
GraphTableModel(tlp::Graph* graph,tlp::ElementType elementType=tlp::NODE,QObject* parent = 0);
//Get set parameters
tlp::Graph* graph()const {
return _graph;
}
/**
* @brief Change the displayed graph. Display the same type of elements than before (by default display nodes).
**/
void setGraph(tlp::Graph* newGraph);
tlp::ElementType elementType()const {
return _elementType;
}
/**
* @brief Change the kind of displayed elements.
**/
void setElementType(tlp::ElementType type);
Qt::Orientation orientation()const {
return _orientation;
}
void setOrientation(Qt::Orientation orientation);
//Data access methods
/**
* @brief Convinience function. See the element(int row, int column,const QModelIndex& parent = QModelIndex()) function for more informations.
*/
inline GraphTableModelIndex element(const QModelIndex& modelIndex)const {
return element(modelIndex.row(),modelIndex.column(),modelIndex.parent());
}
/**
* @brief Return a structure containing the element and the property for a given position in the model.
**/
inline GraphTableModelIndex element(int row, int column,const QModelIndex& parent = QModelIndex())const {
return _orientation == Qt::Vertical?GraphTableModelIndex(idForIndex(row,parent),propertyForIndex(column,parent)):GraphTableModelIndex(idForIndex(column,parent),propertyForIndex(row,parent));
}
/**
* @brief Convinience function to get the id for an index call the idForIndex function with the right index in function of elements orientation in the model.
**/
inline unsigned int idForIndex(const QModelIndex& index)const {
return idForIndex(_orientation==Qt::Vertical?index.row():index.column());
}
/**
* @brief Get the id at the given index. If the index is not valid return UINT_MAX.
**/
virtual unsigned int idForIndex(int index,const QModelIndex& parent = QModelIndex()) const;
/**
* @brief Retrieve the indexes of the given ids. If an id is not in the table it will be ignored.
*
**/
virtual QList<int> indexesForIds(const std::set<unsigned int>& ids)const;
/**
* @brief Convinience function to get the property for an index call the propertyForIndex function with the right index in function of properties orientation in the model.
**/
tlp::PropertyInterface* propertyForIndex(const QModelIndex& index ) const {
return propertyForIndex(_orientation==Qt::Horizontal?index.column():index.row());
}
/**
* @brief Get the id property the given index. If the index is not valid or if the property is marked for deletion return NULL.
**/
virtual tlp::PropertyInterface* propertyForIndex(int index,const QModelIndex& parent = QModelIndex()) const;
/**
* @brief Retrieve the indexes of the given properties. If a property is not in the table it will be ignored.
*
**/
virtual QList<int> indexesForProperties(const std::set<tlp::PropertyInterface*>& properties)const;
/**
* @brief Compute the area for the list of elements. Return top left and bottom right bounds.
**/
std::pair<QModelIndex,QModelIndex> computeElementsArea(const std::set<unsigned int>& elementsIds)const;
/**
* @brief Compute the area for the list of elements. Return top left and bottom right bounds.
**/
std::pair<QModelIndex,QModelIndex> computePropertiesArea(const std::set<tlp::PropertyInterface*>& properties)const;
/**
* @brief Reimplementation of QAbstractTableModel::columnCount(). Return the size of the property table if the orientation of the model is equal to Qt::Vertical else return the size of the elements table.
**/
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;
/**
* @brief Reimplementation of QAbstractTableModel::rowCount(). Return the size of the elements table if the orientation of the model is equal to Qt::Vertical else return the size of the properties table.
**/
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
/**
* @brief Convinience function.
**/
bool setAllElementsData(const QModelIndex& index,const QVariant & value, int role = Qt::EditRole) {
return setAllElementsData(_orientation==Qt::Horizontal?index.row():index.column(),value,role);
}
/**
* @brief Set the given value to all the elements in the model. Call the setAllNodeValue or setAllEdgeValue in function of the kind of elements managed by the model.
**/
virtual bool setAllElementsData(unsigned int propertyIndex,const QVariant & value, int role = Qt::EditRole);
virtual bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
Qt::ItemFlags flags ( const QModelIndex & index ) const;
//Data modification methods
void sort( int column, Qt::SortOrder order = Qt::AscendingOrder );
//Data destruction methods
bool removeColumns( int column, int count, const QModelIndex & parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex &parent= QModelIndex());
bool removeColumns( const QModelIndexList& toRemove);
bool removeRows( const QModelIndexList& toRemove);
//Observable management
void addNode(tlp::Graph *, const tlp::node );
void addEdge(tlp::Graph *, const tlp::edge );
void delNode(tlp::Graph *,const tlp::node );
void delEdge(tlp::Graph *,const tlp::edge );
void addLocalProperty(tlp::Graph*, const std::string&);
void beforeDelLocalProperty(tlp::Graph *, const std::string &);
void addInheritedProperty(tlp::Graph*, const std::string&);
void beforeDelInheritedProperty(tlp::Graph *, const std::string &);
void afterSetNodeValue(tlp::PropertyInterface*, const tlp::node);
void afterSetEdgeValue(tlp::PropertyInterface*, const tlp::edge);
void afterSetAllNodeValue(tlp::PropertyInterface*);
void afterSetAllEdgeValue(tlp::PropertyInterface*);
/**
* @brief Apply all the modifications occured in the graph on the model.
**/
void update();
protected:
/**
* @brief Fill the idTable.
**/
virtual void updateElementsTable();
/**
* @brief Fill the propertyTable. For each property in the graph call the useProperty function to know if we add the property in the table.
**/
virtual void updatePropertyTable();
/**
* @brief Return if the property must be integrated in the property table. This function is called before each insertion in the property table.
**/
virtual bool useProperty(tlp::PropertyInterface* property) const;
/**
* @brief Sort elements according to a given property values.
**/
virtual void sortElements(tlp::PropertyInterface* property, Qt::SortOrder order);
virtual bool removeElements(int first,int last,const QModelIndex& parent = QModelIndex());
virtual bool removeProperties(int first,int last,const QModelIndex& parent = QModelIndex());
private:
template<typename T>
void removeFromVector(const std::set<T>& objects,std::vector<T>& vect,TLP_HASH_MAP<T,int>& objToIndexes,bool deleteRows) {
//Sort from the greatest to the smallest to ensure index are not invalidate during the destruction process
std::set<int, std::greater<int> > indexes;
for( typename std::set<T>::const_iterator it = objects.begin(); it != objects.end(); ++it) {
assert(objToIndexes.find(*it)!=objToIndexes.end());
indexes.insert(objToIndexes[*it]);
objToIndexes.erase(*it);
}
while(!indexes.empty()) {
//Compute the greatest range of successive indexes.
std::set<int, std::greater<int> >::const_iterator from = indexes.begin();
std::set<int, std::greater<int> >::const_iterator current = from;
std::set<int, std::greater<int> >::const_iterator to=from;
++to;
//If the indexes are successive delete them at the same time.
while(to != indexes.end() && *to == (*current)-1 ) {
current = to;
++to;
}
//Range to destruct
int fromIndex = *current;
int toIndex = *from;
//Remove the range from the index list
indexes.erase(from,to);
if(deleteRows) {
beginRemoveRows(QModelIndex(),fromIndex,toIndex);
}
else {
beginRemoveColumns(QModelIndex(),fromIndex,toIndex);
}
//Erase data
vect.erase(vect.begin()+fromIndex,vect.begin()+toIndex+1);
//Update objToIndex map
for(unsigned int i = fromIndex ; i < vect.size() ; ++i) {
objToIndexes[vect[i]] = static_cast<int>(i);
}
if(deleteRows) {
endRemoveRows();
}
else {
endRemoveColumns();
}
}
}
template<typename T, class Compare>
void addToVector(const std::set<T>& objects,std::vector<T>& vect,TLP_HASH_MAP<T,int>& objToIndex,bool addRows, Compare* comp) {
//Reserve size
if(vect.capacity() <= vect.size() + objects.size()) {
vect.reserve(vect.size() + objects.size());
}
if(comp == NULL) {
//If there is no comparator append the elements to add at the end of the vector
if(addRows) {
beginInsertRows(QModelIndex(),vect.size(),vect.size()+objects.size()-1);
}
else {
beginInsertColumns(QModelIndex(),vect.size(),vect.size()+objects.size()-1);
}
for(typename std::set<T>::const_iterator it = objects.begin(); it != objects.end() ; ++it) {
unsigned int i = vect.size();
objToIndex[*it]=i;
vect.push_back((*it));
}
if(addRows) {
endInsertRows();
}
else {
endInsertColumns();
}
}
else {
//Order objects
std::vector<T> sortedObjects(objects.begin(),objects.end());
std::sort(sortedObjects.begin(),sortedObjects.end(),*comp);
unsigned int current = 0;
typename std::vector<T>::iterator it;
for( it = vect.begin() , current = 0 ; it != vect.end() && !sortedObjects.empty() ; ++it, ++current) {
//First element greater than the element to insert
if(!(*comp)(*it,sortedObjects.front())) {
//Compute the elements to insert in the vector
typename std::vector<T>::iterator to = sortedObjects.begin()+1;
unsigned int insertedElementsCount = 1;
//While there is more elements to insert at this position
while(to != sortedObjects.end() && !(*comp)(*it,*to)) {
++to;
++insertedElementsCount;
}
//Notify Qt model
if(addRows) {
beginInsertRows(QModelIndex(),current,current+insertedElementsCount-1);
}
else {
beginInsertColumns(QModelIndex(),current,current+insertedElementsCount-1);
}
//Update data
vect.insert(it,sortedObjects.begin(),to);
//Update data index
for(unsigned int i = current ; i < vect.size() ; ++i) {
objToIndex[vect[i]]=i;
}
if(addRows) {
endInsertRows();
}
else {
endInsertColumns();
}
//Erase added elements.
sortedObjects.erase(sortedObjects.begin(),to);
it = vect.begin()+current;
}
}
//If there is some elements to add insert them at the end of the data.
if(!sortedObjects.empty()) {
if(addRows) {
beginInsertRows(QModelIndex(),vect.size(),vect.size()+sortedObjects.size()-1);
}
else {
beginInsertColumns(QModelIndex(),vect.size(),vect.size()+sortedObjects.size()-1);
}
size_t from = vect.size();
//Update data
vect.insert(vect.end(),sortedObjects.begin(),sortedObjects.end());
//Update data index
for(unsigned int i = from ; i < vect.size() ; ++i) {
objToIndex[vect[i]]=i;
}
if(addRows) {
endInsertRows();
}
else {
endInsertColumns();
}
}
}
}
template<typename T>
void updateIndexMap(const std::vector<T>& table,TLP_HASH_MAP<T,int>& indexMap) {
indexMap.clear();
for(size_t i = 0 ; i < table.size(); ++i) {
indexMap[table[i]]=i;
}
}
template<typename T>
std::pair<unsigned int,unsigned int> computeArea(const std::set<T>& elementsToFind,const std::vector<T>& elements,const TLP_HASH_MAP<T,int>& objToIndex)const {
//Init from and to values
int first=elements.size()-1,last=0;
for(typename std::set<T>::const_iterator it = elementsToFind.begin(); it != elementsToFind.end() ; ++it) {
assert(objToIndex.find(*it) != objToIndex.end());
int index = (objToIndex.find(*it))->second;
first = std::min(first,index);
last = std::max(last,index);
}
first = std::max(first,0);
last = std::min(last,static_cast<int>(elements.size()-1));
return std::make_pair(first,last);
}
tlp::Graph* _graph;
tlp::ElementType _elementType;
Qt::Orientation _orientation;
std::vector<unsigned int> _idTable;
TLP_HASH_MAP<unsigned int,int> _idToIndex;
std::vector<tlp::PropertyInterface*> _propertiesTable;
TLP_HASH_MAP<tlp::PropertyInterface*,int> _propertyToIndex;
//Sorting var
tlp::PropertyInterface* _sortingProperty;
Qt::SortOrder _order;
//Graph update buffers
std::set<tlp::PropertyInterface*> _propertiesToDelete;
std::set<tlp::PropertyInterface*> _propertiesToAdd;
std::set<unsigned int> _idsToDelete;
std::set<unsigned int> _idsToAdd;
std::set<tlp::PropertyInterface*> _propertiesUpdated;
std::vector<GraphTableModelIndex> _dataUpdated;
};
#endif // GRAPHTABLEMODEL_H
|