1 /*****************************************************************************
2 * customwidgets.cpp: Custom widgets
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
5 * Copyright (C) 2004 Daniel Molkentin <molkentin@kde.org>
8 * Authors: Clément Stenac <zorglub@videolan.org>
9 * The "ClickLineEdit" control is based on code by Daniel Molkentin
10 * <molkentin@kde.org> for libkdepim
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
31 #include "customwidgets.hpp"
32 #include "qt4.hpp" /*needed for qtr and CONNECT, but not necessary */
36 #include <QColorGroup>
39 #include <QWheelEvent>
40 #include <QToolButton>
41 #include <QHBoxLayout>
42 #include <vlc_intf_strings.h>
47 ClickLineEdit::ClickLineEdit( const QString &msg, QWidget *parent) : QLineEdit( parent )
50 setClickMessage( msg );
53 void ClickLineEdit::setClickMessage( const QString &msg )
60 void ClickLineEdit::setText( const QString &txt )
62 mDrawClickMsg = txt.isEmpty();
64 QLineEdit::setText( txt );
67 void ClickLineEdit::paintEvent( QPaintEvent *pe )
69 QLineEdit::paintEvent( pe );
70 if ( mDrawClickMsg == true && !hasFocus() ) {
73 p.setPen( palette().color( QPalette::Disabled, QPalette::Text ) );
74 QRect cr = contentsRect();
75 // Add two pixel margin on the left side
76 cr.setLeft( cr.left() + 3 );
77 p.drawText( cr, Qt::AlignLeft | Qt::AlignVCenter, mClickMessage );
83 void ClickLineEdit::dropEvent( QDropEvent *ev )
85 mDrawClickMsg = false;
86 QLineEdit::dropEvent( ev );
89 void ClickLineEdit::focusInEvent( QFocusEvent *ev )
91 if ( mDrawClickMsg == true ) {
92 mDrawClickMsg = false;
95 QLineEdit::focusInEvent( ev );
98 void ClickLineEdit::focusOutEvent( QFocusEvent *ev )
100 if ( text().isEmpty() ) {
101 mDrawClickMsg = true;
104 QLineEdit::focusOutEvent( ev );
107 SearchLineEdit::SearchLineEdit( QWidget *parent ) : QFrame( parent )
109 setFrameStyle( QFrame::WinPanel | QFrame::Sunken );
112 QHBoxLayout *frameLayout = new QHBoxLayout( this );
113 frameLayout->setMargin( 0 );
114 frameLayout->setSpacing( 0 );
117 QBrush brush( QColor(255, 255, 255, 255) );
118 brush.setStyle(Qt::SolidPattern);
119 palette.setBrush(QPalette::Active, QPalette::Window, brush); //Qt::white
122 setAutoFillBackground(true);
124 searchLine = new ClickLineEdit( qtr(I_PL_FILTER), 0 );
125 searchLine->setFrame( false );
126 searchLine->setMinimumWidth( 80 );
128 CONNECT( searchLine, textChanged( const QString ),
129 this, updateText( const QString ) );
130 frameLayout->addWidget( searchLine );
132 clearButton = new QToolButton;
133 clearButton->setAutoRaise( true );
134 clearButton->setMaximumWidth( 30 );
135 clearButton->setIcon( QIcon( ":/clear" ) );
136 clearButton->setToolTip( qtr( "Clear" ) );
139 CONNECT( clearButton, clicked(), searchLine, clear() );
140 frameLayout->addWidget( clearButton );
143 void SearchLineEdit::updateText( const QString text )
145 clearButton->setVisible( !text.isEmpty() );
146 emit textChanged( text );
149 /***************************************************************************
151 ***************************************************************************/
152 int qtKeyModifiersToVLC( QInputEvent* e )
154 int i_keyModifiers = 0;
155 if( e->modifiers() & Qt::ShiftModifier ) i_keyModifiers |= KEY_MODIFIER_SHIFT;
156 if( e->modifiers() & Qt::AltModifier ) i_keyModifiers |= KEY_MODIFIER_ALT;
157 if( e->modifiers() & Qt::ControlModifier ) i_keyModifiers |= KEY_MODIFIER_CTRL;
158 if( e->modifiers() & Qt::MetaModifier ) i_keyModifiers |= KEY_MODIFIER_META;
159 return i_keyModifiers;
162 int qtEventToVLCKey( QKeyEvent *e )
165 /* Handle modifiers */
166 i_vlck |= qtKeyModifiersToVLC( e );
169 /* Look for some special keys */
170 #define HANDLE( qt, vk ) case Qt::qt : i_vlck |= vk; found = true;break
173 HANDLE( Key_Left, KEY_LEFT );
174 HANDLE( Key_Right, KEY_RIGHT );
175 HANDLE( Key_Up, KEY_UP );
176 HANDLE( Key_Down, KEY_DOWN );
177 HANDLE( Key_Space, KEY_SPACE );
178 HANDLE( Key_Escape, KEY_ESC );
179 HANDLE( Key_Return, KEY_ENTER );
180 HANDLE( Key_Enter, KEY_ENTER );
181 HANDLE( Key_F1, KEY_F1 );
182 HANDLE( Key_F2, KEY_F2 );
183 HANDLE( Key_F3, KEY_F3 );
184 HANDLE( Key_F4, KEY_F4 );
185 HANDLE( Key_F5, KEY_F5 );
186 HANDLE( Key_F6, KEY_F6 );
187 HANDLE( Key_F7, KEY_F7 );
188 HANDLE( Key_F8, KEY_F8 );
189 HANDLE( Key_F9, KEY_F9 );
190 HANDLE( Key_F10, KEY_F10 );
191 HANDLE( Key_F11, KEY_F11 );
192 HANDLE( Key_F12, KEY_F12 );
193 HANDLE( Key_PageUp, KEY_PAGEUP );
194 HANDLE( Key_PageDown, KEY_PAGEDOWN );
195 HANDLE( Key_Home, KEY_HOME );
196 HANDLE( Key_End, KEY_END );
197 HANDLE( Key_Insert, KEY_INSERT );
198 HANDLE( Key_Delete, KEY_DELETE );
199 HANDLE( Key_VolumeDown, KEY_VOLUME_DOWN);
200 HANDLE( Key_VolumeUp, KEY_VOLUME_UP );
201 HANDLE( Key_VolumeMute, KEY_VOLUME_MUTE );
202 HANDLE( Key_MediaPlay, KEY_MEDIA_PLAY_PAUSE );
203 HANDLE( Key_MediaStop, KEY_MEDIA_STOP );
204 HANDLE( Key_MediaPrevious, KEY_MEDIA_PREV_TRACK );
205 HANDLE( Key_MediaNext, KEY_MEDIA_NEXT_TRACK );
210 /* Force lowercase */
211 if( e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z )
212 i_vlck += e->key() + 32;
213 /* Rest of the ascii range */
214 else if( e->key() >= Qt::Key_Space && e->key() <= Qt::Key_AsciiTilde )
220 int qtWheelEventToVLCKey( QWheelEvent *e )
223 /* Handle modifiers */
224 i_vlck |= qtKeyModifiersToVLC( e );
225 if ( e->delta() > 0 )
226 i_vlck |= KEY_MOUSEWHEELUP;
228 i_vlck |= KEY_MOUSEWHEELDOWN;
232 QString VLCKeyToString( int val )
234 const char *base = KeyToString (val & ~KEY_MODIFIER);
237 if( val & KEY_MODIFIER_CTRL )
239 if( val & KEY_MODIFIER_ALT )
241 if( val & KEY_MODIFIER_SHIFT )
244 return r + (base ? base : "Unset");