OptionalEdit.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include "OptionalEdit.h"
25
26#include <QAbstractItemView>
27#include <QApplication>
28#include <QFocusEvent>
29#include <QSettings>
30
33
35
36using namespace armarx;
37
38OptionalEdit::OptionalEdit(const QString& elementName,
39 const QString& propertyName,
40 QWidget* parent) :
41 QWidget(parent), elementName(elementName), propertyName(propertyName)
42{
43 layout = new QHBoxLayout;
44 layout->setMargin(0);
45 layout->setSpacing(0);
46
47 const std::string file =
48 armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
49 QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
50 valueList = settings.value(elementName + "/" + propertyName).toStringList();
51
52
53 combo = new CustomComboBox(nullptr);
54 combo->setEditable(true);
55 combo->setAutoCompletion(true);
56 combo->setAutoCompletionCaseSensitivity(Qt::CaseInsensitive);
57 combo->addItems(valueList);
58
59 readOnlyLineEdit = new QLineEdit();
60 readOnlyLineEdit->setReadOnly(true);
61 readOnlyLineEdit->setVisible(false);
62
63 checkbox = new QCheckBox(nullptr);
64 checkbox->setToolTip("If checked, this property will be written into the config file");
65 checkbox->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred));
66
67 setFocusPolicy(Qt::StrongFocus);
68 setAttribute(Qt::WA_InputMethodEnabled);
69
70 // connect(edit, SIGNAL(textChanged(QString)), this, SIGNAL(valueChanged(QString)));
71 connect(combo, SIGNAL(valueChanged(QString)), this, SIGNAL(valueChanged(QString)));
72 connect(combo, SIGNAL(currentTextChanged(QString)), this, SLOT(setPropertyEnabled()));
73
74 connect(combo, SIGNAL(valueChanged(QString)), this, SLOT(updateHistory(QString)));
75 connect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(slotEnabledChanged(int)));
76
77
78 layout->addWidget(checkbox);
79 layout->addWidget(combo);
80 layout->addWidget(readOnlyLineEdit);
81
82 this->setLayout(layout);
83}
84
85void
86OptionalEdit::setPossibleValues(const QStringList& values)
87{
88 if (!values.empty())
89 {
90 fixComboboxValues = true;
91 // combo->setEditable(false);
92 combo->clear();
93 combo->addItems(values);
94 }
95}
96
97void
99{
100 // ARMARX_WARNING << "Property is enabled: " << enabled;
101 auto changed = enabled != checkbox->isChecked();
102 checkbox->setChecked(enabled);
103 if (changed)
104 {
105 emit enabledChanged(checkbox->isChecked());
106 }
107}
108
109void
111{
112 checkbox->setDisabled(readOnly);
113
114 combo->setVisible(not readOnly);
115 readOnlyLineEdit->setVisible(readOnly);
116}
117
118void
119OptionalEdit::setValue(const QString& value)
120{
121 auto changed = value != combo->currentText();
122 bool enabledState = checkbox->isChecked();
123 // ARMARX_INFO << VAROUT(value.toStdString()) << VAROUT(enabledState);
124 combo->setCurrentText(value);
125 readOnlyLineEdit->setText(value);
126
127 if (checkbox->isChecked() != enabledState)
128 {
129 setPropertyEnabled(enabledState);
130 }
131 if (changed)
132 {
133 emit valueChanged(combo->currentText());
134 }
135}
136
137void
138OptionalEdit::updateHistory(const QString& value)
139{
140 valueList.push_front(combo->currentText());
141 valueList.removeDuplicates();
142 if (!fixComboboxValues)
143 // ARMARX_INFO << "Adding " << combo->currentText().toStdString() << " to history";
144 {
145 combo->clear();
146 combo->addItems(valueList);
147 }
148
149
150 const std::string file =
151 armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
152 QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
153 settings.setValue(elementName + "/" + propertyName, QVariant(valueList));
154}
155
156void
157OptionalEdit::slotEnabledChanged(int value)
158{
159 emit enabledChanged(value != 0);
160}
161
162void
164{
165 QWidget::focusInEvent(e);
166}
167
168void
170{
171 QWidget::focusOutEvent(e);
172}
173
174void
176{
177}
178
179void
181{
182}
183
184void
186{
187 // ARMARX_INFO << "focus out - " << int(event->reason()) << " line focus: " << (lineEdit()->hasFocus() ? "true" : "false");
188
189 if (event->reason() != Qt::FocusReason::PopupFocusReason)
190 {
191 // ARMARX_INFO << "focus out - current text: " << currentText().toStdString() << " " << VAROUT(oldValue.toStdString());
192 if (oldValue != currentText())
193 {
194 emit valueChanged(currentText());
195 }
196 }
197 QComboBox::focusOutEvent(event);
198}
static std::string GetDefaultUserConfigPath()
The user config directory of ArmarX.
void focusOutEvent(QFocusEvent *event) override
void valueChanged(const QString &value)
OptionalEdit(const QString &elementName, const QString &propertyName, QWidget *parent=0)
void focusInEvent(QFocusEvent *e) override
void keyReleaseEvent(QKeyEvent *e) override
void setPossibleValues(const QStringList &values)
void keyPressEvent(QKeyEvent *e) override
void enabledChanged(const bool &enabled)
void focusOutEvent(QFocusEvent *e) override
void valueChanged(const QString &value)
void updateHistory(const QString &value)
void setValue(const QString &value)
void setPropertyEnabled(const bool &enabled=true)
void setReadOnly(bool readOnly)
This file offers overloads of toIce() and fromIce() functions for STL container types.