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-2006 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 "kexidataawareobjectiface.h"
00023 #include <kexidb/field.h>
00024 #include <kexidb/utils.h>
00025 
00026 #include <qpalette.h>
00027 #include <qpainter.h>
00028 
00029 #include <kglobal.h>
00030 #include <klocale.h>
00031 #include <kdebug.h>
00032 
00033 KexiTableEdit::KexiTableEdit(KexiTableViewColumn &column, QWidget* parent)
00034 : QWidget(dynamic_cast<QScrollView*>(parent) ? dynamic_cast<QScrollView*>(parent)->viewport() : parent)
00035  ,m_column(&column)
00036 // ,m_field(&f)
00037 // ,m_type(f.type()) //copied because the rest of code uses m_type
00038  ,m_scrollView(dynamic_cast<QScrollView*>(parent))
00039  ,m_usesSelectedTextColor(true)
00040  ,m_view(0)
00041 // ,m_hasFocusableWidget(true)
00042 // ,m_acceptEditorAfterDeleteContents(false)
00043 {
00044     setPaletteBackgroundColor( palette().color(QPalette::Active, QColorGroup::Base) );
00045     installEventFilter(this);
00046 
00047     //margins
00048     if (displayedField()->isFPNumericType()) {
00049 #ifdef Q_WS_WIN
00050         m_leftMargin = 0;
00051 #else
00052         m_leftMargin = 0;
00053 #endif
00054     }
00055     else if (displayedField()->isIntegerType()) {
00056 #ifdef Q_WS_WIN
00057         m_leftMargin = 1;
00058 #else
00059         m_leftMargin = 0;
00060 #endif
00061     }
00062     else {//default
00063 #ifdef Q_WS_WIN
00064         m_leftMargin = 5;
00065 #else
00066         m_leftMargin = 5;
00067 #endif
00068     }
00069 
00070     m_rightMargin = 0;
00071     m_rightMarginWhenFocused = 0;
00072 }
00073 
00074 KexiTableEdit::~KexiTableEdit()
00075 {
00076 }
00077 
00078 KexiDB::Field *KexiTableEdit::displayedField() const
00079 {
00080     if (m_column->visibleLookupColumnInfo)
00081         return m_column->visibleLookupColumnInfo->field; //mainly for lookup field in KexiComboBoxTableEdit:
00082 
00083     return m_column->field(); //typical case
00084 }
00085 
00086 void KexiTableEdit::setViewWidget(QWidget *v)
00087 {
00088     m_view = v;
00089     m_view->move(0,0);
00090     m_view->installEventFilter(this);
00091     setFocusProxy(m_view);
00092 }
00093 
00094 void KexiTableEdit::moveChild( QWidget * child, int x, int y )
00095 {
00096     if (m_scrollView)
00097         m_scrollView->moveChild(child, x, y);
00098 }
00099 
00100 void KexiTableEdit::resize(int w, int h)
00101 {
00102     QWidget::resize(w, h);
00103     if (m_view) {
00104         if (!layout()) { //if there is layout (eg. KexiInputTableEdit), resize is automatic
00105             m_view->move(0,0);
00106             m_view->resize(w, h);
00107         }
00108     }
00109 }
00110 
00111 bool
00112 KexiTableEdit::eventFilter(QObject* watched, QEvent* e)
00113 {
00114 /*  if (watched == m_view) {
00115         if(e->type() == QEvent::KeyPress) {
00116             QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00117 //          if (ev->key()==Key_Tab) {
00118 
00119 //          }
00120         }
00121     }*/
00122 
00123     if(watched == this)
00124     {
00125         if(e->type() == QEvent::KeyPress)
00126         {
00127             QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00128 
00129             if(ev->key() == Key_Escape)
00130             {
00131                 return false;
00132             }
00133         }
00134         else
00135         {
00136             return false;
00137         }
00138     }
00139     return false;
00140 //  return QWidget::eventFilter(watched, e);
00141 }
00142 
00143 void KexiTableEdit::paintFocusBorders( QPainter *p, QVariant &, int x, int y, int w, int h )
00144 {
00145     p->drawRect(x, y, w, h);
00146 }
00147 
00148 void KexiTableEdit::setupContents( QPainter *p, bool focused, const QVariant& val, 
00149     QString &txt, int &align, int &/*x*/, int &y_offset, int &w, int &h  )
00150 {
00151     Q_UNUSED(p);
00152     Q_UNUSED(focused);
00153     Q_UNUSED(h);
00154     KexiDB::Field *realField = displayedField();
00155 
00156 #ifdef Q_WS_WIN
00157 //  x = 1;
00158     y_offset = -1;
00159 #else
00160 //  x = 1;
00161     y_offset = 0;
00162 #endif
00163 
00164     if (realField->isFPNumericType()) {
00166         if (!val.isNull()) {
00167             txt = KexiDB::formatNumberForVisibleDecimalPlaces(
00168                 val.toDouble(), realField->visibleDecimalPlaces());
00169         }
00170         w -= 6;
00171         align |= AlignRight;
00172     }
00173     else if (realField->isIntegerType()) {
00174         Q_LLONG num = val.toLongLong();
00175         w -= 6;
00176         align |= AlignRight;
00177         if (!val.isNull())
00178             txt = QString::number(num);
00179     }
00180     else {//default:
00181         if (!val.isNull()) {
00182             txt = val.toString();
00183         }
00184         align |= AlignLeft;
00185     }
00186 }
00187 
00188 void KexiTableEdit::paintSelectionBackground( QPainter *p, bool /*focused*/, 
00189     const QString& txt, int align, int x, int y_offset, int w, int h, const QColor& fillColor,
00190     const QFontMetrics &fm, bool readOnly, bool fullRowSelection )
00191 {
00192     if (!readOnly && !fullRowSelection && !txt.isEmpty()) {
00193         QRect bound=fm.boundingRect(x, y_offset, w - (x+x), h, align, txt);
00194         bound.setY(0);
00195         bound.setWidth( QMIN( bound.width()+2, w - (x+x)+1 ) );
00196         if (align & Qt::AlignLeft) {
00197             bound.setX(bound.x()-1);
00198         }
00199         else if (align & Qt::AlignRight) {
00200             bound.moveLeft( w - bound.width() ); //move to left, if too wide
00201         }
00202 //TODO align center
00203         bound.setHeight(h-1);
00204         p->fillRect(bound, fillColor);
00205     }
00206     else if (fullRowSelection) {
00207         p->fillRect(0, 0, w, h, fillColor);
00208     }
00209 }
00210 
00211 int KexiTableEdit::widthForValue( QVariant &val, const QFontMetrics &fm )
00212 {
00213     return fm.width( val.toString() );
00214 }
00215 
00216 void KexiTableEdit::repaintRelatedCell()
00217 {
00218     if (dynamic_cast<KexiDataAwareObjectInterface*>(m_scrollView))
00219         dynamic_cast<KexiDataAwareObjectInterface*>(m_scrollView)->updateCurrentCell();
00220 }
00221 
00222 bool KexiTableEdit::showToolTipIfNeeded(const QVariant& value, const QRect& rect, const QFontMetrics& fm,
00223     bool focused)
00224 {
00225     Q_UNUSED(value);
00226     Q_UNUSED(rect);
00227     Q_UNUSED(fm);
00228     Q_UNUSED(focused);
00229     return false;
00230 }
00231 
00232 int KexiTableEdit::rightMargin(bool focused) const
00233 {
00234     return focused ? m_rightMarginWhenFocused : m_rightMargin;
00235 }
00236 
00237 #include "kexitableedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys