QGCToolWidgetItem.cc 1.92 KB
Newer Older
1 2 3 4 5
#include "QGCToolWidgetItem.h"

#include <QMenu>
#include <QContextMenuEvent>

lm's avatar
lm committed
6
#include "QGCToolWidget.h"
7
#include "UASManager.h"
8
#include <QDockWidget>
9 10

QGCToolWidgetItem::QGCToolWidgetItem(const QString& name, QWidget *parent) :
11
    QWidget(parent),
12
    uas(NULL),
13
    isInEditMode(false),
14
    qgcToolWidgetItemName(name),
15 16
    _component(-1)
{
lm's avatar
lm committed
17
    startEditAction = new QAction(tr("Edit %1").arg(qgcToolWidgetItemName), this);
18
    connect(startEditAction, SIGNAL(triggered()), this, SLOT(startEditMode()));
lm's avatar
lm committed
19
    stopEditAction = new QAction(tr("Finish Editing %1").arg(qgcToolWidgetItemName), this);
20
    connect(stopEditAction, SIGNAL(triggered()), this, SLOT(endEditMode()));
lm's avatar
lm committed
21 22 23
    deleteAction = new QAction(tr("Delete %1").arg(qgcToolWidgetItemName), this);
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteLater()));

24 25 26 27
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)),
            this, SLOT(setActiveUAS(UASInterface*)));
    // Set first UAS if it exists
    setActiveUAS(UASManager::instance()->getActiveUAS());
28 29 30 31 32 33
}

QGCToolWidgetItem::~QGCToolWidgetItem()
{
    delete startEditAction;
    delete stopEditAction;
lm's avatar
lm committed
34
    delete deleteAction;
35 36 37 38 39
}

void QGCToolWidgetItem::contextMenuEvent (QContextMenuEvent* event)
{
    QMenu menu(this);
40
    if (!isInEditMode) {
41
        menu.addAction(startEditAction);
lm's avatar
lm committed
42
        menu.addAction(deleteAction);
43
    } else {
44 45 46 47
        menu.addAction(stopEditAction);
    }
    menu.exec(event->globalPos());
}
48 49 50 51 52

void QGCToolWidgetItem::setActiveUAS(UASInterface *uas)
{
    this->uas = uas;
}
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

void QGCToolWidgetItem::setEditMode(bool editMode)
{
    isInEditMode = editMode;

    // Attempt to undock the dock widget
    QWidget* p = this;
    QDockWidget* dock;

    do {
        p = p->parentWidget();
        dock = dynamic_cast<QDockWidget*>(p);

        if (dock)
        {
            dock->setFloating(editMode);
            break;
        }
    } while (p && !dock);

    emit editingFinished();
}