QGCToolWidget.cc 10.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 10
#include <QSettings>

11 12
#include "QGCParamSlider.h"
#include "QGCActionButton.h"
lm's avatar
lm committed
13
#include "QGCCommandButton.h"
14 15
#include "UASManager.h"

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

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

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

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

46 47 48 49 50 51 52 53 54 55
    QList<UASInterface*> systems = UASManager::instance()->getUASList();
    foreach (UASInterface* uas, systems)
    {
        UAS* newMav = dynamic_cast<UAS*>(uas);
        if (newMav)
        {
            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;
}

lm's avatar
lm committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/**
 * @param parent Object later holding these widgets, usually the main window
 * @return List of all widgets
 */
QList<QGCToolWidget*> QGCToolWidget::createWidgetsFromSettings(QWidget* parent)
{
    // Store list of widgets
    QSettings settings;
    QList<QGCToolWidget*> newWidgets;
    int size = settings.beginReadArray("QGC_TOOL_WIDGET_NAMES");
    for (int i = 0; i < size; i++)
    {
        settings.setArrayIndex(i);
        QString name = settings.value("TITLE", tr("UNKNOWN WIDGET %1").arg(i)).toString();

        if (!instances()->contains(name))
        {
            QGCToolWidget* tool = new QGCToolWidget(name, parent);
            instances()->insert(name, tool);
            newWidgets.append(tool);
        }
    }
    settings.endArray();

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

    // Load individual widget items
    for (int i = 0; i < newWidgets.size(); i++)
    {
        QString widgetName = newWidgets.at(i)->getTitle();
        qDebug() << "READING: " << widgetName;
        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(newWidgets.at(i));
                    qDebug() << "CREATED BUTTON";
                }
lm's avatar
lm committed
110 111 112 113 114
                else if (type == "COMMANDBUTTON")
                {
                    item = new QGCCommandButton(newWidgets.at(i));
                    qDebug() << "CREATED COMMANDBUTTON";
                }
115 116 117 118 119
                else if (type == "SLIDER")
                {
                    item = new QGCParamSlider(newWidgets.at(i));
                    qDebug() << "CREATED PARAM SLIDER";
                }
lm's avatar
lm committed
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

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

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

    return instances()->values();
}

void QGCToolWidget::storeWidgetsToSettings()
{
    // Store list of widgets
    QSettings settings;
    settings.beginWriteArray("QGC_TOOL_WIDGET_NAMES");
    for (int i = 0; i < instances()->size(); ++i)
    {
        settings.setArrayIndex(i);
        settings.setValue("TITLE", instances()->values().at(i)->getTitle());
    }
    settings.endArray();

    // Store individual widget items
    for (int i = 0; i < instances()->size(); ++i)
    {
        QString widgetName = instances()->values().at(i)->getTitle();
        settings.beginGroup(widgetName);
        settings.beginWriteArray("QGC_TOOL_WIDGET_ITEMS");
        int k = 0; // QGCToolItem counter
        for (int j = 0; j  < instances()->values().at(i)->children().size(); ++j)
        {
            // Store only QGCToolWidgetItems
            QGCToolWidgetItem* item = dynamic_cast<QGCToolWidgetItem*>(instances()->values().at(i)->children().at(j));
            if (item)
            {
                settings.setArrayIndex(k++);
                // Store the ToolWidgetItem
                item->writeSettings(settings);
            }
        }
        settings.endArray();
        settings.endGroup();
    }
}

177 178 179 180 181 182 183 184 185 186 187 188 189
void QGCToolWidget::addUAS(UASInterface* uas)
{
    UAS* newMav = dynamic_cast<UAS*>(uas);
    if (newMav)
    {
        // FIXME Convert to list
        if (mav == NULL) mav = newMav;
    }
}

void QGCToolWidget::contextMenuEvent (QContextMenuEvent* event)
{
    QMenu menu(this);
190
    menu.addAction(addParamAction);
lm's avatar
lm committed
191
    menu.addAction(addCommandAction);
192
    menu.addAction(setTitleAction);
lm's avatar
lm committed
193
    menu.addAction(deleteAction);
lm's avatar
lm committed
194 195
    menu.addSeparator();
    menu.addAction(addButtonAction);
196 197 198 199 200 201 202 203 204
    menu.exec(event->globalPos());
}

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
205 206 207
    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()));
208 209 210 211

    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
212 213 214 215

    deleteAction = new QAction(tr("Delete this widget"), this);
    deleteAction->setStatusTip(tr("Delete this widget permanently"));
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteWidget()));
216 217 218 219 220 221 222 223

    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
224 225 226 227

    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
228 229 230 231 232 233 234 235 236 237 238 239 240 241
}

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;
242 243 244 245 246
}

void QGCToolWidget::addParam()
{
    QGCParamSlider* slider = new QGCParamSlider(this);
lm's avatar
lm committed
247 248 249
    if (ui->hintLabel)
    {
        ui->hintLabel->deleteLater();
250
        ui->hintLabel = NULL;
lm's avatar
lm committed
251
    }
252 253 254 255 256 257 258
    toolLayout->addWidget(slider);
    slider->startEditMode();
}

void QGCToolWidget::addAction()
{
    QGCActionButton* button = new QGCActionButton(this);
lm's avatar
lm committed
259 260 261
    if (ui->hintLabel)
    {
        ui->hintLabel->deleteLater();
262
        ui->hintLabel = NULL;
lm's avatar
lm committed
263
    }
264 265 266 267
    toolLayout->addWidget(button);
    button->startEditMode();
}

lm's avatar
lm committed
268 269 270 271 272 273 274 275 276 277 278 279
void QGCToolWidget::addCommand()
{
    QGCCommandButton* button = new QGCCommandButton(this);
    if (ui->hintLabel)
    {
        ui->hintLabel->deleteLater();
        ui->hintLabel = NULL;
    }
    toolLayout->addWidget(button);
    button->startEditMode();
}

lm's avatar
lm committed
280 281
void QGCToolWidget::addToolWidget(QGCToolWidgetItem* widget)
{
lm's avatar
lm committed
282 283 284
    if (ui->hintLabel)
    {
        ui->hintLabel->deleteLater();
285
        ui->hintLabel = NULL;
lm's avatar
lm committed
286
    }
lm's avatar
lm committed
287 288 289
    toolLayout->addWidget(widget);
}

290 291 292 293 294 295 296 297 298 299
void QGCToolWidget::exportWidget()
{

}

void QGCToolWidget::importWidget(const QString& fileName)
{

}

lm's avatar
lm committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313
const QString QGCToolWidget::getTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
    if (parent)
    {
        return parent->windowTitle();
    }
    else
    {
        return this->windowTitle();
    }
}


314 315 316 317 318
void QGCToolWidget::setTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
    if (parent)
    {
lm's avatar
lm committed
319
        bool ok;
320 321 322 323
        QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
                                             tr("Widget title:"), QLineEdit::Normal,
                                             parent->windowTitle(), &ok);
        if (ok && !text.isEmpty())
lm's avatar
lm committed
324 325 326 327 328 329 330 331 332 333 334
        {
            QSettings settings;
            settings.beginGroup(parent->windowTitle());
            settings.remove("");
            settings.endGroup();
            parent->setWindowTitle(text);

            storeWidgetsToSettings();
            emit titleChanged(text);
            if (mainMenuAction) mainMenuAction->setText(text);
        }
335 336
    }
}
lm's avatar
lm committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

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();
}