symbolviewer.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005 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 "config.h"
00012 #ifndef NO_GPSIM
00013 
00014 #include "gpsimprocessor.h"
00015 #include "symbolviewer.h"
00016 
00017 #include <kcombobox.h>
00018 #include <kconfig.h>
00019 #include <kdebug.h>
00020 #include <klocale.h>
00021 #include <qlabel.h>
00022 #include <qlayout.h>
00023 
00024 #include <cassert>
00025 
00026 static const int NAME_COLUMN = 0;
00027 static const int VALUE_COLUMN = 1;
00028 
00029 
00030 //BEGIN class SymbolViewerItem
00031 SymbolViewerItem::SymbolViewerItem( SymbolViewer * symbolViewer, RegisterInfo * registerInfo )
00032         : KListViewItem( symbolViewer->symbolList() )
00033 {
00034         assert(registerInfo);
00035         m_pRegisterInfo = registerInfo;
00036         m_pSymbolViewer = symbolViewer;
00037         
00038         setText( NAME_COLUMN, m_pRegisterInfo->name() );
00039 //      setText( TYPE_COLUMN, RegisterInfo::toString( m_pRegisterInfo->type() ) );
00040         radixChanged(); // force update of displayed string
00041         
00042         connect( m_pRegisterInfo, SIGNAL(valueChanged(unsigned)), this, SLOT(valueChanged(unsigned)) );
00043         connect( m_pSymbolViewer, SIGNAL(valueRadixChanged(SymbolViewer::Radix)), this, SLOT(radixChanged()) );
00044         
00045 }
00046 
00047 
00048 void SymbolViewerItem::valueChanged( unsigned newValue )
00049 {
00050         setText( VALUE_COLUMN, m_pSymbolViewer->toDisplayString( newValue ) );
00051 }
00052 
00053 
00054 void SymbolViewerItem::radixChanged()
00055 {
00056         valueChanged( m_pRegisterInfo->value() );
00057 }
00058 //END class SymbolViewerItem
00059 
00060 
00061 
00062 //BEGIN class SymbolView
00063 SymbolViewer * SymbolViewer::m_pSelf = 0;
00064 SymbolViewer * SymbolViewer::self( KateMDI::ToolView * parent )
00065 {
00066         if (!m_pSelf)
00067         {
00068                 assert (parent);
00069                 m_pSelf = new SymbolViewer(parent);
00070         }
00071         return m_pSelf;
00072 }
00073 
00074 SymbolViewer::SymbolViewer( KateMDI::ToolView * parent )
00075         : QWidget( (QWidget*)parent )
00076 {
00077         QGridLayout  * grid = new QGridLayout( this, 1, 1, 0, 6 );
00078         
00079         m_pSymbolList = new KListView(this);
00080         grid->addMultiCellWidget( m_pSymbolList, 0, 0, 0, 1 );
00081         
00082         grid->addWidget( new QLabel( i18n("Value radix:"), this ), 1, 0 );
00083         
00084         m_pRadixCombo = new KComboBox( false, this );
00085         grid->addWidget( m_pRadixCombo, 1, 1 );
00086         m_pRadixCombo->insertItem( i18n("Binary") );
00087         m_pRadixCombo->insertItem( i18n("Octal") );
00088         m_pRadixCombo->insertItem( i18n("Decimal") );
00089         m_pRadixCombo->insertItem( i18n("Hexadecimal") );
00090         m_valueRadix = Decimal;
00091         m_pRadixCombo->setCurrentItem(2);
00092         connect( m_pRadixCombo, SIGNAL(activated(int)), this, SLOT(selectRadix(int)) );
00093         
00094         m_pGpsim = 0;
00095         m_pCurrentContext = 0;
00096         
00097         m_pSymbolList->addColumn( i18n("Name") );
00098         m_pSymbolList->addColumn( i18n("Value") );
00099         m_pSymbolList->setFullWidth(true);
00100         m_pSymbolList->setAllColumnsShowFocus( true );
00101 }
00102 
00103 
00104 SymbolViewer::~SymbolViewer()
00105 {
00106 }
00107 
00108 
00109 void SymbolViewer::saveProperties( KConfig * config )
00110 {
00111         QString oldGroup = config->group();
00112         
00113         config->setGroup( "SymbolEditor" );
00114         config->writeEntry( "Radix", m_valueRadix );
00115         
00116         config->setGroup( oldGroup );
00117 }
00118 
00119 
00120 void SymbolViewer::readProperties( KConfig * config )
00121 {
00122         QString oldGroup = config->group();
00123         
00124         config->setGroup( "SymbolEditor" );
00125         m_valueRadix = (SymbolViewer::Radix)config->readNumEntry( "Radix", Decimal );
00126         
00127         int pos = 4;
00128         switch ( m_valueRadix )
00129         {
00130                 case Binary:
00131                         pos--;
00132                 case Octal:
00133                         pos--;
00134                 case Decimal:
00135                         pos--;
00136                 case Hexadecimal:
00137                         pos--;
00138         }
00139         m_pRadixCombo->setCurrentItem( pos );
00140         
00141         config->setGroup( oldGroup );
00142 }
00143 
00144 
00145 void SymbolViewer::setContext( GpsimProcessor * gpsim )
00146 {
00147         RegisterSet * registerSet = gpsim ? gpsim->registerMemory() : 0;
00148         
00149         if ( registerSet == m_pCurrentContext )
00150                 return;
00151         
00152         m_pSymbolList->clear();
00153         m_pGpsim = gpsim;
00154         m_pCurrentContext = registerSet;
00155         
00156         if (!m_pCurrentContext)
00157                 return;
00158                 
00159         connect( gpsim, SIGNAL(destroyed()), m_pSymbolList, SLOT(clear()) );
00160         
00161         unsigned count = m_pCurrentContext->size();
00162         for ( unsigned i = 0; i < count; ++i )
00163         {
00164                 RegisterInfo * reg = m_pCurrentContext->fromAddress(i);
00165                 
00166                 if ( (reg->type() == RegisterInfo::Generic) ||
00167                                         (reg->type() == RegisterInfo::Invalid) )
00168                 continue;
00169                 
00170                 new SymbolViewerItem( this, reg );
00171         }
00172 }
00173 
00174 
00175 void SymbolViewer::selectRadix( int selectIndex )
00176 {
00177         if ( (selectIndex<0) || (selectIndex>3) )
00178         {
00179                 kdWarning() << k_funcinfo << "Invalid select position for radix: " << selectIndex << endl;
00180                 return;
00181         }
00182         
00183         Radix radii[] = { Binary, Octal, Decimal, Hexadecimal };
00184         Radix newRadix = radii[selectIndex];
00185         
00186         if ( newRadix == m_valueRadix )
00187                 return;
00188         
00189         m_valueRadix = newRadix;
00190         
00191         emit valueRadixChanged(m_valueRadix);
00192 }
00193 
00194 
00195 QString SymbolViewer::toDisplayString( unsigned value ) const
00196 {
00197         switch ( m_valueRadix )
00198         {
00199                 case Binary:
00200                         return QString::number( value, 2 ).rightJustify( 8, '0', false );
00201                         
00202                 case Octal:
00203                         return "0" + QString::number( value, 8 );
00204                         
00205                 case Decimal:
00206                         return QString::number( value, 10 );
00207                         
00208                 case Hexadecimal:
00209                         return "0x" + QString::number( value, 16 );
00210         }
00211         
00212         return "?";
00213 }
00214 //END class SymbolView
00215 
00216 #include "symbolviewer.moc"
00217 
00218 #endif

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