forked from LazyT/rrcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.cpp
172 lines (144 loc) · 5.44 KB
/
logger.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
#include "logger.h"
loggerDialog::loggerDialog(QWidget *parent) : QDialog(parent)
{
setupUi(this);
setWindowFlags(Qt::Window | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
connect(treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_customContextMenuRequested(QPoint)));
resize(QApplication::primaryScreen()->availableGeometry().width(), 0);
move(0, 0);
QTimer::singleShot(1, this, SLOT(calcHeight()));
}
void loggerDialog::calcHeight()
{
treeWidget->setMinimumHeight(10 * treeWidget->visualItemRect(treeWidget->topLevelItem(0)).height() + treeWidget->header()->height() + style()->pixelMetric(QStyle::PM_ScrollBarExtent) + 2);
}
void loggerDialog::log(QString p1, QString p2, QString p3, QString p4, QString p5, QString p6)
{
static qlonglong counter = 0;
QTreeWidgetItem *item = new QTreeWidgetItem(QStringList() << QString::number(++counter) << QDateTime::currentDateTime().toString("dd.MM hh:mm:ss") << p1 << p2 << p3 << p4 << p5 << (p6.isEmpty() ? "" : p6.insert(4, '-').insert(9, '-').insert(18, '-').insert(27, '-').insert(36, '-')));
item->setTextAlignment(0, Qt::AlignHCenter);
item->setTextAlignment(1, Qt::AlignHCenter);
item->setTextAlignment(2, Qt::AlignHCenter);
item->setTextAlignment(3, Qt::AlignHCenter);
item->setTextAlignment(4, Qt::AlignHCenter);
item->setTextAlignment(5, Qt::AlignHCenter);
treeWidget->addTopLevelItem(item);
if(treeWidget->topLevelItemCount() == 1)
{
treeWidget->topLevelItem(treeWidget->topLevelItemCount() - 1)->setSelected(true);
}
treeWidget->resizeColumnToContents(0);
treeWidget->resizeColumnToContents(1);
treeWidget->resizeColumnToContents(2);
treeWidget->resizeColumnToContents(3);
treeWidget->resizeColumnToContents(4);
treeWidget->resizeColumnToContents(5);
treeWidget->resizeColumnToContents(6);
treeWidget->resizeColumnToContents(7);
treeWidget->scrollToBottom();
}
void loggerDialog::on_customContextMenuRequested(QPoint pos)
{
QMenu menu(this);
QAction action1(QIcon(":/png/png/clipboard.png"), tr("Copy all to clipboard"), this);
QAction action2(QIcon(":/png/png/clipboard.png"), tr("Copy line to clipboard"), this);
QAction action3(QIcon(":/png/png/clipboard.png"), tr("Copy field to clipboard"), this);
QAction *selected;
menu.addAction(&action1);
menu.addAction(&action2);
menu.addAction(&action3);
selected = menu.exec(treeWidget->viewport()->mapToGlobal(pos));
if(selected)
{
QClipboard *clipboard = QGuiApplication::clipboard();
QTreeWidgetItem *item = treeWidget->itemAt(pos);
if(selected->text() == action1.text())
{
QString text;
for(int i = 0; i < treeWidget->topLevelItemCount(); i++)
{
item = treeWidget->topLevelItem(i);
text.append(QString("%1\t%2\t%3\t%4\t%5\t%6\t%7\t%8\n").arg(item->text(0)).arg(item->text(1)).arg(item->text(2)).arg(item->text(3)).arg(item->text(4)).arg(item->text(5)).arg(item->text(6)).arg(item->text(7)));
}
clipboard->setText(text);
}
else if(selected->text() == action2.text())
{
clipboard->setText(QString("%1\t%2\t%3\t%4\t%5\t%6\t%7\t%8\n").arg(item->text(0)).arg(item->text(1)).arg(item->text(2)).arg(item->text(3)).arg(item->text(4)).arg(item->text(5)).arg(item->text(6)).arg(item->text(7)));
}
else if(selected->text() == action3.text())
{
item = treeWidget->itemAt(pos);
if(item)
{
int colums[7];
colums[0] = treeWidget->header()->sectionSize(0);
colums[1] = colums[0] + treeWidget->header()->sectionSize(1);
colums[2] = colums[1] + treeWidget->header()->sectionSize(2);
colums[3] = colums[2] + treeWidget->header()->sectionSize(3);
colums[4] = colums[3] + treeWidget->header()->sectionSize(4);
colums[5] = colums[4] + treeWidget->header()->sectionSize(5);
colums[6] = colums[5] + treeWidget->header()->sectionSize(6);
if(pos.x() > colums[6])
{
clipboard->setText(item->text(7));
}
else if(pos.x() > colums[5])
{
clipboard->setText(item->text(6));
}
else if(pos.x() > colums[4])
{
clipboard->setText(item->text(5));
}
else if(pos.x() > colums[3])
{
clipboard->setText(item->text(4));
}
else if(pos.x() > colums[2])
{
clipboard->setText(item->text(3));
}
else if(pos.x() > colums[1])
{
clipboard->setText(item->text(2));
}
else if(pos.x() > colums[0])
{
clipboard->setText(item->text(1));
}
else
{
clipboard->setText(item->text(0));
}
}
}
}
}
void loggerDialog::on_buttonBox_clicked(QAbstractButton *button)
{
if(buttonBox->standardButton(button) == QDialogButtonBox::Save)
{
QFile logfile(QFileDialog::getSaveFileName(this, tr("Select file to save log"), LOG, "*.*", nullptr, QFileDialog::DontUseNativeDialog));
if(!logfile.fileName().isEmpty())
{
if(logfile.open(QIODevice::WriteOnly | QIODevice::Text))
{
for(int i = 0; i < treeWidget->topLevelItemCount(); i++)
{
QTreeWidgetItem *item = treeWidget->topLevelItem(i);
logfile.write(QString("%1\t%2\t%3\t%4\t%5\t%6\t%7\t%8\n").arg(item->text(0)).arg(item->text(1)).arg(item->text(2)).arg(item->text(3)).arg(item->text(4)).arg(item->text(5)).arg(item->text(6)).arg(item->text(7)).toUtf8());
}
logfile.close();
}
else
{
QMessageBox::warning(this, APPNAME, tr("Could not open logfile!\n\n%1 : %2").arg(logfile.fileName()).arg(logfile.errorString()));
}
}
}
else if(buttonBox->standardButton(button) == QDialogButtonBox::Close)
{
close();
}
}