Mercurial > minori
diff src/gui/pages/search.cc @ 273:f31305b9f60a
*: various code safety changes
this also makes the code build on Qt 5.7. I can't test it though
because I don't have it working... FAIL!
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Thu, 18 Apr 2024 16:53:17 -0400 |
parents | 862d0d8619f6 |
children | 657fda1b9cac |
line wrap: on
line diff
--- a/src/gui/pages/search.cc Thu Apr 18 16:51:35 2024 -0400 +++ b/src/gui/pages/search.cc Thu Apr 18 16:53:17 2024 -0400 @@ -26,6 +26,17 @@ #include "anitomy/anitomy.h" #include "pugixml.hpp" +SearchPageSearchThread::SearchPageSearchThread(QObject* parent) : QThread(parent) { +} + +void SearchPageSearchThread::SetSearch(const std::string& search) { + search_ = search; +} + +void SearchPageSearchThread::run() { + emit GotResults(Services::Search(search_)); +} + SearchPageListSortFilter::SearchPageListSortFilter(QObject* parent) : QSortFilterProxyModel(parent) { } @@ -139,7 +150,7 @@ const QString d = data(index, Qt::DisplayRole).toString(); const QFontMetrics metric = QFontMetrics(QFont()); - return QSize(std::max(metric.horizontalAdvance(d), 100), metric.height()); + return QSize(std::max(metric.boundingRect(d).width(), 100), metric.height()); } } break; @@ -282,21 +293,15 @@ QLineEdit* line_edit = new QLineEdit("", toolbar); connect(line_edit, &QLineEdit::returnPressed, this, [this, line_edit] { /* static thread here. */ - static QThread* thread = nullptr; + if (thread_.isRunning()) + thread_.exit(1); /* fail */ - if (thread) - return; + thread_.SetSearch(Strings::ToUtf8String(line_edit->text())); - thread = QThread::create([this, line_edit] { - model->ParseSearch(Services::Search(Strings::ToUtf8String(line_edit->text()))); - }); - - connect(thread, &QThread::finished, this, [] { - thread->deleteLater(); - thread = nullptr; - }); - - thread->start(); + thread_.start(); + }); + connect(&thread_, &SearchPageSearchThread::GotResults, this, [this](const std::vector<int>& search) { + model->ParseSearch(search); }); toolbar->addWidget(line_edit); }