source/ui/dialog.cxx

Go to the documentation of this file.
00001 /*************************************************************************
00002  *
00003  *  The Contents of this file are made available subject to
00004  *  the terms of GNU Lesser General Public License Version 2.1.
00005  *
00006  *
00007  *    GNU Lesser General Public License Version 2.1
00008  *    =============================================
00009  *    Copyright 2005 by Kohei Yoshida.
00010  *    1039 Kingsway Dr., Apex, NC 27502, USA
00011  *
00012  *    This library is free software; you can redistribute it and/or
00013  *    modify it under the terms of the GNU Lesser General Public
00014  *    License version 2.1, as published by the Free Software Foundation.
00015  *
00016  *    This library is distributed in the hope that it will be useful,
00017  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  *    Lesser General Public License for more details.
00020  *
00021  *    You should have received a copy of the GNU Lesser General Public
00022  *    License along with this library; if not, write to the Free Software
00023  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00024  *    MA  02111-1307  USA
00025  *
00026  ************************************************************************/
00027 
00028 
00029 #include "dialog.hxx"
00030 #include "tool/global.hxx"
00031 #include "unoglobal.hxx"
00032 #include "solver.hxx"
00033 #include "listener.hxx"
00034 #include "xcalc.hxx"
00035 #include "unohelper.hxx"
00036 #include "msgdlg.hxx"
00037 #include "optiondlg.hxx"
00038 
00039 #include <rtl/ustrbuf.hxx>
00040 
00041 #include "scsolver.hrc"
00042 
00043 #include <com/sun/star/awt/PosSize.hpp>
00044 #include <com/sun/star/awt/XButton.hpp>
00045 #include <com/sun/star/awt/XControl.hpp>
00046 #include <com/sun/star/awt/XControlModel.hpp>
00047 #include <com/sun/star/awt/XControlContainer.hpp>
00048 #include <com/sun/star/awt/XListBox.hpp>
00049 #include <com/sun/star/awt/XRadioButton.hpp>
00050 #include <com/sun/star/awt/XTextComponent.hpp>
00051 #include <com/sun/star/awt/XWindow.hpp>
00052 #include <com/sun/star/beans/XPropertySet.hpp>
00053 #include <com/sun/star/container/XNameContainer.hpp>
00054 #include <com/sun/star/container/XNamed.hpp>
00055 #include <com/sun/star/frame/XDispatchProvider.hpp>
00056 #include <com/sun/star/frame/XModel.hpp>
00057 #include <com/sun/star/frame/XNotifyingDispatch.hpp>
00058 #include <com/sun/star/lang/XInitialization.hpp>
00059 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
00060 #include <com/sun/star/lang/XServiceInfo.hpp>
00061 #include <com/sun/star/lang/XServiceName.hpp>
00062 #include <com/sun/star/sheet/XRangeSelection.hpp>
00063 #include <com/sun/star/uno/XComponentContext.hpp>
00064 
00065 #include <iostream>
00066 
00067 using namespace ::scsolver::numeric;
00068 using ::com::sun::star::uno::Reference;
00069 using ::std::vector;
00070 using ::std::cout;
00071 using ::std::endl;
00072 using ::std::distance;
00073 
00074 namespace scsolver {
00075 
00081 class ConstDlgCloseAction : public SimpleActionObject
00082 {
00083 public:
00084         virtual void execute(BaseDialog *dlg)
00085         {
00086                 dlg->close();
00087         }
00088 };
00089 
00090 ConstEditDialog::ConstEditDialog( SolverImpl* p ) :
00091         BaseDialog(p),
00092         m_bIsChangeMode(false),
00093         m_nConstraintId(0),
00094         m_pOKListener(NULL),
00095         m_pCancelListener(NULL),
00096         m_pLeftRngListener(NULL),
00097         m_pRightRngListener(NULL),
00098         m_pTopWindowListener(NULL),
00099         m_pCloseAction( new ConstDlgCloseAction )
00100 {
00101         initialize();
00102 }
00103 
00104 ConstEditDialog::~ConstEditDialog() throw()
00105 {       
00106         unregisterListeners();
00107 }
00108 
00109 void ConstEditDialog::registerListeners()
00110 {
00111         CalcInterface* pCalc = getSolverImpl()->getCalcInterface();
00112 
00113         m_pOKListener = new OKCancelBtnListener( this, ascii( "OK" ) );
00114         registerListener( ascii( "btnOK" ), m_pOKListener );
00115         
00116         m_pCancelListener = new OKCancelBtnListener( this, ascii( "Cancel" ) );
00117         registerListener( ascii( "btnCancel" ), m_pCancelListener );
00118         
00119         Reference< sheet::XRangeSelection > xRngSel = pCalc->getXRangeSelection();
00120         OSL_ASSERT( xRngSel != NULL );
00121         
00122         m_pLeftRngListener = new RngBtnListener( this, xRngSel, ascii( "editLeft" ) );
00123         m_pLeftRngListener->setSingleCell( true );
00124         registerListener( ascii( "btnLeft" ), m_pLeftRngListener );
00125                 
00126         m_pRightRngListener = new RngBtnListener( this, xRngSel, ascii( "editRight" ) );
00127         m_pRightRngListener->setSingleCell( true );
00128         registerListener( ascii( "btnRight" ), m_pRightRngListener );
00129 
00130         m_pTopWindowListener = new TopWindowListener(this);
00131         m_pTopWindowListener->setActionClosing(m_pCloseAction);
00132         registerListener(m_pTopWindowListener);
00133 }
00134 
00135 void ConstEditDialog::unregisterListeners()
00136 {
00137         unregisterListener( ascii( "btnOK" ), m_pOKListener );
00138         unregisterListener( ascii( "btnCancel" ), m_pCancelListener );
00139         unregisterListener( ascii( "btnLeft" ), m_pLeftRngListener );
00140         unregisterListener( ascii( "btnRight" ), m_pRightRngListener );
00141         unregisterListener(m_pTopWindowListener);
00142 }
00143 
00144 void ConstEditDialog::initialize()
00145 {
00146         initializeDefault( static_cast<sal_Int16>(180), static_cast<sal_Int16>(55), 
00147                 getResStr(SCSOLVER_STR_CONSTRAINTDLG_TITLE) );
00148         
00149         sal_Int32 nX, nY;
00150         
00151         nX = 5;
00152         nY = 5;
00153         addFixedText( nX, nY, 100, 10, ascii( "ftLeft" ), getResStr(SCSOLVER_STR_CELL_REFERENCE) );
00154         nY += 10;
00155         addRangeEdit( nX, nY, 70, 12, ascii( "editLeft" ), ascii( "btnLeft" ) );
00156         nX += 72;
00157         apWidgetProp p = addListBox( nX, nY, 25, 12, ascii( "lbEqual" ) );
00158 
00159         uno::Any aDropdown, aItems, aMultiSel, aSelItems;
00160         uno::Sequence< rtl::OUString > sItems( 3 );
00161         sItems[0] = ascii( "<=" );
00162         sItems[1] = ascii( "=" );
00163         sItems[2] = ascii( ">=" );
00164         aItems <<= sItems;
00165         uno::Sequence< sal_Int16 > sSelItems( 1 );
00166         sSelItems[0] = 0;
00167         aSelItems <<= sSelItems;
00168         aDropdown <<= static_cast<sal_Bool>(true);
00169         aMultiSel <<= static_cast<sal_Bool>(false);
00170         p->setPropertyValue( "Dropdown", aDropdown );
00171         p->setPropertyValue( "StringItemList", aItems );
00172         p->setPropertyValue( "MultiSelection", aMultiSel );
00173         p->setPropertyValue( "SelectedItems", aSelItems );
00174         
00175         nX += 27;
00176         addFixedText( nX, 5, 100, 10, ascii( "ftRight" ), getResStr(SCSOLVER_STR_CONSTRAINT) );
00177         addRangeEdit( nX, nY, 70, 12, ascii( "editRight" ), ascii( "btnRight" ) );
00178         
00179         nX = 5;
00180         nY = 35;
00181         addButton( nX, nY, 50, 15, ascii( "btnOK" ), getResStr(SCSOLVER_STR_BTN_OK) );
00182         addButton( nX+55, nY, 50, 15, ascii( "btnCancel" ), getResStr(SCSOLVER_STR_BTN_CANCEL) );       
00183 
00184         registerListeners();
00185 }
00186 
00187 void ConstEditDialog::reset()
00188 {
00189         Reference< uno::XInterface > oEditLeft = getWidgetByName( ascii( "editLeft" ) );
00190         OSL_ASSERT( oEditLeft != NULL );
00191         Reference< awt::XTextComponent > xCompL( oEditLeft, UNO_QUERY );
00192         xCompL->setText( ascii( "" ) );
00193         
00194         Reference< uno::XInterface > oEditRight = getWidgetByName( ascii( "editRight" ) );
00195         OSL_ASSERT( oEditRight != NULL );
00196         Reference< awt::XTextComponent > xCompR( oEditRight, UNO_QUERY );
00197         xCompR->setText( ascii( "" ) );
00198         
00199         Reference< uno::XInterface > oEq = getWidgetByName( ascii( "lbEqual" ) );
00200         OSL_ASSERT( oEq != NULL );
00201         Reference< awt::XListBox > xEq( oEq, UNO_QUERY );
00202         xEq->selectItemPos( 0, true );
00203 }
00204 
00205 void ConstEditDialog::setVisible( bool bVisible )
00206 {
00207         if ( bVisible )
00208                 toFront();
00209 
00210         setVisibleDefault( bVisible );
00211 }
00212 
00213 bool ConstEditDialog::doneRangeSelection() const
00214 {
00215         SolverDialog* pDlg = getSolverImpl()->getMainDialog();
00216         pDlg->setVisible( true );
00217     setFocus();
00218 
00219         if ( !isCellRangeGeometryEqual() )
00220         {
00221                 pDlg->showMessage( getResStr(SCSOLVER_STR_MSG_REF_CON_RANGE_MISMATCH) );
00222                 return false;
00223         }
00224 
00225         return true;
00226 }
00227 
00228 void ConstEditDialog::close()
00229 {
00230         reset();
00231         setVisible(false);
00232 }
00233 
00234 rtl::OUString ConstEditDialog::getLeftCellReference()
00235 {
00236         Reference< uno::XInterface > oEdit = getWidgetByName( ascii( "editLeft" ) );
00237         Reference< awt::XTextComponent > xComp( oEdit, UNO_QUERY );
00238         return xComp->getText();
00239 }
00240 
00241 rtl::OUString ConstEditDialog::getRightCellReference()
00242 {
00243         Reference< uno::XInterface > oEdit = getWidgetByName( ascii( "editRight" ) );
00244         Reference< awt::XTextComponent > xComp( oEdit, UNO_QUERY );
00245         return xComp->getText();
00246 }
00247 
00248 EqualityType ConstEditDialog::getEquality()
00249 {
00250         Reference< uno::XInterface > oEq = getWidgetByName( ascii( "lbEqual" ) );
00251         Reference< awt::XListBox > xEq( oEq, UNO_QUERY );
00252         if ( xEq != NULL )
00253         {
00254                 sal_Int16 nPos = xEq->getSelectedItemPos();
00255                 switch( nPos )
00256                 {
00257                         case 0:
00258                                 return LESS_EQUAL;
00259                         case 1:
00260                                 return EQUAL;
00261                         case 2:
00262                                 return GREATER_EQUAL;
00263                         default:
00264                                 OSL_ASSERT( !"Unexpected (in)equal sign" );
00265                 }
00266         }
00267         else
00268                 OSL_ASSERT( !"xEq == NULL" );
00269         
00270         return EQUAL;
00271 }
00272 
00273 void ConstEditDialog::setConstraintId( sal_uInt32 nId )
00274 {
00275         m_nConstraintId = nId;
00276 }
00277 
00278 const rtl::OUString ConstEditDialog::getLeftCellReference() const
00279 {
00280         Reference< uno::XInterface > oEdit = getWidgetByName( ascii( "editLeft" ) );
00281         Reference< awt::XTextComponent > xComp( oEdit, UNO_QUERY );
00282         return xComp->getText();
00283 }
00284 
00285 const rtl::OUString ConstEditDialog::getRightCellReference() const
00286 {
00287         Reference< uno::XInterface > oEdit = getWidgetByName( ascii( "editRight" ) );
00288         Reference< awt::XTextComponent > xComp( oEdit, UNO_QUERY );
00289         return xComp->getText();
00290 }
00291 
00292 void ConstEditDialog::setLeftCellReference( const rtl::OUString& s )
00293 {
00294         Reference< uno::XInterface > oEdit = getWidgetByName( ascii( "editLeft" ) );
00295         Reference< awt::XTextComponent > xComp( oEdit, UNO_QUERY );     
00296         xComp->setText( s );
00297 }
00298 
00299 void ConstEditDialog::setRightCellReference( const rtl::OUString& s )
00300 {
00301         Reference< uno::XInterface > oEdit = getWidgetByName( ascii( "editRight" ) );
00302         Reference< awt::XTextComponent > xComp( oEdit, UNO_QUERY );     
00303         xComp->setText( s );
00304 }
00305 
00306 void ConstEditDialog::setEquality( const EqualityType e )
00307 {
00308         Reference< uno::XInterface > oEq = getWidgetByName( ascii( "lbEqual" ) );
00309         Reference< awt::XListBox > xEq( oEq, UNO_QUERY );
00310         if ( xEq != NULL )
00311         {
00312                 switch( e )
00313                 {
00314                         case LESS_EQUAL:
00315                                 xEq->selectItemPos( 0, true );
00316                                 break;
00317                         case EQUAL:
00318                                 xEq->selectItemPos( 1, true );
00319                                 break;
00320                         case GREATER_EQUAL:
00321                                 xEq->selectItemPos( 2, true );
00322                                 break;
00323                         default:
00324                                 OSL_ASSERT( !"Unexpected Equality" );
00325                 }
00326         }
00327         else
00328                 OSL_ASSERT( !"xEq == NULL" );
00329 }
00330 
00337 bool ConstEditDialog::isCellRangeGeometryEqual() const
00338 {
00339         using table::CellRangeAddress;
00340         
00341         rtl::OUString sLeftRef = getLeftCellReference();
00342         rtl::OUString sRightRef = getRightCellReference();
00343         if ( sLeftRef.getLength() > 0 && sRightRef.getLength() > 0 )
00344         {
00345                 CalcInterface* pCalc = getSolverImpl()->getCalcInterface();
00346 
00347                 CellRangeAddress aLAddr = pCalc->getCellRangeAddress( sLeftRef );
00348                 CellRangeAddress aRAddr = pCalc->getCellRangeAddress( sRightRef );
00349                 sal_Int32 nLCol = aLAddr.EndColumn - aLAddr.StartColumn;
00350                 sal_Int32 nRCol = aRAddr.EndColumn - aRAddr.StartColumn;
00351                 sal_Int32 nLRow = aLAddr.EndRow - aLAddr.StartRow;
00352                 sal_Int32 nRRow = aRAddr.EndRow - aRAddr.StartRow;
00353 
00354                 return nLCol == nRCol && nLRow == nRRow;
00355         }
00356 
00357         return true;
00358 }
00359 
00360 //-----------------------------------------------------------------
00361 
00365 class SolverDlgCloseAction : public SimpleActionObject
00366 {
00367 public:
00368         virtual void execute(BaseDialog *dlg)
00369         {
00370                 dlg->close();
00371         }
00372 };
00373 
00374 SolverDialog::SolverDialog( SolverImpl* p )
00375         : BaseDialog( p ),
00376 
00377         m_pConstEditDialog( NULL ),
00378         m_pOptionDialog( NULL ),
00379         m_pTopWindowListener(NULL),
00380         m_pTargetRngListener( NULL ),
00381         m_pVarCellsRngListener( NULL ),
00382         m_pSolveListener( NULL ),
00383         m_pOKListener( NULL ),
00384         m_pSaveListener( NULL ),
00385         m_pLoadListener( NULL ),
00386         m_pResetListener( NULL ),
00387         m_pOptionListener( NULL ),
00388         m_pMaxListener( NULL ),
00389         m_pConstAddListener( NULL ),
00390         m_pConstChangeListener( NULL ),
00391         m_pConstDeleteListener( NULL ),
00392         m_pConstListBoxListener( NULL ),
00393         m_pCloseAction( new SolverDlgCloseAction )
00394 {
00395         initialize();
00396 }
00397 
00398 SolverDialog::~SolverDialog() throw()
00399 {       
00400         unregisterListeners();
00401 }
00402 
00403 void SolverDialog::initialize()
00404 {
00405         initializeDefault( static_cast<sal_Int16>(260), static_cast<sal_Int16>(200), 
00406                 getResStr(SCSOLVER_STR_MAINDLG_TITLE) );
00407 
00408         // START CREATING WIDGETS
00409         sal_Int32 nX, nY;
00410         
00411         nY = 5;
00412         addFixedLine( 5, nY, 195, 12, ascii( "flModel" ), getResStr(SCSOLVER_STR_DEFINE_MODEL) );
00413         
00414         nY += 13;
00415         addFixedText( 10, nY+2, 60, 12, ascii( "ftTargetCell" ), getResStr(SCSOLVER_STR_SET_TARGET_CELL) );
00416         addRangeEdit( 70, nY, 80, 12, ascii( "editTargetCell" ), ascii( "btnTargetCellRange" ) );
00417 
00418         nY += 18;
00419         addFixedText( 10, nY, 35, 12, ascii( "ftObj" ), getResStr(SCSOLVER_STR_GOAL) );
00420         addRadioButton( 50, nY, 40, 12, ascii( "rbMax" ), getResStr(SCSOLVER_STR_MAXIMIZE) );
00421         addRadioButton( 115, nY, 40, 12, ascii( "rbMin" ), getResStr(SCSOLVER_STR_MINIMIZE) );
00422         
00423         nY += 15;
00424         addFixedText( 10, nY+2, 60, 12, ascii( "ftDecVars" ), getResStr(SCSOLVER_STR_DECISIONVAR_CELLS) );
00425         addRangeEdit( 85, nY, 115, 12, ascii( "editVarCells" ), ascii( "btnRangeSelect" ) );
00426         
00427         nY += 20;
00428         addFixedLine( 5, nY, 195, 12, ascii( "flConstraints" ), getResStr(SCSOLVER_STR_CONSTRAINT_SEP) );
00429         
00430         nY += 13;
00431         nX = 10;
00432         addListBox( nX, nY, 130, 80, ascii( "lbConstraint" ) );
00433         nX += 135;
00434 
00435         addButton( nX, nY,    50, 15, ascii( "btnConstAdd" ), getResStr(SCSOLVER_STR_BTN_ADD) );
00436 
00437         uno::Any aBool;
00438         aBool <<= static_cast<sal_Bool>(false);
00439 
00440         apWidgetProp p = addButton( 
00441                 nX, nY+20, 50, 15, ascii( "btnConstChange" ), getResStr(SCSOLVER_STR_BTN_CHANGE) );
00442 
00443         p->setPropertyValue( "Enabled", aBool );
00444         
00445         p = addButton( 
00446                 nX, nY+40, 50, 15, ascii( "btnConstDelete" ), getResStr(SCSOLVER_STR_BTN_DELETE) );
00447 
00448         p->setPropertyValue( "Enabled", aBool );
00449         
00450         addButton( 205, 10, 50, 15, ascii( "btnSolve" ), getResStr(SCSOLVER_STR_BTN_SOLVE) );
00451         addButton( 205, 30, 50, 15, ascii( "btnReset" ), getResStr(SCSOLVER_STR_BTN_RESET) );
00452         
00453         p = addButton( 205, 50, 50, 15, ascii( "btnOptions" ), getResStr(SCSOLVER_STR_BTN_OPTIONS) );
00454 
00455         addButton( 205, 90, 50, 15, ascii( "btnSave" ), getResStr(SCSOLVER_STR_BTN_SAVE_MODEL) );
00456         addButton( 205, 110, 50, 15, ascii( "btnLoad" ), getResStr(SCSOLVER_STR_BTN_LOAD_MODEL) );
00457 
00458         addButton( 205, 180, 50, 15, ascii( "btnClose" ), getResStr(SCSOLVER_STR_BTN_CLOSE) );
00459 
00460         registerListeners();
00461 }
00462 
00463 void SolverDialog::registerListeners()
00464 {
00465         CalcInterface* pCalc = getSolverImpl()->getCalcInterface();
00466 
00467         Reference< sheet::XRangeSelection > xRngSel = pCalc->getXRangeSelection();
00468         OSL_ASSERT( xRngSel != NULL );
00469 
00470         m_pVarCellsRngListener = new RngBtnListener( this, xRngSel, ascii( "editVarCells" ) );
00471         m_pVarCellsRngListener->setSingleCell( false );
00472         registerListener( ascii( "btnRangeSelect" ), m_pVarCellsRngListener );
00473 
00474         m_pTargetRngListener = new RngBtnListener( this, xRngSel, ascii( "editTargetCell" ) );
00475         m_pTargetRngListener->setSingleCell( true );
00476         registerListener( ascii( "btnTargetCellRange" ), m_pTargetRngListener );
00477         
00478         m_pSolveListener = new SolveBtnListener( this );
00479         registerListener( ascii( "btnSolve" ), m_pSolveListener );
00480         
00481         m_pOKListener = new CloseBtnListener( this );
00482         registerListener( ascii( "btnClose" ), m_pOKListener );
00483         
00484         m_pSaveListener = new SaveBtnListener( this );
00485         registerListener( ascii( "btnSave" ), m_pSaveListener );
00486         m_pLoadListener = new LoadBtnListener( this );
00487         registerListener( ascii( "btnLoad" ), m_pLoadListener );
00488         
00489         m_pResetListener = new ResetBtnListener( this );
00490         registerListener( ascii( "btnReset" ), m_pResetListener );
00491 
00492         m_pOptionListener = new OptionBtnListener( this );
00493         registerListener( ascii( "btnOptions" ), m_pOptionListener );
00494 
00495         m_pMaxListener = new MaxRadioBtnListener( this );
00496         registerListener( ascii( "rbMax" ), m_pMaxListener );
00497         
00498         m_pConstAddListener = new ConstEditBtnListener( this, CONST_ADD );
00499         registerListener( ascii( "btnConstAdd" ), m_pConstAddListener );
00500 
00501         m_pConstChangeListener = new ConstEditBtnListener( this, CONST_CHANGE );
00502         registerListener( ascii( "btnConstChange" ), m_pConstChangeListener );
00503 
00504         m_pConstDeleteListener = new ConstEditBtnListener( this, CONST_DELETE );
00505         registerListener( ascii( "btnConstDelete" ), m_pConstDeleteListener );
00506 
00507         m_pConstListBoxListener = new ConstListBoxListener( this );
00508         registerListener( ascii( "lbConstraint" ), m_pConstListBoxListener );
00509         
00510         m_pTopWindowListener = new TopWindowListener(this);
00511         m_pTopWindowListener->setActionClosing(m_pCloseAction);
00512         registerListener(m_pTopWindowListener);
00513 }
00514 
00515 void SolverDialog::unregisterListeners()
00516 {
00517         // Unregistering a listener object from its associated widget causes it
00518         // to be automatically destroyed, so that a manual delete of such object 
00519         // is no longer necessary.
00520         
00521         unregisterListener( ascii( "btnRangeSelect" ), m_pVarCellsRngListener );
00522         unregisterListener( ascii( "btnTargetCellRange" ), m_pTargetRngListener );
00523         unregisterListener( ascii( "btnClose" ), m_pOKListener );
00524         unregisterListener( ascii( "btnSave" ), m_pSaveListener );
00525         unregisterListener( ascii( "btnLoad" ), m_pLoadListener );
00526         unregisterListener( ascii( "btnReset" ), m_pResetListener );
00527         unregisterListener( ascii( "btnOptions" ), m_pOptionListener );
00528         unregisterListener( ascii( "rbMax" ), m_pMaxListener );
00529         unregisterListener( ascii( "btnConstAdd" ), m_pConstAddListener );
00530         unregisterListener( ascii( "btnConstChange" ), m_pConstChangeListener );
00531         unregisterListener( ascii( "btnConstDelete" ), m_pConstDeleteListener );
00532         unregisterListener( ascii( "lbConstraint" ), m_pConstListBoxListener );
00533         unregisterListener(m_pTopWindowListener);
00534 }
00535 
00536 void SolverDialog::setVisible( bool bVisible )
00537 {
00538         if ( bVisible )
00539         {
00540                 // Get currently active sheet (no use as long as the dialog is modeless).
00541                 CalcInterface* pCalc = getSolverImpl()->getCalcInterface();
00542 
00543                 Reference< sheet::XSpreadsheet > xSheet = pCalc->getActiveSheet();
00544                 Reference< container::XNamed > xNamed( xSheet, UNO_QUERY );
00545 //      printOUStr( ascii( "Current sheet is " ) + xNamed->getName() );
00546         }
00547         else
00548                 getConstEditDialog()->setVisible( false );
00549 
00550         setVisibleDefault( bVisible );
00551 }
00552 
00553 void SolverDialog::close()
00554 {
00555         getConstEditDialog()->reset();
00556         getConstEditDialog()->setVisible(false);
00557         getOptionDialog()->setVisible(false);
00558         setVisible(false);
00559 }
00560 
00561 ConstEditDialog* SolverDialog::getConstEditDialog()
00562 {
00563     if (!m_pConstEditDialog.get())
00564     {
00565         m_pConstEditDialog.reset( new ConstEditDialog(getSolverImpl()) );
00566         m_pConstEditDialog->setRefBoundingBox(getPosSize());
00567     }
00568     
00569     return m_pConstEditDialog.get();
00570 }
00571 
00572 OptionDialog* SolverDialog::getOptionDialog()
00573 {
00574     if(!m_pOptionDialog.get())
00575     {
00576         m_pOptionDialog.reset( new OptionDialog( getSolverImpl() ) );
00577         m_pOptionDialog->setRefBoundingBox(getPosSize());
00578     }
00579 
00580     return m_pOptionDialog.get();
00581 }
00582 
00583 sal_Int16 SolverDialog::getSelectedConstraintPos()
00584 {
00585         Reference< uno::XInterface > oWgt = getWidgetByName( ascii( "lbConstraint" ) );
00586         OSL_ASSERT( oWgt != NULL );
00587         Reference< awt::XListBox > xLB( oWgt, UNO_QUERY );
00588         OSL_ASSERT( xLB != NULL );
00589         return xLB->getSelectedItemPos();
00590 }
00591 
00592 Reference< awt::XTextComponent > SolverDialog::getXTextComponentFromWidget(
00593         const rtl::OUString& sName ) const
00594 {
00595         Reference< uno::XInterface > oRngEdit = getWidgetByName( sName );
00596         OSL_ASSERT( oRngEdit != NULL );
00597         Reference< awt::XTextComponent > xComp( oRngEdit, UNO_QUERY );
00598         return xComp;
00599 }
00600 
00601 rtl::OUString SolverDialog::getTargetCellAddress() const
00602 {
00603         Reference< awt::XTextComponent > xComp = getXTextComponentFromWidget( 
00604                         ascii( "editTargetCell" ) );
00605         return xComp->getText();
00606 }
00607 
00608 void SolverDialog::setTargetCellAddress( const rtl::OUString& sAddr )
00609 {
00610         Reference< awt::XTextComponent > xComp = getXTextComponentFromWidget( 
00611                         ascii( "editTargetCell" ) );
00612         xComp->setText( sAddr );
00613 }
00614 
00615 rtl::OUString SolverDialog::getVarCellAddress()
00616 {
00617         Reference< awt::XTextComponent > xComp = getXTextComponentFromWidget( 
00618                         ascii( "editVarCells" ) );
00619         return xComp->getText();
00620 }
00621 
00622 void SolverDialog::setVarCellAddress( const rtl::OUString& sAddr )
00623 {
00624         Reference< awt::XTextComponent > xComp = getXTextComponentFromWidget( 
00625                         ascii( "editVarCells" ) );
00626         xComp->setText( sAddr );
00627 }
00628 
00629 GoalType SolverDialog::getGoal() const
00630 {
00631         Reference< uno::XInterface > xModel = getWidgetModelByName( ascii( "rbMax" ) );
00632         sal_Int16 nState;
00633         unohelper::getPropertyValue( xModel, ascii( "State" ), nState );
00634         if ( nState )
00635                 return GOAL_MAXIMIZE;
00636         
00637         xModel = getWidgetModelByName( ascii( "rbMin" ) );
00638         unohelper::getPropertyValue( xModel, ascii( "State" ), nState );
00639         if ( nState )
00640                 return GOAL_MINIMIZE;
00641 
00642         return GOAL_UNKNOWN;
00643 }
00644 
00645 void SolverDialog::setGoal( GoalType eGoal )
00646 {
00647         rtl::OUString sName = ascii( "" );
00648         switch ( eGoal )
00649         {
00650         case GOAL_MAXIMIZE:
00651                 sName = ascii( "rbMax" );
00652                 break;
00653         case GOAL_MINIMIZE:
00654                 sName = ascii( "rbMin" );
00655                 break;
00656         case GOAL_UNKNOWN:
00657                 break;
00658         }
00659 
00660         if ( sName.getLength() > 0 )
00661         {
00662                 Reference< uno::XInterface > xModel = getWidgetModelByName( sName );
00663                 sal_Int16 nState = 1;
00664                 uno::Any aVal;
00665                 aVal <<= nState;
00666                 unohelper::setPropertyValue( xModel, ascii( "State" ), aVal );
00667         }
00668 }
00669 
00670 void SolverDialog::output()
00671 {
00672         cout << "----------------------------------------------" << endl;
00673         cout << "Subject to the constraints:" << endl;
00674 
00675         vector< ConstraintString >::iterator pos = m_aConstraint.begin();
00676         while( pos != m_aConstraint.end() )
00677         {
00678                 ConstraintString aItem = *pos;
00679                 rtl::OUString sLine = ascii( "  " ) + aItem.Left;
00680                 switch ( aItem.Equal )
00681                 {
00682                         case GREATER_EQUAL:
00683                                 sLine += ascii( " >= " );
00684                                 break;
00685                         case EQUAL:
00686                                 sLine += ascii( " = " );
00687                                 break;
00688                         case LESS_EQUAL:
00689                                 sLine += ascii( " <= " );
00690                                 break;
00691                         default:
00692                                 OSL_ASSERT( !"wrong equality type" );
00693                 }
00694                 sLine += aItem.Right;
00695                 printOUStr( sLine );
00696                 ++pos;
00697         }
00698         cout << "----------------------------------------------" << endl;
00699 }
00700 
00701 void SolverDialog::getConstraint( const sal_uInt32 nIdx, 
00702         rtl::OUString& sLeft, rtl::OUString& sRight, EqualityType& eEq )
00703 {
00704         ConstraintString aConstItem = m_aConstraint.at( nIdx );
00705         sLeft = aConstItem.Left;
00706         sRight = aConstItem.Right;
00707         eEq = aConstItem.Equal;
00708 }
00709 
00710 vector< ConstraintString > SolverDialog::getAllConstraints() const
00711 {
00712         return m_aConstraint;
00713 }
00714 
00715 void SolverDialog::setConstraint( 
00716                 const rtl::OUString& sLeft, const rtl::OUString& sRight, const EqualityType eEq )
00717 {
00718         ConstraintString aConstItem;
00719         aConstItem.Left = sLeft;
00720         aConstItem.Right = sRight;
00721         aConstItem.Equal = eEq;
00722         m_aConstraint.push_back( aConstItem );
00723         
00724 #if SCSOLVER_DEBUG      
00725     output();
00726 #endif    
00727         setConstraintImpl( sLeft, sRight, eEq, false );
00728 }
00729 
00730 void SolverDialog::editConstraint( const sal_uInt32 nId,
00731                 const rtl::OUString& sLeft, const rtl::OUString& sRight, const EqualityType eEqVal )
00732 {
00733         if ( m_aConstraint.size() <= nId )
00734                 return;
00735         
00736         vector< ConstraintString > aConstraint;
00737         vector< ConstraintString >::iterator pos = m_aConstraint.begin();
00738         sal_uInt32 nCurId = 0;
00739         while ( pos != m_aConstraint.end() )
00740         {
00741                 if ( nCurId == nId )
00742                 {
00743                         ConstraintString aConstItem;
00744                         aConstItem.Left = sLeft;
00745                         aConstItem.Right = sRight;
00746                         aConstItem.Equal = eEqVal;
00747                         m_aConstraint.erase( pos );
00748                         m_aConstraint.insert( pos, aConstItem );
00749                 }
00750                 ++pos; ++nCurId;
00751         }
00752         
00753 #if SCSOLVER_DEBUG      
00754     output();
00755 #endif    
00756         setConstraintImpl( sLeft, sRight, eEqVal, true, nId );
00757 }
00758 
00759 void SolverDialog::removeConstraint( const sal_uInt32 nIdx )
00760 {       
00761         if ( m_aConstraint.size() <= nIdx )
00762                 return;
00763         
00764         sal_uInt32 nCurId = 0;
00765         vector< ConstraintString >::iterator pos = m_aConstraint.begin();
00766         while ( pos != m_aConstraint.end() )
00767         {
00768                 if ( nCurId == nIdx )
00769                 {
00770                         m_aConstraint.erase( pos );
00771                         break;
00772                 }
00773                 ++pos; ++nCurId;
00774         }
00775 
00776 #if SCSOLVER_DEBUG      
00777     output();
00778 #endif    
00779         
00780         removeConstraintsFromListBox( nIdx, 1 );
00781 }
00782 
00784 void SolverDialog::clearConstraints()
00785 {
00786         m_aConstraint.clear();
00787         removeConstraintsFromListBox();
00788 }
00789 
00795 void SolverDialog::removeConstraintsFromListBox( sal_Int16 nPos, sal_Int16 nCount )
00796 {
00797         Reference< uno::XInterface > oWgt = getWidgetByName( ascii( "lbConstraint" ) );
00798         OSL_ASSERT( oWgt != NULL );
00799         Reference< awt::XListBox > xLB( oWgt, UNO_QUERY );
00800         OSL_ASSERT( xLB != NULL );
00801         if ( nCount == 0 )
00802         {
00803                 sal_Int16 nItemCount = xLB->getItemCount();
00804                 nCount = nItemCount - nPos;
00805         }
00806         xLB->removeItems( nPos, nCount );       
00807 }
00808 
00816 void SolverDialog::setConstraintImpl( 
00817                 const rtl::OUString& sLeft, const rtl::OUString& sRight, const EqualityType eEq,
00818                 const sal_Bool bChangeMode, const sal_uInt32 nId )
00819 {
00820         rtl::OUString sEq;
00821         switch( eEq )
00822         {
00823                 case GREATER_EQUAL:
00824                         sEq = ascii( ">=" );
00825                         break;
00826                 case EQUAL:
00827                         sEq = ascii( "=" );
00828                         break;
00829                 case LESS_EQUAL:
00830                         sEq = ascii( "<=" );
00831                         break;
00832                 default:
00833                         OSL_ASSERT( !"Unknown equality" );
00834         }
00835         
00836         rtl::OUString sWhole = sLeft + ascii( " " ) + sEq + ascii( " " ) + sRight;
00837         printOUStr( sWhole );
00838         Reference< uno::XInterface > oLB = getWidgetByName( ascii( "lbConstraint" ) );
00839         if ( oLB != NULL )
00840         {
00841                 Reference< awt::XListBox > xLB( oLB, UNO_QUERY );
00842                 if ( xLB != NULL )
00843                 {
00844                         if ( bChangeMode )
00845                         {
00846                                 xLB->removeItems( nId, 1 );
00847                                 xLB->addItem( sWhole, nId );
00848                         }
00849                         else
00850                         {
00851                                 sal_Int16 nCount = xLB->getItemCount();
00852                                 xLB->addItem( sWhole, nCount );
00853                         }
00854                 }
00855                 else
00856                         OSL_ASSERT( !"xLB is NULL" );
00857         }
00858         else
00859                 OSL_ASSERT( !"oLB is NULL" );
00860 }
00861 
00862 void SolverDialog::showMessage( const rtl::OUString& sMsg )
00863 {
00864     MessageDialog dlg(getSolverImpl(), sMsg);
00865     dlg.setRefBoundingBox(getPosSize());
00866     dlg.execute();
00867 }
00868 
00869 void SolverDialog::showSolutionInfeasible()
00870 {
00871     showMessage(getResStr(SCSOLVER_STR_MSG_SOLUTION_NOT_FOUND));
00872 }
00873 
00874 void SolverDialog::showSolutionFound()
00875 {
00876     showMessage(getResStr(SCSOLVER_STR_MSG_SOLUTION_FOUND));
00877 }
00878 
00879 void SolverDialog::reset()
00880 {
00881         setTextByWidget( this, ascii( "editTargetCell" ), ascii( "" ) );
00882         setTextByWidget( this, ascii( "editVarCells" ), ascii( "" ) );
00883         setGoal( GOAL_UNKNOWN );
00884         clearConstraints();
00885 }
00886 
00887 void SolverDialog::saveModelToDocument()
00888 {
00889         using rtl::OUString;
00890         using rtl::OUStringBuffer;
00891         
00892         OUStringBuffer sb;
00893         
00894         sb.append( getTextByWidget( this, ascii( "editTargetCell" ) ) );
00895         sb.append( Global::STRING_SEPARATOR );
00896         GoalType eGoal = getGoal();
00897         switch ( eGoal )
00898         {
00899         case GOAL_MAXIMIZE:
00900                 sb.append( ascii( "max" ) );
00901                 break;
00902         case GOAL_MINIMIZE:
00903                 sb.append( ascii( "min" ) );
00904                 break;
00905         default:
00906                 OSL_ASSERT( eGoal == GOAL_UNKNOWN );
00907                 sb.append( ascii( "unknown" ) );
00908         }
00909         sb.append( Global::STRING_SEPARATOR );
00910         sb.append( getTextByWidget( this, ascii( "editVarCells" ) ) );
00911         
00912         typedef std::vector< ConstraintString > ConStr;
00913         ConStr cnConStr = getAllConstraints();
00914         ConStr::iterator itEnd = cnConStr.end();
00915         for ( ConStr::iterator it = cnConStr.begin(); it != itEnd; ++it )
00916         {
00917                 sb.append( Global::STRING_SEPARATOR );
00918                 sb.append( it->Left );
00919                 sb.append( Global::STRING_SEPARATOR );
00920                 switch ( it->Equal )
00921                 {
00922                 case numeric::EQUAL:
00923                         sb.append( ascii( "=" ) );
00924                         break;
00925                 case numeric::GREATER_EQUAL:
00926                         sb.append( ascii( ">=" ) );
00927                         break;
00928                 case numeric::LESS_EQUAL:
00929                         sb.append( ascii( "<=" ) );
00930                         break;
00931                 default:
00932                         OSL_ASSERT( !"unknown equality sign" );
00933                 }
00934                 sb.append( Global::STRING_SEPARATOR );
00935                 sb.append( it->Right );
00936         }
00937 
00938         OUString sStr = sb.makeStringAndClear();
00939         CalcInterface* pCalc = getSolverImpl()->getCalcInterface();
00940         uno::Any aVal;
00941         aVal <<= sStr;
00942         pCalc->setDocumentProperty( Global::MODEL_METADATA_NAME, aVal );
00943 }
00944 
00945 void SolverDialog::loadModelFromDocument()
00946 {
00947         using rtl::OUString;
00948         typedef std::vector< rtl::OUString > ContStr;
00949 
00950         CalcInterface* pCalc = getSolverImpl()->getCalcInterface();
00951         uno::Any aVal = pCalc->getDocumentProperty( Global::MODEL_METADATA_NAME );
00952         OUString sVal;
00953         aVal >>= sVal;
00954         OUString sSep = Global::STRING_SEPARATOR;
00955         ContStr cnVal;
00956         split( sVal, sSep, cnVal );
00957         clearConstraints();
00958 
00959         ContStr::iterator it, itBeg = cnVal.begin(), itEnd = cnVal.end();
00960         for ( it = itBeg; it != itEnd; ++it )
00961         {
00962                 sal_Int32 nIdx = distance( itBeg, it );
00963                 if ( nIdx == 0 )
00964                         setTextByWidget( this, ascii( "editTargetCell" ), *it );
00965                 else if ( nIdx == 1 )
00966                 {
00967                         OUString sItem = *it;
00968                         if ( sItem.equals( ascii( "min" ) ) )
00969                                 setGoal( GOAL_MINIMIZE );
00970                         else if ( sItem.equals( ascii( "max" ) ) )
00971                                 setGoal( GOAL_MAXIMIZE );
00972                 }
00973                 else if ( nIdx == 2 )
00974                         setTextByWidget( this, ascii( "editVarCells" ), *it );
00975                 else if ( nIdx > 2 )
00976                 {
00977                         OUString sLeft = *it++;
00978                         OSL_ASSERT( it != itEnd );
00979                         OUString sEqual = *it++;
00980                         OSL_ASSERT( it != itEnd );
00981                         OUString sRight = *it;
00982                         numeric::EqualityType eEq;
00983                         if ( sEqual.equals( ascii( ">=" ) ) )
00984                                 eEq = GREATER_EQUAL;
00985                         else if ( sEqual.equals( ascii( "<=" ) ) )
00986                                 eEq = LESS_EQUAL;
00987                         else if ( sEqual.equals( ascii( "=" ) ) )
00988                                 eEq = EQUAL;
00989                         else
00990                                 OSL_ASSERT( !"unknown equality value" );
00991                         setConstraint( sLeft, sRight, eEq );
00992                 }
00993         }
00994 }
00995 
00996 void SolverDialog::updateWidgets()
00997 {
00998         if ( getSelectedConstraintPos() < 0 )
00999         {
01000                 enableWidget(ascii("btnConstChange"), false);
01001                 enableWidget(ascii("btnConstDelete"), false);
01002         }
01003         else
01004         {
01005                 enableWidget(ascii("btnConstChange"), true);
01006                 enableWidget(ascii("btnConstDelete"), true);
01007         }
01008 }
01009 
01010 }

Generated on Mon Jul 28 09:13:20 2008 for scsolver by  doxygen 1.5.3