VPTissue Reference Manual
ConversionList.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 "gui/ConversionList.h"
21 
24 
25 #include <QApplication>
26 #include <QBoxLayout>
27 #include <QCheckBox>
28 #include <QFileInfo>
29 #include <QGraphicsScene>
30 #include <QGraphicsView>
31 #include <QHeaderView>
32 #include <QLabel>
33 #include <QLineEdit>
34 #include <QList>
35 #include <QMessageBox>
36 #include <QRegExp>
37 #include <QRegExpValidator>
38 #include <QScrollBar>
39 #include <QStandardItemModel>
40 #include <QTableView>
41 
42 #include <cassert>
43 #include <set>
44 
45 using namespace std;
46 using namespace SimPT_Sim::Util;
47 
48 namespace SimPT_Shell {
49 
50 ConversionList::ConversionList(QWidget *parent)
51  : QWidget(parent),
52  m_model(new QStandardItemModel(this)),
53  m_filter_model(new StepFilterProxyModel(this))
54 {
55  connect(m_model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(UpdateSelectAllCheckState()));
56  m_filter_model->setSourceModel(m_model);
57  m_filter_model->setFilterKeyColumn(1);
58 
59  SetupGui();
60  Clear();
61 }
62 
64 {
65 }
66 
68 {
69  QList<QStandardItem*> row;
70 
71  auto timestep_item = new QStandardItem();
72  auto filename_item = new QStandardItem();
73 
74  timestep_item->setCheckable(true);
75  timestep_item->setData(entry.timestep, Qt::DisplayRole);
76  timestep_item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
77  timestep_item->setEditable(false);
78  row.append(timestep_item);
79 
80  filename_item->setData(QString::fromStdString(entry.files), Qt::DisplayRole);
81  filename_item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
82  filename_item->setEditable(false);
83  row.append(filename_item);
84 
85  m_model->appendRow(row);
86 }
87 
89 {
90  m_model->clear();
91  m_model->setHorizontalHeaderLabels(QStringList() << "Step" << "File(s)");
92  m_select_all_check_box->setChecked(false);
93 }
94 
95 void ConversionList::EmitSelectionChanged()
96 {
97  emit SelectionChanged(!m_table_view->selectionModel()->selection().isEmpty());
98 }
99 
100 vector<ConversionList::EntryType> ConversionList::GetCheckedEntries() const
101 {
102  vector<EntryType> result;
103  for (int i = 0; i < m_model->rowCount(); ++i) {
104  auto timestep_item = m_model->item(i, 0);
105  if (timestep_item->checkState() == Qt::Checked) {
106  auto filename_item = m_model->item(i, 1);
107  bool ok;
108  int timestep = timestep_item->data(Qt::DisplayRole).toInt(&ok);
109  assert(ok && "Couldn't get int from checked row");
110  string filename = filename_item->data(Qt::DisplayRole).toString().toStdString();
111  result.push_back(EntryType({timestep, filename}));
112  }
113  }
114  return result;
115 }
116 
117 bool ConversionList::HasCheckedEntries() const
118 {
119  return m_model->rowCount() > 0 && m_select_all_check_box->checkState() != Qt::Unchecked;
120 }
121 
122 void ConversionList::UpdateStepFilter()
123 {
124  if (m_step_filter->hasAcceptableInput())
125  m_filter_model->SetStepRanges(m_step_filter->text());
126 
127  ResizeColumns();
128  UpdateSelectAllCheckState();
129 }
130 
131 void ConversionList::UpdateFileFilter()
132 {
133  m_filter_model->setFilterWildcard(m_file_filter->text());
134 
135  ResizeColumns();
136  UpdateSelectAllCheckState();
137 }
138 
139 void ConversionList::UpdateSelectAllCheckState()
140 {
141  Qt::CheckState checkState = Qt::Unchecked;
142  if (m_filter_model->rowCount() > 0) {
143  QModelIndex index = m_filter_model->mapToSource(m_filter_model->index(0, 0));
144  checkState = m_model->itemFromIndex(index)->checkState();
145  for (int i = 1; i < m_filter_model->rowCount(); ++i) {
146  index = m_filter_model->mapToSource(m_filter_model->index(i, 0));
147  if (checkState != m_model->itemFromIndex(index)->checkState()) {
148  checkState = Qt::PartiallyChecked;
149  break;
150  }
151  }
152  }
153 
154  // Possibly set true on setCheckState(Qt::PartiallyChecked)CheckedSimStatesChanged();
155  m_select_all_check_box->setTristate(false);
156  m_select_all_check_box->setCheckState(checkState);
157  emit CheckedChanged();
158 }
159 
160 void ConversionList::SelectAll()
161 {
162  Qt::CheckState checkState = m_select_all_check_box->checkState();
163  for (int i = 0; i < m_filter_model->rowCount(); ++i) {
164  QModelIndex index = m_filter_model->mapToSource(m_filter_model->index(i, 0));
165  m_model->itemFromIndex(index)->setCheckState(checkState);
166  }
167  // Possibly set true on setCheckState(Qt::PartiallyChecked)CheckedSimStatesChanged();
168  m_select_all_check_box->setTristate(false);
169  ResizeColumns();
170 }
171 
172 void ConversionList::SetupGui()
173 {
174  // > Main layout
175  QVBoxLayout* layout = new QVBoxLayout();
176  layout->setMargin(0);
177 
178  // > Table view
179  m_table_view = new QTableView();
180  m_table_view->setModel(m_filter_model);
181  m_table_view->setShowGrid(false);
182  m_table_view->verticalHeader()->setHidden(true);
183  m_table_view->setSelectionBehavior(QAbstractItemView::SelectRows);
184  m_table_view->setHorizontalScrollMode(QTableView::ScrollPerPixel);
185 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
186  m_table_view->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
187  m_table_view->horizontalHeader()->setSectionsClickable(false);
188 #else
189  m_table_view->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
190  m_table_view->horizontalHeader()->setClickable(false);
191 #endif
192  m_table_view->horizontalHeader()->setStretchLastSection(true);
193  m_table_view->horizontalHeader()->setHighlightSections(false);
194  layout->addWidget(m_table_view);
195  connect(m_table_view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
196  this, SLOT(EmitSelectionChanged()));
197  // < Table view
198 
199  // > Filter & Select all
200  QHBoxLayout* selectionLayout = new QHBoxLayout();
201  selectionLayout->addWidget(new QLabel("Filter:"));
202 
203  // > Step filter
204  m_step_filter = new QLineEdit();
205  m_step_filter->setPlaceholderText("steps");
206  m_step_filter->setSizePolicy(QSizePolicy::Ignored, m_step_filter->sizePolicy().verticalPolicy());
207  m_step_filter->setValidator(new QRegExpValidator(StepSelection::g_repeated_regex, this));
208  selectionLayout->addWidget(m_step_filter, 1);
209  connect(m_step_filter, SIGNAL(textChanged(const QString&)), this, SLOT(UpdateStepFilter()));
210  // < Step filter
211 
212  selectionLayout->addWidget(new QLabel("in"));
213 
214  // > File filter
215  m_file_filter = new QLineEdit();
216  m_file_filter->setPlaceholderText("files");
217  m_file_filter->setSizePolicy(QSizePolicy::Ignored, m_file_filter->sizePolicy().verticalPolicy());
218  selectionLayout->addWidget(m_file_filter, 1);
219  connect(m_file_filter, SIGNAL(textChanged(const QString&)), this, SLOT(UpdateFileFilter()));
220  // < File filter
221 
222  m_select_all_check_box = new QCheckBox("Select All");
223  connect(m_select_all_check_box, SIGNAL(clicked()), this, SLOT(SelectAll()));
224  selectionLayout->addWidget(m_select_all_check_box);
225  layout->addLayout(selectionLayout);
226  // < Filter & Select all
227 
228  setLayout(layout);
229  // < Main layout
230 
231 }
232 
233 void ConversionList::ResizeColumns()
234 {
235  // To resize the first column to the longest number (the last)
236  int scrollPos = m_table_view->verticalScrollBar()->value();
237  m_table_view->scrollToBottom();
238  m_table_view->resizeColumnsToContents();
239  m_table_view->horizontalHeader()->setStretchLastSection(true);
240  m_table_view->verticalScrollBar()->setValue(scrollPos);
241 }
242 
243 } // namespace
STL namespace.
Interface for ConversionList.
void AddEntry(const EntryType &)
Add entry.
Custom proxy model to filter a QStandardModel with file names with time steps.
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
Namespace for SimPT shell package.
Definition: Client.cpp:50
Interface for StepSelection.
Interface for StepFilterProxyModel.
void CheckedChanged()
Signal emitted when the check state of some SimStates in the conversion has changed.
void Clear()
Clear all entries.
void SelectionChanged(bool selected)
Signal emitted when the selection in the conversion changes.
see the online Qt documentation
static const QRegExp g_repeated_regex
Regex for a series of repeated ranges, separated by , of ;.
Definition: StepSelection.h:54
void SetStepRanges(const QString &ranges)
Sets the ranges of the accepted steps of the first column.
virtual ~ConversionList()
Destructor.