Stepan Salenikovich | 9816a94 | 2015-04-22 17:49:16 -0400 | [diff] [blame] | 1 | /* |
| 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 "models.h" |
| 32 | |
| 33 | #include <gtk/gtk.h> |
| 34 | #include "../models/gtkqtreemodel.h" |
| 35 | #include "../models/gtkqsortfiltertreemodel.h" |
| 36 | #include <QtCore/QModelIndex> |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 37 | #include <QtCore/QItemSelectionModel> |
Stepan Salenikovich | 9816a94 | 2015-04-22 17:49:16 -0400 | [diff] [blame] | 38 | |
| 39 | QModelIndex |
| 40 | get_index_from_selection(GtkTreeSelection *selection) |
| 41 | { |
| 42 | GtkTreeIter iter; |
| 43 | GtkTreeModel *model = NULL; |
| 44 | |
| 45 | if (gtk_tree_selection_get_selected(selection, &model, &iter)) { |
| 46 | if (GTK_IS_Q_TREE_MODEL(model)) |
| 47 | return gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter); |
| 48 | else if (GTK_IS_Q_SORT_FILTER_TREE_MODEL(model)) |
| 49 | return gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(model), &iter); |
| 50 | } |
| 51 | return QModelIndex(); |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | QModelIndex |
| 55 | gtk_combo_box_get_index(GtkComboBox *box) |
| 56 | { |
| 57 | GtkTreeIter filter_iter; |
| 58 | GtkTreeIter child_iter; |
| 59 | GtkTreeModel *filter_model = gtk_combo_box_get_model(box); |
| 60 | GtkTreeModel *model = filter_model; |
| 61 | |
| 62 | GtkTreeIter *iter = NULL; |
| 63 | |
| 64 | if (GTK_IS_TREE_MODEL_FILTER(filter_model)) |
| 65 | model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(filter_model)); |
| 66 | |
| 67 | if (gtk_combo_box_get_active_iter(box, &filter_iter)) { |
| 68 | if (GTK_IS_TREE_MODEL_FILTER(filter_model)) { |
| 69 | gtk_tree_model_filter_convert_iter_to_child_iter( |
| 70 | GTK_TREE_MODEL_FILTER(filter_model), |
| 71 | &child_iter, |
| 72 | &filter_iter); |
| 73 | iter = &child_iter; |
| 74 | } else { |
| 75 | iter = &filter_iter; |
| 76 | } |
| 77 | |
| 78 | if (GTK_IS_Q_TREE_MODEL(model)) |
| 79 | return gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), iter); |
| 80 | else if (GTK_IS_Q_SORT_FILTER_TREE_MODEL(model)) |
| 81 | return gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(model), iter); |
| 82 | } |
| 83 | return QModelIndex(); |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | update_selection(GtkComboBox *box, QItemSelectionModel *selection_model) |
| 88 | { |
| 89 | QModelIndex idx = gtk_combo_box_get_index(box); |
| 90 | if (idx.isValid()) { |
| 91 | selection_model->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static gboolean |
| 96 | filter_disabled_items(GtkTreeModel *model, GtkTreeIter *iter, G_GNUC_UNUSED gpointer data) |
| 97 | { |
| 98 | QModelIndex idx; |
| 99 | if (GTK_IS_Q_TREE_MODEL(model)) |
| 100 | idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), iter); |
| 101 | else if (GTK_IS_Q_SORT_FILTER_TREE_MODEL(model)) |
| 102 | idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(model), iter); |
| 103 | |
| 104 | if (idx.isValid()) { |
| 105 | return idx.flags() & Qt::ItemIsEnabled ? TRUE : FALSE; |
| 106 | } |
| 107 | return FALSE; |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | gtk_combo_box_set_active_index(GtkComboBox *box, const QModelIndex& idx) |
| 112 | { |
| 113 | if (idx.isValid()) { |
| 114 | GtkTreeIter new_iter; |
| 115 | GtkTreeModel *filter_model = gtk_combo_box_get_model(box); |
| 116 | g_return_if_fail(filter_model); |
| 117 | GtkTreeModel *model = filter_model; |
| 118 | |
| 119 | if (GTK_IS_TREE_MODEL_FILTER(filter_model)) |
| 120 | model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(filter_model)); |
| 121 | |
Stepan Salenikovich | e97b940 | 2015-12-24 14:06:23 -0500 | [diff] [blame] | 122 | gboolean valid = FALSE; |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 123 | if (GTK_IS_Q_TREE_MODEL(model)) { |
| 124 | valid = gtk_q_tree_model_source_index_to_iter( |
| 125 | GTK_Q_TREE_MODEL(model), idx, &new_iter); |
| 126 | } else if (GTK_IS_Q_SORT_FILTER_TREE_MODEL(model)) { |
| 127 | valid = gtk_q_sort_filter_tree_model_source_index_to_iter( |
| 128 | GTK_Q_SORT_FILTER_TREE_MODEL(model), idx, &new_iter); |
| 129 | } |
| 130 | |
| 131 | if (valid) { |
| 132 | if (GTK_IS_TREE_MODEL_FILTER(filter_model)) { |
| 133 | GtkTreeIter filter_iter; |
| 134 | if (gtk_tree_model_filter_convert_child_iter_to_iter( |
| 135 | GTK_TREE_MODEL_FILTER(filter_model), |
| 136 | &filter_iter, |
| 137 | &new_iter) |
| 138 | ) { |
| 139 | gtk_combo_box_set_active_iter(box, &filter_iter); |
| 140 | } else { |
| 141 | g_warning("failed to convert iter from source model to filter model iter"); |
| 142 | } |
| 143 | } else { |
| 144 | gtk_combo_box_set_active_iter(box, &new_iter); |
| 145 | } |
| 146 | } else { |
| 147 | g_warning("Given QModelIndex doesn't exist in GtkTreeModel"); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | QMetaObject::Connection |
| 153 | gtk_combo_box_set_qmodel(GtkComboBox *box, QAbstractItemModel *qmodel, QItemSelectionModel *selection_model) |
| 154 | { |
| 155 | QMetaObject::Connection connection; |
| 156 | GtkTreeModel *model; |
| 157 | |
| 158 | /* check if its a QAbstractItemModel or a QSortFilterProxyModel */ |
| 159 | QSortFilterProxyModel *proxy_qmodel = qobject_cast<QSortFilterProxyModel*>(qmodel); |
| 160 | if (proxy_qmodel) { |
| 161 | model = (GtkTreeModel *)gtk_q_sort_filter_tree_model_new( |
| 162 | proxy_qmodel, |
| 163 | 1, |
| 164 | Qt::DisplayRole, G_TYPE_STRING); |
| 165 | } else { |
| 166 | model = (GtkTreeModel *)gtk_q_tree_model_new( |
| 167 | qmodel, |
| 168 | 1, |
| 169 | Qt::DisplayRole, G_TYPE_STRING); |
| 170 | } |
| 171 | |
| 172 | /* use a filter model to remove disabled items */ |
| 173 | GtkTreeModel *filter_model = gtk_tree_model_filter_new(model, NULL); |
| 174 | gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(filter_model), |
| 175 | (GtkTreeModelFilterVisibleFunc)filter_disabled_items, |
| 176 | NULL, NULL); |
| 177 | |
| 178 | gtk_combo_box_set_model(box, GTK_TREE_MODEL(filter_model)); |
| 179 | GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); |
| 180 | gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(box), renderer, FALSE); |
| 181 | gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(box), renderer, |
| 182 | "text", 0, NULL); |
| 183 | |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 184 | if (!selection_model) return connection; |
| 185 | |
Stepan Salenikovich | 9d29449 | 2015-05-14 16:34:24 -0400 | [diff] [blame] | 186 | /* sync the initial selection */ |
| 187 | gtk_combo_box_set_active_index(box, selection_model->currentIndex()); |
| 188 | |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 189 | /* connect signals to and from the selection model */ |
| 190 | connection = QObject::connect( |
| 191 | selection_model, |
| 192 | &QItemSelectionModel::currentChanged, |
Stepan Salenikovich | 9d29449 | 2015-05-14 16:34:24 -0400 | [diff] [blame] | 193 | [=](const QModelIndex current, G_GNUC_UNUSED const QModelIndex & previous) { |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 194 | gtk_combo_box_set_active_index(box, current); |
| 195 | } |
| 196 | ); |
| 197 | g_signal_connect(box, |
| 198 | "changed", |
| 199 | G_CALLBACK(update_selection), |
| 200 | selection_model); |
| 201 | |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 202 | return connection; |
Stepan Salenikovich | 9d29449 | 2015-05-14 16:34:24 -0400 | [diff] [blame] | 203 | } |