
/*

  KLayout Layout Viewer
  Copyright (C) 2006-2018 Matthias Koefferlein

  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 2 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, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/


#include "layBookmarkManagementForm.h"
#include "dbCellInst.h"
#include "layLayoutView.h"

#include <QListWidgetItem>

namespace lay
{

// ------------------------------------------------------------

class BookmarkListLVI 
  : public QListWidgetItem
{
public:
  BookmarkListLVI (QListWidget *parent, const std::string &name, const lay::DisplayState &state)
    : QListWidgetItem (tl::to_qstring (name), parent), m_state (state)
  {
    setFlags (flags () | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled);
  }

  const lay::DisplayState &state () const
  {
    return m_state;
  }

private:
  lay::DisplayState m_state;
};

// ------------------------------------------------------------

BookmarkManagementForm::BookmarkManagementForm (QWidget *parent, const char *name, const lay::BookmarkList &bookmarks)
  : QDialog (parent), Ui::BookmarkManagementForm (),
    m_bookmarks (bookmarks)
{
  setObjectName (QString::fromUtf8 (name));

  Ui::BookmarkManagementForm::setupUi (this);

  for (size_t i = 0; i < m_bookmarks.size (); ++i) {
    new BookmarkListLVI (bookmark_list, m_bookmarks.name (i), m_bookmarks.state (i));
  }

  connect (delete_button, SIGNAL (clicked ()), this, SLOT (delete_pressed ()));
}

void
BookmarkManagementForm::delete_pressed ()
{
  QList<QListWidgetItem *> sel = bookmark_list->selectedItems ();
  for (QList<QListWidgetItem *>::const_iterator i = sel.begin (); i != sel.end (); ++i) {
    delete *i;
  }
}

void
BookmarkManagementForm::accept ()
{
  m_bookmarks.clear ();
  m_bookmarks.reserve (bookmark_list->count ());

  //  TODO: is there an iterator? Here we use the trick to select all and then get the
  //  list of items
  bookmark_list->selectAll (); 
  QList<QListWidgetItem *> sel = bookmark_list->selectedItems ();
  for (QList<QListWidgetItem *>::const_iterator i = sel.begin (); i != sel.end (); ++i) {
    BookmarkListLVI *bm = dynamic_cast<BookmarkListLVI *> (*i);
    if (bm) {
      m_bookmarks.add (tl::to_string (bm->text ()), bm->state ());
    }
  }

  QDialog::accept ();
}

}



