You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nmWATI/Src/nmNum/nmSubWxs/nmWxSelectWellsWidget.cpp

147 lines
4.2 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "nmWxSelectWellsWidget.h"
#include <QLineEdit>
#include <QCheckBox>
#include <QEvent>
#include <QStringList>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QMouseEvent>
#include <QLabel>
#include <QDebug>
#include <QListWidget>
#include "nmData/nmDataLogFile.h"
#include <QTextEdit>
nmWxSelectWellsWidget::nmWxSelectWellsWidget(QWidget* aParent) :
QWidget(aParent),
m_pListWidget(new QListWidget(this)),
m_pLineEdit(new QTextEdit(this))
{
// 主布局
QGridLayout* layout = new QGridLayout;
this->setLayout(layout);
layout->addWidget(new QLabel(tr("Select Wells: ")), 0, 0);
layout->addWidget(m_pListWidget, 1, 0);
layout->addWidget(new QLabel(tr("Selected Wells: ")), 0, 1);
layout->addWidget(m_pLineEdit, 1, 1);
// m_pLineEdit->setText(multiLineText);
m_pLineEdit->setReadOnly(true);
this->setMinimumWidth(200);
}
void nmWxSelectWellsWidget::addItems(QStringList items, QList<QVariant> values)
{
if (m_items.count() > values.count()) {
return;
}
for (int i = 0; i < items.count(); i++) {
this->addItem(items[i], values[i]);
}
}
void nmWxSelectWellsWidget::addItem(QString name, QVariant value)
{
m_items.append(name);
m_values.append(value);
nmWxSelectWellItemWidget* itemWidget = new nmWxSelectWellItemWidget(NULL, name, value);
itemWidget->setFixedHeight(25);
itemWidget->setFixedWidth(200);
if (m_selectItems.contains(name)) {
itemWidget->setSelected(true);
}
// itemWidget->setMinimumWidth(m_pListWidget->viewport()->width()); // 设置widget的最小宽度为列表视图的宽度
QListWidgetItem* item = new QListWidgetItem;
nmDataLogFile::getInstance()->writeLog(QString(" %1 ").arg(item->sizeHint().width()));
item->setSizeHint(QSize(200, 30));
m_pListWidget->addItem(item);
m_pListWidget->setItemWidget(item, itemWidget);
connect(itemWidget, SIGNAL(sigItemClicked()), this, SLOT(onUpdateSelections()));
}
QStringList nmWxSelectWellsWidget::getItems()
{
return m_pLineEdit->toPlainText().split("\n");
}
void nmWxSelectWellsWidget::setSelectedItems(QStringList selectItems)
{
m_selectItems = selectItems;
}
void nmWxSelectWellsWidget::onUpdateSelections()
{
QStringList nameList;
// 遍历所有的itemWidget获取所有被选中的信息
for (int i = 0; i < m_pListWidget->count(); i++) {
nmWxSelectWellItemWidget* itemWidget = static_cast<nmWxSelectWellItemWidget*>(m_pListWidget->itemWidget(m_pListWidget->item(i)));
if (itemWidget->getSelected()) {
nameList.append(m_items[i]);
}
}
m_pLineEdit->setPlainText(nameList.join("\n"));
}
nmWxSelectWellItemWidget::nmWxSelectWellItemWidget()
{
}
nmWxSelectWellItemWidget::nmWxSelectWellItemWidget(QWidget *parent, QString name, QVariant value)
{
m_sName = name;
m_iValue = value;
m_pCBox = NULL;
this->initUI();
}
void nmWxSelectWellItemWidget::setSelected(bool selected)
{
if (m_pCBox) {
m_pCBox->setChecked(selected);
}
}
bool nmWxSelectWellItemWidget::getSelected()
{
if (m_pCBox) {
return m_pCBox->isChecked();
}
return false;
}
void nmWxSelectWellItemWidget::toggleSelected()
{
this->setSelected(!this->getSelected());
}
void nmWxSelectWellItemWidget::initUI()
{
// 可以设置鼠标样式,表明这个部件可以被点击
setCursor(Qt::PointingHandCursor);
// 设置QCheckBox
QHBoxLayout* layout = new QHBoxLayout;
setLayout(layout);
m_pCBox = new QCheckBox(this);
m_pCBox->setText(m_sName);
layout->setAlignment(Qt::AlignVCenter);
layout->setContentsMargins(10, 0, 0, 0);
layout->setSpacing(0);
connect(m_pCBox, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
}
void nmWxSelectWellItemWidget::mousePressEvent(QMouseEvent *event)
{
// 当鼠标左键被按下时,执行一些操作
if (event->button() == Qt::LeftButton) {
// 处理点击事件,例如发出一个信号或者调用一个槽函数
qDebug() << "ClickableWidget was clicked!";
}
QWidget::mousePressEvent(event); // 调用基类的处理函数
this->toggleSelected();
emit this->sigItemClicked();
}
void nmWxSelectWellItemWidget::onStateChanged(int)
{
emit this->sigItemClicked();
}