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

lm's avatar
lm committed
18
QGCToolWidget::QGCToolWidget(const QString& title, QWidget *parent) :
19 20 21
        QWidget(parent),
        mav(NULL),
        mainMenuAction(NULL),
22
        widgetTitle(title),
23
        ui(new Ui::QGCToolWidget)
24 25
{
    ui->setupUi(this);
lm's avatar
lm committed
26
    setObjectName(title);
27
    createActions();
lm's avatar
lm committed
28
    toolLayout = ui->toolLayout;
29
    toolLayout->setAlignment(Qt::AlignTop);
30
    toolLayout->setSpacing(8);
31

lm's avatar
lm committed
32
    QDockWidget* dock = dynamic_cast<QDockWidget*>(this->parentWidget());
33
    if (dock) {
lm's avatar
lm committed
34
        dock->setWindowTitle(title);
35
        dock->setObjectName(title+"DOCK");
lm's avatar
lm committed
36
    }
37 38

    // Try with parent
39
    dock = dynamic_cast<QDockWidget*>(parent);
40
    if (dock) {
41 42 43 44
        dock->setWindowTitle(title);
        dock->setObjectName(title+"DOCK");
    }

lm's avatar
lm committed
45
    this->setWindowTitle(title);
46
    setObjectName(title+"WIDGET");
lm's avatar
lm committed
47

48
    QList<UASInterface*> systems = UASManager::instance()->getUASList();
49
    foreach (UASInterface* uas, systems) {
50
        UAS* newMav = dynamic_cast<UAS*>(uas);
51
        if (newMav) {
52 53 54 55
            addUAS(uas);
        }
    }
    connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)), this, SLOT(addUAS(UASInterface*)));
lm's avatar
lm committed
56
    if (!instances()->contains(title)) instances()->insert(title, this);
57 58 59 60 61 62 63
}

QGCToolWidget::~QGCToolWidget()
{
    delete ui;
}

64 65 66 67 68 69 70 71 72 73 74 75
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
76 77 78 79
/**
 * @param parent Object later holding these widgets, usually the main window
 * @return List of all widgets
 */
80
QList<QGCToolWidget*> QGCToolWidget::createWidgetsFromSettings(QWidget* parent, QString settingsFile)
lm's avatar
lm committed
81
{
82 83 84 85 86 87 88 89 90 91 92 93 94 95
    // 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();
    }

lm's avatar
lm committed
96
    QList<QGCToolWidget*> newWidgets;
97
    int size = settings->beginReadArray("QGC_TOOL_WIDGET_NAMES");
98
    for (int i = 0; i < size; i++) {
99 100
        settings->setArrayIndex(i);
        QString name = settings->value("TITLE", tr("UNKNOWN WIDGET %1").arg(i)).toString();
lm's avatar
lm committed
101

102
        if (!instances()->contains(name)) {
lm's avatar
lm committed
103 104 105 106
            QGCToolWidget* tool = new QGCToolWidget(name, parent);
            instances()->insert(name, tool);
            newWidgets.append(tool);
        }
107 108 109 110
        else
        {
            qDebug() << "WIDGET DID ALREADY EXIST, REJECTING";
        }
lm's avatar
lm committed
111
    }
112
    settings->endArray();
lm's avatar
lm committed
113 114 115 116

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

    // Load individual widget items
117
    for (int i = 0; i < newWidgets.size(); i++) {
118
        newWidgets.at(i)->loadSettings(*settings);
lm's avatar
lm committed
119
    }
120
    delete settings;
lm's avatar
lm committed
121 122 123 124

    return instances()->values();
}

LM's avatar
LM committed
125 126 127 128
/**
 * @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)
129
{
130 131
    QSettings set(settings, QSettings::IniFormat);
    QStringList groups = set.childGroups();
132 133 134 135 136 137 138 139 140 141 142 143 144
    if (groups.length() > 0)
    {
        QString widgetName = groups.first();
        if (singleinstance && QGCToolWidget::instances()->keys().contains(widgetName)) return false;
        setTitle(widgetName);
        qDebug() << "WIDGET TITLE LOADED: " << widgetName;
        loadSettings(set);
        return true;
    }
    else
    {
        return false;
    }
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
}

void QGCToolWidget::loadSettings(QSettings& settings)
{
    QString widgetName = getTitle();
    settings.beginGroup(widgetName);
    int size = settings.beginReadArray("QGC_TOOL_WIDGET_ITEMS");
    qDebug() << "CHILDREN SIZE:" << size;
    for (int j = 0; j < size; j++) {
        settings.setArrayIndex(j);
        QString type = settings.value("TYPE", "UNKNOWN").toString();
        if (type != "UNKNOWN") {
            QGCToolWidgetItem* item = NULL;
            if (type == "BUTTON") {
                item = new QGCActionButton(this);
                qDebug() << "CREATED BUTTON";
            } else if (type == "COMMANDBUTTON") {
                item = new QGCCommandButton(this);
                qDebug() << "CREATED COMMANDBUTTON";
            } else if (type == "SLIDER") {
                item = new QGCParamSlider(this);
                qDebug() << "CREATED PARAM SLIDER";
            }

            if (item) {
                // Configure and add to layout
                addToolWidget(item);
                item->readSettings(settings);

                qDebug() << "Created tool widget";
            }
        } else {
            qDebug() << "UNKNOWN TOOL WIDGET TYPE";
        }
    }
    settings.endArray();
    settings.endGroup();
}

void QGCToolWidget::storeWidgetsToSettings(QString settingsFile)
lm's avatar
lm committed
185 186
{
    // Store list of widgets
187 188 189 190 191 192 193 194 195 196 197
    QSettings* settings;
    if (!settingsFile.isEmpty())
    {
        settings = new QSettings(settingsFile, QSettings::IniFormat);
    }
    else
    {
        settings = new QSettings();
    }

    settings->beginWriteArray("QGC_TOOL_WIDGET_NAMES");
198
    for (int i = 0; i < instances()->size(); ++i) {
199 200
        settings->setArrayIndex(i);
        settings->setValue("TITLE", instances()->values().at(i)->getTitle());
lm's avatar
lm committed
201
    }
202
    settings->endArray();
lm's avatar
lm committed
203 204

    // Store individual widget items
205
    for (int i = 0; i < instances()->size(); ++i) {
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        instances()->values().at(i)->storeSettings(*settings);
    }
    delete settings;
}

void QGCToolWidget::storeSettings(const QString& settingsFile)
{
    QSettings settings(settingsFile, QSettings::IniFormat);
    storeSettings(settings);
}

void QGCToolWidget::storeSettings(QSettings& settings)
{
    QString widgetName = getTitle();
    settings.beginGroup(widgetName);
    settings.beginWriteArray("QGC_TOOL_WIDGET_ITEMS");
    int k = 0; // QGCToolItem counter
    for (int j = 0; j  < children().size(); ++j) {
        // Store only QGCToolWidgetItems
        QGCToolWidgetItem* item = dynamic_cast<QGCToolWidgetItem*>(children().at(j));
        if (item) {
            settings.setArrayIndex(k++);
            // Store the ToolWidgetItem
            item->writeSettings(settings);
lm's avatar
lm committed
230 231
        }
    }
232 233
    settings.endArray();
    settings.endGroup();
lm's avatar
lm committed
234 235
}

236 237 238
void QGCToolWidget::addUAS(UASInterface* uas)
{
    UAS* newMav = dynamic_cast<UAS*>(uas);
239
    if (newMav) {
240 241 242 243 244 245 246 247
        // FIXME Convert to list
        if (mav == NULL) mav = newMav;
    }
}

void QGCToolWidget::contextMenuEvent (QContextMenuEvent* event)
{
    QMenu menu(this);
248
    menu.addAction(addParamAction);
lm's avatar
lm committed
249
    menu.addAction(addCommandAction);
250
    menu.addAction(setTitleAction);
251
    menu.addAction(exportAction);
lm's avatar
lm committed
252
    menu.addAction(deleteAction);
lm's avatar
lm committed
253 254
    menu.addSeparator();
    menu.addAction(addButtonAction);
255 256 257
    menu.exec(event->globalPos());
}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
void QGCToolWidget::hideEvent(QHideEvent* event)
{
    // Store settings
    storeWidgetsToSettings();
    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);
}

276 277 278 279 280 281
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
282 283 284
    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()));
285 286 287 288

    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
289 290 291 292

    deleteAction = new QAction(tr("Delete this widget"), this);
    deleteAction->setStatusTip(tr("Delete this widget permanently"));
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteWidget()));
293 294 295 296 297 298 299 300

    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)"));
    connect(exportAction, SIGNAL(triggered()), this, SLOT(importWidget()));
lm's avatar
lm committed
301 302 303 304

    addButtonAction = new QAction(tr("New MAV Action Button (Deprecated)"), this);
    addButtonAction->setStatusTip(tr("Add a new action button to the tool"));
    connect(addButtonAction, SIGNAL(triggered()), this, SLOT(addAction()));
lm's avatar
lm committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318
}

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;
319 320 321 322 323
}

void QGCToolWidget::addParam()
{
    QGCParamSlider* slider = new QGCParamSlider(this);
324
    if (ui->hintLabel) {
lm's avatar
lm committed
325
        ui->hintLabel->deleteLater();
326
        ui->hintLabel = NULL;
lm's avatar
lm committed
327
    }
328 329 330 331 332 333 334
    toolLayout->addWidget(slider);
    slider->startEditMode();
}

void QGCToolWidget::addAction()
{
    QGCActionButton* button = new QGCActionButton(this);
335
    if (ui->hintLabel) {
lm's avatar
lm committed
336
        ui->hintLabel->deleteLater();
337
        ui->hintLabel = NULL;
lm's avatar
lm committed
338
    }
339 340 341 342
    toolLayout->addWidget(button);
    button->startEditMode();
}

lm's avatar
lm committed
343 344 345
void QGCToolWidget::addCommand()
{
    QGCCommandButton* button = new QGCCommandButton(this);
346
    if (ui->hintLabel) {
lm's avatar
lm committed
347 348 349 350 351 352 353
        ui->hintLabel->deleteLater();
        ui->hintLabel = NULL;
    }
    toolLayout->addWidget(button);
    button->startEditMode();
}

lm's avatar
lm committed
354 355
void QGCToolWidget::addToolWidget(QGCToolWidgetItem* widget)
{
356
    if (ui->hintLabel) {
lm's avatar
lm committed
357
        ui->hintLabel->deleteLater();
358
        ui->hintLabel = NULL;
lm's avatar
lm committed
359
    }
lm's avatar
lm committed
360 361 362
    toolLayout->addWidget(widget);
}

363 364
void QGCToolWidget::exportWidget()
{
365 366 367 368 369 370 371
    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);
372 373
}

374
void QGCToolWidget::importWidget()
375
{
376 377 378
    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);
379 380
}

lm's avatar
lm committed
381 382
const QString QGCToolWidget::getTitle()
{
383
    return widgetTitle;
lm's avatar
lm committed
384 385
}

386 387 388
void QGCToolWidget::setTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
389
    if (parent) {
lm's avatar
lm committed
390
        bool ok;
391 392 393
        QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
                                             tr("Widget title:"), QLineEdit::Normal,
                                             parent->windowTitle(), &ok);
394
        if (ok && !text.isEmpty()) {
lm's avatar
lm committed
395 396 397 398 399
            QSettings settings;
            settings.beginGroup(parent->windowTitle());
            settings.remove("");
            settings.endGroup();
            parent->setWindowTitle(text);
400
            setWindowTitle(text);
lm's avatar
lm committed
401 402 403 404 405

            storeWidgetsToSettings();
            emit titleChanged(text);
            if (mainMenuAction) mainMenuAction->setText(text);
        }
406 407
    }
}
lm's avatar
lm committed
408

409 410
void QGCToolWidget::setTitle(QString title)
{
411
    widgetTitle = title;
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
    if (parent) {
        QSettings settings;
        settings.beginGroup(parent->windowTitle());
        settings.remove("");
        settings.endGroup();
        parent->setWindowTitle(title);
    }
    setWindowTitle(title);

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

lm's avatar
lm committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
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
    mainMenuAction->deleteLater();
    this->deleteLater();
}