]>
vaikene.ee Git - evaf/blob - src/plugins/LogView/logview.cpp
e1c140004a8e21632361f86aef609c59c8013770
2 * @file LogView/logview.cpp
3 * @brief Implementation of the LogView module
6 * Copyright (c) 2011 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
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.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
23 #include <Common/Globals>
24 #include <Common/iLogger>
30 using namespace eVaf::LogView::Internal
;
33 //-------------------------------------------------------------------
35 int const Model::MaxLines
= 1000;
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] ")
46 Model::Model(QObject
* parent
)
47 : QAbstractListModel(parent
)
51 QVariant
Model::data(QModelIndex
const & index
, int role
) const
53 if (!index
.isValid() || index
.row() < 0 || index
.row() >= mData
.size() || index
.column() != 0)
58 // Return the message for the display role
59 case Qt::DisplayRole
: {
60 return mData
.at(index
.row()).text
;
64 // Change color for different message types
65 case Qt::ForegroundRole
: {
66 Common::iLogger::Severity s
= mData
.at(index
.row()).severity
;
68 case Common::iLogger::Info
:
69 return QBrush(QColor(Qt::blue
));
71 case Common::iLogger::Warning
:
72 return QBrush(QColor(Qt::black
));
74 case Common::iLogger::Error
:
75 case Common::iLogger::Fatal
:
76 return QBrush(QColor(Qt::red
));
88 void Model::addMessage(Common::iLogger::Severity severity
, QString
const & text
, QString
const & where
)
90 // Add the message to the end of the queue
91 beginInsertRows(QModelIndex(), mData
.size(), mData
.size());
92 mData
.enqueue(Message(severity
, text
, where
));
95 // Remove oldest messages if the list is full
96 if (mData
.size() > MaxLines
) {
97 beginRemoveRows(QModelIndex(), 0, 0);
103 QString
Model::details(QModelIndex
const & index
) const
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
)))
115 bool Model::copyToClipboard(QModelIndex
const & index
)
117 mErrorString
.clear();
119 QClipboard
* cb
= QApplication::clipboard();
121 cb
->setText(details(index
));
125 mErrorString
= tr("The global clipboard is not available");
130 bool Model::saveToFile(QString
const & fileName
)
132 mErrorString
.clear();
135 if (!f
.open(QFile::WriteOnly
)) {
136 mErrorString
= tr("Failed to open the file '%1' for writing : %2").arg(fileName
).arg(f
.errorString());
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
)))
157 char const * const Model::severityText(Common::iLogger::Severity s
) const
159 if (s
>= Common::iLogger::None
&& s
< Common::iLogger::Count
)
160 return SeverityText
[s
];
162 return SeverityText
[Common::iLogger::None
];
166 //-------------------------------------------------------------------
168 Widget::Widget(QWidget
* parent
, Qt::WindowFlags flags
)
169 : QWidget(parent
, flags
)
171 setObjectName(QString("%1-Widget").arg(VER_MODULE_NAME_STR
));
172 EVAF_INFO("%s created", qPrintable(objectName()));
177 EVAF_INFO("%s destroyed", qPrintable(objectName()));
181 //-------------------------------------------------------------------
187 setObjectName(QString("%1-Module").arg(VER_MODULE_NAME_STR
));
188 EVAF_INFO("%s created", qPrintable(objectName()));
193 EVAF_INFO("%s destroyed", qPrintable(objectName()));
196 bool Module::init(QString
const & args
)
200 wWidget
= new Widget();
202 EVAF_INFO("%s initialized", qPrintable(objectName()));
214 EVAF_INFO("%s finalized", qPrintable(objectName()));