30 #include <QGraphicsLineItem>
45 QColor edge_color_of(
const ClockTreeColors &colors )
47 return colors.edge.
isValid() ? colors.edge : FALLBACK_COLOR;
50 QColor highlight_color_of(
const ClockTreeColors &colors )
52 return colors.highlight.
isValid() ? colors.highlight : edge_color_of( colors );
56 constexpr
double RING_RADIUS_PIXELS = 12.0;
57 constexpr
double RING_WIDTH_PIXELS = 2.0;
60 constexpr
double RING_Z_VALUE = 2.0;
65 , mStyle{ 0.0, 0.0, 0.0, 0.0, false }
77 return { scenePosition.
x(), -scenePosition.
y() };
83 return color.
isValid() ? color : edge_color_of( colors );
91 mEdgeColor = edge_color_of( colors );
92 mHighlightColor = highlight_color_of( colors );
95 mEdgeItems.
reserve(
static_cast<int>( layout.
edges.size() ) );
105 mNodeItems.
reserve(
static_cast<int>( layout.
nodes.size() ) );
106 mNodeColors.
reserve(
static_cast<int>( layout.
nodes.size() ) );
107 mNodeCenters.
reserve(
static_cast<int>( layout.
nodes.size() ) );
108 for( std::size_t idx = 0; idx < layout.
nodes.size(); idx++ )
112 const QColor color = colorOfNode( colors,
static_cast<int>( idx ) );
141 const QColor edge = edge_color_of( colors );
142 if( edge != mEdgeColor )
149 item->setPen( edge_pen );
154 for(
int idx = 0; idx < mNodeItems.
size(); idx++ )
156 const QColor color = colorOfNode( colors, idx );
157 if( color == mNodeColors[idx] )
162 mNodeColors[idx] = color;
164 mNodeItems[idx]->setBrush(
QBrush( color ) );
167 const QColor highlight = highlight_color_of( colors );
168 if( highlight != mHighlightColor )
170 mHighlightColor = highlight;
172 QPen ring_pen( highlight, RING_WIDTH_PIXELS );
176 ring->setPen( ring_pen );
190 for(
const int node : nodes )
192 if( node >= 0 && node < mNodeCenters.
size() )
197 std::sort( wanted.
begin(), wanted.
end() );
200 if( wanted == mHighlighted )
206 qDeleteAll( mRingItems );
209 QPen ring_pen( mHighlightColor, RING_WIDTH_PIXELS );
213 for(
const int node : wanted )
216 -RING_RADIUS_PIXELS, -RING_RADIUS_PIXELS, 2.0 * RING_RADIUS_PIXELS, 2.0 * RING_RADIUS_PIXELS );
218 ring->setPos( mNodeCenters[node] );
219 ring->setPen( ring_pen );
221 ring->setZValue( RING_Z_VALUE );
227 mHighlighted = wanted;
238 double left = mNodeCenters[mHighlighted.
first()].x();
240 double top = mNodeCenters[mHighlighted.
first()].y();
243 for(
const int node : mHighlighted )
245 left = std::min( left, mNodeCenters[node].
x() );
246 right = std::max( right, mNodeCenters[node].
x() );
247 top = std::min( top, mNodeCenters[node].
y() );
248 bottom = std::max( bottom, mNodeCenters[node].
y() );
260 mNodeCenters.
clear();
262 mHighlighted.
clear();
282 return ERR(
"there is no clock tree to take a picture of" );
286 const QRectF source = mContentRect.
adjusted( -margin, -margin, margin, margin );
291 const double longest_side = std::max( source.
width(), source.
height() );
299 double width = std::ceil( source.
width() * pixels_per_unit );
300 double height = std::ceil( source.
height() * pixels_per_unit );
310 return ERR(
"the picture would be " + std::to_string(
static_cast<long long>(
width ) ) +
" x "
311 + std::to_string(
static_cast<long long>(
height ) ) +
" pixels, more than the limit of "
313 +
" pixels per side, use a smaller pixels_per_unit" );
319 return ERR(
"could not allocate a picture of " + std::to_string(
static_cast<long long>(
width ) ) +
" x "
320 + std::to_string(
static_cast<long long>(
height ) ) +
" pixels" );
const ctv::Style & style() const
Result< QImage > renderPicture(const QColor &background)
static QPointF fromChip(double x, double y)
void setHighlight(const QVector< int > &nodes)
ClockTreeScene(QObject *parent=nullptr)
void setLayout(const ctv::Layout &layout, const ctv::Style &style, const ClockTreeColors &colors)
static QPointF toChip(const QPointF &scenePosition)
QRectF highlightedRect() const
void setColors(const ClockTreeColors &colors)
constexpr double MAX_PICTURE_SIDE_PIXELS
bool isValid() const const
ItemIgnoresTransformations
QGraphicsEllipseItem * addEllipse(const QRectF &rect, const QPen &pen, const QBrush &brush)
void addItem(QGraphicsItem *item)
QGraphicsLineItem * addLine(const QLineF &line, const QPen &pen)
qreal height() const const
QRectF itemsBoundingRect() const const
void render(QPainter *painter, const QRectF &target, const QRectF &source, Qt::AspectRatioMode aspectRatioMode)
void setSceneRect(const QRectF &rect)
qreal width() const const
void fill(uint pixelValue)
bool isNull() const const
void setRenderHint(QPainter::RenderHint hint, bool on)
void setCosmetic(bool cosmetic)
QRectF adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const const
qreal height() const const
qreal width() const const
QVector::iterator begin()
QVector::iterator erase(QVector::iterator begin, QVector::iterator end)
bool isEmpty() const const
void push_back(const T &value)
T value(int i) const const
std::vector< Node > nodes
std::vector< Edge > edges
bool pixels_per_unit_is_fixed