24 #include <KMessageBox>
25 #include <KPushButton>
26 #include <KStandardDirs>
29 #include <QtCore/QAbstractTableModel>
30 #include <QtCore/QFile>
31 #include <QtCore/QFileInfo>
34 #include <QMouseEvent>
35 #include <QStyledItemDelegate>
43 class TemplatesModel :
public QAbstractTableModel
46 TemplatesModel(
QObject *parent = 0 )
47 : QAbstractTableModel( parent )
52 virtual int rowCount(
const QModelIndex &parent = QModelIndex() )
const
54 if ( !parent.isValid() ) {
55 return mTemplates.count();
61 virtual int columnCount(
const QModelIndex &parent = QModelIndex() )
const
63 if ( !parent.isValid() ) {
70 virtual QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole )
const
72 if ( !index.isValid() || index.row() >= mTemplates.count() || index.column() >= 2 ) {
76 if ( role == Qt::DisplayRole ) {
77 if ( index.column() == 0 ) {
78 return mTemplates[ index.row() ].displayName;
80 return mTemplates[ index.row() ].fileName;
84 if ( role == Qt::UserRole ) {
85 return mTemplates[ index.row() ].isDeletable;
91 virtual bool removeRows(
int row,
int count,
const QModelIndex &parent = QModelIndex() )
93 if ( parent.isValid() || row < 0 || row >= mTemplates.count() ) {
97 beginRemoveRows( parent, row, row + count - 1 );
98 for (
int i = 0; i < count; ++i ) {
99 if ( !QFile::remove( mTemplates[ row ].fileName ) ) {
102 mTemplates.removeAt( row );
113 const QStringList files =
114 KGlobal::dirs()->findAllResources(
"data", QLatin1String(
"kaddressbook/csv-templates/*.desktop"),
115 KStandardDirs::Recursive | KStandardDirs::NoDuplicates );
116 for (
int i = 0; i < files.count(); ++i ) {
117 KConfig config( files.at( i ), KConfig::SimpleConfig );
119 if ( !config.hasGroup(
"csv column map" ) ) {
123 KConfigGroup group( &config,
"Misc" );
125 info.displayName = group.readEntry(
"Name" );
126 info.fileName = files.at( i );
128 const QFileInfo fileInfo( info.fileName );
129 info.isDeletable = QFileInfo( fileInfo.absolutePath() ).isWritable();
131 mTemplates.append( info );
136 bool templatesAvailable()
const
138 return !mTemplates.isEmpty();
142 QList<TemplateInfo> mTemplates;
145 class TemplateSelectionDelegate :
public QStyledItemDelegate
148 explicit TemplateSelectionDelegate(
QObject *parent = 0 )
149 : QStyledItemDelegate( parent ), mIcon( QLatin1String(
"list-remove") )
153 void paint( QPainter *painter,
const QStyleOptionViewItem &option,
154 const QModelIndex &index )
const
156 QStyledItemDelegate::paint( painter, option, index );
158 if ( index.data( Qt::UserRole ).toBool() ) {
159 mIcon.paint( painter, option.rect, Qt::AlignRight );
163 QSize sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
165 QSize hint = QStyledItemDelegate::sizeHint( option, index );
167 if ( index.data( Qt::UserRole ).toBool() ) {
168 hint.setWidth( hint.width() + 16 );
174 bool editorEvent( QEvent *event, QAbstractItemModel *model,
175 const QStyleOptionViewItem &option,
const QModelIndex &index )
177 if ( event->type() == QEvent::MouseButtonRelease && index.data( Qt::UserRole ).toBool() ) {
178 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent*
>( event );
179 QRect buttonRect = option.rect;
180 buttonRect.setLeft( buttonRect.right() - 16 );
182 if ( buttonRect.contains( mouseEvent->pos() ) ) {
183 const QString templateName = index.data( Qt::DisplayRole ).toString();
184 if ( KMessageBox::questionYesNo(
186 i18nc(
"@label",
"Do you really want to delete template '%1'?",
187 templateName ) ) == KMessageBox::Yes ) {
188 model->removeRows( index.row(), 1 );
194 return QStyledItemDelegate::editorEvent( event, model, option, index );
204 setCaption( i18nc(
"@title:window",
"Template Selection" ) );
205 setButtons( Ok | Cancel );
207 KVBox *wdg =
new KVBox(
this );
208 setMainWidget( wdg );
210 new QLabel( i18nc(
"@info",
"Please select a template, that matches the CSV file:" ), wdg );
212 mView =
new QListView( wdg );
214 mView->setModel(
new TemplatesModel(
this ) );
215 mView->setItemDelegate(
new TemplateSelectionDelegate(
this ) );
217 button( Ok )->setEnabled(
false );
218 connect( mView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
219 this, SLOT(updateButtons()) );
229 const QModelIndex rowIndex = mView->currentIndex();
230 const QModelIndex index = mView->model()->index( rowIndex.row(), 1 );
232 return index.data( Qt::DisplayRole ).toString();
235 void TemplateSelectionDialog::updateButtons()
237 button( Ok )->setEnabled( mView->currentIndex().isValid() );
240 #include "templateselectiondialog.moc"
TemplateSelectionDialog(QWidget *parent=0)
bool templatesAvailable() const
QString selectedTemplate() const