-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
193 lines (174 loc) · 7.34 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "gelfserver.h"
#include "gelfmessagemodel.h"
#include "qjsonmodel.h"
#include <QSortFilterProxyModel>
#include <QSystemTrayIcon>
#include <QSettings>
#include <QDebug>
#include <QMenu>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
system_tray_menu(new QMenu(this)),
system_tray_icon(new QSystemTrayIcon(this)),
gelf_server(new GELFServer(this)),
gelf_message_model(new GELFMessageModel(this)),
gelf_message_proxy_model(new QSortFilterProxyModel(this)),
gelf_details_model(new QJsonModel(this))
{
ui->setupUi(this);
// tray menu
QAction *action;
action = system_tray_menu->addAction(tr("Quit"));
connect(action, SIGNAL(triggered(bool)), this, SLOT(close()));
// tray icon
system_tray_icon->setIcon(QIcon(":/GELFServer.svg"));
system_tray_icon->setContextMenu(system_tray_menu);
system_tray_icon->show();
// Earlysignals connect
connect(gelf_server, SIGNAL(message(QJsonObject)), this, SLOT(onMessage(QJsonObject)));
connect(ui->messagesView->horizontalHeader(), SIGNAL(sectionCountChanged(int,int)), this, SLOT(onSectionCountChanged(int,int)));
connect(ui->columnsWidget, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(onColumnsWidgetItemChange(QListWidgetItem*)));
// configure model and views
gelf_message_proxy_model->setSourceModel(gelf_message_model);
ui->messagesView->setModel(gelf_message_proxy_model);
ui->detailsView->setModel(gelf_details_model);
// finalize UI setttings
ui->messagesView->setSortingEnabled(true);
ui->messagesView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->messagesView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->messagesView->verticalHeader()->setVisible(false);
ui->messagesView->horizontalHeader()->setStretchLastSection(true);
ui->messagesView->horizontalHeader()->setSectionsMovable(true);
ui->splitter->setChildrenCollapsible(false);
// load configuration
QSettings settings;
restoreState(settings.value("MainWindow/state").toByteArray());
restoreGeometry(settings.value("MainWindow/geometry").toByteArray());
ui->splitter->restoreState(settings.value("MainWindow/splitter/state").toByteArray());
ui->splitter->restoreGeometry(settings.value("MainWindow/splitter/geometry").toByteArray());
ui->detailsView->header()->restoreState(settings.value("MainWindow/detailsView/state").toByteArray());
ui->detailsView->header()->restoreGeometry(settings.value("MainWindow/detailsView/geometry").toByteArray());
ui->messagesView->horizontalHeader()->restoreState(settings.value("MainWindow/messagesView/state").toByteArray());
ui->messagesView->horizontalHeader()->restoreGeometry(settings.value("MainWindow/messagesView/geometry").toByteArray());
// Late signals connect
connect(ui->messagesView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
this, SLOT(onCurrentRowChanged(QModelIndex,QModelIndex)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
QSettings settings;
// views and widget state
settings.setValue("MainWindow/messagesView/geometry", ui->messagesView->horizontalHeader()->saveGeometry());
settings.setValue("MainWindow/messagesView/state", ui->messagesView->horizontalHeader()->saveState());
settings.setValue("MainWindow/detailsView/geometry", ui->detailsView->header()->saveGeometry());
settings.setValue("MainWindow/detailsView/state", ui->detailsView->header()->saveState());
settings.setValue("MainWindow/splitter/geometry", ui->splitter->saveGeometry());
settings.setValue("MainWindow/splitter/state", ui->splitter->saveState());
settings.setValue("MainWindow/geometry", saveGeometry());
settings.setValue("MainWindow/state", saveState());
// column state
int columns_count = ui->columnsWidget->count();
for (int i=0; i<columns_count; i++) {
QListWidgetItem* item = ui->columnsWidget->item(i);
if (item->checkState() == Qt::Checked) {
settings.setValue("MainWindow/columns/" + item->text(), true);
} else {
settings.setValue("MainWindow/columns/" + item->text(), false);
}
}
QMainWindow::closeEvent(event);
}
void MainWindow::onMessage(QJsonObject message)
{
// push data to model
gelf_message_model->onMessage(message);
// show notification
QString title = message["host"].toString() + " ";
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information;
int level = message["level"].toInt();
switch (level) {
case 0: // Emergency
icon = QSystemTrayIcon::Critical;
title += tr("Emergency");
break;
case 1: // Alert
icon = QSystemTrayIcon::Critical;
title += tr("Alert");
break;
case 2: // Critical
icon = QSystemTrayIcon::Critical;
title += tr("Critical");
break;
case 3: // Error
icon = QSystemTrayIcon::Critical;
title += tr("Error");
break;
case 4: // Warning
icon = QSystemTrayIcon::Warning;
title += tr("Warning");
break;
case 5: // Notice
icon = QSystemTrayIcon::Warning;
title += tr("Notice");
break;
case 6: // Informational
icon = QSystemTrayIcon::Information;
title += tr("Info");
break;
case 7: // Debug
icon = QSystemTrayIcon::Information;
title += tr("Debug");
break;
default:
break;
}
system_tray_icon->showMessage(title, message["full_message"].toString(), icon, 5000);
}
void MainWindow::onSectionCountChanged(int,int b)
{
QSettings settings;
ui->columnsWidget->clear();
for (int i=0; i<b; i++) {
QString header_name = gelf_message_proxy_model->headerData(i, Qt::Horizontal).toString();
QListWidgetItem* item = new QListWidgetItem(header_name, ui->columnsWidget);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setData(Qt::UserRole, i);
bool column_enabled = false;
if (settings.contains("MainWindow/columns/" + header_name)) {
column_enabled = settings.value("MainWindow/columns/" + header_name, false).toBool();
} else {
if (header_name == "host" || header_name == "short_message" || header_name == "timestamp") {
column_enabled = true;
}
}
if (column_enabled) {
item->setCheckState(Qt::Checked);
ui->messagesView->horizontalHeader()->setSectionHidden(i, false);
} else {
item->setCheckState(Qt::Unchecked);
ui->messagesView->horizontalHeader()->setSectionHidden(i, true);
}
}
}
void MainWindow::onCurrentRowChanged(QModelIndex current, QModelIndex /*previous*/)
{
QModelIndex source_index = gelf_message_proxy_model->mapToSource(current);
QJsonObject data = gelf_message_model->rowData(source_index.row());
gelf_details_model->setJsonObject(data);
}
void MainWindow::onColumnsWidgetItemChange(QListWidgetItem*item)
{
int column_number = item->data(Qt::UserRole).toInt();
if (item->checkState() == Qt::Checked) {
ui->messagesView->horizontalHeader()->setSectionHidden(column_number, false);
} else {
ui->messagesView->horizontalHeader()->setSectionHidden(column_number, true);
}
}