/***************************************************************************
 *   Copyright (C) 2008 by Jakub Stachowski <qbast@go2.pl>                 *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

#include "./configtableview.h"
#include "forwardingmodel.h"
#include <KConfigDialogManager>

ConfigTableView::ConfigTableView(QWidget* w) : QTableView(w) {
static bool registered=false;
if (!registered) {
  KConfigDialogManager::changedMap()->insert( "ConfigTableView", SIGNAL(modelDataChanged()) );
  registered=true;
}

}


void ConfigTableView::setModel(QAbstractItemModel* m)
{
  //FIXME: disconnect previous model if necessary
  QTableView::setModel(m);
  if (dynamic_cast<VariantSerializable*>(m)) {
    connect(m,SIGNAL(dataChanged ( const QModelIndex &, const QModelIndex &)),
      this,SIGNAL(modelDataChanged()));
    connect(m,SIGNAL(rowsInserted ( const QModelIndex &, int, int)), this,
	SIGNAL(modelDataChanged()));
    connect(m,SIGNAL(rowsRemoved ( const QModelIndex &, int, int)), this,
	SIGNAL(modelDataChanged()));
  }
}
QVariant ConfigTableView::getValue() const {
  VariantSerializable* s=dynamic_cast<VariantSerializable*>(model());
  return s ? s->toVariant() : QVariant();
}

void ConfigTableView::setValue(const QVariant& data) {
  VariantSerializable* s=dynamic_cast<VariantSerializable*>(model());
  if (s) s->fromVariant(data);
}

