kexitableedit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Peter Simonsson <psn@linux.se>
3 Copyright (C) 2003-2006 Jaroslaw Staniek <js@iidea.pl>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "kexitableedit.h"
22#include "kexidataawareobjectiface.h"
23#include <kexidb/field.h>
24#include <kexidb/utils.h>
25
26#include <tqpalette.h>
27#include <tqpainter.h>
28
29#include <tdeglobal.h>
30#include <tdelocale.h>
31#include <kdebug.h>
32
33KexiTableEdit::KexiTableEdit(KexiTableViewColumn &column, TQWidget* parent)
34: TQWidget(dynamic_cast<TQScrollView*>(parent) ? dynamic_cast<TQScrollView*>(parent)->viewport() : parent)
35 ,m_column(&column)
36// ,m_field(&f)
37// ,m_type(f.type()) //copied because the rest of code uses m_type
38 ,m_scrollView(dynamic_cast<TQScrollView*>(parent))
39 ,m_usesSelectedTextColor(true)
40 ,m_view(0)
41// ,m_hasFocusableWidget(true)
42// ,m_acceptEditorAfterDeleteContents(false)
43{
44 setPaletteBackgroundColor( palette().color(TQPalette::Active, TQColorGroup::Base) );
45 installEventFilter(this);
46
47 //margins
48 if (displayedField()->isFPNumericType()) {
49#ifdef TQ_WS_WIN
50 m_leftMargin = 0;
51#else
52 m_leftMargin = 0;
53#endif
54 }
55 else if (displayedField()->isIntegerType()) {
56#ifdef TQ_WS_WIN
57 m_leftMargin = 1;
58#else
59 m_leftMargin = 0;
60#endif
61 }
62 else {//default
63#ifdef TQ_WS_WIN
64 m_leftMargin = 5;
65#else
66 m_leftMargin = 5;
67#endif
68 }
69
70 m_rightMargin = 0;
71 m_rightMarginWhenFocused = 0;
72}
73
74KexiTableEdit::~KexiTableEdit()
75{
76}
77
79{
80 if (m_column->visibleLookupColumnInfo)
81 return m_column->visibleLookupColumnInfo->field; //mainly for lookup field in KexiComboBoxTableEdit:
82
83 return m_column->field(); //typical case
84}
85
87{
88 m_view = v;
89 m_view->move(0,0);
90 m_view->installEventFilter(this);
91 setFocusProxy(m_view);
92}
93
94void KexiTableEdit::moveChild( TQWidget * child, int x, int y )
95{
96 if (m_scrollView)
97 m_scrollView->moveChild(child, x, y);
98}
99
100void KexiTableEdit::resize(int w, int h)
101{
102 TQWidget::resize(w, h);
103 if (m_view) {
104 if (!layout()) { //if there is layout (eg. KexiInputTableEdit), resize is automatic
105 m_view->move(0,0);
106 m_view->resize(w, h);
107 }
108 }
109}
110
111bool
112KexiTableEdit::eventFilter(TQObject* watched, TQEvent* e)
113{
114/* if (watched == m_view) {
115 if(e->type() == TQEvent::KeyPress) {
116 TQKeyEvent* ev = static_cast<TQKeyEvent*>(e);
117// if (ev->key()==Key_Tab) {
118
119// }
120 }
121 }*/
122
123 if(watched == this)
124 {
125 if(e->type() == TQEvent::KeyPress)
126 {
127 TQKeyEvent* ev = static_cast<TQKeyEvent*>(e);
128
129 if(ev->key() == Key_Escape)
130 {
131 return false;
132 }
133 }
134 else
135 {
136 return false;
137 }
138 }
139 return false;
140// return TQWidget::eventFilter(watched, e);
141}
142
143void KexiTableEdit::paintFocusBorders( TQPainter *p, TQVariant &, int x, int y, int w, int h )
144{
145 p->drawRect(x, y, w, h);
146}
147
148void KexiTableEdit::setupContents( TQPainter *p, bool focused, const TQVariant& val,
149 TQString &txt, int &align, int &/*x*/, int &y_offset, int &w, int &h )
150{
151 Q_UNUSED(p);
152 Q_UNUSED(focused);
153 Q_UNUSED(h);
154 KexiDB::Field *realField = displayedField();
155
156#ifdef TQ_WS_WIN
157// x = 1;
158 y_offset = -1;
159#else
160// x = 1;
161 y_offset = 0;
162#endif
163
164 if (realField->isFPNumericType()) {
166 if (!val.isNull()) {
168 val.toDouble(), realField->visibleDecimalPlaces());
169 }
170 w -= 6;
171 align |= AlignRight;
172 }
173 else if (realField->isIntegerType()) {
174 TQ_LLONG num = val.toLongLong();
175 w -= 6;
176 align |= AlignRight;
177 if (!val.isNull())
178 txt = TQString::number(num);
179 }
180 else {//default:
181 if (!val.isNull()) {
182 txt = val.toString();
183 }
184 align |= AlignLeft;
185 }
186}
187
188void KexiTableEdit::paintSelectionBackground( TQPainter *p, bool /*focused*/,
189 const TQString& txt, int align, int x, int y_offset, int w, int h, const TQColor& fillColor,
190 const TQFontMetrics &fm, bool readOnly, bool fullRowSelection )
191{
192 if (!readOnly && !fullRowSelection && !txt.isEmpty()) {
193 TQRect bound=fm.boundingRect(x, y_offset, w - (x+x), h, align, txt);
194 bound.setY(0);
195 bound.setWidth( TQMIN( bound.width()+2, w - (x+x)+1 ) );
196 if (align & TQt::AlignLeft) {
197 bound.setX(bound.x()-1);
198 }
199 else if (align & TQt::AlignRight) {
200 bound.moveLeft( w - bound.width() ); //move to left, if too wide
201 }
202//TODO align center
203 bound.setHeight(h-1);
204 p->fillRect(bound, fillColor);
205 }
206 else if (fullRowSelection) {
207 p->fillRect(0, 0, w, h, fillColor);
208 }
209}
210
211int KexiTableEdit::widthForValue( TQVariant &val, const TQFontMetrics &fm )
212{
213 return fm.width( val.toString() );
214}
215
217{
218 if (dynamic_cast<KexiDataAwareObjectInterface*>(m_scrollView))
219 dynamic_cast<KexiDataAwareObjectInterface*>(m_scrollView)->updateCurrentCell();
220}
221
222bool KexiTableEdit::showToolTipIfNeeded(const TQVariant& value, const TQRect& rect, const TQFontMetrics& fm,
223 bool focused)
224{
225 Q_UNUSED(value);
226 Q_UNUSED(rect);
227 Q_UNUSED(fm);
228 Q_UNUSED(focused);
229 return false;
230}
231
232int KexiTableEdit::rightMargin(bool focused) const
233{
234 return focused ? m_rightMarginWhenFocused : m_rightMargin;
235}
236
237#include "kexitableedit.moc"
Meta-data for a field.
Definition field.h:73
bool isFPNumericType() const
Definition field.h:266
bool isIntegerType() const
Definition field.h:259
int visibleDecimalPlaces() const
Definition field.h:361
The KexiDataAwareObjectInterface is an interface for record-based data object.
virtual TQVariant value()=0
void repaintRelatedCell()
virtual bool showToolTipIfNeeded(const TQVariant &value, const TQRect &rect, const TQFontMetrics &fm, bool focused)
virtual void setupContents(TQPainter *p, bool focused, const TQVariant &val, TQString &txt, int &align, int &x, int &y_offset, int &w, int &h)
virtual void resize(int w, int h)
virtual void paintSelectionBackground(TQPainter *p, bool focused, const TQString &txt, int align, int x, int y_offset, int w, int h, const TQColor &fillColor, const TQFontMetrics &fm, bool readOnly, bool fullRowSelection)
void setViewWidget(TQWidget *v)
KexiDB::Field * displayedField() const
void moveChild(TQWidget *child, int x, int y)
TQScrollView * m_scrollView
may be 0 if the parent is not a scrollview
int rightMargin(bool focused) const
virtual int widthForValue(TQVariant &val, const TQFontMetrics &fm)
virtual void paintFocusBorders(TQPainter *p, TQVariant &cal, int x, int y, int w, int h)
KexiDB::Field * field() const
KexiDB::QueryColumnInfo * visibleLookupColumnInfo
KEXI_DB_EXPORT TQString formatNumberForVisibleDecimalPlaces(double value, int decimalPlaces)