File: attalStyle.cpp

package info (click to toggle)
attal 1.0~rc2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 4,348 kB
  • ctags: 7,428
  • sloc: cpp: 55,101; sh: 267; ansic: 100; makefile: 54
file content (311 lines) | stat: -rw-r--r-- 9,695 bytes parent folder | download | duplicates (2)
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
/****************************************************************
**
** Attal : Lords of Doom
**
** attalStyle.cpp
** Manages style of client
**
** Version : $Id: attalStyle.cpp,v 1.11 2008/02/24 21:19:15 lusum Exp $
**
** Author(s) : Cyrille Verrier - Pascal Audoux
**
** Date : 17/08/2000
**
** Licence :
**	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, or (at your option)
**      any later version.
**
**	This program is distributed in the hope that it will be useful,
** 	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
****************************************************************/

#include "attalStyle.h"

// generic include files
#include <limits.h>

// include files for QT
#include <QApplication>
#include <QBoxLayout>
#include <QCheckBox>
#include <QColor>
#include <qdrawutil.h> // for now
#include <QFile>
#include <QImage>
#include <QLabel>
#include <QLineEdit>
#include <QPainter>
#include <QPolygon>
#include <QPushButton>
#include <QScrollBar>
#include <QStyleOption>
#include <QTextStream>
#include <QToolButton>
#include <QWidget>


// application specific include
#include "conf.h"

#include "libCommon/log.h"

#include "libClient/attalButton.h"
#include "libClient/gui.h"
#include "libClient/imageTheme.h"

extern QString IMAGE_PATH;

AttalStyle::AttalStyle( const QString & colorFile ) : QWindowsStyle()
{
	init( colorFile );
}

bool AttalStyle::init( const QString & colorFile )
{
	QFile f( colorFile );
	if (! f.open(QIODevice::ReadOnly) ) {
		logEE("Could not open file %s for reading\n", colorFile.toLatin1().constData() );
		return false;
	}
						
	QTextStream ts( &f );

	if( ts.atEnd() ) {
		return false;
	}
	int colR, colG, colB;
	ts >> colR;
	ts >> colG;
	ts >> colB;
	_textColor.setRgb( colR, colG, colB );
	ts >> colR;
	ts >> colG;
	ts >> colB;
	_buttonColor.setRgb( colR, colG, colB );
	ts >> colR;
	ts >> colG;
	ts >> colB;
	_borderColor.setRgb( colR, colG, colB );				

	f.close();

	backgroundImage = QPixmap( IMAGE_PATH + "misc/background.png" );

	return true;
}

void AttalStyle::polish( QPalette & palette )
{
	QColor brown(212, 140, 95);
	QColor beige(236, 182, 120);
	QColor slightlyOpaqueBlack(0, 0, 0, 63);

	palette = QPalette(brown);

	palette.setBrush( QPalette::BrightText, Qt::white);
	palette.setBrush( QPalette::Base, beige);		
	palette.setBrush( QPalette::AlternateBase, Qt::green ); /// XXX: it is a test...
	palette.setBrush( QPalette::Highlight, Qt::darkGray ); /// XXX: to improve 

	palette.setBrush( QPalette::Button, QBrush( brown, backgroundImage ) );

	palette.setBrush( QPalette::Text, _textColor ); // color in entry widget (lineedit, checkbox...)
	palette.setBrush( QPalette::ButtonText, _buttonColor ); // color in buttons
	palette.setBrush( QPalette::WindowText, _buttonColor ); // color in labels, checkbox label

	//palette.setBrush(QPalette::Disabled, QPalette::ButtonText, brush);
	//setTexture( palette, QPalette::Foreground, backgroundImage);
	setTexture( palette, QPalette::Background, backgroundImage);

	QBrush brush = palette.background();
	brush.setColor( brush.color().dark() );

	palette.setBrush(QPalette::Disabled, QPalette::Foreground, brush);
	palette.setBrush(QPalette::Disabled, QPalette::Text, brush);
	palette.setBrush(QPalette::Disabled, QPalette::ButtonText, brush);
	palette.setBrush(QPalette::Disabled, QPalette::Base, brush);
	palette.setBrush(QPalette::Disabled, QPalette::Button, brush);
	palette.setBrush(QPalette::Disabled, QPalette::Mid, brush);
}
    
void AttalStyle::setTexture( QPalette & palette, QPalette::ColorRole role, const QPixmap & pixmap )
{
	for( int i = 0; i < QPalette::NColorGroups; ++i ) {
		QColor color = palette.brush(QPalette::ColorGroup(i), role).color();
		palette.setBrush(QPalette::ColorGroup(i), role, QBrush(color, pixmap));
	}
}

void AttalStyle::polish( QWidget * w )
{
	// 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 (w->inherits("QTipLabel") || w->inherits("QLCDNumber") ){
		return;
	}

	if ( ! w->isTopLevel() ) {
		if ( w->inherits("QPushButton")
		     || w->inherits("QToolButton")
		     || w->inherits("QGroupBox")
		     || w->inherits("QTabWidget")
		     || w->inherits("QComboBox") ) {
			/// XXX: no more in q4 ? w->setAutoMask( TRUE );
			return;
		}
		/*
		if( w->inherits("QLabel") || w->inherits("QButton") ) {
			//w->setBackgroundOrigin( QWidget::ParentOrigin );
		}
		*/
	}
}

void AttalStyle::unPolish( QWidget * w )
{
	// 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 (w->inherits("QTipLabel") || w->inherits("QLCDNumber") ){
		return;
	}
	if ( !w->isTopLevel() ) {
		if ( w->inherits("QPushButton")
		     || w->inherits("QToolButton")
		     || w->inherits("QGroupBox")
		     || w->inherits("QTabWidget")
		     || w->inherits("QComboBox") ) {
			/// XXX: no more in q4 ? w->setAutoMask( FALSE );
			return;
		}
		/*
		if (w->inherits("QLabel")
		    || w->inherits("QButton") ) {
			w->setBackgroundOrigin( QWidget::WidgetOrigin );
		}
		*/
	}
}

/* function not used cause without this the graphic seem more faster
	 otherwise is the 'right' method to handle controls for qt, so for 
	 future use i will leave this one function (that work, btw)
*/

/*
void AttalStyle::drawControl(ControlElement element, const QStyleOption *option,QPainter *painter,const QWidget *widget) const
{
	    switch (element) {
				    case CE_PushButton:
							        {
												QStyleOptionButton myButtonOption;
												const QStyleOptionButton *buttonOption = qstyleoption_cast<const QStyleOptionButton *>(option);
												myButtonOption = *buttonOption;

												QColor brown(212, 140, 95);
												QColor beige(236, 182, 120);
												QColor slightlyOpaqueBlack(0, 0, 0, 63);

												QPixmap backgroundImage( IMAGE_PATH + "misc/background.png" );

												myButtonOption.palette = QPalette(brown);


												myButtonOption.palette.setBrush( QPalette::BrightText, Qt::white);
												myButtonOption.palette.setBrush( QPalette::Base, beige);		
												myButtonOption.palette.setBrush( QPalette::Window, beige);		
												myButtonOption.palette.setBrush( QPalette::AlternateBase, Qt::green ); /// XXX: it is a test...
												myButtonOption.palette.setBrush( QPalette::Highlight, Qt::darkGray ); /// XXX: to improve 

												myButtonOption.palette.setBrush( QPalette::Button, QBrush( brown, backgroundImage ) );

												myButtonOption.palette.setBrush( QPalette::Text, Qt::yellow ); // color in entry widget (lineedit, checkbox...)
												myButtonOption.palette.setBrush( QPalette::ButtonText, Qt::yellow ); // color in buttons
												myButtonOption.palette.setBrush( QPalette::WindowText, Qt::yellow ); // color in labels, checkbox label

												for( int i = 0; i < QPalette::NColorGroups; ++i ) {
													QColor color = myButtonOption.palette.brush(QPalette::ColorGroup(i), QPalette::Background).color();
													myButtonOption.palette.setBrush(QPalette::ColorGroup(i), QPalette::Background, QBrush(color, backgroundImage));
												}


												QBrush brush = myButtonOption.palette.background();
												brush.setColor( brush.color().dark() );

												myButtonOption.palette.setBrush(QPalette::Disabled, QPalette::Foreground, brush);
												myButtonOption.palette.setBrush(QPalette::Disabled, QPalette::Text, brush);
												myButtonOption.palette.setBrush(QPalette::Disabled, QPalette::ButtonText, brush);
												myButtonOption.palette.setBrush(QPalette::Disabled, QPalette::Base, brush);
												myButtonOption.palette.setBrush(QPalette::Disabled, QPalette::Button, brush);
												myButtonOption.palette.setBrush(QPalette::Disabled, QPalette::Mid, brush);
												QWindowsStyle::drawControl(element, &myButtonOption, painter, widget);

											}
											break;
						default:
											QWindowsStyle::drawControl(element, option, painter, widget);
			}

}
*/												

static inline int buttonthickness( int d )
{ 
	return  d > 20 ? 5 : ( d < 10 ? 2: 3 ); 
}

//
// ----- TestAttalStyle -----
//


TestAttalStyle::TestAttalStyle()
: QDialog( 0 )
{
	QPushButton * pb = new QPushButton( this );
	pb->setText( "test pushbutton" );
	FIXEDSIZE( pb );

	QPushButton * pbDisabled = new QPushButton( this );
	pbDisabled->setText( "test pushbutton disabled" );
	pbDisabled->setEnabled( false );
	FIXEDSIZE( pbDisabled );

	QLabel * lab = new QLabel( "test label", this );
	FIXEDSIZE( lab );

	QLineEdit * le = new QLineEdit( this );
	FIXEDSIZE( le );

	QCheckBox * cb = new QCheckBox( this );
	cb->setText( "Checkbox" );
	FIXEDSIZE( cb );

	QPushButton * pbImage = new QPushButton( this );
	pbImage->setFixedSize( 52, 42 );
	pbImage->setIconSize( ImageTheme.getLordSmallPixmap( 5 )->size() );
	pbImage->setIcon( * ImageTheme.getLordSmallPixmap( 5 ) );
	
	AttalButton * butLord = new AttalButton( this, AttalButton::BT_LORD );
	butLord->setLord( 5 );

	QVBoxLayout * layout = new QVBoxLayout( this );
	layout->setSpacing( 5 );
	layout->addWidget( pb );
	layout->addWidget( pbDisabled );
	layout->addWidget( lab );
	layout->addWidget( le );
	layout->addWidget( cb );
	layout->addWidget( pbImage );
	layout->addWidget( butLord );
	layout->activate();
}