VPTissue Reference Manual
StepSelection.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 "StepSelection.h"
21 
22 #include <algorithm>
23 #include <iterator>
24 #include <QString>
25 
26 namespace SimPT_Shell {
27 
28 // Regex: start[-stop[:step]] separated by , or ; (number not equal to 0)
29 const QRegExp StepSelection::g_range_regex("(\\d+)(?:\\s*-\\s*(\\d+)(?:\\s*:\\s*(\\d*[1-9]\\d*))?)?");
30 const QRegExp StepSelection::g_repeated_regex("\\s*(?:\\s*" + StepSelection::g_range_regex.pattern() + "\\s*(?:[,;](?!$)|$))*");
31 
32 StepSelection::StepSelection() : m_all_selected(true)
33 {
34 }
35 
37 {
38 }
39 
40 void StepSelection::SetSelectionText(const std::string &ranges)
41 {
42  QRegExp regex(g_range_regex);
43  m_selected_steps.clear();
44 
45  QString rangesString = QString::fromStdString(ranges);
46 
47  if (regex.indexIn(rangesString, 0) == -1) {
48  m_all_selected = true;
49  }
50  else {
51  m_all_selected = false;
52 
53  int pos = 0;
54  while ((pos = regex.indexIn(rangesString, pos)) != -1) {
55  int start = regex.cap(1).toInt();
56 
57  if (!regex.cap(2).isEmpty()) {
58  int stop = regex.cap(2).toInt();
59  int step = 1;
60  if (!regex.cap(3).isEmpty()) {
61  step = regex.cap(3).toInt();
62  }
63 
64  for (int i = start; i <= stop; i += step) {
65  m_selected_steps.insert(i);
66  }
67  } else {
68  m_selected_steps.insert(start);
69  }
70 
71  pos += regex.matchedLength();
72  }
73  }
74 }
75 
76 std::set<int> StepSelection::ApplySelection(const std::set<int> &available) const
77 {
78  if (m_all_selected) {
79  return available;
80  } else {
81  std::set<int> intersected;
82  std::set_intersection(available.begin(), available.end(), m_selected_steps.begin(), m_selected_steps.end(), std::inserter(intersected, intersected.begin()));
83  return intersected;
84  }
85 }
86 
87 bool StepSelection::Contains(int step) const
88 {
89  return m_all_selected || m_selected_steps.find(step) != m_selected_steps.end();
90 }
91 
92 } // namespace
bool Contains(int step) const
Checks whether the selection contains a specified step.
Namespace for SimPT shell package.
Definition: Client.cpp:50
static const QRegExp g_range_regex
Regex for one range (start[-stop[:step]])
Definition: StepSelection.h:53
Interface for StepSelection.
void SetSelectionText(const std::string &ranges)
Sets the selected ranges of steps.
std::set< int > ApplySelection(const std::set< int > &available) const
Applies the selection to a set of available steps.
virtual ~StepSelection()
Destructor.
StepSelection()
Constructs a selection with an empty ranges text (ie. everything is selected)
static const QRegExp g_repeated_regex
Regex for a series of repeated ranges, separated by , of ;.
Definition: StepSelection.h:54