kexi

kexitableedit.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Peter Simonsson <psn@linux.se>
00003    Copyright (C) 2003-2005 Jaroslaw Staniek <js@iidea.pl>
00004 
00005    This program is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this program; see the file COPYING.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
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 //KexiTableEdit::KexiTableEdit(KexiDB::Field &f, QScrollView* parent, const char* name)
00032 KexiTableEdit::KexiTableEdit(KexiTableViewColumn &column, QScrollView* parent, const char* name)
00033 : QWidget(parent->viewport(), name)
00034 , m_column(&column)
00035 // ,m_field(&f)
00036 // ,m_type(f.type()) //copied because the rest of code uses m_type
00037  ,m_scrollView(parent)
00038  ,m_view(0)
00039 // ,m_hasFocusableWidget(true)
00040 // ,m_acceptEditorAfterDeleteContents(false)
00041 {
00042     setPaletteBackgroundColor( palette().color(QPalette::Active, QColorGroup::Base) );
00043     installEventFilter(this);
00044 
00045     //margins
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 {//default
00061 #ifdef Q_WS_WIN
00062         m_leftMargin = 5;
00063 #else
00064         m_leftMargin = 5;
00065 #endif
00066     }
00067 
00068 
00069     m_rightMargin = 0;//TODO
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()) { //if there is layout (eg. KexiInputTableEdit), resize is automatic
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 /*  if (watched == m_view) {
00099         if(e->type() == QEvent::KeyPress) {
00100             QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00101 //          if (ev->key()==Key_Tab) {
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 //  return QWidget::eventFilter(watched, e);
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 * /*p*/, bool /*focused*/, QVariant val, 
00133     QString &txt, int &align, int &/*x*/, int &y_offset, int &w, int &/*h*/  )
00134 {
00135 #ifdef Q_WS_WIN
00136 //  x = 1;
00137     y_offset = -1;
00138 #else
00139 //  x = 1;
00140     y_offset = 0;
00141 #endif
00142 
00143 //  const int ctype = columnType(col);
00144 //  align = SingleLine | AlignVCenter;
00145 //  QString txt; //text to draw
00146     if (m_column->field()->isFPNumericType()) {
00147 //js TODO: ADD OPTION to desplaying NULL VALUES as e.g. "(null)"
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 /*#ifdef Q_WS_WIN
00156         x = 1;
00157 #else
00158         x = 0;
00159 #endif*/
00160         w -= 6;
00161         align |= AlignRight;
00162         if (!val.isNull())
00163             txt = QString::number(num);
00164     }
00165 /*MOVE TO Date editor!!!!
00166 else if (m_field->type() == KexiDB::Field::Date) { //todo: datetime & time
00167 #ifdef Q_WS_WIN
00168         x = 5;
00169 #else
00170         x = 5;
00171 #endif
00172         if(cell_value.toDate().isValid())
00173         {
00174 #ifdef USE_KDE
00175             txt = KGlobal::locale()->formatDate(cell_value.toDate(), true);
00176 #else
00177             if (!cell_value.isNull())
00178                 txt = cell_value.toDate().toString(Qt::LocalDate);
00179 #endif
00180         }
00181         align |= AlignLeft;
00182     }*/
00183     else {//default:
00184 /*#ifdef Q_WS_WIN
00185         x = 5;
00186 //      y_offset = -1;
00187 #else
00188         x = 5;
00189 //      y_offset = 0;
00190 #endif*/
00191 //      switch (m_column->field()->type()) {
00192 /*moved     case KexiDB::Field::Time:
00193             //it was QDateTime - a hack needed because QVariant(QTime) has broken isNull()
00194             if (!val.isNull()) {
00195                 txt = val.toTime().toString(Qt::LocalDate);
00196             }
00197             break;
00198         case KexiDB::Field::Date:
00199             if (val.toDate().isValid())
00200                 txt = val.toDate().toString(Qt::LocalDate);
00201             break;
00202         case KexiDB::Field::DateTime:
00203             if (!val.toDateTime().isNull())
00204                 txt = val.toDate().toString(Qt::LocalDate) + " " + val.toTime().toString(Qt::LocalDate);
00205             break;*/
00206 //      default:
00207         if (!val.isNull()) {
00208             txt = val.toString();
00209         }
00210 //      }
00211         align |= AlignLeft;
00212     }
00213 }
00214 
00215 void KexiTableEdit::paintSelectionBackground( QPainter *p, bool /*focused*/, 
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() ); //move to left, if too wide
00228         }
00229 //TODO align center
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"
KDE Home | KDE Accessibility Home | Description of Access Keys