QGCToolWidget.cc 20.3 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
#include <QFileDialog>
11
#include <QStandardPaths>
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
    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)
{
195 196 197 198
    Q_UNUSED(uas);
    Q_UNUSED(component);
    Q_UNUSED(value);
    
199 200
    QString widgetName = getTitle();
    int size = settingsMap["count"].toInt();
201 202
    if (paramToItemMap.contains(parameterName))
    {
203
        //If we already have an item for this parameter, updates are handled internally.
204 205
        return;
    }
206 207 208 209 210 211 212

    for (int j = 0; j < size; j++)
    {
        QString type = settingsMap.value(widgetName + "\\" + QString::number(j) + "\\" + "TYPE", "UNKNOWN").toString();
        QGCToolWidgetItem* item = NULL;
        if (type == "COMMANDBUTTON")
        {
213 214
            //This shouldn't happen, but I'm not sure... so lets test for it.
            continue;
215 216 217 218 219 220 221
        }
        else if (type == "SLIDER")
        {
            QString checkparam = settingsMap.value(widgetName + "\\" + QString::number(j) + "\\" + "QGC_PARAM_SLIDER_PARAMID").toString();
            if (checkparam == parameterName)
            {
                item = new QGCParamSlider(this);
222
                paramToItemMap[parameterName] = item;
223 224 225 226 227 228 229 230 231 232 233 234 235
                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);
236
                paramToItemMap[parameterName] = item;
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
                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";
            }
265 266 267 268 269
            else if (type == "TEXT")
            {
                item = new QGCTextLabel(this);
                item->setActiveUAS(mav);
            }
270 271 272 273 274 275 276 277
            else if (type == "SLIDER")
            {
                item = new QGCParamSlider(this);
                //qDebug() << "CREATED PARAM SLIDER";
            }
            else if (type == "COMBO")
            {
                item = new QGCComboBox(this);
278
                //qDebug() << "CREATED COMBOBOX";
279
            }
John Tapsell's avatar
John Tapsell committed
280 281 282 283 284
            else if (type == "XYPLOT")
            {
                item = new QGCXYPlot(this);
                //qDebug() << "CREATED XYPlot";
            }
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
            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();
}
302 303 304 305 306

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

349 350
            if (item)
            {
351 352 353 354
                // Configure and add to layout
                addToolWidget(item);
                item->readSettings(settings);

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

367
void QGCToolWidget::storeWidgetsToSettings(QSettings &settings) //static
lm's avatar
lm committed
368
{
369 370 371
    settings.beginGroup("Custom_Tool_Widgets");
    int preArraySize = settings.beginReadArray("QGC_TOOL_WIDGET_NAMES");
    settings.endArray();
372

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

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

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

410 411 412 413 414
    if (isFromMetaData)
    {
        //Refuse to store if this is loaded from metadata or dynamically generated.
        return;
    }
Lorenz Meier's avatar
Lorenz Meier committed
415
    //qDebug() << "WRITING WIDGET" << widgetTitle << "TO SETTINGS";
416
    settings.beginGroup(widgetTitle);
417 418
    settings.beginWriteArray("QGC_TOOL_WIDGET_ITEMS");
    int k = 0; // QGCToolItem counter
419 420 421 422 423
    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
424
    }
Lorenz Meier's avatar
Lorenz Meier committed
425
    //qDebug() << "WROTE" << k << "SUB-WIDGETS TO SETTINGS";
426 427
    settings.endArray();
    settings.endGroup();
lm's avatar
lm committed
428 429
}

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

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

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
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);
}

472 473 474 475 476 477
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
478 479 480
    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()));
481

482 483 484 485
    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
486 487 488 489
    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()));

490 491 492
    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
493 494 495 496

    deleteAction = new QAction(tr("Delete this widget"), this);
    deleteAction->setStatusTip(tr("Delete this widget permanently"));
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteWidget()));
497 498 499 500 501 502 503

    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)"));
504
    connect(importAction, SIGNAL(triggered()), this, SLOT(importWidget()));
lm's avatar
lm committed
505 506 507 508 509 510 511 512 513
}

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

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

void QGCToolWidget::addParam()
{
John Tapsell's avatar
John Tapsell committed
525
    addToolWidgetAndEdit(new QGCParamSlider(this));
526 527
}

lm's avatar
lm committed
528 529
void QGCToolWidget::addCommand()
{
John Tapsell's avatar
John Tapsell committed
530
    addToolWidgetAndEdit(new QGCCommandButton(this));
lm's avatar
lm committed
531 532
}

533 534
void QGCToolWidget::addLabel()
{
John Tapsell's avatar
John Tapsell committed
535 536 537
    addToolWidgetAndEdit(new QGCTextLabel(this));
}

John Tapsell's avatar
John Tapsell committed
538 539 540 541 542
void QGCToolWidget::addPlot()
{
    addToolWidgetAndEdit(new QGCXYPlot(this));
}

John Tapsell's avatar
John Tapsell committed
543 544 545 546
void QGCToolWidget::addToolWidgetAndEdit(QGCToolWidgetItem* widget)
{
    addToolWidget(widget);
    widget->startEditMode();
547 548
}

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

562 563 564 565 566 567 568 569 570
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();
}

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

583
void QGCToolWidget::importWidget()
584
{
585
    const QString widgetFileExtension(".qgw");
586
    QString fileName = QFileDialog::getOpenFileName(this, tr("Specify File Name"), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation), tr("QGroundControl Widget (*%1);;").arg(widgetFileExtension));
587
    loadSettings(fileName);
588 589
}

590
QString QGCToolWidget::getTitle() const
lm's avatar
lm committed
591
{
592
    return widgetTitle;
lm's avatar
lm committed
593 594
}

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

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

622 623 624
    //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
625 626
}

lm's avatar
lm committed
627 628 629 630 631 632 633 634 635 636 637
void QGCToolWidget::setMainMenuAction(QAction* action)
{
    this->mainMenuAction = action;
}

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

    // Hide
    this->hide();
638
    instances()->remove(objectName());
639 640 641 642

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

lm's avatar
lm committed
645 646 647 648 649
    storeWidgetsToSettings();

    // Delete
    this->deleteLater();
}