LinuxPack/MainPanel.cpp
2025-01-17 11:43:11 +08:00

187 lines
6.9 KiB
C++

#include "MainPanel.h"
#include "InstallWidget.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)
{
top_sizer_ = new wxBoxSizer(wxVERTICAL);
handle_ = new CFunPack();
InitPanel();
}
CMainPanel::~CMainPanel()
{
delete handle_;
}
void CMainPanel::InitPanel()
{
// 创建 wxStaticBoxSizer,并设置标题
base_box_sizer_ = new wxStaticBoxSizer(wxVERTICAL, this, wxT("一、基本信息"));
// 创建水平 sizer 来放置控件
sizer_select_ = new wxBoxSizer(wxHORIZONTAL);
// 创建控件,并将其父级设置为正确的窗口(即 `this`,代表主窗口或面板)
label_select_ = new wxStaticText(this, wxID_ANY, wxT("要打包的二进制文件:"));
text_select_ctrl_ = new wxTextCtrl(this, wxID_ANY);
text_select_ctrl_->SetMinSize(wxSize(300, -1));
btn_select_ = new wxButton(this, wxID_ANY, wxT("选择文件"));
btn_select_->Bind(wxEVT_BUTTON, &CMainPanel::selectBinaryFile, this);
// 将控件添加到水平 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);
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);
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);
sizer_output_->Add(text_output_ctrl_, 1, wxALL | wxEXPAND, g_Border);
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_ 第二个参数改成 0
top_sizer_->Add(base_box_sizer_, 0, 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_install_ = new wxButton(this, wxID_ANY, wxT("安装到菜单栏"));
btn_gen_->Bind(wxEVT_BUTTON, &CMainPanel::genResult, this);
btn_exit_->Bind(wxEVT_BUTTON, [&](wxCommandEvent& event) { GetParent()->Close(); });
btn_install_->Bind(wxEVT_BUTTON, &CMainPanel::installDialog, this);
// 创建一个伸缩的占位符 sizer
auto* stretch_sizer = new wxBoxSizer(wxHORIZONTAL);
stretch_sizer->AddStretchSpacer(1); // 添加伸缩空间
// 添加伸缩的占位符 sizer 到主 sizer
oper_sizer_->Add(btn_install_, 0, wxALL, g_Border);
oper_sizer_->Add(stretch_sizer, 1, wxALL | wxEXPAND);
oper_sizer_->Add(btn_gen_, 0, wxALL, g_Border);
oper_sizer_->Add(btn_exit_, 0, wxALL, g_Border);
oper_sizer_->SetSizeHints(this);
// 排列的时候在 sizer 里面排列
top_sizer_->Add(oper_sizer_, 0, wxALL | wxEXPAND, g_Border);
// 设置主窗口的 sizer
SetSizer(top_sizer_);
}
void CMainPanel::installDialog(wxCommandEvent& event)
{
CInstallDialog dialog(GetParent(), wxT("安装"));
// dialog.SetSize(wxSize(400, 500));
dialog.SetSize(wxSize(400, -1));
dialog.ShowModal();
// if (dialog.ShowModal() == wxID_OK) // Show modal dialog and check return code
// {
// wxLogMessage("OK pressed");
// }
// else
// {
// wxLogMessage("Cancel pressed");
// }
}
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);
}
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(text_select_ctrl_->GetValue(), 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"), "", "", "*", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() == wxID_CANCEL)
return;
wxString filePath = openFileDialog.GetPath();
text_select_ctrl_->SetValue(filePath);
}