listmodel.h (5021B)
1 #pragma once 2 3 #include "editable.h" 4 5 #include "jucePluginLib/patchdb/patchdbtypes.h" 6 7 #include "juceUiLib/messageBox.h" 8 9 #include "juce_gui_basics/juce_gui_basics.h" 10 11 namespace pluginLib 12 { 13 class FileType; 14 } 15 16 namespace pluginLib::patchDB 17 { 18 struct SearchRequest; 19 struct PatchKey; 20 struct Search; 21 } 22 23 namespace jucePluginEditorLib::patchManager 24 { 25 class PatchManager; 26 27 class ListModel : public juce::ListBoxModel, Editable, public juce::DragAndDropTarget, public juce::FileDragAndDropTarget 28 { 29 public: 30 using Patch = pluginLib::patchDB::PatchPtr; 31 using Patches = std::vector<Patch>; 32 33 explicit ListModel(PatchManager& _pm); 34 35 void setContent(const pluginLib::patchDB::SearchHandle& _handle); 36 void setContent(pluginLib::patchDB::SearchRequest&& _request); 37 void clear(); 38 39 void refreshContent(); 40 41 // ListBoxModel 42 int getNumRows() override; 43 void paintListBoxItem(int _rowNumber, juce::Graphics& _g, int _width, int _height, bool _rowIsSelected) override; 44 juce::var getDragSourceDescription(const juce::SparseSet<int>& rowsToDescribe) override; 45 juce::Component* refreshComponentForRow(int rowNumber, bool isRowSelected, juce::Component* existingComponentToUpdate) override; 46 47 void selectedRowsChanged(int lastRowSelected) override; 48 49 const Patches& getPatches() const 50 { 51 if (m_filter.empty() && !m_hideDuplicatesByHash && !m_hideDuplicatesByName) 52 return m_patches; 53 return m_filteredPatches; 54 } 55 56 Patch getPatch(const size_t _index) const 57 { 58 return getPatch(getPatches(), _index); 59 } 60 61 std::set<Patch> getSelectedPatches() const; 62 bool setSelectedPatches(const std::set<Patch>& _patches); 63 bool setSelectedPatches(const std::set<pluginLib::patchDB::PatchKey>& _patches); 64 65 void activateSelectedPatch() const; 66 67 void processDirty(const pluginLib::patchDB::Dirty& _dirty); 68 69 pluginLib::patchDB::DataSourceNodePtr getDataSource() const; 70 71 static Patch getPatch(const Patches& _patches, const size_t _index) 72 { 73 if (_index >= _patches.size()) 74 return {}; 75 return _patches[_index]; 76 } 77 78 void setFilter(const std::string& _filter); 79 void setFilter(const std::string& _filter, bool _hideDuplicatesByHash, bool _hideDuplicatesByName); 80 81 PatchManager& getPatchManager() const 82 { 83 return m_patchManager; 84 } 85 86 static void sortPatches(Patches& _patches, pluginLib::patchDB::SourceType _sourceType); 87 void listBoxItemClicked(int _row, const juce::MouseEvent&) override; 88 void backgroundClicked(const juce::MouseEvent&) override; 89 90 static void showDeleteConfirmationMessageBox(genericUI::MessageBox::Callback _callback); 91 pluginLib::patchDB::SourceType getSourceType() const; 92 bool canReorderPatches() const; 93 bool hasTagFilters() const; 94 bool hasFilters() const; 95 96 pluginLib::patchDB::SearchHandle getSearchHandle() const; 97 98 template<typename T> 99 static std::set<T> toSet(const juce::SparseSet<int>& _sparseSet) 100 { 101 std::set<T> result; 102 103 const auto& ranges = _sparseSet.getRanges(); 104 105 for (const auto& range : ranges) 106 { 107 for (int i = range.getStart(); i < range.getEnd(); ++i) 108 result.insert(static_cast<T>(i)); 109 } 110 return result; 111 } 112 113 template<typename T> 114 static juce::SparseSet<int> toSparseSet(const std::set<T>& _set) 115 { 116 juce::SparseSet<int> result; 117 for (auto i : _set) 118 { 119 const auto ii = static_cast<int>(i); 120 result.addRange({ii, ii+1}); 121 } 122 return result; 123 } 124 125 // to be implemented in derived class 126 virtual juce::Colour findColor(int _colorId) = 0; 127 virtual const juce::LookAndFeel& getStyle() const = 0; 128 virtual void onModelChanged() = 0; 129 virtual void redraw() = 0; 130 virtual void ensureVisible(int _row) = 0; 131 virtual int getSelectedEntry() const = 0; 132 virtual juce::SparseSet<int> getSelectedEntries() const = 0; 133 virtual void deselectAll() = 0; 134 virtual void setSelectedEntries(const juce::SparseSet<int>&) = 0; 135 virtual juce::Rectangle<int> getEntryPosition(int _row, bool _relativeToComponentTopLeft) = 0; 136 137 // drag & drop support 138 bool isInterestedInDragSource(const SourceDetails& dragSourceDetails) override; 139 void itemDropped(const SourceDetails& dragSourceDetails) override; 140 bool isInterestedInFileDrag(const juce::StringArray& files) override; 141 void filesDropped(const juce::StringArray& files, int x, int y) override; 142 143 private: 144 void sortPatches(); 145 void sortPatches(Patches& _patches) const; 146 void filterPatches(); 147 bool match(const Patch& _patch) const; 148 void setContent(const std::shared_ptr<pluginLib::patchDB::Search>& _search); 149 bool exportPresets(bool _selectedOnly, const pluginLib::FileType& _fileType) const; 150 bool onClicked(const juce::MouseEvent&); 151 void cancelSearch(); 152 153 PatchManager& m_patchManager; 154 155 std::shared_ptr<pluginLib::patchDB::Search> m_search; 156 Patches m_patches; 157 Patches m_filteredPatches; 158 std::string m_filter; 159 bool m_hideDuplicatesByHash = false; 160 bool m_hideDuplicatesByName = false; 161 pluginLib::patchDB::SearchHandle m_searchHandle = pluginLib::patchDB::g_invalidSearchHandle; 162 bool m_ignoreSelectedRowsChanged = false; 163 }; 164 }