From acc60fb6b2504ae3148865b63603bca76e280fee Mon Sep 17 00:00:00 2001 From: deepin-ci-robot Date: Mon, 14 Oct 2024 13:23:08 +0000 Subject: [PATCH] sync: from linuxdeepin/dtkwidget Synchronize source files from linuxdeepin/dtkwidget. Source-pull-request: https://github.com/linuxdeepin/dtkwidget/pull/611 --- src/widgets/dlabel.cpp | 174 ++++++++++++++++++++------------ src/widgets/dsimplelistview.cpp | 8 +- 2 files changed, 111 insertions(+), 71 deletions(-) diff --git a/src/widgets/dlabel.cpp b/src/widgets/dlabel.cpp index c00936fb5..8b03aaa5a 100644 --- a/src/widgets/dlabel.cpp +++ b/src/widgets/dlabel.cpp @@ -28,8 +28,7 @@ DWIDGET_BEGIN_NAMESPACE \a parent The argument is sent to the QLabel constructor. */ DLabel::DLabel(QWidget *parent, Qt::WindowFlags f) - : QLabel(parent, f) - , DObject(*new DLabelPrivate(this)) + : QLabel(parent, f), DObject(*new DLabelPrivate(this)) { D_D(DLabel); d->init(); @@ -42,8 +41,7 @@ DLabel::DLabel(QWidget *parent, Qt::WindowFlags f) \a parent Specifying the parent object. */ DLabel::DLabel(const QString &text, QWidget *parent) - : QLabel(text, parent) - , DObject(*new DLabelPrivate(this)) + : QLabel(text, parent), DObject(*new DLabelPrivate(this)) { D_D(DLabel); d->init(); @@ -51,7 +49,6 @@ DLabel::DLabel(const QString &text, QWidget *parent) DLabel::~DLabel() { - } /*! @@ -86,7 +83,8 @@ void DLabel::setForegroundRole(DPalette::ColorType color) void DLabel::setElideMode(Qt::TextElideMode elideMode) { D_D(DLabel); - if (d->elideMode == elideMode) { + if (d->elideMode == elideMode) + { return; } d->elideMode = elideMode; @@ -111,8 +109,7 @@ Qt::TextElideMode DLabel::elideMode() const \a parent Parent control */ DLabel::DLabel(DLabelPrivate &dd, QWidget *parent) - : QLabel(parent) - , DObject(dd) + : QLabel(parent), DObject(dd) { dd.init(); } @@ -126,7 +123,8 @@ void DLabel::initPainter(QPainter *painter) const { D_DC(DLabel); QLabel::initPainter(painter); - if (d->color != DPalette::NoType) { + if (d->color != DPalette::NoType) + { QBrush color = DPaletteHelper::instance()->palette(this).brush(d->color); painter->setPen(QPen(color.color())); } @@ -141,17 +139,19 @@ void DLabel::initPainter(QPainter *painter) const void DLabel::paintEvent(QPaintEvent *event) { Q_UNUSED(event) - QLabelPrivate *d = static_cast(d_ptr.data()); + QLabelPrivate *d = static_cast(d_ptr.data()); QStyle *style = QWidget::style(); QPainter painter(this); drawFrame(&painter); QRect cr = contentsRect(); cr.adjust(d->margin, d->margin, -d->margin, -d->margin); int align = QStyle::visualAlignment(d->isTextLabel ? DLabelPrivate::textDirection(d) - : layoutDirection(), QFlag(d->align)); + : layoutDirection(), + QFlag(d->align)); #if QT_CONFIG(movie) - if (d->movie && !d->movie->currentPixmap().isNull()) { + if (d->movie && !d->movie->currentPixmap().isNull()) + { if (d->scaledcontents) style->drawItemPixmap(&painter, cr, align, d->movie->currentPixmap().scaled(cr.size())); else @@ -159,16 +159,18 @@ void DLabel::paintEvent(QPaintEvent *event) } else #endif - if (d->isTextLabel) { + if (d->isTextLabel) + { QRectF lr = DLabelPrivate::layoutRect(d).toAlignedRect(); QStyleOption opt; opt.initFrom(this); - if (d->control) { + if (d->control) + { #ifndef QT_NO_SHORTCUT const bool underline = (bool)style->styleHint(QStyle::SH_UnderlineShortcut, 0, this, 0); - if (d->shortcutId != 0 - && underline != d->shortcutCursor.charFormat().fontUnderline()) { + if (d->shortcutId != 0 && underline != d->shortcutCursor.charFormat().fontUnderline()) + { QTextCharFormat fmt; fmt.setFontUnderline(underline); d->shortcutCursor.mergeCharFormat(fmt); @@ -179,9 +181,12 @@ void DLabel::paintEvent(QPaintEvent *event) // Adjust the palette context.palette = opt.palette; - if (d_func()->color != DPalette::NoType) { + if (d_func()->color != DPalette::NoType) + { context.palette.setBrush(QPalette::Text, DPaletteHelper::instance()->palette(this).brush(d_func()->color)); - } else if (foregroundRole() != QPalette::Text && isEnabled()) { + } + else if (foregroundRole() != QPalette::Text && isEnabled()) + { context.palette.setColor(QPalette::Text, context.palette.color(foregroundRole())); } @@ -191,10 +196,13 @@ void DLabel::paintEvent(QPaintEvent *event) d->control->setPalette(context.palette); d->control->drawContents(&painter, QRectF(), this); painter.restore(); - } else { + } + else + { int flags = align | (DLabelPrivate::textDirection(d) == Qt::LeftToRight ? Qt::TextForceLeftToRight : Qt::TextForceRightToLeft); - if (d->hasShortcut) { + if (d->hasShortcut) + { flags |= Qt::TextShowMnemonic; if (!style->styleHint(QStyle::SH_UnderlineShortcut, &opt, this)) flags |= Qt::TextHideMnemonic; @@ -202,22 +210,26 @@ void DLabel::paintEvent(QPaintEvent *event) QPalette palette = opt.palette; - if (d_func()->color != DPalette::NoType) { + if (d_func()->color != DPalette::NoType) + { palette.setBrush(foregroundRole(), DPaletteHelper::instance()->palette(this).brush(d_func()->color)); } QString text = d->text; - if (elideMode() != Qt::ElideNone) { + if (elideMode() != Qt::ElideNone) + { const QFontMetrics fm(fontMetrics()); text = fm.elidedText(text, elideMode(), width(), Qt::TextShowMnemonic); } const DToolTip::ToolTipShowMode &toolTipShowMode = DToolTip::toolTipShowMode(this); - if (toolTipShowMode != DToolTip::Default) { - const bool showToolTip = (toolTipShowMode == DToolTip::AlwaysShow) - || ((toolTipShowMode == DToolTip::ShowWhenElided) && (d->text != text)); - if (DToolTip::needUpdateToolTip(this, showToolTip)) { + if (toolTipShowMode != DToolTip::Default) + { + const bool showToolTip = (toolTipShowMode == DToolTip::AlwaysShow) || ((toolTipShowMode == DToolTip::ShowWhenElided) && (d->text != text)); + if (DToolTip::needUpdateToolTip(this, showToolTip)) + { QString toolTip; - if (showToolTip) { + if (showToolTip) + { QTextOption textOption; textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); textOption.setTextDirection(opt.direction); @@ -230,47 +242,60 @@ void DLabel::paintEvent(QPaintEvent *event) } style->drawItemText(&painter, lr.toRect(), flags, palette, isEnabled(), text, foregroundRole()); } - } else + } + else #ifndef QT_NO_PICTURE - if (d->picture) { + if (d->picture) + { QRect br = d->picture->boundingRect(); int rw = br.width(); int rh = br.height(); - if (d->scaledcontents) { + if (d->scaledcontents) + { painter.save(); painter.translate(cr.x(), cr.y()); - painter.scale((double)cr.width()/rw, (double)cr.height()/rh); + painter.scale((double)cr.width() / rw, (double)cr.height() / rh); painter.drawPicture(-br.x(), -br.y(), *d->picture); painter.restore(); - } else { + } + else + { int xo = 0; int yo = 0; if (align & Qt::AlignVCenter) - yo = (cr.height()-rh)/2; + yo = (cr.height() - rh) / 2; else if (align & Qt::AlignBottom) - yo = cr.height()-rh; + yo = cr.height() - rh; if (align & Qt::AlignRight) - xo = cr.width()-rw; + xo = cr.width() - rw; else if (align & Qt::AlignHCenter) - xo = (cr.width()-rw)/2; - painter.drawPicture(cr.x()+xo-br.x(), cr.y()+yo-br.y(), *d->picture); + xo = (cr.width() - rw) / 2; + painter.drawPicture(cr.x() + xo - br.x(), cr.y() + yo - br.y(), *d->picture); } - } else + } + else #endif - if (d->pixmap && !d->pixmap->isNull()) { + if (d->pixmap && !d->pixmap->isNull()) + { QPixmap pix; - if (d->scaledcontents) { + if (d->scaledcontents) + { QSize scaledSize = cr.size() * devicePixelRatioF(); - if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) { + if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) + { #if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) if (!d->cachedimage) +#endif #if QT_VERSION < QT_VERSION_CHECK(6, 4, 2) d->cachedimage = new QImage(d->pixmap->toImage()); delete d->scaledpixmap; #else - d->cachedimage = QImage(d->pixmap->toImage()); +#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) + d->cachedimage = QImage(d->pixmap->toImage()); +#endif d->scaledpixmap.reset(); #endif +#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) QImage scaledImage = d->cachedimage->scaled(scaledSize, #else @@ -278,17 +303,22 @@ void DLabel::paintEvent(QPaintEvent *event) d->scaledpixmap = d->pixmap->scaled(scaledSize, #endif Qt::IgnoreAspectRatio, Qt::SmoothTransformation); -#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) +#else + d->scaledpixmap = d->pixmap->scaled(scaledSize, + Qt::IgnoreAspectRatio, Qt::SmoothTransformation); +#endif #if QT_VERSION < QT_VERSION_CHECK(6, 4, 2) d->scaledpixmap = new QPixmap(QPixmap::fromImage(scaledImage)); #else +#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) d->scaledpixmap = QPixmap(QPixmap::fromImage(scaledImage)); #endif #endif d->scaledpixmap->setDevicePixelRatio(devicePixelRatioF()); } pix = *d->scaledpixmap; - } else + } + else pix = *d->pixmap; QStyleOption opt; opt.initFrom(this); @@ -305,12 +335,12 @@ DLabelPrivate::DLabelPrivate(DLabel *q) void DLabelPrivate::init() { - } Qt::LayoutDirection DLabelPrivate::textDirection(QLabelPrivate *d) { - if (d->control) { + if (d->control) + { QTextOption opt = d->control->document()->defaultTextOption(); return opt.textDirection(); } @@ -320,16 +350,18 @@ Qt::LayoutDirection DLabelPrivate::textDirection(QLabelPrivate *d) QRectF DLabelPrivate::documentRect(QLabelPrivate *d) { - QLabel *q = qobject_cast(d->q_ptr); + QLabel *q = qobject_cast(d->q_ptr); Q_ASSERT_X(d->isTextLabel, "documentRect", "document rect called for label that is not a text label!"); QRect cr = q->contentsRect(); cr.adjust(d->margin, d->margin, -d->margin, -d->margin); const int align = QStyle::visualAlignment(d->isTextLabel ? textDirection(d) - : q->layoutDirection(), QFlag(d->align)); + : q->layoutDirection(), + QFlag(d->align)); int m = d->indent; if (m < 0 && q->frameWidth()) // no indent, but we do have a frame m = q->fontMetrics().horizontalAdvance(QLatin1Char('x')) / 2 - d->margin; - if (m > 0) { + if (m > 0) + { if (align & Qt::AlignLeft) cr.setLeft(cr.left() + m); if (align & Qt::AlignRight) @@ -352,56 +384,64 @@ QRectF DLabelPrivate::layoutRect(QLabelPrivate *d) qreal rh = d->control->document()->documentLayout()->documentSize().height(); qreal yo = 0; if (d->align & Qt::AlignVCenter) - yo = qMax((cr.height()-rh)/2, qreal(0)); + yo = qMax((cr.height() - rh) / 2, qreal(0)); else if (d->align & Qt::AlignBottom) - yo = qMax(cr.height()-rh, qreal(0)); + yo = qMax(cr.height() - rh, qreal(0)); return QRectF(cr.x(), yo + cr.y(), cr.width(), cr.height()); } void DLabelPrivate::ensureTextLayouted(QLabelPrivate *d) { - if (d->textLayoutDirty) { - if (d->textDirty) { - if (d->control) { + if (d->textLayoutDirty) + { + if (d->textDirty) + { + if (d->control) + { QTextDocument *doc = d->control->document(); - if (d->textDirty) { - #ifndef QT_NO_TEXTHTMLPARSER - #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) + if (d->textDirty) + { +#ifndef QT_NO_TEXTHTMLPARSER +#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) if (d->textformat == Qt::TextFormat::RichText) - #else +#else if (d->isRichText) - #endif +#endif doc->setHtml(d->text); else doc->setPlainText(d->text); - #else +#else doc->setPlainText(d->text); - #endif +#endif doc->setUndoRedoEnabled(false); - #ifndef QT_NO_SHORTCUT - if (d->hasShortcut) { +#ifndef QT_NO_SHORTCUT + if (d->hasShortcut) + { // Underline the first character that follows an ampersand (and remove the others ampersands) int from = 0; bool found = false; QTextCursor cursor; - while (!(cursor = d->control->document()->find((QLatin1String("&")), from)).isNull()) { + while (!(cursor = d->control->document()->find((QLatin1String("&")), from)).isNull()) + { cursor.deleteChar(); // remove the ampersand cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); from = cursor.position(); - if (!found && cursor.selectedText() != QLatin1String("&")) { //not a second & + if (!found && cursor.selectedText() != QLatin1String("&")) + { // not a second & found = true; d->shortcutCursor = cursor; } } } - #endif +#endif } } d->textDirty = false; } - if (d->control) { + if (d->control) + { QTextDocument *doc = d->control->document(); QTextOption opt = doc->defaultTextOption(); diff --git a/src/widgets/dsimplelistview.cpp b/src/widgets/dsimplelistview.cpp index d42e9d6a2..045dfaa8a 100644 --- a/src/widgets/dsimplelistview.cpp +++ b/src/widgets/dsimplelistview.cpp @@ -955,10 +955,10 @@ void DSimpleListView::mouseMoveEvent(QMouseEvent *mouseEvent) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) auto point = QPoint{mouseEvent->x() - columnRenderX, d->renderOffset + mouseEvent->y() - hoverItemIndex * d->rowHeight}; #else - auto point = QPoint{mouseEvent->position().x() - columnRenderX, + auto point = QPointF{mouseEvent->position().x() - columnRenderX, d->renderOffset + mouseEvent->position().y() - hoverItemIndex * d->rowHeight}; #endif - mouseHoverChanged(d->mouseHoverItem, item, columnCounter,point); + mouseHoverChanged(d->mouseHoverItem, item, columnCounter,point.toPoint()); d->mouseHoverItem = item; if (d->lastHoverItem == NULL || !item->sameAs(d->lastHoverItem) || columnCounter != d->lastHoverColumnIndex) { @@ -1235,10 +1235,10 @@ void DSimpleListView::mouseReleaseEvent(QMouseEvent *mouseEvent) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) auto point = QPoint{mouseEvent->x() - columnRenderX, d->renderOffset + mouseEvent->y() - releaseItemIndex * d->rowHeight}; #else - auto point = QPoint{mouseEvent->position().x() - columnRenderX, + auto point = QPointF{mouseEvent->position().x() - columnRenderX, d->renderOffset + mouseEvent->position().y() - releaseItemIndex * d->rowHeight}; #endif - mouseReleaseChanged((*d->renderItems)[releaseItemIndex], columnCounter, point); + mouseReleaseChanged((*d->renderItems)[releaseItemIndex], columnCounter, point.toPoint()); } }