summaryrefslogtreecommitdiffstats
path: root/kmail/snippetdlg.cpp
blob: 6c2efb2ec05ab86fb9b4333e5992a040e7537d24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/***************************************************************************
 *   snippet feature from tdevelop/plugins/snippet/                        *
 *                                                                         *
 *   Copyright (C) 2007 by Robert Gruber                                   *
 *   rgruber@users.sourceforge.net                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "snippetdlg.h"

#include <kdialog.h>
#include <klineedit.h>
#include <klocale.h>

#include <tqlabel.h>
#include <layout.h>
#include <kpushbutton.h>
#include <ktextedit.h>
#include "kkeybutton.h"
#include "kactioncollection.h"
#include "kmessagebox.h"

/*
 *  Constructs a SnippetDlg as a child of 'parent', with the
 *  name 'name' and widget flags set to 'f'.
 *
 *  The dialog will by default be modeless, unless you set 'modal' to
 *  TRUE to construct a modal dialog.
 */
SnippetDlg::SnippetDlg( KActionCollection* ac, TQWidget* parent, const char* name, bool modal, WFlags fl )
    : SnippetDlgBase( parent, name, modal, fl ), actionCollection( ac )
{
    if ( !name )
	setName( "SnippetDlg" );

    textLabel3 = new TQLabel( this, "textLabel3" );
    keyButton = new KKeyButton( this );
    connect( keyButton, TQT_SIGNAL( capturedShortcut( const KShortcut& ) ),
             this, TQT_SLOT( slotCapturedShortcut( const KShortcut& ) ) );

    btnAdd->setEnabled( false );
    connect( snippetName, TQT_SIGNAL(textChanged(const TQString &)),
             this, TQT_SLOT(slotTextChanged(const TQString &)) );
    connect( snippetName, TQT_SIGNAL(returnPressed()),
             this, TQT_SLOT(slotReturnPressed()) );

    layout3->addWidget( textLabel3, 7, 0 );
    layout3->addWidget( keyButton, 7, 1 );

    // tab order
    setTabOrder( snippetText, keyButton );
    setTabOrder( keyButton, btnAdd );
    setTabOrder( btnAdd, btnCancel );

    textLabel3->setBuddy( keyButton );
    languageChange();
}

/*
 *  Destroys the object and frees any allocated resources
 */
SnippetDlg::~SnippetDlg()
{
    // no need to delete child widgets, TQt does it all for us
}

/*
 *  Sets the strings of the subwidgets using the current
 *  language.
 */
void SnippetDlg::languageChange()
{
    textLabel3->setText( i18n( "Sh&ortcut:" ) );
}

static bool shortcutIsValid( const KActionCollection* actionCollection, const KShortcut &sc )
{
  KActionPtrList actions = actionCollection->actions();
  KActionPtrList::Iterator it( actions.begin() );
  for ( ; it != actions.end(); it++ ) {
    if ( (*it)->shortcut() == sc ) return false;
  }
  return true;
}

void SnippetDlg::slotCapturedShortcut( const KShortcut& sc )
{

    if ( sc == keyButton->shortcut() ) return;
    if ( sc.toString().isNull() ) {
      // null is fine, that's reset, but sc.Ń–sNull() will be false :/
      keyButton->setShortcut( KShortcut::null(), false );
    } else {
      if( !shortcutIsValid( actionCollection, sc ) ) {
        TQString msg( i18n( "The selected shortcut is already used, "
              "please select a different one." ) );
        KMessageBox::sorry( this, msg );
      } else {
        keyButton->setShortcut( sc, false );
      }
    }
}

void SnippetDlg::setShowShortcut( bool show )
{
    textLabel3->setShown( show );
    keyButton->setShown( show );
}

void SnippetDlg::slotTextChanged( const TQString &text )
{
  btnAdd->setEnabled( !text.isEmpty() );
}

void SnippetDlg::slotReturnPressed()
{
  if ( !snippetName->text().isEmpty() ) {
    accept();
  }
}

#include "snippetdlg.moc"