js8call/messagewindow.cpp

182 lines
6.4 KiB
C++
Raw Normal View History

2019-02-01 15:53:07 -05:00
#include "messagewindow.h"
#include "ui_messagewindow.h"
2019-02-02 17:06:01 -05:00
#include "moc_messagewindow.cpp"
#include <QDateTime>
#include <QMenu>
2019-02-02 17:06:01 -05:00
#include "Radio.hpp"
#include "keyeater.h"
template<typename T>
QList<T> listCopyReverse(QList<T> const &list){
QList<T> newList = QList<T>();
auto iter = list.end();
while(iter != list.begin()){
newList.append(*(--iter));
}
return newList;
}
2019-02-01 15:53:07 -05:00
MessageWindow::MessageWindow(QWidget *parent) :
2019-02-02 17:06:01 -05:00
QDialog(parent),
2019-02-01 15:53:07 -05:00
ui(new Ui::MessageWindow)
{
ui->setupUi(this);
2019-02-02 17:06:01 -05:00
// connect selection model changed
connect(ui->messageTableWidget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MessageWindow::on_messageTableWidget_selectionChanged);
2019-02-01 15:53:07 -05:00
2019-02-02 17:06:01 -05:00
// 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);
2019-02-01 15:53:07 -05:00
ui->messageTableWidget->resizeColumnsToContents();
ui->messageTableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
auto deleteAction = new QAction("Delete", ui->messageTableWidget);
connect(deleteAction, &QAction::triggered, this, [this](){
auto items = ui->messageTableWidget->selectedItems();
if(items.isEmpty()){
return;
}
auto item = items.first();
auto col = ui->messageTableWidget->item(item->row(), 1);
if(!col){
return;
}
bool ok = false;
auto mid = col->data(Qt::UserRole).toInt(&ok);
if(!ok){
return;
}
ui->messageTableWidget->removeRow(item->row());
emit this->deleteMessage(mid);
});
ui->messageTableWidget->addAction(deleteAction);
2019-02-01 15:53:07 -05:00
}
MessageWindow::~MessageWindow()
{
delete ui;
}
2019-02-02 17:06:01 -05:00
void MessageWindow::setCall(const QString &call){
setWindowTitle(QString("Messages: %1").arg(call == "%" ? "All" : call));
2019-02-02 17:06:01 -05:00
}
void MessageWindow::populateMessages(QList<QPair<int, Message> > msgs){
2019-02-02 17:06:01 -05:00
for(int i = ui->messageTableWidget->rowCount(); i >= 0; i--){
ui->messageTableWidget->removeRow(i);
}
ui->messageTableWidget->setUpdatesEnabled(false);
{
foreach(auto pair, msgs){
auto mid = pair.first;
auto msg = pair.second;
2019-02-02 17:06:01 -05:00
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 midItem = new QTableWidgetItem(QString::number(mid));
midItem->setData(Qt::UserRole, mid);
midItem->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
ui->messageTableWidget->setItem(row, col++, midItem);
2019-02-02 17:06:01 -05:00
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);
2019-02-16 16:00:13 -05:00
auto to = params.value("TO").toString();
auto toItem = new QTableWidgetItem(to);
toItem->setData(Qt::UserRole, to);
toItem->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
ui->messageTableWidget->setItem(row, col++, toItem);
2019-02-02 17:06:01 -05:00
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->resizeColumnToContents(4);
2019-02-16 16:00:13 -05:00
ui->messageTableWidget->resizeColumnToContents(5);
2019-02-02 17:06:01 -05:00
}
ui->messageTableWidget->setUpdatesEnabled(true);
if(ui->messageTableWidget->rowCount() > 0){
ui->messageTableWidget->selectRow(0);
}
}
QString MessageWindow::prepareReplyMessage(QString path, QString text){
2019-02-02 17:06:01 -05:00
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 text = "[MESSAGE]"; // ui->replytextEdit->toPlainText();
auto message = prepareReplyMessage(path, text);
2019-02-02 17:06:01 -05:00
emit replyMessage(message);
}