1 /***************************************************************************
2                           searchmodel.cpp  -  description
3                              -------------------
4     begin                : sep 2020
5     copyright            : (C) 2020 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 "searchmodel.h"
28 
SearchModel(DataProxy_SQLite * dp,QObject * parent)29 SearchModel::SearchModel(DataProxy_SQLite *dp, QObject *parent):QSqlRelationalTableModel(parent)
30 {
31     //qDebug() << "SearchModel::SearchModel "  << QT_ENDL;
32     dataProxy = dp;
33     stationCallsignInHeader = true;
34     setTable("log");
35     setEditStrategy(QSqlTableModel::OnFieldChange);
36     dxcc = -1;
37     bandid = -1;
38     modeid = -1;
39     logn = -1;
40 
41     award = new Awards(dataProxy, Q_FUNC_INFO);
42 
43     //qDebug() << "SearchModel::SearchModel: Rows obtained: " << QString::number(rowCount())  << QT_ENDL;
44     //qDebug() << "SearchModel::SearchModel - END"  << QT_ENDL;
45 }
46 
setDXCCColumn(const int _i)47 void SearchModel::setDXCCColumn(const int _i)
48 {
49    dxcc = _i;
50 }
51 
setBandIdColumn(const int _i)52 void SearchModel::setBandIdColumn(const int _i)
53 {
54    bandid = _i;
55 }
56 
setModeIdColumn(const int _i)57 void SearchModel::setModeIdColumn(const int _i)
58 {
59     modeid = _i;
60 }
61 
setLogNColumn(const int _i)62 void SearchModel::setLogNColumn(const int _i)
63 {
64     logn = _i;
65 }
66 
createSearchModel(const int _i)67 void SearchModel::createSearchModel(const int _i)
68 {
69 /*
70     Log_Id = 0,
71     Log_Name = 1,
72     Log_BandId = 2,
73     Log_ModeId = 3,
74     Log_DateId = 4,
75     Log_TimeId = 5
76 
77 setRelation ( int column, const QSqlRelation & relation )
78 
79     model->setTable("employee");
80     model->setRelation(2, QSqlRelation("city", "id", "name"));
81 
82 The setRelation() call specifies that column 2 in table employee
83 is a foreign key that maps with field id of table city, and that
84 the view should present the city's name field to the user.
85 
86 */
87 
88 /*
89 This should be coherent with the treeview
90 */
91 
92       //qDebug() << "SearchModel::createSearchModel: log: " << QString::number(_i) << QT_ENDL;
93 
94    //QString contestMode = dataProxy->getLogTypeOfUserLog(_i);
95 
96 
97     QString stringQuery = QString("lognumber='%1'").arg(_i);
98     QSqlQuery query(stringQuery);
99     setFilter(stringQuery);
100     //setColumnsToDX();
101     select();
102 
103 
104 }
setStationCallsignInHeader(const bool _s)105 void SearchModel::setStationCallsignInHeader(const bool _s)
106 {
107     stationCallsignInHeader = _s;
108 }
109 
110 /*
111  void SearchModel::setColumnsToDX()
112  {
113         //qDebug() << "SearchModel::setColumnsToDX"  << QT_ENDL;
114 
115      QSqlQuery q;
116 
117      //QString stringQuery = QString("SELECT call, qso_date, bandid, modeid, qsl_sent, qsl_rcvd, station_callsign, id FROM log LIMIT 1");
118      QString stringQuery = QString("SELECT * FROM log _dateLIMIT 1");
119 
120      QSqlRecord rec; // = q.record();
121 
122      int nameCol;
123 
124      bool sqlOK = q.exec(stringQuery);
125      if (!sqlOK)
126      {
127          emit queryError(Q_FUNC_INFO, q.lastError().databaseText(), q.lastError().nativeErrorCode(), q.lastQuery());
128 
129      }
130     q.next();
131     rec = q.record(); // Number of columns
132     //qDebug() << "SearchModel::createSearchModel - query: " << q.lastQuery() << QT_ENDL;
133     //qDebug() << "SearchModel::createSearchModel - columns: " << QString::number(rec.count()) << QT_ENDL;
134 
135      nameCol = rec.indexOf("bandid");
136      setRelation(nameCol, QSqlRelation("band", "id", "name"));
137 
138      nameCol = rec.indexOf("modeid");
139      setRelation(nameCol, QSqlRelation("mode", "id", "submode"));
140 
141      nameCol = rec.indexOf("qso_date");
142      setHeaderData(nameCol, Qt::Horizontal, tr("Date/Time"));
143 
144      nameCol = rec.indexOf("call");
145      setHeaderData(nameCol, Qt::Horizontal,tr("Call"));
146 
147      nameCol = rec.indexOf("bandid");
148      setHeaderData(nameCol, Qt::Horizontal, tr("Band"));
149 
150      nameCol = rec.indexOf("modeid");
151      setHeaderData(nameCol, Qt::Horizontal, tr("Mode"));
152 
153      nameCol = rec.indexOf("qsl_sent");
154      setHeaderData(nameCol, Qt::Horizontal, tr("QSL Sent"));
155 
156      nameCol = rec.indexOf("qsl_rcvd");
157      setHeaderData(nameCol, Qt::Horizontal, tr("QSL Rcvd"));
158     if (stationCallsignInHeader)
159     {
160         nameCol = rec.indexOf("station_callsign");
161         setHeaderData(nameCol, Qt::Horizontal, tr("Station Callsign"));
162     }
163 
164      nameCol = rec.indexOf("id");
165      //setHeaderData(nameCol, Qt::Horizontal, tr("ID"));
166 
167      setSort(nameCol, Qt::AscendingOrder);
168  }
169 */
setFilterString(const QString & _st)170  void SearchModel::setFilterString(const QString &_st)
171  {
172     //qDebug() << "SearchModel::setFilterString: " << _st << QT_ENDL;
173     setFilter(_st);
174     select();
175     //qDebug() << "SearchModel::setFilterString: SelectStatement: " << selectStatement () << QT_ENDL;
176 
177  }
178 
update()179  void SearchModel::update()
180  {
181      select();
182  }
183 
setColors(const QString & _newOne,const QString & _needed,const QString & _worked,const QString & _confirmed,const QString & _default)184  void SearchModel::setColors (const QString &_newOne, const QString &_needed, const QString &_worked, const QString &_confirmed, const QString &_default)
185  {
186         //qDebug() << "DXClusterWidget::setColors: " << _newOne << "/" << _needed << "/" << _worked << "/" << _confirmed << "/" << _default << QT_ENDL;
187      // Just to pass the colors to the awards class
188      award->setColors(_newOne,  _needed, _worked,  _confirmed, _default);
189  }
190 
data(const QModelIndex & index,int role) const191 QVariant SearchModel::data( const QModelIndex &index, int role ) const
192  {
193      if ( index.isValid() && role == Qt::ForegroundRole )
194      {
195          if ( index.column() == 2 )
196          {
197              //QString _qrz = data(index, Qt::DisplayRole).toString();
198              //From Search QSO to QSL: q << _call << bandid << _mode << QString::number(currentLog);
199 
200              QString _dxcc = index.siblingAtColumn(dxcc).data().toString();
201              QString _bandid = index.siblingAtColumn(bandid).data().toString();
202              QString _modeid = index.siblingAtColumn(modeid).data().toString();
203              QString _log = index.siblingAtColumn(logn).data().toString();
204 
205              QStringList qs;
206              qs.clear();
207              qs << _dxcc << QString::number(dataProxy->getIdFromBandName(_bandid)) << "-1" << _log ;
208 
209              //spotBand = QString::number(world->getBandIdFromFreq(  dxFrequency  ) );
210              //qs << QString::number(dxEntity) << spotBand << "-1" << QString::number(currentLog) ;
211 
212             return QVariant( award->getQRZDXStatusColor(qs) );
213             // return QVariant( QColor( Qt::red ) );
214          }
215          return QVariant( QColor( Qt::black ) );
216      }
217 
218      return QSqlRelationalTableModel::data( index, role );
219  }
220 
221 
222