📄 graphicscontextqt.cpp
字号:
void GraphicsContext::drawRect(const IntRect& rect){ if (paintingDisabled()) return; QPainter *p = m_data->p(); const bool antiAlias = p->testRenderHint(QPainter::Antialiasing); p->setRenderHint(QPainter::Antialiasing, m_data->antiAliasingForRectsAndLines); p->drawRect(rect); p->setRenderHint(QPainter::Antialiasing, antiAlias);}// FIXME: Now that this is refactored, it should be shared by all contexts.static void adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, const StrokeStyle& penStyle){ // For odd widths, we add in 0.5 to the appropriate x/y so that the float arithmetic // works out. For example, with a border width of 3, KHTML will pass us (y1+y2)/2, e.g., // (50+53)/2 = 103/2 = 51 when we want 51.5. It is always true that an even width gave // us a perfect position, but an odd width gave us a position that is off by exactly 0.5. if (penStyle == DottedStroke || penStyle == DashedStroke) { if (p1.x() == p2.x()) { p1.setY(p1.y() + strokeWidth); p2.setY(p2.y() - strokeWidth); } else { p1.setX(p1.x() + strokeWidth); p2.setX(p2.x() - strokeWidth); } } if (((int) strokeWidth) % 2) { if (p1.x() == p2.x()) { // We're a vertical line. Adjust our x. p1.setX(p1.x() + 0.5); p2.setX(p2.x() + 0.5); } else { // We're a horizontal line. Adjust our y. p1.setY(p1.y() + 0.5); p2.setY(p2.y() + 0.5); } }}// This is only used to draw borders.void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2){ if (paintingDisabled()) return; FloatPoint p1 = point1; FloatPoint p2 = point2; QPainter *p = m_data->p(); const bool antiAlias = p->testRenderHint(QPainter::Antialiasing); p->setRenderHint(QPainter::Antialiasing, m_data->antiAliasingForRectsAndLines); adjustLineToPixelBoundaries(p1, p2, strokeThickness(), strokeStyle()); IntSize shadowSize; int shadowBlur; Color shadowColor; if (textDrawingMode() == cTextFill && getShadow(shadowSize, shadowBlur, shadowColor)) { p->save(); p->translate(shadowSize.width(), shadowSize.height()); p->setPen(QColor(shadowColor)); p->drawLine(p1, p2); p->restore(); } p->drawLine(p1, p2); p->setRenderHint(QPainter::Antialiasing, antiAlias);}// This method is only used to draw the little circles used in lists.void GraphicsContext::drawEllipse(const IntRect& rect){ if (paintingDisabled()) return; m_data->p()->drawEllipse(rect);}void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan){ if (paintingDisabled() || strokeStyle() == NoStroke || strokeThickness() <= 0.0f || !strokeColor().alpha()) return; QPainter *p = m_data->p(); const bool antiAlias = p->testRenderHint(QPainter::Antialiasing); p->setRenderHint(QPainter::Antialiasing, m_data->antiAliasingForRectsAndLines); p->drawArc(rect, startAngle * 16, angleSpan * 16); p->setRenderHint(QPainter::Antialiasing, antiAlias);}void GraphicsContext::drawConvexPolygon(size_t npoints, const FloatPoint* points, bool shouldAntialias){ if (paintingDisabled()) return; if (npoints <= 1) return; QPolygonF polygon(npoints); for (size_t i = 0; i < npoints; i++) polygon[i] = points[i]; QPainter *p = m_data->p(); p->save(); p->setRenderHint(QPainter::Antialiasing, shouldAntialias); p->drawConvexPolygon(polygon); p->restore();}QPen GraphicsContext::pen(){ if (paintingDisabled()) return QPen(); QPainter *p = m_data->p(); return p->pen();}void GraphicsContext::fillPath(){ if (paintingDisabled()) return; QPainter *p = m_data->p(); QPainterPath path = m_data->currentPath; switch (m_common->state.fillColorSpace) { case SolidColorSpace: if (fillColor().alpha()) p->fillPath(path, p->brush()); break; case PatternColorSpace: { TransformationMatrix affine; p->fillPath(path, QBrush(m_common->state.fillPattern->createPlatformPattern(affine))); break; } case GradientColorSpace: QBrush brush(*m_common->state.fillGradient->platformGradient()); brush.setTransform(m_common->state.fillGradient->gradientSpaceTransform()); p->fillPath(path, brush); break; } m_data->currentPath = QPainterPath();}void GraphicsContext::strokePath(){ if (paintingDisabled()) return; QPainter *p = m_data->p(); QPen pen = p->pen(); QPainterPath path = m_data->currentPath; switch (m_common->state.strokeColorSpace) { case SolidColorSpace: if (strokeColor().alpha()) p->strokePath(path, pen); break; case PatternColorSpace: { TransformationMatrix affine; pen.setBrush(QBrush(m_common->state.strokePattern->createPlatformPattern(affine))); p->setPen(pen); p->strokePath(path, pen); break; } case GradientColorSpace: { QBrush brush(*m_common->state.strokeGradient->platformGradient()); brush.setTransform(m_common->state.strokeGradient->gradientSpaceTransform()); pen.setBrush(brush); p->setPen(pen); p->strokePath(path, pen); break; } } m_data->currentPath = QPainterPath();}void GraphicsContext::fillRect(const FloatRect& rect){ if (paintingDisabled()) return; QPainter *p = m_data->p(); switch (m_common->state.fillColorSpace) { case SolidColorSpace: if (fillColor().alpha()) p->fillRect(rect, p->brush()); break; case PatternColorSpace: { TransformationMatrix affine; p->fillRect(rect, QBrush(m_common->state.fillPattern->createPlatformPattern(affine))); break; } case GradientColorSpace: QBrush brush(*m_common->state.fillGradient->platformGradient()); brush.setTransform(m_common->state.fillGradient->gradientSpaceTransform()); p->fillRect(rect, brush); break; } m_data->currentPath = QPainterPath();}void GraphicsContext::fillRect(const FloatRect& rect, const Color& c){ if (paintingDisabled()) return; m_data->solidColor.setColor(QColor(c)); m_data->p()->fillRect(rect, m_data->solidColor);}void GraphicsContext::fillRoundedRect(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color& color){ if (paintingDisabled() || !color.alpha()) return; Path path = Path::createRoundedRectangle(rect, topLeft, topRight, bottomLeft, bottomRight); m_data->p()->fillPath(*path.platformPath(), QColor(color));}void GraphicsContext::beginPath(){ m_data->currentPath = QPainterPath();}void GraphicsContext::addPath(const Path& path){ QPainterPath newPath = m_data->currentPath; newPath.addPath(*(path.platformPath())); m_data->currentPath = newPath;}bool GraphicsContext::inTransparencyLayer() const{ return !m_data->layers.isEmpty();}PlatformPath* GraphicsContext::currentPath(){ return &m_data->currentPath;}void GraphicsContext::clip(const FloatRect& rect){ if (paintingDisabled()) return; m_data->p()->setClipRect(rect, Qt::IntersectClip);}void GraphicsContext::clipPath(WindRule clipRule){ if (paintingDisabled()) return; QPainter *p = m_data->p(); QPainterPath newPath = m_data->currentPath; newPath.setFillRule(clipRule == RULE_EVENODD ? Qt::OddEvenFill : Qt::WindingFill); p->setClipPath(newPath);}/** * Focus ring handling is not handled here. Qt style in * RenderTheme handles drawing focus on widgets which * need it. */Color focusRingColor() { return Color(0, 0, 0); }void GraphicsContext::drawFocusRing(const Color& color){ if (paintingDisabled()) return; const Vector<IntRect>& rects = focusRingRects(); unsigned rectCount = rects.size(); if (rects.size() == 0) return; QPainter *p = m_data->p(); const bool antiAlias = p->testRenderHint(QPainter::Antialiasing); p->setRenderHint(QPainter::Antialiasing, m_data->antiAliasingForRectsAndLines); const QPen oldPen = p->pen(); const QBrush oldBrush = p->brush(); QPen nPen = p->pen(); nPen.setColor(color); p->setBrush(Qt::NoBrush); nPen.setStyle(Qt::DotLine); p->setPen(nPen);#if 0 // FIXME How do we do a bounding outline with Qt? QPainterPath path; for (int i = 0; i < rectCount; ++i) path.addRect(QRectF(rects[i])); QPainterPathStroker stroker; QPainterPath newPath = stroker.createStroke(path); p->strokePath(newPath, nPen);#else for (int i = 0; i < rectCount; ++i) p->drawRect(QRectF(rects[i]));#endif p->setPen(oldPen); p->setBrush(oldBrush); p->setRenderHint(QPainter::Antialiasing, antiAlias);}void GraphicsContext::drawLineForText(const IntPoint& origin, int width, bool printing){ if (paintingDisabled()) return; IntPoint endPoint = origin + IntSize(width, 0); drawLine(origin, endPoint);}void GraphicsContext::drawLineForMisspellingOrBadGrammar(const IntPoint&, int width, bool grammar){ if (paintingDisabled()) return; notImplemented();}FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& frect){ QRectF rect(frect); rect = m_data->p()->deviceMatrix().mapRect(rect); QRect result = rect.toRect(); //round it return FloatRect(QRectF(result));}void GraphicsContext::setPlatformShadow(const IntSize& pos, int blur, const Color &color){ // Qt doesn't support shadows natively, they are drawn manually in the draw* // functions}void GraphicsContext::clearPlatformShadow(){ // Qt doesn't support shadows natively, they are drawn manually in the draw* // functions}void GraphicsContext::beginTransparencyLayer(float opacity){ if (paintingDisabled()) return; int x, y, w, h; x = y = 0; QPainter *p = m_data->p(); const QPaintDevice *device = p->device(); w = device->width(); h = device->height(); QRectF clip = p->clipPath().boundingRect(); QRectF deviceClip = p->transform().mapRect(clip); x = int(qBound(qreal(0), deviceClip.x(), (qreal)w)); y = int(qBound(qreal(0), deviceClip.y(), (qreal)h)); w = int(qBound(qreal(0), deviceClip.width(), (qreal)w) + 2); h = int(qBound(qreal(0), deviceClip.height(), (qreal)h) + 2); TransparencyLayer * layer = new TransparencyLayer(m_data->p(), QRect(x, y, w, h)); layer->opacity = opacity; m_data->layers.push(layer);}void GraphicsContext::endTransparencyLayer(){ if (paintingDisabled()) return; TransparencyLayer *layer = m_data->layers.pop(); layer->painter.end(); QPainter *p = m_data->p(); p->save(); p->resetTransform(); p->setOpacity(layer->opacity); p->drawPixmap(layer->offset, layer->pixmap); p->restore();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -