Example #1
0
QCursor BaseTool::dottedCursor() // Todo: only one instance required: make fn static?
{
    Q_ASSERT( mEditor->getScribbleArea() );

    qreal zoomFactor = editor()->view()->scaling(); //scale factor

    //qDebug() << "--->" << zoomFactor;
    qreal propWidth = properties.width * zoomFactor;
    qreal propFeather = properties.feather * zoomFactor;
    qreal width = propWidth + 0.5 * propFeather;

    if ( width < 1 ) { width = 1; }
    qreal radius = width / 2;
    qreal xyA = 1 + propFeather / 2;
    qreal xyB = 1 + propFeather / 8;
    qreal whA = qMax( 0.0, propWidth - xyA - 1 );
    qreal whB = qMax( 0.0, width - propFeather / 4 - 2 );
    QPixmap pixmap( width, width );
    if ( !pixmap.isNull() )
    {
        pixmap.fill( QColor( 255, 255, 255, 0 ) );
        QPainter painter( &pixmap );
        QPen pen = painter.pen();

        // Draw cross in center
        pen.setStyle( Qt::SolidLine );
        pen.setColor( QColor( 0, 0, 0, 127 ) );
        painter.setPen(pen);
        painter.drawLine( QPointF( radius - 2, radius ), QPointF( radius + 2, radius ) );
        painter.drawLine( QPointF( radius, radius - 2 ), QPointF( radius, radius + 2 ) );

        // Draw outer circle
        pen.setStyle( Qt::DotLine );
        pen.setColor( QColor( 0, 0, 0, 255 ) );
        painter.setPen(pen);
        painter.drawEllipse( QRectF( xyB, xyB, whB, whB ) );
        pen.setDashOffset( 4 );
        pen.setColor( QColor( 255, 255, 255, 255 ) );
        painter.setPen(pen);
        painter.drawEllipse( QRectF( xyB, xyB, whB, whB ) );

        // Draw inner circle
        pen.setStyle( Qt::DotLine );
        pen.setColor( QColor( 0, 0, 0, 255 ) );
        painter.setPen(pen);
        painter.drawEllipse( QRectF( xyA, xyA, whA, whA ) );
        pen.setDashOffset( 4 );
        pen.setColor( QColor( 255, 255, 255, 255 ) );
        painter.setPen(pen);
        painter.drawEllipse( QRectF( xyA, xyA, whA, whA ) );

        painter.end();
    }
    return QCursor( pixmap );
}
Example #2
0
void koregui::BindPathItem::paint(QPainter* painter,
                                  const QStyleOptionGraphicsItem* option,
                                  QWidget* widget) {
  // TODO(dospelt) use QPainterPathStroker
  QPointF start = mapFromItem(_start,6,6);
  QPointF dest = (_end)?mapFromItem(_end,6,6):_dragend;
  QPointF dist = QPointF((dest.x()- start.x())/2, 0);
  QPainterPath path(start);
  path.cubicTo(start + dist, dest - dist, dest);
  setPath(path);

  QPen p;
  p.setWidth(2);

  if (isSelected()) {
    p.setColor(QColor(100, 255, 255));
  } else {
    p.setColor(QColor(200, 200, 200));
  }
  if(_animated) {
    QVector<qreal> dashes;
    dashes << 4 << 3;
    p.setDashPattern(dashes);
    p.setDashOffset(_animOffset);
  }
  painter->setPen(p);
  painter->drawPath(path);
}
Example #3
0
void SelectionItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
	if(!m_selection->pasteImage().isNull()) {
		if(m_shape.size() == 4) {
			QPolygonF src({
				QPointF(0, 0),
				QPointF(m_selection->pasteImage().width(), 0),
				QPointF(m_selection->pasteImage().width(), m_selection->pasteImage().height()),
				QPointF(0, m_selection->pasteImage().height())
			});

			QTransform t;
			if(QTransform::quadToQuad(src, m_shape, t)) {
				painter->save();
				painter->setTransform(t, true);
				painter->drawImage(0, 0, m_selection->pasteImage());
				painter->restore();
			} else
				qWarning("Couldn't transform pasted image!");

		} else {
			qWarning("Pasted selection item with non-rectangular polygon!");
		}
	}

	painter->setClipRect(boundingRect().adjusted(-1, -1, 1, 1));

	QPen pen;
	pen.setWidth(qApp->devicePixelRatio());
	pen.setColor(Qt::white);
	pen.setStyle(Qt::DashLine);
	pen.setDashOffset(m_marchingants);
	pen.setCosmetic(true);
	painter->setPen(pen);
	painter->setCompositionMode(QPainter::RasterOp_SourceXorDestination);

	if(m_selection->isClosed())
		painter->drawPolygon(m_shape);
	else
		painter->drawPolyline(m_shape);

	// Draw resizing handles
	if(m_selection->isClosed()) {
		QRect rect = m_selection->boundingRect();

		pen.setWidth(m_selection->handleSize());
		pen.setStyle(Qt::SolidLine);
		painter->setPen(pen);
		painter->drawPoint(rect.topLeft());
		painter->drawPoint(rect.topLeft() + QPointF(rect.width()/2, 0));
		painter->drawPoint(rect.topRight());

		painter->drawPoint(rect.topLeft() + QPointF(0, rect.height()/2));
		painter->drawPoint(rect.topRight() + QPointF(0, rect.height()/2));

		painter->drawPoint(rect.bottomLeft());
		painter->drawPoint(rect.bottomLeft() + QPointF(rect.width()/2, 0));
		painter->drawPoint(rect.bottomRight());
	}
}
Example #4
0
void Stroke::paint(QPainter &painter, qreal zoom, bool last)
{
  if (points.length() == 1)
  {
    QRectF pointRect(zoom * points[0], QSizeF(0, 0));
    qreal pad = penWidth * zoom / 2;
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(color));
    painter.drawEllipse(pointRect.adjusted(-pad, -pad, pad, pad));
  }
  else
  {
    QPen pen;
    pen.setColor(color);
    if (pattern != solidLinePattern)
    {
      pen.setDashPattern(pattern);
    }
    pen.setCapStyle(Qt::RoundCap);
    painter.setPen(pen);
    qreal dashOffset = 0.0;
    for (int j = 1; j < points.length(); ++j)
    {
      qreal tmpPenWidth = zoom * penWidth * (pressures.at(j - 1) + pressures.at(j)) / 2.0;
      if (pattern != solidLinePattern)
      {
        pen.setDashOffset(dashOffset);
      }
      pen.setWidthF(tmpPenWidth);
      painter.setPen(pen);
      //                painter.drawLine(zoom * points.at(j-1), zoom * points.at(j));
      if (last == false)
      {
        painter.drawLine(zoom * points.at(j - 1), zoom * points.at(j));
      }
      else if (last == true && j == points.length() - 1)
      {
        painter.drawLine(zoom * points.at(j - 1), zoom * points.at(j));
      }

      if (tmpPenWidth != 0)
        dashOffset += 1.0 / tmpPenWidth * (QLineF(zoom * points.at(j - 1), zoom * points.at(j))).length();
    }
  }
}
void GraphicsContext::setLineDash(const DashArray& dashes, float dashOffset)
{
    QPainter* p = m_data->p();
    QPen pen = p->pen();
    unsigned dashLength = dashes.size();
    if (dashLength) {
        QVector<qreal> pattern;
        unsigned count = dashLength;
        if (dashLength % 2)
            count *= 2;

        float penWidth = narrowPrecisionToFloat(double(pen.widthF()));
        for (unsigned i = 0; i < count; i++)
            pattern.append(dashes[i % dashLength] / penWidth);

        pen.setDashPattern(pattern);
        pen.setDashOffset(dashOffset);
    }
    p->setPen(pen);
}
Example #6
0
void QSvgStrokeStyle::apply(QPainter *p, const QSvgNode *, QSvgExtraStates &states)
{
    m_oldStroke = p->pen();
    m_oldStrokeOpacity = states.strokeOpacity;
    m_oldStrokeDashOffset = states.strokeDashOffset;
    m_oldVectorEffect = states.vectorEffect;

    QPen pen = p->pen();

    qreal oldWidth = pen.widthF();
    qreal width = m_stroke.widthF();
    if (oldWidth == 0)
        oldWidth = 1;
    if (width == 0)
        width = 1;
    qreal scale = oldWidth / width;

    if (m_strokeOpacitySet)
        states.strokeOpacity = m_strokeOpacity;

    if (m_vectorEffectSet)
        states.vectorEffect = m_vectorEffect;

    if (m_strokeSet) {
        if (m_style)
            pen.setBrush(m_style->brush(p, states));
        else
            pen.setBrush(m_stroke.brush());
    }

    if (m_strokeWidthSet)
        pen.setWidthF(m_stroke.widthF());

    bool setDashOffsetNeeded = false;

    if (m_strokeDashOffsetSet) {
        states.strokeDashOffset = m_strokeDashOffset;
        setDashOffsetNeeded = true;
    }

    if (m_strokeDashArraySet) {
        if (m_stroke.style() == Qt::SolidLine) {
            pen.setStyle(Qt::SolidLine);
        } else if (m_strokeWidthSet || oldWidth == 1) {
            // If both width and dash array was set, the dash array is already scaled correctly.
            pen.setDashPattern(m_stroke.dashPattern());
            setDashOffsetNeeded = true;
        } else {
            // If dash array was set, but not the width, the dash array has to be scaled with respect to the old width.
            QVector<qreal> dashes = m_stroke.dashPattern();
            for (int i = 0; i < dashes.size(); ++i)
                dashes[i] /= oldWidth;
            pen.setDashPattern(dashes);
            setDashOffsetNeeded = true;
        }
    } else if (m_strokeWidthSet && pen.style() != Qt::SolidLine && scale != 1) {
        // If the width was set, but not the dash array, the old dash array must be scaled with respect to the new width.
        QVector<qreal> dashes = pen.dashPattern();
        for (int i = 0; i < dashes.size(); ++i)
            dashes[i] *= scale;
        pen.setDashPattern(dashes);
        setDashOffsetNeeded = true;
    }

    if (m_strokeLineCapSet)
        pen.setCapStyle(m_stroke.capStyle());
    if (m_strokeLineJoinSet)
        pen.setJoinStyle(m_stroke.joinStyle());
    if (m_strokeMiterLimitSet)
        pen.setMiterLimit(m_stroke.miterLimit());

    // You can have dash offset on solid strokes in SVG files, but not in Qt.
    // QPen::setDashOffset() will set the pen style to Qt::CustomDashLine,
    // so don't call the method if the pen is solid.
    if (setDashOffsetNeeded && pen.style() != Qt::SolidLine) {
        qreal currentWidth = pen.widthF();
        if (currentWidth == 0)
            currentWidth = 1;
        pen.setDashOffset(states.strokeDashOffset / currentWidth);
    }

    pen.setCosmetic(states.vectorEffect);

    p->setPen(pen);
}
Example #7
0
void ZCircle::displayHelper(ZPainter *painter, int stackFocus, EDisplayStyle style) const
{
  UNUSED_PARAMETER(style);
#if defined(_QT_GUI_USED_)
  double adjustedRadius = getAdjustedRadius(m_r);

  double dataFocus = stackFocus - painter->getZOffset();
  bool visible = false;

  const QBrush &oldBrush = painter->getBrush();
  const QPen &oldPen = painter->getPen();

  double alpha = oldPen.color().alphaF();

  if (stackFocus == -1) {
    visible = true;
  } else {
    if (isCuttingPlane(m_center.z(), m_r, dataFocus, m_zScale)) {
      double h = fabs(m_center.z() - dataFocus) / m_zScale;
      double r = 0.0;
      if (m_r > h) {
        r = sqrt(m_r * m_r - h * h);
        adjustedRadius = getAdjustedRadius(r);
        //adjustedRadius = r + getPenWidth() * 0.5;
        visible = true;
      } else { //too small, show at least one plane
        //adjustedRadius = getPenWidth() * 0.5;
        r = 0.1;
        adjustedRadius = getAdjustedRadius(r);
        visible = true;
      }
      if (hasVisualEffect(VE_OUT_FOCUS_DIM)) {
        alpha *= r * r / m_r / m_r;
        //alpha *= alpha;
      }
    }
  }

  if (visible) {
    if (!hasVisualEffect(VE_NO_CIRCLE)) {
      //qDebug() << painter->brush().color();
      QColor color = painter->getPenColor();
      color.setAlphaF(alpha);
      painter->setPen(color);
      painter->drawEllipse(QPointF(m_center.x(), m_center.y()),
                           adjustedRadius, adjustedRadius);
    }
  }

  if (hasVisualEffect(VE_BOUND_BOX)) {
    QRectF rect;
    double halfSize = adjustedRadius;
    if (m_usingCosmeticPen) {
      halfSize += 0.5;
    }
    rect.setLeft(m_center.x() - halfSize);
    rect.setTop(m_center.y() - halfSize);
    rect.setWidth(halfSize * 2);
    rect.setHeight(halfSize * 2);

    painter->setBrush(Qt::NoBrush);

    QPen pen = oldPen;
    if (visible) {
      pen.setStyle(Qt::SolidLine);
    } else {
      pen.setStyle(Qt::DotLine);
    }
    pen.setCosmetic(m_usingCosmeticPen);
    painter->setPen(pen);

#if 0 //for future versions
    QPen pen = oldPen;
    QVector<qreal> pattern;
    pattern << 1 << 2;
    pen.setDashPattern(pattern);
    painter->setPen(pen);
    painter->drawRect(rect);

    pen.setColor(Qt::black);
    pen.setDashOffset(1.5);
    painter->setPen(pen);
#endif

    //QPainter::CompositionMode oldMode = painter->compositionMode();
    //painter->setCompositionMode(QPainter::RasterOp_SourceXorDestination);
    painter->drawRect(rect);

    //painter->setCompositionMode(oldMode);
  }

  painter->setBrush(oldBrush);
  painter->setPen(oldPen);
#endif
}
Example #8
0
/**
 * @brief precision circular cursor: used for drawing a cursor within scribble area.
 * @return QPixmap
 */
QPixmap BaseTool::canvasCursor(float width, float feather, bool useFeather, float scalingFac, int windowWidth)
{
    float propWidth = width * scalingFac;
    float propFeather = feather * scalingFac;

    float cursorWidth = 0.0f;
    float xyA = 0.0f;
    float xyB = 0.0f;
    float whA = 0.0f;
    float whB = 0.0f;

    if (useFeather)
    {
        cursorWidth = propWidth + 0.5 * propFeather;
        xyA = 1 + propFeather / 2;
        xyB = 1 + propFeather / 8;
        whA = qMax<float>(0, propWidth - xyA - 1);
        whB = qMax<float>(0, cursorWidth - propFeather / 4 - 2);
    }
    else
    {
        cursorWidth = (propWidth + 0.5);
        whA = qMax<float>(0, propWidth - 1);
        whB = qMax<float>(0, cursorWidth / 4 - 2);
    }

    float radius = cursorWidth / 2;

    // deallocate when cursor width gets some value larger than the widget
    if (cursorWidth > windowWidth * 2)
    {
        return QPixmap(0, 0);
    }

    if (cursorWidth < 1) { cursorWidth = 1; }

    QPixmap cursorPixmap = QPixmap(cursorWidth, cursorWidth);
    if (!cursorPixmap.isNull())
    {
        cursorPixmap.fill(QColor(255, 255, 255, 0));
        QPainter cursorPainter(&cursorPixmap);
        QPen cursorPen = cursorPainter.pen();
        cursorPainter.setRenderHint(QPainter::HighQualityAntialiasing);

        // Draw cross in center
        cursorPen.setStyle(Qt::SolidLine);
        cursorPen.setColor(QColor(0, 0, 0, 127));
        cursorPainter.setPen(cursorPen);
        cursorPainter.drawLine(QPointF(radius - 2, radius), QPointF(radius + 2, radius));
        cursorPainter.drawLine(QPointF(radius, radius - 2), QPointF(radius, radius + 2));

        // Draw outer circle
        if (useFeather)
        {
            cursorPen.setStyle(Qt::DotLine);
            cursorPen.setColor(QColor(0, 0, 0, 255));
            cursorPainter.setPen(cursorPen);
            cursorPainter.drawEllipse(QRectF(xyB, xyB, whB, whB));
            cursorPen.setDashOffset(4);
            cursorPen.setColor(QColor(255, 255, 255, 255));
            cursorPainter.setPen(cursorPen);
            cursorPainter.drawEllipse(QRectF(xyB, xyB, whB, whB));
        }

        // Draw inner circle
        cursorPen.setStyle(Qt::DotLine);
        cursorPen.setColor(QColor(0, 0, 0, 255));
        cursorPainter.setPen(cursorPen);
        cursorPainter.drawEllipse(QRectF(xyA, xyA, whA, whA));
        cursorPen.setDashOffset(4);
        cursorPen.setColor(QColor(255, 255, 255, 255));
        cursorPainter.setPen(cursorPen);
        cursorPainter.drawEllipse(QRectF(xyA, xyA, whA, whA));

        cursorPainter.end();
    }
    return cursorPixmap;
}