QGCToolWidget.cc 20.9 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"
lm's avatar
lm committed
16
#include "QGCCommandButton.h"
17 18
#include "UASManager.h"

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

30 31 32 33
    if (title == "Unnamed Tool")
    {
        widgetTitle = QString("%1 %2").arg(title).arg(QGCToolWidget::instances()->count());
    }
Lorenz Meier's avatar
Lorenz Meier committed
34
    //qDebug() << "WidgetTitle" << widgetTitle;
35

36
    createActions();
lm's avatar
lm committed
37
    toolLayout = ui->toolLayout;
38
    toolLayout->setAlignment(Qt::AlignTop);
39
    toolLayout->setSpacing(8);
40

41
    this->setTitle(widgetTitle);
42
    QList<UASInterface*> systems = UASManager::instance()->getUASList();
43 44
    foreach (UASInterface* uas, systems)
    {
45
        UAS* newMav = dynamic_cast<UAS*>(uas);
46 47
        if (newMav)
        {
48 49 50 51
            addUAS(uas);
        }
    }
    connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)), this, SLOT(addUAS(UASInterface*)));
52 53 54

    // Enforce storage if this not loaded from settings
    // is MUST NOT BE SAVED if it was loaded from settings!
55
    //if (!settings) storeWidgetsToSettings();
56 57 58 59
}

QGCToolWidget::~QGCToolWidget()
{
60 61
    if (mainMenuAction) mainMenuAction->deleteLater();
    if (QGCToolWidget::instances()) QGCToolWidget::instances()->remove(widgetTitle);
62 63 64
    delete ui;
}

65 66 67
void QGCToolWidget::setParent(QWidget *parent)
{
    QWidget::setParent(parent);
68
    setTitle(getTitle()); //Update titles
69 70
}

lm's avatar
lm committed
71 72 73 74
/**
 * @param parent Object later holding these widgets, usually the main window
 * @return List of all widgets
 */
75
QList<QGCToolWidget*> QGCToolWidget::createWidgetsFromSettings(QWidget* parent, QString settingsFile)
lm's avatar
lm committed
76
{
77 78 79 80 81 82 83
    // 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
84
       //qDebug() << "LOADING SETTINGS FROM" << settingsFile;
85 86 87 88
    }
    else
    {
        settings = new QSettings();
Lorenz Meier's avatar
Lorenz Meier committed
89
        //qDebug() << "LOADING SETTINGS FROM DEFAULT" << settings->fileName();
90 91
    }

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

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

Lorenz Meier's avatar
Lorenz Meier committed
120
    //qDebug() << "NEW WIDGETS: " << newWidgets.size();
lm's avatar
lm committed
121 122

    // Load individual widget items
123 124
    for (int i = 0; i < newWidgets.size(); i++)
    {
125
        newWidgets.at(i)->loadSettings(*settings);
lm's avatar
lm committed
126
    }
127 128
    settings->endGroup();
    settings->sync();
129
    delete settings;
lm's avatar
lm committed
130 131 132

    return instances()->values();
}
133 134 135 136 137 138 139 140 141 142 143 144 145 146
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
147

LM's avatar
LM committed
148 149 150 151
/**
 * @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)
152
{
153 154
    QSettings set(settings, QSettings::IniFormat);
    QStringList groups = set.childGroups();
155 156 157
    if (groups.length() > 0)
    {
        QString widgetName = groups.first();
158
	this->setObjectName(widgetName);
159
        if (singleinstance && QGCToolWidget::instances()->keys().contains(widgetName)) return false;
160 161 162
        // Do not use setTitle() here,
        // interferes with loading settings
        widgetTitle = widgetName;
Lorenz Meier's avatar
Lorenz Meier committed
163
        //qDebug() << "WIDGET TITLE LOADED: " << widgetName;
164 165 166 167 168 169 170
        loadSettings(set);
        return true;
    }
    else
    {
        return false;
    }
171
}
172 173
void QGCToolWidget::setSettings(QVariantMap& settings)
{
174
    isFromMetaData = true;
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    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();
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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
            }
            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();
}
297 298 299 300 301

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

339 340
            if (item)
            {
341 342 343 344
                // Configure and add to layout
                addToolWidget(item);
                item->readSettings(settings);

Lorenz Meier's avatar
Lorenz Meier committed
345
                //qDebug() << "Created tool widget";
346
            }
347 348 349
        }
        else
        {
Lorenz Meier's avatar
Lorenz Meier committed
350
            //qDebug() << "UNKNOWN TOOL WIDGET TYPE";
351 352 353 354 355 356 357
        }
    }
    settings.endArray();
    settings.endGroup();
}

void QGCToolWidget::storeWidgetsToSettings(QString settingsFile)
lm's avatar
lm committed
358 359
{
    // Store list of widgets
360 361 362 363
    QSettings* settings;
    if (!settingsFile.isEmpty())
    {
        settings = new QSettings(settingsFile, QSettings::IniFormat);
Lorenz Meier's avatar
Lorenz Meier committed
364
        //qDebug() << "STORING SETTINGS TO" << settings->fileName();
365 366 367 368
    }
    else
    {
        settings = new QSettings();
Lorenz Meier's avatar
Lorenz Meier committed
369
        //qDebug() << "STORING SETTINGS TO DEFAULT" << settings->fileName();
370 371
    }

372
    settings->beginGroup("Custom_Tool_Widgets");
373 374 375
    int preArraySize = settings->beginReadArray("QGC_TOOL_WIDGET_NAMES");
    settings->endArray();

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

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

409 410 411 412 413 414
void QGCToolWidget::storeSettings()
{
    QSettings settings;
    storeSettings(settings);
}

415 416 417 418 419 420 421 422
void QGCToolWidget::storeSettings(const QString& settingsFile)
{
    QSettings settings(settingsFile, QSettings::IniFormat);
    storeSettings(settings);
}

void QGCToolWidget::storeSettings(QSettings& settings)
{
423 424 425 426 427
    if (isFromMetaData)
    {
        //Refuse to store if this is loaded from metadata or dynamically generated.
        return;
    }
Lorenz Meier's avatar
Lorenz Meier committed
428
    //qDebug() << "WRITING WIDGET" << widgetTitle << "TO SETTINGS";
429
    settings.beginGroup(widgetTitle);
430 431
    settings.beginWriteArray("QGC_TOOL_WIDGET_ITEMS");
    int k = 0; // QGCToolItem counter
432 433
    for (int j = 0; j  < children().size(); ++j)
    {
434 435
        // Store only QGCToolWidgetItems
        QGCToolWidgetItem* item = dynamic_cast<QGCToolWidgetItem*>(children().at(j));
436 437
        if (item)
        {
438
            // Only count actual tool widget item children
439 440 441
            settings.setArrayIndex(k++);
            // Store the ToolWidgetItem
            item->writeSettings(settings);
lm's avatar
lm committed
442 443
        }
    }
Lorenz Meier's avatar
Lorenz Meier committed
444
    //qDebug() << "WROTE" << k << "SUB-WIDGETS TO SETTINGS";
445 446
    settings.endArray();
    settings.endGroup();
lm's avatar
lm committed
447 448
}

449 450 451
void QGCToolWidget::addUAS(UASInterface* uas)
{
    UAS* newMav = dynamic_cast<UAS*>(uas);
452 453
    if (newMav)
    {
454 455 456 457 458 459 460 461
        // FIXME Convert to list
        if (mav == NULL) mav = newMav;
    }
}

void QGCToolWidget::contextMenuEvent (QContextMenuEvent* event)
{
    QMenu menu(this);
462
    menu.addAction(addParamAction);
lm's avatar
lm committed
463
    menu.addAction(addCommandAction);
464
    menu.addAction(addLabelAction);
465
    menu.addSeparator();
466
    menu.addAction(setTitleAction);
467
    menu.addAction(exportAction);
468
    menu.addAction(importAction);
lm's avatar
lm committed
469
    menu.addAction(deleteAction);
470 471 472
    menu.exec(event->globalPos());
}

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
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);
}

490 491 492 493 494 495
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
496 497 498
    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()));
499

500 501 502 503
    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()));

504 505 506
    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
507 508 509 510

    deleteAction = new QAction(tr("Delete this widget"), this);
    deleteAction->setStatusTip(tr("Delete this widget permanently"));
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteWidget()));
511 512 513 514 515 516 517

    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)"));
518
    connect(importAction, SIGNAL(triggered()), this, SLOT(importWidget()));
lm's avatar
lm committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532
}

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

QList<QGCToolWidgetItem*>* QGCToolWidget::itemList()
{
    static QList<QGCToolWidgetItem*>* instances;
    if (!instances) instances = new QList<QGCToolWidgetItem*>();
    return instances;
533
}
534 535
void QGCToolWidget::addParam(int uas,int component,QString paramname,QVariant value)
{
536
    isFromMetaData = true;
537 538 539 540 541 542 543 544 545 546 547 548 549
    QGCParamSlider* slider = new QGCParamSlider(this);
    connect(slider, SIGNAL(destroyed()), this, SLOT(storeSettings()));
    if (ui->hintLabel)
    {
        ui->hintLabel->deleteLater();
        ui->hintLabel = NULL;
    }
    toolLayout->addWidget(slider);
    slider->setActiveUAS(mav);
    slider->setParameterValue(uas,component,0,-1,paramname,value);


}
550 551 552 553

void QGCToolWidget::addParam()
{
    QGCParamSlider* slider = new QGCParamSlider(this);
554
    connect(slider, SIGNAL(destroyed()), this, SLOT(storeSettings()));
555 556
    if (ui->hintLabel)
    {
lm's avatar
lm committed
557
        ui->hintLabel->deleteLater();
558
        ui->hintLabel = NULL;
lm's avatar
lm committed
559
    }
560 561 562 563
    toolLayout->addWidget(slider);
    slider->startEditMode();
}

lm's avatar
lm committed
564 565 566
void QGCToolWidget::addCommand()
{
    QGCCommandButton* button = new QGCCommandButton(this);
567
    connect(button, SIGNAL(destroyed()), this, SLOT(storeSettings()));
568 569
    if (ui->hintLabel)
    {
lm's avatar
lm committed
570 571 572 573 574 575 576
        ui->hintLabel->deleteLater();
        ui->hintLabel = NULL;
    }
    toolLayout->addWidget(button);
    button->startEditMode();
}

577 578 579 580
void QGCToolWidget::addLabel()
{
    QGCTextLabel* label= new QGCTextLabel(this);
    connect(label, SIGNAL(destroyed()), this, SLOT(storeSettings()));
581 582 583 584 585
    if (ui->hintLabel)
    {
        ui->hintLabel->deleteLater();
        ui->hintLabel = NULL;
    }
586 587 588 589
    toolLayout->addWidget(label);
    label->startEditMode();
}

lm's avatar
lm committed
590 591
void QGCToolWidget::addToolWidget(QGCToolWidgetItem* widget)
{
592 593
    if (ui->hintLabel)
    {
lm's avatar
lm committed
594
        ui->hintLabel->deleteLater();
595
        ui->hintLabel = NULL;
lm's avatar
lm committed
596
    }
597
    connect(widget, SIGNAL(destroyed()), this, SLOT(storeSettings()));
lm's avatar
lm committed
598
    toolLayout->addWidget(widget);
599
    toolItemList.append(widget);
lm's avatar
lm committed
600 601
}

602 603
void QGCToolWidget::exportWidget()
{
604 605 606 607 608 609 610
    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);
    }
    storeSettings(fileName);
611 612
}

613
void QGCToolWidget::importWidget()
614
{
615 616 617
    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);
618 619
}

lm's avatar
lm committed
620 621
const QString QGCToolWidget::getTitle()
{
622
    return widgetTitle;
lm's avatar
lm committed
623 624
}

625 626 627
void QGCToolWidget::setTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
628 629
    if (parent)
    {
lm's avatar
lm committed
630
        bool ok;
631
        QString text = QInputDialog::getText(this, tr("Enter Widget Title"),
632 633
                                             tr("Widget title:"), QLineEdit::Normal,
                                             parent->windowTitle(), &ok);
634 635 636
        if (ok && !text.isEmpty())
        {
            setTitle(text);
lm's avatar
lm committed
637
        }
638 639
    }
}
lm's avatar
lm committed
640

641
void QGCToolWidget::setTitle(const QString& title)
642 643
{
    if (instances()->contains(widgetTitle)) instances()->remove(widgetTitle);
644
    if (!instances()->contains(title)) instances()->insert(title, this);
645

646
    // Sets title and calls setWindowTitle on QWidget
647 648
    widgetTitle = title;
    QWidget::setWindowTitle(title);
649 650 651 652 653 654
    setObjectName(widgetTitle);
    QDockWidget* dock = dynamic_cast<QDockWidget*>(this->parentWidget());
    if (dock) {
        dock->setWindowTitle(widgetTitle);
        dock->setObjectName(widgetTitle+"DOCK");
    }
655 656 657 658
    emit titleChanged(title);
    if (mainMenuAction) mainMenuAction->setText(title);
}

lm's avatar
lm committed
659 660 661 662 663 664 665 666 667 668 669 670
void QGCToolWidget::setMainMenuAction(QAction* action)
{
    this->mainMenuAction = action;
}

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

    // Hide
    this->hide();
    instances()->remove(getTitle());
671 672 673 674

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

lm's avatar
lm committed
677 678 679 680 681
    storeWidgetsToSettings();

    // Delete
    this->deleteLater();
}