
/* BEGIN software license
 *
 * msXpertSuite - mass spectrometry software suite
 * -----------------------------------------------
 * Copyright (C) 2009--2020 Filippo Rusconi
 *
 * http://www.msxpertsuite.org
 *
 * This file is part of the msXpertSuite project.
 *
 * The msXpertSuite project is the successor of the massXpert project. This
 * project now includes various independent modules:
 *
 * - massXpert, model polymer chemistries and simulate mass spectrometric data;
 * - mineXpert, a powerful TIC chromatogram/mass spectrum viewer/miner;
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * END software license
 */


/////////////////////// StdLib includes


/////////////////////// Qt includes
#include <QWidget>


/////////////////////// QCustomPlot


/////////////////////// pappsomspp includes


/////////////////////// Local includes
#include "TracePlotLabel.hpp"


namespace MsXpS
{
namespace MineXpert
{


TracePlotLabel::TracePlotLabel(QObject *parent): QObject(parent)
{
  // The parent HAS to be the QCustomPlot.
  QCustomPlot *parent_p = static_cast<QCustomPlot *>(parent);
  Q_ASSERT(parent_p);
}

TracePlotLabel::TracePlotLabel(const QString &text,
                               const QString &plot_layer_name,
                               QObject *parent)
  : QObject(parent), m_text(text), m_plotLayerName(plot_layer_name)
{
  // The parent HAS to be the QCustomPlot.
  QCustomPlot *parent_p = static_cast<QCustomPlot *>(parent);
  Q_ASSERT(parent_p);
}

TracePlotLabel::TracePlotLabel(const QString &text,
                               const QString &plot_layer_name,
                               const QPointF &data_point,
                               QObject *parent)
  : QObject(parent),
    m_text(text),
    m_plotLayerName(plot_layer_name),
    m_dataPoint(data_point)
{
  // The parent HAS to be the QCustomPlot.
  QCustomPlot *parent_p = static_cast<QCustomPlot *>(parent);
  Q_ASSERT(parent_p);
}

TracePlotLabel::TracePlotLabel(const QString &text,
                               const QString &plot_layer_name,
                               const QPointF &data_point,
                               const QPointF &draw_point,
                               QObject *parent)
  : QObject(parent),
    m_text(text),
    m_plotLayerName(plot_layer_name),
    m_dataPoint(data_point),
    m_drawPoint(draw_point)
{
  // The parent HAS to be the QCustomPlot.
  QCustomPlot *parent_p = static_cast<QCustomPlot *>(parent);
  Q_ASSERT(parent_p);
}

TracePlotLabel::TracePlotLabel(const QString &text,
                               const QString &plot_layer_name,
                               const QPointF &data_point,
                               const QPointF &draw_point,
                               int vertical_shift_count,
                               QObject *parent)
  : QObject(parent),
    m_text(text),
    m_plotLayerName(plot_layer_name),
    m_dataPoint(data_point),
    m_drawPoint(draw_point),
    m_verticalShiftCount(vertical_shift_count)
{
  // The parent HAS to be the QCustomPlot.
  QCustomPlot *parent_p = static_cast<QCustomPlot *>(parent);
  Q_ASSERT(parent_p);
}

TracePlotLabel::~TracePlotLabel()
{
  // qDebug() << "Destructing label with text:" << mrp_qcpItemText->text();
  static_cast<QCustomPlot *>(parent())->removeItem(mrp_qcpItemText);
}

void
TracePlotLabel::setText(const QString &text)
{
  m_text = text;
}

QString
TracePlotLabel::getText() const
{
  return m_text;
}

void
TracePlotLabel::clearText()
{
  m_text.clear();
}

void
TracePlotLabel::setPlotLayerName(const QString &plot_layer_name)
{
  if(plot_layer_name.isEmpty())
    m_plotLayerName = "labelsLayer";
  else
    m_plotLayerName = plot_layer_name;
}

QString
TracePlotLabel::getPlotLayerName() const
{
  return m_plotLayerName;
}

void
TracePlotLabel::write(const QFont &font, const QColor &color)
{
  if(mrp_qcpItemText == nullptr)
    mrp_qcpItemText =
      new QCPItemText(static_cast<QCustomPlot *>(QObject::parent()));

  // We may be relayouting labels, but if this label was hidden, then,
  // maintain it hidden.
  bool was_visible = mrp_qcpItemText->visible();

  mrp_qcpItemText->setText(m_text);
  mrp_qcpItemText->setLayer(m_plotLayerName);

  mrp_qcpItemText->setFont(font);
  mrp_qcpItemText->setColor(color);

  mrp_qcpItemText->setPositionAlignment(Qt::AlignBottom | Qt::AlignLeft);
  mrp_qcpItemText->position->setType(QCPItemPosition::ptPlotCoords);
  mrp_qcpItemText->position->setCoords(m_drawPoint.x(), m_drawPoint.y());

  // qDebug() << "Writing label for data point:" << m_dataPoint
  //          << "to draw point:" << m_drawPoint;

  mrp_qcpItemText->setRotation(-60);
  mrp_qcpItemText->setVisible(was_visible);
}

void
TracePlotLabel::show()
{
  if(mrp_qcpItemText == nullptr)
    {
      qDebug() << "The member mrp_qcpItemText is nullptr !!!";

      return;
    }

  mrp_qcpItemText->setVisible(true);

  // qDebug() << "Shown label for text:" << mrp_qcpItemText->text();
}

void
TracePlotLabel::hide()
{
  if(mrp_qcpItemText == nullptr)
    {
      qDebug() << "The member mrp_qcpItemText is nullptr !!!";

      return;
    }

  mrp_qcpItemText->setVisible(false);

  // qDebug() << "Hidden label for text:" << mrp_qcpItemText->text();
}

void
TracePlotLabel::eraseLabel()
{
  if(mrp_qcpItemText == nullptr)
    {
      qDebug() << "The member mrp_qcpItemText is nullptr !!!";

      return;
    }

  static_cast<QCustomPlot *>(parent())->removeItem(mrp_qcpItemText);
}

void
TracePlotLabel::setVisible(bool visible)
{
  mrp_qcpItemText->setVisible(visible);
}

bool
TracePlotLabel::isVisible()
{
  if(mrp_qcpItemText == nullptr)
    {
      qDebug() << "The member mrp_qcpItemText is nullptr !!!";

      return false;
    }

  return mrp_qcpItemText->visible();
}

void
TracePlotLabel::toggleVisibility()
{
  if(mrp_qcpItemText == nullptr)
    {
      qDebug() << "The member mrp_qcpItemText is nullptr !!!";

      return;
    }

  mrp_qcpItemText->setVisible(!mrp_qcpItemText->visible());
}

void
TracePlotLabel::setDataPoint(const QPointF &data_point)
{
  m_dataPoint = data_point;
}

QPointF
TracePlotLabel::getDataPoint() const
{
  return m_dataPoint;
}

void
TracePlotLabel::setDrawPoint(const QPointF &draw_point)
{
  m_drawPoint = draw_point;
}

QPointF
TracePlotLabel::getDrawPoint() const
{
  return m_drawPoint;
}

void
TracePlotLabel::setVerticalShiftCount(int vertical_shift_count)
{
  m_verticalShiftCount = vertical_shift_count;
}

int
TracePlotLabel::getVerticalShiftCount() const
{
  return m_verticalShiftCount;
}

QString
TracePlotLabel::toString() const
{
  QString text =
    QString(
      "Trace plot label: %1 (data point: (%2,%3) ; draw point (%4,%5) "
      "; vertical shift count %6\n")
      .arg(m_text)
      .arg(m_dataPoint.x(), 0, 'f', 5)
      .arg(m_dataPoint.y(), 0, 'f', 0)
      .arg(m_drawPoint.x(), 0, 'f', 5)
      .arg(m_drawPoint.y(), 0, 'f', 0)
      .arg(m_verticalShiftCount);

  return text;
}

} // namespace MineXpert

} // namespace MsXpS
