From a314d29115cc013a9fff5048cc07e459cfad7e62 Mon Sep 17 00:00:00 2001 From: Jordan Sherer Date: Sat, 2 Feb 2019 17:06:01 -0500 Subject: [PATCH] Working through message window wireup --- mainwindow.cpp | 52 ++++++++++++++++++- messagewindow.cpp | 129 +++++++++++++++++++++++++++++++++++++++++++++- messagewindow.h | 19 ++++++- messagewindow.ui | 76 +++++++++++++++++++++++++-- 4 files changed, 266 insertions(+), 10 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index e5d531f..1827057 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1380,6 +1380,40 @@ MainWindow::MainWindow(QDir const& temp_directory, bool multiple, displayActivity(true); }); + auto historyAction = new QAction(QString("Message History..."), ui->tableWidgetCalls); + connect(historyAction, &QAction::triggered, this, [this](){ + QString selectedCall = callsignSelected(); + if(selectedCall.isEmpty()){ + return; + } + + Inbox inbox(inboxPath()); + if(!inbox.open()){ + return; + } + + QList msgs; + foreach(auto pair, inbox.values("UNREAD", "$.params.FROM", selectedCall, 0, 1000)){ + msgs.append(pair.second); + } + foreach(auto pair, inbox.values("READ", "$.params.FROM", selectedCall, 0, 1000)){ + msgs.append(pair.second); + } + + qStableSort(msgs.begin(), msgs.end(), [](Message const &a, Message const &b){ + return a.params().value("UTC") < b.params().value("UTC"); + }); + + auto mw = new MessageWindow(this); + connect(mw, &MessageWindow::replyMessage, this, [this](const QString &text){ + addMessageText(text, true, false); + toggleTx(true); + }); + mw->setCall(selectedCall); + mw->populateMessages(msgs); + mw->show(); + }); + ui->tableWidgetCalls->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->tableWidgetCalls->horizontalHeader(), &QHeaderView::customContextMenuRequested, this, [this](QPoint const &point){ QMenu * menu = new QMenu(ui->tableWidgetCalls); @@ -1390,13 +1424,14 @@ MainWindow::MainWindow(QDir const& temp_directory, bool multiple, }); ui->tableWidgetCalls->setContextMenuPolicy(Qt::CustomContextMenu); - connect(ui->tableWidgetCalls, &QTableWidget::customContextMenuRequested, this, [this, logAction, clearAction4, clearActionAll, addStation, removeStation](QPoint const &point){ + connect(ui->tableWidgetCalls, &QTableWidget::customContextMenuRequested, this, [this, logAction, historyAction, clearAction4, clearActionAll, addStation, removeStation](QPoint const &point){ QMenu * menu = new QMenu(ui->tableWidgetCalls); ui->tableWidgetRXAll->selectionModel()->clearSelection(); QString selectedCall = callsignSelected(); bool isAllCall = isAllCallIncluded(selectedCall); + bool isGroupCall = isGroupCallIncluded(selectedCall); bool missingCallsign = selectedCall.isEmpty(); if(!missingCallsign && !isAllCall){ @@ -1413,6 +1448,9 @@ MainWindow::MainWindow(QDir const& temp_directory, bool multiple, menu->addAction(logAction); logAction->setDisabled(missingCallsign || isAllCall); + menu->addAction(historyAction); + historyAction->setDisabled(missingCallsign || isAllCall || isGroupCall); + menu->addSeparator(); auto savedMenu = menu->addMenu("Saved messages..."); @@ -7832,7 +7870,9 @@ void MainWindow::on_tableWidgetCalls_cellDoubleClicked(int row, int col){ on_tableWidgetCalls_cellClicked(row, col); auto call = callsignSelected(); + addMessageText(call); +#if 0 if(m_rxInboxCountCache.value(call, 0) > 0){ // TODO: @@ -7843,6 +7883,15 @@ void MainWindow::on_tableWidgetCalls_cellDoubleClicked(int row, int col){ Inbox i(inboxPath()); if(i.open()){ + QList msgs; + foreach(auto pair, i.values("UNREAD", "$.params.FROM", call, 0, 1000)){ + msgs.append(pair.second); + } + + auto mw = new MessageWindow(this); + mw->populateMessages(msgs); + mw->show(); + auto pair = i.firstUnreadFrom(call); auto id = pair.first; auto msg = pair.second; @@ -7873,6 +7922,7 @@ void MainWindow::on_tableWidgetCalls_cellDoubleClicked(int row, int col){ } else { addMessageText(call); } +#endif } void MainWindow::on_tableWidgetCalls_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected){ diff --git a/messagewindow.cpp b/messagewindow.cpp index f197beb..c985846 100644 --- a/messagewindow.cpp +++ b/messagewindow.cpp @@ -1,14 +1,45 @@ #include "messagewindow.h" #include "ui_messagewindow.h" +#include "moc_messagewindow.cpp" + +#include + +#include "Radio.hpp" +#include "keyeater.h" + +template +QList listCopyReverse(QList const &list){ + QList newList = QList(); + auto iter = list.end(); + while(iter != list.begin()){ + newList.append(*(--iter)); + } + return newList; +} MessageWindow::MessageWindow(QWidget *parent) : - QWidget(parent), + QDialog(parent), ui(new Ui::MessageWindow) { ui->setupUi(this); - this->setWindowTitle("Message History"); + // connect selection model changed + connect(ui->messageTableWidget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MessageWindow::on_messageTableWidget_selectionChanged); + // reply when key pressed in the reply box + auto eke = new EnterKeyPressEater(); + connect(eke, &EnterKeyPressEater::enterKeyPressed, this, [this](QObject *, QKeyEvent * e, bool *pProcessed){ + if(e->modifiers() & Qt::ShiftModifier){ + if(pProcessed) *pProcessed = false; + return; + } + + if(pProcessed) *pProcessed = true; + ui->replyPushButton->click(); + }); + ui->replytextEdit->installEventFilter(eke); + + ui->messageTableWidget->horizontalHeader()->setVisible(true); ui->messageTableWidget->resizeColumnsToContents(); } @@ -16,3 +47,97 @@ MessageWindow::~MessageWindow() { delete ui; } + +void MessageWindow::setCall(const QString &call){ + setWindowTitle(QString("Message History: %1").arg(call)); +} + +void MessageWindow::populateMessages(QList msgs){ + for(int i = ui->messageTableWidget->rowCount(); i >= 0; i--){ + ui->messageTableWidget->removeRow(i); + } + + ui->messageTableWidget->setUpdatesEnabled(false); + { + foreach(auto msg, msgs){ + auto params = msg.params(); + + int row = ui->messageTableWidget->rowCount(); + ui->messageTableWidget->insertRow(row); + + int col = 0; + + auto typeItem = new QTableWidgetItem(msg.type() == "UNREAD" ? "\u2691" : ""); + typeItem->setData(Qt::UserRole, msg.type()); + typeItem->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); + ui->messageTableWidget->setItem(row, col++, typeItem); + + auto date = params.value("UTC").toString(); + auto timestamp = QDateTime::fromString(date, "yyyy-MM-dd hh:mm:ss"); + auto dateItem = new QTableWidgetItem(timestamp.toString()); + dateItem->setData(Qt::UserRole, timestamp); + dateItem->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); + ui->messageTableWidget->setItem(row, col++, dateItem); + + auto dial = (quint64)params.value("DIAL").toInt(); + auto dialItem = new QTableWidgetItem(QString("%1 MHz").arg(Radio::pretty_frequency_MHz_string(dial))); + dialItem->setData(Qt::UserRole, dial); + dialItem->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); + ui->messageTableWidget->setItem(row, col++, dialItem); + + auto path = params.value("PATH").toString(); + auto segs = listCopyReverse(path.split(">")); + auto fromItem = new QTableWidgetItem(segs.join(" via ")); + fromItem->setData(Qt::UserRole, path); + fromItem->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); + ui->messageTableWidget->setItem(row, col++, fromItem); + + auto text = params.value("TEXT").toString(); + auto textItem = new QTableWidgetItem(text); + textItem->setData(Qt::UserRole, text); + textItem->setTextAlignment(Qt::AlignVCenter); + ui->messageTableWidget->setItem(row, col++, textItem); + } + + ui->messageTableWidget->resizeColumnToContents(0); + ui->messageTableWidget->resizeColumnToContents(1); + ui->messageTableWidget->resizeColumnToContents(2); + ui->messageTableWidget->resizeColumnToContents(3); + } + ui->messageTableWidget->setUpdatesEnabled(true); + + if(ui->messageTableWidget->rowCount() > 0){ + ui->messageTableWidget->selectRow(0); + } +} + +QString MessageWindow::prepareReplyMessage(QString path){ + auto text = ui->replytextEdit->toPlainText(); + return QString("%1 MSG %2").arg(path).arg(text); +} + +void MessageWindow::on_messageTableWidget_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/){ + auto row = ui->messageTableWidget->currentRow(); + auto item = ui->messageTableWidget->item(row, ui->messageTableWidget->columnCount()-1); + if(!item){ + return; + } + + auto text = item->data(Qt::UserRole).toString(); + ui->messageTextEdit->setPlainText(text); +} + +void MessageWindow::on_replyPushButton_clicked(){ + auto row = ui->messageTableWidget->currentRow(); + auto item = ui->messageTableWidget->item(row, ui->messageTableWidget->columnCount()-2); + if(!item){ + return; + } + + auto path = item->data(Qt::UserRole).toString(); + auto message = prepareReplyMessage(path); + + emit replyMessage(message); + + close(); +} diff --git a/messagewindow.h b/messagewindow.h index a4594a1..c97457e 100644 --- a/messagewindow.h +++ b/messagewindow.h @@ -1,13 +1,16 @@ #ifndef MESSAGEWINDOW_H #define MESSAGEWINDOW_H -#include +#include +#include + +#include "Message.h" namespace Ui { class MessageWindow; } -class MessageWindow : public QWidget +class MessageWindow : public QDialog { Q_OBJECT @@ -15,6 +18,18 @@ public: explicit MessageWindow(QWidget *parent = 0); ~MessageWindow(); +signals: + void replyMessage(const QString &call); + +public slots: + void setCall(const QString &call); + void populateMessages(QList msgs); + QString prepareReplyMessage(QString path); + +private slots: + void on_messageTableWidget_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/); + void on_replyPushButton_clicked(); + private: Ui::MessageWindow *ui; }; diff --git a/messagewindow.ui b/messagewindow.ui index 343dceb..705b00b 100644 --- a/messagewindow.ui +++ b/messagewindow.ui @@ -1,7 +1,7 @@ MessageWindow - + 0 @@ -11,7 +11,7 @@ - Form + Message History @@ -38,21 +38,74 @@ 0 - 4 + 10 + + + 12 + + + + <html><head/><body><p>Received band activity is displayed with time since last heard, SNR, and the text received for each frequency offset in the passband.</p></body></html> + + + QAbstractScrollArea::AdjustToContentsOnFirstShow + + + false + + + QAbstractItemView::NoEditTriggers + + + false + + + false + true + + QAbstractItemView::SingleSelection + QAbstractItemView::SelectRows + + Qt::ElideRight + + + Qt::DotLine + + + false + + + false + + + true + + + false + + + 30 + + + false + - 10 + 20 true + + false + @@ -66,6 +119,11 @@ Date + + + Frequency + + From @@ -79,7 +137,7 @@ - + 0 1 @@ -93,6 +151,14 @@ QTextEdit { background-color:#ffeaa7; } + + + + 0 + 1 + + +