34 #include "ui_shortcutsettings.h"
46 #include <QHeaderView>
52 using namespace Core::Internal;
59 ShortcutSettings::~ShortcutSettings()
67 return QLatin1String(
"Keyboard");
72 return tr(
"Keyboard");
77 return QLatin1String(
"Environment");
82 return tr(
"Environment");
87 m_keyNum = m_key[0] = m_key[1] = m_key[2] = m_key[3] = 0;
89 m_page =
new Ui_ShortcutSettings();
90 QWidget *w =
new QWidget(parent);
94 m_page->shortcutEdit->installEventFilter(
this);
96 connect(m_page->resetButton, SIGNAL(clicked()),
this, SLOT(resetKeySequence()));
97 connect(m_page->removeButton, SIGNAL(clicked()),
this, SLOT(removeKeySequence()));
98 connect(m_page->defaultButton, SIGNAL(clicked()),
this, SLOT(defaultAction()));
102 m_page->commandList->sortByColumn(0, Qt::AscendingOrder);
104 connect(m_page->filterEdit, SIGNAL(textChanged(QString)),
this, SLOT(filterChanged(QString)));
105 connect(m_page->commandList, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
106 this, SLOT(commandChanged(QTreeWidgetItem *)));
107 connect(m_page->shortcutEdit, SIGNAL(textChanged(QString)),
this, SLOT(keyChanged()));
111 commandChanged(
nullptr);
124 qDeleteAll(m_scitems);
134 if (e->type() == QEvent::KeyPress) {
135 QKeyEvent *k =
static_cast<QKeyEvent *
>(
e);
140 if (e->type() == QEvent::Shortcut || e->type() == QEvent::ShortcutOverride
141 || e->type() == QEvent::KeyRelease)
147 void ShortcutSettings::commandChanged(QTreeWidgetItem *current)
149 if (!current || !current->data(0, Qt::UserRole).isValid()) {
150 m_page->shortcutEdit->setText(
"");
151 m_page->seqGrp->setEnabled(
false);
154 m_page->seqGrp->setEnabled(
true);
156 setKeySequence(scitem->
m_key);
159 void ShortcutSettings::filterChanged(
const QString &f)
161 for (
int i = 0;
i < m_page->commandList->topLevelItemCount(); ++
i) {
162 QTreeWidgetItem *item = m_page->commandList->topLevelItem(
i);
163 item->setHidden(filter(f, item));
167 void ShortcutSettings::keyChanged()
169 QTreeWidgetItem *current = m_page->commandList->currentItem();
170 if (current && current->data(0, Qt::UserRole).isValid()) {
172 scitem->
m_key = QKeySequence(m_key[0], m_key[1], m_key[2], m_key[3]);
173 current->setText(2, scitem->
m_key.toString());
177 void ShortcutSettings::setKeySequence(
const QKeySequence &key)
179 m_keyNum = m_key[0] = m_key[1] = m_key[2] = m_key[3] = 0;
180 m_keyNum = key.count();
181 for (
int i = 0;
i < m_keyNum; ++
i) {
184 m_page->shortcutEdit->setText(key.toString());
187 bool ShortcutSettings::filter(
const QString &f,
const QTreeWidgetItem *item)
189 if (item->childCount() == 0) {
192 for (
int i = 0;
i < item->columnCount(); ++
i) {
193 if (item->text(
i).contains(f, Qt::CaseInsensitive))
200 for (
int i = 0;
i < item->childCount(); ++
i) {
201 QTreeWidgetItem *citem = item->child(
i);
202 if (filter(f, citem)) {
203 citem->setHidden(
true);
205 citem->setHidden(
false);
212 void ShortcutSettings::resetKeySequence()
214 QTreeWidgetItem *current = m_page->commandList->currentItem();
215 if (current && current->data(0, Qt::UserRole).isValid()) {
221 void ShortcutSettings::removeKeySequence()
223 m_keyNum = m_key[0] = m_key[1] = m_key[2] = m_key[3] = 0;
224 m_page->shortcutEdit->clear();
227 void ShortcutSettings::defaultAction()
232 if (item->
m_item == m_page->commandList->currentItem())
233 commandChanged(item->
m_item);
237 void ShortcutSettings::initialize()
242 foreach (Command *c, m_am->
commands()) {
245 if (c->action() && c->action()->isSeparator())
248 QTreeWidgetItem *item =
nullptr;
251 item =
new QTreeWidgetItem(m_page->commandList);
255 item->setText(0, uidm->stringForUniqueIdentifier(c->id()));
260 : c->action()->text();
261 text.remove(QRegExp(
"&(?!&)"));
262 s->m_key = c->action()->shortcut();
263 item->setText(1, text);
265 s->m_key = c->shortcut()->key();
266 item->setText(1, c->shortcut()->whatsThis());
269 item->setText(2, s->m_key.toString());
270 item->setData(0, Qt::UserRole, qVariantFromValue(s));
274 void ShortcutSettings::handleKeyEvent(QKeyEvent *
e)
276 int nextKey = e->key();
277 if (m_keyNum > 3 || nextKey == Qt::Key_Control || nextKey == Qt::Key_Shift
278 || nextKey == Qt::Key_Meta || nextKey == Qt::Key_Alt)
281 nextKey |= translateModifiers(e->modifiers(), e->text());
299 QKeySequence ks(m_key[0], m_key[1], m_key[2], m_key[3]);
300 m_page->shortcutEdit->setText(ks.toString());
304 int ShortcutSettings::translateModifiers(Qt::KeyboardModifiers state,
const QString &text)
309 if ((state & Qt::ShiftModifier) && (text.size() == 0 || !text.at(0).isPrint()
310 || text.at(0).isLetter() || text.at(0).isSpace()))
312 if (state & Qt::ControlModifier)
314 if (state & Qt::MetaModifier)
316 if (state & Qt::AltModifier)
bool eventFilter(QObject *o, QEvent *e)
static UniqueIDManager * instance()
const char *const ICON_RESET
static ActionManagerPrivate * instance()
QString trCategory() const
virtual void setKeySequence(const QKeySequence &key)=0
virtual QKeySequence defaultKeySequence() const =0
The IOptionsPage is an interface for providing options pages.
Q_DECLARE_METATYPE(Core::Internal::ShortcutItem *) using namespace Core
QList< CommandPrivate * > commands() const
QWidget * createPage(QWidget *parent)