blob: 17d970b70f002851cc6175a9a909e9c1b7c56654 [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{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050037 GtkScrolledWindow parent;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040038};
39
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040040struct _MediaSettingsViewClass
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040041{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050042 GtkScrolledWindowClass parent_class;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040043};
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 Salenikovich1118d7a2016-02-05 15:52:55 -050049 GtkWidget *vbox_main;
50
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -040051 /* audio settings */
52 GtkWidget *combobox_manager;
53 GtkWidget *combobox_ringtone;
54 GtkWidget *combobox_output;
55 GtkWidget *combobox_input;
56
57 QMetaObject::Connection manager_selection;
58 QMetaObject::Connection ringtone_selection;
59 QMetaObject::Connection output_selection;
60 QMetaObject::Connection input_selection;
61
62 /* camera settings */
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040063 GtkWidget *combobox_device;
64 GtkWidget *combobox_channel;
65 GtkWidget *combobox_resolution;
66 GtkWidget *combobox_framerate;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040067 GtkWidget *video_widget;
68
Stepan Salenikovich41118912015-05-01 11:25:46 -040069 /* this is used to keep track of the state of the preview when the settings
70 * are opened; if a call is in progress, then the preview should already be
71 * started and we don't want to stop it when the settings are closed, in this
72 * case */
73 gboolean video_started_by_settings;
74
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040075 QMetaObject::Connection local_renderer_connection;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040076 QMetaObject::Connection device_selection;
77 QMetaObject::Connection channel_selection;
78 QMetaObject::Connection resolution_selection;
79 QMetaObject::Connection rate_selection;
80};
81
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050082G_DEFINE_TYPE_WITH_PRIVATE(MediaSettingsView, media_settings_view, GTK_TYPE_SCROLLED_WINDOW);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040083
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040084#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 -040085
86static void
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040087media_settings_view_dispose(GObject *object)
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040088{
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040089 MediaSettingsView *view = MEDIA_SETTINGS_VIEW(object);
90 MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(view);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040091
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -040092 /* make sure to stop the preview if this view is getting destroyed */
93 if (priv->video_started_by_settings) {
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040094 Video::PreviewManager::instance().stopPreview();
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -040095 priv->video_started_by_settings = FALSE;
96 }
97
98 QObject::disconnect(priv->manager_selection);
99 QObject::disconnect(priv->ringtone_selection);
100 QObject::disconnect(priv->output_selection);
101 QObject::disconnect(priv->input_selection);
102
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400103 QObject::disconnect(priv->local_renderer_connection);
104 QObject::disconnect(priv->device_selection);
105 QObject::disconnect(priv->channel_selection);
106 QObject::disconnect(priv->resolution_selection);
107 QObject::disconnect(priv->rate_selection);
108
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400109 G_OBJECT_CLASS(media_settings_view_parent_class)->dispose(object);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400110}
111
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400112static QModelIndex
113get_index_from_combobox(GtkComboBox *box)
114{
115 GtkTreeIter iter;
116 GtkTreeModel *model = gtk_combo_box_get_model(box);
117 if (gtk_combo_box_get_active_iter(box, &iter)) {
118 return gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
119 } else {
120 return QModelIndex();
121 }
122}
123
124static void
125update_selection(GtkComboBox *box, QItemSelectionModel *selection_model)
126{
127 QModelIndex idx = get_index_from_combobox(box);
128 if (idx.isValid())
129 selection_model->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect);
130}
131
132static QMetaObject::Connection
133connect_combo_box_qmodel(GtkComboBox *box, QAbstractItemModel *qmodel, QItemSelectionModel *selection_model)
134{
135 QMetaObject::Connection connection;
136 GtkCellRenderer *renderer;
137 GtkQTreeModel *model = gtk_q_tree_model_new(qmodel,
138 1,
aviau9bbb19b2016-05-16 15:53:44 -0400139 0, Qt::DisplayRole, G_TYPE_STRING);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400140
141 gtk_combo_box_set_model(box, GTK_TREE_MODEL(model));
142 renderer = gtk_cell_renderer_text_new();
143 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(box), renderer, FALSE);
144 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(box), renderer,
145 "text", 0, NULL);
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -0500146 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400147
148 /* connect signals to and from the selection model */
149 connection = QObject::connect(
150 selection_model,
151 &QItemSelectionModel::currentChanged,
152 [=](const QModelIndex & current, G_GNUC_UNUSED const QModelIndex & previous) {
153 /* select the current */
154 if (current.isValid()) {
155 GtkTreeIter new_iter;
156 GtkTreeModel *model = gtk_combo_box_get_model(box);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400157 g_return_if_fail(model);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400158 if (gtk_q_tree_model_source_index_to_iter(GTK_Q_TREE_MODEL(model), current, &new_iter)) {
159 gtk_combo_box_set_active_iter(box, &new_iter);
160 } else {
161 g_warning("SelectionModel changed to invalid QModelIndex?");
162 }
163 }
164 }
165 );
166 g_signal_connect(box,
167 "changed",
168 G_CALLBACK(update_selection),
169 selection_model);
170
171 /* sync the initial selection */
172 QModelIndex idx = selection_model->currentIndex();
173 if (idx.isValid()) {
174 GtkTreeIter iter;
175 if (gtk_q_tree_model_source_index_to_iter(model, idx, &iter))
176 gtk_combo_box_set_active_iter(box, &iter);
177 }
178
179 return connection;
180}
181
182static void
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400183media_settings_view_init(MediaSettingsView *view)
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400184{
185 gtk_widget_init_template(GTK_WIDGET(view));
186
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400187 MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(view);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400188
Stepan Salenikovich13b7fa12016-03-04 14:45:17 -0500189 /* video settings */
190 /* TODO: this is a workaround for a possible LRC issue
191 * make sure all the models are instantiated before making a selection
192 */
193 Video::ConfigurationProxy::rateModel();
194 Video::ConfigurationProxy::resolutionModel();
195 Video::ConfigurationProxy::channelModel();
196 Video::ConfigurationProxy::deviceModel();
197
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400198 priv->device_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_device),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400199 &Video::ConfigurationProxy::deviceModel(),
200 &Video::ConfigurationProxy::deviceSelectionModel());
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400201 priv->channel_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_channel),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400202 &Video::ConfigurationProxy::channelModel(),
203 &Video::ConfigurationProxy::channelSelectionModel());
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400204 priv->resolution_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_resolution),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400205 &Video::ConfigurationProxy::resolutionModel(),
206 &Video::ConfigurationProxy::resolutionSelectionModel());
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400207 priv->rate_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_framerate),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400208 &Video::ConfigurationProxy::rateModel(),
209 &Video::ConfigurationProxy::rateSelectionModel());
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400210
211 /* audio settings */
Stepan Salenikovich13b7fa12016-03-04 14:45:17 -0500212 /* TODO: this is a workaround for a possible LRC issue
213 * make sure all the models are instantiated before making a selection
214 */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400215 Audio::Settings::instance().alsaPluginModel();
216 Audio::Settings::instance().ringtoneDeviceModel();
217 Audio::Settings::instance().inputDeviceModel();
218 Audio::Settings::instance().outputDeviceModel();
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400219 priv->manager_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_manager),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400220 Audio::Settings::instance().managerModel(),
221 Audio::Settings::instance().managerModel()->selectionModel());
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400222 priv->ringtone_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_ringtone),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400223 Audio::Settings::instance().ringtoneDeviceModel(),
224 Audio::Settings::instance().ringtoneDeviceModel()->selectionModel());
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400225 priv->input_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_input),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400226 Audio::Settings::instance().inputDeviceModel(),
227 Audio::Settings::instance().inputDeviceModel()->selectionModel());
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400228 priv->output_selection = connect_combo_box_qmodel(GTK_COMBO_BOX(priv->combobox_output),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400229 Audio::Settings::instance().outputDeviceModel(),
230 Audio::Settings::instance().outputDeviceModel()->selectionModel());
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400231
232
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400233}
234
235static void
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400236media_settings_view_class_init(MediaSettingsViewClass *klass)
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400237{
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400238 G_OBJECT_CLASS(klass)->dispose = media_settings_view_dispose;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400239
240 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400241 "/cx/ring/RingGnome/mediasettingsview.ui");
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400242
Stepan Salenikovich1118d7a2016-02-05 15:52:55 -0500243 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, vbox_main);
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400244 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_manager);
245 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_ringtone);
246 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_output);
247 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_input);
248 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_device);
249 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_channel);
250 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_resolution);
251 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_framerate);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400252}
253
254GtkWidget *
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400255media_settings_view_new()
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400256{
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400257 gpointer view = g_object_new(MEDIA_SETTINGS_VIEW_TYPE, NULL);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400258
259 return (GtkWidget *)view;
260}
Stepan Salenikovich41118912015-05-01 11:25:46 -0400261
262void
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400263media_settings_view_show_preview(MediaSettingsView *self, gboolean show_preview)
Stepan Salenikovich41118912015-05-01 11:25:46 -0400264{
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400265 g_return_if_fail(IS_MEDIA_SETTINGS_VIEW(self));
266 MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
Stepan Salenikovich41118912015-05-01 11:25:46 -0400267
268 /* if TRUE, create a VideoWidget, then check if the preview has already been
269 * started (because a call was in progress); if not, then start it.
270 * if FALSE, check if the preview was started by this function, if so
271 * then stop the preview; then destroy the VideoWidget to make sure we don't
272 * get useless frame updates */
273
274 if (show_preview) {
275 /* put video widget in */
276 priv->video_widget = video_widget_new();
277 gtk_widget_show_all(priv->video_widget);
Stepan Salenikovich1118d7a2016-02-05 15:52:55 -0500278 gtk_box_pack_start(GTK_BOX(priv->vbox_main), priv->video_widget, TRUE, TRUE, 0);
279 // set minimum size for video so it doesn't shrink too much
Adrien BĂ©raudcf3e08c2016-03-03 22:55:21 -0500280 gtk_widget_set_size_request(priv->video_widget, 400, -1);
Stepan Salenikovich41118912015-05-01 11:25:46 -0400281
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400282 if (Video::PreviewManager::instance().isPreviewing()) {
Stepan Salenikovich41118912015-05-01 11:25:46 -0400283 priv->video_started_by_settings = FALSE;
284
285 /* local renderer, but set as "remote" so that it takes up the whole screen */
286 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400287 Video::PreviewManager::instance().previewRenderer(),
Stepan Salenikovich41118912015-05-01 11:25:46 -0400288 VIDEO_RENDERER_REMOTE);
289 } else {
290 priv->video_started_by_settings = TRUE;
291 priv->local_renderer_connection = QObject::connect(
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400292 &Video::PreviewManager::instance(),
Stepan Salenikovich41118912015-05-01 11:25:46 -0400293 &Video::PreviewManager::previewStarted,
294 [=](Video::Renderer *renderer) {
295 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
296 renderer,
297 VIDEO_RENDERER_REMOTE);
298 }
299 );
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400300 Video::PreviewManager::instance().startPreview();
Stepan Salenikovich41118912015-05-01 11:25:46 -0400301 }
302 } else {
303 if (priv->video_started_by_settings) {
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400304 Video::PreviewManager::instance().stopPreview();
Stepan Salenikovich41118912015-05-01 11:25:46 -0400305 QObject::disconnect(priv->local_renderer_connection);
306 priv->video_started_by_settings = FALSE;
307 }
308
309 if (priv->video_widget && IS_VIDEO_WIDGET(priv->video_widget))
Stepan Salenikovich1118d7a2016-02-05 15:52:55 -0500310 gtk_container_remove(GTK_CONTAINER(priv->vbox_main), priv->video_widget);
Stepan Salenikovich41118912015-05-01 11:25:46 -0400311 priv->video_widget = NULL;
312 }
313
314}