kspread

kspread_dlg_reference.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002-2003 Norbert Andres <nandres@web.de>
00003              (C) 2002 Ariya Hidayat <ariya@kde.org>
00004              (C) 2002 Harri Porten <porten@kde.org>
00005              (C) 2002 John Dailey <dailey@vt.edu>
00006              (C) 1999-2002 Laurent Montel <montel@kde.org>
00007              (C) 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
00008              (C) 1998-2000 Torben Weis <weis@kde.org>
00009 
00010    This library is free software; you can redistribute it and/or
00011    modify it under the terms of the GNU Library General Public
00012    License as published by the Free Software Foundation; either
00013    version 2 of the License, or (at your option) any later version.
00014 
00015    This library is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018    Library General Public License for more details.
00019 
00020    You should have received a copy of the GNU Library General Public License
00021    along with this library; see the file COPYING.LIB.  If not, write to
00022    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00023  * Boston, MA 02110-1301, USA.
00024 */
00025 
00026 
00027 
00028 #include "kspread_dlg_reference.h"
00029 #include "kspread_canvas.h"
00030 #include "kspread_doc.h"
00031 #include "kspread_util.h"
00032 #include "kspread_map.h"
00033 #include "kspread_view.h"
00034 #include "kspread_sheet.h"
00035 #include "selection.h"
00036 #include "kspread_locale.h"
00037 
00038 #include <qvariant.h>
00039 #include <qcombobox.h>
00040 #include <qlabel.h>
00041 #include <qlineedit.h>
00042 #include <qpushbutton.h>
00043 #include <qlayout.h>
00044 
00045 #include <kbuttonbox.h>
00046 #include <kmessagebox.h>
00047 #include <kdebug.h>
00048 #include <kdeversion.h>
00049 
00050 #include <kstdguiitem.h>
00051 
00052 using namespace KSpread;
00053 
00054 reference::reference( View* parent, const char* name )
00055   : QDialog( parent, name,TRUE )
00056 {
00057   m_pView = parent;
00058   QVBoxLayout *lay1 = new QVBoxLayout( this );
00059   lay1->setMargin( KDialog::marginHint() );
00060   lay1->setSpacing( KDialog::spacingHint() );
00061   m_list = new QListBox(this);
00062   lay1->addWidget( m_list );
00063 
00064   setCaption( i18n("Area Name") );
00065 
00066   m_rangeName = new QLabel(this);
00067   lay1->addWidget(m_rangeName);
00068 
00069   m_pRemove = new QPushButton(i18n("&Remove"), this);
00070   lay1->addWidget( m_pRemove );
00071   //m_pRemove->setEnabled(false);
00072   KButtonBox *bb = new KButtonBox( this );
00073   //  bb->addStretch();
00074   m_pEdit = bb->addButton( i18n("&Edit...") );
00075   m_pOk = bb->addButton( KStdGuiItem::ok() );
00076   m_pCancel = bb->addButton( KStdGuiItem::cancel() );
00077   m_pOk->setDefault( TRUE );
00078   bb->layout();
00079   lay1->addWidget( bb );
00080 
00081   QString text;
00082   QStringList sheetName;
00083   QPtrListIterator<Sheet> it2 ( m_pView->doc()->map()->sheetList() );
00084   for( ; it2.current(); ++it2 )
00085   {
00086       sheetName.append( it2.current()->sheetName());
00087   }
00088 
00089   QValueList<Reference>::Iterator it;
00090   QValueList<Reference> area = m_pView->doc()->listArea();
00091   for ( it = area.begin(); it != area.end(); ++it )
00092   {
00093     text = (*it).ref_name;
00094     if ( sheetName.contains((*it).sheet_name ))
00095         m_list->insertItem(text);
00096   }
00097 
00098   if ( !m_list->count() )
00099   {
00100     m_pOk->setEnabled( false );
00101     m_pRemove->setEnabled( false );
00102     m_pEdit->setEnabled( false );
00103   }
00104 
00105   connect( m_pOk, SIGNAL( clicked() ), this, SLOT( slotOk() ) );
00106   connect( m_pCancel, SIGNAL( clicked() ), this, SLOT( slotCancel() ) );
00107   connect( m_pEdit, SIGNAL( clicked() ), this, SLOT( slotEdit() ) );
00108   connect( m_pRemove, SIGNAL( clicked() ), this, SLOT( slotRemove() ) );
00109   connect( m_list, SIGNAL(doubleClicked(QListBoxItem *)), this,
00110            SLOT(slotDoubleClicked(QListBoxItem *)));
00111   connect( m_list, SIGNAL(highlighted ( QListBoxItem * ) ), this,
00112            SLOT(slotHighlighted(QListBoxItem * )));
00113   m_rangeName->setText(i18n("Area: %1").arg(""));
00114 
00115   resize( 250, 200 );
00116 }
00117 
00118 void reference::displayAreaValues(QString const & areaName)
00119 {
00120   QString tmpName;
00121   QValueList<Reference>::Iterator it;
00122   QValueList<Reference> area( m_pView->doc()->listArea() );
00123   for ( it = area.begin(); it != area.end(); ++it )
00124   {
00125     if ((*it).ref_name == areaName)
00126     {
00127       if (!m_pView->doc()->map()->findSheet( (*it).sheet_name))
00128         kdDebug(36001) << "(*it).table_name '" << (*it).sheet_name
00129                        << "' not found!*********" << endl;
00130       else
00131         tmpName = util_rangeName(m_pView->doc()->map()->findSheet( (*it).sheet_name),
00132                                  (*it).rect);
00133       break;
00134     }
00135   }
00136 
00137   tmpName = i18n("Area: %1").arg(tmpName);
00138   m_rangeName->setText(tmpName);
00139 }
00140 
00141 void reference::slotHighlighted(QListBoxItem * )
00142 {
00143   QString tmp = m_list->text(m_list->currentItem());
00144   displayAreaValues(tmp);
00145 }
00146 
00147 void reference::slotDoubleClicked(QListBoxItem *)
00148 {
00149   slotOk();
00150 }
00151 
00152 void reference::slotRemove()
00153 {
00154   if (m_list->currentItem() == -1)
00155     return;
00156 
00157   int ret = KMessageBox::warningContinueCancel( this, i18n("Do you really want to remove this area name?"),i18n("Remove Area"),KStdGuiItem::del());
00158   if (ret == KMessageBox::Cancel)
00159     return;
00160 
00161   QString textRemove;
00162   if ( m_list->currentItem() != -1)
00163   {
00164     m_pView->doc()->emitBeginOperation( false );
00165 
00166     QString textRemove = m_list->text(m_list->currentItem());
00167     m_pView->doc()->removeArea(textRemove );
00168     m_pView->doc()->setModified(true);
00169     /*
00170       m_list->clear();
00171       QString text;
00172       QValueList<Reference>::Iterator it;
00173       QValueList<Reference> area=m_pView->doc()->listArea();
00174       for ( it = area.begin(); it != area.end(); ++it )
00175       {
00176       text=(*it).ref_name;
00177       m_list->insertItem(text);
00178       }
00179     */
00180 
00181     m_list->removeItem(m_list->currentItem());
00182 
00183     Sheet *tbl;
00184 
00185     for ( tbl = m_pView->doc()->map()->firstSheet(); tbl != 0L; tbl = m_pView->doc()->map()->nextSheet() )
00186     {
00187       tbl->refreshRemoveAreaName(textRemove);
00188     }
00189 
00190     m_pView->slotUpdateView( m_pView->activeSheet() );
00191   }
00192 
00193   if ( !m_list->count() )
00194   {
00195     m_pOk->setEnabled( false );
00196     m_pRemove->setEnabled( false );
00197     m_pEdit->setEnabled( false );
00198   }
00199 }
00200 
00201 void reference::slotEdit()
00202 {
00203   QString name(m_list->text(m_list->currentItem()));
00204   if ( name.isEmpty() )
00205     return;
00206   EditAreaName editDlg( m_pView, "EditArea", name );
00207   editDlg.exec();
00208 
00209   m_rangeName->setText(i18n("Area: %1").arg(""));
00210   QString tmp = m_list->text(m_list->currentItem());
00211   if (!tmp.isEmpty())
00212     displayAreaValues( tmp );
00213 }
00214 
00215 void reference::slotOk()
00216 {
00217   m_pView->doc()->emitBeginOperation( false );
00218 
00219   QString text;
00220   if (m_list->currentItem() != -1)
00221   {
00222     int index = m_list->currentItem();
00223     text = m_list->text(index);
00224     QValueList<Reference> area = m_pView->doc()->listArea();
00225 
00226     if (m_pView->activeSheet()->sheetName() != area[ index ].sheet_name)
00227     {
00228       Sheet *sheet = m_pView->doc()->map()->findSheet(area[ index ].sheet_name);
00229       if (sheet)
00230         m_pView->setActiveSheet(sheet);
00231     }
00232 
00233     Region region(m_pView, Cell::fullName(m_pView->activeSheet(), area[index].rect.left(), area[index].rect.top()));
00234     m_pView->selectionInfo()->initialize(region);
00235     m_pView->selectionInfo()->initialize(area[ index ].rect);//,                                           m_pView->activeSheet() );
00236   }
00237 
00238   m_pView->slotUpdateView( m_pView->activeSheet() );
00239   accept();
00240 }
00241 
00242 void reference::slotCancel()
00243 {
00244   reject();
00245 }
00246 
00247 
00248 
00249 EditAreaName::EditAreaName( View * parent,
00250                                           const char * name,
00251                                           QString const & areaname )
00252     : KDialogBase( parent, name ,true,i18n( "Edit Area" ) , Ok|Cancel )
00253 {
00254   m_pView = parent;
00255 
00256   resize( 350, 142 );
00257   setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5,
00258                               (QSizePolicy::SizeType)4, 0, 0,
00259                               sizePolicy().hasHeightForWidth() ) );
00260 
00261   QWidget * page = new QWidget( this );
00262   setMainWidget(page);
00263 
00264   QGridLayout * EditAreaNameLayout
00265     = new QGridLayout( page, 1, 1, 11, 6, "EditAreaNameLayout");
00266 
00267   QHBoxLayout * Layout1 = new QHBoxLayout( 0, 0, 6, "Layout1");
00268   QSpacerItem * spacer = new QSpacerItem( 0, 0, QSizePolicy::Expanding,
00269                                           QSizePolicy::Minimum );
00270   Layout1->addItem( spacer );
00271 
00272   EditAreaNameLayout->addMultiCellLayout( Layout1, 3, 3, 0, 1 );
00273 
00274   QLabel * TextLabel4 = new QLabel( page, "TextLabel4" );
00275   TextLabel4->setText( i18n( "Cells:" ) );
00276 
00277   EditAreaNameLayout->addWidget( TextLabel4, 2, 0 );
00278 
00279   m_area = new QLineEdit( page, "m_area" );
00280 
00281   EditAreaNameLayout->addWidget( m_area, 2, 1 );
00282 
00283   QLabel * TextLabel1 = new QLabel( page, "TextLabel1" );
00284   TextLabel1->setText( i18n( "Sheet:" ) );
00285 
00286   EditAreaNameLayout->addWidget( TextLabel1, 1, 0 );
00287 
00288   m_sheets = new QComboBox( FALSE, page, "m_sheets" );
00289 
00290   EditAreaNameLayout->addWidget( m_sheets, 1, 1 );
00291 
00292   QLabel * TextLabel2 = new QLabel( page, "TextLabel2" );
00293   TextLabel2->setText( i18n( "Area name:" ) );
00294 
00295   EditAreaNameLayout->addWidget( TextLabel2, 0, 0 );
00296 
00297   m_areaName = new QLabel( page, "m_areaName" );
00298   m_areaName->setText( areaname );
00299 
00300   EditAreaNameLayout->addWidget( m_areaName, 0, 1 );
00301 
00302   QPtrList<Sheet> sheetList = m_pView->doc()->map()->sheetList();
00303   for (unsigned int c = 0; c < sheetList.count(); ++c)
00304   {
00305     Sheet * t = sheetList.at(c);
00306     if (!t)
00307       continue;
00308     m_sheets->insertItem( t->sheetName() );
00309   }
00310 
00311   QString tmpName;
00312   QValueList<Reference>::Iterator it;
00313   QValueList<Reference> area(m_pView->doc()->listArea());
00314   for ( it = area.begin(); it != area.end(); ++it )
00315   {
00316     if ((*it).ref_name == areaname)
00317     {
00318       if (!m_pView->doc()->map()->findSheet( (*it).sheet_name))
00319         kdDebug(36001) << "(*it).table_name '" << (*it).sheet_name
00320                        << "' not found!*********" << endl;
00321       else
00322         tmpName = util_rangeName( (*it).rect );
00323       break;
00324     }
00325   }
00326 
00327   m_sheets->setCurrentText( (*it).sheet_name );
00328   m_area->setText( tmpName );
00329 
00330 }
00331 
00332 EditAreaName::~EditAreaName()
00333 {
00334 }
00335 
00336 void EditAreaName::slotOk()
00337 {
00338   Range range( m_area->text() );
00339 
00340   if ( !range.isValid() )
00341   {
00342     Point point( m_area->text() );
00343     if ( !point.isValid() )
00344       return;
00345 
00346     m_area->setText( m_area->text() + ":" + m_area->text() );
00347 
00348     range = Range( m_area->text() );
00349   }
00350 
00351   m_pView->doc()->emitBeginOperation( false );
00352 
00353   m_pView->doc()->removeArea( m_areaName->text() );
00354   m_pView->doc()->addAreaName(range.range(), m_areaName->text(), m_sheets->currentText() );
00355 
00356   Sheet *sheet;
00357 
00358   for ( sheet = m_pView->doc()->map()->firstSheet(); sheet != 0L;
00359         sheet = m_pView->doc()->map()->nextSheet() )
00360   {
00361     sheet->refreshChangeAreaName( m_areaName->text() );
00362   }
00363 
00364   m_pView->slotUpdateView( m_pView->activeSheet() );
00365   accept();
00366 }
00367 
00368 #include "kspread_dlg_reference.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys