File indexing completed on 2022-11-22 17:50:16

0001 /*
0002     SPDX-FileCopyrightText: 2006 Pino Toscano <toscano.pino@tiscali.it>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "search.h"
0008 
0009 #include "kalziumdataobject.h"
0010 
0011 Search::Search()
0012     : m_isActive(false)
0013     , m_searchKind(Search::SearchAll)
0014 {
0015 }
0016 
0017 QString Search::searchText() const
0018 {
0019     return m_searchText;
0020 }
0021 
0022 Search::SearchKind Search::searchKind() const
0023 {
0024     return m_searchKind;
0025 }
0026 
0027 bool Search::isActive() const
0028 {
0029     return m_isActive;
0030 }
0031 
0032 const QList<Element *> &Search::foundElements() const
0033 {
0034     return m_foundElements;
0035 }
0036 
0037 bool Search::matches(Element *e) const
0038 {
0039     return m_foundElements.contains(e);
0040 }
0041 
0042 bool Search::matches(int el) const
0043 {
0044     Element *element = KalziumDataObject::instance()->element(el);
0045     return matches(element);
0046 }
0047 
0048 void Search::doSearch(const QString &text, SearchKind kind)
0049 {
0050     m_isActive = true;
0051     m_searchText = text;
0052     m_searchKind = kind;
0053     QList<Element *> newresults;
0054     foreach (Element *e, KalziumDataObject::instance()->ElementList) {
0055         bool found = false;
0056         if (!found && e->dataAsString(ChemicalDataObject::name).contains(text, Qt::CaseInsensitive)) {
0057             found = true;
0058         }
0059         if (!found && e->dataAsString(ChemicalDataObject::symbol).contains(text, Qt::CaseInsensitive)) {
0060             found = true;
0061         }
0062         if (found) {
0063             newresults << e;
0064         }
0065     }
0066     if (newresults != m_foundElements) {
0067         m_foundElements = newresults;
0068         Q_EMIT searchChanged();
0069     }
0070 }
0071 
0072 void Search::resetSearch()
0073 {
0074     if (!m_isActive) {
0075         return;
0076     }
0077 
0078     m_foundElements.clear();
0079     m_isActive = false;
0080     Q_EMIT searchReset();
0081 }