|
15 | 15 |
|
16 | 16 | #include <cstddef> |
17 | 17 | #include <openstudio/measure/OSArgument.hpp> |
18 | | - |
19 | 18 | #include <openstudio/utilities/bcl/BCL.hpp> |
20 | 19 | #include <openstudio/utilities/bcl/LocalBCL.hpp> |
21 | 20 | #include <openstudio/utilities/bcl/RemoteBCL.hpp> |
@@ -169,53 +168,90 @@ void BuildingComponentDialogCentralWidget::setTid() { |
169 | 168 | requestComponents(m_filterType, m_tid, m_pageIdx, m_searchString); |
170 | 169 | } |
171 | 170 |
|
| 171 | +std::vector<openstudio::BCLSearchResult> BuildingComponentDialogCentralWidget::fetchAndSortResponses(const std::string& filterType, int tid, const QString& searchString) { |
| 172 | + m_allResponses.clear(); |
| 173 | + |
| 174 | + RemoteBCL remoteBCL; |
| 175 | + remoteBCL.setTimeOutSeconds(m_timeoutSeconds); |
| 176 | + |
| 177 | + std::vector<BCLSearchResult> responses; |
| 178 | + int totalPages = 1; |
| 179 | + int currentPage = 0; |
| 180 | + |
| 181 | + // Collect all responses from all pages |
| 182 | + do { |
| 183 | + std::vector<BCLSearchResult> pageResponses; |
| 184 | + if (filterType == "components") { |
| 185 | + pageResponses = remoteBCL.searchComponentLibrary(searchString.toStdString(), tid, currentPage); |
| 186 | + } else if (filterType == "measures") { |
| 187 | + pageResponses = remoteBCL.searchMeasureLibrary(searchString.toStdString(), tid, currentPage); |
| 188 | + } |
| 189 | + responses.insert(responses.end(), pageResponses.begin(), pageResponses.end()); |
| 190 | + totalPages = remoteBCL.numResultPages(); |
| 191 | + } while (++currentPage < totalPages); |
| 192 | + |
| 193 | + if (!responses.empty()) { |
| 194 | + std::sort(responses.begin(), responses.end(), [](const BCLSearchResult& a, const BCLSearchResult& b) { |
| 195 | + return a.name() < b.name(); |
| 196 | + }); |
| 197 | + } |
| 198 | + |
| 199 | + return responses; |
| 200 | +} |
| 201 | + |
172 | 202 | // Note: don't call this directly if the "wait" screen is desired |
173 | 203 | void BuildingComponentDialogCentralWidget::setTid(const std::string& filterType, int tid, int pageIdx, const QString& title, |
174 | 204 | const QString& searchString) { |
175 | 205 |
|
176 | | - if (m_tid != tid || m_searchString != searchString) { |
177 | | - m_collapsibleComponentList->firstPage(); |
178 | | - } |
| 206 | + std::string newKey = std::to_string(tid) + filterType + searchString.toStdString(); |
| 207 | + std::string currentKey = std::to_string(m_tid) + m_filterType + m_searchString.toStdString(); |
179 | 208 |
|
| 209 | + m_searchString = searchString; |
180 | 210 | m_filterType = filterType; |
181 | | - |
182 | 211 | m_tid = tid; |
183 | 212 |
|
184 | | - m_searchString = searchString; |
| 213 | + if (newKey != currentKey) { |
| 214 | + m_allResponses = fetchAndSortResponses(filterType, tid, searchString); |
| 215 | + m_collapsibleComponentList->firstPage(); |
| 216 | + pageIdx = 0; |
| 217 | + } |
185 | 218 |
|
186 | | - //std::vector<Component *> components = m_collapsibleComponentList->components(); |
| 219 | + // Clear existing components |
187 | 220 | std::vector<Component*> components = m_componentList->components(); // TODO replace with code above |
188 | | - |
189 | 221 | for (auto& comp : components) { |
190 | 222 | delete comp; |
191 | 223 | } |
192 | 224 |
|
193 | | - RemoteBCL remoteBCL; |
194 | | - remoteBCL.setTimeOutSeconds(m_timeoutSeconds); |
195 | | - std::vector<BCLSearchResult> responses; |
196 | | - if (filterType == "components") { |
197 | | - responses = remoteBCL.searchComponentLibrary(searchString.toStdString(), tid, pageIdx); |
198 | | - } else if (filterType == "measures") { |
199 | | - responses = remoteBCL.searchMeasureLibrary(searchString.toStdString(), tid, pageIdx); |
200 | | - } |
201 | | - |
202 | | - for (const auto& response : responses) { |
203 | | - auto* component = new Component(response); |
204 | | - |
205 | | - // TODO replace with a componentList owned by m_collapsibleComponentList |
206 | | - m_componentList->addComponent(component); |
| 225 | + // Paginate responses |
| 226 | + int itemsPerPage = 10; // Assuming 10 items per page |
| 227 | + |
| 228 | + if (!m_allResponses.empty()) { |
| 229 | + size_t startIdx = pageIdx * itemsPerPage; |
| 230 | + size_t endIdx = std::min(startIdx + itemsPerPage, m_allResponses.size()); |
| 231 | + std::vector<BCLSearchResult> paginatedResponses(m_allResponses.begin() + startIdx, m_allResponses.begin() + endIdx); |
| 232 | + |
| 233 | + for (const auto& response : paginatedResponses) { |
| 234 | + auto* component = new Component(response); |
| 235 | + |
| 236 | + // TODO replace with a componentList owned by m_collapsibleComponentList |
| 237 | + m_componentList->addComponent(component); |
| 238 | + } |
207 | 239 | } |
208 | 240 |
|
209 | 241 | // the parent taxonomy |
210 | 242 | m_collapsibleComponentList->setText(title); |
211 | 243 |
|
212 | 244 | // the total number of results |
213 | | - int lastTotalResults = remoteBCL.lastTotalResults(); |
| 245 | + int lastTotalResults = m_allResponses.size(); |
214 | 246 | m_collapsibleComponentList->setNumResults(lastTotalResults); |
215 | 247 |
|
216 | 248 | // the number of pages of results |
217 | | - int numResultPages = remoteBCL.numResultPages(); |
218 | | - m_collapsibleComponentList->setNumPages(numResultPages); |
| 249 | + if (lastTotalResults == 0) { |
| 250 | + m_collapsibleComponentList->setNumPages(0); |
| 251 | + } else { |
| 252 | + int numResultPages = (lastTotalResults % itemsPerPage == 0) ? (lastTotalResults / itemsPerPage) : (lastTotalResults / itemsPerPage) + 1; |
| 253 | + m_collapsibleComponentList->setNumPages(numResultPages); |
| 254 | + } |
219 | 255 |
|
220 | 256 | // make sure the header is expanded |
221 | 257 | if (m_collapsibleComponentList->checkedCollapsibleComponent()) { |
|
0 commit comments