oopapidocs
2.0
|
00001 #include <QAction> 00002 #include "actioneditordialog.h" 00003 #include <QStyle> 00004 #include <QKeyEvent> 00005 #include <QIcon> 00006 00007 #include <QDebug> 00008 static QSet<int> modifiers; 00009 00010 ActionEditorDialog::ActionEditorDialog(QAction* action, QWidget* parent) : 00011 QDialog(parent), m_action(action) { 00012 if (modifiers.size() == 0) { 00013 modifiers << 0 << Qt::Key_unknown << Qt::Key_Shift << 16777250 << 00014 Qt::Key_Alt << Qt::Key_Control; 00015 } 00016 setupUi(this); 00017 m_actionNameLabel->setText(action->text()); 00018 m_actionIconLabel->setPixmap(action->icon().pixmap(50, 50)); 00019 m_sequence = m_action->shortcut(); 00020 setFocusPolicy(Qt::StrongFocus); 00021 updateUi(); 00022 } 00023 //start id="keypressevent" 00024 void ActionEditorDialog::keyPressEvent(QKeyEvent* evt) { /* evt contains information about our keypress */ 00025 int key = evt->key(); 00026 if (modifiers.contains(key)) return; 00027 if (m_events.size() > 3) return; /* only 4 can fit into a QKeySequence */ 00028 QPair<int, int> pair = QPair<int, int>(evt->modifiers(), evt->key()); 00029 qDebug() << "mods: " << evt->modifiers() << "key: " << evt->key(); 00030 m_events << pair; /* Save the keystroke as a pair, to list of key events */ 00031 evt->accept(); /* Event does not need to be processed by any other widget */ 00032 updateUi(); 00033 } 00034 //end 00035 //start id="updateui" 00036 void ActionEditorDialog::updateUi() { 00037 if (!m_events.isEmpty()) { 00038 int keys[4] = {0,0,0,0}; 00039 for (int i=0; i<m_events.size(); ++i) { 00040 QPair<int, int> pair = m_events[i]; 00041 keys[i] = pair.first | pair.second; 00042 } 00043 m_sequence = QKeySequence(keys[0], keys[1], keys[2], keys[3]); 00044 } 00045 m_shortcutLineEdit->setText(m_sequence.toString()); 00046 } 00047 //end 00048 void ActionEditorDialog::on_m_clearButton_clicked() { 00049 m_events.clear(); 00050 m_sequence = QKeySequence(); 00051 m_shortcutLineEdit->clear(); 00052 } 00053 00054 void ActionEditorDialog::changeEvent(QEvent* e) 00055 { 00056 QDialog::changeEvent(e); 00057 switch (e->type()) { 00058 case QEvent::LanguageChange: 00059 retranslateUi(this); 00060 break; 00061 default: 00062 break; 00063 } 00064 }