QGCToolWidget.cc 10.4 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
    menu.exec(event->globalPos());
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
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);
}

195 196 197 198 199 200
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
201 202 203
    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()));
204 205 206 207

    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
208 209 210 211

    deleteAction = new QAction(tr("Delete this widget"), this);
    deleteAction->setStatusTip(tr("Delete this widget permanently"));
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteWidget()));
212 213 214 215 216 217 218 219

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

    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
224 225 226 227 228 229 230 231 232 233 234 235 236 237
}

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;
238 239 240 241 242
}

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

void QGCToolWidget::addAction()
{
    QGCActionButton* button = new QGCActionButton(this);
254
    if (ui->hintLabel) {
lm's avatar
lm committed
255
        ui->hintLabel->deleteLater();
256
        ui->hintLabel = NULL;
lm's avatar
lm committed
257
    }
258 259 260 261
    toolLayout->addWidget(button);
    button->startEditMode();
}

lm's avatar
lm committed
262 263 264
void QGCToolWidget::addCommand()
{
    QGCCommandButton* button = new QGCCommandButton(this);
265
    if (ui->hintLabel) {
lm's avatar
lm committed
266 267 268 269 270 271 272
        ui->hintLabel->deleteLater();
        ui->hintLabel = NULL;
    }
    toolLayout->addWidget(button);
    button->startEditMode();
}

lm's avatar
lm committed
273 274
void QGCToolWidget::addToolWidget(QGCToolWidgetItem* widget)
{
275
    if (ui->hintLabel) {
lm's avatar
lm committed
276
        ui->hintLabel->deleteLater();
277
        ui->hintLabel = NULL;
lm's avatar
lm committed
278
    }
lm's avatar
lm committed
279 280 281
    toolLayout->addWidget(widget);
}

282 283 284 285 286 287 288 289 290 291
void QGCToolWidget::exportWidget()
{

}

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

}

lm's avatar
lm committed
292 293 294
const QString QGCToolWidget::getTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
295
    if (parent) {
lm's avatar
lm committed
296
        return parent->windowTitle();
297
    } else {
lm's avatar
lm committed
298 299 300 301 302
        return this->windowTitle();
    }
}


303 304 305
void QGCToolWidget::setTitle()
{
    QDockWidget* parent = dynamic_cast<QDockWidget*>(this->parentWidget());
306
    if (parent) {
lm's avatar
lm committed
307
        bool ok;
308 309 310
        QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
                                             tr("Widget title:"), QLineEdit::Normal,
                                             parent->windowTitle(), &ok);
311
        if (ok && !text.isEmpty()) {
lm's avatar
lm committed
312 313 314 315 316 317 318 319 320 321
            QSettings settings;
            settings.beginGroup(parent->windowTitle());
            settings.remove("");
            settings.endGroup();
            parent->setWindowTitle(text);

            storeWidgetsToSettings();
            emit titleChanged(text);
            if (mainMenuAction) mainMenuAction->setText(text);
        }
322 323
    }
}
lm's avatar
lm committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

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