2011-09-28 21:47:40 +02:00
|
|
|
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2011-09-28 21:47:40 +02:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2011-09-28 21:47:40 +02:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file dialog_progress.cpp
|
2011-09-28 21:47:40 +02:00
|
|
|
/// @brief Progress-bar dialog box for displaying during long operations
|
2009-07-29 07:43:02 +02:00
|
|
|
/// @ingroup utility
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
#include "dialog_progress.h"
|
|
|
|
|
|
|
|
#include <libaegisub/exception.h>
|
|
|
|
|
|
|
|
#include "compat.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#ifndef AGI_PRE
|
2007-09-12 01:22:26 +02:00
|
|
|
#include <wx/button.h>
|
2011-09-28 21:47:40 +02:00
|
|
|
#include <wx/gauge.h>
|
2007-09-12 01:22:26 +02:00
|
|
|
#include <wx/sizer.h>
|
2011-09-28 21:47:40 +02:00
|
|
|
#include <wx/stattext.h>
|
|
|
|
#include <wx/textctrl.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#endif
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
wxDEFINE_EVENT(EVT_TITLE, wxThreadEvent);
|
|
|
|
wxDEFINE_EVENT(EVT_MESSAGE, wxThreadEvent);
|
|
|
|
wxDEFINE_EVENT(EVT_PROGRESS, wxThreadEvent);
|
|
|
|
wxDEFINE_EVENT(EVT_INDETERMINATE, wxThreadEvent);
|
|
|
|
wxDEFINE_EVENT(EVT_LOG, wxThreadEvent);
|
|
|
|
wxDEFINE_EVENT(EVT_COMPLETE, wxThreadEvent);
|
|
|
|
|
|
|
|
class DialogProgressSink : public agi::ProgressSink {
|
|
|
|
DialogProgress *dialog;
|
|
|
|
bool cancelled;
|
|
|
|
wxMutex cancelled_mutex;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void SafeQueue(wxEventType type, T const& value) {
|
|
|
|
wxThreadEvent *evt = new wxThreadEvent(type);
|
|
|
|
evt->SetPayload(value);
|
|
|
|
wxQueueEvent(dialog, evt);
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
public:
|
|
|
|
DialogProgressSink(DialogProgress *dialog)
|
|
|
|
: dialog(dialog)
|
|
|
|
, cancelled(false)
|
|
|
|
{
|
|
|
|
}
|
2008-10-28 05:03:29 +01:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void SetTitle(std::string const& title) {
|
|
|
|
SafeQueue(EVT_TITLE, lagi_wxString(title));
|
|
|
|
}
|
2008-10-28 05:03:29 +01:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void SetMessage(std::string const& msg) {
|
|
|
|
SafeQueue(EVT_MESSAGE, lagi_wxString(msg));
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-03-10 02:00:13 +01:00
|
|
|
void SetProgress(int64_t cur, int64_t max) {
|
2011-09-28 21:47:40 +02:00
|
|
|
SafeQueue(EVT_PROGRESS, int(double(cur) / max * 100));
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void Log(std::string const& str) {
|
|
|
|
SafeQueue(EVT_LOG, lagi_wxString(str));
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
bool IsCancelled() {
|
|
|
|
wxMutexLocker l(cancelled_mutex);
|
|
|
|
return cancelled;
|
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void Cancel() {
|
|
|
|
wxMutexLocker l(cancelled_mutex);
|
|
|
|
cancelled = true;
|
2008-10-28 05:03:29 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void SetIndeterminate() {
|
|
|
|
wxQueueEvent(dialog, new wxThreadEvent(EVT_INDETERMINATE));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TaskRunner : public wxThread {
|
|
|
|
std::tr1::function<void(agi::ProgressSink*)> task;
|
|
|
|
agi::ProgressSink *ps;
|
|
|
|
wxDialog *dialog;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TaskRunner(std::tr1::function<void(agi::ProgressSink*)> task, agi::ProgressSink *ps, wxDialog *dialog, int priority)
|
|
|
|
: task(task)
|
|
|
|
, ps(ps)
|
|
|
|
, dialog(dialog)
|
2008-10-28 05:03:29 +01:00
|
|
|
{
|
2011-09-28 21:47:40 +02:00
|
|
|
Create();
|
|
|
|
if (priority != -1)
|
|
|
|
SetPriority(priority);
|
|
|
|
Run();
|
2008-10-28 05:03:29 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
wxThread::ExitCode Entry() {
|
|
|
|
task(ps);
|
|
|
|
wxQueueEvent(dialog, new wxThreadEvent(EVT_COMPLETE));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
DialogProgress::DialogProgress(wxWindow *parent, wxString const& title_text, wxString const& message)
|
|
|
|
: wxDialog(parent, -1, title_text, wxDefaultPosition, wxDefaultSize, wxBORDER_RAISED)
|
|
|
|
, pulse_timer(GetEventHandler())
|
2008-10-28 05:03:29 +01:00
|
|
|
{
|
2011-09-28 21:47:40 +02:00
|
|
|
title = new wxStaticText(this, -1, title_text, wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE | wxST_NO_AUTORESIZE);
|
|
|
|
gauge = new wxGauge(this, -1, 100, wxDefaultPosition, wxSize(300,20));
|
|
|
|
text = new wxStaticText(this, -1, message, wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE | wxST_NO_AUTORESIZE);
|
|
|
|
cancel_button = new wxButton(this, wxID_CANCEL);
|
|
|
|
log_output = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxSize(300, 120), wxTE_MULTILINE | wxTE_READONLY);
|
|
|
|
|
|
|
|
// make the title a slightly larger font
|
|
|
|
wxFont title_font = title->GetFont();
|
|
|
|
int fontsize = title_font.GetPointSize();
|
|
|
|
title_font.SetPointSize(fontsize * 1.375);
|
|
|
|
title_font.SetWeight(wxFONTWEIGHT_BOLD);
|
|
|
|
title->SetFont(title_font);
|
|
|
|
|
|
|
|
wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
sizer->Add(title, wxSizerFlags().Expand().Center());
|
|
|
|
sizer->Add(gauge, wxSizerFlags(1).Expand().Border());
|
|
|
|
sizer->Add(text, wxSizerFlags().Expand().Center());
|
|
|
|
sizer->Add(cancel_button, wxSizerFlags().Center().Border());
|
|
|
|
sizer->Add(log_output, wxSizerFlags().Expand().Border(wxALL & ~wxTOP));
|
|
|
|
sizer->Hide(log_output);
|
|
|
|
|
|
|
|
SetSizerAndFit(sizer);
|
|
|
|
CenterOnParent();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
Bind(wxEVT_SHOW, &DialogProgress::OnShow, this);
|
|
|
|
Bind(wxEVT_TIMER, &DialogProgress::OnPulseTimer, this);
|
2012-02-16 22:22:04 +01:00
|
|
|
Bind(wxEVT_IDLE, &DialogProgress::OnIdle, this);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
Bind(EVT_TITLE, &DialogProgress::OnSetTitle, this);
|
|
|
|
Bind(EVT_MESSAGE, &DialogProgress::OnSetMessage, this);
|
|
|
|
Bind(EVT_PROGRESS, &DialogProgress::OnSetProgress, this);
|
|
|
|
Bind(EVT_INDETERMINATE, &DialogProgress::OnSetIndeterminate, this);
|
|
|
|
Bind(EVT_COMPLETE, &DialogProgress::OnComplete, this);
|
|
|
|
Bind(EVT_LOG, &DialogProgress::OnLog, this);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void DialogProgress::Run(std::tr1::function<void(agi::ProgressSink*)> task, int priority) {
|
|
|
|
DialogProgressSink ps(this);
|
|
|
|
this->ps = &ps;
|
|
|
|
new TaskRunner(task, &ps, this, priority);
|
2011-10-23 19:00:21 +02:00
|
|
|
if (!ShowModal())
|
2011-09-28 21:47:40 +02:00
|
|
|
throw agi::UserCancelException("Cancelled by user");
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void DialogProgress::OnShow(wxShowEvent&) {
|
|
|
|
// Restore the cancel button in case it was previously switched to a close
|
|
|
|
// button
|
|
|
|
Bind(wxEVT_COMMAND_BUTTON_CLICKED, &DialogProgress::OnCancel, this, wxID_CANCEL);
|
|
|
|
cancel_button->SetLabelText(_("Cancel"));
|
|
|
|
cancel_button->Enable();
|
|
|
|
|
|
|
|
wxSizer *sizer = GetSizer();
|
|
|
|
if (sizer->IsShown(log_output)) {
|
|
|
|
sizer->Hide(log_output);
|
|
|
|
Layout();
|
|
|
|
sizer->Fit(this);
|
|
|
|
log_output->Clear();
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-02-16 22:22:04 +01:00
|
|
|
void DialogProgress::OnIdle(wxIdleEvent&) {
|
|
|
|
if (!pending_log) return;
|
|
|
|
|
|
|
|
if (log_output->IsEmpty()) {
|
|
|
|
wxSizer *sizer = GetSizer();
|
|
|
|
sizer->Show(log_output);
|
|
|
|
Layout();
|
|
|
|
sizer->Fit(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
*log_output << pending_log;
|
|
|
|
log_output->SetInsertionPointEnd();
|
|
|
|
pending_log.clear();
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void DialogProgress::OnSetTitle(wxThreadEvent &evt) {
|
|
|
|
title->SetLabelText(evt.GetPayload<wxString>());
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void DialogProgress::OnSetMessage(wxThreadEvent &evt) {
|
|
|
|
text->SetLabelText(evt.GetPayload<wxString>());
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void DialogProgress::OnSetProgress(wxThreadEvent &evt) {
|
|
|
|
gauge->SetValue(mid(0, evt.GetPayload<int>(), 100));
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:09:31 +01:00
|
|
|
void DialogProgress::OnSetIndeterminate(wxThreadEvent &) {
|
2011-09-28 21:47:40 +02:00
|
|
|
pulse_timer.Start(1000);
|
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2011-12-22 22:09:31 +01:00
|
|
|
void DialogProgress::OnComplete(wxThreadEvent &) {
|
2011-09-28 21:47:40 +02:00
|
|
|
pulse_timer.Stop();
|
|
|
|
|
|
|
|
// Unbind the cancel handler so that the default behavior happens (i.e. the
|
|
|
|
// dialog is closed) as there's no longer a task to cancel
|
|
|
|
Unbind(wxEVT_COMMAND_BUTTON_CLICKED, &DialogProgress::OnCancel, this, wxID_CANCEL);
|
|
|
|
|
|
|
|
// If it ran to completion and there is debug output, leave the window open
|
|
|
|
// so the user can read the debug output and switch the cancel button to a
|
|
|
|
// close button
|
|
|
|
bool cancelled = ps->IsCancelled();
|
2012-02-16 22:22:04 +01:00
|
|
|
if (cancelled || (log_output->IsEmpty() && !pending_log))
|
2011-10-23 19:00:21 +02:00
|
|
|
EndModal(!cancelled);
|
2011-09-28 21:47:40 +02:00
|
|
|
else
|
|
|
|
cancel_button->SetLabelText(_("Close"));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void DialogProgress::OnLog(wxThreadEvent &evt) {
|
2012-02-16 22:22:04 +01:00
|
|
|
pending_log += evt.GetPayload<wxString>();
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2011-12-22 22:09:31 +01:00
|
|
|
void DialogProgress::OnCancel(wxCommandEvent &) {
|
2011-09-28 21:47:40 +02:00
|
|
|
ps->Cancel();
|
|
|
|
cancel_button->Enable(false);
|
|
|
|
cancel_button->SetLabelText(_("Cancelling..."));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2009-07-29 07:43:02 +02:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void DialogProgress::OnPulseTimer(wxTimerEvent&) {
|
2010-09-01 08:50:35 +02:00
|
|
|
gauge->Pulse();
|
|
|
|
}
|