kexi

kexilookupcolumnpage.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library 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 library 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 library; see the file COPYING.LIB.  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 "kexilookupcolumnpage.h"
00021 
00022 #include <qlabel.h>
00023 #include <qlayout.h>
00024 #include <qtooltip.h>
00025 #include <qheader.h>
00026 
00027 #include <kiconloader.h>
00028 #include <klocale.h>
00029 #include <ktoolbarbutton.h>
00030 #include <kdebug.h>
00031 #include <kpopupmenu.h>
00032 
00033 #include <widget/kexipropertyeditorview.h>
00034 #include <widget/kexidatasourcecombobox.h>
00035 #include <widget/kexifieldlistview.h>
00036 #include <widget/kexifieldcombobox.h>
00037 #include <widget/kexismalltoolbutton.h>
00038 #include <kexidb/connection.h>
00039 #include <kexiproject.h>
00040 
00041 #include <koproperty/property.h>
00042 #include <koproperty/utils.h>
00043 
00044 QString mimeTypeToType(const QString& mimeType)
00045 {
00046     if (mimeType=="kexi/table")
00047         return "table";
00048     else if (mimeType=="kexi/query")
00049         return "query";
00051     return mimeType;
00052 }
00053 
00054 QString typeToMimeType(const QString& type)
00055 {
00056     if (type=="table")
00057         return "kexi/table";
00058     else if (type=="query")
00059         return "kexi/query";
00061     return type;
00062 }
00063 
00064 //----------------------------------------------
00065 
00067 class KexiLookupColumnPage::Private
00068 {
00069     public:
00070         Private()
00071          : currentFieldUid(-1)
00072          , insideClearRowSourceSelection(false)
00073          , propertySetEnabled(true)
00074         {
00075         }
00076         ~Private()
00077         {
00078         }
00079 
00080         bool hasPropertySet() const {
00081             return propertySet;
00082         }
00083 
00084         void setPropertySet(KoProperty::Set* aPropertySet) {
00085             propertySet = aPropertySet;
00086         }
00087 
00088         QVariant propertyValue(const QCString& propertyName) const {
00089             return propertySet ? propertySet->property(propertyName).value() : QVariant();
00090         }
00091 
00092         void changeProperty(const QCString &property, const QVariant &value)
00093         {
00094             if (!propertySetEnabled)
00095                 return;
00096             propertySet->changeProperty(property, value);
00097         }
00098 
00099         void updateInfoLabelForPropertySet(const QString& textToDisplayForNullSet) {
00100             KexiPropertyEditorView::updateInfoLabelForPropertySet(
00101                 objectInfoLabel, propertySet, textToDisplayForNullSet);
00102         }
00103 
00104         KexiDataSourceComboBox *rowSourceCombo;
00105         KexiFieldComboBox *boundColumnCombo, *visibleColumnCombo;
00106         KexiObjectInfoLabel *objectInfoLabel;
00107         QLabel *rowSourceLabel, *boundColumnLabel, *visibleColumnLabel;
00108         QToolButton *clearRowSourceButton, *gotoRowSourceButton, *clearBoundColumnButton,
00109             *clearVisibleColumnButton;
00111         int currentFieldUid;
00112 
00113         bool insideClearRowSourceSelection : 1;
00115         bool propertySetEnabled : 1;
00116 
00117     private:
00120         QGuardedPtr<KoProperty::Set> propertySet;
00121 };
00122 
00123 //----------------------------------------------
00124 
00125 KexiLookupColumnPage::KexiLookupColumnPage(QWidget *parent)
00126  : QWidget(parent)
00127  , d(new Private())
00128 {
00129     setName("KexiLookupColumnPage");
00130 
00131     QVBoxLayout *vlyr = new QVBoxLayout(this);
00132     d->objectInfoLabel = new KexiObjectInfoLabel(this, "KexiObjectInfoLabel");
00133     vlyr->addWidget(d->objectInfoLabel);
00134 
00135 //todo  d->noDataSourceAvailableSingleText = i18n("No data source could be assigned for this widget.");
00136 //todo  d->noDataSourceAvailableMultiText = i18n("No data source could be assigned for multiple widgets.");
00137 
00138     //-Row Source
00139     QWidget *contents = new QWidget(this);
00140     vlyr->addWidget(contents);
00141     QVBoxLayout *contentsVlyr = new QVBoxLayout(contents);
00142 
00143     QHBoxLayout *hlyr = new QHBoxLayout(contentsVlyr);
00144     d->rowSourceLabel = new QLabel(i18n("Row source:"), contents);
00145     d->rowSourceLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
00146     d->rowSourceLabel->setMargin(2);
00147     d->rowSourceLabel->setMinimumHeight(IconSize(KIcon::Small)+4);
00148     d->rowSourceLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
00149     hlyr->addWidget(d->rowSourceLabel);
00150 
00151     d->gotoRowSourceButton = new KexiSmallToolButton(contents, QString::null, "goto", "gotoRowSourceButton");
00152     d->gotoRowSourceButton->setMinimumHeight(d->rowSourceLabel->minimumHeight());
00153     QToolTip::add(d->gotoRowSourceButton, i18n("Go to selected row source"));
00154     hlyr->addWidget(d->gotoRowSourceButton);
00155     connect(d->gotoRowSourceButton, SIGNAL(clicked()), this, SLOT(slotGotoSelectedRowSource()));
00156 
00157     d->clearRowSourceButton = new KexiSmallToolButton(contents, QString::null,
00158         "clear_left", "clearRowSourceButton");
00159     d->clearRowSourceButton->setMinimumHeight(d->rowSourceLabel->minimumHeight());
00160     QToolTip::add(d->clearRowSourceButton, i18n("Clear row source"));
00161     hlyr->addWidget(d->clearRowSourceButton);
00162     connect(d->clearRowSourceButton, SIGNAL(clicked()), this, SLOT(clearRowSourceSelection()));
00163 
00164     d->rowSourceCombo = new KexiDataSourceComboBox(contents, "rowSourceCombo");
00165     d->rowSourceLabel->setBuddy(d->rowSourceCombo);
00166     contentsVlyr->addWidget(d->rowSourceCombo);
00167 
00168     contentsVlyr->addSpacing(8);
00169 
00170     //- Bound Column
00171     hlyr = new QHBoxLayout(contentsVlyr);
00172     d->boundColumnLabel = new QLabel(i18n("Bound column:"), contents);
00173     d->boundColumnLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
00174     d->boundColumnLabel->setMargin(2);
00175     d->boundColumnLabel->setMinimumHeight(IconSize(KIcon::Small)+4);
00176     d->boundColumnLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
00177     hlyr->addWidget(d->boundColumnLabel);
00178 
00179     d->clearBoundColumnButton = new KexiSmallToolButton(contents, QString::null,
00180         "clear_left", "clearBoundColumnButton");
00181     d->clearBoundColumnButton->setMinimumHeight(d->boundColumnLabel->minimumHeight());
00182     QToolTip::add(d->clearBoundColumnButton, i18n("Clear bound column"));
00183     hlyr->addWidget(d->clearBoundColumnButton);
00184     connect(d->clearBoundColumnButton, SIGNAL(clicked()), this, SLOT(clearBoundColumnSelection()));
00185 
00186     d->boundColumnCombo = new KexiFieldComboBox(contents, "boundColumnCombo");
00187     d->boundColumnLabel->setBuddy(d->boundColumnCombo);
00188     contentsVlyr->addWidget(d->boundColumnCombo);
00189 
00190     contentsVlyr->addSpacing(8);
00191 
00192     //- Visible Column
00193     hlyr = new QHBoxLayout(contentsVlyr);
00194     d->visibleColumnLabel = new QLabel(i18n("Visible column:"), contents);
00195     d->visibleColumnLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
00196     d->visibleColumnLabel->setMargin(2);
00197     d->visibleColumnLabel->setMinimumHeight(IconSize(KIcon::Small)+4);
00198     d->visibleColumnLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
00199     hlyr->addWidget(d->visibleColumnLabel);
00200 
00201     d->clearVisibleColumnButton = new KexiSmallToolButton(contents, QString::null,
00202         "clear_left", "clearVisibleColumnButton");
00203     d->clearVisibleColumnButton->setMinimumHeight(d->visibleColumnLabel->minimumHeight());
00204     QToolTip::add(d->clearVisibleColumnButton, i18n("Clear visible column"));
00205     hlyr->addWidget(d->clearVisibleColumnButton);
00206     connect(d->clearVisibleColumnButton, SIGNAL(clicked()), this, SLOT(clearVisibleColumnSelection()));
00207 
00208     d->visibleColumnCombo = new KexiFieldComboBox(contents, "visibleColumnCombo");
00209     d->visibleColumnLabel->setBuddy(d->visibleColumnCombo);
00210     contentsVlyr->addWidget(d->visibleColumnCombo);
00211 
00212     vlyr->addStretch(1);
00213 
00214     connect(d->rowSourceCombo, SIGNAL(textChanged(const QString &)), 
00215         this, SLOT(slotRowSourceTextChanged(const QString &)));
00216     connect(d->rowSourceCombo, SIGNAL(dataSourceChanged()), this, SLOT(slotRowSourceChanged()));
00217     connect(d->boundColumnCombo, SIGNAL(selected()), this, SLOT(slotBoundColumnSelected()));
00218     connect(d->visibleColumnCombo, SIGNAL(selected()), this, SLOT(slotVisibleColumnSelected()));
00219 
00220     clearBoundColumnSelection();
00221     clearVisibleColumnSelection();
00222 }
00223 
00224 KexiLookupColumnPage::~KexiLookupColumnPage()
00225 {
00226     delete d;
00227 }
00228 
00229 void KexiLookupColumnPage::setProject(KexiProject *prj)
00230 {
00231     d->rowSourceCombo->setProject(prj,
00232         true/*showTables*/, true/*showQueries*/
00233     );
00234     d->boundColumnCombo->setProject(prj);
00235     d->visibleColumnCombo->setProject(prj);
00236 }
00237 
00238 void KexiLookupColumnPage::assignPropertySet(KoProperty::Set* propertySet)
00239 {
00240     if (!d->hasPropertySet() && !propertySet)
00241         return;
00242     if (propertySet && d->currentFieldUid == (*propertySet)["uid"].value().toInt())
00243         return; //already assigned
00244 
00245     d->propertySetEnabled = false;
00246     d->setPropertySet( propertySet );
00247     d->updateInfoLabelForPropertySet( i18n("No field selected") );
00248     
00249     const bool hasRowSource = d->hasPropertySet() && !d->propertyValue("rowSourceType").isNull()
00250         && !d->propertyValue("rowSource").isNull();
00251 
00252     QString rowSource, rowSourceType;
00253     if (hasRowSource) {
00254         rowSourceType = typeToMimeType( d->propertyValue("rowSourceType").toString() );
00255         rowSource = d->propertyValue("rowSource").toString();
00256     }
00257     d->rowSourceCombo->setDataSource( rowSourceType, rowSource );
00258     d->rowSourceLabel->setEnabled( d->hasPropertySet() );
00259     d->rowSourceCombo->setEnabled( d->hasPropertySet() );
00260     if (!d->hasPropertySet())
00261         d->clearRowSourceButton->setEnabled( false );
00262 
00263     int boundColumn = -1, visibleColumn = -1;
00264     if (d->rowSourceCombo->isSelectionValid()) {
00265         boundColumn = d->propertyValue("boundColumn").toInt();
00266         visibleColumn = d->propertyValue("visibleColumn").toInt();
00267     }
00268     d->boundColumnCombo->setFieldOrExpression(boundColumn);
00269     d->visibleColumnCombo->setFieldOrExpression(visibleColumn);
00270     updateBoundColumnWidgetsAvailability();
00271     d->propertySetEnabled = true;
00272 }
00273 
00274 void KexiLookupColumnPage::clearBoundColumnSelection()
00275 {
00276     d->boundColumnCombo->setCurrentText("");
00277     d->boundColumnCombo->setFieldOrExpression(QString::null);
00278     slotBoundColumnSelected();
00279     d->clearBoundColumnButton->setEnabled(false);
00280 }
00281 
00282 void KexiLookupColumnPage::slotBoundColumnSelected()
00283 {
00284 //  KexiDB::Field::Type dataType = KexiDB::Field::InvalidType;
00286 /*disabled  KexiDB::Field *field = d->fieldListView->schema()->field( d->boundColumnCombo->fieldOrExpression() );
00287     if (field)
00288         dataType = field->type();
00289 */
00290     d->clearBoundColumnButton->setEnabled( !d->boundColumnCombo->fieldOrExpression().isEmpty() );
00291     if (!d->boundColumnCombo->fieldOrExpression().isEmpty()) {
00292         kdDebug() << endl;
00293     }
00294 
00295     // update property set
00296     if (d->hasPropertySet()) {
00297         d->changeProperty("boundColumn", d->boundColumnCombo->indexOfField());
00298     }
00299 /*
00300     emit boundColumnChanged(
00301         d->boundColumnCombo->fieldOrExpression(),
00302         d->boundColumnCombo->fieldOrExpressionCaption(),
00303         dataType
00304     );*/
00305 }
00306 
00307 void KexiLookupColumnPage::clearVisibleColumnSelection()
00308 {
00309     d->visibleColumnCombo->setCurrentText("");
00310     d->visibleColumnCombo->setFieldOrExpression(QString::null);
00311     slotVisibleColumnSelected();
00312     d->clearVisibleColumnButton->setEnabled(false);
00313 }
00314 
00315 void KexiLookupColumnPage::slotVisibleColumnSelected()
00316 {
00317 //  KexiDB::Field::Type dataType = KexiDB::Field::InvalidType;
00319     d->clearVisibleColumnButton->setEnabled( !d->visibleColumnCombo->fieldOrExpression().isEmpty() );
00320 
00321     // update property set
00322     if (d->hasPropertySet()) {
00324         d->changeProperty("visibleColumn", d->visibleColumnCombo->indexOfField());
00325     }
00326 }
00327 
00328 void KexiLookupColumnPage::slotRowSourceChanged()
00329 {
00330     if (!d->rowSourceCombo->project())
00331         return;
00332     QString mime = d->rowSourceCombo->selectedMimeType();
00333     bool rowSourceFound = false;
00334     QString name = d->rowSourceCombo->selectedName();
00335     if ((mime=="kexi/table" || mime=="kexi/query") && d->rowSourceCombo->isSelectionValid()) {
00336         KexiDB::TableOrQuerySchema *tableOrQuery = new KexiDB::TableOrQuerySchema(
00337             d->rowSourceCombo->project()->dbConnection(), name.latin1(), mime=="kexi/table");
00338         if (tableOrQuery->table() || tableOrQuery->query()) {
00339 //disabled          d->fieldListView->setSchema( tableOrQuery );
00340 /*tmp*/         delete tableOrQuery;
00341             rowSourceFound = true;
00342             d->boundColumnCombo->setTableOrQuery(name, mime=="kexi/table");
00343             d->visibleColumnCombo->setTableOrQuery(name, mime=="kexi/table");
00344         }
00345         else {
00346             delete tableOrQuery;
00347         }
00348     }
00349     if (!rowSourceFound) {
00350         d->boundColumnCombo->setTableOrQuery("", true);
00351         d->visibleColumnCombo->setTableOrQuery("", true);
00352     }
00353     clearBoundColumnSelection();
00354     clearVisibleColumnSelection();
00355     d->clearRowSourceButton->setEnabled(rowSourceFound);
00356     d->gotoRowSourceButton->setEnabled(rowSourceFound);
00357 /* disabled
00358     if (dataSourceFound) {
00359         slotFieldListViewSelectionChanged();
00360     } else {
00361         d->addField->setEnabled(false);
00362     }*/
00363     updateBoundColumnWidgetsAvailability();
00364 
00365     //update property set
00366     if (d->hasPropertySet()) {
00367         d->changeProperty("rowSourceType", mimeTypeToType(mime));
00368         d->changeProperty("rowSource", name);
00369     }
00370 
00371 //disabled  emit formDataSourceChanged(mime, name);
00373 }
00374 
00375 void KexiLookupColumnPage::slotRowSourceTextChanged(const QString & string)
00376 {
00377     Q_UNUSED(string);
00378     const bool enable = d->rowSourceCombo->isSelectionValid();
00379     if (enable) {
00380         updateBoundColumnWidgetsAvailability();
00381     }
00382     else {
00383         clearRowSourceSelection( d->rowSourceCombo->selectedName().isEmpty()/*alsoClearComboBox*/ );
00384     }
00385 }
00386 
00387 void KexiLookupColumnPage::clearRowSourceSelection(bool alsoClearComboBox)
00388 {
00389     if (d->insideClearRowSourceSelection)
00390         return;
00391     d->insideClearRowSourceSelection = true;
00392     if (alsoClearComboBox && !d->rowSourceCombo->selectedName().isEmpty())
00393         d->rowSourceCombo->setDataSource("", "");
00394     d->clearRowSourceButton->setEnabled(false);
00395     d->gotoRowSourceButton->setEnabled(false);
00396     d->insideClearRowSourceSelection = false;
00397 }
00398 
00399 void KexiLookupColumnPage::slotGotoSelectedRowSource()
00400 {
00401     QString mime = d->rowSourceCombo->selectedMimeType();
00402     if (mime=="kexi/table" || mime=="kexi/query") {
00403         if (d->rowSourceCombo->isSelectionValid())
00404             emit jumpToObjectRequested(mime.latin1(), d->rowSourceCombo->selectedName().latin1());
00405     }
00406 }
00407 
00408 void KexiLookupColumnPage::updateBoundColumnWidgetsAvailability()
00409 {
00410     const bool hasRowSource = d->rowSourceCombo->isSelectionValid();
00411     d->boundColumnCombo->setEnabled( hasRowSource );
00412     d->boundColumnLabel->setEnabled( hasRowSource );
00413     d->clearBoundColumnButton->setEnabled( hasRowSource && !d->boundColumnCombo->fieldOrExpression().isEmpty() );
00414     d->visibleColumnCombo->setEnabled( hasRowSource );
00415     d->visibleColumnLabel->setEnabled( hasRowSource );
00416     d->clearVisibleColumnButton->setEnabled( hasRowSource && !d->visibleColumnCombo->fieldOrExpression().isEmpty() );
00417 }
00418 
00419 #include "kexilookupcolumnpage.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys