Semi-yearly copyright bump
[quassel.git] / src / qtui / settingspages / keysequencewidget.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 {
45     Q_OBJECT
46 public:
47     KeySequenceWidget(QWidget *parent = 0);
48
49     void setModel(ShortcutsModel *model);
50
51 public slots:
52     void setKeySequence(const QKeySequence &seq);
53
54 signals:
55     /**
56      * This signal is emitted when the current key sequence has changed by user input
57      * \param seq         The key sequence the user has chosen
58      * \param conflicting The index of an action that needs to have its shortcut removed. The user has already been
59      *                    asked to agree (if he declines, this signal won't be emitted at all).
60      */
61     void keySequenceChanged(const QKeySequence &seq, const QModelIndex &conflicting = QModelIndex());
62
63     void clicked();
64
65 private slots:
66     void updateShortcutDisplay();
67     void startRecording();
68     void cancelRecording();
69     void clear();
70
71 private:
72     inline bool isRecording() const { return _isRecording; }
73     void doneRecording();
74
75     bool isOkWhenModifierless(int keyQt) const;
76     bool isShiftAsModifierAllowed(int keyQt) const;
77     bool isKeySequenceAvailable(const QKeySequence &seq);
78
79     ShortcutsModel *_shortcutsModel;
80     bool _isRecording;
81     QKeySequence _keySequence, _oldKeySequence;
82     uint _modifierKeys;
83     QModelIndex _conflictingIndex;
84
85     KeySequenceButton *_keyButton;
86     QToolButton *_clearButton;
87
88     friend class KeySequenceButton;
89 };
90
91
92 /*****************************************************************************/
93
94 class KeySequenceButton : public QPushButton
95 {
96     Q_OBJECT
97 public:
98     explicit KeySequenceButton(KeySequenceWidget *d, QWidget *parent = 0);
99
100 protected:
101     virtual bool event(QEvent *event);
102     virtual void keyPressEvent(QKeyEvent *event);
103     virtual void keyReleaseEvent(QKeyEvent *event);
104
105 private:
106     KeySequenceWidget *d;
107 };
108
109
110 #endif // KEYSEQUENCEWIDGET_H