Bayeux  3.4.1
Core Foundation library for SuperNEMO
variant_registry_tree_model.h
Go to the documentation of this file.
1 /* Author(s) : Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date : 2014-10-06
4  * Last modified : 2016-11-16
5  *
6  * Copyright (C) 2014-2016 Francois Mauger <mauger@lpccaen.in2p3.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or (at
11  * your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  * Description:
24  *
25  * Qt-based tree model for a variant registry.
26  */
27 
28 #ifndef DATATOOLS_CONFIGURATION_UI_VARIANT_REGISTRY_TREE_MODEL_H
29 #define DATATOOLS_CONFIGURATION_UI_VARIANT_REGISTRY_TREE_MODEL_H
30 
31 // Third party:
32 // - Qt:
33 #include <QObject>
34 #include <QString>
35 #include <QList>
36 #include <QAbstractItemModel>
37 #include <QModelIndex>
38 #include <QVariant>
39 // - Boost:
40 #include <boost/scoped_ptr.hpp>
41 
42 // This project
43 #include <datatools/logger.h>
44 #include <datatools/i_tree_dump.h>
45 
46 namespace datatools {
47 
48  namespace configuration {
49 
50  // Forward declarations:
51  class variant_record;
52  class variant_registry;
53 
54  namespace ui {
55 
57  class tree_item : public QObject,
59  {
60  Q_OBJECT
61 
62  public:
63 
65  enum column_index {
66  CI_KEY = 0,
67  CI_TYPE = 1,
68  CI_VALUE = 2,
70  };
71 
73  tree_item(variant_record & vr_, tree_item * parent_ = 0);
74 
76  virtual ~tree_item();
77 
79  void append_child(tree_item * child_);
80 
82  tree_item * grab_child(int row_);
83 
85  const tree_item * get_child(int row_) const;
86 
88  int get_child_count() const;
89 
91  int get_column_count() const;
92 
95 
97  const variant_record & get_record() const;
98 
100  int get_row() const;
101 
103  bool has_parent() const;
104 
107 
109  const tree_item * get_parent() const;
110 
112 
117  QVariant data(int column_) const;
118 
120  bool setData(int column_, const QVariant & value_);
121 
123  bool unsetData(int column_);
124 
126  void set_parent(tree_item * parent_);
127 
130 
133 
135  QList<tree_item *> & grab_children();
136 
138  const QList<tree_item *> & get_children() const;
139 
141  virtual void tree_dump(std::ostream & out = std::clog,
142  const std::string & title_ = "",
143  const std::string & indent_ = "",
144  bool inherit_ = false) const;
145 
146  private:
147 
148  datatools::logger::priority _logging_;
149  variant_record * _record_;
150  tree_item * _parent_;
151  QList<tree_item *> _children_;
152 
154 
155  };
156 
158  class variant_registry_tree_model : public QAbstractItemModel,
160  {
161  Q_OBJECT
162 
163  public:
164 
166  static const std::string & default_top_label();
167 
169  variant_registry_tree_model(QObject * parent_ = 0);
170 
173 
175  bool has_root() const;
176 
178  void set_root(tree_item * node_);
179 
181  tree_item * grab_root();
182 
184  const tree_item * get_root() const;
185 
187  QModelIndex rootIndex() const;
188 
190  QModelIndex index(int row_,
191  int column_,
192  const QModelIndex &parent_) const;
193 
195  QModelIndex parent(const QModelIndex &child_) const;
196 
198  Qt::ItemFlags flags(const QModelIndex &index_) const;
199 
201  int rowCount(const QModelIndex &parent_) const;
202 
204  int columnCount(const QModelIndex &parent_) const;
205 
207  QVariant data(const QModelIndex &index_, int role_) const;
208 
210  bool setData(const QModelIndex & index_, const QVariant & value_, int role_ = Qt::EditRole);
211 
213  bool unsetData(const QModelIndex & index_, int role_ = Qt::EditRole);
214 
215 
217  QVariant headerData(int section_,
218  Qt::Orientation orientation_,
219  int role_) const;
220 
222  void construct(variant_registry & registry_,
223  const std::string & registry_name_ = "");
224 
226  void set_registry_name(const std::string & registry_name_);
227 
228  // Return the name of the registry
229  const std::string & get_registry_name() const;
230 
233 
236 
238  bool is_read_only() const;
239 
241  void set_read_only(bool);
242 
244  bool has_registry() const;
245 
248 
250  const variant_registry & get_registry() const;
251 
253  bool has_restore_buffer() const;
254 
256  void reset_restore_buffer();
257 
259  virtual void tree_dump(std::ostream & out = std::clog,
260  const std::string & title_ = "",
261  const std::string & indent_ = "",
262  bool inherit_ = false) const;
263 
265  tree_item * node_from_index(const QModelIndex & index_) const;
266 
267  public slots:
268 
270  void slot_broadcast_data_change(const QModelIndex &, const QModelIndex &);
271 
273  void slot_store_to_restore_buffer(std::string * buffer_ = 0);
274 
276  void slot_load_from_restore_buffer(const std::string * buffer_ = 0);
277 
279  void slot_store_to_reg_file(const QString & out_reg_filename_) const;
280 
282  void slot_load_from_reg_file(const QString & in_reg_filename_);
283 
284  signals:
285 
286  void sig_read_only_changed(bool);
287 
289 
290  void sig_logging_changed();
291 
292  void sig_registry_changed(std::string);
293 
294  void sig_registry_data_changed(std::string, std::string);
295 
296  private:
297 
299  tree_item * _construct_node_(variant_registry & registry_, variant_record & record_);
300 
302  tree_item * _node_from_index_(const QModelIndex & index_) const;
303 
304  private:
305 
306  logger::priority _logging_;
307  bool _read_only_;
308  std::string _registry_name_;
309  variant_registry * _registry_;
310  tree_item * _root_;
311  boost::scoped_ptr<std::string> _restore_buffer_;
312 
313  };
314 
315  } // end of namespace ui
316 
317  } // end of namespace configuration
318 
319 } // end of namespace datatools
320 
321 #endif // DATATOOLS_CONFIGURATION_UI_VARIANT_REGISTRY_TREE_MODEL_H
322 
323 // Local Variables: --
324 // mode: c++ --
325 // c-file-style: "gnu" --
326 // tab-width: 2 --
327 // End: --
Registry of configuration variant parameters and associated variants.
Definition: variant_registry.h:53
bool unsetData(int column_)
Unset the data stored at a given column.
bool unsetData(const QModelIndex &index_, int role_=Qt::EditRole)
Unset the value of the data associated to a given model index and role.
void slot_load_from_reg_file(const QString &in_reg_filename_)
Slot: load the current status from a file.
Node/item of the tree model wrapping a variant registry record.
Definition: variant_registry_tree_model.h:57
priority
Priority levels for logging from most to least critical.
Definition: logger.h:82
variant_record & grab_record()
Return the mutable handled variant record.
Qt tree model for configuration variant registry.
Definition: variant_registry_tree_model.h:158
bool has_root() const
Check if the model has a root.
QVariant headerData(int section_, Qt::Orientation orientation_, int role_) const
Return the value of the header data associated to a given role.
int rowCount(const QModelIndex &parent_) const
Return the number of rows of a model index.
An interface with utilities for printable objects.
Definition: i_tree_dump.h:36
bool setData(const QModelIndex &index_, const QVariant &value_, int role_=Qt::EditRole)
Set the value of the data associated to a given model index and role.
const variant_registry & get_registry() const
Return a reference to the non mutable registry.
tree_item * grab_root()
Return the root node/item.
Qt::ItemFlags flags(const QModelIndex &index_) const
Return the flags associated to a model index.
bool setData(int column_, const QVariant &value_)
Set the data stored at a given column.
const tree_item * get_root() const
Return the root node/item.
QModelIndex parent(const QModelIndex &child_) const
Return the parent model index associated to a child model index.
const tree_item * get_parent() const
Return the non mutable parent item.
datatools::logger::priority get_logging() const
Return the logging priority.
variant_registry & grab_registry()
Return a reference to the mutable registry.
The index of the 'Key' column.
Definition: variant_registry_tree_model.h:66
void slot_load_from_restore_buffer(const std::string *buffer_=0)
Slot: load the current status from a buffer.
variant_registry_tree_model(QObject *parent_=0)
Constructor.
void set_logging(datatools::logger::priority p_)
Set the logging priority.
tree_item * grab_child(int row_)
Return a mutable child per row.
bool has_parent() const
Check if the node has a parent.
int get_row() const
Return the current row for this item.
void slot_store_to_restore_buffer(std::string *buffer_=0)
Slot: store the current status to a buffer.
void set_logging(datatools::logger::priority p_)
Set the logging priority.
virtual void tree_dump(std::ostream &out=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
void set_parent(tree_item *parent_)
Set the parent tree item.
The index of the 'Value' column.
Definition: variant_registry_tree_model.h:68
tree_item * node_from_index(const QModelIndex &index_) const
Return the node/item associated to a given model index.
int get_column_count() const
Return the number of column.
bool has_restore_buffer() const
Check if a restore buffer is available.
tree_item * grab_parent()
Return the mutable parent item.
QVariant data(const QModelIndex &index_, int role_) const
Return the value of the data associated to a given model index and role.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
column_index
Index of the column for record rendering.
Definition: variant_registry_tree_model.h:65
Utilities for logging information.
tree_item(variant_record &vr_, tree_item *parent_=0)
Default constructor.
void construct(variant_registry &registry_, const std::string &registry_name_="")
Construct the model from a registry and a registry name.
int get_child_count() const
Return the number of children.
static const std::string & default_top_label()
Return the default label for top variant.
QModelIndex index(int row_, int column_, const QModelIndex &parent_) const
Return the model index associated to a given row, column and parent.
The index of the 'Type' column.
Definition: variant_registry_tree_model.h:67
QModelIndex rootIndex() const
Return the model index associated to the root.
bool has_registry() const
Check if a registry is handled.
void set_registry_name(const std::string &registry_name_)
Set the name of the registry.
QVariant data(int column_) const
Return the data stored at a given column.
virtual void tree_dump(std::ostream &out=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
Variant registry record node.
Definition: variant_record.h:52
void reset_restore_buffer()
Reset the restore buffer is available.
datatools::logger::priority get_logging() const
Return the logging priority.
const tree_item * get_child(int row_) const
Return a non mutable child per row.
The index of the 'Description' column.
Definition: variant_registry_tree_model.h:69
QList< tree_item * > & grab_children()
Return the mutable list of children.
void set_root(tree_item *node_)
Set the root node.
int columnCount(const QModelIndex &parent_) const
Return the number of columns of a model index.
void append_child(tree_item *child_)
Append a child item.
const variant_record & get_record() const
Return the non mutable handled variant record.
void slot_store_to_reg_file(const QString &out_reg_filename_) const
Slot: store the current status to a file.
bool is_read_only() const
Check the read-only flag.
const QList< tree_item * > & get_children() const
Return the non mutable list of children.
void sig_registry_data_changed(std::string, std::string)
void slot_broadcast_data_change(const QModelIndex &, const QModelIndex &)
Slot: broadcast data change.