📄 q3iconview.cpp
字号:
/*! Paints the item using the painter \a p and the color group \a cg. If you want the item to be drawn with a different font or color, reimplement this function, change the values of the color group or the painter's font, and then call the Q3IconViewItem::paintItem() with the changed values.*/void Q3IconViewItem::paintItem(QPainter *p, const QColorGroup &cg){ if (!view) return; p->save(); if (isSelected()) { p->setPen(cg.highlightedText()); } else { p->setPen(cg.text()); } calcTmpText();#ifndef QT_NO_PICTURE if (picture()) { QPicture *pic = picture(); if (isSelected()) { p->fillRect(pixmapRect(false), QBrush(cg.highlight(), Qt::Dense4Pattern)); } p->drawPicture(x()-pic->boundingRect().x(), y()-pic->boundingRect().y(), *pic); if (isSelected()) { p->fillRect(textRect(false), cg.highlight()); p->setPen(QPen(cg.highlightedText())); } else if (view->d->itemTextBrush != QBrush(Qt::NoBrush)) p->fillRect(textRect(false), view->d->itemTextBrush); int align = view->itemTextPos() == Q3IconView::Bottom ? Qt::AlignHCenter : Qt::AlignAuto; if (view->d->wordWrapIconText) align |= Qt::WordBreak | Qt::BreakAnywhere; p->drawText(textRect(false), align, view->d->wordWrapIconText ? itemText : tmpText); p->restore(); return; }#endif bool textOnBottom = (view->itemTextPos() == Q3IconView::Bottom); int dim; if (textOnBottom) dim = (pixmap() ? pixmap() : unknown_icon)->width(); else dim = (pixmap() ? pixmap() : unknown_icon)->height(); if (isSelected()) { QPixmap *pix = pixmap() ? pixmap() : unknown_icon; if (pix && !pix->isNull()) { QPixmap *buffer = get_qiv_buffer_pixmap(pix->size()); QBitmap mask = view->mask(pix); QPainter p2(buffer); p2.fillRect(pix->rect(), Qt::white); p2.drawPixmap(0, 0, *pix); p2.end(); p2.begin(buffer); p2.fillRect(pix->rect(), QBrush(cg.highlight(), Qt::Dense4Pattern)); p2.end(); buffer->setMask(mask); QRect cr = pix->rect(); if (textOnBottom) p->drawPixmap(x() + (width() - dim) / 2, y(), *buffer, 0, 0, cr.width(), cr.height()); else p->drawPixmap(x() , y() + (height() - dim) / 2, *buffer, 0, 0, cr.width(), cr.height()); } } else { if (textOnBottom) p->drawPixmap(x() + (width() - dim) / 2, y(), *(pixmap() ? pixmap() : unknown_icon)); else p->drawPixmap(x() , y() + (height() - dim) / 2, *(pixmap() ? pixmap() : unknown_icon)); } p->save(); if (isSelected()) { p->fillRect(textRect(false), cg.highlight()); p->setPen(QPen(cg.highlightedText())); } else if (view->d->itemTextBrush != QBrush(Qt::NoBrush)) p->fillRect(textRect(false), view->d->itemTextBrush); int align = Qt::AlignHCenter; if (view->d->wordWrapIconText) align |= Qt::WordBreak | Qt::BreakAnywhere; p->drawText(textRect(false), align, view->d->wordWrapIconText ? itemText : tmpText); p->restore(); p->restore();}/*! Paints the focus rectangle of the item using the painter \a p and the color group \a cg.*/void Q3IconViewItem::paintFocus(QPainter *p, const QColorGroup &cg){ if (!view) return; QStyleOptionFocusRect opt; opt.rect = textRect(false); opt.palette = cg; if (isSelected()) { opt.state = QStyle::State_FocusAtBorder; opt.backgroundColor = cg.highlight(); } else { opt.state = QStyle::State_None; opt.backgroundColor = cg.base(); } view->style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p); if (this != view->d->currentItem) { opt.rect = pixmapRect(false); opt.backgroundColor = cg.base(); opt.state = QStyle::State_None; view->style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p); }}#ifndef QT_NO_DRAGANDDROP/*! \fn void Q3IconViewItem::dropped(QDropEvent *e, const Q3ValueList<Q3IconDragItem> &lst) This function is called when something is dropped on the item. \a e provides all the information about the drop. If the drag object of the drop was a Q3IconDrag, \a lst contains the list of the dropped items. You can get the data by calling Q3IconDragItem::data() on each item. If the \a lst is empty, i.e. the drag was not a Q3IconDrag, you must decode the data in \a e and work with that. The default implementation does nothing; subclasses may reimplement this function.*/void Q3IconViewItem::dropped(QDropEvent *, const Q3ValueList<Q3IconDragItem> &){}#endif/*! This function is called when a drag enters the item's bounding rectangle. The default implementation does nothing; subclasses may reimplement this function.*/void Q3IconViewItem::dragEntered(){}/*! This function is called when a drag leaves the item's bounding rectangle. The default implementation does nothing; subclasses may reimplement this function.*/void Q3IconViewItem::dragLeft(){}/*! Sets the bounding rectangle of the whole item to \a r. This function is provided for subclasses which reimplement calcRect(), so that they can set the calculated rectangle. \e{Any other use is discouraged.} \sa calcRect() textRect() setTextRect() pixmapRect() setPixmapRect()*/void Q3IconViewItem::setItemRect(const QRect &r){ itemRect = r; checkRect(); if (view) view->updateItemContainer(this);}/*! Sets the bounding rectangle of the item's text to \a r. This function is provided for subclasses which reimplement calcRect(), so that they can set the calculated rectangle. \e{Any other use is discouraged.} \sa calcRect() textRect() setItemRect() setPixmapRect()*/void Q3IconViewItem::setTextRect(const QRect &r){ itemTextRect = r; if (view) view->updateItemContainer(this);}/*! Sets the bounding rectangle of the item's icon to \a r. This function is provided for subclasses which reimplement calcRect(), so that they can set the calculated rectangle. \e{Any other use is discouraged.} \sa calcRect() pixmapRect() setItemRect() setTextRect()*/void Q3IconViewItem::setPixmapRect(const QRect &r){ itemIconRect = r; if (view) view->updateItemContainer(this);}/*! \internal*/void Q3IconViewItem::calcTmpText(){ if (!view || view->d->wordWrapIconText || !wordWrapDirty) return; wordWrapDirty = false; int w = iconView()->maxItemWidth() - (iconView()->itemTextPos() == Q3IconView::Bottom ? 0 : pixmapRect().width()); if (view->d->fm->width(itemText) < w) { tmpText = itemText; return; } tmpText = QLatin1String("..."); int i = 0; while (view->d->fm->width(tmpText + itemText[i]) < w) tmpText += itemText[i++]; tmpText.remove((uint)0, 3); tmpText += QLatin1String("...");}/*! \internal */QString Q3IconViewItem::tempText() const{ return tmpText;}void Q3IconViewItem::checkRect(){ int x = itemRect.x(); int y = itemRect.y(); int w = itemRect.width(); int h = itemRect.height(); bool changed = false; if (x < 0) { x = 0; changed = true; } if (y < 0) { y = 0; changed = true; } if (changed) itemRect.setRect(x, y, w, h);}/*! \class Q3IconView \brief The Q3IconView class provides an area with movable labelled icons. \compat A Q3IconView can display and manage a grid or other 2D layout of labelled icons. Each labelled icon is a Q3IconViewItem. Items (Q3IconViewItems) can be added or deleted at any time; items can be moved within the Q3IconView. Single or multiple items can be selected. Items can be renamed in-place. Q3IconView also supports \link #draganddrop drag and drop\endlink. Each item contains a label string, a pixmap or picture (the icon itself) and optionally a sort key. The sort key is used for sorting the items and defaults to the label string. The label string can be displayed below or to the right of the icon (see \l ItemTextPos). The simplest way to create a Q3IconView is to create a Q3IconView object and create some Q3IconViewItems with the Q3IconView as their parent, set the icon view's geometry and show it. For example: \code Q3IconView *iv = new Q3IconView(this); QDir dir(path, "*.xpm"); for (uint i = 0; i < dir.count(); i++) { (void) new Q3IconViewItem(iv, dir[i], QPixmap(path + dir[i])); } iv->resize(600, 400); iv->show(); \endcode The Q3IconViewItem call passes a pointer to the Q3IconView we wish to populate, along with the label text and a QPixmap. When an item is inserted the Q3IconView allocates a position for it. Existing items are rearranged if autoArrange() is true. The default arrangement is \l LeftToRight -- the Q3IconView fills up the \e left-most column from top to bottom, then moves one column \e right and fills that from top to bottom and so on. The arrangement can be modified with any of the following approaches: \list \i Call setArrangement(), e.g. with \l TopToBottom which will fill the \e top-most row from left to right, then moves one row \e down and fills that row from left to right and so on. \i Construct each Q3IconViewItem using a constructor which allows you to specify which item the new one is to follow. \i Call setSorting() or sort() to sort the items. \endlist The spacing between items is set with setSpacing(). Items can be laid out using a fixed grid using setGridX() and setGridY(); by default the Q3IconView calculates a grid dynamically. The position of items' label text is set with setItemTextPos(). The text's background can be set with setItemTextBackground(). The maximum width of an item and of its text are set with setMaxItemWidth() and setMaxItemTextLength(). The label text will be word-wrapped if it is too long; this is controlled by setWordWrapIconText(). If the label text is truncated, the user can still see the entire text in a tool tip if they hover the mouse over the item. This is controlled with setShowToolTips(). Items which are \link Q3IconViewItem::isSelectable() selectable\endlink may be selected depending on the SelectionMode; the default is \l Single. Because Q3IconView offers multiple selection it must display keyboard focus and selection state separately. Therefore there are functions to set the selection state of an item (setSelected()) and to select which item displays keyboard focus (setCurrentItem()). When multiple items may be selected the icon view provides a rubberband, too. When in-place renaming is enabled (it is disabled by default), the user may change the item's label. They do this by selecting the item (single clicking it or navigating to it with the arrow keys), then single clicking it (or pressing F2), and entering their text. If no key has been set with Q3IconViewItem::setKey() the new text will also serve as the key. (See Q3IconViewItem::setRenameEnabled().) You can control whether users can move items themselves with setItemsMovable(). Because the internal structure used to store the icon view items is linear, no iterator class is needed to iterate over all the items. Instead we iterate by getting the first item from the \e{icon view} and then each subsequent (\l Q3IconViewItem::nextItem()) from each \e item in turn: \code for (Q3IconViewItem *item = iv->firstItem(); item; item = item->nextItem()) do_something(item); \endcode Q3IconView also provides currentItem(). You can search for an item using findItem() (searching by position or for label text) and with findFirstVisibleItem() and findLastVisibleItem(). The number of items is returned by count(). An item can be removed from an icon view using takeItem(); to delete an item use \c delete. All the items can be deleted with clear(). The Q3IconView emits a wide range of useful signals, including selectionChanged(), currentChanged(), clicked(), moved() and itemRenamed(). \target draganddrop \section1 Drag and Drop Q3IconView supports the drag and drop of items within the Q3IconView itself. It also supports the drag and drop of items out of or into the Q3IconView and drag and drop onto items themselves. The drag and drop of items outside the Q3IconView can be achieved in a simple way with basic functionality, or in a more sophisticated way which provides more power and control. The simple approach to dragging items out of the icon view is to subclass Q3IconView and reimplement Q3IconView::dragObject(). \code Q3DragObject *MyIconView::dragObject() { return new Q3TextDrag(currentItem()->text(), this); } \endcode In this example we create a Q3TextDrag object, (derived from Q3DragObject), containing the item's label and return it as the drag object. We could just as easily have created a Q3ImageDrag from the item's pixmap and returned that instead. Q3IconViews and their Q3IconViewItems can also be the targets of drag and drops. To make the Q3IconView itself able to accept drops connect to the dropped() signal. When a drop occurs this signal will be emitted with a QDragEvent and a QLinkedList of Q3IconDragItems. To make a Q3IconViewItem into a drop target subclass Q3IconViewItem and reimplement Q3IconViewItem::acceptDrop() and Q3IconViewItem::dropped(). \code bool MyIconViewItem::acceptDrop(const QMimeSource *mime) const { if (mime->provides("text/plain")) return true; return false; } void MyIconViewItem::dropped(QDropEvent *evt, const Q3ValueList<Q3IconDragItem>&) { QString label; if (Q3TextDrag::decode(evt, label)) setText(label); } \endcode If you want to use extended drag-and-drop or have drag shapes drawn you must take a more sophisticated approach. The first part is starting drags -- you should use a Q3IconDrag (or a class derived from it) for the drag object. In dragObject() create the drag object, populate it with Q3IconDragItems and return it. Normally such a drag should offer each selected item's data. So in dragObject() you should iterate over all the items, and create a Q3IconDragItem for each selected item, and append these items with Q3IconDrag::append() to the Q3IconDrag object. You can use Q3IconDragItem::setData() to set the data of each item that should be dragged. If you want to offer the data in additional mime-types, it's best to use a class derived from Q3IconDrag, which implements additional encoding and decoding functions. When a drag enters the icon view, there is little to do. Simply connect to the dropped() signal and reimplement Q3IconViewItem::acceptDrop() and Q3IconViewItem::dropped(). If you've used a Q3IconDrag (or a subclass of it) the second argument to the dropped signal contains a QLinkedList of Q3IconDragItems -- you can access their data by calling Q3IconDragItem::data() on each one. For an example implementation of complex drag-and-drop look at the fileiconview example (qt/examples/fileiconview). \sa Q3IconViewItem::setDragEnabled(), Q3IconViewItem::setDropEnabled(), Q3IconViewItem::acceptDrop(), Q3IconViewItem::dropped()*//*! \enum Q3IconView::ResizeMode This enum type is used to tell Q3IconView how it should treat the positions of its icons when the widget is resized. The modes are: \value Fixed The icons' positions are not changed. \value Adjust The icons' positions are adjusted to be within the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -