⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ktreelist.cpp

📁 一种效率高
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// enables/disables auto update of displayvoid KTreeList::setAutoUpdate(bool enable){  QTableView::setAutoUpdate(enable);}// enables/disables horizontal scrollbarvoid KTreeList::setBottomScrollBar(bool enable){  enable ? setTableFlags(Tbl_hScrollBar) :    clearTableFlags(Tbl_hScrollBar);}// sets the current item and hightlights it, emitting signalvoid KTreeList::setCurrentItem(int index){  if(index == current)    return;  int numVisible = visibleCount();  if(index > numVisible)    return;  int oldCurrent = current;  current = index;  if(oldCurrent < numVisible)    updateCell(oldCurrent, 0);  if(current > -1) {    updateCell(current, 0, FALSE);    emit highlighted(current);  }}// enables/disables drawing of expand buttonvoid KTreeList::setExpandButtonDrawing(bool enable){  if(drawExpandButton == enable)    return;  drawExpandButton = enable;  forEveryItem(&KTreeList::setItemExpandButtonDrawing, 0);  if(autoUpdate() && isVisible())    repaint();}// sets depth to which subtrees are automatically expanded, and// redraws tree if auto update enabledvoid KTreeList::setExpandLevel(int level){  if(expansion == level)    return;  expansion = level;  KTreeListItem *item = getCurrentItem();  forEveryItem(&KTreeList::setItemExpanded, 0);  while(item) {    if(item->getParent()->isExpanded())      break;    item = item->getParent();  }  setCurrentItem(itemIndex(item));  if(autoUpdate() && isVisible())    repaint();}// sets the indent margin for all branches and repaints if auto update enabled// setting to -1 uses default spacing in items themselvesvoid KTreeList::setIndentSpacing(int spacing){  if(indent == spacing)    return;  indent = spacing;  forEveryItem(&KTreeList::setItemIndent, 0);  if(autoUpdate() && isVisible())    repaint();}// enables/disables vertical scrollbarvoid KTreeList::setScrollBar(bool enable){  enable ? setTableFlags(Tbl_vScrollBar) :    clearTableFlags(Tbl_vScrollBar);}// enables/disables display of item text (keys)void KTreeList::setShowItemText(bool enable){  if(showText == enable)    return;  showText = enable;  forEveryItem(&KTreeList::setItemShowText, 0);  if(autoUpdate() && isVisible())    repaint();}// indicates whether vertical scrolling is by pixel or rowvoid KTreeList::setSmoothScrolling(bool enable){  enable ? setTableFlags(Tbl_smoothVScrolling) :    clearTableFlags(Tbl_smoothVScrolling);}// enables/disables tree branch drawingvoid KTreeList::setTreeDrawing(bool enable){  if(drawTree == enable)    return;  drawTree = enable;  forEveryItem(&KTreeList::setItemTreeDrawing, 0);  if(autoUpdate() && isVisible())    repaint();}    // indicates whether item text keys are displayedbool KTreeList::showItemText() const{  return showText;}// indicates whether scrolling is by pixel or rowbool KTreeList::smoothScrolling() const{  return testTableFlags(Tbl_smoothVScrolling);}// indents the item at the given index, splitting the tree into// a new branchvoid KTreeList::split(int index){  KTreeListItem *item = itemAt(index);  if(item)    split(item);}// indents the item at the given path, splitting the tree into// a new branchvoid KTreeList::split(const KPath *path){  KTreeListItem *item = itemAt(path);  if(item)    split(item);}// removes item at specified index from tree but does not delete it// returns pointer to the item or 0 if not succesfulKTreeListItem *KTreeList::takeItem(int index){  KTreeListItem *item = itemAt(index);  if(item)    takeItem(item);  return item;}// removes item at specified path from tree but does not delete it// returns pointer to the item or 0 if not successfulKTreeListItem *KTreeList::takeItem(const KPath *path){  KTreeListItem *item = itemAt(path);  if(item)    takeItem(item);  return item;}// indicates whether tree branches are drawnbool KTreeList::treeDrawing() const{  return drawTree;}// returns the number of items visibleint KTreeList::visibleCount(){  int total = 0;  forEveryVisibleItem(&KTreeList::countItem, (void *)&total);  return total;}// adds the new item at the end of the parent item's branchvoid KTreeList::addChildItem(KTreeListItem *theParent,				KTreeListItem *newItem){   theParent->appendChild(newItem);    // set item state    if(indent > -1)  // custom indent    newItem->setIndent(indent);  newItem->setDrawExpandButton(drawExpandButton);  newItem->setDrawTree(drawTree);  newItem->setDrawText(showText);  if(newItem->getBranch() < expansion)    newItem->setExpanded(TRUE); 		// fix up branch levels of any children new item may already have		if(newItem->hasChild())		fixChildBranches(newItem);		  // if necessary, adjust cell width, number of rows and repaint    if(theParent->isExpanded() || theParent->childCount() == 1) {    bool autoU = autoUpdate();    setAutoUpdate(FALSE);    setNumRows(visibleCount());    updateCellWidth();    if(autoU && isVisible())      repaint();    setAutoUpdate(autoU);  }}// returns the height of the cell(row) at the specified row (index)int KTreeList::cellHeight(int row){  return itemAt(row)->height(this);}// changes the given item with the new text and/or pixmapvoid KTreeList::changeItem(KTreeListItem *toChange,			      int itemIndex,			      const char *newText,			      const QPixmap *newPixmap){  int oldWidth = toChange->width(this);  if(newText)    toChange->setText(newText);  if(newPixmap)    toChange->setPixmap(newPixmap);  if(oldWidth != toChange->width(this))    updateCellWidth();  if(itemIndex == -1)    return;  if(autoUpdate() && rowIsVisible(itemIndex))    updateCell(itemIndex, 0);}// returns bool indicating valid item pathbool KTreeList::checkItemPath(const KPath *path) const{  bool isValid = TRUE;  if(!path)     isValid = FALSE;  if(path->isEmpty())    isValid = FALSE;   return isValid;}// returns bool indicating valid item textbool KTreeList::checkItemText(const char *text) const{  bool isValid = TRUE;  if(!text)    isValid = FALSE;  else if(*text == 0)     isValid = FALSE;  return isValid;}// collapses the subtree at the specified itemvoid KTreeList::collapseSubTree(KTreeListItem *subRoot){  subRoot->setExpanded(FALSE);  setNumRows(visibleCount());  updateCellWidth();}// used by count() with forEach() function to count total number// of items in the listbool KTreeList::countItem(KTreeListItem *, void *total){  (*((int *)total))++;  return FALSE;}// if item is expanded, collapses it or vice-versavoid KTreeList::expandOrCollapse(KTreeListItem *parent){  bool autoU = autoUpdate();  setAutoUpdate(FALSE);  int parentIndex = itemIndex(parent);  if(parent->isExpanded()) {     collapseSubTree(parent);    emit collapsed(parentIndex);  }  else {    expandSubTree(parent);    emit expanded(parentIndex);  }  if(autoU && isVisible())    repaint();  setAutoUpdate(autoU);}// expands the subtree at the given itemvoid KTreeList::expandSubTree(KTreeListItem *subRoot){  subRoot->setExpanded(TRUE);  setNumRows(visibleCount());  updateCellWidth();}// called by itemAt for each itembool KTreeList::findItemAt(KTreeListItem *item, void *user){  KItemSearchInfo *searchInfo = (KItemSearchInfo *)user;  if(++searchInfo->count == searchInfo->index) {    searchInfo->foundItem = item;    return TRUE;  }  return FALSE;}// fix up branch levels out of whack from split/join operations on the treevoid KTreeList::fixChildBranches(KTreeListItem *parentItem){	KTreeListItem *childItem = 0, *siblingItem = 0;	int childBranch = parentItem->getBranch() + 1;	if(parentItem->hasChild()) {		childItem = parentItem->getChild(); 		childItem->setBranch(childBranch);		fixChildBranches(childItem);	}	while(childItem && childItem->hasSibling()) {		siblingItem = childItem->getSibling();		siblingItem->setBranch(childBranch);		fixChildBranches(siblingItem);		childItem = siblingItem;	}	}// handles QFocusEvent processing by setting current item to top// row if there is no current item, and updates cell to add or// delete the focus rectangle on the highlight barvoid KTreeList::focusInEvent(QFocusEvent *){  if(current < 0 && numRows() > 0)    setCurrentItem(topCell());  updateCell(current, 0);}// visits every item in the tree, visible or not and applies the user // supplied member function with the item and user data passed as parameters// if the user supplied member function returns TRUE, traversal// ends and the function returnsvoid KTreeList::forEveryItem(KForEveryM func,			   void *user){  KTreeListItem *item = treeRoot->getChild();  QStack<KTreeListItem> stack;  while(item) {    stack.push(item);    while(!stack.isEmpty()) {      KTreeListItem *poppedItem = stack.pop();      if((this->*func)(poppedItem, user))	return;      if(poppedItem->hasChild()) {	KTreeListItem *childItem = poppedItem->getChild();	while(childItem) {	  stack.push(childItem);	  childItem = childItem->getSibling();	}      }    }    item = item->getSibling();  }}// visits every visible item in the tree in order and applies the user // supplied member function with the item and user data passed as parameters// if user supplied function returns TRUE, traversal ends and function// returnsvoid KTreeList::forEveryVisibleItem(KForEveryM func,				       void *user){  QStack<KTreeListItem> stack;  KTreeListItem *item = treeRoot->getChild();  do {    while(item) {      if((this->*func)(item, user)) return;      if(item->hasChild() && item->isExpanded()) {        stack.push(item);        item = item->getChild();      }      else        item = item->getSibling();    }    if(stack.isEmpty())      break;    item = stack.pop()->getSibling();  } while(TRUE);}// called by itemIndex() for each item in visible list  bool KTreeList::getItemIndex(KTreeListItem *item, void *user){  KItemSearchInfo *searchInfo = (KItemSearchInfo *)user;  searchInfo->count++;  if(item == searchInfo->foundItem) {    searchInfo->index = searchInfo->count;    return TRUE;  }  return FALSE;}// called by updateCellWidth() for each item in the visible listbool KTreeList::getMaxItemWidth(KTreeListItem *item, void *user){  int *maxW = (int *)user;  int w = item->width(this);  if(w > *maxW)    *maxW = w;  return FALSE;}// inserts the new item before or after the reference item, depending// on the value of prefixvoid KTreeList::insertItem(KTreeListItem *referenceItem,			      KTreeListItem *newItem,			      bool prefix){	  // set the new item's state	  if(indent > -1)  // custom indent    newItem->setIndent(indent);  newItem->setDrawExpandButton(drawExpandButton);  newItem->setDrawTree(drawTree);  newItem->setDrawText(showText);  KTreeListItem *parentItem;  if(referenceItem) {    parentItem = referenceItem->getParent();     int insertIndex = parentItem->childIndex(referenceItem);    if(!prefix)      insertIndex++;    parentItem->insertChild(insertIndex, newItem);  }    // no reference item, append at end of visible tree  // need to repaint    else {    parentItem = treeRoot;    parentItem->appendChild(newItem);  }    // set item expansion    if(newItem->getBranch() < expansion)    newItem->setExpanded(TRUE);     // fix up branch levels of any children    if(newItem->hasChild())		fixChildBranches(newItem);	  	  // if repaint necessary, do it if visible and auto update  // enabled    //if(parentItem->isExpanded())  //  setNumRows(visibleCount());    if(parentItem->isExpanded() || parentItem->childCount() == 1) {    bool autoU = autoUpdate();    setAutoUpdate(FALSE);    setNumRows(visibleCount());    updateCellWidth();    if(autoU && isVisible())      repaint();    setAutoUpdate(autoU);  }}// joins the item's branch into the tree, making the item a sibling// of its parentvoid KTreeList::join(KTreeListItem *item){  KTreeListItem *itemParent = item->getParent();  if(itemParent->hasParent()) {    bool autoU = autoUpdate();    setAutoUpdate(FALSE);    takeItem(item);    insertItem(itemParent, item, FALSE);    if(autoU && isVisible())      repaint();    setAutoUpdate(autoU);  }}// handles keyboard interface to tree listvoid KTreeList::keyPressEvent(QKeyEvent *e){  if(numRows() == 0)         // nothing to be done        return;  if(currentItem() < 0)        // if no current item, make the top item current      setCurrentItem(topCell());   int pageSize, delta;  switch(e->key()) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -