lib

pixmapedit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
4 Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#include "pixmapedit.h"
23#include "editoritem.h"
24#include "property.h"
25
26#include <tqlayout.h>
27#include <tqpainter.h>
28#include <tqlabel.h>
29#include <tqcursor.h>
30#include <tqpushbutton.h>
31#include <tqfont.h>
32#include <tqfontmetrics.h>
33#include <tqimage.h>
34#include <tqfiledialog.h>
35#include <tqtooltip.h>
36#include <tqapplication.h>
37
38#include <kdebug.h>
39#include <kimageio.h>
40
41#ifdef TQ_WS_WIN
42#include <win32_utils.h>
43#include <tderecentdirs.h>
44#endif
45
46#ifndef PURE_QT
47#include <tdefiledialog.h>
48#include <tdelocale.h>
49#include <tdefiledialog.h>
50#endif
51
52using namespace KoProperty;
53
54PixmapEdit::PixmapEdit(Property *property, TQWidget *parent, const char *name)
55 : Widget(property, parent, name)
56{
57 setHasBorders(false);
58
59 m_edit = new TQLabel(this, "m_edit");
60 TQToolTip::add(m_edit, i18n("Click to show image preview"));
61 m_edit->setAlignment(TQt::AlignLeft | TQt::AlignVCenter);
62 m_edit->setMinimumHeight(5);
63 m_edit->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Fixed);
64 m_edit->setBackgroundMode(TQt::PaletteBase);
65 m_edit->setMouseTracking(true);
66 setBackgroundMode(TQt::PaletteBase);
67
68 m_button = new TQPushButton(i18n("..."), this, "m_button");
69 TQToolTip::add(m_button, i18n("Insert image from file"));
70 m_button->setSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Fixed);
71 TQFontMetrics fm(m_button->font());
72 m_button->setFixedWidth(fm.width(m_button->text()+' '));
73 m_button->setFocusPolicy(TQWidget::NoFocus);
74
75 m_popup = new TQLabel(0, "m_popup", TQt::WStyle_Customize|TQt::WStyle_NoBorder|TQt::WX11BypassWM|WStyle_StaysOnTop);
76 m_popup->setPaletteBackgroundColor(m_popup->palette().active().base());
77 m_popup->setFrameStyle(TQFrame::Plain|TQFrame::Box);
78 m_popup->setMargin(2);
79 m_popup->setLineWidth(1);
80 m_popup->hide();
81
82 setFocusWidget(m_edit);
83 connect(m_button, TQ_SIGNAL(clicked()), this, TQ_SLOT(selectPixmap()));
84}
85
86PixmapEdit::~PixmapEdit()
87{
88 delete m_popup;
89}
90
91TQVariant
93{
94 return m_pixmap;
95}
96
97void
98PixmapEdit::setValue(const TQVariant &value, bool emitChange)
99{
100 m_pixmap = value.toPixmap();
101 if (m_pixmap.isNull() || (m_pixmap.height()<=height())) {
102 m_edit->setPixmap(m_pixmap);
103 m_previewPixmap = m_pixmap;
104 }
105 else {
106 TQImage img(m_pixmap.convertToImage());
107 if (!TQRect(TQPoint(0,0), m_edit->size()*3).contains(m_pixmap.rect())) {
108 img = img.smoothScale(m_edit->size()*3, TQImage::ScaleMin);
109 m_previewPixmap.convertFromImage(img);//preview pixmap is a bit larger
110 }
111 else {
112 m_previewPixmap = m_pixmap;
113 }
114 img = img.smoothScale(m_edit->size(), TQImage::ScaleMin);
115 TQPixmap pm;
116 pm.convertFromImage(img);
117 m_edit->setPixmap(pm);
118 }
119 if (emitChange)
120 emit valueChanged(this);
121}
122
123void
124PixmapEdit::drawViewer(TQPainter *p, const TQColorGroup &, const TQRect &r, const TQVariant &value)
125{
126 TQRect r2(r);
127 r2.setHeight(r2.height()+1);
128 p->setClipRect(r2, TQPainter::CoordPainter);
129 p->setClipping(true);
130 p->eraseRect(r2);
131 if (value.toPixmap().isNull())
132 return;
133 if (m_recentlyPainted!=value) {
134 m_recentlyPainted = value;
135 m_scaledPixmap = value.toPixmap();
136 if (m_scaledPixmap.height() > r2.height() || m_scaledPixmap.width() > r2.width()) { //scale down
137 TQImage img(m_scaledPixmap.convertToImage());
138 img = img.smoothScale(r2.size()/*+TQSize(0,2)*/, TQImage::ScaleMin);
139 m_scaledPixmap.convertFromImage(img);
140 }
141 }
142 p->drawPixmap(r2.topLeft().x(), //+KPROPEDITOR_ITEM_MARGIN,
143 r2.topLeft().y()+(r2.height()-m_scaledPixmap.height())/2, m_scaledPixmap);
144}
145
146TQString
147PixmapEdit::selectPixmapFileName()
148{
149/*#ifdef PURE_QT
150 TQString url = TQFileDialog::getOpenFileName();
151 if (!url.isEmpty()) {
152 m_edit->setPixmap(TQPixmap(url));
153 emit valueChanged(this);
154 }
155#endif*/
156 TQString caption( i18n("Insert Image From File (for \"%1\" property)").arg(property()->caption()) );
157#ifdef TQ_WS_WIN
158 TQString recentDir;
159 TQString fileName = TQFileDialog::getOpenFileName(
160 KFileDialog::getStartURL(":lastVisitedImagePath", recentDir).path(),
161 convertKFileDialogFilterToTQFileDialogFilter(KImageIO::pattern(KImageIO::Reading)),
162 this, 0, caption);
163#else
164 KURL url( KFileDialog::getImageOpenURL(
165 ":lastVisitedImagePath", this, caption) );
166 TQString fileName = url.isLocalFile() ? url.path() : url.prettyURL();
167
169#endif
170 return fileName;
171}
172
173void
174PixmapEdit::selectPixmap()
175{
176 TQString fileName( selectPixmapFileName() );
177 if (fileName.isEmpty())
178 return;
179
180 TQPixmap pm;
181 if (!pm.load(fileName)) {
183 return;
184 }
185 setValue(pm);
186
187#ifdef TQ_WS_WIN
188 //save last visited path
189 KURL url(fileName);
190 if (url.isLocalFile())
191 TDERecentDirs::add(":lastVisitedImagePath", url.directory());
192#endif
193}
194
195void
196PixmapEdit::resizeEvent(TQResizeEvent *e)
197{
198 Widget::resizeEvent(e);
199 m_edit->move(0,0);
200 m_edit->resize(e->size()-TQSize(m_button->width(),-1));
201 m_button->move(m_edit->width(),0);
202 m_button->setFixedSize(m_button->width(), height());
203}
204
205bool
206PixmapEdit::eventFilter(TQObject *o, TQEvent *ev)
207{
208 if(o == m_edit) {
209 if(ev->type() == TQEvent::MouseButtonPress && static_cast<TQMouseEvent*>(ev)->button()==TQt::LeftButton) {
210 if(m_previewPixmap.height() <= m_edit->height()
211 && m_previewPixmap.width() <= m_edit->width())
212 return false;
213
214 m_popup->setPixmap(m_previewPixmap.isNull() ? m_pixmap : m_previewPixmap);
215 m_popup->resize(m_previewPixmap.size()+TQSize(2*3,2*3));
216 TQPoint pos = TQCursor::pos()+TQPoint(3,15);
217 TQRect screenRect = TQApplication::desktop()->availableGeometry( this );
218 if ((pos.x()+m_popup->width()) > screenRect.width())
219 pos.setX(screenRect.width()-m_popup->width());
220 if ((pos.y()+m_popup->height()) > screenRect.height())
221 pos.setY(mapToGlobal(TQPoint(0,0)).y()-m_popup->height());
222 m_popup->move(pos);
223 m_popup->show();
224 }
225 else if(ev->type() == TQEvent::MouseButtonRelease || ev->type() == TQEvent::Hide) {
226 if(m_popup->isVisible())
227 m_popup->hide();
228 }
229 else if(ev->type() == TQEvent::KeyPress) {
230 TQKeyEvent* e = static_cast<TQKeyEvent*>(ev);
231 if((e->key() == Key_Enter) || (e->key()== Key_Space) || (e->key() == Key_Return)) {
232 m_button->animateClick();
233 return true;
234 }
235 }
236 }
237
238 return Widget::eventFilter(o, ev);
239}
240
241void
242PixmapEdit::setReadOnlyInternal(bool readOnly)
243{
244 m_button->setEnabled(!readOnly);
245}
246
247#include "pixmapedit.moc"
The base class representing a single property.
Definition property.h:177
The base class for all item editors used in Editor.
Definition widget.h:37
virtual Property * property() const
Definition widget.cpp:72
virtual bool eventFilter(TQObject *watched, TQEvent *e)
Definition widget.cpp:108
virtual TQVariant value() const =0
const char * name(StdAction id)
Namespace for a set of classes implementing generic properties framework.