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
49#include <kstdguiitem.h>
50
51using namespace KSpread;
52
53reference::reference( View* parent, const char* name )
54 : TQDialog( parent, name,true )
55{
56 m_pView = parent;
57 TQVBoxLayout *lay1 = new TQVBoxLayout( this );
58 lay1->setMargin( KDialog::marginHint() );
59 lay1->setSpacing( KDialog::spacingHint() );
60 m_list = new TQListBox(this);
61 lay1->addWidget( m_list );
62
63 setCaption( i18n("Area Name") );
64
65 m_rangeName = new TQLabel(this);
66 lay1->addWidget(m_rangeName);
67
68 m_pRemove = new TQPushButton(i18n("&Remove"), this);
69 lay1->addWidget( m_pRemove );
70 //m_pRemove->setEnabled(false);
71 KButtonBox *bb = new KButtonBox( this );
72 // bb->addStretch();
73 m_pEdit = bb->addButton( i18n("&Edit...") );
74 m_pOk = bb->addButton( KStdGuiItem::ok() );
75 m_pCancel = bb->addButton( KStdGuiItem::cancel() );
76 m_pOk->setDefault( true );
77 bb->layout();
78 lay1->addWidget( bb );
79
80 TQString text;
81 TQStringList sheetName;
82 TQPtrListIterator<Sheet> it2 ( m_pView->doc()->map()->sheetList() );
83 for( ; it2.current(); ++it2 )
84 {
85 sheetName.append( it2.current()->sheetName());
86 }
87
88 TQValueList<Reference>::Iterator it;
89 TQValueList<Reference> area = m_pView->doc()->listArea();
90 for ( it = area.begin(); it != area.end(); ++it )
91 {
92 text = (*it).ref_name;
93 if ( sheetName.contains((*it).sheet_name ))
94 m_list->insertItem(text);
95 }
96
97 if ( !m_list->count() )
98 {
99 m_pOk->setEnabled( false );
100 m_pRemove->setEnabled( false );
101 m_pEdit->setEnabled( false );
102 }
103
104 connect( m_pOk, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotOk() ) );
105 connect( m_pCancel, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotCancel() ) );
106 connect( m_pEdit, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotEdit() ) );
107 connect( m_pRemove, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotRemove() ) );
108 connect( m_list, TQ_SIGNAL(doubleClicked(TQListBoxItem *)), this,
109 TQ_SLOT(slotDoubleClicked(TQListBoxItem *)));
110 connect( m_list, TQ_SIGNAL(highlighted ( TQListBoxItem * ) ), this,
111 TQ_SLOT(slotHighlighted(TQListBoxItem * )));
112 m_rangeName->setText(i18n("Area: %1").arg(""));
113
114 resize( 250, 200 );
115}
116
117void reference::displayAreaValues(TQString const & areaName)
118{
119 TQString tmpName;
120 TQValueList<Reference>::Iterator it;
121 TQValueList<Reference> area( m_pView->doc()->listArea() );
122 for ( it = area.begin(); it != area.end(); ++it )
123 {
124 if ((*it).ref_name == areaName)
125 {
126 if (!m_pView->doc()->map()->findSheet( (*it).sheet_name))
127 kdDebug(36001) << "(*it).table_name '" << (*it).sheet_name
128 << "' not found!*********" << endl;
129 else
130 tmpName = util_rangeName(m_pView->doc()->map()->findSheet( (*it).sheet_name),
131 (*it).rect);
132 break;
133 }
134 }
135
136 tmpName = i18n("Area: %1").arg(tmpName);
137 m_rangeName->setText(tmpName);
138}
139
140void reference::slotHighlighted(TQListBoxItem * )
141{
142 TQString tmp = m_list->text(m_list->currentItem());
143 displayAreaValues(tmp);
144}
145
146void reference::slotDoubleClicked(TQListBoxItem *)
147{
148 slotOk();
149}
150
151void reference::slotRemove()
152{
153 if (m_list->currentItem() == -1)
154 return;
155
156 int ret = KMessageBox::warningContinueCancel( this, i18n("Do you really want to remove this area name?"),i18n("Remove Area"),KStdGuiItem::del());
157 if (ret == KMessageBox::Cancel)
158 return;
159
160 TQString textRemove;
161 if ( m_list->currentItem() != -1)
162 {
163 m_pView->doc()->emitBeginOperation( false );
164
165 TQString textRemove = m_list->text(m_list->currentItem());
166 m_pView->doc()->removeArea(textRemove );
167 m_pView->doc()->setModified(true);
168 /*
169 m_list->clear();
170 TQString text;
171 TQValueList<Reference>::Iterator it;
172 TQValueList<Reference> area=m_pView->doc()->listArea();
173 for ( it = area.begin(); it != area.end(); ++it )
174 {
175 text=(*it).ref_name;
176 m_list->insertItem(text);
177 }
178 */
179
180 m_list->removeItem(m_list->currentItem());
181
182 Sheet *tbl;
183
184 for ( tbl = m_pView->doc()->map()->firstSheet(); tbl != 0L; tbl = m_pView->doc()->map()->nextSheet() )
185 {
186 tbl->refreshRemoveAreaName(textRemove);
187 }
188
189 m_pView->slotUpdateView( m_pView->activeSheet() );
190 }
191
192 if ( !m_list->count() )
193 {
194 m_pOk->setEnabled( false );
195 m_pRemove->setEnabled( false );
196 m_pEdit->setEnabled( false );
197 }
198}
199
200void reference::slotEdit()
201{
202 TQString name(m_list->text(m_list->currentItem()));
203 if ( name.isEmpty() )
204 return;
205 EditAreaName editDlg( m_pView, "EditArea", name );
206 editDlg.exec();
207
208 m_rangeName->setText(i18n("Area: %1").arg(""));
209 TQString tmp = m_list->text(m_list->currentItem());
210 if (!tmp.isEmpty())
211 displayAreaValues( tmp );
212}
213
214void reference::slotOk()
215{
216 m_pView->doc()->emitBeginOperation( false );
217
218 TQString text;
219 if (m_list->currentItem() != -1)
220 {
221 int index = m_list->currentItem();
222 text = m_list->text(index);
223 TQValueList<Reference> area = m_pView->doc()->listArea();
224
225 if (m_pView->activeSheet()->sheetName() != area[ index ].sheet_name)
226 {
227 Sheet *sheet = m_pView->doc()->map()->findSheet(area[ index ].sheet_name);
228 if (sheet)
229 m_pView->setActiveSheet(sheet);
230 }
231
232 Region region(m_pView, Cell::fullName(m_pView->activeSheet(), area[index].rect.left(), area[index].rect.top()));
233 m_pView->selectionInfo()->initialize(region);
234 m_pView->selectionInfo()->initialize(area[ index ].rect);//, m_pView->activeSheet() );
235 }
236
237 m_pView->slotUpdateView( m_pView->activeSheet() );
238 accept();
239}
240
241void reference::slotCancel()
242{
243 reject();
244}
245
246
247
248EditAreaName::EditAreaName( View * parent,
249 const char * name,
250 TQString const & areaname )
251 : KDialogBase( parent, name ,true,i18n( "Edit Area" ) , Ok|Cancel )
252{
253 m_pView = parent;
254
255 resize( 350, 142 );
256 setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)5,
257 (TQSizePolicy::SizeType)4, 0, 0,
258 sizePolicy().hasHeightForWidth() ) );
259
260 TQWidget * page = new TQWidget( this );
261 setMainWidget(page);
262
263 TQGridLayout * EditAreaNameLayout
264 = new TQGridLayout( page, 1, 1, 11, 6, "EditAreaNameLayout");
265
266 TQHBoxLayout * Layout1 = new TQHBoxLayout( 0, 0, 6, "Layout1");
267 TQSpacerItem * spacer = new TQSpacerItem( 0, 0, TQSizePolicy::Expanding,
268 TQSizePolicy::Minimum );
269 Layout1->addItem( spacer );
270
271 EditAreaNameLayout->addMultiCellLayout( Layout1, 3, 3, 0, 1 );
272
273 TQLabel * TextLabel4 = new TQLabel( page, "TextLabel4" );
274 TextLabel4->setText( i18n( "Cells:" ) );
275
276 EditAreaNameLayout->addWidget( TextLabel4, 2, 0 );
277
278 m_area = new TQLineEdit( page, "m_area" );
279
280 EditAreaNameLayout->addWidget( m_area, 2, 1 );
281
282 TQLabel * TextLabel1 = new TQLabel( page, "TextLabel1" );
283 TextLabel1->setText( i18n( "Sheet:" ) );
284
285 EditAreaNameLayout->addWidget( TextLabel1, 1, 0 );
286
287 m_sheets = new TQComboBox( false, page, "m_sheets" );
288
289 EditAreaNameLayout->addWidget( m_sheets, 1, 1 );
290
291 TQLabel * TextLabel2 = new TQLabel( page, "TextLabel2" );
292 TextLabel2->setText( i18n( "Area name:" ) );
293
294 EditAreaNameLayout->addWidget( TextLabel2, 0, 0 );
295
296 m_areaName = new TQLabel( page, "m_areaName" );
297 m_areaName->setText( areaname );
298
299 EditAreaNameLayout->addWidget( m_areaName, 0, 1 );
300
301 TQPtrList<Sheet> sheetList = m_pView->doc()->map()->sheetList();
302 for (unsigned int c = 0; c < sheetList.count(); ++c)
303 {
304 Sheet * t = sheetList.at(c);
305 if (!t)
306 continue;
307 m_sheets->insertItem( t->sheetName() );
308 }
309
310 TQString tmpName;
311 TQValueList<Reference>::Iterator it;
312 TQValueList<Reference> area(m_pView->doc()->listArea());
313 for ( it = area.begin(); it != area.end(); ++it )
314 {
315 if ((*it).ref_name == areaname)
316 {
317 if (!m_pView->doc()->map()->findSheet( (*it).sheet_name))
318 kdDebug(36001) << "(*it).table_name '" << (*it).sheet_name
319 << "' not found!*********" << endl;
320 else
321 tmpName = util_rangeName( (*it).rect );
322 break;
323 }
324 }
325
326 m_sheets->setCurrentText( (*it).sheet_name );
327 m_area->setText( tmpName );
328
329}
330
331EditAreaName::~EditAreaName()
332{
333}
334
335void EditAreaName::slotOk()
336{
337 Range range( m_area->text() );
338
339 if ( !range.isValid() )
340 {
341 Point point( m_area->text() );
342 if ( !point.isValid() )
343 return;
344
345 m_area->setText( m_area->text() + ":" + m_area->text() );
346
347 range = Range( m_area->text() );
348 }
349
350 m_pView->doc()->emitBeginOperation( false );
351
352 m_pView->doc()->removeArea( m_areaName->text() );
353 m_pView->doc()->addAreaName(range.range(), m_areaName->text(), m_sheets->currentText() );
354
355 Sheet *sheet;
356
357 for ( sheet = m_pView->doc()->map()->firstSheet(); sheet != 0L;
358 sheet = m_pView->doc()->map()->nextSheet() )
359 {
360 sheet->refreshChangeAreaName( m_areaName->text() );
361 }
362
363 m_pView->slotUpdateView( m_pView->activeSheet() );
364 accept();
365}
366
367#include "kspread_dlg_reference.moc"
TQString fullName() const
Returns the full name of the cell, i.e.
Sheet * findSheet(const TQString &_name)
searches for a sheet named _name
The View class displays a KSpread document.
The KSpread namespace.
Definition commands.h:39