/* 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 <QWidgetAction>
#include <QPushButton>

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


/////////////////////// Local includes
#include "MsXpS/libXpertMassGui/LabelledMultiButtonAction.hpp"


namespace MsXpS
{
  namespace libXpertMassGui
  {


  LabelledMultiButtonAction::LabelledMultiButtonAction(const QString &text,
                                                       QObject *parent)
    : QWidgetAction(parent), mpa_label(new QLabel(text))
  {
    mpa_buttonsLayout = new QHBoxLayout(mpa_buttonBox);
    mpa_buttonsLayout->setContentsMargins(0., 0, 0, 0);

    mpa_buttonBox = new QWidget(static_cast<QWidget *>(parent));

    mpa_buttonBox->setLayout(mpa_buttonsLayout);

    QWidget *composite_widget_p = new QWidget(static_cast<QWidget *>(parent));

    QHBoxLayout *layout_p = new QHBoxLayout(composite_widget_p);
    layout_p->setContentsMargins(0., 0, 0, 0);

    layout_p->addWidget(mpa_buttonBox);
    layout_p->addWidget(mpa_label);

    composite_widget_p->setLayout(layout_p);

    setDefaultWidget(composite_widget_p);
  };

  LabelledMultiButtonAction::LabelledMultiButtonAction(
    const QString &text, QList<QPushButton *> buttons, QObject *parent)
    : QWidgetAction(parent), mpa_label(new QLabel(text)), m_buttons(buttons)
  {
    mpa_buttonsLayout = new QHBoxLayout(mpa_buttonBox);
    mpa_buttonsLayout->setContentsMargins(0., 0, 0, 0);

    for(int iter = 0; iter < m_buttons.size(); ++iter)
      mpa_buttonsLayout->addWidget(m_buttons.at(iter));

    mpa_buttonBox->setLayout(mpa_buttonsLayout);

    mpa_buttonBox = new QWidget(static_cast<QWidget *>(parent));

    QWidget *composite_widget_p = new QWidget(static_cast<QWidget *>(parent));

    QHBoxLayout *layout_p = new QHBoxLayout(composite_widget_p);
    layout_p->setContentsMargins(0., 0, 0, 0);

    layout_p->addWidget(mpa_buttonBox);
    layout_p->addWidget(mpa_label);

    composite_widget_p->setLayout(layout_p);

    setDefaultWidget(composite_widget_p);
  }

  LabelledMultiButtonAction::~LabelledMultiButtonAction()
  {
  }

  QLabel *
  LabelledMultiButtonAction::getLabel() const
  {
    return mpa_label;
  }

  void
  LabelledMultiButtonAction::setLabelText(const QString &text)
  {
    mpa_label->setText(text);
  }

  QString
  LabelledMultiButtonAction::getLabelText() const
  {
    return mpa_label->text();
  }

  void
  LabelledMultiButtonAction::addButton(QPushButton *button_p)
  {
    if(button_p == nullptr)
      qFatal() << "Programming error. Pointer cannot be nullptr.";

    m_buttons.append(button_p);
    mpa_buttonsLayout->addWidget(button_p);
  }

  QPushButton *
  LabelledMultiButtonAction::addButton(const QIcon &icon, const QString &text)
  {
    QPushButton *push_button_p = new QPushButton(icon, text);
    addButton(push_button_p);

    return push_button_p;
  }

  QPushButton *
  LabelledMultiButtonAction::getButton(int index) const
  {
    if(index >= m_buttons.size())
      {
        qWarning() << "Out of bonds error.";
        return nullptr;
      }

    return m_buttons.at(index);
  }

  QPushButton *
  LabelledMultiButtonAction::getButton(const QString &text) const
  {
    for(int iter = 0; iter < m_buttons.size(); ++iter)
      {
        if(m_buttons.at(iter)->text() == text)
          return m_buttons.at(iter);
      }

    return nullptr;
  }


  } // namespace libXpertMassGui
} // namespace MsXpS
