blob: bc63b153126b47af00bb20415eb9f7eb20100a68 [file] [log] [blame]
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Stepan Salenikovich61cbab02015-03-16 18:35:10 -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 Salenikovich61cbab02015-03-16 18:35:10 -040018 */
19
20#include "accountvideotab.h"
21
22#include <gtk/gtk.h>
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040023#include <glib/gi18n.h>
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040024#include <account.h>
Edric Milaret58e71072016-03-21 12:16:37 -040025#include <codecmodel.h>
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040026#include "models/gtkqsortfiltertreemodel.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040027#include "utils/models.h"
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040028
29struct _AccountVideoTab
30{
31 GtkBox parent;
32};
33
34struct _AccountVideoTabClass
35{
36 GtkBoxClass parent_class;
37};
38
39typedef struct _AccountVideoTabPrivate AccountVideoTabPrivate;
40
41struct _AccountVideoTabPrivate
42{
43 Account *account;
44 GtkWidget *treeview_codecs;
45 GtkWidget *checkbutton_enable;
46 GtkWidget *button_moveup;
47 GtkWidget *button_movedown;
48};
49
50G_DEFINE_TYPE_WITH_PRIVATE(AccountVideoTab, account_video_tab, GTK_TYPE_BOX);
51
52#define ACCOUNT_VIDEO_TAB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_VIDEO_TAB_TYPE, AccountVideoTabPrivate))
53
54static void
55account_video_tab_dispose(GObject *object)
56{
57 G_OBJECT_CLASS(account_video_tab_parent_class)->dispose(object);
58}
59
60static void
61account_video_tab_init(AccountVideoTab *view)
62{
63 gtk_widget_init_template(GTK_WIDGET(view));
64}
65
66static void
67account_video_tab_class_init(AccountVideoTabClass *klass)
68{
69 G_OBJECT_CLASS(klass)->dispose = account_video_tab_dispose;
70
71 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
72 "/cx/ring/RingGnome/accountvideotab.ui");
73
74 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountVideoTab, treeview_codecs);
75 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountVideoTab, checkbutton_enable);
76 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountVideoTab, button_moveup);
77 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountVideoTab, button_movedown);
78}
79
80static void
81video_enable(GtkToggleButton *checkbutton, AccountVideoTab *view)
82{
83 g_return_if_fail(IS_ACCOUNT_VIDEO_TAB(view));
84 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
85 priv->account->setVideoEnabled(gtk_toggle_button_get_active(checkbutton));
86}
87
88static void
89codec_active_toggled(GtkCellRendererToggle *renderer, gchar *path, AccountVideoTab *view)
90{
91 g_return_if_fail(IS_ACCOUNT_VIDEO_TAB(view));
92 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
93
94 /* we want to set it to the opposite of the current value */
95 gboolean toggle = !gtk_cell_renderer_toggle_get_active(renderer);
96
97 /* get iter which was clicked */
98 GtkTreePath *tree_path = gtk_tree_path_new_from_string(path);
99 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_codecs));
100 GtkTreeIter iter;
101 gtk_tree_model_get_iter(model, &iter, tree_path);
102
103 /* get qmodelindex from iter and set the model data */
104 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(model), &iter);
105 if (idx.isValid()) {
106 priv->account->codecModel()->videoCodecs()->setData(idx, QVariant(toggle), Qt::CheckStateRole);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400107 }
108}
109
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400110static void
111move_selected_codec(AccountVideoTab *view, int position_diff)
112{
113 g_return_if_fail(IS_ACCOUNT_VIDEO_TAB(view));
114 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
115
116 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_codecs));
117 QModelIndex idx = get_index_from_selection(selection);
118
119 if(!idx.isValid())
120 return;
121
122 QMimeData* mime = priv->account->codecModel()->videoCodecs()->mimeData(QModelIndexList() << idx);
123 priv->account->codecModel()->videoCodecs()->dropMimeData(
124 mime,
125 Qt::MoveAction,
126 idx.row() + position_diff,
127 0,
128 QModelIndex());
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400129
130 /* now make sure to select the same codec which was moved
131 * TODO: UGLY! this should be somehow done in the qt modle bindings,
132 * or via a selection model, not here
133 */
134 int new_row = idx.row() + position_diff;
135 int row_count = priv->account->codecModel()->videoCodecs()->rowCount(idx.parent());
136 if (new_row < 0)
137 new_row = row_count - 1;
138 else if (new_row >= row_count)
139 new_row = 0;
140
141 idx = idx.sibling(new_row, idx.column());
142 GtkTreeIter iter;
143 if (gtk_q_sort_filter_tree_model_source_index_to_iter(
144 GTK_Q_SORT_FILTER_TREE_MODEL(gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_codecs))),
145 idx,
146 &iter)) {
147 gtk_tree_selection_select_iter(selection, &iter);
148 }
149}
150
151static void
152move_codec_up(G_GNUC_UNUSED GtkButton *button, AccountVideoTab *view)
153{
154 move_selected_codec(view, -1);
155}
156
157static void
158move_codec_down(G_GNUC_UNUSED GtkButton *button, AccountVideoTab *view)
159{
160 move_selected_codec(view, +1);
161}
162
163static void
164build_tab_view(AccountVideoTab *view)
165{
166 g_return_if_fail(IS_ACCOUNT_VIDEO_TAB(view));
167 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
168
169 /* codec model */
170 GtkQSortFilterTreeModel *codec_model;
171 GtkCellRenderer *renderer;
172 GtkTreeViewColumn *column;
173
174 codec_model = gtk_q_sort_filter_tree_model_new(
175 priv->account->codecModel()->videoCodecs(),
176 3,
177 Qt::CheckStateRole, G_TYPE_BOOLEAN,
178 CodecModel::Role::NAME, G_TYPE_STRING,
179 CodecModel::Role::BITRATE, G_TYPE_STRING);
180 gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_codecs), GTK_TREE_MODEL(codec_model));
181
182 renderer = gtk_cell_renderer_toggle_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400183 column = gtk_tree_view_column_new_with_attributes(_("Enabled"), renderer, "active", 0, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400184 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
185
186 g_signal_connect(renderer, "toggled", G_CALLBACK(codec_active_toggled), view);
187
188 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400189 column = gtk_tree_view_column_new_with_attributes(C_("The name of the codec", "Name"), renderer, "text", 1, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400190 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
191
192 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400193 column = gtk_tree_view_column_new_with_attributes(_("Bitrate"), renderer, "text", 2, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400194 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
195
196 /* enable video checkbutton */
197 gtk_toggle_button_set_active(
198 GTK_TOGGLE_BUTTON(priv->checkbutton_enable), priv->account->isVideoEnabled());
199 g_signal_connect(priv->checkbutton_enable, "toggled", G_CALLBACK(video_enable), view);
200
201 /* connect move codecs up/down signals */
202 g_signal_connect(priv->button_moveup, "clicked", G_CALLBACK(move_codec_up), view);
203 g_signal_connect(priv->button_movedown, "clicked", G_CALLBACK(move_codec_down), view);
204}
205
206GtkWidget *
207account_video_tab_new(Account *account)
208{
209 g_return_val_if_fail(account != NULL, NULL);
210
211 gpointer view = g_object_new(ACCOUNT_VIDEO_TAB_TYPE, NULL);
212
213 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
214 priv->account = account;
215
216 build_tab_view(ACCOUNT_VIDEO_TAB(view));
217
218 return (GtkWidget *)view;
219}