QGCToolWidget.cc 14.6 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"
lm's avatar
lm committed
14
#include "QGCCommandButton.h"
15 16
#include "UASManager.h"

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

27 28 29 30 31
    if (title == "Unnamed Tool")
    {
        widgetTitle = QString("%1 %2").arg(title).arg(QGCToolWidget::instances()->count());
    }
    qDebug() << "WidgetTitle" << widgetTitle;
32 33

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

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

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

52
    this->setWindowTitle(widgetTitle);
53
    QList<UASInterface*> systems = UASManager::instance()->getUASList();
54 55
    foreach (UASInterface* uas, systems)
    {
56
        UAS* newMav = dynamic_cast<UAS*>(uas);
57 58
        if (newMav)
        {
59 60 61 62
            addUAS(uas);
        }
    }
    connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)), this, SLOT(addUAS(UASInterface*)));
63 64 65 66 67
    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!
    if (!settings) storeWidgetsToSettings();
68 69 70 71
}

QGCToolWidget::~QGCToolWidget()
{
72 73
    if (mainMenuAction) mainMenuAction->deleteLater();
    if (QGCToolWidget::instances()) QGCToolWidget::instances()->remove(widgetTitle);
74 75 76
    delete ui;
}

77 78 79 80 81 82 83 84 85 86 87 88
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
89 90 91 92
/**
 * @param parent Object later holding these widgets, usually the main window
 * @return List of all widgets
 */
93
QList<QGCToolWidget*> QGCToolWidget::createWidgetsFromSettings(QWidget* parent, QString settingsFile)
lm's avatar
lm committed
94
{
95 96 97 98 99 100 101 102 103 104 105 106
    // Load widgets from application settings
    QSettings* settings;

    // Or load them from a settings file
    if (!settingsFile.isEmpty())
    {
        settings = new QSettings(settingsFile, QSettings::IniFormat);
        qDebug() << "LOADING SETTINGS FROM" << settingsFile;
    }
    else
    {
        settings = new QSettings();
107
        qDebug() << "LOADING SETTINGS FROM DEFAULT" << settings->fileName();
108 109
    }

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

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

    qDebug() << "NEW WIDGETS: " << newWidgets.size();

    // Load individual widget items
138 139
    for (int i = 0; i < newWidgets.size(); i++)
    {
140
        newWidgets.at(i)->loadSettings(*settings);
lm's avatar
lm committed
141
    }
142
    delete settings;
lm's avatar
lm committed
143 144 145 146

    return instances()->values();
}

LM's avatar
LM committed
147 148 149 150
/**
 * @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)
151
{
152 153
    QSettings set(settings, QSettings::IniFormat);
    QStringList groups = set.childGroups();
154 155 156 157
    if (groups.length() > 0)
    {
        QString widgetName = groups.first();
        if (singleinstance && QGCToolWidget::instances()->keys().contains(widgetName)) return false;
158 159 160
        // Do not use setTitle() here,
        // interferes with loading settings
        widgetTitle = widgetName;
161 162 163 164 165 166 167 168
        qDebug() << "WIDGET TITLE LOADED: " << widgetName;
        loadSettings(set);
        return true;
    }
    else
    {
        return false;
    }
169 170 171 172 173 174
}

void QGCToolWidget::loadSettings(QSettings& settings)
{
    QString widgetName = getTitle();
    settings.beginGroup(widgetName);
175
    qDebug() << "LOADING FOR" << widgetName;
176 177
    int size = settings.beginReadArray("QGC_TOOL_WIDGET_ITEMS");
    qDebug() << "CHILDREN SIZE:" << size;
178 179
    for (int j = 0; j < size; j++)
    {
180 181
        settings.setArrayIndex(j);
        QString type = settings.value("TYPE", "UNKNOWN").toString();
182 183
        if (type != "UNKNOWN")
        {
184
            QGCToolWidgetItem* item = NULL;
185 186
            if (type == "COMMANDBUTTON")
            {
187 188
                item = new QGCCommandButton(this);
                qDebug() << "CREATED COMMANDBUTTON";
189 190 191
            }
            else if (type == "SLIDER")
            {
192 193 194 195
                item = new QGCParamSlider(this);
                qDebug() << "CREATED PARAM SLIDER";
            }

196 197
            if (item)
            {
198 199 200 201 202 203
                // Configure and add to layout
                addToolWidget(item);
                item->readSettings(settings);

                qDebug() << "Created tool widget";
            }
204 205 206
        }
        else
        {
207 208 209 210 211 212 213 214
            qDebug() << "UNKNOWN TOOL WIDGET TYPE";
        }
    }
    settings.endArray();
    settings.endGroup();
}

void QGCToolWidget::storeWidgetsToSettings(QString settingsFile)
lm's avatar
lm committed
215 216
{
    // Store list of widgets
217 218 219 220
    QSettings* settings;
    if (!settingsFile.isEmpty())
    {
        settings = new QSettings(settingsFile, QSettings::IniFormat);
221
        qDebug() << "STORING SETTINGS TO" << settings->fileName();
222 223 224 225
    }
    else
    {
        settings = new QSettings();
226
        qDebug() << "STORING SETTINGS TO DEFAULT" << settings->fileName();
227 228
    }

229 230 231
    int preArraySize = settings->beginReadArray("QGC_TOOL_WIDGET_NAMES");
    settings->endArray();

232
    settings->beginWriteArray("QGC_TOOL_WIDGET_NAMES");
233
    for (int i = 0; i < qMax(preArraySize, instances()->size()); ++i)
234
    {
235
        settings->setArrayIndex(i);
236 237 238 239 240 241 242 243 244 245 246
        if (i < instances()->size())
        {
            // Updating value
            settings->setValue("TITLE", instances()->values().at(i)->getTitle());
            qDebug() << "WRITING TITLE" << instances()->values().at(i)->getTitle();
        }
        else
        {
            // Deleting old value
            settings->remove("TITLE");
        }
lm's avatar
lm committed
247
    }
248
    settings->endArray();
lm's avatar
lm committed
249 250

    // Store individual widget items
251 252
    for (int i = 0; i < instances()->size(); ++i)
    {
253 254 255 256 257
        instances()->values().at(i)->storeSettings(*settings);
    }
    delete settings;
}

258 259 260 261 262 263
void QGCToolWidget::storeSettings()
{
    QSettings settings;
    storeSettings(settings);
}

264 265 266 267 268 269 270 271
void QGCToolWidget::storeSettings(const QString& settingsFile)
{
    QSettings settings(settingsFile, QSettings::IniFormat);
    storeSettings(settings);
}

void QGCToolWidget::storeSettings(QSettings& settings)
{
272 273
    qDebug() << "WRITING WIDGET" << widgetTitle << "TO SETTINGS";
    settings.beginGroup(widgetTitle);
274 275
    settings.beginWriteArray("QGC_TOOL_WIDGET_ITEMS");
    int k = 0; // QGCToolItem counter
276 277
    for (int j = 0; j  < children().size(); ++j)
    {
278 279
        // Store only QGCToolWidgetItems
        QGCToolWidgetItem* item = dynamic_cast<QGCToolWidgetItem*>(children().at(j));
280 281
        if (item)
        {
282
            // Only count actual tool widget item children
283 284 285
            settings.setArrayIndex(k++);
            // Store the ToolWidgetItem
            item->writeSettings(settings);
lm's avatar
lm committed
286 287
        }
    }
288
    qDebug() << "WROTE" << k << "SUB-WIDGETS TO SETTINGS";
289 290
    settings.endArray();
    settings.endGroup();
lm's avatar
lm committed
291 292
}

293 294 295
void QGCToolWidget::addUAS(UASInterface* uas)
{
    UAS* newMav = dynamic_cast<UAS*>(uas);
296 297
    if (newMav)
    {
298 299 300 301 302 303 304 305
        // FIXME Convert to list
        if (mav == NULL) mav = newMav;
    }
}

void QGCToolWidget::contextMenuEvent (QContextMenuEvent* event)
{
    QMenu menu(this);
306
    menu.addAction(addParamAction);
lm's avatar
lm committed
307
    menu.addAction(addCommandAction);
308
    menu.addSeparator();
309
    menu.addAction(setTitleAction);
310
    menu.addAction(exportAction);
311
    menu.addAction(importAction);
lm's avatar
lm committed
312
    menu.addAction(deleteAction);
313 314 315
    menu.exec(event->globalPos());
}

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
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);
}

333 334 335 336 337 338
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
339 340 341
    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()));
342 343 344 345

    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
346 347 348 349

    deleteAction = new QAction(tr("Delete this widget"), this);
    deleteAction->setStatusTip(tr("Delete this widget permanently"));
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteWidget()));
350 351 352 353 354 355 356

    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)"));
357
    connect(importAction, SIGNAL(triggered()), this, SLOT(importWidget()));
lm's avatar
lm committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371
}

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;
372 373 374 375 376
}

void QGCToolWidget::addParam()
{
    QGCParamSlider* slider = new QGCParamSlider(this);
377
    connect(slider, SIGNAL(destroyed()), this, SLOT(storeSettings()));
378 379
    if (ui->hintLabel)
    {
lm's avatar
lm committed
380
        ui->hintLabel->deleteLater();
381
        ui->hintLabel = NULL;
lm's avatar
lm committed
382
    }
383 384 385 386
    toolLayout->addWidget(slider);
    slider->startEditMode();
}

lm's avatar
lm committed
387 388 389
void QGCToolWidget::addCommand()
{
    QGCCommandButton* button = new QGCCommandButton(this);
390
    connect(button, SIGNAL(destroyed()), this, SLOT(storeSettings()));
391 392
    if (ui->hintLabel)
    {
lm's avatar
lm committed
393 394 395 396 397 398 399
        ui->hintLabel->deleteLater();
        ui->hintLabel = NULL;
    }
    toolLayout->addWidget(button);
    button->startEditMode();
}

lm's avatar
lm committed
400 401
void QGCToolWidget::addToolWidget(QGCToolWidgetItem* widget)
{
402 403
    if (ui->hintLabel)
    {
lm's avatar
lm committed
404
        ui->hintLabel->deleteLater();
405
        ui->hintLabel = NULL;
lm's avatar
lm committed
406
    }
407
    connect(widget, SIGNAL(destroyed()), this, SLOT(storeSettings()));
lm's avatar
lm committed
408 409 410
    toolLayout->addWidget(widget);
}

411 412
void QGCToolWidget::exportWidget()
{
413 414 415 416 417 418 419
    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);
420 421
}

422
void QGCToolWidget::importWidget()
423
{
424 425 426
    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);
427 428
}

lm's avatar
lm committed
429 430
const QString QGCToolWidget::getTitle()
{
431
    return widgetTitle;
lm's avatar
lm committed
432 433
}

434 435 436
void QGCToolWidget::setTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
437 438
    if (parent)
    {
lm's avatar
lm committed
439
        bool ok;
440
        QString text = QInputDialog::getText(this, tr("Enter Widget Title"),
441 442
                                             tr("Widget title:"), QLineEdit::Normal,
                                             parent->windowTitle(), &ok);
443 444 445
        if (ok && !text.isEmpty())
        {
            setTitle(text);
lm's avatar
lm committed
446
        }
447 448
    }
}
lm's avatar
lm committed
449

450 451 452
void QGCToolWidget::setWindowTitle(const QString& title)
{
    // Sets title and calls setWindowTitle on QWidget
453 454
    widgetTitle = title;
    QWidget::setWindowTitle(title);
455 456
}

457 458
void QGCToolWidget::setTitle(QString title)
{
459
    // Remove references to old title
460 461 462 463 464 465
    QSettings settings;
    settings.beginGroup(widgetTitle);
    settings.remove("");
    settings.endGroup();
    settings.sync();

466 467 468 469 470 471 472
    if (instances()->contains(widgetTitle)) instances()->remove(widgetTitle);

    // Switch to new title
    widgetTitle = title;

    if (!instances()->contains(title)) instances()->insert(title, this);
    QWidget::setWindowTitle(title);
473
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
474
    if (parent) parent->setWindowTitle(title);
475 476
    // Store all widgets
    storeWidgetsToSettings();
477 478 479 480 481

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

lm's avatar
lm committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
void QGCToolWidget::setMainMenuAction(QAction* action)
{
    this->mainMenuAction = action;
}

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

    // Hide
    this->hide();
    instances()->remove(getTitle());
    QSettings settings;
    settings.beginGroup(getTitle());
    settings.remove("");
    settings.endGroup();
    storeWidgetsToSettings();

    // Delete
    this->deleteLater();
}