kexi

kexicomboboxpopup.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "kexicomboboxpopup.h"
00021 
00022 #include "kexitableview.h"
00023 #include "kexitableview_p.h"
00024 #include "kexitableitem.h"
00025 #include "kexitableedit.h"
00026 
00027 #include <kdebug.h>
00028 
00029 #include <qlayout.h>
00030 #include <qevent.h>
00031 
00033 class KexiComboBoxPopupPrivate
00034 {
00035     public:
00036         KexiComboBoxPopupPrivate() 
00037          : int_f(0)
00038         {
00039             max_rows = KexiComboBoxPopup::defaultMaxRows;
00040         }
00041         ~KexiComboBoxPopupPrivate() {
00042             delete int_f;
00043         }
00044         
00045         KexiTableView *tv;
00046         KexiDB::Field *int_f; //TODO: remove this -temporary
00047         int max_rows;
00048 };
00049 
00050 //========================================
00051 
00054 class KexiComboBoxPopup_KexiTableView : public KexiTableView
00055 {
00056     public:
00057         KexiComboBoxPopup_KexiTableView(QWidget* parent=0)
00058          : KexiTableView(0, parent, "KexiComboBoxPopup_tv")
00059         {
00060             setReadOnly( true );
00061             setLineWidth( 0 );
00062             d->moveCursorOnMouseRelease = true;
00063         //  setBackgroundAltering( false ); 
00064             KexiTableView::Appearance a(appearance());
00065             a.navigatorEnabled = false;
00066             a.backgroundAltering = false; //TODO add option??
00067             a.fullRowSelection = true;
00068             a.rowHighlightingEnabled = true;
00069             a.rowMouseOverHighlightingEnabled = true;
00070             a.persistentSelections = false;
00071             a.rowMouseOverHighlightingColor = colorGroup().highlight();
00072             a.rowMouseOverHighlightingTextColor = colorGroup().highlightedText();
00073             a.rowHighlightingTextColor = a.rowMouseOverHighlightingTextColor;
00074             a.gridEnabled = false;
00075             setAppearance(a);
00076             setInsertingEnabled( false );
00077             setSortingEnabled( false );
00078             setVerticalHeaderVisible( false );
00079             setHorizontalHeaderVisible( false );
00080             setColumnStretchEnabled( true, -1 );
00081             setContextMenuEnabled( false );
00082             setScrollbarToolTipsEnabled( false );
00083             installEventFilter(this);
00084             setBottomMarginInternal( - horizontalScrollBar()->sizeHint().height() );
00085         }
00086 };
00087 
00088 //========================================
00089 
00090 const int KexiComboBoxPopup::defaultMaxRows = 8;
00091 
00092 
00093 KexiComboBoxPopup::KexiComboBoxPopup(QWidget* parent, KexiDB::Field &f)
00094  : QFrame( parent, "KexiComboBoxPopup", WType_Popup )
00095 {
00096     init();
00097     //setup tv data
00098     setData(f);
00099 }
00100 
00101 KexiComboBoxPopup::KexiComboBoxPopup(QWidget* parent, KexiTableViewColumn &column)
00102  : QFrame( parent, "KexiComboBoxPopup", WType_Popup )
00103 {
00104     init();
00105     //setup tv data
00106     setData(column);
00107 }
00108 
00109 KexiComboBoxPopup::~KexiComboBoxPopup()
00110 {
00111     delete d;
00112 }
00113 
00114 void KexiComboBoxPopup::init()
00115 {
00116     d = new KexiComboBoxPopupPrivate();
00117     setPaletteBackgroundColor(palette().color(QPalette::Active,QColorGroup::Base));
00118     setLineWidth( 1 );
00119     setFrameStyle( Box | Plain );
00120     
00121 //  QVBoxLayout *lyr = new QVBoxLayout(this, 1);
00122     d->tv = new KexiComboBoxPopup_KexiTableView(this);
00123     installEventFilter(this);
00124     
00125     connect(d->tv, SIGNAL(itemReturnPressed(KexiTableItem*,int,int)),
00126         this, SLOT(slotTVItemAccepted(KexiTableItem*,int,int)));
00127 
00128     connect(d->tv, SIGNAL(itemMouseReleased(KexiTableItem*,int,int)),
00129         this, SLOT(slotTVItemAccepted(KexiTableItem*,int,int)));
00130 
00131     connect(d->tv, SIGNAL(itemDblClicked(KexiTableItem*,int,int)),
00132         this, SLOT(slotTVItemAccepted(KexiTableItem*,int,int)));
00133 
00134 //  lyr->addWidget(d->tv);
00135 }
00136 
00137 void KexiComboBoxPopup::setData(KexiDB::Field &f)
00138 {
00139 //j: TODO: THIS IS PRIMITIVE: we'd need to employ KexiDB::Reference here!
00140     d->int_f = new KexiDB::Field(f.name(), KexiDB::Field::Text);
00141     KexiTableViewData *data = new KexiTableViewData();
00142 //  data->setReadOnly( true );
00143     data->addColumn( new KexiTableViewColumn( *d->int_f ) );
00144     QValueVector<QString> hints = f.enumHints();
00145     for(uint i=0; i < hints.size(); i++) {
00146         KexiTableItem *item = data->createItem();//new KexiTableItem(1);
00147         (*item)[0]=QVariant(hints[i]);
00148         kdDebug() << "added: '" << hints[i] <<"'"<<endl;
00149         data->append( item );
00150     }
00151     setDataInternal( data, true );
00152 }
00153 
00154 void KexiComboBoxPopup::setData(KexiTableViewColumn &column)
00155 {
00156     if (!column.relatedData()) {
00157         kdWarning() << "KexiComboBoxPopup::setData(KexiTableViewColumn &): no column relatedData \n - moving to setData(KexiDB::Field &)" << endl;
00158         setData(*column.field());
00159         return;
00160     }
00161     setDataInternal( column.relatedData(), false  );
00162 }
00163 
00164 void KexiComboBoxPopup::setDataInternal( KexiTableViewData *data, bool owner )
00165 {
00166     if (d->tv->data())
00167         d->tv->data()->disconnect( this );
00168     d->tv->setData( data, owner );
00169     connect( d->tv, SIGNAL(dataRefreshed()), this, SLOT(slotDataReloadRequested()));
00170 //  connect( data, SIGNAL(refreshRequested()), this, SLOT(slotDataRefreshRequested()));
00171 
00172     updateSize();
00173 }
00174 
00175 void KexiComboBoxPopup::updateSize(int minWidth)
00176 {
00177     d->tv->setColumnStretchEnabled( true, -1 );
00178 //  d->tv->adjustColumnWidthToContents( -1 ); //TODO: not only for column 0, if there are more columns!
00179 //                                           //TODO: check if the width is not too big
00180     d->tv->adjustHorizontalHeaderSize();
00181 //  d->tv->adjustColumnWidthToContents( 0 ); //TODO: not only for column 0, if there are more columns!
00182 //  d->tv->adjustColumnWidthToContents( 0 ); //TODO: not only for column 0, if there are more columns!
00183 //                                           //TODO: check if the width is not too big
00184     const int rows = QMIN( d->max_rows, d->tv->rows() );
00185     KexiTableEdit *te = dynamic_cast<KexiTableEdit*>(parentWidget());
00186     const int width = QMAX( d->tv->tableSize().width(), 
00187         (te ? te->totalSize().width() : parentWidget()->width()/*sanity*/) );
00188 //  resize( QMAX(minWidth, width), d->tv->rowHeight() * rows +2 );
00189     resize( QMAX(minWidth, width), d->tv->rowHeight() * rows +2 );
00190 
00191 //  resize( d->tv->columnWidth( 0 ), d->tv->rowHeight() * QMIN( d->max_rows, d->tv->rows() ) +2 );
00192 }
00193 
00194 KexiTableView* KexiComboBoxPopup::tableView()
00195 {
00196     return d->tv;
00197 }
00198 
00199 void KexiComboBoxPopup::resize( int w, int h )
00200 {
00201     d->tv->horizontalScrollBar()->hide();
00202     d->tv->verticalScrollBar()->hide();
00203 //  hide();
00204     d->tv->move(1,1);
00205     d->tv->resize( w-2, h-2 );
00206     QFrame::resize(w,h);
00207     update();
00208     updateGeometry();
00209 //  show();
00210 }
00211 
00212 void KexiComboBoxPopup::setMaxRows(int r)
00213 {
00214     d->max_rows = r;
00215 }
00216 
00218 int KexiComboBoxPopup::maxRows() const
00219 {
00220     return d->max_rows;
00221 }
00222 
00223 void KexiComboBoxPopup::slotTVItemAccepted(KexiTableItem *item, int row, int)
00224 {
00225     hide();
00226     emit rowAccepted(item, row);
00227 }
00228 
00229 bool KexiComboBoxPopup::eventFilter( QObject *o, QEvent *e )
00230 {
00231     if (o==this && e->type()==QEvent::Hide) {
00232         emit hidden();
00233     }
00234     else if (e->type()==QEvent::MouseButtonPress) {
00235         kdDebug() << "QEvent::MousePress" << endl;
00236     }
00237     else if (o==d->tv) {
00238         if (e->type()==QEvent::KeyPress) {
00239             QKeyEvent *ke = static_cast<QKeyEvent*>(e);
00240             const int k = ke->key();
00241             if ((ke->state()==NoButton && (k==Key_Escape || k==Key_F4))
00242                 || (ke->state()==AltButton && k==Key_Up))
00243             {
00244                 hide();
00245                 emit cancelled();
00246                 return true;
00247             }
00248         }
00249     }
00250     return QFrame::eventFilter( o, e );
00251 }
00252 
00253 void KexiComboBoxPopup::slotDataReloadRequested()
00254 {
00255     updateSize();
00256 }
00257 
00258 
00259 #include "kexicomboboxpopup.moc"
00260 
KDE Home | KDE Accessibility Home | Description of Access Keys