2015-09-15 18:49:24 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vtablesearch.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 15 9, 2015
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2015-09-15 18:49:24 +02:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2015 Valentina project
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2015-09-15 18:49:24 +02:00
|
|
|
**
|
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vtablesearch.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
#include <QPalette>
|
2019-01-06 10:25:12 +01:00
|
|
|
#include <QStringBuilder>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QTableWidget>
|
|
|
|
#include <QTableWidgetItem>
|
|
|
|
#include <Qt>
|
|
|
|
|
2015-09-15 18:49:24 +02:00
|
|
|
#include "../vmisc/def.h"
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-08-26 15:16:06 +02:00
|
|
|
VTableSearch::VTableSearch(QTableWidget *table, QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
table(table),
|
2015-09-15 18:49:24 +02:00
|
|
|
searchIndex(-1),
|
|
|
|
searchList()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::Clear()
|
|
|
|
{
|
2016-12-20 19:57:20 +01:00
|
|
|
SCASSERT(table != nullptr)
|
2015-09-15 18:49:24 +02:00
|
|
|
|
2016-08-25 18:32:59 +02:00
|
|
|
for(int i = 0; i < table->rowCount(); ++i)
|
2015-09-15 18:49:24 +02:00
|
|
|
{
|
2016-08-25 18:32:59 +02:00
|
|
|
for(int j = 0; j < table->columnCount(); ++j)
|
2015-09-15 18:49:24 +02:00
|
|
|
{
|
2016-08-25 18:32:59 +02:00
|
|
|
if (QTableWidgetItem *item = table->item(i, j))
|
|
|
|
{
|
|
|
|
if (item->row() % 2 != 0 && table->alternatingRowColors())
|
|
|
|
{
|
|
|
|
item->setBackground(QPalette().alternateBase());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item->setBackground(QPalette().base());
|
|
|
|
}
|
|
|
|
}
|
2015-09-15 18:49:24 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-25 18:32:59 +02:00
|
|
|
|
|
|
|
searchList.clear();
|
2015-09-15 18:49:24 +02:00
|
|
|
searchIndex = -1;
|
2016-08-26 15:16:06 +02:00
|
|
|
|
|
|
|
emit HasResult(false);
|
2015-09-15 18:49:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::ShowNext(int newIndex)
|
|
|
|
{
|
|
|
|
if (not searchList.isEmpty())
|
|
|
|
{
|
|
|
|
QTableWidgetItem *item = searchList.at(searchIndex);
|
|
|
|
item->setBackground(Qt::yellow);
|
|
|
|
|
|
|
|
item = searchList.at(newIndex);
|
|
|
|
item->setBackground(Qt::red);
|
|
|
|
table->scrollToItem(item);
|
|
|
|
searchIndex = newIndex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 12:46:22 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QList<QTableWidgetItem *> VTableSearch::FindTableItems(const QString& term)
|
2019-01-06 10:25:12 +01:00
|
|
|
{
|
|
|
|
if (term.isEmpty())
|
|
|
|
{
|
2021-11-19 12:23:51 +01:00
|
|
|
return {};
|
2019-01-06 10:25:12 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 12:46:22 +01:00
|
|
|
if (term.startsWith(QChar('/')))
|
2019-01-06 10:25:12 +01:00
|
|
|
{
|
2019-01-06 12:46:22 +01:00
|
|
|
QRegularExpression qre(QStringLiteral("^/(?<searchType>[^/]+)/(?<searchString>.+)$"));
|
2019-01-06 10:25:12 +01:00
|
|
|
QScopedPointer<QRegularExpressionMatch> match(new QRegularExpressionMatch());
|
|
|
|
if (!term.contains(qre, match.data()))
|
|
|
|
{
|
|
|
|
return QList<QTableWidgetItem *>();
|
|
|
|
}
|
|
|
|
|
2019-01-06 12:46:22 +01:00
|
|
|
auto searchType = match->capturedRef(QStringLiteral("searchType"));
|
|
|
|
auto searchString = match->capturedRef(QStringLiteral("searchString"));
|
|
|
|
if (searchType == QChar('r'))
|
2019-01-06 10:25:12 +01:00
|
|
|
{
|
|
|
|
QString reSearchString = ".*" % searchString % ".*";
|
2020-07-08 09:31:53 +02:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
|
2019-01-06 10:25:12 +01:00
|
|
|
return table->findItems(reSearchString, Qt::MatchRegExp);
|
2020-07-08 09:31:53 +02:00
|
|
|
#else
|
|
|
|
return table->findItems(reSearchString, Qt::MatchRegularExpression);
|
|
|
|
#endif
|
2019-01-06 10:25:12 +01:00
|
|
|
}
|
2019-01-06 12:46:22 +01:00
|
|
|
|
2021-11-19 12:23:51 +01:00
|
|
|
return {};
|
2019-01-06 10:25:12 +01:00
|
|
|
}
|
2019-01-06 12:46:22 +01:00
|
|
|
|
|
|
|
return table->findItems(term, Qt::MatchContains);
|
2019-01-06 10:25:12 +01:00
|
|
|
}
|
|
|
|
|
2015-09-15 18:49:24 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::Find(const QString &term)
|
|
|
|
{
|
2016-12-20 19:57:20 +01:00
|
|
|
SCASSERT(table != nullptr)
|
2015-09-15 18:49:24 +02:00
|
|
|
|
2016-08-25 18:32:59 +02:00
|
|
|
Clear();
|
2015-09-15 18:49:24 +02:00
|
|
|
|
2019-01-06 10:25:12 +01:00
|
|
|
searchList = FindTableItems(term);
|
2015-09-15 18:49:24 +02:00
|
|
|
|
2019-01-06 10:25:12 +01:00
|
|
|
if (not searchList.isEmpty())
|
|
|
|
{
|
2021-11-19 12:23:51 +01:00
|
|
|
for (auto *item : qAsConst(searchList))
|
2015-09-15 18:49:24 +02:00
|
|
|
{
|
2019-01-06 10:25:12 +01:00
|
|
|
item->setBackground(Qt::yellow);
|
|
|
|
}
|
2016-08-25 18:32:59 +02:00
|
|
|
|
2019-01-06 10:25:12 +01:00
|
|
|
searchIndex = 0;
|
|
|
|
QTableWidgetItem *item = searchList.at(searchIndex);
|
|
|
|
item->setBackground(Qt::red);
|
|
|
|
table->scrollToItem(item);
|
2016-08-26 15:16:06 +02:00
|
|
|
|
2019-01-06 10:25:12 +01:00
|
|
|
emit HasResult(true);
|
2015-09-15 18:49:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::FindPrevious()
|
|
|
|
{
|
|
|
|
int newIndex = searchIndex - 1;
|
|
|
|
|
|
|
|
if (newIndex < 0)
|
|
|
|
{
|
|
|
|
newIndex = searchList.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShowNext(newIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::FindNext()
|
|
|
|
{
|
|
|
|
int newIndex = searchIndex + 1;
|
|
|
|
|
|
|
|
if (newIndex >= searchList.size())
|
|
|
|
{
|
|
|
|
newIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShowNext(newIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::RemoveRow(int row)
|
|
|
|
{
|
|
|
|
if (searchIndex < 0 || searchIndex >= searchList.size())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int indexRow = searchList.at(searchIndex)->row();
|
|
|
|
|
|
|
|
if (row <= indexRow)
|
|
|
|
{
|
2018-04-03 10:15:58 +02:00
|
|
|
for (auto item : qAsConst(searchList))
|
2015-09-15 18:49:24 +02:00
|
|
|
{
|
|
|
|
if (item->row() == row)
|
|
|
|
{
|
|
|
|
--searchIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::AddRow(int row)
|
|
|
|
{
|
|
|
|
if (searchIndex < 0 || searchIndex >= searchList.size())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int indexRow = searchList.at(searchIndex)->row();
|
|
|
|
|
|
|
|
if (row <= indexRow)
|
|
|
|
{
|
2018-04-03 10:15:58 +02:00
|
|
|
for (auto item : qAsConst(searchList))
|
2015-09-15 18:49:24 +02:00
|
|
|
{
|
|
|
|
if (item->row() == row)
|
|
|
|
{
|
|
|
|
++searchIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::RefreshList(const QString &term)
|
|
|
|
{
|
2016-12-20 19:57:20 +01:00
|
|
|
SCASSERT(table != nullptr)
|
2015-09-15 18:49:24 +02:00
|
|
|
|
2019-01-06 10:25:12 +01:00
|
|
|
searchList = FindTableItems(term);
|
2015-09-15 18:49:24 +02:00
|
|
|
|
|
|
|
if (not searchList.isEmpty())
|
|
|
|
{
|
2019-01-06 10:25:12 +01:00
|
|
|
for (auto item : qAsConst(searchList))
|
|
|
|
{
|
|
|
|
item->setBackground(Qt::yellow);
|
|
|
|
}
|
|
|
|
|
2015-09-15 18:49:24 +02:00
|
|
|
if (searchIndex < 0)
|
|
|
|
{
|
|
|
|
searchIndex = searchList.size() - 1;
|
|
|
|
}
|
|
|
|
else if (searchIndex >= searchList.size())
|
|
|
|
{
|
|
|
|
searchIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTableWidgetItem *item = searchList.at(searchIndex);
|
|
|
|
item->setBackground(Qt::red);
|
|
|
|
table->scrollToItem(item);
|
2016-08-26 15:16:06 +02:00
|
|
|
|
|
|
|
emit HasResult(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit HasResult(false);
|
2015-09-15 18:49:24 +02:00
|
|
|
}
|
|
|
|
}
|