From d7724c57b73a4f9ff7f4e160ed5cfaf4e3d07b69 Mon Sep 17 00:00:00 2001 From: lostjared Date: Fri, 10 Aug 2018 14:46:43 -0700 Subject: [PATCH] search box --- src/main_window.cpp | 9 +++- src/main_window.h | 4 +- src/search_box.cpp | 32 ++++++++++++- src/search_box.h | 9 ++-- src/tokenize.h | 111 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 8 deletions(-) create mode 100644 src/tokenize.h diff --git a/src/main_window.cpp b/src/main_window.cpp index 9da9566..05dc726 100644 --- a/src/main_window.cpp +++ b/src/main_window.cpp @@ -68,7 +68,8 @@ AC_MainWindow::AC_MainWindow(QWidget *parent) : QMainWindow(parent) { search_box = new SearchWindow(this); search_box->setParent(this); - + search_box->setFiltersControl(filters, custom_filters); + search_box->main_window = this; statusBar()->showMessage(tr("Acid Cam v2 Loaded - Use File Menu to Start")); take_snapshot = false; disp = new DisplayWindow(this); @@ -403,6 +404,12 @@ void AC_MainWindow::addClicked() { } } +void AC_MainWindow::updateList() { + std::vector> v; + buildVector(v); + playback->setVector(v); +} + void AC_MainWindow::rmvClicked() { int item = custom_filters->currentRow(); if(item != -1) { diff --git a/src/main_window.h b/src/main_window.h index 7ed4106..d903df0 100644 --- a/src/main_window.h +++ b/src/main_window.h @@ -14,6 +14,8 @@ #include "playback_thread.h" #include "search_box.h" +class SearchWindow; + class AC_MainWindow : public QMainWindow { Q_OBJECT public: @@ -36,7 +38,7 @@ public: QAction *help_about; QAction *open_search; QRadioButton *filter_single, *filter_custom; - + void updateList(); public slots: void addClicked(); void rmvClicked(); diff --git a/src/search_box.cpp b/src/search_box.cpp index 5a8c549..43c2e8f 100644 --- a/src/search_box.cpp +++ b/src/search_box.cpp @@ -1,4 +1,12 @@ #include "search_box.h" +#include"tokenize.h" + +std::string lowerString(std::string text) { + std::string new_text; + for(unsigned long i = 0; i < text.length(); ++i) + new_text += tolower(text[i]); + return new_text; +} SearchWindow::SearchWindow(QWidget *parent) : QDialog(parent) { setFixedSize(640, 480); @@ -27,12 +35,32 @@ void SearchWindow::createControls() { } void SearchWindow::search_filter() { - while(search_list->count() > 0) { search_list->takeItem(0); } + std::string search = lowerString(search_text->text().toStdString()); + std::vector tokens; + token::tokenize(search, std::string(" "), tokens); + for(int i = 0; i < filters->count(); ++i) { + std::string search_items = lowerString(filters->itemText(i).toStdString()); + for(unsigned q = 0; q < tokens.size(); ++q) { + if(search_items.find(tokens[q]) != std::string::npos) { + search_list->addItem(filters->itemText(i)); + } + } + } } void SearchWindow::add_current() { - + int index = search_list->currentRow(); + if(index >= 0) { + QListWidgetItem *in = search_list->item(index); + custom_list->addItem(in->text()); + main_window->updateList(); + } +} + +void SearchWindow::setFiltersControl(QComboBox *filter_box, QListWidget *custombox) { + filters = filter_box; + custom_list = custombox; } diff --git a/src/search_box.h b/src/search_box.h index 17b70b3..3899442 100644 --- a/src/search_box.h +++ b/src/search_box.h @@ -3,23 +3,24 @@ #define __SEARCHBOX__H__ #include "qtheaders.h" +#include "main_window.h" class SearchWindow : public QDialog { Q_OBJECT public: + AC_MainWindow *main_window; SearchWindow(QWidget *parent = 0); void createControls(); + void setFiltersControl(QComboBox *filter_box, QListWidget *custom); public slots: void search_filter(); void add_current(); private: - QListWidget *search_list; + QListWidget *search_list,*custom_list; QLineEdit *search_text; QPushButton *search, *add; - + QComboBox *filters; }; - - #endif diff --git a/src/tokenize.h b/src/tokenize.h new file mode 100644 index 0000000..9758a3c --- /dev/null +++ b/src/tokenize.h @@ -0,0 +1,111 @@ +/* Acid Cam Functions for OpenCV + * written by Jared Bruni https://github.com/lostjared + + This software is dedicated to all the people that struggle with mental illness. + + Website: http://lostsidedead.com + YouTube: http://youtube.com/LostSideDead + Instagram: http://instagram.com/jaredbruni + Twitter: http://twitter.com/jaredbruni + Facebook: http://facebook.com/LostSideDead0x + + You can use this program free of charge and redistrubute as long + as you do not charge anything for this program. This program is 100% + Free. + + BSD 2-Clause License + + Copyright (c) 2018, Jared Bruni + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + */ + +#ifndef __TOKENIZE_H__ +#define __TOKENIZE_H__ + +#include +#include +#include + +namespace token { + + template + type substr(type t, size_t start, size_t stop) { + type temp; + for(size_t i = start; i < stop; i++) + temp += t[i]; + return temp; + } + + template<> char* substr(char *t, size_t start, size_t stop) { + char *temp = new char [ stop-start+1 ]; + size_t pos = 0; + for(size_t i = start; i < stop; i++) + temp[pos++] = t[i]; + temp[pos] = 0; + return temp; + } + + template + size_t len(type &t) { + size_t c = 0; + for(c = 0; t[c] != 0; c++); + return c; + } + + template + size_t find(size_t start, type& source, type& sub) { + for(size_t i = start; source[i] != 0; i++) { + bool add = true; + for(size_t z = 0; sub[z] != 0; z++) { + if(source[i+z] != sub[z]) { + add = false; + break; + } + } + if(add == true) + return i; + } + return 0; + } + + template + size_t tokenize(type source, type delim, std::vector &v) { + size_t i = find(0,source,delim),z=0; + if(i == 0) i = find(i+1, source,delim); + size_t lenz = len(source), dlen = len(delim); + while (i != 0 && i < lenz && z < lenz ) { + type s = substr(source,z,i); + if(len(s) > 0 && s[0] != delim[0]) + v.push_back(s); + z = i+dlen; + i = find(i+1, source, delim); + } + if( z < lenz ) v.push_back( substr(source,z,lenz) ); + return v.size(); + } +} + +#endif +