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

📄 q3listview.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include <qplatformdefs.h>#include "q3listview.h"#ifndef QT_NO_LISTVIEW#include "q3tl.h"#include "qapplication.h"#include "qbitmap.h"#include "q3cleanuphandler.h"#include "qcursor.h"#include "qdatetime.h"#include "q3dragobject.h"#include "qevent.h"#include "qhash.h"#include "q3header.h"#include "qicon.h"#include "qlineedit.h"#include "qpainter.h"#include "qpixmapcache.h"#include "qstack.h"#include "qstyle.h"#include "qstyleoption.h"#include "qtimer.h"#include "qtooltip.h"#include "qdebug.h"#ifndef QT_NO_ACCESSIBILITY#include "qaccessible.h"#endifconst int Unsorted = 16383;static Q3CleanupHandler<QBitmap> qlv_cleanup_bitmap;struct Q3ListViewPrivate{    // classes that are here to avoid polluting the global name space    // the magical hidden mother of all items    class Root: public Q3ListViewItem {    public:        Root(Q3ListView * parent);        void setHeight(int);        void invalidateHeight();        void setup();        Q3ListView * theListView() const;        Q3ListView * lv;    };    // to remember what's on screen    class DrawableItem {    public:        DrawableItem() {}        DrawableItem(int level, int ypos, Q3ListViewItem * item)            : l(level), y(ypos), i(item) {};        int l;        int y;        Q3ListViewItem * i;    };    // for sorting    class SortableItem {    public:        /*          We could be smarter and keep a pointer to the Q3ListView          item instead of numCols, col and asc. This would then allow          us to use the physical ordering of columns rather than the          logical. Microsoft uses the logical ordering, so there is          some virtue in doing so, although it prevents the user from          choosing the secondary key.        */        Q3ListViewItem * item;        int numCols;        int col;        bool asc;        int cmp(const SortableItem& i) const {            int diff = item->compare(i.item, col, asc);            if (diff == 0 && numCols != 1) {                for (int j = 0; j < numCols; j++) {                    if (j != col) {                        diff = item->compare(i.item, j, asc);                        if (diff != 0)                            break;                    }                }            }            return diff;        }        bool operator<(const SortableItem& i) const { return cmp(i) < 0; }        bool operator<=(const SortableItem& i) const { return cmp(i) <= 0; }        bool operator>(const SortableItem& i) const { return cmp(i) > 0; }    };    class ItemColumnInfo {    public:        ItemColumnInfo(): pm(0), next(0), truncated(false), dirty(false), allow_rename(false), width(0) {}        ~ItemColumnInfo() { delete pm; delete next; }        QString text, tmpText;        QPixmap * pm;        ItemColumnInfo * next;        uint truncated : 1;        uint dirty : 1;        uint allow_rename : 1;        int width;    };    class ViewColumnInfo {    public:        ViewColumnInfo(): align(Qt::AlignAuto), sortable(true), next(0) {}        ~ViewColumnInfo() { delete next; }        int align;        bool sortable;        ViewColumnInfo * next;    };    // private variables used in Q3ListView    ViewColumnInfo * vci;    Q3Header * h;    Root * r;    uint rootIsExpandable : 1;    int margin;    Q3ListViewItem * focusItem, *highlighted, *oldFocusItem;    QTimer * timer;    QTimer * dirtyItemTimer;    QTimer * visibleTimer;    int levelWidth;    // the list of drawables, and the range drawables covers entirely    // (it may also include a few items above topPixel)    QList<DrawableItem> drawables;    int topPixel;    int bottomPixel;    QList<const Q3ListViewItem *> dirtyItems;    Q3ListView::SelectionMode selectionMode;    // Per-column structure for information not in the Q3Header    struct Column {        Q3ListView::WidthMode wmode;    };    QVector<Column> column;    // suggested height for the items    int fontMetricsHeight;    int minLeftBearing, minRightBearing;    int ellipsisWidth;    // currently typed prefix for the keyboard interface, and the time    // of the last key-press    QString currentPrefix;    QTime currentPrefixTime;    // holds a list of iterators    QList<Q3ListViewItemIterator *> iterators;    Q3ListViewItem *pressedItem, *selectAnchor;    QTimer *scrollTimer;    QTimer *renameTimer;    QTimer *autoopenTimer;    // sort column and order   #### may need to move to Q3Header [subclass]    int sortcolumn;    bool ascending                :1;    bool sortIndicator                :1;    // whether to select or deselect during this mouse press.    bool allColumnsShowFocus        :1;    bool select                        :1;    // true if the widget should take notice of mouseReleaseEvent    bool buttonDown                :1;    // true if the widget should ignore a double-click    bool ignoreDoubleClick        :1;    bool clearing                :1;    bool pressedSelected        :1;    bool pressedEmptyArea         :1;    bool toolTips                :1;    bool fullRepaintOnComlumnChange:1;    bool updateHeader                :1;    bool startEdit : 1;    bool ignoreEditAfterFocus : 1;    bool inMenuMode :1;    Q3ListView::RenameAction defRenameAction;    Q3ListViewItem *startDragItem;    QPoint dragStartPos;    int pressedColumn;    Q3ListView::ResizeMode resizeMode;};Q_DECLARE_TYPEINFO(Q3ListViewPrivate::DrawableItem, Q_PRIMITIVE_TYPE);// these should probably be in Q3ListViewPrivate, for future thread safetystatic bool activatedByClick;static QPoint activatedP;#ifndef QT_NO_ACCESSIBILITYstatic int indexOfItem(Q3ListViewItem *item){    if (!QAccessible::isActive())        return 0;    static Q3ListViewItem *lastItem = 0;    static int lastIndex = 0;    if (!item || !item->listView())        return 0;    if (item == lastItem)        return lastIndex;    lastItem = item;    int index = 1;    Q3ListViewItemIterator it(item->listView());    while (it.current()) {        if (it.current() == item) {            lastIndex = index;            return index;        }        ++it;        ++index;    }    lastIndex = 0;    return 0;}#endif/*!    Creates a string with ... like "Trollte..." or "...olltech", depending on the alignment.*/static QString qEllipsisText(const QString &org, const QFontMetrics &fm, int width, int align){    int ellWidth = fm.width(QLatin1String("..."));    QString text = QString::fromLatin1("");    int i = 0;    int len = org.length();    int offset = (align & Qt::AlignRight) ? (len-1) - i : i;    while (i < len && fm.width(text + org[offset]) + ellWidth < width) {        if (align & Qt::AlignRight)            text.prepend(org[offset]);        else            text += org[offset];        offset = (align & Qt::AlignRight) ? (len-1) - ++i : ++i;    }    if (text.isEmpty())        text = (align & Qt::AlignRight) ? org.right(1) : text = org.left(1);    if (align & Qt::AlignRight)        text.prepend(QLatin1String("..."));    else        text += QLatin1String("...");    return text;}/*!    \class Q3ListViewItem    \brief The Q3ListViewItem class implements a list view item.    \compat    A list view item is a multi-column object capable of displaying    itself in a Q3ListView.    The easiest way to use Q3ListViewItem is to construct one with a    few constant strings, and either a Q3ListView or another    Q3ListViewItem as parent.    \code        (void) new Q3ListViewItem(listView, "Column 1", "Column 2");        (void) new Q3ListViewItem(listView->firstChild(), "A", "B", "C");    \endcode    We've discarded the pointers to the items since we can still access    them via their parent \e listView. By default, Q3ListView sorts its    items; this can be switched off with Q3ListView::setSorting(-1).    The parent must be another Q3ListViewItem or a Q3ListView. If the    parent is a Q3ListView, the item becomes a top-level item within    that Q3ListView. If the parent is another Q3ListViewItem, the item    becomes a child of that list view item.    If you keep the pointer, you can set or change the texts using    setText(), add pixmaps using setPixmap(), change its mode using    setSelectable(), setSelected(), setOpen() and setExpandable().    You'll also be able to change its height using setHeight(), and    traverse its sub-items. You don't have to keep the pointer since    you can get a pointer to any Q3ListViewItem in a Q3ListView using    Q3ListView::selectedItem(), Q3ListView::currentItem(),    Q3ListView::firstChild(), Q3ListView::lastItem() and    Q3ListView::findItem().    If you call \c delete on a list view item, it will be deleted as    expected, and as usual for \l{QObject}s, if it has any child items    (to any depth), all these will be deleted too.    \l{Q3CheckListItem}s are list view items that have a checkbox or    radio button and can be used in place of plain Q3ListViewItems.    You can traverse the tree as if it were a doubly-linked list using    itemAbove() and itemBelow(); they return pointers to the items    directly above and below this item on the screen (even if none of    them are actually visible at the moment).    Here's how to traverse all of an item's children (but not its    children's children, etc.):    Example:    \code        Q3ListViewItem * myChild = myItem->firstChild();        while(myChild) {            doSomething(myChild);            myChild = myChild->nextSibling();        }    \endcode    If you want to iterate over every item, to any level of depth use    an iterator. To iterate over the entire tree, initialize the    iterator with the list view itself; to iterate over an item's    children (and children's children to any depth), initialize the    iterator with the item:    \code        Q3ListViewItemIterator it(listview);        while (it.current()) {            Q3ListViewItem *item = it.current();            doSomething(item);            ++it;        }    \endcode    Note that the order of the children will change when the sorting    order changes and is undefined if the items are not visible. You    can, however, call enforceSortOrder() at any time; Q3ListView will    always call it before it needs to show an item.    Many programs will need to reimplement Q3ListViewItem. The most    commonly reimplemented functions are:    \table    \header \i Function \i Description    \row \i \l text()         \i Returns the text in a column. Many subclasses will compute            this on the fly.    \row \i \l key()         \i Used for sorting. The default key() simply calls            text(), but judicious use of key() can give you fine            control over sorting; for example, QFileDialog            reimplements key() to sort by date.    \row \i \l setup()         \i Called before showing the item and whenever the list            view's font changes, for example.    \row \i \l activate()         \i Called whenever the user clicks on the item or presses            Space when the item is the current item.    \endtable    Some subclasses call setExpandable(true) even when they have no    children, and populate themselves when setup() or setOpen(true) is    called. The \c dirview/dirview.cpp example program uses this    technique to start up quickly: The files and subdirectories in a    directory aren't inserted into the tree until they're actually    needed.    \img qlistviewitems.png List View Items    \sa Q3CheckListItem Q3ListView*//*!    \fn int Q3CheckListItem::rtti() const    Returns 1.    Make your derived classes return their own values for rtti(), and    you can distinguish between list view items. You should use values    greater than 1000, to allow for extensions to this class.*//*!    Constructs a new top-level list view item in the Q3ListView \a    parent.*/Q3ListViewItem::Q3ListViewItem(Q3ListView * parent){    init();    parent->insertItem(this);}/*!    Constructs a new list view item that is a child of \a parent and    first in the parent's list of children.*/Q3ListViewItem::Q3ListViewItem(Q3ListViewItem * parent){    init();    parent->insertItem(this);}/*!    Constructs an empty list view item that is a child of \a parent    and is after item \a after in the parent's list of children. Since    \a parent is a Q3ListView the item will be a top-level item.*/Q3ListViewItem::Q3ListViewItem(Q3ListView * parent, Q3ListViewItem * after){    init();    parent->insertItem(this);    moveToJustAfter(after);

⌨️ 快捷键说明

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