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 //-----------------------------------------------------------------------------
30 #include "./uiStyleView.h"
31 #include "../qanStyle.h"
36 #include <QHeaderView>
37 #include <QColorDialog>
38 #include <QPushButton>
39 #include <QToolButton>
40 #include <QHBoxLayout>
42 #include <QItemEditorFactory>
43 #include <QFileDialog>
47 namespace qan
{ // ::qan
50 ColorEditWidget::ColorEditWidget( QWidget
* parent
, QColor color
) :
55 setAutoFillBackground( true ); // Delegate widget are transparent by default
57 QHBoxLayout
* hbox
= new QHBoxLayout( this );
59 hbox
->setSpacing( 1 );
61 _label
= new QLabel( this );
62 _label
->setText( _color
.name( ) );
64 QPushButton
* b
= new QPushButton( "...", parent
);
66 b
->setMaximumWidth( 40 );
67 connect( b
, SIGNAL( clicked() ), this, SLOT( selectColor() ) );
69 hbox
->addSpacing( 2 );
70 hbox
->addWidget( _label
);
76 void ColorEditWidget::setColor( QColor color
)
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 );
97 /*if ( _delegate != 0 )
98 emit _delegate->commitData( this );*/
102 StringEditWidget::StringEditWidget( QWidget
* parent
, QString 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
);
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() ) );
128 void StringEditWidget::setString( QString s
)
135 void StringEditWidget::selectString( )
137 QString s
= QFileDialog::getOpenFileName( this, "Choose an image file", "./", "Images (*.png *.jpg)" );
142 void StringEditWidget::editingFinished( )
144 setString( _edit
->text( ) );
148 StyleDelegate::StyleDelegate( QAbstractItemModel
& model
) :
151 setItemEditorFactory( new QItemEditorFactory( ) );
154 QWidget
* StyleDelegate::createEditor ( QWidget
* parent
, const QStyleOptionViewItem
& option
, const QModelIndex
& index
) const
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
);
170 case QVariant::Color
:
172 QColor color
= attribute
.value
< QColor
>( );
173 editor
= new ColorEditWidget( parent
, color
);
177 editor
= itemEditorFactory( )->createEditor( attribute
.type( ), parent
);
182 editor
->installEventFilter( const_cast< StyleDelegate
* >( this ) );
184 return QItemDelegate::createEditor( parent
, option
, index
);
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
);
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
);
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( ) );
219 QItemDelegate::paint( painter
, option
, index
);
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( ) );
237 case QVariant::Color
:
239 ColorEditWidget
* w
= static_cast< ColorEditWidget
* >( editor
);
240 QVariant attribute
= _model
.data( index
);
241 w
->setColor( attribute
.value
< QColor
>( ) );
245 QItemDelegate::setEditorData( editor
, index
);
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( ) );
262 case QVariant::Color
:
264 ColorEditWidget
* w
= static_cast< ColorEditWidget
* >( editor
);
265 model
->setData( index
, w
->getColor( ) );
269 QItemDelegate::setModelData( editor
, model
, index
);
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
),
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
);
335 void StyleEditor::setStyle( Style
* style
)
340 _tableView
->setDisabled( false );
341 _tableView
->setItemDelegate( new StyleDelegate( *style
) );
342 _tableView
->setModel( style
);
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( ) )
359 _style
->add( attrName
, Qt::black
);
362 _style
->add( attrName
, QString( "" ) );
375 void StyleEditor::removeAttribute( )
379 //-----------------------------------------------------------------------------