scide: implement selectionLength for openDocument
[supercollider.git] / editors / sc-ide / widgets / settings / shortcuts_page.cpp
blob131ac1ed9e90cc0749107f0a474ec2de0f12c985
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "shortcuts_page.hpp"
22 #include "ui_settings_shortcuts.h"
23 #include "../../core/settings/manager.hpp"
24 #include "../../core/main.hpp"
26 #include <QHeaderView>
27 #include <QTreeWidget>
28 #include <QTreeWidgetItem>
29 #include <QKeyEvent>
31 Q_DECLARE_METATYPE(QAction*);
32 Q_DECLARE_METATYPE(QKeySequence);
34 namespace ScIDE { namespace Settings {
36 ShortcutsPage::ShortcutsPage(QWidget *parent) :
37 QWidget(parent),
38 ui( new Ui::ShortcutConfigPage )
40 ui->setupUi(this);
42 ui->clearSeq->setIcon( style()->standardIcon(QStyle::SP_DockWidgetCloseButton) );
44 connect(ui->filter, SIGNAL(textChanged(QString)), this, SLOT(filterBy(QString)));
45 connect(ui->actionTree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
46 this, SLOT(showItem(QTreeWidgetItem*,QTreeWidgetItem*)));
48 // automation
49 connect(ui->defaultOpt, SIGNAL(clicked()), ui->customSeq, SLOT(reset()));
50 connect(ui->customOpt, SIGNAL(clicked()), ui->customSeq, SLOT(setFocus()));
51 connect(ui->customSeq, SIGNAL(editingStarted()), ui->customOpt, SLOT(toggle()));
52 connect(ui->clearSeq, SIGNAL(clicked()), ui->customSeq, SLOT(reset()));
53 connect(ui->clearSeq, SIGNAL(clicked()), ui->customOpt, SLOT(click()));
55 // value application
56 connect(ui->defaultOpt, SIGNAL(clicked()), this, SLOT(apply()));
57 connect(ui->customOpt, SIGNAL(clicked()), this, SLOT(apply()));
58 connect(ui->customSeq, SIGNAL(editingFinished()), this, SLOT(apply()));
61 ShortcutsPage::~ShortcutsPage()
63 delete ui;
66 void ShortcutsPage::load( Manager *s )
68 ui->actionTree->clear();
70 s->beginGroup("IDE/shortcuts");
72 const QList<QAction*> & actions = s->actions();
73 foreach (QAction *action, actions)
74 addAction(action, s);
76 s->endGroup();
78 ui->actionTree->sortByColumn(0, Qt::AscendingOrder);
79 ui->actionTree->header()->resizeSections(QHeaderView::ResizeToContents);
82 void ShortcutsPage::store( Manager *s )
84 s->beginGroup("IDE/shortcuts");
86 s->remove("");
88 int c = ui->actionTree->topLevelItemCount();
89 for (int i = 0; i < c; ++i)
91 QTreeWidgetItem *item = ui->actionTree->topLevelItem(i);
92 QAction *action = item->data(0, ActionRole).value<QAction*>();
93 Q_ASSERT(action);
95 QVariant var = item->data(0, CustomSequenceRole);
96 if (var.isValid()) {
97 s->setValue( s->keyForAction(action), var );
99 else {
100 var = s->defaultValue( s->keyForAction(action) );
102 action->setShortcut( var.value<QKeySequence>() );
105 s->endGroup();
108 void ShortcutsPage::addAction( QAction *a, Manager *s )
110 QTreeWidgetItem *item = new QTreeWidgetItem;
111 item->setIcon(0, a->icon());
112 item->setText(0, a->text().remove('&'));
113 item->setText(2, a->statusTip() );
115 QString key = s->keyForAction(a);
117 item->setData(0, ActionRole, QVariant::fromValue<QAction*>(a));
119 item->setData(0, DefaultSequenceRole, s->defaultValue(key));
121 if (s->isOverridden(key)) {
122 // For reason of performance, ensure the variant holds a QKeySequence
123 QKeySequence seq = s->value(key).value<QKeySequence>();
124 item->setData(0, CustomSequenceRole, QVariant::fromValue<QKeySequence>(seq) );
127 updateItem(item);
129 ui->actionTree->addTopLevelItem(item);
132 void ShortcutsPage::filterBy( const QString &str )
134 int c = ui->actionTree->topLevelItemCount();
135 if (str.isEmpty())
137 for (int i = 0; i < c; ++i)
138 ui->actionTree->topLevelItem(i)->setHidden(false);
140 else
142 for (int i = 0; i < c; ++i)
144 QTreeWidgetItem *item = ui->actionTree->topLevelItem(i);
145 bool visible =
146 item->text(0).contains(str, Qt::CaseInsensitive)
147 || item->text(2).contains(str, Qt::CaseInsensitive);
148 item->setHidden(!visible);
153 void ShortcutsPage::showItem( QTreeWidgetItem *item, QTreeWidgetItem *prev )
155 QVariant defaultSeqVar = item ? item->data(0, DefaultSequenceRole) : QVariant();
156 QString defaultSeq = defaultSeqVar.value<QKeySequence>().toString(QKeySequence::NativeText);
157 ui->defaultSeq->setText(defaultSeq);
159 QVariant customSeqVar = item ? item->data(0, CustomSequenceRole) : QVariant();
160 ui->customSeq->setSequence( customSeqVar.value<QKeySequence>() );
162 if (customSeqVar.isValid())
163 ui->customOpt->setChecked(true);
164 else
165 ui->defaultOpt->setChecked(true);
168 void ShortcutsPage::apply()
170 applyTo( ui->actionTree->currentItem() );
173 void ShortcutsPage::applyTo( QTreeWidgetItem *item )
175 if (!item) return;
177 if (ui->customOpt->isChecked()) {
178 QKeySequence seq = ui->customSeq->sequence();
179 item->setData(0, CustomSequenceRole, QVariant::fromValue<QKeySequence>(seq));
181 else {
182 item->setData(0, CustomSequenceRole, QVariant());
184 updateItem(item);
187 void ShortcutsPage::updateItem( QTreeWidgetItem *item )
189 QVariant seqData = item->data(0, CustomSequenceRole);
190 if (!seqData.isValid())
191 seqData = item->data(0, DefaultSequenceRole);
193 QKeySequence seq = seqData.value<QKeySequence>();
195 item->setText(1, seq.toString(QKeySequence::NativeText));
198 }} // namespace ScIDE::Settings