This repository has been archived by the owner on Oct 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMainFrame.cpp
288 lines (228 loc) · 9.48 KB
/
MainFrame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#pragma GCC diagnostic ignored "-Wwrite-strings"
#include "MainFrame.h"
#include "ArchiveExtractor.h"
#include "FileEntry.h"
#include "ImageViewPanel.h"
#include "ZipEntry.h"
#include "res/appicon.xpm"
#include <algorithm>
#include <wx/button.h>
#include <wx/log.h>
#include <wx/mstream.h>
enum MainFrameIds { ID_DIRECTORY_TREE = 1, ID_IMAGE_BUTTON };
MainFrame::MainFrame() : wxFrame(NULL, wxID_ANY, "ZipPicView") {
auto panel = new wxPanel(this);
auto outerSizer = new wxBoxSizer(wxVERTICAL);
panel->SetSizer(outerSizer);
auto toolSizer = new wxBoxSizer(wxHORIZONTAL);
onTopChk = new wxCheckBox(panel, wxID_ANY, "On Top");
onTopChk->Bind(wxEVT_CHECKBOX, &MainFrame::OnOnTopChecked, this);
onTopChk->SetToolTip("Force the application to stay on top.");
notebook = new wxNotebook(panel, wxID_ANY);
currentFileCtrl =
new wxTextCtrl(panel, wxID_ANY, wxEmptyString, wxDefaultPosition,
wxDefaultSize, wxTE_READONLY);
dirBrowseBtn = new wxButton(panel, wxID_ANY, "Directory...");
dirBrowseBtn->Bind(wxEVT_BUTTON, &MainFrame::OnDirBrowsePressed, this);
dirBrowseBtn->SetToolTip("Load images from a directory.");
zipBrowseBtn = new wxButton(panel, wxID_ANY, "Archive...");
zipBrowseBtn->Bind(wxEVT_BUTTON, &MainFrame::OnZipBrowsePressed, this);
zipBrowseBtn->SetToolTip("Load images from a zip file.");
toolSizer->Add(currentFileCtrl, 1, wxEXPAND);
toolSizer->Add(dirBrowseBtn, 0, wxEXPAND | wxLEFT | wxALIGN_BOTTOM, 5);
toolSizer->Add(zipBrowseBtn, 0, wxEXPAND | wxLEFT | wxALIGN_BOTTOM, 5);
toolSizer->Add(onTopChk, 0, wxEXPAND | wxLEFT | wxALIGN_BOTTOM, 5);
auto statusBar = CreateStatusBar();
statusBar->SetStatusText("Idle");
outerSizer->Add(toolSizer, 0, wxEXPAND | wxALL, 5);
outerSizer->Add(notebook, 1, wxEXPAND | wxALL, 5);
splitter = new wxSplitterWindow(notebook, wxID_ANY);
splitter->Bind(wxEVT_SPLITTER_DOUBLECLICKED,
[](wxSplitterEvent &event) { event.Veto(); }, wxID_ANY);
dirTree =
new wxTreeCtrl(splitter, ID_DIRECTORY_TREE, wxDefaultPosition,
wxDefaultSize, wxTR_SINGLE | wxTR_FULL_ROW_HIGHLIGHT);
dirTree->Bind(wxEVT_TREE_SEL_CHANGED, &MainFrame::OnTreeSelectionChanged,
this, ID_DIRECTORY_TREE);
dirTree->Bind(wxEVT_TREE_ITEM_COLLAPSING,
[=](wxTreeEvent &event) { event.Veto(); }, ID_DIRECTORY_TREE);
dirTree->SetMinSize({250, 250});
auto rightWindow = new wxScrolledWindow(splitter, wxID_ANY);
auto grid = new wxGridSizer(5);
rightWindow->SetSizer(grid);
rightWindow->SetScrollRate(10, 10);
rightWindow->SetMinSize({250, 250});
rightWindow->SetWindowStyle(wxBORDER_SIMPLE);
rightWindow->Bind(wxEVT_SIZE, &MainFrame::OnGridPanelSize, this);
splitter->SplitVertically(dirTree, rightWindow, 250);
notebook->AddPage(splitter, "Browse");
Bind(wxEVT_COMMAND_THMBTREAD_UPDATE, &MainFrame::OnThumbnailLoadUpdated,
this);
Bind(wxEVT_COMMAND_THMBTREAD_DONE, &MainFrame::OnThumbnailLoadDone, this);
SetMinSize({700, 480});
SetSize({700, 480});
SetIcons(wxICON(IDI_ICON1));
SetWindowStyle(wxRESIZE_BORDER | wxDEFAULT_FRAME_STYLE);
}
void MainFrame::BuildDirectoryTree() {
auto root = dirTree->AddRoot(currentEntry->Name(), -1, -1,
new EntryItemData(currentEntry.get()));
AddTreeItemsFromEntry(root, currentEntry.get());
}
void MainFrame::AddTreeItemsFromEntry(const wxTreeItemId &itemId,
Entry *entry) {
for (auto childEntry : *entry) {
if (!childEntry->IsDirectory())
return;
auto child = dirTree->AppendItem(itemId, childEntry->Name(), -1, -1,
new EntryItemData(childEntry));
AddTreeItemsFromEntry(child, childEntry);
}
}
void MainFrame::OnTreeSelectionChanged(wxTreeEvent &event) {
auto treeItemId = event.GetItem();
auto rootId = dirTree->GetRootItem();
auto currentFileEntry =
dynamic_cast<EntryItemData *>(dirTree->GetItemData(treeItemId))->Get();
auto gridPanel = dynamic_cast<wxScrolledWindow *>(splitter->GetWindow2());
if (loadThread) {
loadThread->Delete(nullptr, wxTHREAD_WAIT_BLOCK);
loadThread = nullptr;
}
gridPanel->Show(false);
auto grid = gridPanel->GetSizer();
grid->Clear(true);
std::vector<Entry *> loadEntries;
imgButtons.clear();
for (int i = 0; i < currentFileEntry->Count(); ++i) {
Entry *childEntry = (*currentFileEntry)[i];
if (childEntry->IsDirectory())
continue;
auto ext = childEntry->Name().AfterLast('.').Lower();
if (ext != "jpg" && ext != "jpeg" && ext != "png" && ext != "gif")
continue;
loadEntries.push_back(childEntry);
}
for (auto entry : loadEntries) {
auto button = new wxButton(gridPanel, wxID_ANY, "", wxDefaultPosition,
wxDefaultSize, wxBU_EXACTFIT);
imgButtons.push_back(button);
button->Bind(wxEVT_BUTTON, &MainFrame::OnImageButtonClick, this);
button->SetClientObject(new EntryItemData(entry));
button->SetMinSize({250, 250});
auto staticText = new wxStaticText(gridPanel, wxID_ANY, entry->Name());
staticText->SetMaxSize({250, 50});
auto btnSizer = new wxBoxSizer(wxVERTICAL);
btnSizer->Add(button, 0, wxEXPAND);
btnSizer->Add(staticText);
grid->Add(btnSizer, 0, wxALL | wxEXPAND, 5);
}
GetStatusBar()->SetStatusText(wxString::Format("Loading Thumbnail %i of %i",
1, (int)loadEntries.size()));
grid->FitInside(gridPanel);
gridPanel->Show(true);
gridPanel->Scroll(0, 0);
gridPanel->Refresh();
gridPanel->Update();
loadThread = new ThumbnailLoadThread(this, loadEntries, currentEntry);
loadThread->Run();
threadId = loadThread->GetId();
}
void MainFrame::OnImageButtonClick(wxCommandEvent &event) {
wxProgressDialog progress("Loading", "Now Loading", 4, this);
auto button = dynamic_cast<wxButton *>(event.GetEventObject());
auto clientData =
dynamic_cast<wxStringClientData *>(button->GetClientObject());
auto page = notebook->GetPageCount();
progress.Update(1);
auto childEntry =
dynamic_cast<EntryItemData *>(button->GetClientObject())->Get();
progress.Update(2);
auto bitmapCtl = new ImageViewPanel(notebook, childEntry);
progress.Update(3);
notebook->AddPage(bitmapCtl, childEntry->Name());
progress.Update(4);
bitmapCtl->SwitchToTheActualImage();
notebook->SetSelection(page);
}
void MainFrame::OnGridPanelSize(wxSizeEvent &event) {
auto grid = dynamic_cast<wxGridSizer *>(splitter->GetWindow2()->GetSizer());
auto size = event.GetSize();
int col = (size.GetWidth() / 250);
grid->SetCols(col > 0 ? col : 1);
grid->FitInside(splitter->GetWindow2());
splitter->Refresh();
splitter->Update();
}
void MainFrame::OnOnTopChecked(wxCommandEvent &event) {
auto style = GetWindowStyle();
if (onTopChk->IsChecked()) {
style += wxSTAY_ON_TOP;
} else {
style -= wxSTAY_ON_TOP;
}
SetWindowStyle(style);
}
void MainFrame::OnDirBrowsePressed(wxCommandEvent &event) {
wxDirDialog dlg(this, "Choose directory", filename.GetPath(),
wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
if (dlg.ShowModal() == wxID_CANCEL)
return;
wxFileName filename = wxFileName::DirName(dlg.GetPath());
LoadEntryFromFile<FileEntry>(filename);
}
void MainFrame::OnZipBrowsePressed(wxCommandEvent &event) {
wxFileDialog dialog(this, _("Open Archive file"), "", "",
"Archive Files|*.rar;*.zip;*.7z|ZIP files"
"(*.zip)|*.zip|RAR files (*.rar)|*.rar|7z files "
"(*.7z)|*.7z|All Files| *.*",
wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (dialog.ShowModal() == wxID_CANCEL)
return;
auto path = dialog.GetPath();
filename = path;
if (filename.GetExt() == "zip")
LoadEntryFromFile<ZipEntry>(filename);
else
LoadEntryFromFile<ArchiveExtractor>(filename);
}
template <class T>
void MainFrame::LoadEntryFromFile(const wxFileName &filename) {
wxProgressDialog progress("Loading", "Now Loading", 4, this,
wxPD_CAN_ABORT | wxPD_AUTO_HIDE | wxPD_APP_MODAL);
auto entry = T::Create(filename, [&progress]() { return progress.Pulse(); });
if (entry == nullptr) {
// wxMessageBox("Failed to load file", "Error", wxICON_ERROR);
return;
}
SetEntry(std::shared_ptr<Entry>(entry));
currentFileCtrl->SetValue(filename.GetFullPath());
}
void MainFrame::SetEntry(std::shared_ptr<Entry> entry) {
MainFrame::currentEntry = entry;
dirTree->DeleteAllItems();
BuildDirectoryTree();
dirTree->ExpandAll();
dirTree->UnselectAll();
dirTree->SelectItem(dirTree->GetRootItem());
while (notebook->GetPageCount() > 1) {
notebook->DeletePage(1);
}
}
void MainFrame::OnThumbnailLoadUpdated(wxThreadEvent &event) {
auto data = event.GetPayload<ThumbnailData>();
if (data.index >= data.total)
return;
if (data.id != threadId)
return;
GetStatusBar()->SetStatusText(wxString::Format("Loading Thumbnail %i of %i",
data.index + 1, data.total));
if (data.index < imgButtons.size())
imgButtons[data.index]->SetBitmap(data.image);
}
void MainFrame::OnThumbnailLoadDone(wxThreadEvent &event) {
if (event.GetExtraLong() != threadId)
return;
GetStatusBar()->SetStatusText("Idle");
loadThread = nullptr;
}