From 4f24794fc32cd94dfbd473fa8dbc4b504c20bcb3 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Thu, 12 Nov 2009 22:03:07 +0000 Subject: [PATCH] initial code to provide a Nepomuk query string out of the search criterions svn path=/trunk/KDE/kdebase/apps/; revision=1048202 --- src/search/dolphinsearchbox.cpp | 2 - .../dolphinsearchoptionsconfigurator.cpp | 38 ++++-- src/search/dolphinsearchoptionsconfigurator.h | 9 +- src/search/searchcriterionselector.cpp | 120 ++++++++++-------- src/search/searchcriterionselector.h | 9 +- src/search/searchcriterionvalue.cpp | 23 +++- src/search/searchcriterionvalue.h | 4 +- 7 files changed, 128 insertions(+), 77 deletions(-) diff --git a/src/search/dolphinsearchbox.cpp b/src/search/dolphinsearchbox.cpp index 666405014..c9a86111f 100644 --- a/src/search/dolphinsearchbox.cpp +++ b/src/search/dolphinsearchbox.cpp @@ -277,7 +277,6 @@ bool DolphinSearchBox::event(QEvent* event) return QWidget::event(event); } -#include bool DolphinSearchBox::eventFilter(QObject* watched, QEvent* event) { if ((watched == m_searchInput) && (event->type() == QEvent::FocusIn)) { @@ -287,7 +286,6 @@ bool DolphinSearchBox::eventFilter(QObject* watched, QEvent* event) if (m_completer == 0) { m_completer = new DolphinSearchCompleter(m_searchInput); } - kDebug() << "---- got focus! is visible? " << isVisible(); emit requestSearchOptions(); } diff --git a/src/search/dolphinsearchoptionsconfigurator.cpp b/src/search/dolphinsearchoptionsconfigurator.cpp index 81aee12a9..6271350cc 100644 --- a/src/search/dolphinsearchoptionsconfigurator.cpp +++ b/src/search/dolphinsearchoptionsconfigurator.cpp @@ -35,13 +35,16 @@ #include #include +#include + DolphinSearchOptionsConfigurator::DolphinSearchOptionsConfigurator(QWidget* parent) : QWidget(parent), m_initialized(false), m_searchFromBox(0), m_searchWhatBox(0), m_addSelectorButton(0), - m_vBoxLayout(0) + m_vBoxLayout(0), + m_criterions() { m_vBoxLayout = new QVBoxLayout(this); @@ -118,9 +121,9 @@ void DolphinSearchOptionsConfigurator::showEvent(QShowEvent* event) Q_ASSERT(static_cast(SearchCriterionSelector::Date) == 0); Q_ASSERT(static_cast(SearchCriterionSelector::Size) == 1); Q_ASSERT(static_cast(SearchCriterionSelector::Tag) == 2); - addSelector(dateCriterion); - addSelector(sizeCriterion); - addSelector(tagCriterion); + addCriterion(dateCriterion); + addCriterion(sizeCriterion); + addCriterion(tagCriterion); m_initialized = true; } @@ -130,14 +133,28 @@ void DolphinSearchOptionsConfigurator::showEvent(QShowEvent* event) void DolphinSearchOptionsConfigurator::slotAddSelectorButtonClicked() { SearchCriterionSelector* selector = new SearchCriterionSelector(SearchCriterionSelector::Tag, this); - addSelector(selector); + addCriterion(selector); +} + +void DolphinSearchOptionsConfigurator::slotCriterionChanged() +{ + QString searchOptions; + foreach (const SearchCriterionSelector* criterion, m_criterions) { + searchOptions += criterion->queryString() + ' '; + } + kDebug() << "Search option string:" << searchOptions; + emit searchOptionsChanged(searchOptions); } void DolphinSearchOptionsConfigurator::removeCriterion() { - QWidget* criterion = qobject_cast(sender()); + SearchCriterionSelector* criterion = qobject_cast(sender()); Q_ASSERT(criterion != 0); m_vBoxLayout->removeWidget(criterion); + + const int index = m_criterions.indexOf(criterion); + m_criterions.removeAt(index); + criterion->deleteLater(); updateSelectorButton(); @@ -175,14 +192,17 @@ void DolphinSearchOptionsConfigurator::saveQuery() dialog.exec(); // TODO... } -void DolphinSearchOptionsConfigurator::addSelector(SearchCriterionSelector* selector) +void DolphinSearchOptionsConfigurator::addCriterion(SearchCriterionSelector* criterion) { - connect(selector, SIGNAL(removeCriterion()), this, SLOT(removeCriterion())); + connect(criterion, SIGNAL(removeCriterion()), this, SLOT(removeCriterion())); + connect(criterion, SIGNAL(criterionChanged()), this, SLOT(slotCriterionChanged())); // insert the new selector before the lastLineLayout and the KSeparator at the bottom const int index = m_vBoxLayout->count() - 2; - m_vBoxLayout->insertWidget(index, selector); + m_vBoxLayout->insertWidget(index, criterion); updateSelectorButton(); + + m_criterions.append(criterion); } #include "dolphinsearchoptionsconfigurator.moc" diff --git a/src/search/dolphinsearchoptionsconfigurator.h b/src/search/dolphinsearchoptionsconfigurator.h index d384f5296..46950e422 100644 --- a/src/search/dolphinsearchoptionsconfigurator.h +++ b/src/search/dolphinsearchoptionsconfigurator.h @@ -20,6 +20,7 @@ #ifndef DOLPHINSEARCHOPTIONSCONFIGURATOR_H #define DOLPHINSEARCHOPTIONSCONFIGURATOR_H +#include #include class KComboBox; @@ -38,12 +39,17 @@ public: DolphinSearchOptionsConfigurator(QWidget* parent = 0); virtual ~DolphinSearchOptionsConfigurator(); +signals: + void searchOptionsChanged(const QString& options); + protected: virtual void showEvent(QShowEvent* event); private slots: void slotAddSelectorButtonClicked(); + void slotCriterionChanged(); + void removeCriterion(); /** @@ -62,7 +68,7 @@ private: * Adds the new search description selector to the bottom * of the layout. */ - void addSelector(SearchCriterionSelector* selector); + void addCriterion(SearchCriterionSelector* selector); private: bool m_initialized; @@ -70,6 +76,7 @@ private: KComboBox* m_searchWhatBox; QPushButton* m_addSelectorButton; QVBoxLayout* m_vBoxLayout; + QList m_criterions; }; #endif diff --git a/src/search/searchcriterionselector.cpp b/src/search/searchcriterionselector.cpp index 08987153a..34756cc30 100644 --- a/src/search/searchcriterionselector.cpp +++ b/src/search/searchcriterionselector.cpp @@ -73,6 +73,63 @@ SearchCriterionSelector::~SearchCriterionSelector() { } +QString SearchCriterionSelector::queryString() const +{ + if (m_valueWidget == 0) { + return QString(); + } + + const int descIndex = m_descriptionsBox->currentIndex(); + const SearchCriterionDescription& descr = m_descriptions[descIndex]; + + const int compIndex = m_comparatorBox->currentIndex(); + const SearchCriterionDescription::Comparator& comp = descr.comparators()[compIndex]; + + return comp.prefix + descr.identifier() + comp.operation + + '"' + m_valueWidget->value() + '"'; +} + +void SearchCriterionSelector::slotDescriptionChanged(int index) +{ + if (m_valueWidget != 0) { + m_valueWidget->hide(); + m_layout->removeWidget(m_valueWidget); + m_valueWidget = 0; + // the value widget is obtained by the Search Criterion + // Selector instance and may not get deleted + } + + // add comparator items + disconnect(m_comparatorBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int))); + m_comparatorBox->clear(); + + const SearchCriterionDescription& description = m_descriptions[index]; + foreach (const SearchCriterionDescription::Comparator& comp, description.comparators()) { + m_comparatorBox->addItem(comp.name); + } + + // add value widget + m_valueWidget = description.valueWidget(); + m_layout->insertWidget(2, m_valueWidget); + + m_comparatorBox->setCurrentIndex(0); + slotComparatorChanged(0); + connect(m_comparatorBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int))); +} + +void SearchCriterionSelector::slotComparatorChanged(int index) +{ + Q_ASSERT(index >= 0); + + // only show the value widget if an operation is defined by the comparator + const int descIndex = m_descriptionsBox->currentIndex(); + const SearchCriterionDescription& descr = m_descriptions[descIndex]; + const SearchCriterionDescription::Comparator& comp = descr.comparators()[index]; + m_valueWidget->setVisible(!comp.operation.isEmpty()); + + emit criterionChanged(); +} + void SearchCriterionSelector::createDescriptions() { Q_ASSERT(m_descriptionsBox != 0); @@ -98,8 +155,8 @@ void SearchCriterionSelector::createDescriptions() DateValue* dateValue = new DateValue(this); dateValue->hide(); - SearchCriterionDescription date(i18nc("@label", "Date"), - "sourceModified", + SearchCriterionDescription date(i18nc("@label", "Date:"), + "lastModified", dateComps, dateValue); @@ -109,7 +166,7 @@ void SearchCriterionSelector::createDescriptions() SizeValue* sizeValue = new SizeValue(this); sizeValue->hide(); - SearchCriterionDescription size(i18nc("@label", "Size"), + SearchCriterionDescription size(i18nc("@label", "Size:"), "contentSize", sizeComps, sizeValue); @@ -117,11 +174,11 @@ void SearchCriterionSelector::createDescriptions() // add "Tag" description QList tagComps; tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label All (tags)", "All"))); - tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Equal to"), "==")); + tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Equal to"), ":")); TagValue* tagValue = new TagValue(this); tagValue->hide(); - SearchCriterionDescription tag(i18nc("@label", "Tag"), + SearchCriterionDescription tag(i18nc("@label", "Tag:"), "tag", tagComps, tagValue); @@ -133,54 +190,13 @@ void SearchCriterionSelector::createDescriptions() m_descriptions.append(size); m_descriptions.append(tag); - // add all descriptions to the combo box - const int count = m_descriptions.count(); - for (int i = 0; i < count; ++i) { - m_descriptionsBox->addItem(m_descriptions[i].name(), i); - } -} - -void SearchCriterionSelector::slotDescriptionChanged(int index) -{ - if (m_valueWidget != 0) { - m_valueWidget->hide(); - m_layout->removeWidget(m_valueWidget); - m_valueWidget = 0; - // the value widget is obtained by the Search Criterion - // Selector instance and may not get deleted - } - - // add comparator items - disconnect(m_comparatorBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int))); - m_comparatorBox->clear(); - - const SearchCriterionDescription& description = m_descriptions[index]; - foreach (const SearchCriterionDescription::Comparator& comp, description.comparators()) { - m_comparatorBox->addItem(comp.name); + // add all descriptions to the combo box and connect the value widgets + int i = 0; + foreach (const SearchCriterionDescription& desc, m_descriptions) { + m_descriptionsBox->addItem(desc.name(), i); + connect(desc.valueWidget(), SIGNAL(valueChanged(QString)), this, SIGNAL(criterionChanged())); + ++i; } - - // add value widget - m_valueWidget = description.valueWidget(); - m_layout->insertWidget(2, m_valueWidget); - - m_comparatorBox->setCurrentIndex(0); - slotComparatorChanged(0); - connect(m_comparatorBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int))); -} - -void SearchCriterionSelector::slotComparatorChanged(int index) -{ - Q_ASSERT(index >= 0); - - // only show the value widget if an operation is defined by the comparator - const int descIndex = m_descriptionsBox->currentIndex(); - const SearchCriterionDescription& descr = m_descriptions[descIndex]; - const SearchCriterionDescription::Comparator& comp = descr.comparators()[index]; - m_valueWidget->setVisible(!comp.operation.isEmpty()); - - // create query string - const QString queryString = comp.prefix + descr.identifier() + comp.operation + m_valueWidget->value(); - emit criterionChanged(queryString); } #include "searchcriterionselector.moc" diff --git a/src/search/searchcriterionselector.h b/src/search/searchcriterionselector.h index abaed2d0a..c6c3b22e0 100644 --- a/src/search/searchcriterionselector.h +++ b/src/search/searchcriterionselector.h @@ -50,6 +50,8 @@ public: SearchCriterionSelector(Type type, QWidget* parent = 0); virtual ~SearchCriterionSelector(); + QString queryString() const; + signals: /** * Is emitted if the criterion selector should be removed @@ -57,11 +59,8 @@ signals: */ void removeCriterion(); - /** - * Is emitted if the user changed the search criterion. - * \p queryString represents a Nepomuk conform query string. - */ - void criterionChanged(const QString& queryString); + /** Is emitted if the user has changed the search criterion. */ + void criterionChanged(); private slots: void slotDescriptionChanged(int index); diff --git a/src/search/searchcriterionvalue.cpp b/src/search/searchcriterionvalue.cpp index 531db3e01..7bf83e94f 100644 --- a/src/search/searchcriterionvalue.cpp +++ b/src/search/searchcriterionvalue.cpp @@ -20,13 +20,15 @@ #include "searchcriterionvalue.h" +#include #include #include #include #include -#include +#include +#include #include #include #include @@ -44,13 +46,13 @@ SearchCriterionValue::~SearchCriterionValue() DateValue::DateValue(QWidget* parent) : SearchCriterionValue(parent), - m_dateEdit(0) + m_dateWidget(0) { - m_dateEdit = new QDateEdit(this); + m_dateWidget = new KDateWidget(QDate::currentDate(), this); QHBoxLayout* layout = new QHBoxLayout(this); layout->setMargin(0); - layout->addWidget(m_dateEdit); + layout->addWidget(m_dateWidget); } DateValue::~DateValue() @@ -59,7 +61,7 @@ DateValue::~DateValue() QString DateValue::value() const { - return QString(); + return m_dateWidget->date().toString(Qt::ISODate); } // ------------------------------------------------------------------------- @@ -74,6 +76,9 @@ TagValue::TagValue(QWidget* parent) : QHBoxLayout* layout = new QHBoxLayout(this); layout->setMargin(0); layout->addWidget(m_tags); + + connect(m_tags, SIGNAL(activated(QString)), + this, SIGNAL(valueChanged(QString))); } TagValue::~TagValue() @@ -82,7 +87,7 @@ TagValue::~TagValue() QString TagValue::value() const { - return QString(); + return m_tags->currentText(); } void TagValue::showEvent(QShowEvent* event) @@ -109,6 +114,8 @@ SizeValue::SizeValue(QWidget* parent) : { m_lineEdit = new KLineEdit(this); m_lineEdit->setClearButtonShown(true); + m_lineEdit->setValidator(new QIntValidator(this)); + m_lineEdit->setAlignment(Qt::AlignRight); m_units = new QComboBox(this); // TODO: check the KByte vs. KiByte dilemma :-/ @@ -117,6 +124,10 @@ SizeValue::SizeValue(QWidget* parent) : m_units->addItem(i18nc("@label", "MByte")); m_units->addItem(i18nc("@label", "GByte")); + // set 1 MByte as default + m_lineEdit->setText("1"); + m_units->setCurrentIndex(2); + QHBoxLayout* layout = new QHBoxLayout(this); layout->setMargin(0); layout->addWidget(m_lineEdit); diff --git a/src/search/searchcriterionvalue.h b/src/search/searchcriterionvalue.h index 5144c7181..972b95f7a 100644 --- a/src/search/searchcriterionvalue.h +++ b/src/search/searchcriterionvalue.h @@ -24,7 +24,7 @@ #include class QComboBox; -class QDateEdit; +class KDateWidget; class KLineEdit; /** @@ -58,7 +58,7 @@ public: virtual QString value() const; private: - QDateEdit* m_dateEdit; + KDateWidget* m_dateWidget; }; -- 2.47.3