forked from cloose/CuteMarkEd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabletooldialog.cpp
More file actions
203 lines (166 loc) · 5.58 KB
/
tabletooldialog.cpp
File metadata and controls
203 lines (166 loc) · 5.58 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
/*
* Copyright 2013 Christian Loose <christian.loose@hamburg.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tabletooldialog.h"
#include "ui_tabletooldialog.h"
#include <QComboBox>
#include <QLineEdit>
// Make QPoint in QMap work
bool operator<(const QPoint& lhs, const QPoint& rhs)
{
if (lhs.x() < rhs.x())
return true;
else if (lhs.x() == rhs.x())
return lhs.y() < rhs.y();
else
return false;
}
TableToolDialog::TableToolDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::TableToolDialog),
previousRowCount(0),
previousColumnCount(0)
{
ui->setupUi(this);
tableSizeChanged();
}
TableToolDialog::~TableToolDialog()
{
delete ui;
}
int TableToolDialog::rows() const
{
return ui->rowsSpinBox->value();
}
int TableToolDialog::columns() const
{
return ui->columnsSpinBox->value();
}
QList<Qt::Alignment> TableToolDialog::alignments() const
{
QList<Qt::Alignment> alignments;
foreach (QComboBox *cb, alignmentComboBoxList) {
Qt::Alignment alignment = (Qt::Alignment)cb->itemData(cb->currentIndex()).toInt();
alignments.append(alignment);
}
return alignments;
}
QList<QStringList> TableToolDialog::tableCells() const
{
QList<QStringList> table;
for (int row = 0; row < rows(); ++row) {
QStringList rowData;
for (int col = 0; col < columns(); ++col) {
rowData << cellEditorMap[QPoint(col, row)]->text();
}
table.append(rowData);
}
return table;
}
void TableToolDialog::tableSizeChanged()
{
int rowDiff = rows() - previousRowCount;
int columnDiff = columns() - previousColumnCount;
if (columnDiff > 0) {
addColumns(columnDiff);
} else if (columnDiff < 0) {
removeColumns(columnDiff);
} else if (rowDiff > 0) {
addRows(rowDiff);
} else if (rowDiff < 0) {
removeRows(rowDiff);
}
updateTabOrder();
previousColumnCount = columns();
previousRowCount = rows();
}
void TableToolDialog::addColumns(int newColumns)
{
for (int col = 0; col < newColumns; ++col) {
// add combo box to choose alignment of column
QComboBox *cb = new QComboBox(ui->tableGroupBox);
cb->addItem(tr("Left"), Qt::AlignLeft);
cb->addItem(tr("Center"), Qt::AlignCenter);
cb->addItem(tr("Right"), Qt::AlignRight);
ui->tableGridLayout->addWidget(cb, 0, col+previousColumnCount+1);
alignmentComboBoxList << cb;
// add a column of new line edits
for (int row = 0; row < rows(); ++row) {
QLineEdit *edit = new QLineEdit(ui->tableGroupBox);
ui->tableGridLayout->addWidget(edit, row+1, col+previousColumnCount+1);
cellEditorMap.insert(QPoint(col+previousColumnCount, row), edit);
}
}
}
void TableToolDialog::removeColumns(int removedColumns)
{
for (int col = 0; col < qAbs(removedColumns); ++col) {
// remove alignment combo box in last column
QComboBox *cb = alignmentComboBoxList.last();
alignmentComboBoxList.removeLast();
ui->tableGridLayout->removeWidget(cb);
cb->deleteLater();
// remove all line edits in last column
for (int row = 0; row < rows(); ++row) {
QLineEdit *edit = cellEditorMap[QPoint(previousColumnCount-col-1, row)];
cellEditorMap.remove(QPoint(previousColumnCount-col-1, row));
ui->tableGridLayout->removeWidget(edit);
edit->deleteLater();
}
}
}
void TableToolDialog::addRows(int newRows)
{
for (int row = 0; row < newRows; ++row) {
// add a new row of line edits
for (int col = 0; col < columns(); ++col) {
QLineEdit *edit = new QLineEdit(ui->tableGroupBox);
ui->tableGridLayout->addWidget(edit, row+previousRowCount+1, col+1);
cellEditorMap.insert(QPoint(col, row+previousRowCount), edit);
}
}
}
void TableToolDialog::removeRows(int removedRows)
{
for (int row = 0; row < qAbs(removedRows); ++row) {
// remove all line edits in current row
for (int col = 0; col < columns(); ++col) {
QLineEdit *edit = cellEditorMap[QPoint(col, previousRowCount-row-1)];
cellEditorMap.remove(QPoint(col, previousRowCount-row-1));
ui->tableGridLayout->removeWidget(edit);
edit->deleteLater();
}
}
}
void TableToolDialog::updateTabOrder()
{
// tab between spin boxes
this->setTabOrder(ui->rowsSpinBox, ui->columnsSpinBox);
QWidget *first = ui->columnsSpinBox;
foreach (QComboBox *cb, alignmentComboBoxList) {
this->setTabOrder(first, cb);
first = cb;
}
// tab between line edits from left-to-right
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < columns(); ++col) {
this->setTabOrder(first, cellEditorMap[QPoint(col, row)]);
first = cellEditorMap[QPoint(col, row)];
}
}
// tab between last line edit and okay button
this->setTabOrder(first, ui->buttonBox);
}