| 4081 | */ |
| 4082 | template<class PlottableType> |
| 4083 | PlottableType *QCustomPlot::plottableAt(const QPointF &pos, bool onlySelectable, int *dataIndex) const |
| 4084 | { |
| 4085 | PlottableType *resultPlottable = 0; |
| 4086 | QVariant resultDetails; |
| 4087 | double resultDistance = mSelectionTolerance; // only regard clicks with distances smaller than mSelectionTolerance as selections, so initialize with that value |
| 4088 | |
| 4089 | foreach (QCPAbstractPlottable *plottable, mPlottables) |
| 4090 | { |
| 4091 | PlottableType *currentPlottable = qobject_cast<PlottableType*>(plottable); |
| 4092 | if (!currentPlottable || (onlySelectable && !currentPlottable->selectable())) // we could have also passed onlySelectable to the selectTest function, but checking here is faster, because we have access to QCPAbstractPlottable::selectable |
| 4093 | continue; |
| 4094 | if (currentPlottable->clipRect().contains(pos.toPoint())) // only consider clicks where the plottable is actually visible |
| 4095 | { |
| 4096 | QVariant details; |
| 4097 | double currentDistance = currentPlottable->selectTest(pos, false, dataIndex ? &details : nullptr); |
| 4098 | if (currentDistance >= 0 && currentDistance < resultDistance) |
| 4099 | { |
| 4100 | resultPlottable = currentPlottable; |
| 4101 | resultDetails = details; |
| 4102 | resultDistance = currentDistance; |
| 4103 | } |
| 4104 | } |
| 4105 | } |
| 4106 | |
| 4107 | if (resultPlottable && dataIndex) |
| 4108 | { |
| 4109 | QCPDataSelection sel = resultDetails.value<QCPDataSelection>(); |
| 4110 | if (!sel.isEmpty()) |
| 4111 | *dataIndex = sel.dataRange(0).begin(); |
| 4112 | } |
| 4113 | return resultPlottable; |
| 4114 | } |
| 4115 | |
| 4116 | /*! |
| 4117 | Returns the item at the pixel position \a pos. The item type (a QCPAbstractItem subclass) that shall be |