summaryrefslogtreecommitdiffstats
path: root/knights/tabbox.cpp
blob: 8f9ecdf4386cb5606cf46639d14ef2818cd64cec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/***************************************************************************
                          tabbox.cpp  -  description
                             -------------------
    begin                : Fri Sep 13 2002
    copyright            : (C) 2003 by Troy Corbin Jr.
    email                : tcorbin@users.sf.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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "tabbox.moc"
#include "tabpage.h"
#include "resource.h"
#include "accel.h"
#include <klocale.h>
#include <tqstyle.h>
#include <tqtabwidget.h>

TabBox::TabBox( resource *rsrc ) : TQVBox(0,"TabBox",TQt::WDestructiveClose)
{
	myResource = rsrc;
	setMargin( TQApplication::style().defaultFrameWidth() );

	myTabs = new TQTabWidget( this, "myTabs" );
	myTabs->setTabShape( TQTabWidget::Rounded );
	myTabs->setTabPosition( TQTabWidget::Top );

	myAccel = new Accel( this, myResource->myAccel );

	connect( myTabs, TQT_SIGNAL( currentChanged( TQWidget* ) ), this, TQT_SLOT( changeMyCaption( TQWidget* ) ) );
	connect( this, TQT_SIGNAL( focus( const TQChar& ) ), myResource->myAccel, TQT_SIGNAL( focus( const TQChar& ) ) );
}
TabBox::~TabBox()
{
	while( myTabs->count() )
	{
		removeTab( myTabs->page(0), TRUE );
	}
	delete myAccel;
}
///////////////////////////////////////
//
//	TabBox::keyPressEvent
//
///////////////////////////////////////
void TabBox::keyPressEvent( TQKeyEvent *e )
{
	TQChar input;

	if( ( e->state() | TQt::ShiftButton ) == TQt::ShiftButton )
	{
		input = e->text().at(0);
		if( input.isLetterOrNumber() )
		{
			emit focus( input );
			e->accept();
			return;
		}
	}
	e->ignore();
}
///////////////////////////////////////
//
//	TabBox::addTab
//
///////////////////////////////////////
void TabBox::addTab( TQWidget *child, const TQString &caption )
{
	if( TQString( child->className() ) == "TabPage" )
	{
		myTabs->addTab( child, caption );
		myTabs->showPage( child );
		changeMyCaption( child );
		connect( child, TQT_SIGNAL( newParent( TabBox* ) ), this, TQT_SIGNAL( newTabBox( TabBox* ) ) );
		connect( child, TQT_SIGNAL( requestDestruction() ), this, TQT_SLOT( destroyChild() ) );
	}
	else
	{
		TabPage *newPage = new TabPage( (TQWidget*)this, child, myResource );
		newPage->setCaption( caption );

		myTabs->addTab( newPage, caption );
		myTabs->showPage( newPage );
		changeMyCaption( newPage );
		connect( newPage, TQT_SIGNAL( newParent( TabBox* ) ), this, TQT_SIGNAL( newTabBox( TabBox* ) ) );
		connect( newPage, TQT_SIGNAL( requestDestruction() ), this, TQT_SLOT( destroyChild() ) );
	}
}
///////////////////////////////////////
//
//	TabBox::removeTab
//
///////////////////////////////////////
void TabBox::removeTab( TQWidget *child, bool deleteChild )
{
	if( TQString( child->className() ) == "TabPage" )
	{
		emit saveTabGeometry( TQString( ((TabPage*)child)->getChild()->className() ), size() );
		myTabs->removePage( child );
	}
	else
	{
		emit saveTabGeometry( TQString( child->className() ), size() );
		myTabs->removePage( child->parentWidget() );
	}

	/* Delete it if requested */
	if( deleteChild )
	{
		delete child;
	}
}
///////////////////////////////////////
//
//	TabBox::showTab
//
///////////////////////////////////////
void TabBox::showTab( TQWidget *child )
{
	if( isChild( child ) )
		myTabs->showPage( child->parentWidget() );
}
///////////////////////////////////////
//
//	TabBox::destroyChild
//
///////////////////////////////////////
void TabBox::destroyChild( void )
{
	TQObject *child = ((TQObject*)sender());

	/* Be careful... make sure we have a valid child calling us */
	if( child == NULL )
		return;
	if( !child->isWidgetType() )
		return;
	if( !isChild( ((TQWidget*)child) ) )
		return;

	/* Nuke it */
	removeTab( ((TQWidget*)child), TRUE );

	if( count() == 0 )
	{
		/* TabManager will be notified of the delete via TQObject::destroyed(TQObject*) */
		delete this;
	}
}
///////////////////////////////////////
//
//	TabBox::changeMyCaption
//
///////////////////////////////////////
void TabBox::changeMyCaption( TQWidget *child )
{
	setCaption( i18n("%1 - Knights").arg( myTabs->tabLabel( child ) ) );
}
///////////////////////////////////////
//
//	TabBox::count
//
///////////////////////////////////////
int TabBox::count( void )
{
	return myTabs->count();
}
///////////////////////////////////////
//
//	TabBox::isChild
//
///////////////////////////////////////
bool TabBox::isChild( TQWidget *child )
{
	for( int tmp=0; tmp < myTabs->count(); tmp++ )
	{
		if( myTabs->page(tmp) == child )
			return TRUE;
		if( ((TabPage*)myTabs->page(tmp))->getChild() == child )
			return TRUE;
	}
	return FALSE;
}
///////////////////////////////////////
//
//	TabBox::changeCaption
//
///////////////////////////////////////
void TabBox::changeCaption( TQWidget *child, const TQString &caption )
{
	if( TQString( child->className() ) == "TabPage" )
	{
		((TabPage*)child)->setCaption( caption );
		myTabs->setTabLabel( child, caption );
	}
	else if( isChild( child ) )
	{
		((TabPage*)child->parentWidget())->setCaption( caption );
		myTabs->setTabLabel( child->parentWidget(), caption );
	}
	changeMyCaption( myTabs->currentPage() );
}