11#include < QVBoxLayout>
22#include < QKeyEvent>
33#include < QDebug>
4+ #include < QStyle>
45#include " ActionHotkeyWidget.hpp"
56#include " ImageManager.hpp"
67
78ActionHotkeyWidget::ActionHotkeyWidget (const QString& actionName,
8- const QString& hotkeyStr ,
9+ const QString& key ,
910 const QString& iconName,
1011 QWidget* parent)
1112 : QWidget{parent}
12- , hotkey{hotkeyStr }
13+ , hotkey{key }
1314 , actionNameLabel{actionName}
14- , hotkeyLabel{hotkey}
15- , newHotkeyButton{" +" }
15+ , btnHotkey{hotkey}
1616 , signalTimer{}
1717 , timerMseconds{1300 }
1818{
1919 // Object name for css
20- hotkeyLabel .setObjectName (" HotkeyLabel " );
21- hotkeyLabel. setEnabled ( false );
22- hotkeyLabel .setMinimumSize (ImageManager::DecodeMissingWebpIcon ().size ().width () + 15 , // Checked for letter W
23- ImageManager::DecodeMissingWebpIcon ().size ().height ());
20+ btnHotkey .setObjectName (" btnHotkey " );
21+ btnHotkey. setProperty ( " unique " , false );
22+ btnHotkey .setMinimumSize (ImageManager::DecodeMissingWebpIcon ().size ().width () + 15 , // Checked for letter W
23+ ImageManager::DecodeMissingWebpIcon ().size ().height ());
2424
25- connect (&newHotkeyButton , &QPushButton::pressed, this , &ActionHotkeyWidget::OnNewHotkeyPressed );
25+ connect (&btnHotkey , &QPushButton::pressed, this , &ActionHotkeyWidget::ChangeHotkeyClick );
2626
2727 // Signal timer settings
2828 signalTimer.setSingleShot (true );
2929 connect (&signalTimer, &QTimer::timeout, this , &ActionHotkeyWidget::SignalRepeatNewHotkey);
3030
31- QLabel* imageLb = new QLabel ();
32- imageLb->setPixmap (QPixmap::fromImage (ImageManager::DecodeWebpIcon (iconName)));
33-
34- // Hotkey label
35- hotkeyLabel.setAlignment (Qt::AlignCenter);
36-
37- // TODO: doesn't work
38- hotkeyLabel.setWindowOpacity (0.7 );
39-
40- // Square button size
41- newHotkeyButton.setFixedSize (newHotkeyButton.sizeHint ().height (), newHotkeyButton.sizeHint ().height ());
31+ QLabel* lblImage = new QLabel ();
32+ lblImage->setPixmap (QPixmap::fromImage (ImageManager::DecodeWebpIcon (iconName)));
4233
4334 QHBoxLayout* ltMainBlock = new QHBoxLayout{this };
4435 ltMainBlock->setAlignment (Qt::AlignTop);
45- ltMainBlock->addWidget (imageLb );
36+ ltMainBlock->addWidget (lblImage );
4637 ltMainBlock->addWidget (&actionNameLabel);
4738 // Move action name label to left
4839 ltMainBlock->setStretch (1 , 1 );
49- ltMainBlock->addWidget (&hotkeyLabel);
50- ltMainBlock->addWidget (&newHotkeyButton);
40+ ltMainBlock->addWidget (&btnHotkey);
5141 setLayout (ltMainBlock);
5242}
5343
54- QString ActionHotkeyWidget::GetActionName () const
55- {
56- return actionNameLabel.text ();
57- }
44+ QString ActionHotkeyWidget::GetActionName () const { return actionNameLabel.text (); }
5845
59- QString ActionHotkeyWidget::GetHotkey () const
60- {
61- return hotkeyLabel.text ();
62- }
46+ QString ActionHotkeyWidget::GetHotkey () const { return btnHotkey.text (); }
6347
64- void ActionHotkeyWidget::HighlightKey (bool collision )
48+ void ActionHotkeyWidget::HighlightKey (bool isKeysMoreThanTwo )
6549{
66- if (collision )
67- hotkeyLabel. setEnabled ( true );
50+ if (isKeysMoreThanTwo )
51+ btnHotkey. setProperty ( " unique " , false );
6852 else
69- hotkeyLabel.setDisabled (true );
53+ btnHotkey.setProperty (" unique" , true );
54+
55+ btnHotkey.style ()->unpolish (&btnHotkey);
56+ btnHotkey.style ()->polish (&btnHotkey);
57+
58+ btnHotkey.update ();
7059}
7160
7261void ActionHotkeyWidget::keyPressEvent (QKeyEvent* event)
@@ -83,61 +72,61 @@ void ActionHotkeyWidget::keyPressEvent(QKeyEvent* event)
8372 {
8473 // Set new text
8574 hotkey = QKeySequence (key).toString ();
86- hotkeyLabel.setEnabled (false );
8775
8876 // If the key is correct -> disconnect the input error reset signal
89- disconnect (this , &ActionHotkeyWidget::SignalRepeatNewHotkey, this , &ActionHotkeyWidget::OnNewHotkeyPressed );
77+ disconnect (this , &ActionHotkeyWidget::SignalRepeatNewHotkey, this , &ActionHotkeyWidget::ChangeHotkeyClick );
9078
9179 // Return focus to parent
9280 parentWidget ()->setFocus ();
9381 }
9482 else
9583 {
96- if (KEYBOARD_KEYS.contains (key))
97- hotkeyLabel .setText (tr (" This key doesn 't allowed !" ));
84+ if (! KEYBOARD_KEYS.contains (key))
85+ btnHotkey .setText (tr (" It isn 't latin key !" ));
9886 else
99- hotkeyLabel .setText (tr (" It isn 't latin key !" ));
87+ btnHotkey .setText (tr (" This key doesn 't allowed !" ));
10088
10189 // Start the signal timer with a delay of n seconds
10290 if (signalTimer.isActive ())
10391 signalTimer.stop ();
10492
10593 signalTimer.start (timerMseconds);
106- hotkeyLabel.setEnabled (true );
10794 }
95+
96+ btnHotkey.setProperty (" unique" , true );
10897 QWidget::keyPressEvent (event);
10998}
11099
111100void ActionHotkeyWidget::focusOutEvent (QFocusEvent* event)
112101{
113102 // Unset decoration
114- QFont fnt (hotkeyLabel .font ());
103+ QFont fnt (btnHotkey .font ());
115104 fnt.setItalic (false );
116- hotkeyLabel .setFont (fnt);
117- hotkeyLabel .setText (hotkey);
105+ btnHotkey .setFont (fnt);
106+ btnHotkey .setText (hotkey);
118107
119108 emit HotkeyChanged (hotkey);
120- if (!hotkeyLabel. isEnabled () )
121- hotkeyLabel. setEnabled ( false );
109+ if (btnHotkey. property ( " unique " ). Bool )
110+ btnHotkey. setProperty ( " unique " , false );
122111
123112 // Stop timer
124113 signalTimer.stop ();
125114
126115 QWidget::focusOutEvent (event);
127116}
128117
129- void ActionHotkeyWidget::OnNewHotkeyPressed ()
118+ void ActionHotkeyWidget::ChangeHotkeyClick ()
130119{
131120 // Reconnect the input error reset signal
132- disconnect (this , &ActionHotkeyWidget::SignalRepeatNewHotkey, this , &ActionHotkeyWidget::OnNewHotkeyPressed );
133- connect (this , &ActionHotkeyWidget::SignalRepeatNewHotkey, this , &ActionHotkeyWidget::OnNewHotkeyPressed );
121+ disconnect (this , &ActionHotkeyWidget::SignalRepeatNewHotkey, this , &ActionHotkeyWidget::ChangeHotkeyClick );
122+ connect (this , &ActionHotkeyWidget::SignalRepeatNewHotkey, this , &ActionHotkeyWidget::ChangeHotkeyClick );
134123
135124 // Decorate
136- hotkeyLabel .setText (tr (" Press latin key..." ));
137- hotkeyLabel. setEnabled ( false );
138- QFont f (hotkeyLabel .font ());
125+ btnHotkey .setText (tr (" Press latin key..." ));
126+ btnHotkey. setProperty ( " unique " , true );
127+ QFont f (btnHotkey .font ());
139128 f.setItalic (true );
140- hotkeyLabel .setFont (f);
129+ btnHotkey .setFont (f);
141130
142131 // Set focus on hotkey element
143132 setFocus ();
0 commit comments