File: bookmarkstaglist.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 (316 lines) | stat: -rw-r--r-- 8,364 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* -*- c++ -*- */
/*
 * Gqrx SDR: Software defined radio receiver powered by GNU Radio and Qt
 *           http://gqrx.dk/
 *
 * Copyright 2014 Stefano Leucci, Christian Lindner DL2VCL.
 *
 * 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 "bookmarkstaglist.h"
#include "bookmarks.h"
#include <QColorDialog>
#include <stdio.h>
#include <QMenu>
#include <QHeaderView>

BookmarksTagList::BookmarksTagList(QWidget *parent, bool bShowUntagged )
    : QTableWidget(parent)
    , m_bUpdating(false)
    , m_bShowUntagged(bShowUntagged)
{
    connect(this, SIGNAL(cellClicked(int,int)),
            this, SLOT(on_cellClicked(int,int)));

    // right click menu
    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
            this, SLOT(ShowContextMenu(const QPoint&)));

    horizontalHeader()->setVisible(false);
    verticalHeader()->setVisible(false);
    setColumnCount(2);
    setColumnWidth(0, 20);
    horizontalHeader()->setStretchLastSection(true);
    setSelectionMode(QAbstractItemView::SingleSelection);
    setSelectionBehavior(QAbstractItemView::SelectRows);
    setSortingEnabled(true);
}

void BookmarksTagList::on_cellClicked(int row, int column)
{
    if(column==0)
    {
        changeColor(row, column);
    }
    if(column==1)
    {
        toggleCheckedState(row, column);
    }
}

void BookmarksTagList::changeColor(int row, int /*column*/)
{
    TagInfo &info = Bookmarks::Get().findOrAddTag(item(row, 1)->text());
    QColor color = QColorDialog::getColor(info.color, this);

    if(!color.isValid())
        return;

    info.color=color;
    updateTags();
    Bookmarks::Get().save();
}

void BookmarksTagList::toggleCheckedState(int row, int column)
{
    QTableWidgetItem* p = item(row,column);
    if(p->checkState()==Qt::Unchecked)
    {
        p->setCheckState(Qt::Checked);
    }
    else
    {
        p->setCheckState(Qt::Unchecked);
    }
}

void BookmarksTagList::updateTags()
{
    m_bUpdating = true;

    // Remember which items were unchecked.
    QStringList unchecked;
    for(int i=0; i<rowCount(); i++)
    {
        if(item(i,1)->checkState()==Qt::Unchecked)
            unchecked.append(item(i,1)->text());
    }

    // Get current List of Tags.
    QList<TagInfo> newTags = Bookmarks::Get().getTagList();
    if(!m_bShowUntagged)
    {
        for(int i=0; i<newTags.size(); ++i)
        {
            TagInfo& taginfo = newTags[i];
            if(taginfo.name.compare(TagInfo::strUntagged)==0)
            {
                newTags.removeAt(i);
                break;
            }
        }
    }

    // Rebuild List in GUI.
    clear();
    setSortingEnabled(false);
    setRowCount(0);
    for(int i=0; i<newTags.count(); i++)
    {
        AddTag(newTags[i].name,
                  ( unchecked.contains(newTags[i].name) ? Qt::Unchecked : Qt::Checked ),
                  newTags[i].color);
    }
    setSortingEnabled(true);

    m_bUpdating = false;
}

void BookmarksTagList::setSelectedTagsAsString(const QString& strTags)
{
    QStringList list = strTags.split(",");
    int iRows = rowCount();
    for(int i=0; i<iRows; ++i)
    {
        QTableWidgetItem* pItem = item(i,1);
        QString name = pItem->text();
        bool bChecked = list.contains(name);
        pItem->setCheckState(bChecked ? Qt::Checked : Qt::Unchecked);
    }
    setSortingEnabled(true);
}

void BookmarksTagList::setSelectedTags(QList<TagInfo*> tags)
{
    int iRows = rowCount();
    for(int i=0; i<iRows; ++i)
    {
        QTableWidgetItem* pItem = item(i,1);
        QString name = pItem->text();
        bool bChecked = false;
        for(QList<TagInfo*>::const_iterator it=tags.begin(), itend=tags.end(); it!=itend; ++it)
        {
            TagInfo* pTag = *it;
            if(pTag->name == name) bChecked = true;
        }
        pItem->setCheckState(bChecked ? Qt::Checked : Qt::Unchecked);
    }
    setSortingEnabled(true);
}

QString BookmarksTagList::getSelectedTagsAsString()
{
    QString strResult;

    int iRows = rowCount();
    bool bFirst = true;
    for(int i=0; i<iRows; ++i)
    {
        QTableWidgetItem* pItem = item(i,1);
        if(pItem->checkState() == Qt::Checked)
        {
            if(!bFirst) strResult += ", ";
            strResult += pItem->text();
            bFirst = false;
        }
    }
    return strResult;
}

void BookmarksTagList::ShowContextMenu(const QPoint& pos)
{
    QMenu* menu=new QMenu(this);

    // Rename currently does not work.
    // The problem is that after the tag name is changed in GUI
    // you can not find the right TagInfo because you dont know
    // the old tag name.
    #if 0
    // MenuItem "Rename"
    {
        QAction* actionRename = new QAction("Rename", this);
        menu->addAction(actionRename);
        connect(actionRename, SIGNAL(triggered()), this, SLOT(RenameSelectedTag()));
    }
    #endif

    // MenuItem "Create new Tag"
    {
        QAction* actionNewTag = new QAction("Create new Tag", this);
        menu->addAction(actionNewTag);
        connect(actionNewTag, SIGNAL(triggered()), this, SLOT(AddNewTag()));
    }

    // Menu "Delete Tag"
    {
        QAction* actionDeleteTag = new QAction("Delete Tag", this);
        menu->addAction(actionDeleteTag);
        connect(actionDeleteTag, SIGNAL(triggered()), this, SLOT(DeleteSelectedTag()));
    }

    // Menu "Select All"
    {
        QAction* action = new QAction("Select All", this);
        menu->addAction(action);
        connect(action, SIGNAL(triggered()), this, SLOT(SelectAll()));
    }

    // Menu "Deselect All"
    {
        QAction* action = new QAction("Deselect All", this);
        menu->addAction(action);
        connect(action, SIGNAL(triggered()), this, SLOT(DeselectAll()));
    }

    menu->popup(viewport()->mapToGlobal(pos));
}

#if 0
bool BookmarksTagList::RenameSelectedTag()
{
    QModelIndexList selected = selectionModel()->selectedRows();

    if(selected.empty())
    {
        return true;
    }

    int iRow = selected.first().row();
    QTableWidgetItem* pItem = item(iRow,1);bUpdating
    editItem(pItem);
    //Bookmarks::Get().save();

    return true;
}
#endif

void BookmarksTagList::AddNewTag()
{
    AddTag("*new*");
    scrollToBottom();
    editItem(item(rowCount()-1, 1));
}

void BookmarksTagList::AddTag(QString name, Qt::CheckState checkstate, QColor color)
{
    int i = rowCount();
    setRowCount(i+1);

    // Column 1
    QTableWidgetItem *item = new QTableWidgetItem(name);
    item->setCheckState(checkstate);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
    setItem(i, 1, item);

    // Column 0
    item = new QTableWidgetItem();
    item->setFlags(Qt::ItemIsEnabled);
    item->setBackgroundColor(color);
    setItem(i, 0, item);
}

void BookmarksTagList::DeleteSelectedTag()
{
    QModelIndexList selected = selectionModel()->selectedRows();
    if(selected.empty())
    {
        return;
    }
    int iRow = selected.first().row();
    QTableWidgetItem* pItem = item(iRow,1);
    QString strTagName = pItem->text();
    DeleteTag(strTagName);
    return;
}

void BookmarksTagList::DeleteTag(const QString& name)
{
    Bookmarks::Get().removeTag(name);
    updateTags();
}

void BookmarksTagList::SelectAll()
{
    int iRows = rowCount();
    for(int i=0; i<iRows; ++i)
    {
        QTableWidgetItem* pItem = item(i,1);
        QString name = pItem->text();
        pItem->setCheckState(Qt::Checked);
    }
}

void BookmarksTagList::DeselectAll()
{
    int iRows = rowCount();
    for(int i=0; i<iRows; ++i)
    {
        QTableWidgetItem* pItem = item(i,1);
        QString name = pItem->text();
        pItem->setCheckState(Qt::Unchecked);
    }
}