mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-19 08:52:15 -07:00
Add a search bar.
This commit is contained in:
parent
3dab095356
commit
cee2de45d5
5 changed files with 103 additions and 2 deletions
|
|
@ -162,6 +162,7 @@ set(cockatrice_SOURCES
|
|||
src/client/tabs/visual_deck_storage/tab_deck_storage_visual.cpp
|
||||
src/client/ui/widgets/cards/deck_preview_card_picture_widget.cpp
|
||||
src/client/ui/widgets/visual_deck_storage/visual_deck_storage_widget.cpp
|
||||
src/client/ui/widgets/visual_deck_storage/visual_deck_storage_search_widget.cpp
|
||||
${VERSION_STRING_CPP}
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
#include "visual_deck_storage_search_widget.h"
|
||||
|
||||
/**
|
||||
* @brief Constructs a PrintingSelectorCardSearchWidget for searching cards by set name or set code.
|
||||
*
|
||||
* This widget provides a search bar that allows users to search for cards by either their set name
|
||||
* or set code. It uses a debounced timer to trigger the search action after the user stops typing.
|
||||
*
|
||||
* @param parent The parent PrintingSelector widget that will handle the search results.
|
||||
*/
|
||||
VisualDeckStorageSearchWidget::VisualDeckStorageSearchWidget(VisualDeckStorageWidget *parent) : parent(parent)
|
||||
{
|
||||
layout = new QHBoxLayout(this);
|
||||
setLayout(layout);
|
||||
|
||||
searchBar = new QLineEdit(this);
|
||||
searchBar->setPlaceholderText(tr("Search by filename"));
|
||||
layout->addWidget(searchBar);
|
||||
|
||||
// Add a debounce timer for the search bar to limit frequent updates
|
||||
searchDebounceTimer = new QTimer(this);
|
||||
searchDebounceTimer->setSingleShot(true);
|
||||
connect(searchBar, &QLineEdit::textChanged, this, [this]() {
|
||||
searchDebounceTimer->start(300); // 300ms debounce
|
||||
});
|
||||
|
||||
connect(searchDebounceTimer, &QTimer::timeout, parent, &VisualDeckStorageWidget::refreshBannerCards);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Retrieves the current text in the search bar.
|
||||
*
|
||||
* @return The text entered by the user in the search bar.
|
||||
*/
|
||||
QString VisualDeckStorageSearchWidget::getSearchText()
|
||||
{
|
||||
return searchBar->text();
|
||||
}
|
||||
|
||||
QStringList VisualDeckStorageSearchWidget::filterFiles(const QStringList &files, const QString &searchText)
|
||||
{
|
||||
if (searchText.isEmpty() || searchText.isNull()) {
|
||||
return files;
|
||||
}
|
||||
|
||||
QStringList filteredFiles;
|
||||
|
||||
for (const auto &file : files) {
|
||||
QFileInfo fileInfo(file);
|
||||
QString fileName = fileInfo.fileName().toLower();
|
||||
|
||||
if (fileName.contains(searchText.toLower())) {
|
||||
filteredFiles << file;
|
||||
}
|
||||
}
|
||||
|
||||
return filteredFiles;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef VISUAL_DECK_STORAGE_SEARCH_WIDGET_H
|
||||
#define VISUAL_DECK_STORAGE_SEARCH_WIDGET_H
|
||||
|
||||
#include "visual_deck_storage_widget.h"
|
||||
|
||||
#include <QHBoxLayout>
|
||||
#include <QLineEdit>
|
||||
#include <QTimer>
|
||||
#include <QWidget>
|
||||
|
||||
class VisualDeckStorageWidget;
|
||||
class VisualDeckStorageSearchWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit VisualDeckStorageSearchWidget(VisualDeckStorageWidget *parent);
|
||||
QString getSearchText();QStringList filterFiles(const QStringList&files, const QString&searchText);
|
||||
|
||||
private:
|
||||
QHBoxLayout *layout;
|
||||
VisualDeckStorageWidget *parent;
|
||||
QLineEdit *searchBar;
|
||||
QTimer *searchDebounceTimer;
|
||||
};
|
||||
|
||||
#endif // VISUAL_DECK_STORAGE_SEARCH_WIDGET_H
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
#include "visual_deck_storage_widget.h"
|
||||
|
||||
#include "visual_deck_storage_search_widget.h"
|
||||
#include "../../../../deck/deck_loader.h"
|
||||
#include "../../../../game/cards/card_database_manager.h"
|
||||
#include "../../../../settings/cache_settings.h"
|
||||
|
|
@ -21,8 +22,11 @@ VisualDeckStorageWidget::VisualDeckStorageWidget(QWidget *parent) : QWidget(pare
|
|||
sortComboBox->addItem("Sort Alphabetically (Filename)", Alphabetical);
|
||||
sortComboBox->addItem("Sort by Last Modified", ByLastModified);
|
||||
|
||||
searchWidget = new VisualDeckStorageSearchWidget(this);
|
||||
|
||||
// Add combo box to the main layout
|
||||
layout->addWidget(sortComboBox);
|
||||
layout->addWidget(searchWidget);
|
||||
|
||||
flowWidget = new FlowWidget(this, Qt::ScrollBarAlwaysOff, Qt::ScrollBarAsNeeded);
|
||||
layout->addWidget(flowWidget);
|
||||
|
|
@ -32,7 +36,11 @@ VisualDeckStorageWidget::VisualDeckStorageWidget(QWidget *parent) : QWidget(pare
|
|||
sortOrder = static_cast<SortOrder>(sortComboBox->currentData().toInt());
|
||||
refreshBannerCards(); // Refresh the banner cards with the new sort order
|
||||
});
|
||||
}
|
||||
|
||||
void VisualDeckStorageWidget::showEvent(QShowEvent *event)
|
||||
{
|
||||
QWidget::showEvent(event);
|
||||
refreshBannerCards();
|
||||
}
|
||||
|
||||
|
|
@ -73,9 +81,11 @@ void VisualDeckStorageWidget::refreshBannerCards()
|
|||
return false; // Default case
|
||||
});
|
||||
|
||||
auto filteredFiles = searchWidget->filterFiles(allFiles, searchWidget->getSearchText());
|
||||
|
||||
flowWidget->clearLayout(); // Clear existing widgets in the flow layout
|
||||
|
||||
foreach (const QString &file, allFiles) {
|
||||
foreach (const QString &file, filteredFiles) {
|
||||
qDebug() << file;
|
||||
auto deckLoader = new DeckLoader();
|
||||
deckLoader->loadFromFile(file, DeckLoader::CockatriceFormat);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef VISUAL_DECK_STORAGE_WIDGET_H
|
||||
#define VISUAL_DECK_STORAGE_WIDGET_H
|
||||
|
||||
#include "visual_deck_storage_search_widget.h"
|
||||
#include "../../../../deck/deck_list_model.h"
|
||||
#include "../../../../deck/deck_view.h"
|
||||
#include "../../../ui/widgets/cards/deck_preview_card_picture_widget.h"
|
||||
|
|
@ -8,6 +9,7 @@
|
|||
|
||||
#include <QFileSystemModel>
|
||||
|
||||
class VisualDeckStorageSearchWidget;
|
||||
class VisualDeckStorageWidget final : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
@ -18,6 +20,8 @@ public:
|
|||
public slots:
|
||||
void imageClickedEvent(QMouseEvent *event, DeckPreviewCardPictureWidget *instance);
|
||||
void imageDoubleClickedEvent(QMouseEvent *event, DeckPreviewCardPictureWidget *instance);
|
||||
void refreshBannerCards(); // Refresh the display of cards based on the current sorting option
|
||||
void showEvent(QShowEvent *event) override;
|
||||
|
||||
signals:
|
||||
void imageClicked(QMouseEvent *event, DeckPreviewCardPictureWidget *instance);
|
||||
|
|
@ -36,7 +40,7 @@ private:
|
|||
QMap<QString, DeckViewCardContainer *> cardContainers;
|
||||
|
||||
SortOrder sortOrder; // Current sorting option
|
||||
void refreshBannerCards(); // Refresh the display of cards based on the current sorting option
|
||||
VisualDeckStorageSearchWidget *searchWidget;
|
||||
};
|
||||
|
||||
#endif // VISUAL_DECK_STORAGE_WIDGET_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue