From d8c700311c6acf378ffe7c512d2e224cc9701774 Mon Sep 17 00:00:00 2001 From: goldenhawking Date: Sat, 6 Feb 2021 14:19:41 +0800 Subject: [PATCH] Qt6 deprecated issues. --- uhd_cpp/uhd_spectrum/qtgui/bookmarks.cpp | 478 +++--- .../qtgui/bookmarkstablemodel.cpp | 302 ++-- .../uhd_spectrum/qtgui/bookmarkstaglist.cpp | 436 +++--- uhd_cpp/uhd_spectrum/qtgui/freqctrl.cpp | 1298 +++++++++-------- uhd_cpp/uhd_spectrum/qtgui/freqctrl.h | 217 +-- uhd_cpp/uhd_spectrum/qtgui/plotter.cpp | 28 +- uhd_cpp/uhd_spectrum/uhd_device_win32.cpp | 498 +++---- 7 files changed, 1633 insertions(+), 1624 deletions(-) diff --git a/uhd_cpp/uhd_spectrum/qtgui/bookmarks.cpp b/uhd_cpp/uhd_spectrum/qtgui/bookmarks.cpp index 5f9c280..db5594f 100644 --- a/uhd_cpp/uhd_spectrum/qtgui/bookmarks.cpp +++ b/uhd_cpp/uhd_spectrum/qtgui/bookmarks.cpp @@ -1,4 +1,4 @@ -/* -*- c++ -*- */ +/* -*- c++ -*- */ /* * Gqrx SDR: Software defined radio receiver powered by GNU Radio and Qt * http://gqrx.dk/ @@ -30,310 +30,310 @@ #include "bookmarks.h" #include #include - +#include const QColor TagInfo::DefaultColor(Qt::lightGray); const QString TagInfo::strUntagged("Untagged"); Bookmarks* Bookmarks::m_pThis = 0; Bookmarks::Bookmarks() { - TagInfo tag(TagInfo::strUntagged); - m_TagList.append(tag); + TagInfo tag(TagInfo::strUntagged); + m_TagList.append(tag); } void Bookmarks::create() { - m_pThis = new Bookmarks; + m_pThis = new Bookmarks; } Bookmarks& Bookmarks::Get() { - return *m_pThis; + return *m_pThis; } void Bookmarks::setConfigDir(const QString& cfg_dir) { - m_bookmarksFile = cfg_dir + "/bookmarks.csv"; - printf("BookmarksFile is %s\n", m_bookmarksFile.toStdString().c_str()); + m_bookmarksFile = cfg_dir + "/bookmarks.csv"; + printf("BookmarksFile is %s\n", m_bookmarksFile.toStdString().c_str()); } void Bookmarks::add(BookmarkInfo &info) { - m_BookmarkList.append(info); - std::stable_sort(m_BookmarkList.begin(),m_BookmarkList.end()); - save(); - emit( BookmarksChanged() ); + m_BookmarkList.append(info); + std::stable_sort(m_BookmarkList.begin(),m_BookmarkList.end()); + save(); + emit( BookmarksChanged() ); } void Bookmarks::remove(int index) { - m_BookmarkList.removeAt(index); - save(); - emit BookmarksChanged(); + m_BookmarkList.removeAt(index); + save(); + emit BookmarksChanged(); } bool Bookmarks::load() { - QFile file(m_bookmarksFile); - if (file.open(QIODevice::ReadOnly | QIODevice::Text)) - { - m_BookmarkList.clear(); - m_TagList.clear(); - - // always create the "Untagged" entry. - findOrAddTag(TagInfo::strUntagged); - - // Read Tags, until first empty line. - while (!file.atEnd()) - { - QString line = QString::fromUtf8(file.readLine().trimmed()); - - if(line.isEmpty()) - break; - - if(line.startsWith("#")) - continue; - - QStringList strings = line.split(";"); - if(strings.count() == 2) - { - TagInfo &info = findOrAddTag(strings[0]); - info.color = QColor(strings[1].trimmed()); - } - else - { - printf("\nBookmarks: Ignoring Line:\n %s\n", line.toLatin1().data()); - } - } - std::sort(m_TagList.begin(),m_TagList.end()); - - // Read Bookmarks, after first empty line. - while (!file.atEnd()) - { - QString line = QString::fromUtf8(file.readLine().trimmed()); - if(line.isEmpty() || line.startsWith("#")) - continue; - - QStringList strings = line.split(";"); - if(strings.count() == 5) - { - BookmarkInfo info; - info.frequency = strings[0].toLongLong(); - info.name = strings[1].trimmed(); - info.modulation = strings[2].trimmed(); - info.bandwidth = strings[3].toInt(); - // Multiple Tags may be separated by comma. - QString strTags = strings[4]; - QStringList TagList = strTags.split(","); - for(int iTag=0; iTag usedTags; - for (int iBookmark = 0; iBookmark < m_BookmarkList.size(); iBookmark++) - { - BookmarkInfo& info = m_BookmarkList[iBookmark]; - for(int iTag = 0; iTag < info.tags.size(); ++iTag) - { - TagInfo& tag = *info.tags[iTag]; - usedTags.insert(&tag); - } - } - - for (QSet::iterator i = usedTags.begin(); i != usedTags.end(); i++) - { - TagInfo& info = **i; - stream << info.name.leftJustified(20) + "; " + info.color.name() << endl; - } - - stream << endl; - - stream << QString("# Frequency").leftJustified(12) + "; " + - QString("Name").leftJustified(25)+ "; " + - QString("Modulation").leftJustified(20) + "; " + - QString("Bandwidth").rightJustified(10) + "; " + - QString("Tags") << endl; - - for (int i = 0; i < m_BookmarkList.size(); i++) - { - BookmarkInfo& info = m_BookmarkList[i]; - QString line = QString::number(info.frequency).rightJustified(12) + - "; " + info.name.leftJustified(25) + "; " + - info.modulation.leftJustified(20)+ "; " + - QString::number(info.bandwidth).rightJustified(10) + "; "; - for(int iTag = 0; iTag usedTags; + for (int iBookmark = 0; iBookmark < m_BookmarkList.size(); iBookmark++) + { + BookmarkInfo& info = m_BookmarkList[iBookmark]; + for(int iTag = 0; iTag < info.tags.size(); ++iTag) + { + TagInfo& tag = *info.tags[iTag]; + usedTags.insert(&tag); + } + } + + for (QSet::iterator i = usedTags.begin(); i != usedTags.end(); i++) + { + TagInfo& info = **i; + stream << info.name.leftJustified(20) + "; " + info.color.name() << Qt::endl; + } + + stream << Qt::endl; + + stream << QString("# Frequency").leftJustified(12) + "; " + + QString("Name").leftJustified(25)+ "; " + + QString("Modulation").leftJustified(20) + "; " + + QString("Bandwidth").rightJustified(10) + "; " + + QString("Tags") << Qt::endl; + + for (int i = 0; i < m_BookmarkList.size(); i++) + { + BookmarkInfo& info = m_BookmarkList[i]; + QString line = QString::number(info.frequency).rightJustified(12) + + "; " + info.name.leftJustified(25) + "; " + + info.modulation.leftJustified(20)+ "; " + + QString::number(info.bandwidth).rightJustified(10) + "; "; + for(int iTag = 0; iTag Bookmarks::getBookmarksInRange(qint64 low, qint64 high) { - BookmarkInfo info; - info.frequency=low; - QList::const_iterator lb = qLowerBound(m_BookmarkList, info); - info.frequency=high; - QList::const_iterator ub = qUpperBound(m_BookmarkList, info); - - QList found; - - while (lb != ub) - { - const BookmarkInfo& info = *lb; - //if(info.IsActive()) - { - found.append(info); - } - lb++; - } - - return found; + BookmarkInfo info; + info.frequency=low; + QList::const_iterator lb = std::lower_bound(m_BookmarkList.begin(), m_BookmarkList.end(),info); + info.frequency=high; + QList::const_iterator ub = std::upper_bound(m_BookmarkList.begin(), m_BookmarkList.end(),info); + + QList found; + + while (lb != ub) + { + const BookmarkInfo& info = *lb; + //if(info.IsActive()) + { + found.append(info); + } + lb++; + } + + return found; } TagInfo &Bookmarks::findOrAddTag(QString tagName) { - tagName = tagName.trimmed(); + tagName = tagName.trimmed(); - if (tagName.isEmpty()) - tagName=TagInfo::strUntagged; + if (tagName.isEmpty()) + tagName=TagInfo::strUntagged; - int idx = getTagIndex(tagName); + int idx = getTagIndex(tagName); - if (idx != -1) - return m_TagList[idx]; + if (idx != -1) + return m_TagList[idx]; - TagInfo info; - info.name=tagName; - m_TagList.append(info); - emit TagListChanged(); - return m_TagList.last(); + TagInfo info; + info.name=tagName; + m_TagList.append(info); + emit TagListChanged(); + return m_TagList.last(); } bool Bookmarks::removeTag(QString tagName) { - tagName = tagName.trimmed(); - - // Do not delete "Untagged" tag. - if(tagName.compare(TagInfo::strUntagged, tagName)==0) - return false; - - int idx = getTagIndex(tagName); - if (idx == -1) - return false; - - // Delete Tag from all Bookmarks that use it. - TagInfo* pTagToDelete = &m_TagList[idx]; - for(int i=0; i1) bmi.tags.removeAt(t); - else bmi.tags[0] = &findOrAddTag(TagInfo::strUntagged); - } - } - } - - // Delete Tag. - m_TagList.removeAt(idx); - - emit BookmarksChanged(); - emit TagListChanged(); - - return true; + tagName = tagName.trimmed(); + + // Do not delete "Untagged" tag. + if(tagName.compare(TagInfo::strUntagged, tagName)==0) + return false; + + int idx = getTagIndex(tagName); + if (idx == -1) + return false; + + // Delete Tag from all Bookmarks that use it. + TagInfo* pTagToDelete = &m_TagList[idx]; + for(int i=0; i1) bmi.tags.removeAt(t); + else bmi.tags[0] = &findOrAddTag(TagInfo::strUntagged); + } + } + } + + // Delete Tag. + m_TagList.removeAt(idx); + + emit BookmarksChanged(); + emit TagListChanged(); + + return true; } bool Bookmarks::setTagChecked(QString tagName, bool bChecked) { - int idx = getTagIndex(tagName); - if (idx == -1) return false; - m_TagList[idx].active = bChecked; - emit BookmarksChanged(); - emit TagListChanged(); - return true; + int idx = getTagIndex(tagName); + if (idx == -1) return false; + m_TagList[idx].active = bChecked; + emit BookmarksChanged(); + emit TagListChanged(); + return true; } int Bookmarks::getTagIndex(QString tagName) { - tagName = tagName.trimmed(); - for (int i = 0; i < m_TagList.size(); i++) - { - if (m_TagList[i].name == tagName) - return i; - } - - return -1; + tagName = tagName.trimmed(); + for (int i = 0; i < m_TagList.size(); i++) + { + if (m_TagList[i].name == tagName) + return i; + } + + return -1; } const QColor BookmarkInfo::GetColor() const { - for(int iTag=0; iTag BookmarksTagList::BookmarksTagList(QWidget *parent, bool bShowUntagged ) - : QTableWidget(parent) - , m_bUpdating(false) - , m_bShowUntagged(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); + 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); - } + 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); + TagInfo &info = Bookmarks::Get().findOrAddTag(item(row, 1)->text()); + QColor color = QColorDialog::getColor(info.color, this); - if(!color.isValid()) - return; + if(!color.isValid()) + return; - info.color=color; - updateTags(); - Bookmarks::Get().save(); + 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); - } + 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; icheckState()==Qt::Unchecked) - unchecked.append(item(i,1)->text()); - } - - // Get current List of Tags. - QList newTags = Bookmarks::Get().getTagList(); - if(!m_bShowUntagged) - { - for(int i=0; icheckState()==Qt::Unchecked) + unchecked.append(item(i,1)->text()); + } + + // Get current List of Tags. + QList newTags = Bookmarks::Get().getTagList(); + if(!m_bShowUntagged) + { + for(int i=0; itext(); - bool bChecked = list.contains(name); - pItem->setCheckState(bChecked ? Qt::Checked : Qt::Unchecked); - } - setSortingEnabled(true); + QStringList list = strTags.split(","); + int iRows = rowCount(); + for(int i=0; itext(); + bool bChecked = list.contains(name); + pItem->setCheckState(bChecked ? Qt::Checked : Qt::Unchecked); + } + setSortingEnabled(true); } void BookmarksTagList::setSelectedTags(QList tags) { - int iRows = rowCount(); - for(int i=0; itext(); - bool bChecked = false; - for(QList::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); + int iRows = rowCount(); + for(int i=0; itext(); + bool bChecked = false; + for(QList::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; icheckState() == Qt::Checked) - { - if(!bFirst) strResult += ", "; - strResult += pItem->text(); - bFirst = false; - } - } - return strResult; + QString strResult; + + int iRows = rowCount(); + bool bFirst = true; + for(int i=0; icheckState() == 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)); + 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(); + QModelIndexList selected = selectionModel()->selectedRows(); - if(selected.empty()) - { - return true; - } + if(selected.empty()) + { + return true; + } - int iRow = selected.first().row(); - QTableWidgetItem* pItem = item(iRow,1);bUpdating - editItem(pItem); - //Bookmarks::Get().save(); + int iRow = selected.first().row(); + QTableWidgetItem* pItem = item(iRow,1);bUpdating + editItem(pItem); + //Bookmarks::Get().save(); - return true; + return true; } #endif void BookmarksTagList::AddNewTag() { - AddTag("*new*"); - scrollToBottom(); - editItem(item(rowCount()-1, 1)); + 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); + 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); } 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; + 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(); + Bookmarks::Get().removeTag(name); + updateTags(); } void BookmarksTagList::SelectAll() { - int iRows = rowCount(); - for(int i=0; itext(); - pItem->setCheckState(Qt::Checked); - } + int iRows = rowCount(); + for(int i=0; itext(); + pItem->setCheckState(Qt::Checked); + } } void BookmarksTagList::DeselectAll() { - int iRows = rowCount(); - for(int i=0; itext(); - pItem->setCheckState(Qt::Unchecked); - } + int iRows = rowCount(); + for(int i=0; itext(); + pItem->setCheckState(Qt::Unchecked); + } } diff --git a/uhd_cpp/uhd_spectrum/qtgui/freqctrl.cpp b/uhd_cpp/uhd_spectrum/qtgui/freqctrl.cpp index 2e26b59..8c80fb8 100644 --- a/uhd_cpp/uhd_spectrum/qtgui/freqctrl.cpp +++ b/uhd_cpp/uhd_spectrum/qtgui/freqctrl.cpp @@ -1,4 +1,4 @@ -/* +/* * Frequency controller widget (originally from CuteSDR) * * This file is part of gqrx sdr. @@ -43,32 +43,32 @@ #define SEPRATIO_D 3 // separation rectangle size ratio denominator #define STATUS_TIP \ - "Scroll or left-click to increase/decrease digit. " \ - "Right-click to clear digits." + "Scroll or left-click to increase/decrease digit. " \ + "Right-click to clear digits." CFreqCtrl::CFreqCtrl(QWidget *parent) : - QFrame(parent) -{ - setAutoFillBackground(false); - setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - setFocusPolicy(Qt::StrongFocus); - setMouseTracking(true); - m_BkColor = QColor(0x1F, 0x1D, 0x1D, 0xFF); - m_InactiveColor = QColor(0x43, 0x43, 0x43, 0xFF); - m_DigitColor = QColor(0xFF, 0xFF, 0xFF, 0xFF); - m_HighlightColor = QColor(0x5A, 0x5A, 0x5A, 0xFF); - m_UnitsColor = Qt::gray; + QFrame(parent) +{ + setAutoFillBackground(false); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + setFocusPolicy(Qt::StrongFocus); + setMouseTracking(true); + m_BkColor = QColor(0x1F, 0x1D, 0x1D, 0xFF); + m_InactiveColor = QColor(0x43, 0x43, 0x43, 0xFF); + m_DigitColor = QColor(0xFF, 0xFF, 0xFF, 0xFF); + m_HighlightColor = QColor(0x5A, 0x5A, 0x5A, 0xFF); + m_UnitsColor = Qt::gray; m_freq = 0; setup(0, 1, 6000000000U, 1, FCTL_UNIT_NONE); - m_Oldfreq = 0; - m_LastLeadZeroPos = 0; - m_LRMouseFreqSel = false; - m_ActiveEditDigit = -1; - m_ResetLowerDigits = true; - m_UnitsFont = QFont("Arial", 12, QFont::Normal); - m_DigitFont = QFont("Arial", 12, QFont::Normal); + m_Oldfreq = 0; + m_LastLeadZeroPos = 0; + m_LRMouseFreqSel = false; + m_ActiveEditDigit = -1; + m_ResetLowerDigits = true; + m_UnitsFont = QFont("Arial", 12, QFont::Normal); + m_DigitFont = QFont("Arial", 12, QFont::Normal); - setStatusTip(tr(STATUS_TIP)); + setStatusTip(tr(STATUS_TIP)); } CFreqCtrl::~CFreqCtrl() @@ -77,775 +77,783 @@ CFreqCtrl::~CFreqCtrl() QSize CFreqCtrl::minimumSizeHint() const { - return QSize(100, 20); + return QSize(100, 20); } QSize CFreqCtrl::sizeHint() const { - return QSize(100, 20); + return QSize(100, 20); } bool CFreqCtrl::inRect(QRect &rect, QPoint &point) { - if ((point.x() < rect.right()) && (point.x() > rect.x()) && - (point.y() < rect.bottom()) && (point.y() > rect.y())) - return true; - else - return false; + if ((point.x() < rect.right()) && (point.x() > rect.x()) && + (point.y() < rect.bottom()) && (point.y() > rect.y())) + return true; + else + return false; +} +bool CFreqCtrl::inRect(QRect &rect, QPointF &point) +{ + if ((point.x() < rect.right()) && (point.x() > rect.x()) && + (point.y() < rect.bottom()) && (point.y() > rect.y())) + return true; + else + return false; } static int fmax_to_numdigits(qint64 fmax) { - if (fmax < 10e6) - return 7; - else if (fmax < 100e6) - return 8; - else if (fmax < 1e9) - return 9; - else if (fmax < 10e9) - return 10; - else if (fmax < 100e9) - return 11; + if (fmax < 10e6) + return 7; + else if (fmax < 100e6) + return 8; + else if (fmax < 1e9) + return 9; + else if (fmax < 10e9) + return 10; + else if (fmax < 100e9) + return 11; - return 12; + return 12; } void CFreqCtrl::setup(int NumDigits, qint64 Minf, qint64 Maxf, int MinStep, - FctlUnit unit) + FctlUnit unit) { - int i; - qint64 pwr = 1; - m_LastEditDigit = 0; - m_Oldfreq = -1; + int i; + qint64 pwr = 1; + m_LastEditDigit = 0; + m_Oldfreq = -1; - m_NumDigits = NumDigits ? NumDigits : fmax_to_numdigits(Maxf); + m_NumDigits = NumDigits ? NumDigits : fmax_to_numdigits(Maxf); - if (m_NumDigits > FCTL_MAX_DIGITS) - m_NumDigits = FCTL_MAX_DIGITS; + if (m_NumDigits > FCTL_MAX_DIGITS) + m_NumDigits = FCTL_MAX_DIGITS; - if (m_NumDigits < FCTL_MIN_DIGITS) - m_NumDigits = FCTL_MIN_DIGITS; + if (m_NumDigits < FCTL_MIN_DIGITS) + m_NumDigits = FCTL_MIN_DIGITS; - m_UnitString = ""; - m_MinStep = MinStep; + m_UnitString = ""; + m_MinStep = MinStep; - if (m_MinStep == 0) - m_MinStep = 1; + if (m_MinStep == 0) + m_MinStep = 1; - m_MinFreq = Minf; - m_MaxFreq = Maxf; + m_MinFreq = Minf; + m_MaxFreq = Maxf; - if (m_freq < m_MinFreq) - m_freq = m_MinFreq; + if (m_freq < m_MinFreq) + m_freq = m_MinFreq; - if (m_freq > m_MaxFreq) - m_freq = m_MaxFreq; + if (m_freq > m_MaxFreq) + m_freq = m_MaxFreq; - for (i = 0; i < m_NumDigits; i++) - { - m_DigitInfo[i].weight = pwr; - m_DigitInfo[i].incval = pwr; - m_DigitInfo[i].modified = true; - m_DigitInfo[i].editmode = false; - m_DigitInfo[i].val = 0; - pwr *= 10; - } + for (i = 0; i < m_NumDigits; i++) + { + m_DigitInfo[i].weight = pwr; + m_DigitInfo[i].incval = pwr; + m_DigitInfo[i].modified = true; + m_DigitInfo[i].editmode = false; + m_DigitInfo[i].val = 0; + pwr *= 10; + } - if (m_MaxFreq > pwr) - m_MaxFreq = pwr - 1; + if (m_MaxFreq > pwr) + m_MaxFreq = pwr - 1; - m_MaxFreq = m_MaxFreq - m_MaxFreq % m_MinStep; + m_MaxFreq = m_MaxFreq - m_MaxFreq % m_MinStep; - if (m_MinFreq > pwr) - m_MinFreq = 1; + if (m_MinFreq > pwr) + m_MinFreq = 1; - m_MinFreq = m_MinFreq - m_MinFreq % m_MinStep; - m_DigStart = 0; + m_MinFreq = m_MinFreq - m_MinFreq % m_MinStep; + m_DigStart = 0; - setUnit(unit); + setUnit(unit); - for (i = m_NumDigits - 1; i >= 0; i--) - { - if (m_DigitInfo[i].weight <= m_MinStep) - { - if (m_DigStart == 0) - { - m_DigitInfo[i].incval = m_MinStep; - m_DigStart = i; - } - else - { - if ((m_MinStep % m_DigitInfo[i + 1].weight) != 0) - m_DigStart = i; - m_DigitInfo[i].incval = 0; - } - } - } + for (i = m_NumDigits - 1; i >= 0; i--) + { + if (m_DigitInfo[i].weight <= m_MinStep) + { + if (m_DigStart == 0) + { + m_DigitInfo[i].incval = m_MinStep; + m_DigStart = i; + } + else + { + if ((m_MinStep % m_DigitInfo[i + 1].weight) != 0) + m_DigStart = i; + m_DigitInfo[i].incval = 0; + } + } + } - m_NumSeps = (m_NumDigits - 1) / 3 - m_DigStart / 3; + m_NumSeps = (m_NumDigits - 1) / 3 - m_DigStart / 3; } void CFreqCtrl::setFrequency(qint64 freq) { - int i; - qint64 acc = 0; - qint64 rem; - int val; - - if (freq == m_Oldfreq) - return; - - if (freq < m_MinFreq) - freq = m_MinFreq; - - if (freq > m_MaxFreq) - freq = m_MaxFreq; - - m_freq = freq - freq % m_MinStep; - rem = m_freq; - m_LeadZeroPos = m_NumDigits; - - for (i = m_NumDigits - 1; i >= m_DigStart; i--) - { - val = (int)(rem / m_DigitInfo[i].weight); - if (m_DigitInfo[i].val != val) - { - m_DigitInfo[i].val = val; - m_DigitInfo[i].modified = true; - } - rem = rem - val * m_DigitInfo[i].weight; - acc += val; - if ((acc == 0) && (i > m_DecPos)) - { - m_LeadZeroPos = i; - } - } - - // If the sign changed and the frequency is less than 1 unit, - // redraw the leading zero to get the correct sign. - if ((m_Oldfreq ^ m_freq) < 0 && m_DigitInfo[m_LeadZeroPos - 1].val == 0) - m_DigitInfo[m_LeadZeroPos - 1].modified = true; - - // When frequency is negative all non-zero digits that - // have changed will have a negative sign. This loop will - // change all digits back to positive, except the one at - // position m_leadZeroPos-1. If that position is zero, - // it will be checked in the drawing method, drawDigits(). - /** TBC if this works for all configurations */ - if (m_freq < 0) - { - if (m_DigitInfo[m_LeadZeroPos - 1].val > 0) - m_DigitInfo[m_LeadZeroPos - - 1].val = -m_DigitInfo[m_LeadZeroPos - 1].val; - - for (i = 0; i < (m_LeadZeroPos - 1); i++) - { - if (m_DigitInfo[i].val < 0) - m_DigitInfo[i].val = -m_DigitInfo[i].val; - } - } - - // signal the new frequency to world - m_Oldfreq = m_freq; - emit newFrequency(m_freq); - updateCtrl(m_LastLeadZeroPos != m_LeadZeroPos); - m_LastLeadZeroPos = m_LeadZeroPos; + int i; + qint64 acc = 0; + qint64 rem; + int val; + + if (freq == m_Oldfreq) + return; + + if (freq < m_MinFreq) + freq = m_MinFreq; + + if (freq > m_MaxFreq) + freq = m_MaxFreq; + + m_freq = freq - freq % m_MinStep; + rem = m_freq; + m_LeadZeroPos = m_NumDigits; + + for (i = m_NumDigits - 1; i >= m_DigStart; i--) + { + val = (int)(rem / m_DigitInfo[i].weight); + if (m_DigitInfo[i].val != val) + { + m_DigitInfo[i].val = val; + m_DigitInfo[i].modified = true; + } + rem = rem - val * m_DigitInfo[i].weight; + acc += val; + if ((acc == 0) && (i > m_DecPos)) + { + m_LeadZeroPos = i; + } + } + + // If the sign changed and the frequency is less than 1 unit, + // redraw the leading zero to get the correct sign. + if ((m_Oldfreq ^ m_freq) < 0 && m_DigitInfo[m_LeadZeroPos - 1].val == 0) + m_DigitInfo[m_LeadZeroPos - 1].modified = true; + + // When frequency is negative all non-zero digits that + // have changed will have a negative sign. This loop will + // change all digits back to positive, except the one at + // position m_leadZeroPos-1. If that position is zero, + // it will be checked in the drawing method, drawDigits(). + /** TBC if this works for all configurations */ + if (m_freq < 0) + { + if (m_DigitInfo[m_LeadZeroPos - 1].val > 0) + m_DigitInfo[m_LeadZeroPos - + 1].val = -m_DigitInfo[m_LeadZeroPos - 1].val; + + for (i = 0; i < (m_LeadZeroPos - 1); i++) + { + if (m_DigitInfo[i].val < 0) + m_DigitInfo[i].val = -m_DigitInfo[i].val; + } + } + + // signal the new frequency to world + m_Oldfreq = m_freq; + emit newFrequency(m_freq); + updateCtrl(m_LastLeadZeroPos != m_LeadZeroPos); + m_LastLeadZeroPos = m_LeadZeroPos; } void CFreqCtrl::setDigitColor(QColor col) { - m_UpdateAll = true; - m_DigitColor = col; - for (int i = m_DigStart; i < m_NumDigits; i++) - m_DigitInfo[i].modified = true; - updateCtrl(true); + m_UpdateAll = true; + m_DigitColor = col; + for (int i = m_DigStart; i < m_NumDigits; i++) + m_DigitInfo[i].modified = true; + updateCtrl(true); } void CFreqCtrl::setUnit(FctlUnit unit) { - m_NumDigitsForUnit = 2; - switch (unit) - { - case FCTL_UNIT_NONE: - m_NumDigitsForUnit = 0; - m_DecPos = 0; - m_UnitString = QString(); - break; - case FCTL_UNIT_HZ: - m_DecPos = 0; - m_UnitString = "Hz "; - break; - case FCTL_UNIT_KHZ: - m_DecPos = 3; - m_UnitString = "kHz"; - break; - case FCTL_UNIT_MHZ: - m_DecPos = 6; - m_UnitString = "MHz"; - break; - case FCTL_UNIT_GHZ: - m_DecPos = 9; - m_UnitString = "GHz"; - break; - case FCTL_UNIT_SEC: - m_DecPos = 6; - m_UnitString = "Sec"; - break; - case FCTL_UNIT_MSEC: - m_DecPos = 3; - m_UnitString = "mS "; - break; - case FCTL_UNIT_USEC: - m_DecPos = 0; - m_UnitString = "uS "; - break; - case FCTL_UNIT_NSEC: - m_DecPos = 0; - m_UnitString = "nS "; - break; - } - m_Unit = unit; - m_UpdateAll = true; - updateCtrl(true); + m_NumDigitsForUnit = 2; + switch (unit) + { + case FCTL_UNIT_NONE: + m_NumDigitsForUnit = 0; + m_DecPos = 0; + m_UnitString = QString(); + break; + case FCTL_UNIT_HZ: + m_DecPos = 0; + m_UnitString = "Hz "; + break; + case FCTL_UNIT_KHZ: + m_DecPos = 3; + m_UnitString = "kHz"; + break; + case FCTL_UNIT_MHZ: + m_DecPos = 6; + m_UnitString = "MHz"; + break; + case FCTL_UNIT_GHZ: + m_DecPos = 9; + m_UnitString = "GHz"; + break; + case FCTL_UNIT_SEC: + m_DecPos = 6; + m_UnitString = "Sec"; + break; + case FCTL_UNIT_MSEC: + m_DecPos = 3; + m_UnitString = "mS "; + break; + case FCTL_UNIT_USEC: + m_DecPos = 0; + m_UnitString = "uS "; + break; + case FCTL_UNIT_NSEC: + m_DecPos = 0; + m_UnitString = "nS "; + break; + } + m_Unit = unit; + m_UpdateAll = true; + updateCtrl(true); } void CFreqCtrl::setBgColor(QColor col) { - m_UpdateAll = true; - m_BkColor = col; + m_UpdateAll = true; + m_BkColor = col; - for (int i = m_DigStart; i < m_NumDigits; i++) - m_DigitInfo[i].modified = true; + for (int i = m_DigStart; i < m_NumDigits; i++) + m_DigitInfo[i].modified = true; - updateCtrl(true); + updateCtrl(true); } void CFreqCtrl::setUnitsColor(QColor col) { - m_UpdateAll = true; - m_UnitsColor = col; - updateCtrl(true); + m_UpdateAll = true; + m_UnitsColor = col; + updateCtrl(true); } void CFreqCtrl::setHighlightColor(QColor col) { - m_UpdateAll = true; - m_HighlightColor = col; - updateCtrl(true); + m_UpdateAll = true; + m_HighlightColor = col; + updateCtrl(true); } void CFreqCtrl::updateCtrl(bool all) { - if (all) - { - m_UpdateAll = true; - for (int i = m_DigStart; i < m_NumDigits; i++) - m_DigitInfo[i].modified = true; - } - update(); + if (all) + { + m_UpdateAll = true; + for (int i = m_DigStart; i < m_NumDigits; i++) + m_DigitInfo[i].modified = true; + } + update(); } void CFreqCtrl::resizeEvent(QResizeEvent *) { // qDebug() <= 0) - { - if (m_DigitInfo[m_ActiveEditDigit].editmode) - { - m_DigitInfo[m_ActiveEditDigit].editmode = false; - m_DigitInfo[m_ActiveEditDigit].modified = true; - m_ActiveEditDigit = -1; - updateCtrl(false); - } - } + // called when mouse cursor leaves this control so deactivate any highlights + if (m_ActiveEditDigit >= 0) + { + if (m_DigitInfo[m_ActiveEditDigit].editmode) + { + m_DigitInfo[m_ActiveEditDigit].editmode = false; + m_DigitInfo[m_ActiveEditDigit].modified = true; + m_ActiveEditDigit = -1; + updateCtrl(false); + } + } } void CFreqCtrl::paintEvent(QPaintEvent *) { - QPainter painter(&m_Pixmap); + QPainter painter(&m_Pixmap); - if (m_UpdateAll) // if need to redraw everything - { - drawBkGround(painter); - m_UpdateAll = false; - } - // draw any modified digits to the m_MemDC - drawDigits(painter); - // now draw pixmap onto screen - QPainter scrnpainter(this); - scrnpainter.drawPixmap(0, 0, m_Pixmap); // blt to the screen(flickers like a candle, why?) + if (m_UpdateAll) // if need to redraw everything + { + drawBkGround(painter); + m_UpdateAll = false; + } + // draw any modified digits to the m_MemDC + drawDigits(painter); + // now draw pixmap onto screen + QPainter scrnpainter(this); + scrnpainter.drawPixmap(0, 0, m_Pixmap); // blt to the screen(flickers like a candle, why?) } void CFreqCtrl::mouseMoveEvent(QMouseEvent *event) { - QPoint pt = event->pos(); - // find which digit is to be edited - if (isActiveWindow()) - { - if (!hasFocus()) - setFocus(Qt::MouseFocusReason); - - for (int i = m_DigStart; i < m_NumDigits; i++) - { - if (inRect(m_DigitInfo[i].dQRect, pt)) - { - if (!m_DigitInfo[i].editmode) - { - m_DigitInfo[i].editmode = true; - m_ActiveEditDigit = i; - } - } - else - { - // un-highlight the previous digit if moved off it - if (m_DigitInfo[i].editmode) - { - m_DigitInfo[i].editmode = false; - m_DigitInfo[i].modified = true; - } - } - } - - updateCtrl(false); - } + QPoint pt = event->pos(); + // find which digit is to be edited + if (isActiveWindow()) + { + if (!hasFocus()) + setFocus(Qt::MouseFocusReason); + + for (int i = m_DigStart; i < m_NumDigits; i++) + { + if (inRect(m_DigitInfo[i].dQRect, pt)) + { + if (!m_DigitInfo[i].editmode) + { + m_DigitInfo[i].editmode = true; + m_ActiveEditDigit = i; + } + } + else + { + // un-highlight the previous digit if moved off it + if (m_DigitInfo[i].editmode) + { + m_DigitInfo[i].editmode = false; + m_DigitInfo[i].modified = true; + } + } + } + + updateCtrl(false); + } } void CFreqCtrl::mousePressEvent(QMouseEvent *event) { - QPoint pt = event->pos(); - if (event->button() == Qt::LeftButton) - { - for (int i = m_DigStart; i < m_NumDigits; i++) - { - if (inRect(m_DigitInfo[i].dQRect, pt)) // if in i'th digit - { - if (m_LRMouseFreqSel) - { - incFreq(); - } - else - { - if (pt.y() < m_DigitInfo[i].dQRect.bottom() / 2) // top half? - incFreq(); - else - decFreq(); // bottom half - } - } - } - } - else if (event->button() == Qt::RightButton) - { - for (int i = m_DigStart; i < m_NumDigits; i++) - { - if (inRect(m_DigitInfo[i].dQRect, pt)) // if in i'th digit - { - if (m_LRMouseFreqSel) - { - decFreq(); - } - else - { - clearFreq(); - } - } - } - } + QPoint pt = event->pos(); + if (event->button() == Qt::LeftButton) + { + for (int i = m_DigStart; i < m_NumDigits; i++) + { + if (inRect(m_DigitInfo[i].dQRect, pt)) // if in i'th digit + { + if (m_LRMouseFreqSel) + { + incFreq(); + } + else + { + if (pt.y() < m_DigitInfo[i].dQRect.bottom() / 2) // top half? + incFreq(); + else + decFreq(); // bottom half + } + } + } + } + else if (event->button() == Qt::RightButton) + { + for (int i = m_DigStart; i < m_NumDigits; i++) + { + if (inRect(m_DigitInfo[i].dQRect, pt)) // if in i'th digit + { + if (m_LRMouseFreqSel) + { + decFreq(); + } + else + { + clearFreq(); + } + } + } + } } void CFreqCtrl::wheelEvent(QWheelEvent *event) { - QPoint pt = event->pos(); - int numDegrees = event->delta() / 8; - int numSteps = numDegrees / 15; + QPointF pt = event->position(); + int numDegrees = (event->angleDelta().x() +event->angleDelta().y() ) / 8; + int numSteps = numDegrees / 15; - for (int i = m_DigStart; i < m_NumDigits; i++) - { - if (inRect(m_DigitInfo[i].dQRect, pt)) // if in i'th digit - { - if (numSteps > 0) - incFreq(); - else if (numSteps < 0) - decFreq(); - } - } + for (int i = m_DigStart; i < m_NumDigits; i++) + { + if (inRect(m_DigitInfo[i].dQRect, pt)) // if in i'th digit + { + if (numSteps > 0) + incFreq(); + else if (numSteps < 0) + decFreq(); + } + } } void CFreqCtrl::keyPressEvent(QKeyEvent *event) { - // call base class if dont over ride key - bool fSkipMsg = false; - qint64 tmp; - - // qDebug() <key(); - - switch (event->key()) - { - case Qt::Key_0: - case Qt::Key_1: - case Qt::Key_2: - case Qt::Key_3: - case Qt::Key_4: - case Qt::Key_5: - case Qt::Key_6: - case Qt::Key_7: - case Qt::Key_8: - case Qt::Key_9: - if (m_ActiveEditDigit >= 0) - { - if (m_DigitInfo[m_ActiveEditDigit].editmode) - { - tmp = (m_freq / m_DigitInfo[m_ActiveEditDigit].weight) % 10; - m_freq -= tmp * m_DigitInfo[m_ActiveEditDigit].weight; - m_freq = m_freq + (event->key() - '0') * - m_DigitInfo[m_ActiveEditDigit].weight; - setFrequency(m_freq); - } - } - moveCursorRight(); - fSkipMsg = true; - break; - case Qt::Key_Backspace: - case Qt::Key_Left: - if (m_ActiveEditDigit != -1) - { - moveCursorLeft(); - fSkipMsg = true; - } - break; - case Qt::Key_Up: - if (m_ActiveEditDigit != -1) - { - incFreq(); - fSkipMsg = true; - } - break; - case Qt::Key_Down: - if (m_ActiveEditDigit != -1) - { - decFreq(); - fSkipMsg = true; - } - break; - case Qt::Key_Right: - if (m_ActiveEditDigit != -1) - { - moveCursorRight(); - fSkipMsg = true; - } - break; - case Qt::Key_Home: - cursorHome(); - fSkipMsg = true; - break; - case Qt::Key_End: - cursorEnd(); - fSkipMsg = true; - break; - default: - break; - } - if (!fSkipMsg) - QFrame::keyPressEvent(event); + // call base class if dont over ride key + bool fSkipMsg = false; + qint64 tmp; + + // qDebug() <key(); + + switch (event->key()) + { + case Qt::Key_0: + case Qt::Key_1: + case Qt::Key_2: + case Qt::Key_3: + case Qt::Key_4: + case Qt::Key_5: + case Qt::Key_6: + case Qt::Key_7: + case Qt::Key_8: + case Qt::Key_9: + if (m_ActiveEditDigit >= 0) + { + if (m_DigitInfo[m_ActiveEditDigit].editmode) + { + tmp = (m_freq / m_DigitInfo[m_ActiveEditDigit].weight) % 10; + m_freq -= tmp * m_DigitInfo[m_ActiveEditDigit].weight; + m_freq = m_freq + (event->key() - '0') * + m_DigitInfo[m_ActiveEditDigit].weight; + setFrequency(m_freq); + } + } + moveCursorRight(); + fSkipMsg = true; + break; + case Qt::Key_Backspace: + case Qt::Key_Left: + if (m_ActiveEditDigit != -1) + { + moveCursorLeft(); + fSkipMsg = true; + } + break; + case Qt::Key_Up: + if (m_ActiveEditDigit != -1) + { + incFreq(); + fSkipMsg = true; + } + break; + case Qt::Key_Down: + if (m_ActiveEditDigit != -1) + { + decFreq(); + fSkipMsg = true; + } + break; + case Qt::Key_Right: + if (m_ActiveEditDigit != -1) + { + moveCursorRight(); + fSkipMsg = true; + } + break; + case Qt::Key_Home: + cursorHome(); + fSkipMsg = true; + break; + case Qt::Key_End: + cursorEnd(); + fSkipMsg = true; + break; + default: + break; + } + if (!fSkipMsg) + QFrame::keyPressEvent(event); } void CFreqCtrl::drawBkGround(QPainter &Painter) { - QRect rect(0, 0, width(), height()); - - // qDebug() < m_DigStart) && ((i % 3) == 0)) - { - m_SepRect[i].setCoords(digpos - sepwidth, - rect.top(), - digpos, - rect.bottom()); - Painter.fillRect(m_SepRect[i], m_BkColor); - digpos -= sepwidth; - if (m_Unit == FCTL_UNIT_NONE) - { - if (m_LeadZeroPos > i) - dgsep = '.'; - else - dgsep = ' '; - } - else - { - if (i == m_DecPos) - dgsep = '.'; - else - dgsep = ' '; - } - Painter.drawText(m_SepRect[i], Qt::AlignHCenter | Qt::AlignVCenter, - QChar(dgsep)); - } - else - { - m_SepRect[i].setCoords(0, 0, 0, 0); - } - m_DigitInfo[i].dQRect.setCoords(digpos - cellwidth, rect.top(), - digpos, rect.bottom()); - digpos -= cellwidth; - } + QRect rect(0, 0, width(), height()); + + // qDebug() < m_DigStart) && ((i % 3) == 0)) + { + m_SepRect[i].setCoords(digpos - sepwidth, + rect.top(), + digpos, + rect.bottom()); + Painter.fillRect(m_SepRect[i], m_BkColor); + digpos -= sepwidth; + if (m_Unit == FCTL_UNIT_NONE) + { + if (m_LeadZeroPos > i) + dgsep = '.'; + else + dgsep = ' '; + } + else + { + if (i == m_DecPos) + dgsep = '.'; + else + dgsep = ' '; + } + Painter.drawText(m_SepRect[i], Qt::AlignHCenter | Qt::AlignVCenter, + QChar(dgsep)); + } + else + { + m_SepRect[i].setCoords(0, 0, 0, 0); + } + m_DigitInfo[i].dQRect.setCoords(digpos - cellwidth, rect.top(), + digpos, rect.bottom()); + digpos -= cellwidth; + } } // Draws just the Digits that have been modified void CFreqCtrl::drawDigits(QPainter &Painter) { - Painter.setFont(m_DigitFont); - m_FirstEditableDigit = m_DigStart; - - for (int i = m_DigStart; i < m_NumDigits; i++) - { - if (m_DigitInfo[i].incval == 0) - m_FirstEditableDigit++; - - if (m_DigitInfo[i].modified || m_DigitInfo[i].editmode) - { - if (m_DigitInfo[i].editmode && m_DigitInfo[i].incval != 0) - Painter.fillRect(m_DigitInfo[i].dQRect, m_HighlightColor); - else - Painter.fillRect(m_DigitInfo[i].dQRect, m_BkColor); - - if (i >= m_LeadZeroPos) - Painter.setPen(m_InactiveColor); - else - Painter.setPen(m_DigitColor); - - if (m_freq < 0 && i == m_LeadZeroPos - 1 && m_DigitInfo[i].val == 0) - Painter.drawText(m_DigitInfo[i].dQRect, - Qt::AlignHCenter | Qt::AlignVCenter, - QString("-0")); - else - Painter.drawText(m_DigitInfo[i].dQRect, - Qt::AlignHCenter | Qt::AlignVCenter, - QString().number(m_DigitInfo[i].val)); - m_DigitInfo[i].modified = false; - } - } + Painter.setFont(m_DigitFont); + m_FirstEditableDigit = m_DigStart; + + for (int i = m_DigStart; i < m_NumDigits; i++) + { + if (m_DigitInfo[i].incval == 0) + m_FirstEditableDigit++; + + if (m_DigitInfo[i].modified || m_DigitInfo[i].editmode) + { + if (m_DigitInfo[i].editmode && m_DigitInfo[i].incval != 0) + Painter.fillRect(m_DigitInfo[i].dQRect, m_HighlightColor); + else + Painter.fillRect(m_DigitInfo[i].dQRect, m_BkColor); + + if (i >= m_LeadZeroPos) + Painter.setPen(m_InactiveColor); + else + Painter.setPen(m_DigitColor); + + if (m_freq < 0 && i == m_LeadZeroPos - 1 && m_DigitInfo[i].val == 0) + Painter.drawText(m_DigitInfo[i].dQRect, + Qt::AlignHCenter | Qt::AlignVCenter, + QString("-0")); + else + Painter.drawText(m_DigitInfo[i].dQRect, + Qt::AlignHCenter | Qt::AlignVCenter, + QString().number(m_DigitInfo[i].val)); + m_DigitInfo[i].modified = false; + } + } } // Increment just the digit active in edit mode void CFreqCtrl::incDigit() { - /** FIXME: no longer used? */ - int tmp; - qint64 tmpl; - - if (m_ActiveEditDigit >= 0) - { - if (m_DigitInfo[m_ActiveEditDigit].editmode) - { - if (m_DigitInfo[m_ActiveEditDigit].weight == - m_DigitInfo[m_ActiveEditDigit].incval) - { - // get the current digit value - tmp = - (int)((m_freq / m_DigitInfo[m_ActiveEditDigit].weight) % - 10); - // set the current digit value to zero - m_freq -= tmp * m_DigitInfo[m_ActiveEditDigit].weight; - tmp++; - if (tmp > 9) - tmp = 0; - m_freq = m_freq + (qint64)tmp * - m_DigitInfo[m_ActiveEditDigit].weight; - } - else - { - tmp = - (int)((m_freq / m_DigitInfo[m_ActiveEditDigit + 1].weight) % - 10); - tmpl = m_freq + m_DigitInfo[m_ActiveEditDigit].incval; - if (tmp != - (int)((tmpl / m_DigitInfo[m_ActiveEditDigit + 1].weight) % - 10)) - { - tmpl -= m_DigitInfo[m_ActiveEditDigit + 1].weight; - } - m_freq = tmpl; - } - setFrequency(m_freq); - } - } + /** FIXME: no longer used? */ + int tmp; + qint64 tmpl; + + if (m_ActiveEditDigit >= 0) + { + if (m_DigitInfo[m_ActiveEditDigit].editmode) + { + if (m_DigitInfo[m_ActiveEditDigit].weight == + m_DigitInfo[m_ActiveEditDigit].incval) + { + // get the current digit value + tmp = + (int)((m_freq / m_DigitInfo[m_ActiveEditDigit].weight) % + 10); + // set the current digit value to zero + m_freq -= tmp * m_DigitInfo[m_ActiveEditDigit].weight; + tmp++; + if (tmp > 9) + tmp = 0; + m_freq = m_freq + (qint64)tmp * + m_DigitInfo[m_ActiveEditDigit].weight; + } + else + { + tmp = + (int)((m_freq / m_DigitInfo[m_ActiveEditDigit + 1].weight) % + 10); + tmpl = m_freq + m_DigitInfo[m_ActiveEditDigit].incval; + if (tmp != + (int)((tmpl / m_DigitInfo[m_ActiveEditDigit + 1].weight) % + 10)) + { + tmpl -= m_DigitInfo[m_ActiveEditDigit + 1].weight; + } + m_freq = tmpl; + } + setFrequency(m_freq); + } + } } // Increment the frequency by this digit active in edit mode void CFreqCtrl::incFreq() { - if (m_ActiveEditDigit >= 0) - { - if (m_DigitInfo[m_ActiveEditDigit].editmode) - { - m_freq += m_DigitInfo[m_ActiveEditDigit].incval; - if (m_ResetLowerDigits) - { - /* Set digits below the active one to 0 */ - m_freq = m_freq - m_freq % - m_DigitInfo[m_ActiveEditDigit].weight; - } - setFrequency(m_freq); - m_LastEditDigit = m_ActiveEditDigit; - } - } + if (m_ActiveEditDigit >= 0) + { + if (m_DigitInfo[m_ActiveEditDigit].editmode) + { + m_freq += m_DigitInfo[m_ActiveEditDigit].incval; + if (m_ResetLowerDigits) + { + /* Set digits below the active one to 0 */ + m_freq = m_freq - m_freq % + m_DigitInfo[m_ActiveEditDigit].weight; + } + setFrequency(m_freq); + m_LastEditDigit = m_ActiveEditDigit; + } + } } // Decrement the digit active in edit mode void CFreqCtrl::decDigit() { - /** FIXME: no longer used? */ - int tmp; - qint64 tmpl; - - if (m_ActiveEditDigit >= 0) - { - if (m_DigitInfo[m_ActiveEditDigit].editmode) - { - if (m_DigitInfo[m_ActiveEditDigit].weight == - m_DigitInfo[m_ActiveEditDigit].incval) - { - // get the current digit value - tmp = - (int)((m_freq / m_DigitInfo[m_ActiveEditDigit].weight) % - 10); - // set the current digit value to zero - m_freq -= tmp * m_DigitInfo[m_ActiveEditDigit].weight; - tmp--; - if (tmp < 0) - tmp = 9; - m_freq = m_freq + (qint64)tmp * - m_DigitInfo[m_ActiveEditDigit].weight; - } - else - { - tmp = - (int)((m_freq / m_DigitInfo[m_ActiveEditDigit + 1].weight) % - 10); - tmpl = m_freq - m_DigitInfo[m_ActiveEditDigit].incval; - if (tmp != - (int)((tmpl / m_DigitInfo[m_ActiveEditDigit + 1].weight) % - 10)) - { - tmpl += m_DigitInfo[m_ActiveEditDigit + 1].weight; - } - m_freq = tmpl; - } - setFrequency(m_freq); - } - } + /** FIXME: no longer used? */ + int tmp; + qint64 tmpl; + + if (m_ActiveEditDigit >= 0) + { + if (m_DigitInfo[m_ActiveEditDigit].editmode) + { + if (m_DigitInfo[m_ActiveEditDigit].weight == + m_DigitInfo[m_ActiveEditDigit].incval) + { + // get the current digit value + tmp = + (int)((m_freq / m_DigitInfo[m_ActiveEditDigit].weight) % + 10); + // set the current digit value to zero + m_freq -= tmp * m_DigitInfo[m_ActiveEditDigit].weight; + tmp--; + if (tmp < 0) + tmp = 9; + m_freq = m_freq + (qint64)tmp * + m_DigitInfo[m_ActiveEditDigit].weight; + } + else + { + tmp = + (int)((m_freq / m_DigitInfo[m_ActiveEditDigit + 1].weight) % + 10); + tmpl = m_freq - m_DigitInfo[m_ActiveEditDigit].incval; + if (tmp != + (int)((tmpl / m_DigitInfo[m_ActiveEditDigit + 1].weight) % + 10)) + { + tmpl += m_DigitInfo[m_ActiveEditDigit + 1].weight; + } + m_freq = tmpl; + } + setFrequency(m_freq); + } + } } // Decrement the frequency by this digit active in edit mode void CFreqCtrl::decFreq() { - if (m_ActiveEditDigit >= 0) - { - if (m_DigitInfo[m_ActiveEditDigit].editmode) - { - m_freq -= m_DigitInfo[m_ActiveEditDigit].incval; - if (m_ResetLowerDigits) - { - /* digits below the active one are reset to 0 */ - m_freq = m_freq - m_freq % - m_DigitInfo[m_ActiveEditDigit].weight; - } + if (m_ActiveEditDigit >= 0) + { + if (m_DigitInfo[m_ActiveEditDigit].editmode) + { + m_freq -= m_DigitInfo[m_ActiveEditDigit].incval; + if (m_ResetLowerDigits) + { + /* digits below the active one are reset to 0 */ + m_freq = m_freq - m_freq % + m_DigitInfo[m_ActiveEditDigit].weight; + } - setFrequency(m_freq); - m_LastEditDigit = m_ActiveEditDigit; - } - } + setFrequency(m_freq); + m_LastEditDigit = m_ActiveEditDigit; + } + } } // Clear the selected digit and the digits below (i.e. set them to 0) void CFreqCtrl::clearFreq() { - if (m_ActiveEditDigit >= 0) - { - if (m_DigitInfo[m_ActiveEditDigit].editmode) - { - m_freq -= m_DigitInfo[m_ActiveEditDigit].val * - m_DigitInfo[m_ActiveEditDigit].incval; + if (m_ActiveEditDigit >= 0) + { + if (m_DigitInfo[m_ActiveEditDigit].editmode) + { + m_freq -= m_DigitInfo[m_ActiveEditDigit].val * + m_DigitInfo[m_ActiveEditDigit].incval; - /* digits below the active one are reset to 0 */ - m_freq -= m_freq % m_DigitInfo[m_ActiveEditDigit].weight; + /* digits below the active one are reset to 0 */ + m_freq -= m_freq % m_DigitInfo[m_ActiveEditDigit].weight; - setFrequency(m_freq); - m_LastEditDigit = m_ActiveEditDigit; - } - } + setFrequency(m_freq); + m_LastEditDigit = m_ActiveEditDigit; + } + } } // Cursor move routines for arrow key editing void CFreqCtrl::moveCursorLeft() { - if ((m_ActiveEditDigit >= 0) && (m_ActiveEditDigit < m_NumDigits - 1)) - { - cursor().setPos(mapToGlobal(m_DigitInfo[++m_ActiveEditDigit].dQRect. - center())); - } + if ((m_ActiveEditDigit >= 0) && (m_ActiveEditDigit < m_NumDigits - 1)) + { + cursor().setPos(mapToGlobal(m_DigitInfo[++m_ActiveEditDigit].dQRect. + center())); + } } void CFreqCtrl::moveCursorRight() { - if (m_ActiveEditDigit > m_FirstEditableDigit) - { - cursor().setPos(mapToGlobal(m_DigitInfo[--m_ActiveEditDigit].dQRect. - center())); - } + if (m_ActiveEditDigit > m_FirstEditableDigit) + { + cursor().setPos(mapToGlobal(m_DigitInfo[--m_ActiveEditDigit].dQRect. + center())); + } } void CFreqCtrl::cursorHome() { - if (m_ActiveEditDigit >= 0) - { - cursor().setPos(mapToGlobal( - m_DigitInfo[m_NumDigits - 1].dQRect.center())); - } + if (m_ActiveEditDigit >= 0) + { + cursor().setPos(mapToGlobal( + m_DigitInfo[m_NumDigits - 1].dQRect.center())); + } } void CFreqCtrl::cursorEnd() { - if (m_ActiveEditDigit > 0) - { - cursor().setPos(mapToGlobal(m_DigitInfo[m_FirstEditableDigit].dQRect. - center())); - } + if (m_ActiveEditDigit > 0) + { + cursor().setPos(mapToGlobal(m_DigitInfo[m_FirstEditableDigit].dQRect. + center())); + } } diff --git a/uhd_cpp/uhd_spectrum/qtgui/freqctrl.h b/uhd_cpp/uhd_spectrum/qtgui/freqctrl.h index 08143ab..98a4469 100644 --- a/uhd_cpp/uhd_spectrum/qtgui/freqctrl.h +++ b/uhd_cpp/uhd_spectrum/qtgui/freqctrl.h @@ -1,4 +1,4 @@ -/* +/* * Frequency controller widget (originally from CuteSDR) */ #pragma once @@ -8,15 +8,15 @@ #include enum FctlUnit { - FCTL_UNIT_NONE, // Freq displayed without unit: 14.236.000 - FCTL_UNIT_HZ, - FCTL_UNIT_KHZ, - FCTL_UNIT_MHZ, - FCTL_UNIT_GHZ, - FCTL_UNIT_SEC, - FCTL_UNIT_MSEC, - FCTL_UNIT_USEC, - FCTL_UNIT_NSEC + FCTL_UNIT_NONE, // Freq displayed without unit: 14.236.000 + FCTL_UNIT_HZ, + FCTL_UNIT_KHZ, + FCTL_UNIT_MHZ, + FCTL_UNIT_GHZ, + FCTL_UNIT_SEC, + FCTL_UNIT_MSEC, + FCTL_UNIT_USEC, + FCTL_UNIT_NSEC }; #define FCTL_MAX_DIGITS 12 @@ -24,112 +24,113 @@ enum FctlUnit { class CFreqCtrl : public QFrame { - Q_OBJECT + Q_OBJECT public: - explicit CFreqCtrl(QWidget *parent = 0); - ~CFreqCtrl(); - - QSize minimumSizeHint() const; - QSize sizeHint() const; - - // Use NumDigits=0 for auto - void setup(int NumDigits, qint64 Minf, qint64 Maxf, int MinStep, - FctlUnit unit); - void setUnit(FctlUnit unit); - void setDigitColor(QColor col); - void setBgColor(QColor col); - void setUnitsColor(QColor col); - void setHighlightColor(QColor col); - qint64 getFrequency() const - { - return m_freq; - } - - void setResetLowerDigits(bool reset) - { - m_ResetLowerDigits = reset; - } + explicit CFreqCtrl(QWidget *parent = 0); + ~CFreqCtrl(); + + QSize minimumSizeHint() const; + QSize sizeHint() const; + + // Use NumDigits=0 for auto + void setup(int NumDigits, qint64 Minf, qint64 Maxf, int MinStep, + FctlUnit unit); + void setUnit(FctlUnit unit); + void setDigitColor(QColor col); + void setBgColor(QColor col); + void setUnitsColor(QColor col); + void setHighlightColor(QColor col); + qint64 getFrequency() const + { + return m_freq; + } + + void setResetLowerDigits(bool reset) + { + m_ResetLowerDigits = reset; + } signals: - void newFrequency(qint64 freq); // emitted when frequency has changed + void newFrequency(qint64 freq); // emitted when frequency has changed public slots: - void setFrequency(qint64 freq); + void setFrequency(qint64 freq); protected: - void paintEvent(QPaintEvent *); - void resizeEvent(QResizeEvent *); - void mouseMoveEvent(QMouseEvent *); - void mousePressEvent(QMouseEvent *); - void wheelEvent(QWheelEvent *); - void leaveEvent(QEvent *); - void keyPressEvent(QKeyEvent *); + void paintEvent(QPaintEvent *); + void resizeEvent(QResizeEvent *); + void mouseMoveEvent(QMouseEvent *); + void mousePressEvent(QMouseEvent *); + void wheelEvent(QWheelEvent *); + void leaveEvent(QEvent *); + void keyPressEvent(QKeyEvent *); private: - void updateCtrl(bool all); - void drawBkGround(QPainter &Painter); - void drawDigits(QPainter &Painter); - void incDigit(); - void decDigit(); - void incFreq(); - void decFreq(); - void clearFreq(); - void cursorHome(); - void cursorEnd(); - void moveCursorLeft(); - void moveCursorRight(); - bool inRect(QRect &rect, QPoint &point); - - bool m_UpdateAll; - bool m_ExternalKeyActive; - bool m_LRMouseFreqSel; /* Use left/right mouse buttons. If FALSE click area determines up/down. */ - - bool m_ResetLowerDigits; /* If TRUE digits below the active one will be reset to 0 - * when the active digit is incremented or decremented. */ - - int m_FirstEditableDigit; - int m_LastLeadZeroPos; - int m_LeadZeroPos; - int m_NumDigits; - int m_NumDigitsForUnit; // number of digits allocated for unit (kHz, MHz, ...) - int m_DigStart; - int m_ActiveEditDigit; - int m_LastEditDigit; - int m_DecPos; - int m_NumSeps; - - qint64 m_MinStep; - qint64 m_freq; - qint64 m_Oldfreq; - qint64 m_MinFreq; - qint64 m_MaxFreq; - - QColor m_DigitColor; - QColor m_BkColor; - QColor m_InactiveColor; - QColor m_UnitsColor; - QColor m_HighlightColor; - - QPixmap m_Pixmap; - QSize m_Size; - FctlUnit m_Unit; - - QRect m_rectCtrl; // main control rectangle - QRect m_UnitsRect; // rectangle where Units text goes - QRect m_SepRect[FCTL_MAX_DIGITS]; // separation rectangles for commas, decimal point, etc. - - QString m_UnitString; - - QFont m_DigitFont; - QFont m_UnitsFont; - - struct DigStuct { - qint64 weight; // decimal weight of this digit - qint64 incval; // value this digit increments or decrements - QRect dQRect; // Digit bounding rectangle - int val; // value of this digit(0-9) - bool modified; // set if this digit has been modified - bool editmode; // set if this digit is selected for editing - } m_DigitInfo[FCTL_MAX_DIGITS]; + void updateCtrl(bool all); + void drawBkGround(QPainter &Painter); + void drawDigits(QPainter &Painter); + void incDigit(); + void decDigit(); + void incFreq(); + void decFreq(); + void clearFreq(); + void cursorHome(); + void cursorEnd(); + void moveCursorLeft(); + void moveCursorRight(); + bool inRect(QRect &rect, QPoint &point); + bool inRect(QRect &rect, QPointF &point); + + bool m_UpdateAll; + bool m_ExternalKeyActive; + bool m_LRMouseFreqSel; /* Use left/right mouse buttons. If FALSE click area determines up/down. */ + + bool m_ResetLowerDigits; /* If TRUE digits below the active one will be reset to 0 + * when the active digit is incremented or decremented. */ + + int m_FirstEditableDigit; + int m_LastLeadZeroPos; + int m_LeadZeroPos; + int m_NumDigits; + int m_NumDigitsForUnit; // number of digits allocated for unit (kHz, MHz, ...) + int m_DigStart; + int m_ActiveEditDigit; + int m_LastEditDigit; + int m_DecPos; + int m_NumSeps; + + qint64 m_MinStep; + qint64 m_freq; + qint64 m_Oldfreq; + qint64 m_MinFreq; + qint64 m_MaxFreq; + + QColor m_DigitColor; + QColor m_BkColor; + QColor m_InactiveColor; + QColor m_UnitsColor; + QColor m_HighlightColor; + + QPixmap m_Pixmap; + QSize m_Size; + FctlUnit m_Unit; + + QRect m_rectCtrl; // main control rectangle + QRect m_UnitsRect; // rectangle where Units text goes + QRect m_SepRect[FCTL_MAX_DIGITS]; // separation rectangles for commas, decimal point, etc. + + QString m_UnitString; + + QFont m_DigitFont; + QFont m_UnitsFont; + + struct DigStuct { + qint64 weight; // decimal weight of this digit + qint64 incval; // value this digit increments or decrements + QRect dQRect; // Digit bounding rectangle + int val; // value of this digit(0-9) + bool modified; // set if this digit has been modified + bool editmode; // set if this digit is selected for editing + } m_DigitInfo[FCTL_MAX_DIGITS]; }; diff --git a/uhd_cpp/uhd_spectrum/qtgui/plotter.cpp b/uhd_cpp/uhd_spectrum/qtgui/plotter.cpp index 3751b3f..7a795a4 100644 --- a/uhd_cpp/uhd_spectrum/qtgui/plotter.cpp +++ b/uhd_cpp/uhd_spectrum/qtgui/plotter.cpp @@ -404,13 +404,13 @@ void CPlotter::mouseMoveEvent(QMouseEvent* event) } else if (XAXIS == m_CursorCaptured) { - if (event->buttons() & (Qt::LeftButton | Qt::MidButton)) + if (event->buttons() & (Qt::LeftButton | Qt::MiddleButton)) { setCursor(QCursor(Qt::ClosedHandCursor)); // pan viewable range or move center frequency int delta_px = m_Xzero - pt.x(); qint64 delta_hz = delta_px * m_Span / m_OverlayPixmap.width(); - if (event->buttons() & Qt::MidButton) + if (event->buttons() & Qt::MiddleButton) { m_CenterFreq += delta_hz; m_DemodCenterFreq += delta_hz; @@ -709,7 +709,7 @@ void CPlotter::mousePressEvent(QMouseEvent * event) m_GrabPosition = 1; drawOverlay(); } - else if (event->buttons() == Qt::MidButton) + else if (event->buttons() == Qt::MiddleButton) { // set center freq m_CenterFreq = roundFreq(freqFromX(pt.x()), m_ClickResolution); @@ -827,10 +827,10 @@ void CPlotter::zoomOnXAxis(double level) // Called when a mouse wheel is turned void CPlotter::wheelEvent(QWheelEvent * event) { - QPoint pt = event->pos(); + QPointF pt = event->position(); pt.setX(pt.x() / pixRatio()+.5); pt.setY(pt.y() / pixRatio()+.5); - int numDegrees = event->delta() / 8; + int numDegrees = (event->angleDelta().x() + event->angleDelta().y()) / 8; int numSteps = numDegrees / 15; /** FIXME: Only used for direction **/ /** FIXME: zooming could use some optimisation **/ @@ -838,7 +838,7 @@ void CPlotter::wheelEvent(QWheelEvent * event) { // Vertical zoom. Wheel down: zoom out, wheel up: zoom in // During zoom we try to keep the point (dB or kHz) under the cursor fixed - double zoom_fac = event->delta() < 0 ? 1.1 : 0.9; + double zoom_fac = numDegrees < 0 ? 1.1 : 0.9; double ratio = (double)pt.y() / (double)m_OverlayPixmap.height(); double db_range = m_PandMaxdB - m_PandMindB; double y_range = (double)m_OverlayPixmap.height(); @@ -857,7 +857,7 @@ void CPlotter::wheelEvent(QWheelEvent * event) } else if (m_CursorCaptured == XAXIS) { - zoomStepX(event->delta() < 0 ? 1.1 : 0.9, pt.x()); + zoomStepX(numDegrees < 0 ? 1.1 : 0.9, pt.x()); } else if (event->modifiers() & Qt::ControlModifier) { @@ -1351,7 +1351,7 @@ void CPlotter::drawOverlay() QFontMetrics metrics(m_Font); QPainter painter(&m_OverlayPixmap); - painter.initFrom(this); + painter.begin(this); painter.setFont(m_Font); // solid background @@ -1362,7 +1362,7 @@ void CPlotter::drawOverlay() #define VER_MARGIN 5 // X and Y axis areas - m_YAxisWidth = metrics.width("XXXX") + 2 * HOR_MARGIN; + m_YAxisWidth = metrics.horizontalAdvance("XXXX") + 2 * HOR_MARGIN; m_XAxisYCenter = h - metrics.height()/2; int xAxisHeight = metrics.height() + 2 * VER_MARGIN; int xAxisTop = h - xAxisHeight; @@ -1384,7 +1384,7 @@ void CPlotter::drawOverlay() x = xFromFreq(bookmarks[i].frequency); #if defined(_WIN16) || defined(_WIN32) || defined(_WIN64) - int nameWidth = fm.width(bookmarks[i].name); + int nameWidth = fm.horizontalAdvance(bookmarks[i].name); #else int nameWidth = fm.boundingRect(bookmarks[i].name).width(); #endif @@ -1397,7 +1397,7 @@ void CPlotter::drawOverlay() level = 0; tagEnd[level] = x + nameWidth + slant - 1; - m_BookmarkTags.append(qMakePair(QRect(x, level * levelHeight, nameWidth + slant, fontHeight), bookmarks[i].frequency)); + m_BookmarkTags.append(qMakePair(QRect(x, level * levelHeight, nameWidth + slant, fontHeight), bookmarks[i].frequency)); QColor color = QColor(bookmarks[i].GetColor()); color.setAlpha(0x60); @@ -1437,7 +1437,7 @@ void CPlotter::drawOverlay() QString label; label.setNum(double((StartFreq + m_Span) / m_FreqUnits), 'f', m_FreqDigits); calcDivSize(StartFreq, StartFreq + m_Span, - qMin(w/(metrics.width(label) + metrics.width("O")), HORZ_DIVS_MAX), + qMin(w/(metrics.horizontalAdvance(label) + metrics.horizontalAdvance("O")), HORZ_DIVS_MAX), m_StartFreqAdj, m_FreqPerDiv, m_HorDivs); pixperdiv = (double)w * (double) m_FreqPerDiv / (double) m_Span; adjoffset = pixperdiv * double (m_StartFreqAdj - StartFreq) / (double) m_FreqPerDiv; @@ -1455,7 +1455,7 @@ void CPlotter::drawOverlay() painter.setPen(QColor(PLOTTER_TEXT_COLOR)); for (int i = 0; i <= m_HorDivs; i++) { - int tw = metrics.width(m_HDivText[i]); + int tw = metrics.horizontalAdvance(m_HDivText[i]); x = (int)((double)i*pixperdiv + adjoffset); if (x > m_YAxisWidth) { @@ -1494,7 +1494,7 @@ void CPlotter::drawOverlay() // draw amplitude values (y axis) int dB = m_PandMaxdB; - m_YAxisWidth = metrics.width("-120 "); + m_YAxisWidth = metrics.horizontalAdvance("-120 "); painter.setPen(QColor(PLOTTER_TEXT_COLOR)); for (int i = 0; i < m_VerDivs; i++) { diff --git a/uhd_cpp/uhd_spectrum/uhd_device_win32.cpp b/uhd_cpp/uhd_spectrum/uhd_device_win32.cpp index 30deb80..2607668 100644 --- a/uhd_cpp/uhd_spectrum/uhd_device_win32.cpp +++ b/uhd_cpp/uhd_spectrum/uhd_device_win32.cpp @@ -1,4 +1,4 @@ -#include "uhd_device_win32.h" +#include "uhd_device_win32.h" #include #include #include @@ -9,26 +9,26 @@ using std::shared_ptr; #define UHD_DO(X) \ {\ - uhd_error e = (X);\ - char buf_errs[512];\ - if (e) { snprintf(buf_errs,sizeof(buf_errs),"Error in %s line %d, NO %d.",__FILE__,__LINE__,e);\ - std::string ev = __FILE__;\ - ev += ":";\ - ev += buf_errs;\ - return_code = 1;\ - fputs(buf_errs,stderr);\ - }\ - }; + uhd_error e = (X);\ + char buf_errs[512];\ + if (e) { snprintf(buf_errs,sizeof(buf_errs),"Error in %s line %d, NO %d.",__FILE__,__LINE__,e);\ + std::string ev = __FILE__;\ + ev += ":";\ + ev += buf_errs;\ + return_code = 1;\ + fputs(buf_errs,stderr);\ + }\ + }; uhd_io_thread::uhd_io_thread (std::function runner,QObject * p) - :QThread(p) - ,m_runner(runner) + :QThread(p) + ,m_runner(runner) { } uhd_io_thread::uhd_io_thread (QObject * p) - :QThread(p) + :QThread(p) { @@ -36,126 +36,126 @@ uhd_io_thread::uhd_io_thread (QObject * p) void uhd_io_thread::run() { - if (m_runner) - m_runner(); + if (m_runner) + m_runner(); } uhd_device::uhd_device() - :stop_signal_called(false) - ,streaming(false) - ,rx_count(0) + :stop_signal_called(false) + ,streaming(false) + ,rx_count(0) { - m_buffer_tmFrag = new double [bufsz]; - m_buffer_tmSec = new long long [bufsz]; - m_buffer_iq_all = new short [bufsz * m_spb_size * 2]; - m_buffer_iq = new short * [bufsz]; - m_buf_iqsize = new size_t [bufsz]; - for (int i=0;im_dev_args = args; + this->m_dev_args = args; } std::string uhd_device::devArgs() const { - return m_dev_args; + return m_dev_args; } bool uhd_device::open_device() { - string ref = "internal"; - std::atomic finished(false); - std::thread thr([&]()->void{ - try{ - UHD_DO(uhd_usrp_make(&usrp, m_dev_args.c_str())); - } - catch(std::string er) - { - fputs(er.c_str(),stderr); - } - finished = true; - }); - - while (!finished) - { - QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); - QThread::msleep(20); - } - thr.join(); - - - if (!usrp) - return false; - try{ - uhd_usrp_set_clock_source(usrp,ref.c_str(),0); - } - catch(std::string er) - { - fputs(er.c_str(),stderr); - } - - //if (rx_op.subdev.size()) - // usrp->set_rx_subdev_spec(rx_op.subdev,multi_usrp::ALL_MBOARDS); - stop_signal_called = false; - return usrp?true:false; + string ref = "internal"; + std::atomic finished(false); + std::thread thr([&]()->void{ + try{ + UHD_DO(uhd_usrp_make(&usrp, m_dev_args.c_str())); + } + catch(std::string er) + { + fputs(er.c_str(),stderr); + } + finished = true; + }); + + while (!finished) + { + QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); + QThread::msleep(20); + } + thr.join(); + + + if (!usrp) + return false; + try{ + uhd_usrp_set_clock_source(usrp,ref.c_str(),0); + } + catch(std::string er) + { + fputs(er.c_str(),stderr); + } + + //if (rx_op.subdev.size()) + // usrp->set_rx_subdev_spec(rx_op.subdev,multi_usrp::ALL_MBOARDS); + stop_signal_called = false; + return usrp?true:false; } bool uhd_device::close_device() { - stop_signal_called = true; - while (streaming) - { - QThread::msleep(1000); - } - if (usrp) - uhd_usrp_free(&usrp); - usrp = 0; - return true; + stop_signal_called = true; + while (streaming) + { + QThread::msleep(1000); + } + if (usrp) + uhd_usrp_free(&usrp); + usrp = 0; + return true; } /*! @@ -163,173 +163,173 @@ bool uhd_device::close_device() */ void uhd_device::run_IO() { - //设置接收通道,母板号为0(multi-usrp支持级联) - //UHD_DO(uhd_usrp_set_rx_subdev_spec(usrp,0,0)); - - //创建流对象实例 - char rx_cpu_format[] = "sc16"; - char rx_otw_format[] = "sc16"; - char rx_args[] = ""; - const size_t rx_channel_count = 1; - //接收信号。MIMO时,可以指定0,1 - size_t rx_channel[] = {(size_t)m_channel}; - uhd_stream_args_t rx_stream_args = { - /*.cpu_format = */rx_cpu_format, - /*.otw_format = */rx_otw_format, - /*.args = */rx_args, - /*.channel_list = */rx_channel, - /*.n_channels = */rx_channel_count - }; - uhd_rx_streamer_handle rx_streamer = 0; - uhd_rx_metadata_handle rx_meta = 0; - - - rx_count = 0; - //接收线程 - auto thcall_rx = [&]()->void{ - // Create RX streamer - UHD_DO(uhd_rx_streamer_make(&rx_streamer)); - // Create RX metadata - UHD_DO(uhd_rx_metadata_make(&rx_meta)); - uhd_stream_cmd_t rx_stream_cmd = { - /*.stream_mode = */UHD_STREAM_MODE_START_CONTINUOUS, - /*.num_samps = */0, - /*.stream_now = */true, - /*.time_spec_full_secs = */0, - /*.time_spec_frac_secs = */0 - }; - streaming = true; - rx_stream_args.channel_list = rx_channel; - UHD_DO(uhd_usrp_get_rx_stream(usrp, &rx_stream_args, rx_streamer)); - // Set up buffer - UHD_DO(uhd_rx_streamer_max_num_samps(rx_streamer, &m_spb_size)); - fprintf(stderr, "Buffer size in samples: %zu\n", m_spb_size); - - try { - UHD_DO(uhd_rx_streamer_issue_stream_cmd(rx_streamer, &rx_stream_cmd)); - while (!stop_signal_called) - { - const int off = rx_count % bufsz; - UHD_DO(uhd_rx_streamer_recv(rx_streamer,(void **) &(m_buffer_iq[off]), m_spb_size, &rx_meta, 10, true, &m_buf_iqsize[off])); - ++rx_count; - //md_rx可以读取时戳 - UHD_DO(uhd_rx_metadata_time_spec(rx_meta,&m_buffer_tmSec[off],&m_buffer_tmFrag[off])); - uhd_rx_metadata_error_code_t error_code; - UHD_DO(uhd_rx_metadata_error_code(rx_meta, &error_code)); - - if(error_code != UHD_RX_METADATA_ERROR_CODE_NONE){ - fprintf(stderr, "Warning: Error code 0x%x was returned during streaming.\n", error_code); - uhd_usrp_last_error(usrp, error_string, 512); - fputs(error_string,stderr); - UHD_DO(uhd_rx_streamer_issue_stream_cmd(rx_streamer, &rx_stream_cmd)); - } - } - - } catch (...) { - stop_signal_called = true; - } - rx_stream_cmd.stream_now = false; - UHD_DO(uhd_rx_streamer_issue_stream_cmd(rx_streamer, &rx_stream_cmd)); - streaming = false; - }; - //启动线程 - uhd_io_thread * rx_thread = new uhd_io_thread(thcall_rx,0); - rx_thread->start(QThread::TimeCriticalPriority); - //主线程不断打印状态 - while (!stop_signal_called) - { - //cerr<<"RX" << rx_count<<"\r"; - std::this_thread::sleep_for(std::chrono::milliseconds(400)); - } - if (rx_streamer) uhd_rx_streamer_free(&rx_streamer); - if (rx_meta) uhd_rx_metadata_free(&rx_meta); - //退出 - rx_thread->wait(); - rx_thread->deleteLater(); + //设置接收通道,母板号为0(multi-usrp支持级联) + //UHD_DO(uhd_usrp_set_rx_subdev_spec(usrp,0,0)); + + //创建流对象实例 + char rx_cpu_format[] = "sc16"; + char rx_otw_format[] = "sc16"; + char rx_args[] = ""; + const size_t rx_channel_count = 1; + //接收信号。MIMO时,可以指定0,1 + size_t rx_channel[] = {(size_t)m_channel}; + uhd_stream_args_t rx_stream_args = { + /*.cpu_format = */rx_cpu_format, + /*.otw_format = */rx_otw_format, + /*.args = */rx_args, + /*.channel_list = */rx_channel, + /*.n_channels = */rx_channel_count + }; + uhd_rx_streamer_handle rx_streamer = 0; + uhd_rx_metadata_handle rx_meta = 0; + + + rx_count = 0; + //接收线程 + auto thcall_rx = [&]()->void{ + // Create RX streamer + UHD_DO(uhd_rx_streamer_make(&rx_streamer)); + // Create RX metadata + UHD_DO(uhd_rx_metadata_make(&rx_meta)); + uhd_stream_cmd_t rx_stream_cmd = { + /*.stream_mode = */UHD_STREAM_MODE_START_CONTINUOUS, + /*.num_samps = */0, + /*.stream_now = */true, + /*.time_spec_full_secs = */0, + /*.time_spec_frac_secs = */0 + }; + streaming = true; + rx_stream_args.channel_list = rx_channel; + UHD_DO(uhd_usrp_get_rx_stream(usrp, &rx_stream_args, rx_streamer)); + // Set up buffer + UHD_DO(uhd_rx_streamer_max_num_samps(rx_streamer, &m_spb_size)); + fprintf(stderr, "Buffer size in samples: %zu\n", m_spb_size); + + try { + UHD_DO(uhd_rx_streamer_issue_stream_cmd(rx_streamer, &rx_stream_cmd)); + while (!stop_signal_called) + { + const int off = rx_count % bufsz; + UHD_DO(uhd_rx_streamer_recv(rx_streamer,(void **) &(m_buffer_iq[off]), m_spb_size, &rx_meta, 10, true, &m_buf_iqsize[off])); + ++rx_count; + //md_rx可以读取时戳 + UHD_DO(uhd_rx_metadata_time_spec(rx_meta,&m_buffer_tmSec[off],&m_buffer_tmFrag[off])); + uhd_rx_metadata_error_code_t error_code; + UHD_DO(uhd_rx_metadata_error_code(rx_meta, &error_code)); + + if(error_code != UHD_RX_METADATA_ERROR_CODE_NONE){ + fprintf(stderr, "Warning: Error code 0x%x was returned during streaming.\n", error_code); + uhd_usrp_last_error(usrp, error_string, 512); + fputs(error_string,stderr); + UHD_DO(uhd_rx_streamer_issue_stream_cmd(rx_streamer, &rx_stream_cmd)); + } + } + + } catch (...) { + stop_signal_called = true; + } + rx_stream_cmd.stream_now = false; + UHD_DO(uhd_rx_streamer_issue_stream_cmd(rx_streamer, &rx_stream_cmd)); + streaming = false; + }; + //启动线程 + uhd_io_thread * rx_thread = new uhd_io_thread(thcall_rx,0); + rx_thread->start(QThread::TimeCriticalPriority); + //主线程不断打印状态 + while (!stop_signal_called) + { + //cerr<<"RX" << rx_count<<"\r"; + std::this_thread::sleep_for(std::chrono::milliseconds(400)); + } + if (rx_streamer) uhd_rx_streamer_free(&rx_streamer); + if (rx_meta) uhd_rx_metadata_free(&rx_meta); + //退出 + rx_thread->wait(); + rx_thread->deleteLater(); } quint64 uhd_device::set_center_freq(const quint64 freq_in_hz) { - if (!usrp) - return freq_in_hz; + if (!usrp) + return freq_in_hz; double srate = 0; UHD_DO(uhd_usrp_get_rx_rate(usrp, m_channel, &srate)); fprintf(stderr, "Setting RX frequency: %f MHz...\n", freq_in_hz/1e6); - uhd_tune_result_t rx_tune_result; - uhd_tune_request_t rx_tune_request = - { - /*.target_freq =*/ (double)freq_in_hz, - /*.rf_freq_policy =*/ UHD_TUNE_REQUEST_POLICY_AUTO, - /*.rf_freq =*/ 0, - /*.dsp_freq_policy = */UHD_TUNE_REQUEST_POLICY_MANUAL, - /*.dsp_freq =*/ srate/2, - /*.args =*/ 0 - }; - UHD_DO(uhd_usrp_set_rx_freq(usrp, &rx_tune_request, m_channel, &rx_tune_result)); - double freq = 0; - UHD_DO(uhd_usrp_get_rx_freq(usrp, m_channel, &freq)); - fprintf(stderr, "Actual RX frequency: %f MHz...\n", freq / 1e6); + uhd_tune_result_t rx_tune_result; + uhd_tune_request_t rx_tune_request = + { + /*.target_freq =*/ (double)freq_in_hz, + /*.rf_freq_policy =*/ UHD_TUNE_REQUEST_POLICY_AUTO, + /*.rf_freq =*/ 0, + /*.dsp_freq_policy = */UHD_TUNE_REQUEST_POLICY_AUTO, + /*.dsp_freq =*/ 0, + /*.args =*/ 0 + }; + UHD_DO(uhd_usrp_set_rx_freq(usrp, &rx_tune_request, m_channel, &rx_tune_result)); + double freq = 0; + UHD_DO(uhd_usrp_get_rx_freq(usrp, m_channel, &freq)); + fprintf(stderr, "Actual RX frequency: %f MHz...\n", freq / 1e6); return (quint64)(freq +.5); } double uhd_device::set_sample_rate(const double sprate) { - if (usrp) - { - double srate = sprate; - double bw = sprate; - if (bw >56e6) - bw = 56e6; - if (bw <200000) - bw = 200000; - fprintf(stderr, "Setting RX Rate: %f...\n", sprate); - UHD_DO(uhd_usrp_set_rx_rate(usrp, sprate, m_channel)); - - // See what rate actually is - UHD_DO(uhd_usrp_get_rx_rate(usrp, m_channel, &srate)); - fprintf(stderr, "Actual RX Rate: %f...\n", srate); - - fprintf(stderr, "Setting RX Bandwidth: %f MHz...\n", bw/1e6); - UHD_DO(uhd_usrp_set_rx_bandwidth(usrp, bw, m_channel)); - - //Band - UHD_DO(uhd_usrp_get_rx_bandwidth(usrp, m_channel, &bw)); - fprintf(stderr, "Actual RX Bandwidth: %f MHz...\n", bw / 1e6); - - return srate; - } - return sprate; + if (usrp) + { + double srate = sprate; + double bw = sprate; + if (bw >56e6) + bw = 56e6; + if (bw <200000) + bw = 200000; + fprintf(stderr, "Setting RX Rate: %f...\n", sprate); + UHD_DO(uhd_usrp_set_rx_rate(usrp, sprate, m_channel)); + + // See what rate actually is + UHD_DO(uhd_usrp_get_rx_rate(usrp, m_channel, &srate)); + fprintf(stderr, "Actual RX Rate: %f...\n", srate); + + fprintf(stderr, "Setting RX Bandwidth: %f MHz...\n", bw/1e6); + UHD_DO(uhd_usrp_set_rx_bandwidth(usrp, bw, m_channel)); + + //Band + UHD_DO(uhd_usrp_get_rx_bandwidth(usrp, m_channel, &bw)); + fprintf(stderr, "Actual RX Bandwidth: %f MHz...\n", bw / 1e6); + + return srate; + } + return sprate; } void uhd_device::set_rx_atn(const std::string & atn) { - if (usrp) - uhd_usrp_set_rx_antenna(usrp,atn.c_str(),m_channel); + if (usrp) + uhd_usrp_set_rx_antenna(usrp,atn.c_str(),m_channel); } double uhd_device::set_rx_gain(double gain) { - if (!usrp) - return gain; - // Set gain - fprintf(stderr, "Setting RX Gain: %f dB...\n",(float) gain); - UHD_DO(uhd_usrp_set_rx_gain(usrp, gain,m_channel,"")); - - // See what gain actually is - UHD_DO(uhd_usrp_get_rx_gain(usrp, m_channel,"", &gain)); - fprintf(stderr, "Actual RX Gain: %lf...\n", gain); - return gain; + if (!usrp) + return gain; + // Set gain + fprintf(stderr, "Setting RX Gain: %f dB...\n",(float) gain); + UHD_DO(uhd_usrp_set_rx_gain(usrp, gain,m_channel,"")); + + // See what gain actually is + UHD_DO(uhd_usrp_get_rx_gain(usrp, m_channel,"", &gain)); + fprintf(stderr, "Actual RX Gain: %lf...\n", gain); + return gain; } double uhd_device::set_rx_bandwidth(double bw) { - if (usrp) - { - fprintf(stderr, "Setting RX Bandwidth: %f MHz...\n", bw/1e6); - UHD_DO(uhd_usrp_set_rx_bandwidth(usrp, bw, m_channel)); - - //Band - UHD_DO(uhd_usrp_get_rx_bandwidth(usrp, m_channel, &bw)); - fprintf(stderr, "Actual RX Bandwidth: %f MHz...\n", bw / 1e6); - } - return bw; + if (usrp) + { + fprintf(stderr, "Setting RX Bandwidth: %f MHz...\n", bw/1e6); + UHD_DO(uhd_usrp_set_rx_bandwidth(usrp, bw, m_channel)); + + //Band + UHD_DO(uhd_usrp_get_rx_bandwidth(usrp, m_channel, &bw)); + fprintf(stderr, "Actual RX Bandwidth: %f MHz...\n", bw / 1e6); + } + return bw; } -- GitLab