CANdevStudio
Development tool for CAN bus simulation
Loading...
Searching...
No Matches
propertyfields.h
Go to the documentation of this file.
1#ifndef __PROPERTYFIELDS_H
2#define __PROPERTYFIELDS_H
3
4#include <QColorDialog>
5#include <QComboBox>
6#include <QFileDialog>
7#include <QHBoxLayout>
8#include <QIntValidator>
9#include <QLineEdit>
10#include <QPushButton>
11
12struct PropertyField : public QWidget {
13 virtual void setPropText(const QString& text) = 0;
14 virtual QString propText() = 0;
15};
16
18 PropertyFieldText(bool numberOnly = false)
19 : _numberOnly(numberOnly)
20 {
21 setLayout(new QHBoxLayout);
22 layout()->setContentsMargins(0, 0, 0, 0);
23 _le = new QLineEdit(this);
24 _le->setFrame(false);
25 layout()->addWidget(_le);
26
27 if (_numberOnly) {
28 _le->setValidator(new QIntValidator(_le));
29 }
30 }
31
32 void setPropText(const QString& text) override
33 {
34 _le->setText(text);
35 }
36
37 QString propText() override
38 {
39 return _le->text();
40 }
41
42protected:
43 QLineEdit* _le;
44
45private:
46 const bool _numberOnly;
47};
48
50 PropertyFieldPath(bool folderOnly = false)
51 : _folderOnly(folderOnly)
52 {
53 _pb = new QPushButton(this);
54 _pb->setText("...");
55 _pb->setFixedSize(24, 24);
56 _pb->setFlat(true);
57 _pb->setProperty("type", "PropertyFieldPath");
58 layout()->addWidget(_pb);
59
60 connect(_pb, &QPushButton::pressed, [&] {
61 QString path;
62 QString currPath = QFileInfo(_le->text()).path();
63
64 if (currPath.length() == 0) {
65 currPath = QDir::homePath();
66 }
67
68 if (_folderOnly) {
69 path = QFileDialog::getExistingDirectory(
70 this, "Select directory", currPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
71 } else {
72 path = QFileDialog::getOpenFileName(this, "Select file", currPath);
73 }
74
75 if (path.length() > 0) {
76 _le->setText(path);
77 }
78 });
79 }
80
81private:
82 QPushButton* _pb;
83 const bool _folderOnly;
84};
85
87 Q_OBJECT
88
89public:
90 PropertyFieldCombo(bool editable = true)
91 {
92 setLayout(new QHBoxLayout);
93 layout()->setContentsMargins(0, 0, 0, 0);
94 _cb = new QComboBox(this);
95 _cb->setEditable(editable);
96 _cb->setFrame(false);
97 layout()->addWidget(_cb);
98 connect(_cb, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
99 [this](int) { emit currentTextChanged(_cb->currentText()); });
100 }
101
102 void setPropText(const QString& text) override
103 {
104 _cb->setCurrentText(text);
105 }
106
107 QString propText() override
108 {
109 QVariant data = _cb->currentData();
110
111 if (data.isValid()) {
112 return data.toString();
113 } else {
114 return _cb->currentText();
115 }
116 }
117
118 void addItems(const QStringList& list)
119 {
120 _cb->clear();
121 _cb->addItems(list);
122 }
123
124 void addItem(const QString& text, const QVariant v = QVariant())
125 {
126 _cb->addItem(text, v);
127 }
128
129 void sort()
130 {
131 _cb->model()->sort(0);
132 }
133
134signals:
135 void currentTextChanged(const QString& text);
136
137private:
138 QComboBox* _cb;
139};
140
143 {
144 _pb = new QPushButton(this);
145 _pb->setText("...");
146 _pb->setFixedSize(24, 24);
147 _pb->setFlat(true);
148 _pb->setProperty("type", "PropertyFieldPath");
149 layout()->addWidget(_pb);
150
151 _cd = new QColorDialog(this);
152
153 connect(_pb, &QPushButton::pressed, [&] {
154 _cd->setCurrentColor(QColor(_le->text()));
155 if (_cd->exec() == QDialog::Accepted) {
156 _le->setText(_cd->selectedColor().name(QColor::HexRgb).toUpper());
157 }
158 });
159 }
160
161private:
162 QPushButton* _pb;
163 QColorDialog* _cd;
164};
165#endif /* !__PROPERTYFIELDS_H */
Definition propertyfields.h:86
void addItems(const QStringList &list)
Definition propertyfields.h:118
void setPropText(const QString &text) override
Definition propertyfields.h:102
void sort()
Definition propertyfields.h:129
QString propText() override
Definition propertyfields.h:107
void addItem(const QString &text, const QVariant v=QVariant())
Definition propertyfields.h:124
PropertyFieldCombo(bool editable=true)
Definition propertyfields.h:90
void currentTextChanged(const QString &text)
Definition propertyfields.h:141
PropertyFieldColor()
Definition propertyfields.h:142
Definition propertyfields.h:12
virtual QString propText()=0
virtual void setPropText(const QString &text)=0
Definition propertyfields.h:49
PropertyFieldPath(bool folderOnly=false)
Definition propertyfields.h:50
Definition propertyfields.h:17
QString propText() override
Definition propertyfields.h:37
PropertyFieldText(bool numberOnly=false)
Definition propertyfields.h:18
void setPropText(const QString &text) override
Definition propertyfields.h:32
QLineEdit * _le
Definition propertyfields.h:43