comparison 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
comparison
equal deleted inserted replaced
272:5437009cb10e 273:f31305b9f60a
24 #include <sstream> 24 #include <sstream>
25 25
26 #include "anitomy/anitomy.h" 26 #include "anitomy/anitomy.h"
27 #include "pugixml.hpp" 27 #include "pugixml.hpp"
28 28
29 SearchPageSearchThread::SearchPageSearchThread(QObject* parent) : QThread(parent) {
30 }
31
32 void SearchPageSearchThread::SetSearch(const std::string& search) {
33 search_ = search;
34 }
35
36 void SearchPageSearchThread::run() {
37 emit GotResults(Services::Search(search_));
38 }
39
29 SearchPageListSortFilter::SearchPageListSortFilter(QObject* parent) : QSortFilterProxyModel(parent) { 40 SearchPageListSortFilter::SearchPageListSortFilter(QObject* parent) : QSortFilterProxyModel(parent) {
30 } 41 }
31 42
32 bool SearchPageListSortFilter::lessThan(const QModelIndex& l, const QModelIndex& r) const { 43 bool SearchPageListSortFilter::lessThan(const QModelIndex& l, const QModelIndex& r) const {
33 QVariant left = sourceModel()->data(l, sortRole()); 44 QVariant left = sourceModel()->data(l, sortRole());
137 default: { 148 default: {
138 /* max horizontal size of 100, height size = size of current font */ 149 /* max horizontal size of 100, height size = size of current font */
139 const QString d = data(index, Qt::DisplayRole).toString(); 150 const QString d = data(index, Qt::DisplayRole).toString();
140 const QFontMetrics metric = QFontMetrics(QFont()); 151 const QFontMetrics metric = QFontMetrics(QFont());
141 152
142 return QSize(std::max(metric.horizontalAdvance(d), 100), metric.height()); 153 return QSize(std::max(metric.boundingRect(d).width(), 100), metric.height());
143 } 154 }
144 } 155 }
145 break; 156 break;
146 } 157 }
147 case Qt::TextAlignmentRole: return headerData(index.column(), Qt::Horizontal, Qt::TextAlignmentRole); 158 case Qt::TextAlignmentRole: return headerData(index.column(), Qt::Horizontal, Qt::TextAlignmentRole);
280 291
281 { 292 {
282 QLineEdit* line_edit = new QLineEdit("", toolbar); 293 QLineEdit* line_edit = new QLineEdit("", toolbar);
283 connect(line_edit, &QLineEdit::returnPressed, this, [this, line_edit] { 294 connect(line_edit, &QLineEdit::returnPressed, this, [this, line_edit] {
284 /* static thread here. */ 295 /* static thread here. */
285 static QThread* thread = nullptr; 296 if (thread_.isRunning())
286 297 thread_.exit(1); /* fail */
287 if (thread) 298
288 return; 299 thread_.SetSearch(Strings::ToUtf8String(line_edit->text()));
289 300
290 thread = QThread::create([this, line_edit] { 301 thread_.start();
291 model->ParseSearch(Services::Search(Strings::ToUtf8String(line_edit->text()))); 302 });
292 }); 303 connect(&thread_, &SearchPageSearchThread::GotResults, this, [this](const std::vector<int>& search) {
293 304 model->ParseSearch(search);
294 connect(thread, &QThread::finished, this, [] {
295 thread->deleteLater();
296 thread = nullptr;
297 });
298
299 thread->start();
300 }); 305 });
301 toolbar->addWidget(line_edit); 306 toolbar->addWidget(line_edit);
302 } 307 }
303 308
304 layout->addWidget(toolbar); 309 layout->addWidget(toolbar);