DbModel Pile   version 0.0.1
DbModel pile.
dbmodelprivate.h
1 /* ========================================================================= */
2 /* ------------------------------------------------------------------------- *//*
11 
12  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13  Please read COPYING and README files in root folder
14  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 */
16 /* ------------------------------------------------------------------------- */
17 /* ========================================================================= */
18 #ifndef DBMODELPRIVATE_H
19 #define DBMODELPRIVATE_H
20 //
21 //
22 //
23 //
24 /* INCLUDES ------------------------------------------------------------ */
25 
26 #include <QAbstractTableModel>
27 #include <QList>
28 
29 #include <dbmodel/dbmodel-config.h>
30 #include <dbmodel/dbmodelcol.h>
31 #include <dbmodel/dbmodeltbl.h>
32 #include <dbstruct/dbstruct.h>
33 #include <dbstruct/dbtaew.h>
34 
35 /* INCLUDES ============================================================ */
36 //
37 //
38 //
39 //
40 /* DEFINITIONS --------------------------------------------------------- */
41 
42 #if 1
43 # define DBMODEL_DEBUGM printf
44 #else
45 # define DBMODEL_DEBUGM black_hole
46 #endif
47 
48 #if 0
49 # define DBMODEL_TRACE_ENTRY printf("DBMODEL ENTRY %s in %s[%d]\n", __func__, __FILE__, __LINE__)
50 #else
51 # define DBMODEL_TRACE_ENTRY
52 #endif
53 
54 #if 0
55 # define DBMODEL_TRACE_EXIT printf("DBMODEL EXIT %s in %s[%d]\n", __func__, __FILE__, __LINE__)
56 #else
57 # define DBMODEL_TRACE_EXIT
58 #endif
59 
60 #ifndef TMP_A
61 # define TMP_A(__s__) __s__.toLatin1 ().constData ()
62 #endif
63 
64 static inline void black_hole (...)
65 {}
66 
67 /* DEFINITIONS ========================================================= */
68 //
69 //
70 //
71 //
72 /* CLASS --------------------------------------------------------------- */
73 
75 class DbModelPrivate : public QAbstractTableModel {
76  Q_OBJECT
77 
85  //
86  //
87  //
88  //
89  /* DEFINITIONS ----------------------------------------------------- */
90 
91  /* DEFINITIONS ===================================================== */
92  //
93  //
94  //
95  //
96  /* DATA ------------------------------------------------------------ */
97 
98 private:
99 
100  DbStruct * db_;
101  QList<DbModelCol> mapping_;
103  QList<DbModelTbl> tables_;
104  int row_highlite_;
105  int col_highlite_;
106  void * user_data_;
108  /* DATA ============================================================ */
109  //
110  //
111  //
112  //
113  /* FUNCTIONS ------------------------------------------------------- */
114 
115 public:
116 
119  DbStruct * db,
120  DbTaew * meta,
121  DbModel * parent);
122 
125  DbStruct * db,
126  int component,
127  DbModel * parent);
128 
129  Q_DISABLE_COPY(DbModelPrivate)
130 
131 
132  virtual ~DbModelPrivate();
133 
135  DbModel *
136  parentDbModel () const;
137 
139  bool
140  isValid() const {
141  return (db_ != NULL) &&
142  (tables_.count() > 0) &&
143  tables_.at (0).isValid();
144  }
145 
148  void
149  setMeta (
150  DbTaew * meta);
151 
154  void
155  setMeta (
156  DbStruct * database,
157  DbTaew * meta);
158 
159 
161  inline DbTaew *
162  takeMeta () {
163  if (tables_.count() == 0)
164  return NULL;
165  DbTaew * result = tables_.first().metadata();
166  tables_.first().setMetadata (NULL);
167  terminateMeta ();
168  col_highlite_ = -1;
169  row_highlite_ = -1;
170  return result;
171  }
172 
174  inline DbTaew *
175  metaTaew () const {
176  if (tables_.count() == 0)
177  return NULL;
178  return tables_.at (0).metadata();
179  }
180 
182  inline QSqlTableModel *
183  mainModel () const {
184  if (tables_.count() == 0)
185  return NULL;
186  return tables_.at (0).sqlModel ();
187  }
188 
190  bool
191  selectMe ();
192 
194  int
195  rowCount () const;
196 
198  int
199  columnCount () const;
200 
203  inline DbStruct *
204  database () const {
205 
206  return db_;
207  }
208 
210  void
211  setDatabase (
212  DbStruct * value);
213 
215  inline DbStruct *
217  DbStruct * result = db_;
218  db_ = NULL;
219  setMeta (NULL);
220  col_highlite_ = -1;
221  row_highlite_ = -1;
222  return result;
223  }
224 
226  const DbModelCol &
228  int index) const {
229 
230  return mapping_.at (index);
231  }
232 
234  const DbModelTbl &
236  int table_index) const {
237 
238  return tables_.at (table_index);
239  }
240 
242  bool
243  setFilter (
244  const QString & filter,
245  int table_index = 0);
246 
248  bool
249  setFilter (
250  const QString & filter,
251  const QString & table);
252 
254  bool
255  setOrder (
256  int column,
257  Qt::SortOrder order,
258  int table_index = 0);
259 
261  bool
262  setOrder (
263  int column,
264  Qt::SortOrder order,
265  const QString & table);
266 
268  int
269  findTable (
270  const QString & table) const;
271 
273  QSqlRecord
274  record (
275  int row) const;
276 
278  void
279  reloadHeaders ();
280 
281 
282  /* - - - - - - - - - - - - - - - - */
286 
288 public:
289 
290  Qt::ItemFlags
291  flags (
292  const QModelIndex &index) const;
293 
294  QVariant
295  data (
296  const QModelIndex &idx,
297  int role = Qt::DisplayRole) const;
298 
299  QVariant
300  headerData (
301  int section,
302  Qt::Orientation orientation,
303  int role = Qt::DisplayRole) const;
304 
305  bool
306  setHeaderData (
307  int section,
308  Qt::Orientation orientation,
309  const QVariant & value,
310  int role = Qt::EditRole);
311 
312  bool
313  setData (
314  const QModelIndex &index,
315  const QVariant &value,
316  int role = Qt::EditRole);
317 
319  bool
320  validateIndex (
321  const QModelIndex &idx) const;
322 
324  void
326  int column,
327  Qt::SortOrder order = Qt::AscendingOrder) {
328 
329  setOrder (column, order);
330  }
331 
333  static QVariant
334  formattedData (
335  const DbColumn &colorig,
336  const QVariant &original_value);
337 
339  bool
340  removeRows (
341  int row,
342  int count,
343  const QModelIndex &parent);
344 
345 
346 protected:
347 
348  int
349  rowCount (
350  const QModelIndex &parent) const;
351 
352  int
353  columnCount (
354  const QModelIndex &parent) const;
355 
356 
357  /*/! Prevent the user from using this method - use `setMeta()` instead.
358  virtual void
359  setTable (
360  const QString & value) {
361  QSqlRelationalTableModel::setTable (value);
362  } */
363 
365  /* - - - - - - - - - - - - - - - - */
366 
367 
368  /* - - - - - - - - - - - - - - - - */
374 
376 public:
377 
379  bool
381  int row,
382  int column);
383 
385  inline int
386  getMarkerRow () const {
387  return row_highlite_;
388  }
389 
391  inline int
392  getMarkerCol () const {
393  return col_highlite_;
394  }
395 
397  inline bool
398  hasMarkerCell () const {
399  return (col_highlite_ > -1) && (row_highlite_ > -1);
400  }
401 
403  /* - - - - - - - - - - - - - - - - */
404 
405 
406  bool
408  int table_index,
409  int column_index,
410  DbColumn::Callback value,
411  void *user_data);
412 
413  DbColumn::Callback
414  columnCallback (
415  int table_index,
416  int column_index);
417 
418  void *
419  columnCallbackData() {
420  return user_data_;
421  }
422 
423 private:
424 
426  void
427  terminateMeta ();
428 
430  bool
431  loadMeta (
432  DbTaew * meta);
433 
435  const DbModelTbl &
436  table (
437  const QString &name);
438 
440  void
441  clearTables ();
442 
444  void
445  addForeignKeyColumn (
446  const DbColumn & col,
447  int & col_idx);
448 
449 
450 
451  /* FUNCTIONS ======================================================= */
452  //
453  //
454  //
455  //
456 }; /* class DbModelPrivate */
457 
458 /* CLASS =============================================================== */
459 //
460 //
461 //
462 //
463 
464 
465 #endif // DBMODELPRIVATE_H
466 /* ------------------------------------------------------------------------- */
467 /* ========================================================================= */
468 
469 
bool selectMe()
Select the model (retreive information using options).
Definition: dbmodelprivate.cc:191
int columnCount() const
Number of columns.
Definition: dbmodelprivate.cc:252
const DbModelTbl & tableData(int table_index) const
Get the model data regarding a table.
Definition: dbmodelprivate.h:235
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
Set sorting order.
Definition: dbmodelprivate.h:325
Internal model.
Definition: dbmodelprivate.h:75
A Qt model capable of representing sql tables.
Definition: dbmodel.h:26
DbModelPrivate(DbStruct *db, DbTaew *meta, DbModel *parent)
Create a new model from a table or view definition and a database.
Definition: dbmodelprivate.cc:73
bool isValid() const
Is this a valid model (with a table set) or not?
Definition: dbmodelprivate.h:140
int rowCount() const
Number of rows.
Definition: dbmodelprivate.cc:237
QVariant data(const QModelIndex &idx, int role=Qt::DisplayRole) const
Definition: dbmodelprivate.cc:717
static QVariant formattedData(const DbColumn &colorig, const QVariant &original_value)
Convert the data for the user.
Definition: dbmodelprivate.cc:499
bool removeRows(int row, int count, const QModelIndex &parent)
Remove a number of rows.
Definition: dbmodelprivate.cc:701
bool validateIndex(const QModelIndex &idx) const
Tell if an index is valid for this model.
Definition: dbmodelprivate.cc:973
DbTaew * metaTaew() const
Get a pointer to meta object; ownership stays with this instance.
Definition: dbmodelprivate.h:175
Model representation of a column.
Definition: dbmodelcol.h:56
int findTable(const QString &table) const
Find the index of a model identified by its name.
Definition: dbmodelprivate.cc:430
bool setFilter(const QString &filter, int table_index=0)
Set a filter on one of the internal models identified by its index.
Definition: dbmodelprivate.cc:284
void reloadHeaders()
Read the labels again (possibly in a different language).
Definition: dbmodelprivate.cc:1196
bool hasMarkerCell() const
Tell if we have a highlite cell set.
Definition: dbmodelprivate.h:398
void setMeta(DbTaew *meta)
Definition: dbmodelprivate.cc:148
DbTaew * takeMeta()
Give away the pointer and remove it from internal storage.
Definition: dbmodelprivate.h:162
file dbmodeltbl.h date November 2015 author Nicu Tofan
Definition: dbmodeltbl.h:46
bool setCurrentMarker(int row, int column)
Set the indicated cell.
Definition: dbmodelprivate.cc:1220
QSqlRecord record(int row) const
Get a record for a row.
Definition: dbmodelprivate.cc:1186
bool setColumnCallback(int table_index, int column_index, DbColumn::Callback value, void *user_data)
Definition: dbmodelprivate.cc:1254
const DbModelCol & columnData(int index) const
Get the model data regarding a column; index is a real index.
Definition: dbmodelprivate.h:227
virtual ~DbModelPrivate()
destructor
Definition: dbmodelprivate.cc:118
DbStruct * takeDatabase()
Give away the pointer and remove it from internal storage.
Definition: dbmodelprivate.h:216
bool setOrder(int column, Qt::SortOrder order, int table_index=0)
Set a filter on one of the internal models identified by its index.
Definition: dbmodelprivate.cc:350
void setDatabase(DbStruct *value)
Set the database; also invalidates the table.
Definition: dbmodelprivate.cc:265
DbStruct * database() const
Definition: dbmodelprivate.h:204
int getMarkerCol() const
Retreive highlite column.
Definition: dbmodelprivate.h:392
DbModel * parentDbModel() const
Get the front-end model.
Definition: dbmodelprivate.cc:128
QSqlTableModel * mainModel() const
Get a pointer to internal main model object.
Definition: dbmodelprivate.h:183
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole)
Definition: dbmodelprivate.cc:955
int getMarkerRow() const
Retreive highlite row.
Definition: dbmodelprivate.h:386