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,7 +104,31 @@ QVariant AppListModel::data(const QModelIndex &index, int role) const
switch (role) {
// Label
case Qt::DisplayRole:
case Qt::ToolTipRole: {
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();
@ -134,17 +158,16 @@ QVariant AppListModel::data(const QModelIndex &index, int role) const
return tr("Allow");
}
case 3:
return (role == Qt::DisplayRole || appRow.endTime.isNull()) ? QVariant()
: appRow.endTime;
return (role == Qt::DisplayRole || appRow.endTime.isNull()) ? QVariant() : appRow.endTime;
case 4:
return appRow.creatTime;
}
break;
return QVariant();
}
// Icon
case Qt::DecorationRole: {
QVariant AppListModel::dataDecoration(const QModelIndex &index) const
{
const int column = index.column();
if (column == 0 || column == 2 || column == 3) {
@ -162,11 +185,11 @@ QVariant AppListModel::data(const QModelIndex &index, int role) const
}
}
break;
return QVariant();
}
// Font
case Qt::FontRole: {
QVariant AppListModel::dataFont(const QModelIndex &index) const
{
const int column = index.column();
if (column == 2) {
@ -175,11 +198,11 @@ QVariant AppListModel::data(const QModelIndex &index, int role) const
return font;
}
break;
return QVariant();
}
// Foreground
case Qt::ForegroundRole: {
QVariant AppListModel::dataForeground(const QModelIndex &index) const
{
const int column = index.column();
if (column == 1 || column == 2) {
@ -203,21 +226,17 @@ QVariant AppListModel::data(const QModelIndex &index, int role) const
}
}
break;
return QVariant();
}
// Text Alignment
case Qt::TextAlignmentRole: {
QVariant AppListModel::dataTextAlignment(const QModelIndex &index) const
{
const int column = index.column();
if (column == 1) {
return int(Qt::AlignHCenter | Qt::AlignVCenter);
}
break;
}
}
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,7 +113,18 @@ QVariant AppStatModel::data(const QModelIndex &index, int role) const
QVariant();
// Label
if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
if (role == Qt::DisplayRole || role == Qt::ToolTipRole)
return dataDisplay(index);
// Icon
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");
@ -127,8 +138,8 @@ QVariant AppStatModel::data(const QModelIndex &index, int role) const
return FileUtil::fileName(appPath);
}
// Icon
if (role == Qt::DecorationRole) {
QVariant AppStatModel::dataDecoration(const QModelIndex &index) const
{
const int row = index.row();
if (row == 0) {
return IconCache::icon(":/images/computer-96.png");
@ -137,6 +148,3 @@ QVariant AppStatModel::data(const QModelIndex &index, int role) const
const auto appPath = list().at(row);
return appInfoCache()->appIcon(appPath);
}
return StringListModel::data(index, role);
}

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,7 +59,19 @@ QVariant ZoneListModel::data(const QModelIndex &index, int role) const
switch (role) {
// Label
case Qt::DisplayRole:
case Qt::ToolTipRole: {
case Qt::ToolTipRole:
return dataDisplay(index);
// Enabled
case Qt::CheckStateRole:
return dataDisplay(index);
}
return QVariant();
}
QVariant ZoneListModel::dataDisplay(const QModelIndex &index) const
{
const int row = index.row();
const int column = index.column();
@ -78,16 +90,15 @@ QVariant ZoneListModel::data(const QModelIndex &index, int role) const
return zoneRow.lastSuccess;
}
break;
return QVariant();
}
case Qt::CheckStateRole:
QVariant ZoneListModel::dataCheckState(const QModelIndex &index) const
{
if (index.column() == 0) {
const auto zoneRow = zoneRowAt(index.row());
return zoneRow.enabled;
}
break;
}
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,7 +57,28 @@ QVariant TaskListModel::data(const QModelIndex &index, int role) const
return QVariant();
switch (role) {
case Qt::DisplayRole: {
case Qt::DisplayRole:
case Qt::ToolTipRole:
return dataDisplay(index);
case Qt::CheckStateRole:
case RoleEnabled:
return dataCheckState(index);
case RoleIntervalHours:
return taskIntervalHours(index.row());
case RoleRunning: {
const auto taskInfo = taskInfoAt(index.row());
return taskInfo->running();
}
}
return QVariant();
}
QVariant TaskListModel::dataDisplay(const QModelIndex &index) const
{
const int row = index.row();
const int column = index.column();
@ -74,24 +95,14 @@ QVariant TaskListModel::data(const QModelIndex &index, int role) const
return formatDateTime(taskInfo->lastSuccess());
}
break;
return QVariant();
}
case Qt::CheckStateRole:
case RoleEnabled:
QVariant TaskListModel::dataCheckState(const QModelIndex &index) const
{
if (index.column() == 0) {
return taskEnabled(index.row());
}
break;
case RoleIntervalHours:
return taskIntervalHours(index.row());
case RoleRunning: {
const auto taskInfo = taskInfoAt(index.row());
return taskInfo->running();
}
}
return QVariant();
}

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();