kmail

snippetdlg.cpp
1/***************************************************************************
2 * snippet feature from tdevelop/plugins/snippet/ *
3 * *
4 * Copyright (C) 2007 by Robert Gruber *
5 * rgruber@users.sourceforge.net *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 ***************************************************************************/
13
14#include "snippetdlg.h"
15
16#include <kdialog.h>
17#include <klineedit.h>
18#include <tdelocale.h>
19
20#include <tqlabel.h>
21#include <tqlayout.h>
22#include <kpushbutton.h>
23#include <ktextedit.h>
24#include "kkeybutton.h"
25#include "tdeactioncollection.h"
26#include "tdemessagebox.h"
27
28/*
29 * Constructs a SnippetDlg as a child of 'parent', with the
30 * name 'name' and widget flags set to 'f'.
31 *
32 * The dialog will by default be modeless, unless you set 'modal' to
33 * TRUE to construct a modal dialog.
34 */
35SnippetDlg::SnippetDlg( TDEActionCollection* ac, TQWidget* parent, const char* name, bool modal, WFlags fl )
36 : SnippetDlgBase( parent, name, modal, fl ), actionCollection( ac )
37{
38 if ( !name )
39 setName( "SnippetDlg" );
40
41 textLabel3 = new TQLabel( this, "textLabel3" );
42 keyButton = new KKeyButton( this );
43 connect( keyButton, TQ_SIGNAL( capturedShortcut( const TDEShortcut& ) ),
44 this, TQ_SLOT( slotCapturedShortcut( const TDEShortcut& ) ) );
45
46 btnAdd->setEnabled( false );
47 connect( snippetName, TQ_SIGNAL(textChanged(const TQString &)),
48 this, TQ_SLOT(slotTextChanged(const TQString &)) );
49 connect( snippetName, TQ_SIGNAL(returnPressed()),
50 this, TQ_SLOT(slotReturnPressed()) );
51
52 layout3->addWidget( textLabel3, 7, 0 );
53 layout3->addWidget( keyButton, 7, 1 );
54
55 // tab order
56 setTabOrder( snippetText, keyButton );
57 setTabOrder( keyButton, btnAdd );
58 setTabOrder( btnAdd, btnCancel );
59
60 textLabel3->setBuddy( keyButton );
61 languageChange();
62}
63
64/*
65 * Destroys the object and frees any allocated resources
66 */
67SnippetDlg::~SnippetDlg()
68{
69 // no need to delete child widgets, TQt does it all for us
70}
71
72/*
73 * Sets the strings of the subwidgets using the current
74 * language.
75 */
76void SnippetDlg::languageChange()
77{
78 textLabel3->setText( i18n( "Sh&ortcut:" ) );
79}
80
81static bool shortcutIsValid( const TDEActionCollection* actionCollection, const TDEShortcut &sc )
82{
83 TDEActionPtrList actions = actionCollection->actions();
84 TDEActionPtrList::Iterator it( actions.begin() );
85 for ( ; it != actions.end(); it++ ) {
86 if ( (*it)->shortcut() == sc ) return false;
87 }
88 return true;
89}
90
91void SnippetDlg::slotCapturedShortcut( const TDEShortcut& sc )
92{
93
94 if ( sc == keyButton->shortcut() ) return;
95 if ( sc.toString().isNull() ) {
96 // null is fine, that's reset, but sc.Ń–sNull() will be false :/
97 keyButton->setShortcut( TDEShortcut::null(), false );
98 } else {
99 if( !shortcutIsValid( actionCollection, sc ) ) {
100 TQString msg( i18n( "The selected shortcut is already used, "
101 "please select a different one." ) );
102 KMessageBox::sorry( this, msg );
103 } else {
104 keyButton->setShortcut( sc, false );
105 }
106 }
107}
108
109void SnippetDlg::setShowShortcut( bool show )
110{
111 textLabel3->setShown( show );
112 keyButton->setShown( show );
113}
114
115void SnippetDlg::slotTextChanged( const TQString &text )
116{
117 btnAdd->setEnabled( !text.isEmpty() );
118}
119
120void SnippetDlg::slotReturnPressed()
121{
122 if ( !snippetName->text().isEmpty() ) {
123 accept();
124 }
125}
126
127#include "snippetdlg.moc"