QGCToolWidget.cc 20.2 KB
Newer Older
1 2 3 4 5 6 7 8
#include "QGCToolWidget.h"
#include "ui_QGCToolWidget.h"

#include <QMenu>
#include <QList>
#include <QInputDialog>
#include <QDockWidget>
#include <QContextMenuEvent>
lm's avatar
lm committed
9
#include <QSettings>
10 11
#include <QFileDialog>
#include <QDesktopServices>
lm's avatar
lm committed
12

13
#include "QGCParamSlider.h"
14
#include "QGCComboBox.h"
15
#include "QGCTextLabel.h"
John Tapsell's avatar
John Tapsell committed
16
#include "QGCXYPlot.h"
lm's avatar
lm committed
17
#include "QGCCommandButton.h"
18 19
#include "UASManager.h"

20
QGCToolWidget::QGCToolWidget(const QString& objectName, const QString& title, QWidget *parent, QSettings* settings) :
21 22 23
        QWidget(parent),
        mav(NULL),
        mainMenuAction(NULL),
24
        widgetTitle(title),
25
        ui(new Ui::QGCToolWidget)
26
{
27
    isFromMetaData = false;
28 29 30
    ui->setupUi(this);
    if (settings) loadSettings(*settings);

31
    createActions();
lm's avatar
lm committed
32
    toolLayout = ui->toolLayout;
33
    toolLayout->setAlignment(Qt::AlignTop);
34
    toolLayout->setSpacing(8);
35

36
    this->setTitle(widgetTitle);
37
    QList<UASInterface*> systems = UASManager::instance()->getUASList();
38 39
    foreach (UASInterface* uas, systems)
    {
40
        UAS* newMav = dynamic_cast<UAS*>(uas);
41 42
        if (newMav)
        {
43 44 45 46
            addUAS(uas);
        }
    }
    connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)), this, SLOT(addUAS(UASInterface*)));
47

48 49 50 51
    if(!objectName.isEmpty()) {
        instances()->insert(objectName, this);
        setObjectName(objectName);
    } //Otherwise we must call loadSettings() immediately to set the object name
52 53 54 55
}

QGCToolWidget::~QGCToolWidget()
{
56
    if (mainMenuAction) mainMenuAction->deleteLater();
57
    if (QGCToolWidget::instances()) QGCToolWidget::instances()->remove(objectName());
58 59 60
    delete ui;
}

61 62 63
void QGCToolWidget::setParent(QWidget *parent)
{
    QWidget::setParent(parent);
64
    setTitle(getTitle()); //Update titles
65 66
}

lm's avatar
lm committed
67 68 69 70
/**
 * @param parent Object later holding these widgets, usually the main window
 * @return List of all widgets
 */
71
QList<QGCToolWidget*> QGCToolWidget::createWidgetsFromSettings(QWidget* parent, QString settingsFile)
lm's avatar
lm committed
72
{
73 74 75 76 77 78 79
    // Load widgets from application settings
    QSettings* settings;

    // Or load them from a settings file
    if (!settingsFile.isEmpty())
    {
        settings = new QSettings(settingsFile, QSettings::IniFormat);
Lorenz Meier's avatar
Lorenz Meier committed
80
       //qDebug() << "LOADING SETTINGS FROM" << settingsFile;
81 82 83 84
    }
    else
    {
        settings = new QSettings();
Lorenz Meier's avatar
Lorenz Meier committed
85
        //qDebug() << "LOADING SETTINGS FROM DEFAULT" << settings->fileName();
86 87
    }

lm's avatar
lm committed
88
    QList<QGCToolWidget*> newWidgets;
89
    settings->beginGroup("Custom_Tool_Widgets");
90
    int size = settings->beginReadArray("QGC_TOOL_WIDGET_NAMES");
91 92
    for (int i = 0; i < size; i++)
    {
93
        settings->setArrayIndex(i);
94
        QString name = settings->value("TITLE", "").toString();
95
        QString objname = settings->value("OBJECT_NAME", "").toString();
lm's avatar
lm committed
96

97
        if (!instances()->contains(objname) && !objname.isEmpty())
98
        {
Lorenz Meier's avatar
Lorenz Meier committed
99
            //qDebug() << "CREATED WIDGET:" << name;
100
            QGCToolWidget* tool = new QGCToolWidget(objname, name, parent, settings);
lm's avatar
lm committed
101 102
            newWidgets.append(tool);
        }
103 104 105 106 107
        else if (name.length() == 0)
        {
            // Silently catch empty widget name - sanity check
            // to survive broken settings (e.g. from user manipulation)
        }
108 109
        else
        {
Lorenz Meier's avatar
Lorenz Meier committed
110
            //qDebug() << "WIDGET" << name << "DID ALREADY EXIST, REJECTING";
111
        }
lm's avatar
lm committed
112
    }
113
    settings->endArray();
lm's avatar
lm committed
114

Lorenz Meier's avatar
Lorenz Meier committed
115
    //qDebug() << "NEW WIDGETS: " << newWidgets.size();
lm's avatar
lm committed
116 117

    // Load individual widget items
118 119
    for (int i = 0; i < newWidgets.size(); i++)
    {
120
        newWidgets.at(i)->loadSettings(*settings);
lm's avatar
lm committed
121
    }
122 123
    settings->endGroup();
    settings->sync();
124
    delete settings;
lm's avatar
lm committed
125 126 127

    return instances()->values();
}
128 129 130 131 132 133 134 135 136 137 138 139 140 141
void QGCToolWidget::showLabel(QString name,int num)
{
    for (int i=0;i<toolItemList.size();i++)
    {
        if (toolItemList[i]->objectName() == name)
        {
            QGCTextLabel *label = qobject_cast<QGCTextLabel*>(toolItemList[i]);
            if (label)
            {
                label->enableText(num);
            }
        }
    }
}
lm's avatar
lm committed
142

LM's avatar
LM committed
143 144 145 146
/**
 * @param singleinstance If this is set to true, the widget settings will only be loaded if not another widget with the same title exists
 */
bool QGCToolWidget::loadSettings(const QString& settings, bool singleinstance)
147
{
148 149
    QSettings set(settings, QSettings::IniFormat);
    QStringList groups = set.childGroups();
150 151
    if (groups.length() > 0)
    {
152 153 154 155
        QString objectName = groups.first();
        setObjectName(objectName);
        if (singleinstance && QGCToolWidget::instances()->contains(objectName)) return false;
        instances()->insert(objectName, this);
156 157
        // Do not use setTitle() here,
        // interferes with loading settings
158
        widgetTitle = objectName;
Lorenz Meier's avatar
Lorenz Meier committed
159
        //qDebug() << "WIDGET TITLE LOADED: " << widgetName;
160 161 162 163 164 165 166
        loadSettings(set);
        return true;
    }
    else
    {
        return false;
    }
167
}
168 169
void QGCToolWidget::setSettings(QVariantMap& settings)
{
170
    isFromMetaData = true;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    settingsMap = settings;
    QString widgetName = getTitle();
    int size = settingsMap["count"].toInt();
    for (int j = 0; j < size; j++)
    {
        QString type = settings.value(widgetName + "\\" + QString::number(j) + "\\" + "TYPE", "UNKNOWN").toString();
        if (type == "SLIDER")
        {
            QString checkparam = settingsMap.value(widgetName + "\\" + QString::number(j) + "\\" + "QGC_PARAM_SLIDER_PARAMID").toString();
            paramList.append(checkparam);
        }
        else if (type == "COMBO")
        {
            QString checkparam = settingsMap.value(widgetName + "\\" + QString::number(j) + "\\" + "QGC_PARAM_COMBOBOX_PARAMID").toString();
            paramList.append(checkparam);
        }
    }
}
QList<QString> QGCToolWidget::getParamList()
{
    return paramList;
}
void QGCToolWidget::setParameterValue(int uas, int component, QString parameterName, const QVariant value)
{
    QString widgetName = getTitle();
    int size = settingsMap["count"].toInt();
197 198
    if (paramToItemMap.contains(parameterName))
    {
199
        //If we already have an item for this parameter, updates are handled internally.
200 201
        return;
    }
202 203 204 205 206 207 208

    for (int j = 0; j < size; j++)
    {
        QString type = settingsMap.value(widgetName + "\\" + QString::number(j) + "\\" + "TYPE", "UNKNOWN").toString();
        QGCToolWidgetItem* item = NULL;
        if (type == "COMMANDBUTTON")
        {
209 210
            //This shouldn't happen, but I'm not sure... so lets test for it.
            continue;
211 212 213 214 215 216 217
        }
        else if (type == "SLIDER")
        {
            QString checkparam = settingsMap.value(widgetName + "\\" + QString::number(j) + "\\" + "QGC_PARAM_SLIDER_PARAMID").toString();
            if (checkparam == parameterName)
            {
                item = new QGCParamSlider(this);
218
                paramToItemMap[parameterName] = item;
219 220 221 222 223 224 225 226 227 228 229 230 231
                addToolWidget(item);
                item->readSettings(widgetName + "\\" + QString::number(j) + "\\",settingsMap);
                return;
            }
        }
        else if (type == "COMBO")
        {
            QString checkparam = settingsMap.value(widgetName + "\\" + QString::number(j) + "\\" + "QGC_PARAM_COMBOBOX_PARAMID").toString();
            if (checkparam == parameterName)
            {
                item = new QGCComboBox(this);
                addToolWidget(item);
                item->readSettings(widgetName + "\\" + QString::number(j) + "\\",settingsMap);
232
                paramToItemMap[parameterName] = item;
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
                return;
            }
        }
    }
}

void QGCToolWidget::loadSettings(QVariantMap& settings)
{

    QString widgetName = getTitle();
    //settings.beginGroup(widgetName);
    qDebug() << "LOADING FOR" << widgetName;
    //int size = settings.beginReadArray("QGC_TOOL_WIDGET_ITEMS");
    int size = settings["count"].toInt();
    qDebug() << "CHILDREN SIZE:" << size;
    for (int j = 0; j < size; j++)
    {
        QApplication::processEvents();
        //settings.setArrayIndex(j);
        QString type = settings.value(widgetName + "\\" + QString::number(j) + "\\" + "TYPE", "UNKNOWN").toString();
        if (type != "UNKNOWN")
        {
            QGCToolWidgetItem* item = NULL;
            if (type == "COMMANDBUTTON")
            {
                item = new QGCCommandButton(this);
                //qDebug() << "CREATED COMMANDBUTTON";
            }
261 262 263 264 265
            else if (type == "TEXT")
            {
                item = new QGCTextLabel(this);
                item->setActiveUAS(mav);
            }
266 267 268 269 270 271 272 273
            else if (type == "SLIDER")
            {
                item = new QGCParamSlider(this);
                //qDebug() << "CREATED PARAM SLIDER";
            }
            else if (type == "COMBO")
            {
                item = new QGCComboBox(this);
274
                //qDebug() << "CREATED COMBOBOX";
275
            }
John Tapsell's avatar
John Tapsell committed
276 277 278 279 280
            else if (type == "XYPLOT")
            {
                item = new QGCXYPlot(this);
                //qDebug() << "CREATED XYPlot";
            }
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
            if (item)
            {
                // Configure and add to layout
                addToolWidget(item);
                item->readSettings(widgetName + "\\" + QString::number(j) + "\\",settings);

                //qDebug() << "Created tool widget";
            }
        }
        else
        {
            qDebug() << "UNKNOWN TOOL WIDGET TYPE" << type;
        }
    }
    //settings.endArray();
    //settings.endGroup();
}
298 299 300 301 302

void QGCToolWidget::loadSettings(QSettings& settings)
{
    QString widgetName = getTitle();
    settings.beginGroup(widgetName);
Lorenz Meier's avatar
Lorenz Meier committed
303
    //qDebug() << "LOADING FOR" << widgetName;
304
    int size = settings.beginReadArray("QGC_TOOL_WIDGET_ITEMS");
Lorenz Meier's avatar
Lorenz Meier committed
305
    //qDebug() << "CHILDREN SIZE:" << size;
306 307
    for (int j = 0; j < size; j++)
    {
308 309
        settings.setArrayIndex(j);
        QString type = settings.value("TYPE", "UNKNOWN").toString();
310 311
        if (type != "UNKNOWN")
        {
312
            QGCToolWidgetItem* item = NULL;
313 314
            if (type == "COMMANDBUTTON")
            {
315 316 317 318
                QGCCommandButton *button = new QGCCommandButton(this);
                connect(button,SIGNAL(showLabel(QString,int)),this,SLOT(showLabel(QString,int)));
                item = button;
                item->setActiveUAS(mav);
Lorenz Meier's avatar
Lorenz Meier committed
319
                //qDebug() << "CREATED COMMANDBUTTON";
320 321 322
            }
            else if (type == "SLIDER")
            {
323
                item = new QGCParamSlider(this);
324
                item->setActiveUAS(mav);
Lorenz Meier's avatar
Lorenz Meier committed
325
                //qDebug() << "CREATED PARAM SLIDER";
326
            }
327 328 329
            else if (type == "COMBO")
            {
                item = new QGCComboBox(this);
330
                item->setActiveUAS(mav);
331 332
                qDebug() << "CREATED PARAM COMBOBOX";
            }
333 334 335 336 337 338
            else if (type == "TEXT")
            {
                item = new QGCTextLabel(this);
                item->setObjectName(settings.value("QGC_TEXT_ID").toString());
                item->setActiveUAS(mav);
            }
John Tapsell's avatar
John Tapsell committed
339 340 341 342 343
            else if (type == "XYPLOT")
            {
                item = new QGCXYPlot(this);
                item->setActiveUAS(mav);
            }
344

345 346
            if (item)
            {
347 348 349 350
                // Configure and add to layout
                addToolWidget(item);
                item->readSettings(settings);

Lorenz Meier's avatar
Lorenz Meier committed
351
                //qDebug() << "Created tool widget";
352
            }
353 354 355
        }
        else
        {
Lorenz Meier's avatar
Lorenz Meier committed
356
            //qDebug() << "UNKNOWN TOOL WIDGET TYPE";
357 358 359 360 361 362
        }
    }
    settings.endArray();
    settings.endGroup();
}

363
void QGCToolWidget::storeWidgetsToSettings(QSettings &settings) //static
lm's avatar
lm committed
364
{
365 366 367
    settings.beginGroup("Custom_Tool_Widgets");
    int preArraySize = settings.beginReadArray("QGC_TOOL_WIDGET_NAMES");
    settings.endArray();
368

369
    settings.beginWriteArray("QGC_TOOL_WIDGET_NAMES");
370
    int num = 0;
371
    for (int i = 0; i < qMax(preArraySize, instances()->size()); ++i)
372
    {
373 374 375
        if (i < instances()->size())
        {
            // Updating value
376 377
            if (!instances()->values().at(i)->fromMetaData())
            {
378 379 380
                settings.setArrayIndex(num++);
                settings.setValue("TITLE", instances()->values().at(i)->getTitle());
                settings.setValue("OBJECT_NAME", instances()->values().at(i)->objectName());
381
                qDebug() << "WRITING TITLE" << instances()->values().at(i)->getTitle() << "object:" << instances()->values().at(i)->objectName();
382
            }
383 384 385 386
        }
        else
        {
            // Deleting old value
387
            settings.remove("TITLE");
388
        }
lm's avatar
lm committed
389
    }
390
    settings.endArray();
lm's avatar
lm committed
391 392

    // Store individual widget items
393 394
    for (int i = 0; i < instances()->size(); ++i)
    {
395
        instances()->values().at(i)->storeSettings(settings);
396
    }
397 398
    settings.endGroup();
    settings.sync();
399 400 401 402
}

void QGCToolWidget::storeSettings(QSettings& settings)
{
403 404 405
    /* This function should be called from storeWidgetsToSettings() which sets up the group etc */
    Q_ASSERT(settings.group() == "Custom_Tool_Widgets");

406 407 408 409 410
    if (isFromMetaData)
    {
        //Refuse to store if this is loaded from metadata or dynamically generated.
        return;
    }
Lorenz Meier's avatar
Lorenz Meier committed
411
    //qDebug() << "WRITING WIDGET" << widgetTitle << "TO SETTINGS";
412
    settings.beginGroup(widgetTitle);
413 414
    settings.beginWriteArray("QGC_TOOL_WIDGET_ITEMS");
    int k = 0; // QGCToolItem counter
415 416 417 418 419
    foreach(QGCToolWidgetItem *item, toolItemList) {
        // Only count actual tool widget item children
        settings.setArrayIndex(k++);
        // Store the ToolWidgetItem
        item->writeSettings(settings);
lm's avatar
lm committed
420
    }
Lorenz Meier's avatar
Lorenz Meier committed
421
    //qDebug() << "WROTE" << k << "SUB-WIDGETS TO SETTINGS";
422 423
    settings.endArray();
    settings.endGroup();
lm's avatar
lm committed
424 425
}

426 427 428
void QGCToolWidget::addUAS(UASInterface* uas)
{
    UAS* newMav = dynamic_cast<UAS*>(uas);
429 430
    if (newMav)
    {
431 432 433 434 435 436 437 438
        // FIXME Convert to list
        if (mav == NULL) mav = newMav;
    }
}

void QGCToolWidget::contextMenuEvent (QContextMenuEvent* event)
{
    QMenu menu(this);
439
    menu.addAction(addParamAction);
lm's avatar
lm committed
440
    menu.addAction(addCommandAction);
441
    menu.addAction(addLabelAction);
John Tapsell's avatar
John Tapsell committed
442
    menu.addAction(addPlotAction);
443
    menu.addSeparator();
444
    menu.addAction(setTitleAction);
445
    menu.addAction(exportAction);
446
    menu.addAction(importAction);
lm's avatar
lm committed
447
    menu.addAction(deleteAction);
448 449 450
    menu.exec(event->globalPos());
}

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
void QGCToolWidget::hideEvent(QHideEvent* event)
{
    // Store settings
    QWidget::hideEvent(event);
}

/**
 * The widgets current view and the applied dock widget area.
 * Both values are only stored internally and allow an external
 * widget to configure it accordingly
 */
void QGCToolWidget::setViewVisibilityAndDockWidgetArea(int view, bool visible, Qt::DockWidgetArea area)
{
    viewVisible.insert(view, visible);
    dockWidgetArea.insert(view, area);
}

468 469 470 471 472 473
void QGCToolWidget::createActions()
{
    addParamAction = new QAction(tr("New &Parameter Slider"), this);
    addParamAction->setStatusTip(tr("Add a parameter setting slider widget to the tool"));
    connect(addParamAction, SIGNAL(triggered()), this, SLOT(addParam()));

lm's avatar
lm committed
474 475 476
    addCommandAction = new QAction(tr("New MAV &Command Button"), this);
    addCommandAction->setStatusTip(tr("Add a new action button to the tool"));
    connect(addCommandAction, SIGNAL(triggered()), this, SLOT(addCommand()));
477

478 479 480 481
    addLabelAction = new QAction(tr("New &Text Label"), this);
    addLabelAction->setStatusTip(tr("Add a new label to the tool"));
    connect(addLabelAction, SIGNAL(triggered()), this, SLOT(addLabel()));

John Tapsell's avatar
John Tapsell committed
482 483 484 485
    addPlotAction = new QAction(tr("New &XY Plot"), this);
    addPlotAction->setStatusTip(tr("Add a XY Plot to the tool"));
    connect(addPlotAction, SIGNAL(triggered()), this, SLOT(addPlot()));

486 487 488
    setTitleAction = new QAction(tr("Set Widget Title"), this);
    setTitleAction->setStatusTip(tr("Set the title caption of this tool widget"));
    connect(setTitleAction, SIGNAL(triggered()), this, SLOT(setTitle()));
lm's avatar
lm committed
489 490 491 492

    deleteAction = new QAction(tr("Delete this widget"), this);
    deleteAction->setStatusTip(tr("Delete this widget permanently"));
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteWidget()));
493 494 495 496 497 498 499

    exportAction = new QAction(tr("Export this widget"), this);
    exportAction->setStatusTip(tr("Export this widget to be reused by others"));
    connect(exportAction, SIGNAL(triggered()), this, SLOT(exportWidget()));

    importAction = new QAction(tr("Import widget"), this);
    importAction->setStatusTip(tr("Import this widget from a file (current content will be removed)"));
500
    connect(importAction, SIGNAL(triggered()), this, SLOT(importWidget()));
lm's avatar
lm committed
501 502 503 504 505 506 507 508 509
}

QMap<QString, QGCToolWidget*>* QGCToolWidget::instances()
{
    static QMap<QString, QGCToolWidget*>* instances;
    if (!instances) instances = new QMap<QString, QGCToolWidget*>();
    return instances;
}

510 511
void QGCToolWidget::addParam(int uas,int component,QString paramname,QVariant value)
{
512
    isFromMetaData = true;
513
    QGCParamSlider* slider = new QGCParamSlider(this);
John Tapsell's avatar
John Tapsell committed
514
    addToolWidget(slider);
515 516 517
    slider->setActiveUAS(mav);
    slider->setParameterValue(uas,component,0,-1,paramname,value);
}
518 519 520

void QGCToolWidget::addParam()
{
John Tapsell's avatar
John Tapsell committed
521
    addToolWidgetAndEdit(new QGCParamSlider(this));
522 523
}

lm's avatar
lm committed
524 525
void QGCToolWidget::addCommand()
{
John Tapsell's avatar
John Tapsell committed
526
    addToolWidgetAndEdit(new QGCCommandButton(this));
lm's avatar
lm committed
527 528
}

529 530
void QGCToolWidget::addLabel()
{
John Tapsell's avatar
John Tapsell committed
531 532 533
    addToolWidgetAndEdit(new QGCTextLabel(this));
}

John Tapsell's avatar
John Tapsell committed
534 535 536 537 538
void QGCToolWidget::addPlot()
{
    addToolWidgetAndEdit(new QGCXYPlot(this));
}

John Tapsell's avatar
John Tapsell committed
539 540 541 542
void QGCToolWidget::addToolWidgetAndEdit(QGCToolWidgetItem* widget)
{
    addToolWidget(widget);
    widget->startEditMode();
543 544
}

lm's avatar
lm committed
545 546
void QGCToolWidget::addToolWidget(QGCToolWidgetItem* widget)
{
547 548
    if (ui->hintLabel)
    {
lm's avatar
lm committed
549
        ui->hintLabel->deleteLater();
550
        ui->hintLabel = NULL;
lm's avatar
lm committed
551
    }
552
    connect(widget, SIGNAL(editingFinished()), this, SLOT(storeWidgetsToSettings()));
553
    connect(widget, SIGNAL(destroyed()), this, SLOT(widgetRemoved()));
lm's avatar
lm committed
554
    toolLayout->addWidget(widget);
555
    toolItemList.append(widget);
lm's avatar
lm committed
556 557
}

558 559 560 561 562 563 564 565 566
void QGCToolWidget::widgetRemoved()
{
    //Must static cast and not dynamic cast since the object is in the destructor
    //and we only want to use it as a pointer value
    QGCToolWidgetItem *widget = static_cast<QGCToolWidgetItem *>(QObject::sender());
    toolItemList.removeAll(widget);
    storeWidgetsToSettings();
}

567 568
void QGCToolWidget::exportWidget()
{
569 570 571 572 573 574
    const QString widgetFileExtension(".qgw");
    QString fileName = QFileDialog::getSaveFileName(this, tr("Specify File Name"), QDesktopServices::storageLocation(QDesktopServices::DesktopLocation), tr("QGroundControl Widget (*%1);;").arg(widgetFileExtension));
    if (!fileName.endsWith(widgetFileExtension))
    {
        fileName = fileName.append(widgetFileExtension);
    }
575 576
    QSettings settings(fileName, QSettings::IniFormat);
    storeSettings(settings);
577 578
}

579
void QGCToolWidget::importWidget()
580
{
581 582 583
    const QString widgetFileExtension(".qgw");
    QString fileName = QFileDialog::getOpenFileName(this, tr("Specify File Name"), QDesktopServices::storageLocation(QDesktopServices::DesktopLocation), tr("QGroundControl Widget (*%1);;").arg(widgetFileExtension));
    loadSettings(fileName);
584 585
}

586
QString QGCToolWidget::getTitle() const
lm's avatar
lm committed
587
{
588
    return widgetTitle;
lm's avatar
lm committed
589 590
}

591 592 593
void QGCToolWidget::setTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
594 595
    if (parent)
    {
lm's avatar
lm committed
596
        bool ok;
597
        QString text = QInputDialog::getText(this, tr("Enter Widget Title"),
598 599
                                             tr("Widget title:"), QLineEdit::Normal,
                                             parent->windowTitle(), &ok);
600 601 602
        if (ok && !text.isEmpty())
        {
            setTitle(text);
lm's avatar
lm committed
603
        }
604 605
    }
}
lm's avatar
lm committed
606

607
void QGCToolWidget::setTitle(const QString& title)
608
{
609
    // Sets title and calls setWindowTitle on QWidget
610 611
    widgetTitle = title;
    QWidget::setWindowTitle(title);
612
    QDockWidget* dock = dynamic_cast<QDockWidget*>(this->parentWidget());
613
    if (dock)
614
        dock->setWindowTitle(widgetTitle);
615 616
    emit titleChanged(title);
    if (mainMenuAction) mainMenuAction->setText(title);
617

618 619 620
    //Do not save the settings here, because this function might be
    //called while loading, and thus saving here could end up clobbering
    //all of the other widgets
621 622
}

lm's avatar
lm committed
623 624 625 626 627 628 629 630 631 632 633
void QGCToolWidget::setMainMenuAction(QAction* action)
{
    this->mainMenuAction = action;
}

void QGCToolWidget::deleteWidget()
{
    // Remove from settings

    // Hide
    this->hide();
634
    instances()->remove(objectName());
635 636 637 638

    QSettings settings;
    settings.beginGroup("QGC_MAINWINDOW");
    settings.remove(QString("TOOL_PARENT_") + objectName());
lm's avatar
lm committed
639
    settings.endGroup();
640

lm's avatar
lm committed
641 642 643 644 645
    storeWidgetsToSettings();

    // Delete
    this->deleteLater();
}