gui:界面架子基本都有了,还差一点细节和功能实现。

This commit is contained in:
taynpg 2024-08-06 13:52:19 +08:00
parent 4c8d77e690
commit 2136171bcc
7 changed files with 253 additions and 21 deletions

View File

@ -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)

View File

@ -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;
}

View File

@ -6,13 +6,22 @@
#define FUNCTIONIMP_H
#include <wx/arrstr.h>
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

134
InstallWidget.cpp Normal file
View File

@ -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<wxCheckBox*>(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);
}

45
InstallWidget.h Normal file
View File

@ -0,0 +1,45 @@
//
// Created by taynpg on 24-8-6.
//
#ifndef INSTALL_H
#define INSTALL_H
#include <wx/wx.h>
#include <wx/dialog.h>
#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

View File

@ -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 // 父窗口
);
}

View File

@ -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