blob: 3d8fe01ae1f4c6c695bba7b616fafa00c1d13c09 [file] [log] [blame]
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04003 * 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.
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040018 */
19
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040020#include "mediasettingsview.h"
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040021
22#include <gtk/gtk.h>
23#include "models/gtkqtreemodel.h"
24#include "video/video_widget.h"
25#include <video/previewmanager.h>
26#include <video/configurationproxy.h>
27#include <QtCore/QItemSelectionModel>
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -040028#include <audio/settings.h>
29#include <audio/managermodel.h>
30#include <audio/alsapluginmodel.h>
31#include <audio/outputdevicemodel.h>
32#include <audio/inputdevicemodel.h>
33#include <audio/ringtonedevicemodel.h>
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040034
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040035struct _MediaSettingsView
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040036{
37 GtkBox parent;
38};
39
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040040struct _MediaSettingsViewClass
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040041{
42 GtkBoxClass parent_class;
43};
44
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040045typedef struct _MediaSettingsViewPrivate MediaSettingsViewPrivate;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040046
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040047struct _MediaSettingsViewPrivate
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040048{
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -040049 /* audio settings */
50 GtkWidget *combobox_manager;
51 GtkWidget *combobox_ringtone;
52 GtkWidget *combobox_output;
53 GtkWidget *combobox_input;
54
55 QMetaObject::Connection manager_selection;
56 QMetaObject::Connection ringtone_selection;
57 QMetaObject::Connection output_selection;
58 QMetaObject::Connection input_selection;
59
60 /* camera settings */
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040061 GtkWidget *combobox_device;
62 GtkWidget *combobox_channel;
63 GtkWidget *combobox_resolution;
64 GtkWidget *combobox_framerate;
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -040065 GtkWidget *hbox_camera;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040066 GtkWidget *video_widget;
67
Stepan Salenikovich41118912015-05-01 11:25:46 -040068 /* this is used to keep track of the state of the preview when the settings
69 * are opened; if a call is in progress, then the preview should already be
70 * started and we don't want to stop it when the settings are closed, in this
71 * case */
72 gboolean video_started_by_settings;
73
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040074 QMetaObject::Connection local_renderer_connection;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040075 QMetaObject::Connection device_selection;
76 QMetaObject::Connection channel_selection;
77 QMetaObject::Connection resolution_selection;
78 QMetaObject::Connection rate_selection;
79};
80
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040081G_DEFINE_TYPE_WITH_PRIVATE(MediaSettingsView, media_settings_view, GTK_TYPE_BOX);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040082
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040083#define MEDIA_SETTINGS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), MEDIA_SETTINGS_VIEW_TYPE, MediaSettingsViewPrivate))
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040084
85static void
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040086media_settings_view_dispose(GObject *object)
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040087{
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040088 MediaSettingsView *view = MEDIA_SETTINGS_VIEW(object);
89 MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(view);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040090
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -040091 /* make sure to stop the preview if this view is getting destroyed */
92 if (priv->video_started_by_settings) {
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040093 Video::PreviewManager::instance().stopPreview();
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -040094 priv->video_started_by_settings = FALSE;
95 }
96
97 QObject::disconnect(priv->manager_selection);
98 QObject::disconnect(priv->ringtone_selection);
99 QObject::disconnect(priv->output_selection);
100 QObject::disconnect(priv->input_selection);
101
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400102 QObject::disconnect(priv->local_renderer_connection);
103 QObject::disconnect(priv->device_selection);
104 QObject::disconnect(priv->channel_selection);
105 QObject::disconnect(priv->resolution_selection);
106 QObject::disconnect(priv->rate_selection);
107
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400108 G_OBJECT_CLASS(media_settings_view_parent_class)->dispose(object);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400109}
110
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400111static QModelIndex
112get_index_from_combobox(GtkComboBox *box)
113{
114 GtkTreeIter iter;
115 GtkTreeModel *model = gtk_combo_box_get_model(box);
116 if (gtk_combo_box_get_active_iter(box, &iter)) {
117 return gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
118 } else {
119 return QModelIndex();
120 }
121}
122
123static void
124update_selection(GtkComboBox *box, QItemSelectionModel *selection_model)
125{
126 QModelIndex idx = get_index_from_combobox(box);
127 if (idx.isValid())
128 selection_model->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect);
129}
130
131static QMetaObject::Connection
132connect_combo_box_qmodel(GtkComboBox *box, QAbstractItemModel *qmodel, QItemSelectionModel *selection_model)
133{
134 QMetaObject::Connection connection;
135 GtkCellRenderer *renderer;
136 GtkQTreeModel *model = gtk_q_tree_model_new(qmodel,
137 1,
138 Qt::DisplayRole, G_TYPE_STRING);
139
140 gtk_combo_box_set_model(box, GTK_TREE_MODEL(model));
141 renderer = gtk_cell_renderer_text_new();
142 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(box), renderer, FALSE);
143 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(box), renderer,
144 "text", 0, NULL);
145
146 /* connect signals to and from the selection model */
147 connection = QObject::connect(
148 selection_model,
149 &QItemSelectionModel::currentChanged,
150 [=](const QModelIndex & current, G_GNUC_UNUSED const QModelIndex & previous) {
151 /* select the current */
152 if (current.isValid()) {
153 GtkTreeIter new_iter;
154 GtkTreeModel *model = gtk_combo_box_get_model(box);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400155 g_return_if_fail(model);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400156 if (gtk_q_tree_model_source_index_to_iter(GTK_Q_TREE_MODEL(model), current, &new_iter)) {
157 gtk_combo_box_set_active_iter(box, &new_iter);
158 } else {
159 g_warning("SelectionModel changed to invalid QModelIndex?");
160 }
161 }
162 }
163 );
164 g_signal_connect(box,
165 "changed",
166 G_CALLBACK(update_selection),
167 selection_model);
168
169 /* sync the initial selection */
170 QModelIndex idx = selection_model->currentIndex();
171 if (idx.isValid()) {
172 GtkTreeIter iter;
173 if (gtk_q_tree_model_source_index_to_iter(model, idx, &iter))
174 gtk_combo_box_set_active_iter(box, &iter);
175 }
176
177 return connection;
178}
179
180static void
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400181media_settings_view_init(MediaSettingsView *view)
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400182{
183 gtk_widget_init_template(GTK_WIDGET(view));
184
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400185 MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(view);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400186
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400187 priv->device_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_device),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400188 &Video::ConfigurationProxy::deviceModel(),
189 &Video::ConfigurationProxy::deviceSelectionModel());
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400190 priv->channel_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_channel),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400191 &Video::ConfigurationProxy::channelModel(),
192 &Video::ConfigurationProxy::channelSelectionModel());
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400193 priv->resolution_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_resolution),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400194 &Video::ConfigurationProxy::resolutionModel(),
195 &Video::ConfigurationProxy::resolutionSelectionModel());
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400196 priv->rate_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_framerate),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400197 &Video::ConfigurationProxy::rateModel(),
198 &Video::ConfigurationProxy::rateSelectionModel());
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400199
200 /* audio settings */
201 /* instantiate all the models before the manager model first */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400202 Audio::Settings::instance().alsaPluginModel();
203 Audio::Settings::instance().ringtoneDeviceModel();
204 Audio::Settings::instance().inputDeviceModel();
205 Audio::Settings::instance().outputDeviceModel();
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400206 priv->manager_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_manager),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400207 Audio::Settings::instance().managerModel(),
208 Audio::Settings::instance().managerModel()->selectionModel());
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400209 priv->ringtone_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_ringtone),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400210 Audio::Settings::instance().ringtoneDeviceModel(),
211 Audio::Settings::instance().ringtoneDeviceModel()->selectionModel());
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400212 priv->input_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_input),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400213 Audio::Settings::instance().inputDeviceModel(),
214 Audio::Settings::instance().inputDeviceModel()->selectionModel());
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400215 priv->output_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_output),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400216 Audio::Settings::instance().outputDeviceModel(),
217 Audio::Settings::instance().outputDeviceModel()->selectionModel());
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400218
219
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400220}
221
222static void
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400223media_settings_view_class_init(MediaSettingsViewClass *klass)
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400224{
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400225 G_OBJECT_CLASS(klass)->dispose = media_settings_view_dispose;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400226
227 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400228 "/cx/ring/RingGnome/mediasettingsview.ui");
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400229
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400230 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_manager);
231 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_ringtone);
232 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_output);
233 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_input);
234 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_device);
235 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_channel);
236 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_resolution);
237 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_framerate);
238 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, hbox_camera);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400239}
240
241GtkWidget *
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400242media_settings_view_new()
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400243{
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400244 gpointer view = g_object_new(MEDIA_SETTINGS_VIEW_TYPE, NULL);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400245
246 return (GtkWidget *)view;
247}
Stepan Salenikovich41118912015-05-01 11:25:46 -0400248
249void
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400250media_settings_view_show_preview(MediaSettingsView *self, gboolean show_preview)
Stepan Salenikovich41118912015-05-01 11:25:46 -0400251{
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400252 g_return_if_fail(IS_MEDIA_SETTINGS_VIEW(self));
253 MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
Stepan Salenikovich41118912015-05-01 11:25:46 -0400254
255 /* if TRUE, create a VideoWidget, then check if the preview has already been
256 * started (because a call was in progress); if not, then start it.
257 * if FALSE, check if the preview was started by this function, if so
258 * then stop the preview; then destroy the VideoWidget to make sure we don't
259 * get useless frame updates */
260
261 if (show_preview) {
262 /* put video widget in */
263 priv->video_widget = video_widget_new();
264 gtk_widget_show_all(priv->video_widget);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400265 gtk_box_pack_start(GTK_BOX(priv->hbox_camera), priv->video_widget, TRUE, TRUE, 0);
Stepan Salenikovich41118912015-05-01 11:25:46 -0400266
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400267 if (Video::PreviewManager::instance().isPreviewing()) {
Stepan Salenikovich41118912015-05-01 11:25:46 -0400268 priv->video_started_by_settings = FALSE;
269
270 /* local renderer, but set as "remote" so that it takes up the whole screen */
271 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400272 Video::PreviewManager::instance().previewRenderer(),
Stepan Salenikovich41118912015-05-01 11:25:46 -0400273 VIDEO_RENDERER_REMOTE);
274 } else {
275 priv->video_started_by_settings = TRUE;
276 priv->local_renderer_connection = QObject::connect(
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400277 &Video::PreviewManager::instance(),
Stepan Salenikovich41118912015-05-01 11:25:46 -0400278 &Video::PreviewManager::previewStarted,
279 [=](Video::Renderer *renderer) {
280 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
281 renderer,
282 VIDEO_RENDERER_REMOTE);
283 }
284 );
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400285 Video::PreviewManager::instance().startPreview();
Stepan Salenikovich41118912015-05-01 11:25:46 -0400286 }
287 } else {
288 if (priv->video_started_by_settings) {
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400289 Video::PreviewManager::instance().stopPreview();
Stepan Salenikovich41118912015-05-01 11:25:46 -0400290 QObject::disconnect(priv->local_renderer_connection);
291 priv->video_started_by_settings = FALSE;
292 }
293
294 if (priv->video_widget && IS_VIDEO_WIDGET(priv->video_widget))
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400295 gtk_container_remove(GTK_CONTAINER(priv->hbox_camera), priv->video_widget);
Stepan Salenikovich41118912015-05-01 11:25:46 -0400296 priv->video_widget = NULL;
297 }
298
299}