/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you wish to add, delete or rename functions or slots use
** TQt Designer which will update this file, preserving your code. Create an
** init() function in place of a constructor, and a destroy() function in
** place of a destructor.
*****************************************************************************/

void QuickToolConfigWidget::updateSequence(const TQString &sequence)
{
	TQStringList toollist = KileTool::toolList(KGlobal::config(), true);
	toollist.sort();
	m_cbTools->clear();
	m_cbTools->insertStringList(toollist);

	updateConfigs(m_cbTools->currentText());
	connect(m_cbTools, TQT_SIGNAL(activated(const TQString &)), this, TQT_SLOT(updateConfigs(const TQString& )));

	m_sequence=sequence;
	TQStringList list = TQStringList::split(",",sequence);
	TQString tl,cfg;
	m_lstbSeq->clear();
	for ( uint i=0; i < list.count(); ++i)
	{
		KileTool::extract(list[i], tl, cfg);
		if ( !cfg.isNull() )
			m_lstbSeq->insertItem(tl+" ("+cfg+")");
		else
			m_lstbSeq->insertItem(tl);
	}
}

void QuickToolConfigWidget::updateConfigs(const TQString &tool)
{
	m_cbConfigs->clear();
	m_cbConfigs->insertItem(i18n("Not Specified"));
	m_cbConfigs->insertStringList(KileTool::configNames(tool, KGlobal::config()));
}

void QuickToolConfigWidget::down()
{
	int current = m_lstbSeq->currentItem();
	if ( (current != -1) && (current < ( ((int)m_lstbSeq->count())-1) ))
	{
		TQString text = m_lstbSeq->text(current+1);
		m_lstbSeq->changeItem(m_lstbSeq->text(current), current+1);
		m_lstbSeq->changeItem(text, current);
		m_lstbSeq->setCurrentItem(current+1);
		changed();
	}
}

void QuickToolConfigWidget::up()
{
	int current = m_lstbSeq->currentItem();
	if ( (current != -1) && (current > 0) )
	{
		TQString text = m_lstbSeq->text(current-1);
		m_lstbSeq->changeItem(m_lstbSeq->text(current), current-1);
		m_lstbSeq->changeItem(text, current);
		m_lstbSeq->setCurrentItem(current-1);
		changed();
	}
}

void QuickToolConfigWidget::remove()
{
	int current = m_lstbSeq->currentItem();
	if ( current != -1)
	{
		m_lstbSeq->removeItem(current);
		changed();
	}
}

void QuickToolConfigWidget::add()
{
	TQString entry = m_cbTools->currentText();
	if ( m_cbConfigs->currentText() != i18n("Not Specified") )
		entry += " (" + m_cbConfigs->currentText() + ")";
	m_lstbSeq->insertItem(entry);
	changed();
}


void QuickToolConfigWidget::changed()
{
	TQString sequence, tool, cfg;
	for (uint i = 0; i < m_lstbSeq->count(); ++i)
	{
	    KileTool::extract(m_lstbSeq->text(i), tool, cfg);
	    sequence += KileTool::format(tool,cfg)+",";
	}
	if (sequence.endsWith(",") ) sequence = sequence.left(sequence.length()-1);
	m_sequence = sequence;
	emit sequenceChanged(m_sequence);
}
