00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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_selection.h"
00035 #include "kspread_locale.h"
00036
00037 #include <qvariant.h>
00038 #include <qcombobox.h>
00039 #include <qlabel.h>
00040 #include <qlineedit.h>
00041 #include <qpushbutton.h>
00042 #include <qlayout.h>
00043
00044 #include <kbuttonbox.h>
00045 #include <kmessagebox.h>
00046 #include <kdebug.h>
00047 #include <kdeversion.h>
00048
00049 #include <kstdguiitem.h>
00050
00051 KSpreadreference::KSpreadreference( KSpreadView* parent, const char* name )
00052 : QDialog( parent, name,TRUE )
00053 {
00054 m_pView = parent;
00055 QVBoxLayout *lay1 = new QVBoxLayout( this );
00056 lay1->setMargin( KDialog::marginHint() );
00057 lay1->setSpacing( KDialog::spacingHint() );
00058 m_list = new QListBox(this);
00059 lay1->addWidget( m_list );
00060
00061 setCaption( i18n("Area Name") );
00062
00063 m_rangeName = new QLabel(this);
00064 lay1->addWidget(m_rangeName);
00065
00066 m_pRemove = new QPushButton(i18n("&Remove"), this);
00067 lay1->addWidget( m_pRemove );
00068
00069 KButtonBox *bb = new KButtonBox( this );
00070
00071 m_pEdit = bb->addButton( i18n("&Edit...") );
00072 m_pOk = bb->addButton( KStdGuiItem::ok() );
00073 m_pCancel = bb->addButton( KStdGuiItem::cancel() );
00074 m_pOk->setDefault( TRUE );
00075 bb->layout();
00076 lay1->addWidget( bb );
00077
00078 QString text;
00079 QStringList sheetName;
00080 QPtrListIterator<KSpreadSheet> it2 ( m_pView->doc()->map()->sheetList() );
00081 for( ; it2.current(); ++it2 )
00082 {
00083 sheetName.append( it2.current()->sheetName());
00084 }
00085
00086 QValueList<Reference>::Iterator it;
00087 QValueList<Reference> area = m_pView->doc()->listArea();
00088 for ( it = area.begin(); it != area.end(); ++it )
00089 {
00090 text = (*it).ref_name;
00091 if ( sheetName.contains((*it).sheet_name ))
00092 m_list->insertItem(text);
00093 }
00094
00095 if ( !m_list->count() )
00096 {
00097 m_pOk->setEnabled( false );
00098 m_pRemove->setEnabled( false );
00099 m_pEdit->setEnabled( false );
00100 }
00101
00102 connect( m_pOk, SIGNAL( clicked() ), this, SLOT( slotOk() ) );
00103 connect( m_pCancel, SIGNAL( clicked() ), this, SLOT( slotCancel() ) );
00104 connect( m_pEdit, SIGNAL( clicked() ), this, SLOT( slotEdit() ) );
00105 connect( m_pRemove, SIGNAL( clicked() ), this, SLOT( slotRemove() ) );
00106 connect( m_list, SIGNAL(doubleClicked(QListBoxItem *)), this,
00107 SLOT(slotDoubleClicked(QListBoxItem *)));
00108 connect( m_list, SIGNAL(highlighted ( QListBoxItem * ) ), this,
00109 SLOT(slotHighlighted(QListBoxItem * )));
00110 m_rangeName->setText(i18n("Area: %1").arg(""));
00111
00112 resize( 250, 200 );
00113 }
00114
00115 void KSpreadreference::displayAreaValues(QString const & areaName)
00116 {
00117 QString tmpName;
00118 QValueList<Reference>::Iterator it;
00119 QValueList<Reference> area( m_pView->doc()->listArea() );
00120 for ( it = area.begin(); it != area.end(); ++it )
00121 {
00122 if ((*it).ref_name == areaName)
00123 {
00124 if (!m_pView->doc()->map()->findSheet( (*it).sheet_name))
00125 kdDebug(36001) << "(*it).table_name '" << (*it).sheet_name
00126 << "' not found!*********" << endl;
00127 else
00128 tmpName = util_rangeName(m_pView->doc()->map()->findSheet( (*it).sheet_name),
00129 (*it).rect);
00130 break;
00131 }
00132 }
00133
00134 tmpName = i18n("Area: %1").arg(tmpName);
00135 m_rangeName->setText(tmpName);
00136 }
00137
00138 void KSpreadreference::slotHighlighted(QListBoxItem * )
00139 {
00140 QString tmp = m_list->text(m_list->currentItem());
00141 displayAreaValues(tmp);
00142 }
00143
00144 void KSpreadreference::slotDoubleClicked(QListBoxItem *)
00145 {
00146 slotOk();
00147 }
00148
00149 void KSpreadreference::slotRemove()
00150 {
00151 if (m_list->currentItem() == -1)
00152 return;
00153
00154 int ret = KMessageBox::warningContinueCancel( this, i18n("Do you really want to remove this area name?"),i18n("Remove Area"),KGuiItem(i18n("&Delete"),"editdelete"));
00155 if (ret == KMessageBox::Cancel)
00156 return;
00157
00158 QString textRemove;
00159 if ( m_list->currentItem() != -1)
00160 {
00161 m_pView->doc()->emitBeginOperation( false );
00162
00163 QString textRemove = m_list->text(m_list->currentItem());
00164 m_pView->doc()->removeArea(textRemove );
00165 m_pView->doc()->setModified(true);
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 m_list->removeItem(m_list->currentItem());
00179
00180 KSpreadSheet *tbl;
00181
00182 for ( tbl = m_pView->doc()->map()->firstSheet(); tbl != 0L; tbl = m_pView->doc()->map()->nextSheet() )
00183 {
00184 tbl->refreshRemoveAreaName(textRemove);
00185 }
00186
00187 m_pView->slotUpdateView( m_pView->activeSheet() );
00188 }
00189
00190 if ( !m_list->count() )
00191 {
00192 m_pOk->setEnabled( false );
00193 m_pRemove->setEnabled( false );
00194 m_pEdit->setEnabled( false );
00195 }
00196 }
00197
00198 void KSpreadreference::slotEdit()
00199 {
00200 QString name(m_list->text(m_list->currentItem()));
00201 if ( name.isEmpty() )
00202 return;
00203 KSpreadEditAreaName editDlg( m_pView, "EditArea", name );
00204 editDlg.exec();
00205
00206 m_rangeName->setText(i18n("Area: %1").arg(""));
00207 QString tmp = m_list->text(m_list->currentItem());
00208 if (!tmp.isEmpty())
00209 displayAreaValues( tmp );
00210 }
00211
00212 void KSpreadreference::slotOk()
00213 {
00214 m_pView->doc()->emitBeginOperation( false );
00215
00216 QString text;
00217 if (m_list->currentItem() != -1)
00218 {
00219 int index = m_list->currentItem();
00220 text = m_list->text(index);
00221 QValueList<Reference> area = m_pView->doc()->listArea();
00222
00223 if (m_pView->activeSheet()->sheetName() != area[ index ].sheet_name)
00224 {
00225 KSpreadSheet *sheet = m_pView->doc()->map()->findSheet(area[ index ].sheet_name);
00226 if (sheet)
00227 m_pView->setActiveSheet(sheet);
00228 }
00229
00230 m_pView->canvasWidget()->
00231 gotoLocation(KSpreadPoint(KSpreadCell::fullName(m_pView->activeSheet(),
00232 area[ index ].rect.left(), area[ index ].rect.top() ),
00233 m_pView->doc()->map() ) );
00234 m_pView->selectionInfo()->setSelection(area[ index ].rect.topLeft(),
00235 area[index].rect.bottomRight(),
00236 m_pView->activeSheet() );
00237 }
00238
00239 m_pView->slotUpdateView( m_pView->activeSheet() );
00240 accept();
00241 }
00242
00243 void KSpreadreference::slotCancel()
00244 {
00245 reject();
00246 }
00247
00248
00249
00250 KSpreadEditAreaName::KSpreadEditAreaName( KSpreadView * parent,
00251 const char * name,
00252 QString const & areaname )
00253 : KDialogBase( parent, name ,true,i18n( "Edit Area" ) , Ok|Cancel )
00254 {
00255 m_pView = parent;
00256
00257 resize( 350, 142 );
00258 setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5,
00259 (QSizePolicy::SizeType)4, 0, 0,
00260 sizePolicy().hasHeightForWidth() ) );
00261
00262 QWidget * page = new QWidget( this );
00263 setMainWidget(page);
00264
00265 QGridLayout * KSpreadEditAreaNameLayout
00266 = new QGridLayout( page, 1, 1, 11, 6, "KSpreadEditAreaNameLayout");
00267
00268 QHBoxLayout * Layout1 = new QHBoxLayout( 0, 0, 6, "Layout1");
00269 QSpacerItem * spacer = new QSpacerItem( 0, 0, QSizePolicy::Expanding,
00270 QSizePolicy::Minimum );
00271 Layout1->addItem( spacer );
00272
00273 KSpreadEditAreaNameLayout->addMultiCellLayout( Layout1, 3, 3, 0, 1 );
00274
00275 QLabel * TextLabel4 = new QLabel( page, "TextLabel4" );
00276 TextLabel4->setText( i18n( "Cells:" ) );
00277
00278 KSpreadEditAreaNameLayout->addWidget( TextLabel4, 2, 0 );
00279
00280 m_area = new QLineEdit( page, "m_area" );
00281
00282 KSpreadEditAreaNameLayout->addWidget( m_area, 2, 1 );
00283
00284 QLabel * TextLabel1 = new QLabel( page, "TextLabel1" );
00285 TextLabel1->setText( i18n( "Sheet:" ) );
00286
00287 KSpreadEditAreaNameLayout->addWidget( TextLabel1, 1, 0 );
00288
00289 m_sheets = new QComboBox( FALSE, page, "m_sheets" );
00290
00291 KSpreadEditAreaNameLayout->addWidget( m_sheets, 1, 1 );
00292
00293 QLabel * TextLabel2 = new QLabel( page, "TextLabel2" );
00294 TextLabel2->setText( i18n( "Area name:" ) );
00295
00296 KSpreadEditAreaNameLayout->addWidget( TextLabel2, 0, 0 );
00297
00298 m_areaName = new QLabel( page, "m_areaName" );
00299 m_areaName->setText( areaname );
00300
00301 KSpreadEditAreaNameLayout->addWidget( m_areaName, 0, 1 );
00302
00303 QPtrList<KSpreadSheet> sheetList = m_pView->doc()->map()->sheetList();
00304 for (unsigned int c = 0; c < sheetList.count(); ++c)
00305 {
00306 KSpreadSheet * t = sheetList.at(c);
00307 if (!t)
00308 continue;
00309 m_sheets->insertItem( t->sheetName() );
00310 }
00311
00312 QString tmpName;
00313 QValueList<Reference>::Iterator it;
00314 QValueList<Reference> area(m_pView->doc()->listArea());
00315 for ( it = area.begin(); it != area.end(); ++it )
00316 {
00317 if ((*it).ref_name == areaname)
00318 {
00319 if (!m_pView->doc()->map()->findSheet( (*it).sheet_name))
00320 kdDebug(36001) << "(*it).table_name '" << (*it).sheet_name
00321 << "' not found!*********" << endl;
00322 else
00323 tmpName = util_rangeName( (*it).rect );
00324 break;
00325 }
00326 }
00327
00328 m_sheets->setCurrentText( (*it).sheet_name );
00329 m_area->setText( tmpName );
00330
00331 }
00332
00333 KSpreadEditAreaName::~KSpreadEditAreaName()
00334 {
00335 }
00336
00337 void KSpreadEditAreaName::slotOk()
00338 {
00339 KSpreadRange range( m_area->text() );
00340
00341 if ( !range.isValid() )
00342 {
00343 KSpreadPoint point( m_area->text() );
00344 if ( !point.isValid() )
00345 return;
00346
00347 m_area->setText( m_area->text() + ":" + m_area->text() );
00348
00349 range = KSpreadRange( m_area->text() );
00350 }
00351
00352 m_pView->doc()->emitBeginOperation( false );
00353
00354 m_pView->doc()->removeArea( m_areaName->text() );
00355 m_pView->doc()->addAreaName(range.range, m_areaName->text(), m_sheets->currentText() );
00356
00357 KSpreadSheet *sheet;
00358
00359 for ( sheet = m_pView->doc()->map()->firstSheet(); sheet != 0L;
00360 sheet = m_pView->doc()->map()->nextSheet() )
00361 {
00362 sheet->refreshChangeAreaName( m_areaName->text() );
00363 }
00364
00365 m_pView->slotUpdateView( m_pView->activeSheet() );
00366 accept();
00367 }
00368
00369 #include "kspread_dlg_reference.moc"