1 /*	Copyright 2008 Filipe Azevedo <pasnox@gmail.com>
2    Copyright 2013 Theo Berkau <cwx@cyberwarriorx.com>
3 
4         This file is part of Yabause.
5 
6         Yabause is free software; you can redistribute it and/or modify
7         it under the terms of the GNU General Public License as published by
8         the Free Software Foundation; either version 2 of the License, or
9         (at your option) any later version.
10 
11         Yabause is distributed in the hope that it will be useful,
12         but WITHOUT ANY WARRANTY; without even the implied warranty of
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14         GNU General Public License for more details.
15 
16         You should have received a copy of the GNU General Public License
17         along with Yabause; if not, write to the Free Software
18         Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19 */
20 #include "UIPadSetting.h"
21 #include "UIPortManager.h"
22 #include "../Settings.h"
23 
24 #include <QKeyEvent>
25 #include <QTimer>
26 #include <QStylePainter>
27 #include <QStyleOptionToolButton>
28 
29 // Make a parent class for all controller setting classes
30 
31 
UIControllerSetting(PerInterface_struct * core,uint port,uint pad,uint perType,QWidget * parent)32 UIControllerSetting::UIControllerSetting( PerInterface_struct* core, uint port, uint pad, uint perType, QWidget* parent )
33 	: QDialog( parent )
34 {
35 	Q_ASSERT( core );
36 
37 	mCore = core;
38 	mPort = port;
39 	mPad = pad;
40 	mPerType = perType;
41 	mTimer = new QTimer( this );
42 	mTimer->setInterval( 25 );
43 	curTb = NULL;
44 	mPadKey = 0;
45 	mlInfos = NULL;
46 	scanFlags = PERSF_ALL;
47 	QtYabause::retranslateWidget( this );
48 }
49 
~UIControllerSetting()50 UIControllerSetting::~UIControllerSetting()
51 {
52 }
53 
setInfos(QLabel * lInfos)54 void UIControllerSetting::setInfos(QLabel *lInfos)
55 {
56    mlInfos = lInfos;
57 }
58 
setScanFlags(u32 scanMask)59 void UIControllerSetting::setScanFlags(u32 scanMask)
60 {
61 	switch (mPerType)
62 	{
63 		case PERPAD:
64       case PERVIRTUALON:
65 			scanFlags = PERSF_KEY | PERSF_BUTTON | PERSF_HAT | PERSF_AXIS;
66 			break;
67 		case PERWHEEL:
68 		case PERMISSIONSTICK:
69 		case PER3DPAD:
70 		case PERTWINSTICKS:
71 			scanFlags = PERSF_KEY | PERSF_BUTTON | PERSF_HAT | PERSF_AXIS;
72 			break;
73 		case PERGUN:
74 			scanFlags = PERSF_KEY | PERSF_BUTTON | PERSF_MOUSEMOVE;
75 			break;
76 		case PERKEYBOARD:
77 			scanFlags = PERSF_KEY;
78 			break;
79 		case PERMOUSE:
80 			scanFlags = PERSF_KEY | PERSF_BUTTON | PERSF_HAT | PERSF_MOUSEMOVE;
81 			break;
82 		default:
83 			scanFlags = PERSF_ALL;
84 			break;
85 	}
86 
87 	scanFlags &= scanMask;
88 	setMouseTracking(scanFlags & PERSF_MOUSEMOVE ? true : false);
89 }
90 
keyPressEvent(QKeyEvent * e)91 void UIControllerSetting::keyPressEvent( QKeyEvent* e )
92 {
93 	if ( mTimer->isActive() )
94 	{
95 		if ( e->key() != Qt::Key_Escape )
96       {
97 			setPadKey( e->key() );
98 		}
99 		else
100 		{
101 			e->ignore();
102 			mButtons.key( mPadKey )->setChecked( false );
103 			mlInfos->clear();
104 			mTimer->stop();
105 			curTb->setAttribute(Qt::WA_TransparentForMouseEvents, false);
106 		}
107 	}
108 	else if ( e->key() == Qt::Key_Escape )
109 	{
110 		reject();
111 	}
112 	else
113 	{
114 		QWidget::keyPressEvent( e );
115 	}
116 }
117 
mouseMoveEvent(QMouseEvent * e)118 void UIControllerSetting::mouseMoveEvent( QMouseEvent * e )
119 {
120 	if ( mTimer->isActive() )
121 	{
122 		if (scanFlags & PERSF_MOUSEMOVE)
123 			setPadKey((1 << 30));
124 	}
125 	else
126 		QWidget::mouseMoveEvent( e );
127 }
128 
mousePressEvent(QMouseEvent * e)129 void UIControllerSetting::mousePressEvent( QMouseEvent * e )
130 {
131 	if ( mTimer->isActive() )
132 	{
133 		if (scanFlags & PERSF_BUTTON)
134 			setPadKey( (1 << 31) | e->button() );
135 	}
136 	else
137 		QWidget::mousePressEvent( e );
138 }
139 
setPadKey(u32 key)140 void UIControllerSetting::setPadKey( u32 key )
141 {
142 	Q_ASSERT( mlInfos );
143 
144 	const QString settingsKey = QString( UIPortManager::mSettingsKey )
145 		.arg( mPort )
146 		.arg( mPad )
147 		.arg( mPerType )
148 		.arg( mPadKey );
149 
150 	QtYabause::settings()->setValue( settingsKey, (quint32)key );
151 	mButtons.key( mPadKey )->setIcon( QIcon( ":/actions/icons/actions/button_ok.png" ) );
152 	mButtons.key( mPadKey )->setChecked( false );
153 	mlInfos->clear();
154 	mTimer->stop();
155 	if (curTb)
156 	   curTb->setAttribute(Qt::WA_TransparentForMouseEvents, false);
157 }
158 
loadPadSettings()159 void UIControllerSetting::loadPadSettings()
160 {
161 	Settings* settings = QtYabause::settings();
162 
163 	foreach ( const u8& name, mNames.keys() )
164 	{
165 		mPadKey = name;
166 		const QString settingsKey = QString( UIPortManager::mSettingsKey )
167 			.arg( mPort )
168 			.arg( mPad )
169 			.arg( mPerType )
170 			.arg( mPadKey );
171 
172 		if ( settings->contains( settingsKey ) )
173 		{
174 			setPadKey( settings->value( settingsKey ).toUInt() );
175 		}
176 	}
177 }
178 
eventFilter(QObject * object,QEvent * event)179 bool UIControllerSetting::eventFilter( QObject* object, QEvent* event )
180 {
181 	if ( event->type() == QEvent::Paint )
182 	{
183 		QToolButton* tb = qobject_cast<QToolButton*>( object );
184 
185 		if ( tb )
186 		{
187 			if ( tb->isChecked() )
188 			{
189 				QStylePainter sp( tb );
190 				QStyleOptionToolButton options;
191 
192 				options.initFrom( tb );
193 				options.arrowType = Qt::NoArrow;
194 				options.features = QStyleOptionToolButton::None;
195 				options.icon = tb->icon();
196 				options.iconSize = tb->iconSize();
197 				options.state = QStyle::State_Enabled | QStyle::State_HasFocus | QStyle::State_On | QStyle::State_AutoRaise;
198 
199 				sp.drawComplexControl( QStyle::CC_ToolButton, options );
200 
201 				return true;
202 			}
203 		}
204 	}
205 
206 	return false;
207 }
208 
tbButton_clicked()209 void UIControllerSetting::tbButton_clicked()
210 {
211 	QToolButton* tb = qobject_cast<QToolButton*>( sender() );
212 
213 	if ( !mTimer->isActive() )
214 	{
215 		tb->setChecked( true );
216 		mPadKey = mButtons[ tb ];
217 
218 		QString text1 = QtYabause::translate(QString("Awaiting input for"));
219 		QString text2 = QtYabause::translate(mNames[ mPadKey ]);
220 		QString text3 = QtYabause::translate(QString("Press Esc key to cancel"));
221 
222 		mlInfos->setText( text1 + QString(": %1\n").arg(text2) + text3 );
223 		setScanFlags(mScanMasks[mPadKey]);
224 		mCore->Flush();
225 		curTb=tb;
226 		tb->setAttribute(Qt::WA_TransparentForMouseEvents);
227 		mTimer->start();
228 	}
229 	else
230 	{
231 		tb->setChecked( tb == mButtons.key( mPadKey ) );
232 	}
233 }
234 
timer_timeout()235 void UIControllerSetting::timer_timeout()
236 {
237 	u32 key = 0;
238 	key = mCore->Scan(scanFlags);
239 
240 	if ( key != 0 )
241 	{
242 		setPadKey( key );
243 	}
244 }
245