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*/, 
00234         false/*showQueries*/
00235     );
00236     d->boundColumnCombo->setProject(prj);
00237     d->visibleColumnCombo->setProject(prj);
00238 }
00239 
00240 void KexiLookupColumnPage::assignPropertySet(KoProperty::Set* propertySet)
00241 {
00242     if (!d->hasPropertySet() && !propertySet)
00243         return;
00244     if (propertySet && d->currentFieldUid == (*propertySet)["uid"].value().toInt())
00245         return; //already assigned
00246 
00247     d->propertySetEnabled = false;
00248     d->setPropertySet( propertySet );
00249     d->updateInfoLabelForPropertySet( i18n("No field selected") );
00250     
00251     const bool hasRowSource = d->hasPropertySet() && !d->propertyValue("rowSourceType").isNull()
00252         && !d->propertyValue("rowSource").isNull();
00253 
00254     QString rowSource, rowSourceType;
00255     if (hasRowSource) {
00256         rowSourceType = typeToMimeType( d->propertyValue("rowSourceType").toString() );
00257         rowSource = d->propertyValue("rowSource").toString();
00258     }
00259     d->rowSourceCombo->setDataSource( rowSourceType, rowSource );
00260     d->rowSourceLabel->setEnabled( d->hasPropertySet() );
00261     d->rowSourceCombo->setEnabled( d->hasPropertySet() );
00262     if (!d->hasPropertySet())
00263         d->clearRowSourceButton->setEnabled( false );
00264 
00265     int boundColumn = -1, visibleColumn = -1;
00266     if (d->rowSourceCombo->isSelectionValid()) {
00267         boundColumn = d->propertyValue("boundColumn").toInt();
00268         visibleColumn = d->propertyValue("visibleColumn").toInt();
00269     }
00270     d->boundColumnCombo->setFieldOrExpression(boundColumn);
00271     d->visibleColumnCombo->setFieldOrExpression(visibleColumn);
00272     updateBoundColumnWidgetsAvailability();
00273     d->propertySetEnabled = true;
00274 }
00275 
00276 void KexiLookupColumnPage::clearBoundColumnSelection()
00277 {
00278     d->boundColumnCombo->setCurrentText("");
00279     d->boundColumnCombo->setFieldOrExpression(QString::null);
00280     slotBoundColumnSelected();
00281     d->clearBoundColumnButton->setEnabled(false);
00282 }
00283 
00284 void KexiLookupColumnPage::slotBoundColumnSelected()
00285 {
00286 //  KexiDB::Field::Type dataType = KexiDB::Field::InvalidType;
00288 /*disabled  KexiDB::Field *field = d->fieldListView->schema()->field( d->boundColumnCombo->fieldOrExpression() );
00289     if (field)
00290         dataType = field->type();
00291 */
00292     d->clearBoundColumnButton->setEnabled( !d->boundColumnCombo->fieldOrExpression().isEmpty() );
00293     if (!d->boundColumnCombo->fieldOrExpression().isEmpty()) {
00294         kdDebug() << endl;
00295     }
00296 
00297     // update property set
00298     if (d->hasPropertySet()) {
00299         d->changeProperty("boundColumn", d->boundColumnCombo->indexOfField());
00300     }
00301 /*
00302     emit boundColumnChanged(
00303         d->boundColumnCombo->fieldOrExpression(),
00304         d->boundColumnCombo->fieldOrExpressionCaption(),
00305         dataType
00306     );*/
00307 }
00308 
00309 void KexiLookupColumnPage::clearVisibleColumnSelection()
00310 {
00311     d->visibleColumnCombo->setCurrentText("");
00312     d->visibleColumnCombo->setFieldOrExpression(QString::null);
00313     slotVisibleColumnSelected();
00314     d->clearVisibleColumnButton->setEnabled(false);
00315 }
00316 
00317 void KexiLookupColumnPage::slotVisibleColumnSelected()
00318 {
00319 //  KexiDB::Field::Type dataType = KexiDB::Field::InvalidType;
00321     d->clearVisibleColumnButton->setEnabled( !d->visibleColumnCombo->fieldOrExpression().isEmpty() );
00322 
00323     // update property set
00324     if (d->hasPropertySet()) {
00326         d->changeProperty("visibleColumn", d->visibleColumnCombo->indexOfField());
00327     }
00328 }
00329 
00330 void KexiLookupColumnPage::slotRowSourceChanged()
00331 {
00332     if (!d->rowSourceCombo->project())
00333         return;
00334     QString mime = d->rowSourceCombo->selectedMimeType();
00335     bool rowSourceFound = false;
00336     QString name = d->rowSourceCombo->selectedName();
00337     if ((mime=="kexi/table" || mime=="kexi/query") && d->rowSourceCombo->isSelectionValid()) {
00338         KexiDB::TableOrQuerySchema *tableOrQuery = new KexiDB::TableOrQuerySchema(
00339             d->rowSourceCombo->project()->dbConnection(), name.latin1(), mime=="kexi/table");
00340         if (tableOrQuery->table() || tableOrQuery->query()) {
00341 //disabled          d->fieldListView->setSchema( tableOrQuery );
00342 /*tmp*/         delete tableOrQuery;
00343             rowSourceFound = true;
00344             d->boundColumnCombo->setTableOrQuery(name, mime=="kexi/table");
00345             d->visibleColumnCombo->setTableOrQuery(name, mime=="kexi/table");
00346         }
00347         else {
00348             delete tableOrQuery;
00349         }
00350     }
00351     if (!rowSourceFound) {
00352         d->boundColumnCombo->setTableOrQuery("", true);
00353         d->visibleColumnCombo->setTableOrQuery("", true);
00354     }
00355     clearBoundColumnSelection();
00356     clearVisibleColumnSelection();
00357     d->clearRowSourceButton->setEnabled(rowSourceFound);
00358     d->gotoRowSourceButton->setEnabled(rowSourceFound);
00359 /* disabled
00360     if (dataSourceFound) {
00361         slotFieldListViewSelectionChanged();
00362     } else {
00363         d->addField->setEnabled(false);
00364     }*/
00365     updateBoundColumnWidgetsAvailability();
00366 
00367     //update property set
00368     if (d->hasPropertySet()) {
00369         d->changeProperty("rowSourceType", mimeTypeToType(mime));
00370         d->changeProperty("rowSource", name);
00371     }
00372 
00373 //disabled  emit formDataSourceChanged(mime, name);
00375 }
00376 
00377 void KexiLookupColumnPage::slotRowSourceTextChanged(const QString & string)
00378 {
00379     Q_UNUSED(string);
00380     const bool enable = d->rowSourceCombo->isSelectionValid();
00381     if (enable) {
00382         updateBoundColumnWidgetsAvailability();
00383     }
00384     else {
00385         clearRowSourceSelection( d->rowSourceCombo->selectedName().isEmpty()/*alsoClearComboBox*/ );
00386     }
00387 }
00388 
00389 void KexiLookupColumnPage::clearRowSourceSelection(bool alsoClearComboBox)
00390 {
00391     if (d->insideClearRowSourceSelection)
00392         return;
00393     d->insideClearRowSourceSelection = true;
00394     if (alsoClearComboBox && !d->rowSourceCombo->selectedName().isEmpty())
00395         d->rowSourceCombo->setDataSource("", "");
00396     d->clearRowSourceButton->setEnabled(false);
00397     d->gotoRowSourceButton->setEnabled(false);
00398     d->insideClearRowSourceSelection = false;
00399 }
00400 
00401 void KexiLookupColumnPage::slotGotoSelectedRowSource()
00402 {
00403     QString mime = d->rowSourceCombo->selectedMimeType();
00404     if (mime=="kexi/table" || mime=="kexi/query") {
00405         if (d->rowSourceCombo->isSelectionValid())
00406             emit jumpToObjectRequested(mime.latin1(), d->rowSourceCombo->selectedName().latin1());
00407     }
00408 }
00409 
00410 void KexiLookupColumnPage::updateBoundColumnWidgetsAvailability()
00411 {
00412     const bool hasRowSource = d->rowSourceCombo->isSelectionValid();
00413     d->boundColumnCombo->setEnabled( hasRowSource );
00414     d->boundColumnLabel->setEnabled( hasRowSource );
00415     d->clearBoundColumnButton->setEnabled( hasRowSource && !d->boundColumnCombo->fieldOrExpression().isEmpty() );
00416     d->visibleColumnCombo->setEnabled( hasRowSource );
00417     d->visibleColumnLabel->setEnabled( hasRowSource );
00418     d->clearVisibleColumnButton->setEnabled( hasRowSource && !d->visibleColumnCombo->fieldOrExpression().isEmpty() );
00419 }
00420 
00421 #include "kexilookupcolumnpage.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys