blob: 18c81dd099ff73d5b87b731af4dc15c66a30494b [file] [log] [blame]
Stepan Salenikovich9816a942015-04-22 17:49:16 -04001/*
Guillaume Roguez77c579d2018-01-30 15:54:02 -05002 * Copyright (C) 2015-2018 Savoir-faire Linux Inc.
Stepan Salenikovich9816a942015-04-22 17:49:16 -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 Salenikovich9816a942015-04-22 17:49:16 -040018 */
19
20#include "models.h"
21
Stepan Salenikovich9816a942015-04-22 17:49:16 -040022#include "../models/gtkqtreemodel.h"
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040023#include <QtCore/QItemSelectionModel>
Stepan Salenikovich9816a942015-04-22 17:49:16 -040024
25QModelIndex
26get_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 Salenikovich9816a942015-04-22 17:49:16 -040034 }
35 return QModelIndex();
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040036}
37
38QModelIndex
39gtk_combo_box_get_index(GtkComboBox *box)
40{
Stepan Salenikovich4c31b212017-04-26 13:45:12 -040041 GtkTreeIter iter;
42 GtkTreeModel *model = gtk_combo_box_get_model(box);
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040043
Stepan Salenikovich4c31b212017-04-26 13:45:12 -040044 if (gtk_combo_box_get_active_iter(box, &iter)) {
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040045 if (GTK_IS_Q_TREE_MODEL(model))
Stepan Salenikovich4c31b212017-04-26 13:45:12 -040046 return gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040047 }
48 return QModelIndex();
49}
50
51static void
52update_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 Salenikovich4c31b212017-04-26 13:45:12 -040058 selection_model->clearSelection();
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040059}
60
61void
62gtk_combo_box_set_active_index(GtkComboBox *box, const QModelIndex& idx)
63{
64 if (idx.isValid()) {
65 GtkTreeIter new_iter;
Stepan Salenikovich4c31b212017-04-26 13:45:12 -040066 GtkTreeModel *model = gtk_combo_box_get_model(box);
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040067
Stepan Salenikovich4c31b212017-04-26 13:45:12 -040068 g_return_if_fail(GTK_IS_Q_TREE_MODEL(model));
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040069
Stepan Salenikovich4c31b212017-04-26 13:45:12 -040070 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 Salenikovich7a1e71c2015-05-07 11:14:48 -040072 } else {
73 g_warning("Given QModelIndex doesn't exist in GtkTreeModel");
74 }
75 }
76}
77
78QMetaObject::Connection
79gtk_combo_box_set_qmodel(GtkComboBox *box, QAbstractItemModel *qmodel, QItemSelectionModel *selection_model)
80{
81 QMetaObject::Connection connection;
82 GtkTreeModel *model;
83
Stepan Salenikovichf6078222016-10-03 17:31:16 -040084 model = (GtkTreeModel *)gtk_q_tree_model_new(
85 qmodel,
86 1,
87 0, Qt::DisplayRole, G_TYPE_STRING);
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040088
Stepan Salenikovich4c31b212017-04-26 13:45:12 -040089 gtk_combo_box_set_model(box, GTK_TREE_MODEL(model));
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040090
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040091 if (!selection_model) return connection;
92
Stepan Salenikovich9d294492015-05-14 16:34:24 -040093 /* sync the initial selection */
94 gtk_combo_box_set_active_index(box, selection_model->currentIndex());
95
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040096 /* connect signals to and from the selection model */
97 connection = QObject::connect(
98 selection_model,
99 &QItemSelectionModel::currentChanged,
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400100 [=](const QModelIndex current, G_GNUC_UNUSED const QModelIndex & previous) {
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -0400101 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 Salenikovich7a1e71c2015-05-07 11:14:48 -0400109 return connection;
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400110}
Stepan Salenikovich4c31b212017-04-26 13:45:12 -0400111
112QMetaObject::Connection
113gtk_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}