gth-image-histogram.c 4.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  GThumb
 *
 *  Copyright (C) 2010 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>
23
#include <math.h>
24 25 26 27
#include <glib/gi18n.h>
#include <gthumb.h>
#include "gth-image-histogram.h"
#include "gth-image-viewer-page.h"
28
#include "preferences.h"
29

30

31
#define MIN_HISTOGRAM_HEIGHT 280
32 33


34 35 36
static void gth_image_histogram_gth_property_view_interface_init (GthPropertyViewInterface *iface);


37 38 39 40 41 42
struct _GthImageHistogramPrivate {
	GthHistogram *histogram;
	GtkWidget    *histogram_view;
};


43 44 45 46 47 48 49 50
G_DEFINE_TYPE_WITH_CODE (GthImageHistogram,
			 gth_image_histogram,
			 GTK_TYPE_BOX,
			 G_ADD_PRIVATE (GthImageHistogram)
			 G_IMPLEMENT_INTERFACE (GTH_TYPE_PROPERTY_VIEW,
						gth_image_histogram_gth_property_view_interface_init))


51
static gboolean
52 53 54 55 56
gth_image_histogram_real_set_file (GthPropertyView *base,
		 		   GthFileData     *file_data)
{
	GthImageHistogram *self = GTH_IMAGE_HISTOGRAM (base);
	GthBrowser        *browser;
57
	GthViewerPage     *viewer_page;
58 59

	if (file_data == NULL) {
60
		gth_histogram_calculate_for_image (self->priv->histogram, NULL);
61
		return FALSE;
62 63 64
	}

	browser = (GthBrowser *) gtk_widget_get_toplevel (GTK_WIDGET (base));
65 66
	if (! gtk_widget_is_toplevel (GTK_WIDGET (browser)))
		return FALSE;
67 68

	viewer_page = gth_browser_get_viewer_page (browser);
69 70
	if (! GTH_IS_IMAGE_VIEWER_PAGE (viewer_page))
		return FALSE;
71

72
	gth_histogram_calculate_for_image (self->priv->histogram, gth_image_viewer_page_get_current_image (GTH_IMAGE_VIEWER_PAGE (viewer_page)));
73 74

	return TRUE;
75 76 77
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
78
static const char *
79
gth_image_histogram_real_get_name (GthPropertyView *self)
80 81 82 83 84
{
	return _("Histogram");
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
85
static const char *
86
gth_image_histogram_real_get_icon (GthPropertyView *self)
87
{
88
	return "histogram-symbolic";
89 90 91 92 93 94 95 96 97
}


static void
gth_image_histogram_finalize (GObject *base)
{
	GthImageHistogram *self = GTH_IMAGE_HISTOGRAM (base);

	g_object_unref (self->priv->histogram);
98
	G_OBJECT_CLASS (gth_image_histogram_parent_class)->finalize (base);
99 100 101 102 103 104 105 106 107 108 109
}


static void
gth_image_histogram_class_init (GthImageHistogramClass *klass)
{
	G_OBJECT_CLASS (klass)->finalize = gth_image_histogram_finalize;
}


static void
110
gth_image_histogram_gth_property_view_interface_init (GthPropertyViewInterface *iface)
111 112 113 114 115 116 117
{
	iface->get_name = gth_image_histogram_real_get_name;
	iface->get_icon = gth_image_histogram_real_get_icon;
	iface->set_file = gth_image_histogram_real_set_file;
}


118 119 120 121 122 123 124 125 126 127 128 129 130 131
static void
histogram_view_notify_scale_type_cb (GObject    *gobject,
				     GParamSpec *pspec,
				     gpointer    user_data)
{
	GthImageHistogram *self = user_data;
	GSettings         *settings;

	settings = g_settings_new (GTHUMB_IMAGE_VIEWER_SCHEMA);
	g_settings_set_enum (settings, PREF_IMAGE_VIEWER_HISTOGRAM_SCALE, gth_histogram_view_get_scale_type (GTH_HISTOGRAM_VIEW (self->priv->histogram_view)));
	g_object_unref (settings);
}


132 133
static void
gth_image_histogram_init (GthImageHistogram *self)
134
{
135 136
	GSettings *settings;

137
	self->priv = gth_image_histogram_get_instance_private (self);
138
	self->priv->histogram = gth_histogram_new ();
139

140
	gtk_orientable_set_orientation (GTK_ORIENTABLE (self), GTK_ORIENTATION_VERTICAL);
141 142
	gtk_box_set_spacing (GTK_BOX (self), 6);

143 144
	settings = g_settings_new (GTHUMB_IMAGE_VIEWER_SCHEMA);

145 146
	self->priv->histogram_view = gth_histogram_view_new (self->priv->histogram);
	gth_histogram_view_show_info (GTH_HISTOGRAM_VIEW (self->priv->histogram_view), TRUE);
147
	gth_histogram_view_set_scale_type (GTH_HISTOGRAM_VIEW (self->priv->histogram_view), g_settings_get_enum (settings, PREF_IMAGE_VIEWER_HISTOGRAM_SCALE));
148 149 150
	gtk_widget_set_size_request (self->priv->histogram_view, -1, MIN_HISTOGRAM_HEIGHT);
	gtk_widget_show (self->priv->histogram_view);
	gtk_box_pack_start (GTK_BOX (self), self->priv->histogram_view, FALSE, FALSE, 0);
151 152 153 154 155 156
	g_signal_connect (self->priv->histogram_view,
			  "notify::scale-type",
			  G_CALLBACK (histogram_view_notify_scale_type_cb),
			  self);

	g_object_unref (settings);
157
}