kspread

kspread_dlg_reference.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002-2003 Norbert Andres <nandres@web.de>
3 (C) 2002 Ariya Hidayat <ariya@kde.org>
4 (C) 2002 Harri Porten <porten@kde.org>
5 (C) 2002 John Dailey <dailey@vt.edu>
6 (C) 1999-2002 Laurent Montel <montel@kde.org>
7 (C) 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
8 (C) 1998-2000 Torben Weis <weis@kde.org>
9
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
14
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
19
20 You should have received a copy of the GNU Library General Public License
21 along with this library; see the file COPYING.LIB. If not, write to
22 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24*/
25
26
27
28#include "kspread_dlg_reference.h"
29#include "kspread_canvas.h"
30#include "kspread_doc.h"
31#include "kspread_util.h"
32#include "kspread_map.h"
33#include "kspread_view.h"
34#include "kspread_sheet.h"
35#include "selection.h"
36#include "kspread_locale.h"
37
38#include <tqvariant.h>
39#include <tqcombobox.h>
40#include <tqlabel.h>
41#include <tqlineedit.h>
42#include <tqpushbutton.h>
43#include <tqlayout.h>
44
45#include <kbuttonbox.h>
46#include <tdemessagebox.h>
47#include <kdebug.h>
48#include <tdeversion.h>
49
50#include <kstdguiitem.h>
51
52using namespace KSpread;
53
54reference::reference( View* parent, const char* name )
55 : TQDialog( parent, name,true )
56{
57 m_pView = parent;
58 TQVBoxLayout *lay1 = new TQVBoxLayout( this );
59 lay1->setMargin( KDialog::marginHint() );
60 lay1->setSpacing( KDialog::spacingHint() );
61 m_list = new TQListBox(this);
62 lay1->addWidget( m_list );
63
64 setCaption( i18n("Area Name") );
65
66 m_rangeName = new TQLabel(this);
67 lay1->addWidget(m_rangeName);
68
69 m_pRemove = new TQPushButton(i18n("&Remove"), this);
70 lay1->addWidget( m_pRemove );
71 //m_pRemove->setEnabled(false);
72 KButtonBox *bb = new KButtonBox( this );
73 // bb->addStretch();
74 m_pEdit = bb->addButton( i18n("&Edit...") );
75 m_pOk = bb->addButton( KStdGuiItem::ok() );
76 m_pCancel = bb->addButton( KStdGuiItem::cancel() );
77 m_pOk->setDefault( true );
78 bb->layout();
79 lay1->addWidget( bb );
80
81 TQString text;
82 TQStringList sheetName;
83 TQPtrListIterator<Sheet> it2 ( m_pView->doc()->map()->sheetList() );
84 for( ; it2.current(); ++it2 )
85 {
86 sheetName.append( it2.current()->sheetName());
87 }
88
89 TQValueList<Reference>::Iterator it;
90 TQValueList<Reference> area = m_pView->doc()->listArea();
91 for ( it = area.begin(); it != area.end(); ++it )
92 {
93 text = (*it).ref_name;
94 if ( sheetName.contains((*it).sheet_name ))
95 m_list->insertItem(text);
96 }
97
98 if ( !m_list->count() )
99 {
100 m_pOk->setEnabled( false );
101 m_pRemove->setEnabled( false );
102 m_pEdit->setEnabled( false );
103 }
104
105 connect( m_pOk, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotOk() ) );
106 connect( m_pCancel, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotCancel() ) );
107 connect( m_pEdit, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotEdit() ) );
108 connect( m_pRemove, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotRemove() ) );
109 connect( m_list, TQ_SIGNAL(doubleClicked(TQListBoxItem *)), this,
110 TQ_SLOT(slotDoubleClicked(TQListBoxItem *)));
111 connect( m_list, TQ_SIGNAL(highlighted ( TQListBoxItem * ) ), this,
112 TQ_SLOT(slotHighlighted(TQListBoxItem * )));
113 m_rangeName->setText(i18n("Area: %1").arg(""));
114
115 resize( 250, 200 );
116}
117
118void reference::displayAreaValues(TQString const & areaName)
119{
120 TQString tmpName;
121 TQValueList<Reference>::Iterator it;
122 TQValueList<Reference> area( m_pView->doc()->listArea() );
123 for ( it = area.begin(); it != area.end(); ++it )
124 {
125 if ((*it).ref_name == areaName)
126 {
127 if (!m_pView->doc()->map()->findSheet( (*it).sheet_name))
128 kdDebug(36001) << "(*it).table_name '" << (*it).sheet_name
129 << "' not found!*********" << endl;
130 else
131 tmpName = util_rangeName(m_pView->doc()->map()->findSheet( (*it).sheet_name),
132 (*it).rect);
133 break;
134 }
135 }
136
137 tmpName = i18n("Area: %1").arg(tmpName);
138 m_rangeName->setText(tmpName);
139}
140
141void reference::slotHighlighted(TQListBoxItem * )
142{
143 TQString tmp = m_list->text(m_list->currentItem());
144 displayAreaValues(tmp);
145}
146
147void reference::slotDoubleClicked(TQListBoxItem *)
148{
149 slotOk();
150}
151
152void reference::slotRemove()
153{
154 if (m_list->currentItem() == -1)
155 return;
156
157 int ret = KMessageBox::warningContinueCancel( this, i18n("Do you really want to remove this area name?"),i18n("Remove Area"),KStdGuiItem::del());
158 if (ret == KMessageBox::Cancel)
159 return;
160
161 TQString textRemove;
162 if ( m_list->currentItem() != -1)
163 {
164 m_pView->doc()->emitBeginOperation( false );
165
166 TQString textRemove = m_list->text(m_list->currentItem());
167 m_pView->doc()->removeArea(textRemove );
168 m_pView->doc()->setModified(true);
169 /*
170 m_list->clear();
171 TQString text;
172 TQValueList<Reference>::Iterator it;
173 TQValueList<Reference> area=m_pView->doc()->listArea();
174 for ( it = area.begin(); it != area.end(); ++it )
175 {
176 text=(*it).ref_name;
177 m_list->insertItem(text);
178 }
179 */
180
181 m_list->removeItem(m_list->currentItem());
182
183 Sheet *tbl;
184
185 for ( tbl = m_pView->doc()->map()->firstSheet(); tbl != 0L; tbl = m_pView->doc()->map()->nextSheet() )
186 {
187 tbl->refreshRemoveAreaName(textRemove);
188 }
189
190 m_pView->slotUpdateView( m_pView->activeSheet() );
191 }
192
193 if ( !m_list->count() )
194 {
195 m_pOk->setEnabled( false );
196 m_pRemove->setEnabled( false );
197 m_pEdit->setEnabled( false );
198 }
199}
200
201void reference::slotEdit()
202{
203 TQString name(m_list->text(m_list->currentItem()));
204 if ( name.isEmpty() )
205 return;
206 EditAreaName editDlg( m_pView, "EditArea", name );
207 editDlg.exec();
208
209 m_rangeName->setText(i18n("Area: %1").arg(""));
210 TQString tmp = m_list->text(m_list->currentItem());
211 if (!tmp.isEmpty())
212 displayAreaValues( tmp );
213}
214
215void reference::slotOk()
216{
217 m_pView->doc()->emitBeginOperation( false );
218
219 TQString text;
220 if (m_list->currentItem() != -1)
221 {
222 int index = m_list->currentItem();
223 text = m_list->text(index);
224 TQValueList<Reference> area = m_pView->doc()->listArea();
225
226 if (m_pView->activeSheet()->sheetName() != area[ index ].sheet_name)
227 {
228 Sheet *sheet = m_pView->doc()->map()->findSheet(area[ index ].sheet_name);
229 if (sheet)
230 m_pView->setActiveSheet(sheet);
231 }
232
233 Region region(m_pView, Cell::fullName(m_pView->activeSheet(), area[index].rect.left(), area[index].rect.top()));
234 m_pView->selectionInfo()->initialize(region);
235 m_pView->selectionInfo()->initialize(area[ index ].rect);//, m_pView->activeSheet() );
236 }
237
238 m_pView->slotUpdateView( m_pView->activeSheet() );
239 accept();
240}
241
242void reference::slotCancel()
243{
244 reject();
245}
246
247
248
249EditAreaName::EditAreaName( View * parent,
250 const char * name,
251 TQString const & areaname )
252 : KDialogBase( parent, name ,true,i18n( "Edit Area" ) , Ok|Cancel )
253{
254 m_pView = parent;
255
256 resize( 350, 142 );
257 setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)5,
258 (TQSizePolicy::SizeType)4, 0, 0,
259 sizePolicy().hasHeightForWidth() ) );
260
261 TQWidget * page = new TQWidget( this );
262 setMainWidget(page);
263
264 TQGridLayout * EditAreaNameLayout
265 = new TQGridLayout( page, 1, 1, 11, 6, "EditAreaNameLayout");
266
267 TQHBoxLayout * Layout1 = new TQHBoxLayout( 0, 0, 6, "Layout1");
268 TQSpacerItem * spacer = new TQSpacerItem( 0, 0, TQSizePolicy::Expanding,
269 TQSizePolicy::Minimum );
270 Layout1->addItem( spacer );
271
272 EditAreaNameLayout->addMultiCellLayout( Layout1, 3, 3, 0, 1 );
273
274 TQLabel * TextLabel4 = new TQLabel( page, "TextLabel4" );
275 TextLabel4->setText( i18n( "Cells:" ) );
276
277 EditAreaNameLayout->addWidget( TextLabel4, 2, 0 );
278
279 m_area = new TQLineEdit( page, "m_area" );
280
281 EditAreaNameLayout->addWidget( m_area, 2, 1 );
282
283 TQLabel * TextLabel1 = new TQLabel( page, "TextLabel1" );
284 TextLabel1->setText( i18n( "Sheet:" ) );
285
286 EditAreaNameLayout->addWidget( TextLabel1, 1, 0 );
287
288 m_sheets = new TQComboBox( false, page, "m_sheets" );
289
290 EditAreaNameLayout->addWidget( m_sheets, 1, 1 );
291
292 TQLabel * TextLabel2 = new TQLabel( page, "TextLabel2" );
293 TextLabel2->setText( i18n( "Area name:" ) );
294
295 EditAreaNameLayout->addWidget( TextLabel2, 0, 0 );
296
297 m_areaName = new TQLabel( page, "m_areaName" );
298 m_areaName->setText( areaname );
299
300 EditAreaNameLayout->addWidget( m_areaName, 0, 1 );
301
302 TQPtrList<Sheet> sheetList = m_pView->doc()->map()->sheetList();
303 for (unsigned int c = 0; c < sheetList.count(); ++c)
304 {
305 Sheet * t = sheetList.at(c);
306 if (!t)
307 continue;
308 m_sheets->insertItem( t->sheetName() );
309 }
310
311 TQString tmpName;
312 TQValueList<Reference>::Iterator it;
313 TQValueList<Reference> area(m_pView->doc()->listArea());
314 for ( it = area.begin(); it != area.end(); ++it )
315 {
316 if ((*it).ref_name == areaname)
317 {
318 if (!m_pView->doc()->map()->findSheet( (*it).sheet_name))
319 kdDebug(36001) << "(*it).table_name '" << (*it).sheet_name
320 << "' not found!*********" << endl;
321 else
322 tmpName = util_rangeName( (*it).rect );
323 break;
324 }
325 }
326
327 m_sheets->setCurrentText( (*it).sheet_name );
328 m_area->setText( tmpName );
329
330}
331
332EditAreaName::~EditAreaName()
333{
334}
335
336void EditAreaName::slotOk()
337{
338 Range range( m_area->text() );
339
340 if ( !range.isValid() )
341 {
342 Point point( m_area->text() );
343 if ( !point.isValid() )
344 return;
345
346 m_area->setText( m_area->text() + ":" + m_area->text() );
347
348 range = Range( m_area->text() );
349 }
350
351 m_pView->doc()->emitBeginOperation( false );
352
353 m_pView->doc()->removeArea( m_areaName->text() );
354 m_pView->doc()->addAreaName(range.range(), m_areaName->text(), m_sheets->currentText() );
355
356 Sheet *sheet;
357
358 for ( sheet = m_pView->doc()->map()->firstSheet(); sheet != 0L;
359 sheet = m_pView->doc()->map()->nextSheet() )
360 {
361 sheet->refreshChangeAreaName( m_areaName->text() );
362 }
363
364 m_pView->slotUpdateView( m_pView->activeSheet() );
365 accept();
366}
367
368#include "kspread_dlg_reference.moc"
TQString fullName() const
Returns the full name of the cell, i.e.
Represents the position of a single cell in a workbook.
Represents a region within a workbook.
The one for all class for points and ranges.
Definition region.h:44
The View class displays a KSpread document.
The KSpread namespace.
Definition commands.h:39