QGCToolWidget.cc 21.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"
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();
120
        QString objname = settings->value("OBJECT_NAME", "").toString();
lm's avatar
lm committed
121

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

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

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

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

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

    for (int j = 0; j < size; j++)
    {
        QString type = settingsMap.value(widgetName + "\\" + QString::number(j) + "\\" + "TYPE", "UNKNOWN").toString();
        QGCToolWidgetItem* item = NULL;
        if (type == "COMMANDBUTTON")
        {
234 235
            //This shouldn't happen, but I'm not sure... so lets test for it.
            continue;
236 237 238 239 240 241 242
        }
        else if (type == "SLIDER")
        {
            QString checkparam = settingsMap.value(widgetName + "\\" + QString::number(j) + "\\" + "QGC_PARAM_SLIDER_PARAMID").toString();
            if (checkparam == parameterName)
            {
                item = new QGCParamSlider(this);
243
                paramToItemMap[parameterName] = item;
244 245 246 247 248 249 250 251 252 253 254 255 256
                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);
257
                paramToItemMap[parameterName] = item;
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 284 285
                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";
            }
286 287 288 289 290
            else if (type == "TEXT")
            {
                item = new QGCTextLabel(this);
                item->setActiveUAS(mav);
            }
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 316 317
            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();
}
318 319 320 321 322

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

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

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

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

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

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

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

430 431 432 433 434 435
void QGCToolWidget::storeSettings()
{
    QSettings settings;
    storeSettings(settings);
}

436 437 438 439 440 441 442 443
void QGCToolWidget::storeSettings(const QString& settingsFile)
{
    QSettings settings(settingsFile, QSettings::IniFormat);
    storeSettings(settings);
}

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

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

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

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

510 511 512 513 514 515
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
516 517 518
    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()));
519 520 521 522

    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
523 524 525 526

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

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

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


}
566 567 568 569

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

lm's avatar
lm committed
580 581 582
void QGCToolWidget::addCommand()
{
    QGCCommandButton* button = new QGCCommandButton(this);
583
    connect(button, SIGNAL(destroyed()), this, SLOT(storeSettings()));
584 585
    if (ui->hintLabel)
    {
lm's avatar
lm committed
586 587 588 589 590 591 592
        ui->hintLabel->deleteLater();
        ui->hintLabel = NULL;
    }
    toolLayout->addWidget(button);
    button->startEditMode();
}

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

605 606
void QGCToolWidget::exportWidget()
{
607 608 609 610 611 612 613
    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);
614 615
}

616
void QGCToolWidget::importWidget()
617
{
618 619 620
    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);
621 622
}

lm's avatar
lm committed
623 624
const QString QGCToolWidget::getTitle()
{
625
    return widgetTitle;
lm's avatar
lm committed
626 627
}

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

644 645 646
void QGCToolWidget::setWindowTitle(const QString& title)
{
    // Sets title and calls setWindowTitle on QWidget
647 648
    widgetTitle = title;
    QWidget::setWindowTitle(title);
649 650
}

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

660 661 662 663 664 665 666
    if (instances()->contains(widgetTitle)) instances()->remove(widgetTitle);

    // Switch to new title
    widgetTitle = title;

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

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

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

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

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

    // Delete
    this->deleteLater();
}