Skip to content

Commit

Permalink
Extend error log API so that observer can be notified of new log entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jcfr committed Aug 20, 2013
1 parent 8156536 commit 0ab3178
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 23 deletions.
56 changes: 37 additions & 19 deletions Libs/Core/ctkErrorLogModel.cpp
Expand Up @@ -437,29 +437,19 @@ void ctkErrorLogModel::addEntry(const QDateTime& currentDateTime, const QString&
bool groupEntry = false;
if (d->LogEntryGrouping)
{
// Retrieve threadId associated with last row
QModelIndex lastRowThreadIdIndex =
d->StandardItemModel.index(d->StandardItemModel.rowCount() - 1, ctkErrorLogModel::ThreadIdColumn);
int lastRowIndex = d->StandardItemModel.rowCount() - 1;

bool threadIdMatched = threadId == lastRowThreadIdIndex.data().toString();
QString lastRowThreadId = this->logEntry(Self::ThreadIdColumn, lastRowIndex).toString();
bool threadIdMatched = threadId == lastRowThreadId;

// Retrieve logLevel associated with last row
QModelIndex lastRowLogLevelIndex =
d->StandardItemModel.index(d->StandardItemModel.rowCount() - 1, ctkErrorLogModel::LogLevelColumn);
QString lastRowLogLevel = this->logEntry(Self::LogLevelColumn, lastRowIndex).toString();
bool logLevelMatched = d->ErrorLogLevel(logLevel) == lastRowLogLevel;

bool logLevelMatched = d->ErrorLogLevel(logLevel) == lastRowLogLevelIndex.data().toString();

// Retrieve origin associated with last row
QModelIndex lastRowOriginIndex =
d->StandardItemModel.index(d->StandardItemModel.rowCount() - 1, ctkErrorLogModel::OriginColumn);

bool originMatched = origin == lastRowOriginIndex.data().toString();

// Retrieve time associated with last row
QModelIndex lastRowTimeIndex =
d->StandardItemModel.index(d->StandardItemModel.rowCount() - 1, ctkErrorLogModel::TimeColumn);
QDateTime lastRowDateTime = QDateTime::fromString(lastRowTimeIndex.data().toString(), timeFormat);
QString lastRowOrigin = this->logEntry(Self::OriginColumn, lastRowIndex).toString();
bool originMatched = origin == lastRowOrigin;

QDateTime lastRowDateTime =
QDateTime::fromString(this->logEntry(Self::TimeColumn, lastRowIndex).toString(), timeFormat);
int groupingIntervalInMsecs = 1000;
bool withinGroupingInterval = lastRowDateTime.time().msecsTo(currentDateTime.time()) <= groupingIntervalInMsecs;

Expand Down Expand Up @@ -522,6 +512,8 @@ void ctkErrorLogModel::addEntry(const QDateTime& currentDateTime, const QString&
}

d->AddingEntry = false;

emit this->entryAdded(logLevel);
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -653,6 +645,32 @@ void ctkErrorLogModel::setAsynchronousLogging(bool value)
d->AsynchronousLogging = value;
}

// --------------------------------------------------------------------------
QVariant ctkErrorLogModel::logEntry(int column, int index, int role) const
{
Q_D(const ctkErrorLogModel);
if (column < 0 || column >= Self::InvalidColumn
|| index < 0 || index > this->logEntryCount())
{
return QVariant();
}
QModelIndex rowDescriptionIndex = d->StandardItemModel.index(index, column);
return rowDescriptionIndex.data(role);
}

// --------------------------------------------------------------------------
QString ctkErrorLogModel::logEntryDescription(int index) const
{
return this->logEntry(Self::DescriptionColumn, Self::DescriptionTextRole, index).toString();
}

// --------------------------------------------------------------------------
int ctkErrorLogModel::logEntryCount()const
{
Q_D(const ctkErrorLogModel);
return d->StandardItemModel.rowCount();
}

// --------------------------------------------------------------------------
// ctkErrorLogAbstractMessageHandlerPrivate

Expand Down
19 changes: 15 additions & 4 deletions Libs/Core/ctkErrorLogModel.h
Expand Up @@ -110,7 +110,8 @@ class CTK_CORE_EXPORT ctkErrorLogModel : public QSortFilterProxyModel
ThreadIdColumn,
LogLevelColumn,
OriginColumn,
DescriptionColumn
DescriptionColumn,
InvalidColumn // Add new columnId before this line

This comment has been minimized.

Copy link
@finetjul

finetjul Aug 20, 2013

rename into ColumnCount ? or MaxColumn = DescriptionColumn ?

};

enum ItemDataRole{
Expand Down Expand Up @@ -156,9 +157,6 @@ class CTK_CORE_EXPORT ctkErrorLogModel : public QSortFilterProxyModel
/// \sa TerminalOutput
void setTerminalOutputs(const TerminalOutputs& terminalOutput);

/// Remove all message from model
void clear();

ctkErrorLogLevel::LogLevels logLevelFilter()const;

void filterEntry(const ctkErrorLogLevel::LogLevels& logLevel = ctkErrorLogLevel::Unknown, bool disableFilter = false);
Expand All @@ -169,13 +167,26 @@ class CTK_CORE_EXPORT ctkErrorLogModel : public QSortFilterProxyModel
bool asynchronousLogging()const;
void setAsynchronousLogging(bool value);

QVariant logEntry(int column, int index, int role = Qt::DisplayRole) const;

This comment has been minimized.

Copy link
@finetjul

finetjul Aug 20, 2013

comments would be nice

This comment has been minimized.

Copy link
@finetjul

finetjul Aug 20, 2013

rename into logEntryData ?
is index same as row ?

This comment has been minimized.

Copy link
@finetjul

finetjul Aug 20, 2013

It's a bit confusing....

I would suggest to give a column by default (the most interesting one, Description?)


Q_INVOKABLE QString logEntryDescription(int index) const;

This comment has been minimized.

Copy link
@finetjul

finetjul Aug 20, 2013

comments would be nice


Q_INVOKABLE int logEntryCount() const;

This comment has been minimized.

Copy link
@finetjul

finetjul Aug 20, 2013

comments would be nice


public Q_SLOTS:

/// Remove all message from model
void clear();

void addEntry(const QDateTime& currentDateTime, const QString& threadId,
ctkErrorLogLevel::LogLevel logLevel, const QString& origin, const QString& text);

Q_SIGNALS:
void logLevelFilterChanged();

/// \sa addEntry()
void entryAdded(ctkErrorLogLevel::LogLevel logLevel);

protected:
QScopedPointer<ctkErrorLogModelPrivate> d_ptr;

Expand Down
10 changes: 10 additions & 0 deletions Libs/Widgets/ctkErrorLogWidget.cpp
Expand Up @@ -287,3 +287,13 @@ void ctkErrorLogWidget::onSelectionChanged(const QItemSelection & selected,

// fprintf(stdout, "onSelectionChanged: %d\n", start.msecsTo(QTime::currentTime()));
}

// --------------------------------------------------------------------------
void ctkErrorLogWidget::changeEvent(QEvent * event)
{
this->Superclass::changeEvent(event);
if(event->type() == QEvent::ActivationChange)
{
emit this->windowActivationChanged(this->isActiveWindow());
}
}
7 changes: 7 additions & 0 deletions Libs/Widgets/ctkErrorLogWidget.h
Expand Up @@ -65,6 +65,13 @@ protected Q_SLOTS:

void onSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected);

Q_SIGNALS:

void windowActivationChanged(bool);

This comment has been minimized.

Copy link
@finetjul

finetjul Aug 20, 2013

I wouldn't expose this signal, it's too low level.
However the object interested in such "event" should install an event filter on the error log widget instead.

This comment has been minimized.

Copy link
@jcfr

jcfr Aug 20, 2013

Author Owner

Good point. Makes sense


protected:
void changeEvent(QEvent * event);

This comment has been minimized.

Copy link
@finetjul

finetjul Aug 20, 2013

virtual is missing


protected:
QScopedPointer<ctkErrorLogWidgetPrivate> d_ptr;

Expand Down

0 comments on commit 0ab3178

Please sign in to comment.