1 /***************************************************************************
2                           logmodel.cpp  -  description
3                              -------------------
4     begin                : june 2017
5     copyright            : (C) 2017 by Jaime Robles
6     email                : jaime@robles.es
7  ***************************************************************************/
8 
9 /*****************************************************************************
10  * This file is part of KLog.                                                *
11  *                                                                           *
12  *    KLog is free software: you can redistribute it and/or modify           *
13  *    it under the terms of the GNU General Public License as published by   *
14  *    the Free Software Foundation, either version 3 of the License, or      *
15  *    (at your option) any later version.                                    *
16  *                                                                           *
17  *    KLog is distributed in the hope that it will be useful,                *
18  *    but WITHOUT ANY WARRANTY; without even the implied warranty of         *
19  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
20  *    GNU General Public License for more details.                           *
21  *                                                                           *
22  *    You should have received a copy of the GNU General Public License      *
23  *    along with KLog.  If not, see <https://www.gnu.org/licenses/>.         *
24  *                                                                           *
25  *****************************************************************************/
26 
27 #include "logmodel.h"
28 
LogModel(DataProxy_SQLite * dp,QObject * parent)29 LogModel::LogModel(DataProxy_SQLite *dp, QObject *parent):QSqlRelationalTableModel(parent)
30 {
31     //qDebug() << Q_FUNC_INFO ;
32     //logModel = new QSqlRelationalTableModel(this);
33     dataProxy = dp;
34     util = new Utilities;
35     //qDebug() << Q_FUNC_INFO << "llamando a filterValidFields";
36     columns = dataProxy->filterValidFields(util->getDefaultLogFields());
37     setTable("log");
38     setEditStrategy(QSqlTableModel::OnFieldChange);
39     //qDebug() << Q_FUNC_INFO << " - END";
40 }
41 
42 
createlogModel(const int _i)43 void LogModel::createlogModel(const int _i)
44 {
45 /*
46     Log_Id = 0,
47     Log_Name = 1,
48     Log_BandId = 2,
49     Log_ModeId = 3,
50     Log_DateId = 4,
51     Log_TimeId = 5
52 
53 setRelation ( int column, const QSqlRelation & relation )
54 
55     model->setTable("employee");
56     model->setRelation(2, QSqlRelation("city", "id", "name"));
57 
58 The setRelation() call specifies that column 2 in table employee
59 is a foreign key that maps with field id of table city, and that
60 the view should present the city's name field to the user.
61 
62 */
63 
64 /*
65 This should be coherent with the logview
66 */
67 
68     //qDebug() << Q_FUNC_INFO ;
69 
70 
71     QString stringQuery = QString("lognumber='%1'").arg(_i);
72     //QSqlQuery query(stringQuery);
73     setFilter(stringQuery);
74     setColumns(columns);
75 
76     select();
77     //qDebug() << Q_FUNC_INFO << " - END";
78 }
79 
setColumns(const QStringList & _columns)80  void LogModel::setColumns(const QStringList &_columns)
81  {
82      //qDebug() << Q_FUNC_INFO ;
83      //QString auxt;
84      //foreach(auxt, _columns)
85      //{
86      //    //qDebug() << Q_FUNC_INFO << ": " << auxt;
87      //}
88     columns.clear();
89     //qDebug() << Q_FUNC_INFO << "llamando a filterValidFields";
90     columns << dataProxy->filterValidFields(_columns);
91 
92      QSqlQuery q;
93      QString stringQuery = QString("SELECT * from log LIMIT 1");
94      QSqlRecord rec; // = q.record();
95 
96      int nameCol;
97 
98      bool sqlOK = q.exec(stringQuery);
99      if (!sqlOK)
100      {
101          emit queryError(Q_FUNC_INFO, q.lastError().databaseText(), q.lastError().nativeErrorCode(), q.lastQuery());
102 
103      }
104      q.next();
105      rec = q.record(); // Number of columns
106 
107     //qDebug() << "LogModel::createlogModel - columns: " << QString::number(rec.count()) << QT_ENDL;
108 
109      if (_columns.contains("bandid"))
110      {
111          nameCol = rec.indexOf("bandid");
112          setRelation(nameCol, QSqlRelation("band", "id", "name"));
113      }
114 
115     if (_columns.contains("band_rx"))
116     {
117         nameCol = rec.indexOf("band_rx");
118         setRelation(nameCol, QSqlRelation("band", "id", "name"));
119     }
120 
121     if (_columns.contains("modeid"))
122     {
123         nameCol = rec.indexOf("modeid");
124         setRelation(nameCol, QSqlRelation("mode", "id", "submode"));
125     }
126 
127      if (_columns.contains("dxcc"))
128      {
129          nameCol = rec.indexOf("dxcc");
130          setRelation(nameCol, QSqlRelation("entity", "dxcc", "name"));
131      }
132 
133 
134      nameCol = rec.indexOf("id");
135      setSort(nameCol, Qt::AscendingOrder);
136      QString aux;
137 
138      foreach(aux, columns)
139      {
140          nameCol = rec.indexOf(aux);
141          setHeaderData(nameCol, Qt::Horizontal, util->getLogColumnName(aux));
142          //qDebug() << Q_FUNC_INFO << ": - " << aux;
143      }
144      //qDebug() << Q_FUNC_INFO << " - END";
145  }
146 /*
147  void LogModel::showColumn(const QString &_columnName)
148  {
149      QString stringQuery;
150      stringQuery = QString("SELECT * FROM log LIMIT 1");
151      QSqlQuery query;
152      bool sqlOK = query.exec(stringQuery);
153      if (!sqlOK)
154      {
155          emit queryError(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
156      }
157      QSqlRecord rec;
158      rec = query.record(); // Number of columns
159 
160      int nameCol = rec.indexOf(_columnName);
161      setHeaderData(nameCol, Qt::Horizontal, _columnName);
162 
163  }
164 */
165