/***************************************************************************
 *   Copyright (C) 2005 by yunfan                                          *
 *   yunfan_zg@163.com                                                     *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "evachatuibase.h"
#include "evatextedit.h"
#include "evachatview.h"

#include <qvariant.h>
#include <qpushbutton.h>
#include <qtoolbutton.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <ktextbrowser.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qimage.h>
#include <qpixmap.h>
#include <klocale.h>

#include <kapplication.h>

EvaChatUIBase::EvaChatUIBase( QWidget* parent, const char* name, WFlags fl )
    : QWidget( parent, name, fl )
{
	if ( !name )
		setName( "EvaChatUIBase" );
	EvaChatUIBaseLayout = new QGridLayout( this, 1, 1, 0, 0, "EvaChatUIBaseLayout"); 
	
	layout5 = new QVBoxLayout( 0, 0, 3, "layout5"); 
	
	layout4 = new QHBoxLayout( 0, 3, 6, "layout4"); 
	
	tbBuddy = new QToolButton( this, "tbBuddy" );
	tbBuddy->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, tbBuddy->sizePolicy().hasHeightForWidth() ) );
	tbBuddy->setMinimumSize( QSize( 20, 24 ) );
	tbBuddy->setMaximumSize( QSize( 300, 24 ) );
	tbBuddy->setUsesTextLabel( TRUE );
	tbBuddy->setAutoRaise( TRUE );
	tbBuddy->setTextPosition( QToolButton::BesideIcon );
	layout4->addWidget( tbBuddy );
	
	lblIP = new QLabel( this, "lblIP" );
	lblIP->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, lblIP->sizePolicy().hasHeightForWidth() ) );
	lblIP->setMinimumSize( QSize( 15, 0 ) );
	lblIP->setMaximumSize( QSize( 15, 32767 ) );
	layout4->addWidget( lblIP );
	
	leIP = new QLineEdit( this, "leIP" );
	leIP->setPaletteBackgroundColor( QColor( 238, 238, 230 ) );
	leIP->setFrameShape( QLineEdit::NoFrame );
	leIP->setReadOnly( TRUE );
	layout4->addWidget( leIP );
	layout5->addLayout( layout4 );
	
// 	ktbDisplay = new KTextBrowser( this, "ktbDisplay", true );
// 	ktbDisplay->setFocusPolicy( QTextEdit::NoFocus );
// 	ktbDisplay->setFrameShadow( QTextEdit::Plain );
// 	ktbDisplay->setHScrollBarMode( QTextEdit::AlwaysOff );
// 	ktbDisplay->setWordWrap( QTextEdit::WidgetWidth );
// 	ktbDisplay->setWrapPolicy( QTextEdit::AtWordOrDocumentBoundary );
// 	ktbDisplay->setReadOnly( TRUE );
// 	ktbDisplay->setMinimumSize( QSize( 0, 170 ) );
// 	ktbDisplay->setHScrollBarMode( KTextBrowser::AlwaysOff );
// 	layout5->addWidget( ktbDisplay );

	chatDisplay = new EvaChatView(this, "chatDisplay");
	chatDisplay->view()->setMinimumSize(QSize( 0, 170 ) );
	//chatDisplay->view()->setHScrollBarMode(QScrollView::AlwaysOff);
	layout5->addWidget( chatDisplay->view() );
	
	
	layout3 = new QHBoxLayout( 0, 0, 0, "layout3"); 
	
	tbSmiley = new QToolButton( this, "tbSmiley" );
	tbSmiley->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, tbSmiley->sizePolicy().hasHeightForWidth() ) );
	tbSmiley->setMinimumSize( QSize( 24, 24 ) );
	tbSmiley->setMaximumSize( QSize( 24, 24 ) );
	tbSmiley->setAutoRaise( TRUE );
	layout3->addWidget( tbSmiley );
	
	tbFont = new QToolButton( this, "tbFont" );
	tbFont->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, tbFont->sizePolicy().hasHeightForWidth() ) );
	tbFont->setMinimumSize( QSize( 24, 24 ) );
	tbFont->setMaximumSize( QSize( 24, 24 ) );
	tbFont->setAutoRaise( TRUE );
	layout3->addWidget( tbFont );
	spcTootBtn1 = new QSpacerItem( 20, 20, QSizePolicy::Fixed, QSizePolicy::Minimum );
	layout3->addItem( spcTootBtn1 );
	
	tbB = new QToolButton( this, "tbB" );
	tbB->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, tbB->sizePolicy().hasHeightForWidth() ) );
	tbB->setMinimumSize( QSize( 24, 24 ) );
	tbB->setMaximumSize( QSize( 24, 24 ) );
	tbB->setToggleButton( TRUE );
	tbB->setAutoRaise( TRUE );
	layout3->addWidget( tbB );
	
	tbU = new QToolButton( this, "tbU" );
	tbU->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, tbU->sizePolicy().hasHeightForWidth() ) );
	tbU->setMinimumSize( QSize( 24, 24 ) );
	tbU->setMaximumSize( QSize( 24, 24 ) );
	tbU->setToggleButton( TRUE );
	tbU->setUsesTextLabel( FALSE );
	tbU->setAutoRaise( TRUE );
	layout3->addWidget( tbU );
	
	tbI = new QToolButton( this, "tbI" );
	tbI->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, tbI->sizePolicy().hasHeightForWidth() ) );
	tbI->setMinimumSize( QSize( 24, 24 ) );
	tbI->setMaximumSize( QSize( 24, 24 ) );
	tbI->setToggleButton( TRUE );
	tbI->setAutoRaise( TRUE );
	layout3->addWidget( tbI );
	spcToolBtn2 = new QSpacerItem( 20, 20, QSizePolicy::Fixed, QSizePolicy::Minimum );
	layout3->addItem( spcToolBtn2 );
	
	tbQuickReply = new QToolButton( this, "tbQuickReply" );
	tbQuickReply->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, tbQuickReply->sizePolicy().hasHeightForWidth() ) );
	tbQuickReply->setMinimumSize( QSize( 24, 24 ) );
	tbQuickReply->setMaximumSize( QSize( 24, 24 ) );
	tbQuickReply->setAutoRaise( TRUE );
	layout3->addWidget( tbQuickReply );
	spcToolBtn3 = new QSpacerItem( 71, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
	layout3->addItem( spcToolBtn3 );
	layout5->addLayout( layout3 );
	
	kteInput = new EvaTextEdit( this, "kteInput" );
	kteInput->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)7, (QSizePolicy::SizeType)1, 0, 0, kteInput->sizePolicy().hasHeightForWidth() ) );
	kteInput->setMinimumSize( QSize( 0, 40 ) );
	kteInput->setMaximumSize( QSize( 32767, 150 ) );
	kteInput->setHScrollBarMode( KTextEdit::AlwaysOff );
	kteInput->setTextFormat(Qt::RichText);
	layout5->addWidget( kteInput );
	
	layout2 = new QHBoxLayout( 0, 4, 6, "layout2"); 
	
	pbHistory = new QPushButton( this, "pbHistory" );
	layout2->addWidget( pbHistory );
	spcBottom = new QSpacerItem( 60, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
	layout2->addItem( spcBottom );
	
	pbClose = new QPushButton( this, "pbClose" );
	layout2->addWidget( pbClose );
	
	pbSend = new QPushButton( this, "pbSend" );
	layout2->addWidget( pbSend );
	
	pbSendKey = new QPushButton( this, "pbSendKey" );
	pbSendKey->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, pbSendKey->sizePolicy().hasHeightForWidth() ) );
	pbSendKey->setMaximumSize( QSize( 15, 32767 ) );
	layout2->addWidget( pbSendKey );
	layout5->addLayout( layout2 );
	
	EvaChatUIBaseLayout->addLayout( layout5, 0, 0 );
	
	layout19 = new QVBoxLayout( 0, 6, 6, "layout19"); 
	spacer12 = new QSpacerItem( 20, 3, QSizePolicy::Minimum, QSizePolicy::Expanding );
	layout19->addItem( spacer12 );
	
	lblBuddyShow = new QLabel( this, "lblBuddyShow" );
	lblBuddyShow->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)5, 0, 0, lblBuddyShow->sizePolicy().hasHeightForWidth() ) );
	layout19->addWidget( lblBuddyShow );
	
	tbShowBuddy = new QToolButton( this, "tbShowBuddy" );
	tbShowBuddy->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5, (QSizePolicy::SizeType)5, 0, 0, tbShowBuddy->sizePolicy().hasHeightForWidth() ) );
	tbShowBuddy->setMinimumSize( QSize( 90, 120 ) );
	tbShowBuddy->setMaximumSize( QSize( 90, 120 ) );
	tbShowBuddy->setAutoRepeat( false );
	tbShowBuddy->setUsesBigPixmap( true );
	layout19->addWidget( tbShowBuddy );
	
	lblMeShow = new QLabel( this, "lblMeShow" );
	lblMeShow->setAlignment( int( QLabel::AlignVCenter ) );
	layout19->addWidget( lblMeShow );
	
	tbShowMe = new QToolButton( this, "tbShowMe" );
	tbShowMe->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5, (QSizePolicy::SizeType)5, 0, 0, tbShowMe->sizePolicy().hasHeightForWidth() ) );
	tbShowMe->setMinimumSize( QSize( 90, 120 ) );
	tbShowMe->setMaximumSize( QSize( 90, 120 ) );
	tbShowMe->setUsesBigPixmap( true );
	tbShowMe->setAutoRaise( false );
	layout19->addWidget( tbShowMe );
	spacer10 = new QSpacerItem( 20, 3, QSizePolicy::Minimum, QSizePolicy::Expanding );
	layout19->addItem( spacer10 );
	
	EvaChatUIBaseLayout->addLayout( layout19, 0, 1 );
	languageChange();
	resize( QSize(471, 367).expandedTo(minimumSizeHint()) );
	clearWState( WState_Polished );
	QRect scr = KApplication::desktop()->screenGeometry();
	move(scr.center()-rect().center());
}

/*
 *  Destroys the object and frees any allocated resources
 */
EvaChatUIBase::~EvaChatUIBase()
{
    // no need to delete child widgets, Qt does it all for us
}

/*
 *  Sets the strings of the subwidgets using the current
 *  language.
 */
void EvaChatUIBase::languageChange()
{
	setCaption( i18n( "Chat" ) );
	tbBuddy->setText( i18n( "..." ) );
	QToolTip::add( tbBuddy, i18n( "click for details" ) );
	lblIP->setText( i18n( "IP:" ) );
	tbSmiley->setText( i18n( "..." ) );
	tbSmiley->setAccel( QKeySequence( tr( "Alt+F" ) ) );
	QToolTip::add( tbSmiley, i18n( "add a smiley icon" ) );
	tbFont->setText( i18n( "..." ) );
	tbFont->setAccel( QKeySequence( tr( "Alt+S" ) ) );
	QToolTip::add( tbFont, i18n( "choose font size and color" ) );
	tbB->setText( i18n( "B" ) );
	tbB->setAccel( QKeySequence( tr( "Alt+B" ) ) );
	QToolTip::add( tbB, i18n( "bold" ) );
	tbU->setText( i18n( "U" ) );
	tbU->setAccel( QKeySequence( tr( "Alt+U" ) ) );
	tbU->setTextLabel( QString::null );
	QToolTip::add( tbU, i18n( "underline" ) );
	tbI->setText( i18n( "I" ) );
	tbI->setAccel( QKeySequence( tr( "Alt+I" ) ) );
	QToolTip::add( tbI, i18n( "italic" ) );
	tbQuickReply->setText( i18n( "..." ) );
	tbQuickReply->setAccel( QKeySequence( tr( "Alt+R" ) ) );
	QToolTip::add( tbQuickReply, i18n( "select an quick-reply message" ) );
	pbHistory->setText( i18n( "&History" ) );
	pbHistory->setAccel( QKeySequence( tr( "Alt+H" ) ) );
	QToolTip::add( pbHistory, i18n( "view IM history(Alt+H)" ) );
	pbClose->setText( i18n( "Close" ) );
	pbClose->setAccel( QKeySequence( tr( "Alt+X" ) ) );
	QToolTip::add( pbClose, i18n( "close this window(Alt+X)" ) );
	pbSend->setText( i18n( "Send" ) );
	pbSend->setAccel( QKeySequence( QString::null ) );
	QToolTip::add( pbSend, i18n( "send message" ) );
	pbSendKey->setText( QString::null );
	pbSendKey->setAccel( QKeySequence( QString::null ) );
	QToolTip::add( pbSendKey, i18n( "change send key" ) );
	lblBuddyShow->setText( i18n( "Buddy" ) );
	tbShowBuddy->setText( i18n( "..." ) );
	QToolTip::add( tbShowBuddy, i18n( "Click to update" ) );
	lblMeShow->setText( i18n( "Me" ) );
	tbShowMe->setText( i18n( "..." ) );
	tbShowMe->setAccel( QKeySequence( QString::null ) );
	QToolTip::add( tbShowMe, i18n( "Click to update" ) );
}

