2020-08-26 18:58:36 +00:00
|
|
|
#include "historymodel.h"
|
|
|
|
#include <QDateTime>
|
|
|
|
|
|
|
|
HistoryModel::HistoryModel(QObject *parent)
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
int HistoryModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
|
|
|
if(current_items) {
|
|
|
|
return current_items->size();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HistoryModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant HistoryModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if(role != Qt::DisplayRole) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!current_items) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
int row = index.row();
|
|
|
|
int col = index.column();
|
|
|
|
try {
|
|
|
|
auto item = current_items->at(row);
|
|
|
|
switch(col) {
|
|
|
|
case 0: {
|
|
|
|
return QString::number(item.id);
|
|
|
|
}
|
|
|
|
case 1: {
|
|
|
|
QDateTime timestamp;
|
|
|
|
timestamp.setTime_t(item.timestamp);
|
|
|
|
return timestamp.toString(Qt::SystemLocaleShortDate);
|
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
std::string url = item.scheme + "://" + item.host + ":" + std::to_string(item.port) + item.path;
|
|
|
|
return QString::fromStdString(url);
|
|
|
|
}
|
|
|
|
case 3: {
|
|
|
|
return item.status_code;
|
|
|
|
}
|
|
|
|
case 4: {
|
|
|
|
return QString::fromStdString(item.reason);
|
|
|
|
}
|
|
|
|
case 5: {
|
|
|
|
return item.rtt;
|
|
|
|
}
|
|
|
|
case 6:{
|
|
|
|
return QString::number(item.size);
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 23:09:41 +00:00
|
|
|
throw std::out_of_range("history model col");
|
2020-08-26 18:58:36 +00:00
|
|
|
} catch (std::out_of_range const& exc) {
|
|
|
|
qDebug() << "historymodel data " << exc.what();
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2020-08-28 23:09:41 +00:00
|
|
|
QVariant HistoryModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if(role != Qt::DisplayRole) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
if(orientation == Qt::Horizontal) {
|
|
|
|
switch(section) {
|
|
|
|
case 0: {
|
|
|
|
return "id";
|
|
|
|
}
|
|
|
|
case 1: {
|
|
|
|
return "time";
|
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
return "url";
|
|
|
|
}
|
|
|
|
case 3: {
|
|
|
|
return "code";
|
|
|
|
}
|
|
|
|
case 4: {
|
|
|
|
return "reason";
|
|
|
|
}
|
|
|
|
case 5: {
|
|
|
|
return "rtt";
|
|
|
|
}
|
|
|
|
case 6: {
|
|
|
|
return "size";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2020-08-26 18:58:36 +00:00
|
|
|
void HistoryModel::update(std::vector<HistoryItem> *items)
|
|
|
|
{
|
|
|
|
if(current_items) {
|
|
|
|
delete current_items;
|
|
|
|
}
|
|
|
|
current_items = items;
|
2020-08-28 23:09:41 +00:00
|
|
|
emit layoutChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HistoryProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
|
|
|
|
{
|
|
|
|
return QSortFilterProxyModel::lessThan(source_left, source_right);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HistoryProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
|
|
|
|
{
|
|
|
|
return true;
|
2020-08-26 18:58:36 +00:00
|
|
|
}
|