Initial commit
This commit is contained in:
commit
b7d90b9afe
16245 files changed
+3396666
No files matched your search
+152
@@ -0,0 +1,152 @@
|
||||
#include "lts_data_view.h"
|
||||
#include "tree_view_delegate.h"
|
||||
#include "search_model.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QComboBox>
|
||||
#include <QTimer>
|
||||
//#include <QDebug>
|
||||
|
||||
namespace LtsDataView
|
||||
{
|
||||
TreeViewDelegate::ComboBoxData TreeViewDelegate::comboBoxData(const QModelIndex &index)
|
||||
{
|
||||
const SearchModel *sm = qobject_cast<const SearchModel *>(index.model());
|
||||
if (!sm)
|
||||
return {};
|
||||
const QAbstractItemModel *aim = sm->sourceModel();
|
||||
if (!aim)
|
||||
return {};
|
||||
const QVariant val = aim->headerData(index.column(), Qt::Horizontal, LtsWidgets::LtsColumnFilterProxyModel::ComboboxFilterRole);
|
||||
return LtsWidgets::ComboboxFilterContent::fromVariant(val);
|
||||
}
|
||||
|
||||
|
||||
void TreeViewDelegate::paint(QPainter *painter,
|
||||
const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
const ComboBoxData cbd = comboBoxData(index);
|
||||
if (!cbd.isValid())
|
||||
return QStyledItemDelegate::paint(painter, option, index);
|
||||
|
||||
const QWidget *const widget = option.widget;
|
||||
QStyle* const style = widget ? widget->style() : QApplication::style();
|
||||
QStyleOptionComboBox cbOption;
|
||||
cbOption.rect = option.rect;
|
||||
cbOption.direction = option.direction;
|
||||
const auto currentData = index.data(LtsWidgets::LtsColumnFilterProxyModel::ComboboxFilterRole);
|
||||
for (const auto &content : cbd.data)
|
||||
{
|
||||
if (content.data == currentData)
|
||||
{
|
||||
cbOption.currentIcon = content.icon;
|
||||
break;
|
||||
}
|
||||
}
|
||||
cbOption.currentText = index.data(Qt::DisplayRole).toString();
|
||||
cbOption.fontMetrics = option.fontMetrics;
|
||||
const int iconWidth = style->pixelMetric(QStyle::PM_SmallIconSize, nullptr, widget);
|
||||
cbOption.iconSize = QSize(iconWidth, iconWidth);
|
||||
cbOption.palette = option.palette;
|
||||
cbOption.state = option.state;
|
||||
cbOption.styleObject = option.styleObject;
|
||||
style->drawComplexControl(QStyle::CC_ComboBox, &cbOption, painter, widget);
|
||||
style->drawControl(QStyle::CE_ComboBoxLabel, &cbOption, painter, widget);
|
||||
}
|
||||
|
||||
|
||||
QWidget *TreeViewDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
Q_UNUSED(option);
|
||||
|
||||
const ComboBoxData cbd = comboBoxData(index);
|
||||
if (cbd.isValid())
|
||||
{
|
||||
QComboBox *cbx = new QComboBox(parent);
|
||||
for (const auto &content : cbd.data)
|
||||
cbx->addItem(content.icon, content.text, content.data);
|
||||
if (cbd.sortItems)
|
||||
cbx->model()->sort(0);
|
||||
cbx->insertItem(0, noFilterString());
|
||||
connect(cbx, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
this, &TreeViewDelegate::slotFinished);
|
||||
QTimer::singleShot(0, cbx, &QComboBox::showPopup);
|
||||
return cbx;
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef TABLET_MODE
|
||||
//qWarning() << "TABLET MODE !!!!!!!!";
|
||||
// no line edit as it crashes
|
||||
return nullptr;
|
||||
#else
|
||||
//qWarning() << "****NO**** TABLET MODE !!!!!!!!!!!!!!!!";
|
||||
SearchLineEdit *edit = new SearchLineEdit(parent);
|
||||
edit->setFrame(false);
|
||||
connect(edit, &SearchLineEdit::editingFinished,
|
||||
this, &TreeViewDelegate::slotFinished);
|
||||
return edit;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void TreeViewDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
const ComboBoxData cbd = comboBoxData(index);
|
||||
if (cbd.isValid())
|
||||
{
|
||||
QComboBox *cbx = qobject_cast<QComboBox *>(editor);
|
||||
if (cbx)
|
||||
{
|
||||
// save the current data since it may change within setData() due to dataChanged() signals
|
||||
const QString curTxt = cbx->currentText();
|
||||
const QVariant curData = cbx->currentData();
|
||||
model->setData(index, curData, LtsWidgets::LtsColumnFilterProxyModel::ComboboxFilterRole);
|
||||
model->setData(index, curTxt, Qt::DisplayRole);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SearchLineEdit *edit = qobject_cast<SearchLineEdit *>(editor);
|
||||
if (edit)
|
||||
{
|
||||
model->setData(index, edit->text(), Qt::DisplayRole);
|
||||
model->setData(index, edit->text(), Qt::EditRole);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TreeViewDelegate::slotFinished()
|
||||
{
|
||||
QWidget *w = qobject_cast<QWidget *>(sender());
|
||||
if (!w)
|
||||
return;
|
||||
|
||||
emit commitData(w);
|
||||
emit closeEditor(w);
|
||||
}
|
||||
|
||||
void TreeViewDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
||||
{
|
||||
const ComboBoxData cbd = comboBoxData(index);
|
||||
if (cbd.isValid())
|
||||
{
|
||||
QComboBox *cbx = qobject_cast<QComboBox *>(editor);
|
||||
if (cbx)
|
||||
{
|
||||
const auto currentData = index.data(LtsWidgets::LtsColumnFilterProxyModel::ComboboxFilterRole);
|
||||
cbx->setCurrentIndex(cbx->findData(currentData));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SearchLineEdit *edit = qobject_cast<SearchLineEdit *>(editor);
|
||||
if (edit)
|
||||
edit->setText(index.data().toString());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace LtsDataView
|
||||
Reference in new issue
Block a user