VirtualBox

Changeset 78598 in vbox for trunk/src


Ignore:
Timestamp:
May 20, 2019 1:42:42 PM (6 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:6248: D&D attachment movement, initial implementation.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/settings/machine
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/settings/machine/UIMachineSettingsStorage.cpp

    r78597 r78598  
    1717
    1818/* Qt includes: */
     19#include <QDrag>
     20#include <QDragMoveEvent>
    1921#include <QHeaderView>
    2022#include <QItemEditorFactory>
    2123#include <QMenu>
     24#include <QMimeData>
    2225#include <QMouseEvent>
    2326#include <QScrollBar>
     
    19591962
    19601963        /* Then we have to make sure moved attachment is valid: */
    1961         if (enmDeviceType != KDeviceType_Null && !uMediumId.isNull())
     1964        if (enmDeviceType != KDeviceType_Null)
    19621965        {
    19631966            /* And create new attachment item finally: */
     
    22452248};
    22462249
     2250
     2251/* static */
     2252const QString UIMachineSettingsStorage::ControllerMimeType = QString("application/virtualbox;value=StorageControllerID");
     2253const QString UIMachineSettingsStorage::AttachmentMimeType = QString("application/virtualbox;value=StorageAttachmentID");
    22472254
    22482255UIMachineSettingsStorage::UIMachineSettingsStorage()
     
    34313438void UIMachineSettingsStorage::sltHandleMouseMove(QMouseEvent *pEvent)
    34323439{
     3440    /* Make sure event is valid: */
     3441    AssertPtrReturnVoid(pEvent);
     3442
    34333443    const QModelIndex index = m_pTreeStorage->indexAt(pEvent->pos());
    34343444    const QRect indexRect = m_pTreeStorage->visualRect(index);
     
    35143524    if (m_pModelStorage->data(index, StorageModel::R_ToolTipType).value<StorageModel::ToolTipType>() != StorageModel::DefaultToolTip)
    35153525        m_pModelStorage->setData(index, StorageModel::DefaultToolTip, StorageModel::R_ToolTipType);
     3526
     3527    /* Check whether we should initiate dragging: */
     3528    if (   !m_mousePressPosition.isNull()
     3529        && QLineF(pEvent->screenPos(), m_mousePressPosition).length() >= QApplication::startDragDistance())
     3530    {
     3531        /* Forget last mouse press position: */
     3532        m_mousePressPosition = QPoint();
     3533
     3534        /* Check what item we are hovering currently: */
     3535        QModelIndex index = m_pTreeStorage->indexAt(pEvent->pos());
     3536        AbstractItem *pItem = static_cast<AbstractItem*>(index.internalPointer());
     3537        /* And make sure this is attachment item, we are supporting dragging for this kind only: */
     3538        AttachmentItem *pItemAttachment = qobject_cast<AttachmentItem*>(pItem);
     3539        if (pItemAttachment)
     3540        {
     3541            /* Initialize dragging: */
     3542            QDrag *pDrag = new QDrag(this);
     3543            if (pDrag)
     3544            {
     3545                /* Assign pixmap: */
     3546                pDrag->setPixmap(pItem->pixmap());
     3547                /* Prepare mime: */
     3548                QMimeData *pMimeData = new QMimeData;
     3549                if (pMimeData)
     3550                {
     3551                    pMimeData->setData(ControllerMimeType, pItemAttachment->parent()->id().toString().toLatin1());
     3552                    pMimeData->setData(AttachmentMimeType, pItemAttachment->id().toString().toLatin1());
     3553                    pDrag->setMimeData(pMimeData);
     3554                }
     3555                /* Start dragging: */
     3556                pDrag->exec();
     3557            }
     3558        }
     3559    }
    35163560}
    35173561
    35183562void UIMachineSettingsStorage::sltHandleMouseClick(QMouseEvent *pEvent)
    35193563{
     3564    /* Make sure event is valid: */
     3565    AssertPtrReturnVoid(pEvent);
     3566
     3567    /* Remember last mouse press position: */
     3568    m_mousePressPosition = pEvent->globalPos();
     3569
    35203570    const QModelIndex index = m_pTreeStorage->indexAt(pEvent->pos());
    35213571    const QRect indexRect = m_pTreeStorage->visualRect(index);
     
    35773627}
    35783628
     3629void UIMachineSettingsStorage::sltHandleMouseRelease(QMouseEvent *)
     3630{
     3631    /* Forget last mouse press position: */
     3632    m_mousePressPosition = QPoint();
     3633}
     3634
     3635void UIMachineSettingsStorage::sltHandleDragEnter(QDragEnterEvent *pEvent)
     3636{
     3637    /* Make sure event is valid: */
     3638    AssertPtrReturnVoid(pEvent);
     3639
     3640    /* Accept event but not the proposed action: */
     3641    pEvent->accept();
     3642}
     3643
     3644void UIMachineSettingsStorage::sltHandleDragMove(QDragMoveEvent *pEvent)
     3645{
     3646    /* Make sure event is valid: */
     3647    AssertPtrReturnVoid(pEvent);
     3648    /* And mime-data is set: */
     3649    const QMimeData *pMimeData = pEvent->mimeData();
     3650    AssertPtrReturnVoid(pMimeData);
     3651
     3652    /* Make sure mime-data format is valid: */
     3653    if (   !pMimeData->hasFormat(UIMachineSettingsStorage::ControllerMimeType)
     3654        || !pMimeData->hasFormat(UIMachineSettingsStorage::AttachmentMimeType))
     3655        return;
     3656
     3657    /* Get controller id: */
     3658    const QString strControllerId = pMimeData->data(UIMachineSettingsStorage::ControllerMimeType);
     3659
     3660    /* Check what item we are hovering currently: */
     3661    QModelIndex index = m_pTreeStorage->indexAt(pEvent->pos());
     3662    AbstractItem *pItem = static_cast<AbstractItem*>(index.internalPointer());
     3663    /* And make sure this is controller item, we are supporting dropping for this kind only: */
     3664    ControllerItem *pItemController = qobject_cast<ControllerItem*>(pItem);
     3665    if (!pItemController || pItemController->id().toString() == strControllerId)
     3666        return;
     3667    /* Also make sure there is enough place for new attachment: */
     3668    const KStorageBus enmBus = pItemController->ctrBusType();
     3669    const int uMaxPortCount = vboxGlobal().virtualBox().GetSystemProperties().GetMaxPortCountForStorageBus(enmBus);
     3670    const int uMaxDevicePerPortCount = vboxGlobal().virtualBox().GetSystemProperties().GetMaxDevicesPerPortForStorageBus(enmBus);
     3671    const SlotsList usedSlots = pItemController->ctrUsedSlots();
     3672    if (usedSlots.size() >= uMaxPortCount * uMaxDevicePerPortCount)
     3673        return;
     3674
     3675    /* Accept drag-enter event: */
     3676    pEvent->acceptProposedAction();
     3677}
     3678
     3679void UIMachineSettingsStorage::sltHandleDragDrop(QDropEvent *pEvent)
     3680{
     3681    /* Make sure event is valid: */
     3682    AssertPtrReturnVoid(pEvent);
     3683    /* And mime-data is set: */
     3684    const QMimeData *pMimeData = pEvent->mimeData();
     3685    AssertPtrReturnVoid(pMimeData);
     3686
     3687    /* Check what item we are hovering currently: */
     3688    QModelIndex index = m_pTreeStorage->indexAt(pEvent->pos());
     3689    AbstractItem *pItem = static_cast<AbstractItem*>(index.internalPointer());
     3690    /* And make sure this is controller item, we are supporting dropping for this kind only: */
     3691    ControllerItem *pItemController = qobject_cast<ControllerItem*>(pItem);
     3692    if (pItemController)
     3693    {
     3694        /* Get controller/attachment ids: */
     3695        const QString strControllerId = pMimeData->data(UIMachineSettingsStorage::ControllerMimeType);
     3696        const QString strAttachmentId = pMimeData->data(UIMachineSettingsStorage::AttachmentMimeType);
     3697        m_pModelStorage->moveAttachment(strAttachmentId, strControllerId, pItemController->id());
     3698    }
     3699}
     3700
    35793701void UIMachineSettingsStorage::prepare()
    35803702{
     
    36333755        mLsLeftPane->setBuddy(m_pTreeStorage);
    36343756        m_pTreeStorage->setMouseTracking(true);
     3757        m_pTreeStorage->setAcceptDrops(true);
    36353758        m_pTreeStorage->setContextMenuPolicy(Qt::CustomContextMenu);
    36363759
     
    38583981    connect(m_pTreeStorage, SIGNAL(mouseMoved(QMouseEvent *)),
    38593982             this, SLOT(sltHandleMouseMove(QMouseEvent *)));
    3860     connect(m_pTreeStorage, SIGNAL(mousePressed(QMouseEvent *)),
    3861              this, SLOT(sltHandleMouseClick(QMouseEvent *)));
     3983    connect(m_pTreeStorage, &QITreeView::mousePressed,
     3984            this, &UIMachineSettingsStorage::sltHandleMouseClick);
     3985    connect(m_pTreeStorage, &QITreeView::mouseReleased,
     3986            this, &UIMachineSettingsStorage::sltHandleMouseRelease);
    38623987    connect(m_pTreeStorage, SIGNAL(mouseDoubleClicked(QMouseEvent *)),
    38633988             this, SLOT(sltHandleMouseClick(QMouseEvent *)));
     3989    connect(m_pTreeStorage, &QITreeView::dragEntered,
     3990            this, &UIMachineSettingsStorage::sltHandleDragEnter);
     3991    connect(m_pTreeStorage, &QITreeView::dragMoved,
     3992            this, &UIMachineSettingsStorage::sltHandleDragMove);
     3993    connect(m_pTreeStorage, &QITreeView::dragDropped,
     3994            this, &UIMachineSettingsStorage::sltHandleDragDrop);
    38643995
    38653996    /* Create model: */
  • trunk/src/VBox/Frontends/VirtualBox/src/settings/machine/UIMachineSettingsStorage.h

    r78597 r78598  
    556556public:
    557557
     558    /** Holds the controller mime-type for the D&D system. */
     559    static const QString ControllerMimeType;
     560    /** Holds the attachment mime-type for the D&D system. */
     561    static const QString AttachmentMimeType;
     562
    558563    /** Constructs Storage settings page. */
    559564    UIMachineSettingsStorage();
     
    676681    /** Handles mouse-click @a pEvent. */
    677682    void sltHandleMouseClick(QMouseEvent *pEvent);
     683    /** Handles mouse-release @a pEvent. */
     684    void sltHandleMouseRelease(QMouseEvent *pEvent);
     685
     686    /** Handles drag-enter @a pEvent. */
     687    void sltHandleDragEnter(QDragEnterEvent *pEvent);
     688    /** Handles drag-move @a pEvent. */
     689    void sltHandleDragMove(QDragMoveEvent *pEvent);
     690    /** Handles drag-drop @a pEvent. */
     691    void sltHandleDragDrop(QDropEvent *pEvent);
    678692
    679693private:
     
    790804    bool  m_fLoadingInProgress;
    791805
     806    /** Holds the last mouse-press position. */
     807    QPoint  m_mousePressPosition;
     808
    792809    /** Holds the page data cache instance. */
    793810    UISettingsCacheMachineStorage *m_pCache;
Note: See TracChangeset for help on using the changeset viewer.

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