2017-10-02 16:18:40 +00:00
|
|
|
#include "ImportsWidget.h"
|
|
|
|
#include "ui_ImportsWidget.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-10-01 19:09:42 +00:00
|
|
|
#include "MainWindow.h"
|
|
|
|
#include "utils/Helpers.h"
|
2017-04-13 15:14:02 +00:00
|
|
|
|
|
|
|
#include <QTreeWidget>
|
|
|
|
#include <QPen>
|
|
|
|
#include <QPainter>
|
|
|
|
|
2018-04-12 06:32:31 +00:00
|
|
|
ImportsModel::ImportsModel(QList<ImportDescription> *imports, QObject *parent) :
|
|
|
|
QAbstractTableModel(parent),
|
|
|
|
imports(imports)
|
|
|
|
{}
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-04-12 06:32:31 +00:00
|
|
|
int ImportsModel::rowCount(const QModelIndex &parent) const
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2018-09-30 20:00:53 +00:00
|
|
|
return parent.isValid() ? 0 : imports->count();
|
2018-04-12 06:32:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 20:00:53 +00:00
|
|
|
int ImportsModel::columnCount(const QModelIndex &) const
|
2018-04-12 06:32:31 +00:00
|
|
|
{
|
2018-04-23 07:53:35 +00:00
|
|
|
return ImportsModel::ColumnCount;
|
2018-04-12 06:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ImportsModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
const ImportDescription &import = imports->at(index.row());
|
2018-09-30 20:00:53 +00:00
|
|
|
switch (role) {
|
2018-04-12 06:32:31 +00:00
|
|
|
case Qt::ForegroundRole:
|
2018-04-23 07:53:35 +00:00
|
|
|
if (index.column() < ImportsModel::ColumnCount) {
|
|
|
|
if (banned.match(import.name).hasMatch())
|
2018-04-12 06:32:31 +00:00
|
|
|
return QColor(255, 129, 123);
|
2018-04-23 07:53:35 +00:00
|
|
|
}
|
2018-04-12 06:32:31 +00:00
|
|
|
break;
|
|
|
|
case Qt::DisplayRole:
|
2018-09-30 20:00:53 +00:00
|
|
|
switch (index.column()) {
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::AddressColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return RAddressString(import.plt);
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::TypeColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return import.type;
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::SafetyColumn:
|
2018-09-30 20:00:53 +00:00
|
|
|
return banned.match(import.name).hasMatch() ? tr("Unsafe") : QStringLiteral("");
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::NameColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return import.name;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::ImportDescriptionRole:
|
|
|
|
return QVariant::fromValue(import);
|
|
|
|
case ImportsModel::AddressRole:
|
|
|
|
return import.plt;
|
2018-04-12 06:32:31 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ImportsModel::headerData(int section, Qt::Orientation, int role) const
|
|
|
|
{
|
2018-09-30 20:00:53 +00:00
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
switch (section) {
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::AddressColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return tr("Address");
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::TypeColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return tr("Type");
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::SafetyColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return tr("Safety");
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::NameColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return tr("Name");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportsModel::beginReload()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportsModel::endReload()
|
|
|
|
{
|
|
|
|
endResetModel();
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 18:40:40 +00:00
|
|
|
ImportsProxyModel::ImportsProxyModel(ImportsModel *sourceModel, QObject *parent)
|
2018-04-23 07:53:35 +00:00
|
|
|
: QSortFilterProxyModel(parent)
|
|
|
|
{
|
|
|
|
setSourceModel(sourceModel);
|
|
|
|
setFilterCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
setSortCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:40:40 +00:00
|
|
|
bool ImportsProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
|
2018-04-23 07:53:35 +00:00
|
|
|
{
|
2018-04-24 18:40:40 +00:00
|
|
|
QModelIndex index = sourceModel()->index(row, 0, parent);
|
|
|
|
auto import = index.data(ImportsModel::ImportDescriptionRole).value<ImportDescription>();
|
2018-04-23 07:53:35 +00:00
|
|
|
|
|
|
|
return import.name.contains(filterRegExp());
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:40:40 +00:00
|
|
|
bool ImportsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
|
2018-04-23 07:53:35 +00:00
|
|
|
{
|
|
|
|
if (!left.isValid() || !right.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (left.parent().isValid() || right.parent().isValid())
|
|
|
|
return false;
|
|
|
|
|
2018-04-24 18:40:40 +00:00
|
|
|
auto leftImport = left.data(ImportsModel::ImportDescriptionRole).value<ImportDescription>();
|
|
|
|
auto rightImport = right.data(ImportsModel::ImportDescriptionRole).value<ImportDescription>();
|
2018-04-23 07:53:35 +00:00
|
|
|
|
|
|
|
switch (left.column()) {
|
|
|
|
case ImportsModel::AddressColumn:
|
|
|
|
return leftImport.plt < rightImport.plt;
|
|
|
|
case ImportsModel::TypeColumn:
|
|
|
|
return leftImport.type < rightImport.type;
|
|
|
|
case ImportsModel::SafetyColumn:
|
|
|
|
break;
|
|
|
|
case ImportsModel::NameColumn:
|
|
|
|
return leftImport.name < rightImport.name;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
/*
|
|
|
|
* Imports Widget
|
|
|
|
*/
|
|
|
|
|
2018-03-16 21:46:57 +00:00
|
|
|
ImportsWidget::ImportsWidget(MainWindow *main, QAction *action) :
|
|
|
|
CutterDockWidget(main, action),
|
2018-04-12 06:32:31 +00:00
|
|
|
ui(new Ui::ImportsWidget),
|
2018-04-23 07:53:35 +00:00
|
|
|
importsModel(new ImportsModel(&imports, this)),
|
2018-04-24 18:40:40 +00:00
|
|
|
importsProxyModel(new ImportsProxyModel(importsModel, this))
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2018-04-23 07:53:35 +00:00
|
|
|
ui->importsTreeView->setModel(importsProxyModel);
|
|
|
|
ui->importsTreeView->sortByColumn(ImportsModel::NameColumn, Qt::AscendingOrder);
|
|
|
|
|
|
|
|
// Ctrl-F to show/hide the filter entry
|
|
|
|
QShortcut *searchShortcut = new QShortcut(QKeySequence::Find, this);
|
|
|
|
connect(searchShortcut, &QShortcut::activated, ui->quickFilterView, &QuickFilterView::showFilter);
|
|
|
|
searchShortcut->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
|
|
|
|
// Esc to clear the filter entry
|
|
|
|
QShortcut *clearShortcut = new QShortcut(QKeySequence(Qt::Key_Escape), this);
|
|
|
|
connect(clearShortcut, &QShortcut::activated, ui->quickFilterView, &QuickFilterView::clearFilter);
|
|
|
|
clearShortcut->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
|
|
|
|
connect(ui->quickFilterView, SIGNAL(filterTextChanged(const QString &)),
|
|
|
|
importsProxyModel, SLOT(setFilterWildcard(const QString &)));
|
|
|
|
connect(ui->quickFilterView, SIGNAL(filterClosed()), ui->importsTreeView, SLOT(setFocus()));
|
2017-04-13 15:14:02 +00:00
|
|
|
|
|
|
|
setScrollMode();
|
|
|
|
|
2018-04-12 06:32:31 +00:00
|
|
|
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshImports()));
|
2017-04-13 15:14:02 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 12:56:10 +00:00
|
|
|
ImportsWidget::~ImportsWidget() {}
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-04-12 06:32:31 +00:00
|
|
|
void ImportsWidget::refreshImports()
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2018-04-23 07:53:35 +00:00
|
|
|
importsModel->beginReload();
|
2018-04-12 06:32:31 +00:00
|
|
|
imports = Core()->getAllImports();
|
2018-04-23 07:53:35 +00:00
|
|
|
importsModel->endReload();
|
2018-04-12 06:32:31 +00:00
|
|
|
qhelpers::adjustColumns(ui->importsTreeView, 4, 0);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 15:14:02 +00:00
|
|
|
void ImportsWidget::setScrollMode()
|
|
|
|
{
|
2018-04-12 06:32:31 +00:00
|
|
|
qhelpers::setVerticalScrollMode(ui->importsTreeView);
|
2017-04-13 15:14:02 +00:00
|
|
|
}
|
2017-05-19 07:45:26 +00:00
|
|
|
|
2018-04-12 06:32:31 +00:00
|
|
|
void ImportsWidget::on_importsTreeView_doubleClicked(const QModelIndex &index)
|
2017-05-19 07:45:26 +00:00
|
|
|
{
|
2018-04-23 07:54:06 +00:00
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
|
2018-04-12 06:32:31 +00:00
|
|
|
Core()->seek(index.data(ImportsModel::AddressRole).toLongLong());
|
2017-05-19 07:45:26 +00:00
|
|
|
}
|