blob: 2048e62992ff8345618bfc15285fd16ca00bbcad [file] [log] [blame]
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001/*
2 * Copyright (C) 2015 Savoir-Faire Linux Inc.
3 * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU 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 * This program 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 General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30
31#include "videosettingsview.h"
32
33#include <gtk/gtk.h>
34#include "models/gtkqtreemodel.h"
35#include "video/video_widget.h"
36#include <video/previewmanager.h>
37#include <video/configurationproxy.h>
38#include <QtCore/QItemSelectionModel>
39
40struct _VideoSettingsView
41{
42 GtkBox parent;
43};
44
45struct _VideoSettingsViewClass
46{
47 GtkBoxClass parent_class;
48};
49
50typedef struct _VideoSettingsViewPrivate VideoSettingsViewPrivate;
51
52struct _VideoSettingsViewPrivate
53{
54 GtkWidget *combobox_device;
55 GtkWidget *combobox_channel;
56 GtkWidget *combobox_resolution;
57 GtkWidget *combobox_framerate;
58 GtkWidget *button_startstop;
59 GtkWidget *vbox_camera_preview;
60 GtkWidget *video_widget;
61
Stepan Salenikovich41118912015-05-01 11:25:46 -040062 /* this is used to keep track of the state of the preview when the settings
63 * are opened; if a call is in progress, then the preview should already be
64 * started and we don't want to stop it when the settings are closed, in this
65 * case */
66 gboolean video_started_by_settings;
67
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040068 QMetaObject::Connection local_renderer_connection;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040069 QMetaObject::Connection device_selection;
70 QMetaObject::Connection channel_selection;
71 QMetaObject::Connection resolution_selection;
72 QMetaObject::Connection rate_selection;
73};
74
75G_DEFINE_TYPE_WITH_PRIVATE(VideoSettingsView, video_settings_view, GTK_TYPE_BOX);
76
77#define VIDEO_SETTINGS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), VIDEO_SETTINGS_VIEW_TYPE, VideoSettingsViewPrivate))
78
79static void
80video_settings_view_dispose(GObject *object)
81{
82 VideoSettingsView *view = VIDEO_SETTINGS_VIEW(object);
83 VideoSettingsViewPrivate *priv = VIDEO_SETTINGS_VIEW_GET_PRIVATE(view);
84
85 QObject::disconnect(priv->local_renderer_connection);
86 QObject::disconnect(priv->device_selection);
87 QObject::disconnect(priv->channel_selection);
88 QObject::disconnect(priv->resolution_selection);
89 QObject::disconnect(priv->rate_selection);
90
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040091 G_OBJECT_CLASS(video_settings_view_parent_class)->dispose(object);
92}
93
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040094static QModelIndex
95get_index_from_combobox(GtkComboBox *box)
96{
97 GtkTreeIter iter;
98 GtkTreeModel *model = gtk_combo_box_get_model(box);
99 if (gtk_combo_box_get_active_iter(box, &iter)) {
100 return gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
101 } else {
102 return QModelIndex();
103 }
104}
105
106static void
107update_selection(GtkComboBox *box, QItemSelectionModel *selection_model)
108{
109 QModelIndex idx = get_index_from_combobox(box);
110 if (idx.isValid())
111 selection_model->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect);
112}
113
114static QMetaObject::Connection
115connect_combo_box_qmodel(GtkComboBox *box, QAbstractItemModel *qmodel, QItemSelectionModel *selection_model)
116{
117 QMetaObject::Connection connection;
118 GtkCellRenderer *renderer;
119 GtkQTreeModel *model = gtk_q_tree_model_new(qmodel,
120 1,
121 Qt::DisplayRole, G_TYPE_STRING);
122
123 gtk_combo_box_set_model(box, GTK_TREE_MODEL(model));
124 renderer = gtk_cell_renderer_text_new();
125 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(box), renderer, FALSE);
126 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(box), renderer,
127 "text", 0, NULL);
128
129 /* connect signals to and from the selection model */
130 connection = QObject::connect(
131 selection_model,
132 &QItemSelectionModel::currentChanged,
133 [=](const QModelIndex & current, G_GNUC_UNUSED const QModelIndex & previous) {
134 /* select the current */
135 if (current.isValid()) {
136 GtkTreeIter new_iter;
137 GtkTreeModel *model = gtk_combo_box_get_model(box);
138 g_return_if_fail(model != NULL);
139 if (gtk_q_tree_model_source_index_to_iter(GTK_Q_TREE_MODEL(model), current, &new_iter)) {
140 gtk_combo_box_set_active_iter(box, &new_iter);
141 } else {
142 g_warning("SelectionModel changed to invalid QModelIndex?");
143 }
144 }
145 }
146 );
147 g_signal_connect(box,
148 "changed",
149 G_CALLBACK(update_selection),
150 selection_model);
151
152 /* sync the initial selection */
153 QModelIndex idx = selection_model->currentIndex();
154 if (idx.isValid()) {
155 GtkTreeIter iter;
156 if (gtk_q_tree_model_source_index_to_iter(model, idx, &iter))
157 gtk_combo_box_set_active_iter(box, &iter);
158 }
159
160 return connection;
161}
162
163static void
164video_settings_view_init(VideoSettingsView *view)
165{
166 gtk_widget_init_template(GTK_WIDGET(view));
167
168 VideoSettingsViewPrivate *priv = VIDEO_SETTINGS_VIEW_GET_PRIVATE(view);
169
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400170 priv->device_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_device),
171 Video::ConfigurationProxy::deviceModel(),
172 Video::ConfigurationProxy::deviceSelectionModel());
173 priv->channel_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_channel),
174 Video::ConfigurationProxy::channelModel(),
175 Video::ConfigurationProxy::channelSelectionModel());
176 priv->resolution_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_resolution),
177 Video::ConfigurationProxy::resolutionModel(),
178 Video::ConfigurationProxy::resolutionSelectionModel());
179 priv->rate_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_framerate),
180 Video::ConfigurationProxy::rateModel(),
181 Video::ConfigurationProxy::rateSelectionModel());
182}
183
184static void
185video_settings_view_class_init(VideoSettingsViewClass *klass)
186{
187 G_OBJECT_CLASS(klass)->dispose = video_settings_view_dispose;
188
189 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
190 "/cx/ring/RingGnome/videosettingsview.ui");
191
192 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), VideoSettingsView, combobox_device);
193 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), VideoSettingsView, combobox_channel);
194 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), VideoSettingsView, combobox_resolution);
195 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), VideoSettingsView, combobox_framerate);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400196 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), VideoSettingsView, vbox_camera_preview);
197}
198
199GtkWidget *
200video_settings_view_new()
201{
202 gpointer view = g_object_new(VIDEO_SETTINGS_VIEW_TYPE, NULL);
203
204 return (GtkWidget *)view;
205}
Stepan Salenikovich41118912015-05-01 11:25:46 -0400206
207void
208video_settings_show_preview(VideoSettingsView *self, gboolean show_preview)
209{
210 g_return_if_fail(IS_VIDEO_SETTINGS_VIEW(self));
211 VideoSettingsViewPrivate *priv = VIDEO_SETTINGS_VIEW_GET_PRIVATE(self);
212
213 /* if TRUE, create a VideoWidget, then check if the preview has already been
214 * started (because a call was in progress); if not, then start it.
215 * if FALSE, check if the preview was started by this function, if so
216 * then stop the preview; then destroy the VideoWidget to make sure we don't
217 * get useless frame updates */
218
219 if (show_preview) {
220 /* put video widget in */
221 priv->video_widget = video_widget_new();
222 gtk_widget_show_all(priv->video_widget);
223 gtk_box_pack_start(GTK_BOX(priv->vbox_camera_preview), priv->video_widget, TRUE, TRUE, 0);
224
225 if (Video::PreviewManager::instance()->isPreviewing()) {
226 priv->video_started_by_settings = FALSE;
227
228 /* local renderer, but set as "remote" so that it takes up the whole screen */
229 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
230 Video::PreviewManager::instance()->previewRenderer(),
231 VIDEO_RENDERER_REMOTE);
232 } else {
233 priv->video_started_by_settings = TRUE;
234 priv->local_renderer_connection = QObject::connect(
235 Video::PreviewManager::instance(),
236 &Video::PreviewManager::previewStarted,
237 [=](Video::Renderer *renderer) {
238 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
239 renderer,
240 VIDEO_RENDERER_REMOTE);
241 }
242 );
243 Video::PreviewManager::instance()->startPreview();
244 }
245 } else {
246 if (priv->video_started_by_settings) {
247 Video::PreviewManager::instance()->stopPreview();
248 QObject::disconnect(priv->local_renderer_connection);
249 priv->video_started_by_settings = FALSE;
250 }
251
252 if (priv->video_widget && IS_VIDEO_WIDGET(priv->video_widget))
253 gtk_container_remove(GTK_CONTAINER(priv->vbox_camera_preview), priv->video_widget);
254 priv->video_widget = NULL;
255 }
256
257}