📄 ktreelist.h
字号:
/* qktstreelist.h-------------------------------------------------------------------$Revision: 1.1 $$Date: 2003/09/08 19:42:12 $ KTreeList class declarationCopyright (C) 1996 Keith Brown and KtSoftThis program is free software; you can redistribute it and/or modifyit under the terms of the GNU Library General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULibrary General Public License for more details. You should have received acopy of the GNU Library General Public License along with this program; ifnot, write to the Free Software Foundation, Inc, 675 Mass Ave, Cambridge, MA 02139, USA.-------------------------------------------------------------------*/#ifndef KTREE_LIST_H#define KTREE_LIST_H#include <qapplication.h> // used for QApplication::closingDown()#include <qkeycode.h> // used for keyboard interface#include <qpainter.h> // used to paint items#include <qpixmap.h> // used in items#include <qstack.h> // used to specify tree paths#include <qstring.h> // used in items#include <qtableview.h> // base class for widget// use stack of strings to represent path informationtypedef QStack<QString> KPath;class KTreeList; // forward declaration/** Items for the KTreeList widget */class KTreeListItem{public: /** Item constructor. While text defaults to a null pointer, and the item can be constructed this way, the text has to be non-null when the item is added to the tree, or it will not be inserted. */ KTreeListItem(const char *theText = 0, const QPixmap *thePixmap = 0); // text can not be null when added to the list! virtual ~KTreeListItem(); void appendChild(KTreeListItem *newChild); virtual QRect boundingRect(const QFontMetrics& fm) const; /** Returns a pointer to the child item at the given index in this item's sub tree, or 0 if not found. */ KTreeListItem *childAt(int index); /** Returns the number of child items in this item's sub tree. */ uint childCount() const; /** Returns the index in this items sub tree of the given item or -1 if not found. */ int childIndex(KTreeListItem *child); bool drawExpandButton() const; bool drawText() const; bool drawTree() const; bool expandButtonClicked(const QPoint& coord) const; int getBranch() const; /** Returns a pointer to the first child item in this item's sub tree, or 0 if none. */ KTreeListItem *getChild(); int getIndent() const; /** Returns a pointer to the parent of this item, or 0 if none. */ KTreeListItem *getParent(); /** Returns a pointer to this item's pixmap. If there is no pixmap associated with this item, it will return a pointer to a valid, null QPixmap. */ const QPixmap *getPixmap() const; /** Returns a pointer to the next item in the same branch below this one, or 0 if none. */ KTreeListItem *getSibling(); /** Returns a pointer to this item's text. */ const char *getText() const; /** Indicates whether this item has any children. */ bool hasChild() const; /** Indicates whether this item has a parent. */ bool hasParent() const; /** Indicates whether this item has an item in the same branch below it. */ bool hasSibling() const; virtual int height(const KTreeList *theOwner) const; void insertChild(int index, KTreeListItem *newItem); bool isExpanded() const; virtual void paint(QPainter *p, const QColorGroup& cg, bool highlighted); void removeChild(KTreeListItem *child); void setBranch(int level); void setChild(KTreeListItem *newChild); void setDrawExpandButton(bool doit); void setDrawText(bool doit); void setDrawTree(bool doit); void setExpanded(bool is); void setIndent(int value); void setParent(KTreeListItem *newParent); /** Sets the item pixmap to the given pixmap. */ void setPixmap(const QPixmap *pm); void setSibling(KTreeListItem *newSibling); /** Sets the item text to the given item. */ void setText(const char *t); virtual QRect textBoundingRect(const QFontMetrics& fm) const; virtual QRect itemBoundingRect(const QFontMetrics& fm) const; virtual int width(const KTreeList *theOwner) const;protected: virtual int itemHeight(const QFontMetrics& fm) const; virtual int itemWidth(const QFontMetrics& fm) const;private: int branch; int indent; int numChildren; bool doExpandButton; bool expanded; bool doTree; bool doText; QRect expandButton; KTreeListItem *child; KTreeListItem *parent; KTreeListItem *sibling; QPixmap pixmap; QString text;};// easier declarations of function prototypes for forEvery type functionstypedef bool (KTreeList::*KForEveryM) (KTreeListItem *, void *);typedef bool (*KForEvery) (KTreeListItem *, void *);struct KItemSearchInfo { int count; int index; KTreeListItem *foundItem; KItemSearchInfo() : count(0), index(0), foundItem(0) {}};/** A collapsible treelist widget. 1. Introduction 2. Features 3. Installation 4. Public interface 1. Introduction ================================================================================ KTreeList is a class inherited from QTableView in the Qt user interface library. It provides a way to display hierarchical data in a single-inheritance tree, similar to tree controls in Microsoft Windows and other GUI's. It is most suitable for directory trees or outlines, but I'm sure other uses will come to mind. Frankly, it was designed mostly with the above two functions in mind, but I have tried to make it as flexible as I know how to make it easy to adapt to other uses. In case of problems, I encourage you to read all of the other documentation files in this package before contacting me as you may find the answer to your question in one of them. Also read the source code if you have time. I have tried to comment it adequately and make the source understandable. 2. Features ================================================================================ * Displays both text and optional pixmap supplied by the programmer. A support class, KTreeListItem, can be inherited and modified to draw items as needed by the programmer. * The list items can be returned by index or logical path and the tree navigated by parent, child or sibling references contained in them. Also, item information such as text, pixmap, branch level can be obtained. * Items can be inserted, changed and removed either by index in the visible structure, or by logical paths through the tree hierarchy. * The logical path through the tree for any item can be obtained with the index of the item. * Tree structure display and expanding/collapsing of sub-trees is handled with no intervention from the programmer. * entire tree can be expanded or collapsed to a specified sub-level (handy for outline views) * Configuration as follows: enable/disable item text display (if you only want to display pixmaps) enable/disable drawing of expand/collapse button enable/disable drawing of tree structure * Keyboard support as follows: up/down arrows move the highlight appropriately and scroll the list an item at a time, if necessary pgup/pgdn move the highlight a 'page' up or down as applicable and scroll the view +/- keys expand/collapse the highlighted item if it appropriate enter key selects the highlighted item * Mouse support as follows: left click on item highlights it left click on an item "hot button" expands or collapses its sub-tree, as applicable double click on item selects it normal scrolling functions in conjunction with scrollbars if present 2nd scrolling with the middle mouse button: pressing MMB inserts a rubberband, showing which part of the whole tree is currently visible. moving the mouse will scroll the visible part * Signals/Slots signal void highlighted(int) - emitted when an item in the tree is highlighted; sends the index of the item signal void selected(int) - emitted when an item in the tree is selected; sends the index of the item signal void expanded(int) - emitted when an item in the tree is expanded; sends the index of the item signal void collpased(int) - emitted when an item in the tree is collapsed; sends the index of the item */class KTreeList : public QTableView{ Q_OBJECTpublic: /** Widget contructor. Passes all parameters on to base QTableView, and does not use them directly. Does internal initialization, sets the current item to -1, and sets default values for scroll bars (both auto) and grid snap (snap to grid vertically). */ KTreeList(QWidget *parent = 0, const char *name = 0, WFlags f = 0); virtual ~KTreeList(); /** Adds a new item to the tree with the given text and pixmap as a child of the item currently at the given index. If the current item already has children, the new item is appended below them. */ void addChildItem(const char *theText, const QPixmap *thePixmap, int index); /** Same as above except parent item is specified by path. */ void addChildItem(const char *theText, const QPixmap *thePixmap, const KPath *thePath); /** Adds the given item as a child of the item currently at the given index. If the current item already has children, the new item is appended below them. */ void addChildItem(KTreeListItem *newItem, int index); /** Same as above except parent item is specified by path. */ void addChildItem(KTreeListItem *newItem, const KPath *thePath); /** Returns a bool value indicating whether the list will display a horizontal scrollbar if one of the displayed items is wider than can be displayed at the current width of the view. */ bool autoBottomScrollBar() const; /** Returns a bool value indicating whether the list will display a vertical scrollbar if the number of displayed items is more than can be displayed at the current height of the view. */ bool autoScrollBar() const; /** Returns a bool value indicating whether the list will update immediately on changing the state of the widget in some way. */ bool autoUpdate() const; /** Returns a bool value indicating whether the list has currently has a horizontal scroll bar.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -