Salome HOME
c53feb6e8dab6e91bd8b386226a21ea384d1582c
[modules/gui.git] / src / Style / SalomeStyle.cxx
1 #include "SalomeStyle.h"
2
3 #include <qimage.h>
4 #include <qcursor.h>
5 #include <qtabbar.h>
6 #include <qpalette.h>
7 #include <qtoolbar.h>
8 #include <qspinbox.h>
9 #include <qpainter.h>
10 #include <qlineedit.h>
11 #include <qlistview.h>
12 #include <qdrawutil.h>
13 #include <qpopupmenu.h>
14 #include <qpushbutton.h>
15 #include <qobjectlist.h>
16 #include <qtoolbutton.h>
17 #include <qapplication.h>
18
19 static const char* const hole_xpm[] = {
20 "3 3 3 1",
21 ". c None",
22 "a c #999999",
23 "b c #FFFFFF",
24 "aa.",
25 "aab",
26 ".bb"
27 };
28
29 static const char* const cross_xpm[] = {
30 "12 12 4 1",
31 ". c None",
32 "a c #000000",
33 "b c #FFFFFF",
34 "c c #666666",
35 "............",
36 "............",
37 "............",
38 ".aaaa..aaaa.",
39 "..abbaabba..",
40 "...abbbba...",
41 "....abba....",
42 "...abbbba...",
43 "..abbaabba..",
44 ".aaaa..aaaa.",
45 "............",
46 "............"
47 };
48
49 static const char* const maximize_xpm[] = {
50 "12 12 4 1",
51 ". c None",
52 "a c #000000",
53 "b c #FFFFFF",
54 "c c #666666",
55 "............",
56 ".aaaaaaaaaa.",
57 ".acccccccca.",
58 ".acccccccca.",
59 ".abbbbbbbba.",
60 ".abbbbbbbba.",
61 ".abbbbbbbba.",
62 ".abbbbbbbba.",
63 ".abbbbbbbba.",
64 ".abbbbbbbba.",
65 ".aaaaaaaaaa.",
66 "............"
67 };
68
69 static const char* const normal_xpm[] = {
70 "12 12 4 1",
71 ". c None",
72 "a c #000000",
73 "b c #FFFFFF",
74 "c c #666666",
75 "............",
76 "...aaaaaaaa.",
77 "...acccccca.",
78 "...abbbbbba.",
79 "...aaaaaaba.",
80 ".aaaaaaaaba.",
81 ".accccccaba.",
82 ".abbbbbbaaa.",
83 ".abbbbbba...",
84 ".abbbbbba...",
85 ".aaaaaaaa...",
86 "............"
87 };
88
89 static const char* const minimize_xpm[] = {
90 "12 12 4 1",
91 ". c None",
92 "a c #000000",
93 "b c #FFFFFF",
94 "c c #666666",
95 "............",
96 "............",
97 "............",
98 "............",
99 "............",
100 "............",
101 "............",
102 "............",
103 "aaaaaaaaaaaa",
104 "abbbbbbbbbba",
105 "aaaaaaaaaaaa",
106 "............"
107 };
108
109 /*!
110     Class: SalomeStyle [Public]
111     Descr: Style for SALOME platform
112 */
113
114 SalomeStyle::SalomeStyle()
115 : myTitleParent( 0 )
116 {
117   qApp->installEventFilter( this ); 
118 }
119
120 SalomeStyle::~SalomeStyle()
121 {
122 }
123
124 void SalomeStyle::polish( QWidget* w )
125 {
126   if ( !w )
127     return;
128 /*
129   if ( w->inherits( "QLineEdit" ) )
130   {
131     QLineEdit* fr = (QLineEdit*)w;
132     if ( w->parentWidget() && w->parentWidget()->inherits( "QSpinWidget" ) )
133       fr->setFrameStyle( QFrame::Plain );
134     else
135     {
136       fr->setFrameStyle( QFrame::Plain | QFrame::Box );
137       fr->setLineWidth( 1 );
138       fr->setMidLineWidth( 0 );
139     }
140   }
141   else
142 */
143   if ( w->inherits( "QToolBar" ) )
144   {
145     QToolBar* tb = (QToolBar*)w;
146     tb->setFrameStyle( QFrame::ToolBarPanel | QFrame::Plain );
147     tb->setLineWidth( 1 );
148     tb->setMidLineWidth( 1 );
149   }
150   else
151   if ( w->inherits( "QDockWindow" ) )
152   {
153     QDockWindow* dw = (QDockWindow*)w;
154     dw->setFrameStyle( QFrame::ToolBarPanel | QFrame::Raised );
155   }
156 /*
157   else if ( w->inherits( "QListView" ) )
158   {
159     QListView* lv = (QListView*)w;
160     lv->setFrameStyle( QFrame::Box | QFrame::Plain );
161   }
162 */
163   else
164     PARENT_STYLE::polish( w );
165 }
166
167 bool SalomeStyle::eventFilter( QObject* o, QEvent* e )
168 {
169   if ( e->type() == QEvent::FocusIn || e->type() == QEvent::FocusOut )
170   {
171     QDockWindow* wnd = NULL;
172     while( o && !wnd )
173       if( o->inherits( "QDockWindow" ) )
174         wnd = ( QDockWindow* )o;
175       else
176         o = o->parent();
177
178     if ( wnd )
179     {
180       const QObjectList* objList = o->children();
181       for( QObjectListIterator anIt( *objList ); anIt.current(); ++anIt )
182         if( anIt.current()->inherits( "QDockWindowHandle" ) )
183         {
184           QWidget* ww = ( QWidget* )anIt.current();
185           ww->repaint( ww->rect() );
186           const QObjectList* wc = ww->children();
187           for( QObjectListIterator anIt2( *wc ); anIt2.current(); ++anIt2 )
188             if( anIt2.current()->inherits( "QToolButton" ) )
189             {
190               QWidget* tb = ( QWidget* )anIt2.current();
191               tb->repaint( tb->rect() );
192             }
193         }      
194     }
195   }
196
197   if ( o && o->inherits( "QPushButton" ) && ( e->type() == QEvent::Enter || e->type() == QEvent::Leave ) )
198     ((QWidget*)o)->update();
199
200   return false;
201 }
202
203 double linear( double x )
204 {
205   return x;
206 }
207
208 void SalomeStyle::mix( const double t, const QColor& c1, const QColor& c2, QColor& res )
209 {
210   if( t<0.0 || t>1.0 )
211     return;
212
213   int r = int( c1.red() * (1-t) + c2.red() * t ),
214       g = int( c1.green() * (1-t) + c2.green() * t ),
215       b = int( c1.blue() * (1-t) + c2.blue() * t );
216   res.setRgb( r, g, b );
217 }
218
219 void SalomeStyle::mix( const double t, QRgb& rgb1, const QRgb& rgb2 )
220 {
221   if( t<0.0 || t>1.0 )
222     return;
223
224   int c[2][4] = { qRed( rgb1 ), qGreen( rgb1 ), qBlue( rgb1 ), qAlpha( rgb1 ),
225                   qRed( rgb2 ), qGreen( rgb2 ), qBlue( rgb2 ), qAlpha( rgb2 ) };
226   for( int i=0; i<4; i++ )
227     c[0][i] = (int)( c[0][i] * (1-t) + c[1][i] * t );
228
229   rgb1 = qRgba( c[0][0], c[0][1], c[0][2], qAlpha( rgb1 ) );
230 }
231
232 void SalomeStyle::mix( const double t, QPixmap& pix, const QColor& col )
233 {
234   if( t<0.0 || t>1.0 )
235     return;
236
237   QImage anImage = pix.convertToImage();
238   if( anImage.isNull() )
239     return;
240
241   if( anImage.depth()!=32 )
242     anImage = anImage.convertDepth( 32 );
243
244   int w = anImage.width(),
245       h = anImage.height();
246
247   QRgb colrgb = col.rgb(), cur;
248   for( int y=0; y<h; y++ )
249   {
250     uchar* line = anImage.scanLine( y );
251     QRgb*  colorline = ( QRgb* ) line;
252     for( int x=0; x<w; x++ )
253     {
254       cur = colorline[ x ];
255       mix( t, cur, colrgb );
256       colorline[ x ] = cur;
257     }
258   }
259   pix = anImage;
260 }
261
262 void SalomeStyle::toGrayscale( QPixmap& pix, double k )
263 {
264   QImage anImage = pix.convertToImage();
265   if( anImage.isNull() )
266     return;
267
268   if( anImage.depth()!=32 )
269     anImage = anImage.convertDepth( 32 );
270
271   int w = anImage.width(),
272       h = anImage.height();
273
274   for( int y=0; y<h; y++ )
275   {
276     uchar* line = anImage.scanLine( y );
277     QRgb*  colorline = ( QRgb* ) line;
278     for( int x=0; x<w; x++ )
279     {
280       int gray = (int)( k * qGray( colorline[ x ] ) );
281       if ( gray>255 )
282         gray = 255;
283       else if( gray<0 )
284         gray = 0;
285       colorline[ x ] = qRgba( gray, gray, gray, qAlpha( colorline[x] ) );
286     }
287   }
288   pix = anImage;
289 }
290
291 void SalomeStyle::drawGradient( QPainter* p, const QRect& r,
292                                     const QColor& c1, const QColor& c2,
293                                     const Direction d, gradient_func f ) const 
294 {
295   p->save();
296   p->translate( r.x(), r.y() );
297
298   QColor mid;
299   if( d==LeftToRight || d==RightToLeft )
300     for( int xmax = r.width(), x = d==LeftToRight ? 0 : xmax;
301          d==LeftToRight ? x<=xmax : x>=0;
302          d==LeftToRight ? x++ : x-- )
303     {
304       double t = double( x ) / double( xmax );
305       mix( d==LeftToRight ? f(t) : 1-f(t), c1, c2, mid );
306       p->setPen( mid );
307       p->drawLine( x, 0, x, r.height() );
308     }
309   else
310     for( int ymax = r.height(), y = d==UpToDown ? 0 : ymax;
311          d==UpToDown ? y<=ymax : y>=0;
312          d==UpToDown ? y++ : y-- )
313     {
314       double t = double( y ) / double( ymax );
315       mix( d==UpToDown ? f(t) : 1-f(t), c1, c2, mid );
316       p->setPen( mid );
317       p->drawLine( 0, y, r.width(), y );
318     }
319
320   p->restore();
321 }
322
323 void SalomeStyle::drawPrimitive( PrimitiveElement pe, QPainter* p, const QRect& r,
324                                  const QColorGroup& cg, SFlags flags, const QStyleOption& opt ) const
325 {
326   switch ( pe )
327   {
328   case PE_ButtonTool:
329     if ( !myTitleParent )
330       PARENT_STYLE::drawPrimitive( pe, p, r, cg, flags, opt );
331     break;
332
333   case PE_DockWindowResizeHandle:
334     p->fillRect( r.x(), r.y(), r.width(), r.height(), cg.dark() );
335     if ( flags & Style_Horizontal )
336       p->fillRect( r.x(), r.y() + 1, r.width(), r.height() - 2, cg.midlight() );
337     else
338       p->fillRect( r.x() + 1, r.y(), r.width() - 2, r.height(), cg.midlight() );
339     break;
340
341   case PE_DockWindowHandle:
342     {
343       QPaintDevice* pd = p->device();
344       if ( !pd )
345         break;
346
347       QWidget* w = (QWidget*)pd;
348
349       if ( !w || !w->inherits( "QDockWindowHandle" ) )
350         break;
351
352       QDockWindow* wnd = ( QDockWindow* ) w->parent();
353       if ( !wnd )
354         return;
355
356       QObject* activeWidget = qApp->focusWidget();
357       QDockWindow* activeWnd = 0;
358       while ( !activeWnd && activeWidget )
359       {
360         if ( activeWidget->inherits( "QDockWindow" ) )
361           activeWnd = (QDockWindow*)activeWidget;
362         else
363           activeWidget = activeWidget->parent();
364       }
365
366       bool act = wnd == activeWnd, horiz = flags & Style_Horizontal;
367
368       QPixmap hole( (const char**)hole_xpm );
369
370       if ( wnd->inherits( "QToolBar" ) )
371       {
372         drawGradient( p, r, cg.light(), cg.background(), horiz ? UpToDown : LeftToRight, linear );
373
374         int c = 4, i; double d = ( horiz ? r.height() : r.width() ) / ( c + 1 );
375         QBrush fill = cg.brush( QColorGroup::Dark );
376         p->setPen( Qt::red );
377         for ( i = 0; i < c; i++ )
378         {
379           if ( horiz )
380             p->drawPixmap( r.x() + r.width() / 2 - 1, (int)( r.y() + ( i + 1 ) * d - 1 ), hole );
381           else
382             p->drawPixmap( (int) ( r.x() + ( i + 1 ) * d - 1 ), r.y() + r.height() / 2 - 1, hole );
383         }
384
385         int dd = (int(d)/2) + (int(d)%2);
386         for ( i = 0; i < c - 1; i++ )
387         {
388           if ( horiz )
389             p->drawPixmap( r.x() + r.width() / 2 + 2, (int)( r.y() + dd + ( i + 1 ) * d - 1 ), hole );
390           else
391             p->drawPixmap( (int)( r.x() + dd + ( i + 1 ) * d - 1 ), r.y() + r.height() / 2 + 2, hole );
392         }
393       }
394       else
395       {
396         int d = 1;
397         QRect rr( r.x()+d, r.y()+d, r.width()-2*d, r.height()-2*d );
398
399         QColor col; 
400         if ( act )
401           mix( 0.5, cg.highlight(), Qt::white, col );
402         else
403           col = cg.background();
404
405         drawGradient( p, rr, act ? cg.highlight() : cg.dark(), col,
406                       horiz ? LeftToRight : UpToDown, linear );
407
408         QRect rt = rr;
409         if ( flags & Style_Horizontal )
410           rt.addCoords( 0, 20, 0, 0 );
411         else
412           rt.addCoords( 0, 0, -20, 0 );
413
414         int textW = flags & Style_Horizontal ? rt.height() : rt.width();
415         int textH = flags & Style_Horizontal ? rt.width() : rt.height();
416
417         QString title = titleText( wnd->caption(), textW, p->fontMetrics() );
418
419         if ( wnd )
420         {
421           p->setPen( act ? cg.highlightedText() : cg.text() );
422           
423           if ( flags & Style_Horizontal )
424           {
425             p->rotate( 270.0 );
426             p->translate( -(rt.height()+rt.y()), (rt.width()-rt.x()) );
427             p->drawText( 0, 0, title );
428           }
429           else
430             p->drawText( 2, 2, textW, textH, AlignLeft, title );
431         }
432       }
433       break;
434     }
435
436   case PE_DockWindowSeparator:
437     {
438       bool horiz = ( flags & Style_Horizontal );
439       QWidget* w = ( QWidget* )p->device();
440       if( w->inherits( "QToolBarSeparator" ) )
441       {
442         drawGradient( p, r, cg.light(), cg.background(), horiz ? UpToDown : LeftToRight, linear );
443         int w = r.width(), h = r.height();
444         int part = 8;
445
446         p->setPen( cg.dark() );
447         if( horiz )
448           p->drawLine( r.x() + w/2, r.y() + h/part, r.x() + w/2, r.y() + (part-1)*h/part );
449         else
450           p->drawLine( r.x() + w/part, r.y() + h/2, r.x() + (part-1)*w/part, r.y() + h/2 );
451
452         p->setPen( cg.light() );
453         if( horiz )
454           p->drawLine( r.x() + w/2 + 1, r.y() + h/part + 1, r.x() + w/2 + 1, r.y() + (part-1)*h/part + 1 );
455         else
456           p->drawLine( r.x() + w/part + 1, r.y() + h/2 + 1, r.x() + (part-1)*w/part + 1, r.y() + h/2 + 1 );
457       }
458       else
459         PARENT_STYLE::drawPrimitive( pe, p, r, cg, flags, opt );
460       break;
461     }
462     
463   case PE_PanelDockWindow:
464     {
465       QWidget* w = ( QWidget* )p->device();
466
467       int offset = pixelMetric( PM_DockWindowHandleExtent, w );
468       if ( w->inherits( "QToolBar" ) )
469       {
470         bool vert = ( ( QToolBar* )w )->orientation() == Qt::Vertical;
471         if ( vert )
472           drawGradient( p, QRect( r.x(), r.y()+offset, r.x()+r.width(), r.y()+offset ),
473                         cg.light(), cg.background(), LeftToRight, linear );
474         else
475           drawGradient( p, QRect( r.x()+offset, r.y(), r.x()+offset, r.y()+r.height() ),
476                         cg.light(), cg.background(), UpToDown, linear );
477
478         p->setPen( QPen( cg.light(), 1 ) );
479         p->drawRect( r );
480       }
481       else
482       {
483         p->setPen( QPen( cg.dark(), 1 ) );
484         p->setBrush( cg.light() );
485         p->drawRect( r );
486       }
487
488       break;
489     }
490
491   case PE_PanelPopup:
492     p->setPen( QPen( cg.dark(), 1 ) );
493     p->setBrush( cg.light() );
494     p->drawRect( r );
495     break;
496
497   case PE_HeaderSection:
498     {
499       QColor col; mix( 0.2, cg.light(), Qt::black, col );
500       drawGradient( p, r, cg.light(), col, DownToUp, linear );
501       drawGradient( p, QRect( r.x(), r.y(), 1, r.height() ), cg.light(), cg.dark(), DownToUp, linear );
502       p->setPen( cg.dark() );
503       p->drawLine( r.x(), r.y()+r.height()-1, r.x()+r.width(), r.y()+r.height()-1 );
504       break;
505     }
506
507   case PE_Splitter:
508     p->fillRect( r.x() + 1, r.y() + 1, r.width() - 2, r.height() - 2, cg.dark() );
509     p->fillRect( r.x() + 2, r.y() + 2, r.width() - 4, r.height() - 4, cg.midlight() );
510           break;
511
512   case PE_ButtonBevel:
513     PARENT_STYLE::drawPrimitive( PE_ButtonDropDown, p, r, cg, flags, opt );
514     break;
515
516   case PE_ButtonCommand:
517     {
518       QColorGroup cGrp( cg );
519       if ( flags & Style_MouseOver )
520         cGrp.setColor( QColorGroup::Button, cGrp.midlight() );
521
522       if ( flags & Style_MouseOver )
523         PARENT_STYLE::drawPrimitive( PE_ButtonDropDown, p, r, cGrp, flags, opt );
524       else
525         PARENT_STYLE::drawPrimitive( flags & Style_Raised ? PE_ButtonDropDown : pe, p, r, cGrp, flags, opt );
526     }
527     break;
528
529   default:
530     PARENT_STYLE::drawPrimitive( pe, p, r, cg, flags, opt );
531     break;
532   }
533 }
534
535 void SalomeStyle::drawControl( ControlElement element, QPainter* p, const QWidget* widget, const QRect& r,
536                                const QColorGroup& cg, SFlags flags, const QStyleOption& opt ) const 
537 {
538   switch( element )
539   {
540     case CE_PopupMenuItem:
541     {
542           if(! widget || opt.isDefault())
543             break;
544
545       const QPopupMenu *popupmenu = (const QPopupMenu *) widget;
546       QMenuItem *mi = opt.menuItem();
547       if( !mi )
548             break;
549
550           int tab = opt.tabWidth();
551           int maxpmw = opt.maxIconWidth();
552           bool dis = !( flags & Style_Enabled );
553           bool checkable = popupmenu->isCheckable();
554           bool act = flags & Style_Active;
555       bool use2000style = true;
556           int x, y, w, h;
557
558       int windowsItemFrame = 2,
559           windowsItemHMargin = 5,
560           windowsItemVMargin = 1,
561           windowsRightBorder = 2,
562           windowsArrowHMargin = 2;
563
564           r.rect(&x, &y, &w, &h);
565
566           if ( checkable )
567       {
568             // space for the checkmarks
569         if (use2000style)
570                   maxpmw = QMAX( maxpmw, 20 );
571                 else
572                   maxpmw = QMAX( maxpmw, 12 );
573           }
574
575           int checkcol = maxpmw;
576
577           if ( mi && mi->isSeparator() )
578       {
579                 p->setPen( cg.dark() );
580                 p->drawLine( x+3*checkcol/2, y, x+w, y );
581                 //p->setPen( cg.light() );
582                 //p->drawLine( x, y+1, x+w, y+1 );
583                 return;
584           }
585
586       QBrush fill = ( act && !dis ?
587                  cg.brush( QColorGroup::Highlight ) :
588                          cg.brush( QColorGroup::Light ) );
589           p->fillRect( x, y, w, h, fill );
590
591           if( !mi )
592                 return;
593
594           int xpos = x;
595           QRect vrect = visualRect( QRect( xpos, y, checkcol, h ), r );
596           int xvis = vrect.x();
597
598           if( mi->isChecked() )
599       {
600             if( act && !dis )
601                   qDrawShadePanel( p, xvis, y, checkcol, h,
602                            cg, TRUE, 1, &cg.brush( QColorGroup::Button ) );
603                 else
604         {
605                   QBrush fill( cg.light(), Dense4Pattern );
606                   // set the brush origin for the hash pattern to the x/y coordinate
607                   // of the menu item's checkmark... this way, the check marks have
608                   // a consistent look
609                   QPoint origin = p->brushOrigin();
610                   p->setBrushOrigin( xvis, y );
611                   qDrawShadePanel( p, xvis, y, checkcol, h, cg, TRUE, 1, &fill );
612           // restore the previous brush origin
613                   p->setBrushOrigin( origin );
614                 }
615           }
616       else if( !act )
617                 p->fillRect(x+checkcol, y, w-checkcol , h, cg.brush( QColorGroup::Light ));
618
619       if( !( act && !dis ) && !mi->isChecked() )
620       {
621         QColor col; mix( 0.5, cg.mid(), Qt::white, col );
622         drawGradient( p, QRect( xvis, y, checkcol, h+1 ), cg.light(), col, LeftToRight, linear );
623       }
624
625       if( mi->iconSet() )
626       { // draw iconset
627                 QIconSet::Mode mode = dis ? QIconSet::Active : QIconSet::Normal;
628                 if(act && !dis )
629                   mode = QIconSet::Active;
630                 QPixmap pixmap;
631                 if( checkable && mi->isChecked() )
632                   pixmap = mi->iconSet()->pixmap( QIconSet::Small, mode, QIconSet::On );
633                 else
634                   pixmap = mi->iconSet()->pixmap( QIconSet::Small, mode );
635                 int pixw = pixmap.width();
636                 int pixh = pixmap.height();
637
638         if( dis )
639           toGrayscale( pixmap, 1.1 );
640
641                 //if( act && !dis && !mi->isChecked() )
642                 //  qDrawShadePanel( p, xvis, y, checkcol, h, cg, FALSE, 1, &cg.brush( QColorGroup::Button ) );
643
644                 QRect pmr( 0, 0, pixw, pixh );
645                 pmr.moveCenter( vrect.center() );
646                 p->setPen( cg.text() );
647                 p->drawPixmap( pmr.topLeft(), pixmap );
648
649                 QBrush fill = ( act ? cg.brush( QColorGroup::Highlight ) : cg.brush( QColorGroup::Light ));
650                 int xp = xpos + checkcol + 1;
651         if( !dis )
652                   p->fillRect( visualRect( QRect( xp, y, w - checkcol - 1, h ), r ), fill);
653           }
654       else if( checkable )
655       {  // just "checking"...
656                 if( mi->isChecked() )
657         {
658                   int xp = xpos + windowsItemFrame;
659
660                   SFlags cflags = Style_Default;
661                   if( !dis )
662                     cflags |= Style_Enabled;
663                   if( act )
664                         cflags |= Style_On;
665
666                   drawPrimitive( PE_CheckMark, p,
667                                          visualRect( QRect( xp, y + windowsItemFrame,
668                                              checkcol - 2*windowsItemFrame,
669                                              h - 2*windowsItemFrame), r ), cg, cflags, opt );
670                 }
671           }
672
673           p->setPen( act ? cg.highlightedText() : cg.buttonText() );
674
675           QColor discol;
676           if( dis )
677       {
678                 discol = cg.text();
679                 p->setPen( discol );
680           }
681
682           int xm = windowsItemFrame + checkcol + windowsItemHMargin;
683           xpos += xm;
684
685           vrect = visualRect( QRect( xpos, y+windowsItemVMargin, w-xm-tab+1, h-2*windowsItemVMargin ), r );
686           xvis = vrect.x();
687           if( mi->custom() )
688       {
689                 p->save();
690                 if ( dis && !act )
691         {
692                   p->setPen( cg.light() );
693                   mi->custom()->paint( p, cg, act, !dis,
694                                                    xvis+1, y+windowsItemVMargin+1,
695                                w-xm-tab+1, h-2*windowsItemVMargin );
696                   p->setPen( discol );
697                 }
698                 mi->custom()->paint( p, cg, act, !dis, xvis, y+windowsItemVMargin,
699                              w-xm-tab+1, h-2*windowsItemVMargin );
700                 p->restore();
701           }
702           QString s = mi->text();
703           if( !s.isNull() )
704       { // draw text
705                 int t = s.find( '\t' );
706                 int text_flags = AlignVCenter | ShowPrefix | DontClip | SingleLine;
707                 if( !styleHint( SH_UnderlineAccelerator, widget ) )
708                   text_flags |= NoAccel;
709                 text_flags |= (QApplication::reverseLayout() ? AlignRight : AlignLeft );
710                 if( t >= 0 )
711         { // draw tab text
712                   int xp = x + w - tab - windowsItemHMargin - windowsItemFrame + 1;
713                   if( use2000style )
714                     xp -= 20;
715                   else
716                         xp -= windowsRightBorder;
717                   int xoff = visualRect( QRect( xp, y+windowsItemVMargin, tab, h-2*windowsItemVMargin ), r ).x();
718                   if( dis && !act )
719           {
720                         p->setPen( cg.light() );
721                         p->drawText( xoff+1, y+windowsItemVMargin+1, tab, h-2*windowsItemVMargin, text_flags, s.mid( t+1 ));
722                         p->setPen( discol );
723                   }
724                   p->drawText( xoff, y+windowsItemVMargin, tab, h-2*windowsItemVMargin, text_flags, s.mid( t+1 ) );
725                   s = s.left( t );
726                 }
727                 if( dis && !act )
728         {
729                   p->setPen( cg.light() );
730                   p->drawText( xvis+1, y+windowsItemVMargin+1, w-xm-tab+1, h-2*windowsItemVMargin, text_flags, s, t );
731                   p->setPen( discol );
732                 }
733                 p->drawText( xvis, y+windowsItemVMargin, w-xm-tab+1, h-2*windowsItemVMargin, text_flags, s, t );
734           }
735       else if( mi->pixmap() )
736       { // draw pixmap
737                 QPixmap *pixmap = mi->pixmap();
738                 if( pixmap->depth() == 1 )
739                   p->setBackgroundMode( OpaqueMode );
740                 p->drawPixmap( xvis, y+windowsItemFrame, *pixmap );
741                 if( pixmap->depth() == 1 )
742                   p->setBackgroundMode( TransparentMode );
743           }
744           if( mi->popup() )
745       { // draw sub menu arrow
746                 int dim = (h-2*windowsItemFrame) / 2;
747                 PrimitiveElement arrow;
748                 arrow = ( QApplication::reverseLayout() ? PE_ArrowLeft : PE_ArrowRight );
749                 xpos = x+w - windowsArrowHMargin - windowsItemFrame - dim;
750                 vrect = visualRect( QRect(xpos, y + h / 2 - dim / 2, dim, dim), r );
751                 if( act )
752         {
753                   QColorGroup g2 = cg;
754                   g2.setColor( QColorGroup::ButtonText, g2.highlightedText() );
755                   drawPrimitive( arrow, p, vrect, g2, dis ? Style_Default : Style_Enabled, opt );
756                 }
757         else
758         {
759                   drawPrimitive( arrow, p, vrect, cg, dis ? Style_Default : Style_Enabled, opt );
760                 }
761           }
762
763       if( act && !dis )
764       {
765         p->setPen( cg.dark() );
766         p->drawRect( r );
767       }
768           break;
769     }
770
771     case CE_MenuBarItem:
772         {
773       bool active = flags & Style_Active;
774           bool down = flags & Style_Down;
775           QRect pr = r;
776
777       p->save();
778       if( active && down )
779       {
780         //QColor col; mix( 0.5, cg.mid(), Qt::white, col );
781         //drawGradient( p, r, col, cg.light(), UpToDown, linear );
782         p->setPen( cg.light() );
783         p->setBrush( cg.light() );
784         p->drawRect( r );
785         
786         p->setPen( cg.dark() );
787         int w = r.width()-1, h = r.height();
788         p->drawLine( r.x(), r.y(), r.x()+w, r.y() );
789         p->drawLine( r.x(), r.y(), r.x(), r.y()+h );
790         p->drawLine( r.x()+w, r.y(), r.x()+w, r.y()+h );
791
792         p->translate( 1, 1 );
793       }
794       else
795         drawGradient( p, r, cg.light(), cg.background(), active ? DownToUp : UpToDown, linear );
796
797           QCommonStyle::drawControl(element, p, widget, pr, cg, flags, opt);
798       p->restore();
799           break;
800         }
801
802     case CE_MenuBarEmptyArea:
803     {
804       drawGradient( p, r, cg.light(), cg.background(), UpToDown, linear );
805           break;
806     }
807
808     case CE_ToolButtonLabel:
809         {
810       const QToolButton *toolbutton = (const QToolButton *) widget;
811       QObject* parent = toolbutton->parent();
812
813       QToolBar* tb = NULL;
814       while( parent )
815         if( parent->inherits( "QToolBar" ) )
816         {
817           tb = ( QToolBar* )parent;
818           break;
819         }
820         else
821           parent = parent->parent();
822       parent = toolbutton->parent();
823
824       int horiz = -1;
825       if( tb )
826       {
827         if( tb->orientation()==Qt::Horizontal )
828           horiz = 1;
829         else
830           horiz = 0;
831         drawGradient( p, r, cg.light(), cg.background(), horiz ? UpToDown : LeftToRight, linear );
832       }
833       else if( parent->inherits( "QDockWindowHandle" ) )
834       {
835         QWidget* w = ( QWidget* ) parent;
836         QDockWindow* wnd = ( QDockWindow* )w->parent();
837         if( !wnd->orientation()==Qt::Horizontal )
838           horiz=1;
839         else
840           horiz=0;
841
842         QPoint pp( r.x(), r.y() ),
843                qq = toolbutton->mapToParent( pp );
844         
845         bitBlt( p->device(), r.x(), r.y(), w, horiz ? qq.x()-r.width() : qq.x(),
846                 horiz ? qq.y() : qq.y()+r.height(),
847                 r.width(), r.height(), Qt::CopyROP, true );
848       }
849
850       bool highlight = ( flags & Style_MouseOver ),
851            down = ( flags & ( Style_Down | Style_On ) ),
852            en = toolbutton->isEnabled();
853
854       if( down )
855         if( horiz>=0 )
856         {
857           drawGradient( p, r, cg.light(), cg.background(), horiz==1 ? DownToUp : RightToLeft, linear );
858           p->setPen( cg.background() );
859           p->drawRect( r );
860         }
861         else
862         {
863           p->setPen( cg.dark() );
864           p->setBrush( cg.light() );
865           p->drawRect( r );
866         }
867       if( highlight )
868       {
869         p->setPen( cg.dark() );
870         QColor col; mix( 0.05, cg.background(), Qt::black, col );
871         p->setBrush( col );
872         p->drawRect( r );
873       }
874
875
876           QRect rect = r;
877           Qt::ArrowType arrowType = opt.isDefault() ? Qt::DownArrow : opt.arrowType();
878
879           int shiftX = 0, shiftY = 0;
880           if( flags & ( Style_Down | Style_On ) )
881       {
882                 shiftX = pixelMetric(PM_ButtonShiftHorizontal, widget);
883                 shiftY = pixelMetric(PM_ButtonShiftVertical, widget);
884           }
885
886           if (!opt.isDefault())
887       {
888                 PrimitiveElement pe;
889                 switch (arrowType)
890         {
891                   case Qt::LeftArrow:  pe = PE_ArrowLeft;  break;
892                   case Qt::RightArrow: pe = PE_ArrowRight; break;
893                   case Qt::UpArrow:    pe = PE_ArrowUp;    break;
894                   default:
895                   case Qt::DownArrow:  pe = PE_ArrowDown;  break;
896         }
897
898         rect.moveBy(shiftX, shiftY);
899             drawPrimitive(pe, p, rect, cg, flags, opt);
900           }
901       else
902       {
903                 QColor btext = toolbutton->paletteForegroundColor();
904
905                 if (toolbutton->iconSet().isNull() &&
906                     ! toolbutton->text().isNull() &&
907                     ! toolbutton->usesTextLabel())
908         {
909                     int alignment = AlignCenter | ShowPrefix;
910                     if (!styleHint(SH_UnderlineAccelerator, widget, QStyleOption::Default, 0))
911                           alignment |= NoAccel;
912
913                     rect.moveBy(shiftX, shiftY);
914                     drawItem( p, rect, alignment, cg, flags & Style_Enabled, 0, toolbutton->text(),
915                                   toolbutton->text().length(), &btext);
916         }
917         else
918         {
919               QPixmap pm;
920                   QIconSet::Size size =
921                   toolbutton->usesBigPixmap() ? QIconSet::Large : QIconSet::Small;
922                   QIconSet::State state = toolbutton->isOn() ? QIconSet::On : QIconSet::Off;
923                   QIconSet::Mode mode;
924                   if( !toolbutton->isEnabled() )
925                 mode = QIconSet::Active; //QIconSet::Disabled;
926                   else if( flags & (Style_Down | Style_On ) || ( flags & Style_Raised ) && (flags & Style_AutoRaise))
927                     mode = QIconSet::Active;
928                   else
929                     mode = QIconSet::Normal;
930                   pm = toolbutton->iconSet().pixmap( size, mode, state );
931
932                   if( toolbutton->usesTextLabel() )
933           {
934                     p->setFont( toolbutton->font() );
935                     QRect pr = rect, tr = rect;
936                     int alignment = ShowPrefix;
937                     if (!styleHint(SH_UnderlineAccelerator, widget, QStyleOption::Default, 0))
938                           alignment |= NoAccel;
939
940                     if( toolbutton->textPosition() == QToolButton::Under )
941             {
942                           int fh = p->fontMetrics().height();
943                           pr.addCoords( 0, 1, 0, -fh-3 );
944                           tr.addCoords( 0, pr.bottom(), 0, -3 );
945                           pr.moveBy(shiftX, shiftY);
946                           drawItem( p, pr, AlignCenter, cg, TRUE, &pm, QString::null );
947                           alignment |= AlignCenter;
948             }
949             else
950             {
951                           pr.setWidth( pm.width() + 8 );
952                           tr.addCoords( pr.right(), 0, 0, 0 );
953                           pr.moveBy(shiftX, shiftY);
954  
955                           drawItem( p, pr, AlignCenter, cg, TRUE, &pm, QString::null );
956                           alignment |= AlignLeft | AlignVCenter;
957             }
958
959                 tr.moveBy(shiftX, shiftY);
960                     drawItem( p, tr, alignment, cg, flags & Style_Enabled, 0, toolbutton->textLabel(),
961                                       toolbutton->textLabel().length(), &btext);
962           }
963           else
964           {
965             /*if( en && !highlight )
966               mix( 0.6, pm, cg.background() );
967             else*/ if( !en )
968             {
969               toGrayscale( pm, 1.1 );
970               mix( 0.8, pm, cg.background() );
971             }
972
973                     rect.moveBy(shiftX, shiftY);
974                     drawItem( p, rect, AlignCenter, cg, true, &pm, QString::null );
975           }
976         }
977       }
978     }
979     break;
980
981     case CE_TabBarTab:
982           {
983       QPen oldPen = p->pen();
984
985             QRect r2( r );
986             bool selected = flags & Style_Selected;
987
988       p->setPen( cg.midlight() );
989       p->drawLine( r2.left(), r2.bottom(), r2.right(), r2.bottom() );
990
991       if ( !selected )
992         r2.setRect( r2.left(), r2.top() + 1, r2.width(), r2.height() - 1 );
993       else
994         r2.setRect( r2.left(), r2.top(), r2.width() - 2, r2.height() - 1 );
995
996       p->setPen( cg.foreground() );
997       QBrush brsh = cg.brush( selected ? QColorGroup::Base : QColorGroup::Background );
998       p->fillRect( r2.left(), r2.top(), r2.width(), r2.height() - 1, brsh );
999       p->drawRect( r2.left(), r2.top(), r2.width(), r2.height() - 1 );
1000
1001       if ( selected )
1002       {
1003         p->setPen( cg.shadow() );
1004         p->drawLine( r2.left() + 1, r2.bottom(), r2.right() + 1, r2.bottom() );
1005         p->drawLine( r2.right() + 1, r2.top() + 1, r2.right() + 1, r2.bottom() );
1006       }
1007
1008             break;
1009     }
1010
1011     case CE_PushButton:
1012           {
1013       if ( flags & Style_Enabled && !( flags & Style_Down || flags & Style_On ) )
1014       {
1015               const QPushButton* button = (const QPushButton*)widget;
1016         QRect br = QRect( button->mapToGlobal( button->rect().topLeft() ), button->rect().size() );
1017         if ( br.contains( QCursor::pos() )  )
1018           flags |= Style_MouseOver;
1019       }
1020
1021       PARENT_STYLE::drawControl( element, p, widget, r, cg, flags, opt );
1022             break;
1023           }
1024
1025     default:
1026       PARENT_STYLE::drawControl( element, p, widget, r, cg, flags, opt );
1027       break;
1028   }
1029 }
1030
1031 QRect SalomeStyle::subRect( SubRect subrect, const QWidget* w ) const
1032 {
1033   QRect r = PARENT_STYLE::subRect( subrect, w );
1034   if ( subrect==SR_DockWindowHandleRect && w->inherits( "QDockWindowHandle" ) )
1035   {
1036     QDockWindow* wnd = ( QDockWindow* ) w->parent();
1037     if( wnd->orientation()==Qt::Vertical )
1038       r.setWidth( w->width() );
1039     else
1040     {
1041       r.setY( r.y()+r.height()-w->height() );
1042       r.setHeight( w->height() );
1043     }
1044   }
1045
1046   return r;
1047 }
1048
1049 void SalomeStyle::drawComplexControl( ComplexControl control, QPainter* p, const QWidget* widget,
1050                                                               const QRect& r, const QColorGroup& cg, SFlags flags,
1051                                                               SCFlags controls, SCFlags active, const QStyleOption& opt ) const
1052 {
1053
1054
1055
1056
1057   ((SalomeStyle*)this)->myTitleParent = 0;
1058
1059   switch ( control )
1060   {
1061   case CC_ComboBox:
1062           if ( controls & SC_ComboBoxArrow )
1063     {
1064             SFlags flags = Style_Default;
1065
1066       qDrawShadeRect( p, r, cg, true, pixelMetric( PM_DefaultFrameWidth, widget ),
1067                       0, widget->isEnabled() ? &cg.brush( QColorGroup::Base ) : &cg.brush( QColorGroup::Background ) );
1068
1069             QRect ar = QStyle::visualRect( querySubControlMetrics( CC_ComboBox, widget, SC_ComboBoxArrow ), widget );
1070             if ( active == SC_ComboBoxArrow )
1071       {
1072                     p->setPen( cg.dark() );
1073                     p->setBrush( cg.brush( QColorGroup::Button ) );
1074                     p->drawRect( ar );
1075             }
1076       else
1077                     qDrawWinPanel( p, ar, cg, FALSE, &cg.brush( QColorGroup::Button ) );
1078
1079             ar.addCoords( 2, 2, -2, -2 );
1080             if ( widget->isEnabled() )
1081                     flags |= Style_Enabled;
1082
1083             if ( active == SC_ComboBoxArrow )
1084                     flags |= Style_Sunken;
1085             drawPrimitive( PE_ArrowDown, p, ar, cg, flags );
1086           }
1087     PARENT_STYLE::drawComplexControl( control, p, widget, r, cg, flags, controls & ~SC_ComboBoxArrow, active, opt );
1088     break;
1089
1090   case CC_TitleBar:
1091     if( widget->inherits( "QTitleBar" ) )
1092       ( ( SalomeStyle* )this )->myTitleParent = ( QWidget* )widget->parent();
1093
1094   default:
1095     PARENT_STYLE::drawComplexControl( control, p, widget, r, cg, flags, controls, active, opt );
1096     break;
1097   }
1098 }
1099
1100 QPixmap SalomeStyle::stylePixmap( StylePixmap st, const QWidget* w, const QStyleOption& opt ) const
1101 {
1102   switch ( st )
1103   {
1104   case SP_DockWindowCloseButton:
1105   case SP_TitleBarCloseButton:
1106     return QPixmap( cross_xpm );
1107   case SP_TitleBarMaxButton:
1108     return QPixmap( maximize_xpm );
1109   case SP_TitleBarNormalButton:
1110     return QPixmap( normal_xpm );
1111   case SP_TitleBarMinButton:
1112     return QPixmap( minimize_xpm );
1113   default:
1114     return PARENT_STYLE::stylePixmap( st, w, opt );
1115   }
1116 }
1117
1118 int SalomeStyle::pixelMetric( PixelMetric pm, const QWidget* widget ) const
1119 {
1120   int ret = 0;
1121
1122   switch ( pm )
1123   {
1124   case PM_ButtonMargin:
1125     ret = 5;
1126     break;
1127   case PM_DockWindowFrameWidth:
1128     ret = 0;
1129     break;
1130   case PM_TabBarTabOverlap:
1131           ret = 2;
1132           break;
1133   case PM_TabBarTabHSpace:
1134           ret = 20;
1135           break;
1136   case PM_TabBarTabShiftHorizontal:
1137   case PM_TabBarTabShiftVertical:
1138           ret = 0;
1139           break;
1140   case PM_TabBarTabVSpace:
1141                 ret = 5;
1142     break;
1143   case PM_DefaultFrameWidth:
1144     ret = 1;
1145     break;
1146   case PM_SpinBoxFrameWidth:
1147     ret = 1;
1148     break;
1149   case PM_ProgressBarChunkWidth:
1150     ret = 1;
1151     break;
1152   default:
1153     ret = PARENT_STYLE::pixelMetric( pm, widget );
1154     break;
1155   }
1156   return ret;
1157 }
1158
1159 QString SalomeStyle::titleText( const QString& txt, const int W, const QFontMetrics& fm ) const
1160 {
1161   QString res = txt.stripWhiteSpace();
1162
1163   if ( fm.width( res ) > W )
1164   {
1165     QString end( "..." );
1166     while ( !res.isEmpty() && fm.width( res + end ) > W )
1167       res.remove( res.length() - 1, 1 );
1168
1169     if ( !res.isEmpty() )
1170       res += end;
1171   }
1172
1173   return res;
1174 }
1175
1176 /*!
1177     Class: SalomeStylePlugin [Internal]
1178     Descr: Plugin for Qt style mechanism
1179 */
1180
1181 SalomeStylePlugin::SalomeStylePlugin()
1182 {
1183 }
1184
1185 SalomeStylePlugin::~SalomeStylePlugin()
1186 {
1187 }
1188
1189 QStringList SalomeStylePlugin::keys() const
1190 {
1191   return QStringList() << "salome";
1192 }
1193
1194 QStyle* SalomeStylePlugin::create( const QString& str )
1195 {
1196   if ( str == "salome" )
1197     return new SalomeStyle();
1198   else
1199     return 0;
1200 }
1201
1202 Q_EXPORT_PLUGIN( SalomeStylePlugin )