Parallel Coordinate System with Time Series Data
Multilist.h
Go to the documentation of this file.
1 #pragma once
12 #ifndef MULTILIST
13 #define MULTILIST
14 
15 #include <QtWidgets>
16 
17 class MultiList : public QComboBox {
18  Q_OBJECT
19 
20 private:
21  QString displayText;
22  QStandardItemModel* model;
23 
24 public:
25  MultiList(QWidget* parent = 0) : QComboBox(parent) {
26  setEditable(true);
27 
28  displayText = "";
29  model = new QStandardItemModel;
31 
32  connect(model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotUpdate()));
33  }
34 
35  void addItem(const QString& text) {
36  int row = model->rowCount();
37  QStandardItem* item = new QStandardItem();
38  item->setText(text);
39  item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
40  item->setData(Qt::Unchecked, Qt::CheckStateRole);
41  model->setItem(row, 0, item);
42  this->setModel(model);
43  }
44 
45  void addItems(const QStringList& texts) {
46  for (int i = 0; i < texts.count(); i++) {
47  addItem(texts.at(i));
48  }
49  }
50 
51  QStringList getCheckedItems() const {
52  QStringList checkedItems;
53 
54  for (int i = 0; i < model->rowCount(); i++) {
55  if (model->item(i, 0)->checkState() == Qt::Checked) {
56  checkedItems << model->item(i, 0)->text();
57  }
58  }
59 
60  return checkedItems;
61  }
62 
63  void setCheckedItems(const QStringList& items) {
64  for (int i = 0; i < items.count(); i++) {
65  int index = findText(items.at(i));
66 
67  if (index != -1) {
68  model->item(index)->setData(Qt::Checked, Qt::CheckStateRole);
69  }
70  }
71 
72  slotUpdate();
73  }
74 
75 public slots:
76  void slotUpdateText() {
77  lineEdit()->setText(displayText);
78  }
79 
80  void slotUpdate() {
81  displayText = "";
82 
83  for (int i = 0; i < model->rowCount(); i++) {
84  if (model->item(i, 0)->checkState() == Qt::Checked) {
85  displayText = displayText + model->item(i, 0)->text() + "; ";
86  }
87  }
88 
89  QTimer::singleShot(0, this, SLOT(slotUpdateText()));
90  }
91 };
92 
93 
94 #endif
Definition: Multilist.h:17
void addItems(const QStringList &texts)
Definition: Multilist.h:45
void addItem(const QString &text)
Definition: Multilist.h:35
QStringList getCheckedItems() const
Definition: Multilist.h:51
void slotUpdateText()
Definition: Multilist.h:76
MultiList(QWidget *parent=0)
Definition: Multilist.h:25
void setCheckedItems(const QStringList &items)
Definition: Multilist.h:63
void slotUpdate()
Definition: Multilist.h:80