1 /***************************************************************************
2 *                                                                         *
3 *   This program is free software; you can redistribute it and/or modify  *
4 *   it under the terms of the GNU General Public License as published by  *
5 *   the Free Software Foundation; either version 3 of the License, or     *
6 *   (at your option) any later version.                                   *
7 *                                                                         *
8 ***************************************************************************/
9 
10 #include "SettingsShortcuts.h"
11 #include "ShortcutManager.h"
12 #include "MainWindow.h"
13 #include "ShortcutGetter.h"
14 #include "WulforSettings.h"
15 
16 #include <QMap>
17 #include <QKeySequence>
18 #include <QByteArray>
19 #include <QtDebug>
20 #include <QHeaderView>
21 
22 static const QString &TREEVIEW_STATE_KEY = "settings-shortcuts-tableview-state";
23 
SettingsShortcuts(QWidget * parent)24 SettingsShortcuts::SettingsShortcuts(QWidget *parent) :
25     QWidget(parent)
26 {
27     setupUi(this);
28 
29     model = new ShortcutsModel(this);
30     treeView->setModel(model);
31     treeView->header()->restoreState(QByteArray::fromBase64(WSGET(TREEVIEW_STATE_KEY).toUtf8()));
32 
33     connect(treeView, SIGNAL(clicked(QModelIndex)), this, SLOT(slotIndexClicked(QModelIndex)));
34 }
35 
~SettingsShortcuts()36 SettingsShortcuts::~SettingsShortcuts(){
37     model->deleteLater();
38 }
39 
ok()40 void SettingsShortcuts::ok(){
41     model->save();
42 
43     WSSET(TREEVIEW_STATE_KEY, treeView->header()->saveState().toBase64());
44 }
45 
slotIndexClicked(const QModelIndex & index)46 void SettingsShortcuts::slotIndexClicked(const QModelIndex &index){
47     if (!(index.isValid() && index.column() == 1))
48         return;
49 
50     ShortcutItem *item = reinterpret_cast<ShortcutItem*>(index.internalPointer());
51 
52     if (!item)
53         return;
54 
55     ShortcutGetter getter(MainWindow::getInstance());
56     QString ret;
57 
58     if ((ret = getter.exec(item->shortcut)).isNull())
59         return;
60 
61     item->shortcut = ret;
62 
63     model->repaint();
64 }
65 
ShortcutsModel(QObject * parent)66 ShortcutsModel::ShortcutsModel(QObject * parent) : QAbstractItemModel(parent) {
67     rootItem = new ShortcutItem(NULL);
68 
69     MainWindow *MW = MainWindow::getInstance();
70     QMap<QString, QKeySequence> shs;
71     shs = ShortcutManager::getInstance()->getShortcuts();
72 
73     auto it = shs.begin();
74     const QAction *act = NULL;
75 
76     for (; it != shs.end(); ++it){
77         act = MW->findChild<QAction* >(it.key());
78 
79         if (!act)
80             continue;
81 
82         ShortcutItem *item = new ShortcutItem(rootItem);
83         item->title = act->text();
84         item->shortcut = it.value().toString();
85 
86         rootItem->appendChild(item);
87 
88         items.insert(item, it.key());
89     }
90 
91     emit layoutChanged();
92 }
93 
94 
~ShortcutsModel()95 ShortcutsModel::~ShortcutsModel() {
96     delete rootItem;
97 }
98 
save()99 void ShortcutsModel::save(){
100     auto it = items.begin();
101     MainWindow *MW = MainWindow::getInstance();
102     QAction *act = NULL;
103 
104     for (; it != items.end(); ++it){
105         act = MW->findChild<QAction* >(it.value());
106 
107         if (!act)
108             continue;
109 
110         ShortcutManager::getInstance()->updateShortcut(act, ((it.key())->shortcut));
111     }
112 
113     ShortcutManager::getInstance()->save();
114 }
115 
rowCount(const QModelIndex &) const116 int ShortcutsModel::rowCount(const QModelIndex & ) const {
117     return rootItem->childCount();
118 }
119 
columnCount(const QModelIndex &) const120 int ShortcutsModel::columnCount(const QModelIndex & ) const {
121     return 2;
122 }
123 
flags(const QModelIndex & index) const124 Qt::ItemFlags ShortcutsModel::flags(const QModelIndex &index) const {
125     if (!index.isValid())
126         return 0;
127 
128     return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
129 }
130 
data(const QModelIndex & index,int role) const131 QVariant ShortcutsModel::data(const QModelIndex & index, int role) const {
132     if (!index.isValid())
133         return QVariant();
134 
135     ShortcutItem * item = static_cast<ShortcutItem*>(index.internalPointer());
136 
137     if (!item)
138         return QVariant();
139 
140     switch (role){
141         case Qt::DisplayRole:
142         {
143             switch (index.column()) {
144                 case 0: return item->title;
145                 case 1: return item->shortcut;
146             }
147 
148             break;
149         }
150         case Qt::DecorationRole:
151         {
152             break;
153         }
154         case Qt::ToolTipRole:
155         {
156             break;
157         }
158         case Qt::TextAlignmentRole:
159         {
160             break;
161         }
162         case Qt::FontRole:
163         {
164             break;
165         }
166     }
167 
168     return QVariant();
169 }
170 
171 
headerData(int section,Qt::Orientation orientation,int role) const172 QVariant ShortcutsModel::headerData(int section, Qt::Orientation orientation, int role) const {
173     if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)) {
174         switch (section) {
175             case 0: return tr("Action");
176             case 1: return tr("Hotkey");
177         }
178     }
179 
180     return QVariant();
181 }
182 
sort(int column,Qt::SortOrder order)183 void ShortcutsModel::sort(int column, Qt::SortOrder order) {
184     emit layoutChanged();
185 }
186 
index(int row,int column,const QModelIndex &) const187 QModelIndex ShortcutsModel::index(int row, int column, const QModelIndex &) const {
188     if (row > (rootItem->childCount() - 1) || row < 0)
189         return QModelIndex();
190 
191     return createIndex(row, column, rootItem->child(row));
192 }
193 
parent(const QModelIndex &) const194 QModelIndex ShortcutsModel::parent(const QModelIndex & ) const {
195     return QModelIndex();
196 }
197 
ShortcutItem(ShortcutItem * parent)198 ShortcutItem::ShortcutItem(ShortcutItem *parent) : parentItem(parent)
199 {
200 }
201 
~ShortcutItem()202 ShortcutItem::~ShortcutItem()
203 {
204     qDeleteAll(childItems);
205 }
206 
appendChild(ShortcutItem * item)207 void ShortcutItem::appendChild(ShortcutItem *item) {
208     item->parentItem = this;
209     childItems.append(item);
210 }
211 
child(int row)212 ShortcutItem *ShortcutItem::child(int row) {
213     return childItems.value(row);
214 }
215 
childCount() const216 int ShortcutItem::childCount() const {
217     return childItems.count();
218 }
219 
columnCount() const220 int ShortcutItem::columnCount() const {
221     return 7;
222 }
parent()223 ShortcutItem *ShortcutItem::parent() {
224     return parentItem;
225 }
226 
row() const227 int ShortcutItem::row() const {
228     if (parentItem)
229         return parentItem->childItems.indexOf(const_cast<ShortcutItem*>(this));
230 
231     return 0;
232 }
233