/* * Copyright (C) OpenTX * * Based on code named * th9x - http://code.google.com/p/th9x * er9x - http://code.google.com/p/er9x * gruvin9x - http://code.google.com/p/gruvin9x * * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #pragma once #include #include "genericpanel.h" #include "rawsource.h" #include "rawswitch.h" class AutoComboBox: public QComboBox { Q_OBJECT public: explicit AutoComboBox(QWidget * parent = nullptr): QComboBox(parent), field(nullptr), panel(nullptr), next(0), lock(false), hasModel(false), rawSource(nullptr), rawSwitch(nullptr) { connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int))); } void clear() { if (!hasModel) { lock = true; QComboBox::clear(); next = 0; lock = false; } } virtual void insertItems(int index, const QStringList & items) { if (!hasModel) { foreach(QString item, items) { addItem(item); } } } virtual void addItem(const QString & item) { if (!hasModel) addItem(item, next++); } virtual void addItem(const QString & item, int value) { if (!hasModel) { lock = true; QComboBox::addItem(item, value); lock = false; updateValue(); } } void setField(unsigned int & field, GenericPanel * panel = nullptr) { this->field = (int *)&field; this->rawSource = nullptr; this->rawSwitch = nullptr; this->panel = panel; updateValue(); } void setField(int & field, GenericPanel * panel = nullptr) { this->field = &field; this->rawSource = nullptr; this->rawSwitch = nullptr; this->panel = panel; updateValue(); } void setField(RawSource & field, GenericPanel * panel = nullptr) { this->rawSource = &field; this->rawSwitch = nullptr; this->field = nullptr; this->panel = panel; updateValue(); } void setField(RawSwitch & field, GenericPanel * panel = nullptr) { this->rawSwitch = &field; this->rawSource = nullptr; this->field = nullptr; this->panel = panel; updateValue(); } void setModel(QAbstractItemModel * model) { lock = true; QComboBox::setModel(model); lock = false; hasModel = true; updateValue(); } void setAutoIndexes() { if (!hasModel) { for (int i = 0; i < count(); ++i) setItemData(i, i); updateValue(); } } void updateValue() { if (!field && !rawSource && !rawSwitch) return; lock = true; if (field) setCurrentIndex(findData(*field)); else if (rawSource) setCurrentIndex(findData(rawSource->toValue())); else if (rawSwitch) setCurrentIndex(findData(rawSwitch->toValue())); lock = false; } signals: void currentDataChanged(int value); protected slots: void onCurrentIndexChanged(int index) { if (panel && panel->lock) return; if (lock || index < 0) return; bool ok; const int val = itemData(index).toInt(&ok); if (!ok) return; if (field && *field != val) { *field = val; } else if (rawSource && rawSource->toValue() != val) { *rawSource = RawSource(val); } else if (rawSwitch && rawSwitch->toValue() != val) { *rawSwitch = RawSwitch(val); } else return; emit currentDataChanged(val); if (panel) emit panel->modified(); } protected: int *field = nullptr; GenericPanel *panel = nullptr; int next = 0; bool lock = false; bool hasModel = false; RawSource *rawSource = nullptr; RawSwitch *rawSwitch = nullptr; };