bool CellModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) { Cell* srcCell; Cell* desCell; bool result = true; if (!sourceParent.isValid()) srcCell = topCell; else srcCell = getCellByIndex(sourceParent); if (!destinationParent.isValid()) desCell = topCell; else desCell = getCellByIndex(destinationParent); if (sourceRow+count-1 > srcCell->childCount() || destinationChild > desCell->childCount()) return false; beginMoveRows(sourceParent,sourceRow,sourceRow+count-1,destinationParent,destinationChild); for (int i=0; i<count; i++){ result &= desCell->insertChildren(destinationChild,1,srcCell->getChild(sourceRow)); result &= srcCell->removeChildren(sourceRow,1,false); } endMoveRows(); return result; }
QVariant CellModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || role != Qt::DisplayRole) return QVariant(); switch(index.column()) { case NAME : return getCellByIndex(index)->getName(); case LAYER: return getCellByIndex(index)->getLayer()->getName(); } return QVariant(); }
void TableView::onTouchEnded(Touch* touch, Event* unusedEvent) { ScrollView::onTouchEnded(touch, unusedEvent); do { CC_BREAK_IF(m_startFocusCellIndex == -1); auto _focusItem = getCellByIndex(m_startFocusCellIndex); CC_BREAK_IF(!_focusItem); Rect _rect = { 0, 0, getContentSize().width, getContentSize().height }; int32_t _cellIndex; if (_rect.containsPoint(this->convertTouchToNodeSpace(touch))) _cellIndex = getCellIndex(m_container->convertTouchToNodeSpace(touch)); else _cellIndex = -1; if (_cellIndex == m_startFocusCellIndex) { _focusItem->onBlur(); _focusItem->onClick(); if (m_cellTouchCallfunc) { m_cellTouchCallfunc(this, _focusItem, m_startFocusCellIndex, TouchType::CLICK); } } else _focusItem->onBlur(); } while (0); }
bool CellModel::hasChildren(const QModelIndex &parent) const { if (!parent.isValid()) return true; if (getCellByIndex(parent)->childCount()==0) return false; else return true; }
QModelIndex CellModel::parent(const QModelIndex &child) const { Cell* pCell; if (!child.isValid()){ return QModelIndex(); }else{ pCell = getCellByIndex(child)->getParent(); } return createIndex(pCell->getRowNum(),0,pCell); }
bool CellModel::setData(const QModelIndex &index, const QVariant &value, int role) { Q_UNUSED(role); if (!index.isValid()) return false; switch(index.column()) { case NAME : return getCellByIndex(index)->setName(value.toString()); case LAYER : return getCellByIndex(index)->setLayer(value.value<Layer*>()); case DESCRIPTION: return true; //case POINT: //return getCellByIndex(index)->setPointSet(value.value<QList<Point*>*>()); } return false; }
QModelIndex CellModel::index(int row, int column, const QModelIndex &parent) const { Cell* pCell; if(!hasIndex(row,column,parent)) return QModelIndex(); // TODO try to cancel this part if (!parent.isValid()) pCell = topCell; else pCell = getCellByIndex(parent); Cell* cCell = pCell->getChild(row); return createIndex(row,column,cCell); }
bool TableView::onTouchBegan(Touch* touch, Event* unusedEvent) { bool _re = ScrollView::onTouchBegan(touch, unusedEvent); if (_re) { m_startFocusCellIndex = getCellIndex(m_container->convertTouchToNodeSpace(touch)); GridCell* _item = getCellByIndex(m_startFocusCellIndex); if (_item) { _item->onFocus(); } } return _re; }
void TableView::onLongPress() { do { CC_BREAK_IF(m_startFocusCellIndex < 0); auto _item = getCellByIndex(m_startFocusCellIndex); CC_BREAK_IF(!_item); _item->onLongPress(); if (m_cellTouchCallfunc) { m_cellTouchCallfunc(this, _item, m_startFocusCellIndex, TouchType::LONG_PRESS); } } while (0); }
bool CellModel::removeRows(int row, int count, const QModelIndex &parent) { Cell* pCell; bool result; if (!parent.isValid()) pCell = topCell; else pCell = getCellByIndex(parent); if(row+count-1> pCell->childCount()) return false; beginRemoveRows(parent,row,row+count-1); result = pCell->removeChildren(row,count,true); endRemoveRows(); return result; }
void TableView::onTouchMoved(Touch* touch, Event* unusedEvent) { ScrollView::onTouchMoved(touch, unusedEvent); do { CC_BREAK_IF(m_startFocusCellIndex == -1); int _cellIndex = getCellIndex(m_container->convertTouchToNodeSpace(touch)); //如果手指移动距离大于可移动值则判断无法触发点击 if (touch->getDelta().getLength() > CLICK_MAX_MOVE_DISTANCE || m_movedPos.getDistance(m_beganPos) > CLICK_MAX_MOVE_DISTANCE || _cellIndex < 0 || _cellIndex != m_startFocusCellIndex ) { GridCell* _focusItem = _focusItem = getCellByIndex(m_startFocusCellIndex); if (_focusItem) { _focusItem->onBlur(); } m_startFocusCellIndex = -1; } } while (0); }
int CellModel::rowCount(const QModelIndex &parent) const { return getCellByIndex(parent)->childCount(); }