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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/***************************************************************************
copyright : (C) 2003-2006 by Robby Stephenson
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of version 2 of the GNU General Public License as *
* published by the Free Software Foundation; *
* *
***************************************************************************/
#include "tablefieldwidget.h"
#include "../field.h"
#include "../tellico_utils.h"
#include "../tellico_kernel.h"
#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <kiconloader.h>
#include <kinputdialog.h>
#include <tqtable.h>
namespace {
static const int MIN_TABLE_ROWS = 5;
static const int MAX_TABLE_COLS = 10;
}
using Tellico::GUI::TableFieldWidget;
TableFieldWidget::TableFieldWidget(Data::FieldPtr field_, TQWidget* parent_, const char* name_/*=0*/)
: FieldWidget(field_, parent_, name_), m_field(field_), m_row(-1), m_col(-1) {
bool ok;
m_columns = Tellico::toUInt(field_->property(TQString::fromLatin1("columns")), &ok);
if(!ok) {
m_columns = 1;
} else {
m_columns = TQMIN(m_columns, MAX_TABLE_COLS); // max of 5 columns
}
m_table = new TQTable(MIN_TABLE_ROWS, m_columns, this);
labelColumns(m_field);
// allow renaming of column titles
m_table->horizontalHeader()->setClickEnabled(true);
m_table->horizontalHeader()->installEventFilter(this);
m_table->verticalHeader()->setClickEnabled(true);
m_table->verticalHeader()->installEventFilter(this);
connect(m_table->verticalHeader(), TQ_SIGNAL(indexChange(int, int, int)), TQ_SIGNAL(modified()));
m_table->setDragEnabled(false);
m_table->setFocusStyle(TQTable::FollowStyle);
m_table->setRowMovingEnabled(true); // rows can be moved
m_table->setColumnMovingEnabled(false); // columns remain fixed
m_table->setColumnStretchable(m_columns-1, true);
m_table->adjustColumn(m_columns-1);
m_table->setSelectionMode(TQTable::NoSelection);
m_table->setHScrollBarMode(TQScrollView::AlwaysOff);
connect(m_table, TQ_SIGNAL(valueChanged(int, int)), TQ_SIGNAL(modified()));
connect(m_table, TQ_SIGNAL(currentChanged(int, int)), TQ_SLOT(slotCheckRows(int, int)));
connect(m_table, TQ_SIGNAL(valueChanged(int, int)), TQ_SLOT(slotResizeColumn(int, int)));
connect(m_table, TQ_SIGNAL(contextMenuRequested(int, int, const TQPoint&)), TQ_SLOT(contextMenu(int, int, const TQPoint&)));
registerWidget();
}
TQString TableFieldWidget::text() const {
TQString text, str, rstack, cstack, rowStr;
for(int row = 0; row < m_table->numRows(); ++row) {
rowStr.truncate(0);
cstack.truncate(0);
for(int col = 0; col < m_table->numCols(); ++col) {
str = m_table->text(row, col).simplifyWhiteSpace();
if(str.isEmpty()) {
cstack += TQString::fromLatin1("::");
} else {
rowStr += cstack + str + TQString::fromLatin1("::");
cstack.truncate(0);
}
}
if(rowStr.isEmpty()) {
rstack += TQString::fromLatin1("; ");
} else {
rowStr.truncate(rowStr.length()-2); // remove last semi-colon and space
text += rstack + rowStr + TQString::fromLatin1("; ");
rstack.truncate(0);
}
}
if(!text.isEmpty()) {
text.truncate(text.length()-2); // remove last semi-colon and space
}
// now reduce number of rows if necessary
bool loop = true;
for(int row = m_table->numRows()-1; loop && row > MIN_TABLE_ROWS; --row) {
bool empty = true;
for(int col = 0; col < m_table->numCols(); ++col) {
if(!m_table->text(row, col).isEmpty()) {
empty = false;
break;
}
}
if(empty) {
m_table->removeRow(row);
} else {
loop = false;
}
}
return text;
}
void TableFieldWidget::setText(const TQString& text_) {
TQStringList list = Data::Field::split(text_, true);
// add additional rows if needed
if(static_cast<int>(list.count()) > m_table->numRows()) {
m_table->insertRows(m_table->numRows(), list.count()-m_table->numRows());
}
int row;
for(row = 0; row < static_cast<int>(list.count()); ++row) {
for(int col = 0; col < m_table->numCols(); ++col) {
m_table->setText(row, col, list[row].section(TQString::fromLatin1("::"), col, col));
}
m_table->showRow(row);
}
// remove any un-needed rows
int minRow = TQMAX(row, MIN_TABLE_ROWS);
for(row = m_table->numRows()-1; row >= minRow; --row) {
m_table->removeRow(row);
}
// adjust all columns
for(int col = 0; col < m_table->numCols()-1; ++col) {
m_table->adjustColumn(col);
}
}
void TableFieldWidget::clear() {
bool wasEmpty = true;
for(int row = 0; row < m_table->numRows(); ++row) {
if(!emptyRow(row)) {
wasEmpty = false;
}
for(int col = 0; col < m_table->numCols(); ++col) {
m_table->setText(row, col, TQString());
}
if(row >= MIN_TABLE_ROWS) {
m_table->removeRow(row);
--row;
}
}
editMultiple(false);
if(!wasEmpty) {
emit modified();
}
}
TQWidget* TableFieldWidget::widget() {
return m_table;
}
void TableFieldWidget::slotCheckRows(int row_, int) {
if(row_ == m_table->numRows()-1 && !emptyRow(row_)) { // if is last row and row above is not empty
m_table->insertRows(m_table->numRows());
}
}
void TableFieldWidget::slotResizeColumn(int, int col_) {
m_table->adjustColumn(col_);
}
void TableFieldWidget::slotRenameColumn() {
if(m_col < 0 || m_col >= m_columns) {
return;
}
TQString name = m_table->horizontalHeader()->label(m_col);
bool ok;
TQString newName = KInputDialog::getText(i18n("Rename Column"), i18n("New column name:"),
name, &ok, this);
if(ok && !newName.isEmpty()) {
Data::FieldPtr newField = new Data::Field(*m_field);
newField->setProperty(TQString::fromLatin1("column%1").arg(m_col+1), newName);
if(Kernel::self()->modifyField(newField)) {
m_field = newField;
labelColumns(m_field);
}
}
}
bool TableFieldWidget::emptyRow(int row_) const {
for(int col = 0; col < m_table->numCols(); ++col) {
if(!m_table->text(row_, col).isEmpty()) {
return false;
}
}
return true;
}
void TableFieldWidget::labelColumns(Data::FieldPtr field_) {
for(int i = 0; i < m_columns; ++i) {
TQString s = field_->property(TQString::fromLatin1("column%1").arg(i+1));
if(s.isEmpty()) {
s = i18n("Column %1").arg(i+1);
}
m_table->horizontalHeader()->setLabel(i, s);
}
}
void TableFieldWidget::updateFieldHook(Data::FieldPtr, Data::FieldPtr newField_) {
bool ok;
m_columns = Tellico::toUInt(newField_->property(TQString::fromLatin1("columns")), &ok);
if(!ok) {
m_columns = 1;
} else {
m_columns = TQMIN(m_columns, MAX_TABLE_COLS); // max of 5 columns
}
if(m_columns != m_table->numCols()) {
m_table->setNumCols(m_columns);
}
m_table->horizontalHeader()->adjustHeaderSize();
labelColumns(newField_);
}
bool TableFieldWidget::eventFilter(TQObject* obj_, TQEvent* ev_) {
if(ev_->type() == TQEvent::MouseButtonPress
&& static_cast<TQMouseEvent*>(ev_)->button() == TQt::RightButton) {
if(obj_ == m_table->horizontalHeader()) {
TQMouseEvent* ev = static_cast<TQMouseEvent*>(ev_);
// might be scrolled
int pos = ev->x() + m_table->horizontalHeader()->offset();
int col = m_table->horizontalHeader()->sectionAt(pos);
if(col >= m_columns) {
return false;
}
m_row = -1;
m_col = col;
TDEPopupMenu menu(this);
menu.insertItem(SmallIconSet(TQString::fromLatin1("edit")), i18n("Rename Column..."),
this, TQ_SLOT(slotRenameColumn()));
menu.exec(ev->globalPos());
return true;
} else if(obj_ == m_table->verticalHeader()) {
TQMouseEvent* ev = static_cast<TQMouseEvent*>(ev_);
// might be scrolled
int pos = ev->y() + m_table->verticalHeader()->offset();
int row = m_table->verticalHeader()->sectionAt(pos);
if(row < 0 || row > m_table->numRows()-1) {
return false;
}
m_row = row;
m_col = -1;
// show regular right-click menu
contextMenu(m_row, m_col, ev->globalPos());
return true;
}
}
return FieldWidget::eventFilter(obj_, ev_);
}
void TableFieldWidget::contextMenu(int row_, int col_, const TQPoint& p_) {
// might get called with col == -1 for clicking on vertical header
// but a negative row means clicking outside bounds of table
if(row_ < 0) {
return;
}
m_row = row_;
m_col = col_;
int id;
TDEPopupMenu menu(this);
menu.insertItem(SmallIconSet(TQString::fromLatin1("insrow")), i18n("Insert Row"),
this, TQ_SLOT(slotInsertRow()));
menu.insertItem(SmallIconSet(TQString::fromLatin1("remrow")), i18n("Remove Row"),
this, TQ_SLOT(slotRemoveRow()));
id = menu.insertItem(SmallIconSet(TQString::fromLatin1("1uparrow")), i18n("Move Row Up"),
this, TQ_SLOT(slotMoveRowUp()));
if(m_row == 0) {
menu.setItemEnabled(id, false);
}
id = menu.insertItem(SmallIconSet(TQString::fromLatin1("1downarrow")), i18n("Move Row Down"),
this, TQ_SLOT(slotMoveRowDown()));
if(m_row == m_table->numRows()-1) {
menu.setItemEnabled(id, false);
}
menu.insertSeparator();
id = menu.insertItem(SmallIconSet(TQString::fromLatin1("edit")), i18n("Rename Column..."),
this, TQ_SLOT(slotRenameColumn()));
if(m_col < 0 || m_col > m_columns-1) {
menu.setItemEnabled(id, false);
}
menu.insertSeparator();
menu.insertItem(SmallIconSet(TQString::fromLatin1("locationbar_erase")), i18n("Clear Table"),
this, TQ_SLOT(clear()));
menu.exec(p_);
}
void TableFieldWidget::slotInsertRow() {
if(m_row > -1) {
m_table->insertRows(m_row);
emit modified();
}
}
void TableFieldWidget::slotRemoveRow() {
if(m_row > -1) {
m_table->removeRow(m_row);
emit modified();
}
}
void TableFieldWidget::slotMoveRowUp() {
if(m_row > 0) {
m_table->swapRows(m_row, m_row-1, true);
m_table->updateContents();
emit modified();
}
}
void TableFieldWidget::slotMoveRowDown() {
if(m_row < m_table->numRows()-1) {
m_table->swapRows(m_row, m_row+1, true);
m_table->updateContents();
emit modified();
}
}
#include "tablefieldwidget.moc"
|