cutter/src/widgets/ImportsWidget.cpp
Abdel-Rahman A e1f17db235 Refactor ImportsWidget to a view and a model (#438)
* Refactor ImportsWidget to a view and a model
* Remove unused delegate
2018-04-12 08:32:31 +02:00

128 lines
2.8 KiB
C++

#include "ImportsWidget.h"
#include "ui_ImportsWidget.h"
#include "MainWindow.h"
#include "utils/Helpers.h"
#include <QTreeWidget>
#include <QPen>
#include <QPainter>
ImportsModel::ImportsModel(QList<ImportDescription> *imports, QObject *parent) :
QAbstractTableModel(parent),
imports(imports)
{}
int ImportsModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid()? 0 : imports->count();
}
int ImportsModel::columnCount(const QModelIndex&) const
{
return COUNT;
}
QVariant ImportsModel::data(const QModelIndex &index, int role) const
{
const ImportDescription &import = imports->at(index.row());
switch(role)
{
case AddressRole:
return import.plt;
case Qt::ForegroundRole:
if(index.column() < COUNT)
if(banned.match(import.name).hasMatch())
return QColor(255, 129, 123);
break;
case Qt::DisplayRole:
switch(index.column())
{
case ADDRESS:
return RAddressString(import.plt);
case TYPE:
return import.type;
case SAFETY:
return banned.match(import.name).hasMatch()? tr("Unsafe") : QStringLiteral("");
case NAME:
return import.name;
default:
break;
}
break;
default:
break;
}
return QVariant();
}
QVariant ImportsModel::headerData(int section, Qt::Orientation, int role) const
{
if(role == Qt::DisplayRole)
{
switch(section)
{
case ADDRESS:
return tr("Address");
case TYPE:
return tr("Type");
case SAFETY:
return tr("Safety");
case NAME:
return tr("Name");
default:
break;
}
}
return QVariant();
}
void ImportsModel::beginReload()
{
beginResetModel();
}
void ImportsModel::endReload()
{
endResetModel();
}
/*
* Imports Widget
*/
ImportsWidget::ImportsWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
ui(new Ui::ImportsWidget),
model(new ImportsModel(&imports, this))
{
ui->setupUi(this);
ui->importsTreeView->setModel(model);
ui->importsTreeView->sortByColumn(3, Qt::AscendingOrder);
setScrollMode();
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshImports()));
}
ImportsWidget::~ImportsWidget() {}
void ImportsWidget::refreshImports()
{
model->beginReload();
imports = Core()->getAllImports();
model->endReload();
qhelpers::adjustColumns(ui->importsTreeView, 4, 0);
}
void ImportsWidget::setScrollMode()
{
qhelpers::setVerticalScrollMode(ui->importsTreeView);
}
void ImportsWidget::on_importsTreeView_doubleClicked(const QModelIndex &index)
{
Core()->seek(index.data(ImportsModel::AddressRole).toLongLong());
}