Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

buttonsettings.cpp

Go to the documentation of this file.
00001 /*
00002        =.            This file is part of the OPIE Project
00003       .=l.            Copyright (c)  2002 Robert Griebl <sandman@handhelds.org>
00004      .>+-=
00005 _;:,   .>  :=|.         This file is free software; you can
00006 .> <`_,  > .  <=          redistribute it and/or modify it under
00007 :`=1 )Y*s>-.--  :           the terms of the GNU General Public
00008 .="- .-=="i,   .._         License as published by the Free Software
00009 - .  .-<_>   .<>         Foundation; either version 2 of the License,
00010   ._= =}    :          or (at your option) any later version.
00011   .%`+i>    _;_.
00012   .i_,=:_.   -<s.       This file is distributed in the hope that
00013   + . -:.    =       it will be useful, but WITHOUT ANY WARRANTY;
00014   : ..  .:,   . . .    without even the implied warranty of
00015   =_    +   =;=|`    MERCHANTABILITY or FITNESS FOR A
00016  _.=:.    :  :=>`:     PARTICULAR PURPOSE. See the GNU General
00017 ..}^=.=    =    ;      Public License for more details.
00018 ++=  -.   .`   .:
00019 :   = ...= . :.=-        You should have received a copy of the GNU
00020 -.  .:....=;==+<;          General Public License along with this file;
00021  -_. . .  )=. =           see the file COPYING. If not, write to the
00022   --    :-=`           Free Software Foundation, Inc.,
00023                              59 Temple Place - Suite 330,
00024                              Boston, MA 02111-1307, USA.
00025 
00026 */
00027 
00028 #include "buttonsettings.h"
00029 #include "buttonutils.h"
00030 #include "remapdlg.h"
00031 
00032 #include <opie2/odevice.h>
00033 
00034 #include <qpe/applnk.h>
00035 
00036 #include <qlayout.h>
00037 #include <qlabel.h>
00038 #include <qtimer.h>
00039 #include <qscrollview.h>
00040 #include <qcopchannel_qws.h>
00041 
00042 using namespace Opie::Core;
00043 
00044 struct buttoninfo {
00045     const ODeviceButton *m_button;
00046     int          m_index;
00047 
00048     OQCopMessage m_pmsg;
00049     QLabel      *m_picon;
00050     QLabel      *m_plabel;
00051 
00052     OQCopMessage m_hmsg;
00053     QLabel      *m_hicon;
00054     QLabel      *m_hlabel;
00055 
00056     bool        m_pdirty : 1;
00057     bool        m_hdirty : 1;
00058 };
00059 
00060 
00061 ButtonSettings::ButtonSettings ( QWidget *parent , const char *,  bool, WFlags  )
00062     : QDialog ( parent, "ButtonSettings", false, WStyle_ContextHelp )
00063 {
00064     const QValueList <ODeviceButton> &buttons = ODevice::inst ( )-> buttons ( );
00065     (void) ButtonUtils::inst ( ); // initialise
00066 
00067     setCaption ( tr( "Button Settings" ));
00068 
00069     QVBoxLayout *toplay = new QVBoxLayout ( this, 3, 3 );
00070     QScrollView*mainview = new QScrollView(this);
00071     mainview->setResizePolicy( QScrollView::AutoOneFit );
00072 
00073     toplay->addWidget(mainview);
00074     QFrame*framehold=new QFrame(mainview->viewport());
00075     framehold->setFrameStyle(QFrame::NoFrame|QFrame::Plain);
00076     mainview->addChild(framehold);
00077     toplay = new QVBoxLayout (framehold, 3, 3 );
00078 
00079     QLabel *l = new QLabel ( tr( "<center>Press or hold the button you want to remap.</center>" ), framehold );
00080     toplay-> addWidget ( l );
00081 
00082     QGridLayout *lay = new QGridLayout ( toplay );
00083     lay-> setMargin ( 0 );
00084     lay-> setColStretch ( 0, 0 );
00085     lay-> setColStretch ( 1, 0 );
00086     lay-> setColStretch ( 2, 0 );
00087     lay-> setColStretch ( 3, 10 );
00088 
00089     m_infos. setAutoDelete ( true );
00090 
00091     int i = 1;
00092     int index = 0;
00093     for ( QValueList<ODeviceButton>::ConstIterator it = buttons. begin ( ); it != buttons. end ( ); it++ ) {
00094         if ( it != buttons. begin ( )) {
00095             QFrame *f = new QFrame ( framehold );
00096             f-> setFrameStyle ( QFrame::Sunken | QFrame::VLine );
00097             lay-> addMultiCellWidget ( f, i, i, 0, 3 );
00098             i++;
00099         }
00100 
00101         buttoninfo *bi = new buttoninfo ( );
00102         bi-> m_button = &(*it);
00103         bi-> m_index = index++;
00104         bi-> m_pmsg = (*it). pressedAction ( );
00105         bi-> m_hmsg = (*it). heldAction ( );
00106         bi-> m_pdirty = false;
00107         bi-> m_hdirty = false;
00108 
00109         l = new QLabel ( framehold );
00110         l-> setPixmap (( *it ). pixmap ( ));
00111 
00112         lay-> addMultiCellWidget ( l, i, i + 1, 0, 0 );
00113 
00114         l = new QLabel ( tr( "Press:" ), framehold );
00115         lay-> addWidget ( l, i, 1, AlignLeft | AlignBottom );
00116         l = new QLabel ( tr( "Hold:" ), framehold );
00117         lay-> addWidget ( l, i + 1, 1, AlignLeft | AlignTop );
00118 
00119         l = new QLabel ( framehold );
00120         l-> setFixedSize ( AppLnk::smallIconSize(), AppLnk::smallIconSize() );
00121         lay-> addWidget ( l, i, 2, AlignLeft | AlignBottom );
00122         bi-> m_picon = l;
00123 
00124         l = new QLabel ( framehold );
00125         l-> setAlignment ( AlignLeft | AlignVCenter | SingleLine );
00126         lay-> addWidget ( l, i, 3, AlignLeft | AlignBottom );
00127         bi-> m_plabel = l;
00128 
00129         l = new QLabel ( framehold );
00130         l-> setFixedSize ( AppLnk::smallIconSize(), AppLnk::smallIconSize() );
00131         lay-> addWidget ( l, i + 1, 2, AlignLeft | AlignTop );
00132         bi-> m_hicon = l;
00133 
00134         l = new QLabel ( framehold );
00135         l-> setAlignment ( AlignLeft | AlignVCenter | SingleLine );
00136         lay-> addWidget ( l, i + 1, 3, AlignLeft | AlignTop );
00137         bi-> m_hlabel = l;
00138 
00139         i += 2;
00140 
00141         m_infos. append ( bi );
00142     }
00143 
00144     toplay-> addStretch ( 10 );
00145 
00146     m_last_button = 0;
00147     m_lock = false;
00148 
00149     m_timer = new QTimer ( this );
00150     connect ( m_timer, SIGNAL( timeout()), this, SLOT( keyTimeout()));
00151 
00152     updateLabels ( );
00153 
00154     QPEApplication::grabKeyboard ( );
00155 }
00156 
00157 ButtonSettings::~ButtonSettings ( )
00158 {
00159     QPEApplication::ungrabKeyboard ( );
00160     QCopChannel::send ("QPE/System", "deviceButtonMappingChanged()" );
00161 }
00162 
00163 void ButtonSettings::updateLabels ( )
00164 {
00165     for ( QListIterator <buttoninfo> it ( m_infos ); *it; ++it ) {
00166         qCopInfo cip = ButtonUtils::inst ( )-> messageToInfo ((*it)-> m_pmsg );
00167 
00168         QPixmap pic;
00169         pic.convertFromImage( cip.m_icon.convertToImage().smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) );
00170         (*it)-> m_picon-> setPixmap ( pic );
00171         (*it)-> m_plabel-> setText ( cip. m_name );
00172 
00173         qCopInfo cih = ButtonUtils::inst ( )-> messageToInfo ((*it)-> m_hmsg );
00174 
00175         pic.convertFromImage( cih.m_icon.convertToImage().smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) );
00176         (*it)-> m_hicon-> setPixmap ( pic );
00177         (*it)-> m_hlabel-> setText ( cih. m_name );
00178     }
00179 }
00180 
00181 buttoninfo *ButtonSettings::buttonInfoForKeycode ( ushort key )
00182 {
00183     for ( QListIterator <buttoninfo> it ( m_infos ); *it; ++it ) {
00184         if ((*it)-> m_button-> keycode ( ) == key )
00185             return *it;
00186     }
00187     return 0;
00188 }
00189 
00190 void ButtonSettings::keyPressEvent ( QKeyEvent *e )
00191 {
00192     buttoninfo *bi = buttonInfoForKeycode ( e-> key ( ));
00193 
00194     if ( bi && !e-> isAutoRepeat ( )) {
00195         m_timer-> stop ( );
00196         m_last_button = bi;
00197         m_timer-> start ( ODevice::inst ( )-> buttonHoldTime ( ), true );
00198     }
00199     else
00200         QDialog::keyPressEvent ( e );
00201 }
00202 
00203 void ButtonSettings::keyReleaseEvent ( QKeyEvent *e )
00204 {
00205     buttoninfo *bi = buttonInfoForKeycode ( e-> key ( ));
00206 
00207     if ( bi && !e-> isAutoRepeat ( ) && m_timer-> isActive ( )) {
00208         m_timer-> stop ( );
00209         edit ( bi, false );
00210     }
00211     else
00212         QDialog::keyReleaseEvent ( e );
00213 }
00214 
00215 void ButtonSettings::keyTimeout ( )
00216 {
00217     if ( m_last_button ) {
00218         edit ( m_last_button, true );
00219         m_last_button = false;
00220     }
00221 }
00222 
00223 void ButtonSettings::edit ( buttoninfo *bi, bool hold )
00224 {
00225 
00226     if ( m_lock )
00227         return;
00228     m_lock = true;
00229 
00230     RemapDlg *d = new RemapDlg ( bi-> m_button, hold, this );
00231 
00232     if ( QPEApplication::execDialog ( d ) == QDialog::Accepted ) {
00233 
00234 
00235         if ( hold ) {
00236             bi-> m_hmsg = d-> message ( );
00237             bi-> m_hdirty = true;
00238         }
00239         else {
00240             bi-> m_pmsg = d-> message ( );
00241             bi-> m_pdirty = true;
00242         }
00243 
00244         updateLabels ( );
00245     }
00246 
00247     delete d;
00248 
00249     m_lock = false;
00250 }
00251 
00252 void ButtonSettings::accept ( )
00253 {
00254     for ( QListIterator <buttoninfo> it ( m_infos ); *it; ++it ) {
00255         buttoninfo *bi = *it;
00256 
00257         if ( bi-> m_pdirty )
00258             ODevice::inst ( )-> remapPressedAction ( bi-> m_index, bi-> m_pmsg );
00259         if ( bi-> m_hdirty )
00260             ODevice::inst ( )-> remapHeldAction ( bi-> m_index, bi-> m_hmsg );
00261     }
00262     QDialog::accept ( );
00263 }
00264 
00265 void ButtonSettings::done ( int r )
00266 {
00267     QDialog::done ( r );
00268     close ( );
00269 }

Generated on Sat Nov 5 16:15:56 2005 for OPIE by  doxygen 1.4.2