diff options
Diffstat (limited to 'src/libs/widgets/common/searchtextbar.cpp')
-rw-r--r-- | src/libs/widgets/common/searchtextbar.cpp | 260 |
1 files changed, 260 insertions, 0 deletions
diff --git a/src/libs/widgets/common/searchtextbar.cpp b/src/libs/widgets/common/searchtextbar.cpp new file mode 100644 index 00000000..1a81c03b --- /dev/null +++ b/src/libs/widgets/common/searchtextbar.cpp @@ -0,0 +1,260 @@ +/* ============================================================ + * + * This file is a part of digiKam project + * http://www.digikam.org + * + * Date : 2007-11-25 + * Description : a bar used to search a string. + * + * Copyright (C) 2007-2008 by Gilles Caulier <caulier dot gilles at gmail dot com> + * + * This program is free software; you can redistribute it + * and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; + * either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * ============================================================ */ + +// TQt includes. + +#include <tqcolor.h> +#include <tqpalette.h> +#include <tqpainter.h> +#include <tqlabel.h> +#include <tqlayout.h> +#include <tqtoolbutton.h> + +// KDE includes. + +#include <tdeapplication.h> +#include <kiconloader.h> +#include <kdialogbase.h> +#include <tdeconfig.h> + +// Local includes. + +#include "searchtextbar.h" +#include "searchtextbar.moc" + +namespace Digikam +{ + +class DLineEditPriv +{ +public: + + DLineEditPriv() + { + drawMsg = true; + } + + bool drawMsg; + + TQString message; +}; + +DLineEdit::DLineEdit(const TQString &msg, TQWidget *parent) + : KLineEdit(parent) +{ + d = new DLineEditPriv; + setMessage(msg); +} + +DLineEdit::~DLineEdit() +{ + delete d; +} + +TQString DLineEdit::message() const +{ + return d->message; +} + +void DLineEdit::setMessage(const TQString &msg) +{ + d->message = msg; + repaint(); +} + +void DLineEdit::setText(const TQString &txt) +{ + d->drawMsg = txt.isEmpty(); + repaint(); + KLineEdit::setText(txt); +} + +void DLineEdit::drawContents(TQPainter *p) +{ + KLineEdit::drawContents(p); + + if (d->drawMsg && !hasFocus()) + { + TQPen tmp = p->pen(); + p->setPen(palette().color(TQPalette::Disabled, TQColorGroup::Text)); + TQRect cr = contentsRect(); + + // Add two pixel margin on the left side + cr.rLeft() += 3; + p->drawText(cr, AlignAuto | AlignVCenter, d->message); + p->setPen( tmp ); + } +} + +void DLineEdit::dropEvent(TQDropEvent *e) +{ + d->drawMsg = false; + KLineEdit::dropEvent(e); +} + +void DLineEdit::focusInEvent(TQFocusEvent *e) +{ + if (d->drawMsg) + { + d->drawMsg = false; + repaint(); + } + TQLineEdit::focusInEvent(e); +} + +void DLineEdit::focusOutEvent(TQFocusEvent *e) +{ + if (text().isEmpty()) + { + d->drawMsg = true; + repaint(); + } + TQLineEdit::focusOutEvent(e); +} + +// --------------------------------------------------------------------- + +class SearchTextBarPriv +{ +public: + + SearchTextBarPriv() + { + textQueryCompletion = false; + searchEdit = 0; + clearButton = 0; + } + + bool textQueryCompletion; + + TQToolButton *clearButton; + + DLineEdit *searchEdit; +}; + +SearchTextBar::SearchTextBar(TQWidget *parent, const char* name, const TQString &msg) + : TQWidget(parent, 0, TQt::WDestructiveClose) +{ + d = new SearchTextBarPriv; + setFocusPolicy(TQWidget::NoFocus); + setName(name); + + TQHBoxLayout *hlay = new TQHBoxLayout(this); + + d->clearButton = new TQToolButton(this); + d->clearButton->setEnabled(false); + d->clearButton->setAutoRaise(true); + d->clearButton->setIconSet(kapp->iconLoader()->loadIcon("clear_left", + TDEIcon::Toolbar, TDEIcon::SizeSmall)); + + d->searchEdit = new DLineEdit(msg, this); + TDECompletion *kcom = new TDECompletion; + kcom->setOrder(TDECompletion::Sorted); + d->searchEdit->setCompletionObject(kcom, true); + d->searchEdit->setAutoDeleteCompletionObject(true); + d->searchEdit->setSizePolicy(TQSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Minimum)); + + hlay->setSpacing(0); + hlay->setMargin(0); + hlay->addWidget(d->searchEdit); + hlay->addWidget(d->clearButton); + + connect(d->clearButton, TQ_SIGNAL(clicked()), + d->searchEdit, TQ_SLOT(clear())); + + connect(d->searchEdit, TQ_SIGNAL(textChanged(const TQString&)), + this, TQ_SLOT(slotTextChanged(const TQString&))); + + TDEConfig *config = kapp->config(); + config->setGroup(name + TQString(" Search Text Tool")); + d->searchEdit->setCompletionMode((TDEGlobalSettings::Completion)config->readNumEntry("AutoCompletionMode", + (int)TDEGlobalSettings::CompletionAuto)); +} + +SearchTextBar::~SearchTextBar() +{ + TDEConfig *config = kapp->config(); + config->setGroup(name() + TQString(" Search Text Tool")); + config->writeEntry("AutoCompletionMode", (int)d->searchEdit->completionMode()); + config->sync(); + + delete d; +} + +void SearchTextBar::setEnableTextQueryCompletion(bool b) +{ + d->textQueryCompletion = b; +} + +bool SearchTextBar::textQueryCompletion() const +{ + return d->textQueryCompletion; +} + +void SearchTextBar::setText(const TQString& text) +{ + d->searchEdit->setText(text); +} + +TQString SearchTextBar::text() const +{ + return d->searchEdit->text(); +} + +DLineEdit *SearchTextBar::lineEdit() const +{ + return d->searchEdit; +} + +void SearchTextBar::slotTextChanged(const TQString& text) +{ + if (d->searchEdit->text().isEmpty()) + d->searchEdit->unsetPalette(); + + d->clearButton->setEnabled(text.isEmpty() ? false : true); + + emit signalTextChanged(text); +} + +void SearchTextBar::slotSearchResult(bool match) +{ + if (d->searchEdit->text().isEmpty()) + { + d->searchEdit->unsetPalette(); + return; + } + + TQPalette pal = d->searchEdit->palette(); + pal.setColor(TQPalette::Active, TQColorGroup::Base, + match ? TQColor(200, 255, 200) : + TQColor(255, 200, 200)); + pal.setColor(TQPalette::Active, TQColorGroup::Text, TQt::black); + d->searchEdit->setPalette(pal); + + // If search result match the text query, we put the text + // in auto-completion history. + if (d->textQueryCompletion && match) + d->searchEdit->completionObject()->addItem(d->searchEdit->text()); +} + +} // namespace Digikam |