@@ -122,6 +122,15 @@ void GLCanvasWidget::setGridScales(int topScale, int bottomScale)
122122 update ();
123123}
124124
125+ void GLCanvasWidget::setGridRegions (const QRect& topRegion, const QRect& bottomRegion)
126+ {
127+ m_gridTopRegion = topRegion;
128+ m_gridBottomRegion = bottomRegion;
129+ m_gridHasTopRegion = topRegion.isValid () && !topRegion.isEmpty ();
130+ m_gridHasBottomRegion = bottomRegion.isValid () && !bottomRegion.isEmpty ();
131+ update ();
132+ }
133+
125134void GLCanvasWidget::setMaskOutline (const cv::Mat& mask, const QColor& color, const QRect& region)
126135{
127136 if (mask.empty ()) {
@@ -283,14 +292,23 @@ void GLCanvasWidget::paintGL()
283292 painter.scale (m_zoom, m_zoom);
284293 const int w = m_image.cols ;
285294 const int h = m_image.rows ;
286- auto drawRegion = [&](int yStart, int yEnd, int scale) {
295+ auto drawRegion = [&](int yStart, int yEnd, int scale, const QRect* regionOverride ) {
287296 if (yEnd <= yStart) {
288297 return ;
289298 }
299+ int xStart = 0 ;
300+ int xEnd = w;
301+ if (regionOverride && regionOverride->isValid () && !regionOverride->isEmpty ()) {
302+ xStart = std::clamp (regionOverride->x (), 0 , w);
303+ xEnd = std::clamp (regionOverride->x () + regionOverride->width (), 0 , w);
304+ }
305+ if (xEnd <= xStart) {
306+ return ;
307+ }
290308 painter.save ();
291- painter.setClipRect (QRectF (- w / 2.0 ,
309+ painter.setClipRect (QRectF (xStart - w / 2.0 ,
292310 yStart - h / 2.0 ,
293- w ,
311+ xEnd - xStart ,
294312 yEnd - yStart));
295313 const double gapRatio = 0.5 ;
296314 const double cell = std::max (1 , scale);
@@ -305,24 +323,26 @@ void GLCanvasWidget::paintGL()
305323 gridPen.setWidthF (lineWidth);
306324 gridPen.setCapStyle (Qt::SquareCap);
307325 painter.setPen (gridPen);
308- for (int x = 0 ; x <= w ; x += step) {
326+ for (int x = xStart ; x <= xEnd ; x += step) {
309327 painter.drawLine (QPointF (x - w / 2.0 , yStart - h / 2.0 ),
310328 QPointF (x - w / 2.0 , yEnd - h / 2.0 ));
311329 }
312330 for (int y = yStart; y <= yEnd; y += step) {
313- painter.drawLine (QPointF (- w / 2.0 , y - h / 2.0 ),
314- QPointF (w / 2.0 , y - h / 2.0 ));
331+ painter.drawLine (QPointF (xStart - w / 2.0 , y - h / 2.0 ),
332+ QPointF (xEnd - w / 2.0 , y - h / 2.0 ));
315333 }
316334 painter.restore ();
317335 };
318336 if (m_gridTopHeight > 0 && m_gridBottomHeight > 0 &&
319337 m_gridTopHeight + m_gridGap + m_gridBottomHeight <= h) {
320- drawRegion (0 , m_gridTopHeight, m_gridTopScale);
338+ drawRegion (0 , m_gridTopHeight, m_gridTopScale,
339+ m_gridHasTopRegion ? &m_gridTopRegion : nullptr );
321340 drawRegion (m_gridTopHeight + m_gridGap,
322341 m_gridTopHeight + m_gridGap + m_gridBottomHeight,
323- m_gridBottomScale);
342+ m_gridBottomScale,
343+ m_gridHasBottomRegion ? &m_gridBottomRegion : nullptr );
324344 } else {
325- drawRegion (0 , h, 1 );
345+ drawRegion (0 , h, 1 , nullptr );
326346 }
327347 painter.restore ();
328348 }
0 commit comments