author | zecke <zecke> | 2003-08-25 15:00:50 (UTC) |
---|---|---|
committer | zecke <zecke> | 2003-08-25 15:00:50 (UTC) |
commit | bf3c4abb9dff716e098f05852d9a3d8ac8cbcb44 (patch) (unidiff) | |
tree | f74d4765868000d27c45771573032515ac8db8be /core/launcher/launchertab.cpp | |
parent | 292b097e7db25dd231381c5b09307a1fbe81a492 (diff) | |
download | opie-bf3c4abb9dff716e098f05852d9a3d8ac8cbcb44.zip opie-bf3c4abb9dff716e098f05852d9a3d8ac8cbcb44.tar.gz opie-bf3c4abb9dff716e098f05852d9a3d8ac8cbcb44.tar.bz2 |
Initial revision
-rw-r--r-- | core/launcher/launchertab.cpp | 284 |
1 files changed, 284 insertions, 0 deletions
diff --git a/core/launcher/launchertab.cpp b/core/launcher/launchertab.cpp new file mode 100644 index 0000000..10cfd5f --- a/dev/null +++ b/core/launcher/launchertab.cpp | |||
@@ -0,0 +1,284 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of the Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include "launchertab.h" | ||
21 | #include <qapplication.h> | ||
22 | #include <qstyle.h> | ||
23 | #include <qpainter.h> | ||
24 | #include <qbitmap.h> | ||
25 | |||
26 | |||
27 | LauncherTabBar::LauncherTabBar( QWidget *parent, const char *name ) | ||
28 | : QTabBar( parent, name ) | ||
29 | { | ||
30 | setFocusPolicy( NoFocus ); | ||
31 | connect( this, SIGNAL( selected(int) ), this, SLOT( layoutTabs() ) ); | ||
32 | } | ||
33 | |||
34 | LauncherTabBar::~LauncherTabBar() | ||
35 | { | ||
36 | } | ||
37 | |||
38 | void LauncherTabBar::insertTab( LauncherTab *t, int index ) | ||
39 | { | ||
40 | if ( index < 0 ) | ||
41 | items.append( t ); | ||
42 | else | ||
43 | items.insert( (uint)index, t ); | ||
44 | tabs.insert( t->type, t ); | ||
45 | QTabBar::insertTab( t, index ); | ||
46 | } | ||
47 | |||
48 | void LauncherTabBar::removeTab( QTab *tab ) | ||
49 | { | ||
50 | LauncherTab *t = (LauncherTab *)tab; | ||
51 | tabs.remove( t->type ); | ||
52 | items.remove( t ); | ||
53 | QTabBar::removeTab( t ); | ||
54 | } | ||
55 | |||
56 | void LauncherTabBar::prevTab() | ||
57 | { | ||
58 | int n = count(); | ||
59 | int tab = currentTab(); | ||
60 | if ( tab >= 0 ) | ||
61 | setCurrentTab( (tab - 1 + n)%n ); | ||
62 | } | ||
63 | |||
64 | void LauncherTabBar::nextTab() | ||
65 | { | ||
66 | int n = count(); | ||
67 | int tab = currentTab(); | ||
68 | setCurrentTab( (tab + 1)%n ); | ||
69 | } | ||
70 | |||
71 | void LauncherTabBar::showTab( const QString& id ) | ||
72 | { | ||
73 | setCurrentTab( tabs[id] ); | ||
74 | } | ||
75 | |||
76 | void LauncherTabBar::layoutTabs() | ||
77 | { | ||
78 | if ( !count() ) | ||
79 | return; | ||
80 | |||
81 | int available = width()-1; | ||
82 | |||
83 | QFontMetrics fm = fontMetrics(); | ||
84 | int hiddenTabWidth = -12; | ||
85 | LauncherTab *current = currentLauncherTab(); | ||
86 | int hframe, vframe, overlap; | ||
87 | style().tabbarMetrics( this, hframe, vframe, overlap ); | ||
88 | int x = 0; | ||
89 | QRect r; | ||
90 | LauncherTab *t; | ||
91 | QListIterator< LauncherTab > it( items ); | ||
92 | int required = 0; | ||
93 | int eventabwidth = (width()-1)/count(); | ||
94 | enum Mode { HideBackText, Pack, Even } mode=Even; | ||
95 | for (it.toFirst(); it.current(); ++it ) { | ||
96 | t = it.current(); | ||
97 | if ( !t ) | ||
98 | continue; | ||
99 | int iw = fm.width( t->text() ) + hframe - overlap; | ||
100 | if ( t != current ) { | ||
101 | available -= hiddenTabWidth + hframe - overlap; | ||
102 | if ( t->iconSet() != 0 ) | ||
103 | available -= t->iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).width(); | ||
104 | } | ||
105 | if ( t->iconSet() != 0 ) | ||
106 | iw += t->iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).width(); | ||
107 | required += iw; | ||
108 | // As space gets tight, packed looks better than even. "10" must be at least 0. | ||
109 | if ( iw >= eventabwidth-10 ) | ||
110 | mode = Pack; | ||
111 | } | ||
112 | if ( mode == Pack && required > width()-1 ) | ||
113 | mode = HideBackText; | ||
114 | for ( it.toFirst(); it.current(); ++it ) { | ||
115 | t = it.current(); | ||
116 | if ( !t ) | ||
117 | continue; | ||
118 | if ( mode != HideBackText ) { | ||
119 | int w = fm.width( t->text() ); | ||
120 | int ih = 0; | ||
121 | if ( t->iconSet() != 0 ) { | ||
122 | w += t->iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).width(); | ||
123 | ih = t->iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).height(); | ||
124 | } | ||
125 | int h = QMAX( fm.height(), ih ); | ||
126 | h = QMAX( h, QApplication::globalStrut().height() ); | ||
127 | |||
128 | h += vframe; | ||
129 | w += hframe; | ||
130 | |||
131 | QRect totr(x, 0, | ||
132 | mode == Even ? eventabwidth : w * (width()-1)/required, h); | ||
133 | t->setRect(totr); | ||
134 | x += totr.width() - overlap; | ||
135 | r = r.unite(totr); | ||
136 | } else if ( t != current ) { | ||
137 | int w = hiddenTabWidth; | ||
138 | int ih = 0; | ||
139 | if ( t->iconSet() != 0 ) { | ||
140 | w += t->iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).width(); | ||
141 | ih = t->iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).height(); | ||
142 | } | ||
143 | int h = QMAX( fm.height(), ih ); | ||
144 | h = QMAX( h, QApplication::globalStrut().height() ); | ||
145 | |||
146 | h += vframe; | ||
147 | w += hframe; | ||
148 | |||
149 | t->setRect( QRect(x, 0, w, h) ); | ||
150 | x += t->rect().width() - overlap; | ||
151 | r = r.unite( t->rect() ); | ||
152 | } else { | ||
153 | int ih = 0; | ||
154 | if ( t->iconSet() != 0 ) { | ||
155 | ih = t->iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).height(); | ||
156 | } | ||
157 | int h = QMAX( fm.height(), ih ); | ||
158 | h = QMAX( h, QApplication::globalStrut().height() ); | ||
159 | |||
160 | h += vframe; | ||
161 | |||
162 | t->setRect( QRect(x, 0, available, h) ); | ||
163 | x += t->rect().width() - overlap; | ||
164 | r = r.unite( t->rect() ); | ||
165 | } | ||
166 | } | ||
167 | |||
168 | t = it.toLast(); | ||
169 | if (t) { | ||
170 | QRect rr = t->rect(); | ||
171 | rr.setRight(width()-1); | ||
172 | t->setRect( rr ); | ||
173 | } | ||
174 | |||
175 | for (it.toFirst(); it.current(); ++it ) { | ||
176 | t = it.current(); | ||
177 | QRect tr = t->rect(); | ||
178 | tr.setHeight( r.height() ); | ||
179 | t->setRect( tr ); | ||
180 | } | ||
181 | |||
182 | update(); | ||
183 | } | ||
184 | |||
185 | void LauncherTabBar::paint( QPainter * p, QTab * t, bool selected ) const | ||
186 | { | ||
187 | LauncherTabBar *that = (LauncherTabBar *) this; | ||
188 | LauncherTab *ct = (LauncherTab *)t; | ||
189 | QPalette pal = palette(); | ||
190 | bool setPal = FALSE; | ||
191 | if ( ct->bgColor.isValid() ) { | ||
192 | pal.setColor( QPalette::Active, QColorGroup::Background, ct->bgColor ); | ||
193 | pal.setColor( QPalette::Active, QColorGroup::Button, ct->bgColor ); | ||
194 | pal.setColor( QPalette::Inactive, QColorGroup::Background, ct->bgColor ); | ||
195 | pal.setColor( QPalette::Inactive, QColorGroup::Button, ct->bgColor ); | ||
196 | that->setUpdatesEnabled( FALSE ); | ||
197 | that->setPalette( pal ); | ||
198 | setPal = TRUE; | ||
199 | } | ||
200 | #if QT_VERSION >= 0x030000 | ||
201 | QStyle::SFlags flags = QStyle::Style_Default; | ||
202 | if ( selected ) | ||
203 | flags |= QStyle::Style_Selected; | ||
204 | style().drawControl( QStyle::CE_TabBarTab, p, this, t->rect(), | ||
205 | colorGroup(), flags, QStyleOption(t) ); | ||
206 | #else | ||
207 | style().drawTab( p, this, t, selected ); | ||
208 | #endif | ||
209 | |||
210 | QRect r( t->rect() ); | ||
211 | QFont f( font() ); | ||
212 | if ( selected ) | ||
213 | f.setBold( TRUE ); | ||
214 | p->setFont( f ); | ||
215 | |||
216 | if ( ct->fgColor.isValid() ) { | ||
217 | pal.setColor( QPalette::Active, QColorGroup::Foreground, ct->fgColor ); | ||
218 | pal.setColor( QPalette::Inactive, QColorGroup::Foreground, ct->fgColor ); | ||
219 | that->setUpdatesEnabled( FALSE ); | ||
220 | that->setPalette( pal ); | ||
221 | setPal = TRUE; | ||
222 | } | ||
223 | int iw = 0; | ||
224 | int ih = 0; | ||
225 | if ( t->iconSet() != 0 ) { | ||
226 | iw = t->iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).width() + 2; | ||
227 | ih = t->iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).height(); | ||
228 | } | ||
229 | int w = iw + p->fontMetrics().width( t->text() ) + 4; | ||
230 | int h = QMAX(p->fontMetrics().height() + 4, ih ); | ||
231 | paintLabel( p, QRect( r.left() + (r.width()-w)/2 - 3, | ||
232 | r.top() + (r.height()-h)/2, w, h ), t, | ||
233 | #if QT_VERSION >= 0x030000 | ||
234 | t->identifier() == keyboardFocusTab() | ||
235 | #else | ||
236 | t->identitifer() == keyboardFocusTab() | ||
237 | #endif | ||
238 | ); | ||
239 | if ( setPal ) { | ||
240 | that->unsetPalette(); | ||
241 | that->setUpdatesEnabled( TRUE ); | ||
242 | } | ||
243 | } | ||
244 | |||
245 | void LauncherTabBar::paintLabel( QPainter* p, const QRect&, | ||
246 | QTab* t, bool has_focus ) const | ||
247 | { | ||
248 | QRect r = t->rect(); | ||
249 | // if ( t->id != currentTab() ) | ||
250 | //r.moveBy( 1, 1 ); | ||
251 | // | ||
252 | if ( t->iconSet() ) { | ||
253 | // the tab has an iconset, draw it in the right mode | ||
254 | QIconSet::Mode mode = (t->isEnabled() && isEnabled()) ? QIconSet::Normal : QIconSet::Disabled; | ||
255 | if ( mode == QIconSet::Normal && has_focus ) | ||
256 | mode = QIconSet::Active; | ||
257 | QPixmap pixmap = t->iconSet()->pixmap( QIconSet::Small, mode ); | ||
258 | int pixw = pixmap.width(); | ||
259 | int pixh = pixmap.height(); | ||
260 | p->drawPixmap( r.left() + 6, r.center().y() - pixh / 2 + 1, pixmap ); | ||
261 | r.setLeft( r.left() + pixw + 5 ); | ||
262 | } | ||
263 | |||
264 | QRect tr = r; | ||
265 | |||
266 | if ( r.width() < 20 ) | ||
267 | return; | ||
268 | |||
269 | if ( t->isEnabled() && isEnabled() ) { | ||
270 | #if defined(_WS_WIN32_) | ||
271 | if ( colorGroup().brush( QColorGroup::Button ) == colorGroup().brush( QColorGroup::Background ) ) | ||
272 | p->setPen( colorGroup().buttonText() ); | ||
273 | else | ||
274 | p->setPen( colorGroup().foreground() ); | ||
275 | #else | ||
276 | p->setPen( colorGroup().foreground() ); | ||
277 | #endif | ||
278 | p->drawText( tr, AlignCenter | AlignVCenter | ShowPrefix, t->text() ); | ||
279 | } else { | ||
280 | p->setPen( palette().disabled().foreground() ); | ||
281 | p->drawText( tr, AlignCenter | AlignVCenter | ShowPrefix, t->text() ); | ||
282 | } | ||
283 | } | ||
284 | |||