1 | /* This file is part of the KDE project |
2 | Copyright (C) 2006 - 2007, 2012 Dag Andersen <danders@get2net.dk> |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to |
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "kptitemmodelbase.h" |
21 | |
22 | #include "kptproject.h" |
23 | #include "kptschedule.h" |
24 | #include "kptdurationspinbox.h" |
25 | #include "kptresourcemodel.h" |
26 | #include "kptresourceallocationmodel.h" |
27 | #include "kptdebug.h" |
28 | |
29 | #include <QApplication> |
30 | #include <QComboBox> |
31 | #include <QKeyEvent> |
32 | #include <QModelIndex> |
33 | #include <QItemSelection> |
34 | #include <QStyleOptionViewItem> |
35 | #include <QTimeEdit> |
36 | #include <QPainter> |
37 | #include <QToolTip> |
38 | #include <QTreeView> |
39 | #include <QHeaderView> |
40 | #include <QStylePainter> |
41 | |
42 | #include <kcombobox.h> |
43 | #include <klineedit.h> |
44 | |
45 | |
46 | namespace KPlato |
47 | { |
48 | |
49 | //-------------------------------------- |
50 | bool ItemDelegate::eventFilter(QObject *object, QEvent *event) |
51 | { |
52 | QWidget *editor = ::qobject_cast<QWidget*>(object); |
53 | if (!editor) { |
54 | return false; |
55 | } |
56 | m_lastHint = Delegate::NoHint; |
57 | if (event->type() == QEvent::KeyPress) { |
58 | QKeyEvent *e = static_cast<QKeyEvent *>(event); |
59 | if ( e->modifiers() & Qt::AltModifier && e->modifiers() & Qt::ControlModifier ) { |
60 | switch ( e->key() ) { |
61 | case Qt::Key_Left: |
62 | m_lastHint = Delegate::EditLeftItem; |
63 | emit commitData(editor); |
64 | emit closeEditor(editor, QAbstractItemDelegate::NoHint ); |
65 | return true; |
66 | case Qt::Key_Right: |
67 | m_lastHint = Delegate::EditRightItem; |
68 | emit commitData(editor); |
69 | emit closeEditor( editor, QAbstractItemDelegate::NoHint ); |
70 | return true; |
71 | case Qt::Key_Down: |
72 | m_lastHint = Delegate::EditDownItem; |
73 | emit commitData(editor); |
74 | emit closeEditor(editor, QAbstractItemDelegate::NoHint ); |
75 | return true; |
76 | case Qt::Key_Up: |
77 | m_lastHint = Delegate::EditUpItem; |
78 | emit commitData(editor); |
79 | emit closeEditor(editor, QAbstractItemDelegate::NoHint ); |
80 | return true; |
81 | default: |
82 | break; |
83 | } |
84 | } |
85 | } |
86 | return QStyledItemDelegate::eventFilter( object, event ); |
87 | } |
88 | |
89 | QSize ItemDelegate::sizeHint( const QStyleOptionViewItem & option, const QModelIndex & index ) const |
90 | { |
91 | // 18 is a bit arbitrary, it gives (most?) editors a usable size |
92 | QSize s = QStyledItemDelegate::sizeHint( option, index ); |
93 | return QSize( s.width(), qMax( s.height(), 18 ) ); |
94 | } |
95 | |
96 | //---------------------- |
97 | CheckStateItemDelegate::CheckStateItemDelegate( QObject *parent ) |
98 | : ItemDelegate( parent ) |
99 | { |
100 | } |
101 | |
102 | bool CheckStateItemDelegate::editorEvent( QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index ) |
103 | { |
104 | Q_ASSERT(event); |
105 | Q_ASSERT(model); |
106 | kDebug(planDbg()); |
107 | |
108 | Qt::ItemFlags flags = model->flags(index); |
109 | if ( ! ( option.state & QStyle::State_Enabled ) || ! ( flags & Qt::ItemIsEnabled ) ) { |
110 | return false; |
111 | } |
112 | |
113 | // make sure that we have a check state |
114 | QVariant value = index.data( Qt::EditRole ); |
115 | if ( ! value.isValid() ) { |
116 | return false; |
117 | } |
118 | |
119 | QStyle *style = QApplication::style(); |
120 | |
121 | // make sure that we have the right event type |
122 | if ( ( event->type() == QEvent::MouseButtonRelease ) || ( event->type() == QEvent::MouseButtonDblClick ) || ( event->type() == QEvent::MouseButtonPress ) ) { |
123 | QStyleOptionViewItemV4 viewOpt( option ); |
124 | initStyleOption( &viewOpt, index ); |
125 | QRect checkRect = style->subElementRect( QStyle::SE_ItemViewItemDecoration, &viewOpt, 0 ); |
126 | QMouseEvent *me = static_cast<QMouseEvent*>( event ); |
127 | if ( me->button() != Qt::LeftButton || ! checkRect.contains( me->pos() ) ) { |
128 | return false; |
129 | } |
130 | if ( ( event->type() == QEvent::MouseButtonPress ) || ( event->type() == QEvent::MouseButtonDblClick ) ) { |
131 | return true; |
132 | } |
133 | } else if ( event->type() == QEvent::KeyPress ) { |
134 | if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space && static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select) { |
135 | return false; |
136 | } |
137 | } else { |
138 | return false; |
139 | } |
140 | Qt::CheckState state = ( static_cast<Qt::CheckState>( value.toInt() ) == Qt::Checked ? Qt::Unchecked : Qt::Checked ); |
141 | return model->setData(index, state, Qt::CheckStateRole); |
142 | } |
143 | |
144 | |
145 | //---------------------- |
146 | DateTimeCalendarDelegate::DateTimeCalendarDelegate( QObject *parent ) |
147 | : ItemDelegate( parent ) |
148 | { |
149 | } |
150 | |
151 | QWidget *DateTimeCalendarDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const |
152 | { |
153 | QDateTimeEdit *editor = new QDateTimeEdit(parent); |
154 | editor->setCalendarPopup( true ); |
155 | editor->installEventFilter(const_cast<DateTimeCalendarDelegate*>(this)); |
156 | return editor; |
157 | } |
158 | |
159 | void DateTimeCalendarDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const |
160 | { |
161 | QDateTime value = index.model()->data(index, Qt::EditRole).toDateTime(); |
162 | |
163 | QDateTimeEdit *e = static_cast<QDateTimeEdit*>(editor); |
164 | e->setDateTime( value ); |
165 | } |
166 | |
167 | void DateTimeCalendarDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
168 | const QModelIndex &index) const |
169 | { |
170 | QDateTimeEdit *e = static_cast<QDateTimeEdit*>(editor); |
171 | model->setData( index, e->dateTime(), Qt::EditRole ); |
172 | } |
173 | |
174 | void DateTimeCalendarDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
175 | { |
176 | kDebug(planDbg())<<editor<<":" <<option.rect<<"," <<editor->sizeHint(); |
177 | QRect r = option.rect; |
178 | //r.setHeight(r.height() 50); |
179 | editor->setGeometry(r); |
180 | } |
181 | |
182 | |
183 | //----------------------------- |
184 | ProgressBarDelegate::ProgressBarDelegate( QObject *parent ) |
185 | : ItemDelegate( parent ) |
186 | { |
187 | } |
188 | |
189 | ProgressBarDelegate::~ProgressBarDelegate() |
190 | { |
191 | } |
192 | |
193 | void ProgressBarDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, |
194 | const QModelIndex &index ) const |
195 | { |
196 | QStyle *style; |
197 | |
198 | QStyleOptionViewItemV4 opt = option; |
199 | initStyleOption( &opt, index ); |
200 | |
201 | style = opt.widget ? opt.widget->style() : QApplication::style(); |
202 | style->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter ); |
203 | |
204 | if ( !( opt.state & QStyle::State_Editing ) ) { |
205 | bool ok = false; |
206 | (void) index.data().toInt(&ok); |
207 | if ( ok ) { |
208 | QStyleOptionProgressBar pbOption; |
209 | pbOption.QStyleOption::operator=( option ); |
210 | initStyleOptionProgressBar( &pbOption, index ); |
211 | |
212 | style->drawControl( QStyle::CE_ProgressBar, &pbOption, painter ); |
213 | // Draw focus, copied from qt |
214 | if (opt.state & QStyle::State_HasFocus) { |
215 | painter->save(); |
216 | QStyleOptionFocusRect o; |
217 | o.QStyleOption::operator=( opt ); |
218 | o.rect = style->subElementRect( QStyle::SE_ItemViewItemFocusRect, &opt, opt.widget ); |
219 | o.state |= QStyle::State_KeyboardFocusChange; |
220 | o.state |= QStyle::State_Item; |
221 | QPalette::ColorGroup cg = ( opt.state & QStyle::State_Enabled ) |
222 | ? QPalette::Normal : QPalette::Disabled; |
223 | o.backgroundColor = opt.palette.color( cg, ( opt.state & QStyle::State_Selected ) |
224 | ? QPalette::Highlight : QPalette::Window ); |
225 | style->drawPrimitive( QStyle::PE_FrameFocusRect, &o, painter, opt.widget ); |
226 | //kDebug(planDbg())<<"Focus"<<o.rect<<opt.rect<<pbOption.rect; |
227 | painter->restore(); |
228 | } |
229 | } else { |
230 | EnumDelegate del; |
231 | del.paint( painter, option, index ); |
232 | } |
233 | } |
234 | } |
235 | |
236 | QSize ProgressBarDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const |
237 | { |
238 | QStyleOptionViewItemV4 opt = option; |
239 | // initStyleOption( &opt, index ); |
240 | |
241 | QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); |
242 | |
243 | QStyleOptionProgressBar pbOption; |
244 | pbOption.QStyleOption::operator=( option ); |
245 | initStyleOptionProgressBar( &pbOption, index ); |
246 | |
247 | return style->sizeFromContents( QStyle::CT_ProgressBar, &pbOption, QSize(), opt.widget ); |
248 | } |
249 | |
250 | void ProgressBarDelegate::initStyleOptionProgressBar( QStyleOptionProgressBar *option, const QModelIndex &index ) const |
251 | { |
252 | option->rect.adjust( 0, 1, 0, -1 ); |
253 | option->minimum = 0; |
254 | int max = index.data( Role::Maximum ).toInt(); |
255 | option->maximum = max > option->minimum ? max : option->minimum + 100; |
256 | option->progress = index.data().toInt(); |
257 | option->text = QString::number( ( option->progress * 100 ) / ( option->maximum - option->minimum ) ) + QLatin1Char( '%' ); |
258 | option->textAlignment = Qt::AlignCenter; |
259 | option->textVisible = true; |
260 | } |
261 | |
262 | QWidget *ProgressBarDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &, const QModelIndex & ) const |
263 | { |
264 | Slider *slider = new Slider( parent ); |
265 | slider->setRange( 0, 100 ); |
266 | slider->setOrientation( Qt::Horizontal ); |
267 | //kDebug(planDbg())<<slider->minimumSizeHint()<<slider->minimumSize(); |
268 | return slider; |
269 | } |
270 | |
271 | void ProgressBarDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const |
272 | { |
273 | QSlider *slider = static_cast<QSlider *>( editor ); |
274 | slider->setValue( index.data( Qt::EditRole ).toInt() ); |
275 | } |
276 | |
277 | void ProgressBarDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const |
278 | { |
279 | QSlider *slider = static_cast<QSlider *>( editor ); |
280 | model->setData( index, slider->value() ); |
281 | } |
282 | |
283 | void ProgressBarDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & ) const |
284 | { |
285 | editor->setGeometry( option.rect ); |
286 | //kDebug(planDbg())<<editor->minimumSizeHint()<<editor->minimumSize()<<editor->geometry()<<editor->size(); |
287 | } |
288 | |
289 | Slider::Slider( QWidget *parent ) |
290 | : QSlider( parent ) |
291 | { |
292 | connect( this, SIGNAL(valueChanged(int)), this, SLOT(updateTip(int)) ); |
293 | } |
294 | |
295 | void Slider::updateTip( int value ) |
296 | { |
297 | QPoint p; |
298 | p.setY( height() / 2 ); |
299 | p.setX( style()->sliderPositionFromValue ( minimum(), maximum(), value, width() ) ); |
300 | |
301 | QString text = QString::number( value ) + QLatin1Char( '%' ); |
302 | QToolTip::showText( mapToGlobal( p ), text, this ); |
303 | } |
304 | |
305 | //-------------------------------------- |
306 | // Hmmm, a bit hacky, but this makes it possible to use index specific editors... |
307 | SelectorDelegate::SelectorDelegate( QObject *parent ) |
308 | : ItemDelegate( parent ) |
309 | { |
310 | } |
311 | |
312 | QWidget *SelectorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex & index ) const |
313 | { |
314 | switch ( index.model()->data( index, Role::EditorType ).toInt() ) { |
315 | case Delegate::EnumEditor: { |
316 | QComboBox *editor = new KComboBox(parent); |
317 | editor->installEventFilter(const_cast<SelectorDelegate*>(this)); |
318 | return editor; |
319 | } |
320 | case Delegate::TimeEditor: { |
321 | QTimeEdit *editor = new QTimeEdit(parent); |
322 | editor->installEventFilter(const_cast<SelectorDelegate*>(this)); |
323 | return editor; |
324 | } |
325 | } |
326 | return 0; // FIXME: What to do? |
327 | } |
328 | |
329 | void SelectorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const |
330 | { |
331 | switch ( index.model()->data( index, Role::EditorType ).toInt() ) { |
332 | case Delegate::EnumEditor: { |
333 | QStringList lst = index.model()->data( index, Role::EnumList ).toStringList(); |
334 | int value = index.model()->data(index, Role::EnumListValue).toInt(); |
335 | QComboBox *box = static_cast<QComboBox*>(editor); |
336 | box->addItems( lst ); |
337 | box->setCurrentIndex( value ); |
338 | return; |
339 | } |
340 | case Delegate::TimeEditor: |
341 | QTime value = index.model()->data(index, Qt::EditRole).toTime(); |
342 | QTimeEdit *e = static_cast<QTimeEdit*>(editor); |
343 | e->setMinimumTime( index.model()->data( index, Role::Minimum ).toTime() ); |
344 | e->setMaximumTime( index.model()->data( index, Role::Maximum ).toTime() ); |
345 | e->setTime( value ); |
346 | return; |
347 | } |
348 | } |
349 | |
350 | void SelectorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
351 | const QModelIndex &index) const |
352 | { |
353 | switch ( index.model()->data( index, Role::EditorType ).toInt() ) { |
354 | case Delegate::EnumEditor: { |
355 | QComboBox *box = static_cast<QComboBox*>(editor); |
356 | int value = box->currentIndex(); |
357 | model->setData( index, value, Qt::EditRole ); |
358 | return; |
359 | } |
360 | case Delegate::TimeEditor: { |
361 | QTimeEdit *e = static_cast<QTimeEdit*>(editor); |
362 | model->setData( index, e->time(), Qt::EditRole ); |
363 | return; |
364 | } |
365 | } |
366 | } |
367 | |
368 | void SelectorDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
369 | { |
370 | QRect r = option.rect; |
371 | editor->setGeometry(r); |
372 | } |
373 | |
374 | EnumDelegate::EnumDelegate( QObject *parent ) |
375 | : ItemDelegate( parent ) |
376 | { |
377 | } |
378 | |
379 | QWidget *EnumDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const |
380 | { |
381 | QComboBox *editor = new KComboBox(parent); |
382 | editor->installEventFilter(const_cast<EnumDelegate*>(this)); |
383 | return editor; |
384 | } |
385 | |
386 | void EnumDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const |
387 | { |
388 | QStringList lst = index.model()->data( index, Role::EnumList ).toStringList(); |
389 | int value = index.model()->data(index, Role::EnumListValue).toInt(); |
390 | |
391 | QComboBox *box = static_cast<QComboBox*>(editor); |
392 | box->addItems( lst ); |
393 | box->setCurrentIndex( value ); |
394 | } |
395 | |
396 | void EnumDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
397 | const QModelIndex &index) const |
398 | { |
399 | QComboBox *box = static_cast<QComboBox*>(editor); |
400 | int value = box->currentIndex(); |
401 | model->setData( index, value, Qt::EditRole ); |
402 | } |
403 | |
404 | void EnumDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
405 | { |
406 | kDebug(planDbg())<<editor<<":" <<option.rect<<"," <<editor->sizeHint(); |
407 | QRect r = option.rect; |
408 | //r.setHeight(r.height() 50); |
409 | editor->setGeometry(r); |
410 | } |
411 | |
412 | //--------------------------- |
413 | RequieredResourceDelegate::RequieredResourceDelegate( QObject *parent ) |
414 | : ItemDelegate( parent ) |
415 | { |
416 | } |
417 | |
418 | QWidget *RequieredResourceDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &index) const |
419 | { |
420 | if ( index.data( Qt::CheckStateRole ).toInt() == Qt::Unchecked ) { |
421 | return 0; |
422 | } |
423 | TreeComboBox *editor = new TreeComboBox(parent); |
424 | editor->installEventFilter(const_cast<RequieredResourceDelegate*>(this)); |
425 | ResourceItemSFModel *m = new ResourceItemSFModel( editor ); |
426 | editor->setModel( m ); |
427 | return editor; |
428 | } |
429 | |
430 | void RequieredResourceDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const |
431 | { |
432 | TreeComboBox *box = static_cast<TreeComboBox*>(editor); |
433 | ResourceItemSFModel *pm = static_cast<ResourceItemSFModel*>( box->model() ); |
434 | ResourceItemModel *rm = qobject_cast<ResourceItemModel*>( pm->sourceModel() ); |
435 | Q_ASSERT( rm ); |
436 | const ResourceAllocationItemModel *model = qobject_cast<const ResourceAllocationItemModel*>( index.model() ); |
437 | Q_ASSERT( model ); |
438 | rm->setProject( model->project() ); |
439 | pm->addFilteredResource( model->resource( index ) ); |
440 | QItemSelectionModel *sm = box->view()->selectionModel(); |
441 | sm->clearSelection(); |
442 | foreach ( const Resource *r, model->required( index ) ) { |
443 | QModelIndex i = pm->mapFromSource( rm->index( r ) ); |
444 | sm->select( i, QItemSelectionModel::Select | QItemSelectionModel::Rows ); |
445 | } |
446 | box->setCurrentIndexes( sm->selectedRows() ); |
447 | box->view()->expandAll(); |
448 | } |
449 | |
450 | void RequieredResourceDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
451 | const QModelIndex &index) const |
452 | { |
453 | TreeComboBox *box = static_cast<TreeComboBox*>(editor); |
454 | |
455 | QAbstractProxyModel *pm = static_cast<QAbstractProxyModel*>( box->model() ); |
456 | ResourceItemModel *rm = qobject_cast<ResourceItemModel*>( pm->sourceModel() ); |
457 | QList<Resource*> lst; |
458 | foreach ( const QModelIndex &i, box->currentIndexes() ) { |
459 | lst << rm->resource( pm->mapToSource( i ) ); |
460 | } |
461 | ResourceAllocationItemModel *mdl = qobject_cast<ResourceAllocationItemModel*>( model ); |
462 | Q_ASSERT( mdl ); |
463 | mdl->setRequired( index, lst ); |
464 | } |
465 | |
466 | void RequieredResourceDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
467 | { |
468 | kDebug(planDbg())<<editor<<":" <<option.rect<<"," <<editor->sizeHint(); |
469 | QRect r = option.rect; |
470 | r.setWidth( qMax( 100, r.width() ) ); |
471 | editor->setGeometry(r); |
472 | } |
473 | |
474 | //------------------------------- |
475 | DurationSpinBoxDelegate::DurationSpinBoxDelegate( QObject *parent ) |
476 | : ItemDelegate( parent ) |
477 | { |
478 | } |
479 | |
480 | QWidget *DurationSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const |
481 | { |
482 | DurationSpinBox *editor = new DurationSpinBox(parent); |
483 | editor->installEventFilter(const_cast<DurationSpinBoxDelegate*>(this)); |
484 | return editor; |
485 | } |
486 | |
487 | void DurationSpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const |
488 | { |
489 | DurationSpinBox *dsb = static_cast<DurationSpinBox*>(editor); |
490 | // dsb->setScales( index.model()->data( index, Role::DurationScales ) ); |
491 | dsb->setMinimumUnit( (Duration::Unit)(index.data( Role::Minimum ).toInt()) ); |
492 | dsb->setMaximumUnit( (Duration::Unit)(index.data( Role::Maximum ).toInt()) ); |
493 | dsb->setUnit( (Duration::Unit)( index.model()->data( index, Role::DurationUnit ).toInt() ) ); |
494 | dsb->setValue( index.model()->data( index, Qt::EditRole ).toDouble() ); |
495 | } |
496 | |
497 | void DurationSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
498 | const QModelIndex &index) const |
499 | { |
500 | DurationSpinBox *dsb = static_cast<DurationSpinBox*>(editor); |
501 | QVariantList lst; |
502 | lst << QVariant( dsb->value() ) << QVariant( (int)( dsb->unit() ) ); |
503 | model->setData( index, QVariant( lst ), Qt::EditRole ); |
504 | } |
505 | |
506 | void DurationSpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
507 | { |
508 | kDebug(planDbg())<<editor<<":" <<option.rect<<"," <<editor->sizeHint(); |
509 | QRect r = option.rect; |
510 | //r.setHeight(r.height() + 50); |
511 | editor->setGeometry(r); |
512 | } |
513 | |
514 | //--------------------------- |
515 | SpinBoxDelegate::SpinBoxDelegate( QObject *parent ) |
516 | : ItemDelegate( parent ) |
517 | { |
518 | } |
519 | |
520 | QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const |
521 | { |
522 | QSpinBox *editor = new QSpinBox(parent); |
523 | editor->installEventFilter(const_cast<SpinBoxDelegate*>(this)); |
524 | return editor; |
525 | } |
526 | |
527 | void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const |
528 | { |
529 | int value = index.model()->data(index, Qt::EditRole).toInt(); |
530 | int min = index.model()->data(index, Role::Minimum).toInt(); |
531 | int max = index.model()->data(index, Role::Maximum).toInt(); |
532 | |
533 | QSpinBox *box = static_cast<QSpinBox*>(editor); |
534 | box->setRange( min, max ); |
535 | box->setValue( value ); |
536 | } |
537 | |
538 | void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
539 | const QModelIndex &index) const |
540 | { |
541 | QSpinBox *box = static_cast<QSpinBox*>(editor); |
542 | model->setData( index, box->value(), Qt::EditRole ); |
543 | } |
544 | |
545 | void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
546 | { |
547 | kDebug(planDbg())<<editor<<":" <<option.rect<<"," <<editor->sizeHint(); |
548 | QRect r = option.rect; |
549 | //r.setHeight(r.height() + 50); |
550 | editor->setGeometry(r); |
551 | } |
552 | |
553 | //--------------------------- |
554 | DoubleSpinBoxDelegate::DoubleSpinBoxDelegate( QObject *parent ) |
555 | : ItemDelegate( parent ) |
556 | { |
557 | } |
558 | |
559 | QWidget *DoubleSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const |
560 | { |
561 | QDoubleSpinBox *editor = new QDoubleSpinBox(parent); |
562 | editor->installEventFilter(const_cast<DoubleSpinBoxDelegate*>(this)); |
563 | return editor; |
564 | } |
565 | |
566 | void DoubleSpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const |
567 | { |
568 | double value = index.model()->data(index, Qt::EditRole).toDouble(); |
569 | double min = 0.0;//index.model()->data(index, Role::Minimum).toInt(); |
570 | double max = 24.0;//index.model()->data(index, Role::Maximum).toInt(); |
571 | |
572 | QDoubleSpinBox *box = static_cast<QDoubleSpinBox*>(editor); |
573 | box->setDecimals( 1 ); |
574 | box->setRange( min, max ); |
575 | box->setValue( value ); |
576 | box->selectAll(); |
577 | } |
578 | |
579 | void DoubleSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
580 | const QModelIndex &index) const |
581 | { |
582 | QDoubleSpinBox *box = static_cast<QDoubleSpinBox*>(editor); |
583 | model->setData( index, box->value(), Qt::EditRole ); |
584 | } |
585 | |
586 | void DoubleSpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
587 | { |
588 | QRect r = option.rect; |
589 | editor->setGeometry(r); |
590 | } |
591 | |
592 | //--------------------------- |
593 | MoneyDelegate::MoneyDelegate( QObject *parent ) |
594 | : ItemDelegate( parent ) |
595 | { |
596 | } |
597 | |
598 | QWidget *MoneyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const |
599 | { |
600 | KLineEdit *editor = new KLineEdit(parent); |
601 | //TODO: validator |
602 | editor->installEventFilter(const_cast<MoneyDelegate*>(this)); |
603 | return editor; |
604 | } |
605 | |
606 | void MoneyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const |
607 | { |
608 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
609 | KLineEdit *e = static_cast<KLineEdit*>(editor); |
610 | e->setText( value ); |
611 | } |
612 | |
613 | void MoneyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
614 | const QModelIndex &index) const |
615 | { |
616 | KLineEdit *e = static_cast<KLineEdit*>(editor); |
617 | model->setData( index, e->text(), Qt::EditRole ); |
618 | } |
619 | |
620 | void MoneyDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
621 | { |
622 | QRect r = option.rect; |
623 | editor->setGeometry(r); |
624 | } |
625 | |
626 | //--------------------------- |
627 | TimeDelegate::TimeDelegate( QObject *parent ) |
628 | : ItemDelegate( parent ) |
629 | { |
630 | } |
631 | |
632 | QWidget *TimeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const |
633 | { |
634 | QTimeEdit *editor = new QTimeEdit(parent); |
635 | editor->installEventFilter(const_cast<TimeDelegate*>(this)); |
636 | return editor; |
637 | } |
638 | |
639 | void TimeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const |
640 | { |
641 | QTime value = index.model()->data(index, Qt::EditRole).toTime(); |
642 | QTimeEdit *e = static_cast<QTimeEdit*>(editor); |
643 | e->setMinimumTime( index.model()->data( index, Role::Minimum ).toTime() ); |
644 | e->setMaximumTime( index.model()->data( index, Role::Maximum ).toTime() ); |
645 | e->setTime( value ); |
646 | } |
647 | |
648 | void TimeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
649 | const QModelIndex &index) const |
650 | { |
651 | QTimeEdit *e = static_cast<QTimeEdit*>(editor); |
652 | model->setData( index, e->time(), Qt::EditRole ); |
653 | } |
654 | |
655 | void TimeDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
656 | { |
657 | QRect r = option.rect; |
658 | editor->setGeometry(r); |
659 | } |
660 | |
661 | //-------------------------- |
662 | ItemModelBase::ItemModelBase( QObject *parent ) |
663 | : QAbstractItemModel( parent ), |
664 | m_project(0), |
665 | m_manager( 0 ), |
666 | m_readWrite( false )//part->isReadWrite() ) |
667 | { |
668 | } |
669 | |
670 | ItemModelBase::~ItemModelBase() |
671 | { |
672 | } |
673 | |
674 | void ItemModelBase::setProject( Project *project ) |
675 | { |
676 | m_project = project; |
677 | } |
678 | |
679 | void ItemModelBase::setScheduleManager( ScheduleManager *sm ) |
680 | { |
681 | m_manager = sm; |
682 | } |
683 | |
684 | void ItemModelBase::slotLayoutChanged() |
685 | { |
686 | kDebug(planDbg()); |
687 | emit layoutChanged(); |
688 | } |
689 | |
690 | void ItemModelBase::slotLayoutToBeChanged() |
691 | { |
692 | kDebug(planDbg()); |
693 | emit layoutAboutToBeChanged(); |
694 | } |
695 | |
696 | bool ItemModelBase::dropAllowed( const QModelIndex &index, int, const QMimeData *data ) |
697 | { |
698 | if ( flags( index ) & Qt::ItemIsDropEnabled ) { |
699 | foreach ( const QString &s, data->formats() ) { |
700 | if ( mimeTypes().contains( s ) ) { |
701 | return true; |
702 | } |
703 | } |
704 | } |
705 | return false; |
706 | } |
707 | |
708 | QVariant ItemModelBase::data( const QModelIndex &index, int role ) const |
709 | { |
710 | if ( index.isValid() && role == Role::ColumnTag ) { |
711 | return columnMap().key( index.column() ); |
712 | } |
713 | return QVariant(); |
714 | } |
715 | |
716 | QVariant ItemModelBase::( int section, Qt::Orientation orientation, int role ) const |
717 | { |
718 | Q_UNUSED(orientation); |
719 | if ( role == Role::ColumnTag ) { |
720 | return columnMap().key( section ); |
721 | } |
722 | return QVariant(); |
723 | } |
724 | |
725 | bool ItemModelBase::setData( const QModelIndex &index, const QVariant &value, int role ) |
726 | { |
727 | Q_UNUSED(index); |
728 | if ( role == Role::ReadWrite ) { |
729 | setReadWrite( value.toBool() ); |
730 | return true; |
731 | } |
732 | return false; |
733 | } |
734 | |
735 | } //namespace KPlato |
736 | |
737 | #include "kptitemmodelbase.moc" |
738 | |
739 | |