VPTissue Reference Manual
SelectByIDWidget.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "SelectByIDWidget.h"
21 
22 #include "../../cpp_simshell/common/InstallDirs.h"
23 #include "util/misc/StringUtils.h"
24 
25 #include <cassert>
26 #include <list>
27 #include <QHBoxLayout>
28 #include <QLabel>
29 #include <QLineEdit>
30 #include <QPushButton>
31 #include <QRegExp>
32 #include <string>
33 #include <vector>
34 
35 namespace SimPT_Editor {
36 
37 using namespace std;
38 using namespace SimPT_Sim::Util;
39 
40 SelectByIDWidget::SelectByIDWidget(QWidget* parent)
41  : QWidget(parent), m_line_edit(new QLineEdit()), m_lock(new QPushButton()), m_max_id(0)
42 {
43  QString strict_pos_int_regex = "[1-9]+[0-9]*";
44  QString pos_int_regex = "(0|" + strict_pos_int_regex + ")";
45  QString range_regex = pos_int_regex + "(-" + pos_int_regex + "(:" + strict_pos_int_regex + ")?)?";
46  QString comb_regex = "(" + range_regex + "(," + range_regex + ")*)?";
47  m_regex = QRegExp(comb_regex);
48 
49  m_lock->setCheckable(true);
50  connect(m_line_edit, SIGNAL(returnPressed()), this, SLOT(ProcessInput()));
51  connect(m_line_edit, SIGNAL(textChanged(const QString&)), this, SLOT(SetTextBoxStyle()));
52 
53  SetupGui();
54 }
55 
56 SelectByIDWidget::~SelectByIDWidget() {}
57 
58 void SelectByIDWidget::SetMaxID(unsigned int maxID)
59 {
60  m_max_id = maxID;
61  m_line_edit->setText("");
62 }
63 
64 void SelectByIDWidget::ProcessInput() {
65  if (m_regex.exactMatch(m_line_edit->text()))
66  {
67  ParseText();
68  } else {
69  SetTextBoxStyle(true);
70  }
71 }
72 
73 void SelectByIDWidget::ParseText()
74 {
75  std::vector<bool> selected(m_max_id + 1, false);
76 
77  vector<string> tokens = Tokenize(m_line_edit->text().toStdString(), ",");
78  for (const std::string& token : tokens) {
79  vector<string> range = Tokenize(token, "-:");
80 
81  int from = -1;
82  int to = -1;
83  int step = -1;
84  if (range.size() == 1) {
85  from = atoi(range[0].c_str());
86  to = from;
87  step = 1;
88  } else if (range.size() == 2) {
89  from = atoi(range[0].c_str());
90  to = atoi(range[1].c_str());
91  step = 1;
92  } else if (range.size() == 3) {
93  from = atoi(range[0].c_str());
94  to = atoi(range[1].c_str());
95  step = atoi(range[2].c_str());
96  } else {
97  assert("This is not a proper format for selection by ID.");
98  }
99  assert(from >= 0 && to >= 0 && step > 0 && "The given bounds for the range are not valid.");
100 
101  for (int id = from; id <= std::min(m_max_id, to); id = id + step) {
102  selected[id] = true;
103  }
104  }
105 
106  std::list<unsigned int> selected_ids;
107  for (unsigned int id = 0; id < selected.size(); ++id) {
108  if (selected[id]) {
109  selected_ids.push_back(id);
110  }
111  }
112 
113  emit IDsSelected(selected_ids, m_lock->isChecked());
114 }
115 
116 void SelectByIDWidget::SetTextBoxStyle(bool error)
117 {
118  if (error) {
119  m_line_edit->setStyleSheet("QLineEdit{background: #FFCCCC; color: #FF0000;}");
120  } else {
121  m_line_edit->setStyleSheet("QLineEdit{background: #FFFFFF; color: #000000;}");
122  }
123 }
124 
125 void SelectByIDWidget::SetupGui()
126 {
127  QLayout* layout = new QHBoxLayout();
128 
129  QIcon lockIcon(QString::fromStdString(SimShell::InstallDirs::GetDataDir() + "/icons/Tango/22x22/emblems/emblem-readonly.png"));
130  m_lock->setIcon(lockIcon);
131  m_lock->setToolTip("Keep/Discard currently selected items");
132 
133  layout->addWidget(new QLabel("Search by ID:"));
134  layout->addWidget(m_line_edit);
135  layout->addWidget(m_lock);
136 
137  setLayout(layout);
138 }
139 
140 } // namespace
static std::string GetDataDir()
Utility method: get name of the directory for data files.
STL namespace.
Namespace for SimPT tissue editor package.
Definition: Cell.h:32
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
static std::vector< std::string > Tokenize(const std::string &str, const std::string &delimiters)
Tokenize a string (in order of occurence) by splitting it on the given delimiters.
Definition: StringUtils.h:47
Interface for SelectByIDWidget.
String manipulation utilities.
see the online Qt documentation