VirtualBox

Ignore:
Timestamp:
Jun 4, 2019 6:29:55 PM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
131102
Message:

FE/Qt: bugref:6143. Adding a key cap editor.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/softkeyboard
Files:
2 edited

Legend:

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

    r78964 r78975  
    1919#include <QApplication>
    2020#include <QFile>
     21#include <QLineEdit>
    2122#include <QMenu>
    2223#include <QPainter>
     
    6364*   UISoftKeyboardRow definition.                                                                                  *
    6465*********************************************************************************************************************************/
     66
    6567/** UISoftKeyboardRow represents a row in the physical keyboard. */
    6668class UISoftKeyboardRow
     
    9496*   UISoftKeyboardKey definition.                                                                                  *
    9597*********************************************************************************************************************************/
     98
    9699/** UISoftKeyboardKey is a place holder for a keyboard key. Graphical key represantations are drawn according to this class. */
    97100class UISoftKeyboardKey
     
    199202    void mouseReleaseEvent(QMouseEvent *pEvent) /* override */;
    200203    void mouseMoveEvent(QMouseEvent *pEvent) /* override */;
     204    bool eventFilter(QObject *pWatched, QEvent *pEvent)/* override */;
    201205
    202206    virtual void retranslateUi() /* override */;
     
    206210    void sltHandleMenuBarContextMenuRequest(const QPoint &point);
    207211    void sltHandleLoadLayout(QAction *pSenderAction);
    208     void sltHandleShowKeyPositions();
     212    void sltHandleKeyCapEdit();
    209213
    210214private:
     
    214218    /** Searches for the key which contains the position of the @p pEvent and returns it if found. */
    215219    UISoftKeyboardKey *keyUnderMouse(QMouseEvent *pEvent);
     220    UISoftKeyboardKey *keyUnderMouse(const QPoint &point);
    216221    void               handleKeyPress(UISoftKeyboardKey *pKey);
    217222    void               handleKeyRelease(UISoftKeyboardKey *pKey);
     
    222227
    223228    UISoftKeyboardKey *m_pKeyUnderMouse;
     229    UISoftKeyboardKey *m_pKeyBeingEdited;
     230
    224231    UISoftKeyboardKey *m_pKeyPressed;
    225232    QColor m_keyDefaultColor;
     
    248255    QAction *m_pLoadLayoutFileAction;
    249256    QAction *m_pLastSelectedLayout;
     257    QAction *m_pChangeKeyCapAction;
     258    QLineEdit *m_pKeyCapEditor;
    250259};
    251260
     
    362371void UISoftKeyboardKey::setKeyCap(const QString &strKeyCap)
    363372{
    364     if (m_strKeyCap.isEmpty())
    365         m_strKeyCap = strKeyCap;
    366     else
    367         m_strKeyCap += "\n" + strKeyCap;
     373    m_strKeyCap = strKeyCap;
    368374}
    369375
     
    545551    :QIWithRetranslateUI<QWidget>(pParent)
    546552    , m_pKeyUnderMouse(0)
     553    , m_pKeyBeingEdited(0)
    547554    , m_pKeyPressed(0)
    548555    , m_keyDefaultColor(QColor(103, 128, 159))
     
    562569    , m_pLoadLayoutFileAction(0)
    563570    , m_pLastSelectedLayout(0)
     571    , m_pChangeKeyCapAction(0)
    564572{
    565573
     
    597605    float fLedRadius =  0.8 * unitSize;
    598606    float fLedMargin =  0.6 * unitSize;
    599 
     607    if (!m_pKeyBeingEdited && m_pKeyCapEditor)
     608        m_pKeyCapEditor->hide();
    600609    for (int i = 0; i < m_rows.size(); ++i)
    601610    {
     
    609618            else
    610619                painter.setBrush(QBrush(m_keyDefaultColor));
     620
     621            if (&key == m_pKeyBeingEdited)
     622            {
     623                //m_pKeyCapEditor->setText(key.keyCap());
     624                m_pKeyCapEditor->setFont(painterFont);
     625                m_pKeyCapEditor->setGeometry(m_fScaleFactorX * key.keyGeometry().x(), m_fScaleFactorY * key.keyGeometry().y(),
     626                                             m_fScaleFactorX * key.keyGeometry().width(), m_fScaleFactorY * key.keyGeometry().height());
     627                m_pKeyCapEditor->show();
     628
     629            }
    611630
    612631            if (&key  == m_pKeyPressed)
     
    647666void UISoftKeyboardWidget::mousePressEvent(QMouseEvent *pEvent)
    648667{
     668    QWidget::mousePressEvent(pEvent);
    649669    if (pEvent->button() != Qt::LeftButton)
    650670        return;
     
    656676void UISoftKeyboardWidget::mouseReleaseEvent(QMouseEvent *pEvent)
    657677{
     678    QWidget::mouseReleaseEvent(pEvent);
    658679    if (pEvent->button() != Qt::LeftButton)
    659680        return;
     
    667688void UISoftKeyboardWidget::mouseMoveEvent(QMouseEvent *pEvent)
    668689{
     690    QWidget::mouseMoveEvent(pEvent);
    669691    keyUnderMouse(pEvent);
    670692}
    671693
    672 void UISoftKeyboard::focusOutEvent(QFocusEvent *pEvent)
    673 {
    674     QIWithRetranslateUI<QMainWindow>::focusOutEvent(pEvent);
    675     printf("booo\n");
    676 }
     694bool UISoftKeyboardWidget::eventFilter(QObject *pWatched, QEvent *pEvent)
     695{
     696    if (pWatched != m_pKeyCapEditor)
     697        return QIWithRetranslateUI<QWidget>::eventFilter(pWatched, pEvent);
     698
     699    switch (pEvent->type())
     700    {
     701        /* Process key press only: */
     702        case QEvent::KeyPress:
     703        {
     704            /* Cast to corresponding key press event: */
     705            QKeyEvent *pKeyEvent = static_cast<QKeyEvent*>(pEvent);
     706
     707            /* Handle Ctrl+T key combination as a shortcut to focus search field: */
     708            if (pKeyEvent->key() == Qt::Key_Escape)
     709            {
     710                m_pKeyBeingEdited = 0;
     711                update();
     712                return true;
     713            }
     714            else if (pKeyEvent->key() == Qt::Key_Enter || pKeyEvent->key() == Qt::Key_Return)
     715            {
     716                if (m_pKeyBeingEdited)
     717                    m_pKeyBeingEdited->setKeyCap(m_pKeyCapEditor->text());
     718                m_pKeyBeingEdited = 0;
     719                update();
     720                return true;
     721            }
     722            break;
     723        }
     724        default:
     725            break;
     726    }
     727
     728    return QIWithRetranslateUI<QWidget>::eventFilter(pWatched, pEvent);
     729}
     730
    677731
    678732void UISoftKeyboardWidget::retranslateUi()
     
    682736void UISoftKeyboardWidget::sltHandleMenuBarContextMenuRequest(const QPoint &point)
    683737{
     738    m_pChangeKeyCapAction->setEnabled(m_pKeyUnderMouse);
    684739    m_pContextMenu->exec(mapToGlobal(point));
    685740    update();
     
    719774}
    720775
    721 void UISoftKeyboardWidget::sltHandleShowKeyPositions()
    722 {
    723 
     776void UISoftKeyboardWidget::sltHandleKeyCapEdit()
     777{
     778    m_pKeyBeingEdited = m_pKeyUnderMouse;
     779    if (m_pKeyBeingEdited && m_pKeyCapEditor)
     780        m_pKeyCapEditor->setText(m_pKeyBeingEdited->keyCap());
    724781}
    725782
     
    739796{
    740797    QPoint eventPosition(pEvent->pos().x() / m_fScaleFactorX, pEvent->pos().y() / m_fScaleFactorY);
    741     QWidget::mousePressEvent(pEvent);
     798    return keyUnderMouse(eventPosition);
     799}
     800
     801UISoftKeyboardKey *UISoftKeyboardWidget::keyUnderMouse(const QPoint &eventPosition)
     802{
     803    UISoftKeyboardKey *pKey = 0;
    742804    for (int i = 0; i < m_rows.size(); ++i)
    743805    {
     
    748810            if (key.polygonInGlobal().containsPoint(eventPosition, Qt::OddEvenFill))
    749811            {
    750                 if (&key != m_pKeyUnderMouse)
    751                 {
    752                     m_pKeyUnderMouse = &key;
    753                     update();
    754                 }
    755                 return &key;
     812                pKey = &key;
    756813                break;
    757814            }
    758815        }
    759816    }
    760     return 0;
     817    if (m_pKeyUnderMouse != pKey)
     818    {
     819        m_pKeyUnderMouse = pKey;
     820        update();
     821    }
     822    return pKey;
    761823}
    762824
     
    894956void UISoftKeyboardWidget::prepareObjects()
    895957{
     958    m_pKeyCapEditor = new QLineEdit(this);
     959    if (m_pKeyCapEditor)
     960    {
     961        m_pKeyCapEditor->hide();
     962        m_pKeyCapEditor->installEventFilter(this);
     963    }
    896964    m_pContextMenu = new QMenu(this);
    897     //m_pShowPositionsAction = m_pContextMenu->addAction("
    898965    m_pLayoutActionGroup = new QActionGroup(this);
    899966    m_pLayoutActionGroup->setExclusive(true);
     
    914981
    915982    m_pShowPositionsAction = m_pContextMenu->addAction(UISoftKeyboard::tr("Show key positions instead of key caps"));
    916     connect(m_pLayoutActionGroup, &QActionGroup::triggered, this, &UISoftKeyboardWidget::sltHandleShowKeyPositions);
     983
    917984    m_pShowPositionsAction->setCheckable(true);
    918985    m_pShowPositionsAction->setChecked(false);
     986
     987    m_pChangeKeyCapAction = m_pContextMenu->addAction(UISoftKeyboard::tr("Add/change key cap"));
     988    connect(m_pChangeKeyCapAction, &QAction::triggered, this, &UISoftKeyboardWidget::sltHandleKeyCapEdit);
     989    QAction *pSaveKepCapFile = m_pContextMenu->addAction(UISoftKeyboard::tr("Save key caps to file"));
     990    Q_UNUSED(pSaveKepCapFile);
    919991
    920992    /* Choose the first layput action's data as the defaults one: */
     
    10181090    key.setWidth(row.defaultWidth());
    10191091    key.setHeight(row.defaultHeight());
    1020 
     1092    QString strKeyCap;
    10211093    while (m_xmlReader.readNextStartElement())
    10221094    {
     
    10381110        }
    10391111        else if (m_xmlReader.name() == "keycap")
    1040             key.setKeyCap(m_xmlReader.readElementText());
     1112        {
     1113            QString strCap = m_xmlReader.readElementText();
     1114            if (strKeyCap.isEmpty())
     1115                strKeyCap = strCap;
     1116            else
     1117                strKeyCap += "\n" + strCap;
     1118        }
    10411119        else if (m_xmlReader.name() == "cutout")
    10421120            parseCutout(key);
    1043        else if (m_xmlReader.name() == "position")
     1121        else if (m_xmlReader.name() == "position")
    10441122            key.setPosition(m_xmlReader.readElementText().toInt());
    10451123        else if (m_xmlReader.name() == "type")
     
    10541132            m_xmlReader.skipCurrentElement();
    10551133    }
     1134    key.setKeyCap(strKeyCap);
    10561135}
    10571136
  • trunk/src/VBox/Frontends/VirtualBox/src/softkeyboard/UISoftKeyboard.h

    r78964 r78975  
    5656
    5757    virtual void retranslateUi() /* override */;
    58     void focusOutEvent(QFocusEvent *pEvent) /* override */;
    5958
    6059private slots:
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