From 902fe178e508c1dbb7bc880cc9be6249ae1a20f0 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Tue, 15 Sep 2015 19:49:24 +0300 Subject: [PATCH] Resolved issue #364. Add Search feature to Measurement dialogs. --HG-- branch : develop --- src/app/tape/tape.pri | 6 +- src/app/tape/tmainwindow.cpp | 54 ++++++++- src/app/tape/tmainwindow.h | 7 +- src/app/tape/tmainwindow.ui | 65 ++++++++++- src/app/tape/vtablesearch.cpp | 208 ++++++++++++++++++++++++++++++++++ src/app/tape/vtablesearch.h | 58 ++++++++++ 6 files changed, 392 insertions(+), 6 deletions(-) create mode 100644 src/app/tape/vtablesearch.cpp create mode 100644 src/app/tape/vtablesearch.h diff --git a/src/app/tape/tape.pri b/src/app/tape/tape.pri index ae955616d..745b28067 100644 --- a/src/app/tape/tape.pri +++ b/src/app/tape/tape.pri @@ -12,7 +12,8 @@ SOURCES += \ $$PWD/dialogs/tapeconfigdialog.cpp \ $$PWD/dialogs/configpages/tapeconfigurationpage.cpp \ $$PWD/dialogs/configpages/tapepathpage.cpp \ - $$PWD/vlitepattern.cpp + $$PWD/vlitepattern.cpp \ + $$PWD/vtablesearch.cpp HEADERS += \ $$PWD/tmainwindow.h \ @@ -25,7 +26,8 @@ HEADERS += \ $$PWD/dialogs/tapeconfigdialog.h \ $$PWD/dialogs/configpages/tapeconfigurationpage.h \ $$PWD/dialogs/configpages/tapepathpage.h \ - $$PWD/vlitepattern.h + $$PWD/vlitepattern.h \ + $$PWD/vtablesearch.h FORMS += \ $$PWD/tmainwindow.ui \ diff --git a/src/app/tape/tmainwindow.cpp b/src/app/tape/tmainwindow.cpp index 8f433f598..4e39e180b 100644 --- a/src/app/tape/tmainwindow.cpp +++ b/src/app/tape/tmainwindow.cpp @@ -68,9 +68,11 @@ TMainWindow::TMainWindow(QWidget *parent) gradationSizes(nullptr), comboBoxUnits(nullptr), formulaBaseHeight(0), - lock(nullptr) + lock(nullptr), + search() { ui->setupUi(this); + search = QSharedPointer(new VTableSearch(ui->tableWidget)); ui->tabWidget->setVisible(false); ui->mainToolBar->setContextMenuPolicy(Qt::PreventContextMenu); @@ -350,6 +352,24 @@ void TMainWindow::OpenTemplate() } } +//--------------------------------------------------------------------------------------------------------------------- +void TMainWindow::Find(const QString &term) +{ + search->Find(term); +} + +//--------------------------------------------------------------------------------------------------------------------- +void TMainWindow::FindPrevious() +{ + search->FindPrevious(); +} + +//--------------------------------------------------------------------------------------------------------------------- +void TMainWindow::FindNext() +{ + search->FindNext(); +} + //--------------------------------------------------------------------------------------------------------------------- void TMainWindow::closeEvent(QCloseEvent *event) { @@ -602,7 +622,9 @@ void TMainWindow::Remove() MeasurementsWasSaved(false); + search->RemoveRow(row); RefreshData(); + search->RefreshList(ui->lineEditFind->text()); if (ui->tableWidget->rowCount() > 0) { @@ -668,6 +690,7 @@ void TMainWindow::MoveUp() m->MoveUp(nameField->text()); MeasurementsWasSaved(false); RefreshData(); + search->RefreshList(ui->lineEditFind->text()); ui->tableWidget->selectRow(row-1); } @@ -685,6 +708,7 @@ void TMainWindow::MoveDown() m->MoveDown(nameField->text()); MeasurementsWasSaved(false); RefreshData(); + search->RefreshList(ui->lineEditFind->text()); ui->tableWidget->selectRow(row+1); } @@ -719,6 +743,8 @@ void TMainWindow::Fx() RefreshData(); + search->RefreshList(ui->lineEditFind->text()); + ui->tableWidget->selectRow(row); } delete dialog; @@ -749,7 +775,9 @@ void TMainWindow::AddCustom() m->AddEmptyAfter(nameField->text(), name); } + search->AddRow(currentRow); RefreshData(); + search->RefreshList(ui->lineEditFind->text()); ui->tableWidget->selectRow(currentRow); @@ -778,6 +806,8 @@ void TMainWindow::AddKnown() { m->AddEmpty(list.at(i)); } + + search->AddRow(currentRow); } } else @@ -795,11 +825,13 @@ void TMainWindow::AddKnown() { m->AddEmptyAfter(after, list.at(i)); } + search->AddRow(currentRow); after = list.at(i); } } RefreshData(); + search->RefreshList(ui->lineEditFind->text()); ui->tableWidget->selectRow(currentRow); @@ -888,6 +920,8 @@ void TMainWindow::ImportFromPattern() RefreshData(); + search->RefreshList(ui->lineEditFind->text()); + ui->tableWidget->selectRow(currentRow); MeasurementsWasSaved(false); @@ -899,6 +933,7 @@ void TMainWindow::ChangedSize(const QString &text) const int row = ui->tableWidget->currentRow(); data->SetSize(text.toInt()); RefreshData(); + search->RefreshList(ui->lineEditFind->text()); ui->tableWidget->selectRow(row); } @@ -908,6 +943,7 @@ void TMainWindow::ChangedHeight(const QString &text) const int row = ui->tableWidget->currentRow(); data->SetHeight(text.toInt()); RefreshData(); + search->RefreshList(ui->lineEditFind->text()); ui->tableWidget->selectRow(row); } @@ -1052,6 +1088,7 @@ void TMainWindow::SaveMName() m->SetMName(nameField->text(), newName); MeasurementsWasSaved(false); RefreshData(); + search->RefreshList(ui->lineEditFind->text()); ui->tableWidget->blockSignals(true); ui->tableWidget->selectRow(row); @@ -1121,6 +1158,7 @@ void TMainWindow::SaveMValue() const QTextCursor cursor = ui->plainTextEditFormula->textCursor(); RefreshData(); + search->RefreshList(ui->lineEditFind->text()); ui->tableWidget->blockSignals(true); ui->tableWidget->selectRow(row); @@ -1145,6 +1183,7 @@ void TMainWindow::SaveMBaseValue(double value) MeasurementsWasSaved(false); RefreshData(); + search->RefreshList(ui->lineEditFind->text()); ui->tableWidget->blockSignals(true); ui->tableWidget->selectRow(row); @@ -1167,6 +1206,7 @@ void TMainWindow::SaveMSizeIncrease(double value) MeasurementsWasSaved(false); RefreshData(); + search->RefreshList(ui->lineEditFind->text()); ui->tableWidget->blockSignals(true); ui->tableWidget->selectRow(row); @@ -1189,6 +1229,7 @@ void TMainWindow::SaveMHeightIncrease(double value) MeasurementsWasSaved(false); RefreshData(); + search->RefreshList(ui->lineEditFind->text()); ui->tableWidget->selectRow(row); } @@ -1446,6 +1487,10 @@ void TMainWindow::InitWindow() connect(ui->toolButtonExpr, &QToolButton::clicked, this, &TMainWindow::Fx); } + connect(ui->lineEditFind, &QLineEdit::textEdited, this, &TMainWindow::Find); + connect(ui->toolButtonFindPrevious, &QToolButton::clicked, this, &TMainWindow::FindPrevious); + connect(ui->toolButtonFindNext, &QToolButton::clicked, this, &TMainWindow::FindNext); + ui->plainTextEditNotes->setPlainText(m->Notes()); connect(ui->plainTextEditNotes, &QPlainTextEdit::textChanged, this, &TMainWindow::SaveNotes); @@ -1660,6 +1705,7 @@ void TMainWindow::SetDefaultSize(int value) //--------------------------------------------------------------------------------------------------------------------- void TMainWindow::RefreshData() { + data->ClearUniqueNames(); data->ClearVariables(VarType::Measurement); m->ReadMeasurements(); @@ -1799,6 +1845,10 @@ void TMainWindow::MFields(bool enabled) ui->pushButtonGrow->setEnabled(enabled); ui->toolButtonExpr->setEnabled(enabled); } + + ui->lineEditFind->setEnabled(enabled); + ui->toolButtonFindPrevious->setEnabled(enabled); + ui->toolButtonFindNext->setEnabled(enabled); } //--------------------------------------------------------------------------------------------------------------------- @@ -1985,6 +2035,8 @@ void TMainWindow::UpdatePatternUnit() ShowUnits(); RefreshTable(); + search->RefreshList(ui->lineEditFind->text()); + ui->tableWidget->selectRow(row); } diff --git a/src/app/tape/tmainwindow.h b/src/app/tape/tmainwindow.h index 816c590dd..3115d4127 100644 --- a/src/app/tape/tmainwindow.h +++ b/src/app/tape/tmainwindow.h @@ -35,6 +35,7 @@ #include "../vmisc/def.h" #include "../vmisc/vlockguard.h" #include "../vformat/vmeasurements.h" +#include "vtablesearch.h" namespace Ui { @@ -113,10 +114,11 @@ private slots: void SaveMFullName(); void NewWindow(); - void Preferences(); - void PatternUnitChanged(int index); + void Find(const QString &term); + void FindPrevious(); + void FindNext(); private: Q_DISABLE_COPY(TMainWindow) @@ -132,6 +134,7 @@ private: QComboBox *comboBoxUnits; int formulaBaseHeight; VLockGuardPtr lock; + QSharedPointer search; void SetupMenu(); void InitWindow(); diff --git a/src/app/tape/tmainwindow.ui b/src/app/tape/tmainwindow.ui index 6d9cd8037..d2ad073ef 100644 --- a/src/app/tape/tmainwindow.ui +++ b/src/app/tape/tmainwindow.ui @@ -7,7 +7,7 @@ 0 0 536 - 694 + 726 @@ -43,6 +43,69 @@ Measurements + + + + + + Find: + + + + + + + false + + + + + + + false + + + Find Previous + + + ... + + + + + + + + Ctrl+Shift+G + + + + + + + false + + + Find Next + + + ... + + + + + + + + Ctrl+G + + + false + + + + + diff --git a/src/app/tape/vtablesearch.cpp b/src/app/tape/vtablesearch.cpp new file mode 100644 index 000000000..330973824 --- /dev/null +++ b/src/app/tape/vtablesearch.cpp @@ -0,0 +1,208 @@ +/************************************************************************ + ** + ** @file vtablesearch.cpp + ** @author Roman Telezhynskyi + ** @date 15 9, 2015 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentine project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2015 Valentina project + ** All Rights Reserved. + ** + ** 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 . + ** + *************************************************************************/ + +#include "vtablesearch.h" +#include "../vmisc/def.h" + +//--------------------------------------------------------------------------------------------------------------------- +VTableSearch::VTableSearch(QTableWidget *table) + :table(table), + searchIndex(-1), + searchList() +{ +} + +//--------------------------------------------------------------------------------------------------------------------- +void VTableSearch::Clear() +{ + SCASSERT(table != nullptr); + + foreach(QTableWidgetItem *item, searchList) + { + if (item->row() % 2 != 0 && table->alternatingRowColors()) + { + item->setBackground(QPalette().alternateBase()); + } + else + { + item->setBackground(QPalette().base()); + } + } + searchIndex = -1; +} + +//--------------------------------------------------------------------------------------------------------------------- +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(); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VTableSearch::Find(const QString &term) +{ + SCASSERT(table != nullptr); + + const QList list = table->findItems(term, Qt::MatchContains); + + if (list.isEmpty() || term.isEmpty()) + { + Clear(); + } + else + { + Clear(); + + searchList = list; + foreach(QTableWidgetItem *item, searchList) + { + item->setBackground(Qt::yellow); + } + + searchIndex = 0; + QTableWidgetItem *item = searchList.at(searchIndex); + item->setBackground(Qt::red); + table->scrollToItem(item); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +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) + { + foreach(QTableWidgetItem *item, searchList) + { + 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) + { + foreach(QTableWidgetItem *item, searchList) + { + if (item->row() == row) + { + ++searchIndex; + } + } + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VTableSearch::RefreshList(const QString &term) +{ + SCASSERT(table != nullptr); + + if (term.isEmpty()) + { + return; + } + + searchList = table->findItems(term, Qt::MatchContains); + + foreach(QTableWidgetItem *item, searchList) + { + item->setBackground(Qt::yellow); + } + + if (not searchList.isEmpty()) + { + 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); + } +} diff --git a/src/app/tape/vtablesearch.h b/src/app/tape/vtablesearch.h new file mode 100644 index 000000000..187f5571d --- /dev/null +++ b/src/app/tape/vtablesearch.h @@ -0,0 +1,58 @@ +/************************************************************************ + ** + ** @file vtablesearch.h + ** @author Roman Telezhynskyi + ** @date 15 9, 2015 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentine project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2015 Valentina project + ** All Rights Reserved. + ** + ** 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 . + ** + *************************************************************************/ + +#ifndef VTABLESEARCH_H +#define VTABLESEARCH_H + +#include +#include + +class VTableSearch +{ +public: + VTableSearch(QTableWidget *table); + + void Find(const QString &term); + void FindPrevious(); + void FindNext(); + void RemoveRow(int row); + void AddRow(int row); + void RefreshList(const QString &term); + +private: + Q_DISABLE_COPY(VTableSearch) + + QTableWidget *table; + int searchIndex; + QList searchList; + + void Clear(); + void ShowNext(int newIndex); +}; + +#endif // VTABLESEARCH_H