From 2136171bcc53bb34bd8df05897e5cc9793fa21fb Mon Sep 17 00:00:00 2001 From: taynpg Date: Tue, 6 Aug 2024 13:52:19 +0800 Subject: [PATCH] =?UTF-8?q?gui=EF=BC=9A=E7=95=8C=E9=9D=A2=E6=9E=B6?= =?UTF-8?q?=E5=AD=90=E5=9F=BA=E6=9C=AC=E9=83=BD=E6=9C=89=E4=BA=86=EF=BC=8C?= =?UTF-8?q?=E8=BF=98=E5=B7=AE=E4=B8=80=E7=82=B9=E7=BB=86=E8=8A=82=E5=92=8C?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AE=9E=E7=8E=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 4 +- FunctionImp.cpp | 13 +++-- FunctionImp.h | 13 ++++- InstallWidget.cpp | 134 ++++++++++++++++++++++++++++++++++++++++++++++ InstallWidget.h | 45 ++++++++++++++++ MainPanel.cpp | 59 +++++++++++++++----- MainPanel.h | 6 ++- 7 files changed, 253 insertions(+), 21 deletions(-) create mode 100644 InstallWidget.cpp create mode 100644 InstallWidget.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d5a427..245f4d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,8 @@ add_executable(LinuxPack main.cpp MainFrame.cpp MainFrame.h FunctionImp.cpp - FunctionImp.h) + FunctionImp.h + InstallWidget.cpp + InstallWidget.h) target_link_libraries(LinuxPack PRIVATE ${wxWidgets_LIBRARIES}) set_target_properties(LinuxPack PROPERTIES WIN32_EXECUTABLE TRUE) diff --git a/FunctionImp.cpp b/FunctionImp.cpp index 46d609f..7c84fa1 100644 --- a/FunctionImp.cpp +++ b/FunctionImp.cpp @@ -4,10 +4,17 @@ #include "FunctionImp.h" -CFunctionImp::CFunctionImp() -= default; +CFunPack::CFunPack() = default; -bool CFunctionImp::gen(const wxString& out_dir, const wxArrayString& dirs) +bool CFunPack::gen(const wxString& out_dir, const wxArrayString& dirs) +{ + return true; +} + +CFunInstall::CFunInstall() += default; + +bool CFunInstall::install(const wxString& file, const wxString& ico) { return true; } diff --git a/FunctionImp.h b/FunctionImp.h index b863cd1..2d1cf74 100644 --- a/FunctionImp.h +++ b/FunctionImp.h @@ -6,13 +6,22 @@ #define FUNCTIONIMP_H #include -class CFunctionImp +class CFunPack { public: - CFunctionImp(); + CFunPack(); public: bool gen(const wxString& out_dir, const wxArrayString& dirs); }; +class CFunInstall +{ +public: + CFunInstall(); + +public: + bool install(const wxString& file, const wxString& ico = ""); +}; + #endif //FUNCTIONIMP_H diff --git a/InstallWidget.cpp b/InstallWidget.cpp new file mode 100644 index 0000000..21704a5 --- /dev/null +++ b/InstallWidget.cpp @@ -0,0 +1,134 @@ +// +// Created by taynpg on 24-8-6. +// + +#include "InstallWidget.h" + +CInstallDialog::CInstallDialog(wxWindow* parent, const wxString& title) : wxDialog(parent, wxID_ANY, title, wxDefaultPosition) +{ + handle_ = new CFunInstall(); + InitInstall(); +} + +CInstallDialog::~CInstallDialog() +{ + delete handle_; +} + +void CInstallDialog::InitInstall() +{ + panel_ = new wxPanel(this); + + top_sizer_ = new wxBoxSizer(wxVERTICAL); + label_binary_ = new wxStaticText(panel_, wxID_ANY, wxT("二进制文件:")); + top_sizer_->Add(label_binary_, 0, wxALL, g_Border2); + + auto* hs1 = new wxBoxSizer(wxHORIZONTAL); + text_binary_file_ = new wxTextCtrl(panel_, wxID_ANY); + btn_select_binary_ = new wxButton(panel_, wxID_ANY, wxT("选择")); + btn_select_binary_->Bind(wxEVT_BUTTON, &CInstallDialog::selectBinaryFile, this); + hs1->Add(text_binary_file_, 1, wxALL | wxEXPAND, g_Border2); + hs1->Add(btn_select_binary_, 0, wxALL, g_Border2); + top_sizer_->Add(hs1, 0, wxALL | wxEXPAND, g_Border2); + + use_default_ico_ = new wxCheckBox(panel_, wxID_ANY, wxT("使用默认图标")); + use_default_ico_->Bind(wxEVT_CHECKBOX, &CInstallDialog::checkChange, this); + top_sizer_->Add(use_default_ico_, 0, wxALL | wxEXPAND, g_Border2); + + label_ico_file_ = new wxStaticText(panel_, wxID_ANY, wxT("图标文件:")); + top_sizer_->Add(label_ico_file_, 0, wxALL, g_Border2); + + auto* hs2 = new wxBoxSizer(wxHORIZONTAL); + text_ico_file_ = new wxTextCtrl(panel_, wxID_ANY); + btn_select_ico_ = new wxButton(panel_, wxID_ANY, wxT("选择")); + btn_select_ico_->Bind(wxEVT_BUTTON, &CInstallDialog::selectIcoFile, this); + hs2->Add(text_ico_file_, 1, wxALL | wxEXPAND, g_Border2); + hs2->Add(btn_select_ico_, 0, wxALL, g_Border2); + top_sizer_->Add(hs2, 0, wxALL | wxEXPAND, g_Border2); + + auto* hs3 = new wxBoxSizer(wxHORIZONTAL); + label_category_ = new wxStaticText(panel_, wxID_ANY, wxT("程序类别:")); + choice_category_ = new wxChoice(panel_, wxID_ANY); + choice_category_->Append(wxT("Audio")); + choice_category_->Append(wxT("Video")); + choice_category_->Append(wxT("Development")); + choice_category_->Append(wxT("Education")); + choice_category_->Append(wxT("Game")); + choice_category_->Append(wxT("Graphics")); + choice_category_->Append(wxT("Office")); + choice_category_->Append(wxT("Science")); + choice_category_->Append(wxT("Settings")); + choice_category_->Append(wxT("Utility")); + choice_category_->SetSelection(9); + + hs3->Add(label_category_, 0, wxALL | wxALIGN_CENTER_VERTICAL, g_Border2); + hs3->Add(choice_category_, 0, wxALL, g_Border2); + top_sizer_->Add(hs3, 0, wxALL | wxEXPAND, g_Border2); + + btn_install_ = new wxButton(panel_, wxID_ANY, wxT("安装")); + btn_install_->Bind(wxEVT_BUTTON, &CInstallDialog::installToDesktop, this); + top_sizer_->Add(btn_install_, 0, wxALL | wxALIGN_RIGHT, g_Border2); + panel_->SetSizer(top_sizer_); + + auto* dialogSizer = new wxBoxSizer(wxVERTICAL); + dialogSizer->Add(panel_, 1, wxEXPAND | wxALL, g_Border2); + // 设置对话框的 sizer + SetSizerAndFit(dialogSizer); +} + +void CInstallDialog::installToDesktop(wxCommandEvent& event) +{ + wxString ico = text_ico_file_->GetValue(); + if (!text_ico_file_->IsEnabled()) { + ico.clear(); + } + if (handle_->install(text_binary_file_->GetValue(), ico)) { + // 显示消息框 + wxMessageBox(wxT("成功"), // 消息内容 + wxT("成功"), // 标题 + wxOK | wxICON_INFORMATION, // 按钮和图标 + this // 父窗口 + ); + return; + } + wxMessageBox(wxT("失败"), // 消息内容 + wxT("失败"), // 标题 + wxOK | wxICON_INFORMATION, // 按钮和图标 + this // 父窗口 + ); +} + +void CInstallDialog::checkChange(wxCommandEvent& event) +{ + auto* checkBox = dynamic_cast(event.GetEventObject()); + if (checkBox) { + if (checkBox->IsChecked()) { + text_ico_file_->Enable(false); + btn_select_ico_->Enable(false); + } else { + text_ico_file_->Enable(true); + btn_select_ico_->Enable(true); + } + } +} + +void CInstallDialog::selectBinaryFile(wxCommandEvent& event) +{ + wxFileDialog openFileDialog(this, wxT("选择图标文件"), "", "", "All files (*.*)|*.*", wxFD_OPEN | wxFD_FILE_MUST_EXIST); + + if (openFileDialog.ShowModal() == wxID_CANCEL) return; + + wxString filePath = openFileDialog.GetPath(); + text_binary_file_->SetValue(filePath); +} +void CInstallDialog::selectIcoFile(wxCommandEvent& event) +{ + wxFileDialog openFileDialog(this, _("Open file"), "", "", "ICO (*.ico)|*.ico|PNG (*.png)|*.png|SVG (*.svg)|*.svg|All files (*.*)|*.*", + wxFD_OPEN | wxFD_FILE_MUST_EXIST); + + if (openFileDialog.ShowModal() == wxID_CANCEL) return; + + wxString filePath = openFileDialog.GetPath(); + text_ico_file_->SetValue(filePath); +} + diff --git a/InstallWidget.h b/InstallWidget.h new file mode 100644 index 0000000..9bb9b84 --- /dev/null +++ b/InstallWidget.h @@ -0,0 +1,45 @@ +// +// Created by taynpg on 24-8-6. +// + +#ifndef INSTALL_H +#define INSTALL_H + +#include +#include +#include "FunctionImp.h" + +constexpr int g_Border2 = 3; + +class CInstallDialog : public wxDialog +{ +private: + CFunInstall* handle_{}; +private: + wxPanel* panel_{}; + wxBoxSizer* top_sizer_{}; + wxStaticText* label_binary_{}; + wxTextCtrl* text_binary_file_{}; + wxButton* btn_select_binary_{}; + wxCheckBox* use_default_ico_{}; + wxStaticText* label_default_ico_{}; + wxStaticText* label_ico_file_{}; + wxTextCtrl* text_ico_file_{}; + wxButton* btn_select_ico_{}; + wxStaticText* label_category_{}; + wxChoice* choice_category_{}; + wxButton* btn_install_{}; + +public: + explicit CInstallDialog(wxWindow* parent, const wxString& title); + ~CInstallDialog() override; + +private: + void InitInstall(); + void installToDesktop(wxCommandEvent& event); + void selectBinaryFile(wxCommandEvent& event); + void selectIcoFile(wxCommandEvent& event); + void checkChange(wxCommandEvent& event); +}; + +#endif // INSTALL_H diff --git a/MainPanel.cpp b/MainPanel.cpp index b3131ca..e12007e 100644 --- a/MainPanel.cpp +++ b/MainPanel.cpp @@ -1,13 +1,21 @@ #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 CFunctionImp(); + handle_ = new CFunPack(); InitPanel(); } +CMainPanel::~CMainPanel() +{ + delete handle_; +} + + void CMainPanel::InitPanel() { // 创建 wxStaticBoxSizer,并设置标题 @@ -37,7 +45,7 @@ void CMainPanel::InitPanel() 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); + 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); @@ -64,24 +72,49 @@ void CMainPanel::InitPanel() 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, wxALIGN_RIGHT | wxALL, g_Border); + 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); } @@ -98,17 +131,17 @@ void CMainPanel::genResult(wxCommandEvent& event) wxString out_dir = text_output_ctrl_->GetValue(); if (handle_->gen(out_dir, arrayStrings)) { // 显示消息框 - wxMessageBox(wxT("成功"), // 消息内容 - wxT("成功"), // 标题 - wxOK | wxICON_INFORMATION, // 按钮和图标 - this // 父窗口 + wxMessageBox(wxT("成功"), // 消息内容 + wxT("成功"), // 标题 + wxOK | wxICON_INFORMATION, // 按钮和图标 + this // 父窗口 ); return; } - wxMessageBox(wxT("失败"), // 消息内容 - wxT("失败"), // 标题 - wxOK | wxICON_INFORMATION, // 按钮和图标 - this // 父窗口 + wxMessageBox(wxT("失败"), // 消息内容 + wxT("失败"), // 标题 + wxOK | wxICON_INFORMATION, // 按钮和图标 + this // 父窗口 ); } diff --git a/MainPanel.h b/MainPanel.h index 72f1f07..a3d47e8 100644 --- a/MainPanel.h +++ b/MainPanel.h @@ -11,7 +11,7 @@ constexpr int g_Border = 3; class CMainPanel : public wxPanel { private: - CFunctionImp* handle_{}; + CFunPack* handle_{}; protected: wxBoxSizer* top_sizer_{}; @@ -36,10 +36,11 @@ protected: wxBoxSizer* oper_sizer_{}; wxButton* btn_gen_{}; wxButton* btn_exit_{}; + wxButton* btn_install_{}; public: explicit CMainPanel(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize(g_Width, g_Heigh), long style = wxTAB_TRAVERSAL, const wxString& name = wxEmptyString); - + ~CMainPanel() override; public: void InitPanel(); @@ -49,6 +50,7 @@ private: void addEnv(wxCommandEvent& event); void delEnv(wxCommandEvent& event); void genResult(wxCommandEvent& event); + void installDialog(wxCommandEvent& event); }; #endif \ No newline at end of file