QGCToolWidget.cc 12.5 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 22
        QWidget(parent),
        mav(NULL),
        mainMenuAction(NULL),
        ui(new Ui::QGCToolWidget)
23 24
{
    ui->setupUi(this);
lm's avatar
lm committed
25
    setObjectName(title);
26
    createActions();
lm's avatar
lm committed
27
    toolLayout = ui->toolLayout;
28
    toolLayout->setAlignment(Qt::AlignTop);
29
    toolLayout->setSpacing(8);
30

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

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

lm's avatar
lm committed
44 45
    this->setWindowTitle(title);

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

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

lm's avatar
lm committed
62 63 64 65
/**
 * @param parent Object later holding these widgets, usually the main window
 * @return List of all widgets
 */
66
QList<QGCToolWidget*> QGCToolWidget::createWidgetsFromSettings(QWidget* parent, QString settingsFile)
lm's avatar
lm committed
67
{
68 69 70 71 72 73 74 75 76 77 78 79 80 81
    // 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
82
    QList<QGCToolWidget*> newWidgets;
83
    int size = settings->beginReadArray("QGC_TOOL_WIDGET_NAMES");
84
    for (int i = 0; i < size; i++) {
85 86
        settings->setArrayIndex(i);
        QString name = settings->value("TITLE", tr("UNKNOWN WIDGET %1").arg(i)).toString();
lm's avatar
lm committed
87

88
        if (!instances()->contains(name)) {
lm's avatar
lm committed
89 90 91 92
            QGCToolWidget* tool = new QGCToolWidget(name, parent);
            instances()->insert(name, tool);
            newWidgets.append(tool);
        }
93 94 95 96
        else
        {
            qDebug() << "WIDGET DID ALREADY EXIST, REJECTING";
        }
lm's avatar
lm committed
97
    }
98
    settings->endArray();
lm's avatar
lm committed
99 100 101 102

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

    // Load individual widget items
103
    for (int i = 0; i < newWidgets.size(); i++) {
104
        newWidgets.at(i)->loadSettings(*settings);
lm's avatar
lm committed
105
    }
106
    delete settings;
lm's avatar
lm committed
107 108 109 110

    return instances()->values();
}

111 112
void QGCToolWidget::loadSettings(const QString& settings)
{
113 114 115 116 117 118
    QSettings set(settings, QSettings::IniFormat);
    QStringList groups = set.childGroups();
    QString widgetName = groups.first();
    setTitle(widgetName);
    qDebug() << "WIDGET TITLE LOADED: " << widgetName;
    loadSettings(set);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
}

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
159 160
{
    // Store list of widgets
161 162 163 164 165 166 167 168 169 170 171
    QSettings* settings;
    if (!settingsFile.isEmpty())
    {
        settings = new QSettings(settingsFile, QSettings::IniFormat);
    }
    else
    {
        settings = new QSettings();
    }

    settings->beginWriteArray("QGC_TOOL_WIDGET_NAMES");
172
    for (int i = 0; i < instances()->size(); ++i) {
173 174
        settings->setArrayIndex(i);
        settings->setValue("TITLE", instances()->values().at(i)->getTitle());
lm's avatar
lm committed
175
    }
176
    settings->endArray();
lm's avatar
lm committed
177 178

    // Store individual widget items
179
    for (int i = 0; i < instances()->size(); ++i) {
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        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
204 205
        }
    }
206 207
    settings.endArray();
    settings.endGroup();
lm's avatar
lm committed
208 209
}

210 211 212
void QGCToolWidget::addUAS(UASInterface* uas)
{
    UAS* newMav = dynamic_cast<UAS*>(uas);
213
    if (newMav) {
214 215 216 217 218 219 220 221
        // FIXME Convert to list
        if (mav == NULL) mav = newMav;
    }
}

void QGCToolWidget::contextMenuEvent (QContextMenuEvent* event)
{
    QMenu menu(this);
222
    menu.addAction(addParamAction);
lm's avatar
lm committed
223
    menu.addAction(addCommandAction);
224
    menu.addAction(setTitleAction);
225
    menu.addAction(exportAction);
lm's avatar
lm committed
226
    menu.addAction(deleteAction);
lm's avatar
lm committed
227 228
    menu.addSeparator();
    menu.addAction(addButtonAction);
229 230 231
    menu.exec(event->globalPos());
}

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
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);
}

250 251 252 253 254 255
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
256 257 258
    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()));
259 260 261 262

    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
263 264 265 266

    deleteAction = new QAction(tr("Delete this widget"), this);
    deleteAction->setStatusTip(tr("Delete this widget permanently"));
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteWidget()));
267 268 269 270 271 272 273 274

    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
275 276 277 278

    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
279 280 281 282 283 284 285 286 287 288 289 290 291 292
}

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;
293 294 295 296 297
}

void QGCToolWidget::addParam()
{
    QGCParamSlider* slider = new QGCParamSlider(this);
298
    if (ui->hintLabel) {
lm's avatar
lm committed
299
        ui->hintLabel->deleteLater();
300
        ui->hintLabel = NULL;
lm's avatar
lm committed
301
    }
302 303 304 305 306 307 308
    toolLayout->addWidget(slider);
    slider->startEditMode();
}

void QGCToolWidget::addAction()
{
    QGCActionButton* button = new QGCActionButton(this);
309
    if (ui->hintLabel) {
lm's avatar
lm committed
310
        ui->hintLabel->deleteLater();
311
        ui->hintLabel = NULL;
lm's avatar
lm committed
312
    }
313 314 315 316
    toolLayout->addWidget(button);
    button->startEditMode();
}

lm's avatar
lm committed
317 318 319
void QGCToolWidget::addCommand()
{
    QGCCommandButton* button = new QGCCommandButton(this);
320
    if (ui->hintLabel) {
lm's avatar
lm committed
321 322 323 324 325 326 327
        ui->hintLabel->deleteLater();
        ui->hintLabel = NULL;
    }
    toolLayout->addWidget(button);
    button->startEditMode();
}

lm's avatar
lm committed
328 329
void QGCToolWidget::addToolWidget(QGCToolWidgetItem* widget)
{
330
    if (ui->hintLabel) {
lm's avatar
lm committed
331
        ui->hintLabel->deleteLater();
332
        ui->hintLabel = NULL;
lm's avatar
lm committed
333
    }
lm's avatar
lm committed
334 335 336
    toolLayout->addWidget(widget);
}

337 338
void QGCToolWidget::exportWidget()
{
339 340 341 342 343 344 345
    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);
346 347
}

348
void QGCToolWidget::importWidget()
349
{
350 351 352
    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);
353 354
}

lm's avatar
lm committed
355 356 357
const QString QGCToolWidget::getTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
358
    if (parent) {
lm's avatar
lm committed
359
        return parent->windowTitle();
360
    } else {
lm's avatar
lm committed
361 362 363 364 365
        return this->windowTitle();
    }
}


366 367 368
void QGCToolWidget::setTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
369
    if (parent) {
lm's avatar
lm committed
370
        bool ok;
371 372 373
        QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
                                             tr("Widget title:"), QLineEdit::Normal,
                                             parent->windowTitle(), &ok);
374
        if (ok && !text.isEmpty()) {
lm's avatar
lm committed
375 376 377 378 379
            QSettings settings;
            settings.beginGroup(parent->windowTitle());
            settings.remove("");
            settings.endGroup();
            parent->setWindowTitle(text);
380
            setWindowTitle(text);
lm's avatar
lm committed
381 382 383 384 385

            storeWidgetsToSettings();
            emit titleChanged(text);
            if (mainMenuAction) mainMenuAction->setText(text);
        }
386 387
    }
}
lm's avatar
lm committed
388

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
void QGCToolWidget::setTitle(QString title)
{
    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
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
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();
}