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, const QString& propertyName, QWidget* parent) :
39  QWidget(parent),
40  elementName(elementName),
41  propertyName(propertyName)
42 {
43  layout = new QHBoxLayout;
44  layout->setMargin(0);
45  layout->setSpacing(0);
46 
47  const std::string file = armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
48  QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
49  valueList = settings.value(elementName + "/" + propertyName).toStringList();
50 
51 
52 
53 
54  combo = new CustomComboBox(nullptr);
55  combo->setEditable(true);
56  combo->setAutoCompletion(true);
57  combo->setAutoCompletionCaseSensitivity(Qt::CaseInsensitive);
58  combo->addItems(valueList);
59 
60  readOnlyLineEdit = new QLineEdit();
61  readOnlyLineEdit->setReadOnly(true);
62  readOnlyLineEdit->setVisible(false);
63 
64  checkbox = new QCheckBox(nullptr);
65  checkbox->setToolTip("If checked, this property will be written into the config file");
66  checkbox->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred));
67 
68  setFocusPolicy(Qt::StrongFocus);
69  setAttribute(Qt::WA_InputMethodEnabled);
70 
71  // connect(edit, SIGNAL(textChanged(QString)), this, SIGNAL(valueChanged(QString)));
72  connect(combo, SIGNAL(valueChanged(QString)), this, SIGNAL(valueChanged(QString)));
73  connect(combo, SIGNAL(currentTextChanged(QString)), this, SLOT(setPropertyEnabled()));
74 
75  connect(combo, SIGNAL(valueChanged(QString)), this, SLOT(updateHistory(QString)));
76  connect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(slotEnabledChanged(int)));
77 
78 
79  layout->addWidget(checkbox);
80  layout->addWidget(combo);
81  layout->addWidget(readOnlyLineEdit);
82 
83  this->setLayout(layout);
84 }
85 
86 void OptionalEdit::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 
97 
98 
100 {
101  // ARMARX_WARNING << "Property is enabled: " << enabled;
102  auto changed = enabled != checkbox->isChecked();
103  checkbox->setChecked(enabled);
104  if (changed)
105  {
106  emit enabledChanged(checkbox->isChecked());
107  }
108 }
109 
110 void OptionalEdit::setReadOnly(bool readOnly)
111 {
112  checkbox->setDisabled(readOnly);
113 
114  combo->setVisible(not readOnly);
115  readOnlyLineEdit->setVisible(readOnly);
116 }
117 
118 
119 void OptionalEdit::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 
137 void OptionalEdit::updateHistory(const QString& value)
138 {
139  valueList.push_front(combo->currentText());
140  valueList.removeDuplicates();
141  if (!fixComboboxValues)
142  // ARMARX_INFO << "Adding " << combo->currentText().toStdString() << " to history";
143  {
144  combo->clear();
145  combo->addItems(valueList);
146  }
147 
148 
149  const std::string file = armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
150  QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
151  settings.setValue(elementName + "/" + propertyName, QVariant(valueList));
152 }
153 
154 
155 void OptionalEdit::slotEnabledChanged(int value)
156 {
157  emit enabledChanged(value != 0);
158 }
159 
160 void OptionalEdit::focusInEvent(QFocusEvent* e)
161 {
162  QWidget::focusInEvent(e);
163 }
164 
165 void OptionalEdit::focusOutEvent(QFocusEvent* e)
166 {
167  QWidget::focusOutEvent(e);
168 }
169 
170 void OptionalEdit::keyPressEvent(QKeyEvent* e)
171 {
172 
173 }
174 
176 {
177 
178 }
179 
180 
181 void CustomComboBox::focusOutEvent(QFocusEvent* event)
182 {
183  // ARMARX_INFO << "focus out - " << int(event->reason()) << " line focus: " << (lineEdit()->hasFocus() ? "true" : "false");
184 
185  if (event->reason() != Qt::FocusReason::PopupFocusReason)
186  {
187  // ARMARX_INFO << "focus out - current text: " << currentText().toStdString() << " " << VAROUT(oldValue.toStdString());
188  if (oldValue != currentText())
189  {
190  emit valueChanged(currentText());
191  }
192  }
193  QComboBox::focusOutEvent(event);
194 }
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:170
OptionalEdit.h
armarx::OptionalEdit::focusOutEvent
void focusOutEvent(QFocusEvent *e) override
Definition: OptionalEdit.cpp:165
armarx::OptionalEdit::focusInEvent
void focusInEvent(QFocusEvent *e) override
Definition: OptionalEdit.cpp:160
armarx::OptionalEdit::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *e) override
Definition: OptionalEdit.cpp:175
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::CustomComboBox::focusOutEvent
void focusOutEvent(QFocusEvent *event) override
Definition: OptionalEdit.cpp:181
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:782
armarx::OptionalEdit::setPossibleValues
void setPossibleValues(const QStringList &values)
Definition: OptionalEdit.cpp:86
armarx::OptionalEdit::updateHistory
void updateHistory(const QString &value)
Definition: OptionalEdit.cpp:137
armarx::OptionalEdit::setReadOnly
void setReadOnly(bool readOnly)
Definition: OptionalEdit.cpp:110
armarx::CustomComboBox::oldValue
QString oldValue
Definition: OptionalEdit.h:54
armarx::OptionalEdit::setPropertyEnabled
void setPropertyEnabled(const bool &enabled=true)
Definition: OptionalEdit.cpp:99
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:28
armarx::CustomComboBox
Definition: OptionalEdit.h:36