127 lines
2.9 KiB
C++
127 lines
2.9 KiB
C++
#include "historymodel.h"
|
|
#include <mutex>
|
|
#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
|
|
{
|
|
const std::lock_guard<std::mutex> lock(history_mutex);
|
|
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);
|
|
}
|
|
}
|
|
throw std::out_of_range("history model col");
|
|
} catch (std::out_of_range const& exc) {
|
|
qDebug() << "historymodel data " << exc.what();
|
|
}
|
|
return QVariant();
|
|
}
|
|
|
|
QVariant HistoryModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
{
|
|
const std::lock_guard<std::mutex> lock(history_mutex);
|
|
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();
|
|
}
|
|
|
|
void HistoryModel::update(std::vector<HistoryItem> *items)
|
|
{
|
|
const std::lock_guard<std::mutex> lock(history_mutex);
|
|
if(current_items) {
|
|
delete current_items;
|
|
}
|
|
current_items = items;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|