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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/* ============================================================
*
* This file is a part of the rekonq project
*
* Copyright (C) 2009 by Andrea Diamantini <adjam7 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 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ============================================================ */
// Self Includes
#include "previewimage.h"
#include "previewimage.moc"
// Local Includes
#include "application.h"
#include "history.h"
#include "rekonq.h"
#include "mainwindow.h"
#include "mainview.h"
// KDE Includes
#include <KUrl>
#include <KStandardDirs>
#include <KDebug>
#include <KMenu>
#include <KAction>
#include <KLocale>
// Qt Includes
#include <QFile>
#include <QMovie>
#include <QMouseEvent>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPainter>
PreviewImage::PreviewImage(const QUrl &url, const QString &title, int index, bool isFavorite)
: QWidget()
, ws(0)
, loadingSnapshot(false)
, m_url(url)
, m_title(title)
, m_isFavorite(isFavorite)
, m_index(index)
, m_button(0)
, m_imageLabel(new QLabel)
, m_textLabel(new QLabel)
, m_backgroundLabel(new QLabel)
, m_previewLabel(new QLabel)
{
int borderTop = 14;
int borderRight = 16;
int borderBottom = 14;
int borderLeft = 16;
int previewWidth = 200;
int previewHeight = 150;
int urlHeight = 18;
m_size = QSize(borderLeft+previewWidth+borderRight, borderTop+previewHeight+borderBottom+urlHeight);
setFixedSize(m_size);
m_previewLabel->setFixedSize(m_size);
m_backgroundLabel->setPixmap(renderBackground(previewWidth,previewHeight, borderTop, borderBottom, borderLeft, borderRight));
m_previewLabel->setAlignment(Qt::AlignCenter);
m_backgroundLabel->setAlignment(Qt::AlignCenter);
m_imageLabel->setAlignment(Qt::AlignCenter);
m_textLabel->setAlignment(Qt::AlignCenter);
m_previewLabel->setLayout(new QVBoxLayout);
m_previewLabel->layout()->setMargin(0);
m_previewLabel->layout()->addWidget(m_backgroundLabel);
m_previewLabel->layout()->addWidget(m_textLabel);
m_previewLabel->setCursor(Qt::PointingHandCursor);
m_backgroundLabel->setLayout(new QVBoxLayout);
m_backgroundLabel->layout()->addWidget(m_imageLabel);
setLayout(new QHBoxLayout);
layout()->setMargin(0);
layout()->setAlignment(Qt::AlignCenter);
layout()->addWidget(m_previewLabel);
loadUrlPreview(url);
}
PreviewImage::~PreviewImage()
{
delete ws;
delete m_textLabel;
delete m_imageLabel;
delete m_backgroundLabel;
delete m_previewLabel;
}
QPixmap PreviewImage::renderBackground(int w, int h, int t, int b, int l, int r)
{
QImage backImage(KStandardDirs::locate("appdata", "pics/bg.png"));
QImage resultImage(QSize(w + l + r, h + t + b), QImage::Format_ARGB32_Premultiplied);
if (!backImage.isNull())
{
int sw = backImage.width() - l - r;
int sh = backImage.height() - t - b;
QPainter pt(&resultImage);
pt.setCompositionMode(QPainter::CompositionMode_Source);
pt.fillRect(resultImage.rect(), Qt::transparent);
pt.drawImage(QRect(0, 0, l, t), backImage, QRect(0, 0, l, t));
pt.drawImage(QRect(l, 0, w, t), backImage, QRect(l, 0, sw, t));
pt.drawImage(QRect(l + w, 0, r, t), backImage, QRect(l + sw, 0, r, t));
pt.drawImage(QRect(0, t, l, h), backImage, QRect(0, t, l, sh));
pt.drawImage(QRect(l, t, w, h), backImage, QRect(l, t, sw, sh));
pt.drawImage(QRect(l + w, t, r, h), backImage, QRect(l + sw, t, r, sh));
pt.drawImage(QRect(0, t + h, l , b), backImage, QRect(0, t + sh, l , b));
pt.drawImage(QRect(l, t + h, w, b), backImage, QRect(l, t + sh, sw, b));
pt.drawImage(QRect(l + w, t + h, w, b), backImage, QRect(l + sw, t + sh, sw, b));
pt.end();
}
return QPixmap::fromImage(resultImage);
}
void PreviewImage::loadUrlPreview(const QUrl& url)
{
m_url = url;
if(url.isEmpty())
{
showEmptyPreview();
return;
}
m_previewLabel->setFixedSize(m_size); //unhide
m_savePath = KStandardDirs::locateLocal("cache", QString("thumbs/") + guessNameFromUrl(m_url) + ".png", true);
if(QFile::exists(m_savePath))
{
m_pixmap.load(m_savePath);
m_imageLabel->setPixmap(m_pixmap);
checkTitle();
m_textLabel->setText(m_title);
}
else
{
loadingSnapshot = true;
ws = new WebSnap( url );
connect(ws, SIGNAL(finished()), this, SLOT(snapFinished()));
QString path = KStandardDirs::locate("appdata", "pics/busywidget.gif");
// load an animation waiting for site preview
QMovie *movie = new QMovie(path, QByteArray(), this);
movie->setSpeed(50);
m_imageLabel->setMovie(movie);
movie->start();
m_textLabel->setText( i18n("Loading preview...") );
setCursor(Qt::BusyCursor);
}
}
void PreviewImage::snapFinished()
{
loadingSnapshot = false;
QMovie *m = m_imageLabel->movie();
delete m;
m_imageLabel->setMovie(0);
m_pixmap = ws->previewImage();
m_imageLabel->setPixmap(m_pixmap);
checkTitle();
m_textLabel->setText(m_title);
setCursor(Qt::PointingHandCursor);
m_pixmap.save(m_savePath);
if(m_index > -1)
{
// Update title
QStringList names = ReKonfig::previewNames();
// update url (for added thumbs)
QStringList urls = ReKonfig::previewUrls();
// stripTrailingSlash to be sure to get the same string for same address
urls.replace(m_index, ws->snapUrl().toString(QUrl::StripTrailingSlash));
names.replace(m_index, ws->snapTitle());
ReKonfig::setPreviewNames(names);
ReKonfig::setPreviewUrls(urls);
ReKonfig::self()->writeConfig();
}
}
void PreviewImage::showEmptyPreview()
{
if(!m_isFavorite)
return;
m_imageLabel->clear();
m_textLabel->clear();
m_previewLabel->setFixedSize(0,0); //hide
m_button = new QToolButton();
m_button->setDefaultAction(historyMenu());
m_button->setPopupMode(QToolButton::InstantPopup);
m_button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
m_button->setText(i18n("Add Preview"));
m_button->setAutoRaise(true);
m_button->setIconSize(QSize(48, 48));
layout()->addWidget(m_button);
}
void PreviewImage::mouseDoubleClickEvent(QMouseEvent *event)
{
kDebug() << "no double click over here, thanks :D";
Q_UNUSED(event);
}
void PreviewImage::mouseMoveEvent(QMouseEvent *event)
{
kDebug() << "moving mouse over preview image";
Q_UNUSED(event)
}
void PreviewImage::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
Application::instance()->loadUrl(m_url);
return;
}
else if(event->button() == Qt::MidButton)
{
Application::instance()->loadUrl(m_url, Rekonq::SettingOpenTab);
return;
}
QWidget::mousePressEvent(event);
}
void PreviewImage::mouseReleaseEvent(QMouseEvent *event)
{
kDebug() << "NO000... don't leave your finger from the button!!";
Q_UNUSED(event)
}
void PreviewImage::contextMenuEvent(QContextMenuEvent* event)
{
if(!m_isFavorite)
return;
if(loadingSnapshot)
return;
KMenu menu(this);
KAction *a;
if(!m_url.isEmpty())
{
a = new KAction(KIcon("edit-delete"), i18n("Remove Thumbnail"), this);
connect(a, SIGNAL(triggered(bool)), this, SLOT(removeMe()));
menu.addAction(a);
a = new KAction(KIcon("view-refresh"), i18n("Refresh Thumbnail"), &menu);
connect(a, SIGNAL(triggered(bool)), this, SLOT(refreshPreview()));
menu.addAction(a);
}
menu.addAction(historyMenu());
menu.exec(mapToGlobal(event->pos()));
}
KActionMenu* PreviewImage::historyMenu()
{
KActionMenu *histMenu = new KActionMenu(KIcon("insert-image"), i18n("Set Page to Preview"), this);
QList<HistoryItem> history = Application::historyManager()->history();
if(history.isEmpty())
{
KAction *a = new KAction(i18n("History is Empty"), this);
a->setEnabled(false);
histMenu->addAction(a);
return histMenu;
}
int maxItems = 15;
for (int i = 0; i < maxItems && i < history.size() ; ++i)
{
HistoryItem it = history.at(i);
KAction *a = new KAction(Application::icon(it.url), it.title, this);
QStringList urlData;
urlData << it.url << it.title;
a->setData(urlData);
connect(a, SIGNAL(triggered(bool)), this, SLOT(setUrlFromAction()));
histMenu->addAction(a);
}
return histMenu;
}
void PreviewImage::removeMe()
{
QStringList names = ReKonfig::previewNames();
QStringList urls = ReKonfig::previewUrls();
int index = urls.indexOf(QRegExp(m_url.toString(QUrl::StripTrailingSlash), Qt::CaseSensitive, QRegExp::FixedString));
urls.replace(index, QString(""));
names.replace(index, QString(""));
ReKonfig::setPreviewNames(names);
ReKonfig::setPreviewUrls(urls);
// sync file data
ReKonfig::self()->writeConfig();
showEmptyPreview();
m_url = "";
}
void PreviewImage::setUrlFromAction()
{
KAction *a = qobject_cast<KAction*>(sender());
QStringList urlData = a->data().toStringList();
m_url = KUrl(urlData.at(0));
m_title = urlData.at(1);
checkTitle();
if(m_button)
{
m_imageLabel->layout()->deleteLater();
m_button->menu()->deleteLater();
m_button->deleteLater();
}
loadUrlPreview(m_url);
// Update title
QStringList names = ReKonfig::previewNames();
// update url (for added thumbs)
QStringList urls = ReKonfig::previewUrls();
// stripTrailingSlash to be sure to get the same string for same address
urls.replace(m_index, m_url.toString(QUrl::StripTrailingSlash));
names.replace(m_index, m_title);
ReKonfig::setPreviewNames(names);
ReKonfig::setPreviewUrls(urls);
ReKonfig::self()->writeConfig();
}
void PreviewImage::refreshPreview()
{
QString path = KStandardDirs::locateLocal("cache", QString("thumbs/") + guessNameFromUrl(m_url) + ".png", true);
QFile::remove(path);
loadUrlPreview(m_url);
}
QString PreviewImage::guessNameFromUrl(QUrl url)
{
QString name = url.toString( QUrl::RemoveScheme | QUrl::RemoveUserInfo | QUrl::StripTrailingSlash );
// TODO learn Regular Expressions :)
// and implement something better here..
name.remove('/');
name.remove('&');
name.remove('.');
name.remove('-');
name.remove('_');
name.remove('?');
name.remove('=');
name.remove('+');
return name;
}
void PreviewImage::checkTitle()
{
if(m_title.length() > 23)
{
m_title.truncate(20);
m_title += "...";
}
}
|