157 lines
4.1 KiB
C++
Executable File
157 lines
4.1 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* Search Model for TableViews (has the ability to give the user a row for searching data)
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by Mike Wille, SLSE, 2016-03-14
|
|
*/
|
|
|
|
#include "search_model.h"
|
|
|
|
#include <QFont>
|
|
#include <QGuiApplication>
|
|
#include <QIcon>
|
|
#include <QPalette>
|
|
#include <LtsWidgets/lts_column_filter_proxy_model.h>
|
|
|
|
namespace LtsDataView
|
|
{
|
|
SearchModel::SearchModel(QObject *parent)
|
|
: QAbstractItemModel(parent)
|
|
, m_searchIcon(QIcon(QStringLiteral("icon:show.svg")))
|
|
{
|
|
}
|
|
|
|
int SearchModel::columnCount(const QModelIndex &parent) const
|
|
{
|
|
if (parent.isValid()) return 0;
|
|
if (!m_sourceModel) return 0;
|
|
// no parent here - the given model index has nothing to do with the source model
|
|
return m_sourceModel->columnCount(QModelIndex());
|
|
}
|
|
|
|
|
|
int SearchModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
if (parent.isValid()) return 0;
|
|
if (!m_sourceModel) return 0;
|
|
return 1; // the search row
|
|
}
|
|
|
|
|
|
QVariant SearchModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (!index.isValid())
|
|
return QVariant();
|
|
|
|
switch (role)
|
|
{
|
|
case Qt::DisplayRole: // Text
|
|
return m_displayData.value(index.column());
|
|
case Qt::EditRole: // Editor Text
|
|
return m_columnFilters.value(index.column());
|
|
case Qt::TextColorRole: // Text-Color
|
|
if (!m_columnFilters.contains(index.column()))
|
|
return QGuiApplication::palette().color(QPalette::Disabled, QPalette::ButtonText);
|
|
break;
|
|
case Qt::DecorationRole:
|
|
return m_hideDecorationColumns.contains(index.column()) ? QVariant() : m_searchIcon;
|
|
case Qt::BackgroundRole:
|
|
return QGuiApplication::palette().color(QPalette::Disabled, QPalette::AlternateBase);
|
|
case LtsWidgets::LtsColumnFilterProxyModel::ComboboxFilterRole:
|
|
return m_comboBoxData.value(index.column());
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return QVariant();
|
|
}
|
|
|
|
|
|
QVariant SearchModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
{
|
|
if (orientation == Qt::Horizontal && role == Qt::FontRole)
|
|
{
|
|
QFont boldFont;
|
|
boldFont.setBold(true);
|
|
return boldFont;
|
|
}
|
|
|
|
return m_sourceModel ? m_sourceModel->headerData(section, orientation, role) : QVariant();
|
|
}
|
|
|
|
|
|
bool SearchModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
{
|
|
if (!m_sourceModel || !index.isValid())
|
|
return false;
|
|
|
|
if (role == Qt::EditRole) // add / replace column filter when edited and in search row (row 0)
|
|
{
|
|
if (value.isNull())
|
|
m_columnFilters.remove(index.column());
|
|
else
|
|
m_columnFilters[index.column()] = value;
|
|
emit stringFilterForColumnChanged(value.toString(), index.column());
|
|
emit signalFilterChanged(); // must be called at last
|
|
emit dataChanged(index, index, {role});
|
|
return true; // done! (report a change, so that text keeps showing on search row)
|
|
}
|
|
if (role == Qt::DisplayRole)
|
|
{
|
|
if (value.isNull())
|
|
m_displayData.remove(index.column());
|
|
else
|
|
m_displayData[index.column()] = value;
|
|
emit dataChanged(index, index, {role});
|
|
return true;
|
|
}
|
|
if (role == LtsWidgets::LtsColumnFilterProxyModel::ComboboxFilterRole)
|
|
{
|
|
if (value.isNull())
|
|
m_comboBoxData.remove(index.column());
|
|
else
|
|
m_comboBoxData[index.column()] = value;
|
|
emit dataFilterForColumnChanged(value, index.column());
|
|
emit signalFilterChanged(); // must be called at last
|
|
emit dataChanged(index, index, {role});
|
|
return true;
|
|
}
|
|
if (role == Qt::DecorationRole)
|
|
{
|
|
if (value.isNull())
|
|
m_hideDecorationColumns.remove(index.column());
|
|
else
|
|
m_hideDecorationColumns.insert(index.column());
|
|
emit dataChanged(index, index, {role});
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
void SearchModel::setSourceModel(QAbstractItemModel* sourceModel)
|
|
{
|
|
m_sourceModel = sourceModel;
|
|
m_columnFilters.clear();
|
|
|
|
emit dataChanged(QModelIndex(), QModelIndex()); // update view
|
|
}
|
|
|
|
|
|
Qt::ItemFlags SearchModel::flags(const QModelIndex &index) const
|
|
{
|
|
Q_UNUSED(index);
|
|
return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable;
|
|
}
|
|
|
|
|
|
QModelIndex SearchModel::index(int row, int column, const QModelIndex &parent) const
|
|
{
|
|
if (parent.isValid())
|
|
return QModelIndex();
|
|
return createIndex(row, column);
|
|
}
|
|
} // namespace LtsDataView
|