summaryrefslogtreecommitdiffstats
path: root/knights/tabmanager.cpp
blob: f2d72fc2f33c8ad45cd870be1c2f99162e33d5fd (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
/***************************************************************************
                          tabmanager.cpp  -  description
                             -------------------
    begin                : Sat Sep 14 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 "tabmanager.moc"
#include "resource.h"
#include <kdebug.h>

static QString myConfigGroup( "TabManager" );

TabManager::TabManager( resource *rsrc ) : QObject()
{
	myResource = rsrc;
	appConfig = kapp->config();
}

TabManager::~TabManager()
{
}

///////////////////////////////////////
//
//	TabManager::addTab
//
///////////////////////////////////////
void TabManager::addTab( QWidget *child, const QString &caption )
{
	TabBox *boxPtr;

	if( myList.count() == 0 )
	{
		/* First Tab always generates a new TabBox */
		boxPtr = new TabBox( myResource );
		manageNewBox( boxPtr );
		boxPtr->addTab( child, caption );

		/* Get Geometry from Saved Config */
		appConfig->setGroup( myConfigGroup );
		QSize defaultSize( 420, 360 );
		QSize newSize = appConfig->readSizeEntry( QString( child->className() ), &defaultSize );
		boxPtr->resize( newSize );
		boxPtr->show();
	}
	else
	{
		boxPtr = myList.at(0);
		boxPtr->addTab( child, caption );
	}
}
///////////////////////////////////////
//
//	TabManager::changeCaption
//
///////////////////////////////////////
void TabManager::changeCaption( QWidget *child, const QString &caption )
{
	TabBox *boxPtr = getParentBox( child );

	if( boxPtr != NULL )
	{
		boxPtr->changeCaption( child, caption );
	}
}
///////////////////////////////////////
//
//	TabManager::removeTab
//
///////////////////////////////////////
void TabManager::removeTab( QObject *child )
{
	/* Make sure this is a QWidget we can manipulate */
	if( !child->isWidgetType() )
	{
		kdError() << "TabManager::removeTab: Can not remove non-widget class " << child->className() << endl;
		return;
	}

	if( myList.find( ((TabBox*)child) ) != -1 )
	{
		/* Remove Child Directly */
		myList.remove( ((TabBox*)child) );
		return;
	}

	QString childName( ((QWidget*)child)->className() );
	TabBox *boxPtr = getParentBox( ((QWidget*)child) );

	/* Remove this Child's Parent, the TabPage */
	if( boxPtr != NULL )
	{
		kdDebug() << "Removing child: " << childName << endl;
		boxPtr->removeTab( ((QWidget*)child), TRUE );
		if( boxPtr->count() == 0 )
		{
			myList.remove( boxPtr );
			delete boxPtr;
		}
		return;
	}
	kdWarning() << "Could not find child: " << childName << endl;
}
///////////////////////////////////////
//
//	TabManager::showTab
//
///////////////////////////////////////
void TabManager::showTab( QWidget *child )
{
	TabBox *boxPtr = getParentBox( child );
	if( boxPtr != NULL )
	{
		boxPtr->showTab( child );
	}
}
///////////////////////////////////////
//
//	TabManager::isTab
//
///////////////////////////////////////
bool TabManager::isTab( QWidget *child )
{
	if( getParentBox( child ) == NULL )
	{
		return FALSE;
	}
	return TRUE;
}
///////////////////////////////////////
//
//	TabManager::saveTabGeometry
//
///////////////////////////////////////
void TabManager::saveTabGeometry( const QString &childName, const QSize &childSize )
{
	/* Save This Widget's Size For Later */
	appConfig->setGroup( myConfigGroup );
	appConfig->writeEntry( childName, childSize );
	appConfig->sync();

	kdDebug() << "Saved geometry for Widget: " << childName << endl;
}
///////////////////////////////////////
//
//	TabManager::childEvent
//
///////////////////////////////////////
void TabManager::childEvent( QChildEvent *event )
{
	if( event->inserted() )
	{
		/* New Child */
		if( QString( event->child()->className() ) == "TabBox" )
		{
			manageNewBox( ((TabBox*)event->child()) );
			return;
		}
		addTab( ((QWidget*)event->child()), QString( "Untitled" ) );
	}
}
///////////////////////////////////////
//
//	TabManager::manageNewBox
//
///////////////////////////////////////
void TabManager::manageNewBox( TabBox *newBox )
{
	myList.append( newBox );
	connect( newBox, SIGNAL( destroyed( QObject* ) ), this, SLOT( removeTab( QObject* ) ) );
	connect( newBox, SIGNAL( saveTabGeometry( const QString&, const QSize& ) ), this, SLOT( saveTabGeometry( const QString&, const QSize& ) ) );
	connect( newBox, SIGNAL( newTabBox( TabBox* ) ), this, SLOT( manageNewBox( TabBox* ) ) );
}
///////////////////////////////////////
//
//	TabManager::getParentBox
//
///////////////////////////////////////
TabBox* TabManager::getParentBox( QWidget *child )
{
	TabBox *boxPtr;
	for( unsigned int index=0; index < myList.count(); index++ )
	{
		boxPtr = myList.at(index);
		if( boxPtr->isChild( child ) )
		{
			return boxPtr;
		}
	}
	return NULL;
}