213 lines
4.6 KiB
C++
Executable File
213 lines
4.6 KiB
C++
Executable File
|
|
|
|
|
|
#include "lts_selection_popup.h"
|
|
#include "ui_lts_selection_popup.h"
|
|
#include "lts_base.h"
|
|
|
|
#include <QSortFilterProxyModel>
|
|
#include <QAbstractItemModel>
|
|
#include <QRadioButton>
|
|
#include <QScrollBar>
|
|
|
|
|
|
#ifdef NEVER
|
|
# include <QDebug>
|
|
#endif
|
|
|
|
|
|
namespace LtsWidgets
|
|
{
|
|
|
|
class RadioButton : public QRadioButton
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
explicit RadioButton(int row, const QString &text)
|
|
: QRadioButton(text)
|
|
, m_row(row)
|
|
{}
|
|
|
|
int row() const { return m_row; }
|
|
|
|
signals:
|
|
void doubleClickEvent();
|
|
|
|
private:
|
|
int m_row;
|
|
|
|
void mouseDoubleClickEvent(QMouseEvent *) override
|
|
{
|
|
emit doubleClickEvent();
|
|
}
|
|
};
|
|
|
|
|
|
|
|
LtsSelectionPopup::LtsSelectionPopup(QAbstractItemModel* model,
|
|
int currentIndex,
|
|
QWidget *parent)
|
|
: LtsFramelessDialog(parent, Behaviour::Popup)
|
|
, m_ui(new Ui::LtsSelectionPopup)
|
|
, m_currentIndex(currentIndex)
|
|
, m_firstIndex(currentIndex)
|
|
, m_filter(new QSortFilterProxyModel(this))
|
|
, m_valid(false)
|
|
{
|
|
m_ui->setupUi(this);
|
|
|
|
m_filter->setSourceModel(model);
|
|
m_filter->setFilterCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
slotFilterChanged();
|
|
#ifdef TABLET_MODE
|
|
m_ui->leSearch->setVisible(false);
|
|
m_ui->label->setVisible(false);
|
|
#endif
|
|
connect(m_ui->pbCancel, &QPushButton::clicked, this, &LtsSelectionPopup::reject);
|
|
connect(m_ui->pbSelect, &QPushButton::clicked, this, &LtsSelectionPopup::accept);
|
|
connect(m_ui->leSearch, &QLineEdit::textEdited,
|
|
this, &LtsSelectionPopup::slotFilterChanged);
|
|
|
|
#ifdef NEVER
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
connect(this, static_cast<void (LtsSelectionPopup::*)(int)>(&LtsSelectionPopup::currentIndexChanged), this, [](int idx){
|
|
qCritical() << " LtsSelectionPopup: called: currentIndexChanged(int), idx=" << idx;
|
|
});
|
|
|
|
|
|
connect(this, static_cast<void (LtsSelectionPopup::*)(int)>(&LtsSelectionPopup::activated), this, [](int idx){
|
|
qCritical() << " LtsSelectionPopup: called: activated(int), idx=" << idx;
|
|
});
|
|
|
|
#endif
|
|
}
|
|
|
|
|
|
LtsSelectionPopup::~LtsSelectionPopup()
|
|
{
|
|
delete m_ui;
|
|
}
|
|
|
|
|
|
void LtsSelectionPopup::slotFilterChanged()
|
|
{
|
|
m_filter->setFilterWildcard(m_ui->leSearch->text().count() ? m_ui->leSearch->text() : QStringLiteral("*") );
|
|
refresh();
|
|
}
|
|
|
|
|
|
void LtsSelectionPopup::refresh()
|
|
{
|
|
m_valid = false;
|
|
qDeleteAll(m_ui->itemLayoutWidget->findChildren<RadioButton*>());
|
|
QLayoutItem *tem = nullptr;
|
|
if (m_ui->itemLayoutWidget->layout()->count() > 0)
|
|
{
|
|
tem = m_ui->itemLayoutWidget->layout()->takeAt(0);
|
|
}
|
|
|
|
int count = m_filter->rowCount();
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
int realRow = m_filter->mapToSource(m_filter->index(i, 0)).row();
|
|
|
|
RadioButton *rbItem = new RadioButton(realRow, m_filter->data(m_filter->index(i, 0)).toString());
|
|
int disable = m_filter->data(m_filter->index(i, 0), Qt::UserRole-1).toInt();
|
|
if (disable == 1)
|
|
rbItem->setEnabled(false);
|
|
m_ui->itemLayoutWidget->layout()->addWidget(rbItem);
|
|
|
|
if (realRow == m_currentIndex)
|
|
{
|
|
rbItem->setChecked(true);
|
|
m_valid = true;
|
|
}
|
|
|
|
connect(rbItem, &RadioButton::doubleClickEvent,
|
|
this, &LtsSelectionPopup::accept);
|
|
connect(rbItem, &RadioButton::toggled,
|
|
this, [this](bool checked)
|
|
{
|
|
if (checked)
|
|
m_ui->pbSelect->setEnabled(true);
|
|
});
|
|
}
|
|
if (tem)
|
|
m_ui->itemLayoutWidget->layout()->addItem(tem);
|
|
|
|
if (count == 1)
|
|
m_valid = true;
|
|
|
|
m_ui->pbSelect->setEnabled(m_valid);
|
|
}
|
|
|
|
|
|
void LtsSelectionPopup::showEvent(QShowEvent *event)
|
|
{
|
|
LtsFramelessDialog::showEvent(event);
|
|
m_ui->leSearch->setFocus();
|
|
|
|
resize(sizeHint().width() + m_ui->scrollArea->horizontalScrollBar()->height() , sizeHint().height());
|
|
}
|
|
|
|
|
|
void LtsSelectionPopup::selectItem(int row, bool bCurrentIndexChanged, bool bActivated)
|
|
{
|
|
|
|
|
|
if (bCurrentIndexChanged)
|
|
emit currentIndexChanged(row);
|
|
|
|
if (bActivated)
|
|
emit activated(row);
|
|
|
|
m_ui->pbSelect->setEnabled(true);
|
|
|
|
LtsFramelessDialog::accept();
|
|
}
|
|
|
|
|
|
void LtsSelectionPopup::accept()
|
|
{
|
|
const QList<RadioButton *> items = m_ui->itemLayoutWidget->findChildren<RadioButton *>();
|
|
|
|
int count = items.count();
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
const RadioButton *rb = items.at(i);
|
|
|
|
if (rb->isChecked())
|
|
{
|
|
int realRow = m_filter->mapToSource(m_filter->index(i, 0)).row();
|
|
|
|
bool bCurrentIndexChanged = realRow != m_firstIndex;
|
|
bool bActivated = true;
|
|
selectItem(realRow, bCurrentIndexChanged, bActivated);
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
if (count == 1)
|
|
{
|
|
const RadioButton *rb = items.at(0);
|
|
selectItem(rb->row(), true, true);
|
|
return;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#include "lts_selection_popup.moc"
|