Factor out duplicated code in DialogAttachments

Originally committed to SVN as r6040.
This commit is contained in:
Thomas Goyne 2011-12-22 21:15:47 +00:00
parent 96aa9e2629
commit 38e2403001
2 changed files with 53 additions and 120 deletions

View file

@ -34,11 +34,10 @@
/// @ingroup tools_ui /// @ingroup tools_ui
/// ///
///////////
// Headers
#include "config.h" #include "config.h"
#include "dialog_attachments.h"
#ifndef AGI_PRE #ifndef AGI_PRE
#include <wx/button.h> #include <wx/button.h>
#include <wx/dirdlg.h> #include <wx/dirdlg.h>
@ -51,24 +50,25 @@
#include "ass_attachment.h" #include "ass_attachment.h"
#include "ass_file.h" #include "ass_file.h"
#include "compat.h" #include "compat.h"
#include "dialog_attachments.h"
#include "help_button.h" #include "help_button.h"
#include "libresrc/libresrc.h" #include "libresrc/libresrc.h"
#include "main.h" #include "main.h"
#include "utils.h" #include "utils.h"
enum {
BUTTON_ATTACH_FONT = 1300,
BUTTON_ATTACH_GRAPHICS,
BUTTON_EXTRACT,
BUTTON_DELETE,
ATTACHMENT_LIST
};
/// @brief Constructor
/// @param parent
///
DialogAttachments::DialogAttachments(wxWindow *parent, AssFile *ass) DialogAttachments::DialogAttachments(wxWindow *parent, AssFile *ass)
: wxDialog(parent,-1,_("Attachment List"),wxDefaultPosition,wxDefaultSize,wxDEFAULT_DIALOG_STYLE) : wxDialog(parent,-1,_("Attachment List"),wxDefaultPosition,wxDefaultSize,wxDEFAULT_DIALOG_STYLE)
, ass(ass) , ass(ass)
{ {
// Set icon
SetIcon(BitmapToIcon(GETIMAGE(attach_button_24))); SetIcon(BitmapToIcon(GETIMAGE(attach_button_24)));
// List view
listView = new wxListView(this,ATTACHMENT_LIST,wxDefaultPosition,wxSize(500,200)); listView = new wxListView(this,ATTACHMENT_LIST,wxDefaultPosition,wxSize(500,200));
UpdateList(); UpdateList();
@ -96,12 +96,7 @@ DialogAttachments::DialogAttachments(wxWindow *parent, AssFile *ass)
CenterOnParent(); CenterOnParent();
} }
/// @brief Update list
///
void DialogAttachments::UpdateList() { void DialogAttachments::UpdateList() {
// Clear list
listView->ClearAll(); listView->ClearAll();
// Insert list columns // Insert list columns
@ -124,12 +119,7 @@ void DialogAttachments::UpdateList() {
} }
} }
/// @brief Destructor
///
DialogAttachments::~DialogAttachments() { DialogAttachments::~DialogAttachments() {
// Remove empty attachments sections from the file // Remove empty attachments sections from the file
std::list<AssEntry*>::iterator cur = ass->Line.end(); std::list<AssEntry*>::iterator cur = ass->Line.end();
@ -178,9 +168,6 @@ DialogAttachments::~DialogAttachments() {
} }
} }
///////////////
// Event table
BEGIN_EVENT_TABLE(DialogAttachments,wxDialog) BEGIN_EVENT_TABLE(DialogAttachments,wxDialog)
EVT_BUTTON(BUTTON_ATTACH_FONT,DialogAttachments::OnAttachFont) EVT_BUTTON(BUTTON_ATTACH_FONT,DialogAttachments::OnAttachFont)
EVT_BUTTON(BUTTON_ATTACH_GRAPHICS,DialogAttachments::OnAttachGraphics) EVT_BUTTON(BUTTON_ATTACH_GRAPHICS,DialogAttachments::OnAttachGraphics)
@ -189,90 +176,55 @@ BEGIN_EVENT_TABLE(DialogAttachments,wxDialog)
EVT_LIST_ITEM_SELECTED(ATTACHMENT_LIST,DialogAttachments::OnListClick) EVT_LIST_ITEM_SELECTED(ATTACHMENT_LIST,DialogAttachments::OnListClick)
EVT_LIST_ITEM_DESELECTED(ATTACHMENT_LIST,DialogAttachments::OnListClick) EVT_LIST_ITEM_DESELECTED(ATTACHMENT_LIST,DialogAttachments::OnListClick)
EVT_LIST_ITEM_FOCUSED(ATTACHMENT_LIST,DialogAttachments::OnListClick) EVT_LIST_ITEM_FOCUSED(ATTACHMENT_LIST,DialogAttachments::OnListClick)
END_EVENT_TABLE() END_EVENT_TABLE();
void DialogAttachments::AttachFile(wxFileDialog &diag, wxString const& group, wxString const& commit_msg) {
if (diag.ShowModal() == wxID_CANCEL) return;
wxArrayString filenames;
diag.GetFilenames(filenames);
wxArrayString paths;
diag.GetPaths(paths);
// Create attachments
for (size_t i = 0; i < filenames.size(); ++i) {
AssAttachment *newAttach = new AssAttachment(filenames[i]);
try {
newAttach->Import(paths[i]);
}
catch (...) {
delete newAttach;
return;
}
newAttach->group = group;
ass->InsertAttachment(newAttach);
}
ass->Commit(commit_msg, AssFile::COMMIT_ATTACHMENT);
UpdateList();
}
/// @brief Attach font
/// @param event
/// @return
///
void DialogAttachments::OnAttachFont(wxCommandEvent &) { void DialogAttachments::OnAttachFont(wxCommandEvent &) {
// Pick files wxFileDialog diag(this,
wxArrayString filenames; _("Choose file to be attached"),
wxArrayString paths; lagi_wxString(OPT_GET("Path/Fonts Collector Destination")->GetString()), "", "Font Files (*.ttf)|*.ttf",
{ wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE);
wxFileDialog diag (this,_("Choose file to be attached"), lagi_wxString(OPT_GET("Path/Fonts Collector Destination")->GetString()), "", "Font Files (*.ttf)|*.ttf", wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE);
if (diag.ShowModal() == wxID_CANCEL) return; AttachFile(diag, "[Fonts]", _("attach font file"));
diag.GetFilenames(filenames);
diag.GetPaths(paths);
} }
// Create attachments
for (size_t i=0;i<filenames.Count();i++) {
//wxFileName file(filenames[i]);
AssAttachment *newAttach = new AssAttachment(filenames[i]);
try {
newAttach->Import(paths[i]);
}
catch (...) {
delete newAttach;
return;
}
newAttach->group = "[Fonts]";
ass->InsertAttachment(newAttach);
}
ass->Commit(_("attach font file"), AssFile::COMMIT_ATTACHMENT);
// Update
UpdateList();
}
/// @brief Attach graphics
/// @param event
/// @return
///
void DialogAttachments::OnAttachGraphics(wxCommandEvent &) { void DialogAttachments::OnAttachGraphics(wxCommandEvent &) {
// Pick files wxFileDialog diag(this,
wxArrayString filenames; _("Choose file to be attached"),
wxArrayString paths; "", "",
{ "Graphic Files (*.bmp,*.gif,*.jpg,*.ico,*.wmf)|*.bmp;*.gif;*.jpg;*.ico;*.wmf",
wxFileDialog diag (this,_("Choose file to be attached"), "", "", "Graphic Files (*.bmp,*.gif,*.jpg,*.ico,*.wmf)|*.bmp;*.gif;*.jpg;*.ico;*.wmf", wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE); wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE);
if (diag.ShowModal() == wxID_CANCEL) return;
diag.GetFilenames(filenames); AttachFile(diag, "[Graphics]", _("attach graphics file"));
diag.GetPaths(paths);
} }
// Create attachments
for (size_t i=0;i<filenames.Count();i++) {
//wxFileName file(filenames[i]);
AssAttachment *newAttach = new AssAttachment(filenames[i]);
try {
newAttach->Import(paths[i]);
}
catch (...) {
delete newAttach;
return;
}
newAttach->group = "[Graphics]";
ass->InsertAttachment(newAttach);
}
ass->Commit(_("attach graphics file"), AssFile::COMMIT_ATTACHMENT);
// Update
UpdateList();
}
/// @brief Extract
/// @param event
/// @return
///
void DialogAttachments::OnExtract(wxCommandEvent &) { void DialogAttachments::OnExtract(wxCommandEvent &) {
// Check if there's a selection // Check if there's a selection
int i = listView->GetFirstSelected(); int i = listView->GetFirstSelected();
@ -303,11 +255,6 @@ void DialogAttachments::OnExtract(wxCommandEvent &) {
} }
} }
/// @brief Delete
/// @param event
///
void DialogAttachments::OnDelete(wxCommandEvent &) { void DialogAttachments::OnDelete(wxCommandEvent &) {
// Loop through items in list // Loop through items in list
int i = listView->GetFirstSelected(); int i = listView->GetFirstSelected();
@ -318,22 +265,11 @@ void DialogAttachments::OnDelete(wxCommandEvent &) {
ass->Commit(_("remove attachment"), AssFile::COMMIT_ATTACHMENT); ass->Commit(_("remove attachment"), AssFile::COMMIT_ATTACHMENT);
// Update list
UpdateList(); UpdateList();
} }
/// @brief List selection changed
/// @param event
///
void DialogAttachments::OnListClick(wxListEvent &) { void DialogAttachments::OnListClick(wxListEvent &) {
// Check if any is selected
bool hasSel = listView->GetFirstSelected() != -1; bool hasSel = listView->GetFirstSelected() != -1;
// Set status
extractButton->Enable(hasSel); extractButton->Enable(hasSel);
deleteButton->Enable(hasSel); deleteButton->Enable(hasSel);
} }

View file

@ -35,9 +35,14 @@
/// ///
class AssFile; class AssFile;
class wxFileDialog;
class wxListView; class wxListView;
class wxListEvent; class wxListEvent;
#ifndef AGI_PRE
#include <wx/dialog.h>
#endif
/// DOCME /// DOCME
/// @class DialogAttachments /// @class DialogAttachments
/// @brief DOCME /// @brief DOCME
@ -62,6 +67,7 @@ class DialogAttachments : public wxDialog {
void OnListClick(wxListEvent &event); void OnListClick(wxListEvent &event);
void UpdateList(); void UpdateList();
void AttachFile(wxFileDialog &diag, wxString const& group, wxString const& commit_msg);
public: public:
DialogAttachments(wxWindow *parent, AssFile *ass); DialogAttachments(wxWindow *parent, AssFile *ass);
@ -69,12 +75,3 @@ public:
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };
/// Event IDs
enum {
BUTTON_ATTACH_FONT = 1300,
BUTTON_ATTACH_GRAPHICS,
BUTTON_EXTRACT,
BUTTON_DELETE,
ATTACHMENT_LIST
};