kexiblobtableedit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Peter Simonsson <psn@linux.se>
3 Copyright (C) 2004, 2006 Jaroslaw Staniek <js@iidea.pl>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "kexiblobtableedit.h"
22
23#include <stdlib.h>
24
25#include <tqdatastream.h>
26#include <tqfile.h>
27#include <tqpopupmenu.h>
28#include <tqtextedit.h>
29#include <tqlayout.h>
30#include <tqstatusbar.h>
31#include <tqlabel.h>
32#include <tqpixmap.h>
33#include <tqimage.h>
34#include <tqpainter.h>
35#include <tqtooltip.h>
36#include <tqapplication.h>
37#include <tqclipboard.h>
38#include <tqbuffer.h>
39
40#include <kdebug.h>
41#include <tdetempfile.h>
42#include <kmimetype.h>
43#include <kmimemagic.h>
44#include <kuserprofile.h>
45#include <kservice.h>
46#include <kprocess.h>
47#include <kopenwith.h>
48#include <kurl.h>
49#include <karrowbutton.h>
50#include <tdelocale.h>
51#include <tdefiledialog.h>
52#include <tdeio/job.h>
53#include <tdeglobal.h>
54#include <kiconloader.h>
55#include <tdepopupmenu.h>
56#include <tdestdaccel.h>
57
58#include <kexiutils/utils.h>
59#include <widget/utils/kexidropdownbutton.h>
60#include <widget/utils/kexicontextmenuutils.h>
61
63class KexiBlobTableEdit::Private
64{
65public:
66 Private()
67 : popup(0)
68 , readOnly(false)
69 , setValueInternalEnabled(true)
70 {
71 }
72
73 TQByteArray value;
74 KexiDropDownButton *button;
75 TQSize totalSize;
77 bool readOnly : 1;
78 bool setValueInternalEnabled : 1;
79};
80
81//======================================================
82
83KexiBlobTableEdit::KexiBlobTableEdit(KexiTableViewColumn &column, TQWidget *parent)
84 : KexiTableEdit(column, parent)
85 , d ( new Private() )
86{
87 setName("KexiBlobTableEdit");
88// m_proc = 0;
89// m_content = 0;
90 m_hasFocusableWidget = false;
91 d->button = new KexiDropDownButton( parentWidget() /*usually a viewport*/ );
92 d->button->hide();
93 TQToolTip::add(d->button, i18n("Click to show available actions for this cell"));
94
95 d->popup = new KexiImageContextMenu(this);
96 d->popup->installEventFilter(this);
97 if (column.columnInfo)
100 "pixmaplabel" );
101 d->button->setPopup( d->popup );
102
103 //force edit requested to start editing... (this will call slotUpdateActionsAvailabilityRequested())
104 //connect(d->popup, TQ_SIGNAL(aboutToShow()), this, TQ_SIGNAL(editRequested()));
105
106 connect(d->popup, TQ_SIGNAL(updateActionsAvailabilityRequested(bool&, bool&)),
107 this, TQ_SLOT(slotUpdateActionsAvailabilityRequested(bool&, bool&)));
108
109 connect(d->popup, TQ_SIGNAL(insertFromFileRequested(const KURL&)),
110 this, TQ_SLOT(handleInsertFromFileAction(const KURL&)));
111 connect(d->popup, TQ_SIGNAL(saveAsRequested(const TQString&)),
112 this, TQ_SLOT(handleSaveAsAction(const TQString&)));
113 connect(d->popup, TQ_SIGNAL(cutRequested()),
114 this, TQ_SLOT(handleCutAction()));
115 connect(d->popup, TQ_SIGNAL(copyRequested()),
116 this, TQ_SLOT(handleCopyAction()));
117 connect(d->popup, TQ_SIGNAL(pasteRequested()),
118 this, TQ_SLOT(handlePasteAction()));
119 connect(d->popup, TQ_SIGNAL(clearRequested()),
120 this, TQ_SLOT(clear()));
121 connect(d->popup, TQ_SIGNAL(showPropertiesRequested()),
122 this, TQ_SLOT(handleShowPropertiesAction()));
123}
124
125KexiBlobTableEdit::~KexiBlobTableEdit()
126{
127 delete d;
128#if 0
129 kdDebug() << "KexiBlobTableEdit: Cleaning up..." << endl;
130 if (m_tempFile) {
131 m_tempFile->unlink();
132 //todo
133 }
134 delete m_proc;
135 m_proc = 0;
136 kdDebug() << "KexiBlobTableEdit: Ready." << endl;
137#endif
138}
139
141void KexiBlobTableEdit::setValueInternal(const TQVariant& add, bool removeOld)
142{
143 if (!d->setValueInternalEnabled)
144 return;
145 if (removeOld)
146 d->value = add.toByteArray();
147 else //do not add "m_origValue" to "add" as this is TQByteArray
148 d->value = m_origValue.toByteArray();
149
150#if 0 //todo?
151 TQByteArray val = m_origValue.toByteArray();
152 kdDebug() << "KexiBlobTableEdit: Size of BLOB: " << val.size() << endl;
153 m_tempFile = new KTempFile();
154 m_tempFile->setAutoDelete(true);
155 kdDebug() << "KexiBlobTableEdit: Creating temporary file: " << m_tempFile->name() << endl;
156 m_tempFile->dataStream()->writeRawBytes(val.data(), val.size());
157 m_tempFile->close();
158 delete m_tempFile;
159 m_tempFile = 0;
160
161 KMimeMagicResult* mmr = KMimeMagic::self()->findFileType(m_tempFile->name());
162 kdDebug() << "KexiBlobTableEdit: Mimetype = " << mmr->mimeType() << endl;
163
164 setViewWidget( new TQWidget(this) );
165#endif
166}
167
168bool KexiBlobTableEdit::valueIsNull()
169{
170//TODO
171 d->value.size();
172 return d->value.isEmpty();
173}
174
175bool KexiBlobTableEdit::valueIsEmpty()
176{
177//TODO
178 return d->value.isEmpty();
179}
180
181TQVariant
182KexiBlobTableEdit::value()
183{
184 return d->value;
185#if 0
186 //todo
187// ok = true;
188
189 if(m_content && m_content->isModified())
190 {
191 return TQVariant(m_content->text());
192 }
193 TQByteArray value;
194 TQFile f( m_tempFile->name() );
195 f.open(IO_ReadOnly);
196 TQDataStream stream(&f);
197 char* data = (char*) malloc(f.size());
198 value.resize(f.size());
199 stream.readRawBytes(data, f.size());
200 value.duplicate(data, f.size());
201 free(data);
202 kdDebug() << "KexiBlobTableEdit: Size of BLOB: " << value.size() << endl;
203 return TQVariant(value);
204#endif
205}
206
207void KexiBlobTableEdit::paintFocusBorders( TQPainter *p, TQVariant &, int x, int y, int w, int h )
208{
209// d->currentEditorWidth = w;
210 if (!d->readOnly && w > d->button->width())
211 w -= d->button->width();
212 p->drawRect(x, y, w, h);
213}
214
215void
216KexiBlobTableEdit::setupContents( TQPainter *p, bool focused, const TQVariant& val,
217 TQString &txt, int &align, int &x, int &y_offset, int &w, int &h )
218{
219 Q_UNUSED(focused);
220 Q_UNUSED(txt);
221 Q_UNUSED(align);
222
224 TQPixmap pixmap;
225 x = 0;
226 w -= 1; //a place for border
227 h -= 1; //a place for border
228 if (p && val.canCast(TQVariant::ByteArray) && pixmap.loadFromData(val.toByteArray())) {
229 KexiUtils::drawPixmap( *p, 0/*lineWidth*/, TQRect(x, y_offset, w, h),
230 pixmap, TQt::AlignCenter, true/*scaledContents*/, true/*keepAspectRatio*/);
231 }
232}
233
234bool KexiBlobTableEdit::cursorAtStart()
235{
236 return true;
237}
238
239bool KexiBlobTableEdit::cursorAtEnd()
240{
241 return true;
242}
243
244void KexiBlobTableEdit::handleInsertFromFileAction(const KURL& url)
245{
246 if (isReadOnly())
247 return;
248
249 TQString fileName( url.isLocalFile() ? url.path() : url.prettyURL() );
250
252 TQFile f(fileName);
253 if (!f.open(IO_ReadOnly)) {
255 return;
256 }
257 TQByteArray ba = f.readAll();
258 if (f.status()!=IO_Ok) {
260 f.close();
261 return;
262 }
263 f.close();
264// m_valueMimeType = KImageIO::mimeType( fileName );
265 setValueInternal( ba, true );
266 signalEditRequested();
267 //emit acceptRequested();
268}
269
270void KexiBlobTableEdit::handleAboutToSaveAsAction(TQString& origFilename, TQString& fileExtension, bool& dataIsEmpty)
271{
272 Q_UNUSED(origFilename);
273 Q_UNUSED(fileExtension);
274 dataIsEmpty = valueIsEmpty();
276}
277
278void KexiBlobTableEdit::handleSaveAsAction(const TQString& fileName)
279{
280 TQFile f(fileName);
281 if (!f.open(IO_WriteOnly)) {
283 return;
284 }
285 f.writeBlock( d->value );
286 if (f.status()!=IO_Ok) {
288 f.close();
289 return;
290 }
291 f.close();
292}
293
294void KexiBlobTableEdit::handleCutAction()
295{
296 if (isReadOnly())
297 return;
298 handleCopyAction();
299 clear();
300}
301
302void KexiBlobTableEdit::handleCopyAction()
303{
304 executeCopyAction(d->value);
305}
306
307void KexiBlobTableEdit::executeCopyAction(const TQByteArray& data)
308{
309 TQPixmap pixmap;
310 if (!pixmap.loadFromData(data))
311 return;
312 tqApp->clipboard()->setPixmap(pixmap, TQClipboard::Clipboard);
313}
314
315void KexiBlobTableEdit::handlePasteAction()
316{
317 if (isReadOnly())
318 return;
319 TQPixmap pm( tqApp->clipboard()->pixmap(TQClipboard::Clipboard) );
320 TQByteArray ba;
321 TQBuffer buffer( ba );
322 buffer.open( IO_WriteOnly );
323 if (pm.save( &buffer, "PNG" )) {// write pixmap into ba in PNG format
324 setValueInternal( ba, true );
325 }
326 else {
327 setValueInternal( TQByteArray(), true );
328 }
329 signalEditRequested();
330 //emit acceptRequested();
331 repaintRelatedCell();
332}
333
334void KexiBlobTableEdit::clear()
335{
336 setValueInternal( TQByteArray(), true );
337 signalEditRequested();
338 //emit acceptRequested();
339 repaintRelatedCell();
340}
341
342void KexiBlobTableEdit::handleShowPropertiesAction()
343{
345}
346
347void KexiBlobTableEdit::showFocus( const TQRect& r, bool readOnly )
348{
349 d->readOnly = readOnly; //cache for slotUpdateActionsAvailabilityRequested()
350// d->button->move( pos().x()+ width(), pos().y() );
351 updateFocus( r );
352// d->button->setEnabled(!readOnly);
353 if (d->readOnly)
354 d->button->hide();
355 else
356 d->button->show();
357}
358
359void KexiBlobTableEdit::resize(int w, int h)
360{
361 d->totalSize = TQSize(w,h);
362 const int addWidth = d->readOnly ? 0 : d->button->width();
363 TQWidget::resize(w - addWidth, h);
364 if (!d->readOnly)
365 d->button->resize( h, h );
366 m_rightMarginWhenFocused = m_rightMargin + addWidth;
367 TQRect r( pos().x(), pos().y(), w+1, h+1 );
368 r.moveBy(m_scrollView->contentsX(),m_scrollView->contentsY());
369 updateFocus( r );
370//todo if (d->popup) {
371//todo d->popup->updateSize();
372//todo }
373}
374
375void KexiBlobTableEdit::updateFocus( const TQRect& r )
376{
377 if (!d->readOnly) {
378 if (d->button->width() > r.width())
379 moveChild(d->button, r.right() + 1, r.top());
380 else
381 moveChild(d->button, r.right() - d->button->width(), r.top() );
382 }
383}
384
385void KexiBlobTableEdit::hideFocus()
386{
387 d->button->hide();
388}
389
390TQSize KexiBlobTableEdit::totalSize() const
391{
392 return d->totalSize;
393}
394
395void KexiBlobTableEdit::slotUpdateActionsAvailabilityRequested(bool& valueIsNull, bool& valueIsReadOnly)
396{
397 emit editRequested();
398 valueIsNull = this->valueIsNull();
399 valueIsReadOnly = d->readOnly || isReadOnly();
400}
401
402void KexiBlobTableEdit::signalEditRequested()
403{
404 d->setValueInternalEnabled = false;
405 emit editRequested();
406 d->setValueInternalEnabled = true;
407}
408
409bool KexiBlobTableEdit::handleKeyPress( TQKeyEvent* ke, bool editorActive )
410{
411 Q_UNUSED(editorActive);
412
413 const int k = ke->key();
414 KKey kkey(ke);
415 if (!d->readOnly) {
416 if ((ke->state()==TQt::NoButton && k==TQt::Key_F4)
417 || (ke->state()==TQt::AltButton && k==TQt::Key_Down)) {
418 d->button->animateClick();
419 TQMouseEvent me( TQEvent::MouseButtonPress, TQPoint(2,2), TQt::LeftButton, TQt::NoButton );
420 TQApplication::sendEvent( d->button, &me );
421 }
422 else if ((ke->state()==TQt::NoButton && (k==TQt::Key_F2 || k==TQt::Key_Space || k==TQt::Key_Enter || k==TQt::Key_Return))) {
423 d->popup->insertFromFile();
424 }
425 else
426 return false;
427 }
428 else
429 return false;
430 return true;
431}
432
433bool KexiBlobTableEdit::handleDoubleClick()
434{
435 d->popup->insertFromFile();
436 return true;
437}
438
439void KexiBlobTableEdit::handleCopyAction(const TQVariant& value, const TQVariant& visibleValue)
440{
441 Q_UNUSED(visibleValue);
442 executeCopyAction(value.toByteArray());
443}
444
445void KexiBlobTableEdit::handleAction(const TQString& actionName)
446{
447 if (actionName=="edit_paste") {
448 d->popup->paste();
449 }
450 else if (actionName=="edit_cut") {
451 emit editRequested();
452 d->popup->cut();
453 }
454}
455
456bool KexiBlobTableEdit::eventFilter( TQObject *o, TQEvent *e )
457{
458 if (o == d->popup && e->type()==TQEvent::KeyPress) {
459 TQKeyEvent* ke = static_cast<TQKeyEvent*>(e);
460 const int state = ke->state();
461 const int k = ke->key();
462 if ( (state==TQt::NoButton && (k==TQt::Key_Tab || k==TQt::Key_Left || k==TQt::Key_Right))
463 || (state==TQt::ShiftButton && k==TQt::Key_Backtab)
464 )
465 {
466 d->popup->hide();
467 TQApplication::sendEvent( this, ke ); //re-send to move cursor
468 return true;
469 }
470 }
471 return false;
472}
473
474KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiBlobEditorFactoryItem, KexiBlobTableEdit)
475
476//=======================
477// KexiTDEIconTableEdit class is temporarily here:
478
479
480class KexiTDEIconTableEdit::Private
481{
482public:
483 Private()
484 : pixmapCache(17, 17, false)
485 {
486 }
488 TQVariant currentValue;
489
490 TQCache<TQPixmap> pixmapCache;
491};
492
493KexiTDEIconTableEdit::KexiTDEIconTableEdit(KexiTableViewColumn &column, TQWidget *parent)
494 : KexiTableEdit(column, parent)
495 , d( new Private() )
496{
497 setName("KexiTDEIconTableEdit");
498 init();
499}
500
501KexiTDEIconTableEdit::~KexiTDEIconTableEdit()
502{
503 delete d;
504}
505
506void KexiTDEIconTableEdit::init()
507{
508 m_hasFocusableWidget = false;
509 d->pixmapCache.setAutoDelete(true);
510}
511
512void KexiTDEIconTableEdit::setValueInternal(const TQVariant& /*add*/, bool /*removeOld*/)
513{
514 d->currentValue = m_origValue;
515}
516
518{
519 return d->currentValue.isNull();
520}
521
523{
524 return d->currentValue.isNull();
525}
526
528{
529 return d->currentValue;
530}
531
533{
534 d->currentValue = TQVariant();
535}
536
538{
539 return true;
540}
541
543{
544 return true;
545}
546
547void KexiTDEIconTableEdit::setupContents( TQPainter *p, bool /*focused*/, const TQVariant& val,
548 TQString &/*txt*/, int &/*align*/, int &/*x*/, int &y_offset, int &w, int &h )
549{
550 Q_UNUSED( y_offset );
551
552#if 0
553#ifdef TQ_WS_WIN
554 y_offset = -1;
555#else
556 y_offset = 0;
557#endif
558 int s = TQMAX(h - 5, 12);
559 s = TQMIN( h-3, s );
560 s = TQMIN( w-3, s );//avoid too large box
561 TQRect r( TQMAX( w/2 - s/2, 0 ) , h/2 - s/2 /*- 1*/, s, s);
562 p->setPen(TQPen(colorGroup().text(), 1));
563 p->drawRect(r);
564 if (val.asBool()) {
565 p->drawLine(r.x(), r.y(), r.right(), r.bottom());
566 p->drawLine(r.x(), r.bottom(), r.right(), r.y());
567 }
568#endif
569
570 TQString key = val.toString();
571 TQPixmap *pix = 0;
572 if (!key.isEmpty() && !(pix = d->pixmapCache[ key ])) {
573 //cache pixmap
574 TQPixmap pm = TDEGlobal::iconLoader()->loadIcon( key, TDEIcon::Small,
575 0, TDEIcon::DefaultState, 0L, true/*canReturnNull*/ );
576 if (!pm.isNull()) {
577 pix = new TQPixmap(pm);
578 d->pixmapCache.insert(key, pix);
579 }
580 }
581
582 if (p && pix) {
583 p->drawPixmap( (w-pix->width())/2, (h-pix->height())/2, *pix );
584 }
585}
586
587void KexiTDEIconTableEdit::handleCopyAction(const TQVariant& value, const TQVariant& visibleValue)
588{
589 Q_UNUSED(value);
590 Q_UNUSED(visibleValue);
591}
592
593KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiTDEIconTableEditorFactoryItem, KexiTDEIconTableEdit)
594
595#include "kexiblobtableedit.moc"
TQString captionOrAliasOrName() const
Definition queryschema.h:65
virtual TQVariant visibleValue()
A button for drop-down "Image" menu.
A context menu used for images within form and table views.
static bool updateTitle(TQPopupMenu *menu, const TQString &title, const TQString &iconName=TQString())
Cell editor for displaying kde icon (using icon name provided as string). Read only.
virtual void setValueInternal(const TQVariant &add, bool removeOld)
initializes this editor with add value
virtual void handleCopyAction(const TQVariant &value, const TQVariant &visibleValue)
virtual void clear()
clears item's data, so the data will contain NULL data
virtual TQVariant value()
virtual void setupContents(TQPainter *p, bool focused, const TQVariant &val, TQString &txt, int &align, int &x, int &y_offset, int &w, int &h)
Abstract class for a cell editor. Handles cell painting and displaying the editor widget.
virtual TQSize totalSize()
KexiDB::QueryColumnInfo * columnInfo
KEXIUTILS_EXPORT void drawPixmap(TQPainter &p, int lineWidth, const TQRect &rect, const TQPixmap &pixmap, int alignment, bool scaledContents, bool keepAspectRatio)
Draws pixmap on painter p using predefined parameters.