📄 ktreelist.cpp
字号:
drawExpandButton(TRUE), drawTree(TRUE), expansion(0), goingDown(FALSE), indent(-1), showText(TRUE), rubberband_mode(false) { initMetaObject(); setCellHeight(0); setCellWidth(0); setNumRows(0); setNumCols(1); setTableFlags(Tbl_autoScrollBars | Tbl_clipCellPainting | Tbl_snapToVGrid); clearTableFlags(Tbl_scrollLastVCell | Tbl_scrollLastHCell | Tbl_snapToVGrid); switch(style()) { case WindowsStyle: case MotifStyle: setFrameStyle(QFrame::WinPanel | QFrame::Sunken); setBackgroundColor(colorGroup().base()); break; default: setFrameStyle(QFrame::Panel | QFrame::Plain); setLineWidth(1); } setAcceptFocus(TRUE); treeRoot = new KTreeListItem; treeRoot->setBranch(-1); treeRoot->setExpanded(TRUE);}// destructorKTreeList::~KTreeList(){ goingDown = TRUE; clear(); delete treeRoot;}// This is needed to make the apply button of kcontroll's // color setup working (Marcin Dalecki)void KTreeList::paletteChange(const QPalette &){ setBackgroundColor(colorGroup().base()); repaint(true);}// appends a child to the item at the given index with the given text// and pixmapvoid KTreeList::addChildItem(const char *theText, const QPixmap *thePixmap, int index){ addChildItem(new KTreeListItem(theText, thePixmap), index);}// appends a child to the item at the end of the given path with// the given text and pixmapvoid KTreeList::addChildItem(const char *theText, const QPixmap *thePixmap, const KPath *thePath){ addChildItem(new KTreeListItem(theText, thePixmap), thePath);}// appends the given item to the children of the item at the given indexvoid KTreeList::addChildItem(KTreeListItem *newItem, int index){ if(!checkItemText(newItem->getText())) return; // find parent item and append new item to parent's sub tree KTreeListItem *parentItem = itemAt(index); if(!parentItem) return; addChildItem(parentItem, newItem);}// appends the given item to the children of the item at the end of the// given pathvoid KTreeList::addChildItem(KTreeListItem *newItem, const KPath *thePath){ if(!checkItemText(newItem->getText())) return; if(!checkItemPath(thePath)) return; // find parent item and append new item to parent's sub tree KTreeListItem *parentItem = itemAt(thePath); if(!parentItem) return; addChildItem(parentItem, newItem);} // indicates whether horizontal scrollbar appears only when neededbool KTreeList::autoBottomScrollBar() const{ return testTableFlags(Tbl_autoHScrollBar);}// indicates whether vertical scrollbar appears only when neededbool KTreeList::autoScrollBar() const{ return testTableFlags(Tbl_autoVScrollBar);}// indicates whether display updates automatically on changesbool KTreeList::autoUpdate() const{ return QTableView::autoUpdate();}// indicates whether horizontal scrollbar is presentbool KTreeList::bottomScrollBar() const{ return testTableFlags(Tbl_hScrollBar);}// find item at specified index and change pixmap and/or textvoid KTreeList::changeItem(const char *newText, const QPixmap *newPixmap, int index){ KTreeListItem *item = itemAt(index); if(item) changeItem(item, index, newText, newPixmap);}// find item at end of specified path, and change pixmap and/or textvoid KTreeList::changeItem(const char *newText, const QPixmap *newPixmap, const KPath *thePath){ KTreeListItem *item = itemAt(thePath); if(item) { int index = itemIndex(item); changeItem(item, index, newText, newPixmap); }}// clear all items from list and erase displayvoid KTreeList::clear(){ // somewhat of a hack for takeItem so it doesn't update the current item... clearing = TRUE; bool autoU = autoUpdate(); setAutoUpdate(FALSE); QStack<KTreeListItem> stack; stack.push(treeRoot); while(!stack.isEmpty()) { KTreeListItem *item = stack.pop(); if(item->hasChild()) { stack.push(item); stack.push(item->getChild()); } else if(item->hasSibling()) { stack.push(item); stack.push(item->getSibling()); } else if(item->getBranch() != -1) { takeItem(item); delete item; } } clearing = FALSE; setCurrentItem(-1); if(goingDown || QApplication::closingDown()) return; if(autoU && isVisible()) repaint(); setAutoUpdate(autoU);}// return a count of all the items in the list, whether visible or notuint KTreeList::count(){ int total = 0; forEveryItem(&KTreeList::countItem, (void *)&total); return total;}// returns the index of the current (highlighted) itemint KTreeList::currentItem() const{ return current;}// only collapses the item if it is expanded. If not expanded, or// index invalid, does nothingvoid KTreeList::collapseItem(int index){ KTreeListItem *item = itemAt(index); if(item && item->isExpanded()) expandOrCollapse(item);}// only expands the item if it is collapsed. If not collapsed, or// index invalid, does nothingvoid KTreeList::expandItem(int index){ KTreeListItem *item = itemAt(index); if(item && !item->isExpanded()) expandOrCollapse(item);}// returns the depth the tree is automatically expanded to when// items are addedint KTreeList::expandLevel() const{ return expansion;}// expands or collapses the subtree rooted at the given item,// as approptiate// if item has no children, does nothingvoid KTreeList::expandOrCollapseItem(int index){ KTreeListItem *item = itemAt(index); if(item) expandOrCollapse(item);}// visits every item in the tree, visible or not and applies // the user supplied function with the item and user data passed as parameters// if user supplied function returns TRUE, traversal ends and function returnsvoid KTreeList::forEveryItem(KForEvery func, void *user){ QStack<KTreeListItem> stack; KTreeListItem *item = treeRoot->getChild(); while(item) { stack.push(item); while(!stack.isEmpty()) { KTreeListItem *poppedItem = stack.pop(); if((*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 function with the item and user data passed as parameters// if user supplied function returns TRUE, traversal ends and function// returnsvoid KTreeList::forEveryVisibleItem(KForEvery func, void *user){ QStack<KTreeListItem> stack; KTreeListItem *item = treeRoot->getChild(); do { while(item) { if((*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);}// returns a pointer to the KTreeListItem at the current index// or 0 if no current itemKTreeListItem *KTreeList::getCurrentItem(){ if(current == -1) return 0; return itemAt(current);}// returns the current indent spacing, or -1 if using item defaultint KTreeList::indentSpacing(){ return indent;}// inserts a new item with the specified text and pixmap before// or after the item at the given index, depending on the value// of prefix// if index is negative, appends item to tree at root levelvoid KTreeList::insertItem(const char *theText, const QPixmap *thePixmap, int index, bool prefix){ insertItem(new KTreeListItem(theText, thePixmap), index, prefix);}// inserts a new item with the specified text and pixmap before// or after the item at the end of the given path, depending on the value// of prefixvoid KTreeList::insertItem(const char *theText, const QPixmap *thePixmap, const KPath *thePath, bool prefix){ insertItem(new KTreeListItem(theText, thePixmap), thePath, prefix);}// inserts the given item or derived object into the tree before// or after the item at the given index, depending on the value// of prefix// if index is negative, appends item to tree at root levelvoid KTreeList::insertItem(KTreeListItem *newItem, int index, bool prefix){ // check for proper parameters if(!checkItemText(newItem->getText())) return; // find the item currently at the index, if there is one KTreeListItem *foundItem = itemAt(index); // insert new item at the appropriate place insertItem(foundItem, newItem, prefix);}// inserts the given item or derived object into the tree before// or after the item at the end of the given path, depending on the value// of prefixvoid KTreeList::insertItem(KTreeListItem *newItem, const KPath *thePath, bool prefix){ // check for valid parameters if(!checkItemText(newItem->getText())) return; if(!checkItemPath(thePath)) return; // find the item currently at the end of the path, if there is one KTreeListItem *foundItem = itemAt(thePath); // insert new item at appropriate place insertItem(foundItem, newItem, prefix);} // returns pointer to KTreeListItem at index or 0 if// index is invalidKTreeListItem *KTreeList::itemAt(int index){ KItemSearchInfo searchInfo; searchInfo.index = index; searchInfo.count = -1; forEveryVisibleItem(&KTreeList::findItemAt, &searchInfo); return searchInfo.foundItem;}// returns pointer to KTreeListItem at the end of the// path or 0 if not foundKTreeListItem *KTreeList::itemAt(const KPath *path){ // reverse the path so we can use it KPath pathCopy = *path, *reversePath = new KPath(); while(!pathCopy.isEmpty()) reversePath->push(pathCopy.pop()); KTreeListItem *found = treeRoot->getChild(); if(!found) { delete reversePath; return 0; } found = recursiveFind(found, reversePath); delete reversePath; return found;}// returns the index in the visible tree of the given item or// -1 if not foundint KTreeList::itemIndex(KTreeListItem *item){ KItemSearchInfo searchInfo; searchInfo.index = searchInfo.count = -1; searchInfo.foundItem = item; forEveryVisibleItem(&KTreeList::getItemIndex, &searchInfo); return searchInfo.index;}// returns pointer to item path or 0// if index is invalid// the first item popped from the returned path will be// the item and the last the root item// in this form, the path can be passed directly to other// functions that take a path, or consecutive levels pushed// onto it firstKPath *KTreeList::itemPath(int index){ KTreeListItem *item = itemAt(index); if(!item) return 0; KPath tempPath, *tempPathCopy = new KPath(); while(item) { if(item != treeRoot) tempPath.push(new QString(item->getText())); item = item->getParent(); } while(!tempPath.isEmpty()) tempPathCopy->push(tempPath.pop()); tempPathCopy->setAutoDelete(TRUE); return tempPathCopy;}// move the subtree at the specified index up one branch level// (make root item a sibling of it's current parent)void KTreeList::join(int index){ KTreeListItem *item = itemAt(index); if(item) join(item);}// move the subtree at the specified index up one branch level// (make root item a sibling of it's current parent)void KTreeList::join(const KPath *path){ KTreeListItem *item = itemAt(path); if(item) join(item);}// move item at specified index one slot down in its parent's sub treevoid KTreeList::lowerItem(int index){ KTreeListItem *item = itemAt(index); if(item) lowerItem(item);}// move item at specified path one slot down in its parent's sub treevoid KTreeList::lowerItem(const KPath *path){ KTreeListItem *item = itemAt(path); if(item) lowerItem(item);}// move item at specified index one slot up in its parent's sub treevoid KTreeList::raiseItem(int index){ KTreeListItem *item = itemAt(index); if(item) raiseItem(item);}// move item at specified path one slot up in its parent's sub treevoid KTreeList::raiseItem(const KPath *path){ KTreeListItem *item = itemAt(path); if(item) raiseItem(item);}// remove the item at the specified index and delete itvoid KTreeList::removeItem(int index){ KTreeListItem *item = itemAt(index); if(item) { takeItem(item); delete item; }}// remove the item at the end of the specified path and delete itvoid KTreeList::removeItem(const KPath *thePath){ KTreeListItem *item = itemAt(thePath); if(item) { takeItem(item); delete item; }}// indicates whether vertical scrollbar is presentbool KTreeList::scrollBar() const{ return testTableFlags(Tbl_vScrollBar);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -