[gthumb] added a GthImageViewerTask to simplify the code



commit eb6689bb3713fe5cf1060f536518e021239335cf
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Sat Sep 21 12:49:19 2013 +0200

    added a GthImageViewerTask to simplify the code
    
    GthImageViewerTask is a GthImageTask that loads the original
    image from a GthImageViewerPage object.

 extensions/image_viewer/Makefile.am             |    3 +
 extensions/image_viewer/gth-image-viewer-task.c |  173 +++++++++++++++++++++++
 extensions/image_viewer/gth-image-viewer-task.h |   62 ++++++++
 extensions/image_viewer/image-viewer.h          |   28 ++++
 gthumb/gth-image-task.c                         |   40 +++++
 gthumb/gth-image-task.h                         |   35 +++--
 6 files changed, 326 insertions(+), 15 deletions(-)
---
diff --git a/extensions/image_viewer/Makefile.am b/extensions/image_viewer/Makefile.am
index 05a6d56..2dbf5cf 100644
--- a/extensions/image_viewer/Makefile.am
+++ b/extensions/image_viewer/Makefile.am
@@ -8,8 +8,11 @@ libimage_viewer_la_SOURCES =           \
        gth-image-histogram.h           \
        gth-image-viewer-page.c         \
        gth-image-viewer-page.h         \
+       gth-image-viewer-task.c         \
+       gth-image-viewer-task.h         \
        gth-metadata-provider-image.c   \
        gth-metadata-provider-image.h   \
+       image-viewer.h                  \
        main.c                          \
        preferences.c                   \
        preferences.h
diff --git a/extensions/image_viewer/gth-image-viewer-task.c b/extensions/image_viewer/gth-image-viewer-task.c
new file mode 100644
index 0000000..19efa5a
--- /dev/null
+++ b/extensions/image_viewer/gth-image-viewer-task.c
@@ -0,0 +1,173 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ *  GThumb
+ *
+ *  Copyright (C) 2013 Free Software Foundation, Inc.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <glib.h>
+#include "gth-image-viewer-page.h"
+#include "gth-image-viewer-task.h"
+
+
+struct _GthImageViewerTaskPrivate {
+       GthImageViewerPage      *viewer_page;
+       GthTask                 *original_image_task;
+       gboolean                 load_original;
+       gboolean                 loading_image;
+};
+
+
+G_DEFINE_TYPE (GthImageViewerTask, gth_image_viewer_task, GTH_TYPE_IMAGE_TASK)
+
+
+static void
+gth_image_viewer_task_finalize (GObject *object)
+{
+       GthImageViewerTask *self;
+
+       g_return_if_fail (GTH_IS_IMAGE_VIEWER_TASK (object));
+
+       self = GTH_IMAGE_VIEWER_TASK (object);
+       _g_object_unref (self->priv->original_image_task);
+
+       G_OBJECT_CLASS (gth_image_viewer_task_parent_class)->finalize (object);
+}
+
+
+static void
+original_image_task_completed_cb (GthTask      *task,
+                                 GError        *error,
+                                 gpointer       user_data)
+{
+       GthImageViewerTask *self = user_data;
+       cairo_surface_t    *image;
+
+       if (error != NULL) {
+               gth_task_completed (task, error);
+               return;
+       }
+
+       image = gth_original_image_task_get_image (task);
+       gth_image_task_set_source_surface (GTH_IMAGE_TASK (self), image);
+       gth_task_progress (GTH_TASK (self), NULL, "", TRUE, 0.0);
+       GTH_TASK_CLASS (gth_image_viewer_task_parent_class)->exec (GTH_TASK (self));
+
+       cairo_surface_destroy (image);
+}
+
+
+static void
+original_image_task_progress_cb (GthTask    *task,
+                                const char *description,
+                                const char *details,
+                                gboolean    pulse,
+                                double      fraction,
+                                gpointer    user_data)
+{
+       GthImageViewerTask *self = user_data;
+       gth_task_progress (GTH_TASK (self), NULL, description, pulse, fraction);
+}
+
+
+static void
+gth_image_viewer_task_exec (GthTask *task)
+{
+       GthImageViewerTask *self;
+
+       self = GTH_IMAGE_VIEWER_TASK (task);
+
+       if (self->priv->load_original) {
+               self->priv->original_image_task = gth_original_image_task_new (self->priv->viewer_page);
+               g_signal_connect (self->priv->original_image_task,
+                                 "completed",
+                                 G_CALLBACK (original_image_task_completed_cb),
+                                 self);
+               g_signal_connect (self->priv->original_image_task,
+                                 "progress",
+                                 G_CALLBACK (original_image_task_progress_cb),
+                                 self);
+
+               gth_task_exec (self->priv->original_image_task, gth_task_get_cancellable (GTH_TASK (self)));
+
+               return;
+       }
+
+       GTH_TASK_CLASS (gth_image_viewer_task_parent_class)->exec (GTH_TASK (self));
+}
+
+
+static void
+gth_image_viewer_task_class_init (GthImageViewerTaskClass *class)
+{
+       GObjectClass *object_class;
+       GthTaskClass *task_class;
+
+       g_type_class_add_private (class, sizeof (GthImageViewerTaskPrivate));
+
+       object_class = G_OBJECT_CLASS (class);
+       object_class->finalize = gth_image_viewer_task_finalize;
+
+       task_class = GTH_TASK_CLASS (class);
+       task_class->exec = gth_image_viewer_task_exec;
+}
+
+
+static void
+gth_image_viewer_task_init (GthImageViewerTask *self)
+{
+       self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTH_TYPE_IMAGE_VIEWER_TASK, 
GthImageViewerTaskPrivate);
+       self->priv->original_image_task = NULL;
+       self->priv->load_original = TRUE;
+       self->priv->loading_image = FALSE;
+}
+
+
+GthTask *
+gth_image_viewer_task_new (GthImageViewerPage *viewer_page,
+                          const char         *description,
+                          GthAsyncInitFunc    before_func,
+                          GthAsyncThreadFunc  exec_func,
+                          GthAsyncReadyFunc   after_func,
+                          gpointer            user_data,
+                          GDestroyNotify      user_data_destroy_func)
+{
+       GthImageViewerTask *self;
+
+       g_return_val_if_fail (viewer_page != NULL, NULL);
+
+       self = (GthImageViewerTask *) g_object_new (GTH_TYPE_IMAGE_VIEWER_TASK,
+                                                   "before-thread", before_func,
+                                                   "thread-func", exec_func,
+                                                   "after-thread", after_func,
+                                                   "user-data", user_data,
+                                                   "user-data-destroy-func", user_data_destroy_func,
+                                                   "description", description,
+                                                   NULL);
+       self->priv->viewer_page = viewer_page;
+
+       return (GthTask *) self;
+}
+
+
+void
+gth_image_viewer_task_set_load_original (GthImageViewerTask *self,
+                                        gboolean            load_original)
+{
+       self->priv->load_original = load_original;
+}
diff --git a/extensions/image_viewer/gth-image-viewer-task.h b/extensions/image_viewer/gth-image-viewer-task.h
new file mode 100644
index 0000000..480677d
--- /dev/null
+++ b/extensions/image_viewer/gth-image-viewer-task.h
@@ -0,0 +1,62 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ *  GThumb
+ *
+ *  Copyright (C) 2013 The Free Software Foundation, Inc.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GTH_IMAGE_VIEWER_TASK_H
+#define GTH_IMAGE_VIEWER_TASK_H
+
+#include <gthumb.h>
+#include "gth-image-viewer-page.h"
+
+G_BEGIN_DECLS
+
+#define GTH_TYPE_IMAGE_VIEWER_TASK            (gth_image_viewer_task_get_type ())
+#define GTH_IMAGE_VIEWER_TASK(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
GTH_TYPE_IMAGE_VIEWER_TASK, GthImageViewerTask))
+#define GTH_IMAGE_VIEWER_TASK_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GTH_TYPE_IMAGE_VIEWER_TASK, 
GthImageViewerTaskClass))
+#define GTH_IS_IMAGE_VIEWER_TASK(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
GTH_TYPE_IMAGE_VIEWER_TASK))
+#define GTH_IS_IMAGE_VIEWER_TASK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTH_TYPE_IMAGE_VIEWER_TASK))
+#define GTH_IMAGE_VIEWER_TASK_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), GTH_TYPE_IMAGE_VIEWER_TASK, 
GthImageViewerTaskClass))
+
+typedef struct _GthImageViewerTask        GthImageViewerTask;
+typedef struct _GthImageViewerTaskClass   GthImageViewerTaskClass;
+typedef struct _GthImageViewerTaskPrivate GthImageViewerTaskPrivate;
+
+struct _GthImageViewerTask {
+       GthImageTask __parent;
+       GthImageViewerTaskPrivate *priv;
+};
+
+struct _GthImageViewerTaskClass {
+       GthImageTaskClass __parent;
+};
+
+GType         gth_image_viewer_task_get_type           (void);
+GthTask *     gth_image_viewer_task_new                        (GthImageViewerPage     *viewer_page,
+                                                        const char             *description,
+                                                        GthAsyncInitFunc        before_func,
+                                                        GthAsyncThreadFunc      exec_func,
+                                                        GthAsyncReadyFunc       after_func,
+                                                        gpointer                user_data,
+                                                        GDestroyNotify          user_data_destroy_func);
+void         gth_image_viewer_task_set_load_original   (GthImageViewerTask     *self,
+                                                        gboolean                load_original);
+G_END_DECLS
+
+#endif /* GTH_IMAGE_VIEWER_TASK_H */
diff --git a/extensions/image_viewer/image-viewer.h b/extensions/image_viewer/image-viewer.h
new file mode 100644
index 0000000..5f9f4f8
--- /dev/null
+++ b/extensions/image_viewer/image-viewer.h
@@ -0,0 +1,28 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ *  GThumb
+ *
+ *  Copyright (C) 2013 The Free Software Foundation, Inc.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IMAGE_VIEWER_H
+#define IMAGE_VIEWER_H
+
+#include "gth-image-viewer-page.h"
+#include "gth-image-viewer-task.h"
+
+#endif /* IMAGE_VIEWER_H */
diff --git a/gthumb/gth-image-task.c b/gthumb/gth-image-task.c
index 7b4cf3d..922d347 100644
--- a/gthumb/gth-image-task.c
+++ b/gthumb/gth-image-task.c
@@ -104,6 +104,19 @@ gth_image_task_set_source (GthImageTask *self,
 }
 
 
+void
+gth_image_task_set_source_surface (GthImageTask    *self,
+                                  cairo_surface_t *surface)
+{
+       GthImage *image;
+
+       image = gth_image_new_for_surface (surface);
+       gth_image_task_set_source (self, image);
+
+       g_object_unref (image);
+}
+
+
 GthImage *
 gth_image_task_get_source (GthImageTask *self)
 {
@@ -111,6 +124,13 @@ gth_image_task_get_source (GthImageTask *self)
 }
 
 
+cairo_surface_t *
+gth_image_task_get_source_surface (GthImageTask *self)
+{
+       return gth_image_get_cairo_surface (self->priv->source);
+}
+
+
 void
 gth_image_task_set_destination (GthImageTask *self,
                                GthImage     *destination)
@@ -123,6 +143,19 @@ gth_image_task_set_destination (GthImageTask *self,
 }
 
 
+void
+gth_image_task_set_destination_surface (GthImageTask    *self,
+                                       cairo_surface_t *surface)
+{
+       GthImage *image;
+
+       image = gth_image_new_for_surface (surface);
+       gth_image_task_set_destination (self, image);
+
+       g_object_unref (image);
+}
+
+
 GthImage *
 gth_image_task_get_destination (GthImageTask *self)
 {
@@ -130,6 +163,13 @@ gth_image_task_get_destination (GthImageTask *self)
 }
 
 
+cairo_surface_t *
+gth_image_task_get_destination_surface (GthImageTask *self)
+{
+       return gth_image_get_cairo_surface (self->priv->destination);
+}
+
+
 void
 gth_image_task_copy_source_to_destination (GthImageTask *self)
 {
diff --git a/gthumb/gth-image-task.h b/gthumb/gth-image-task.h
index c084f9e..86200df 100644
--- a/gthumb/gth-image-task.h
+++ b/gthumb/gth-image-task.h
@@ -48,21 +48,26 @@ struct _GthImageTaskClass {
        GthAsyncTaskClass __parent;
 };
 
-GType         gth_image_task_get_type        (void);
-GthTask *     gth_image_task_new             (const char         *description,
-                                             GthAsyncInitFunc    before_func,
-                                             GthAsyncThreadFunc  exec_func,
-                                             GthAsyncReadyFunc   after_func,
-                                             gpointer            user_data,
-                                             GDestroyNotify      user_data_destroy_func);
-void          gth_image_task_set_source      (GthImageTask       *self,
-                                             GthImage           *source);
-GthImage *    gth_image_task_get_source      (GthImageTask       *self);
-void          gth_image_task_set_destination (GthImageTask       *self,
-                                             GthImage           *destination);
-GthImage *    gth_image_task_get_destination (GthImageTask       *self);
-void          gth_image_task_copy_source_to_destination
-                                            (GthImageTask       *self);
+GType                  gth_image_task_get_type                         (void);
+GthTask *              gth_image_task_new                              (const char             *description,
+                                                                        GthAsyncInitFunc        before_func,
+                                                                        GthAsyncThreadFunc      exec_func,
+                                                                        GthAsyncReadyFunc       after_func,
+                                                                        gpointer                user_data,
+                                                                        GDestroyNotify          
user_data_destroy_func);
+void                   gth_image_task_set_source                       (GthImageTask           *self,
+                                                                        GthImage               *source);
+void                   gth_image_task_set_source_surface               (GthImageTask           *self,
+                                                                        cairo_surface_t        *surface);
+GthImage *             gth_image_task_get_source                       (GthImageTask           *self);
+cairo_surface_t *      gth_image_task_get_source_surface               (GthImageTask           *self);
+void                   gth_image_task_set_destination                  (GthImageTask           *self,
+                                                                        GthImage               *destination);
+void                   gth_image_task_set_destination_surface          (GthImageTask           *self,
+                                                                        cairo_surface_t        *surface);
+GthImage *             gth_image_task_get_destination                  (GthImageTask           *self);
+cairo_surface_t *      gth_image_task_get_destination_surface          (GthImageTask           *self);
+void                   gth_image_task_copy_source_to_destination       (GthImageTask           *self);
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]