cutter/src/widgets/TypesWidget.cpp

142 lines
3.5 KiB
C++
Raw Normal View History

2018-03-06 17:21:48 +00:00
#include "TypesWidget.h"
#include "ui_TypesWidget.h"
#include "MainWindow.h"
#include "utils/Helpers.h"
TypesModel::TypesModel(QList<TypeDescription> *types, QObject *parent)
: QAbstractListModel(parent),
types(types)
{
}
int TypesModel::rowCount(const QModelIndex &) const
{
return types->count();
}
int TypesModel::columnCount(const QModelIndex &) const
{
return Columns::COUNT;
}
QVariant TypesModel::data(const QModelIndex &index, int role) const
{
if (index.row() >= types->count())
return QVariant();
const TypeDescription &exp = types->at(index.row());
2018-03-21 20:32:32 +00:00
switch (role) {
2018-03-06 17:21:48 +00:00
case Qt::DisplayRole:
2018-03-21 20:32:32 +00:00
switch (index.column()) {
2018-03-06 17:21:48 +00:00
case TYPE:
return exp.type;
case SIZE:
return exp.size;
case FORMAT:
return exp.format;
default:
return QVariant();
}
case TypeDescriptionRole:
return QVariant::fromValue(exp);
default:
return QVariant();
}
}
QVariant TypesModel::headerData(int section, Qt::Orientation, int role) const
{
2018-03-21 20:32:32 +00:00
switch (role) {
2018-03-06 17:21:48 +00:00
case Qt::DisplayRole:
2018-03-21 20:32:32 +00:00
switch (section) {
2018-03-06 17:21:48 +00:00
case TYPE:
return tr("Type");
case SIZE:
return tr("Size");
case FORMAT:
return tr("Format");
default:
return QVariant();
}
default:
return QVariant();
}
}
TypesSortFilterProxyModel::TypesSortFilterProxyModel(TypesModel *source_model, QObject *parent)
: QSortFilterProxyModel(parent)
{
setSourceModel(source_model);
}
bool TypesSortFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
{
QModelIndex index = sourceModel()->index(row, 0, parent);
TypeDescription exp = index.data(TypesModel::TypeDescriptionRole).value<TypeDescription>();
return exp.type.contains(filterRegExp());
}
bool TypesSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
TypeDescription left_exp = left.data(TypesModel::TypeDescriptionRole).value<TypeDescription>();
TypeDescription right_exp = right.data(TypesModel::TypeDescriptionRole).value<TypeDescription>();
2018-03-21 20:32:32 +00:00
switch (left.column()) {
2018-03-06 17:21:48 +00:00
case TypesModel::TYPE:
return left_exp.type < right_exp.type;
case TypesModel::SIZE:
return left_exp.size < right_exp.size;
case TypesModel::FORMAT:
return left_exp.format < right_exp.format;
default:
break;
}
return left_exp.size < right_exp.size;
}
TypesWidget::TypesWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
ui(new Ui::TypesWidget)
2018-03-06 17:21:48 +00:00
{
ui->setupUi(this);
types_model = new TypesModel(&types, this);
types_proxy_model = new TypesSortFilterProxyModel(types_model, this);
ui->typesTreeView->setModel(types_proxy_model);
ui->typesTreeView->sortByColumn(TypesModel::TYPE, Qt::AscendingOrder);
setScrollMode();
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshTypes()));
}
TypesWidget::~TypesWidget() {}
void TypesWidget::refreshTypes()
{
types_model->beginResetModel();
2018-04-12 06:33:30 +00:00
types = Core()->getAllTypes();
types_model->endResetModel();
2018-03-06 17:21:48 +00:00
qhelpers::adjustColumns(ui->typesTreeView, 3, 0);
2018-03-06 17:21:48 +00:00
}
void TypesWidget::setScrollMode()
{
qhelpers::setVerticalScrollMode(ui->typesTreeView);
}
void TypesWidget::on_typesTreeView_doubleClicked(const QModelIndex &index)
{
if (!index.isValid())
return;
2018-03-06 17:21:48 +00:00
// TypeDescription exp = index.data(TypesModel::TypeDescriptionRole).value<TypeDescription>();
2018-04-12 06:33:30 +00:00
// Core()->seek(exp.vaddr);
2018-03-06 17:21:48 +00:00
}