/***************************************************************************
                          keditequityentrydlg.cpp  -  description
                             -------------------
    begin                : Sat Mar 6 2004
    copyright            : (C) 2000-2004 by Michael Edwardes
    email                : mte@users.sourceforge.net
                           Javier Campos Morales <javi_c@users.sourceforge.net>
                           Felix Rodriguez <frodriguez@users.sourceforge.net>
                           John C <thetacoturtle@users.sourceforge.net>
                           Thomas Baumgart <ipwizard@users.sourceforge.net>
                           Kevin Tambascio <ktambascio@users.sourceforge.net>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

// ----------------------------------------------------------------------------
// QT Includes
#include <tqtimer.h>

// ----------------------------------------------------------------------------
// KDE Includes

#include <tdelocale.h>
#include <tdeglobal.h>
#include <kpushbutton.h>
#include <kiconloader.h>
#include <kcombobox.h>

// ----------------------------------------------------------------------------
// Project Includes

#include "keditequityentrydlg.h"
#include "kupdatestockpricedlg.h"
#include "../widgets/kmymoneypriceview.h"

KEditEquityEntryDlg::KEditEquityEntryDlg(const MyMoneySecurity& selectedSecurity, TQWidget *parent, const char *name)
  : KEditEquityEntryDecl(parent, name, true)
{
  m_selectedSecurity = selectedSecurity;

  connect(btnOK, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotOKClicked()));
  connect(btnCancel, TQT_SIGNAL(clicked()), this, TQT_SLOT(reject()));
  connect(edtEquityName, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(slotDataChanged()));
  connect(edtMarketSymbol, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(slotDataChanged()));
  connect(edtFraction, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotDataChanged()));
  connect(btnAddEntry, TQT_SIGNAL(clicked()), kpvPriceHistory, TQT_SLOT(slotAddPrice()));
  connect(btnEditEntry, TQT_SIGNAL(clicked()), kpvPriceHistory, TQT_SLOT(slotEditPrice()));
  connect(btnRemoveEntry, TQT_SIGNAL(clicked()), kpvPriceHistory, TQT_SLOT(slotDeletePrice()));
  connect(kpvPriceHistory, TQT_SIGNAL(selectionChanged(TQListViewItem*)), this, TQT_SLOT(slotSelectionChanged(TQListViewItem*)));

  //fill in the fields with what we know.
  edtEquityName->setText(m_selectedSecurity.name());
  edtMarketSymbol->setText(m_selectedSecurity.tradingSymbol());
  edtFraction->setPrecision(0);
  edtFraction->setCalculatorButtonVisible(false);
  edtFraction->loadText(TQString::number(m_selectedSecurity.smallestAccountFraction()));
  cmbInvestmentType->setCurrentItem((int)m_selectedSecurity.securityType());
  // FIXME PRICE
  // kpvPriceHistory->setHistory(m_selectedSecurity.priceHistory());

  // add icons to buttons
  TDEIconLoader *il = TDEGlobal::iconLoader();
  btnOK->setGuiItem(KStdGuiItem::ok());
  btnCancel->setGuiItem(KStdGuiItem::cancel());
  btnRemoveEntry->setGuiItem(KStdGuiItem::remove());
  btnAddEntry->setGuiItem(KStdGuiItem::add());

  KGuiItem editButtenItem( i18n( "&Edit" ),
                    TQIconSet(il->loadIcon("edit", TDEIcon::Small, TDEIcon::SizeSmall)),
                    i18n("Modify the selected entry"),
                    i18n("Change the price information of the selected entry."));
  btnEditEntry->setGuiItem(editButtenItem);

  slotSelectionChanged(0);      // make sure buttons are disabled in the beginning
  slotDataChanged();
  m_changes = false;

  // force a resize to optimize the layout of all widgets
  resize(width()-1, height()-1);
  TQTimer::singleShot(10, this, TQT_SLOT(slotTimerDone()));
}

KEditEquityEntryDlg::~KEditEquityEntryDlg()
{
}

void KEditEquityEntryDlg::slotTimerDone(void)
{
  // the resize operation does the trick to adjust
  // all widgets in the view to the size they should
  // have and show up correctly. Don't ask me, why
  // this is, but it cured the problem (ipwizard).
  resize(width()+1, height()+1);
}

/** No descriptions */
void KEditEquityEntryDlg::slotOKClicked()
{
  if(m_changes /* || kpvPriceHistory->dirty() */)
  {
    m_selectedSecurity.setName(edtEquityName->text());
    m_selectedSecurity.setTradingSymbol(edtMarketSymbol->text());
    m_selectedSecurity.setSmallestAccountFraction(edtFraction->value().abs());
    // FIXME PRICE
    // m_selectedSecurity.setPriceHistory(kpvPriceHistory->history());
  }

  accept();
}

void KEditEquityEntryDlg::slotSelectionChanged(TQListViewItem* item)
{
  btnEditEntry->setEnabled(item != 0);
  btnRemoveEntry->setEnabled(item != 0);
}

void KEditEquityEntryDlg::slotDataChanged(void)
{
  bool okEnabled = true;

  if(!edtFraction->value().isPositive()
  || edtMarketSymbol->text().isEmpty()
  || edtEquityName->text().isEmpty())
    okEnabled = false;

  btnOK->setEnabled(okEnabled);

  m_changes = true;
}

#include "keditequityentrydlg.moc"
