📄 q3iconview.cpp
字号:
}/*! Sets this item to be selectable if \a enable is true (the default) or unselectable if \a enable is false. The user is unable to select a non-selectable item using either the keyboard or the mouse. (The application programmer can select an item in code regardless of this setting.) \sa isSelectable()*/void Q3IconViewItem::setSelectable(bool enable){ selectable = (uint)enable;}/*! Returns true if the item is selected; otherwise returns false. \sa setSelected()*/bool Q3IconViewItem::isSelected() const{ return (bool)selected;}/*! Returns true if the item is selectable; otherwise returns false. \sa setSelectable()*/bool Q3IconViewItem::isSelectable() const{ return (bool)selectable;}/*! Repaints the item.*/void Q3IconViewItem::repaint(){ if (view) view->repaintItem(this);}/*! Moves the item to position (\a x, \a y) in the icon view (these are contents coordinates). Returns true if the item is moved. Returns false if the item is already at the specified position.*/bool Q3IconViewItem::move(int x, int y){ if (x == this->x() && y == this->y()) return false; itemRect.setRect(x, y, itemRect.width(), itemRect.height()); checkRect(); if (view) view->updateItemContainer(this); return true;}/*! Moves the item \a dx pixels in the x-direction and \a dy pixels in the y-direction.*/void Q3IconViewItem::moveBy(int dx, int dy){ itemRect.moveBy(dx, dy); checkRect(); if (view) view->updateItemContainer(this);}/*! \overload Moves the item to the point \a pnt.*/bool Q3IconViewItem::move(const QPoint &pnt){ return move(pnt.x(), pnt.y());}/*! \overload Moves the item by the x, y values in point \a pnt.*/void Q3IconViewItem::moveBy(const QPoint &pnt){ moveBy(pnt.x(), pnt.y());}/*! Returns the bounding rectangle of the item (in contents coordinates).*/QRect Q3IconViewItem::rect() const{ return itemRect;}/*! Returns the x-coordinate of the item (in contents coordinates).*/int Q3IconViewItem::x() const{ return itemRect.x();}/*! Returns the y-coordinate of the item (in contents coordinates).*/int Q3IconViewItem::y() const{ return itemRect.y();}/*! Returns the width of the item.*/int Q3IconViewItem::width() const{ return qMax(itemRect.width(), QApplication::globalStrut().width());}/*! Returns the height of the item.*/int Q3IconViewItem::height() const{ return qMax(itemRect.height(), QApplication::globalStrut().height());}/*! Returns the size of the item.*/QSize Q3IconViewItem::size() const{ return QSize(itemRect.width(), itemRect.height());}/*! Returns the position of the item (in contents coordinates).*/QPoint Q3IconViewItem::pos() const{ return QPoint(itemRect.x(), itemRect.y());}/*! Returns the bounding rectangle of the item's text. If \a relative is true, (the default), the returned rectangle is relative to the origin of the item's rectangle. If \a relative is false, the returned rectangle is relative to the origin of the icon view's contents coordinate system.*/QRect Q3IconViewItem::textRect(bool relative) const{ if (relative) return itemTextRect; else return QRect(x() + itemTextRect.x(), y() + itemTextRect.y(), itemTextRect.width(), itemTextRect.height());}/*! Returns the bounding rectangle of the item's icon. If \a relative is true, (the default), the rectangle is relative to the origin of the item's rectangle. If \a relative is false, the returned rectangle is relative to the origin of the icon view's contents coordinate system.*/QRect Q3IconViewItem::pixmapRect(bool relative) const{ if (relative) return itemIconRect; else return QRect(x() + itemIconRect.x(), y() + itemIconRect.y(), itemIconRect.width(), itemIconRect.height());}/*! Returns true if the item contains the point \a pnt (in contents coordinates); otherwise returns false.*/bool Q3IconViewItem::contains(const QPoint& pnt) const{ QRect textArea = textRect(false); QRect pixmapArea = pixmapRect(false); if (iconView()->itemTextPos() == Q3IconView::Bottom) textArea.setTop(pixmapArea.bottom()); else textArea.setLeft(pixmapArea.right()); return textArea.contains(pnt) || pixmapArea.contains(pnt);}/*! Returns true if the item intersects the rectangle \a r (in contents coordinates); otherwise returns false.*/bool Q3IconViewItem::intersects(const QRect& r) const{ return (textRect(false).intersects(r) || pixmapRect(false).intersects(r));}/*! \fn bool Q3IconViewItem::acceptDrop(const QMimeSource *mime) const Returns true if you can drop things with a QMimeSource of \a mime onto this item; otherwise returns false. The default implementation always returns false. You must subclass Q3IconViewItem and reimplement acceptDrop() to accept drops.*/bool Q3IconViewItem::acceptDrop(const QMimeSource *) const{ return false;}#ifndef QT_NO_TEXTEDIT/*! Starts in-place renaming of an icon, if allowed. This function sets up the icon view so that the user can edit the item text, and then returns. When the user is done, setText() will be called and Q3IconView::itemRenamed() will be emitted (unless the user canceled, e.g. by pressing the Escape key). \sa setRenameEnabled()*/void Q3IconViewItem::rename(){ if (!view) return; if (renameBox) removeRenameBox(); oldRect = rect(); renameBox = new Q3IconViewItemLineEdit(itemText, view->viewport(), this, "qt_renamebox"); iconView()->ensureItemVisible(this); QRect tr(textRect(false)); view->addChild(renameBox, tr.x() + (tr.width() / 2 - renameBox->width() / 2), tr.y() - 3); renameBox->selectAll(); view->viewport()->setFocusProxy(renameBox); renameBox->setFocus(); renameBox->show(); Q_ASSERT(view->d->renamingItem == 0L); view->d->renamingItem = this;}#endif/*! Compares this icon view item to \a i. Returns -1 if this item is less than \a i, 0 if they are equal, and 1 if this icon view item is greater than \a i. The default implementation compares the item keys (key()) using QString::localeAwareCompare(). A reimplementation may use different values and a different comparison function. Here is a reimplementation that uses plain Unicode comparison: \code int MyIconViewItem::compare(Q3IconViewItem *i) const { return key().compare(i->key()); } \endcode \sa key() QString::localeAwareCompare() QString::compare()*/int Q3IconViewItem::compare(Q3IconViewItem *i) const{ return key().localeAwareCompare(i->key());}#ifndef QT_NO_TEXTEDIT/*! This private function is called when the user pressed Return during in-place renaming.*/void Q3IconViewItem::renameItem(){ if (!renameBox || !view) return; if (!view->d->wordWrapIconText) { wordWrapDirty = true; calcRect(); } QRect r = itemRect; setText(renameBox->text()); view->repaintContents(oldRect.x() - 1, oldRect.y() - 1, oldRect.width() + 2, oldRect.height() + 2); view->repaintContents(r.x() - 1, r.y() - 1, r.width() + 2, r.height() + 2); removeRenameBox(); view->emitRenamed(this);}/*! Cancels in-place renaming.*/void Q3IconViewItem::cancelRenameItem(){ if (!view) return; QRect r = itemRect; calcRect(); view->repaintContents(oldRect.x() - 1, oldRect.y() - 1, oldRect.width() + 2, oldRect.height() + 2); view->repaintContents(r.x() - 1, r.y() - 1, r.width() + 2, r.height() + 2); if (!renameBox) return; removeRenameBox();}/*! Removes the editbox that is used for in-place renaming.*/void Q3IconViewItem::removeRenameBox(){ if (!renameBox || !view) return; bool resetFocus = view->viewport()->focusProxy() == renameBox; renameBox->hide(); renameBox->deleteLater(); renameBox = 0; if (resetFocus) { view->viewport()->setFocusProxy(view); view->setFocus(); } Q_ASSERT(view->d->renamingItem == this); view->d->renamingItem = 0L;}#endif/*! This virtual function is responsible for calculating the rectangles returned by rect(), textRect() and pixmapRect(). setRect(), setTextRect() and setPixmapRect() are provided mainly for reimplementations of this function. \a text_ is an internal parameter which defaults to an empty string.*/void Q3IconViewItem::calcRect(const QString &text_){ if (!view) // ##### return; wordWrapDirty = true; int pw = 0; int ph = 0;#ifndef QT_NO_PICTURE if (picture()) { QRect br = picture()->boundingRect(); pw = br.width() + 2; ph = br.height() + 2; } else#endif { pw = (pixmap() ? pixmap() : unknown_icon)->width() + 2; ph = (pixmap() ? pixmap() : unknown_icon)->height() + 2; } itemIconRect.setWidth(pw); itemIconRect.setHeight(ph); calcTmpText(); QString t = text_; if (t.isEmpty()) { if (view->d->wordWrapIconText) t = itemText; else t = tmpText; } int tw = 0; int th = 0; // ##### TODO: fix font bearings! QRect r; if (view->d->wordWrapIconText) { r = QRect(view->d->fm->boundingRect(0, 0, iconView()->maxItemWidth() - (iconView()->itemTextPos() == Q3IconView::Bottom ? 0 : pixmapRect().width()), 0xFFFFFFFF, Qt::AlignHCenter | Qt::WordBreak | Qt::BreakAnywhere, t)); r.setWidth(r.width() + 4); } else { r = QRect(0, 0, view->d->fm->width(t), view->d->fm->height()); r.setWidth(r.width() + 4); } if (r.width() > iconView()->maxItemWidth() - (iconView()->itemTextPos() == Q3IconView::Bottom ? 0 : pixmapRect().width())) r.setWidth(iconView()->maxItemWidth() - (iconView()->itemTextPos() == Q3IconView::Bottom ? 0 : pixmapRect().width())); tw = r.width(); th = r.height(); if (tw < view->d->fm->width(QLatin1String("X"))) tw = view->d->fm->width(QLatin1String("X")); itemTextRect.setWidth(tw); itemTextRect.setHeight(th); int w = 0; int h = 0; if (view->itemTextPos() == Q3IconView::Bottom) { w = qMax(itemTextRect.width(), itemIconRect.width()); h = itemTextRect.height() + itemIconRect.height() + 1; itemRect.setWidth(w); itemRect.setHeight(h); itemTextRect = QRect((width() - itemTextRect.width()) / 2, height() - itemTextRect.height(), itemTextRect.width(), itemTextRect.height()); itemIconRect = QRect((width() - itemIconRect.width()) / 2, 0, itemIconRect.width(), itemIconRect.height()); } else { h = qMax(itemTextRect.height(), itemIconRect.height()); w = itemTextRect.width() + itemIconRect.width() + 1; itemRect.setWidth(w); itemRect.setHeight(h); itemTextRect = QRect(width() - itemTextRect.width(), (height() - itemTextRect.height()) / 2, itemTextRect.width(), itemTextRect.height()); itemIconRect = QRect(0, (height() - itemIconRect.height()) / 2, itemIconRect.width(), itemIconRect.height()); } if (view) view->updateItemContainer(this);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -