Ejemplo n.º 1
0
// Select the point at a position.
void XtalOptPlot::select(const QPoint& pos)
{
  QwtPlotMarker* selection = nullptr;

  // Must be within 10 pixels at least
  double dist = 10.0;

  const QwtPlotItemList& itmList = itemList();
  for (QwtPlotItemIterator it = itmList.begin(); it != itmList.end(); ++it) {
    if ((*it)->rtti() == QwtPlotItem::Rtti_PlotMarker) {
      QwtPlotMarker* m = static_cast<QwtPlotMarker*>(*it);
      // We have to map the plot coordinates onto widget coordinates before
      // comparing
      const QwtScaleMap& xMap = canvasMap(m->xAxis());
      const QwtScaleMap& yMap = canvasMap(m->yAxis());
      double mx = xMap.transform(m->xValue());
      double my = yMap.transform(m->yValue());
      double d = distance(QPointF(mx, my), pos);
      if (d < dist) {
        selection = m;
        dist = d;
      }
    }
  }

  selectMarker(selection);
}
Ejemplo n.º 2
0
bool CanvasPicker::eventFilter(QObject *object, QEvent *e)
{

	QVector<int> images = plot()->imageMarkerKeys();
	QVector<int> lines = plot()->lineMarkerKeys();

	if (object != (QObject *)plot()->plotWidget()->canvas())
		return false;

	switch(e->type())
	{
		case QEvent::MouseButtonPress:
			{				
				plot()->deselect();
				emit selectPlot();

				const QMouseEvent *me = (const QMouseEvent *)e;
				bool allAxisDisabled = true;
				for (int i=0; i < QwtPlot::axisCnt; i++){
					if (plotWidget->axisEnabled(i)){
						allAxisDisabled = false;
						break;
					}
				}

                int dist, point;
                plotWidget->closestCurve(me->pos().x(), me->pos().y(), dist, point);

				if (me->button() == Qt::LeftButton && (plot()->drawLineActive())){
					startLinePoint = me->pos();
					return true;
				}

				if (me->button() == Qt::LeftButton && plot()->drawTextActive()){
					drawTextMarker(me->pos());
					return true;
				}

				if (!plot()->zoomOn() && selectMarker(me)){
					if (me->button() == Qt::RightButton)
						emit showMarkerPopupMenu();
					return true;
				}

				return !(me->modifiers() & Qt::ShiftModifier);
			}
			break;

		case QEvent::MouseButtonDblClick:
			{
				if (d_editing_marker) {
					return d_editing_marker->eventFilter(plotWidget->canvas(), e);
				} else if (plot()->selectedMarkerKey() >= 0) {
					if (lines.contains(plot()->selectedMarkerKey())){
						emit viewLineDialog();
						return true;
					} else if (images.contains(plot()->selectedMarkerKey())){
						emit viewImageDialog();
						return true;
					}
				} else if (plot()->isPiePlot()){
                        emit showPlotDialog(plot()->curveKey(0));
                        return true;
				} else {
					const QMouseEvent *me = (const QMouseEvent *)e;
                    int dist, point;
                    int curveKey = plotWidget->closestCurve(me->pos().x(), me->pos().y(), dist, point);
                    if (dist < 10)
                        emit showPlotDialog(curveKey);
                    else
                        emit showPlotDialog(-1);
					return true;
				}
			}
			break;

		case QEvent::MouseMove:
			{
			    
				const QMouseEvent *me = (const QMouseEvent *)e;
				if (me->state() != Qt::LeftButton)
  	            	return true;

				QPoint pos = me->pos();
				DataCurve *c = plot()->selectedCurveLabels();
				if (c){
					
					c->moveLabels(pos);
					return true;
				}

				if (plot()->drawLineActive()) {
					drawLineMarker(pos, plot()->drawArrow());
					return true;
				}
				return false;
			}
			break;

		case QEvent::MouseButtonRelease:
			{
				const QMouseEvent *me = (const QMouseEvent *)e;
				Graph *g = plot();

				if (g->drawLineActive()) {
					ApplicationWindow *app = g->multiLayer()->applicationWindow();
					if (!app)
						return true;
					
					ArrowMarker mrk;
					mrk.attach(g->plotWidget());
					mrk.setStartPoint(startLinePoint);
					mrk.setEndPoint(QPoint(me->x(), me->y()));
					mrk.setColor(app->defaultArrowColor);
					mrk.setWidth(app->defaultArrowLineWidth);
					mrk.setStyle(app->defaultArrowLineStyle);
					mrk.setHeadLength(app->defaultArrowHeadLength);
					mrk.setHeadAngle(app->defaultArrowHeadAngle);
					mrk.fillArrowHead(app->defaultArrowHeadFill);
					mrk.drawEndArrow(g->drawArrow());
					mrk.drawStartArrow(false);

					g->addArrow(&mrk);
					g->drawLine(false);
					mrk.detach();
					plotWidget->replot();
					return true;
				}
				
				/*if(plot()->zoomOn())
				{
					//fix for colormap changing on releasing the mouse button after zooming
					Graph * gr=plot();
					if(!gr)
						return NULL;
					Spectrogram * spectrogram=gr->getSpectrogram();
					if(spectrogram)
						spectrogram->setupColorBarScaling();
				}*/
				return false;
			}
			break;

		case QEvent::KeyPress:
			{
				int key=((const QKeyEvent *)e)->key();

				int selectedMarker = plot()->selectedMarkerKey();
				if (lines.contains(selectedMarker) &&
						(key==Qt::Key_Enter || key==Qt::Key_Return))
				{
					emit viewLineDialog();
					return true;
				}
				if (images.contains(selectedMarker) &&
						(key==Qt::Key_Enter || key==Qt::Key_Return))
				{
					emit viewImageDialog();
					return true;
				}
			}
			break;

		default:
			break;
	}
	return QObject::eventFilter(object, e);
}
Ejemplo n.º 3
0
// Select the next/previous neighbour of the selected point
// 0 = up
// 1 = down
// 2 = left
// 3 = right
void XtalOptPlot::shiftMarkerCursor(int direction)
{
  if (!m_selectedMarker)
    return;

  QwtPlotMarker* selection = nullptr;

  double dist = 1e300;

  const QwtPlotItemList& itmList = itemList();
  for (QwtPlotItemIterator it = itmList.begin(); it != itmList.end(); ++it) {
    if ((*it)->rtti() == QwtPlotItem::Rtti_PlotMarker) {
      QwtPlotMarker* m = static_cast<QwtPlotMarker*>(*it);

      // Skip over the point that is already selected
      if (m == m_selectedMarker)
        continue;

      // Find the distance and see if it is the shortest so far

      // If we ever want to do this with canvas coordinates, this is how
      // it is done:

      // double x1 = canvasMap(m->xAxis()).transform(m->xValue());
      // double y1 = canvasMap(m->yAxis()).transform(m->yValue());
      // double x2 = canvasMap(m_selectedMarker->xAxis())
      //                 .transform(m_selectedMarker->xValue());
      // double y2 = canvasMap(m_selectedMarker->yAxis())
      //                 .transform(m_selectedMarker->yValue());
      // double d = distance(QPointF(x1, y1), QPointF(x2, y2));

      double d = distance(m->value(), m_selectedMarker->value());
      if (d > dist)
        continue;

      // Let's make sure the direction is correct as well
      switch (direction) {
        // up
        case 0: {
          // Check to make sure this is actually up
          if (m->yValue() - m_selectedMarker->yValue() > 0.0) {
            selection = m;
            dist = d;
          }
          break;
        }
        // down
        case 1: {
          // Check to make sure this is actually down
          if (m_selectedMarker->yValue() - m->yValue() > 0.0) {
            selection = m;
            dist = d;
          }
          break;
        }
        // left
        case 2: {
          // Check to make sure this is actually left
          if (m_selectedMarker->xValue() - m->xValue() > 0.0) {
            selection = m;
            dist = d;
          }
          break;
        }
        // right
        case 3: {
          // Check to make sure this is actually right
          if (m->xValue() - m_selectedMarker->xValue() > 0.0) {
            selection = m;
            dist = d;
          }
          break;
        }
      }
    }
  }

  selectMarker(selection);
}
Ejemplo n.º 4
0
bool CanvasPicker::eventFilter(QObject *object, QEvent *e)
{
	if (object != (QObject *)plot()->canvas())
		return false;

	Graph *g = plot();
	QList<QwtPlotMarker *> lines = g->linesList();
	switch(e->type())
	{
		case QEvent::MouseButtonPress:
			{
                const QMouseEvent *me = (const QMouseEvent *)e;
				if (!(me->modifiers() & Qt::ShiftModifier))
                    g->deselect();

				emit selectPlot();

				bool allAxisDisabled = true;
				for (int i=0; i < QwtPlot::axisCnt; i++){
					if (g->axisEnabled(i)){
						allAxisDisabled = false;
						break;
					}
				}

                int dist, point;
                g->closestCurve(me->pos().x(), me->pos().y(), dist, point);

				if (me->button() == Qt::LeftButton && (g->drawLineActive())){
					startLinePoint = me->pos();
					return true;
				}

				if (!g->zoomOn() && selectMarker(me)){
					if (me->button() == Qt::RightButton)
						emit showMarkerPopupMenu();
					return true;
				}

				return !(me->modifiers() & Qt::ShiftModifier);
			}
			break;

		case QEvent::MouseButtonDblClick:
			{
				if (d_editing_marker) {
					return d_editing_marker->eventFilter(g->canvas(), e);
				} else if (g->selectedMarker()) {
					if (lines.contains(g->selectedMarker())){
						emit viewLineDialog();
						return true;
					}
				} else if (g->isPiePlot()){
                	emit showPlotDialog(0);
                    return true;
				} else {
					const QMouseEvent *me = (const QMouseEvent *)e;
                    int dist, point;
                    QwtPlotCurve *c = g->closestCurve(me->pos().x(), me->pos().y(), dist, point);
                    if (c && dist < 10)
                        emit showPlotDialog(g->curveIndex(c));
                    else
                        emit showPlotDialog(-1);
					return true;
				}
			}
			break;

		case QEvent::MouseMove:
			{
				const QMouseEvent *me = (const QMouseEvent *)e;
				if (me->state() != Qt::LeftButton)
  	            	return true;

				QPoint pos = me->pos();

				DataCurve *c = g->selectedCurveLabels();
				if (c){
					c->moveLabels(pos);
					return true;
				}

				if (plot()->drawLineActive()) {
					drawLineMarker(pos, g->drawArrow());
					return true;
				}

				return false;
			}
			break;

		case QEvent::MouseButtonRelease:
			{
				const QMouseEvent *me = (const QMouseEvent *)e;
				if (g->drawLineActive()) {
					ApplicationWindow *app = g->multiLayer()->applicationWindow();
					if (!app)
						return true;

					ArrowMarker mrk;
					mrk.attach(g);
					mrk.setStartPoint(startLinePoint);
					mrk.setEndPoint(QPoint(me->x(), me->y()));
					mrk.setColor(app->defaultArrowColor);
					mrk.setWidth(app->defaultArrowLineWidth);
					mrk.setStyle(app->defaultArrowLineStyle);
					mrk.setHeadLength(app->defaultArrowHeadLength);
					mrk.setHeadAngle(app->defaultArrowHeadAngle);
					mrk.fillArrowHead(app->defaultArrowHeadFill);
					mrk.drawEndArrow(g->drawArrow());
					mrk.drawStartArrow(false);

					g->addArrow(&mrk);
					g->drawLine(false);
					mrk.detach();
					g->replot();

					return true;
				}
				return false;
			}
			break;

		case QEvent::KeyPress:
			{
				int key = ((const QKeyEvent *)e)->key();

				QwtPlotMarker *selectedMarker = g->selectedMarker();
				if (lines.contains(selectedMarker) &&
					(key == Qt::Key_Enter || key == Qt::Key_Return)){
					emit viewLineDialog();
					return true;
				}
			}
			break;

		default:
			break;
	}
	return QObject::eventFilter(object, e);
}