
/* 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


/////////////////////// libXpertMassCore includes
#include <MsXpS/libXpertMassCore/IsotopicCluster.hpp>


/////////////////////// libXpertMassGui includes


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


namespace MsXpS
{
namespace MineXpert
{

MassModellingMethodSelectorWidget::MassModellingMethodSelectorWidget(
  QWidget *parent)
  : QWidget(parent)
{
  // Make it look like a floating panel
  // setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);

  // If used as a child widget, windowFlags aren't strictly needed,
  // but styling is. Let's use it as a standard child widget.

  QVBoxLayout *layout = new QVBoxLayout(this);
  layout->setContentsMargins(5, 5, 5, 5);
  layout->setSpacing(2);

  // Group the buttons so only one can be checked at a time
  mp_buttonGroup = new QButtonGroup(this);
  mp_buttonGroup->setExclusive(true);

  QPushButton *button_p = nullptr;

  // We do want to keep the NOT_SET item of the enumeration.
  const QMap<int, QString>::const_iterator the_iterator =
    libXpertMassCore::IsotopicCluster::averageMassDeterminationMethodMap
      .constBegin();

  // We do not want to keep the LAST item of the enumeration.
  const QMap<int, QString>::const_iterator the_end_iterator = std::prev(
    libXpertMassCore::IsotopicCluster::averageMassDeterminationMethodMap
      .constEnd());

  for(auto iterator = the_iterator; iterator != the_end_iterator; ++iterator)
    {
      int id = iterator.key();

      QString label = iterator.value();

      qDebug() << "Id:" << id << "Label:" << label;

      button_p = new QPushButton(label);
      button_p->setCheckable(true);
      button_p->setFocusPolicy(Qt::NoFocus);

      layout->addWidget(button_p);

      mp_buttonGroup->addButton(button_p, id);
    }


  setStyleSheet(R"(
            MonoMassModelMethodSelectorWidget {
                background-color: rgba(240, 240, 240, 220);
                border: 1px solid #cccccc;
                border-radius: 4px;
            }
            QPushButton {
                text-align: left;
                padding: 4px 8px;
                border: 1px solid transparent;
                border-radius: 3px;
            }
            QPushButton:checked {
                background-color: #0078d7;
                color: white;
                border: 1px solid #005a9e;
            }
            QPushButton:hover:!checked {
                background-color: rgba(200, 200, 200, 150);
            }
        )");

  connect(mp_buttonGroup,
          &QButtonGroup::idClicked,
          this,
          &MassModellingMethodSelectorWidget::onModelMethodSelected);

  // Start with the MZ_WEIGHTED_INTENSITIES button checked first.
  button_p = dynamic_cast<QPushButton *>(mp_buttonGroup->buttons().at(0));
  Q_ASSERT(button_p);

  m_lastGroupButtonId = mp_buttonGroup->id(button_p);

  button_p->setChecked(true);
}

void
MassModellingMethodSelectorWidget::onModelMethodSelected(int id)
{
  emit modelMethodSelectedSignal(id);
  m_lastGroupButtonId = id;
  qDebug() << "Set the current method to:" << m_lastGroupButtonId;
}

QString
MassModellingMethodSelectorWidget::getCurrentMassModellingMethodAsString() const
{
  return libXpertMassCore::IsotopicCluster::getAvgMassDeterminationMethod(
    m_lastGroupButtonId);
}

libXpertMassCore::IsotopicCluster::AverageMassDeterminationMethod
MassModellingMethodSelectorWidget::getCurrentMassModellingMethod() const
{
  return static_cast<
    libXpertMassCore::IsotopicCluster::AverageMassDeterminationMethod>(
    m_lastGroupButtonId);
}

} // namespace MineXpert

} // namespace MsXpS
