VirtualBox

Ignore:
Timestamp:
Jun 3, 2019 8:36:10 AM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
131041
Message:

FE/Qt: bugref:6143. Some more refactoring

Location:
trunk/src/VBox/Frontends/VirtualBox
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/softkeyboard/UISoftKeyboard.cpp

    r78891 r78921  
    3636#include "CEventSource.h"
    3737
    38 
     38/* Forward declarations: */
     39class UISoftKeyboardWidget;
    3940
    4041enum UIKeyState
     
    4849enum UIKeyType
    4950{
     51    /** Can be in UIKeyState_NotPressed and UIKeyState_Pressed states. */
    5052    UIKeyType_Ordinary,
    51     /** e.g. CapsLock. Can be only in UIKeyState_NotPressed, UIKeyState_Locked, */
     53    /** e.g. CapsLock. Can be only in UIKeyState_NotPressed, UIKeyState_Locked */
    5254    UIKeyType_Toggleable,
    5355    /** e.g. Shift Can be in all 3 states*/
     
    126128    void setCutout(int iCorner, int iWidth, int iHeight);
    127129
     130    void setParentWidget(UISoftKeyboardWidget* pParent);
    128131    UIKeyState state() const;
    129132    QVector<LONG> scanCodeWithPrefix() const;
    130133
    131134    void release();
     135    void press();
    132136
    133137    void setPolygon(const QPolygon &polygon);
     
    163167    /** Key's position in the layout. */
    164168    int        m_iPosition;
     169    UISoftKeyboardWidget  *m_pParentWidget;
    165170};
    166171
     
    172177    Q_OBJECT;
    173178
     179signals:
     180
     181    void sigPutKeyboardSequence(QVector<LONG> sequence);
     182
    174183public:
    175184
    176185    UISoftKeyboardWidget(QWidget *pParent = 0);
     186
    177187    virtual QSize minimumSizeHint() const;
    178188    virtual QSize sizeHint() const;
    179189    void setNewMinimumSize(const QSize &size);
    180     QVector<UISoftKeyboardRow> m_rows;
     190
     191    QVector<UISoftKeyboardRow> &rows();
     192    const QVector<UISoftKeyboardRow> &rows() const;
     193
    181194    void setInitialSize(int iWidth, int iHeight);
     195    void keyStateChange(UISoftKeyboardKey* pKey);
    182196
    183197protected:
    184198
    185199    void paintEvent(QPaintEvent *pEvent) /* override */;
    186     void mousePressEvent(QMouseEvent *pEvent);
     200    void mousePressEvent(QMouseEvent *pEvent) /* override */;
     201    void mouseReleaseEvent(QMouseEvent *pEvent) /* override */;
    187202    void mouseMoveEvent(QMouseEvent *pEvent);
    188203
     
    190205
    191206    UISoftKeyboardKey *keyUnderMouse(QMouseEvent *pEvent);
    192 
     207    void               handleKeyPress(UISoftKeyboardKey *pKey);
     208    void               handleKeyRelease(UISoftKeyboardKey *pKey);
    193209    QSize m_minimumSize;
    194210    int m_iInitialHeight;
     
    197213    float m_fMultiplierY;
    198214    UISoftKeyboardKey *m_pKeyUnderMouse;
     215    UISoftKeyboardKey *m_pKeyPressed;
    199216    QColor m_keyDefaultColor;
    200217    QColor m_keyHoverColor;
    201     QColor m_keyPressColor;
    202218    QColor m_textDefaultColor;
     219    QColor m_textPressedColor;
     220    QVector<UISoftKeyboardKey*> m_pressedModifiers;
     221    QVector<UISoftKeyboardRow> m_rows;
    203222};
    204223
     
    294313    , m_iCutoutCorner(-1)
    295314    , m_iPosition(0)
     315    , m_pParentWidget(0)
    296316{
    297317}
     
    402422}
    403423
     424void UISoftKeyboardKey::setParentWidget(UISoftKeyboardWidget* pParent)
     425{
     426    m_pParentWidget = pParent;
     427}
     428
    404429void UISoftKeyboardKey::release()
    405430{
    406431    updateState(false);
     432}
     433
     434void UISoftKeyboardKey::press()
     435{
     436    updateState(true);
    407437}
    408438
     
    441471void UISoftKeyboardKey::updateState(bool fPressed)
    442472{
    443     if (m_enmType == UIKeyType_Ordinary)
    444         return;
     473    UIKeyState enmPreviousState = state();
    445474    if (m_enmType == UIKeyType_Modifier)
    446475    {
     
    470499        }
    471500    }
     501    else if (m_enmType == UIKeyType_Ordinary)
     502    {
     503        if (m_enmState == UIKeyState_NotPressed)
     504            m_enmState = UIKeyState_Pressed;
     505        else
     506            m_enmState = UIKeyState_NotPressed;
     507    }
     508    if (enmPreviousState != state() && m_pParentWidget)
     509        m_pParentWidget->keyStateChange(this);
    472510}
    473511
     
    479517    :QWidget(pParent)
    480518    , m_pKeyUnderMouse(0)
    481     , m_keyDefaultColor(QColor(255, 228, 192))
    482     , m_keyHoverColor(QColor(238, 208, 169))
    483     , m_keyPressColor(QColor(248, 218, 179))
    484     , m_textDefaultColor(QColor(0, 0, 0))
     519    , m_pKeyPressed(0)
     520    , m_keyDefaultColor(QColor(103, 128, 159))
     521    , m_keyHoverColor(QColor(108, 122, 137))
     522    , m_textDefaultColor(QColor(46, 49, 49))
     523    , m_textPressedColor(QColor(149, 165, 166))
    485524{
    486525    setMouseTracking(true);
     
    507546    m_iInitialWidth = iWidth;
    508547    m_iInitialHeight = iHeight;
     548}
     549
     550QVector<UISoftKeyboardRow> &UISoftKeyboardWidget::rows()
     551{
     552    return m_rows;
     553}
     554
     555const QVector<UISoftKeyboardRow> &UISoftKeyboardWidget::rows() const
     556{
     557    return m_rows;
    509558}
    510559
     
    524573    painter.setFont(painterFont);
    525574    painter.setRenderHint(QPainter::Antialiasing);
    526     painter.setPen(QPen(QColor(m_textDefaultColor), 2));
    527     painter.setBrush(QBrush(m_keyDefaultColor));
    528575    painter.scale(m_fMultiplierX, m_fMultiplierY);
     576    int unitSize = qApp->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing);
     577    float fLedRadius =  1.5 * unitSize;
     578    float fLedMargin =  unitSize;
     579
    529580    for (int i = 0; i < m_rows.size(); ++i)
    530581    {
     
    534585            UISoftKeyboardKey &key = keys[j];
    535586            painter.translate(key.keyGeometry().x(), key.keyGeometry().y());
     587            if (&key  == m_pKeyUnderMouse)
     588                painter.setBrush(QBrush(m_keyHoverColor));
     589            else
     590                painter.setBrush(QBrush(m_keyDefaultColor));
     591
     592            if (&key  == m_pKeyPressed)
     593                painter.setPen(QPen(QColor(m_textPressedColor), 2));
     594            else
     595                painter.setPen(QPen(QColor(m_textDefaultColor), 2));
     596
    536597            painter.drawPolygon(key.polygon());
    537             QRect textRect(5, 5, key.keyGeometry().width(), key.keyGeometry().height());
     598
     599            QRect textRect(0.8 * unitSize, 2 * unitSize
     600                           , key.keyGeometry().width(), key.keyGeometry().height());
    538601            painter.drawText(textRect, Qt::TextWordWrap, key.keyCap());
     602
     603            if (key.type() != UIKeyType_Ordinary)
     604            {
     605                QColor ledColor;
     606                if (key.state() == UIKeyState_NotPressed)
     607                    ledColor = m_textDefaultColor;
     608                else if (key.state() == UIKeyState_Pressed)
     609                    ledColor = QColor(0, 255, 0);
     610                else
     611                    ledColor = QColor(255, 0, 0);
     612                painter.setBrush(ledColor);
     613                painter.setPen(ledColor);
     614                QRectF rectangle(key.keyGeometry().width() - 2 * fLedMargin, fLedMargin, fLedRadius, fLedRadius);
     615                painter.drawEllipse(rectangle);
     616            }
    539617            painter.translate(-key.keyGeometry().x(), -key.keyGeometry().y());
     618
    540619        }
    541620    }
    542 
    543     if (m_pKeyUnderMouse)
    544     {
    545         painter.setBrush(QBrush(m_keyHoverColor));
    546         painter.translate(m_pKeyUnderMouse->keyGeometry().x(), m_pKeyUnderMouse->keyGeometry().y());
    547         painter.drawPolygon(m_pKeyUnderMouse->polygon());
    548         QRect textRect(5, 5, m_pKeyUnderMouse->keyGeometry().width(), m_pKeyUnderMouse->keyGeometry().height());
    549         painter.drawText(textRect, Qt::TextWordWrap, m_pKeyUnderMouse->keyCap());
    550         painter.translate(-m_pKeyUnderMouse->keyGeometry().x(), -m_pKeyUnderMouse->keyGeometry().y());
    551     }
    552621}
    553622
    554623void UISoftKeyboardWidget::mousePressEvent(QMouseEvent *pEvent)
    555624{
    556     keyUnderMouse(pEvent);
     625    m_pKeyPressed = keyUnderMouse(pEvent);
     626    handleKeyPress(m_pKeyPressed);
     627    update();
     628}
     629
     630void UISoftKeyboardWidget::mouseReleaseEvent(QMouseEvent *)
     631{
     632    if (!m_pKeyPressed)
     633        return;
     634    handleKeyRelease(m_pKeyPressed);
     635    m_pKeyPressed = 0;
     636    update();
    557637}
    558638
     
    585665    }
    586666    return 0;
     667}
     668
     669void UISoftKeyboardWidget::handleKeyPress(UISoftKeyboardKey *pKey)
     670{
     671    if (!pKey)
     672        return;
     673    pKey->press();
     674
     675    if (pKey->type() == UIKeyType_Modifier)
     676        return;
     677
     678    QVector<LONG> sequence;
     679
     680     /* Add the pressed modifiers first: */
     681    for (int i = 0; i < m_pressedModifiers.size(); ++i)
     682    {
     683        UISoftKeyboardKey *pModifier = m_pressedModifiers[i];
     684        if (pModifier->scanCodePrefix() != 0)
     685            sequence << pModifier->scanCodePrefix();
     686        sequence << pModifier->scanCode();
     687    }
     688
     689    if (pKey->scanCodePrefix() != 0)
     690        sequence << pKey->scanCodePrefix();
     691    sequence << pKey->scanCode();
     692    emit sigPutKeyboardSequence(sequence);
     693}
     694
     695void UISoftKeyboardWidget::keyStateChange(UISoftKeyboardKey* pKey)
     696{
     697    if (!pKey)
     698        return;
     699    if (pKey->type() == UIKeyType_Modifier)
     700    {
     701        if (pKey->state() == UIKeyState_NotPressed)
     702            m_pressedModifiers.removeOne(pKey);
     703        else
     704            if (!m_pressedModifiers.contains(pKey))
     705                m_pressedModifiers.append(pKey);
     706    }
     707}
     708
     709void UISoftKeyboardWidget::handleKeyRelease(UISoftKeyboardKey *pKey)
     710{
     711    if (!pKey)
     712        return;
     713    if (pKey->type() == UIKeyType_Ordinary)
     714        pKey->release();
     715    /* We only send the scan codes of Ordinary keys: */
     716    if (pKey->type() == UIKeyType_Modifier)
     717        return;
     718
     719    QVector<LONG> sequence;
     720    if (pKey->scanCodePrefix() != 0)
     721        sequence <<  pKey->scanCodePrefix();
     722    sequence << (pKey->scanCode() | 0x80);
     723
     724    /* Add the pressed modifiers in the reverse order: */
     725    for (int i = m_pressedModifiers.size() - 1; i >= 0; --i)
     726    {
     727        UISoftKeyboardKey *pModifier = m_pressedModifiers[i];
     728        if (pModifier->scanCodePrefix() != 0)
     729            sequence << pModifier->scanCodePrefix();
     730        sequence << (pModifier->scanCode() | 0x80);
     731        /* Release the pressed modifiers (if there are not locked): */
     732        pModifier->release();
     733    }
     734    emit sigPutKeyboardSequence(sequence);
    587735}
    588736
     
    693841       else if (m_xmlReader.name() == "position")
    694842            key.setPosition(m_xmlReader.readElementText().toInt());
    695         // else if (m_xmlReader.name() == "type")
    696         // {
    697         //     QString strType = m_xmlReader.readElementText();
    698         //     if (strType == "modifier")
    699         //         pKey->setType(UIKeyType_Modifier);
    700         //     else if (strType == "toggleable")
    701         //         pKey->setType(UIKeyType_Toggleable);
    702         // }
     843        else if (m_xmlReader.name() == "type")
     844        {
     845            QString strType = m_xmlReader.readElementText();
     846            if (strType == "modifier")
     847                key.setType(UIKeyType_Modifier);
     848            else if (strType == "toggleable")
     849                key.setType(UIKeyType_Toggleable);
     850        }
    703851        else
    704852            m_xmlReader.skipCurrentElement();
     
    812960    , m_pContainerWidget(0)
    813961    , m_strMachineName(strMachineName)
    814 
    815     , m_fKeepAspectRatio(false)
    816962    , m_iXSpacing(5)
    817963    , m_iYSpacing(5)
     
    823969    setAttribute(Qt::WA_DeleteOnClose);
    824970    prepareObjects();
    825     parseLayout();
     971    createKeyboard();
    826972    prepareConnections();
    827973    prepareToolBar();
     
    839985}
    840986
    841 void UISoftKeyboard::sltHandleKeyPress()
    842 {
    843 //     UISoftKeyboardKey *pKey = qobject_cast<UISoftKeyboardKey*>(sender());
    844 //     if (!pKey)
    845 //         return;
    846 //     if (pKey->type() == UIKeyType_Modifier)
    847 //         return;
    848 
    849 //     QVector<LONG> sequence;
    850 
    851 //     /* Add the pressed modifiers first: */
    852 //     for (int i = 0; i < m_pressedModifiers.size(); ++i)
    853 //     {
    854 //         UISoftKeyboardKey *pModifier = m_pressedModifiers[i];
    855 //         if (pModifier->scanCodePrefix() != 0)
    856 //             sequence << pModifier->scanCodePrefix();
    857 //         sequence << pModifier->scanCode();
    858 //     }
    859 
    860 //     if (pKey->scanCodePrefix() != 0)
    861 //         sequence << pKey->scanCodePrefix();
    862 //     sequence << pKey->scanCode();
    863 //     keyboard().PutScancodes(sequence);
    864 }
    865 
    866 void UISoftKeyboard::sltHandleKeyRelease()
    867 {
    868     //UISoftKeyboardKey *pKey = qobject_cast<UISoftKeyboardKey*>(sender());
    869 //     if (!pKey)
    870 //         return;
    871 //     /* We only send the scan codes of Ordinary keys: */
    872 //     if (pKey->type() == UIKeyType_Modifier)
    873 //         return;
    874 
    875 //     QVector<LONG> sequence;
    876 //     if (pKey->scanCodePrefix() != 0)
    877 //         sequence <<  pKey->scanCodePrefix();
    878 //     sequence << (pKey->scanCode() | 0x80);
    879 
    880 //     /* Add the pressed modifiers in the reverse order: */
    881 //     for (int i = m_pressedModifiers.size() - 1; i >= 0; --i)
    882 //     {
    883 //         UISoftKeyboardKey *pModifier = m_pressedModifiers[i];
    884 //         if (pModifier->scanCodePrefix() != 0)
    885 //             sequence << pModifier->scanCodePrefix();
    886 //         sequence << (pModifier->scanCode() | 0x80);
    887 //         /* Release the pressed modifiers (if there are not locked): */
    888 //         pModifier->release();
    889 //     }
    890 //     keyboard().PutScancodes(sequence);
    891 }
    892 
    893 void UISoftKeyboard::sltHandleModifierStateChange()
    894 {
    895     // UISoftKeyboardKey *pKey = qobject_cast<UISoftKeyboardKey*>(sender());
    896     // if (!pKey)
    897     //     return;
    898     // if (pKey->type() != UIKeyType_Modifier)
    899     //     return;
    900     // if (pKey->state() == UIKeyState_NotPressed)
    901     // {
    902     //     m_pressedModifiers.removeOne(pKey);
    903     // }
    904     // else
    905     // {
    906     //     if (!m_pressedModifiers.contains(pKey))
    907     //         m_pressedModifiers.append(pKey);
    908     // }
    909 }
    910 
    911987void UISoftKeyboard::sltHandleKeyboardLedsChange()
    912988{
    913989    // bool fNumLockLed = m_pSession->isNumLock();
    914990    // bool fCapsLockLed = m_pSession->isCapsLock();
    915     // bool fScrollLock = m_pSession->isScrollLock();
     991    // bool fScrollLockLed = m_pSession->isScrollLock();
     992}
     993
     994void UISoftKeyboard::sltHandlePutKeyboardSequence(QVector<LONG> sequence)
     995{
     996    keyboard().PutScancodes(sequence);
    916997}
    917998
     
    9291010{
    9301011    connect(m_pSession, &UISession::sigKeyboardLedsChange, this, &UISoftKeyboard::sltHandleKeyboardLedsChange);
     1012    connect(m_pContainerWidget, &UISoftKeyboardWidget::sigPutKeyboardSequence, this, &UISoftKeyboard::sltHandlePutKeyboardSequence);
    9311013}
    9321014
     
    9431025}
    9441026
    945 void UISoftKeyboard::parseLayout()
     1027void UISoftKeyboard::createKeyboard()
    9461028{
    9471029    if (!m_pContainerWidget)
    9481030        return;
    9491031    UIKeyboardLayoutReader reader;
    950     QVector<UISoftKeyboardRow> &rows = m_pContainerWidget->m_rows;
     1032    QVector<UISoftKeyboardRow> &rows = m_pContainerWidget->rows();
    9511033    if (!reader.parseXMLFile(":/102_iso.xml", rows))
    9521034        return;
    9531035    int iY = m_iTopMargin;
    9541036    int iMaxWidth = 0;
     1037
    9551038    for (int i = 0; i < rows.size(); ++i)
    9561039    {
     
    9631046            key.setKeyGeometry(QRect(iX, iY, key.width(), key.height()));
    9641047            key.setPolygon(QPolygon(UIKeyboardLayoutReader::computeKeyVertices(key)));
     1048            key.setParentWidget(m_pContainerWidget);
    9651049            iX += key.width();
    9661050            if (j < row.keys().size() - 1)
  • trunk/src/VBox/Frontends/VirtualBox/src/softkeyboard/UISoftKeyboard.h

    r78891 r78921  
    5959private slots:
    6060
    61     void sltHandleKeyPress();
    62     void sltHandleKeyRelease();
    63     void sltHandleModifierStateChange();
    6461    void sltHandleKeyboardLedsChange();
     62    void sltHandlePutKeyboardSequence(QVector<LONG> sequence);
    6563
    6664private:
     
    7169    void saveSettings();
    7270    void loadSettings();
    73     void parseLayout();
     71    void createKeyboard();
    7472    CKeyboard& keyboard() const;
    7573
     
    7876    UISoftKeyboardWidget       *m_pContainerWidget;
    7977    QString       m_strMachineName;
    80     QVector<UISoftKeyboardKey*> m_pressedModifiers;
    81     bool          m_fKeepAspectRatio;
    8278
    8379    int m_iXSpacing;
  • trunk/src/VBox/Frontends/VirtualBox/xml/102_iso.xml

    r78884 r78921  
    8181        </space>
    8282        <key>
    83             <keycap>PrtScn</keycap>
     83            <keycap>Prt</keycap>
     84            <keycap>Scn</keycap>
    8485            <position>124</position>
    8586            <scancodeprefix>0xe0</scancodeprefix>
     
    294295        </space>
    295296        <key>
    296             <keycap>Delete</keycap>
     297            <keycap>Del</keycap>
    297298            <position>76</position>
    298299            <scancode>0x53</scancode>
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette