kexi

kexidbtextedit.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
00003    Copyright (C) 2004-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 "kexidbtextedit.h"
00022 #include "kexidblineedit.h"
00023 #include <kexidb/queryschema.h>
00024 
00025 #include <kapplication.h>
00026 #include <kstdaccel.h>
00027 #include <kdebug.h>
00028 
00029 #include <qpainter.h>
00030 
00031 KexiDBTextEdit::KexiDBTextEdit(QWidget *parent, const char *name)
00032  : KTextEdit(parent, name)
00033  , KexiDBTextWidgetInterface()
00034  , KexiFormDataItemInterface()
00035  , m_menuExtender(this, this)
00036  , m_slotTextChanged_enabled(true)
00037 {
00038     connect(this, SIGNAL(textChanged()), this, SLOT(slotTextChanged()));
00039     installEventFilter(this);
00040 }
00041 
00042 KexiDBTextEdit::~KexiDBTextEdit()
00043 {
00044 }
00045 
00046 void KexiDBTextEdit::setInvalidState( const QString& displayText )
00047 {
00048     setReadOnly(true);
00050     if (focusPolicy() & TabFocus)
00051         setFocusPolicy(QWidget::ClickFocus);
00052     KTextEdit::setText(displayText);
00053 }
00054 
00055 void KexiDBTextEdit::setValueInternal(const QVariant& add, bool removeOld)
00056 {
00057     if (m_columnInfo && m_columnInfo->field->type()==KexiDB::Field::Boolean) {
00059         KTextEdit::setText( add.toBool() ? "1" : "0" );
00060     }
00061     else {
00062         if (removeOld)
00063             KTextEdit::setText( add.toString() );
00064         else
00065             KTextEdit::setText( m_origValue.toString() + add.toString() );
00066     }
00067 }
00068 
00069 QVariant KexiDBTextEdit::value()
00070 {
00071     return text();
00072 }
00073 
00074 void KexiDBTextEdit::slotTextChanged()
00075 {
00076     if (!m_slotTextChanged_enabled)
00077         return;
00078     signalValueChanged();
00079 }
00080 
00081 bool KexiDBTextEdit::valueIsNull()
00082 {
00083     return text().isNull();
00084 }
00085 
00086 bool KexiDBTextEdit::valueIsEmpty()
00087 {
00088     return text().isEmpty();
00089 }
00090 
00091 bool KexiDBTextEdit::isReadOnly() const
00092 {
00093     return KTextEdit::isReadOnly();
00094 }
00095 
00096 void KexiDBTextEdit::setReadOnly( bool readOnly )
00097 {
00098     KTextEdit::setReadOnly( readOnly );
00099     QPalette p = palette();
00100     QColor c(readOnly ? lighterGrayBackgroundColor(kapp->palette()) : p.color(QPalette::Normal, QColorGroup::Base));
00101     setPaper( c );
00102     p.setColor(QColorGroup::Base, c);
00103     p.setColor(QColorGroup::Background, c);
00104     setPalette( p );
00105 }
00106 
00107 void KexiDBTextEdit::setText( const QString & text, const QString & context )
00108 {
00109     KTextEdit::setText(text, context);
00110 }
00111 
00112 QWidget* KexiDBTextEdit::widget()
00113 {
00114     return this;
00115 }
00116 
00117 bool KexiDBTextEdit::cursorAtStart()
00118 {
00119     int para, index;
00120     getCursorPosition ( &para, &index );
00121     return para==0 && index==0;
00122 }
00123 
00124 bool KexiDBTextEdit::cursorAtEnd()
00125 {
00126     int para, index;
00127     getCursorPosition ( &para, &index );
00128     return (paragraphs()-1)==para && (paragraphLength(paragraphs()-1)-1)==index;
00129 }
00130 
00131 void KexiDBTextEdit::clear()
00132 {
00133     setText(QString::null, QString::null);
00134 }
00135 
00136 void KexiDBTextEdit::setColumnInfo(KexiDB::QueryColumnInfo* cinfo)
00137 {
00138     KexiFormDataItemInterface::setColumnInfo(cinfo);
00139     if (!cinfo)
00140         return;
00141     KexiDBTextWidgetInterface::setColumnInfo(m_columnInfo, this);
00142 }
00143 
00144 void KexiDBTextEdit::paintEvent ( QPaintEvent *pe )
00145 {
00146     KTextEdit::paintEvent( pe );
00147     QPainter p(this);
00148     KexiDBTextWidgetInterface::paint( this, &p, text().isEmpty(), alignment(), hasFocus() );
00149 }
00150 
00151 QPopupMenu * KexiDBTextEdit::createPopupMenu(const QPoint & pos)
00152 {
00153     QPopupMenu *contextMenu = KTextEdit::createPopupMenu(pos);
00154     m_menuExtender.createTitle(contextMenu);
00155     return contextMenu;
00156 }
00157 
00158 void KexiDBTextEdit::undo()
00159 {
00160     cancelEditor();
00161 }
00162 
00163 void KexiDBTextEdit::setDisplayDefaultValue(QWidget* widget, bool displayDefaultValue)
00164 {
00165     KexiFormDataItemInterface::setDisplayDefaultValue(widget, displayDefaultValue);
00166     // initialize display parameters for default / entered value
00167     KexiDisplayUtils::DisplayParameters * const params 
00168         = displayDefaultValue ? m_displayParametersForDefaultValue : m_displayParametersForEnteredValue;
00169     QPalette pal(palette());
00170     pal.setColor(QPalette::Active, QColorGroup::Text, params->textColor);
00171     setPalette(pal);
00172     setFont(params->font);
00174 /*  m_slotTextChanged_enabled = false;
00175         //for rich text...
00176         const QString origText( text() );
00177         KTextEdit::setText(QString::null);
00178         setCurrentFont(params->font);
00179         setColor(params->textColor);
00180         KTextEdit::setText(origText);
00181     m_slotTextChanged_enabled = true;*/
00182 }
00183 
00184 void KexiDBTextEdit::moveCursorToEnd()
00185 {
00186     KTextEdit::setCursorPosition(paragraphs()-1, paragraphLength( paragraphs()-1 ));
00187 }
00188 
00189 void KexiDBTextEdit::moveCursorToStart()
00190 {
00191     KTextEdit::setCursorPosition(0 /*para*/, 0 /*index*/);
00192 }
00193 
00194 void KexiDBTextEdit::selectAll()
00195 {
00196     KTextEdit::selectAll();
00197 }
00198 
00199 void KexiDBTextEdit::keyPressEvent( QKeyEvent *ke )
00200 {
00201     // for instance, Windows uses Ctrl+Tab for moving between tabs, so do not steal this shortcut
00202     if (KStdAccel::tabNext().contains( KKey(ke) ) || KStdAccel::tabPrev().contains( KKey(ke) )) {
00203         ke->ignore();
00204         return;
00205     }
00206     KTextEdit::keyPressEvent(ke);
00207 }
00208 
00209 #include "kexidbtextedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys