mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 01:20:06 +00:00 
			
		
		
		
	Add KeybindEdit widget
This commit is contained in:
		
							parent
							
								
									2bf15f8a69
								
							
						
					
					
						commit
						22e3101c22
					
				
					 3 changed files with 180 additions and 0 deletions
				
			
		
							
								
								
									
										137
									
								
								scwx-qt/source/scwx/qt/ui/keybind_edit.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										137
									
								
								scwx-qt/source/scwx/qt/ui/keybind_edit.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,137 @@ | |||
| #include <scwx/qt/ui/keybind_edit.hpp> | ||||
| #include <scwx/util/logger.hpp> | ||||
| 
 | ||||
| #include <qevent.h> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace qt | ||||
| { | ||||
| namespace ui | ||||
| { | ||||
| 
 | ||||
| static const std::string logPrefix_ = "scwx::qt::ui::keybind_edit"; | ||||
| static const auto        logger_    = scwx::util::Logger::Create(logPrefix_); | ||||
| 
 | ||||
| class KeybindEdit::Impl | ||||
| { | ||||
| public: | ||||
|    explicit Impl() {}; | ||||
|    ~Impl() = default; | ||||
| 
 | ||||
|    QKeySequence sequence_ {}; | ||||
| }; | ||||
| 
 | ||||
| KeybindEdit::KeybindEdit(QWidget* parent) : | ||||
|     QLineEdit(parent), p {std::make_unique<Impl>()} | ||||
| { | ||||
|    setReadOnly(true); | ||||
|    setClearButtonEnabled(true); | ||||
| 
 | ||||
|    QAction* clearAction = findChild<QAction*>(); | ||||
|    if (clearAction != nullptr) | ||||
|    { | ||||
|       clearAction->setEnabled(true); | ||||
| 
 | ||||
|       connect(clearAction, | ||||
|               &QAction::triggered, | ||||
|               this, | ||||
|               [this](bool /* checked */) | ||||
|               { | ||||
|                  logger_->trace("clearAction"); | ||||
| 
 | ||||
|                  if (!p->sequence_.isEmpty()) | ||||
|                  { | ||||
|                     // Clear saved sequence
 | ||||
|                     p->sequence_ = QKeySequence {}; | ||||
|                     setText(p->sequence_.toString()); | ||||
|                     Q_EMIT KeySequenceChanged({}); | ||||
|                  } | ||||
|               }); | ||||
|    } | ||||
| } | ||||
| 
 | ||||
| KeybindEdit::~KeybindEdit() {} | ||||
| 
 | ||||
| QKeySequence KeybindEdit::key_sequence() const | ||||
| { | ||||
|    return p->sequence_; | ||||
| } | ||||
| 
 | ||||
| void KeybindEdit::set_key_sequence(const QKeySequence& sequence) | ||||
| { | ||||
|    if (sequence != p->sequence_) | ||||
|    { | ||||
|       p->sequence_ = sequence; | ||||
|       setText(sequence.toString()); | ||||
|       Q_EMIT KeySequenceChanged(sequence); | ||||
|    } | ||||
| } | ||||
| 
 | ||||
| void KeybindEdit::focusInEvent(QFocusEvent* e) | ||||
| { | ||||
|    logger_->trace("focusInEvent"); | ||||
| 
 | ||||
|    // Replace text with placeholder prompting for input
 | ||||
|    setPlaceholderText("Press any key"); | ||||
|    setText({}); | ||||
| 
 | ||||
|    QLineEdit::focusInEvent(e); | ||||
| } | ||||
| 
 | ||||
| void KeybindEdit::focusOutEvent(QFocusEvent* e) | ||||
| { | ||||
|    logger_->trace("focusOutEvent"); | ||||
| 
 | ||||
|    // Replace text with saved sequence
 | ||||
|    setPlaceholderText({}); | ||||
|    setText(p->sequence_.toString()); | ||||
| 
 | ||||
|    QLineEdit::focusOutEvent(e); | ||||
| } | ||||
| 
 | ||||
| void KeybindEdit::keyPressEvent(QKeyEvent* e) | ||||
| { | ||||
|    logger_->trace("keyPressEvent"); | ||||
| 
 | ||||
|    QKeySequence sequence {}; | ||||
| 
 | ||||
|    switch (e->key()) | ||||
|    { | ||||
|    case Qt::Key::Key_Shift: | ||||
|    case Qt::Key::Key_Control: | ||||
|    case Qt::Key::Key_Alt: | ||||
|    case Qt::Key::Key_Meta: | ||||
|    case Qt::Key::Key_Mode_switch: | ||||
|       // Record modifiers only in sequence
 | ||||
|       sequence = e->modifiers().toInt(); | ||||
|       break; | ||||
| 
 | ||||
|    default: | ||||
|       // Record modifiers and keys in sequence, and save sequence
 | ||||
|       sequence = e->modifiers().toInt() | e->key(); | ||||
| 
 | ||||
|       if (sequence != p->sequence_) | ||||
|       { | ||||
|          p->sequence_ = sequence; | ||||
|          Q_EMIT KeySequenceChanged(sequence); | ||||
|       } | ||||
| 
 | ||||
|       clearFocus(); | ||||
|       break; | ||||
|    } | ||||
| 
 | ||||
|    setText(sequence.toString()); | ||||
| } | ||||
| 
 | ||||
| void KeybindEdit::keyReleaseEvent(QKeyEvent*) | ||||
| { | ||||
|    logger_->trace("keyReleaseEvent"); | ||||
| 
 | ||||
|    // Modifiers were released prior to pressing a non-modifier key
 | ||||
|    setText({}); | ||||
| } | ||||
| 
 | ||||
| } // namespace ui
 | ||||
| } // namespace qt
 | ||||
| } // namespace scwx
 | ||||
							
								
								
									
										41
									
								
								scwx-qt/source/scwx/qt/ui/keybind_edit.hpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								scwx-qt/source/scwx/qt/ui/keybind_edit.hpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,41 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <QLineEdit> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace qt | ||||
| { | ||||
| namespace ui | ||||
| { | ||||
| 
 | ||||
| class KeybindEdit : public QLineEdit | ||||
| { | ||||
|    Q_OBJECT | ||||
|    Q_DISABLE_COPY_MOVE(KeybindEdit) | ||||
| 
 | ||||
| public: | ||||
|    explicit KeybindEdit(QWidget* parent = nullptr); | ||||
|    ~KeybindEdit(); | ||||
| 
 | ||||
|    QKeySequence key_sequence() const; | ||||
| 
 | ||||
|    void set_key_sequence(const QKeySequence& sequence); | ||||
| 
 | ||||
| protected: | ||||
|    void focusInEvent(QFocusEvent* e) override; | ||||
|    void focusOutEvent(QFocusEvent* e) override; | ||||
|    void keyPressEvent(QKeyEvent* e) override; | ||||
|    void keyReleaseEvent(QKeyEvent* e) override; | ||||
| 
 | ||||
| signals: | ||||
|    void KeySequenceChanged(const QKeySequence& sequence); | ||||
| 
 | ||||
| private: | ||||
|    class Impl; | ||||
|    std::unique_ptr<Impl> p; | ||||
| }; | ||||
| 
 | ||||
| } // namespace ui
 | ||||
| } // namespace qt
 | ||||
| } // namespace scwx
 | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat