LinuxPack/MainPanel.cpp

154 lines
5.9 KiB
C++
Raw Normal View History

#include "MainPanel.h"
CMainPanel::CMainPanel(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name)
: wxPanel(parent, id, pos, size, style, name)
{
2024-08-06 10:18:09 +08:00
top_sizer_ = new wxBoxSizer(wxVERTICAL);
handle_ = new CFunctionImp();
InitPanel();
}
void CMainPanel::InitPanel()
{
2024-08-06 10:18:09 +08:00
// 创建 wxStaticBoxSizer,并设置标题
base_box_sizer_ = new wxStaticBoxSizer(wxVERTICAL, this, wxT("一、基本信息"));
2024-08-06 10:18:09 +08:00
// 创建水平 sizer 来放置控件
sizer_select_ = new wxBoxSizer(wxHORIZONTAL);
2024-08-06 10:18:09 +08:00
// 创建控件,并将其父级设置为正确的窗口(即 `this`,代表主窗口或面板)
label_select_ = new wxStaticText(this, wxID_ANY, wxT("要打包的二进制文件:"));
text_select_ctrl_ = new wxTextCtrl(this, wxID_ANY);
text_select_ctrl_->SetMinSize(wxSize(300, -1));
2024-08-06 10:18:09 +08:00
btn_select_ = new wxButton(this, wxID_ANY, wxT("选择文件"));
btn_select_->Bind(wxEVT_BUTTON, &CMainPanel::selectBinaryFile, this);
2024-08-06 10:18:09 +08:00
// 将控件添加到水平 sizer 中
sizer_select_->Add(text_select_ctrl_, 1, wxALL | wxEXPAND, g_Border);
sizer_select_->Add(btn_select_, 0, wxALL | wxALIGN_LEFT, g_Border);
// 将水平 sizer 添加到带标题的 sizer 中, wxALIGN_CENTER_VERTICAL 仅水平可用
base_box_sizer_->Add(label_select_, 0, wxALL | wxEXPAND, g_Border);
2024-08-06 10:18:09 +08:00
base_box_sizer_->Add(sizer_select_, 0, wxALL | wxEXPAND, g_Border);
sizer_output_ = new wxBoxSizer(wxHORIZONTAL);
btn_output_ = new wxButton(this, wxID_ANY, wxT("选择目录"));
btn_output_->Bind(wxEVT_BUTTON, &CMainPanel::selectDir, this);
2024-08-06 10:18:09 +08:00
text_output_ctrl_ = new wxTextCtrl(this, wxID_ANY);
label_output_ = new wxStaticText(this, wxID_ANY, wxT("输出目录:"));
base_box_sizer_->Add(label_output_, 0, wxALL| wxEXPAND, g_Border);
2024-08-06 10:18:09 +08:00
sizer_output_->Add(text_output_ctrl_, 1, wxALL | wxEXPAND, g_Border);
2024-08-06 10:18:09 +08:00
sizer_output_->Add(btn_output_, 0, wxALL, g_Border);
base_box_sizer_->Add(sizer_output_, 0, wxALL | wxEXPAND, g_Border);
env_box_sizer_ = new wxStaticBoxSizer(wxVERTICAL, this, wxT("二、搜索库范围"));
env_list_ = new wxListBox(this, wxID_ANY);
env_oper_sizer_ = new wxBoxSizer(wxHORIZONTAL);
btn_add_env_ = new wxButton(this, wxID_ANY, wxT("添加"));
btn_del_env_ = new wxButton(this, wxID_ANY, wxT("删除"));
btn_add_env_->Bind(wxEVT_BUTTON, &CMainPanel::addEnv, this);
btn_del_env_->Bind(wxEVT_BUTTON, &CMainPanel::delEnv, this);
env_oper_sizer_->Add(btn_add_env_, 0, wxALL | wxALIGN_LEFT, g_Border);
env_oper_sizer_->Add(btn_del_env_, 0, wxALL | wxALIGN_LEFT, g_Border);
env_box_sizer_->Add(env_oper_sizer_, 0, wxALL, g_Border);
env_box_sizer_->Add(env_list_, 1, wxALL | wxEXPAND, g_Border);
top_sizer_->Add(base_box_sizer_, 1, wxALL | wxEXPAND, g_Border);
top_sizer_->Add(env_box_sizer_, 1, wxALL | wxEXPAND, g_Border);
oper_sizer_ = new wxBoxSizer(wxHORIZONTAL);
btn_gen_ = new wxButton(this, wxID_ANY, wxT("生成"));
btn_exit_ = new wxButton(this, wxID_ANY, wxT("退出"));
btn_gen_->Bind(wxEVT_BUTTON, &CMainPanel::genResult, this);
btn_exit_->Bind(wxEVT_BUTTON, [&](wxCommandEvent& event) { GetParent()->Close(); });
oper_sizer_->Add(btn_gen_, 0, wxALL, g_Border);
oper_sizer_->Add(btn_exit_, 0, wxALL, g_Border);
// 排列的时候在 sizer 里面排列
top_sizer_->Add(oper_sizer_, 0, wxALIGN_RIGHT | wxALL, g_Border);
// 设置主窗口的 sizer
SetSizer(top_sizer_);
}
void CMainPanel::selectDir(wxCommandEvent& event)
{
wxDirDialog dirDialog(this, wxT("Select Directory"), wxEmptyString, wxDD_DIR_MUST_EXIST);
if (dirDialog.ShowModal() == wxID_CANCEL) return; // 用户取消选择
wxString selectedDir = dirDialog.GetPath();
text_output_ctrl_->SetValue(selectedDir);
}
2024-08-06 10:18:09 +08:00
void CMainPanel::genResult(wxCommandEvent& event)
{
wxArrayString arrayStrings;
// 获取 wxListBox 中的所有项
unsigned int count = env_list_->GetCount();
for (unsigned int i = 0; i < count; ++i) {
wxString item = env_list_->GetString(i);
arrayStrings.Add(item);
}
wxString out_dir = text_output_ctrl_->GetValue();
if (handle_->gen(out_dir, arrayStrings)) {
// 显示消息框
wxMessageBox(wxT("成功"), // 消息内容
wxT("成功"), // 标题
wxOK | wxICON_INFORMATION, // 按钮和图标
this // 父窗口
);
return;
}
wxMessageBox(wxT("失败"), // 消息内容
wxT("失败"), // 标题
wxOK | wxICON_INFORMATION, // 按钮和图标
this // 父窗口
);
}
void CMainPanel::delEnv(wxCommandEvent& event)
{
int selection = env_list_->GetSelection();
if (selection != wxNOT_FOUND) {
// 删除选中的项
env_list_->Delete(selection);
}
}
void CMainPanel::addEnv(wxCommandEvent& event)
{
wxDirDialog dirDialog(this, wxT("Select Directory"), wxEmptyString, wxDD_DIR_MUST_EXIST);
if (dirDialog.ShowModal() == wxID_CANCEL) return; // 用户取消选择
wxString selectedDir = dirDialog.GetPath();
// 检查列表中是否已经存在此目录
for (unsigned int i = 0; i < env_list_->GetCount(); ++i) {
if (env_list_->GetString(i) == selectedDir) return; // 目录已存在,不再添加
}
// 添加目录到列表
env_list_->Append(selectedDir);
}
void CMainPanel::selectBinaryFile(wxCommandEvent& event)
{
wxFileDialog openFileDialog(
this, _("Open file"), "", "",
"Text files (*.txt)|*.txt|PDF files (*.pdf)|*.pdf|All files (*.*)|*.*",
wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() == wxID_CANCEL)
return;
wxString filePath = openFileDialog.GetPath();
text_select_ctrl_->SetValue(filePath);
}