KDevelop API Documentation

ppixmapedit.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2003 Cedric Pasteur                                     *
00003  *   <cedric.pasteur@free.fr>                                              *
00004  *   Copyright (C) 2004 by Alexander Dymo                                  *
00005  *   cloudtemple@mskat.net                                                 *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU Library General Public License as       *
00009  *   published by the Free Software Foundation; either version 2 of the    *
00010  *   License, or (at your option) any later version.                       *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU Library General Public     *
00018  *   License along with this program; if not, write to the                 *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 #include "ppixmapedit.h"
00023 
00024 #include <qlayout.h>
00025 #include <qpainter.h>
00026 #include <qlabel.h>
00027 #include <qcursor.h>
00028 
00029 #include <klocale.h>
00030 #include <kfiledialog.h>
00031 #include <kpushbutton.h>
00032 
00033 namespace PropertyLib{
00034 
00035 PPixmapEdit::PPixmapEdit(MultiProperty* property, QWidget* parent, const char* name)
00036     :PropertyWidget(property, parent, name)
00037 {
00038     QHBoxLayout *l = new QHBoxLayout(this, 0, 0);
00039     m_edit = new QLabel(this);
00040     m_edit->setAlignment(Qt::AlignTop);
00041     m_edit->resize(width(), height()-1);
00042     m_edit->setBackgroundMode(Qt::PaletteBase);
00043     m_edit->installEventFilter(this);
00044     
00045     m_button = new KPushButton(i18n("..."), this);
00046     m_button->resize(height(), height()-8);
00047     m_button->move(width() - m_button->width() -1, 0);
00048     m_button->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
00049     l->addWidget(m_edit);
00050     l->addWidget(m_button);
00051     m_popup = new QLabel(0, 0, Qt::WStyle_NoBorder|Qt::WX11BypassWM|WStyle_StaysOnTop);
00052     m_popup->hide();
00053     
00054 
00055     connect(m_button, SIGNAL(clicked()), this, SLOT(updateProperty()));
00056 }
00057 
00058 QVariant PPixmapEdit::value() const
00059 {
00060     return QVariant(*(m_edit->pixmap()));
00061 }
00062 
00063 void PPixmapEdit::drawViewer(QPainter* p, const QColorGroup& cg, const QRect& r, const QVariant& value)
00064 {
00065     p->setPen(Qt::NoPen);
00066     p->setBrush(cg.background());
00067     p->drawRect(r);
00068     p->drawPixmap(r.topLeft().x(), r.topLeft().y(), value.toPixmap());    
00069 }
00070 
00071 void PPixmapEdit::setValue(const QVariant& value, bool emitChange)
00072 {
00073     m_edit->setPixmap(value.toPixmap());
00074     if (emitChange)
00075         emit propertyChanged(m_property, value);
00076 }
00077 
00078 void PPixmapEdit::updateProperty()
00079 {
00080     KURL url = KFileDialog::getImageOpenURL(QString::null, this);
00081     if (!url.isEmpty())
00082     {
00083         m_edit->setPixmap(QPixmap(url.path()));
00084         emit propertyChanged(m_property, value());
00085     }
00086 }
00087 
00088 void PPixmapEdit::resizeEvent(QResizeEvent *ev)
00089 {
00090     m_edit->resize(ev->size().width(), ev->size().height()-1);
00091     m_button->move(ev->size().width() - m_button->width(), 0);
00092     m_edit->setMaximumHeight(m_button->height());
00093 }
00094 
00095 bool PPixmapEdit::eventFilter(QObject *o, QEvent *ev)
00096 {
00097     if(o == m_edit)
00098     {
00099         if(ev->type() == QEvent::MouseButtonPress)
00100         {
00101             if(m_edit->pixmap()->size().height() < height()-2
00102                     && m_edit->pixmap()->size().width() < width()-20)
00103                     return false;
00104             m_popup->setPixmap(*(m_edit->pixmap()));
00105             m_popup->resize(m_edit->pixmap()->size());
00106             m_popup->move(QCursor::pos());
00107             m_popup->show();
00108         }
00109         if(ev->type() == QEvent::MouseButtonRelease)
00110         {
00111             if(m_popup->isVisible())
00112                     m_popup->hide();
00113         }
00114         if(ev->type() == QEvent::KeyPress)
00115         {
00116             QKeyEvent* e = static_cast<QKeyEvent*>(ev);
00117             if((e->key() == Key_Enter) || (e->key()== Key_Space) || (e->key() == Key_Return))
00118             {
00119                     m_button->animateClick();
00120                     return true;
00121             }
00122         }
00123     }
00124     return PropertyWidget::eventFilter(o, ev);
00125 }
00126 
00127 }
00128 
00129 #include "ppixmapedit.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 00:03:53 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003