void GraphicsItemEdge::paint(QPainter * painter, const QStyleOptionGraphicsItem *, QWidget *) { double edgeWidth = g_settings->edgeWidth; QColor penColour; if (isSelected()) penColour = g_settings->selectionColour; else penColour = g_settings->edgeColour; QPen edgePen(QBrush(penColour), edgeWidth, Qt::SolidLine, Qt::RoundCap); painter->setPen(edgePen); painter->drawPath(path()); }
// Construct a view showing problem/solution from the given input stream. View::View(std::istream &in, Options options, QGraphicsScene *scene, QWidget *parent) : QGraphicsView(scene, parent), m_legPen(Qt::black, 1.0), m_currentLegPen(QColor(Qt::green).darker(), 1.0), m_currentLeg(0), m_hudFont("Courier", 10), m_labelFont("Courier", 6), m_solutionLength(0), m_options(options) { resize(900, 600); setRenderHints(QPainter::Antialiasing); setTransformationAnchor(QGraphicsView::AnchorUnderMouse); setDragMode(QGraphicsView::ScrollHandDrag); setViewportUpdateMode(QGraphicsView::FullViewportUpdate); // :( setBackgroundBrush(Qt::white); m_legPen.setCosmetic(true); m_currentLegPen.setCosmetic(true); // Read points. int numPoints; in >> numPoints; m_points.resize(numPoints); float minX = std::numeric_limits<float>::max(); float minY = std::numeric_limits<float>::max(); float maxX = std::numeric_limits<float>::lowest(); float maxY = std::numeric_limits<float>::lowest(); double sumX = 0; double sumY = 0; for (int i = 0; i < numPoints; ++i) { float x, y; in >> x >> y; m_points[i].setX(x); m_points[i].setY(y); minX = qMin(minX, x); minY = qMin(minY, y); maxX = qMax(maxX, x); maxY = qMax(maxY, y); sumX += x; sumY += y; Vertex *item; if (numPoints < 100) { // If we have less than 100 vertices, we label them. item = new Vertex(x, y, 8, i, Vertex::ShowLabel); } else { // We just draw them as dots. item = new Vertex(x, y, 2, i, Vertex::NoLabel); } scene->addItem(item); item->setZValue(1); // On top of edges. } if (m_options & CompleteGraph) { // Add the complete set of edges. QPen edgePen(Qt::gray); edgePen.setCosmetic(true); for (int i = 0; i < numPoints; ++i) { for (int j = 0; j < numPoints; ++j) { if (i == j) continue; float x1 = m_points[i].x(); float y1 = m_points[i].y(); float x2 = m_points[j].x(); float y2 = m_points[j].y(); QGraphicsLineItem *edgeItem = scene->addLine(x1, y1, x2, y2, edgePen); m_edges.append(edgeItem); if (m_options & ShowLengths) { addLengthLabel(x1, y1, x2, y2); } } } } // Read solution, if any. if (m_options & PointPairs) { readPointPairs(in); } else { readPointSequence(in); } // Center view on mean coordinate and zoom to fit entire scene. const float margin = qAbs(maxX - minX) / 30; setSceneRect(minX - margin, minY - margin, maxX - minX + 2*margin, maxY - minY + 2*margin); centerOn(QPointF(sumX / numPoints, sumY / numPoints)); fitInView(sceneRect(), Qt::KeepAspectRatio); }