22#include "pixmapedit.h"
23#include "editoritem.h"
30#include <tqpushbutton.h>
32#include <tqfontmetrics.h>
34#include <tqfiledialog.h>
36#include <tqapplication.h>
42#include <win32_utils.h>
43#include <tderecentdirs.h>
47#include <tdefiledialog.h>
49#include <tdefiledialog.h>
54PixmapEdit::PixmapEdit(
Property *property, TQWidget *parent,
const char *name)
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);
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);
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);
82 setFocusWidget(m_edit);
83 connect(m_button, TQ_SIGNAL(clicked()),
this, TQ_SLOT(selectPixmap()));
86PixmapEdit::~PixmapEdit()
98PixmapEdit::setValue(
const TQVariant &value,
bool emitChange)
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;
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);
112 m_previewPixmap = m_pixmap;
114 img = img.smoothScale(m_edit->size(), TQImage::ScaleMin);
116 pm.convertFromImage(img);
117 m_edit->setPixmap(pm);
120 emit valueChanged(
this);
124PixmapEdit::drawViewer(TQPainter *p,
const TQColorGroup &,
const TQRect &r,
const TQVariant &value)
127 r2.setHeight(r2.height()+1);
128 p->setClipRect(r2, TQPainter::CoordPainter);
129 p->setClipping(
true);
131 if (value.toPixmap().isNull())
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()) {
137 TQImage img(m_scaledPixmap.convertToImage());
138 img = img.smoothScale(r2.size(), TQImage::ScaleMin);
139 m_scaledPixmap.convertFromImage(img);
142 p->drawPixmap(r2.topLeft().x(),
143 r2.topLeft().y()+(r2.height()-m_scaledPixmap.height())/2, m_scaledPixmap);
147PixmapEdit::selectPixmapFileName()
156 TQString caption( i18n(
"Insert Image From File (for \"%1\" property)").arg(
property()->caption()) );
159 TQString fileName = TQFileDialog::getOpenFileName(
160 KFileDialog::getStartURL(
":lastVisitedImagePath", recentDir).path(),
161 convertKFileDialogFilterToTQFileDialogFilter(KImageIO::pattern(KImageIO::Reading)),
164 KURL url( KFileDialog::getImageOpenURL(
165 ":lastVisitedImagePath",
this, caption) );
166 TQString fileName = url.isLocalFile() ? url.path() : url.prettyURL();
174PixmapEdit::selectPixmap()
176 TQString fileName( selectPixmapFileName() );
177 if (fileName.isEmpty())
181 if (!pm.load(fileName)) {
190 if (url.isLocalFile())
191 TDERecentDirs::add(
":lastVisitedImagePath", url.directory());
196PixmapEdit::resizeEvent(TQResizeEvent *e)
198 Widget::resizeEvent(e);
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());
206PixmapEdit::eventFilter(TQObject *o, TQEvent *ev)
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())
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());
225 else if(ev->type() == TQEvent::MouseButtonRelease || ev->type() == TQEvent::Hide) {
226 if(m_popup->isVisible())
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();
242PixmapEdit::setReadOnlyInternal(
bool readOnly)
244 m_button->setEnabled(!readOnly);
247#include "pixmapedit.moc"
The base class representing a single property.
const char * name(StdAction id)
Namespace for a set of classes implementing generic properties framework.