kmail

snippetdlg.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   snippet feature from kdevelop/plugins/snippet/                        *
00003  *                                                                         * 
00004  *   Copyright (C) 2007 by Robert Gruber                                   *
00005  *   rgruber@users.sourceforge.net                                         *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  ***************************************************************************/
00013 
00014 #include "snippetdlg.h"
00015 
00016 #include <kdialog.h>
00017 #include <klocale.h>
00018 
00019 #include <qlabel.h>
00020 #include <qlayout.h>
00021 #include <kpushbutton.h>
00022 #include <ktextedit.h>
00023 #include "kkeybutton.h"
00024 #include "kactioncollection.h"
00025 #include "kmessagebox.h"
00026 
00027 /*
00028  *  Constructs a SnippetDlg as a child of 'parent', with the
00029  *  name 'name' and widget flags set to 'f'.
00030  *
00031  *  The dialog will by default be modeless, unless you set 'modal' to
00032  *  TRUE to construct a modal dialog.
00033  */
00034 SnippetDlg::SnippetDlg( KActionCollection* ac, QWidget* parent, const char* name, bool modal, WFlags fl )
00035     : SnippetDlgBase( parent, name, modal, fl ), actionCollection( ac )
00036 {
00037     if ( !name )
00038     setName( "SnippetDlg" );
00039 
00040     textLabel3 = new QLabel( this, "textLabel3" );
00041     keyButton = new KKeyButton( this );
00042     connect( keyButton, SIGNAL( capturedShortcut( const KShortcut& ) ),
00043              this, SLOT( slotCapturedShortcut( const KShortcut& ) ) );
00044 
00045     layout3->addWidget( textLabel3, 7, 0 );
00046     layout3->addWidget( keyButton, 7, 1 );
00047 
00048     // tab order
00049     setTabOrder( snippetText, keyButton );
00050     setTabOrder( keyButton, btnAdd );
00051     setTabOrder( btnAdd, btnCancel );
00052 
00053     textLabel3->setBuddy( keyButton );
00054     languageChange();
00055 }
00056 
00057 /*
00058  *  Destroys the object and frees any allocated resources
00059  */
00060 SnippetDlg::~SnippetDlg()
00061 {
00062     // no need to delete child widgets, Qt does it all for us
00063 }
00064 
00065 /*
00066  *  Sets the strings of the subwidgets using the current
00067  *  language.
00068  */
00069 void SnippetDlg::languageChange()
00070 {
00071     textLabel3->setText( tr2i18n( "Sh&ortcut:" ) );
00072 }
00073 
00074 static bool shortcutIsValid( const KActionCollection* actionCollection, const KShortcut &sc )
00075 {
00076   KActionPtrList actions = actionCollection->actions();
00077   KActionPtrList::Iterator it( actions.begin() );
00078   for ( ; it != actions.end(); it++ ) {
00079     if ( (*it)->shortcut() == sc ) return false;
00080   }
00081   return true;
00082 }
00083 
00084 void SnippetDlg::slotCapturedShortcut( const KShortcut& sc )
00085 {
00086 
00087     if ( sc == keyButton->shortcut() ) return;
00088     if ( sc.toString().isNull() ) {
00089       // null is fine, that's reset, but sc.Ń–sNull() will be false :/
00090       keyButton->setShortcut( KShortcut::null(), false );
00091     } else {
00092       if( !shortcutIsValid( actionCollection, sc ) ) {
00093         QString msg( i18n( "The selected shortcut is already used, "
00094               "please select a different one." ) );
00095         KMessageBox::sorry( this, msg );
00096       } else {
00097         keyButton->setShortcut( sc, false );
00098       }
00099     }
00100 }
00101 
00102 void SnippetDlg::setShowShortcut( bool show )
00103 {
00104     textLabel3->setShown( show );
00105     keyButton->setShown( show );
00106 }
00107 
00108 #include "snippetdlg.moc"