UI: Refactor other *Model::data().

This commit is contained in:
Nodir Temirkhodjaev 2021-03-20 11:45:54 +03:00
parent f11f318127
commit 55509ebe60
10 changed files with 249 additions and 184 deletions

View File

@ -104,118 +104,137 @@ QVariant AppListModel::data(const QModelIndex &index, int role) const
switch (role) {
// Label
case Qt::DisplayRole:
case Qt::ToolTipRole: {
const int row = index.row();
const int column = index.column();
case Qt::ToolTipRole:
return dataDisplay(index, role);
// Icon
case Qt::DecorationRole:
return dataDecoration(index);
// Font
case Qt::FontRole:
return dataFont(index);
// Foreground
case Qt::ForegroundRole:
return dataForeground(index);
// Text Alignment
case Qt::TextAlignmentRole:
return dataTextAlignment(index);
}
return QVariant();
}
QVariant AppListModel::dataDisplay(const QModelIndex &index, int role) const
{
const int row = index.row();
const int column = index.column();
const auto appRow = appRowAt(row);
switch (column) {
case 0: {
auto appName = appRow.appName;
if (appName.isEmpty()) {
const auto appInfo = appInfoCache()->appInfo(appRow.appPath);
appName = !appInfo.fileDescription.isEmpty() ? appInfo.fileDescription
: FileUtil::fileName(appRow.appPath);
if (appInfo.isValid()) {
confManager()->updateAppName(appRow.appId, appName);
}
}
return appName;
}
case 1:
return appGroupAt(appRow.groupIndex)->name();
case 2: {
if (appRow.alerted)
return tr("Alert");
if (appRow.blocked)
return tr("Block");
return tr("Allow");
}
case 3:
return (role == Qt::DisplayRole || appRow.endTime.isNull()) ? QVariant() : appRow.endTime;
case 4:
return appRow.creatTime;
}
return QVariant();
}
QVariant AppListModel::dataDecoration(const QModelIndex &index) const
{
const int column = index.column();
if (column == 0 || column == 2 || column == 3) {
const int row = index.row();
const auto appRow = appRowAt(row);
switch (column) {
case 0: {
auto appName = appRow.appName;
if (appName.isEmpty()) {
const auto appInfo = appInfoCache()->appInfo(appRow.appPath);
appName = !appInfo.fileDescription.isEmpty() ? appInfo.fileDescription
: FileUtil::fileName(appRow.appPath);
if (appInfo.isValid()) {
confManager()->updateAppName(appRow.appId, appName);
}
}
return appName;
}
case 1:
return appGroupAt(appRow.groupIndex)->name();
case 2: {
if (appRow.alerted)
return tr("Alert");
if (appRow.blocked)
return tr("Block");
return tr("Allow");
}
case 0:
return appInfoCache()->appIcon(appRow.appPath);
case 2:
return appRow.blocked ? IconCache::icon(":/icons/sign-ban.png")
: IconCache::icon(":/icons/sign-check.png");
case 3:
return (role == Qt::DisplayRole || appRow.endTime.isNull()) ? QVariant()
: appRow.endTime;
case 4:
return appRow.creatTime;
return appRow.endTime.isNull() ? QVariant() : IconCache::icon(":/icons/clock.png");
}
break;
}
// Icon
case Qt::DecorationRole: {
const int column = index.column();
return QVariant();
}
if (column == 0 || column == 2 || column == 3) {
const int row = index.row();
const auto appRow = appRowAt(row);
QVariant AppListModel::dataFont(const QModelIndex &index) const
{
const int column = index.column();
switch (column) {
case 0:
return appInfoCache()->appIcon(appRow.appPath);
case 2:
return appRow.blocked ? IconCache::icon(":/icons/sign-ban.png")
: IconCache::icon(":/icons/sign-check.png");
case 3:
return appRow.endTime.isNull() ? QVariant() : IconCache::icon(":/icons/clock.png");
}
}
break;
if (column == 2) {
QFont font;
font.setWeight(QFont::DemiBold);
return font;
}
// Font
case Qt::FontRole: {
const int column = index.column();
return QVariant();
}
if (column == 2) {
QFont font;
font.setWeight(QFont::DemiBold);
return font;
QVariant AppListModel::dataForeground(const QModelIndex &index) const
{
const int column = index.column();
if (column == 1 || column == 2) {
const int row = index.row();
const auto appRow = appRowAt(row);
switch (column) {
case 1: {
if (!appRow.useGroupPerm)
return inactiveColor;
if (!appGroupAt(appRow.groupIndex)->enabled())
return blockColor;
break;
}
case 2:
if (appRow.alerted)
return alertColor;
if (appRow.blocked)
return blockColor;
return allowColor;
}
break;
}
// Foreground
case Qt::ForegroundRole: {
const int column = index.column();
return QVariant();
}
if (column == 1 || column == 2) {
const int row = index.row();
const auto appRow = appRowAt(row);
QVariant AppListModel::dataTextAlignment(const QModelIndex &index) const
{
const int column = index.column();
switch (column) {
case 1: {
if (!appRow.useGroupPerm)
return inactiveColor;
if (!appGroupAt(appRow.groupIndex)->enabled())
return blockColor;
break;
}
case 2:
if (appRow.alerted)
return alertColor;
if (appRow.blocked)
return blockColor;
return allowColor;
}
}
break;
}
// Text Alignment
case Qt::TextAlignmentRole: {
const int column = index.column();
if (column == 1) {
return int(Qt::AlignHCenter | Qt::AlignVCenter);
}
break;
}
if (column == 1) {
return int(Qt::AlignHCenter | Qt::AlignVCenter);
}
return QVariant();

View File

@ -74,6 +74,13 @@ protected:
QString sqlBase() const override;
QString sqlOrderColumn() const override;
private:
QVariant dataDisplay(const QModelIndex &index, int role) const;
QVariant dataDecoration(const QModelIndex &index) const;
QVariant dataFont(const QModelIndex &index) const;
QVariant dataForeground(const QModelIndex &index) const;
QVariant dataTextAlignment(const QModelIndex &index) const;
private:
ConfManager *m_confManager = nullptr;
AppInfoCache *m_appInfoCache = nullptr;

View File

@ -113,30 +113,38 @@ QVariant AppStatModel::data(const QModelIndex &index, int role) const
QVariant();
// Label
if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
const int row = index.row();
if (row == 0)
return tr("All");
const auto appPath = list().at(row);
const auto appInfo = appInfoCache()->appInfo(appPath);
if (!appInfo.fileDescription.isEmpty()) {
return appInfo.fileDescription;
}
return FileUtil::fileName(appPath);
}
if (role == Qt::DisplayRole || role == Qt::ToolTipRole)
return dataDisplay(index);
// Icon
if (role == Qt::DecorationRole) {
const int row = index.row();
if (row == 0) {
return IconCache::icon(":/images/computer-96.png");
}
const auto appPath = list().at(row);
return appInfoCache()->appIcon(appPath);
}
if (role == Qt::DecorationRole)
return dataDecoration(index);
return StringListModel::data(index, role);
}
QVariant AppStatModel::dataDisplay(const QModelIndex &index) const
{
const int row = index.row();
if (row == 0)
return tr("All");
const auto appPath = list().at(row);
const auto appInfo = appInfoCache()->appInfo(appPath);
if (!appInfo.fileDescription.isEmpty()) {
return appInfo.fileDescription;
}
return FileUtil::fileName(appPath);
}
QVariant AppStatModel::dataDecoration(const QModelIndex &index) const
{
const int row = index.row();
if (row == 0) {
return IconCache::icon(":/images/computer-96.png");
}
const auto appPath = list().at(row);
return appInfoCache()->appIcon(appPath);
}

View File

@ -44,6 +44,9 @@ private slots:
private:
void updateList();
QVariant dataDisplay(const QModelIndex &index) const;
QVariant dataDecoration(const QModelIndex &index) const;
private:
StatManager *m_statManager = nullptr;
TrafListModel *m_trafListModel = nullptr;

View File

@ -104,6 +104,25 @@ QVariant ConnListModel::headerData(int section, Qt::Orientation orientation, int
return QVariant();
}
QVariant ConnListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
switch (role) {
// Label
case Qt::DisplayRole:
case Qt::ToolTipRole:
return dataDisplay(index, role);
// Icon
case Qt::DecorationRole:
return dataDecoration(index);
}
return QVariant();
}
QVariant ConnListModel::dataDisplay(const QModelIndex &index, int role) const
{
const int row = index.row();
@ -164,25 +183,6 @@ QVariant ConnListModel::dataDecoration(const QModelIndex &index) const
return QVariant();
}
QVariant ConnListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
switch (role) {
// Label
case Qt::DisplayRole:
case Qt::ToolTipRole:
return dataDisplay(index, role);
// Icon
case Qt::DecorationRole:
return dataDecoration(index);
}
return QVariant();
}
void ConnListModel::deleteConn(qint64 rowIdTo, bool blocked, int row)
{
beginRemoveRows(QModelIndex(), 0, row);

View File

@ -61,7 +61,7 @@ QVariant TrafListModel::data(const QModelIndex &index, int role) const
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole) {
if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
const int row = index.row();
const int column = index.column();

View File

@ -59,34 +59,45 @@ QVariant ZoneListModel::data(const QModelIndex &index, int role) const
switch (role) {
// Label
case Qt::DisplayRole:
case Qt::ToolTipRole: {
const int row = index.row();
const int column = index.column();
case Qt::ToolTipRole:
return dataDisplay(index);
const auto zoneRow = zoneRowAt(row);
switch (column) {
case 0:
return QString("%1) %2").arg(QString::number(m_zoneRow.zoneId), zoneRow.zoneName);
case 1: {
const auto zoneSource = ZoneSourceWrapper(zoneSourceByCode(zoneRow.sourceCode));
return zoneSource.title();
}
case 2:
return zoneRow.lastRun;
case 3:
return zoneRow.lastSuccess;
}
break;
// Enabled
case Qt::CheckStateRole:
return dataDisplay(index);
}
case Qt::CheckStateRole:
if (index.column() == 0) {
const auto zoneRow = zoneRowAt(index.row());
return zoneRow.enabled;
}
break;
return QVariant();
}
QVariant ZoneListModel::dataDisplay(const QModelIndex &index) const
{
const int row = index.row();
const int column = index.column();
const auto zoneRow = zoneRowAt(row);
switch (column) {
case 0:
return QString("%1) %2").arg(QString::number(m_zoneRow.zoneId), zoneRow.zoneName);
case 1: {
const auto zoneSource = ZoneSourceWrapper(zoneSourceByCode(zoneRow.sourceCode));
return zoneSource.title();
}
case 2:
return zoneRow.lastRun;
case 3:
return zoneRow.lastSuccess;
}
return QVariant();
}
QVariant ZoneListModel::dataCheckState(const QModelIndex &index) const
{
if (index.column() == 0) {
const auto zoneRow = zoneRowAt(index.row());
return zoneRow.enabled;
}
return QVariant();

View File

@ -81,6 +81,9 @@ protected:
QString sqlBase() const override;
private:
QVariant dataDisplay(const QModelIndex &index) const;
QVariant dataCheckState(const QModelIndex &index) const;
void initZoneTypes();
void initZoneSources();
void initZoneSourceNames();

View File

@ -57,32 +57,13 @@ QVariant TaskListModel::data(const QModelIndex &index, int role) const
return QVariant();
switch (role) {
case Qt::DisplayRole: {
const int row = index.row();
const int column = index.column();
const auto taskInfo = taskInfoAt(row);
switch (column) {
case 0:
return taskInfo->title();
case 1:
return taskIntervalHours(row);
case 2:
return formatDateTime(taskInfo->lastRun());
case 3:
return formatDateTime(taskInfo->lastSuccess());
}
break;
}
case Qt::DisplayRole:
case Qt::ToolTipRole:
return dataDisplay(index);
case Qt::CheckStateRole:
case RoleEnabled:
if (index.column() == 0) {
return taskEnabled(index.row());
}
break;
return dataCheckState(index);
case RoleIntervalHours:
return taskIntervalHours(index.row());
@ -96,6 +77,36 @@ QVariant TaskListModel::data(const QModelIndex &index, int role) const
return QVariant();
}
QVariant TaskListModel::dataDisplay(const QModelIndex &index) const
{
const int row = index.row();
const int column = index.column();
const auto taskInfo = taskInfoAt(row);
switch (column) {
case 0:
return taskInfo->title();
case 1:
return taskIntervalHours(row);
case 2:
return formatDateTime(taskInfo->lastRun());
case 3:
return formatDateTime(taskInfo->lastSuccess());
}
return QVariant();
}
QVariant TaskListModel::dataCheckState(const QModelIndex &index) const
{
if (index.column() == 0) {
return taskEnabled(index.row());
}
return QVariant();
}
bool TaskListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
Q_UNUSED(value);

View File

@ -49,6 +49,9 @@ public slots:
void saveChanges();
private:
QVariant dataDisplay(const QModelIndex &index) const;
QVariant dataCheckState(const QModelIndex &index) const;
void setupTaskRows();
void clearTaskRows();