kspread

kspread_dlg_styles.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Laurent Montel <montel@kde.org>
3 (C) 2003 Norbert Andres <nandres@web.de>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <tqheader.h>
22#include <tqlayout.h>
23#include <tqmap.h>
24
25#include <kcombobox.h>
26#include <kdebug.h>
27#include <tdelistview.h>
28#include <tdelocale.h>
29
30#include "kspread_canvas.h"
31#include "kspread_cell.h"
32#include "kspread_dlg_layout.h"
33#include "kspread_sheet.h"
34#include "kspread_style.h"
35#include "kspread_style_manager.h"
36#include "kspread_view.h"
37
38#include "kspread_dlg_styles.h"
39
40using namespace KSpread;
41
42StyleWidget::StyleWidget( TQWidget * parent, const char * name, WFlags fl )
43 : TQWidget( parent, name, fl )
44{
45 TQVBoxLayout * layout = new TQVBoxLayout( this, 11, 6, "layout");
46
47 m_styleList = new TDEListView( this, "m_styleList" );
48 m_styleList->addColumn( i18n( "Styles" ) );
49 m_styleList->setResizeMode( TDEListView::AllColumns );
50 layout->addWidget( m_styleList );
51
52 m_displayBox = new KComboBox( false, this, "m_displayBox" );
53 layout->addWidget( m_displayBox );
54
55 m_styleList->header()->setLabel( 0, i18n( "Styles" ) );
56 m_displayBox->clear();
57 m_displayBox->insertItem( i18n( "All Styles" ) );
58 m_displayBox->insertItem( i18n( "Applied Styles" ) );
59 m_displayBox->insertItem( i18n( "Custom Styles" ) );
60 m_displayBox->insertItem( i18n( "Hierarchical" ) );
61 connect( m_styleList, TQ_SIGNAL(doubleClicked ( TQListViewItem *)),this, TQ_SIGNAL( modifyStyle()));
62 resize( TQSize(446, 384).expandedTo(minimumSizeHint()) );
63}
64
65StyleWidget::~StyleWidget()
66{
67}
68
69
70
71StyleDlg::StyleDlg( View * parent, StyleManager * manager,
72 const char * name )
73 : KDialogBase( parent, name, true, "",
74 KDialogBase::Ok | KDialogBase::User1 | KDialogBase::User2 | KDialogBase::User3 | KDialogBase::Close,
75 KDialogBase::Ok, false, KGuiItem( i18n( "&New..." ) ), KGuiItem( i18n( "&Modify..." ) ), KGuiItem( i18n( "&Delete" ) ) ),
76 m_view( parent ),
77 m_styleManager( manager ),
78 m_dlg( new StyleWidget( this ) )
79{
80 setCaption( i18n( "Style Manager" ) );
81 setButtonBoxOrientation(TQt::Vertical );
82 setMainWidget( m_dlg );
83
84 slotDisplayMode( 0 );
85 enableButton( KDialogBase::User1, true );
86 enableButton( KDialogBase::User2, true );
87 enableButton( KDialogBase::User3, false );
88
89 connect( m_dlg->m_styleList, TQ_SIGNAL( selectionChanged( TQListViewItem * ) ),
90 this, TQ_SLOT( slotSelectionChanged( TQListViewItem * ) ) );
91 connect( m_dlg->m_displayBox, TQ_SIGNAL( activated( int ) ), this, TQ_SLOT( slotDisplayMode( int ) ) );
92 connect( this, TQ_SIGNAL( user3Clicked() ), this, TQ_SLOT( slotUser3() ) );
93 connect( m_dlg, TQ_SIGNAL( modifyStyle() ), this, TQ_SLOT( slotUser2()));
94}
95
96StyleDlg::~StyleDlg()
97{
98}
99
100void StyleDlg::fillComboBox()
101{
102 class Map : public TQMap<CustomStyle *, TDEListViewItem *> {};
103 Map entries;
104
105 entries.clear();
106 entries[m_styleManager->defaultStyle()] = new TDEListViewItem( m_dlg->m_styleList, i18n( "Default" ) );
107
108 StyleManager::Styles::const_iterator iter = m_styleManager->m_styles.begin();
109 StyleManager::Styles::const_iterator end = m_styleManager->m_styles.end();
110 uint count = m_styleManager->m_styles.count() + 1;
111
112 while ( entries.count() != count )
113 {
114 if ( entries.find( iter.data() ) == entries.end() )
115 {
116 if ( iter.data()->parent() == 0 )
117 entries[iter.data()] = new TDEListViewItem( m_dlg->m_styleList, iter.data()->name() );
118 else
119 {
120 Map::const_iterator i = entries.find( iter.data()->parent() );
121 if ( i != entries.end() )
122 entries[iter.data()] = new TDEListViewItem( i.data(), iter.data()->name() );
123 }
124 }
125
126 ++iter;
127 if ( iter == end )
128 iter = m_styleManager->m_styles.begin();
129 }
130 entries.clear();
131}
132
133void StyleDlg::slotDisplayMode( int mode )
134{
135 m_dlg->m_styleList->clear();
136
137 if ( mode != 3 )
138 m_dlg->m_styleList->setRootIsDecorated( false );
139 else
140 {
141 m_dlg->m_styleList->setRootIsDecorated( true );
142 fillComboBox();
143 return;
144 }
145
146 if ( mode != 2 )
147 new TDEListViewItem( m_dlg->m_styleList, i18n( "Default" ) );
148
149 StyleManager::Styles::iterator iter = m_styleManager->m_styles.begin();
150 StyleManager::Styles::iterator end = m_styleManager->m_styles.end();
151
152 while ( iter != end )
153 {
154 CustomStyle * styleData = iter.data();
155 if ( !styleData || styleData->name().isEmpty() )
156 {
157 ++iter;
158 continue;
159 }
160
161 if ( mode == 2 )
162 {
163 if ( styleData->type() == Style::CUSTOM )
164 new TDEListViewItem( m_dlg->m_styleList, styleData->name() );
165 }
166 else if ( mode == 1 )
167 {
168 if ( styleData->usage() > 0 )
169 new TDEListViewItem( m_dlg->m_styleList, styleData->name() );
170 }
171 else
172 new TDEListViewItem( m_dlg->m_styleList, styleData->name() );
173
174 ++iter;
175 }
176}
177
178void StyleDlg::slotOk()
179{
180 TDEListViewItem * item = (TDEListViewItem *) m_dlg->m_styleList->currentItem();
181
182 if ( !item )
183 {
184 accept();
185 return;
186 }
187
188 CustomStyle * s = 0;
189
190 TQString name( item->text( 0 ) );
191 if ( name == i18n( "Default" ) )
192 s = m_styleManager->defaultStyle();
193 else
194 s = m_styleManager->style( name );
195
196 if ( !s )
197 {
198 accept();
199 return;
200 }
201
202 if ( m_view )
203 {
204 Sheet * sheet = m_view->activeSheet();
205
206 if ( sheet )
207 {
208 m_view->doc()->emitBeginOperation( false );
209 sheet->setSelectionStyle( m_view->selectionInfo(), s );
210 }
211
212 m_view->slotUpdateView( m_view->activeSheet() );
213 }
214 accept();
215}
216
217void StyleDlg::slotUser1()
218{
219 CustomStyle * s = 0;
220
221 TDEListViewItem * item = (TDEListViewItem *) m_dlg->m_styleList->currentItem();
222
223 if ( item )
224 {
225 TQString name( item->text( 0 ) );
226 if ( name == i18n( "Default" ) )
227 s = m_styleManager->defaultStyle();
228 else
229 s = m_styleManager->style( name );
230 }
231 else
232 s = m_styleManager->defaultStyle();
233
234 int i = 1;
235 TQString newName( i18n( "style%1" ).arg( m_styleManager->count() + i ) );
236 while ( m_styleManager->style( newName ) != 0 )
237 {
238 ++i;
239 newName = i18n( "style%1" ).arg( m_styleManager->count() + i );
240 }
241
242 CustomStyle * style = new CustomStyle( newName, s );
243 style->setType( Style::TENTATIVE );
244
245 CellFormatDialog dlg( m_view, style, m_styleManager, m_view->doc() );
246
247 if ( style->type() == Style::TENTATIVE )
248 {
249 delete style;
250 return;
251 }
252
253 m_styleManager->m_styles[ style->name() ] = style;
254
255 slotDisplayMode( m_dlg->m_displayBox->currentItem() );
256}
257
258void StyleDlg::slotUser2()
259{
260 TDEListViewItem * item = (TDEListViewItem *) m_dlg->m_styleList->currentItem();
261
262 if ( !item )
263 return;
264
265 CustomStyle * s = 0;
266
267 TQString name( item->text( 0 ) );
268 if ( name == i18n( "Default" ) )
269 s = m_styleManager->defaultStyle();
270 else
271 s = m_styleManager->style( name );
272
273 if ( !s )
274 return;
275
276 CellFormatDialog dlg( m_view, s, m_styleManager, m_view->doc() );
277 slotDisplayMode( m_dlg->m_displayBox->currentItem() );
278}
279
280void StyleDlg::slotUser3()
281{
282 TDEListViewItem * item = (TDEListViewItem *) m_dlg->m_styleList->currentItem();
283
284 if ( !item )
285 return;
286
287 CustomStyle * s = 0;
288
289 TQString name( item->text( 0 ) );
290 if ( name == i18n( "Default" ) )
291 s = m_styleManager->defaultStyle();
292 else
293 s = m_styleManager->style( name );
294
295 if ( !s )
296 return;
297
298 if ( s->type() != Style::CUSTOM )
299 return;
300
301 s->setType( Style::AUTO );
302 m_styleManager->takeStyle( s );
303
304 slotDisplayMode( m_dlg->m_displayBox->currentItem() );
305}
306
307void StyleDlg::slotSelectionChanged( TQListViewItem * item )
308{
309 if ( !item )
310 return;
311
312 CustomStyle* style = 0;
313 TQString name( item->text( 0 ) );
314 if ( name == i18n( "Default" ) )
315 style = m_styleManager->defaultStyle();
316 else
317 style = m_styleManager->style( name );
318 if ( !style )
319 {
320 enableButton( KDialogBase::User3, false );
321 return;
322 }
323
324 if ( style->type() == Style::BUILTIN )
325 enableButton( KDialogBase::User3, false );
326 else
327 enableButton( KDialogBase::User3, true );
328}
329
330
331#include "kspread_dlg_styles.moc"
332
Dialog for the "Cell Format..." action.
Built-in or custom style defined in StyleManager dialog.
A map is a simple container for all sheets.
Definition kspread_map.h:59
int count() const
@ BUILTIN
built-in style (the default style)
@ AUTO
automatically generated on cell format changes
@ CUSTOM
custom style (defined in the StyleManager dialog)
int usage() const
The View class displays a KSpread document.
The KSpread namespace.
Definition commands.h:39