Thu, 06 Aug 2009 13:21:30 +0200
Remove seemingly declarations unnecessary according to Qt 4.5.2 headers.
1 //
2 // OSSP asgui - Accounting system graphical user interface
3 // Copyright (c) 2002-2009 The OSSP Project (http://www.ossp.org/)
4 // Copyright (c) 2002-2009 Ralf S. Engelschall <rse@engelschall.com>
5 // Copyright (c) 2002-2009 Michael Schloh von Bennewitz <michael@schloh.com>
6 // Copyright (c) 2002-2009 Cable & Wireless Telecommunications Services GmbH
7 //
8 // This file is part of OSSP asgui, an accounting system graphical user
9 // interface which can be found at http://asgui.europalab.com/.
10 //
11 // Permission to use, copy, modify, and distribute this software for
12 // any purpose with or without fee is hereby granted, provided that
13 // the above copyright notice and this permission notice appear in all
14 // copies.
15 //
16 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
17 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 // IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
20 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 // SUCH DAMAGE.
28 //
29 // as_table.cpp: ISO C++ implementation
30 //
32 #include <q3header.h>
34 //Added by qt3to4:
35 #include <QEvent>
36 #include <QMouseEvent>
37 #include <QKeyEvent>
39 #include "as_const.h"
40 #include "as_table.h"
43 // Implements an event filter for catching header double click events
44 bool TiTable::eventFilter(QObject *pObject, QEvent *pEvent)
45 {
46 if (pObject == horizontalHeader() && pEvent->type() == QEvent::MouseButtonDblClick)
47 return true;
48 if (pEvent->type() == QEvent::MouseButtonPress && // Ignore mid, right clicks
49 ((QMouseEvent *)pEvent)->button() == Qt::RightButton ||
50 ((QMouseEvent *)pEvent)->button() == Qt::MidButton)
51 return true;
52 else if (pEvent->type() == QEvent::KeyPress) {
53 if (((QKeyEvent *)pEvent)->key() == Qt::Key_Tab) { // Handle tab key
54 if (this->getEdition() >= 0) {
55 int nIter = 1; // Logic to skip invisible or read only columns
56 while (columnWidth((currentColumn() + nIter) % TITRAQ_IDXTAIL) <= 0
57 || isColumnReadOnly((currentColumn() + nIter) % TITRAQ_IDXTAIL))
58 nIter++;
60 // Advance the column or both row and column possibly
61 int nColadvance = ((currentColumn() + nIter) % TITRAQ_IDXTAIL);
62 if ((currentColumn() + nIter) >= TITRAQ_IDXTAIL) {
63 this->clearSelection(true);
64 this->setCurrentCell(currentRow() + 1, nColadvance);
65 // this->repaint(false); // Really necessary?
66 }
67 else
68 this->setCurrentCell(currentRow(), nColadvance);
69 this->setReadOnly(false);
70 this->editCell(currentRow(), currentColumn());
71 this->setEdition(currentColumn());
72 }
73 return true; // Handle the tab key event and cancel its progress
74 }
75 else if (((QKeyEvent *)pEvent)->key() == Qt::Key_Backtab) { // Handle shift tab key
76 if (this->getEdition() >= 0) {
77 int nIter = 1; // Logic to skip invisible or read only columns
78 while (columnWidth((currentColumn() - nIter + TITRAQ_IDXTAIL) % TITRAQ_IDXTAIL) <= 0
79 || isColumnReadOnly((currentColumn() - nIter + TITRAQ_IDXTAIL) % TITRAQ_IDXTAIL))
80 nIter++;
82 // Advance the column or both row and column possibly
83 int nColadvance = (currentColumn() - nIter + TITRAQ_IDXTAIL) % TITRAQ_IDXTAIL;
84 if ((currentColumn() - nIter) < 0) {
85 this->clearSelection(true);
86 this->setCurrentCell(currentRow() - 1, nColadvance);
87 // this->repaint(false); // Really necessary?
88 }
89 else
90 this->setCurrentCell(currentRow(), nColadvance);
91 this->setReadOnly(false);
92 this->editCell(currentRow(), currentColumn());
93 this->setEdition(currentColumn());
94 }
95 return true; // Handle the shift tab key event and cancel its progress
96 }
97 else if (((QKeyEvent *)pEvent)->key() == Qt::Key_Return && this->getEdition() >= 0) { // Return key
98 this->endEdit(currEditRow(), currEditCol(), true, false);
99 this->setEdition(); // Reset edition
100 return true; // Cancel progress
101 }
102 else if (((QKeyEvent *)pEvent)->key() == Qt::Key_Enter && this->getEdition() >= 0) { // Enter key
103 this->endEdit(currEditRow(), currEditCol(), true, false);
104 this->setEdition(); // Reset edition
105 return true; // Cancel progress
106 }
107 else if (((QKeyEvent *)pEvent)->key() == Qt::Key_Return) { // Return key without edition
108 this->setReadOnly(false); // Allow edition
109 this->editCell(currentRow(), currentColumn()); // Begin edition
110 this->setEdition(currentColumn()); // Store edition state
111 return true; // Cancel further progress
112 }
113 else if (((QKeyEvent *)pEvent)->key() == Qt::Key_Enter) { // Enter key without edition
114 this->setReadOnly(false); // Allow edition
115 this->editCell(currentRow(), currentColumn()); // Begin edition
116 this->setEdition(currentColumn()); // Store edition state
117 return true; // Cancel further progress
118 }
119 else if (((QKeyEvent *)pEvent)->key() == Qt::Key_Escape) // Handle escape key
120 this->setEdition();
121 else if (((QKeyEvent *)pEvent)->key() == Qt::Key_Up && this->getEdition() >= 0) // Handle up key
122 return true; // Capture
123 else if (((QKeyEvent *)pEvent)->key() == Qt::Key_Down && this->getEdition() >= 0) // Handle down key
124 return true; // Capture
126 // Forward incompletely handled key events
127 return Q3Table::eventFilter(pObject, pEvent);
128 }
129 else // Default behaviour is to pass the event onwards
130 return Q3Table::eventFilter(pObject, pEvent);
131 }
133 // Overridden member hack to allow externally connected control
134 // widgets to influence dirty or clean state of table data
135 void TiTable::setText(int nRow, int nCol, const QString &nText)
136 {
137 if (this->numRows() > 0) {
138 // If a cell was edited, emit a signal indicating so
139 // We can't rely on valueChanged for unknown reasons
140 if (nText != this->text(nRow, nCol) && nCol != TITRAQ_IDXLINE)
141 emit textEdited(nRow, nCol);
143 Q3Table::setText(nRow, nCol, nText);
144 }
145 }
147 // Overridden member hack to allow externally connected control
148 // widgets to influence dirty or clean state of table data
149 void TiTable::sortColumn(int nCol, bool bAscend, bool bWhole)
150 {
151 // Guard against a repeat sort behaviour
152 if (nCol == this->getSortcol() && bAscend == this->getSortdir())
153 this->setSortdir(!bAscend);
154 else
155 this->setSortdir(bAscend);
157 this->setSortcol(nCol);
158 Q3Table::sortColumn(nCol, this->getSortdir(), true);
160 // // Announce sorting policy with multiple selections
161 // QTableSelection Testsel = this->selection(this->currentSelection());
162 // if (Testsel.topRow() != Testsel.bottomRow())
163 // m_pStatbar->message(trUtf8("Multiple selections dropped when sorting"), 4000);
165 // Move and display the selection highlight
166 this->removeSelection(this->currentSelection());
167 this->selectRow(this->currentRow());
168 this->ensureCellVisible(this->currentRow(), 0);
170 // Write nonsaving line numbers for all rows
171 for (int nIter = this->numRows() - 1; nIter >= 0; nIter--)
172 this->setText(nIter, TITRAQ_IDXLINE, QString::number(nIter).rightJustify(4, QChar('0')));
173 }
175 // Overriden member to render edge rows differently according to a sort key
176 void TiTable::paintCell(QPainter *pPainter, int nRow, int nCol, const QRect &Recto, bool bSelect, const QColorGroup &Colgroup)
177 {
178 QColorGroup Cgroup(Colgroup);
179 int nRed, nGreen, nBlue;
181 nRed = m_pTiprefs->getNumber(TITRAQ_PREFLIGHTRED, TITRAQ_DEFLIGHTRED);
182 nGreen = m_pTiprefs->getNumber(TITRAQ_PREFLIGHTGREEN, TITRAQ_DEFLIGHTGREEN);
183 nBlue = m_pTiprefs->getNumber(TITRAQ_PREFLIGHTBLUE, TITRAQ_DEFLIGHTBLUE);
184 QColor Bright = QColor(nRed, nGreen, nBlue);
185 nRed = m_pTiprefs->getNumber(TITRAQ_PREFDARKRED, TITRAQ_DEFDARKRED);
186 nGreen = m_pTiprefs->getNumber(TITRAQ_PREFDARKGREEN, TITRAQ_DEFDARKGREEN);
187 nBlue = m_pTiprefs->getNumber(TITRAQ_PREFDARKBLUE, TITRAQ_DEFDARKBLUE);
188 QColor Dark = QColor(nRed, nGreen, nBlue);
190 // Alternate color for nonmatching sort keys
191 QString Cur = this->text(nRow, this->getSortcol());
192 QString Las = this->text(nRow - 1, this->getSortcol());
194 // A nice cascade of conditions providing a linewise base color test and set
195 // The algorythm:
196 // 1 Determine if the current row's index key differs from the former one
197 // 2a If they are the same, then current row should have the same color
198 // 2b If they are different, then current row should have an alt color
199 // 3 Store information about which color we chose for the current row
200 if (!Cur.isNull() && !Las.isNull() && Cur == Las) { // Set the base color conditionally
201 if (this->text(nRow - 1, TITRAQ_IDXSTATUS).at(TITRAQ_IDXSTATCOLOR) == QChar(TITRAQ_BRIGHT)) {
202 Cgroup.setColor(QColorGroup::Base, Bright); // Bright
203 if (this->text(nRow, TITRAQ_IDXSTATUS).at(TITRAQ_IDXSTATCOLOR) != QChar(TITRAQ_BRIGHT))
204 this->setText(nRow, TITRAQ_IDXSTATUS, this->text(nRow, TITRAQ_IDXSTATUS).replace(TITRAQ_IDXSTATCOLOR, sizeof(char), QChar(TITRAQ_BRIGHT)));
205 }
206 else {
207 Cgroup.setColor(QColorGroup::Base, Dark); // Dark
208 if (this->text(nRow, TITRAQ_IDXSTATUS).at(TITRAQ_IDXSTATCOLOR) != QChar(TITRAQ_DARK))
209 this->setText(nRow, TITRAQ_IDXSTATUS, this->text(nRow, TITRAQ_IDXSTATUS).replace(TITRAQ_IDXSTATCOLOR, sizeof(char), QChar(TITRAQ_DARK)));
210 }
211 }
212 else {
213 if (this->text(nRow - 1, TITRAQ_IDXSTATUS).at(TITRAQ_IDXSTATCOLOR) == QChar(TITRAQ_BRIGHT)) {
214 Cgroup.setColor(QColorGroup::Base, Dark); // Dark
215 if (this->text(nRow, TITRAQ_IDXSTATUS).at(TITRAQ_IDXSTATCOLOR) != QChar(TITRAQ_DARK))
216 this->setText(nRow, TITRAQ_IDXSTATUS, this->text(nRow, TITRAQ_IDXSTATUS).replace(TITRAQ_IDXSTATCOLOR, sizeof(char), QChar(TITRAQ_DARK)));
217 }
218 else {
219 Cgroup.setColor(QColorGroup::Base, Bright); // Bright
220 if (this->text(nRow, TITRAQ_IDXSTATUS).at(TITRAQ_IDXSTATCOLOR) != QChar(TITRAQ_BRIGHT))
221 this->setText(nRow, TITRAQ_IDXSTATUS, this->text(nRow, TITRAQ_IDXSTATUS).replace(TITRAQ_IDXSTATCOLOR, sizeof(char), QChar(TITRAQ_BRIGHT)));
222 }
223 }
225 Q3Table::paintCell(pPainter, nRow, nCol, Recto, bSelect, Cgroup);
226 };
228 // Blah
229 void TiTable::activateNextCell(void)
230 {
231 int nIter = 1; // Logic to skip invisible or read only columns
232 while (columnWidth((currentColumn() + nIter) % TITRAQ_IDXTAIL) <= 0
233 || isColumnReadOnly((currentColumn() + nIter) % TITRAQ_IDXTAIL))
234 nIter++;
236 // Advance the column or both row and column possibly
237 int nColadvance = ((currentColumn() + nIter) % TITRAQ_IDXTAIL);
238 if ((currentColumn() + nIter) >= TITRAQ_IDXTAIL)
239 this->setCurrentCell(currentRow() + 1, nColadvance);
240 else
241 this->setCurrentCell(currentRow(), nColadvance);
242 this->setReadOnly(false);
243 this->editCell(currentRow(), currentColumn());
244 this->setEdition(currentColumn());
245 }
247 // Overriden member to properly handle read only attribute after edition
248 void TiTable::endEdit(int nRow, int nCol, bool bAccept, bool bReplace)
249 {
250 Q3Table::endEdit(nRow, nCol, bAccept, bReplace);
252 // Table read only attribute must be set to return to the normal
253 // row highlight and selection behaviour of AS. The reason it was
254 // reset in inplaceEdit() was to allow editing in the first place.
255 this->setReadOnly(true);
256 }