Ejemplo n.º 1
0
/**
 *  @param g :: The Graph widget which will display the curve
 *  @param distr :: True for a distribution
 *  @param style :: The curve type to use
 */
void MantidMatrixCurve::init(Graph *g, bool distr,
                             GraphOptions::CurveType style) {
  // Will throw if name not found but return NULL ptr if the type is incorrect
  MatrixWorkspace_const_sptr workspace =
      AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
          m_wsName.toStdString());

  if (!workspace) // The respective *Data classes will check for index validity
  {
    std::stringstream ss;
    ss << "Workspace named '" << m_wsName.toStdString()
       << "' found but it is not a MatrixWorkspace. ID='"
       << AnalysisDataService::Instance().retrieve(m_wsName.toStdString())->id()
       << "'";
    throw std::invalid_argument(ss.str());
  }

  // Set the curve name if it the non-naming constructor was called
  if (this->title().isEmpty()) {
    // If there's only one spectrum in the workspace, title is simply workspace
    // name
    if (workspace->getNumberHistograms() == 1)
      this->setTitle(m_wsName);
    else
      this->setTitle(createCurveName(workspace));
  }

  Mantid::API::MatrixWorkspace_const_sptr matrixWS =
      boost::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(
          workspace);
  // we need to censor the data if there is a log scale because it can't deal
  // with negative values, only the y-axis has been found to be problem so far
  const bool log = g->isLog(QwtPlot::yLeft);

  // Y units are the same for both spectrum and bin plots, e.g. counts
  m_yUnits.reset(new Mantid::Kernel::Units::Label(matrixWS->YUnit(),
                                                  matrixWS->YUnitLabel()));

  if (m_indexType == Spectrum) // Spectrum plot
  {
    QwtWorkspaceSpectrumData data(*matrixWS, m_index, log, distr);

    setData(data);

    // For spectrum plots, X axis are actual X axis, e.g. TOF
    m_xUnits = matrixWS->getAxis(0)->unit();
  } else // Bin plot
  {
    QwtWorkspaceBinData data(*matrixWS, m_index, log);

    setData(data);

    // For bin plots, X axis are "spectra axis", e.g. spectra numbers
    m_xUnits = matrixWS->getAxis(1)->unit();
  }

  if (!m_xUnits) {
    m_xUnits.reset(new Mantid::Kernel::Units::Empty());
  }

  int lineWidth = 1;
  MultiLayer *ml = dynamic_cast<MultiLayer *>(g->parent()->parent()->parent());
  if (ml && (style == GraphOptions::Unspecified ||
             ml->applicationWindow()->applyCurveStyleToMantid)) {
    applyStyleChoice(style, ml, lineWidth);
  } else if (matrixWS->isHistogramData() && !matrixWS->isDistribution()) {
    setStyle(QwtPlotCurve::Steps);
    setCurveAttribute(
        Inverted,
        true); // this is the Steps style modifier that makes horizontal steps
  } else {
    setStyle(QwtPlotCurve::Lines);
  }
  g->insertCurve(this, lineWidth);

  // set the option to draw all error bars from the global settings
  if (hasErrorBars()) {
    setErrorBars(true, g->multiLayer()->applicationWindow()->drawAllErrors);
  }
  // Initialise error bar colour to match curve colour
  m_errorSettings->m_color = pen().color();
  m_errorSettings->setWidth(pen().widthF());

  connect(g, SIGNAL(axisScaleChanged(int, bool)), this,
          SLOT(axisScaleChanged(int, bool)));
  observePostDelete();
  connect(this, SIGNAL(resetData(const QString &)), this,
          SLOT(dataReset(const QString &)));
  observeAfterReplace();
  observeADSClear();
}