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
|
/***************************************************************************
sq_previewwidget.cpp - description
-------------------
begin : ??? Mar 13 2007
copyright : (C) 2007 by Baryshev Dmitry
email : ksquirrel.iv@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. *
* *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <tqsize.h>
#include <tqimage.h>
#include <tqpainter.h>
#include <tdelocale.h>
#include <tdefileitem.h>
#include <tdepopupmenu.h>
#include <kcolordialog.h>
#include <tdeio/global.h>
#include <ksquirrel-libs/fmt_defs.h>
#include "ksquirrel.h"
#include "sq_previewwidget.h"
#include "sq_iconloader.h"
#include "sq_imageloader.h"
#include "sq_libraryhandler.h"
#include "sq_config.h"
#include "sq_downloader.h"
#include "sq_utils.h"
#ifdef SQ_HAVE_KEXIF
#include <libkexif/kexifdata.h>
#include <algorithm>
#include "sq_utils.h"
#endif
SQ_PreviewWidget * SQ_PreviewWidget::m_inst = 0;
SQ_PreviewWidget::SQ_PreviewWidget(TQWidget *parent, const char *name)
: TQWidget(parent, name, TQt::WNoAutoErase), all(0), small(0), m_ignore(true)
{
m_inst = this;
rereadColor();
down = new SQ_Downloader(TQT_TQOBJECT(this));
connect(down, TQT_SIGNAL(result(const KURL &)), TQT_TQOBJECT(this), TQT_SLOT(slotDownloadResult(const KURL &)));
connect(down, TQT_SIGNAL(percents(int)), TQT_TQOBJECT(this), TQT_SLOT(slotDownloadPercents(int)));
popup = new TDEPopupMenu;
popup->insertItem(i18n("Background color..."), TQT_TQOBJECT(this), TQT_SLOT(slotBackground()));
popup->insertItem(i18n("Text color..."), TQT_TQOBJECT(this), TQT_SLOT(slotText()));
popup->insertSeparator();
popup->insertItem(i18n("Go to first image")+"\tHome", TQT_TQOBJECT(this), TQT_SIGNAL(first()));
popup->insertItem(i18n("Next image")+"\tSpace", TQT_TQOBJECT(this), TQT_SIGNAL(next()));
popup->insertItem(i18n("Previous image")+"\tBackSpace", TQT_TQOBJECT(this), TQT_SIGNAL(previous()));
popup->insertItem(i18n("Go to last image")+"\tEnd", TQT_TQOBJECT(this), TQT_SIGNAL(last()));
popup->insertSeparator();
popup->insertItem(i18n("Execute")+"\tEnter", TQT_TQOBJECT(this), TQT_SIGNAL(execute()));
multi_pix = SQ_IconLoader::instance()->loadIcon("application-vnd.tde.tdemultiple", TDEIcon::Desktop, TDEIcon::SizeSmall);
setMinimumHeight(20);
setFocusPolicy(TQWidget::WheelFocus);
}
SQ_PreviewWidget::~SQ_PreviewWidget()
{
delete popup;
delete small;
delete all;
}
void SQ_PreviewWidget::load(const KURL &_url)
{
if(SQ_LibraryHandler::instance()->maybeSupported(_url) == SQ_LibraryHandler::No)
return;
if(!percentString.isEmpty())
{
percentString = TQString();
update();
}
down->kill();
if(m_forceignore || m_ignore)
{
pending = m_url = _url;
return;
}
else
pending = KURL();
m_url = _url;
if(m_url.isLocalFile())
slotDownloadResult(m_url);
else
{
KFileItem fi(KFileItem::Unknown, KFileItem::Unknown, m_url);
down->start(&fi);
}
}
void SQ_PreviewWidget::fitAndConvert()
{
if(!m_ignore && fit())
pixmap.convertFromImage(small?*small:*all);
}
void SQ_PreviewWidget::resizeEvent(TQResizeEvent *)
{
fitAndConvert();
}
void SQ_PreviewWidget::paintEvent(TQPaintEvent *)
{
TQPainter p(this);
p.fillRect(rect(), color);
int x = 4;
if(!percentString.isEmpty())
{
TQFont fnt = p.font();
fnt.setBold(true);
p.setFont(fnt);
p.setPen(colorText);
p.drawText(x, 4, width(), height(), TQt::AlignLeft, percentString);
}
if(!m_ignore && !pixmap.isNull())
{
p.drawPixmap((width() - pixmap.width()) / 2, (height() - pixmap.height()) / 2, pixmap);
if(multi)
{
x = x + multi_pix.width() + 4;
p.drawPixmap(4, 4, multi_pix);
}
if(dim)
{
TQFont fnt = p.font();
fnt.setBold(true);
p.setFont(fnt);
p.setPen(colorText);
p.drawText(x, 4, width(), height(), TQt::AlignLeft, dimstring);
}
}
}
bool SQ_PreviewWidget::fit()
{
if(!all)
return false;
// image is bigger than preview widget -
// scale it down
if(width() < 2 || height() < 2)
return false;
delete small;
small = 0;
if(all->width() > width() || all->height() > height())
{
small = new TQImage();
*small = SQ_Utils::scale(*all, width(), height(), SQ_Utils::SMOOTH_FAST, TQImage::ScaleMin);
}
return true;
}
void SQ_PreviewWidget::saveValues()
{
SQ_Config::instance()->setGroup("Sidebar");
SQ_Config::instance()->writeEntry("preview_background", color.name());
SQ_Config::instance()->writeEntry("preview_text", colorText.name());
}
void SQ_PreviewWidget::rereadColor()
{
SQ_Config::instance()->setGroup("Sidebar");
bool b = SQ_Config::instance()->readBoolEntry("preview", true);
m_forceignore = !b;
setShown(b);
color.setNamedColor(SQ_Config::instance()->readEntry("preview_background", "#4e4e4e"));
dim = SQ_Config::instance()->readBoolEntry("preview_text_enable", true);
colorText.setNamedColor(SQ_Config::instance()->readEntry("preview_text", "#ffffff"));
m_delay = SQ_Config::instance()->readNumEntry("preview_delay", 400);
m_cancel = SQ_Config::instance()->readBoolEntry("preview_dont", true);
if(m_delay < 50 || m_delay > 2000)
m_delay = 400;
}
void SQ_PreviewWidget::slotBackground()
{
KColorDialog dlg(KSquirrel::app(), 0, true);
dlg.setColor(color);
if(dlg.exec() == TQDialog::Accepted)
{
color = dlg.color();
saveValues();
update();
}
}
void SQ_PreviewWidget::slotText()
{
KColorDialog dlg(KSquirrel::app(), 0, true);
dlg.setColor(colorText);
if(dlg.exec() == TQDialog::Accepted)
{
colorText = dlg.color();
saveValues();
update();
}
}
void SQ_PreviewWidget::mousePressEvent(TQMouseEvent *e)
{
e->accept();
if(e->button() == Qt::RightButton)
popup->exec(e->globalPos());
}
void SQ_PreviewWidget::loadPending()
{
if(pending.isValid())
{
KURL tmp = pending;
load(tmp);
}
}
void SQ_PreviewWidget::slotDownloadResult(const KURL &url)
{
percentString = TQString();
TQString path = url.path();
fmt_info *finfo;
RGBA *bits;
// load first page
bool b = SQ_ImageLoader::instance()->loadImage(path, SQ_CodecSettings::ImageViewer, true, 2);
finfo = SQ_ImageLoader::instance()->info();
bits = SQ_ImageLoader::instance()->bits();
// memory allocation failed in SQ_ImageLoader::loadImage()
if(!b || !bits || !finfo->image.size())
return;
delete small;
delete all;
all = small = 0;
pixmap = TQPixmap();
int w = finfo->image[0].w;
int h = finfo->image[0].h;
dimstring = TQString::fromLatin1("%1x%2").arg(w).arg(h);
const int wh = w * h;
unsigned char t;
for(int i = 0;i < wh;i++)
{
t = (bits+i)->r;
(bits+i)->r = (bits+i)->b;
(bits+i)->b = t;
}
all = new TQImage((uchar *)bits, w, h, 32, 0, 0, TQImage::LittleEndian);
all->setAlphaBuffer(true);
#ifdef SQ_HAVE_KEXIF
KExifData data;
data.readFromFile(path);
int O = data.getImageQt::Orientation();
if(O != KExifData::UNSPECIFIED && O != KExifData::NORMAL)
{
// copy original image
TQImage img = *all;
// rotate image
SQ_Utils::exifRotate(TQString(), img, O);
// transfer back
*all = img;
}
else
#endif
*all = all->copy();
multi = finfo->image.size() > 1;
SQ_ImageLoader::instance()->cleanup();
fitAndConvert();
update();
}
void SQ_PreviewWidget::keyPressEvent(TQKeyEvent *e)
{
e->accept();
int key = e->key();
if(key == TQt::Key_PageDown || key == TQt::Key_Space)
emit next();
else if(key == TQt::Key_PageUp || key == TQt::Key_BackSpace)
emit previous();
else if(key == TQt::Key_Return || key == TQt::Key_Enter)
emit execute();
else if(key == TQt::Key_Home)
emit first();
else if(key == TQt::Key_End)
emit last();
}
void SQ_PreviewWidget::wheelEvent(TQWheelEvent *e)
{
if(e->delta() < 0)
emit next();
else
emit previous();
}
void SQ_PreviewWidget::mouseDoubleClickEvent(TQMouseEvent *e)
{
e->accept();
emit execute();
}
void SQ_PreviewWidget::slotDownloadPercents(int p)
{
percentString = i18n("Downloading...") + ' ' + TDEIO::convertSize(p);
update();
}
#include "sq_previewwidget.moc"
|