Seamly2D
Code documentation
vtablesearch.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file vtablesearch.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 15 9, 2015
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2015 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Seamly2D is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #include "vtablesearch.h"
53 
54 #include <QPalette>
55 #include <QTableWidget>
56 #include <QTableWidgetItem>
57 #include <Qt>
58 
59 #include "../vmisc/def.h"
60 
61 //---------------------------------------------------------------------------------------------------------------------
62 VTableSearch::VTableSearch(QTableWidget *table, QObject *parent)
63  : QObject(parent),
64  table(table),
65  searchIndex(-1),
66  searchList()
67 {
68 }
69 
70 //---------------------------------------------------------------------------------------------------------------------
72 {
73  SCASSERT(table != nullptr)
74 
75  for(int i = 0; i < table->rowCount(); ++i)
76  {
77  for(int j = 0; j < table->columnCount(); ++j)
78  {
79  if (QTableWidgetItem *item = table->item(i, j))
80  {
81  if (item->row() % 2 != 0 && table->alternatingRowColors())
82  {
83  item->setBackground(QPalette().alternateBase());
84  }
85  else
86  {
87  item->setBackground(QPalette().base());
88  }
89  }
90  }
91  }
92 
93  searchList.clear();
94  searchIndex = -1;
95 
96  emit HasResult(false);
97 }
98 
99 //---------------------------------------------------------------------------------------------------------------------
100 void VTableSearch::ShowNext(int newIndex)
101 {
102  if (not searchList.isEmpty())
103  {
104  QTableWidgetItem *item = searchList.at(searchIndex);
105  item->setBackground(Qt::yellow);
106 
107  item = searchList.at(newIndex);
108  item->setBackground(Qt::red);
109  table->scrollToItem(item);
110  searchIndex = newIndex;
111  }
112  else
113  {
114  Clear();
115  }
116 }
117 
118 //---------------------------------------------------------------------------------------------------------------------
119 void VTableSearch::Find(const QString &term)
120 {
121  SCASSERT(table != nullptr)
122 
123  Clear();
124 
125  if (not term.isEmpty())
126  {
127  searchList = table->findItems(term, Qt::MatchContains);
128 
129  if (not searchList.isEmpty())
130  {
131  foreach(QTableWidgetItem *item, searchList)
132  {
133  item->setBackground(Qt::yellow);
134  }
135 
136  searchIndex = 0;
137  QTableWidgetItem *item = searchList.at(searchIndex);
138  item->setBackground(Qt::red);
139  table->scrollToItem(item);
140 
141  emit HasResult(true);
142  }
143  }
144 }
145 
146 //---------------------------------------------------------------------------------------------------------------------
148 {
149  int newIndex = searchIndex - 1;
150 
151  if (newIndex < 0)
152  {
153  newIndex = searchList.size() - 1;
154  }
155 
156  ShowNext(newIndex);
157 }
158 
159 //---------------------------------------------------------------------------------------------------------------------
161 {
162  int newIndex = searchIndex + 1;
163 
164  if (newIndex >= searchList.size())
165  {
166  newIndex = 0;
167  }
168 
169  ShowNext(newIndex);
170 }
171 
172 //---------------------------------------------------------------------------------------------------------------------
174 {
175  if (searchIndex < 0 || searchIndex >= searchList.size())
176  {
177  return;
178  }
179 
180  const int indexRow = searchList.at(searchIndex)->row();
181 
182  if (row <= indexRow)
183  {
184  foreach(QTableWidgetItem *item, searchList)
185  {
186  if (item->row() == row)
187  {
188  --searchIndex;
189  }
190  }
191  }
192 }
193 
194 //---------------------------------------------------------------------------------------------------------------------
195 void VTableSearch::AddRow(int row)
196 {
197  if (searchIndex < 0 || searchIndex >= searchList.size())
198  {
199  return;
200  }
201 
202  const int indexRow = searchList.at(searchIndex)->row();
203 
204  if (row <= indexRow)
205  {
206  foreach(QTableWidgetItem *item, searchList)
207  {
208  if (item->row() == row)
209  {
210  ++searchIndex;
211  }
212  }
213  }
214 }
215 
216 //---------------------------------------------------------------------------------------------------------------------
217 void VTableSearch::RefreshList(const QString &term)
218 {
219  SCASSERT(table != nullptr)
220 
221  if (term.isEmpty())
222  {
223  return;
224  }
225 
226  searchList = table->findItems(term, Qt::MatchContains);
227 
228  foreach(QTableWidgetItem *item, searchList)
229  {
230  item->setBackground(Qt::yellow);
231  }
232 
233  if (not searchList.isEmpty())
234  {
235  if (searchIndex < 0)
236  {
237  searchIndex = searchList.size() - 1;
238  }
239  else if (searchIndex >= searchList.size())
240  {
241  searchIndex = 0;
242  }
243 
244  QTableWidgetItem *item = searchList.at(searchIndex);
245  item->setBackground(Qt::red);
246  table->scrollToItem(item);
247 
248  emit HasResult(true);
249  }
250  else
251  {
252  emit HasResult(false);
253  }
254 }
void AddRow(int row)
void RemoveRow(int row)
VTableSearch(QTableWidget *table, QObject *parent=nullptr)
void Find(const QString &term)
QList< QTableWidgetItem * > searchList
Definition: vtablesearch.h:82
QTableWidget * table
Definition: vtablesearch.h:80
void ShowNext(int newIndex)
void FindPrevious()
void HasResult(bool state)
void RefreshList(const QString &term)
#define SCASSERT(cond)
Definition: def.h:317