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