File: PictureSearchWidget.cpp

package info (click to toggle)
fotowall 0.9-8
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,280 kB
  • sloc: cpp: 23,275; makefile: 51; sh: 25
file content (371 lines) | stat: -rw-r--r-- 12,095 bytes parent folder | download | duplicates (3)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/***************************************************************************
 *                                                                         *
 *   This file is part of the Fotowall project,                            *
 *       http://www.enricoros.com/opensource/fotowall                      *
 *                                                                         *
 *   Copyright (C) 2009 by Enrico Ros <enrico.ros@gmail.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 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "PictureSearchWidget.h"
#ifdef ENABLE_GCOMPLETION
#include "3rdparty/gsuggest.h"
#endif
#include "Shared/PictureServices/FlickrPictureService.h"
#include "Shared/PictureServices/GoogleImagesPictureService.h"
#include "App.h"
#include <QBasicTimer>
#include <QGraphicsScene>
#include <QGraphicsLinearLayout>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QPainter>
#include <QTime>

#define FRAME_RADIUS 6

class SearchSymbol : public QWidget
{
    public:
        SearchSymbol(QWidget * parent)
          : QWidget(parent)
        {
            setFixedSize(25, 25);
            m_timer.start(50, this);
            m_time.start();
        }

    protected:
        void timerEvent(QTimerEvent * event)
        {
            if (event->timerId() != m_timer.timerId())
                return QWidget::timerEvent(event);
            update();
        }

        void paintEvent(QPaintEvent * /*event*/)
        {
            QPainter p(this);
            int elapsed = m_time.elapsed();
            if (elapsed < 1000)
                p.setOpacity((qreal)elapsed / 1000.0);
            int idx = 2 * elapsed - 90*16;
            p.setPen(Qt::NoPen);
            p.setBrush(Qt::blue);
            p.drawPie(rect(), -idx, 50 * 16);
            p.drawPie(rect(), -idx + 180 * 16, 50 * 16);
        }

    private:
        QBasicTimer m_timer;
        QTime m_time;
};

class PicturesListWidget : public QListWidget
{
    public:
        PicturesListWidget(QWidget * parent)
          : QListWidget(parent)
        {
        }

        void startDrag(Qt::DropActions supportedDropActions)
        {
            QList<QListWidgetItem *> items = selectedItems();
            int count = items.size();
            if (count < 1 || !App::pictureService)
                return;

            // make the drag pixmap and the indices data
            QStringList indices;
            QPixmap dragPixmap(50 + 10 * (count - 1), 50 + 10 * (count - 1));
            dragPixmap.fill(Qt::transparent);
            QPainter dragPainter(&dragPixmap);
            int i = 0;
            foreach (QListWidgetItem * item, items) {
                int idx = row(item);
                App::pictureService->startPrefetch(idx);
                dragPainter.drawPixmap(i * 10, i * 10, item->icon().pixmap(50, 50));
                indices.append(QString::number(idx));
                i++;
            }
            dragPainter.end();

            // create and execute the drag operation
            QDrag * drag = new QDrag(this);
            drag->setPixmap(dragPixmap);
            QMimeData * mimeData = new QMimeData();
            mimeData->setData("picturesearch/idx",indices.join(",").toLatin1());
            drag->setMimeData(mimeData);
            Qt::DropAction action = drag->exec(supportedDropActions, Qt::CopyAction);

            // abort prefetches if action was not accepted
            if (action != Qt::CopyAction)
                foreach (QListWidgetItem * item, items)
                    App::pictureService->stopPrefetch(row(item));
        }
};

class DottedLineEdit : public QLineEdit
{
    public:
        DottedLineEdit(QWidget * parent = 0)
          : QLineEdit(parent)
          , m_welcome(true)
        {
            // use a transparent look
            setFrame(false);
            QPalette pal;
            pal.setBrush(QPalette::Base, Qt::transparent);
            setPalette(pal);

            // inital text
            setText(tr("Type here..."));
            selectAll();
        }

        // ::QWidget
        void paintEvent(QPaintEvent * event)
        {
            // customize background
            QPainter painter(this);
            painter.setRenderHint(QPainter::Antialiasing, false);
            //painter.setPen(Qt::NoPen);//QPen(Qt::lightGray, 1));
            //painter.setBrush(Qt::white);
            //painter.drawRect(QRectF(rect()).adjusted(0.5, 0.5, -1.5, -1.5));
            painter.setPen(QPen(Qt::darkGray, 1, Qt::DotLine));
            painter.drawLine(2, height() - 2, width() - 3, height() - 2);
            painter.end();

            // unbreak drawing
            QLineEdit::paintEvent(event);
        }
        void keyPressEvent(QKeyEvent * event)
        {
            normalMode();
            QLineEdit::keyPressEvent(event);
        }
        void mousePressEvent(QMouseEvent * event)
        {
            normalMode();
            QLineEdit::mousePressEvent(event);
        }

    private:
        void normalMode()
        {
            if (m_welcome) {
                m_welcome = false;
                clear();
            }
        }
        bool m_welcome;
};

// included here because it needs the definitions above
#include "ui_PictureSearchWidget.h"

int PictureSearchWidget::LastProvider = 0;

PictureSearchWidget::PictureSearchWidget(QNetworkAccessManager * extAccessManager, QWidget * parent)
    : QWidget(parent)
#ifdef ENABLE_GCOMPLETION
    , m_completion(0)
#endif
    , m_extAccessManager(extAccessManager)
    , m_ui(new Ui_PictureSearchWidget())
    , m_searchSymbol(0)
{
    // customize Ui
    QFont font;
    font.setPointSize(font.pointSize() - 1);
    setFont(font);
    setContentsMargins(0, 0, 0, 0);
    m_ui->setupUi(this);
    m_ui->listWidget->hide();
    connect(m_ui->searchButton, SIGNAL(clicked()), this, SLOT(slotSearchClicked()));
    connect(m_ui->lineEdit, SIGNAL(returnPressed()), m_ui->searchButton, SLOT(click()));
    connect(m_ui->fRadio, SIGNAL(toggled(bool)), this, SLOT(slotProviderChanged()));
    connect(m_ui->gRadio, SIGNAL(toggled(bool)), this, SLOT(slotProviderChanged()));
    if (LastProvider == 0)
        m_ui->fRadio->setChecked(true);
    else if (LastProvider == 1)
        m_ui->gRadio->setChecked(true);
    adjustSize();

    // init
    slotProviderChanged();
    slotSearchEnded(false);

#ifdef ENABLE_GCOMPLETION
    // apply google completion to widget
    m_completion = new GSuggestCompletion(m_ui->lineEdit);
#endif
}

PictureSearchWidget::~PictureSearchWidget()
{
    if (m_ui->fRadio->isChecked())
        LastProvider = 0;
    else if (m_ui->gRadio->isChecked())
        LastProvider = 1;
    m_extAccessManager = 0;
#ifdef ENABLE_GCOMPLETION
    delete m_completion;
#endif
    delete m_searchSymbol;
    m_searchSymbol = 0;
    if (App::pictureService) {
        App::pictureService->disconnect(0, 0, 0);
        delete App::pictureService;
        App::pictureService = 0;
    }
    delete m_ui;
}

void PictureSearchWidget::setFocus()
{
    m_ui->lineEdit->setFocus();
}

void PictureSearchWidget::closeEvent(QCloseEvent *event)
{
    event->ignore();
    emit requestClosure();
}

void PictureSearchWidget::paintEvent(QPaintEvent * event)
{
    QLinearGradient lg(0, 0, 0, 50);
    if (App::pictureService) {
        if (m_ui->fRadio->isChecked())
            lg.setColorAt(0.0, QColor(255, 200, 200, 200));
        else if (m_ui->gRadio->isChecked())
            lg.setColorAt(0.0, QColor(200, 220, 255, 200));
        lg.setColorAt(1.0, QColor(230, 230, 230, 220));
    } else {
        lg.setColorAt(0.0, QColor(255, 255, 255));
        lg.setColorAt(1.0, QColor(200, 200, 200));
    }

    // draw background frame
    QPainter p(this);
    p.setBrush(lg);
#if 0
    p.setPen(QPen(Qt::darkGray, 1));
    p.setRenderHint(QPainter::Antialiasing, true);
    QRectF boundaries = QRectF(rect()).adjusted(0.5 - FRAME_RADIUS, 0.5, -0.5, -0.5);
    p.drawRoundedRect(boundaries, FRAME_RADIUS, FRAME_RADIUS, Qt::AbsoluteSize);    
#else
    p.setCompositionMode(QPainter::CompositionMode_Source);
    p.fillRect(event->rect(), lg);
#endif
}

void PictureSearchWidget::slotProviderChanged()
{
    // no need to create the provider here, it will be created when searching
    m_ui->googleOptions->setVisible(m_ui->gRadio->isChecked());
    update();
}

void PictureSearchWidget::slotSearchClicked()
{
    // search...
    if (!m_searchSymbol) {
        // get the current search term
#ifdef ENABLE_GCOMPLETION
        m_completion->preventSuggest();
#endif
        QString searchName = m_ui->lineEdit->text();
        if (searchName.isEmpty())
            return;

        // start a picture search
        if (!App::pictureService) {
            if (m_ui->fRadio->isChecked())
                App::pictureService = new FlickrPictureService("292287089cdba89fdbd9994830cc4327", m_extAccessManager, this);
            else if (m_ui->gRadio->isChecked()) {
                GoogleImagesPictureService * gis = new GoogleImagesPictureService(m_extAccessManager, this);
                gis->configure(m_ui->contentCombo->currentIndex(), m_ui->sizeCombo->currentIndex());
                App::pictureService = gis;
            } else {
                qWarning("PictureSearchWidget::slotSearchClicked: unknown provider");
                return;
            }
            m_ui->fRadio->setVisible(false);
            m_ui->gRadio->setVisible(false);
            m_ui->googleOptions->setVisible(false);
            connect(App::pictureService, SIGNAL(searchStarted()), this, SLOT(slotSearchBegun()));
            connect(App::pictureService, SIGNAL(searchResult(int,QString,int,int)), this, SLOT(slotSearchResult(int,QString,int,int)));
            connect(App::pictureService, SIGNAL(searchThumbnail(int,QPixmap)), this, SLOT(slotSearchThumbnail(int,QPixmap)));
            connect(App::pictureService, SIGNAL(searchEnded(bool)), this, SLOT(slotSearchEnded(bool)));
        }
        App::pictureService->searchPics(searchName);
    }

    // or cancel...
    else if (App::pictureService) {
        App::pictureService->dropSearch();
        m_ui->listWidget->clear();
    }
}

void PictureSearchWidget::slotSearchBegun()
{
    m_ui->listWidget->clear();
    m_ui->listWidget->show();
    if (!m_searchSymbol) {
        m_searchSymbol = new SearchSymbol(m_ui->listWidget->viewport());
        m_searchSymbol->move(2, 2);
        m_searchSymbol->show();
    }
    m_ui->searchButton->setText(tr("Cancel"));
}

void PictureSearchWidget::slotSearchResult(int idx, const QString & title, int thumb_w, int thumb_h)
{
    // create the placeholder Icon
    QLinearGradient lg(0, 0, 0, thumb_h);
    lg.setColorAt(0.0, Qt::lightGray);
    lg.setColorAt(1.0, Qt::darkGray);
    QPixmap placeHolderIcon(thumb_w, thumb_h);
    QPainter painter(&placeHolderIcon);
    painter.fillRect(0, 0, thumb_w, thumb_h, lg);
    painter.end();

    // create the ListWidget Item
    QListWidgetItem * item = new QListWidgetItem;
    item->setIcon(placeHolderIcon);
#if 0
    item->setTextAlignment(Qt::AlignCenter);
    item->setText(title);
#else
    Q_UNUSED(title);
#endif

    // add it the ListWidget
    m_ui->listWidget->insertItem(idx, item);
}

void PictureSearchWidget::slotSearchThumbnail(int idx, const QPixmap & thumbnail)
{
    // update the pixmap of the Item
    if (QListWidgetItem * item = m_ui->listWidget->item(idx))
        item->setIcon(thumbnail);
}

void PictureSearchWidget::slotSearchEnded(bool)
{
    if (m_searchSymbol) {
        delete m_searchSymbol;
        m_searchSymbol = 0;
    }
    m_ui->searchButton->setText(tr("Search"));
}