QPixmap ViewPointStyle::pixmapForCurrentSettings () const
{
  LOG4CPP_INFO_S ((*mainCat)) << "ViewPointStyle::pixmapForCurrentSettings";

  // Polygon that is sized for the main drawing window.
  QPolygonF polygonUnscaled = m_pointStyle.polygon();

  // Resize polygon to fit icon, by builiding a new scaled polygon from the unscaled polygon
  double xMinGot = polygonUnscaled.boundingRect().left();
  double xMaxGot = polygonUnscaled.boundingRect().right();
  double yMinGot = polygonUnscaled.boundingRect().top();
  double yMaxGot = polygonUnscaled.boundingRect().bottom();

  QPolygonF polygonScaled;
  for (int i = 0; i < polygonUnscaled.length(); i++) {
    QPointF pOld = polygonUnscaled.at(i);
    polygonScaled.append (QPointF ((width () - 1) * (pOld.x() - xMinGot) / (xMaxGot - xMinGot),
                                   (height () - 1) * (pOld.y() - yMinGot) / (yMaxGot - yMinGot)));
  }

  // Color
  QColor color = ColorPaletteToQColor(m_pointStyle.paletteColor());
  if (!m_enabled) {
    color = QColor (Qt::black);
  }

  // Image for drawing
  QImage img (width (),
              height (),
              QImage::Format_RGB32);
  QPainter painter (&img);

  painter.fillRect (0,
                    0,
                    width (),
                    height (),
                    QBrush (m_enabled ? COLOR_FOR_BRUSH_ENABLED : COLOR_FOR_BRUSH_DISABLED));

  if (m_enabled) {
    painter.setPen (QPen (color, m_pointStyle.lineWidth()));
    painter.drawPolygon (polygonScaled);
  }

  // Create pixmap from image
  QPixmap pixmap = QPixmap::fromImage (img);

  return pixmap;
}
Пример #2
0
/*!
 * \fn Util::cullLine
 * Slice \a poly so that the segments that are left are those which remain inside \a rect.
 */
QList<QPolygonF> Util::cullLine(QPolygonF poly, QRectF rect)
{
    // When the plot is zoomed closer, drawing the line as a single poly
    // gets slower - weird and annoying. Therefore, here, the lines are "cut"
    // by the view frame so that excess segment aren't drawn.
    QList<QPolygonF> lines;
    QPointF latestPoint;
    QPolygonF latestPoly;
    bool starting = true;

    // If there's only 1 point, the forloop will not run
    if (poly.size() == 1) {
        if (rect.contains(poly[0])) {
            latestPoly << poly[0];
            lines << latestPoly;
        }
        return lines;
    }

    for (int i=0; i<poly.size() - 1; ++i) {
        QPointF p1 = poly[i], p2 = poly[i+1];
        QLineF l = rectSlice(p1, p2, rect);
        if (l.isNull()) continue;

        // Check if the line has been broken, or is starting
        if (l.p1() != latestPoint || starting) {
            // Throw the current poly in and start a new one
            if (latestPoly.length() > 0) {
                lines << latestPoly;
                latestPoly = QPolygonF();
            }
            starting = false;
            latestPoly << l.p1();
        }
        latestPoly << l.p2();
        latestPoint = l.p2();
    }

    // Need the final whole section
    lines << latestPoly;

    return lines;
}