aboutsummaryrefslogtreecommitdiff
path: root/core/src/UI/UI_DatabaseView.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/UI/UI_DatabaseView.cpp')
-rw-r--r--core/src/UI/UI_DatabaseView.cpp222
1 files changed, 145 insertions, 77 deletions
diff --git a/core/src/UI/UI_DatabaseView.cpp b/core/src/UI/UI_DatabaseView.cpp
index 2b74918..8791c5b 100644
--- a/core/src/UI/UI_DatabaseView.cpp
+++ b/core/src/UI/UI_DatabaseView.cpp
@@ -1,5 +1,6 @@
#include "UI.hpp"
+#include "Model/Filter.hpp"
#include "Model/Project.hpp"
#include "UI/Localization.hpp"
#include "UI/States.hpp"
@@ -10,6 +11,7 @@
#include <SQLiteCpp/Statement.h>
#include <imgui.h>
#include <cstdint>
+#include <ctime>
#include <memory>
#include <vector>
@@ -25,6 +27,13 @@ public:
std::string DeliveryTime;
};
+class PurchaseEntry {
+public:
+ std::string Factory;
+ std::string OrderTime;
+ std::string DeliveryTime;
+};
+
class SalesViewTab {
private:
Project* mProject;
@@ -42,38 +51,63 @@ private:
/// A cached, contiguous (row id of each entry is monotonically increasing, but not necessarily starts at 0) list ready-to-be-presented entries. May be incomplete.
std::vector<SaleEntry> mEntries;
- // TODO this is very impractical (running filter in client code instead of letting SQLite doing it)
- // Maybe simply cache a list of indices produced by a sql query?
-
- /// A bitset of all active (should-be-displayed) entries based on current filter, updated whenever \c mActiveFilter is updated.
- /// Should have the exact same size as \c entries.
- std::vector<bool> mActiveEntries;
-
- /// A cached list of index to entries that should be displayed on the current page.
- std::vector<int> mCurrentPageEntries;
+ /// A vector of row ids of entries (in \c mEntries) that are visible under the current filter. To use these indices, the elements should be mapped to \c mEntries
+ /// index by offsetting by \c mFirstCachedRowId.
+ std::vector<int> mActiveEntries;
- /// The current page the user is on.
- int mCurrentPage;
/// Last possible page for the current set table and filter (inclusive).
int mLastPage;
+ /// The current page the user is on.
+ int mCurrentPage;
- /* UI states */
-
- int mSelectedEntry;
+ int mSelectedEntryRowId;
public:
+ Project* GetProject() const {
+ return mProject;
+ }
+
void OnProjectChanged(Project* newProject) {
mProject = newProject;
+ mFirstCachedRowId = 0;
+ mLastCachedRowId = 0;
+
mEntries.clear();
- mCurrentPage = 0;
- mLastPage = -1;
+ mActiveEntries.clear();
+
+ UpdateLastPage();
+ SetPage(0);
- mSelectedEntry = -1;
+ mSelectedEntryRowId = -1;
+ }
+
+ TableRowsFilter* GetFilter() const {
+ return mActiveFilter.get();
+ }
+
+ void OnFilterChanged() {
+ auto& stmt = mProject->GetTransactionsModel().GetSales().FilterRowsStatement;
+ DEFER {
+ stmt.reset();
+ };
+
+ // TODO lazy loading when too many results
+ mActiveEntries.clear();
+ int columnIdx = stmt.getColumnIndex("rowid");
+ while (stmt.executeStep()) {
+ mActiveEntries.push_back(stmt.getColumn(columnIdx).getInt());
+ }
+
+ UpdateLastPage();
+ SetPage(0);
+
+ mSelectedEntryRowId = -1;
}
void OnFilterChanged(std::unique_ptr<TableRowsFilter> filter) {
- // TODO
+ mActiveFilter = std::move(filter);
+ OnFilterChanged();
}
void Draw() {
@@ -94,7 +128,7 @@ public:
}
ImGui::SameLine();
- if (ImGui::Button(ls->Edit.Get(), mSelectedEntry == -1)) {
+ if (ImGui::Button(ls->Edit.Get(), mSelectedEntryRowId == -1)) {
ImGui::OpenPopup(ls->EditSaleEntryDialogTitle.Get());
}
if (ImGui::BeginPopupModal(ls->EditSaleEntryDialogTitle.Get(), &dummy, ImGuiWindowFlags_AlwaysAutoResize)) {
@@ -109,22 +143,17 @@ public:
ImGui::TableSetupColumn(ls->DatabaseDeliveryTimeColumn.Get());
ImGui::TableHeadersRow();
- for (int i : mCurrentPageEntries) {
- auto& entry = mEntries[i];
-
- ImGui::TableNextColumn();
- if (ImGui::Selectable(entry.Customer.c_str(), mSelectedEntry == i)) {
- mSelectedEntry = i;
+ auto [begin, end] = CalcRangeForPage(mCurrentPage);
+ if (mActiveFilter) {
+ end = std::min(end, (int64_t)mActiveEntries.size() - 1);
+ for (int i = begin; i < end; ++i) {
+ int rowId = mActiveEntries[i];
+ DrawEntry(GetEntry(rowId), rowId);
}
-
- ImGui::NextColumn();
- ImGui::Text("%s", entry.Deadline.c_str());
-
- ImGui::NextColumn();
- if (entry.DeliveryTime.empty()) {
- ImGui::Text("%s", ls->NotDeliveredMessage.Get());
- } else {
- ImGui::Text("%s", entry.DeliveryTime.c_str());
+ } else {
+ end = std::min(end, mLastCachedRowId);
+ for (int rowId = begin; rowId < end; ++rowId) {
+ DrawEntry(GetEntry(rowId), rowId);
}
}
@@ -132,23 +161,43 @@ public:
}
}
+ void DrawEntry(const SaleEntry& entry, int rowId) {
+ auto ls = LocaleStrings::Instance.get();
+
+ ImGui::TableNextColumn();
+ if (ImGui::Selectable(entry.Customer.c_str(), mSelectedEntryRowId == rowId, ImGuiSelectableFlags_SpanAllColumns)) {
+ mSelectedEntryRowId = rowId;
+ }
+
+ ImGui::TableNextColumn();
+ ImGui::Text("%s", entry.Deadline.c_str());
+
+ ImGui::TableNextColumn();
+ if (entry.DeliveryTime.empty()) {
+ ImGui::Text("%s", ls->NotDeliveredMessage.Get());
+ } else {
+ ImGui::Text("%s", entry.DeliveryTime.c_str());
+ }
+ }
+
+ const SaleEntry& GetEntry(int64_t rowId) const {
+ return mEntries[RowIdToIndex(rowId)];
+ }
+
+ SaleEntry& GetEntry(int64_t rowId) {
+ return mEntries[RowIdToIndex(rowId)];
+ }
+
private:
void SetPage(int page) {
- if (mCurrentPage != page) return;
-
mCurrentPage = page;
EnsureCacheCoversPage(page);
+ }
- auto [begin, end] = CalcRangeForPage(page);
- begin -= mFirstCachedRowId;
- end -= mFirstCachedRowId;
-
- mCurrentPageEntries.clear();
- for (auto i = begin; i < end; ++i) {
- if (mActiveEntries[i]) {
- mCurrentPageEntries.push_back(i);
- }
- }
+ void UpdateLastPage() {
+ mLastPage = mActiveEntries.empty()
+ ? 0
+ : CalcPageForRowId(mActiveEntries.back());
}
void EnsureCacheCoversPage(int page) {
@@ -166,11 +215,11 @@ private:
bool doRebuild = false;
if (firstRow < mFirstCachedRowId) {
- newFirst = CalcPageForRowId(firstRow) * kMaxEntriesPerPage;
+ newFirst = (CalcPageForRowId(firstRow) + 1) * kMaxEntriesPerPage;
doRebuild = true;
}
if (lastRow > mLastCachedRowId) {
- newLast = CalcPageForRowId(lastRow) * kMaxEntriesPerPage;
+ newLast = (CalcPageForRowId(lastRow) + 1) * kMaxEntriesPerPage;
doRebuild = true;
}
if (!doRebuild) return;
@@ -178,12 +227,12 @@ private:
auto front = LoadRange(newFirst, mFirstCachedRowId);
auto back = LoadRange(mLastCachedRowId + 1, newLast + 1);
- mEntries.insert(mEntries.begin(), front.begin(), front.end());
- mEntries.insert(mEntries.end(), back.begin(), back.end());
- // TODO update mActiveEntries
+ mFirstCachedRowId -= front.size();
+ mLastCachedRowId += back.size();
- mFirstCachedRowId = newFirst;
- mLastCachedRowId = newLast;
+ // TODO don't reallocate buffer/move elements around all the time. Maybe use a linked list of buckets?
+ mEntries.insert(mEntries.begin(), std::make_move_iterator(front.begin()), std::make_move_iterator(front.end()));
+ mEntries.insert(mEntries.end(), std::make_move_iterator(back.begin()), std::make_move_iterator(back.end()));
}
std::vector<SaleEntry> LoadRange(int64_t begin, int64_t end) {
@@ -196,7 +245,7 @@ private:
result.reserve(size);
- auto& stmt = mProject->GetDatabase().GetSales().GetRowsStatement;
+ auto& stmt = mProject->GetTransactionsModel().GetSales().GetRowsStatement;
DEFER {
stmt.reset();
};
@@ -204,34 +253,54 @@ private:
stmt.bind(1, begin);
stmt.bind(2, end);
- while (true) {
- bool hasResult = stmt.executeStep();
- if (hasResult) {
- result.push_back(stmt.getColumns<SaleEntry, 3>());
- } else {
- return result;
+ auto StringifyEpochTime = [](int64_t epoch) -> std::string {
+ if (epoch == 0) {
+ return "";
}
+
+ auto t = static_cast<time_t>(epoch);
+ std::string str(29, '\0');
+ std::strftime(str.data(), 21, "%Y-%m-%dT%H:%M:%S.", std::localtime(&t));
+ return str;
+ };
+
+ int customerCol = stmt.getColumnIndex("Customer");
+ int deadlineCol = stmt.getColumnIndex("Deadline");
+ int deliveryTimeCol = stmt.getColumnIndex("DeliveryTime");
+
+ while (stmt.executeStep()) {
+ auto customer = stmt.getColumn(customerCol).getInt();
+ auto deadline = stmt.getColumn(deadlineCol).getInt64();
+ auto deliveryTime = stmt.getColumn(deliveryTimeCol).getInt64();
+ result.push_back(SaleEntry{
+ .Customer = mProject->Customers.Find(customer)->GetName(),
+ .Deadline = StringifyEpochTime(deadline),
+ .DeliveryTime = StringifyEpochTime(deliveryTime),
+ });
}
+ return result;
+ }
+
+ int RowIdToIndex(int64_t rowId) const {
+ return rowId - mFirstCachedRowId;
+ }
+
+ int64_t IndexToRowId(int index) const {
+ return index + mFirstCachedRowId;
}
static int CalcPageForRowId(int64_t rowId) {
return rowId / kMaxEntriesPerPage;
}
- /// Calculate range [begin, end) of row ids that the path-th page would show.
+ /// Calculate range [begin, end) of index for the list of entries that are currently visible that the path-th page would show.
+ /// i.e. when there is a filter, look into \c mActiveEntryIndices; when there is no filter, use directly.
static std::pair<int64_t, int64_t> CalcRangeForPage(int page) {
int begin = page * kMaxEntriesPerPage;
return { begin, begin + kMaxEntriesPerPage };
}
};
-class PurchaseEntry {
-public:
- std::string Factory;
- std::string OrderTime;
- std::string DeliveryTime;
-};
-
class PurchasesViewTab {
private:
std::vector<PurchaseEntry> mEntries;
@@ -256,26 +325,25 @@ void UI::DatabaseViewTab() {
auto& uis = UIState::GetInstance();
static Project* currentProject = nullptr;
- static auto salesView = std::make_unique<SalesViewTab>();
- static auto purchasesView = std::make_unique<PurchasesViewTab>();
+ static SalesViewTab salesView;
+ static PurchasesViewTab purchasesView;
if (currentProject != uis.CurrentProject.get()) {
currentProject = uis.CurrentProject.get();
- salesView->OnProjectChanged(currentProject);
- purchasesView->OnProjectChanged(currentProject);
+ salesView.OnProjectChanged(currentProject);
+ purchasesView.OnProjectChanged(currentProject);
}
if (ImGui::BeginTabBar("##DatabaseViewTabs")) {
if (ImGui::BeginTabItem(ls->SalesViewTab.Get())) {
- salesView->Draw();
+ salesView.Draw();
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem(ls->PurchasesViewTab.Get())) {
- purchasesView->Draw();
+ purchasesView.Draw();
ImGui::EndTabItem();
}
- // if (ImGui::BeginTabItem(ls->DeliveriesTableTab.Get())) {
- // ImGui::EndTabItem();
- // }
+
+ ImGui::EndTabBar();
}
}