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 
36 using namespace armarx;
37 
38 OptionalEdit::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 
85 void
87 {
88  if (!values.empty())
89  {
90  fixComboboxValues = true;
91  // combo->setEditable(false);
92  combo->clear();
93  combo->addItems(values);
94  }
95 }
96 
97 void
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 
109 void
111 {
112  checkbox->setDisabled(readOnly);
113 
114  combo->setVisible(not readOnly);
115  readOnlyLineEdit->setVisible(readOnly);
116 }
117 
118 void
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 
137 void
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 
156 void
157 OptionalEdit::slotEnabledChanged(int value)
158 {
159  emit enabledChanged(value != 0);
160 }
161 
162 void
164 {
165  QWidget::focusInEvent(e);
166 }
167 
168 void
170 {
171  QWidget::focusOutEvent(e);
172 }
173 
174 void
176 {
177 }
178 
179 void
181 {
182 }
183 
184 void
185 CustomComboBox::focusOutEvent(QFocusEvent* event)
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 }
armarx::OptionalEdit::enabledChanged
void enabledChanged(const bool &enabled)
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::OptionalEdit::keyPressEvent
void keyPressEvent(QKeyEvent *e) override
Definition: OptionalEdit.cpp:175
OptionalEdit.h
armarx::OptionalEdit::focusOutEvent
void focusOutEvent(QFocusEvent *e) override
Definition: OptionalEdit.cpp:169
armarx::OptionalEdit::focusInEvent
void focusInEvent(QFocusEvent *e) override
Definition: OptionalEdit.cpp:163
armarx::OptionalEdit::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *e) override
Definition: OptionalEdit.cpp:180
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::CustomComboBox::focusOutEvent
void focusOutEvent(QFocusEvent *event) override
Definition: OptionalEdit.cpp:185
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
armarx::OptionalEdit::setValue
void setValue(const QString &value)
Definition: OptionalEdit.cpp:119
InfixCompleter.h
armarx::OptionalEdit::OptionalEdit
OptionalEdit(const QString &elementName, const QString &propertyName, QWidget *parent=0)
Definition: OptionalEdit.cpp:38
armarx::ArmarXDataPath::GetDefaultUserConfigPath
static std::string GetDefaultUserConfigPath()
The user config directory of ArmarX.
Definition: ArmarXDataPath.cpp:777
armarx::OptionalEdit::setPossibleValues
void setPossibleValues(const QStringList &values)
Definition: OptionalEdit.cpp:86
armarx::OptionalEdit::updateHistory
void updateHistory(const QString &value)
Definition: OptionalEdit.cpp:138
armarx::OptionalEdit::setReadOnly
void setReadOnly(bool readOnly)
Definition: OptionalEdit.cpp:110
armarx::CustomComboBox::oldValue
QString oldValue
Definition: OptionalEdit.h:56
armarx::OptionalEdit::setPropertyEnabled
void setPropertyEnabled(const bool &enabled=true)
Definition: OptionalEdit.cpp:98
armarx::OptionalEdit::valueChanged
void valueChanged(const QString &value)
Logging.h
armarx::CustomComboBox::valueChanged
void valueChanged(const QString &value)
ArmarXDataPath.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::CustomComboBox
Definition: OptionalEdit.h:36