-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectrowdatadialog.cpp
More file actions
210 lines (177 loc) · 6.13 KB
/
selectrowdatadialog.cpp
File metadata and controls
210 lines (177 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "selectrowdatadialog.h"
#include "ui_selectrowdatadialog.h"
#include <QCheckBox>
#include <QDebug>
SelectRowDataDialog::SelectRowDataDialog(int nIndex, QWidget *parent) :
QDialog(parent),
ui(new Ui::SelectRowDataDialog),
m_nIndex(nIndex)
{
ui->setupUi(this);
m_pTargetTableView = static_cast<QTableView*>(parent);
m_pTargetModel = static_cast<QStandardItemModel*>(m_pTargetTableView->model());
m_model = new QStandardItemModel(ui->tableView);
ui->tableView->setModel(m_model);
ui->tableView->verticalHeader()->setHidden(true);
m_vCheckBoxs.clear();
m_model->setHorizontalHeaderItem(0, new QStandardItem(""));
m_model->setHorizontalHeaderItem(1, new QStandardItem("名称"));
m_model->setHorizontalHeaderItem(2, new QStandardItem("数量"));
connect(ui->cancelBtn, SIGNAL(clicked()), this, SLOT(close()));
connect(ui->confirmBtn, SIGNAL(clicked()), this, SLOT(OnConfirmBtnClicked()));
this->setWindowFlags(Qt::Popup);
setWindowTitle("筛选");
InitDialog();
}
SelectRowDataDialog::~SelectRowDataDialog()
{
for (auto& data : m_vCheckBoxs)
{
delete data;
data = nullptr;
}
m_vCheckBoxs.clear();
delete ui;
}
void SelectRowDataDialog::OnConfirmBtnClicked()
{
QStringList stringList;
for (int i = 0;i < m_vCheckBoxs.size(); ++i)
{
if (m_vCheckBoxs[i]->checkState() == Qt::Checked)
{
stringList << m_model->index(i, 1).data().toString();
}
}
for (int row = 1; row < m_pTargetModel->rowCount(); ++row)
{
QString sTargetData = m_pTargetModel->index(row, m_nIndex).data().toString();
m_pTargetTableView->setRowHidden(row, stringList.contains(sTargetData) == false);
}
close();
}
void SelectRowDataDialog::InitDialog()
{
int nRow = m_pTargetModel->rowCount();
QVector<QString> vFieldData;
QMap<QString, int> mMap;
int nEmpty = 0;
int nAllSelectRow = 0;
for (int i = 1; i < nRow; ++i)
{
if (m_pTargetTableView->isRowHidden(i) == false)
{
QModelIndex index = m_pTargetModel->index(i, m_nIndex);
QString sValue = "";
if (index.isValid())
{
sValue = index.data().toString();
if (sValue != "")
{
if (mMap.find(sValue) != mMap.end())
{
mMap.find(sValue).value() = mMap.find(sValue).value() + 1;
}
else
{
mMap.insert(sValue, 1);
vFieldData.push_back(sValue);
}
}
}
nAllSelectRow++;
if (sValue == "")
{
nEmpty++;
}
}
}
if(nEmpty > 0)
{
mMap.insert("(空白)", nEmpty);
vFieldData.push_front("(空白)");
}
//把全选放在最前面
mMap.insert("(全选)", nAllSelectRow);
vFieldData.push_front("(全选)");
int i = 0;
for (auto data : vFieldData)
{
auto iter = mMap.find(data);
if (iter != mMap.end())
{
QString sField = iter.key();
QString sNum = QString::number(iter.value());
QStandardItem* item = new QStandardItem();
item->setFlags(Qt::ItemIsEnabled);
m_model->setItem(i, 0, item);
QCheckBox* checkBox = new QCheckBox();
checkBox->setCheckState(Qt::Checked);
ui->tableView->setIndexWidget(item->index(), checkBox);
checkBox->setToolTip(QString::number(i));
m_vCheckBoxs.push_back(checkBox);
connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(OnAllCheckBoxStateChange(int)));
QStandardItem* item1 = new QStandardItem(sField);
item1->setFlags(Qt::ItemIsEnabled);
item1->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
m_model->setItem(i, 1, item1);
QStandardItem* item2 = new QStandardItem(sNum);
item2->setFlags(Qt::ItemIsEnabled);
item2->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
m_model->setItem(i, 2, item2);
i++;
}
}
ui->tableView->setColumnWidth(0, 20);
}
void SelectRowDataDialog::OnAllCheckBoxStateChange(int nState)
{
QObject* sender = QObject::sender();
int nIndex = 0;
if (sender->metaObject()->className() == QStringLiteral("QCheckBox"))
{
QCheckBox* checkBox = qobject_cast<QCheckBox*>(sender);
nIndex = checkBox->toolTip().toInt();
}
if (nIndex == 0)
{
for (int i = 1;i < m_vCheckBoxs.size();++i)
{
disconnect(m_vCheckBoxs[i], SIGNAL(stateChanged(int)), this, SLOT(OnAllCheckBoxStateChange(int)));
m_vCheckBoxs[i]->setCheckState(Qt::CheckState(nState));
connect(m_vCheckBoxs[i], SIGNAL(stateChanged(int)), this, SLOT(OnAllCheckBoxStateChange(int)));
}
}
else
{
//如果单个选中
bool change = true;
if (nState == Qt::Checked)
{
for (int i = 1;i < m_vCheckBoxs.size();++i)
{
if (m_vCheckBoxs[i]->checkState() != Qt::Checked)
{
change = false;
break;
}
}
}
if (change)
{
disconnect(m_vCheckBoxs[0], SIGNAL(stateChanged(int)), this, SLOT(OnAllCheckBoxStateChange(int)));
m_vCheckBoxs[0]->setCheckState(Qt::CheckState(nState));
connect(m_vCheckBoxs[0], SIGNAL(stateChanged(int)), this, SLOT(OnAllCheckBoxStateChange(int)));
}
}
bool noneChecked = true;
for (int i = 1;i < m_vCheckBoxs.size();++i)
{
if (m_vCheckBoxs[i]->checkState() == Qt::Checked)
{
noneChecked = false;
break;
}
}
ui->confirmBtn->setDisabled(noneChecked);
}