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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
/* This file is part of the KDE project
Copyright (C) 2002 Peter Simonsson <psn@linux.se>
Copyright (C) 2004, 2006 Jaroslaw Staniek <js@iidea.pl>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kexiblobtableedit.h"
#include <stdlib.h>
#include <tqdatastream.h>
#include <tqfile.h>
#include <tqpopupmenu.h>
#include <tqtextedit.h>
#include <tqlayout.h>
#include <tqstatusbar.h>
#include <tqlabel.h>
#include <tqpixmap.h>
#include <tqimage.h>
#include <tqpainter.h>
#include <tqtooltip.h>
#include <tqapplication.h>
#include <tqclipboard.h>
#include <tqbuffer.h>
#include <kdebug.h>
#include <ktempfile.h>
#include <kmimetype.h>
#include <kmimemagic.h>
#include <kuserprofile.h>
#include <kservice.h>
#include <kprocess.h>
#include <kopenwith.h>
#include <kurl.h>
#include <karrowbutton.h>
#include <klocale.h>
#include <kfiledialog.h>
#include <kio/job.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kpopupmenu.h>
#include <kstdaccel.h>
#include <kexiutils/utils.h>
#include <widget/utils/kexidropdownbutton.h>
#include <widget/utils/kexicontextmenuutils.h>
//! @internal
class KexiBlobTableEdit::Private
{
public:
Private()
: popup(0)
, readOnly(false)
, setValueInternalEnabled(true)
{
}
TQByteArray value;
KexiDropDownButton *button;
TQSize totalSize;
KexiImageContextMenu *popup;
bool readOnly : 1; //!< cached for slotUpdateActionsAvailabilityRequested()
bool setValueInternalEnabled : 1; //!< used to disable KexiBlobTableEdit::setValueInternal()
};
//======================================================
KexiBlobTableEdit::KexiBlobTableEdit(KexiTableViewColumn &column, TQWidget *parent)
: KexiTableEdit(column, parent)
, d ( new Private() )
{
setName("KexiBlobTableEdit");
// m_proc = 0;
// m_content = 0;
m_hasFocusableWidget = false;
d->button = new KexiDropDownButton( parentWidget() /*usually a viewport*/ );
d->button->hide();
TQToolTip::add(d->button, i18n("Click to show available actions for this cell"));
d->popup = new KexiImageContextMenu(this);
d->popup->installEventFilter(this);
if (column.columnInfo)
KexiImageContextMenu::updateTitle( d->popup, column.columnInfo->captionOrAliasOrName(),
//! @todo pixmaplabel icon is hardcoded...
"pixmaplabel" );
d->button->setPopup( d->popup );
//force edit requested to start editing... (this will call slotUpdateActionsAvailabilityRequested())
//connect(d->popup, TQT_SIGNAL(aboutToShow()), this, TQT_SIGNAL(editRequested()));
connect(d->popup, TQT_SIGNAL(updateActionsAvailabilityRequested(bool&, bool&)),
this, TQT_SLOT(slotUpdateActionsAvailabilityRequested(bool&, bool&)));
connect(d->popup, TQT_SIGNAL(insertFromFileRequested(const KURL&)),
this, TQT_SLOT(handleInsertFromFileAction(const KURL&)));
connect(d->popup, TQT_SIGNAL(saveAsRequested(const TQString&)),
this, TQT_SLOT(handleSaveAsAction(const TQString&)));
connect(d->popup, TQT_SIGNAL(cutRequested()),
this, TQT_SLOT(handleCutAction()));
connect(d->popup, TQT_SIGNAL(copyRequested()),
this, TQT_SLOT(handleCopyAction()));
connect(d->popup, TQT_SIGNAL(pasteRequested()),
this, TQT_SLOT(handlePasteAction()));
connect(d->popup, TQT_SIGNAL(clearRequested()),
this, TQT_SLOT(clear()));
connect(d->popup, TQT_SIGNAL(showPropertiesRequested()),
this, TQT_SLOT(handleShowPropertiesAction()));
}
KexiBlobTableEdit::~KexiBlobTableEdit()
{
delete d;
#if 0
kdDebug() << "KexiBlobTableEdit: Cleaning up..." << endl;
if (m_tempFile) {
m_tempFile->unlink();
//todo
}
delete m_proc;
m_proc = 0;
kdDebug() << "KexiBlobTableEdit: Ready." << endl;
#endif
}
//! initializes this editor with \a add value
void KexiBlobTableEdit::setValueInternal(const TQVariant& add, bool removeOld)
{
if (!d->setValueInternalEnabled)
return;
if (removeOld)
d->value = add.toByteArray();
else //do not add "m_origValue" to "add" as this is TQByteArray
d->value = m_origValue.toByteArray();
#if 0 //todo?
TQByteArray val = m_origValue.toByteArray();
kdDebug() << "KexiBlobTableEdit: Size of BLOB: " << val.size() << endl;
m_tempFile = new KTempFile();
m_tempFile->setAutoDelete(true);
kdDebug() << "KexiBlobTableEdit: Creating temporary file: " << m_tempFile->name() << endl;
m_tempFile->dataStream()->writeRawBytes(val.data(), val.size());
m_tempFile->close();
delete m_tempFile;
m_tempFile = 0;
KMimeMagicResult* mmr = KMimeMagic::self()->findFileType(m_tempFile->name());
kdDebug() << "KexiBlobTableEdit: Mimetype = " << mmr->mimeType() << endl;
setViewWidget( new TQWidget(this) );
#endif
}
bool KexiBlobTableEdit::valueIsNull()
{
//TODO
d->value.size();
return d->value.isEmpty();
}
bool KexiBlobTableEdit::valueIsEmpty()
{
//TODO
return d->value.isEmpty();
}
TQVariant
KexiBlobTableEdit::value()
{
return d->value;
#if 0
//todo
// ok = true;
if(m_content && m_content->isModified())
{
return TQVariant(m_content->text());
}
TQByteArray value;
TQFile f( m_tempFile->name() );
f.open(IO_ReadOnly);
TQDataStream stream(&f);
char* data = (char*) malloc(f.size());
value.resize(f.size());
stream.readRawBytes(data, f.size());
value.duplicate(data, f.size());
free(data);
kdDebug() << "KexiBlobTableEdit: Size of BLOB: " << value.size() << endl;
return TQVariant(value);
#endif
}
void KexiBlobTableEdit::paintFocusBorders( TQPainter *p, TQVariant &, int x, int y, int w, int h )
{
// d->currentEditorWidth = w;
if (!d->readOnly && w > d->button->width())
w -= d->button->width();
p->drawRect(x, y, w, h);
}
void
KexiBlobTableEdit::setupContents( TQPainter *p, bool focused, const TQVariant& val,
TQString &txt, int &align, int &x, int &y_offset, int &w, int &h )
{
Q_UNUSED(focused);
Q_UNUSED(txt);
Q_UNUSED(align);
//! @todo optimize: load to m_pixmap, downsize
TQPixmap pixmap;
x = 0;
w -= 1; //a place for border
h -= 1; //a place for border
if (p && val.canCast(TQVariant::ByteArray) && pixmap.loadFromData(val.toByteArray())) {
KexiUtils::drawPixmap( *p, 0/*lineWidth*/, TQRect(x, y_offset, w, h),
pixmap, TQt::AlignCenter, true/*scaledContents*/, true/*keepAspectRatio*/);
}
}
bool KexiBlobTableEdit::cursorAtStart()
{
return true;
}
bool KexiBlobTableEdit::cursorAtEnd()
{
return true;
}
void KexiBlobTableEdit::handleInsertFromFileAction(const KURL& url)
{
if (isReadOnly())
return;
TQString fileName( url.isLocalFile() ? url.path() : url.prettyURL() );
//! @todo download the file if remote, then set fileName properly
TQFile f(fileName);
if (!f.open(IO_ReadOnly)) {
//! @todo err msg
return;
}
TQByteArray ba = f.readAll();
if (f.status()!=IO_Ok) {
//! @todo err msg
f.close();
return;
}
f.close();
// m_valueMimeType = KImageIO::mimeType( fileName );
setValueInternal( ba, true );
signalEditRequested();
//emit acceptRequested();
}
void KexiBlobTableEdit::handleAboutToSaveAsAction(TQString& origFilename, TQString& fileExtension, bool& dataIsEmpty)
{
Q_UNUSED(origFilename);
Q_UNUSED(fileExtension);
dataIsEmpty = valueIsEmpty();
//! @todo no fname stored for now
}
void KexiBlobTableEdit::handleSaveAsAction(const TQString& fileName)
{
TQFile f(fileName);
if (!f.open(IO_WriteOnly)) {
//! @todo err msg
return;
}
f.writeBlock( d->value );
if (f.status()!=IO_Ok) {
//! @todo err msg
f.close();
return;
}
f.close();
}
void KexiBlobTableEdit::handleCutAction()
{
if (isReadOnly())
return;
handleCopyAction();
clear();
}
void KexiBlobTableEdit::handleCopyAction()
{
executeCopyAction(d->value);
}
void KexiBlobTableEdit::executeCopyAction(const TQByteArray& data)
{
TQPixmap pixmap;
if (!pixmap.loadFromData(data))
return;
tqApp->tqclipboard()->setPixmap(pixmap, TQClipboard::Clipboard);
}
void KexiBlobTableEdit::handlePasteAction()
{
if (isReadOnly())
return;
TQPixmap pm( tqApp->tqclipboard()->pixmap(TQClipboard::Clipboard) );
TQByteArray ba;
TQBuffer buffer( ba );
buffer.open( IO_WriteOnly );
if (pm.save( &buffer, "PNG" )) {// write pixmap into ba in PNG format
setValueInternal( ba, true );
}
else {
setValueInternal( TQByteArray(), true );
}
signalEditRequested();
//emit acceptRequested();
repaintRelatedCell();
}
void KexiBlobTableEdit::clear()
{
setValueInternal( TQByteArray(), true );
signalEditRequested();
//emit acceptRequested();
repaintRelatedCell();
}
void KexiBlobTableEdit::handleShowPropertiesAction()
{
//! @todo
}
void KexiBlobTableEdit::showFocus( const TQRect& r, bool readOnly )
{
d->readOnly = readOnly; //cache for slotUpdateActionsAvailabilityRequested()
// d->button->move( pos().x()+ width(), pos().y() );
updateFocus( r );
// d->button->setEnabled(!readOnly);
if (d->readOnly)
d->button->hide();
else
d->button->show();
}
void KexiBlobTableEdit::resize(int w, int h)
{
d->totalSize = TQSize(w,h);
const int addWidth = d->readOnly ? 0 : d->button->width();
TQWidget::resize(w - addWidth, h);
if (!d->readOnly)
d->button->resize( h, h );
m_rightMarginWhenFocused = m_rightMargin + addWidth;
TQRect r( pos().x(), pos().y(), w+1, h+1 );
r.moveBy(m_scrollView->contentsX(),m_scrollView->contentsY());
updateFocus( r );
//todo if (d->popup) {
//todo d->popup->updateSize();
//todo }
}
void KexiBlobTableEdit::updateFocus( const TQRect& r )
{
if (!d->readOnly) {
if (d->button->width() > r.width())
moveChild(d->button, r.right() + 1, r.top());
else
moveChild(d->button, r.right() - d->button->width(), r.top() );
}
}
void KexiBlobTableEdit::hideFocus()
{
d->button->hide();
}
TQSize KexiBlobTableEdit::totalSize() const
{
return d->totalSize;
}
void KexiBlobTableEdit::slotUpdateActionsAvailabilityRequested(bool& valueIsNull, bool& valueIsReadOnly)
{
emit editRequested();
valueIsNull = this->valueIsNull();
valueIsReadOnly = d->readOnly || isReadOnly();
}
void KexiBlobTableEdit::signalEditRequested()
{
d->setValueInternalEnabled = false;
emit editRequested();
d->setValueInternalEnabled = true;
}
bool KexiBlobTableEdit::handleKeyPress( TQKeyEvent* ke, bool editorActive )
{
Q_UNUSED(editorActive);
const int k = ke->key();
KKey kkey(ke);
if (!d->readOnly) {
if ((ke->state()==Qt::NoButton && k==TQt::Key_F4)
|| (ke->state()==TQt::AltButton && k==TQt::Key_Down)) {
d->button->animateClick();
TQMouseEvent me( TQEvent::MouseButtonPress, TQPoint(2,2), Qt::LeftButton, Qt::NoButton );
TQApplication::sendEvent( d->button, &me );
}
else if ((ke->state()==Qt::NoButton && (k==TQt::Key_F2 || k==TQt::Key_Space || k==TQt::Key_Enter || k==TQt::Key_Return))) {
d->popup->insertFromFile();
}
else
return false;
}
else
return false;
return true;
}
bool KexiBlobTableEdit::handleDoubleClick()
{
d->popup->insertFromFile();
return true;
}
void KexiBlobTableEdit::handleCopyAction(const TQVariant& value, const TQVariant& visibleValue)
{
Q_UNUSED(visibleValue);
executeCopyAction(value.toByteArray());
}
void KexiBlobTableEdit::handleAction(const TQString& actionName)
{
if (actionName=="edit_paste") {
d->popup->paste();
}
else if (actionName=="edit_cut") {
emit editRequested();
d->popup->cut();
}
}
bool KexiBlobTableEdit::eventFilter( TQObject *o, TQEvent *e )
{
if (TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(d->popup) && e->type()==TQEvent::KeyPress) {
TQKeyEvent* ke = TQT_TQKEYEVENT(e);
const int state = ke->state();
const int k = ke->key();
if ( (state==Qt::NoButton && (k==TQt::Key_Tab || k==TQt::Key_Left || k==TQt::Key_Right))
|| (state==TQt::ShiftButton && k==TQt::Key_Backtab)
)
{
d->popup->hide();
TQApplication::sendEvent( this, ke ); //re-send to move cursor
return true;
}
}
return false;
}
KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiBlobEditorFactoryItem, KexiBlobTableEdit)
//=======================
// KexiKIconTableEdit class is temporarily here:
//! @internal
class KexiKIconTableEdit::Private
{
public:
Private()
: pixmapCache(17, 17, false)
{
}
//! We've no editor widget that would store current value, so we do this here
TQVariant currentValue;
TQCache<TQPixmap> pixmapCache;
};
KexiKIconTableEdit::KexiKIconTableEdit(KexiTableViewColumn &column, TQWidget *parent)
: KexiTableEdit(column, parent)
, d( new Private() )
{
setName("KexiKIconTableEdit");
init();
}
KexiKIconTableEdit::~KexiKIconTableEdit()
{
delete d;
}
void KexiKIconTableEdit::init()
{
m_hasFocusableWidget = false;
d->pixmapCache.setAutoDelete(true);
}
void KexiKIconTableEdit::setValueInternal(const TQVariant& /*add*/, bool /*removeOld*/)
{
d->currentValue = m_origValue;
}
bool KexiKIconTableEdit::valueIsNull()
{
return d->currentValue.isNull();
}
bool KexiKIconTableEdit::valueIsEmpty()
{
return d->currentValue.isNull();
}
TQVariant KexiKIconTableEdit::value()
{
return d->currentValue;
}
void KexiKIconTableEdit::clear()
{
d->currentValue = TQVariant();
}
bool KexiKIconTableEdit::cursorAtStart()
{
return true;
}
bool KexiKIconTableEdit::cursorAtEnd()
{
return true;
}
void KexiKIconTableEdit::setupContents( TQPainter *p, bool /*focused*/, const TQVariant& val,
TQString &/*txt*/, int &/*align*/, int &/*x*/, int &y_offset, int &w, int &h )
{
Q_UNUSED( y_offset );
#if 0
#ifdef TQ_WS_WIN
y_offset = -1;
#else
y_offset = 0;
#endif
int s = TQMAX(h - 5, 12);
s = TQMIN( h-3, s );
s = TQMIN( w-3, s );//avoid too large box
TQRect r( TQMAX( w/2 - s/2, 0 ) , h/2 - s/2 /*- 1*/, s, s);
p->setPen(TQPen(colorGroup().text(), 1));
p->drawRect(r);
if (val.asBool()) {
p->drawLine(r.x(), r.y(), r.right(), r.bottom());
p->drawLine(r.x(), r.bottom(), r.right(), r.y());
}
#endif
TQString key = val.toString();
TQPixmap *pix = 0;
if (!key.isEmpty() && !(pix = d->pixmapCache[ key ])) {
//cache pixmap
TQPixmap pm = KGlobal::iconLoader()->loadIcon( key, KIcon::Small,
0, KIcon::DefaultState, 0L, true/*canReturnNull*/ );
if (!pm.isNull()) {
pix = new TQPixmap(pm);
d->pixmapCache.insert(key, pix);
}
}
if (p && pix) {
p->drawPixmap( (w-pix->width())/2, (h-pix->height())/2, *pix );
}
}
void KexiKIconTableEdit::handleCopyAction(const TQVariant& value, const TQVariant& visibleValue)
{
Q_UNUSED(value);
Q_UNUSED(visibleValue);
}
KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiKIconTableEditorFactoryItem, KexiKIconTableEdit)
#include "kexiblobtableedit.moc"
|