boost::any GitModel::data(const WModelIndex& index, int role) const { if (!index.isValid()) return boost::any(); /* Only 3 data roles on column 0 data are supported: * - DisplayRole: the file name * - DecorationRole: an icon (folder or file) * - ContentsRole: the file contents */ if (index.column() == 0) { Git::Object object = getObject(index); if (role == DisplayRole) { if (object.type == Git::Tree) return object.name + '/'; else return object.name; } else if (role == DecorationRole) { if (object.type == Git::Blob) return static_cast<const char*>("resources/icons/git-blob.png"); else if (object.type == Git::Tree) return static_cast<const char*>("resources/icons/git-tree.png"); } else if (role == ContentsRole) { if (object.type == Git::Blob) return git_.catFile(object.id); } else if (role == FilePathRole) { return boost::any(); } } return boost::any(); }
void WSuggestionPopup::modelRowsInserted(const WModelIndex& parent, int start, int end) { if (filterLength_ != 0 && !filtering_) return; if (modelColumn_ >= model_->columnCount()) return; if (parent.isValid()) return; for (int i = start; i <= end; ++i) { WContainerWidget *line = new WContainerWidget(); content_->insertWidget(i, line); boost::any d = model_->data(i, modelColumn_); WText *value = new WText(asString(d), PlainText); boost::any d2 = model_->data(i, modelColumn_, UserRole); if (d2.empty()) d2 = d; line->addWidget(value); value->setAttributeValue("sug", asString(d2)); } }
int GitModel::rowCount(const WModelIndex& index) const { // we are looking for the git SHA1 id of a tree object (since only folders // may contain children). Git::ObjectId objectId; int treeId; if (index.isValid()) { // only column 0 items may contain children if (index.column() != 0) return 0; Git::Object o = getObject(index); if (o.type == Git::Tree) { objectId = o.id; treeId = getTreeId(index.internalId(), index.row()); } else // not a folder: no children return 0; } else { treeId = 0; // the index corresponds to the root object if (treeData_.empty()) // model not yet loaded ! return 0; else objectId = treeData_[0].treeObject(); } return treeData_[treeId].rowCount(); }
bool ArnModelW::setData(const WModelIndex& index, const boost::any& value, int role) { if (index.isValid() && role == Wt::EditRole) { ArnNode* node = nodeFromIndex( index); if (!node) return false; if (node->isFolder()) { if (node->_valueChild) { if (node->_setMap) { node->_valueChild->setValue( node->_setMap->key( toQString( boost::any_cast<WString>(value)))); } else { node->_valueChild->setValue( toQString( boost::any_cast<WString>(value))); } emitDataChangedI( index); } } else { if (value.type() == typeid(QVariant)) node->setValue( boost::any_cast<QVariant>(value)); else node->setValue( toQString( boost::any_cast<WString>(value))); emitDataChangedI( index); } return true; } return false; }
WModelIndex WIdentityProxyModel::mapFromSource(const WModelIndex &sourceIndex) const { if (!sourceIndex.isValid()) return WModelIndex(); return createIndex(sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer()); }
bool WModelIndex::operator< (const WModelIndex& i2) const { const WModelIndex& i1 = *this; if (!i1.isValid()) return i2.isValid(); else if (!i2.isValid()) return false; else if (i1 == i2) return false; else if (i1.model() != i2.model()) { LOG_ERROR("comparing indexes from different models are you?"); return false; } int i1Depth = i1.depth(); int i2Depth = i2.depth(); unsigned e = std::min(i1Depth, i2Depth); WModelIndex a1 = i1.ancestor(i1Depth - e); WModelIndex a2 = i2.ancestor(i2Depth - e); if (a1 == a2) return i1Depth < i2Depth; for (unsigned i = e; i > 0; --i) { WModelIndex p1 = a1.parent(); WModelIndex p2 = a2.parent(); if (p1 == p2) { if (a1.row() < a2.row()) return true; else if (a1.row() > a2.row()) return false; else if (a1.column() < a2.column()) return true; else return false; } a1 = p1; a2 = p2; } return false; // unreachable code }
virtual WModelIndex parent(const WModelIndex& index) const { if (!index.isValid() || index.internalId() == 0) { return WModelIndex(); // treeData_[0] is the tree root } else { const Tree& item = treeData_[index.internalId()]; return createIndex(item.index(), 0, item.parentId()); } }
WModelIndex WSortFilterProxyModel::parent(const WModelIndex& index) const { if (index.isValid()) { Item *parentItem = parentItemFromIndex(index); return mapFromSource(parentItem->sourceIndex_); } else return WModelIndex(); }
WModelIndex WStandardItemModel::parent(const WModelIndex& index) const { if (!index.isValid()) return index; WStandardItem *parent = static_cast<WStandardItem *>(index.internalPointer()); return indexFromItem(parent); }
bool WAbstractItemModel::setData(int row, int column, const boost::any& value, int role, const WModelIndex& parent) { WModelIndex i = index(row, column, parent); if (i.isValid()) return setData(i, value, role); else return false; }
WModelIndex ArnModelW::parent(const WModelIndex& index) const { // qDebug() << "### arnModelW parent: Check"; if (!index.isValid()) return index; ArnNode* parentNode = static_cast<ArnNode*>( index.internalPointer()); return indexFromNode( parentNode, 0); }
void WSortFilterProxyModel::sourceRowsAboutToBeRemoved (const WModelIndex& parent, int start, int end) { WModelIndex pparent = mapFromSource(parent); // distinguish between invalid parent being root item or being filtered out if (parent.isValid() && !pparent.isValid()) return; Item *item = itemFromIndex(pparent); for (int row = start; row <= end; ++row) { int mappedRow = item->sourceRowMap_[row]; if (mappedRow != -1) { beginRemoveRows(pparent, mappedRow, mappedRow); item->proxyRowMap_.erase(item->proxyRowMap_.begin() + mappedRow); rebuildSourceRowMap(item); // erase may have shifted some endRemoveRows(); } } }
WModelIndex WSortFilterProxyModel::mapToSource(const WModelIndex& proxyIndex) const { if (proxyIndex.isValid()) { Item *parentItem = parentItemFromIndex(proxyIndex); return sourceModel()->index(parentItem->proxyRowMap_[proxyIndex.row()], proxyIndex.column(), parentItem->sourceIndex_); } else return WModelIndex(); }
virtual cpp17::any data(const WModelIndex& index, ItemDataRole role = ItemDataRole::Display) const { if (!index.isValid()) return cpp17::any(); Git::Object object = getObject(index); switch (index.column()) { case 0: if (role == ItemDataRole::Display) { if (object.type == Git::Tree) return object.name + '/'; else return object.name; } else if (role == ItemDataRole::Decoration) { if (object.type == Git::Blob) return std::string("icons/git-blob.png"); else if (object.type == Git::Tree) return std::string("icons/git-tree.png"); } else if (role == ContentsRole) { if (object.type == Git::Blob) return git_.catFile(object.id); } break; case 1: if (role == ItemDataRole::Display) { if (object.type == Git::Tree) return std::string("Folder"); else { std::string suffix = getSuffix(object.name); if (suffix == "C" || suffix == "cpp") return std::string("C++ Source"); else if (suffix == "h" || (suffix == "" && !topLevel(index))) return std::string("C++ Header"); else if (suffix == "css") return std::string("CSS Stylesheet"); else if (suffix == "js") return std::string("JavaScript Source"); else if (suffix == "md") return std::string("Markdown"); else if (suffix == "png" || suffix == "gif") return std::string("Image"); else if (suffix == "txt") return std::string("Text"); else return cpp17::any(); } } } return cpp17::any(); }
WFlags<ItemFlag> ArnModelW::flags(const WModelIndex& index) const { if (!index.isValid()) { return WFlags<ItemFlag>(0); } if (index.column() == 1) { return WAbstractItemModel::flags( index) | Wt::ItemIsEditable; } else { return WAbstractItemModel::flags( index); } }
void WSuggestionPopup::modelRowsRemoved(const WModelIndex& parent, int start, int end) { if (parent.isValid()) return; for (int i = start; i <= end; ++i) if (start < impl_->count()) delete impl_->widget(start); else break; }
void WSortFilterProxyModel::sourceRowsRemoved(const WModelIndex& parent, int start, int end) { int count = end - start + 1; endShiftModelIndexes(parent, start, -count, mappedIndexes_); WModelIndex pparent = mapFromSource(parent); // distinguish between invalid parent being root item or being filtered out if (parent.isValid() && !pparent.isValid()) return; Item *item = itemFromIndex(pparent); // Shift existing entries in proxyRowMap, and remove entries in sourceRowMap for (unsigned i = 0; i < item->proxyRowMap_.size(); ++i) { if (item->proxyRowMap_[i] >= start) item->proxyRowMap_[i] -= count; } item->sourceRowMap_.erase(item->sourceRowMap_.begin() + start, item->sourceRowMap_.begin() + start + count); }
void WSortFilterProxyModel::sourceRowsInserted(const WModelIndex& parent, int start, int end) { startShiftModelIndexes(parent, end + 1, (end - start + 1), mappedIndexes_); if (inserting_) return; int count = end - start + 1; WModelIndex pparent = mapFromSource(parent); // distinguish between invalid parent being root item or being filtered out if (parent.isValid() && !pparent.isValid()) return; Item *item = itemFromIndex(pparent); // Shift existing entries in proxyRowMap, and reserve place in sourceRowMap // After this step, existing rows are okay again. for (unsigned i = 0; i < item->proxyRowMap_.size(); ++i) { if (item->proxyRowMap_[i] >= start) item->proxyRowMap_[i] += count; } item->sourceRowMap_.insert(item->sourceRowMap_.begin() + start, count, -1); if (!dynamic_) return; for (int row = start; row <= end; ++row) { int newMappedRow = mappedInsertionPoint(row, item); if (newMappedRow != -1) { beginInsertRows(pparent, newMappedRow, newMappedRow); item->proxyRowMap_.insert (item->proxyRowMap_.begin() + newMappedRow, row); rebuildSourceRowMap(item); // insertion may have shifted some endInsertRows(); } else item->sourceRowMap_[row] = -1; } }
virtual WModelIndex index(int row, int column, const WModelIndex& parent = WModelIndex()) const { int parentId; if (!parent.isValid()) parentId = 0; else { int grandParentId = parent.internalId(); parentId = getTreeId(grandParentId, parent.row()); } return createIndex(row, column, parentId); }
WAbstractItemModel::DataMap WAbstractItemModel::itemData(const WModelIndex& index) const { DataMap result; if (index.isValid()) { for (int i = 0; i <= BarBrushColorRole; ++i) result[i] = data(index, i); result[UserRole] = data(index, UserRole); } return result; }
void WSortFilterProxyModel::sourceRowsAboutToBeInserted (const WModelIndex& parent, int start, int end) { if (inserting_) return; /* * Make sure the item starts in a known state, otherwise if the item * does not yet have a sourceRowMap, it will be created taking into * account the already updated number of source rows, in * sourceRowsInserted(). * * BTW. one might wonder if a user of the proxy model is interested * at all in changes to a node which he has not yet 'opened' ..., but * strictly spoken we are obliged to propagate these changes ! */ WModelIndex pparent = mapFromSource(parent); // distinguish between invalid parent being root item or being filtered out if (parent.isValid() && !pparent.isValid()) return; itemFromIndex(pparent); }
bool WModelIndex::UnorderedLess::operator() (const WModelIndex& i1, const WModelIndex& i2) const { if (!i1.isValid()) return i2.isValid(); else if (!i2.isValid()) return false; else if (i1 == i2) return false; else if (i1.model() != i2.model()) { LOG_ERROR("comparing indexes from different models are you?"); return false; } else if (i1.row() < i2.row()) return true; else if (i1.row() > i2.row()) return false; else if (i1.column() < i2.column()) return true; else if (i1.column() > i2.column()) return false; else return i1.internalId_ < i2.internalId_; }
WModelIndexSet WModelIndex::decodeFromRawIndexes(const WModelIndexSet& encodedIndexes) { WModelIndexSet result; for (WModelIndexSet::const_iterator i = encodedIndexes.begin(); i != encodedIndexes.end(); ++i) { WModelIndex n = i->decodeFromRawIndex(); if (n.isValid()) result.insert(n); } return result; }
WModelIndex GitModel::parent(const WModelIndex& index) const { // treeData_[0] indicates the top-level parent. if (!index.isValid() || index.internalId() == 0) return WModelIndex(); else { // get the item that corresponds to the parent ... const Tree& item = treeData_[index.internalId()]; // ... and construct that identifies the parent: // row = child index in the grand parent // internalId = id of the grand parent return createIndex(item.index(), 0, item.parentId()); } }
virtual int rowCount(const WModelIndex& parent = WModelIndex()) const { int treeId; if (parent.isValid()) { if (parent.column() != 0) return 0; Git::Object o = getObject(parent); if (o.type == Git::Tree) { // is a folder treeId = getTreeId(parent.internalId(), parent.row()); } else // is a file return 0; } else { treeId = 0; } return treeData_[treeId].rowCount(); }
WModelIndex WSortFilterProxyModel::mapFromSource(const WModelIndex& sourceIndex) const { if (sourceIndex.isValid()) { WModelIndex sourceParent = sourceIndex.parent(); Item *item = itemFromSourceIndex(sourceParent); int row = item->sourceRowMap_[sourceIndex.row()]; if (row != -1) return createIndex(row, sourceIndex.column(), static_cast<void *>(item)); else return WModelIndex(); } else return WModelIndex(); }
WModelIndex GitModel::index(int row, int column, const WModelIndex& parent) const { int parentId; // the top-level parent has id=0. if (!parent.isValid()) parentId = 0; else { // the internal id of the parent identifies the grand parent int grandParentId = parent.internalId(); // lookup the parent id for the parent himself, based on grand parent // and child-index (=row) within the grand parent parentId = getTreeId(grandParentId, parent.row()); } return createIndex(row, column, parentId); }
void WSuggestionPopup::modelRowsInserted(const WModelIndex& parent, int start, int end) { if (filterLength_ != 0 && !filtering_) return; if (modelColumn_ >= model_->columnCount()) return; if (parent.isValid()) return; for (int i = start; i <= end; ++i) { WContainerWidget *line = new WContainerWidget(); impl_->insertWidget(i, line); WModelIndex index = model_->index(i, modelColumn_); boost::any d = index.data(); TextFormat format = index.flags() & ItemIsXHTMLText ? XHTMLText : PlainText; WAnchor *anchor = new WAnchor(line); WText *value = new WText(asString(d), format, anchor); boost::any d2 = index.data(UserRole); if (d2.empty()) d2 = d; value->setAttributeValue("sug", asString(d2)); boost::any styleclass = index.data(StyleClassRole); if (!styleclass.empty()) { value->setAttributeValue("class", asString(styleclass)); } } }
void WAbstractProxyModel::shiftModelIndexes(const WModelIndex& sourceParent, int start, int count, ItemMap& items) { /* * We must shift all indexes within sourceParent >= start with count * and delete items when count < 0. */ std::vector<BaseItem *> shifted; std::vector<BaseItem *> erased; for (ItemMap::iterator it = items.lower_bound(sourceModel()->index(start, 0, sourceParent)); it != items.end();) { #ifndef WT_TARGET_JAVA ItemMap::iterator n = it; ++n; #endif WModelIndex i = it->first; if (i.isValid()) { WModelIndex p = i.parent(); if (p != sourceParent && !WModelIndex::isAncestor(p, sourceParent)) break; if (p == sourceParent) { shifted.push_back(it->second); } else if (count < 0) { // delete indexes that are about to be deleted, if they are within // the range of deleted indexes do { if (p.parent() == sourceParent && p.row() >= start && p.row() < start - count) { erased.push_back(it->second); break; } else p = p.parent(); } while (p != sourceParent); } } #ifndef WT_TARGET_JAVA it = n; #endif } for (unsigned i = 0; i < erased.size(); ++i) { items.erase(erased[i]->sourceIndex_); delete erased[i]; } for (unsigned i = 0; i < shifted.size(); ++i) { BaseItem *item = shifted[i]; items.erase(item->sourceIndex_); if (item->sourceIndex_.row() + count >= start) { item->sourceIndex_ = sourceModel()->index (item->sourceIndex_.row() + count, item->sourceIndex_.column(), sourceParent); } else { delete item; shifted[i] = 0; } } for (unsigned i = 0; i < shifted.size(); ++i) { if (shifted[i]) items[shifted[i]->sourceIndex_] = shifted[i]; } }
void WSortFilterProxyModel::sourceDataChanged(const WModelIndex& topLeft, const WModelIndex& bottomRight) { if (!topLeft.isValid() || !bottomRight.isValid()) return; bool refilter = dynamic_ && (filterKeyColumn_ >= topLeft.column() && filterKeyColumn_ <= bottomRight.column()); bool resort = dynamic_ && (sortKeyColumn_ >= topLeft.column() && sortKeyColumn_ <= bottomRight.column()); WModelIndex parent = mapFromSource(topLeft.parent()); // distinguish between invalid parent being root item or being filtered out if (topLeft.parent().isValid() && !parent.isValid()) return; Item *item = itemFromIndex(parent); for (int row = topLeft.row(); row <= bottomRight.row(); ++row) { int oldMappedRow = item->sourceRowMap_[row]; bool propagateDataChange = oldMappedRow != -1; if (refilter || resort) { // Determine new insertion point: erase it temporarily for this if (oldMappedRow != -1) item->proxyRowMap_.erase(item->proxyRowMap_.begin() + oldMappedRow); int newMappedRow = mappedInsertionPoint(row, item); if (oldMappedRow != -1) item->proxyRowMap_.insert(item->proxyRowMap_.begin() + oldMappedRow, row); if (newMappedRow != oldMappedRow) { if (oldMappedRow != -1) { beginRemoveRows(parent, oldMappedRow, oldMappedRow); item->proxyRowMap_.erase (item->proxyRowMap_.begin() + oldMappedRow); rebuildSourceRowMap(item); endRemoveRows(); } if (newMappedRow != -1) { beginInsertRows(parent, newMappedRow, newMappedRow); item->proxyRowMap_.insert (item->proxyRowMap_.begin() + newMappedRow, row); rebuildSourceRowMap(item); endInsertRows(); } propagateDataChange = false; } } if (propagateDataChange) { WModelIndex l = sourceModel()->index(row, topLeft.column(), topLeft.parent()); WModelIndex r = sourceModel()->index(row, bottomRight.column(), topLeft.parent()); dataChanged().emit(mapFromSource(l), mapFromSource(r)); } } }