/*! @file

    @id $Id$
*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

#ifndef __EDITBOOKMARKS_HXX__
#define __EDITBOOKMARKS_HXX__

#include <QtGui/QDialog>
#include <QtGui/QComboBox>
#include <QtGui/QLineEdit>
#include <ui_editbookmarks.h>

#include <QDebug>

class EditBookmarks: public QDialog, protected Ui::EditBookmarks {
    Q_OBJECT;
  Q_SIGNALS:
    void startEdit(bool=true);
    void endEdit(bool=true);
  public:
    EditBookmarks(QComboBox* url, QWidget* p=0): QDialog(p), _url(url) {
      setupUi(this);
      reject();
    }
    virtual void accept() {
      QStringList urls;
      for (int i(0); i<_bookmarks->count(); ++i)
        urls<<_bookmarks->item(i)->text();
      QString current(_url->currentText());
      _url->clear();
      _url->addItems(urls);
      _url->setEditText(current);
      QDialog::accept();
    }
    virtual void reject() {
      reset();
      QDialog::reject();
    }
  protected Q_SLOTS:
    void on__remove_clicked(bool) {
      QList<QListWidgetItem*> items(_bookmarks->selectedItems());
      qDebug()<<"Delete "<<items.size()<<" items";
      for (QList<QListWidgetItem*>::iterator it(items.begin());
           it!=items.end(); ++it) {
        qDebug()<<"Delete Item: "<<(*it)->text();
        delete _bookmarks->takeItem(_bookmarks->row(*it));
      }
    }
    void on__add_clicked(bool) {
      if (_url->currentText().isEmpty()) return;
      _bookmarks->addItem(_url->currentText());
    }
  protected:
    virtual void hideEvent(QHideEvent *event) {
      QDialog::hideEvent(event);
      endEdit();
    }
    virtual void showEvent(QShowEvent *event) {
      startEdit();
      reset();
      QDialog::showEvent(event);
    }
    void reset() {
      QStringList urls;
      for (int i(0); i<_url->count(); ++i)
        urls<<_url->itemText(i);
      _bookmarks->clear();
      _bookmarks->addItems(urls);
    }
  private:
    QComboBox* _url;
};

#endif
