/** Initialisation method. Sets up all widgets and variables not done in the constructor.
*
*/
void AxisDetails::initWidgets()
{
  if (m_initialised)
  {
    return;
  }
  else
  {
    Plot *p = m_graph->plotWidget();
    int style = (int) m_graph->axisType(m_mappedaxis);

    bool axisOn = p->axisEnabled(m_mappedaxis);
    const QList<int> majTicks = p->getMajorTicksType();
    const QList<int> minTicks = p->getMinorTicksType();

    const QwtScaleDraw *sd = p->axisScaleDraw(m_mappedaxis);
    bool labelsOn = sd->hasComponent(QwtAbstractScaleDraw::Labels);

    int format = p->axisLabelFormat(m_mappedaxis);

    //Top
    m_chkShowAxis->setChecked(axisOn);
    m_txtTitle->setText(m_graph->axisTitle(m_mappedaxis));
    m_labelFont = m_graph->axisTitleFont(m_mappedaxis);

    //bottom left
    m_cmbAxisType->setCurrentIndex(style);
    setAxisFormatOptions(style);
    m_scaleFont = p->axisFont(m_mappedaxis);

    m_cbtnAxisColor->setColor(m_graph->axisColor(m_mappedaxis));

    m_cmbMajorTicksType->setCurrentIndex(majTicks[m_mappedaxis]);
    m_cmbMinorTicksType->setCurrentIndex(minTicks[m_mappedaxis]);

    QwtScaleWidget *scale = dynamic_cast<QwtScaleWidget *>(p->axisWidget(m_mappedaxis));
    if (scale)
    {
      m_spnBaseline->setValue(scale->margin());
    }
    else
    {
      m_spnBaseline->setValue(0);
    }

    //bottom right
    m_grpShowLabels->setChecked(labelsOn);

    m_cmbFormat->setEnabled(labelsOn && axisOn);
    m_cmbFormat->setCurrentIndex(format);

    if (m_cmbAxisType->currentIndex() == ScaleDraw::Numeric)
    {
      m_spnPrecision->setValue(p->axisLabelPrecision(m_mappedaxis));
    }
    else if (m_cmbAxisType->currentIndex() == ScaleDraw::Text)
    {
      m_cmbColName->setCurrentText(m_graph->axisFormatInfo(m_mappedaxis));
    }

    m_spnPrecision->setEnabled(format != 0);

    if (m_mappedaxis == QwtPlot::xBottom || m_mappedaxis == QwtPlot::xTop)
    {
      m_spnAngle->setEnabled(labelsOn && axisOn);
      m_spnAngle->setValue(m_graph->labelsRotation(m_mappedaxis));
    }
    else
    {
      m_spnAngle->setEnabled(false);
      m_spnAngle->setValue(0);
    }

    m_cbtnAxisNumColor->setColor(m_graph->axisLabelsColor(m_mappedaxis));

    QString formula = m_graph->axisFormula(m_mappedaxis);
    m_txtFormula->setFixedWidth(150);

    if (!formula.isEmpty())
    {
      m_chkShowFormula->setChecked(true);
      m_txtFormula->setEnabled(true);
      m_txtFormula->setText(formula);
    }
    else
    {
      m_chkShowFormula->setChecked(false);
      m_txtFormula->setEnabled(false);
    }
    showAxis();

    connect(m_chkShowFormula, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
    connect(m_chkShowAxis, SIGNAL(stateChanged(int)), this, SLOT(setModified()));

    connect(m_cmbAxisType, SIGNAL(currentIndexChanged(int)), this, SLOT(setModified()));
    connect(m_cmbAxisType, SIGNAL(editTextChanged(QString)), this, SLOT(setModified()));
    connect(m_cmbMajorTicksType, SIGNAL(currentIndexChanged(int)), this,  SLOT(setModified()));
    connect(m_cmbMajorTicksType, SIGNAL(editTextChanged(QString)), this, SLOT(setModified()));
    connect(m_cmbTableName, SIGNAL(currentIndexChanged(int)), this,  SLOT(setModified()));
    connect(m_cmbTableName, SIGNAL(editTextChanged(QString)), this, SLOT(setModified()));
    connect(m_cmbMinorTicksType, SIGNAL(currentIndexChanged(int)), this,  SLOT(setModified()));
    connect(m_cmbMinorTicksType, SIGNAL(editTextChanged(QString)), this, SLOT(setModified()));
    connect(m_cmbColName, SIGNAL(currentIndexChanged(int)), this,  SLOT(setModified()));
    connect(m_cmbColName, SIGNAL(editTextChanged(QString)), this, SLOT(setModified()));
    connect(m_cmbFormat, SIGNAL(currentIndexChanged(int)), this,  SLOT(setModified()));
    connect(m_cmbFormat, SIGNAL(editTextChanged(QString)), this, SLOT(setModified()));
    connect(m_grpShowLabels, SIGNAL(clicked(bool)), this,  SLOT(setModified()));
    connect(m_btnAxesFont, SIGNAL(clicked()), this, SLOT(setModified()));
    connect(m_btnLabelFont, SIGNAL(clicked()), this, SLOT(setModified()));
    connect(m_txtFormula, SIGNAL(textChanged()), this, SLOT(setModified()));
    connect(m_txtTitle, SIGNAL(textChanged()), this, SLOT(setModified()));
    connect(m_formatButtons, SIGNAL(formattingModified()), this, SLOT(setModified()));
    connect(m_spnPrecision,SIGNAL(valueChanged(int)), this, SLOT(setModified()));
    connect(m_spnAngle,SIGNAL(valueChanged(int)), this, SLOT(setModified()));
    connect(m_spnBaseline,SIGNAL(valueChanged(int)), this, SLOT(setModified()));
    connect(m_cbtnAxisColor, SIGNAL(colorChanged()), this, SLOT(setModified()));
    connect(m_cbtnAxisNumColor, SIGNAL(colorChanged()), this, SLOT(setModified()));

    m_modified = false;
    m_initialised = true;
  }
}