QGCToolWidget.cc 8.26 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 13 14
#include "QGCParamSlider.h"
#include "QGCActionButton.h"
#include "UASManager.h"

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

lm's avatar
lm committed
27 28 29 30 31 32 33
    QDockWidget* dock = dynamic_cast<QDockWidget*>(this->parentWidget());
    if (dock)
    {
        dock->setWindowTitle(title);
    }
    this->setWindowTitle(title);

34 35 36 37 38 39 40 41 42 43
    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
44
    if (!instances()->contains(title)) instances()->insert(title, this);
45 46 47 48 49 50 51
}

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

lm's avatar
lm committed
52 53 54 55 56 57 58 59 60 61 62 63 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 110 111 112 113 114 115 116 117 118 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
/**
 * @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";
                }

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

155 156 157 158 159 160 161 162 163 164 165 166 167
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);
168
    //menu.addAction(addParamAction);
169 170
    menu.addAction(addButtonAction);
    menu.addAction(setTitleAction);
lm's avatar
lm committed
171
    menu.addAction(deleteAction);
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    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()));

    addButtonAction = new QAction(tr("New MAV &Action Button"), this);
    addButtonAction->setStatusTip(tr("Add a new action button to the tool"));
    connect(addButtonAction, SIGNAL(triggered()), this, SLOT(addAction()));

    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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

    deleteAction = new QAction(tr("Delete this widget"), this);
    deleteAction->setStatusTip(tr("Delete this widget permanently"));
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteWidget()));
}

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;
206 207 208 209 210
}

void QGCToolWidget::addParam()
{
    QGCParamSlider* slider = new QGCParamSlider(this);
lm's avatar
lm committed
211 212 213 214
    if (ui->hintLabel)
    {
        ui->hintLabel->deleteLater();
    }
215 216 217 218 219 220 221
    toolLayout->addWidget(slider);
    slider->startEditMode();
}

void QGCToolWidget::addAction()
{
    QGCActionButton* button = new QGCActionButton(this);
lm's avatar
lm committed
222 223 224 225
    if (ui->hintLabel)
    {
        ui->hintLabel->deleteLater();
    }
226 227 228 229
    toolLayout->addWidget(button);
    button->startEditMode();
}

lm's avatar
lm committed
230 231
void QGCToolWidget::addToolWidget(QGCToolWidgetItem* widget)
{
lm's avatar
lm committed
232 233 234 235
    if (ui->hintLabel)
    {
        ui->hintLabel->deleteLater();
    }
lm's avatar
lm committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    toolLayout->addWidget(widget);
}

const QString QGCToolWidget::getTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
    if (parent)
    {
        return parent->windowTitle();
    }
    else
    {
        return this->windowTitle();
    }
}


253 254 255 256 257
void QGCToolWidget::setTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
    if (parent)
    {
lm's avatar
lm committed
258
        bool ok;
259 260 261 262
        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
263 264 265 266 267 268 269 270 271 272 273
        {
            QSettings settings;
            settings.beginGroup(parent->windowTitle());
            settings.remove("");
            settings.endGroup();
            parent->setWindowTitle(text);

            storeWidgetsToSettings();
            emit titleChanged(text);
            if (mainMenuAction) mainMenuAction->setText(text);
        }
274 275
    }
}
lm's avatar
lm committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

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