3
* Copyright (C) 2001-2004 Murray Cumming
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License as
7
* published by the Free Software Foundation; either version 2 of the
8
* License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* General Public License for more details.
15
* You should have received a copy of the GNU General Public
16
* License along with this program; if not, write to the
17
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
* Boston, MA 02111-1307, USA.
21
#include "dialog_groupby_sortfields.h"
22
#include "dialog_field_layout.h"
24
//#include <libgnome/gnome-i18n.h>
25
#include <glibmm/i18n.h>
30
Dialog_GroupBy_SortFields::Dialog_GroupBy_SortFields(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder)
31
: Dialog_Layout(cobject, builder, false /* means no table title */),
34
m_button_field_down(0),
35
m_button_field_add(0),
36
m_button_field_delete(0),
37
m_button_field_edit(0),
40
builder->get_widget("label_table_name", m_label_table_name);
42
builder->get_widget("treeview_fields", m_treeview_fields);
45
m_model_fields = Gtk::ListStore::create(m_ColumnsFields);
46
m_treeview_fields->set_model(m_model_fields);
48
// Append the View columns:
49
Gtk::TreeView::Column* column_name = Gtk::manage( new Gtk::TreeView::Column(_("Name")) );
50
m_treeview_fields->append_column(*column_name);
52
Gtk::CellRendererText* renderer_name = Gtk::manage(new Gtk::CellRendererText);
53
column_name->pack_start(*renderer_name);
54
column_name->set_cell_data_func(*renderer_name, sigc::mem_fun(*this, &Dialog_GroupBy_SortFields::on_cell_data_name));
56
m_treeview_fields->append_column_editable(_("Ascending"), m_ColumnsFields.m_col_ascending);
58
//Sort by sequence, so we can change the order by changing the values in the hidden sequence column.
59
m_model_fields->set_sort_column(m_ColumnsFields.m_col_sequence, Gtk::SORT_ASCENDING);
62
//Respond to changes of selection:
63
Glib::RefPtr<Gtk::TreeView::Selection> refSelection = m_treeview_fields->get_selection();
66
refSelection->signal_changed().connect( sigc::mem_fun(*this, &Dialog_GroupBy_SortFields::on_treeview_fields_selection_changed) );
69
m_model_fields->signal_row_changed().connect( sigc::mem_fun(*this, &Dialog_GroupBy_SortFields::on_treemodel_row_changed) );
73
builder->get_widget("button_field_up", m_button_field_up);
74
m_button_field_up->signal_clicked().connect( sigc::mem_fun(*this, &Dialog_GroupBy_SortFields::on_button_field_up) );
76
builder->get_widget("button_field_down", m_button_field_down);
77
m_button_field_down->signal_clicked().connect( sigc::mem_fun(*this, &Dialog_GroupBy_SortFields::on_button_field_down) );
79
builder->get_widget("button_field_delete", m_button_field_delete);
80
m_button_field_delete->signal_clicked().connect( sigc::mem_fun(*this, &Dialog_GroupBy_SortFields::on_button_delete) );
82
builder->get_widget("button_field_add", m_button_field_add);
83
m_button_field_add->signal_clicked().connect( sigc::mem_fun(*this, &Dialog_GroupBy_SortFields::on_button_add_field) );
85
builder->get_widget("button_field_edit", m_button_field_edit);
86
m_button_field_edit->signal_clicked().connect( sigc::mem_fun(*this, &Dialog_GroupBy_SortFields::on_button_edit_field) );
91
Dialog_GroupBy_SortFields::~Dialog_GroupBy_SortFields()
95
void Dialog_GroupBy_SortFields::set_fields(const Glib::ustring& table_name, const LayoutItem_GroupBy::type_list_sort_fields& fields)
98
m_table_name = table_name;
100
Document* document = get_document();
102
//Update the tree models from the document
105
//Set the table name and title:
106
m_label_table_name->set_text(table_name);
109
//Show the field layout
110
typedef std::list< Glib::ustring > type_listStrings;
112
m_model_fields->clear();
113
guint field_sequence = 0;
114
for(LayoutItem_GroupBy::type_list_sort_fields::const_iterator iter = fields.begin(); iter != fields.end(); ++iter)
116
sharedptr<const LayoutItem_Field> item = sharedptr<const LayoutItem_Field>::cast_dynamic(iter->first);
118
Gtk::TreeModel::iterator iterTree = m_model_fields->append();
119
Gtk::TreeModel::Row row = *iterTree;
121
row[m_ColumnsFields.m_col_layout_item] = item;
122
row[m_ColumnsFields.m_col_ascending] = iter->second;
123
row[m_ColumnsFields.m_col_sequence] = field_sequence;
127
//treeview_fill_sequences(m_model_fields, m_ColumnsFields.m_col_sequence); //The document should have checked this already, but it does not hurt to check again.
133
void Dialog_GroupBy_SortFields::enable_buttons()
136
Glib::RefPtr<Gtk::TreeView::Selection> refSelection = m_treeview_fields->get_selection();
139
Gtk::TreeModel::iterator iter = refSelection->get_selected();
142
//Disable Up if It can't go any higher.
143
bool enable_up = true;
144
if(iter == m_model_fields->children().begin())
145
enable_up = false; //It can't go any higher.
147
m_button_field_up->set_sensitive(enable_up);
150
//Disable Down if It can't go any lower.
151
Gtk::TreeModel::iterator iterNext = iter;
154
bool enable_down = true;
155
if(iterNext == m_model_fields->children().end())
158
m_button_field_down->set_sensitive(enable_down);
160
m_button_field_delete->set_sensitive(true);
164
//Disable all buttons that act on a selection:
165
m_button_field_down->set_sensitive(false);
166
m_button_field_up->set_sensitive(false);
167
m_button_field_delete->set_sensitive(false);
175
void Dialog_GroupBy_SortFields::on_button_field_up()
177
move_treeview_selection_up(m_treeview_fields, m_ColumnsFields.m_col_sequence);
180
void Dialog_GroupBy_SortFields::on_button_field_down()
182
move_treeview_selection_down(m_treeview_fields, m_ColumnsFields.m_col_sequence);
185
LayoutItem_GroupBy::type_list_sort_fields Dialog_GroupBy_SortFields::get_fields() const
187
LayoutItem_GroupBy::type_list_sort_fields result;
189
guint field_sequence = 1; //0 means no sequence
190
for(Gtk::TreeModel::iterator iterFields = m_model_fields->children().begin(); iterFields != m_model_fields->children().end(); ++iterFields)
192
Gtk::TreeModel::Row row = *iterFields;
194
sharedptr<const LayoutItem_Field> item = row[m_ColumnsFields.m_col_layout_item];
195
const Glib::ustring field_name = item->get_name();
196
if(!field_name.empty())
198
sharedptr<LayoutItem_Field> field_copy = glom_sharedptr_clone(item);
200
const bool ascending = row[m_ColumnsFields.m_col_ascending];
201
result.push_back( LayoutItem_GroupBy::type_pair_sort_field(field_copy, ascending) );
210
void Dialog_GroupBy_SortFields::on_treeview_fields_selection_changed()
215
void Dialog_GroupBy_SortFields::on_button_add_field()
217
//Get the chosen fields:
218
type_list_field_items fields_list = offer_field_list(m_table_name, this);
219
for(type_list_field_items::iterator iter_chosen = fields_list.begin(); iter_chosen != fields_list.end(); iter_chosen++)
222
sharedptr<LayoutItem_Field> field = *iter_chosen;
226
//Add the field details to the layout treeview:
227
Gtk::TreeModel::iterator iter = m_model_fields->append();
231
Gtk::TreeModel::Row row = *iter;
232
row[m_ColumnsFields.m_col_layout_item] = field;
233
row[m_ColumnsFields.m_col_ascending] = true; //Default to this so that alphabetical searches go from A to Z by default.
235
//Scroll to, and select, the new row:
236
Glib::RefPtr<Gtk::TreeView::Selection> refTreeSelection = m_treeview_fields->get_selection();
238
refTreeSelection->select(iter);
240
m_treeview_fields->scroll_to_row( Gtk::TreeModel::Path(iter) );
242
treeview_fill_sequences(m_model_fields, m_ColumnsFields.m_col_sequence); //The document should have checked this already, but it does not hurt to check again.
247
void Dialog_GroupBy_SortFields::on_button_delete()
249
Glib::RefPtr<Gtk::TreeView::Selection> refTreeSelection = m_treeview_fields->get_selection();
252
//TODO: Handle multiple-selection:
253
Gtk::TreeModel::iterator iter = refTreeSelection->get_selected();
256
m_model_fields->erase(iter);
264
void Dialog_GroupBy_SortFields::on_cell_data_name(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& iter)
266
//Set the view's cell properties depending on the model's data:
267
Gtk::CellRendererText* renderer_text = dynamic_cast<Gtk::CellRendererText*>(renderer);
272
Gtk::TreeModel::Row row = *iter;
274
sharedptr<const LayoutItem_Field> item = row[m_ColumnsFields.m_col_layout_item]; //TODO_performance: Reduce copying.
275
renderer_text->property_markup() = item->get_layout_display_name();
276
renderer_text->property_editable() = false; //Names can never be edited.
282
void Dialog_GroupBy_SortFields::on_button_edit_field()
284
Glib::RefPtr<Gtk::TreeView::Selection> refTreeSelection = m_treeview_fields->get_selection();
287
//TODO: Handle multiple-selection:
288
Gtk::TreeModel::iterator iter = refTreeSelection->get_selected();
291
Gtk::TreeModel::Row row = *iter;
292
sharedptr<const LayoutItem_Field> field = row[m_ColumnsFields.m_col_layout_item];
294
//Get the chosen field:
295
sharedptr<LayoutItem_Field> field_chosen =
296
offer_field_list_select_one_field(field, m_table_name, this);
299
//Set the field details in the layout treeview:
301
row[m_ColumnsFields.m_col_layout_item] = field_chosen;
303
//Scroll to, and select, the new row:
305
Glib::RefPtr<Gtk::TreeView::Selection> refTreeSelection = m_treeview_fields->get_selection();
307
refTreeSelection->select(iter);
309
m_treeview_fields->scroll_to_row( Gtk::TreeModel::Path(iter) );
311
treeview_fill_sequences(m_model_fields, m_ColumnsFields.m_col_sequence); //The document should have checked this already, but it does not hurt to check again.