📄 q3iconview.cpp
字号:
/******************************************************************************** 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 "qglobal.h"#if defined(Q_CC_BOR)// needed for qsort() because of a std namespace problem on Borland#include "qplatformdefs.h"#endif#include "q3iconview.h"#ifndef QT_NO_ICONVIEW#include "private/q3richtext_p.h"#include "q3textedit.h"#include "qapplication.h"#include "qbitmap.h"#include "qbrush.h"#include "q3cleanuphandler.h"#include "qcursor.h"#include "qevent.h"#include "qfontmetrics.h"#include "qhash.h"#include "qimage.h"#include "qmime.h"#include "qpainter.h"#include "qpalette.h"#include "qpen.h"#include "qpixmapcache.h"#include "qstringlist.h"#include "qstyle.h"#include "qstyleoption.h"#include "qtimer.h"#include "qtooltip.h"#include "q3strlist.h"#include <limits.h>#include <stdlib.h>#define RECT_EXTENSION 300static const char * const unknown_xpm[] = { "32 32 11 1", "c c #ffffff", "g c #c0c0c0", "a c #c0ffc0", "h c #a0a0a4", "d c #585858", "f c #303030", "i c #400000", "b c #00c000", "e c #000000", "# c #000000", ". c None", "...###..........................", "...#aa##........................", ".###baaa##......................", ".#cde#baaa##....................", ".#cccdeebaaa##..##f.............", ".#cccccdeebaaa##aaa##...........", ".#cccccccdeebaaaaaaaa##.........", ".#cccccccccdeebaaaaaaa#.........", ".#cccccgcgghhebbbbaaaaa#........", ".#ccccccgcgggdebbbbbbaa#........", ".#cccgcgcgcgghdeebiebbba#.......", ".#ccccgcggggggghdeddeeba#.......", ".#cgcgcgcggggggggghghdebb#......", ".#ccgcggggggggghghghghd#b#......", ".#cgcgcggggggggghghghhd#b#......", ".#gcggggggggghghghhhhhd#b#......", ".#cgcggggggggghghghhhhd#b#......", ".#ggggggggghghghhhhhhhdib#......", ".#gggggggggghghghhhhhhd#b#......", ".#hhggggghghghhhhhhhhhd#b#......", ".#ddhhgggghghghhhhhhhhd#b#......", "..##ddhhghghhhhhhhhhhhdeb#......", "....##ddhhhghhhhhhhhhhd#b#......", "......##ddhhhhhhhhhhhhd#b#......", "........##ddhhhhhhhhhhd#b#......", "..........##ddhhhhhhhhd#b#......", "............##ddhhhhhhd#b###....", "..............##ddhhhhd#b#####..", "................##ddhhd#b######.", "..................##dddeb#####..", "....................##d#b###....", "......................####......"};static QPixmap *unknown_icon = 0;static QPixmap *qiv_buffer_pixmap = 0;#if !defined(Q_WS_X11)static QPixmap *qiv_selection = 0;#endifstatic bool optimize_layout = false;static Q3CleanupHandler<QPixmap> qiv_cleanup_pixmap;static QPixmap *get_qiv_buffer_pixmap(const QSize &s){ if (!qiv_buffer_pixmap) { qiv_buffer_pixmap = new QPixmap(s); qiv_cleanup_pixmap.add(&qiv_buffer_pixmap); return qiv_buffer_pixmap; } qiv_buffer_pixmap->resize(s); return qiv_buffer_pixmap;}#ifndef QT_NO_DRAGANDDROPclass Q_COMPAT_EXPORT Q3IconDragData{public: Q3IconDragData(); Q3IconDragData(const QRect &ir, const QRect &tr); QRect pixmapRect() const; QRect textRect() const; void setPixmapRect(const QRect &r); void setTextRect(const QRect &r); QRect iconRect_, textRect_; QString key_; bool operator==(const Q3IconDragData &i) const;};class Q_COMPAT_EXPORT Q3IconDragDataItem{public: Q3IconDragDataItem() {} Q3IconDragDataItem(const Q3IconDragItem &i1, const Q3IconDragData &i2) : data(i1), item(i2) {} Q3IconDragItem data; Q3IconDragData item; bool operator== (const Q3IconDragDataItem&) const;};class Q3IconDragPrivate{public: QLinkedList<Q3IconDragDataItem> items; static bool decode(QMimeSource* e, QLinkedList<Q3IconDragDataItem> &lst);};#endifclass Q3IconViewPrivate{public: Q3IconViewItem *firstItem, *lastItem; uint count; Q3IconView::SelectionMode selectionMode; Q3IconViewItem *currentItem, *tmpCurrentItem, *highlightedItem, *startDragItem, *pressedItem, *selectAnchor, *renamingItem; QRect *rubber; QTimer *scrollTimer, *adjustTimer, *updateTimer, *inputTimer, *fullRedrawTimer; int rastX, rastY, spacing; int dragItems; QPoint oldDragPos; Q3IconView::Arrangement arrangement; Q3IconView::ResizeMode resizeMode; QSize oldSize;#ifndef QT_NO_DRAGANDDROP QLinkedList<Q3IconDragDataItem> iconDragData;#endif int numDragItems, cachedW, cachedH; int maxItemWidth, maxItemTextLength; QPoint dragStart; QString currInputString; Q3IconView::ItemTextPos itemTextPos;#ifndef QT_NO_CURSOR QCursor oldCursor;#endif int cachedContentsX, cachedContentsY; QBrush itemTextBrush; QRegion clipRegion; QPoint dragStartPos; QFontMetrics *fm; int minLeftBearing, minRightBearing; uint mousePressed : 1; uint cleared : 1; uint dropped : 1; uint clearing : 1; uint oldDragAcceptAction : 1; uint isIconDrag : 1; uint drawDragShapes : 1; uint dirty : 1; uint rearrangeEnabled : 1; uint reorderItemsWhenInsert : 1; uint drawAllBack : 1; uint resortItemsWhenInsert : 1; uint sortDirection : 1; uint wordWrapIconText : 1; uint containerUpdateLocked : 1; uint firstSizeHint : 1; uint showTips : 1; uint pressedSelected : 1; uint dragging : 1; uint drawActiveSelection : 1; uint inMenuMode : 1; QPoint dragPos; QPixmapCache maskCache; QHash<Q3IconViewItem *, Q3IconViewItem *> selectedItems; struct ItemContainer { ItemContainer(ItemContainer *pr, ItemContainer *nx, const QRect &r) : p(pr), n(nx), rect(r) { if (p) p->n = this; if (n) n->p = this; } ItemContainer *p, *n; QRect rect; QList<Q3IconViewItem*> items; } *firstContainer, *lastContainer; struct SortableItem { Q3IconViewItem *item; };public: /* finds the containers that intersect with \a searchRect in the direction \a dir relative to \a relativeTo */ QList<ItemContainer* >* findContainers( Q3IconView:: Direction dir, const QPoint &relativeTo, const QRect &searchRect) const; // friend int cmpIconViewItems(const void *n1, const void *n2);};QList<Q3IconViewPrivate::ItemContainer *>* Q3IconViewPrivate::findContainers( Q3IconView:: Direction dir, const QPoint &relativeTo, const QRect &searchRect) const{ QList<Q3IconViewPrivate::ItemContainer *>* list = new QList<Q3IconViewPrivate::ItemContainer*>(); if (arrangement == Q3IconView::LeftToRight) { if (dir == Q3IconView::DirLeft || dir == Q3IconView::DirRight) { ItemContainer *c = firstContainer; for (; c; c = c->n) if (c->rect.intersects(searchRect)) list->append(c); } else { if (dir == Q3IconView::DirDown) { ItemContainer *c = firstContainer; for (; c; c = c->n) if (c->rect.intersects(searchRect) && c->rect.bottom() >= relativeTo.y()) list->append(c); } else { ItemContainer *c = lastContainer; for (; c; c = c->p) if (c->rect.intersects(searchRect) && c->rect.top() <= relativeTo.y()) list->append(c); } } } else { if (dir == Q3IconView::DirUp || dir == Q3IconView::DirDown) { ItemContainer *c = firstContainer; for (; c; c = c->n) if (c->rect.intersects(searchRect)) list->append(c); } else { if (dir == Q3IconView::DirRight) { ItemContainer *c = firstContainer; for (; c; c = c->n) if (c->rect.intersects(searchRect) && c->rect.right() >= relativeTo.x()) list->append(c); } else { ItemContainer *c = lastContainer; for (; c; c = c->p) if (c->rect.intersects(searchRect) && c->rect.left() <= relativeTo.x()) list->append(c); } } } return list;}#if defined(Q_C_CALLBACKS)extern "C" {#endif#ifdef Q_OS_TEMPstatic int _cdecl cmpIconViewItems(const void *n1, const void *n2)#elsestatic int cmpIconViewItems(const void *n1, const void *n2)#endif{ if (!n1 || !n2) return 0; Q3IconViewPrivate::SortableItem *i1 = (Q3IconViewPrivate::SortableItem *)n1; Q3IconViewPrivate::SortableItem *i2 = (Q3IconViewPrivate::SortableItem *)n2; return i1->item->compare(i2->item);}#if defined(Q_C_CALLBACKS)}#endifclass Q3IconViewItemPrivate{public: Q3IconViewPrivate::ItemContainer *container1, *container2;};#ifndef QT_NO_TEXTEDITclass Q3IconViewItemLineEdit : public Q3TextEdit{ friend class Q3IconViewItem;public: Q3IconViewItemLineEdit(const QString &text, QWidget *parent, Q3IconViewItem *theItem, const char* name=0);protected: void keyPressEvent(QKeyEvent *e); void focusOutEvent(QFocusEvent *e);protected: Q3IconViewItem *item; QString startText;private: Q_DISABLE_COPY(Q3IconViewItemLineEdit)};Q3IconViewItemLineEdit::Q3IconViewItemLineEdit(const QString &text, QWidget *parent, Q3IconViewItem *theItem, const char *name) : Q3TextEdit(parent, name), item(theItem), startText(text){ setFrameStyle(QFrame::Plain | QFrame::Box); setLineWidth(1); setHScrollBarMode(AlwaysOff); setVScrollBarMode(AlwaysOff); setWordWrap(WidgetWidth); setWrapColumnOrWidth(item->iconView()->maxItemWidth() - (item->iconView()->itemTextPos() == Q3IconView::Bottom ? 0 : item->pixmapRect().width())); document()->formatter()->setAllowBreakInWords(true); resize(200, 200); // ### some size, there should be a forceReformat() setTextFormat(Qt::PlainText); setText(text); setAlignment(Qt::AlignCenter); resize(wrapColumnOrWidth() + 2, heightForWidth(wrapColumnOrWidth()) + 2);}void Q3IconViewItemLineEdit::keyPressEvent(QKeyEvent *e){ if (e->key() == Qt::Key_Escape) { item->Q3IconViewItem::setText(startText); item->cancelRenameItem(); } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) { item->renameItem(); } else { Q3TextEdit::keyPressEvent(e); sync(); resize(width(), document()->height() + 2); }}void Q3IconViewItemLineEdit::focusOutEvent(QFocusEvent *e){ Q_UNUSED(e) // I need this to get rid of a Borland warning if (e->reason() != Qt::PopupFocusReason) item->cancelRenameItem();}#endif#ifndef QT_NO_DRAGANDDROP/*! \class Q3IconDragItem \brief The Q3IconDragItem class encapsulates a drag item. \compat The Q3IconDrag class uses a list of Q3IconDragItems to support drag and drop operations. In practice a Q3IconDragItem object (or an object of a class derived from Q3IconDragItem) is created for each icon view item which is dragged. Each of these Q3IconDragItems is stored in a Q3IconDrag object. See Q3IconView::dragObject() for more information.*//*! Constructs a Q3IconDragItem with no data.*/Q3IconDragItem::Q3IconDragItem(){ ba = "no data";}/*! Destructor.*/Q3IconDragItem::~Q3IconDragItem(){}/*! Returns the data contained in the Q3IconDragItem.*/QByteArray Q3IconDragItem::data() const{ return ba;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -