1 | /* |
2 | Copyright 2007-2008 by Robert Knight <robertknight@gmail.com> |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301 USA. |
18 | */ |
19 | |
20 | #ifndef MANAGEPROFILESDIALOG_H |
21 | #define MANAGEPROFILESDIALOG_H |
22 | |
23 | // Qt |
24 | #include <QStyledItemDelegate> |
25 | #include <QtCore/QSet> |
26 | |
27 | // KDE |
28 | #include <KDialog> |
29 | |
30 | // Konsole |
31 | #include "Profile.h" |
32 | |
33 | class QItemSelection; |
34 | class QShowEvent; |
35 | class QStandardItem; |
36 | class QStandardItemModel; |
37 | |
38 | namespace Ui |
39 | { |
40 | class ManageProfilesDialog; |
41 | } |
42 | |
43 | namespace Konsole |
44 | { |
45 | /** |
46 | * A dialog which lists the available types of profiles and allows |
47 | * the user to add new profiles, and remove or edit existing |
48 | * profile types. |
49 | */ |
50 | class KONSOLEPRIVATE_EXPORT ManageProfilesDialog : public KDialog |
51 | { |
52 | Q_OBJECT |
53 | |
54 | friend class FavoriteItemDelegate; |
55 | friend class ShortcutItemDelegate; |
56 | |
57 | public: |
58 | /** Constructs a new profile type with the specified parent. */ |
59 | explicit ManageProfilesDialog(QWidget* parent = 0); |
60 | virtual ~ManageProfilesDialog(); |
61 | |
62 | /** |
63 | * Specifies whether the shortcut editor should be show. |
64 | * The shortcut editor allows shortcuts to be associated with |
65 | * profiles. When a shortcut is changed, the dialog will call |
66 | * SessionManager::instance()->setShortcut() to update the shortcut |
67 | * associated with the profile. |
68 | * |
69 | * By default the editor is visible. |
70 | */ |
71 | void setShortcutEditorVisible(bool visible); |
72 | |
73 | protected: |
74 | virtual void showEvent(QShowEvent* event); |
75 | |
76 | private slots: |
77 | void deleteSelected(); |
78 | void setSelectedAsDefault(); |
79 | void createProfile(); |
80 | void editSelected(); |
81 | void moveUpSelected(); |
82 | void moveDownSelected(); |
83 | |
84 | void itemDataChanged(QStandardItem* item); |
85 | |
86 | // enables or disables Edit/Delete/Set as Default buttons when the |
87 | // selection changes |
88 | void tableSelectionChanged(const QItemSelection&); |
89 | |
90 | void updateFavoriteStatus(Profile::Ptr profile, bool favorite); |
91 | |
92 | void addItems(const Profile::Ptr); |
93 | void updateItems(const Profile::Ptr); |
94 | void removeItems(const Profile::Ptr); |
95 | |
96 | private: |
97 | Profile::Ptr currentProfile() const; |
98 | QList<Profile::Ptr> selectedProfiles() const; |
99 | bool isProfileDeletable(Profile::Ptr profile) const; |
100 | |
101 | // updates the font of the items to match |
102 | // their default / non-default profile status |
103 | void updateDefaultItem(); |
104 | void updateItemsForProfile(const Profile::Ptr profile, QList<QStandardItem*>& items) const; |
105 | // updates the profile table to be in sync with the |
106 | // session manager |
107 | void populateTable(); |
108 | int rowForProfile(const Profile::Ptr profile) const; |
109 | |
110 | Ui::ManageProfilesDialog* _ui; |
111 | QStandardItemModel* _sessionModel; |
112 | |
113 | static const int ProfileNameColumn = 0; |
114 | static const int FavoriteStatusColumn = 1; |
115 | static const int ShortcutColumn = 2; |
116 | static const int ProfileKeyRole = Qt::UserRole + 1; |
117 | static const int ShortcutRole = Qt::UserRole + 1; |
118 | }; |
119 | |
120 | class StyledBackgroundPainter |
121 | { |
122 | public: |
123 | static void drawBackground(QPainter* painter, const QStyleOptionViewItem& option, |
124 | const QModelIndex& index); |
125 | }; |
126 | |
127 | class FavoriteItemDelegate : public QStyledItemDelegate |
128 | { |
129 | public: |
130 | explicit FavoriteItemDelegate(QObject* parent = 0); |
131 | |
132 | virtual bool editorEvent(QEvent* event, QAbstractItemModel* model, |
133 | const QStyleOptionViewItem& option, const QModelIndex& index); |
134 | virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, |
135 | const QModelIndex& index) const; |
136 | }; |
137 | |
138 | class ShortcutItemDelegate : public QStyledItemDelegate |
139 | { |
140 | Q_OBJECT |
141 | |
142 | public: |
143 | explicit ShortcutItemDelegate(QObject* parent = 0); |
144 | |
145 | virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const; |
146 | virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, |
147 | const QModelIndex& index) const; |
148 | virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, |
149 | const QModelIndex& index) const; |
150 | |
151 | private slots: |
152 | void editorModified(const QKeySequence& keys); |
153 | |
154 | private: |
155 | mutable QSet<QWidget*> _modifiedEditors; |
156 | mutable QSet<QModelIndex> _itemsBeingEdited; |
157 | }; |
158 | } |
159 | #endif // MANAGEPROFILESDIALOG_H |
160 | |
161 | |