#include "nmWxSelectWellsWidget.h" #include #include #include #include #include #include #include #include #include #include #include nmWxSelectWellsWidget::nmWxSelectWellsWidget(QWidget* parent) : QWidget(parent), m_pListWidget(new QListWidget(this)), m_pSelectedWellsEdit(new QTextEdit(this)), m_nLastClickedIndex(-1) { // 两列等比例伸展;窗口纵向拉高时,列表和摘要共同占满内容区。 QGridLayout* pLayout = new QGridLayout(this); pLayout->setContentsMargins(0, 0, 0, 0); pLayout->setHorizontalSpacing(10); pLayout->setVerticalSpacing(6); pLayout->setColumnStretch(0, 1); pLayout->setColumnStretch(1, 1); pLayout->setRowStretch(1, 1); pLayout->addWidget(new QLabel(tr("All wells"), this), 0, 0); pLayout->addWidget(m_pListWidget, 1, 0); pLayout->addWidget(new QLabel(tr("Selected wells"), this), 0, 1); pLayout->addWidget(m_pSelectedWellsEdit, 1, 1); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_pListWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_pSelectedWellsEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_pListWidget->setResizeMode(QListView::Adjust); m_pListWidget->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel); m_pListWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); m_pSelectedWellsEdit->setReadOnly(true); // 长名称保持一口井一行,缩窄窗口时通过水平滚动查看,摘要仍可选中复制。 m_pSelectedWellsEdit->setLineWrapMode(QTextEdit::NoWrap); // 保留原快捷键及其作用范围,不增加额外的筛选或选择功能。 QShortcut* pSelectAllShortcut = new QShortcut(QKeySequence::SelectAll, this); pSelectAllShortcut->setContext(Qt::WidgetWithChildrenShortcut); connect(pSelectAllShortcut, SIGNAL(activated()), this, SLOT(onSelectAllItems())); QShortcut* pClearSelectionShortcut = new QShortcut( QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_A), this); pClearSelectionShortcut->setContext(Qt::WidgetWithChildrenShortcut); connect(pClearSelectionShortcut, SIGNAL(activated()), this, SLOT(onClearSelectedItems())); } QSize nmWxSelectWellsWidget::sizeHint() const { // 初始高度预留约十行井;只提供建议值,不限制用户缩放,也不随井数无限增高。 const QFontMetrics metrics = fontMetrics(); const int nRowHeight = m_pListWidget->count() > 0 ? m_pListWidget->sizeHintForRow(0) : metrics.height() + 6; const int nColumnWidth = metrics.height() * 14; return QWidget::sizeHint().expandedTo(QSize( nColumnWidth * 2 + 10, nRowHeight * 10 + metrics.height() + 6)); } void nmWxSelectWellsWidget::addItems(QStringList items, QList values) { if(items.count() != values.count()) { return; } for(int i = 0; i < items.count(); i++) { this->addItem(items[i], values[i]); } this->onUpdateSelections(); } void nmWxSelectWellsWidget::addItem(QString name, QVariant value) { m_listWellNames.append(name); m_listWellValues.append(value); nmWxSelectWellItemWidget* pItemWidget = new nmWxSelectWellItemWidget(m_pListWidget->viewport(), name); if (m_listSelectedValues.contains(value) || m_listSelectedNames.contains(name)) { pItemWidget->setSelected(true); } // 按实际字体、复选框和布局边距计算行尺寸,不再固定为 200×25 像素。 // 窗口更宽时行控件随列表伸展,内容超宽时由列表提供水平滚动条。 pItemWidget->ensurePolished(); QListWidgetItem* pItem = new QListWidgetItem; pItem->setSizeHint(pItemWidget->sizeHint()); pItem->setToolTip(name); m_pListWidget->addItem(pItem); m_pListWidget->setItemWidget(pItem, pItemWidget); connect(pItemWidget, SIGNAL(sigItemClicked()), this, SLOT(onUpdateSelections())); } QStringList nmWxSelectWellsWidget::getItems() { QStringList items = m_pSelectedWellsEdit->toPlainText().split("\n"); items.removeAll(QString()); return items; } void nmWxSelectWellsWidget::setSelectedItems(QStringList selectItems) { m_listSelectedNames = selectItems; } QList nmWxSelectWellsWidget::getSelectedValues() const { QList listSelectedValues; // 选择结果按左侧列表顺序返回,调用方只使用业务值,不再反查显示名称。 int nIndex = 0; const int nItemCount = qMin(m_pListWidget->count(), m_listWellValues.count()); while(nIndex < nItemCount) { QListWidgetItem* pListItem = m_pListWidget->item(nIndex); nmWxSelectWellItemWidget* pItemWidget = static_cast( m_pListWidget->itemWidget(pListItem)); if(pItemWidget != NULL && pItemWidget->getSelected()) { listSelectedValues.append(m_listWellValues[nIndex]); } ++nIndex; } return listSelectedValues; } void nmWxSelectWellsWidget::setSelectedValues( const QList& selectedValues) { m_listSelectedValues = selectedValues; } void nmWxSelectWellsWidget::onUpdateSelections() { // 如果由井项点击触发,则记录点击位置并处理Shift范围选择 nmWxSelectWellItemWidget* clickedItemWidget = qobject_cast(sender()); if (clickedItemWidget) { const int clickedIndex = this->indexOfItemWidget(clickedItemWidget); if (clickedIndex >= 0) { const bool shiftPressed = QApplication::keyboardModifiers() & Qt::ShiftModifier; if (shiftPressed && m_nLastClickedIndex >= 0 && m_nLastClickedIndex != clickedIndex) { // Shift+点击:将上次点击项到当前项之间的井批量设置为当前选择状态 this->setRangeItemsSelected(m_nLastClickedIndex, clickedIndex, clickedItemWidget->getSelected()); } m_nLastClickedIndex = clickedIndex; } } QStringList nameList; QList listSelectedValues; // 遍历所有的itemWidget,获取所有被选中的信息 int nIndex = 0; while(nIndex < m_pListWidget->count()) { QListWidgetItem* pListItem = m_pListWidget->item(nIndex); nmWxSelectWellItemWidget* pItemWidget = static_cast( m_pListWidget->itemWidget(pListItem)); if(pItemWidget != NULL && pItemWidget->getSelected()) { nameList.append(m_listWellNames[nIndex]); listSelectedValues.append(m_listWellValues[nIndex]); } ++nIndex; } m_listSelectedNames = nameList; m_listSelectedValues = listSelectedValues; m_pSelectedWellsEdit->setPlainText(nameList.join("\n")); } // 查找井项在列表中的位置 int nmWxSelectWellsWidget::indexOfItemWidget(nmWxSelectWellItemWidget* itemWidget) const { for (int i = 0; i < m_pListWidget->count(); i++) { if (m_pListWidget->itemWidget(m_pListWidget->item(i)) == itemWidget) { return i; } } return -1; } // 批量设置所有井的选中状态,最后统一刷新右侧文本 void nmWxSelectWellsWidget::setAllItemsSelected(bool selected) { for (int i = 0; i < m_pListWidget->count(); i++) { nmWxSelectWellItemWidget* itemWidget = static_cast(m_pListWidget->itemWidget(m_pListWidget->item(i))); if (!itemWidget) { continue; } // 屏蔽单项信号,避免批量操作时重复刷新 const bool oldSignalsBlocked = itemWidget->blockSignals(true); itemWidget->setSelected(selected); itemWidget->blockSignals(oldSignalsBlocked); } this->onUpdateSelections(); } // 批量设置指定范围内井的选中状态 void nmWxSelectWellsWidget::setRangeItemsSelected(int fromIndex, int toIndex, bool selected) { // 允许从下往上Shift+点击 if (fromIndex > toIndex) { const int tempIndex = fromIndex; fromIndex = toIndex; toIndex = tempIndex; } for (int i = fromIndex; i <= toIndex && i < m_pListWidget->count(); i++) { nmWxSelectWellItemWidget* itemWidget = static_cast(m_pListWidget->itemWidget(m_pListWidget->item(i))); if (!itemWidget) { continue; } // 屏蔽单项信号,范围处理完成后由外层统一刷新 const bool oldSignalsBlocked = itemWidget->blockSignals(true); itemWidget->setSelected(selected); itemWidget->blockSignals(oldSignalsBlocked); } } // 响应Ctrl+A,全选所有井 void nmWxSelectWellsWidget::onSelectAllItems() { this->setAllItemsSelected(true); } // 响应Ctrl+Shift+A,清空所有已选择井 void nmWxSelectWellsWidget::onClearSelectedItems() { this->setAllItemsSelected(false); } nmWxSelectWellItemWidget::nmWxSelectWellItemWidget(QWidget* parent, const QString& sName) : QWidget(parent), m_pCheckBox(nullptr), m_sName(sName) { initUI(); } void nmWxSelectWellItemWidget::setSelected(bool bSelected) { m_pCheckBox->setChecked(bSelected); } bool nmWxSelectWellItemWidget::getSelected() const { return m_pCheckBox->isChecked(); } void nmWxSelectWellItemWidget::toggleSelected() { setSelected(!getSelected()); } void nmWxSelectWellItemWidget::initUI() { setCursor(Qt::PointingHandCursor); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); setToolTip(m_sName); QHBoxLayout* pLayout = new QHBoxLayout(this); pLayout->setContentsMargins(6, 3, 6, 3); pLayout->setSpacing(0); m_pCheckBox = new QCheckBox(this); // 井名中的 & 是实际文字,不能被复选框解释为快捷键助记符。 QString sDisplayName = m_sName; m_pCheckBox->setText(sDisplayName.replace("&", "&&")); m_pCheckBox->setToolTip(m_sName); m_pCheckBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); pLayout->addWidget(m_pCheckBox); connect(m_pCheckBox, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int))); } void nmWxSelectWellItemWidget::mousePressEvent(QMouseEvent* pEvent) { if (pEvent->button() != Qt::LeftButton) { QWidget::mousePressEvent(pEvent); return; } // 状态变化由复选框统一通知,避免行空白处点击后重复刷新和重复处理 Shift。 toggleSelected(); pEvent->accept(); } void nmWxSelectWellItemWidget::onStateChanged(int nState) { Q_UNUSED(nState); emit sigItemClicked(); }