diff options
Diffstat (limited to 'widgets/displaytext.cpp')
| -rw-r--r-- | widgets/displaytext.cpp | 411 |
1 files changed, 341 insertions, 70 deletions
diff --git a/widgets/displaytext.cpp b/widgets/displaytext.cpp index 5c8e4233..f4b4fcba 100644 --- a/widgets/displaytext.cpp +++ b/widgets/displaytext.cpp @@ -19,6 +19,8 @@ #include <QListIterator> #include <QRegularExpression> #include <QScrollBar> +#include <QToolButton> +#include <QResizeEvent> #include "Configuration.hpp" #include "Decoder/decodedtext.h" @@ -44,14 +46,29 @@ bool play_ITUZ = false; bool play_ITUZOB = false; bool muted = false; +namespace +{ + QRegularExpression const message_73_regexp {"^(73|RR73)$"}; + QRegularExpression const grid_regexp {"\\A(?![Rr]{2}73)[A-Ra-r]{2}[0-9]{2}([A-Xa-x]{2}){0,1}\\z"}; + // Trailing #? matches Q65's "copied last Tx" flag marker (e.g. "q3#"), + // appended after the usual AP-type/averaging-count qualifier. + QRegularExpression const ap_regexp {R"((?:\?\s)?(?:a[0-9]|q[0-9][0-9*]?#?)$)"}; +} + using SpecOp = Configuration::SpecialOperatingActivity; DisplayText::DisplayText(QWidget *parent) : QTextEdit(parent) , m_config {nullptr} , erase_action_ {new QAction {tr ("&Erase"), this}} + , pressed_button_ {Qt::NoButton} + , click_state_ {ClickState::None} , high_volume_ {false} + , sticky_scroll_enabled_ {false} + , following_live_activity_ {true} , modified_vertical_scrollbar_max_ {-1} + , return_to_live_button_ {new QToolButton {viewport ()}} + , live_scroll_position_ {0} { setReadOnly (true); setUndoRedoEnabled (false); @@ -70,6 +87,49 @@ DisplayText::DisplayText(QWidget *parent) delete menu; }); connect (erase_action_, &QAction::triggered, this, &DisplayText::erase); + + return_to_live_button_->setObjectName (QStringLiteral ("returnToLiveActivityButton")); + return_to_live_button_->setText (tr ("Return to live activity")); + return_to_live_button_->setToolButtonStyle (Qt::ToolButtonTextOnly); + return_to_live_button_->setAutoRaise (false); + return_to_live_button_->setFocusPolicy (Qt::StrongFocus); + return_to_live_button_->setCursor (Qt::ArrowCursor); + return_to_live_button_->setToolTip (tr ("Return to the live activity position")); + return_to_live_button_->setAccessibleDescription ( + tr ("Live activity is paused while earlier entries are visible.")); + return_to_live_button_->hide (); + connect (return_to_live_button_, &QToolButton::clicked, this, &DisplayText::scrollToLiveActivity); + connect (verticalScrollBar (), &QScrollBar::sliderMoved, this, &DisplayText::userScrolledTo); + connect (verticalScrollBar (), &QScrollBar::actionTriggered, this, [this] (int) { + userScrolledTo (verticalScrollBar ()->sliderPosition ()); + }); + connect (verticalScrollBar (), &QScrollBar::valueChanged, this, [this] (int) { + updateReturnToLiveButton (); + }); + connect (verticalScrollBar (), &QScrollBar::rangeChanged, this, &DisplayText::scrollRangeChanged); +} + +void DisplayText::set_configuration (Configuration const * configuration, bool high_volume) +{ + disconnect (vertical_scroll_connection_); + m_config = configuration; + high_volume_ = high_volume; + sticky_scroll_enabled_ = true; + following_live_activity_ = true; + live_scroll_position_ = verticalScrollBar ()->value (); + updateReturnToLiveButton (); +} + +void DisplayText::clear () +{ + if (click_state_ == ClickState::Captured) + { + click_state_ = ClickState::Canceled; + } + following_live_activity_ = true; + live_scroll_position_ = 0; + QTextEdit::clear (); + updateReturnToLiveButton (); } void DisplayText::erase () @@ -78,11 +138,95 @@ void DisplayText::erase () Q_EMIT erased (); } +bool DisplayText::decodesFromTop () const +{ + return high_volume_ && m_config && m_config->decodes_from_top (); +} + +void DisplayText::userScrolledTo (int position) +{ + if (sticky_scroll_enabled_) + { + following_live_activity_ = position == live_scroll_position_; + updateReturnToLiveButton (); + } +} + +void DisplayText::scrollRangeChanged (int minimum, int maximum) +{ + if (sticky_scroll_enabled_) + { + if (decodesFromTop ()) + { + live_scroll_position_ = qBound (minimum, live_scroll_position_, maximum); + } + else + { + live_scroll_position_ = maximum; + if (following_live_activity_) + { + verticalScrollBar ()->setValue (live_scroll_position_); + } + } + } + updateReturnToLiveButton (); +} + +void DisplayText::scrollToLiveActivity () +{ + auto * vertical_scroll_bar = verticalScrollBar (); + live_scroll_position_ = qBound (vertical_scroll_bar->minimum (), live_scroll_position_ + , vertical_scroll_bar->maximum ()); + following_live_activity_ = true; + vertical_scroll_bar->setValue (live_scroll_position_); + updateReturnToLiveButton (); +} + +void DisplayText::updateReturnToLiveButton () +{ + auto const * vertical_scroll_bar = verticalScrollBar (); + auto const hint = return_to_live_button_->sizeHint (); + auto const margin = 3; + auto const available_width = qMax (0, viewport ()->width () - 2 * margin); + auto const available_height = qMax (0, viewport ()->height () - 2 * margin); + auto const button_height = qMin (qMax (hint.height (), qMax (20, vertical_scroll_bar->sizeHint ().width ())) + , available_height); + auto const button_width = qMin (hint.width () + 8, available_width); + return_to_live_button_->resize (button_width, button_height); + return_to_live_button_->move (qMax (margin, viewport ()->width () - button_width - margin) + , qMax (margin, viewport ()->height () - button_height - margin)); + auto const wanted = sticky_scroll_enabled_ + && vertical_scroll_bar->maximum () > vertical_scroll_bar->minimum () + && !following_live_activity_ + && button_width > 0 + && button_height > 0; + if (wanted) + { + return_to_live_button_->raise (); + } + return_to_live_button_->setVisible (wanted); +} + +void DisplayText::resizeEvent (QResizeEvent * e) +{ + QTextEdit::resizeEvent (e); + updateReturnToLiveButton (); +} + void DisplayText::setContentFont(QFont const& font) { + auto const preserve_view = sticky_scroll_enabled_ && !following_live_activity_; + QTextCursor view_anchor; + int anchor_offset {0}; + if (preserve_view) + { + view_anchor = cursorForPosition (QPoint {1, 1}); + anchor_offset = cursorRect (view_anchor).top (); + } + char_font_ = font; - selectAll (); - auto cursor = textCursor (); + QTextCursor cursor {document ()}; + cursor.select (QTextCursor::Document); cursor.beginEditBlock (); auto char_format = cursor.charFormat (); char_format.setFont (char_font_); @@ -95,16 +239,66 @@ void DisplayText::setContentFont(QFont const& font) cursor.movePosition (QTextCursor::StartOfLine); cursor.endEditBlock (); - if (!high_volume_ || !m_config || !m_config->decodes_from_top ()) + auto * vertical_scroll_bar = verticalScrollBar (); + if (!sticky_scroll_enabled_ || following_live_activity_) { - setTextCursor (cursor); - ensureCursorVisible (); + if (!decodesFromTop ()) + { + setTextCursor (cursor); + ensureCursorVisible (); + } + if (sticky_scroll_enabled_) + { + live_scroll_position_ = vertical_scroll_bar->value (); + } + } + else + { + vertical_scroll_bar->setValue (vertical_scroll_bar->value () + + cursorRect (view_anchor).top () - anchor_offset); + if (!decodesFromTop ()) + { + live_scroll_position_ = vertical_scroll_bar->maximum (); + } } + updateReturnToLiveButton (); +} + +void DisplayText::captureClick (QMouseEvent const * event) +{ + auto cursor = cursorForPosition (event->pos ()); + pressed_line_ = cursor.block ().text (); + cursor.select (QTextCursor::WordUnderCursor); + pressed_word_ = cursor.selectedText (); + pressed_button_ = event->button (); + click_state_ = ClickState::Captured; } -void DisplayText::mouseDoubleClickEvent(QMouseEvent *e) +void DisplayText::mousePressEvent (QMouseEvent * event) { - Q_EMIT selectCallsign(e->modifiers ()); + captureClick (event); + QTextEdit::mousePressEvent (event); +} + +void DisplayText::mouseDoubleClickEvent (QMouseEvent * event) +{ + if (click_state_ == ClickState::Canceled) + { + pressed_button_ = Qt::NoButton; + click_state_ = ClickState::None; + return; + } + + if (click_state_ == ClickState::None || pressed_button_ != event->button ()) + { + captureClick (event); + } + + auto const line = pressed_line_; + auto const word = pressed_word_; + pressed_button_ = Qt::NoButton; + click_state_ = ClickState::None; + Q_EMIT selectCallsign (line, word, event->modifiers ()); } void DisplayText::insertLineSpacer(QString const& line) @@ -149,6 +343,15 @@ namespace void DisplayText::insertText(QString const& text, QColor bg, QColor fg , QString const& call1, QString const& call2, QTextCursor::MoveOperation location) { + auto const preserve_view = sticky_scroll_enabled_ && !following_live_activity_; + QTextCursor view_anchor; + int anchor_offset {0}; + if (preserve_view) + { + view_anchor = cursorForPosition (QPoint {1, 1}); + anchor_offset = cursorRect (view_anchor).top (); + } + auto cursor = textCursor (); cursor.movePosition (location); auto block_format = cursor.blockFormat (); @@ -225,12 +428,31 @@ void DisplayText::insertText(QString const& text, QColor bg, QColor fg // position so viewport scrolled to left cursor.movePosition (QTextCursor::StartOfLine); - if (!high_volume_ || !m_config || !m_config->decodes_from_top ()) + if ((!sticky_scroll_enabled_ || following_live_activity_) && !decodesFromTop ()) { setTextCursor (cursor); ensureCursorVisible (); } document ()->setMaximumBlockCount (document ()->maximumBlockCount ()); + auto * vertical_scroll_bar = verticalScrollBar (); + if (preserve_view) + { + vertical_scroll_bar->setValue (vertical_scroll_bar->value () + + cursorRect (view_anchor).top () - anchor_offset); + } + if (sticky_scroll_enabled_) + { + if (decodesFromTop ()) + { + live_scroll_position_ = qBound (vertical_scroll_bar->minimum (), live_scroll_position_ + , vertical_scroll_bar->maximum ()); + } + else + { + live_scroll_position_ = vertical_scroll_bar->maximum (); + } + } + updateReturnToLiveButton (); } void DisplayText::extend_vertical_scrollbar (int min, int max) @@ -252,6 +474,15 @@ void DisplayText::extend_vertical_scrollbar (int min, int max) void DisplayText::new_period () { + auto const preserve_view = sticky_scroll_enabled_ && !following_live_activity_; + QTextCursor view_anchor; + int anchor_offset {0}; + if (preserve_view) + { + view_anchor = cursorForPosition (QPoint {0, 0}); + anchor_offset = cursorRect (view_anchor).top (); + } + if (m_config->decodes_from_top ()) { document ()->setMaximumBlockCount (4800); document ()->setMaximumBlockCount (5000); @@ -275,13 +506,31 @@ void DisplayText::new_period () extend_vertical_scrollbar (min, max ); }); } - verticalScrollBar ()->setSliderPosition (verticalScrollBar ()->maximum ()); + auto * vertical_scroll_bar = verticalScrollBar (); + if (sticky_scroll_enabled_) + { + live_scroll_position_ = vertical_scroll_bar->maximum (); + } + if (!sticky_scroll_enabled_ || following_live_activity_) + { + vertical_scroll_bar->setSliderPosition (vertical_scroll_bar->maximum ()); + if (sticky_scroll_enabled_) + { + live_scroll_position_ = vertical_scroll_bar->value (); + } + } + else + { + vertical_scroll_bar->setValue (vertical_scroll_bar->value () + + cursorRect (view_anchor).top () - anchor_offset); + } + updateReturnToLiveButton (); } QString DisplayText::appendWorkedB4 (QString message, QString call, QString const& grid, QColor * bg, QColor * fg, LogBook const& logBook, QString const& currentBand, QString const& currentMode, - QString extra) + QString extra, QString const& state, bool entityMismatch) { QString countryName; bool callB4; @@ -413,30 +662,25 @@ QString DisplayText::appendWorkedB4 (QString message, QString call, QString cons { extra += looked_up.primary_prefix; } + else if (!state.isEmpty () || entityMismatch) + { + // A grid-derived U.S. state overrides the callsign's own DXCC + // entity name here: KL7YY operating from DM78 should be shown + // with "U.S.A." (matching the state that follows when GridMap() + // is on), not "Alaska" -- the entity his callsign happens to be + // licensed under, not where he's actually transmitting from. + // entityMismatch catches this even with GridMap() off (no state + // code follows, but the entity name itself still shouldn't lie); + // state alone catches it whenever GridMap() is on, including the + // case where the call genuinely is in its home entity (KL7UW + // really in Alaska still reads "U.S.A. AK", not "Alaska AK"). + // Worked-before/DXCC credit above is unaffected; this only + // changes what's shown. + extra += "U.S.A."; + } else { - auto countryName = looked_up.entity_name; - - // do some obvious abbreviations - countryName.replace ("Islands", "Is."); - countryName.replace ("Island", "Is."); - countryName.replace ("North ", "N. "); - countryName.replace ("Northern ", "N. "); - countryName.replace ("South ", "S. "); - countryName.replace ("East ", "E. "); - countryName.replace ("Eastern ", "E. "); - countryName.replace ("West ", "W. "); - countryName.replace ("Western ", "W. "); - countryName.replace ("Central ", "C. "); - countryName.replace (" and ", " & "); - countryName.replace ("Republic", "Rep."); - countryName.replace ("United States of America", "U.S.A."); - countryName.replace ("United States", "U.S.A."); - countryName.replace ("Fed. Rep. of ", ""); - countryName.replace ("French ", "Fr."); - countryName.replace ("Asiatic", "AS"); - countryName.replace ("European", "EU"); - countryName.replace ("African", "AF"); + auto countryName = looked_up.abbreviated_entity_name; // assign WAE entities to the correct DXCC when "Include extra WAE entities" is not selected if (!(m_config->include_WAE_entities())) { @@ -448,7 +692,6 @@ QString DisplayText::appendWorkedB4 (QString message, QString call, QString cons countryName.replace ("AF Turkey", "Turkey"); countryName.replace ("EU Turkey", "Turkey"); } - extra += countryName; } } @@ -483,7 +726,7 @@ QString DisplayText::leftJustifyAppendage (QString message, QString const& appen return message; } -void DisplayText::displayDecodedText(DecodedText const& decodedText, QString const& myCall, +bool DisplayText::displayDecodedText(DecodedText const& decodedText, QString const& myCall, QString const& mode, bool displayDXCCEntity, LogBook const& logBook, QString const& currentBand, bool ppfx, bool bCQonly, @@ -497,7 +740,7 @@ void DisplayText::displayDecodedText(DecodedText const& decodedText, QString con QColor bg; QColor fg; bool CQcall = false; - auto is_73 = decodedText.messageWords().filter (QRegularExpression {"^(73|RR73)$"}).size(); + auto is_73 = decodedText.messageWords().filter (message_73_regexp).size(); if (decodedText.string ().contains (" CQ ")) { if (m_config->alert_CQ()) { if (!muted) play_CQ = true; @@ -512,24 +755,66 @@ void DisplayText::displayDecodedText(DecodedText const& decodedText, QString con } else { - if (bCQonly) return; + if (bCQonly) return false; } auto message = decodedText.string(); QString dxCall; QString dxGrid; decodedText.deCallAndGrid (/*out*/ dxCall, dxGrid); - QRegularExpression grid_regexp {"\\A(?![Rr]{2}73)[A-Ra-r]{2}[0-9]{2}([A-Xa-x]{2}){0,1}\\z"}; if(!dxGrid.contains(grid_regexp)) dxGrid=""; message = message.left (message.indexOf (QChar::Nbsp)).trimmed (); // strip appended info QString extra; - QString state; // NJ0A + QString state; // NJ0A -- populated only when GridMap() is enabled; drives + // both the entity-name override below and the trailing + // state code appended at the end of this function. - if (displayDXCCEntity && dxGrid.length() > 0 && logBook.countries ()->lookup (dxCall).primary_prefix == "K") { - //std::cout << dxCall << " -> " << dxGrid << " " << logBook.countries ()->lookup (dxCall).primary_prefix <<"\n"; - if (m_config->GridMap()) { - if (CQcall || is_73 || m_config->GridMapAll()) { - state = logBook.countries ()->findState(dxGrid); - } + // K (contiguous US), KL (Alaska), KH6 (Hawaii), KH0 (Mariana Is.), + // KH2 (Guam), KH8 (American Samoa), KP2 (US Virgin Is.), KP4 (Puerto + // Rico), and KG4 (Guantanamo Bay) are all separate DXCC entities that + // license under the same K/N/W/A callsign blocks (e.g. a WP3 call + // resolves to the KP4 entity via lookup() below, same as KP4 itself). + // cty.dat already exact-matches ordinary stateside KG4-suffix calls back + // to primary_prefix "K", so primary_prefix "KG4" here only ever means a + // genuine Guantanamo Bay call. Any of these can genuinely be transmitting + // from one of the 50 states (e.g. KL7YY portable from Colorado, or KP4 + // portable from Nebraska), so all are eligible for grid-based state + // mapping, not just plain "K" calls. The remaining K/N/W/A-administered + // entities (Baker & Howland, Palmyra & Jarvis, Wake, Navassa, Desecheo, + // Swains, plus the deleted Johnston and Midway) are intentionally + // omitted: they have no resident ham population, so they're never + // genuinely "home" to a callsign the way the entities above are -- + // activations are DXpeditions using calls issued for the trip. + static QStringList const us_family_prefixes { + "K", "KL", "KH6", "KH0", "KH2", "KH8", "KP2", "KP4", "KG4"}; + // KL and KH6 are the only entities in that set with their own recognized + // "state" code in grid.dat (Alaska and Hawaii are modelled there like + // states). If a KL/KH6 call's grid resolves to exactly that code, the + // call really is where its callsign says -- no entity-name mismatch, even + // with GridMap() off. The other entities (KH0/KH2/KH8/KP2/KP4) have no + // such code, since their home territories aren't in grid.dat at all, + // so any resolved state for one of those always indicates the operator + // is actually transmitting from the mainland. + static QHash<QString, QString> const home_state_for_entity { + {"KL", "AK"}, {"KH6", "HI"}}; + auto const& dxRecord = logBook.countries ()->lookup (dxCall); + bool const usFamilyEntity = us_family_prefixes.contains (dxRecord.primary_prefix); + + // entityMismatch answers "does this call's grid contradict its own DXCC + // entity" independently of GridMap()/GridMapAll(), so the entity-name + // choice below can be corrected (KL7YY operating from DM78 should not be + // labelled "Alaska") even when the operator hasn't enabled the separate + // "show the state code too" feature that state/GridMap() controls. + QString rawState; + bool entityMismatch = false; + if (dxGrid.length() > 0 && usFamilyEntity) { + rawState = logBook.countries ()->findState (dxGrid); + if (rawState == "**") rawState.clear (); + entityMismatch = !rawState.isEmpty () + && rawState != home_state_for_entity.value (dxRecord.primary_prefix); + } + if (dxGrid.length() > 0 && usFamilyEntity && m_config->GridMap()) { + if (CQcall || is_73 || m_config->GridMapAll()) { + state = rawState; } } //NJ0A @@ -538,7 +823,7 @@ void DisplayText::displayDecodedText(DecodedText const& decodedText, QString con { extra += QString {"%1"}.arg (fSpread, 5, 'f', fSpread < 0.95 ? 3 : 2) + QChar {' '}; } - auto ap_pos = message.lastIndexOf (QRegularExpression {R"((?:\?\s)?(?:a[0-9]|q[0-9][0-9*]?)$)"}); + auto ap_pos = message.lastIndexOf (ap_regexp); if (ap_pos >= 0) { extra += message.mid (ap_pos) + QChar {' '}; @@ -554,7 +839,7 @@ void DisplayText::displayDecodedText(DecodedText const& decodedText, QString con // preformated text line t1 auto currentMode = mode; message = appendWorkedB4 (message, dxCall, dxGrid, &bg, &fg - , logBook, currentBand, currentMode, extra); + , logBook, currentBand, currentMode, extra, state, entityMismatch); } else { @@ -572,32 +857,15 @@ void DisplayText::displayDecodedText(DecodedText const& decodedText, QString con if (m_config->show_country_names()) { auto const& looked_up = logBook.countries ()->lookup (dxCall); - auto countryName = looked_up.entity_name; + auto countryName = looked_up.abbreviated_entity_name; if (m_bPrincipalPrefix) { extra += looked_up.primary_prefix; + } else if (!state.isEmpty () || entityMismatch) { + // Same reasoning as the CQ-line case above: a grid-derived + // U.S. state overrides the callsign's own DXCC entity name. + extra += "U.S.A."; } else { - // do some obvious abbreviations - countryName.replace ("Islands", "Is."); - countryName.replace ("Island", "Is."); - countryName.replace ("North ", "N. "); - countryName.replace ("Northern ", "N. "); - countryName.replace ("South ", "S. "); - countryName.replace ("East ", "E. "); - countryName.replace ("Eastern ", "E. "); - countryName.replace ("West ", "W. "); - countryName.replace ("Western ", "W. "); - countryName.replace ("Central ", "C. "); - countryName.replace (" and ", " & "); - countryName.replace ("Republic", "Rep."); - countryName.replace ("United States of America", "U.S.A."); - countryName.replace ("United States", "U.S.A."); - countryName.replace ("Fed. Rep. of ", ""); - countryName.replace ("French ", "Fr."); - countryName.replace ("Asiatic", "AS"); - countryName.replace ("European", "EU"); - countryName.replace ("African", "AF"); - // assign WAE entities to the correct DXCC when "Include extra WAE entities" is not selected if (!(m_config->include_WAE_entities())) { countryName.replace ("Bear Is.", "Svalbard"); @@ -694,6 +962,7 @@ void DisplayText::displayDecodedText(DecodedText const& decodedText, QString con } insertText (message.trimmed (), bg, fg, decodedText.call (), dxCall); + return true; } void DisplayText::displayTransmittedText(QString text, QString modeTx, qint32 txFreq, @@ -918,7 +1187,7 @@ void DisplayText::AudioAlerts() #ifdef WIN32 if(m_config->alert_Enabled()) { QAudioOutput info(QAudioDeviceInfo::defaultOutputDevice()); - QString audioPath = app_sounds_directory (m_config->voicesPath()); + QString audioPath = m_config->voice_directory ().absolutePath () + QChar {'/'}; QAudioFormat format; format.setCodec("audio/pcm"); format.setSampleRate (48000); @@ -930,8 +1199,9 @@ void DisplayText::AudioAlerts() connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChanged(QAudio::State))); #else if(m_config->alert_Enabled()) { - QString audioPath = app_sounds_directory (m_config->voicesPath()); + QString audioPath = m_config->voice_directory ().absolutePath () + QChar {'/'}; #endif +#ifdef WIN32 QFile *effect2 = new QFile(this); QFile *effect3 = new QFile(this); QFile *effect4 = new QFile(this); @@ -956,6 +1226,7 @@ void DisplayText::AudioAlerts() effect11->setFileName(QString("%1/%2").arg(audioPath, "Grid.wav")); effect12->setFileName(QString("%1/%2").arg(audioPath, "GridOnBand.wav")); effect13->setFileName(QString("%1/%2").arg(audioPath, "CQ.wav")); +#endif static int startIndex = 0; int nextStartIndex = startIndex +1; switch (startIndex) { |
