libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GtkCRNProgressWindow.cpp
Go to the documentation of this file.
1 /* Copyright 2010-2016 CoReNum, ENS-Lyon
2  *
3  * This file is part of libgtkcrnmm.
4  *
5  * libgtkcrnmm is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libgtkcrnmm is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libgtkcrnmm. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: GtkCRNProgressWindow.cpp
19  * \author Yann LEYDIER
20  */
21 
22 #include <GtkCRNProgressWindow.h>
23 #include <GtkCRNMain.h>
24 #include <GtkCRNApp.h>
25 #include <CRNi18n.h>
26 #include <CRNIO/CRNIO.h>
27 
28 using namespace GtkCRN;
29 
35 ProgressWindow::ProgressWindow(const Glib::ustring title, Gtk::Window *parent, bool auto_close):
36  autoclose(auto_close),
37  terminate_on_exception(true),
38 #ifdef CRN_USING_GTKMM3
39  vbox(Gtk::ORIENTATION_VERTICAL),
40  closebut(_("_Close"))
41 #else
42  closebut(Gtk::Stock::CLOSE)
43 #endif
44 {
45  set_title(title);
46  if (parent)
47  {
48  set_transient_for (*parent);
49  set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
50  }
51  else
52  {
53  Gtk::Window *mainwin = App::get_main_window();
54  if (mainwin)
55  {
56  set_transient_for (*mainwin);
57  set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
58  }
59  else
60  set_position(Gtk::WIN_POS_CENTER);
61  }
62  set_modal();
63  set_deletable(false);
64  set_type_hint(Gdk::WINDOW_TYPE_HINT_DIALOG);
65 
66  signal_delete_event().connect(sigc::mem_fun(this, &ProgressWindow::no_close));
67 
68  closebut.signal_clicked().connect(sigc::mem_fun(this, &ProgressWindow::on_close));
69  closebut.set_sensitive(false);
70 
71  vbox.show();
72  if (!autoclose)
73  {
74 #ifdef CRN_USING_GTKMM3
75  Gtk::Box *hbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
76  hbox->set_halign(Gtk::ALIGN_END);
77  vbox.pack_end(*hbox, false, false, 0);
78  hbox->pack_end(closebut, false, false, 0);
79  hbox->show_all();
80 #else /* CRN_USING_GTKMM3 */
81  Gtk::Alignment *al = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_RIGHT));
82  vbox.pack_end(*al, false, false, 0);
83  Gtk::HBox *hbox = Gtk::manage(new Gtk::HBox());
84  al->add(*hbox);
85  hbox->pack_end(closebut, false, false, 0);
86  al->show_all();
87 #endif /* CRN_USING_GTKMM3 */
88  }
89  add(vbox);
90 }
91 
97 size_t ProgressWindow::add_progress_bar(const crn::String &name, size_t maxcount)
98 {
99  progbars.push_back(std::make_shared<Progress>(name, maxcount));
100  vbox.pack_start(progbars.back()->GetProgressBar());
101  return progbars.size() - 1;
102 }
103 
104 /*
105  * Returns a progress object
106  *
107  * \throws crn::ExceptionDomain index out of bounds
108  *
109  * \param[in] id the id of the progress bar
110  * \return a pointer to a crn::Progress object
111  */
113 {
114  if (id >= progbars.size())
115  throw crn::ExceptionDomain(crn::StringUTF8("crn::Progress* ProgressWindow::get_crn_progress(size_t id): ") + _("index out of bounds."));
116  return progbars[id].get();
117 }
118 
119 /*
120  * Returns a Gtk progress bar
121  *
122  * \throws crn::ExceptionDomain index out of bounds
123  *
124  * \param[in] id the id of the progress bar
125  * \return a pointer to a Gtk::ProgressBar
126  */
127 Gtk::ProgressBar& ProgressWindow::get_gtk_progressbar(size_t id)
128 {
129  if (id >= progbars.size())
130  throw crn::ExceptionDomain(crn::StringUTF8("Gtk::ProgressBar& ProgressWindow::get_gtk_progressbar(size_t id): ") + _("index out of bounds."));
131  return progbars[id]->GetProgressBar();
132 }
133 
137 void ProgressWindow::run(sigc::slot<void> func)
138 {
139  show();
140  Glib::Thread *thread = Glib::Thread::create(sigc::bind(sigc::mem_fun(this, &ProgressWindow::do_run), func), true);
141  working = true;
142  Glib::Thread::create(sigc::bind(sigc::mem_fun(this, &ProgressWindow::wait), thread), false);
143  while (working)
144  {
145  Gtk::Main::iteration();
146  }
147 }
148 
149 bool ProgressWindow::no_close(GdkEventAny *ev)
150 {
151  return true;
152 }
153 
154 void ProgressWindow::do_run(sigc::slot<void> func)
155 {
156  try
157  {
158  func();
159  }
160  catch (crn::Exception &ex)
161  {
162  App::show_exception(ex, terminate_on_exception);
163  }
164  catch (std::exception &ex)
165  {
166  App::show_exception(ex, terminate_on_exception);
167  }
168  catch (...)
169  {
170  App::show_exception(terminate_on_exception);
171  }
172 }
173 
174 void ProgressWindow::wait(Glib::Thread *thread)
175 {
176  thread->join();
177  g_idle_add(GSourceFunc(ProgressWindow::end), this);
178 }
179 
180 void ProgressWindow::on_close()
181 {
182  hide();
183  working = false;
184 }
185 
186 gboolean ProgressWindow::end(ProgressWindow *pw)
187 {
188  pw->closebut.set_sensitive(true);
189  pw->done.emit();
190  if (pw->autoclose)
191  {
192  pw->hide();
193  pw->working = false;
194  }
195  return false;
196 }
197 
Gtk::ProgressBar & get_gtk_progressbar(size_t id)
Gets an existing progress bar widget.
Base class for a progress display.
Definition: CRNProgress.h:39
#define _(String)
Definition: CRNi18n.h:51
A UTF32 character string class.
Definition: CRNString.h:61
crn::Progress * get_crn_progress(size_t id)
Gets an existing progress bar.
A generic domain error.
Definition: CRNException.h:83
Splash window with progress bars.
#define true
Definition: ConvertUTF.cpp:57
ProgressWindow(const Glib::ustring title, Gtk::Window *parent=nullptr, bool auto_close=false)
Constructor.
static Gtk::Window * get_main_window()
Gets a pointer to the main window.
Definition: GtkCRNApp.h:57
void run(sigc::slot< void > func)
Executes a processing.
static void show_exception(crn::Exception &ex, bool kill_app=true)
Displays an exception.
Definition: GtkCRNApp.cpp:207
A character string class.
Definition: CRNStringUTF8.h:49
Base class for exceptions.
Definition: CRNException.h:39
size_t add_progress_bar(const crn::String &name, size_t maxcount=100)
Adds a progress bar to the window.