kexi

kexifinddialog.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004-2007 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 "kexifinddialog.h"
00021 
00022 #include <kstdguiitem.h>
00023 #include <kstdaction.h>
00024 #include <kpushbutton.h>
00025 #include <kcombobox.h>
00026 #include <klocale.h>
00027 #include <kdebug.h>
00028 #include <kdialog.h>
00029 #include <kaction.h>
00030 #include <kiconloader.h>
00031 
00032 #include <qcheckbox.h>
00033 #include <qlabel.h>
00034 #include <qguardedptr.h>
00035 #include <qlayout.h>
00036 #include <qaccel.h>
00037 
00039 class KexiFindDialog::Private
00040 {
00041     public:
00042         Private()
00043         {
00044             accels.setAutoDelete(true);
00045         }
00046         ~Private()
00047         {
00048         }
00052         void setActionAndAccel(KAction *action, QWidget* parent, const char* member)
00053         {
00054             if (!action)
00055                 return;
00056             QObject::connect(parent, member, action, SLOT(activate()));
00057             if (action->shortcut().isNull())
00058                 return;
00059             QAccel *accel = new QAccel(parent); // we want to handle dialog-wide shortcut as well
00060             accels.append( accel );
00061             accel->connectItem(
00062                 accel->insertItem( action->shortcut() ), parent, member );
00063         }
00064 
00065         QStringList lookInColumnNames;
00066         QStringList lookInColumnCaptions;
00067         QString objectName; 
00068         QGuardedPtr<KAction> findnextAction;
00069         QGuardedPtr<KAction> findprevAction;
00070         QGuardedPtr<KAction> replaceAction;
00071         QGuardedPtr<KAction> replaceallAction;
00072         QPtrList<QAccel> accels;
00073         bool replaceMode : 1;
00074 };
00075 
00076 //------------------------------------------
00077 
00078 KexiFindDialog::KexiFindDialog( QWidget* parent )
00079  : KexiFindDialogBase(parent, "KexiFindDialog", false, 
00080     Qt::WType_Dialog|Qt::WStyle_NormalBorder|Qt::WStyle_Title
00081     |Qt::WStyle_SysMenu|Qt::WStyle_Customize|Qt::WStyle_Tool)
00082  , d( new Private() )
00083 {
00084     m_search->setCurrentItem((int)KexiSearchAndReplaceViewInterface::Options::SearchDown);
00085     layout()->setMargin( KDialog::marginHint() );
00086     layout()->setSpacing( KDialog::spacingHint() );
00087     KAction *a = KStdAction::findNext(0, 0, 0);
00088     m_btnFind->setText(a->text());
00089     m_btnFind->setIconSet(a->iconSet());
00090     delete a;
00091     m_btnClose->setText(KStdGuiItem::close().text());
00092     m_btnClose->setIconSet(KStdGuiItem::close().iconSet());
00093     connect(m_btnFind, SIGNAL(clicked()), this, SIGNAL(findNext()));
00094     connect(m_btnClose, SIGNAL(clicked()), this, SLOT(slotCloseClicked()));
00095     connect(m_btnReplace, SIGNAL(clicked()), this, SIGNAL(replaceNext()));
00096     connect(m_btnReplaceAll, SIGNAL(clicked()), this, SIGNAL(replaceAll()));
00097     // clear message after the text is changed
00098     connect(m_textToFind, SIGNAL(textChanged()), this, SIGNAL(updateMessage()));
00099     connect(m_textToReplace, SIGNAL(textChanged()), this, SIGNAL(updateMessage()));
00100 
00101     d->replaceMode = true; //to force updating by setReplaceMode()
00102     setReplaceMode(false);
00103     
00104     setLookInColumnList(QStringList(), QStringList());
00105 }
00106 
00107 KexiFindDialog::~KexiFindDialog()
00108 {
00109     delete d;
00110 }
00111 
00112 void KexiFindDialog::setActions( KAction *findnext, KAction *findprev,
00113     KAction *replace, KAction *replaceall )
00114 {
00115     d->findnextAction = findnext;
00116     d->findprevAction = findprev;
00117     d->replaceAction = replace;
00118     d->replaceallAction = replaceall;
00119     d->accels.clear();
00120     d->setActionAndAccel(d->findnextAction, this, SIGNAL(findNext()));
00121     d->setActionAndAccel(d->findprevAction, this, SIGNAL(findPrevious()));
00122     d->setActionAndAccel(d->replaceAction, this, SIGNAL(replaceNext()));
00123     d->setActionAndAccel(d->replaceallAction, this, SIGNAL(replaceAll()));
00124 }
00125 
00126 QStringList KexiFindDialog::lookInColumnNames() const
00127 {
00128     return d->lookInColumnNames;
00129 }
00130 
00131 QStringList KexiFindDialog::lookInColumnCaptions() const
00132 {
00133     return d->lookInColumnCaptions;
00134 }
00135 
00136 QString KexiFindDialog::currentLookInColumnName() const
00137 {
00138     int index = m_lookIn->currentItem();
00139     if (index <= 0 || index >= (int)d->lookInColumnNames.count())
00140         return QString::null;
00141     else if (index == 1)
00142         return "(field)";
00143     return d->lookInColumnNames[index - 1/*"(All fields)"*/ - 1/*"(Current field)"*/];
00144 }
00145 
00146 QVariant KexiFindDialog::valueToFind() const
00147 {
00148     return m_textToFind->currentText();
00149 }
00150 
00151 QVariant KexiFindDialog::valueToReplaceWith() const
00152 {
00153     return m_textToReplace->currentText();
00154 }
00155 
00156 void KexiFindDialog::setLookInColumnList(const QStringList& columnNames, 
00157     const QStringList& columnCaptions)
00158 {
00159     d->lookInColumnNames = columnNames;
00160     d->lookInColumnCaptions = columnCaptions;
00161     m_lookIn->clear();
00162     m_lookIn->insertItem(i18n("(All fields)"));
00163     m_lookIn->insertItem(i18n("(Current field)"));
00164     m_lookIn->insertStringList(d->lookInColumnCaptions);
00165 }
00166 
00167 void KexiFindDialog::setCurrentLookInColumnName(const QString& columnName)
00168 {
00169     int index;
00170     if (columnName.isEmpty())
00171         index = 0;
00172     else if (columnName == "(field)")
00173         index = 1;
00174     else {
00175         index = d->lookInColumnNames.findIndex( columnName );
00176         if (index == -1) {
00177             kdWarning() << QString("KexiFindDialog::setCurrentLookInColumn(%1) column name not found on the list")
00178                 .arg(columnName) << endl;
00179             return;
00180         }
00181         index = index + 1/*"(All fields)"*/ + 1/*"(Current field)"*/;
00182     }
00183     m_lookIn->setCurrentItem(index);
00184 }
00185 
00186 void KexiFindDialog::setReplaceMode(bool set)
00187 {
00188     if (d->replaceMode == set)
00189         return;
00190     d->replaceMode = set;
00191     if (d->replaceMode) {
00192         m_promptOnReplace->show();
00193         m_replaceLbl->show();
00194         m_textToReplace->show();
00195         m_btnReplace->show();
00196         m_btnReplaceAll->show();
00197     }
00198     else {
00199         m_promptOnReplace->hide();
00200         m_replaceLbl->hide();
00201         m_textToReplace->hide();
00202         m_btnReplace->hide();
00203         m_btnReplaceAll->hide();
00204         resize(width(),height()-30);
00205     }
00206     setObjectNameForCaption(d->objectName);
00207     updateGeometry();
00208 }
00209 
00210 void KexiFindDialog::setObjectNameForCaption(const QString& name)
00211 {
00212     d->objectName = name;
00213     if (d->replaceMode) {
00214         if (name.isEmpty())
00215             setCaption(i18n("Replace"));
00216         else
00217             setCaption(i18n("Replace in \"%1\"").arg(name));
00218     }
00219     else {
00220         if (name.isEmpty())
00221             setCaption(i18n("Find"));
00222         else
00223             setCaption(i18n("Find in \"%1\"").arg(name));
00224     }
00225 }
00226 
00227 void KexiFindDialog::setButtonsEnabled(bool enable)
00228 {
00229     m_btnFind->setEnabled(enable);
00230     m_btnReplace->setEnabled(enable);
00231     m_btnReplaceAll->setEnabled(enable);
00232     if (!enable)
00233         setObjectNameForCaption(QString::null);
00234 }
00235 
00236 void KexiFindDialog::setMessage(const QString& message)
00237 {
00238     m_messageLabel->setText(message);
00239 }
00240 
00241 void KexiFindDialog::updateMessage( bool found )
00242 {
00243     if (found)
00244         setMessage(QString::null);
00245     else
00246         setMessage(i18n("The search item was not found"));
00247 }
00248 
00249 void KexiFindDialog::slotCloseClicked()
00250 {
00251     reject();
00252 }
00253 
00254 void KexiFindDialog::show()
00255 {
00256     m_textToFind->setFocus();
00257     QDialog::show();
00258 }
00259 
00260 KexiSearchAndReplaceViewInterface::Options KexiFindDialog::options() const
00261 {
00262     KexiSearchAndReplaceViewInterface::Options options;
00263     if (m_lookIn->currentItem() <= 0) //"(All fields)"
00264         options.columnNumber = KexiSearchAndReplaceViewInterface::Options::AllColumns;
00265     else if (m_lookIn->currentItem() == 1) //"(Current field)"
00266         options.columnNumber = KexiSearchAndReplaceViewInterface::Options::CurrentColumn;
00267     else
00268         options.columnNumber = m_lookIn->currentItem()  - 1/*"(All fields)"*/ - 1/*"(Current field)"*/;
00269     options.textMatching 
00270         = (KexiSearchAndReplaceViewInterface::Options::TextMatching)m_match->currentItem();
00271     options.searchDirection 
00272         = (KexiSearchAndReplaceViewInterface::Options::SearchDirection)m_search->currentItem();
00273     options.caseSensitive = m_caseSensitive->isChecked();
00274     options.wholeWordsOnly = m_wholeWords->isChecked();
00275     options.promptOnReplace = m_promptOnReplace->isChecked();
00276     return options;
00277 }
00278 
00279 #include "kexifinddialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys