Skip to content

Commit 3a2e656

Browse files
committed
Fix pixel selection tool visibility
1 parent c91e739 commit 3a2e656

File tree

2 files changed

+53
-47
lines changed

2 files changed

+53
-47
lines changed

src/ScatterplotPlugin.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -374,37 +374,30 @@ void ScatterplotPlugin::selectPoints()
374374

375375
_positionDataset->getGlobalIndices(localGlobalIndices);
376376

377-
auto& zoomRectangleAction = _scatterPlotWidget->getNavigationAction().getZoomRectangleAction();
378-
379-
const auto width = selectionAreaImage.width();
380-
const auto height = selectionAreaImage.height();
381-
const auto size = width < height ? width : height;
382-
const auto uvOffset = QPoint((selectionAreaImage.width() - size) / 2.0f, (selectionAreaImage.height() - size) / 2.0f);
383-
384-
QPointF uvNormalized = {};
385-
QPoint uv = {};
386-
387-
auto& navigator = _scatterPlotWidget->_pointRenderer.getNavigator();
377+
auto& pointRenderer = _scatterPlotWidget->_pointRenderer;
378+
auto& navigator = pointRenderer.getNavigator();
388379

389-
auto zoomRectangleWorld = navigator.getZoomRectangleWorld();
390-
391-
const auto margin = _scatterPlotWidget->_pointRenderer.getNavigator().getZoomRectangleMargin();
392-
393-
//zoomRectangleWorld.setSize(zoomRectangleWorld.size() - QSize(2 * margin, 2 * margin));
394-
395-
qDebug() << zoomRectangleWorld;
380+
const auto zoomRectangleWorld = navigator.getZoomRectangleWorld();
381+
const auto screenRectangle = QRect(QPoint(), pointRenderer.getRenderSize());
396382

383+
// Go over all points in the dataset to see if they are selected
397384
for (std::uint32_t localPointIndex = 0; localPointIndex < _positions.size(); localPointIndex++) {
398-
//const auto screenPoint = _scatterPlotWidget->getPointRenderer().getWorldPositionToScreenPoint(QVector3D(_positions[localPointIndex].x, _positions[localPointIndex].y, 0.f));
385+
386+
// Compute the offset of the point in the world space
387+
const auto pointOffsetWorld = QPointF(_positions[localPointIndex].x - zoomRectangleWorld.left(), _positions[localPointIndex].y - zoomRectangleWorld.top());
388+
389+
// Normalize it
390+
const auto pointOffsetWorldNormalized = QPointF(pointOffsetWorld.x() / zoomRectangleWorld.width(), pointOffsetWorld.y() / zoomRectangleWorld.height());
399391

400-
uvNormalized = QPointF((_positions[localPointIndex].x - zoomRectangleWorld.left()) / zoomRectangleWorld.width(), (zoomRectangleWorld.bottom() - _positions[localPointIndex].y) / zoomRectangleWorld.height());
401-
//uvNormalized = QPointF(static_cast<float>(screenPoint.x()) / static_cast<float>(_scatterPlotWidget->width()), static_cast<float>(screenPoint.y()) / static_cast<float>(_scatterPlotWidget->height()));
402-
uv = uvOffset + QPoint(uvNormalized.x() * size, uvNormalized.y() * size);
392+
// Convert it to screen space
393+
const auto pointOffsetScreen = QPoint(pointOffsetWorldNormalized.x() * screenRectangle.width(), screenRectangle.height() - pointOffsetWorldNormalized.y() * screenRectangle.height());
403394

404-
if (uv.x() >= selectionAreaImage.width() || uv.x() < 0 || uv.y() >= selectionAreaImage.height() || uv.y() < 0)
395+
// Continue to next point if the point is outside the screen
396+
if (!screenRectangle.contains(pointOffsetScreen))
405397
continue;
406398

407-
if (selectionAreaImage.pixelColor(uv).alpha() > 0)
399+
// If the corresponding pixel is not transparent, add the point to the selection
400+
if (selectionAreaImage.pixelColor(pointOffsetScreen).alpha() > 0)
408401
targetSelectionIndices.push_back(localGlobalIndices[localPointIndex]);
409402
}
410403

src/ScatterplotWidget.cpp

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,6 @@ ScatterplotWidget::ScatterplotWidget(mv::plugin::ViewPlugin* parentPlugin) :
167167
update();
168168
});
169169

170-
//auto setIsNavigating = [this](bool isNavigating) -> void {
171-
// _isNavigating = isNavigating;
172-
// _pixelSelectionTool.setEnabled(!isNavigating);
173-
// if (isNavigating) {
174-
// _samplerPixelSelectionTool.setEnabled(false);
175-
// }
176-
// else if (_parentPlugin) { // reset to UI-setting
177-
// _samplerPixelSelectionTool.setEnabled(_parentPlugin->getSamplerAction().getEnabledAction().isChecked());
178-
// }
179-
180-
// };
181-
182170
_pointRenderer.getNavigator().initialize(this);
183171
_densityRenderer.getNavigator().initialize(this);
184172
}
@@ -199,6 +187,37 @@ bool ScatterplotWidget::event(QEvent* event)
199187
}
200188
}
201189

190+
auto setIsNavigating = [this](bool isNavigating) -> void {
191+
_pixelSelectionTool.setEnabled(getRenderMode() == RenderMode::SCATTERPLOT && !isNavigating);
192+
193+
if (isNavigating) {
194+
_samplerPixelSelectionTool.setEnabled(false);
195+
}
196+
else if (_parentPlugin) {
197+
_samplerPixelSelectionTool.setEnabled(_parentPlugin->getSamplerAction().getEnabledAction().isChecked());
198+
}
199+
};
200+
201+
if (event->type() == QEvent::KeyPress) {
202+
const auto keyEvent = dynamic_cast<QKeyEvent*>(event);
203+
204+
if (keyEvent->key() == Qt::Key_Alt) {
205+
setIsNavigating(true);
206+
207+
return QOpenGLWidget::event(event);
208+
}
209+
}
210+
211+
if (event->type() == QEvent::KeyRelease) {
212+
const auto keyEvent = dynamic_cast<QKeyEvent*>(event);
213+
214+
if (keyEvent->key() == Qt::Key_Alt) {
215+
setIsNavigating(false);
216+
217+
return QOpenGLWidget::event(event);
218+
}
219+
}
220+
202221
return QOpenGLWidget::event(event);
203222
}
204223

@@ -285,18 +304,13 @@ void ScatterplotWidget::computeDensity()
285304
// by reference then we can upload the data to the GPU, but not store it in the widget.
286305
void ScatterplotWidget::setData(const std::vector<Vector2f>* points)
287306
{
288-
auto dataBounds = getDataBounds(*points);
289-
290-
// pass un-adjusted data bounds to renderer for 2D colormapping
291-
_pointRenderer.setDataBounds(QRectF(dataBounds.getLeft(), dataBounds.getTop(), dataBounds.getWidth(), dataBounds.getHeight()));
292-
293-
const auto shouldSetBounds = (mv::projects().isOpeningProject() || mv::projects().isImportingProject()) ? false : !_navigationAction.getFreezeZoomAction().isChecked();
307+
auto bounds = getDataBounds(*points);
294308

295-
//if (shouldSetBounds)
296-
// _pointRenderer.setViewBounds(dataBounds);
309+
const auto dataBounds = QRectF(bounds.getLeft(), bounds.getTop(), bounds.getWidth(), bounds.getHeight());
297310

298-
_densityRenderer.setBounds(dataBounds);
299-
_dataRectangleAction.setBounds(dataBounds);
311+
_pointRenderer.setDataBounds(dataBounds);
312+
_densityRenderer.setDataBounds(dataBounds);
313+
_dataRectangleAction.setBounds(bounds);
300314

301315
_pointRenderer.setData(*points);
302316
_densityRenderer.setData(points);
@@ -316,7 +330,6 @@ void ScatterplotWidget::setData(const std::vector<Vector2f>* points)
316330
break;
317331
}
318332
}
319-
// _pointRenderer.setSelectionOutlineColor(Vector3f(1, 0, 0));
320333

321334
update();
322335
}

0 commit comments

Comments
 (0)