File: bookmarkstablemodel.cpp

package info (click to toggle)
gqrx-sdr 2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,700 kB
  • ctags: 2,724
  • sloc: cpp: 22,413; ansic: 564; xml: 25; makefile: 4
file content (226 lines) | stat: -rw-r--r-- 6,330 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* -*- c++ -*- */
/*
 * Gqrx SDR: Software defined radio receiver powered by GNU Radio and Qt
 *           http://gqrx.dk/
 *
 * Copyright 2013 Christian Lindner DL2VCL, Stefano Leucci.
 *
 * Gqrx is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * Gqrx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gqrx; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */
#include <QFile>
#include <QStringList>
#include "bookmarks.h"
#include "bookmarkstablemodel.h"
#include "dockrxopt.h"


BookmarksTableModel::BookmarksTableModel(QObject *parent) :
    QAbstractTableModel(parent)
{
}

int BookmarksTableModel::rowCount ( const QModelIndex & /*parent*/ ) const
{
    return m_Bookmarks.size();
}
int BookmarksTableModel::columnCount ( const QModelIndex & /*parent*/ ) const
{
    return 5;
}

QVariant BookmarksTableModel::headerData ( int section, Qt::Orientation orientation, int role ) const
{
    if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
    {
        switch(section)
        {
        case COL_FREQUENCY:
            return QString("Frequency");
            break;
        case COL_NAME:
            return QString("Name");
            break;
        case COL_MODULATION:
            return QString("Modulation");
            break;
        case COL_BANDWIDTH:
            return QString("Bandwidth");
            break;
        case COL_TAGS:
            return QString("Tag");
            break;
        }
    }
    if(orientation == Qt::Vertical && role == Qt::DisplayRole)
    {
        return section;
    }
    return QVariant();
}

QVariant BookmarksTableModel::data ( const QModelIndex & index, int role ) const
{
    BookmarkInfo& info = *m_Bookmarks[index.row()];

    if(role==Qt::BackgroundColorRole)
    {
        QColor bg(info.GetColor());
        bg.setAlpha(0x60);
        return bg;
    }
    else if(role == Qt::DisplayRole || role==Qt::EditRole)
    {
        switch(index.column())
        {
        case COL_FREQUENCY:
                return info.frequency;
        case COL_NAME:
                return (role==Qt::EditRole)?QString(info.name):info.name;
        case COL_MODULATION:
                return info.modulation;
        case COL_BANDWIDTH:
            return (info.bandwidth==0)?QVariant(""):QVariant(info.bandwidth);
         case COL_TAGS:
            QString strTags;
            for(int iTag=0; iTag<info.tags.size(); ++iTag)
            {
                if(iTag!=0)
                {
                    strTags.append(",");
                }
                TagInfo& tag = *info.tags[iTag];
                strTags.append(tag.name);
            }
            return strTags;
        }
    }
    return QVariant();
}

bool BookmarksTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(role==Qt::EditRole)
    {
        BookmarkInfo &info = *m_Bookmarks[index.row()];
        switch(index.column())
        {
        case COL_FREQUENCY:
            {
                info.frequency = value.toLongLong();
                emit dataChanged(index, index);
            }
            break;
        case COL_NAME:
            {
                info.name = value.toString();
                emit dataChanged(index, index);
                return true;
            }
            break;
        case COL_MODULATION:
            {
                Q_ASSERT(!value.toString().contains(";")); // may not contain a comma because tablemodel is saved as comma-separated file (csv).
                if(DockRxOpt::IsModulationValid(value.toString()))
                {
                    info.modulation = value.toString();
                    emit dataChanged(index, index);
                }
            }
            break;
        case COL_BANDWIDTH:
            {
                info.bandwidth = value.toInt();
                emit dataChanged(index, index);
            }
            break;
        case COL_TAGS:
            {
                info.tags.clear();
                QString strValue = value.toString();
                QStringList strList = strValue.split(",");
                for(int i=0; i<strList.size(); ++i)
                {
                    QString strTag = strList[i].trimmed();
                    info.tags.append( &Bookmarks::Get().findOrAddTag(strTag) );
                }
                emit dataChanged(index, index);
                return true;
            }
            break;
        }
        return true; // return true means success
    }
    return false;
}

Qt::ItemFlags BookmarksTableModel::flags ( const QModelIndex& index ) const
{
    Qt::ItemFlags flags = 0;

    switch(index.column())
    {
    case COL_FREQUENCY:
    case COL_NAME:
    case COL_BANDWIDTH:
    case COL_MODULATION:
        flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
        break;
    case COL_TAGS:
        flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
        break;
    }
    return flags;
}

void BookmarksTableModel::update()
{
    int iRow = 0;
    m_Bookmarks.clear();
    for(int iBookmark=0; iBookmark<Bookmarks::Get().size(); iBookmark++)
    {
        BookmarkInfo& info = Bookmarks::Get().getBookmark(iBookmark);

        bool bActive = false;
        for(int iTag=0; iTag<info.tags.size(); ++iTag)
        {
            TagInfo& tag = *info.tags[iTag];
            if(tag.active)
            {
                bActive = true;
                break;
            }
        }
        if(bActive)
        {
            m_mapRowToBookmarksIndex[iRow]=iBookmark;
            m_Bookmarks.append(&info);
            ++iRow;
        }
    }

    emit layoutChanged();
}

BookmarkInfo *BookmarksTableModel::getBookmarkAtRow(int row)
{
    return m_Bookmarks[row];
}

int BookmarksTableModel::GetBookmarksIndexForRow(int iRow)
{
  return m_mapRowToBookmarksIndex[iRow];
}