0.3.1
[qanava.git] / src / ui / uiStyleView.cpp
blob815a9f754bc7fc991534e30994d54c7dfed5a5c1
1 /*
2 Qanava - Graph drawing library for QT
3 Copyright (C) 2006 Benoit AUTHEMAN
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 //-----------------------------------------------------------------------------
21 // This file is a part of the Qanava software.
23 // \file uiStyleView.cpp
24 // \author Benoit Autheman (benoit@libqanava.org)
25 // \date 2005 December 23
26 //-----------------------------------------------------------------------------
29 // Qanava headers
30 #include "./uiStyleView.h"
31 #include "../qanStyle.h"
34 // QT headers
35 #include <QToolBar>
36 #include <QHeaderView>
37 #include <QColorDialog>
38 #include <QPushButton>
39 #include <QToolButton>
40 #include <QHBoxLayout>
41 #include <QPainter>
42 #include <QItemEditorFactory>
43 #include <QFileDialog>
44 #include <QToolBar>
47 namespace qan { // ::qan
50 ColorEditWidget::ColorEditWidget( QWidget* parent, QColor color ) :
51 QWidget( parent ),
52 _label( 0 ),
53 _color( color )
55 setAutoFillBackground( true ); // Delegate widget are transparent by default
57 QHBoxLayout* hbox = new QHBoxLayout( this );
58 hbox->setMargin( 0 );
59 hbox->setSpacing( 1 );
61 _label = new QLabel( this );
62 _label->setText( _color.name( ) );
64 QPushButton* b = new QPushButton( "...", parent );
65 b->setText( "..." );
66 b->setMaximumWidth( 40 );
67 connect( b, SIGNAL( clicked() ), this, SLOT( selectColor() ) );
69 hbox->addSpacing( 2 );
70 hbox->addWidget( _label );
71 hbox->addWidget( b );
73 setColor( color );
76 void ColorEditWidget::setColor( QColor color )
78 _color = color;
80 QPalette palette;
81 palette.setColor( backgroundRole( ), _color );
82 setPalette( palette );
84 QColor colorHsv = color.toHsv( );
85 QColor textColor( colorHsv.saturation( ) < 127 ? Qt::black : Qt::white );
86 palette = _label->palette( );
87 palette.setColor( QPalette::Text, textColor );
88 _label->setPalette( palette );
89 _label->setText( _color.name( ) );
92 void ColorEditWidget::selectColor( )
94 QColor color = QColorDialog::getColor( _color, this );
95 setColor( color );
96 close( );
97 /*if ( _delegate != 0 )
98 emit _delegate->commitData( this );*/
102 StringEditWidget::StringEditWidget( QWidget* parent, QString s ) :
103 QWidget( parent ),
104 _edit( 0 ),
105 _string( s )
107 setAutoFillBackground( true ); // Delegate widget are transparent by default
109 QHBoxLayout* hbox = new QHBoxLayout( this );
110 hbox->setMargin( 0 );
111 hbox->setSpacing( 1 );
113 _edit = new QLineEdit( this );
114 _edit->setFrame( false );
116 QPushButton* b = new QPushButton( "...", parent );
117 b->setText( "..." );
118 b->setMaximumWidth( 40 );
119 connect( b, SIGNAL( clicked() ), this, SLOT( selectString() ) );
121 hbox->addSpacing( 2 );
122 hbox->addWidget( _edit );
123 hbox->addWidget( b );
124 connect( _edit, SIGNAL( editingFinished() ), this, SLOT( editingFinished() ) );
125 setString( s );
128 void StringEditWidget::setString( QString s )
130 _edit->setText( s );
131 _edit->selectAll( );
132 _string = s;
135 void StringEditWidget::selectString( )
137 QString s = QFileDialog::getOpenFileName( this, "Choose an image file", "./", "Images (*.png *.jpg)" );
138 if ( !s.isEmpty( ) )
139 setString( s );
142 void StringEditWidget::editingFinished( )
144 setString( _edit->text( ) );
148 StyleDelegate::StyleDelegate( QAbstractItemModel& model ) :
149 _model( model )
151 setItemEditorFactory( new QItemEditorFactory( ) );
154 QWidget* StyleDelegate::createEditor ( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
156 QWidget* editor = 0;
158 QPalette palette( parent->palette( ) );
160 QVariant attribute = _model.data( index );
161 switch ( attribute.type( ) )
163 case QVariant::String:
165 QString s = attribute.toString( );
166 editor = new StringEditWidget( parent, s );
167 editor->setPalette( palette );
169 break;
170 case QVariant::Color:
172 QColor color = attribute.value< QColor >( );
173 editor = new ColorEditWidget( parent, color );
175 break;
176 default:
177 editor = itemEditorFactory( )->createEditor( attribute.type( ), parent );
178 break;
181 if ( editor != 0 )
182 editor->installEventFilter( const_cast< StyleDelegate* >( this ) );
183 else
184 return QItemDelegate::createEditor( parent, option, index );
186 return editor;
189 void StyleDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
191 if ( !index.isValid( ) || index.row( ) == 0 || index.column( ) != 1 )
193 QItemDelegate::paint( painter, option, index );
194 return;
197 QVariant attribute = _model.data( index );
198 switch ( attribute.type( ) )
200 case QVariant::Color:
202 QColor color = attribute.value< QColor >( );
204 painter->save( ); // StyleOption option does not work for back color
205 painter->setPen( Qt::NoPen );
206 painter->setBrush( color );
207 painter->drawRect( option.rect );
208 painter->restore( );
210 // Invert text color to avoid text beeing unreadable
211 QColor colorHsv = color.toHsv( );
212 QColor textColor( colorHsv.saturation( ) < 127 ? Qt::black : Qt::white );
213 QStyleOptionViewItem customOption( option );
214 customOption.palette.setColor( QPalette::Text, textColor );
215 drawDisplay( painter, customOption, option.rect, color.name( ) );
216 break;
218 default:
219 QItemDelegate::paint( painter, option, index );
220 break;
224 void StyleDelegate::setEditorData( QWidget* editor, const QModelIndex& index ) const
226 QVariant attribute = _model.data( index );
228 switch ( attribute.type( ) )
230 case QVariant::String:
232 StringEditWidget* w = static_cast< StringEditWidget* >( editor );
233 QVariant attribute = _model.data( index );
234 w->setString( attribute.toString( ) );
236 break;
237 case QVariant::Color:
239 ColorEditWidget* w = static_cast< ColorEditWidget* >( editor );
240 QVariant attribute = _model.data( index );
241 w->setColor( attribute.value< QColor >( ) );
243 break;
244 default:
245 QItemDelegate::setEditorData( editor, index );
246 break;
250 void StyleDelegate::setModelData( QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const
252 QVariant attribute = model->data( index );
254 switch ( attribute.type( ) )
256 case QVariant::String:
258 StringEditWidget* w = static_cast< StringEditWidget* >( editor );
259 model->setData( index, w->getString( ) );
261 break;
262 case QVariant::Color:
264 ColorEditWidget* w = static_cast< ColorEditWidget* >( editor );
265 model->setData( index, w->getColor( ) );
267 break;
268 default:
269 QItemDelegate::setModelData( editor, model, index );
270 break;
274 QSize StyleDelegate::sizeHint ( const QStyleOptionViewItem& option, const QModelIndex& index ) const
276 return QSize( 50, 15 );
281 //-----------------------------------------------------------------------------
282 StyleEditor::StyleEditor( QWidget* parent, Style* style ) :
283 QMainWindow( parent ),
284 _style( style ),
285 _tableView( 0 ),
286 _cbType( 0 )
288 if ( layout( ) != 0 )
290 layout( )->setMargin( 0 );
291 layout( )->setSpacing( 1 );
293 QToolBar* toolBar = addToolBar( "Tools" );
294 if ( toolBar->layout( ) != 0 )
295 toolBar->layout( )->setMargin( 2 );
296 toolBar->setIconSize( QSize( 16, 16 ) );
298 _cbType = new QComboBox( toolBar );
299 _cbType->addItem( QIcon(), "<< Type >>" );
300 _cbType->addItem( QIcon(), "Color" );
301 _cbType->addItem( QIcon(), "String" );
302 _cbType->addItem( QIcon(), "Image" );
303 _cbType->addItem( QIcon(), "Int" );
304 _cbType->addItem( QIcon(), "Double" );
306 QToolButton* addAttribute = new QToolButton( toolBar );
307 addAttribute->setIcon( QIcon( "images/qanava_plus.png" ) );
308 addAttribute->setAutoRaise( true );
309 connect( addAttribute, SIGNAL( clicked() ), this, SLOT( addAttribute() ) );
311 QToolButton* removeAttribute = new QToolButton( parent );
312 removeAttribute->setIcon( QIcon( "images/qanava_minus.png" ) );
313 removeAttribute->setAutoRaise( true );
314 connect( removeAttribute, SIGNAL( clicked() ), this, SLOT( removeAttribute() ) );
316 toolBar->addWidget( _cbType );
317 //toolBar->addSeparator( );
318 toolBar->addWidget( addAttribute );
319 toolBar->addWidget( removeAttribute );
321 _tableView = new QTableView( this );
322 _tableView->setAlternatingRowColors( true );
323 _tableView->setMaximumWidth( 200 );
324 _tableView->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
325 _tableView->horizontalHeader( )->resizeSections( QHeaderView::Fixed ); // Fixed
326 _tableView->horizontalHeader( )->setStretchLastSection( true );
327 _tableView->verticalHeader( )->resizeSections( QHeaderView::Fixed );
328 _tableView->verticalHeader( )->setDefaultSectionSize( 20 );
329 _tableView->verticalHeader( )->hide( );
331 setCentralWidget( _tableView );
332 setStyle( style );
335 void StyleEditor::setStyle( Style* style )
337 if ( style != 0 )
339 _style = style;
340 _tableView->setDisabled( false );
341 _tableView->setItemDelegate( new StyleDelegate( *style ) );
342 _tableView->setModel( style );
344 else
346 _style = 0;
347 _tableView->reset( );
348 _tableView->setDisabled( true );
352 void StyleEditor::addAttribute( )
354 QString attrName( "New attribute " );
355 attrName += QString::number( _style->size( ) );
356 switch ( _cbType->currentIndex( ) )
358 case 1: // Color
359 _style->add( attrName, Qt::black );
360 break;
361 case 2: // String
362 _style->add( attrName, QString( "" ) );
363 break;
364 case 3: // Image
365 break;
366 case 4: // Int
367 break;
368 case 5: // Double
369 break;
370 default:
371 break;
375 void StyleEditor::removeAttribute( )
379 //-----------------------------------------------------------------------------
382 } // ::qan