[companion] Adjust GVAR not possible in global functions (fix #5425)
[opentx.git] / companion / src / shared / autocombobox.h
blobc7aea30bf09e538d581f425f521ee9221357162d
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef _AUTOCOMBOBOX_H_
22 #define _AUTOCOMBOBOX_H_
24 #include <QComboBox>
25 #include "genericpanel.h"
27 class AutoComboBox: public QComboBox
29 Q_OBJECT
31 public:
32 explicit AutoComboBox(QWidget *parent = 0):
33 QComboBox(parent),
34 field(NULL),
35 panel(NULL),
36 next(0),
37 lock(false)
39 connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
42 void clear()
44 lock = true;
45 QComboBox::clear();
46 next = 0;
47 lock = false;
50 virtual void insertItems(int index, const QStringList & items)
52 foreach(QString item, items) {
53 addItem(item);
57 virtual void addItem(const QString & item)
59 addItem(item, next++);
62 virtual void addItem(const QString & item, int value)
64 lock = true;
65 QComboBox::addItem(item, value);
66 if (field && *field==value) {
67 setCurrentIndex(count()-1);
69 lock = false;
72 void setField(unsigned int & field, GenericPanel * panel=NULL)
74 this->field = (int *)&field;
75 this->panel = panel;
76 for (int i=0; i<count(); ++i) {
77 if ((int)field == itemData(i))
78 setCurrentIndex(i);
82 void setField(int & field, GenericPanel * panel=NULL)
84 this->field = &field;
85 this->panel = panel;
86 for (int i=0; i<count(); ++i) {
87 if ((int)field == itemData(i))
88 setCurrentIndex(i);
92 void setAutoIndexes()
94 for (int i=0; i<count(); ++i) {
95 setItemData(i, i);
96 if (*this->field == i)
97 setCurrentIndex(i);
101 protected slots:
102 void onCurrentIndexChanged(int index)
104 if (field && !lock) {
105 *field = itemData(index).toInt();
106 if (panel) {
107 emit panel->modified();
112 protected:
113 int * field;
114 GenericPanel * panel;
115 int next;
116 bool lock;
119 #endif // _AUTOCOMBOBOX_H_