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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
/***************************************************************************
* Copyright (C) 2003 by *
* Unai Garro ([email protected]) *
* Cyril Bosselut ([email protected]) *
* *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as publishfed by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "kremenu.h"
#include <tqbitmap.h>
#include <tqcursor.h>
#include <tqfont.h>
#include <tqimage.h>
#include <tqobjectlist.h>
#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqsignalmapper.h>
#include <tdeapplication.h>
#include <kcursor.h>
#include <kdebug.h>
#include <tdeglobalsettings.h>
#include <kiconloader.h>
#include <kimageeffect.h>
#include <tdelocale.h>
#include <kpixmap.h>
#include <kpixmapeffect.h>
KreMenu::KreMenu( TQWidget *parent, const char *name ) :
TQWidget( parent, name, TQt::WNoAutoErase )
{
Menu newMenu;
mainMenuId = menus.append( newMenu );
currentMenuId = mainMenuId;
m_currentMenu = &( *currentMenuId );
setMouseTracking( true );
setFocusPolicy( TQWidget::StrongFocus );
setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Preferred );
}
KreMenu::~KreMenu()
{}
void KreMenu::childEvent ( TQChildEvent *e )
{
if ( e->type() == TQChildEvent::ChildInserted ) {
TQObject * child = e->child();
if ( child->inherits( "KreMenuButton" ) ) {
KreMenuButton * button = ( KreMenuButton* ) child;
Menu *buttonMenu = &( *( button->menuId ) );
if ( !( buttonMenu->activeButton ) ) // Highlight the button if it's the first in the menu
{
button->setActive( true );
buttonMenu->activeButton = button;
}
buttonMenu->addButton( button );
if ( buttonMenu != m_currentMenu )
button->hide();
else
button->show();
connect ( button, TQ_SIGNAL( clicked( KreMenuButton* ) ), this, TQ_SLOT( collectClicks( KreMenuButton* ) ) );
}
}
else if ( e->type() == TQChildEvent::ChildRemoved ) {
TQObject * child = e->child();
KreMenuButton *button = ( KreMenuButton* ) child;
if ( m_currentMenu->positionList.find( button ) != m_currentMenu->positionList.end() ) // Ensure that what was removed was a button
{
// Remove the button from the list first
int pos = m_currentMenu->positionList[ button ]; // FIXME: this works only if the button is removed from the main menu
m_currentMenu->widgetList.remove( pos ); // FIXME: this works only if the button is removed from the main menu
m_currentMenu->positionList.remove( button ); // FIXME: this works only if the button is removed from the main menu
// Now recalculate the position of the next button
( m_currentMenu->widgetNumber ) --; // FIXME: this works only if the button is removed from the main menu
KreMenuButton *lastButton = m_currentMenu->widgetList[ ( m_currentMenu->widgetNumber ) - 1 ];
if ( lastButton )
m_currentMenu->childPos = lastButton->y() + lastButton->height();
m_currentMenu->activeButton = 0;
setMinimumWidth( minimumSizeHint().width() + 10 ); //update the minimum width
}
}
TQWidget::childEvent( e );
}
void KreMenu::collectClicks( KreMenuButton *w )
{
setFocus();
highlightButton( w );
// Emit signal indicating button activation with button ID
KrePanel panel = w->getPanel();
emit clicked( panel );
}
MenuId KreMenu::createSubMenu( const TQString &title, const TQString &icon )
{
// Create the new menu
Menu newMenu;
MenuId id = menus.append( newMenu );
// Add a button to the main menu for this submenu
TDEIconLoader il;
KreMenuButton *newMenuButton = new KreMenuButton( this );
newMenuButton->subMenuId = id;
newMenuButton->setTitle( title );
newMenuButton->setIconSet( il.loadIconSet( icon, TDEIcon::Panel ) );
// Add a button to the submenu to go back to the top menu
KreMenuButton *newSubMenuButton = new KreMenuButton( this );
newSubMenuButton->menuId = id;
newSubMenuButton->subMenuId = mainMenuId;
newSubMenuButton->setTitle( i18n( "Up" ) );
newSubMenuButton->setIconSet( il.loadIconSet( "1uparrow", TDEIcon::Panel ) );
connect( newMenuButton, TQ_SIGNAL( clicked( MenuId ) ), this, TQ_SLOT( showMenu( MenuId ) ) );
connect( newSubMenuButton, TQ_SIGNAL( clicked( MenuId ) ), this, TQ_SLOT( showMenu( MenuId ) ) );
return id;
}
void KreMenu::highlightButton( KreMenuButton *button )
{
MenuId buttonMenuId = button->menuId;
Menu *buttonMenu = &( *buttonMenuId );
//Deactivate the old button
if ( buttonMenu->activeButton ) {
buttonMenu->activeButton->setActive( false );
buttonMenu->activeButton->update();
}
//Activate the new button
button->setActive( true );
button->update();
buttonMenu->activeButton = button;
}
void KreMenu::keyPressEvent( TQKeyEvent *e )
{
switch ( e->key() ) {
case TQt::Key_Up: {
int current_index = m_currentMenu->positionList[ m_currentMenu->activeButton ];
if ( current_index > 0 ) {
highlightButton( m_currentMenu->widgetList[ current_index - 1 ] );
//simulate a mouse click
TQMouseEvent me( TQEvent::MouseButtonPress, TQPoint(), 0, 0 );
TDEApplication::sendEvent( m_currentMenu->activeButton, &me );
e->accept();
}
break;
}
case TQt::Key_Down: {
int current_index = m_currentMenu->positionList[ m_currentMenu->activeButton ];
if ( current_index < int( m_currentMenu->positionList.count() ) - 1 ) {
highlightButton( m_currentMenu->widgetList[ current_index + 1 ] );
//simulate a mouse click
TQMouseEvent me( TQEvent::MouseButtonPress, TQPoint(), 0, 0 );
TDEApplication::sendEvent( m_currentMenu->activeButton, &me );
e->accept();
}
break;
}
case TQt::Key_Enter:
case TQt::Key_Return:
case TQt::Key_Space: {
//simulate a mouse click
TQMouseEvent me( TQEvent::MouseButtonPress, TQPoint(), 0, 0 );
TDEApplication::sendEvent( m_currentMenu->activeButton, &me );
e->accept();
break;
}
default:
e->ignore();
}
}
TQSize KreMenu::sizeHint() const
{
return minimumSizeHint();
}
//the minimum size hint will be the minimum size hint of the largest child
TQSize KreMenu::minimumSizeHint() const
{
int width = 30;
TQObjectList *childElements = queryList( 0, 0, false, false ); //only first-generation children (not recursive)
TQObjectListIterator it( *childElements );
TQObject *obj;
while ( ( obj = it.current() ) != 0 ) {
++it;
if ( obj->isWidgetType() ) {
int obj_width_hint = ( ( TQWidget* ) obj ) ->minimumSizeHint().width();
if ( obj_width_hint > width )
width = obj_width_hint;
}
}
return TQSize( width, 150 );
}
void KreMenu::paintEvent( TQPaintEvent * )
{
// Make sure the size is bigger than the minimum necessary
//if (minimumWidth() <45) setMinimumWidth(45); // FIXME: can somehow setMinimumWidth be restricted? This may not be the best place to do this
// Get gradient colors
TQColor c = colorGroup().button();
TQColor c1 = c.dark( 130 );
TQColor c2 = c.light( 120 );
// Draw the gradient
KPixmap kpm;
kpm.resize( size() );
KPixmapEffect::unbalancedGradient ( kpm, c2, c1, KPixmapEffect::HorizontalGradient, -150, -150 );
// Draw the handle
TQPainter painter( &kpm );
painter.setPen( c1 );
painter.drawLine( width() - 5, 20, width() - 5, height() - 20 );
painter.end();
//Set the border transparent using a mask
TQBitmap mask( kpm.size() );
mask.fill( TQt::color0 );
painter.begin( &mask );
painter.setPen( TQt::color1 );
painter.setBrush( TQt::color1 );
painter.drawRoundRect( 0, 0, width(), height(), ( int ) ( 2.0 / width() * height() ), 2 );
painter.end();
kpm.setMask( mask );
//Draw the border line
painter.begin( &kpm );
painter.setPen( c1 );
painter.drawRoundRect( 0, 0, width(), height(), ( int ) ( 2.0 / width() * height() ), 2 );
//Draw the top line bordering with the first button
if ( m_currentMenu->activeButton ) // draw only if there's a button
{
int w = m_currentMenu->activeButton->width();
painter.setPen( c1 );
painter.drawLine( w / 5, 8, w - 1, 8 );
painter.setPen( c2 );
painter.drawLine( w / 5, 9, w - 1, 9 );
}
painter.end();
// Copy the pixmap to the widget
bitBlt( this, 0, 0, &kpm );
}
void KreMenu::resizeEvent( TQResizeEvent* e )
{
emit resized( ( e->size() ).width(), ( e->size() ).height() );
}
void KreMenu::showMenu( MenuId id )
{
// Hide the buttons in the current menu
// and show the ones in the new menu
TQObjectList * childElements = queryList();
TQObjectListIterator it( *childElements );
TQObject *obj;
while ( ( obj = it.current() ) != 0 ) {
++it;
if ( obj->inherits( "KreMenuButton" ) ) {
KreMenuButton * button = ( KreMenuButton* ) obj;
if ( button->menuId == currentMenuId )
button->hide();
else if ( button->menuId == id )
button->show();
}
}
// Set the new menu as current
currentMenuId = id;
m_currentMenu = &( *( currentMenuId ) );
}
KreMenuButton::KreMenuButton( KreMenu *parent, KrePanel _panel, MenuId id, const char *name ) :
TQWidget( parent, name, TQt::WNoAutoErase ),
panel( _panel )
{
icon = 0;
highlighted = false;
text = TQString::null;
if ( id == 0 )
menuId = parent->mainMenu();
else
menuId = id;
subMenuId = 0; // By default it's not a submenu button
resize( parent->size().width(), 55 );
connect ( parent, TQ_SIGNAL( resized( int, int ) ), this, TQ_SLOT( rescale() ) );
connect( this, TQ_SIGNAL( clicked() ), this, TQ_SLOT( forwardClicks() ) );
setCursor( TQCursor( KCursor::handCursor() ) );
}
KreMenuButton::~KreMenuButton()
{
delete icon;
}
void KreMenuButton::setTitle( const TQString &s )
{
text = s;
#if 0 //this causes problems for the button to go back to editing a recipe
//adjust text to two lines if needed
if ( fontMetrics().width( text ) > 110 ) {
text.replace( ' ', "\n" );
}
#endif
setMinimumWidth( minimumSizeHint().width() );
if ( parentWidget() ->minimumWidth() < minimumSizeHint().width() )
parentWidget() ->setMinimumWidth( minimumSizeHint().width() + 10 );
update();
}
void KreMenuButton::mousePressEvent ( TQMouseEvent * )
{
emit clicked();
}
void KreMenuButton::rescale()
{
resize( parentWidget() ->width() - 10, height() );
}
TQSize KreMenuButton::sizeHint() const
{
if ( parentWidget() )
return ( TQSize( parentWidget() ->size().width() - 10, 40 ) );
else
return TQSize( 100, 30 );
}
TQSize KreMenuButton::minimumSizeHint() const
{
int text_width = TQMAX( fontMetrics().width( text.section( '\n', 0, 0 ) ), fontMetrics().width( text.section( '\n', 1, 1 ) ) );
if ( icon )
return TQSize( 40 + icon->width() + text_width, 30 );
else
return TQSize( 40 + text_width, 30 );
}
void KreMenuButton::paintEvent( TQPaintEvent * )
{
if ( !isShown() )
return ;
// First draw the gradient
int darken = 130, lighten = 120;
TQColor c1, c2, c1h, c2h; //non-highlighted and highlighted versions
// Set the gradient colors
c1 = colorGroup().button().dark( darken );
c2 = colorGroup().button().light( lighten );
if ( highlighted ) {
darken -= 10;
lighten += 10;
c1h = TDEGlobalSettings::highlightColor().dark( darken );
c2h = TDEGlobalSettings::highlightColor().light( lighten );
}
// draw the gradient now
TQPainter painter;
KPixmap kpm;
kpm.resize( ( ( TQWidget * ) parent() ) ->size() ); // use parent's same size to obtain the same gradient
if ( !highlighted ) {
// first the gradient
KPixmapEffect::unbalancedGradient ( kpm, c2, c1, KPixmapEffect::HorizontalGradient, -150, -150 );
}
else {
// top gradient (highlighted)
kpm.resize( width(), height() );
KPixmapEffect::unbalancedGradient ( kpm, c2h, c1h, KPixmapEffect::HorizontalGradient, -150, -150 );
// low gradient besides the line (not hightlighted)
KPixmap kpmb;
kpmb.resize( width(), 2 );
KPixmapEffect::unbalancedGradient ( kpmb, c2, c1, KPixmapEffect::HorizontalGradient, -150, -150 );
// mix the two
bitBlt( &kpm, 0, height() - 2, &kpmb );
}
// Draw the line
painter.begin( &kpm );
painter.setPen( colorGroup().button().dark( darken ) );
painter.drawLine( width() / 5, height() - 2, width() - 1, height() - 2 );
painter.setPen( colorGroup().button().light( lighten ) );
painter.drawLine( width() / 5, height() - 1, width() - 1, height() - 1 );
painter.end();
// Now Add the icon
painter.begin( &kpm );
int xPos, yPos;
if ( icon ) {
// Set the icon's desired horizontal position
xPos = 10;
yPos = 0;
// Make sure it fits in the area
// If not, resize and reposition horizontally to be centered
TQPixmap scaledIcon = *icon;
if ( ( icon->height() > height() ) || ( icon->width() > width() / 3 ) ) // Nice effect, make sure you take less than half in width and fit in height (try making the menu very short in width)
{
TQImage image;
image = ( *icon );
scaledIcon.convertFromImage( image.smoothScale( width() / 3, height(), TQImage::ScaleMin ) );
}
// Calculate the icon's vertical position
yPos = ( height() - scaledIcon.height() ) / 2 - 1;
// Now draw it
painter.drawPixmap( xPos, yPos, scaledIcon );
xPos += scaledIcon.width(); // increase it to place the text area correctly
}
painter.end();
// If it's highlighted, draw a rounded area around the text
// Calculate the rounded area
int areax = xPos + 10;
int areah = fontMetrics().height() * ( text.contains( '\n' ) + 1 ) + fontMetrics().lineSpacing() * text.contains( '\n' ) + 6; // Make sure the area is sensible for text and adjust for multiple lines
int areaw = width() - areax - 10;
if ( areah > ( height() - 4 ) ) {
areah = height() - 4; // Limit to button height
}
int areay = ( height() - areah - 2 ) / 2 + 1; // Center the area vertically
// Calculate roundness
int roundy = 99, roundx = ( int ) ( ( float ) roundy * areah / areaw ); //Make corners round
if ( highlighted && areaw > 0 ) // If there is no space for the text area do not draw it
{
// Draw the gradient
KPixmap area;
area.resize( areaw, areah );
KPixmapEffect::gradient( area, c2h.light( 150 ), c1h.light( 150 ), KPixmapEffect::VerticalGradient );
painter.begin( &area );
painter.setPen( c1h );
painter.setBrush( TQt::NoBrush );
painter.drawRoundRect( 0, 0, areaw, areah, roundx, roundy );
painter.end();
// Make it round
TQBitmap mask( TQSize( areaw, areah ) );
mask.fill( TQt::color0 );
painter.begin( &mask );
painter.setPen( TQt::color1 );
painter.setBrush( TQt::color1 );
painter.drawRoundRect( 0, 0, areaw, areah, roundx, roundy );
painter.end();
area.setMask( mask );
// Copy it to the button
bitBlt( &kpm, areax, areay, &area );
}
// Finally, draw the text besides the icon
TQRect r = rect();
r.setLeft( areax + 5 );
r.setWidth( areaw - 10 );
painter.begin( &kpm );
if ( highlighted )
painter.setPen( TDEGlobalSettings::highlightedTextColor() );
else
painter.setPen( TDEGlobalSettings::textColor() );
painter.setClipRect( r );
painter.drawText( r, TQt::AlignVCenter, text );
painter.end();
// Copy the offscreen button to the widget
bitBlt( this, 0, 0, &kpm, 0, 0, width(), height() ); // Copy the image with correct button size (button is already smaller than parent in width to leave space for the handle, so no need to use -10)
}
void KreMenuButton::setIconSet( const TQIconSet &is )
{
delete icon;
icon = new TQPixmap( is.pixmap( TQIconSet::Small, TQIconSet::Normal, TQIconSet::On ) );
setMinimumWidth( minimumSizeHint().width() );
if ( parentWidget() ->minimumWidth() < minimumSizeHint().width() )
parentWidget() ->setMinimumWidth( minimumSizeHint().width() + 10 );
}
Menu::Menu( void )
{
childPos = 10; // Initial button is on top (10px), then keep scrolling down
widgetNumber = 0; // Initially we have no buttons
activeButton = 0; // Button that is highlighted
}
Menu::Menu( const Menu &m )
{
activeButton = m.activeButton;
childPos = m.childPos;
widgetNumber = m.widgetNumber;
copyMap( positionList, m.positionList );
copyMap( widgetList, m.widgetList );
}
Menu::~Menu( void )
{}
Menu& Menu::operator=( const Menu &m )
{
activeButton = m.activeButton;
childPos = m.childPos;
widgetNumber = m.widgetNumber;
copyMap( positionList, m.positionList );
copyMap( widgetList, m.widgetList );
return *this;
}
void Menu::addButton( KreMenuButton* button )
{
button->move( 0, childPos );
button->rescale();
childPos += button->height();
positionList[ button ] = widgetNumber; // Store index for this widget, and increment number
widgetList[ widgetNumber ] = button; // Store the button in the list (the inverse mapping of the previous one)
widgetNumber++;
}
void Menu::copyMap( TQMap <int, KreMenuButton*> &destMap, const TQMap <int, KreMenuButton*> &origMap )
{
TQMap<int, KreMenuButton*>::ConstIterator it;
destMap.clear();
for ( it = origMap.begin(); it != origMap.end(); ++it ) {
destMap[ it.key() ] = it.data();
}
}
void Menu::copyMap( TQMap <KreMenuButton*, int> &destMap, const TQMap <KreMenuButton*, int> &origMap )
{
TQMap<KreMenuButton*, int>::ConstIterator it;
destMap.clear();
for ( it = origMap.begin(); it != origMap.end(); ++it ) {
destMap[ it.key() ] = it.data();
}
}
#include "kremenu.moc"
|