bookmarkstaglist.cpp 7.3 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
1
/* -*- c++ -*- */
M
manjaro 已提交
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
/*
 * 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 )
丁劲犇's avatar
丁劲犇 已提交
31 32 33
	: QTableWidget(parent)
	, m_bUpdating(false)
	, m_bShowUntagged(bShowUntagged)
M
manjaro 已提交
34
{
丁劲犇's avatar
丁劲犇 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	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);
M
manjaro 已提交
51 52 53 54
}

void BookmarksTagList::on_cellClicked(int row, int column)
{
丁劲犇's avatar
丁劲犇 已提交
55 56 57 58 59 60 61 62
	if(column==0)
	{
		changeColor(row, column);
	}
	if(column==1)
	{
		toggleCheckedState(row, column);
	}
M
manjaro 已提交
63 64 65 66
}

void BookmarksTagList::changeColor(int row, int /*column*/)
{
丁劲犇's avatar
丁劲犇 已提交
67 68
	TagInfo &info = Bookmarks::Get().findOrAddTag(item(row, 1)->text());
	QColor color = QColorDialog::getColor(info.color, this);
M
manjaro 已提交
69

丁劲犇's avatar
丁劲犇 已提交
70 71
	if(!color.isValid())
		return;
M
manjaro 已提交
72

丁劲犇's avatar
丁劲犇 已提交
73 74 75
	info.color=color;
	updateTags();
	Bookmarks::Get().save();
M
manjaro 已提交
76 77 78 79
}

void BookmarksTagList::toggleCheckedState(int row, int column)
{
丁劲犇's avatar
丁劲犇 已提交
80 81 82 83 84 85 86 87 88
	QTableWidgetItem* p = item(row,column);
	if(p->checkState()==Qt::Unchecked)
	{
		p->setCheckState(Qt::Checked);
	}
	else
	{
		p->setCheckState(Qt::Unchecked);
	}
M
manjaro 已提交
89 90 91 92
}

void BookmarksTagList::updateTags()
{
丁劲犇's avatar
丁劲犇 已提交
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
	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;
M
manjaro 已提交
131 132 133 134
}

void BookmarksTagList::setSelectedTagsAsString(const QString& strTags)
{
丁劲犇's avatar
丁劲犇 已提交
135 136 137 138 139 140 141 142 143 144
	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);
M
manjaro 已提交
145 146 147 148
}

void BookmarksTagList::setSelectedTags(QList<TagInfo*> tags)
{
丁劲犇's avatar
丁劲犇 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162
	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);
M
manjaro 已提交
163 164 165 166
}

QString BookmarksTagList::getSelectedTagsAsString()
{
丁劲犇's avatar
丁劲犇 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	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;
M
manjaro 已提交
182 183 184 185
}

void BookmarksTagList::ShowContextMenu(const QPoint& pos)
{
丁劲犇's avatar
丁劲犇 已提交
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
	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));
M
manjaro 已提交
230 231 232 233 234
}

#if 0
bool BookmarksTagList::RenameSelectedTag()
{
丁劲犇's avatar
丁劲犇 已提交
235
	QModelIndexList selected = selectionModel()->selectedRows();
M
manjaro 已提交
236

丁劲犇's avatar
丁劲犇 已提交
237 238 239 240
	if(selected.empty())
	{
		return true;
	}
M
manjaro 已提交
241

丁劲犇's avatar
丁劲犇 已提交
242 243 244 245
	int iRow = selected.first().row();
	QTableWidgetItem* pItem = item(iRow,1);bUpdating
	editItem(pItem);
	//Bookmarks::Get().save();
M
manjaro 已提交
246

丁劲犇's avatar
丁劲犇 已提交
247
	return true;
M
manjaro 已提交
248 249 250 251 252
}
#endif

void BookmarksTagList::AddNewTag()
{
丁劲犇's avatar
丁劲犇 已提交
253 254 255
	AddTag("*new*");
	scrollToBottom();
	editItem(item(rowCount()-1, 1));
M
manjaro 已提交
256 257 258 259
}

void BookmarksTagList::AddTag(QString name, Qt::CheckState checkstate, QColor color)
{
丁劲犇's avatar
丁劲犇 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273
	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->setBackground(color);
	setItem(i, 0, item);
M
manjaro 已提交
274 275 276 277
}

void BookmarksTagList::DeleteSelectedTag()
{
丁劲犇's avatar
丁劲犇 已提交
278 279 280 281 282 283 284 285 286 287
	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;
M
manjaro 已提交
288 289 290 291
}

void BookmarksTagList::DeleteTag(const QString& name)
{
丁劲犇's avatar
丁劲犇 已提交
292 293
	Bookmarks::Get().removeTag(name);
	updateTags();
M
manjaro 已提交
294 295 296 297
}

void BookmarksTagList::SelectAll()
{
丁劲犇's avatar
丁劲犇 已提交
298 299 300 301 302 303 304
	int iRows = rowCount();
	for(int i=0; i<iRows; ++i)
	{
		QTableWidgetItem* pItem = item(i,1);
		QString name = pItem->text();
		pItem->setCheckState(Qt::Checked);
	}
M
manjaro 已提交
305 306 307 308
}

void BookmarksTagList::DeselectAll()
{
丁劲犇's avatar
丁劲犇 已提交
309 310 311 312 313 314 315
	int iRows = rowCount();
	for(int i=0; i<iRows; ++i)
	{
		QTableWidgetItem* pItem = item(i,1);
		QString name = pItem->text();
		pItem->setCheckState(Qt::Unchecked);
	}
M
manjaro 已提交
316
}