//
//   File : optw_texticons.cpp
//   Creation date : Fri May 24 2002 00:16:13 CEST by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2002 Szymon Stefanek (pragma at kvirc dot 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 opinion) 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
//

#include "optw_texticons.h"

#include "kvi_iconmanager.h"
#include "kvi_texticonmanager.h"
#include "kvi_locale.h"
#include "kvi_filedialog.h"

#include <tqlayout.h>
#include <tqcursor.h>
#include "kvi_tal_hbox.h"


KviTextIconEditor::KviTextIconEditor(TQWidget * par,KviTextIcon * icon,KviTextIconTableItem* item)
: KviTalHBox(par),m_pTableItem(item)
{
	m_pIconButton=new TQToolButton(this);
	m_pBrowseButton=new TQToolButton(this);
	m_pBrowseButton->setSizePolicy(TQSizePolicy(TQSizePolicy::Maximum,TQSizePolicy::Maximum));
	m_pBrowseButton->setText("...");
	m_pIcon = icon;
	m_pPopup = 0;
	updateIcon();
	connect(m_pIconButton,TQ_SIGNAL(clicked()),this,TQ_SLOT(doPopup()));
	connect(m_pBrowseButton,TQ_SIGNAL(clicked()),this,TQ_SLOT(chooseFromFile()));
}


KviTextIconEditor::~KviTextIconEditor()
{
	
}

void KviTextIconEditor::doPopup()
{
	if(!m_pPopup)
	{
		m_pPopup = new KviTalPopupMenu(this);
		KviIconWidget * iw = new KviIconWidget(m_pPopup);
		connect(iw,TQ_SIGNAL(selected(int)),this,TQ_SLOT(iconSelected(int)));
		m_pPopup->insertItem(iw);
	}
	m_pPopup->popup(TQCursor::pos());
}

void KviTextIconEditor::iconSelected(int id)
{
	m_pIcon->setId(id);
	updateIcon();
}

void KviTextIconEditor::chooseFromFile()
{
	TQString szFile;
	KviFileDialog::askForOpenFileName(szFile,"Choose icon filename",TQString(),"*.png","options");
	if(!szFile.isEmpty())
	{
		if(g_pIconManager->getPixmap(szFile))
		{
			m_pIcon->setFilename(szFile);
//			tqDebug("%s %s %i |%s| %p",__FILE__,__FUNCTION__,__LINE__,m_pIcon->filename().local8Bit().data(),m_pIcon);
			updateIcon();
		}
	}
}

void KviTextIconEditor::updateIcon()
{
	if(m_pIcon)
	{
		TQPixmap* pix=m_pIcon->pixmap();
		if(pix)
			m_pIconButton->setPixmap(*pix);
	}
}

KviTextIconTableItem::KviTextIconTableItem(KviTalTable * t,KviTextIcon * icon)
: KviTalTableItem(t,KviTalTableItem::WhenCurrent,TQString())
{
	if(icon)
		m_pIcon=icon;
	else
		m_pIcon=new KviTextIcon(0);
	TQPixmap* pix=m_pIcon->pixmap();
	if(pix)
		setPixmap(*pix);
}

KviTextIconTableItem::~KviTextIconTableItem()
{
	delete m_pIcon;
}

void KviTextIconTableItem::setId(int id)
{
	m_pIcon->setId(id);
	TQPixmap* pix=m_pIcon->pixmap();
	if(pix)
		setPixmap(*pix);
}

TQWidget * KviTextIconTableItem::createEditor() const
{
	return new KviTextIconEditor(table()->viewport(),m_pIcon,(KviTextIconTableItem*)this);
}


void KviTextIconTableItem::setContentFromEditor(TQWidget * w)
{
	if(w->inherits("KviTextIconEditor"))
	{
		TQPixmap* pix=m_pIcon->pixmap();
		if(pix)
			setPixmap(*pix);
	}
}

KviTextIconsOptionsWidget::KviTextIconsOptionsWidget(TQWidget * parent)
: KviOptionsWidget(parent,"texticons_options_widget")
{
	createLayout(2,2);

	KviPointerHashTableIterator<TQString,KviTextIcon> it(*(g_pTextIconManager->textIconDict()));

	m_pTable = new KviTalTable(g_pTextIconManager->textIconDict()->count(),2,this);

#ifdef COMPILE_INFO_TIPS
	mergeTip(m_pTable->viewport(),__tr2qs_ctx("This table contains the text icon associations.<br>" \
			"KVirc will use them to display the CTRL+I escape sequences and eventually the " \
			"emoticons.","options"));
#endif

	int idx = 0;
	while(KviTextIcon * i = it.current())
	{
		m_pTable->setText(idx,0,it.currentKey());
		m_pTable->setItem(idx,1,new KviTextIconTableItem(m_pTable,new KviTextIcon(i)));
		++idx;
		++it;
	}

	layout()->addMultiCellWidget(m_pTable,0,0,0,1);

	m_pAdd = new TQPushButton(__tr2qs_ctx("Add","options"),this);
	layout()->addWidget(m_pAdd,1,0);
	connect(m_pAdd,TQ_SIGNAL(clicked()),this,TQ_SLOT(addClicked()));

	m_pDel = new TQPushButton(__tr2qs_ctx("Delete","options"),this);
	layout()->addWidget(m_pDel,1,1);
	connect(m_pDel,TQ_SIGNAL(clicked()),this,TQ_SLOT(delClicked()));

	m_pDel->setEnabled(false);

	connect(m_pTable,TQ_SIGNAL(selectionChanged()),this,TQ_SLOT(selectionChanged()));
}

KviTextIconsOptionsWidget::~KviTextIconsOptionsWidget()
{
}

void KviTextIconsOptionsWidget::selectionChanged()
{
	int i = m_pTable->currentRow();
	m_pDel->setEnabled(i >= 0 && i < m_pTable->numRows());
}

void KviTextIconsOptionsWidget::addClicked()
{
	m_pTable->setNumRows(m_pTable->numRows() + 1);
	m_pTable->setText(m_pTable->numRows() - 1,0,__tr2qs_ctx("unnamed","options"));
	m_pTable->setItem(m_pTable->numRows() - 1,1,new KviTextIconTableItem(m_pTable,0));
	m_pDel->setEnabled(true);
}

void KviTextIconsOptionsWidget::delClicked()
{
	int i = m_pTable->currentRow();

	if((i > -1) && (i < m_pTable->numRows()))
	{
		// remove row i
		m_pTable->clearCell(i,0);
		m_pTable->clearCell(i,1);
		m_pTable->clearCell(i,2);

		for(;i < (m_pTable->numRows() - 1);i++)
		{
			m_pTable->swapRows(i,i+1);
		}

		m_pTable->setNumRows(m_pTable->numRows() - 1);
		if(m_pTable->numRows() == 0)m_pDel->setEnabled(false);
	}
}

void KviTextIconsOptionsWidget::commit()
{

	KviOptionsWidget::commit();
	g_pTextIconManager->clear();
	int n = m_pTable->numRows();
	for(int i=0;i < n;i++)
	{
		TQString szVal = m_pTable->text(i,0);
		if(!szVal.isEmpty())
		{
			KviTextIconTableItem * it = (KviTextIconTableItem *)m_pTable->item(i,1);
			if(it)
			{
				g_pTextIconManager->insert(szVal,*(it->icon()));
			}
		}
	}
	g_pTextIconManager->checkDefaultAssociations();
	
	for(int i=0; i<n; i++)
		for (int j=0; j<m_pTable->numCols(); j++)
			if (m_pTable->item(i,j))
				m_pTable->clearCell(i,j);
	
}

#include "optw_texticons.moc"
