summaryrefslogtreecommitdiffstats
path: root/digikam/digikam/searchquickdialog.cpp
blob: 67ca2a6257eee891acd21d92caf45f87f9ffba78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-05-19
 * Description : a dialog to perform simple search in albums
 *
 * Copyright (C) 2005 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
 * Copyright (C) 2006-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.
 *
 * ============================================================ */

/** @file searchquickdialog.cpp */

// TQt includes.

#include <tqtimer.h>
#include <tqlayout.h>
#include <tqstringlist.h>
#include <tqdatetime.h>
#include <tqlabel.h>
#include <tqwhatsthis.h>

// KDE includes.

#include <klineedit.h>
#include <tdelocale.h>
#include <kurl.h>

// Local includes.

#include "ddebug.h"
#include "searchtextbar.h"
#include "searchresultsview.h"
#include "searchquickdialog.h"
#include "searchquickdialog.moc"

namespace Digikam
{

class SearchQuickDialogPriv
{
public:

    SearchQuickDialogPriv()
    {
        timer       = 0;
        searchEdit  = 0;
        nameEdit    = 0;
        resultsView = 0;
    }

    TQTimer            *timer;

    KLineEdit         *nameEdit;

    SearchTextBar     *searchEdit;

    SearchResultsView *resultsView;
};

SearchQuickDialog::SearchQuickDialog(TQWidget* parent, KURL& url)
                 : KDialogBase(Plain, i18n("Quick Search"), Help|Ok|Cancel, Ok, 
                               parent, 0, true, true), m_url(url)
{
    d = new SearchQuickDialogPriv;
    d->timer = new TQTimer(this);
    setHelp("quicksearchtool.anchor", "digikam");
    
    TQGridLayout* grid = new TQGridLayout(plainPage(), 2, 2, 0, spacingHint());
    
    TQLabel *label1 = new TQLabel("<b>" + i18n("Search:") + "</b>", plainPage());
    d->searchEdit  = new SearchTextBar(plainPage(), "SearchQuickDialogSearchEdit", i18n("Enter here your search criteria"));
    TQWhatsThis::add( d->searchEdit, i18n("<p>Enter your search criteria to find items in the album library"));
    
    d->resultsView = new SearchResultsView(plainPage());
    d->resultsView->setMinimumSize(320, 200);
    TQWhatsThis::add( d->resultsView, i18n("<p>Here you can see the items found in album library, "
                                          "using the current search criteria"));
    
    TQLabel *label2 = new TQLabel(i18n("Save search as:"), plainPage());
    d->nameEdit    = new KLineEdit(plainPage());
    d->nameEdit->setText(i18n("Last Search"));
    TQWhatsThis::add( d->nameEdit, i18n("<p>Enter the name of the current search to save in the "
                                       "\"My Searches\" view"));

    grid->addMultiCellWidget(label1, 0, 0, 0, 0);
    grid->addMultiCellWidget(d->searchEdit, 0, 0, 1, 2);
    grid->addMultiCellWidget(d->resultsView, 1, 1, 0, 2);
    grid->addMultiCellWidget(label2, 2, 2, 0, 1);
    grid->addMultiCellWidget(d->nameEdit, 2, 2, 2, 2);
    
    connect(d->searchEdit, TQT_SIGNAL(signalTextChanged(const TQString&)),
            this, TQT_SLOT(slotSearchChanged(const TQString&)));

    connect(d->resultsView, TQT_SIGNAL(signalSearchResultsMatch(bool)),
            d->searchEdit, TQT_SLOT(slotSearchResult(bool)));

    connect(d->timer, TQT_SIGNAL(timeout()),
            this, TQT_SLOT(slotTimeOut()));

    enableButtonOK(false);
    resize(configDialogSize("QuickSearch Dialog"));

    // check if we are being passed a valid url
    if (m_url.isValid())
    {
        int count = m_url.queryItem("count").toInt();
        if (count > 0)
        {
            TQStringList strList;

            for (int i=1; i<=count; i++)
            {
                TQString val = m_url.queryItem(TQString::number(i) + ".val");
                if (!strList.contains(val))
                {
                    strList.append(val);
                }
            }
            
            d->searchEdit->setText(strList.join(" "));
            d->nameEdit->setText(url.queryItem("name"));
            d->timer->start(0, true);
        }
    }
}

SearchQuickDialog::~SearchQuickDialog()
{
    saveDialogSize(("QuickSearch Dialog"));
    delete d->timer;    
    delete d;
}

void SearchQuickDialog::slotTimeOut()
{
    if (d->searchEdit->text().isEmpty())
    {
        d->resultsView->clear();
        enableButtonOK(false);
        return;
    }

    enableButtonOK(true);
    
    KURL url;
    url.setProtocol("digikamsearch");

    TQString path, num;
    int     count = 0;
    
    TQStringList textList = TQStringList::split(' ', d->searchEdit->text());
    for (TQStringList::iterator it = textList.begin(); it != textList.end(); ++it)
    {
        if (count != 0)
            path += " AND ";

        path += TQString(" %1 ").arg(count + 1);

        num = TQString::number(++count);
        url.addQueryItem(num + ".key", "keyword");
        url.addQueryItem(num + ".op", "like");
        url.addQueryItem(num + ".val", *it);
    }

    url.setPath(path);
    url.addQueryItem("name", "Live Search");
    url.addQueryItem("count", num);

    m_url = url;
    d->resultsView->openURL(url);
}

void SearchQuickDialog::slotSearchChanged(const TQString&)
{
    d->timer->start(500, true);    
}

void SearchQuickDialog::hideEvent(TQHideEvent* e)
{
    m_url.removeQueryItem("name");
    m_url.addQueryItem("name", d->nameEdit->text().isEmpty() ?
                       i18n("Last Search") : d->nameEdit->text());
    KDialogBase::hideEvent(e);
}

}  // namespace Digikam