| 1 | /* |
| 2 | * midisnoop - MIDI monitor and prober |
| 3 | * Copyright (C) 2012 Devin Anderson |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License as published by the Free |
| 7 | * Software Foundation; either version 2 of the License, or (at your option) |
| 8 | * any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 675 Mass |
| 17 | * Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <cassert> |
| 21 | |
| 22 | #include <QtWidgets/QApplication> |
| 23 | |
| 24 | #include "mainview.h" |
| 25 | #include "util.h" |
| 26 | |
| 27 | MainView::MainView(QObject *parent): |
| 28 | DesignerView(":/midisnoop/mainview.ui", parent) |
| 29 | { |
| 30 | QWidget *widget = getRootWidget(); |
| 31 | |
| 32 | aboutAction = getChild<QAction>(widget, "aboutAction"); |
| 33 | connect(aboutAction, SIGNAL(triggered()), |
| 34 | SIGNAL(aboutRequest())); |
| 35 | |
| 36 | addAction = getChild<QAction>(widget, "addAction"); |
| 37 | connect(addAction, SIGNAL(triggered()), |
| 38 | SIGNAL(addMessageRequest())); |
| 39 | |
| 40 | clearAction = getChild<QAction>(widget, "clearAction"); |
| 41 | connect(clearAction, SIGNAL(triggered()), |
| 42 | SIGNAL(clearMessagesRequest())); |
| 43 | |
| 44 | configureAction = getChild<QAction>(widget, "configureAction"); |
| 45 | connect(configureAction, SIGNAL(triggered()), |
| 46 | SIGNAL(configureRequest())); |
| 47 | |
| 48 | quitAction = getChild<QAction>(widget, "quitAction"); |
| 49 | connect(quitAction, SIGNAL(triggered()), |
| 50 | SIGNAL(closeRequest())); |
| 51 | |
| 52 | tableModel.setColumnCount(MESSAGETABLECOLUMN_TOTAL); |
| 53 | tableModel.setRowCount(0); |
| 54 | tableModel.setHeaderData(MESSAGETABLECOLUMN_DATA, Qt::Horizontal, |
| 55 | tr("Data"), Qt::DisplayRole); |
| 56 | tableModel.setHeaderData(MESSAGETABLECOLUMN_STATUS, Qt::Horizontal, |
| 57 | tr("Status"), Qt::DisplayRole); |
| 58 | tableModel.setHeaderData(MESSAGETABLECOLUMN_TIMESTAMP, Qt::Horizontal, |
| 59 | tr("Timestamp"), Qt::DisplayRole); |
| 60 | |
| 61 | tableView = getChild<QTableView>(widget, "centralWidget"); |
| 62 | tableView->setItemDelegate(&tableDelegate); |
| 63 | tableView->setModel(&tableModel); |
| 64 | } |
| 65 | |
| 66 | MainView::~MainView() |
| 67 | { |
| 68 | // Empty |
| 69 | } |
| 70 | |
| 71 | int |
| 72 | MainView::addMessage(quint64 timeStamp, const QString &statusDescription, |
| 73 | const QString &dataDescription, bool valid) |
| 74 | { |
| 75 | int count = tableModel.rowCount(); |
| 76 | bool inserted = tableModel.insertRow(count); |
| 77 | assert(inserted); |
| 78 | Qt::AlignmentFlag alignment = Qt::AlignTop; |
| 79 | setModelData(count, MESSAGETABLECOLUMN_DATA, dataDescription); |
| 80 | setModelData(count, MESSAGETABLECOLUMN_DATA, alignment, |
| 81 | Qt::TextAlignmentRole); |
| 82 | setModelData(count, MESSAGETABLECOLUMN_STATUS, statusDescription); |
| 83 | setModelData(count, MESSAGETABLECOLUMN_STATUS, alignment, |
| 84 | Qt::TextAlignmentRole); |
| 85 | setModelData(count, MESSAGETABLECOLUMN_TIMESTAMP, timeStamp); |
| 86 | setModelData(count, MESSAGETABLECOLUMN_TIMESTAMP, alignment, |
| 87 | Qt::TextAlignmentRole); |
| 88 | if (! valid) { |
| 89 | setModelData(count, MESSAGETABLECOLUMN_STATUS, |
| 90 | QIcon(":/midisnoop/images/16x16/error.png"), |
| 91 | Qt::DecorationRole); |
| 92 | } |
| 93 | tableView->resizeRowToContents(count); |
| 94 | tableView->scrollToBottom(); |
| 95 | return count; |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | MainView::addReceivedMessage(quint64 timeStamp, |
| 100 | const QString &statusDescription, |
| 101 | const QString &dataDescription, bool valid) |
| 102 | { |
| 103 | addMessage(timeStamp, statusDescription, dataDescription, valid); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | MainView::addSentMessage(quint64 timeStamp, const QString &statusDescription, |
| 108 | const QString &dataDescription, bool valid) |
| 109 | { |
| 110 | int index = addMessage(timeStamp, statusDescription, dataDescription, |
| 111 | valid); |
| 112 | const QBrush &brush = qApp->palette().alternateBase(); |
| 113 | setModelData(index, MESSAGETABLECOLUMN_DATA, brush, Qt::BackgroundRole); |
| 114 | setModelData(index, MESSAGETABLECOLUMN_STATUS, brush, Qt::BackgroundRole); |
| 115 | setModelData(index, MESSAGETABLECOLUMN_TIMESTAMP, brush, |
| 116 | Qt::BackgroundRole); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | MainView::clearMessages() |
| 121 | { |
| 122 | int count = tableModel.rowCount(); |
| 123 | if (count) { |
| 124 | bool removed = tableModel.removeRows(0, tableModel.rowCount()); |
| 125 | assert(removed); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | MainView::setMessageSendEnabled(bool enabled) |
| 131 | { |
| 132 | addAction->setEnabled(enabled); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | MainView::setModelData(int row, int column, const QVariant &value, int role) |
| 137 | { |
| 138 | bool result = tableModel.setData(tableModel.index(row, column), value, |
| 139 | role); |
| 140 | assert(result); |
| 141 | } |