Stepan Salenikovich | 9816a94 | 2015-04-22 17:49:16 -0400 | [diff] [blame] | 1 | /* |
Guillaume Roguez | 2a6150d | 2017-07-19 18:24:47 -0400 | [diff] [blame] | 2 | * Copyright (C) 2015-2017 Savoir-faire Linux Inc. |
Stepan Salenikovich | 9816a94 | 2015-04-22 17:49:16 -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 | 9816a94 | 2015-04-22 17:49:16 -0400 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "models.h" |
| 21 | |
Stepan Salenikovich | 9816a94 | 2015-04-22 17:49:16 -0400 | [diff] [blame] | 22 | #include "../models/gtkqtreemodel.h" |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 23 | #include <QtCore/QItemSelectionModel> |
Stepan Salenikovich | 9816a94 | 2015-04-22 17:49:16 -0400 | [diff] [blame] | 24 | |
| 25 | QModelIndex |
| 26 | get_index_from_selection(GtkTreeSelection *selection) |
| 27 | { |
| 28 | GtkTreeIter iter; |
| 29 | GtkTreeModel *model = NULL; |
| 30 | |
| 31 | if (gtk_tree_selection_get_selected(selection, &model, &iter)) { |
| 32 | if (GTK_IS_Q_TREE_MODEL(model)) |
| 33 | return gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter); |
Stepan Salenikovich | 9816a94 | 2015-04-22 17:49:16 -0400 | [diff] [blame] | 34 | } |
| 35 | return QModelIndex(); |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | QModelIndex |
| 39 | gtk_combo_box_get_index(GtkComboBox *box) |
| 40 | { |
Stepan Salenikovich | 4c31b21 | 2017-04-26 13:45:12 -0400 | [diff] [blame] | 41 | GtkTreeIter iter; |
| 42 | GtkTreeModel *model = gtk_combo_box_get_model(box); |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 43 | |
Stepan Salenikovich | 4c31b21 | 2017-04-26 13:45:12 -0400 | [diff] [blame] | 44 | if (gtk_combo_box_get_active_iter(box, &iter)) { |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 45 | if (GTK_IS_Q_TREE_MODEL(model)) |
Stepan Salenikovich | 4c31b21 | 2017-04-26 13:45:12 -0400 | [diff] [blame] | 46 | return gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter); |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 47 | } |
| 48 | return QModelIndex(); |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | update_selection(GtkComboBox *box, QItemSelectionModel *selection_model) |
| 53 | { |
| 54 | QModelIndex idx = gtk_combo_box_get_index(box); |
| 55 | if (idx.isValid()) { |
| 56 | selection_model->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect); |
| 57 | } |
Stepan Salenikovich | 4c31b21 | 2017-04-26 13:45:12 -0400 | [diff] [blame] | 58 | selection_model->clearSelection(); |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
| 62 | gtk_combo_box_set_active_index(GtkComboBox *box, const QModelIndex& idx) |
| 63 | { |
| 64 | if (idx.isValid()) { |
| 65 | GtkTreeIter new_iter; |
Stepan Salenikovich | 4c31b21 | 2017-04-26 13:45:12 -0400 | [diff] [blame] | 66 | GtkTreeModel *model = gtk_combo_box_get_model(box); |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 67 | |
Stepan Salenikovich | 4c31b21 | 2017-04-26 13:45:12 -0400 | [diff] [blame] | 68 | g_return_if_fail(GTK_IS_Q_TREE_MODEL(model)); |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 69 | |
Stepan Salenikovich | 4c31b21 | 2017-04-26 13:45:12 -0400 | [diff] [blame] | 70 | if (gtk_q_tree_model_source_index_to_iter(GTK_Q_TREE_MODEL(model), idx, &new_iter)) { |
| 71 | gtk_combo_box_set_active_iter(box, &new_iter); |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 72 | } else { |
| 73 | g_warning("Given QModelIndex doesn't exist in GtkTreeModel"); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | QMetaObject::Connection |
| 79 | gtk_combo_box_set_qmodel(GtkComboBox *box, QAbstractItemModel *qmodel, QItemSelectionModel *selection_model) |
| 80 | { |
| 81 | QMetaObject::Connection connection; |
| 82 | GtkTreeModel *model; |
| 83 | |
Stepan Salenikovich | f607822 | 2016-10-03 17:31:16 -0400 | [diff] [blame] | 84 | model = (GtkTreeModel *)gtk_q_tree_model_new( |
| 85 | qmodel, |
| 86 | 1, |
| 87 | 0, Qt::DisplayRole, G_TYPE_STRING); |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 88 | |
Stepan Salenikovich | 4c31b21 | 2017-04-26 13:45:12 -0400 | [diff] [blame] | 89 | gtk_combo_box_set_model(box, GTK_TREE_MODEL(model)); |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 90 | |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 91 | if (!selection_model) return connection; |
| 92 | |
Stepan Salenikovich | 9d29449 | 2015-05-14 16:34:24 -0400 | [diff] [blame] | 93 | /* sync the initial selection */ |
| 94 | gtk_combo_box_set_active_index(box, selection_model->currentIndex()); |
| 95 | |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 96 | /* connect signals to and from the selection model */ |
| 97 | connection = QObject::connect( |
| 98 | selection_model, |
| 99 | &QItemSelectionModel::currentChanged, |
Stepan Salenikovich | 9d29449 | 2015-05-14 16:34:24 -0400 | [diff] [blame] | 100 | [=](const QModelIndex current, G_GNUC_UNUSED const QModelIndex & previous) { |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 101 | gtk_combo_box_set_active_index(box, current); |
| 102 | } |
| 103 | ); |
| 104 | g_signal_connect(box, |
| 105 | "changed", |
| 106 | G_CALLBACK(update_selection), |
| 107 | selection_model); |
| 108 | |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 109 | return connection; |
Stepan Salenikovich | 9d29449 | 2015-05-14 16:34:24 -0400 | [diff] [blame] | 110 | } |
Stepan Salenikovich | 4c31b21 | 2017-04-26 13:45:12 -0400 | [diff] [blame] | 111 | |
| 112 | QMetaObject::Connection |
| 113 | gtk_combo_box_set_qmodel_text(GtkComboBox *box, QAbstractItemModel *qmodel, QItemSelectionModel *selection_model) |
| 114 | { |
| 115 | auto connection = gtk_combo_box_set_qmodel(box, qmodel, selection_model); |
| 116 | |
| 117 | GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); |
| 118 | gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(box), renderer, FALSE); |
| 119 | gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(box), renderer, |
| 120 | "text", 0, NULL); |
| 121 | |
| 122 | return connection; |
| 123 | } |