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>
|
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
#include "../vmisc/def.h"
|
2015-09-15 18:49:24 +02:00
|
|
|
|
2021-11-22 14:24:48 +01:00
|
|
|
const int VTableSearch::MaxHistoryRecords = 10;
|
|
|
|
|
2015-09-15 18:49:24 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-08-26 15:16:06 +02:00
|
|
|
VTableSearch::VTableSearch(QTableWidget *table, QObject *parent)
|
2023-08-05 16:51:23 +02:00
|
|
|
: QObject(parent),
|
|
|
|
table(table)
|
2015-09-15 18:49:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::Clear()
|
|
|
|
{
|
2016-12-20 19:57:20 +01:00
|
|
|
SCASSERT(table != nullptr)
|
2015-09-15 18:49:24 +02:00
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
for (int i = 0; i < table->rowCount(); ++i)
|
2015-09-15 18:49:24 +02:00
|
|
|
{
|
2023-08-05 16:51:23 +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())
|
|
|
|
{
|
2023-08-05 16:51:23 +02:00
|
|
|
item->setBackground(table->palette().alternateBase());
|
2016-08-25 18:32:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-05 16:51:23 +02:00
|
|
|
item->setBackground(table->palette().base());
|
2016-08-25 18:32:59 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-02-09 16:23:11 +01:00
|
|
|
void VTableSearch::ShowNext(vsizetype newIndex)
|
2015-09-15 18:49:24 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2021-11-22 14:24:48 +01:00
|
|
|
auto VTableSearch::FindTableItems(QString term) -> QList<QTableWidgetItem *>
|
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
|
|
|
}
|
|
|
|
|
2021-11-22 14:24:48 +01:00
|
|
|
QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption;
|
|
|
|
|
|
|
|
if (not m_matchCase)
|
|
|
|
{
|
|
|
|
options |= QRegularExpression::CaseInsensitiveOption;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_matchWord)
|
|
|
|
{
|
|
|
|
options |= QRegularExpression::UseUnicodePropertiesOption;
|
|
|
|
term = "\\b" % term % "\\b";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_matchRegexp && m_useUnicodePreperties)
|
2019-01-06 10:25:12 +01:00
|
|
|
{
|
2021-11-22 14:24:48 +01:00
|
|
|
options |= QRegularExpression::UseUnicodePropertiesOption;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegularExpression re(term, options);
|
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
if (not re.isValid())
|
2021-11-22 14:24:48 +01:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QTableWidgetItem *> list;
|
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
for (int r = 0; r < table->rowCount(); ++r)
|
2021-11-22 14:24:48 +01:00
|
|
|
{
|
2023-08-05 16:51:23 +02:00
|
|
|
for (int c = 0; c < table->columnCount(); ++c)
|
2019-01-06 10:25:12 +01:00
|
|
|
{
|
2021-11-22 14:24:48 +01:00
|
|
|
QTableWidgetItem *cell = table->item(r, c);
|
|
|
|
if (cell != nullptr)
|
|
|
|
{
|
|
|
|
QString text = cell->text();
|
|
|
|
QRegularExpressionMatch match = re.match(text);
|
|
|
|
if (match.hasMatch())
|
|
|
|
{
|
|
|
|
list.append(cell);
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 10:25:12 +01:00
|
|
|
}
|
2021-11-22 14:24:48 +01:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
auto VTableSearch::FindCurrentMatchIndex() const -> int
|
|
|
|
{
|
|
|
|
if (searchList.isEmpty())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
QList<QTableWidgetItem *> selectedItems = table->selectedItems();
|
2021-11-22 14:24:48 +01:00
|
|
|
if (selectedItems.isEmpty())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2019-01-06 10:25:12 +01:00
|
|
|
|
2023-08-12 11:03:28 +02:00
|
|
|
QTableWidgetItem *selectedItem = selectedItems.constFirst();
|
2021-11-22 14:24:48 +01:00
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
for (int i = 0; i < searchList.size(); ++i)
|
2021-11-22 14:24:48 +01:00
|
|
|
{
|
2023-08-05 16:51:23 +02:00
|
|
|
QTableWidgetItem *item = searchList.at(i);
|
|
|
|
if (item->row() >= selectedItem->row() && item->column() >= selectedItem->column())
|
2019-01-06 10:25:12 +01:00
|
|
|
{
|
2021-11-22 14:24:48 +01:00
|
|
|
return i;
|
2019-01-06 10:25:12 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-06 12:46:22 +01:00
|
|
|
|
2021-11-22 14:24:48 +01:00
|
|
|
return 0;
|
2019-01-06 10:25:12 +01:00
|
|
|
}
|
|
|
|
|
2015-09-15 18:49:24 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::Find(const QString &term)
|
|
|
|
{
|
2023-08-05 16:51:23 +02:00
|
|
|
if (table == nullptr)
|
2021-11-22 14:24:48 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
2021-11-22 14:24:48 +01:00
|
|
|
searchIndex = FindCurrentMatchIndex();
|
2019-01-06 10:25:12 +01:00
|
|
|
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);
|
2021-11-22 14:24:48 +01:00
|
|
|
return;
|
2015-09-15 18:49:24 +02:00
|
|
|
}
|
2021-11-22 14:24:48 +01:00
|
|
|
|
|
|
|
emit HasResult(false);
|
2015-09-15 18:49:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::FindPrevious()
|
|
|
|
{
|
2023-02-09 16:23:11 +01:00
|
|
|
vsizetype newIndex = searchIndex - 1;
|
2015-09-15 18:49:24 +02:00
|
|
|
|
|
|
|
if (newIndex < 0)
|
|
|
|
{
|
|
|
|
newIndex = searchList.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShowNext(newIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::FindNext()
|
|
|
|
{
|
2023-02-09 16:23:11 +01:00
|
|
|
vsizetype newIndex = searchIndex + 1;
|
2015-09-15 18:49:24 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-11-22 14:24:48 +01:00
|
|
|
for (auto *item : qAsConst(searchList))
|
2015-09-15 18:49:24 +02:00
|
|
|
{
|
|
|
|
if (item->row() == row)
|
|
|
|
{
|
|
|
|
--searchIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-02-09 16:23:11 +01:00
|
|
|
void VTableSearch::AddRow(vsizetype row)
|
2015-09-15 18:49:24 +02:00
|
|
|
{
|
|
|
|
if (searchIndex < 0 || searchIndex >= searchList.size())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int indexRow = searchList.at(searchIndex)->row();
|
|
|
|
|
|
|
|
if (row <= indexRow)
|
|
|
|
{
|
2021-11-22 14:24:48 +01: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())
|
|
|
|
{
|
2021-11-22 14:24:48 +01:00
|
|
|
for (auto *item : qAsConst(searchList))
|
2019-01-06 10:25:12 +01:00
|
|
|
{
|
|
|
|
item->setBackground(Qt::yellow);
|
|
|
|
}
|
|
|
|
|
2015-09-15 18:49:24 +02:00
|
|
|
if (searchIndex < 0)
|
|
|
|
{
|
2023-08-05 16:51:23 +02:00
|
|
|
searchIndex = searchList.size() - 1;
|
2015-09-15 18:49:24 +02:00
|
|
|
}
|
|
|
|
else if (searchIndex >= searchList.size())
|
|
|
|
{
|
2023-08-05 16:51:23 +02:00
|
|
|
searchIndex = 0;
|
2015-09-15 18:49:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-11-22 14:24:48 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::SetMatchCase(bool value)
|
|
|
|
{
|
|
|
|
m_matchCase = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
auto VTableSearch::IsMatchCase() const -> bool
|
|
|
|
{
|
|
|
|
return m_matchCase;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::SetMatchWord(bool value)
|
|
|
|
{
|
|
|
|
m_matchWord = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
auto VTableSearch::IsMatchWord() const -> bool
|
|
|
|
{
|
|
|
|
return m_matchWord;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::SetMatchRegexp(bool value)
|
|
|
|
{
|
|
|
|
m_matchRegexp = value;
|
|
|
|
m_matchWord = false;
|
|
|
|
|
|
|
|
if (not m_matchRegexp)
|
|
|
|
{
|
|
|
|
m_useUnicodePreperties = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
auto VTableSearch::IsMatchRegexp() const -> bool
|
|
|
|
{
|
|
|
|
return m_matchRegexp;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTableSearch::SetUseUnicodePreperties(bool value)
|
|
|
|
{
|
|
|
|
m_useUnicodePreperties = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
auto VTableSearch::IsUseUnicodePreperties() const -> bool
|
|
|
|
{
|
|
|
|
return m_useUnicodePreperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-02-09 16:23:11 +01:00
|
|
|
auto VTableSearch::MatchIndex() const -> vsizetype
|
2021-11-22 14:24:48 +01:00
|
|
|
{
|
|
|
|
return searchIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-02-09 16:23:11 +01:00
|
|
|
auto VTableSearch::MatchCount() const -> vsizetype
|
2021-11-22 14:24:48 +01:00
|
|
|
{
|
|
|
|
return searchList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
auto VTableSearch::SearchPlaceholder() const -> QString
|
|
|
|
{
|
|
|
|
if (m_matchCase && not m_matchWord && not m_matchRegexp)
|
|
|
|
{
|
|
|
|
return tr("Match case");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not m_matchCase && m_matchWord && not m_matchRegexp)
|
|
|
|
{
|
|
|
|
return tr("Words");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not m_matchCase && m_matchRegexp)
|
|
|
|
{
|
|
|
|
return tr("Regex");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_matchCase && m_matchWord)
|
|
|
|
{
|
|
|
|
return tr("Match case and words");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_matchCase && m_matchRegexp)
|
|
|
|
{
|
|
|
|
return tr("Match case and regex");
|
|
|
|
}
|
|
|
|
|
|
|
|
return tr("Search");
|
|
|
|
}
|