00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kexitableedit.h"
00022 #include <kexidb/field.h>
00023
00024 #include <qpalette.h>
00025 #include <qpainter.h>
00026
00027 #include <kglobal.h>
00028 #include <klocale.h>
00029 #include <kdebug.h>
00030
00031
00032 KexiTableEdit::KexiTableEdit(KexiTableViewColumn &column, QScrollView* parent, const char* name)
00033 : QWidget(parent->viewport(), name)
00034 , m_column(&column)
00035
00036
00037 ,m_scrollView(parent)
00038 ,m_view(0)
00039
00040
00041 {
00042 setPaletteBackgroundColor( palette().color(QPalette::Active, QColorGroup::Base) );
00043 installEventFilter(this);
00044
00045
00046 if (m_column->field()->isFPNumericType()) {
00047 #ifdef Q_WS_WIN
00048 m_leftMargin = 0;
00049 #else
00050 m_leftMargin = 0;
00051 #endif
00052 }
00053 else if (m_column->field()->isIntegerType()) {
00054 #ifdef Q_WS_WIN
00055 m_leftMargin = 1;
00056 #else
00057 m_leftMargin = 0;
00058 #endif
00059 }
00060 else {
00061 #ifdef Q_WS_WIN
00062 m_leftMargin = 5;
00063 #else
00064 m_leftMargin = 5;
00065 #endif
00066 }
00067
00068
00069 m_rightMargin = 0;
00070 }
00071
00072 KexiTableEdit::~KexiTableEdit()
00073 {
00074 }
00075
00076 void KexiTableEdit::setViewWidget(QWidget *v)
00077 {
00078 m_view = v;
00079 m_view->move(0,0);
00080 m_view->installEventFilter(this);
00081 setFocusProxy(m_view);
00082 }
00083
00084 void KexiTableEdit::resize(int w, int h)
00085 {
00086 QWidget::resize(w, h);
00087 if (m_view) {
00088 if (!layout()) {
00089 m_view->move(0,0);
00090 m_view->resize(w, h);
00091 }
00092 }
00093 }
00094
00095 bool
00096 KexiTableEdit::eventFilter(QObject* watched, QEvent* e)
00097 {
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 if(watched == this)
00108 {
00109 if(e->type() == QEvent::KeyPress)
00110 {
00111 QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00112
00113 if(ev->key() == Key_Escape)
00114 {
00115 return false;
00116 }
00117 }
00118 else
00119 {
00120 return false;
00121 }
00122 }
00123 return false;
00124
00125 }
00126
00127 void KexiTableEdit::paintFocusBorders( QPainter *p, QVariant &, int x, int y, int w, int h )
00128 {
00129 p->drawRect(x, y, w, h);
00130 }
00131
00132 void KexiTableEdit::setupContents( QPainter * , bool , QVariant val,
00133 QString &txt, int &align, int &, int &y_offset, int &w, int & )
00134 {
00135 #ifdef Q_WS_WIN
00136
00137 y_offset = -1;
00138 #else
00139
00140 y_offset = 0;
00141 #endif
00142
00143
00144
00145
00146 if (m_column->field()->isFPNumericType()) {
00147
00148 if (!val.isNull())
00149 txt = KGlobal::locale()->formatNumber(val.toDouble());
00150 w -= 6;
00151 align |= AlignRight;
00152 }
00153 else if (m_column->field()->isIntegerType()) {
00154 Q_LLONG num = val.toLongLong();
00155
00156
00157
00158
00159
00160 w -= 6;
00161 align |= AlignRight;
00162 if (!val.isNull())
00163 txt = QString::number(num);
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 else {
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 if (!val.isNull()) {
00208 txt = val.toString();
00209 }
00210
00211 align |= AlignLeft;
00212 }
00213 }
00214
00215 void KexiTableEdit::paintSelectionBackground( QPainter *p, bool ,
00216 const QString& txt, int align, int x, int y_offset, int w, int h, const QColor& fillColor,
00217 bool readOnly, bool fullRowSelection )
00218 {
00219 if (!readOnly && !fullRowSelection && !txt.isEmpty()) {
00220 QRect bound=fontMetrics().boundingRect(x, y_offset, w - (x+x), h, align, txt);
00221 bound.setY(0);
00222 bound.setWidth( QMIN( bound.width()+2, w - (x+x)+1 ) );
00223 if (align & Qt::AlignLeft) {
00224 bound.setX(bound.x()-1);
00225 }
00226 else if (align & Qt::AlignRight) {
00227 bound.moveLeft( w - bound.width() );
00228 }
00229
00230 bound.setHeight(h-1);
00231 p->fillRect(bound, fillColor);
00232 }
00233 else if (fullRowSelection) {
00234 p->fillRect(0, 0, w, h, fillColor);
00235 }
00236 }
00237
00238 int KexiTableEdit::widthForValue( QVariant &val, QFontMetrics &fm )
00239 {
00240 return fm.width( val.toString() );
00241 }
00242
00243
00244 #include "kexitableedit.moc"