143 lines
3.0 KiB
C++
143 lines
3.0 KiB
C++
#include "vpnmodel.h"
|
|
|
|
#include <QDebug>
|
|
#include <QFont>
|
|
|
|
#include <iterator>
|
|
|
|
namespace {
|
|
enum {
|
|
ColumnName,
|
|
ColumnCommand,
|
|
NumberOfColumns
|
|
};
|
|
}
|
|
|
|
VpnModel::VpnModel(QObject *parent) :
|
|
QAbstractTableModel{parent}
|
|
{
|
|
for (Entry &entry : m_entries)
|
|
connect(&entry, &Entry::dataChanged, this, &VpnModel::entryChanged);
|
|
}
|
|
|
|
int VpnModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
Q_UNUSED(parent)
|
|
return std::size(m_entries);
|
|
}
|
|
|
|
int VpnModel::columnCount(const QModelIndex &parent) const
|
|
{
|
|
Q_UNUSED(parent)
|
|
return NumberOfColumns;
|
|
}
|
|
|
|
QVariant VpnModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
const auto &entry = m_entries[index.row()];
|
|
|
|
switch (index.column())
|
|
{
|
|
case ColumnName:
|
|
switch (Qt::ItemDataRole(role))
|
|
{
|
|
case Qt::DisplayRole:
|
|
case Qt::EditRole:
|
|
return entry.name();
|
|
case Qt::CheckStateRole:
|
|
return entry.state();
|
|
case Qt::FontRole:
|
|
QFont font;
|
|
font.setBold(true);
|
|
return font;
|
|
}
|
|
break;
|
|
case ColumnCommand:
|
|
switch (Qt::ItemDataRole(role))
|
|
{
|
|
case Qt::DisplayRole:
|
|
case Qt::EditRole:
|
|
return entry.binaryName() + ' ' + entry.arguments().join(' ');
|
|
}
|
|
break;
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
QVariant VpnModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
{
|
|
switch (orientation)
|
|
{
|
|
case Qt::Horizontal:
|
|
switch (section)
|
|
{
|
|
case ColumnName:
|
|
switch (Qt::ItemDataRole(role))
|
|
{
|
|
case Qt::DisplayRole:
|
|
case Qt::EditRole:
|
|
return tr("Name");
|
|
}
|
|
break;
|
|
case ColumnCommand:
|
|
switch (Qt::ItemDataRole(role))
|
|
{
|
|
case Qt::DisplayRole:
|
|
case Qt::EditRole:
|
|
return tr("Command");
|
|
}
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
bool VpnModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
{
|
|
auto &entry = m_entries[index.row()];
|
|
|
|
switch (index.column())
|
|
{
|
|
case ColumnName:
|
|
switch (role)
|
|
{
|
|
case Qt::CheckStateRole:
|
|
entry.toggle();
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
Qt::ItemFlags VpnModel::flags(const QModelIndex &index) const
|
|
{
|
|
auto flags = QAbstractTableModel::flags(index);
|
|
if (index.column() == 0)
|
|
flags |= Qt::ItemIsUserCheckable | Qt::ItemIsAutoTristate;
|
|
return flags;
|
|
}
|
|
|
|
void VpnModel::entryChanged()
|
|
{
|
|
const auto *sender_ptr = sender();
|
|
const auto iter = std::find_if(std::cbegin(m_entries), std::cend(m_entries), [sender_ptr](const Entry &entry){
|
|
return &entry == sender_ptr;
|
|
});
|
|
|
|
if (iter == std::cend(m_entries))
|
|
{
|
|
qCritical() << "unknown sender" << sender_ptr;
|
|
return;
|
|
}
|
|
|
|
const auto row = std::distance(std::cbegin(m_entries), iter);
|
|
|
|
const auto index = createIndex(row, 0);
|
|
emit dataChanged(index, index, { Qt::CheckStateRole });
|
|
}
|