Warning, file /office/calligra/libs/widgets/KoResourceSelector.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Library General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Library General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Library General Public License 0015 * along with this library; see the file COPYING.LIB. If not, write to 0016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "KoResourceSelector.h" 0021 #include <KoResourceServerAdapter.h> 0022 #include <KoResourceModel.h> 0023 #include <KoResourceItemView.h> 0024 #include <KoResourceItemDelegate.h> 0025 #include <QPainter> 0026 #include <QTableView> 0027 #include <QListView> 0028 #include <QHeaderView> 0029 #include <QMouseEvent> 0030 #include <QStyledItemDelegate> 0031 0032 #include <WidgetsDebug.h> 0033 0034 class Q_DECL_HIDDEN KoResourceSelector::Private 0035 { 0036 public: 0037 Private() : displayMode(ImageMode) {} 0038 DisplayMode displayMode; 0039 0040 void updateIndex( KoResourceSelector * me ) 0041 { 0042 KoResourceModel * resourceModel = qobject_cast<KoResourceModel*>(me->model()); 0043 if (!resourceModel) 0044 return; 0045 if (!resourceModel->rowCount()) 0046 return; 0047 0048 int currentIndex = me->currentIndex(); 0049 QModelIndex currentModelIndex = me->view()->currentIndex(); 0050 0051 if (currentIndex < 0 || !currentModelIndex.isValid()) { 0052 me->blockSignals(true); 0053 me->view()->setCurrentIndex( resourceModel->index( 0, 0 ) ); 0054 me->setCurrentIndex(0); 0055 me->blockSignals(false); 0056 me->update(); 0057 } 0058 } 0059 }; 0060 0061 KoResourceSelector::KoResourceSelector(QWidget * parent) 0062 : QComboBox( parent ), d( new Private() ) 0063 { 0064 connect( this, SIGNAL(currentIndexChanged(int)), 0065 this, SLOT(indexChanged(int)) ); 0066 0067 setMouseTracking(true); 0068 } 0069 0070 KoResourceSelector::KoResourceSelector( QSharedPointer<KoAbstractResourceServerAdapter> resourceAdapter, QWidget * parent ) 0071 : QComboBox( parent ), d( new Private() ) 0072 { 0073 Q_ASSERT(resourceAdapter); 0074 0075 setView( new KoResourceItemView(this) ); 0076 setModel( new KoResourceModel(resourceAdapter, this) ); 0077 setItemDelegate( new KoResourceItemDelegate( this ) ); 0078 setMouseTracking(true); 0079 d->updateIndex(this); 0080 0081 connect( this, SIGNAL(currentIndexChanged(int)), 0082 this, SLOT(indexChanged(int)) ); 0083 0084 connect(resourceAdapter.data(), SIGNAL(resourceAdded(KoResource*)), 0085 this, SLOT(resourceAdded(KoResource*))); 0086 connect(resourceAdapter.data(), SIGNAL(removingResource(KoResource*)), 0087 this, SLOT(resourceRemoved(KoResource*))); 0088 } 0089 0090 KoResourceSelector::~KoResourceSelector() 0091 { 0092 delete d; 0093 } 0094 0095 void KoResourceSelector::paintEvent( QPaintEvent *pe ) 0096 { 0097 QComboBox::paintEvent( pe ); 0098 0099 if (d->displayMode == ImageMode) { 0100 QStyleOptionComboBox option; 0101 option.initFrom( this ); 0102 QRect r = style()->subControlRect( QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxEditField, this ); 0103 0104 QStyleOptionViewItem viewOption; 0105 viewOption.initFrom( this ); 0106 viewOption.rect = r; 0107 0108 QPainter painter( this ); 0109 itemDelegate()->paint( &painter, viewOption, view()->currentIndex() ); 0110 } 0111 } 0112 0113 void KoResourceSelector::mousePressEvent( QMouseEvent * event ) 0114 { 0115 QStyleOptionComboBox opt; 0116 opt.initFrom( this ); 0117 opt.subControls = QStyle::SC_All; 0118 opt.activeSubControls = QStyle::SC_ComboBoxArrow; 0119 QStyle::SubControl sc = style()->hitTestComplexControl(QStyle::CC_ComboBox, &opt, 0120 mapFromGlobal(event->globalPos()), 0121 this); 0122 // only clicking on combobox arrow shows popup, 0123 // otherwise the resourceApplied signal is send with the current resource 0124 if (sc == QStyle::SC_ComboBoxArrow) 0125 QComboBox::mousePressEvent( event ); 0126 else { 0127 QModelIndex index = view()->currentIndex(); 0128 if( ! index.isValid() ) 0129 return; 0130 0131 KoResource * resource = static_cast<KoResource*>( index.internalPointer() ); 0132 if( resource ) 0133 emit resourceApplied( resource ); 0134 } 0135 } 0136 0137 void KoResourceSelector::mouseMoveEvent( QMouseEvent * event ) 0138 { 0139 QStyleOptionComboBox option; 0140 option.initFrom( this ); 0141 QRect r = style()->subControlRect( QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxEditField, this ); 0142 if (r.contains(event->pos())) 0143 setCursor(Qt::PointingHandCursor); 0144 else 0145 unsetCursor(); 0146 } 0147 0148 void KoResourceSelector::setResourceAdapter(QSharedPointer<KoAbstractResourceServerAdapter>resourceAdapter) 0149 { 0150 Q_ASSERT(resourceAdapter); 0151 setModel(new KoResourceModel(resourceAdapter, this)); 0152 d->updateIndex(this); 0153 0154 connect(resourceAdapter.data(), SIGNAL(resourceAdded(KoResource*)), 0155 this, SLOT(resourceAdded(KoResource*))); 0156 connect(resourceAdapter.data(), SIGNAL(removingResource(KoResource*)), 0157 this, SLOT(resourceRemoved(KoResource*))); 0158 } 0159 0160 void KoResourceSelector::setDisplayMode(DisplayMode mode) 0161 { 0162 if (mode == d->displayMode) 0163 return; 0164 0165 switch(mode) { 0166 case ImageMode: 0167 setItemDelegate(new KoResourceItemDelegate(this)); 0168 setView( new KoResourceItemView(this) ); 0169 break; 0170 case TextMode: 0171 setItemDelegate(new QStyledItemDelegate(this)); 0172 setView(new QListView(this)); 0173 break; 0174 } 0175 0176 d->displayMode = mode; 0177 d->updateIndex(this); 0178 } 0179 0180 void KoResourceSelector::setColumnCount( int columnCount ) 0181 { 0182 KoResourceModel * resourceModel = qobject_cast<KoResourceModel*>(model()); 0183 if (resourceModel) 0184 resourceModel->setColumnCount( columnCount ); 0185 } 0186 0187 void KoResourceSelector::setRowHeight( int rowHeight ) 0188 { 0189 QTableView * tableView = qobject_cast<QTableView*>(view()); 0190 if (tableView) 0191 tableView->verticalHeader()->setDefaultSectionSize( rowHeight ); 0192 } 0193 0194 void KoResourceSelector::indexChanged( int ) 0195 { 0196 QModelIndex index = view()->currentIndex(); 0197 if( ! index.isValid() ) 0198 return; 0199 0200 KoResource * resource = static_cast<KoResource*>( index.internalPointer() ); 0201 if( resource ) 0202 emit resourceSelected( resource ); 0203 } 0204 0205 void KoResourceSelector::resourceAdded(KoResource*) 0206 { 0207 d->updateIndex(this); 0208 } 0209 0210 void KoResourceSelector::resourceRemoved(KoResource*) 0211 { 0212 d->updateIndex(this); 0213 }