bookmarkstablemodel.cpp 4.8 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
/*
 * 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"


BookmarksTableModel::BookmarksTableModel(QObject *parent) :
丁劲犇's avatar
丁劲犇 已提交
30
	QAbstractTableModel(parent)
M
manjaro 已提交
31 32 33 34 35
{
}

int BookmarksTableModel::rowCount ( const QModelIndex & /*parent*/ ) const
{
丁劲犇's avatar
丁劲犇 已提交
36
	return m_Bookmarks.size();
M
manjaro 已提交
37 38 39
}
int BookmarksTableModel::columnCount ( const QModelIndex & /*parent*/ ) const
{
丁劲犇's avatar
丁劲犇 已提交
40
	return 5;
M
manjaro 已提交
41 42 43 44
}

QVariant BookmarksTableModel::headerData ( int section, Qt::Orientation orientation, int role ) const
{
丁劲犇's avatar
丁劲犇 已提交
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
	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();
M
manjaro 已提交
71 72 73 74
}

QVariant BookmarksTableModel::data ( const QModelIndex & index, int role ) const
{
丁劲犇's avatar
丁劲犇 已提交
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
	BookmarkInfo& info = *m_Bookmarks[index.row()];

	if(role==Qt::BackgroundRole)
	{
		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();
M
manjaro 已提交
110 111 112 113
}

bool BookmarksTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
丁劲犇's avatar
丁劲犇 已提交
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
	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:

			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;
M
manjaro 已提交
159 160 161 162
}

Qt::ItemFlags BookmarksTableModel::flags ( const QModelIndex& index ) const
{
丁劲犇's avatar
丁劲犇 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	Qt::ItemFlags flags = Qt::NoItemFlags;

	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;
M
manjaro 已提交
178 179 180 181
}

void BookmarksTableModel::update()
{
丁劲犇's avatar
丁劲犇 已提交
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
	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();
M
manjaro 已提交
207 208 209 210
}

BookmarkInfo *BookmarksTableModel::getBookmarkAtRow(int row)
{
丁劲犇's avatar
丁劲犇 已提交
211
	return m_Bookmarks[row];
M
manjaro 已提交
212 213 214 215 216 217 218
}

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