QGCTextLabel.cc 4.28 KB
Newer Older
1 2 3 4 5 6 7 8 9
#include <QDockWidget>

#include "QGCTextLabel.h"
#include "ui_QGCTextLabel.h"

#include "MAVLinkProtocol.h"
#include "UASManager.h"

QGCTextLabel::QGCTextLabel(QWidget *parent) :
10
    QGCToolWidgetItem("Text Label", parent),
11 12 13 14 15 16 17
    ui(new Ui::QGCTextLabel)
{
    uas = 0;
    enabledNum = -1;
    ui->setupUi(this);

    connect(ui->editFinishButton, SIGNAL(clicked()), this, SLOT(endEditMode()));
18
    connect(ui->isMavCommand, SIGNAL(toggled(bool)), this, SLOT(update_isMavCommand()));
19 20 21 22 23

    // Hide all edit items
    ui->editFinishButton->hide();
    ui->editNameLabel->hide();
    ui->editLine1->hide();
24 25 26
    ui->editLine2->hide();
    ui->isMavCommand->hide();
    ui->textLabel->setText(QString());
27 28 29 30 31 32 33 34 35 36 37 38 39
}

QGCTextLabel::~QGCTextLabel()
{
    delete ui;
}

void QGCTextLabel::startEditMode()
{
    // Hide elements
    ui->editFinishButton->show();
    ui->editNameLabel->show();
    ui->editLine1->show();
40 41
    ui->editLine2->show();
    ui->isMavCommand->show();
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

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

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

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

    isInEditMode = true;
}

void QGCTextLabel::endEditMode()
{
63
    update_isMavCommand();
64 65 66
    ui->editFinishButton->hide();
    ui->editNameLabel->hide();
    ui->editLine1->hide();
67 68
    ui->editLine2->hide();
    ui->isMavCommand->hide();
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    // Write to settings
    emit editingFinished();

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

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

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

    isInEditMode = false;
}

void QGCTextLabel::writeSettings(QSettings& settings)
{
93 94 95
    settings.setValue("TYPE", "TEXT");
    settings.setValue("QGC_TEXT_TEXT", ui->editNameLabel->text());
    settings.setValue("QGC_TEXT_SOURCE", ui->isMavCommand->isChecked()?"MAV":"NONE");
96 97 98 99 100

    settings.sync();
}
void QGCTextLabel::readSettings(const QString& pre,const QVariantMap& settings)
{
101
    ui->editNameLabel->setText(settings.value(pre + "QGC_TEXT_TEXT","").toString());
102
    ui->isMavCommand->setChecked(settings.value(pre + "QGC_TEXT_SOURCE", "NONE").toString() == "MAV");
103
    update_isMavCommand();
104 105 106
}
void QGCTextLabel::textMessageReceived(int uasid, int component, int severity, QString message)
{
107 108 109
    Q_UNUSED(uasid);
    Q_UNUSED(component);
    Q_UNUSED(severity);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    if (enabledNum != -1)
    {
        //SUCCESS: Executed CMD: 241
        if (message.contains("SUCCESS"))
        {
            if (message.trimmed().endsWith(QString::number(enabledNum)))
            {
                enabledNum = -1;
                ui->textLabel->setText(ui->textLabel->text() + " Complete");
            }
        }
        else
        {
            ui->textLabel->setText(message);
        }
    }
}

void QGCTextLabel::readSettings(const QSettings& settings)
{
130
    ui->editNameLabel->setText(settings.value("QGC_TEXT_TEXT","").toString()); //Place this before setting isMavCommand
131
    ui->isMavCommand->setChecked(settings.value("QGC_TEXT_SOURCE", "NONE").toString() == "MAV");
132
    update_isMavCommand();
133
}
134

135 136 137 138 139 140 141
void QGCTextLabel::enableText(int num)
{
    enabledNum = num;
}

void QGCTextLabel::setActiveUAS(UASInterface *uas)
{
142 143
    if(this->uas)
        this->uas->disconnect(this);
144
    this->uas = uas;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    update_isMavCommand(); //Might need to update the signal connections
}

void QGCTextLabel::update_isMavCommand()
{
    ui->textLabel->setText("");
    if (!ui->isMavCommand->isChecked())
    {
        ui->nameLabel->setText(ui->editNameLabel->text());
        if(this->uas)
            disconnect(uas,SIGNAL(textMessageReceived(int,int,int,QString)),this,SLOT(textMessageReceived(int,int,int,QString)));
        if(ui->nameLabel->text().isEmpty())
            ui->nameLabel->setText(tr("Text Label")); //Show something, so that we don't end up with just an empty label
    }
    else
    {
        //MAV command text
        ui->nameLabel->setText(ui->editNameLabel->text());
        if(this->uas)
        connect(uas,SIGNAL(textMessageReceived(int,int,int,QString)),this,SLOT(textMessageReceived(int,int,int,QString)));
    }
166
}