propertieslistview.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2003-2004 by David Saxton                               *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "item.h"
00012 #include "cnitemgroup.h"
00013 #include "itemgroup.h"
00014 #include "iteminterface.h"
00015 #include "pieditor.h"
00016 #include "plvitem.h"
00017 #include "propertieslistview.h"
00018 #include "variant.h"
00019 
00020 
00021 #include <kdebug.h>
00022 #include <klocale.h>
00023 #include <qevent.h>
00024 #include <qheader.h>
00025 #include <qpushbutton.h>
00026 #include <qvariant.h>
00027 
00028 PropertiesListView::PropertiesListView(QWidget *parent, const char *name)
00029  : KListView(parent, name)
00030 {
00031         addColumn(i18n("Property"));
00032         addColumn(i18n("Data"));
00033         setFullWidth(true);
00034         setColumnAlignment(1,Qt::AlignRight);
00035         
00036         p_lastItem = 0;
00037         m_diffBt = 0;
00038         
00039         connect(this,SIGNAL(selectionChanged(QListViewItem*)),this,SLOT(slotSelectionChanged(QListViewItem*)));
00040         m_editor = 0;
00041         connect(header(),SIGNAL(sizeChange(int,int,int)),this,SLOT(headerSizeChanged(int,int,int)));
00042 }
00043 
00044 PropertiesListView::~PropertiesListView()
00045 {
00046 }
00047 
00048 void PropertiesListView::slotClear()
00049 {
00050         destroyEditor();
00051         delete m_diffBt;
00052         m_diffBt = 0;
00053         clear();
00054         m_plvItemMap.clear();
00055 }
00056 
00057 void PropertiesListView::slotCreate( ItemGroup * itemGroup )
00058 {
00059         if ( !itemGroup || !itemGroup->activeItem() )
00060         {
00061                 slotClear();
00062                 return;
00063         }
00064         
00065         Item *item = itemGroup->activeItem();
00066         
00067         VariantDataMap *vmap = item->variantMap();
00068         // Build the list
00069         for( VariantDataMap::iterator vait = vmap->begin(); vait != vmap->end(); ++vait )
00070         {
00071                 if ( vait.data()->isHidden() )
00072                         continue;
00073                 
00074                 switch( vait.data()->type() )
00075                 {
00076                         case Variant::Type::Int:
00077                         case Variant::Type::Double:
00078                         case Variant::Type::String:
00079                         case Variant::Type::FileName:
00080                         case Variant::Type::Port:
00081                         case Variant::Type::Pin:
00082                         case Variant::Type::VarName:
00083                         case Variant::Type::Combo:
00084                         case Variant::Type::Select:
00085                         case Variant::Type::Bool:
00086                         case Variant::Type::PenStyle:
00087                         case Variant::Type::PenCapStyle:
00088                         case Variant::Type::SevenSegment:
00089                         case Variant::Type::KeyPad:
00090                         {
00091                                 m_plvItemMap[vait.key()] = new PLVItem( this, vait.key(), vait.data() );
00092                                 break;
00093                         }
00094                         case Variant::Type::Color:
00095                         {
00096                                 m_plvItemMap[vait.key()] = new PLVColorItem( this, vait.key(), vait.data() );
00097                                 break;
00098                         }
00099                         case Variant::Type::Raw:
00100                         case Variant::Type::Multiline:
00101                         case Variant::Type::None:
00102                         {
00103                                 break;
00104                         }
00105                 }
00106         }
00107         
00108         slotUpdate(itemGroup);
00109 }
00110 
00111 
00112 void PropertiesListView::slotUpdate( ItemGroup * itemGroup )
00113 {
00114         if ( !itemGroup )
00115         {
00116                 slotClear();
00117                 return;
00118         }
00119         
00120         const PLVItemMap::iterator end = m_plvItemMap.end();
00121         for ( PLVItemMap::iterator it = m_plvItemMap.begin(); it != end; ++it )
00122         {
00123                 it.data()->setEnabled( itemGroup->itemsHaveSameDataValue( it.key() ) );
00124         }
00125 }
00126 
00127 void PropertiesListView::slotSelectionChanged(QListViewItem *item)
00128 {
00129         if (!item) return;
00130         destroyEditor();
00131         p_lastItem = dynamic_cast<PLVItem*>(item);
00132         if ( !p_lastItem->data() ) return;
00133         
00134         const Variant::Type::Value type = p_lastItem->data()->type();
00135         switch(type)
00136         {
00137                 case Variant::Type::String:
00138                 {
00139                         m_editor = new PILineEdit(p_lastItem->id(),p_lastItem->data(),this);
00140                         break;
00141                 }
00142                 case Variant::Type::Port:
00143                 case Variant::Type::Pin:
00144                 case Variant::Type::Combo:
00145                 case Variant::Type::VarName:
00146                 case Variant::Type::Select:
00147                 case Variant::Type::PenStyle:
00148                 case Variant::Type::PenCapStyle:
00149                 case Variant::Type::SevenSegment:
00150                 case Variant::Type::KeyPad:
00151                 {
00152                         m_editor = new PIStringCombo(p_lastItem->id(),p_lastItem->data(),this);
00153                         break;
00154                 }
00155                 case Variant::Type::FileName:
00156                 {
00157                         m_editor = new PIFilename(p_lastItem->id(),p_lastItem->data(),this);
00158                         break;
00159                 }
00160                 case Variant::Type::Int:
00161                 {
00162                         m_editor = new PIInt(p_lastItem->id(),p_lastItem->data(),this);
00163                         break;
00164                 }
00165                 case Variant::Type::Double:
00166                 {
00167                         m_editor = new PIDouble(p_lastItem->id(),p_lastItem->data(),this);
00168                         break;
00169                 }
00170                 case Variant::Type::Color:
00171                 {
00172                         m_editor = new PIColor(p_lastItem->id(),p_lastItem->data(),this);
00173                         break;
00174                 }
00175                 case Variant::Type::Bool:
00176                 {
00177                         m_editor = new PIBool(p_lastItem->id(),p_lastItem->data(),this);
00178                         break;
00179                 }
00180                 case Variant::Type::Raw:
00181                 case Variant::Type::Multiline:
00182                 case Variant::Type::None:
00183                 {
00184                         break;
00185                 }
00186         }
00187         
00188         connect(p_lastItem->data(),SIGNAL(destroyed()),this,SLOT(destroyEditor())); 
00189         // Connect so that changes in the editor change the canvas item data.
00190         connect(m_editor,SIGNAL(editorDataChanged(const QString&,QVariant)),ItemInterface::self(),SLOT(slotSetData(const QString&,QVariant)));
00191         connect(m_editor,SIGNAL(editorDataChanged(const QString&,QVariant)),this,SLOT(slotDataChanged(const QString&,QVariant)));
00192         
00193         int x = columnWidth(0);
00194         int y = viewportToContents(QPoint(0,itemRect(p_lastItem).y())).y();
00195         addChild(m_editor,x,y);
00196         m_editor->setFocus();
00197         m_editor->show();
00198         m_editor->setGeometry(QRect(x,y,columnWidth(1),itemRect(p_lastItem).height()));
00199         
00200         if(p_lastItem->data()->type() == Variant::Type::FileName)
00201         {
00202                 // The folder button in the KURLComboBox has a minimum size taller than
00203                 // the height of the ListViewItems so this is a temporary kludge to
00204                 // make it look slightly acceptable.
00205                 m_editor->setGeometry(QRect(x,y,columnWidth(1),itemRect(p_lastItem).height()+7));
00206         }
00207         
00208         // Active the editor as appropriate
00209         switch(type)
00210         {
00211                 case Variant::Type::Port:
00212                 case Variant::Type::Pin:
00213                 case Variant::Type::Combo:
00214                 case Variant::Type::VarName:
00215                 case Variant::Type::Select:
00216                 case Variant::Type::PenStyle:
00217                 case Variant::Type::PenCapStyle:
00218                 case Variant::Type::SevenSegment:
00219                 case Variant::Type::KeyPad:
00220                 {
00221                         (static_cast<PIStringCombo*>(m_editor))->popup();
00222                         break;
00223                 }
00224                 case Variant::Type::Color:
00225                 {
00226                         (static_cast<PIColor*>(m_editor))->popup();
00227                         break;
00228                 }
00229                 case Variant::Type::Bool:
00230                 {
00231                         (static_cast<PIBool*>(m_editor))->popup();
00232                         break;
00233                 }
00234                 case Variant::Type::FileName:
00235                 {
00236                         break;
00237                 }
00238                 case Variant::Type::Int:
00239                 {
00240                         break;
00241                 }
00242                 case Variant::Type::Double:
00243                 {
00244                         break;
00245                 }
00246                 case Variant::Type::String:
00247                 {
00248                         break;
00249                 }
00250                 case Variant::Type::Raw:
00251                 case Variant::Type::Multiline:
00252                 case Variant::Type::None:
00253                 {
00254                         break;
00255                 }
00256         }
00257 }
00258 
00259 void PropertiesListView::destroyEditor()
00260 {
00261         if( !m_editor ) return;
00262         
00263         removeChild( m_editor );
00264         delete m_editor;
00265         m_editor = 0;
00266 }
00267 
00268 void PropertiesListView::headerSizeChanged(int section, int /*oldSize*/, int newSize)
00269 {
00270         if( !m_editor || section != 1 ) return;
00271         
00272         // Resize the editor to the new column width
00273         // and move it to the right place.
00274         QRect rect = m_editor->geometry();
00275         rect.setWidth(newSize);
00276         rect.setX( columnWidth(0) );
00277         m_editor->setGeometry(rect);
00278 }
00279 
00280 void PropertiesListView::slotDataChanged(const QString &/*id*/, QVariant data)
00281 {
00282         PLVItem *pItem = static_cast<PLVItem*>(currentItem());
00283         pItem->updateData(data);
00284 }
00285 
00286 void PropertiesListView::slotMergeProperties()
00287 {
00288         for( QListViewItemIterator it( this ); it.current(); ++it )
00289         {
00290                 PLVItem * pItem = static_cast<PLVItem*>(it.current());
00291                 if (pItem->isEnabled())
00292                         continue;
00293                 
00294                 pItem->setEnabled(true);
00295                 // manually call the updates on the canvas
00296                 // and in the list
00297                 pItem->updateData(pItem->data()->defaultValue());
00298                 ItemInterface::self()->slotSetData(pItem->id(),pItem->data()->defaultValue());
00299         }
00300 }
00301 
00302 
00303 void PropertiesListView::slotSetDefaults()
00304 {
00305         for( QListViewItemIterator it( this ); it.current(); ++it )
00306         {
00307                 PLVItem *pItem = static_cast<PLVItem*>(it.current());
00308                 ItemInterface::self()->slotSetData(pItem->id(),pItem->data()->defaultValue());
00309         }
00310 }
00311 
00312 
00313 void PropertiesListView::wheelEvent( QWheelEvent *e )
00314 {
00315         QPoint _pos = contentsToViewport(e->pos());
00316         _pos -= pos();
00317         _pos.setY( _pos.y()+header()->height() );
00318         QListViewItem *item = itemAt(_pos);
00319         if ( item && item != dynamic_cast<QListViewItem*>(p_lastItem) )
00320         {
00321                 e->accept();
00322                 if(!item->isSelected()) slotSelectionChanged(item);
00323         }
00324         else KListView::wheelEvent(e);
00325 }
00326 
00327 #include "propertieslistview.moc"
00328 

Generated on Tue May 8 17:05:32 2007 for KTechLab by  doxygen 1.5.1