22 #include <QAbstractItemView>
23 #include <QMouseEvent>
24 #include <QStandardItemModel>
25 #include <QApplication>
27 #include <QStandardItem>
29 using namespace Marble;
35 : QAbstractItemDelegate( parent )
39 view->setMouseTracking(
true );
47 const QStyleOptionViewItem& option,
48 const QModelIndex& index )
const
50 Q_ASSERT( index.isValid() );
51 QRect rect = option.rect;
52 QStyle *style = QApplication::style();
57 QStyleOption background = option;
58 style->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );
60 painter->translate( rect.topLeft() );
63 rect.moveTopLeft( QPoint( 0, 0 ) );
65 QPoint topLeft( 0, 0 );
67 QPoint topRight( rect.topRight() );
69 QRect nameRect = rect;
72 QStyleOptionButton checkBox = checkboxOption( option, index, topLeft.x(), Qt::AlignLeft );
74 style->drawControl( QStyle::CE_CheckBox, &checkBox, painter );
77 nameRect.setLeft( checkBox.rect.right() + 1 );
80 QStyleOptionButton button = buttonOption( option, index, PluginItemDelegate::About,
81 topRight.x(), Qt::AlignRight );
82 style->drawControl( QStyle::CE_PushButton, &button, painter );
83 topRight -= QPoint( button.rect.width(), 0 );
87 QStyleOptionButton button = buttonOption( option, index, PluginItemDelegate::Configure,
88 topRight.x(), Qt::AlignRight );
89 style->drawControl( QStyle::CE_PushButton, &button, painter );
90 topRight -= QPoint( button.rect.width(), 0 );
92 nameRect.setRight( button.rect.left() -1 );
96 const QIcon icon = index.data( Qt::DecorationRole ).value<QIcon>();
97 const QPixmap iconPixmap = icon.pixmap(16, 16);
99 nameRect.moveBottom( nameRect.bottom()+5 );
100 style->drawItemPixmap( painter,
105 nameRect.setLeft( nameRect.left() + 16 + 5 );
106 nameRect.moveBottom( nameRect.bottom()-5 );
109 QString name = index.data( Qt::DisplayRole ).toString();
111 style->drawItemText( painter,
113 Qt::AlignLeft | Qt::AlignVCenter,
122 const QModelIndex & index )
const
126 QStyleOptionViewItem opt = option;
127 opt.rect = QRect( 0, 0, 0, 0 );
128 QList<QSize> elementSize;
129 QStyleOptionButton checkBox = checkboxOption( opt, index );
130 elementSize.append( checkBox.rect.size() );
131 QStyleOptionButton aboutButton = buttonOption( opt, index, PluginItemDelegate::About );
132 elementSize.append( aboutButton.rect.size() );
133 QStyleOptionButton configButton = buttonOption( opt, index, PluginItemDelegate::Configure );
134 elementSize.append( configButton.rect.size() );
135 elementSize.append( nameSize( index ) );
137 foreach(
const QSize& buttonSize, elementSize ) {
138 if( buttonSize.height() > size.height() )
139 size.setHeight( buttonSize.height() );
140 size.setWidth( size.width() + buttonSize.width() );
158 const QStyleOptionViewItem &option,
159 const QModelIndex &index )
164 if ( ( event->type() == QEvent::MouseButtonRelease )
165 || ( event->type() == QEvent::MouseButtonDblClick )
166 || ( event->type() == QEvent::MouseButtonPress )
167 || ( event->type() == QEvent::MouseMove ) )
169 QMouseEvent *me =
static_cast<QMouseEvent*
>(event);
170 QPoint mousePosition = me->pos() - option.rect.topLeft();
172 if ( ( event->type() == QEvent::MouseMove )
173 && !( me->buttons() & Qt::LeftButton ) )
177 m_aboutPressedIndex = QModelIndex();
178 m_configPressedIndex = QModelIndex();
183 QRect checkRect = checkboxOption( option, index, 0, Qt::AlignLeft ).rect;
184 if ( checkRect.contains( mousePosition )
185 && ( (
event->type() == QEvent::MouseButtonDblClick )
186 || ( event->type() == QEvent::MouseButtonRelease ) ) )
189 Qt::ItemFlags flags = model->flags(index);
190 if ( !( flags & Qt::ItemIsUserCheckable ) || !( option.state & QStyle::State_Enabled )
191 || !( flags & Qt::ItemIsEnabled ) )
195 QVariant checkValue = index.data( Qt::CheckStateRole );
196 if ( !checkValue.isValid() )
200 if ( event->type() == QEvent::MouseButtonDblClick )
203 Qt::CheckState state = (
static_cast<Qt::CheckState
>( checkValue.toInt() ) == Qt::Checked
204 ? Qt::Unchecked : Qt::Checked );
205 return model->setData(index, state, Qt::CheckStateRole);
208 if ( ( event->type() == QEvent::MouseMove )
209 && !( me->buttons() & Qt::LeftButton ) )
211 m_aboutPressedIndex = QModelIndex();
212 m_configPressedIndex = QModelIndex();
216 QPoint topRight = option.rect.topRight();
220 QRect aboutRect = buttonOption( option,
222 PluginItemDelegate::About,
224 Qt::AlignRight ).rect;
225 if ( aboutRect.contains( mousePosition ) ) {
226 if ( event->type() == QEvent::MouseButtonDblClick )
228 if ( event->type() == QEvent::MouseButtonPress ) {
229 m_aboutPressedIndex = index;
230 m_configPressedIndex = QModelIndex();
233 if ( event->type() == QEvent::MouseButtonRelease ) {
234 m_aboutPressedIndex = QModelIndex();
235 m_configPressedIndex = QModelIndex();
239 if ( event->type() == QEvent::MouseMove ) {
240 if ( me->buttons() & Qt::LeftButton ) {
241 m_aboutPressedIndex = index;
242 m_configPressedIndex = QModelIndex();
246 m_aboutPressedIndex = QModelIndex();
247 m_configPressedIndex = QModelIndex();
255 m_aboutPressedIndex = QModelIndex();
257 topRight -= QPoint( aboutRect.width(), 0 );
263 QRect configRect = buttonOption( option,
265 PluginItemDelegate::Configure,
267 Qt::AlignRight ).rect;
268 if( configRect.contains( mousePosition ) ) {
269 if ( event->type() == QEvent::MouseButtonDblClick )
272 if ( event->type() == QEvent::MouseButtonPress ) {
273 m_aboutPressedIndex = QModelIndex();
274 m_configPressedIndex = index;
277 if ( event->type() == QEvent::MouseButtonRelease ) {
278 m_aboutPressedIndex = QModelIndex();
279 m_configPressedIndex = QModelIndex();
283 if ( event->type() == QEvent::MouseMove ) {
284 if ( me->buttons() & Qt::LeftButton ) {
285 m_aboutPressedIndex = QModelIndex();
286 m_configPressedIndex = index;
290 m_aboutPressedIndex = QModelIndex();
291 m_configPressedIndex = QModelIndex();
299 m_configPressedIndex = QModelIndex();
302 topRight -= QPoint( configRect.width(), 0 );
307 m_configPressedIndex = QModelIndex();
314 QStyleOptionButton PluginItemDelegate::checkboxOption(
const QStyleOptionViewItem& option,
315 const QModelIndex& index,
317 Qt::AlignmentFlag alignment )
const
319 QStyleOptionButton checkboxOption;
320 if ( index.data( Qt::CheckStateRole ).toBool() )
321 checkboxOption.state = option.state | QStyle::State_On;
323 checkboxOption.state = option.state | QStyle::State_Off;
324 QSize size = QApplication::style()->sizeFromContents( QStyle::CT_CheckBox, &option, QSize() );
325 if ( size.isEmpty() ) {
327 checkboxOption.rect.setSize( QSize( 22, 22 ) );
330 checkboxOption.rect.setSize( QSize( size.width(), size.height() ) );
332 checkboxOption.rect = alignRect( checkboxOption.rect, option.rect, position, alignment );
333 return checkboxOption;
336 QStyleOptionButton PluginItemDelegate::buttonOption(
const QStyleOptionViewItem& option,
337 const QModelIndex& index,
338 PluginItemDelegate::ButtonType type,
340 Qt::AlignmentFlag alignment )
const
342 QStyleOptionButton buttonOption;
343 buttonOption.state = option.state;
344 buttonOption.state &= ~QStyle::State_HasFocus;
346 buttonOption.rect.setTopLeft( QPoint( 0, 0 ) );
347 buttonOption.palette = option.palette;
351 if ( type == PluginItemDelegate::About ) {
352 if ( m_aboutIcon.isNull() ) {
353 buttonOption.text = tr(
"About" );
354 contentSize = buttonOption.fontMetrics.size( 0, buttonOption.text ) + QSize( 4, 4 );
357 buttonOption.icon = m_aboutIcon;
362 if ( m_aboutPressedIndex == index ) {
363 buttonOption.state |= QStyle::State_Sunken;
366 else if ( type == PluginItemDelegate::Configure ) {
367 if ( m_configIcon.isNull() ) {
368 buttonOption.text = tr(
"Configure" );
369 contentSize = buttonOption.fontMetrics.size( 0, buttonOption.text ) + QSize( 4, 4 );
372 buttonOption.icon = m_configIcon;
376 if ( m_configPressedIndex == index ) {
377 buttonOption.state |= QStyle::State_Sunken;
381 QSize buttonSize = QApplication::style()->sizeFromContents( QStyle::CT_PushButton,
384 buttonOption.rect.setSize( buttonSize );
385 buttonOption.rect = alignRect( buttonOption.rect, option.rect, position, alignment );
389 QSize PluginItemDelegate::nameSize(
const QModelIndex& index )
const
391 QString name = index.data( Qt::DisplayRole ).toString();
393 QSize nameSize( QApplication::fontMetrics().size( 0, name ) );
397 QRect PluginItemDelegate::alignRect( QRect
object,
400 Qt::AlignmentFlag alignment )
const
404 rect.setTopLeft( QPoint( 0, 0 ) );
406 if ( rect.height() < frame.height() ) {
407 rect.moveTop( ( frame.height() - rect.height() ) / 2 );
410 if ( alignment & Qt::AlignLeft ) {
411 rect.moveLeft( position );
413 else if ( alignment & Qt::AlignRight ) {
414 rect.moveRight( position );
421 #include "PluginItemDelegate.moc"
void aboutPluginClicked(const QModelIndex &index)
This signal is emitted if the user clicks on a "about"-button of an item in the view passed to the co...
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
PluginItemDelegate(QAbstractItemView *view, QObject *parent=0)
void configPluginClicked(const QModelIndex &index)
This signal is emitted if the user clicks on a "configure"-button of an item in the view passed to th...
void setAboutIcon(const QIcon &icon)
void setConfigIcon(const QIcon &icon)
const QSize iconSize(16, 16)
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)