]> vaikene.ee Git - evaf/blob - src/plugins/LogView/logview.cpp
e1c140004a8e21632361f86aef609c59c8013770
[evaf] / src / plugins / LogView / logview.cpp
1 /**
2 * @file LogView/logview.cpp
3 * @brief Implementation of the LogView module
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #include "logview.h"
21 #include "version.h"
22
23 #include <Common/Globals>
24 #include <Common/iLogger>
25
26 #include <QtGui>
27
28
29 using namespace eVaf;
30 using namespace eVaf::LogView::Internal;
31
32
33 //-------------------------------------------------------------------
34
35 int const Model::MaxLines = 1000;
36
37 char const * const Model::SeverityText[Common::iLogger::Count] = {
38 QT_TR_NOOP("[NONE] "),
39 QT_TR_NOOP("[FATAL] "),
40 QT_TR_NOOP("[ERROR] "),
41 QT_TR_NOOP("[WARNING]"),
42 QT_TR_NOOP("[INFO] "),
43 QT_TR_NOOP("[DEBUG] ")
44 };
45
46 Model::Model(QObject * parent)
47 : QAbstractListModel(parent)
48 {
49 }
50
51 QVariant Model::data(QModelIndex const & index, int role) const
52 {
53 if (!index.isValid() || index.row() < 0 || index.row() >= mData.size() || index.column() != 0)
54 return QVariant();
55
56 switch (role) {
57
58 // Return the message for the display role
59 case Qt::DisplayRole: {
60 return mData.at(index.row()).text;
61 break;
62 }
63
64 // Change color for different message types
65 case Qt::ForegroundRole: {
66 Common::iLogger::Severity s = mData.at(index.row()).severity;
67 switch (s) {
68 case Common::iLogger::Info:
69 return QBrush(QColor(Qt::blue));
70 break;
71 case Common::iLogger::Warning:
72 return QBrush(QColor(Qt::black));
73 break;
74 case Common::iLogger::Error:
75 case Common::iLogger::Fatal:
76 return QBrush(QColor(Qt::red));
77 break;
78 default:
79 return QVariant();
80 }
81 break;
82 }
83 } // switch (role)
84
85 return QVariant();
86 }
87
88 void Model::addMessage(Common::iLogger::Severity severity, QString const & text, QString const & where)
89 {
90 // Add the message to the end of the queue
91 beginInsertRows(QModelIndex(), mData.size(), mData.size());
92 mData.enqueue(Message(severity, text, where));
93 endInsertRows();
94
95 // Remove oldest messages if the list is full
96 if (mData.size() > MaxLines) {
97 beginRemoveRows(QModelIndex(), 0, 0);
98 mData.dequeue();
99 endRemoveRows();
100 }
101 }
102
103 QString Model::details(QModelIndex const & index) const
104 {
105 Message const & m = mData.at(index.row());
106 return tr("%1 %2.%3 %4 : %5\nOccurred in %6")
107 .arg(m.dt.date().toString(Qt::DefaultLocaleShortDate))
108 .arg(m.dt.time().toString(Qt::DefaultLocaleLongDate))
109 .arg(m.dt.time().msec(), 3, 10, QChar('0'))
110 .arg(tr(severityText(m.severity)))
111 .arg(m.text)
112 .arg(m.where);
113 }
114
115 bool Model::copyToClipboard(QModelIndex const & index)
116 {
117 mErrorString.clear();
118
119 QClipboard * cb = QApplication::clipboard();
120 if (cb) {
121 cb->setText(details(index));
122 return true;
123 }
124 else {
125 mErrorString = tr("The global clipboard is not available");
126 return false;
127 }
128 }
129
130 bool Model::saveToFile(QString const & fileName)
131 {
132 mErrorString.clear();
133
134 QFile f(fileName);
135 if (!f.open(QFile::WriteOnly)) {
136 mErrorString = tr("Failed to open the file '%1' for writing : %2").arg(fileName).arg(f.errorString());
137 return false;
138 }
139
140 QTextStream out(&f);
141
142 for (int i = 0; i < mData.size(); ++i) {
143 Message const & m = mData.at(i);
144 out << tr("%1 %2.%3 %4 : %5 (occurred in %6)\n")
145 .arg(m.dt.date().toString(Qt::DefaultLocaleShortDate))
146 .arg(m.dt.time().toString(Qt::DefaultLocaleLongDate))
147 .arg(m.dt.time().msec(), 3, 10, QChar('0'))
148 .arg(tr(severityText(m.severity)))
149 .arg(m.text)
150 .arg(m.where)
151 << endl;
152 }
153
154 return true;
155 }
156
157 char const * const Model::severityText(Common::iLogger::Severity s) const
158 {
159 if (s >= Common::iLogger::None && s < Common::iLogger::Count)
160 return SeverityText[s];
161 else
162 return SeverityText[Common::iLogger::None];
163 }
164
165
166 //-------------------------------------------------------------------
167
168 Widget::Widget(QWidget * parent, Qt::WindowFlags flags)
169 : QWidget(parent, flags)
170 {
171 setObjectName(QString("%1-Widget").arg(VER_MODULE_NAME_STR));
172 EVAF_INFO("%s created", qPrintable(objectName()));
173 }
174
175 Widget::~Widget()
176 {
177 EVAF_INFO("%s destroyed", qPrintable(objectName()));
178 }
179
180
181 //-------------------------------------------------------------------
182
183 Module::Module()
184 : Plugins::iPlugin()
185 , wWidget(0)
186 {
187 setObjectName(QString("%1-Module").arg(VER_MODULE_NAME_STR));
188 EVAF_INFO("%s created", qPrintable(objectName()));
189 }
190
191 Module::~Module()
192 {
193 EVAF_INFO("%s destroyed", qPrintable(objectName()));
194 }
195
196 bool Module::init(QString const & args)
197 {
198 Q_UNUSED(args);
199
200 wWidget = new Widget();
201
202 EVAF_INFO("%s initialized", qPrintable(objectName()));
203
204 return true;
205 }
206
207 void Module::done()
208 {
209 if (wWidget) {
210 delete wWidget;
211 wWidget = 0;
212 }
213
214 EVAF_INFO("%s finalized", qPrintable(objectName()));
215 }