Larger improvements
This commit is contained in:
1 parent
b1ce4ecf64
commit
0704012542
22 files changed
+553
-185
No files matched your search
+104
-63
@@ -9,23 +9,27 @@ ChecklistModel::ChecklistModel(const QStringList &items, QObject *parent) :
|
||||
QAbstractListModel(parent)
|
||||
{
|
||||
for (const auto &item : items)
|
||||
m_items.append(std::make_pair(item, true));
|
||||
m_items.append({ item, item, true });
|
||||
}
|
||||
|
||||
ChecklistModel::ChecklistModel(const QList<std::pair<QString, bool> > &items, QObject *parent) :
|
||||
ChecklistModel::ChecklistModel(const QList<ChecklistItem> &items, QObject *parent) :
|
||||
QAbstractListModel(parent),
|
||||
m_items(items)
|
||||
{
|
||||
}
|
||||
|
||||
QStringList ChecklistModel::items() const
|
||||
const QList<ChecklistModel::ChecklistItem> &ChecklistModel::items() const
|
||||
{
|
||||
QStringList items;
|
||||
return m_items;
|
||||
}
|
||||
|
||||
for (const auto &pair : m_items)
|
||||
items.append(std::get<0>(pair));
|
||||
void ChecklistModel::setItems(const QList<ChecklistItem> &items)
|
||||
{
|
||||
emit beginResetModel();
|
||||
|
||||
return items;
|
||||
m_items = items;
|
||||
|
||||
emit endResetModel();
|
||||
}
|
||||
|
||||
void ChecklistModel::setItems(const QStringList &items)
|
||||
@@ -35,38 +39,93 @@ void ChecklistModel::setItems(const QStringList &items)
|
||||
m_items.clear();
|
||||
|
||||
for (const auto &item : items)
|
||||
m_items.append(std::make_pair(item, true));
|
||||
m_items.append({ item, item, true });
|
||||
|
||||
emit endResetModel();
|
||||
}
|
||||
|
||||
void ChecklistModel::setItems(const QList<std::pair<QString, bool> > &items)
|
||||
{
|
||||
emit beginResetModel();
|
||||
|
||||
m_items = items;
|
||||
|
||||
emit endResetModel();
|
||||
}
|
||||
|
||||
QStringList ChecklistModel::enabledItems() const
|
||||
QStringList ChecklistModel::itemTexts() const
|
||||
{
|
||||
QStringList items;
|
||||
|
||||
for (const auto &pair : m_items)
|
||||
if (std::get<1>(pair))
|
||||
items.append(std::get<0>(pair));
|
||||
for (const auto &item : m_items)
|
||||
items.append(item.displayText);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
QStringList ChecklistModel::disabledItems() const
|
||||
QVariantList ChecklistModel::itemDatas() const
|
||||
{
|
||||
QVariantList items;
|
||||
|
||||
for (const auto &item : m_items)
|
||||
items.append(item.data);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
QList<ChecklistModel::ChecklistItem> ChecklistModel::enabledItems() const
|
||||
{
|
||||
QList<ChecklistModel::ChecklistItem> items;
|
||||
|
||||
for (const auto &item : m_items)
|
||||
if (item.checked)
|
||||
items.append(item);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
QList<ChecklistModel::ChecklistItem> ChecklistModel::disabledItems() const
|
||||
{
|
||||
QList<ChecklistModel::ChecklistItem> items;
|
||||
|
||||
for (const auto &item : m_items)
|
||||
if (!item.checked)
|
||||
items.append(item);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
QStringList ChecklistModel::enabledTexts() const
|
||||
{
|
||||
QStringList items;
|
||||
|
||||
for (const auto &pair : m_items)
|
||||
if (!std::get<1>(pair))
|
||||
items.append(std::get<0>(pair));
|
||||
for (const auto &item : m_items)
|
||||
if (item.checked)
|
||||
items.append(item.displayText);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
QStringList ChecklistModel::disabledTexts() const
|
||||
{
|
||||
QStringList items;
|
||||
|
||||
for (const auto &item : m_items)
|
||||
if (!item.checked)
|
||||
items.append(item.displayText);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
QVariantList ChecklistModel::enabledItemDatas() const
|
||||
{
|
||||
QVariantList items;
|
||||
|
||||
for (const auto &item : m_items)
|
||||
if (item.checked)
|
||||
items.append(item.data);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
QVariantList ChecklistModel::disabledItemDatas() const
|
||||
{
|
||||
QVariantList items;
|
||||
|
||||
for (const auto &item : m_items)
|
||||
if (!item.checked)
|
||||
items.append(item.data);
|
||||
|
||||
return items;
|
||||
}
|
||||
@@ -75,57 +134,38 @@ int ChecklistModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid())
|
||||
return 0;
|
||||
|
||||
return m_items.count();
|
||||
}
|
||||
|
||||
QModelIndex ChecklistModel::sibling(int row, int column, const QModelIndex &idx) const
|
||||
{
|
||||
if (!idx.isValid() || column != 0 || row >= m_items.count() || row < 0)
|
||||
return QModelIndex();
|
||||
return createIndex(row, 0);
|
||||
}
|
||||
|
||||
QMap<int, QVariant> ChecklistModel::itemData(const QModelIndex &index) const
|
||||
{
|
||||
if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid))
|
||||
return QMap<int, QVariant>{};
|
||||
const auto &item = m_items.at(index.row());
|
||||
return QMap<int, QVariant>{{
|
||||
std::make_pair<int>(Qt::DisplayRole, std::get<0>(item)),
|
||||
std::make_pair<int>(Qt::EditRole, std::get<0>(item)),
|
||||
std::make_pair<int>(Qt::CheckStateRole, std::get<1>(item) ? Qt::Checked : Qt::Unchecked)
|
||||
}};
|
||||
}
|
||||
|
||||
bool ChecklistModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
|
||||
{
|
||||
if (roles.isEmpty())
|
||||
return false;
|
||||
if (std::any_of(roles.keyBegin(), roles.keyEnd(), [](int role) { return role != Qt::CheckStateRole; }))
|
||||
return false;
|
||||
auto roleIter = roles.constFind(Qt::CheckStateRole);
|
||||
Q_ASSERT(roleIter != roles.constEnd());
|
||||
return setData(index, roleIter.value(), roleIter.key());
|
||||
}
|
||||
|
||||
QVariant ChecklistModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (index.row() < 0 || index.row() >= m_items.size())
|
||||
return QVariant();
|
||||
return {};
|
||||
|
||||
const auto &item = m_items.at(index.row());
|
||||
if (role == Qt::DisplayRole || role == Qt::EditRole)
|
||||
return std::get<0>(item);
|
||||
if (role == Qt::CheckStateRole)
|
||||
return std::get<1>(item) ? Qt::Checked : Qt::Unchecked;
|
||||
|
||||
switch (role)
|
||||
{
|
||||
case Qt::DisplayRole: return item.displayText;
|
||||
case Qt::EditRole: return item.data;
|
||||
case Qt::CheckStateRole: return item.checked ? Qt::Checked : Qt::Unchecked;
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
bool ChecklistModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
if (index.row() >= 0 && index.row() < m_items.size() && role == Qt::CheckStateRole)
|
||||
if (index.row() < 0 || index.row() >= m_items.size())
|
||||
return false;
|
||||
|
||||
auto &item = m_items[index.row()];
|
||||
|
||||
switch (role)
|
||||
{
|
||||
auto &item = m_items[index.row()];
|
||||
std::get<1>(item) = value.toBool();
|
||||
case Qt::CheckStateRole:
|
||||
item.checked = value.toBool();
|
||||
emit dataChanged(index, index, { Qt::CheckStateRole });
|
||||
return true;
|
||||
}
|
||||
@@ -137,5 +177,6 @@ Qt::ItemFlags ChecklistModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return QAbstractListModel::flags(index);
|
||||
|
||||
return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable;
|
||||
}
|
||||
+20
-11
@@ -3,31 +3,40 @@
|
||||
#include <QAbstractListModel>
|
||||
#include <QList>
|
||||
|
||||
#include <utility>
|
||||
|
||||
class Q_CORE_EXPORT ChecklistModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
struct ChecklistItem
|
||||
{
|
||||
QString displayText;
|
||||
QVariant data;
|
||||
bool checked;
|
||||
};
|
||||
|
||||
explicit ChecklistModel(QObject *parent = nullptr);
|
||||
explicit ChecklistModel(const QStringList &items, QObject *parent = nullptr);
|
||||
explicit ChecklistModel(const QList<std::pair<QString, bool> > &strings, QObject *parent = nullptr);
|
||||
explicit ChecklistModel(const QList<ChecklistItem> &strings, QObject *parent = nullptr);
|
||||
|
||||
QStringList items() const;
|
||||
const QList<ChecklistItem> &items() const;
|
||||
void setItems(const QList<ChecklistItem> &items);
|
||||
void setItems(const QStringList &items);
|
||||
void setItems(const QList<std::pair<QString, bool> > &items);
|
||||
QStringList enabledItems() const;
|
||||
QStringList disabledItems() const;
|
||||
|
||||
QStringList itemTexts() const;
|
||||
QVariantList itemDatas() const;
|
||||
QList<ChecklistItem> enabledItems() const;
|
||||
QList<ChecklistItem> disabledItems() const;
|
||||
QStringList enabledTexts() const;
|
||||
QStringList disabledTexts() const;
|
||||
QVariantList enabledItemDatas() const;
|
||||
QVariantList disabledItemDatas() const;
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
|
||||
QMap<int, QVariant> itemData(const QModelIndex &index) const override;
|
||||
bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
|
||||
private:
|
||||
QList<std::pair<QString, bool> > m_items;
|
||||
QList<ChecklistItem> m_items;
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
#include "logmodel.h"
|
||||
|
||||
LogModel::LogModel(QObject *parent) :
|
||||
QSqlQueryModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
QVariant LogModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (orientation == Qt::Vertical)
|
||||
return {};
|
||||
|
||||
return QSqlQueryModel::headerData(section, orientation, role);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <QSqlQueryModel>
|
||||
|
||||
class LogModel : public QSqlQueryModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit LogModel(QObject *parent = nullptr);
|
||||
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||
};
|
||||
@@ -0,0 +1,148 @@
|
||||
#include "progressmodel.h"
|
||||
|
||||
#include <QImage>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
ProgressModel::ProgressModel(QObject *parent) :
|
||||
QAbstractListModel(parent)
|
||||
{
|
||||
connect(&m_movieLoading, &QMovie::frameChanged, this, &ProgressModel::frameChanged);
|
||||
}
|
||||
|
||||
ProgressModel::ProgressModel(const QStringList &items, QObject *parent) :
|
||||
QAbstractListModel(parent)
|
||||
{
|
||||
connect(&m_movieLoading, &QMovie::frameChanged, this, &ProgressModel::frameChanged);
|
||||
|
||||
m_items.reserve(items.count());
|
||||
for (const auto &item : items)
|
||||
m_items.append({ item, Item::Status::None });
|
||||
}
|
||||
|
||||
ProgressModel::ProgressModel(const QVector<ProgressModel::Item> &items, QObject *parent) :
|
||||
QAbstractListModel(parent),
|
||||
m_items(items)
|
||||
{
|
||||
connect(&m_movieLoading, &QMovie::frameChanged, this, &ProgressModel::frameChanged);
|
||||
|
||||
if (anyLoading())
|
||||
m_movieLoading.start();
|
||||
}
|
||||
|
||||
const QVector<ProgressModel::Item> &ProgressModel::items() const
|
||||
{
|
||||
return m_items;
|
||||
}
|
||||
|
||||
void ProgressModel::setItems(const QStringList &items)
|
||||
{
|
||||
emit beginResetModel();
|
||||
m_items.clear();
|
||||
m_items.reserve(items.count());
|
||||
for (const auto &item : items)
|
||||
m_items.append({ item, Item::Status::None });
|
||||
emit endResetModel();
|
||||
|
||||
m_movieLoading.stop();
|
||||
}
|
||||
|
||||
void ProgressModel::setItems(const QVector<ProgressModel::Item> &items)
|
||||
{
|
||||
emit beginResetModel();
|
||||
m_items = items;
|
||||
emit endResetModel();
|
||||
|
||||
updateMovieStatus();
|
||||
}
|
||||
|
||||
void ProgressModel::setText(int row, const QString &text)
|
||||
{
|
||||
m_items[row].text = text;
|
||||
|
||||
const auto index = createIndex(row, 0);
|
||||
emit dataChanged(index, index, { Qt::DisplayRole, Qt::EditRole });
|
||||
}
|
||||
|
||||
void ProgressModel::setStatus(int row, ProgressModel::Item::Status status)
|
||||
{
|
||||
m_items[row].status = status;
|
||||
|
||||
const auto index = createIndex(row, 0);
|
||||
emit dataChanged(index, index, { Qt::DecorationRole });
|
||||
|
||||
if (status == Item::Status::Loading)
|
||||
m_movieLoading.start();
|
||||
else
|
||||
updateMovieStatus();
|
||||
}
|
||||
|
||||
void ProgressModel::clearItems()
|
||||
{
|
||||
emit beginResetModel();
|
||||
m_items.clear();
|
||||
emit endResetModel();
|
||||
|
||||
m_movieLoading.stop();
|
||||
}
|
||||
|
||||
int ProgressModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid())
|
||||
return 0;
|
||||
|
||||
return m_items.count();
|
||||
}
|
||||
|
||||
QVariant ProgressModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (index.row() < 0 || index.row() >= m_items.size())
|
||||
return {};
|
||||
|
||||
const auto &item = m_items.at(index.row());
|
||||
|
||||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
case Qt::EditRole:
|
||||
return item.text;
|
||||
case Qt::DecorationRole:
|
||||
switch (item.status)
|
||||
{
|
||||
case Item::Status::None:
|
||||
return m_pixmapEmpty;
|
||||
case Item::Status::Loading:
|
||||
return m_movieLoading.currentPixmap();
|
||||
case Item::Status::Succeeded:
|
||||
return m_pixmapSucceeded;
|
||||
case Item::Status::Failed:
|
||||
return m_pixmapFailed;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void ProgressModel::frameChanged()
|
||||
{
|
||||
for (auto iter = m_items.constBegin(); iter != m_items.constEnd(); iter++)
|
||||
{
|
||||
if (iter->status == Item::Status::Loading)
|
||||
{
|
||||
const auto index = createIndex(std::distance(m_items.constBegin(), iter), 0);
|
||||
emit dataChanged(index, index, { Qt::DecorationRole });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ProgressModel::anyLoading() const
|
||||
{
|
||||
return std::any_of(m_items.constBegin(), m_items.constEnd(), [](const auto &item){ return item.status == Item::Status::Loading; });
|
||||
}
|
||||
|
||||
void ProgressModel::updateMovieStatus()
|
||||
{
|
||||
if (anyLoading())
|
||||
m_movieLoading.start();
|
||||
else
|
||||
m_movieLoading.stop();
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QPixmap>
|
||||
#include <QMovie>
|
||||
|
||||
class ProgressModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
struct Item
|
||||
{
|
||||
enum class Status
|
||||
{
|
||||
None,
|
||||
Loading,
|
||||
Succeeded,
|
||||
Failed
|
||||
};
|
||||
|
||||
QString text;
|
||||
Status status;
|
||||
};
|
||||
|
||||
explicit ProgressModel(QObject *parent = nullptr);
|
||||
ProgressModel(const QStringList &items, QObject *parent = nullptr);
|
||||
ProgressModel(const QVector<Item> &items, QObject *parent = nullptr);
|
||||
|
||||
const QVector<Item> &items() const;
|
||||
void setItems(const QStringList &items);
|
||||
void setItems(const QVector<Item> &items);
|
||||
|
||||
void setText(int row, const QString &text);
|
||||
void setStatus(int row, Item::Status status);
|
||||
|
||||
void clearItems();
|
||||
|
||||
int rowCount(const QModelIndex &parent) const override;
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
|
||||
private slots:
|
||||
void frameChanged();
|
||||
|
||||
private:
|
||||
bool anyLoading() const;
|
||||
void updateMovieStatus();
|
||||
|
||||
QVector<Item> m_items;
|
||||
|
||||
const QPixmap m_pixmapSucceeded { ":/loganalyzer/succeeded.png" };
|
||||
const QPixmap m_pixmapFailed { ":/loganalyzer/failed.png" };
|
||||
const QPixmap m_pixmapEmpty { [this](){
|
||||
QPixmap pixmap(m_pixmapSucceeded.size());
|
||||
pixmap.fill();
|
||||
return pixmap;
|
||||
}() };
|
||||
QMovie m_movieLoading { ":/loganalyzer/loading.gif" };
|
||||
};
|
||||
@@ -1,14 +0,0 @@
|
||||
#include "sqlrelationaltablemodel.h"
|
||||
|
||||
SqlRelationalTableModel::SqlRelationalTableModel(QObject *parent, QSqlDatabase db) :
|
||||
QSqlRelationalTableModel(parent, db)
|
||||
{
|
||||
}
|
||||
|
||||
QVariant SqlRelationalTableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (orientation == Qt::Vertical)
|
||||
return {};
|
||||
|
||||
return QSqlRelationalTableModel::headerData(section, orientation, role);
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <QSqlRelationalTableModel>
|
||||
|
||||
class SqlRelationalTableModel : public QSqlRelationalTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SqlRelationalTableModel(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
|
||||
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||
};
|
||||
Reference in new issue
Block a user