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 <kdialog.h> 00017 #include <klineedit.h> 00018 #include <klocale.h> 00019 00020 #include <tqlabel.h> 00021 #include <tqlayout.h> 00022 #include <kpushbutton.h> 00023 #include <ktextedit.h> 00024 #include "kkeybutton.h" 00025 #include "kactioncollection.h" 00026 #include "kmessagebox.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, TQWidget* parent, const char* name, bool modal, WFlags fl ) 00036 : SnippetDlgBase( parent, name, modal, fl ), actionCollection( ac ) 00037 { 00038 if ( !name ) 00039 setName( "SnippetDlg" ); 00040 00041 textLabel3 = new TQLabel( this, "textLabel3" ); 00042 keyButton = new KKeyButton( this ); 00043 connect( keyButton, TQT_SIGNAL( capturedShortcut( const KShortcut& ) ), 00044 this, TQT_SLOT( slotCapturedShortcut( const KShortcut& ) ) ); 00045 00046 btnAdd->setEnabled( false ); 00047 connect( snippetName, TQT_SIGNAL(textChanged(const TQString &)), 00048 this, TQT_SLOT(slotTextChanged(const TQString &)) ); 00049 connect( snippetName, TQT_SIGNAL(returnPressed()), 00050 this, TQT_SLOT(slotReturnPressed()) ); 00051 00052 layout3->addWidget( textLabel3, 7, 0 ); 00053 layout3->addWidget( keyButton, 7, 1 ); 00054 00055 // tab order 00056 setTabOrder( snippetText, keyButton ); 00057 setTabOrder( keyButton, btnAdd ); 00058 setTabOrder( btnAdd, btnCancel ); 00059 00060 textLabel3->setBuddy( keyButton ); 00061 languageChange(); 00062 } 00063 00064 /* 00065 * Destroys the object and frees any allocated resources 00066 */ 00067 SnippetDlg::~SnippetDlg() 00068 { 00069 // no need to delete child widgets, TQt does it all for us 00070 } 00071 00072 /* 00073 * Sets the strings of the subwidgets using the current 00074 * language. 00075 */ 00076 void SnippetDlg::languageChange() 00077 { 00078 textLabel3->setText( i18n( "Sh&ortcut:" ) ); 00079 } 00080 00081 static bool shortcutIsValid( const KActionCollection* actionCollection, const KShortcut &sc ) 00082 { 00083 KActionPtrList actions = actionCollection->actions(); 00084 KActionPtrList::Iterator it( actions.begin() ); 00085 for ( ; it != actions.end(); it++ ) { 00086 if ( (*it)->shortcut() == sc ) return false; 00087 } 00088 return true; 00089 } 00090 00091 void SnippetDlg::slotCapturedShortcut( const KShortcut& sc ) 00092 { 00093 00094 if ( sc == keyButton->shortcut() ) return; 00095 if ( sc.toString().isNull() ) { 00096 // null is fine, that's reset, but sc.Ń–sNull() will be false :/ 00097 keyButton->setShortcut( KShortcut::null(), false ); 00098 } else { 00099 if( !shortcutIsValid( actionCollection, sc ) ) { 00100 TQString msg( i18n( "The selected shortcut is already used, " 00101 "please select a different one." ) ); 00102 KMessageBox::sorry( this, msg ); 00103 } else { 00104 keyButton->setShortcut( sc, false ); 00105 } 00106 } 00107 } 00108 00109 void SnippetDlg::setShowShortcut( bool show ) 00110 { 00111 textLabel3->setShown( show ); 00112 keyButton->setShown( show ); 00113 } 00114 00115 void SnippetDlg::slotTextChanged( const TQString &text ) 00116 { 00117 btnAdd->setEnabled( !text.isEmpty() ); 00118 } 00119 00120 void SnippetDlg::slotReturnPressed() 00121 { 00122 if ( !snippetName->text().isEmpty() ) { 00123 accept(); 00124 } 00125 } 00126 00127 #include "snippetdlg.moc"