Allow configuration of shortcuts for platforms other than KDE
[quassel.git] / src / qtui / settingspages / keysequencewidget.h
1 /***************************************************************************
2  *   Copyright (C) 2010 by the Quassel Project                             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This class has been inspired by KDE's KKeySequenceWidget and uses     *
6  *   some code snippets of its implementation, part of kdelibs.            *
7  *   The original file is                                                  *
8  *       Copyright (C) 1998 Mark Donohoe <donohoe@kde.org>                 *
9  *       Copyright (C) 2001 Ellis Whitehead <ellis@kde.org>                *
10  *       Copyright (C) 2007 Andreas Hartmetz <ahartmetz@gmail.com>         *
11  *                                                                         *
12  *   This program is free software; you can redistribute it and/or modify  *
13  *   it under the terms of the GNU General Public License as published by  *
14  *   the Free Software Foundation; either version 2 of the License, or     *
15  *   (at your option) any later version.                                   *
16  *                                                                         *
17  *   This program is distributed in the hope that it will be useful,       *
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
20  *   GNU General Public License for more details.                          *
21  *                                                                         *
22  *   You should have received a copy of the GNU General Public License     *
23  *   along with this program; if not, write to the                         *
24  *   Free Software Foundation, Inc.,                                       *
25  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
26  ***************************************************************************/
27
28 #ifndef KEYSEQUENCEWIDGET_H
29 #define KEYSEQUENCEWIDGET_H
30
31 #include <QKeySequence>
32 #include <QPushButton>
33 #include <QSet>
34 #include <QWidget>
35
36 #include "shortcutsmodel.h"
37
38 class Action;
39 class ActionCollection;
40 class KeySequenceButton;
41 class QToolButton;
42
43 class KeySequenceWidget : public QWidget {
44   Q_OBJECT
45 public:
46   KeySequenceWidget(QWidget *parent = 0);
47
48   void setModel(ShortcutsModel *model);
49
50 public slots:
51   void setKeySequence(const QKeySequence &seq);
52
53 signals:
54   /**
55    * This signal is emitted when the current key sequence has changed by user input
56    * \param seq         The key sequence the user has chosen
57    * \param conflicting The index of an action that needs to have its shortcut removed. The user has already been
58    *                    asked to agree (if he declines, this signal won't be emitted at all).
59    */
60   void keySequenceChanged(const QKeySequence &seq, const QModelIndex &conflicting = QModelIndex());
61
62   void clicked();
63
64 private slots:
65   void updateShortcutDisplay();
66   void startRecording();
67   void cancelRecording();
68   void clear();
69
70 private:
71   inline bool isRecording() const { return _isRecording; }
72   void doneRecording();
73
74   bool isOkWhenModifierless(int keyQt) const;
75   bool isShiftAsModifierAllowed(int keyQt) const;
76   bool isKeySequenceAvailable(const QKeySequence &seq);
77
78   ShortcutsModel *_shortcutsModel;
79   bool _isRecording;
80   QKeySequence _keySequence, _oldKeySequence;
81   uint _modifierKeys;
82   QModelIndex _conflictingIndex;
83
84   KeySequenceButton *_keyButton;
85   QToolButton *_clearButton;
86
87   friend class KeySequenceButton;
88 };
89
90
91 /*****************************************************************************/
92
93 class KeySequenceButton : public QPushButton {
94   Q_OBJECT
95 public:
96   explicit KeySequenceButton(KeySequenceWidget *d, QWidget *parent = 0);
97
98 protected:
99   virtual bool event(QEvent *event);
100   virtual void keyPressEvent(QKeyEvent *event);
101   virtual void keyReleaseEvent(QKeyEvent *event);
102
103 private:
104   KeySequenceWidget *d;
105 };
106
107 #endif // KEYSEQUENCEWIDGET_H