+/**
+ * @file LogView/logview.cpp
+ * @brief Implementation of the LogView module
+ * @author Enar Vaikene
+ *
+ * Copyright (c) 2011 Enar Vaikene
+ *
+ * This file is part of the eVaf C++ cross-platform application development framework.
+ *
+ * This file can be used under the terms of the GNU General Public License
+ * version 3.0 as published by the Free Software Foundation and appearing in
+ * the file LICENSE included in the packaging of this file. Please review the
+ * the following information to ensure the GNU General Public License version
+ * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
+ *
+ * Alternatively, this file may be used in accordance with the Commercial License
+ * Agreement provided with the Software.
+ */
+
+#include "logview.h"
+#include "version.h"
+
+#include <Common/Globals>
+#include <Common/iLogger>
+
+#include <QtGui>
+
+
+using namespace eVaf;
+using namespace eVaf::LogView::Internal;
+
+
+//-------------------------------------------------------------------
+
+int const Model::MaxLines = 1000;
+
+char const * const Model::SeverityText[Common::iLogger::Count] = {
+ QT_TR_NOOP("[NONE] "),
+ QT_TR_NOOP("[FATAL] "),
+ QT_TR_NOOP("[ERROR] "),
+ QT_TR_NOOP("[WARNING]"),
+ QT_TR_NOOP("[INFO] "),
+ QT_TR_NOOP("[DEBUG] ")
+};
+
+Model::Model(QObject * parent)
+ : QAbstractListModel(parent)
+{
+}
+
+QVariant Model::data(QModelIndex const & index, int role) const
+{
+ if (!index.isValid() || index.row() < 0 || index.row() >= mData.size() || index.column() != 0)
+ return QVariant();
+
+ switch (role) {
+
+ // Return the message for the display role
+ case Qt::DisplayRole: {
+ return mData.at(index.row()).text;
+ break;
+ }
+
+ // Change color for different message types
+ case Qt::ForegroundRole: {
+ Common::iLogger::Severity s = mData.at(index.row()).severity;
+ switch (s) {
+ case Common::iLogger::Info:
+ return QBrush(QColor(Qt::blue));
+ break;
+ case Common::iLogger::Warning:
+ return QBrush(QColor(Qt::black));
+ break;
+ case Common::iLogger::Error:
+ case Common::iLogger::Fatal:
+ return QBrush(QColor(Qt::red));
+ break;
+ default:
+ return QVariant();
+ }
+ break;
+ }
+ } // switch (role)
+
+ return QVariant();
+}
+
+void Model::addMessage(Common::iLogger::Severity severity, QString const & text, QString const & where)
+{
+ // Add the message to the end of the queue
+ beginInsertRows(QModelIndex(), mData.size(), mData.size());
+ mData.enqueue(Message(severity, text, where));
+ endInsertRows();
+
+ // Remove oldest messages if the list is full
+ if (mData.size() > MaxLines) {
+ beginRemoveRows(QModelIndex(), 0, 0);
+ mData.dequeue();
+ endRemoveRows();
+ }
+}
+
+QString Model::details(QModelIndex const & index) const
+{
+ Message const & m = mData.at(index.row());
+ return tr("%1 %2.%3 %4 : %5\nOccurred in %6")
+ .arg(m.dt.date().toString(Qt::DefaultLocaleShortDate))
+ .arg(m.dt.time().toString(Qt::DefaultLocaleLongDate))
+ .arg(m.dt.time().msec(), 3, 10, QChar('0'))
+ .arg(tr(severityText(m.severity)))
+ .arg(m.text)
+ .arg(m.where);
+}
+
+bool Model::copyToClipboard(QModelIndex const & index)
+{
+ mErrorString.clear();
+
+ QClipboard * cb = QApplication::clipboard();
+ if (cb) {
+ cb->setText(details(index));
+ return true;
+ }
+ else {
+ mErrorString = tr("The global clipboard is not available");
+ return false;
+ }
+}
+
+bool Model::saveToFile(QString const & fileName)
+{
+ mErrorString.clear();
+
+ QFile f(fileName);
+ if (!f.open(QFile::WriteOnly)) {
+ mErrorString = tr("Failed to open the file '%1' for writing : %2").arg(fileName).arg(f.errorString());
+ return false;
+ }
+
+ QTextStream out(&f);
+
+ for (int i = 0; i < mData.size(); ++i) {
+ Message const & m = mData.at(i);
+ out << tr("%1 %2.%3 %4 : %5 (occurred in %6)\n")
+ .arg(m.dt.date().toString(Qt::DefaultLocaleShortDate))
+ .arg(m.dt.time().toString(Qt::DefaultLocaleLongDate))
+ .arg(m.dt.time().msec(), 3, 10, QChar('0'))
+ .arg(tr(severityText(m.severity)))
+ .arg(m.text)
+ .arg(m.where)
+ << endl;
+ }
+
+ return true;
+}
+
+char const * const Model::severityText(Common::iLogger::Severity s) const
+{
+ if (s >= Common::iLogger::None && s < Common::iLogger::Count)
+ return SeverityText[s];
+ else
+ return SeverityText[Common::iLogger::None];
+}
+
+
+//-------------------------------------------------------------------
+
+Widget::Widget(QWidget * parent, Qt::WindowFlags flags)
+ : QWidget(parent, flags)
+{
+ setObjectName(QString("%1-Widget").arg(VER_MODULE_NAME_STR));
+ EVAF_INFO("%s created", qPrintable(objectName()));
+}
+
+Widget::~Widget()
+{
+ EVAF_INFO("%s destroyed", qPrintable(objectName()));
+}
+
+
+//-------------------------------------------------------------------
+
+Module::Module()
+ : Plugins::iPlugin()
+ , wWidget(0)
+{
+ setObjectName(QString("%1-Module").arg(VER_MODULE_NAME_STR));
+ EVAF_INFO("%s created", qPrintable(objectName()));
+}
+
+Module::~Module()
+{
+ EVAF_INFO("%s destroyed", qPrintable(objectName()));
+}
+
+bool Module::init(QString const & args)
+{
+ Q_UNUSED(args);
+
+ wWidget = new Widget();
+
+ EVAF_INFO("%s initialized", qPrintable(objectName()));
+
+ return true;
+}
+
+void Module::done()
+{
+ if (wWidget) {
+ delete wWidget;
+ wWidget = 0;
+ }
+
+ EVAF_INFO("%s finalized", qPrintable(objectName()));
+}