22 #include "../../cpp_simshell/common/InstallDirs.h"
27 #include <QHBoxLayout>
30 #include <QPushButton>
40 SelectByIDWidget::SelectByIDWidget(
QWidget* parent)
41 :
QWidget(parent), m_line_edit(new QLineEdit()), m_lock(new QPushButton()), m_max_id(0)
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);
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()));
56 SelectByIDWidget::~SelectByIDWidget() {}
58 void SelectByIDWidget::SetMaxID(
unsigned int maxID)
61 m_line_edit->setText(
"");
64 void SelectByIDWidget::ProcessInput() {
65 if (m_regex.exactMatch(m_line_edit->text()))
69 SetTextBoxStyle(
true);
73 void SelectByIDWidget::ParseText()
75 std::vector<bool> selected(m_max_id + 1,
false);
77 vector<string> tokens =
Tokenize(m_line_edit->text().toStdString(),
",");
78 for (
const std::string& token : tokens) {
79 vector<string> range =
Tokenize(token,
"-:");
84 if (range.size() == 1) {
85 from = atoi(range[0].c_str());
88 }
else if (range.size() == 2) {
89 from = atoi(range[0].c_str());
90 to = atoi(range[1].c_str());
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());
97 assert(
"This is not a proper format for selection by ID.");
99 assert(from >= 0 && to >= 0 && step > 0 &&
"The given bounds for the range are not valid.");
101 for (
int id = from;
id <= std::min(m_max_id, to);
id =
id + step) {
106 std::list<unsigned int> selected_ids;
107 for (
unsigned int id = 0;
id < selected.size(); ++id) {
109 selected_ids.push_back(
id);
113 emit IDsSelected(selected_ids, m_lock->isChecked());
116 void SelectByIDWidget::SetTextBoxStyle(
bool error)
119 m_line_edit->setStyleSheet(
"QLineEdit{background: #FFCCCC; color: #FF0000;}");
121 m_line_edit->setStyleSheet(
"QLineEdit{background: #FFFFFF; color: #000000;}");
125 void SelectByIDWidget::SetupGui()
127 QLayout* layout =
new QHBoxLayout();
130 m_lock->setIcon(lockIcon);
131 m_lock->setToolTip(
"Keep/Discard currently selected items");
133 layout->addWidget(
new QLabel(
"Search by ID:"));
134 layout->addWidget(m_line_edit);
135 layout->addWidget(m_lock);
static std::string GetDataDir()
Utility method: get name of the directory for data files.
Namespace for SimPT tissue editor package.
Namespace for miscellaneous utilities.
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.
Interface for SelectByIDWidget.
String manipulation utilities.
see the online Qt documentation