summaryrefslogtreecommitdiffstats
path: root/examples/themes/metal.cpp
blob: 4a8bac80e3d5b67e3ca0fd57b8032afd77f3dd98 (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
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
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "metal.h"

#ifndef QT_NO_STYLE_WINDOWS

#include "qapplication.h"
#include "qcombobox.h"
#include "qpainter.h"
#include "qdrawutil.h" // for now
#include "qpixmap.h" // for now
#include "qpalette.h" // for now
#include "qwidget.h"
#include "qlabel.h"
#include "qimage.h"
#include "qpushbutton.h"
#include "qwidget.h"
#include "qrangecontrol.h"
#include "qscrollbar.h"
#include "qslider.h"
#include <limits.h>


/////////////////////////////////////////////////////////
//#include "stonedark.xpm"
#include "stone1.xpm"
#include "marble.xpm"
///////////////////////////////////////////////////////



MetalStyle::MetalStyle() : QWindowsStyle() { }

/*!
  Reimplementation from QStyle
 */
void MetalStyle::applicationPolish( const QStyleControlElementData &ceData, ControlElementFlags elementFlags, void *ptr )
{
	oldPalette = ceData.palette;
	
	// we simply create a nice QColorGroup with a couple of fancy
	// pixmaps here and apply to it all widgets
	
	QFont f("times", ceData.font.pointSize() );
	f.setBold( TRUE );
	f.setItalic( TRUE );
	applicationActionRequest(ceData, elementFlags, ptr, AAR_SetFont, QStyleApplicationActionRequestData(f, TRUE, "QMenuBar"));
	applicationActionRequest(ceData, elementFlags, ptr, AAR_SetFont, QStyleApplicationActionRequestData(f, TRUE, "QPopupMenu"));
	
	//    QPixmap button( stonedark_xpm );
	
	QColor gold("#B9B9A5A54040"); //same as topgrad below
	QPixmap button( 1, 1 ); button.fill( gold );
	
	QPixmap background(marble_xpm);
	QPixmap dark( 1, 1 ); dark.fill( red.dark() );
	QPixmap mid( stone1_xpm );
	QPixmap light( stone1_xpm );//1, 1 ); light.fill( green );
	
	QPalette op = ceData.palette;
	
	QColor backCol( 227,227,227 );
	
	// QPalette op(white);
	QColorGroup active (op.active().foreground(),
			QBrush(op.active().button(),button),
			QBrush(op.active().light(), light),
			QBrush(op.active().dark(), dark),
			QBrush(op.active().mid(), mid),
			op.active().text(),
			Qt::white,
			op.active().base(),//		     QColor(236,182,120),
			QBrush(backCol, background)
			);
	active.setColor( QColorGroup::ButtonText,  Qt::white  );
	active.setColor( QColorGroup::Shadow,  Qt::black  );
	QColorGroup disabled (op.disabled().foreground(),
			QBrush(op.disabled().button(),button),
			QBrush(op.disabled().light(), light),
			op.disabled().dark(),
			QBrush(op.disabled().mid(), mid),
			op.disabled().text(),
			Qt::white,
			op.disabled().base(),//		     QColor(236,182,120),
			QBrush(backCol, background)
			);
	
	QPalette newPalette( active, disabled, active );
	applicationActionRequest(ceData, elementFlags, ptr, AAR_SetPalette, QStyleApplicationActionRequestData(newPalette, TRUE));
}

/*!
  Reimplementation from QStyle
 */
void MetalStyle::applicationUnPolish( const QStyleControlElementData &ceData, ControlElementFlags elementFlags, void *ptr )
{
	applicationActionRequest(ceData, elementFlags, ptr, AAR_SetPalette, QStyleApplicationActionRequestData(oldPalette, TRUE));
	applicationActionRequest(ceData, elementFlags, ptr, AAR_SetFont, QStyleApplicationActionRequestData(ceData.font, TRUE));
}

/*!
  Reimplementation from QStyle
 */
void MetalStyle::polish( const QStyleControlElementData &ceData, ControlElementFlags elementFlags, void *ptr )
{
	// the polish function sets some widgets to transparent mode and
	// some to translate background mode in order to get the full
	// benefit from the nice pixmaps in the color group.

	if (ceData.widgetObjectTypes.contains("QPushButton")) {
		widgetActionRequest(ceData, elementFlags, ptr, WAR_SetBackgroundMode, QStyleWidgetActionRequestData(QWidget::NoBackground));
		return;
	}
	
	if ( !(elementFlags & CEF_IsTopLevel) ) {
		if ( !ceData.bgPixmap.isNull() ) {
			widgetActionRequest(ceData, elementFlags, ptr, WAR_SetBackgroundOrigin, QStyleWidgetActionRequestData(QWidget::WindowOrigin));
		}
	}
}

void MetalStyle::unPolish( const QStyleControlElementData &ceData, ControlElementFlags elementFlags, void *ptr )
{
	// the polish function sets some widgets to transparent mode and
	// some to translate background mode in order to get the full
	// benefit from the nice pixmaps in the color group.
	
	if (ceData.widgetObjectTypes.contains("QPushButton")) {
		widgetActionRequest(ceData, elementFlags, ptr, WAR_SetBackgroundMode, QStyleWidgetActionRequestData(QWidget::PaletteButton));
		return;
	}
	if ( !(elementFlags & CEF_IsTopLevel) ) {
		if ( !ceData.bgPixmap.isNull() ) {
			widgetActionRequest(ceData, elementFlags, ptr, WAR_SetBackgroundOrigin, QStyleWidgetActionRequestData(QWidget::WidgetOrigin));
		}
	}
}

void MetalStyle::drawPrimitive( PrimitiveElement pe,
				QPainter *p,
				const QStyleControlElementData &ceData,
				ControlElementFlags elementFlags,
				const QRect &r,
				const QColorGroup &cg,
				SFlags flags, const QStyleOption& opt ) const
{
    switch( pe ) {
    case PE_HeaderSection:
	if ( flags & Style_Sunken )
	    flags ^= Style_Sunken | Style_Raised;
	// fall through
    case PE_ButtonBevel:
    case PE_ButtonCommand:
	    drawMetalButton( p, r.x(), r.y(), r.width(), r.height(),
			     (flags & (Style_Sunken|Style_On|Style_Down)),
			     TRUE, !(flags & Style_Raised) );
	    break;
    case PE_PanelMenuBar:
	drawMetalFrame( p, r.x(), r.y(), r.width(), r.height() );
	break;
    case PE_ScrollBarAddLine:	
	drawMetalButton( p, r.x(), r.y(), r.width(), r.height(),
			 flags & Style_Down, !( flags & Style_Horizontal ) );
	drawPrimitive( (flags & Style_Horizontal) ? PE_ArrowRight :PE_ArrowDown,
		       p, ceData, elementFlags, r, cg, flags, opt );
	break;
    case PE_ScrollBarSubLine:
	drawMetalButton( p, r.x(), r.y(), r.width(), r.height(),
			 flags & Style_Down, !( flags & Style_Horizontal ) );
	drawPrimitive( (flags & Style_Horizontal) ? PE_ArrowLeft : PE_ArrowUp,
		       p, ceData, elementFlags, r, cg, flags, opt );
	break;

	
    case PE_ScrollBarSlider:
	drawMetalButton( p, r.x(), r.y(), r.width(), r.height(), FALSE,
			 flags & Style_Horizontal );
	break;
    default:
	QWindowsStyle::drawPrimitive( pe, p, ceData, elementFlags, r, cg, flags, opt );
	break;
    }
}

void MetalStyle::drawControl( ControlElement element,
			      QPainter *p,
			      const QStyleControlElementData &ceData,
			      ControlElementFlags elementFlags,
			      const QRect &r,
			      const QColorGroup &cg,
			      SFlags how,
			      const QStyleOption& opt,
			      const QWidget *widget ) const
{
    switch( element ) {
    case CE_PushButton:
	{
	    int x1, y1, x2, y2;
	
	    r.coords( &x1, &y1, &x2, &y2 );
	
	    p->setPen( cg.foreground() );
	    p->setBrush( QBrush(cg.button(), NoBrush) );

	
	    QBrush fill;
	    if ( elementFlags & CEF_IsDown )
		fill = cg.brush( QColorGroup::Mid );
	    else if ( elementFlags & CEF_IsOn )
		fill = QBrush( cg.mid(), Dense4Pattern );
	    else
		fill = cg.brush( QColorGroup::Button );
	
	    if ( elementFlags & CEF_IsDefault ) {
		QPointArray a;
		a.setPoints( 9,
			     x1, y1, x2, y1, x2, y2, x1, y2, x1, y1+1,
			     x2-1, y1+1, x2-1, y2-1, x1+1, y2-1, x1+1, y1+1 );
		p->setPen( Qt::black );
		p->drawPolyline( a );
		x1 += 2;
		y1 += 2;
		x2 -= 2;
		y2 -= 2;
	    }
	    SFlags flags = Style_Default;
	    if ( elementFlags & CEF_IsOn )
		flags |= Style_On;
	    if ( elementFlags & CEF_IsDown )
		flags |= Style_Down;
	    if ( !(elementFlags & CEF_IsFlat) && !(elementFlags & CEF_IsDown) )
		flags |= Style_Raised;
	    drawPrimitive( PE_ButtonCommand, p, ceData, elementFlags,
			   QRect( x1, y1, x2 - x1 + 1, y2 - y1 + 1),
			   cg, flags, opt );
	
	    if ( (elementFlags & CEF_IsMenuWidget) ) {
		flags = Style_Default;
		if ( elementFlags & CEF_IsEnabled )
		    flags |= Style_Enabled;
		
		int dx = ( y1 - y2 - 4 ) / 3;
		drawPrimitive( PE_ArrowDown, p, ceData, elementFlags,
			       QRect(x2 - dx, dx, y1, y2 - y1),
			       cg, flags, opt );
	    }
	    if ( p->brush().style() != NoBrush )
		p->setBrush( NoBrush );
	    break;
	}
    case CE_PushButtonLabel:
	{
	    int x, y, w, h;
	    r.rect( &x, &y, &w, &h );
	
	    int x1, y1, x2, y2;
	    r.coords( &x1, &y1, &x2, &y2 );
	    int dx = 0;
	    int dy = 0;
	    if ( (elementFlags & CEF_IsMenuWidget) )
		dx = ( y2 - y1 ) / 3;
	    if ( (elementFlags & CEF_IsOn) || (elementFlags & CEF_IsDown) ) {
		dx--;
		dy--;
	    }
	    if ( dx || dy )
		p->translate( dx, dy );
	    x += 2;
	    y += 2;
	    w -= 4;
	    h -= 4;
	    drawItem( p, QRect( x, y, w, h ),
		      AlignCenter|ShowPrefix,
		      cg, (elementFlags & CEF_IsEnabled),
		      (ceData.fgPixmap.isNull())?NULL:&ceData.fgPixmap, ceData.text, -1,
		      ((elementFlags & CEF_IsDown) || (elementFlags & CEF_IsOn))? &cg.brightText() : &cg.buttonText() );
	    if ( dx || dy )
		p->translate( -dx, -dy );
	    break;
	}
    default:
	QWindowsStyle::drawControl( element, p, ceData, elementFlags, r, cg, how, opt, widget );
	break;
    }
}
void MetalStyle::drawComplexControl( ComplexControl cc,
				     QPainter *p,
				     const QStyleControlElementData &ceData,
				     ControlElementFlags elementFlags,
				     const QRect &r,
				     const QColorGroup &cg,
				     SFlags how,
				     SCFlags sub,
				     SCFlags subActive,
				     const QStyleOption& opt,
				     const QWidget *widget ) const
{
    switch ( cc ) {
    case CC_Slider:
	{
	    QRect handle = querySubControlMetrics( CC_Slider, ceData, elementFlags,
						   SC_SliderHandle, opt, widget);
	    if ( sub & SC_SliderGroove )
		QWindowsStyle::drawComplexControl( cc, p, ceData, elementFlags, r, cg, how,
						   SC_SliderGroove, subActive, opt, widget );
	    if ( (sub & SC_SliderHandle) && handle.isValid() )
		drawMetalButton( p, handle.x(), handle.y(), handle.width(),
				 handle.height(), FALSE,
				 ceData.orientation == QSlider::Horizontal);
	    break;
	}
    case CC_ComboBox:
	{
	    qDrawWinPanel( p, r.x(), r.y(), r.width(), r.height(), cg, TRUE,
			   (elementFlags & CEF_IsEnabled) ? &cg.brush( QColorGroup::Base ) :
			                      &cg.brush( QColorGroup::Background ) );
	    drawMetalButton( p, r.x() + r.width() - 2 - 16, r.y() + 2, 16, r.height() - 4,
			     how & Style_Sunken, TRUE );
	    drawPrimitive( PE_ArrowDown, p, ceData, elementFlags,
			   QRect( r.x() + r.width() - 2 - 16 + 2,
				  r.y() + 2 + 2, 16 - 4, r.height() - 4 -4 ),
			   cg,
			   (elementFlags & CEF_IsEnabled) ? Style_Enabled : Style_Default,
			   opt );
	    break;
	}
    default:
	QWindowsStyle::drawComplexControl( cc, p, ceData, elementFlags, r, cg, how, sub, subActive,
					   opt, widget );
	break;
    }
}
		

/*!
  Draw a metallic button, sunken if \a sunken is TRUE, horizontal if
  /a horz is TRUE.
*/

void MetalStyle::drawMetalButton( QPainter *p, int x, int y, int w, int h,
				  bool sunken, bool horz, bool flat  ) const
{

    drawMetalFrame( p, x, y, w, h );
    drawMetalGradient( p, x, y, w, h, sunken, horz, flat );
}




void MetalStyle::drawMetalFrame( QPainter *p, int x, int y, int w, int h ) const
{
    QColor top1("#878769691515");
    QColor top2("#C6C6B4B44949");

    QColor bot2("#70705B5B1414");
    QColor bot1("#56564A4A0E0E"); //first from the bottom


    int x2 = x + w - 1;
    int y2 = y + h - 1;

    //frame:

    p->setPen( top1 );
    p->drawLine( x, y2, x, y );
    p->drawLine( x, y, x2-1, y );
    p->setPen( top2 );
    p->drawLine( x+1, y2 -1, x+1, y+1 );
    p->drawLine( x+1, y+1 , x2-2, y+1 );

    p->setPen( bot1 );
    p->drawLine( x+1, y2, x2, y2 );
    p->drawLine( x2, y2, x2, y );
    p->setPen( bot2 );
    p->drawLine( x+1, y2-1, x2-1, y2-1 );
    p->drawLine( x2-1, y2-1, x2-1, y+1 );


}


void MetalStyle::drawMetalGradient( QPainter *p, int x, int y, int w, int h,
				    bool sunken, bool horz, bool flat  ) const

{
    QColor highlight("#E8E8DDDD6565");
    QColor subh1("#CECEBDBD5151");
    QColor subh2("#BFBFACAC4545");

    QColor topgrad("#B9B9A5A54040");
    QColor botgrad("#89896C6C1A1A");



    if ( flat && !sunken ) {
	    p->fillRect( x + 2, y + 2, w - 4,h -4, topgrad );
    } else {
	// highlight:
	int i = 0;
	int x1 = x + 2;
	int y1 = y + 2;
	int x2 = x + w - 1;
	int y2 = y + h - 1;
	if ( horz )
	    x2 = x2 - 2;
	else
	    y2 = y2 - 2;
	
#define DRAWLINE if (horz) \
                    p->drawLine( x1, y1+i, x2, y1+i ); \
		 else \
                    p->drawLine( x1+i, y1, x1+i, y2 ); \
                 i++;

	if ( !sunken ) {
	    p->setPen( highlight );
	    DRAWLINE;
	    DRAWLINE;
	    p->setPen( subh1 );
	    DRAWLINE;
	    p->setPen( subh2 );
	    DRAWLINE;
	}
	// gradient:
	int ng = (horz ? h : w) - 8; // how many lines for the gradient?
	
	int h1, h2, s1, s2, v1, v2;
	if ( !sunken ) {
	    topgrad.hsv( &h1, &s1, &v1 );
	    botgrad.hsv( &h2, &s2, &v2 );
	} else {
	    botgrad.hsv( &h1, &s1, &v1 );
	    topgrad.hsv( &h2, &s2, &v2 );
	}
	
	if ( ng > 1 ) {	
	    for ( int j =0; j < ng; j++ ) {
		p->setPen( QColor( h1 + ((h2-h1)*j)/(ng-1),
				   s1 + ((s2-s1)*j)/(ng-1),
				   v1 + ((v2-v1)*j)/(ng-1),  QColor::Hsv ) );
		DRAWLINE;
	    }
	} else if ( ng == 1 ) {
	    p->setPen( QColor((h1+h2)/2, (s1+s2)/2, (v1+v2)/2, QColor::Hsv) );
	    DRAWLINE;
	}
	if ( sunken ) {
	    p->setPen( subh2 );
	    DRAWLINE;
	    
	    p->setPen( subh1 );
	    DRAWLINE;
	    
	    p->setPen( highlight );
	    DRAWLINE;
	    DRAWLINE;
	}
    }    
}



int MetalStyle::pixelMetric( PixelMetric metric, const QStyleControlElementData &ceData, ControlElementFlags elementFlags, const QWidget *w ) const
{
    switch ( metric ) {
    case PM_MenuBarFrameWidth:
	return 2;
    default:
	return QWindowsStyle::pixelMetric( metric, ceData, elementFlags, w );
    }
}

#endif