KDevelop API Documentation

pcombobox.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2002-2004 by Alexander Dymo                             *
00003  *   cloudtemple@mskat.net                                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 2 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Library General Public     *
00016  *   License along with this program; if not, write to the                 *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "pcombobox.h"
00021 
00022 #include <qcombobox.h>
00023 #include <qlayout.h>
00024 
00025 namespace PropertyLib{
00026 
00027 PComboBox::PComboBox(MultiProperty *property, const QMap<QString, QVariant> &list, QWidget *parent, const char *name)
00028     :PropertyWidget(property, parent, name), m_valueList(list)
00029 {
00030     init(false);
00031 }
00032 
00033 PComboBox::PComboBox(MultiProperty *property, const QMap<QString, QVariant> &list, bool rw, QWidget *parent, const char *name)
00034     :PropertyWidget(property, parent, name), m_valueList(list)
00035 {
00036     init(rw);
00037 }
00038 
00039 void PComboBox::init(bool rw)
00040 {
00041     QHBoxLayout *l = new QHBoxLayout(this, 0, 0);
00042     m_edit = new QComboBox(rw, this);
00043     m_edit->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
00044     l->addWidget(m_edit);
00045     
00046     fillBox();
00047     
00048     connect(m_edit, SIGNAL(activated(int)), this, SLOT(updateProperty(int)));
00049 }
00050 
00051 void PComboBox::fillBox()
00052 {
00053     for (QMap<QString, QVariant>::const_iterator it = m_valueList.begin(); it != m_valueList.end(); it++)
00054     {
00055         m_edit->insertItem(it.key());
00056     }
00057 }
00058 
00059 QVariant PComboBox::value() const
00060 {
00061     QMap<QString, QVariant>::const_iterator it = m_valueList.find(m_edit->currentText());
00062     if (it == m_valueList.end())
00063         return QVariant("");
00064     return QVariant(it.data());
00065 }
00066 
00067 void PComboBox::setValue(const QVariant &value, bool emitChange)
00068 {
00069 #if QT_VERSION >= 0x030100
00070     if (!value.isNull())
00071 #else
00072     if (value.canCast(QVariant::String))
00073 #endif
00074     {
00075         disconnect(m_edit, SIGNAL(activated(int)), this, SLOT(updateProperty(int)));
00076         m_edit->setCurrentText(findDescription(value));
00077         connect(m_edit, SIGNAL(activated(int)), this, SLOT(updateProperty(int)));
00078         if (emitChange)
00079             emit propertyChanged(m_property, value);
00080     }
00081 }
00082 
00083 void PComboBox::updateProperty(int /*val*/)
00084 {
00085     emit propertyChanged(m_property, value());
00086 }
00087 
00088 QString PComboBox::findDescription(const QVariant &value)
00089 {
00090     for (QMap<QString, QVariant>::const_iterator it = m_valueList.begin(); it != m_valueList.end(); ++ it)
00091     {
00092         if (it.data() == value)
00093             return it.key();
00094     }
00095     return "";
00096 }
00097 
00098 void PComboBox::setValueList(const QMap<QString, QVariant> &valueList)
00099 {
00100     m_valueList = valueList;
00101     m_edit->clear();
00102     fillBox();
00103 }
00104 
00105 }
00106 
00107 #ifndef PURE_QT
00108 #include "pcombobox.moc"
00109 #endif
00110 
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