QGCToolWidget.cc 9.88 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 19 20
    QWidget(parent),
    mav(NULL),
    mainMenuAction(NULL),
    ui(new Ui::QGCToolWidget)
21 22
{
    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
    QDockWidget* dock = dynamic_cast<QDockWidget*>(this->parentWidget());
30
    if (dock) {
lm's avatar
lm committed
31
        dock->setWindowTitle(title);
32
        dock->setObjectName(title+"DOCK");
lm's avatar
lm committed
33
    }
34 35

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

lm's avatar
lm committed
42 43
    this->setWindowTitle(title);

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

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

lm's avatar
lm committed
60 61 62 63 64 65 66 67 68 69
/**
 * @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");
70
    for (int i = 0; i < size; i++) {
lm's avatar
lm committed
71 72 73
        settings.setArrayIndex(i);
        QString name = settings.value("TITLE", tr("UNKNOWN WIDGET %1").arg(i)).toString();

74
        if (!instances()->contains(name)) {
lm's avatar
lm committed
75 76 77 78 79 80 81 82 83 84
            QGCToolWidget* tool = new QGCToolWidget(name, parent);
            instances()->insert(name, tool);
            newWidgets.append(tool);
        }
    }
    settings.endArray();

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

    // Load individual widget items
85
    for (int i = 0; i < newWidgets.size(); i++) {
lm's avatar
lm committed
86 87 88 89 90
        QString widgetName = newWidgets.at(i)->getTitle();
        qDebug() << "READING: " << widgetName;
        settings.beginGroup(widgetName);
        int size = settings.beginReadArray("QGC_TOOL_WIDGET_ITEMS");
        qDebug() << "CHILDREN SIZE:" << size;
91
        for (int j = 0; j < size; j++) {
lm's avatar
lm committed
92 93
            settings.setArrayIndex(j);
            QString type = settings.value("TYPE", "UNKNOWN").toString();
94
            if (type != "UNKNOWN") {
lm's avatar
lm committed
95
                QGCToolWidgetItem* item = NULL;
96
                if (type == "BUTTON") {
lm's avatar
lm committed
97 98
                    item = new QGCActionButton(newWidgets.at(i));
                    qDebug() << "CREATED BUTTON";
99
                } else if (type == "COMMANDBUTTON") {
lm's avatar
lm committed
100 101
                    item = new QGCCommandButton(newWidgets.at(i));
                    qDebug() << "CREATED COMMANDBUTTON";
102
                } else if (type == "SLIDER") {
103 104 105
                    item = new QGCParamSlider(newWidgets.at(i));
                    qDebug() << "CREATED PARAM SLIDER";
                }
lm's avatar
lm committed
106

107
                if (item) {
lm's avatar
lm committed
108 109 110 111 112 113
                    // Configure and add to layout
                    newWidgets.at(i)->addToolWidget(item);
                    item->readSettings(settings);

                    qDebug() << "Created tool widget";
                }
114
            } else {
lm's avatar
lm committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
                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");
130
    for (int i = 0; i < instances()->size(); ++i) {
lm's avatar
lm committed
131 132 133 134 135 136
        settings.setArrayIndex(i);
        settings.setValue("TITLE", instances()->values().at(i)->getTitle());
    }
    settings.endArray();

    // Store individual widget items
137
    for (int i = 0; i < instances()->size(); ++i) {
lm's avatar
lm committed
138 139 140 141
        QString widgetName = instances()->values().at(i)->getTitle();
        settings.beginGroup(widgetName);
        settings.beginWriteArray("QGC_TOOL_WIDGET_ITEMS");
        int k = 0; // QGCToolItem counter
142
        for (int j = 0; j  < instances()->values().at(i)->children().size(); ++j) {
lm's avatar
lm committed
143 144
            // Store only QGCToolWidgetItems
            QGCToolWidgetItem* item = dynamic_cast<QGCToolWidgetItem*>(instances()->values().at(i)->children().at(j));
145
            if (item) {
lm's avatar
lm committed
146 147 148 149 150 151 152 153 154 155
                settings.setArrayIndex(k++);
                // Store the ToolWidgetItem
                item->writeSettings(settings);
            }
        }
        settings.endArray();
        settings.endGroup();
    }
}

156 157 158
void QGCToolWidget::addUAS(UASInterface* uas)
{
    UAS* newMav = dynamic_cast<UAS*>(uas);
159
    if (newMav) {
160 161 162 163 164 165 166 167
        // FIXME Convert to list
        if (mav == NULL) mav = newMav;
    }
}

void QGCToolWidget::contextMenuEvent (QContextMenuEvent* event)
{
    QMenu menu(this);
168
    menu.addAction(addParamAction);
lm's avatar
lm committed
169
    menu.addAction(addCommandAction);
170
    menu.addAction(setTitleAction);
lm's avatar
lm committed
171
    menu.addAction(deleteAction);
lm's avatar
lm committed
172 173
    menu.addSeparator();
    menu.addAction(addButtonAction);
174 175 176 177 178 179 180 181 182
    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
183 184 185
    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()));
186 187 188 189

    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
190 191 192 193

    deleteAction = new QAction(tr("Delete this widget"), this);
    deleteAction->setStatusTip(tr("Delete this widget permanently"));
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteWidget()));
194 195 196 197 198 199 200 201

    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
202 203 204 205

    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
206 207 208 209 210 211 212 213 214 215 216 217 218 219
}

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;
220 221 222 223 224
}

void QGCToolWidget::addParam()
{
    QGCParamSlider* slider = new QGCParamSlider(this);
225
    if (ui->hintLabel) {
lm's avatar
lm committed
226
        ui->hintLabel->deleteLater();
227
        ui->hintLabel = NULL;
lm's avatar
lm committed
228
    }
229 230 231 232 233 234 235
    toolLayout->addWidget(slider);
    slider->startEditMode();
}

void QGCToolWidget::addAction()
{
    QGCActionButton* button = new QGCActionButton(this);
236
    if (ui->hintLabel) {
lm's avatar
lm committed
237
        ui->hintLabel->deleteLater();
238
        ui->hintLabel = NULL;
lm's avatar
lm committed
239
    }
240 241 242 243
    toolLayout->addWidget(button);
    button->startEditMode();
}

lm's avatar
lm committed
244 245 246
void QGCToolWidget::addCommand()
{
    QGCCommandButton* button = new QGCCommandButton(this);
247
    if (ui->hintLabel) {
lm's avatar
lm committed
248 249 250 251 252 253 254
        ui->hintLabel->deleteLater();
        ui->hintLabel = NULL;
    }
    toolLayout->addWidget(button);
    button->startEditMode();
}

lm's avatar
lm committed
255 256
void QGCToolWidget::addToolWidget(QGCToolWidgetItem* widget)
{
257
    if (ui->hintLabel) {
lm's avatar
lm committed
258
        ui->hintLabel->deleteLater();
259
        ui->hintLabel = NULL;
lm's avatar
lm committed
260
    }
lm's avatar
lm committed
261 262 263
    toolLayout->addWidget(widget);
}

264 265 266 267 268 269 270 271 272 273
void QGCToolWidget::exportWidget()
{

}

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

}

lm's avatar
lm committed
274 275 276
const QString QGCToolWidget::getTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
277
    if (parent) {
lm's avatar
lm committed
278
        return parent->windowTitle();
279
    } else {
lm's avatar
lm committed
280 281 282 283 284
        return this->windowTitle();
    }
}


285 286 287
void QGCToolWidget::setTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
288
    if (parent) {
lm's avatar
lm committed
289
        bool ok;
290 291 292
        QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
                                             tr("Widget title:"), QLineEdit::Normal,
                                             parent->windowTitle(), &ok);
293
        if (ok && !text.isEmpty()) {
lm's avatar
lm committed
294 295 296 297 298 299 300 301 302 303
            QSettings settings;
            settings.beginGroup(parent->windowTitle());
            settings.remove("");
            settings.endGroup();
            parent->setWindowTitle(text);

            storeWidgetsToSettings();
            emit titleChanged(text);
            if (mainMenuAction) mainMenuAction->setText(text);
        }
304 305
    }
}
lm's avatar
lm committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

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