/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 * 
 *
 *   This file is part of MoleCuilder.
 *
 *    MoleCuilder 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 2 of the License, or
 *    (at your option) any later version.
 *
 *    MoleCuilder 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 MoleCuilder.  If not, see .
 */
/*
 * QtQueryList.cpp
 *
 *  Created on: Jul 24, 2012
 *      Author: ankele
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include 
#include 
#include 
//#include "CodePatterns/MemDebug.hpp"
#include "UIElements/Qt4/Query/QtQueryList.hpp"
QtQueryListUntyped::QtQueryListUntyped(QBoxLayout *parent, Dialog *_dialog) :
  dialog(_dialog)
{
  thisVLayout = new QVBoxLayout();
  thisHLayout = new QHBoxLayout();
  buttonBox = new QVBoxLayout();
  inputList = new QListWidget();
  inputList->setSelectionMode(QAbstractItemView::ExtendedSelection);
  addButton = new QPushButton("Add");
  addButton->setEnabled(false);
  removeButton = new QPushButton("Remove");
  removeButton->setEnabled(false);
  buttonBox->addWidget(addButton);
  buttonBox->addWidget(removeButton);
  thisHLayout->addWidget(inputList);
  thisHLayout->addLayout(buttonBox);
  thisVLayout->addLayout(thisHLayout);
  parent->addLayout(thisVLayout);
}
void QtQueryListUntyped::onUpdate() {
  dialog->update();
}
void QtQueryListUntyped::elementSelected()
{
  removeButton->setEnabled(!inputList->selectedItems().empty());
}
void QtQueryListUntyped::addElementToListWidget(const std::string &str)
{
  inputList->addItem(QString(str.c_str()));
}
std::vector QtQueryListUntyped::getSelectedRows()
{
  std::vector rows;
  QList items = inputList->selectedItems();
  for (QList::iterator iter = items.begin(); !items.empty(); iter = items.begin()){
    rows.push_back(inputList->row(*iter));
    items.erase(iter);
  }
  return rows;
}
void QtQueryListUntyped::removeSelectedRows(const std::vector &rows)
{
  for (int i = rows.size() - 1; i >= 0; i --)
    inputList->takeItem(rows[i]);
}