QGCToolWidget.cc 21 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

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

lm's avatar
lm committed
42
    QDockWidget* dock = dynamic_cast<QDockWidget*>(this->parentWidget());
43
    if (dock) {
44 45
        dock->setWindowTitle(widgetTitle);
        dock->setObjectName(widgetTitle+"DOCK");
lm's avatar
lm committed
46
    }
47 48

    // Try with parent
49
    dock = dynamic_cast<QDockWidget*>(parent);
50
    if (dock) {
51 52
        dock->setWindowTitle(widgetTitle);
        dock->setObjectName(widgetTitle+"DOCK");
53 54
    }

55
    this->setWindowTitle(widgetTitle);
56
    QList<UASInterface*> systems = UASManager::instance()->getUASList();
57 58
    foreach (UASInterface* uas, systems)
    {
59
        UAS* newMav = dynamic_cast<UAS*>(uas);
60 61
        if (newMav)
        {
62 63 64 65
            addUAS(uas);
        }
    }
    connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)), this, SLOT(addUAS(UASInterface*)));
66 67 68 69
    if (!instances()->contains(widgetTitle)) instances()->insert(widgetTitle, this);

    // Enforce storage if this not loaded from settings
    // is MUST NOT BE SAVED if it was loaded from settings!
70
    //if (!settings) storeWidgetsToSettings();
71 72 73 74
}

QGCToolWidget::~QGCToolWidget()
{
75 76
    if (mainMenuAction) mainMenuAction->deleteLater();
    if (QGCToolWidget::instances()) QGCToolWidget::instances()->remove(widgetTitle);
77 78 79
    delete ui;
}

80 81 82 83 84 85 86 87 88 89 90 91
void QGCToolWidget::setParent(QWidget *parent)
{
    QWidget::setParent(parent);
    // Try with parent
    QDockWidget* dock = dynamic_cast<QDockWidget*>(parent);
    if (dock)
    {
        dock->setWindowTitle(getTitle());
        dock->setObjectName(getTitle()+"DOCK");
    }
}

lm's avatar
lm committed
92 93 94 95
/**
 * @param parent Object later holding these widgets, usually the main window
 * @return List of all widgets
 */
96
QList<QGCToolWidget*> QGCToolWidget::createWidgetsFromSettings(QWidget* parent, QString settingsFile)
lm's avatar
lm committed
97
{
98 99 100 101 102 103 104
    // 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
105
       //qDebug() << "LOADING SETTINGS FROM" << settingsFile;
106 107 108 109
    }
    else
    {
        settings = new QSettings();
Lorenz Meier's avatar
Lorenz Meier committed
110
        //qDebug() << "LOADING SETTINGS FROM DEFAULT" << settings->fileName();
111 112
    }

lm's avatar
lm committed
113
    QList<QGCToolWidget*> newWidgets;
114
    settings->beginGroup("Custom_Tool_Widgets");
115
    int size = settings->beginReadArray("QGC_TOOL_WIDGET_NAMES");
116 117
    for (int i = 0; i < size; i++)
    {
118
        settings->setArrayIndex(i);
119
        QString name = settings->value("TITLE", "").toString();
lm's avatar
lm committed
120

121
        if (!instances()->contains(name) && name.length() != 0)
122
        {
Lorenz Meier's avatar
Lorenz Meier committed
123
            //qDebug() << "CREATED WIDGET:" << name;
124
            QGCToolWidget* tool = new QGCToolWidget(name, parent, settings);
lm's avatar
lm committed
125 126
            newWidgets.append(tool);
        }
127 128 129 130 131
        else if (name.length() == 0)
        {
            // Silently catch empty widget name - sanity check
            // to survive broken settings (e.g. from user manipulation)
        }
132 133
        else
        {
Lorenz Meier's avatar
Lorenz Meier committed
134
            //qDebug() << "WIDGET" << name << "DID ALREADY EXIST, REJECTING";
135
        }
lm's avatar
lm committed
136
    }
137
    settings->endArray();
lm's avatar
lm committed
138

Lorenz Meier's avatar
Lorenz Meier committed
139
    //qDebug() << "NEW WIDGETS: " << newWidgets.size();
lm's avatar
lm committed
140 141

    // Load individual widget items
142 143
    for (int i = 0; i < newWidgets.size(); i++)
    {
144
        newWidgets.at(i)->loadSettings(*settings);
lm's avatar
lm committed
145
    }
146 147
    settings->endGroup();
    settings->sync();
148
    delete settings;
lm's avatar
lm committed
149 150 151

    return instances()->values();
}
152 153 154 155 156 157 158 159 160 161 162 163 164 165
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
166

LM's avatar
LM committed
167 168 169 170
/**
 * @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)
171
{
172 173
    QSettings set(settings, QSettings::IniFormat);
    QStringList groups = set.childGroups();
174 175 176
    if (groups.length() > 0)
    {
        QString widgetName = groups.first();
177
	this->setObjectName(widgetName);
178
        if (singleinstance && QGCToolWidget::instances()->keys().contains(widgetName)) return false;
179 180 181
        // Do not use setTitle() here,
        // interferes with loading settings
        widgetTitle = widgetName;
Lorenz Meier's avatar
Lorenz Meier committed
182
        //qDebug() << "WIDGET TITLE LOADED: " << widgetName;
183 184 185 186 187 188 189
        loadSettings(set);
        return true;
    }
    else
    {
        return false;
    }
190
}
191 192
void QGCToolWidget::setSettings(QVariantMap& settings)
{
193
    isFromMetaData = true;
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    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();
220 221
    if (paramToItemMap.contains(parameterName))
    {
222
        //If we already have an item for this parameter, updates are handled internally.
223 224
        return;
    }
225 226 227 228 229 230 231

    for (int j = 0; j < size; j++)
    {
        QString type = settingsMap.value(widgetName + "\\" + QString::number(j) + "\\" + "TYPE", "UNKNOWN").toString();
        QGCToolWidgetItem* item = NULL;
        if (type == "COMMANDBUTTON")
        {
232 233
            //This shouldn't happen, but I'm not sure... so lets test for it.
            continue;
234 235 236 237 238 239 240
        }
        else if (type == "SLIDER")
        {
            QString checkparam = settingsMap.value(widgetName + "\\" + QString::number(j) + "\\" + "QGC_PARAM_SLIDER_PARAMID").toString();
            if (checkparam == parameterName)
            {
                item = new QGCParamSlider(this);
241
                paramToItemMap[parameterName] = item;
242 243 244 245 246 247 248 249 250 251 252 253 254
                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);
255
                paramToItemMap[parameterName] = item;
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
                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";
            }
284 285 286 287 288
            else if (type == "TEXT")
            {
                item = new QGCTextLabel(this);
                item->setActiveUAS(mav);
            }
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
            else if (type == "SLIDER")
            {
                item = new QGCParamSlider(this);
                //qDebug() << "CREATED PARAM SLIDER";
            }
            else if (type == "COMBO")
            {
                item = new QGCComboBox(this);
                //qDebug() << "CREATED PARAM COMBOBOX";
            }
            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();
}
316 317 318 319 320

void QGCToolWidget::loadSettings(QSettings& settings)
{
    QString widgetName = getTitle();
    settings.beginGroup(widgetName);
Lorenz Meier's avatar
Lorenz Meier committed
321
    //qDebug() << "LOADING FOR" << widgetName;
322
    int size = settings.beginReadArray("QGC_TOOL_WIDGET_ITEMS");
Lorenz Meier's avatar
Lorenz Meier committed
323
    //qDebug() << "CHILDREN SIZE:" << size;
324 325
    for (int j = 0; j < size; j++)
    {
326 327
        settings.setArrayIndex(j);
        QString type = settings.value("TYPE", "UNKNOWN").toString();
328 329
        if (type != "UNKNOWN")
        {
330
            QGCToolWidgetItem* item = NULL;
331 332
            if (type == "COMMANDBUTTON")
            {
333 334 335 336
                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
337
                //qDebug() << "CREATED COMMANDBUTTON";
338 339 340
            }
            else if (type == "SLIDER")
            {
341
                item = new QGCParamSlider(this);
342
                item->setActiveUAS(mav);
Lorenz Meier's avatar
Lorenz Meier committed
343
                //qDebug() << "CREATED PARAM SLIDER";
344
            }
345 346 347
            else if (type == "COMBO")
            {
                item = new QGCComboBox(this);
348
                item->setActiveUAS(mav);
349 350
                qDebug() << "CREATED PARAM COMBOBOX";
            }
351 352 353 354 355 356
            else if (type == "TEXT")
            {
                item = new QGCTextLabel(this);
                item->setObjectName(settings.value("QGC_TEXT_ID").toString());
                item->setActiveUAS(mav);
            }
357

358 359
            if (item)
            {
360 361 362 363
                // Configure and add to layout
                addToolWidget(item);
                item->readSettings(settings);

Lorenz Meier's avatar
Lorenz Meier committed
364
                //qDebug() << "Created tool widget";
365
            }
366 367 368
        }
        else
        {
Lorenz Meier's avatar
Lorenz Meier committed
369
            //qDebug() << "UNKNOWN TOOL WIDGET TYPE";
370 371 372 373 374 375 376
        }
    }
    settings.endArray();
    settings.endGroup();
}

void QGCToolWidget::storeWidgetsToSettings(QString settingsFile)
lm's avatar
lm committed
377 378
{
    // Store list of widgets
379 380 381 382
    QSettings* settings;
    if (!settingsFile.isEmpty())
    {
        settings = new QSettings(settingsFile, QSettings::IniFormat);
Lorenz Meier's avatar
Lorenz Meier committed
383
        //qDebug() << "STORING SETTINGS TO" << settings->fileName();
384 385 386 387
    }
    else
    {
        settings = new QSettings();
Lorenz Meier's avatar
Lorenz Meier committed
388
        //qDebug() << "STORING SETTINGS TO DEFAULT" << settings->fileName();
389 390
    }

391
    settings->beginGroup("Custom_Tool_Widgets");
392 393 394
    int preArraySize = settings->beginReadArray("QGC_TOOL_WIDGET_NAMES");
    settings->endArray();

395
    settings->beginWriteArray("QGC_TOOL_WIDGET_NAMES");
396
    int num = 0;
397
    for (int i = 0; i < qMax(preArraySize, instances()->size()); ++i)
398
    {
399 400 401
        if (i < instances()->size())
        {
            // Updating value
402 403 404 405 406 407
            if (!instances()->values().at(i)->fromMetaData())
            {
                settings->setArrayIndex(num++);
                settings->setValue("TITLE", instances()->values().at(i)->getTitle());
                //qDebug() << "WRITING TITLE" << instances()->values().at(i)->getTitle();
            }
408 409 410 411 412 413
        }
        else
        {
            // Deleting old value
            settings->remove("TITLE");
        }
lm's avatar
lm committed
414
    }
415
    settings->endArray();
lm's avatar
lm committed
416 417

    // Store individual widget items
418 419
    for (int i = 0; i < instances()->size(); ++i)
    {
420 421
        instances()->values().at(i)->storeSettings(*settings);
    }
422 423
    settings->endGroup();
    settings->sync();
424 425 426
    delete settings;
}

427 428 429 430 431 432
void QGCToolWidget::storeSettings()
{
    QSettings settings;
    storeSettings(settings);
}

433 434 435 436 437 438 439 440
void QGCToolWidget::storeSettings(const QString& settingsFile)
{
    QSettings settings(settingsFile, QSettings::IniFormat);
    storeSettings(settings);
}

void QGCToolWidget::storeSettings(QSettings& settings)
{
441 442 443 444 445
    if (isFromMetaData)
    {
        //Refuse to store if this is loaded from metadata or dynamically generated.
        return;
    }
Lorenz Meier's avatar
Lorenz Meier committed
446
    //qDebug() << "WRITING WIDGET" << widgetTitle << "TO SETTINGS";
447
    settings.beginGroup(widgetTitle);
448 449
    settings.beginWriteArray("QGC_TOOL_WIDGET_ITEMS");
    int k = 0; // QGCToolItem counter
450 451
    for (int j = 0; j  < children().size(); ++j)
    {
452 453
        // Store only QGCToolWidgetItems
        QGCToolWidgetItem* item = dynamic_cast<QGCToolWidgetItem*>(children().at(j));
454 455
        if (item)
        {
456
            // Only count actual tool widget item children
457 458 459
            settings.setArrayIndex(k++);
            // Store the ToolWidgetItem
            item->writeSettings(settings);
lm's avatar
lm committed
460 461
        }
    }
Lorenz Meier's avatar
Lorenz Meier committed
462
    //qDebug() << "WROTE" << k << "SUB-WIDGETS TO SETTINGS";
463 464
    settings.endArray();
    settings.endGroup();
lm's avatar
lm committed
465 466
}

467 468 469
void QGCToolWidget::addUAS(UASInterface* uas)
{
    UAS* newMav = dynamic_cast<UAS*>(uas);
470 471
    if (newMav)
    {
472 473 474 475 476 477 478 479
        // FIXME Convert to list
        if (mav == NULL) mav = newMav;
    }
}

void QGCToolWidget::contextMenuEvent (QContextMenuEvent* event)
{
    QMenu menu(this);
480
    menu.addAction(addParamAction);
lm's avatar
lm committed
481
    menu.addAction(addCommandAction);
482
    menu.addSeparator();
483
    menu.addAction(setTitleAction);
484
    menu.addAction(exportAction);
485
    menu.addAction(importAction);
lm's avatar
lm committed
486
    menu.addAction(deleteAction);
487 488 489
    menu.exec(event->globalPos());
}

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
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);
}

507 508 509 510 511 512
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
513 514 515
    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()));
516 517 518 519

    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
520 521 522 523

    deleteAction = new QAction(tr("Delete this widget"), this);
    deleteAction->setStatusTip(tr("Delete this widget permanently"));
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteWidget()));
524 525 526 527 528 529 530

    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)"));
531
    connect(importAction, SIGNAL(triggered()), this, SLOT(importWidget()));
lm's avatar
lm committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545
}

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;
546
}
547 548
void QGCToolWidget::addParam(int uas,int component,QString paramname,QVariant value)
{
549
    isFromMetaData = true;
550 551 552 553 554 555 556 557 558 559 560 561 562
    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);


}
563 564 565 566

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

lm's avatar
lm committed
577 578 579
void QGCToolWidget::addCommand()
{
    QGCCommandButton* button = new QGCCommandButton(this);
580
    connect(button, SIGNAL(destroyed()), this, SLOT(storeSettings()));
581 582
    if (ui->hintLabel)
    {
lm's avatar
lm committed
583 584 585 586 587 588 589
        ui->hintLabel->deleteLater();
        ui->hintLabel = NULL;
    }
    toolLayout->addWidget(button);
    button->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 642 643
void QGCToolWidget::setWindowTitle(const QString& title)
{
    // Sets title and calls setWindowTitle on QWidget
644 645
    widgetTitle = title;
    QWidget::setWindowTitle(title);
646 647
}

648 649
void QGCToolWidget::setTitle(QString title)
{
650
    // Remove references to old title
651
    /*QSettings settings;
652 653 654
    settings.beginGroup(widgetTitle);
    settings.remove("");
    settings.endGroup();
655
    settings.sync();*/
656

657 658 659 660 661 662 663
    if (instances()->contains(widgetTitle)) instances()->remove(widgetTitle);

    // Switch to new title
    widgetTitle = title;

    if (!instances()->contains(title)) instances()->insert(title, this);
    QWidget::setWindowTitle(title);
664
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
665
    if (parent) parent->setWindowTitle(title);
666
    // Store all widgets
667
    //storeWidgetsToSettings();
668 669 670 671 672

    emit titleChanged(title);
    if (mainMenuAction) mainMenuAction->setText(title);
}

lm's avatar
lm committed
673 674 675 676 677 678 679 680 681 682 683 684
void QGCToolWidget::setMainMenuAction(QAction* action)
{
    this->mainMenuAction = action;
}

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

    // Hide
    this->hide();
    instances()->remove(getTitle());
685
    /*QSettings settings;
lm's avatar
lm committed
686 687 688
    settings.beginGroup(getTitle());
    settings.remove("");
    settings.endGroup();
689
    storeWidgetsToSettings();*/
lm's avatar
lm committed
690 691 692 693 694
    storeWidgetsToSettings();

    // Delete
    this->deleteLater();
}