BookShelf.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include "BookList.hpp"
  3. #include "BookTile.hpp"
  4. #include <gtkmm/scrolledwindow.h>
  5. #include <gtkmm/gridview.h>
  6. #include <gtkmm/multiselection.h>
  7. #include <gtkmm/signallistitemfactory.h>
  8. #include <gtkmm/gestureclick.h>
  9. #include <gtkmm/eventcontrollermotion.h>
  10. #include <giomm/liststore.h>
  11. /**
  12. * @brief GridView-based widget that renders and filters the user's library.
  13. *
  14. * BookShelf mirrors the books held in @ref BookList into a Gio::ListStore,
  15. * drives a @ref Gtk::GridView factory to build @ref BookTile instances,
  16. * and exposes a fuzzy filtering API plus activation signal for the rest
  17. * of the UI.
  18. */
  19. class BookShelf : public Gtk::ScrolledWindow {
  20. public:
  21. explicit BookShelf(BookList& bookList, int coverSize = 128);
  22. // force rebuild (e.g., on reset)
  23. /// Recompute the visible grid contents from the backing model.
  24. void reload();
  25. // signal when a tile is activated (double-clicked)
  26. sigc::signal<void(const Book&)>& signalBookActivated() { return m_signalBookActivated; }
  27. // signal when selection changes
  28. sigc::signal<void()>& signalSelectionChanged() { return m_signalSelectionChanged; }
  29. /// Apply a fuzzy search query (empty string restores the full collection).
  30. void setFilter(const std::string& query);
  31. const std::string& filter() const noexcept { return m_filter; }
  32. bool empty() const noexcept;
  33. /// Get the currently selected book (first if multiple), if any
  34. std::optional<Book> getSelectedBook() const;
  35. /// Get all selected books
  36. std::vector<Book> getSelectedBooks() const;
  37. /// Get count of selected books
  38. std::size_t getSelectionCount() const;
  39. /// Clear the current selection
  40. void clearSelection();
  41. /// Select all visible books
  42. void selectAll();
  43. private:
  44. // signal handlers
  45. void onBookAdded(const Book& b);
  46. void onBookUpdated(const Book& b);
  47. void onBookRemoved(const std::string& id);
  48. void onReset();
  49. // helpers
  50. void addTile(const Book&);
  51. void updateTile(const Book&);
  52. void removeTile(const std::string&);
  53. void onFactorySetup(const Glib::RefPtr<Gtk::ListItem>& item);
  54. void onFactoryBind(const Glib::RefPtr<Gtk::ListItem>& item);
  55. void onFactoryUnbind(const Glib::RefPtr<Gtk::ListItem>& item);
  56. void refreshVisible();
  57. class BookObject;
  58. BookList& m_bookList;
  59. int m_coverSize = 128;
  60. Glib::RefPtr<Gio::ListStore<BookObject>> m_store;
  61. Glib::RefPtr<Gtk::MultiSelection> m_selection;
  62. Gtk::GridView m_grid;
  63. Glib::RefPtr<Gtk::GestureClick> m_backgroundClick;
  64. std::string m_filter;
  65. sigc::signal<void(const Book&)> m_signalBookActivated;
  66. sigc::signal<void()> m_signalSelectionChanged;
  67. };