kmail

snippetdlg.cpp

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 <kactioncollection.h>
00017 #include <kcombobox.h>
00018 #include <kkeybutton.h>
00019 #include <klineedit.h>
00020 #include <klocale.h>
00021 #include <kmessagebox.h>
00022 #include <kpushbutton.h>
00023 #include <ktextedit.h>
00024 
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 
00028 /*
00029  *  Constructs a SnippetDlg as a child of 'parent', with the
00030  *  name 'name' and widget flags set to 'f'.
00031  *
00032  *  The dialog will by default be modeless, unless you set 'modal' to
00033  *  TRUE to construct a modal dialog.
00034  */
00035 SnippetDlg::SnippetDlg( KActionCollection *ac, QWidget *parent, const char *name,
00036                         bool modal, WFlags fl )
00037   : SnippetDlgBase( parent, name, modal, fl ), actionCollection( ac )
00038 {
00039   if ( !name ) {
00040     setName( "SnippetDlg" );
00041   }
00042 
00043   shortcutLabel = new QLabel( this, "shortcutLabel" );
00044   shortcutButton = new KKeyButton( this );
00045   connect( shortcutButton, SIGNAL(capturedShortcut( const KShortcut &)),
00046            this, SLOT(slotCapturedShortcut(const KShortcut &)) );
00047 
00048   btnAdd->setEnabled( false );
00049   connect( snippetName, SIGNAL(textChanged(const QString &)),
00050            this, SLOT(slotTextChanged(const QString &)) );
00051   connect( snippetName, SIGNAL(returnPressed()),
00052            this, SLOT(slotReturnPressed()) );
00053 
00054   layout3->addWidget( shortcutLabel, 7, 0 );
00055   layout3->addWidget( shortcutButton, 7, 1 );
00056 
00057   snippetText->setMinimumSize( 500, 300 );
00058 
00059   // tab order
00060   setTabOrder( snippetText, shortcutButton );
00061   setTabOrder( shortcutButton, btnAdd );
00062   setTabOrder( btnAdd, btnCancel );
00063 
00064   shortcutLabel->setBuddy( shortcutButton );
00065   languageChange();
00066 }
00067 
00068 /*
00069  *  Destroys the object and frees any allocated resources
00070  */
00071 SnippetDlg::~SnippetDlg()
00072 {
00073   // no need to delete child widgets, Qt does it all for us
00074 }
00075 
00076 /*
00077  *  Sets the strings of the subwidgets using the current
00078  *  language.
00079  */
00080 void SnippetDlg::languageChange()
00081 {
00082   shortcutLabel->setText( i18n( "Sh&ortcut:" ) );
00083 }
00084 
00085 static bool shortcutIsValid( const KActionCollection *actionCollection, const KShortcut &sc )
00086 {
00087   KActionPtrList actions = actionCollection->actions();
00088   KActionPtrList::Iterator it( actions.begin() );
00089   for ( ; it != actions.end(); it++ ) {
00090     if ( (*it)->shortcut() == sc ) {
00091       return false;
00092     }
00093   }
00094   return true;
00095 }
00096 
00097 void SnippetDlg::slotCapturedShortcut( const KShortcut &sc )
00098 {
00099 
00100   if ( sc == shortcutButton->shortcut() ) {
00101     return;
00102   }
00103   if ( sc.toString().isNull() ) {
00104     // null is fine, that's reset, but sc.Ń–sNull() will be false :/
00105     shortcutButton->setShortcut( KShortcut::null(), false );
00106   } else {
00107     if ( !shortcutIsValid( actionCollection, sc ) ) {
00108       QString msg( i18n( "The selected shortcut is already used, "
00109                          "please select a different one." ) );
00110       KMessageBox::sorry( this, msg );
00111     } else {
00112       shortcutButton->setShortcut( sc, false );
00113     }
00114   }
00115 }
00116 
00117 void SnippetDlg::setShowShortcut( bool show )
00118 {
00119   shortcutLabel->setShown( show );
00120   shortcutButton->setShown( show );
00121 }
00122 
00123 void SnippetDlg::slotTextChanged( const QString &text )
00124 {
00125   btnAdd->setEnabled( !text.isEmpty() );
00126 }
00127 
00128 void SnippetDlg::slotReturnPressed()
00129 {
00130   if ( !snippetName->text().isEmpty() ) {
00131     accept();
00132   }
00133 }
00134 
00135 void SnippetDlg::setGroupMode( bool groupMode )
00136 {
00137   const bool full = !groupMode;
00138   textLabelGroup->setShown( full );
00139   cbGroup->setShown( full );
00140   textLabel2->setShown( full );
00141   snippetText->setShown( full );
00142   setShowShortcut( !groupMode );
00143   if ( groupMode ) {
00144     resize( width(), 20 );
00145   }
00146 }
00147 
00148 #include "snippetdlg.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys