/** * @file PaiListDialog.cpp * @brief PAI系统默认列表对话框 * @date 2014-11-25 */ #include #include #include "PaiListDialog.h" #include "PaiPushButton.h" #include "PaiSearchLineEdit.h" #include "PaiListItemDelegate.h" using namespace pai::gui; PaiListDialog::PaiListDialog(QWidget *pParent) : PaiDialog(pParent) { setWindowTitle(tr("π-Frame")); // 搜索控件 m_pSearchLineEdit = new PaiSearchLineEdit(); m_pSearchLineEdit->setMaximumHeight(22); m_pSearchLineEdit->setMinimumHeight(22); // 内容列表 m_pListWgt = new QListView(this); m_pListWgt->setStyleSheet("QListView::item:hover{\ background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\ stop: 0 #F8FCFE, stop: 1 #E8F4FC);\ }"); m_pListWgt->setItemDelegate(new PaiListItemDelegate(m_pListWgt)); // 设置数据模型 QStringListModel *pModel = new QStringListModel(m_pListWgt); m_pListWgt->setModel(pModel); // ok按钮 m_pOkPBtn = new PaiPushButton(tr("OK"), this); m_pOkPBtn->setEnabled(false); // cancel按钮 m_pCancelPBtn = new PaiPushButton(tr("Cancel"), this); QHBoxLayout *pEditLayout = new QHBoxLayout(); pEditLayout->addSpacing(3); pEditLayout->addWidget(m_pSearchLineEdit); pEditLayout->addSpacing(65); QHBoxLayout *pPBtnHLayout = new QHBoxLayout; pPBtnHLayout->addStretch(); pPBtnHLayout->setContentsMargins(5, 5, 5, 5); pPBtnHLayout->setSpacing(10); pPBtnHLayout->addWidget(m_pOkPBtn); pPBtnHLayout->addWidget(m_pCancelPBtn); QVBoxLayout *pLstWgtVLayout = new QVBoxLayout(); pLstWgtVLayout->setSpacing(0); pLstWgtVLayout->setContentsMargins(0,0, 0, 0); pLstWgtVLayout->addSpacing(3); pLstWgtVLayout->addLayout(pEditLayout); pLstWgtVLayout->addSpacing(3); pLstWgtVLayout->addWidget(m_pListWgt); pLstWgtVLayout->addLayout(pPBtnHLayout); setLayout(pLstWgtVLayout); m_pListWgt->setEditTriggers(QAbstractItemView::NoEditTriggers); m_pListWgt->setSelectionBehavior(QAbstractItemView::SelectRows); connect(m_pOkPBtn, SIGNAL(clicked()), this, SLOT(OkButtonClicked())); connect(m_pCancelPBtn, SIGNAL(clicked()), this, SLOT(close())); connect(m_pListWgt, SIGNAL(doubleClicked (const QModelIndex &)), this, SLOT(ItemDoubleClicked(const QModelIndex &))); connect(m_pSearchLineEdit, SIGNAL(EditingStoped(const QString&)), this, SLOT(OnSearchData(const QString&))); connect(m_pListWgt->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(EnableOkButton(const QModelIndex &, const QModelIndex &))); } PaiListDialog::~PaiListDialog() { } void PaiListDialog::EnableOkButton(const QModelIndex & current, const QModelIndex & previous) { m_pOkPBtn->setEnabled(previous.isValid() && current.isValid()); } void PaiListDialog::ItemDoubleClicked(const QModelIndex & index) { if(index.isValid()) { emit SelectedAccepted(index.data().toString()); close(); } } void PaiListDialog::SetTextList(const QStringList & textList) { if(m_pListWgt && m_pListWgt->model()) { QStringListModel *pModel = dynamic_cast(m_pListWgt->model()); if(pModel) { pModel->removeRows(0,pModel->rowCount()); pModel->setStringList(textList); } } } bool PaiListDialog::Filter(const QString & keyword, int row) { bool pass = false; if(m_pListWgt && m_pListWgt->model()) { if(keyword.isEmpty()) { pass = true; } else if(keyword.contains('*') || keyword.contains('?')) { QRegExp rx(keyword, Qt::CaseInsensitive, QRegExp::Wildcard); QModelIndex index = m_pListWgt->model()->index(row,0); if(rx.exactMatch(index.data().toString())) { pass = true; } } else { QModelIndex index = m_pListWgt->model()->index(row,0); if(index.data().toString().contains(keyword, Qt::CaseInsensitive)) { pass = true; } } } return pass; } void PaiListDialog::OnSearchData(const QString & keyword) { if(m_pListWgt && m_pListWgt->model()) { for(int rowIndex = 0; rowIndex < m_pListWgt->model()->rowCount(); ++rowIndex) { bool pass = Filter(keyword,rowIndex); // 只显示过滤的结果 m_pListWgt->setRowHidden(rowIndex, !pass); } } } void PaiListDialog::OkButtonClicked() { if(m_pListWgt) { emit SelectedAccepted(m_pListWgt->currentIndex().data().toString()); close(); } }