QGCToolWidgetItem.cc 1.96 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 18 19
    startEditAction = new QAction(tr("Edit %1").arg(qgcToolWidgetItemName), this);
    stopEditAction = new QAction(tr("Finish Editing %1").arg(qgcToolWidgetItemName), this);
    deleteAction = new QAction(tr("Delete %1").arg(qgcToolWidgetItemName), this);
20 21 22 23 24 25
}

void QGCToolWidgetItem::init()
{
    connect(startEditAction, SIGNAL(triggered()), this, SLOT(startEditMode()));
    connect(stopEditAction, SIGNAL(triggered()), this, SLOT(endEditMode()));
lm's avatar
lm committed
26 27
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteLater()));

28 29
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)),
            this, SLOT(setActiveUAS(UASInterface*)));
30

31 32
    // Set first UAS if it exists
    setActiveUAS(UASManager::instance()->getActiveUAS());
33 34 35 36 37 38
}

QGCToolWidgetItem::~QGCToolWidgetItem()
{
    delete startEditAction;
    delete stopEditAction;
lm's avatar
lm committed
39
    delete deleteAction;
40 41 42 43 44
}

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

void QGCToolWidgetItem::setActiveUAS(UASInterface *uas)
{
    this->uas = uas;
}
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

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();
}