Commit b34822f6 authored by Julien Wintz's avatar Julien Wintz
Browse files

Adding log model.

parent 25b58a30
......@@ -4,9 +4,9 @@
## Copyright (C) 2008-2011 - Julien Wintz, Inria.
## Created: Thu Mar 1 14:34:49 2012 (+0100)
## Version: $Id$
## Last-Updated: Fri Mar 2 15:32:15 2012 (+0100)
## Last-Updated: Mon Mar 5 11:07:46 2012 (+0100)
## By: Julien Wintz
## Update #: 23
## Update #: 25
######################################################################
##
### Commentary:
......@@ -28,9 +28,11 @@ set(${PROJECT_NAME}_HEADERS
dtkLogger.h
dtkLogEngine.h
dtkLogDestination.h
dtkLogModel.h
dtkLogView.h)
set(${PROJECT_NAME}_HEADERS_MOC
dtkLogModel.h
dtkLogView.h
dtkLogView_p.h)
......@@ -38,6 +40,7 @@ set(${PROJECT_NAME}_SOURCES
dtkLogger.cpp
dtkLogEngine.cpp
dtkLogDestination.cpp
dtkLogModel.cpp
dtkLogView.cpp)
## #################################################################
......
......@@ -4,9 +4,9 @@
* Copyright (C) 2008-2011 - Julien Wintz, Inria.
* Created: Thu Mar 1 15:15:19 2012 (+0100)
* Version: $Id$
* Last-Updated: Fri Mar 2 19:04:40 2012 (+0100)
* Last-Updated: Mon Mar 5 12:28:40 2012 (+0100)
* By: Julien Wintz
* Update #: 53
* Update #: 59
*/
/* Commentary:
......@@ -18,7 +18,7 @@
*/
#include "dtkLogDestination.h"
#include "dtkLogView_p.h"
#include "dtkLogModel.h"
// /////////////////////////////////////////////////////////////////
// dtkLogDestination
......@@ -74,28 +74,28 @@ void dtkLogDestinationFile::write(const QString& message)
}
// /////////////////////////////////////////////////////////////////
// dtkLogDestinationView
// dtkLogDestinationModel
// /////////////////////////////////////////////////////////////////
class dtkLogDestinationViewPrivate
class dtkLogDestinationModelPrivate
{
public:
dtkLogViewList *view;
dtkLogModel *model;
};
dtkLogDestinationView::dtkLogDestinationView(dtkLogViewList *view) : d(new dtkLogDestinationViewPrivate)
dtkLogDestinationModel::dtkLogDestinationModel(dtkLogModel *model) : d(new dtkLogDestinationModelPrivate)
{
d->view = view;
d->model = model;
}
dtkLogDestinationView::~dtkLogDestinationView(void)
dtkLogDestinationModel::~dtkLogDestinationModel(void)
{
delete d;
d = NULL;
}
void dtkLogDestinationView::write(const QString& message)
void dtkLogDestinationModel::write(const QString& message)
{
d->view->append(message);
d->model->append(message);
}
......@@ -4,9 +4,9 @@
* Copyright (C) 2008-2011 - Julien Wintz, Inria.
* Created: Thu Mar 1 15:10:22 2012 (+0100)
* Version: $Id$
* Last-Updated: Fri Mar 2 19:01:08 2012 (+0100)
* Last-Updated: Mon Mar 5 11:11:34 2012 (+0100)
* By: Julien Wintz
* Update #: 35
* Update #: 41
*/
/* Commentary:
......@@ -24,6 +24,8 @@
#include <QtCore>
class dtkLogModel;
// /////////////////////////////////////////////////////////////////
// dtkLogDestination
// /////////////////////////////////////////////////////////////////
......@@ -70,20 +72,19 @@ private:
// dtkLogDestinationList
// /////////////////////////////////////////////////////////////////
class dtkLogDestinationViewPrivate;
class dtkLogViewList;
class dtkLogDestinationModelPrivate;
class DTKLOG_EXPORT dtkLogDestinationView : public dtkLogDestination
class DTKLOG_EXPORT dtkLogDestinationModel : public dtkLogDestination
{
public:
dtkLogDestinationView(dtkLogViewList *view);
~dtkLogDestinationView(void);
dtkLogDestinationModel(dtkLogModel *model);
~dtkLogDestinationModel(void);
public:
void write(const QString& message);
private:
dtkLogDestinationViewPrivate *d;
dtkLogDestinationModelPrivate *d;
};
// /////////////////////////////////////////////////////////////////
......
/* dtkLogModel.cpp ---
*
* Author: Julien Wintz
* Copyright (C) 2008-2011 - Julien Wintz, Inria.
* Created: Mon Mar 5 10:25:48 2012 (+0100)
* Version: $Id$
* Last-Updated: Mon Mar 5 12:29:52 2012 (+0100)
* By: Julien Wintz
* Update #: 18
*/
/* Commentary:
*
*/
/* Change log:
*
*/
#include "dtkLogModel.h"
class dtkLogModelPrivate
{
public:
QStringList logs;
};
dtkLogModel::dtkLogModel(QObject *parent) : QAbstractListModel(parent), d(new dtkLogModelPrivate)
{
}
void dtkLogModel::append(const QString& message)
{
int row = d->logs.count();
beginInsertRows(QModelIndex(), row, row);
d->logs.append(message);
endInsertRows();
}
int dtkLogModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return d->logs.count();
}
QVariant dtkLogModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= d->logs.size())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole)
return d->logs.at(index.row());
return QVariant();
}
Qt::ItemFlags dtkLogModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return QAbstractItemModel::flags(index) | Qt::ItemIsDropEnabled;
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
}
bool dtkLogModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.row() >= 0 && index.row() < d->logs.size()
&& (role == Qt::EditRole || role == Qt::DisplayRole)) {
d->logs.replace(index.row(), value.toString());
emit dataChanged(index, index);
return true;
}
return false;
}
bool dtkLogModel::insertRows(int row, int count, const QModelIndex &parent)
{
if (count < 1 || row < 0 || row > rowCount(parent))
return false;
beginInsertRows(QModelIndex(), row, row + count - 1);
for (int r = 0; r < count; ++r)
d->logs.insert(row, QString());
endInsertRows();
return true;
}
bool dtkLogModel::removeRows(int row, int count, const QModelIndex &parent)
{
if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
return false;
beginRemoveRows(QModelIndex(), row, row + count - 1);
for (int r = 0; r < count; ++r)
d->logs.removeAt(row);
endRemoveRows();
return true;
}
static bool ascendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2)
{
return s1.first < s2.first;
}
static bool decendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2)
{
return s1.first > s2.first;
}
void dtkLogModel::sort(int, Qt::SortOrder order)
{
emit layoutAboutToBeChanged();
QList<QPair<QString, int> > list;
for (int i = 0; i < d->logs.count(); ++i)
list.append(QPair<QString, int>(d->logs.at(i), i));
if (order == Qt::AscendingOrder)
qSort(list.begin(), list.end(), ascendingLessThan);
else
qSort(list.begin(), list.end(), decendingLessThan);
d->logs.clear();
QVector<int> forwarding(list.count());
for (int i = 0; i < list.count(); ++i) {
d->logs.append(list.at(i).first);
forwarding[list.at(i).second] = i;
}
QModelIndexList oldList = persistentIndexList();
QModelIndexList newList;
for (int i = 0; i < oldList.count(); ++i)
newList.append(index(forwarding.at(oldList.at(i).row()), 0));
changePersistentIndexList(oldList, newList);
emit layoutChanged();
}
Qt::DropActions dtkLogModel::supportedDropActions(void) const
{
return QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
}
/* dtkLogModel.h ---
*
* Author: Julien Wintz
* Copyright (C) 2008-2011 - Julien Wintz, Inria.
* Created: Mon Mar 5 10:13:44 2012 (+0100)
* Version: $Id$
* Last-Updated: Mon Mar 5 11:14:39 2012 (+0100)
* By: Julien Wintz
* Update #: 27
*/
/* Commentary:
*
*/
/* Change log:
*
*/
#ifndef DTKLOGMODEL_H
#define DTKLOGMODEL_H
#include "dtkLogExport.h"
#include <QtGui>
class dtkLogModelPrivate;
class DTKLOG_EXPORT dtkLogModel : public QAbstractListModel
{
Q_OBJECT
public:
dtkLogModel(QObject *parent = 0);
public:
void append(const QString& message);
public:
int rowCount(const QModelIndex &parent = QModelIndex()) const;
public:
QVariant data(const QModelIndex &index, int role) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
public:
Qt::ItemFlags flags(const QModelIndex &index) const;
public:
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
public:
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
public:
Qt::DropActions supportedDropActions(void) const;
private:
dtkLogModelPrivate *d;
};
#endif
......@@ -4,9 +4,9 @@
* Copyright (C) 2008-2011 - Julien Wintz, Inria.
* Created: Fri Mar 2 15:13:52 2012 (+0100)
* Version: $Id$
* Last-Updated: Fri Mar 2 19:08:23 2012 (+0100)
* Last-Updated: Mon Mar 5 12:32:17 2012 (+0100)
* By: Julien Wintz
* Update #: 98
* Update #: 107
*/
/* Commentary:
......@@ -17,6 +17,7 @@
*
*/
#include "dtkLogModel.h"
#include "dtkLogView.h"
#include "dtkLogView_p.h"
......@@ -53,6 +54,8 @@ dtkLogViewTree::~dtkLogViewTree(void)
dtkLogViewList::dtkLogViewList(QWidget *parent) : QListView(parent)
{
this->model = new dtkLogModel(this);
this->setAttribute(Qt::WA_MacShowFocusRect, false);
this->setFrameShape(QFrame::NoFrame);
this->setRuntime();
......@@ -65,16 +68,9 @@ dtkLogViewList::~dtkLogViewList(void)
void dtkLogViewList::setRuntime(void)
{
dtkLogger::instance().attachView(this);
this->setModel(&(this->model));
}
void dtkLogViewList::append(const QString& message)
{
this->list << message;
dtkLogger::instance().attachModel(this->model);
this->model.setStringList(this->list);
this->setModel(this->model);
}
// /////////////////////////////////////////////////////////////////
......
......@@ -4,9 +4,9 @@
* Copyright (C) 2008-2011 - Julien Wintz, Inria.
* Created: Fri Mar 2 15:30:16 2012 (+0100)
* Version: $Id$
* Last-Updated: Fri Mar 2 19:03:52 2012 (+0100)
* Last-Updated: Mon Mar 5 11:10:55 2012 (+0100)
* By: Julien Wintz
* Update #: 35
* Update #: 39
*/
/* Commentary:
......@@ -24,6 +24,8 @@
#include <QtGui>
class dtkLogModel;
// /////////////////////////////////////////////////////////////////
// dtkLogViewTree
// /////////////////////////////////////////////////////////////////
......@@ -52,12 +54,8 @@ public:
public:
void setRuntime(void);
public:
void append(const QString& message);
private:
QStringList list;
QStringListModel model;
dtkLogModel *model;
};
// /////////////////////////////////////////////////////////////////
......
......@@ -4,9 +4,9 @@
* Copyright (C) 2008-2011 - Julien Wintz, Inria.
* Created: Thu Mar 1 17:19:52 2012 (+0100)
* Version: $Id$
* Last-Updated: Fri Mar 2 18:57:59 2012 (+0100)
* Last-Updated: Mon Mar 5 11:13:23 2012 (+0100)
* By: Julien Wintz
* Update #: 86
* Update #: 87
*/
/* Commentary:
......@@ -65,21 +65,21 @@ void dtkLogger::detachFile(const QString& path)
d->files.remove(path);
}
void dtkLogger::attachView(dtkLogViewList *view)
void dtkLogger::attachModel(dtkLogModel *model)
{
d->views[view] = dtkLogDestinationPointer(new dtkLogDestinationView(view));
d->models[model] = dtkLogDestinationPointer(new dtkLogDestinationModel(model));
d->destinations << d->views[view];
d->destinations << d->models[model];
}
void dtkLogger::detachView(dtkLogViewList *view)
void dtkLogger::detachModel(dtkLogModel *model)
{
if(!d->views.contains(view))
if(!d->models.contains(model))
return;
d->destinations.removeOne(d->views[view]);
d->destinations.removeOne(d->models[model]);
d->views.remove(view);
d->models.remove(model);
}
dtkLogger::dtkLogger(void) : d(new dtkLoggerPrivate)
......
......@@ -4,9 +4,9 @@
* Copyright (C) 2008-2011 - Julien Wintz, Inria.
* Created: Thu Mar 1 17:18:31 2012 (+0100)
* Version: $Id$
* Last-Updated: Fri Mar 2 18:47:34 2012 (+0100)
* Last-Updated: Mon Mar 5 11:13:44 2012 (+0100)
* By: Julien Wintz
* Update #: 29
* Update #: 30
*/
/* Commentary:
......@@ -27,7 +27,7 @@
class dtkLoggerPrivate;
class dtkLogDestination;
class dtkLogViewList;
class dtkLogModel;
class DTKLOG_EXPORT dtkLogger
{
......@@ -49,8 +49,8 @@ public:
void detachFile(const QString& path);
public:
void attachView(dtkLogViewList *view);
void detachView(dtkLogViewList *view);
void attachModel(dtkLogModel *model);
void detachModel(dtkLogModel *model);
private:
dtkLogger(void);
......
......@@ -4,9 +4,9 @@
* Copyright (C) 2008-2011 - Julien Wintz, Inria.
* Created: Thu Mar 1 17:26:54 2012 (+0100)
* Version: $Id$
* Last-Updated: Fri Mar 2 18:56:14 2012 (+0100)
* Last-Updated: Mon Mar 5 11:12:53 2012 (+0100)
* By: Julien Wintz
* Update #: 41
* Update #: 43
*/
/* Commentary:
......@@ -33,9 +33,9 @@ public:
dtkLog::Level level;
public:
dtkLogDestinationPointer console;
QHash<QString, dtkLogDestinationPointer> files;
QHash<dtkLogViewList *, dtkLogDestinationPointer> views;
dtkLogDestinationPointer console;
QHash<QString, dtkLogDestinationPointer> files;
QHash<dtkLogModel *, dtkLogDestinationPointer> models;
public:
QList<dtkLogDestinationPointer> destinations;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment