1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: QCMakeCacheView.cxx,v $
6 Date: $Date: 2009-03-26 15:42:37 $
7 Version: $Revision: 1.40 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
18 #include "QCMakeCacheView.h"
20 #include <QHBoxLayout>
21 #include <QHeaderView>
25 #include <QSortFilterProxyModel>
27 #include "QCMakeWidgets.h"
29 // filter for searches
30 class QCMakeSearchFilter
: public QSortFilterProxyModel
33 QCMakeSearchFilter(QObject
* o
) : QSortFilterProxyModel(o
) {}
35 bool filterAcceptsRow(int row
, const QModelIndex
& p
) const
38 const QAbstractItemModel
* m
= this->sourceModel();
39 QModelIndex idx
= m
->index(row
, 0, p
);
41 // if there are no children, get strings for column 0 and 1
42 if(!m
->hasChildren(idx
))
44 strs
.append(m
->data(idx
).toString());
45 idx
= m
->index(row
, 1, p
);
46 strs
.append(m
->data(idx
).toString());
50 // get strings for children entries to compare with
51 // instead of comparing with the parent
52 int num
= m
->rowCount(idx
);
53 for(int i
=0; i
<num
; i
++)
55 QModelIndex tmpidx
= m
->index(i
, 0, idx
);
56 strs
.append(m
->data(tmpidx
).toString());
57 tmpidx
= m
->index(i
, 1, idx
);
58 strs
.append(m
->data(tmpidx
).toString());
62 // check all strings for a match
63 foreach(QString str
, strs
)
65 if(str
.contains(this->filterRegExp()))
75 // filter for searches
76 class QCMakeAdvancedFilter
: public QSortFilterProxyModel
79 QCMakeAdvancedFilter(QObject
* o
)
80 : QSortFilterProxyModel(o
), ShowAdvanced(false) {}
82 void setShowAdvanced(bool f
)
84 this->ShowAdvanced
= f
;
87 bool showAdvanced() const { return this->ShowAdvanced
; }
93 bool filterAcceptsRow(int row
, const QModelIndex
& p
) const
95 const QAbstractItemModel
* m
= this->sourceModel();
96 QModelIndex idx
= m
->index(row
, 0, p
);
98 // if there are no children
99 if(!m
->hasChildren(idx
))
101 bool adv
= m
->data(idx
, QCMakeCacheModel::AdvancedRole
).toBool();
102 if(!adv
|| (adv
&& this->ShowAdvanced
))
110 int num
= m
->rowCount(idx
);
111 for(int i
=0; i
<num
; i
++)
113 bool accept
= this->filterAcceptsRow(i
, idx
);
123 QCMakeCacheView::QCMakeCacheView(QWidget
* p
)
126 // hook up our model and search/filter proxies
127 this->CacheModel
= new QCMakeCacheModel(this);
128 this->AdvancedFilter
= new QCMakeAdvancedFilter(this);
129 this->AdvancedFilter
->setSourceModel(this->CacheModel
);
130 this->AdvancedFilter
->setDynamicSortFilter(true);
131 this->SearchFilter
= new QCMakeSearchFilter(this);
132 this->SearchFilter
->setSourceModel(this->AdvancedFilter
);
133 this->SearchFilter
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
134 this->SearchFilter
->setDynamicSortFilter(true);
135 this->setModel(this->SearchFilter
);
137 // our delegate for creating our editors
138 QCMakeCacheModelDelegate
* delegate
= new QCMakeCacheModelDelegate(this);
139 this->setItemDelegate(delegate
);
141 this->setEditTriggers(QAbstractItemView::DoubleClicked
|
142 QAbstractItemView::SelectedClicked
|
143 QAbstractItemView::EditKeyPressed
|
144 QAbstractItemView::AnyKeyPressed
);
146 // tab, backtab doesn't step through items
147 this->setTabKeyNavigation(false);
149 this->setRootIsDecorated(false);
152 bool QCMakeCacheView::event(QEvent
* e
)
154 if(e
->type() == QEvent::Show
)
156 this->header()->setDefaultSectionSize(this->viewport()->width()/2);
158 return QTreeView::event(e
);
161 QCMakeCacheModel
* QCMakeCacheView::cacheModel() const
163 return this->CacheModel
;
166 QModelIndex
QCMakeCacheView::moveCursor(CursorAction act
,
167 Qt::KeyboardModifiers mod
)
169 // want home/end to go to begin/end of rows, not columns
172 return this->model()->index(0, 1);
174 else if(act
== MoveEnd
)
176 return this->model()->index(this->model()->rowCount()-1, 1);
178 return QTreeView::moveCursor(act
, mod
);
181 void QCMakeCacheView::setShowAdvanced(bool s
)
183 #if QT_VERSION >= 040300
184 // new 4.3 api that needs to be called. what about an older Qt?
185 this->SearchFilter
->invalidate();
188 this->AdvancedFilter
->setShowAdvanced(s
);
191 bool QCMakeCacheView::showAdvanced() const
193 return this->AdvancedFilter
->showAdvanced();
196 void QCMakeCacheView::setSearchFilter(const QString
& s
)
198 this->SearchFilter
->setFilterFixedString(s
);
201 QCMakeCacheModel::QCMakeCacheModel(QObject
* p
)
202 : QStandardItemModel(p
),
208 labels
<< tr("Name") << tr("Value");
209 this->setHorizontalHeaderLabels(labels
);
212 QCMakeCacheModel::~QCMakeCacheModel()
216 static uint
qHash(const QCMakeProperty
& p
)
221 void QCMakeCacheModel::clear()
223 this->QStandardItemModel::clear();
224 this->NewPropertyCount
= 0;
227 labels
<< tr("Name") << tr("Value");
228 this->setHorizontalHeaderLabels(labels
);
231 void QCMakeCacheModel::setProperties(const QCMakePropertyList
& props
)
233 QSet
<QCMakeProperty
> newProps
= props
.toSet();
234 QSet
<QCMakeProperty
> newProps2
= newProps
;
235 QSet
<QCMakeProperty
> oldProps
= this->properties().toSet();
237 oldProps
.intersect(newProps
);
238 newProps
.subtract(oldProps
);
239 newProps2
.subtract(newProps
);
241 bool b
= this->blockSignals(true);
244 this->NewPropertyCount
= newProps
.size();
248 QCMakePropertyList newP
= newProps
.toList();
249 QCMakePropertyList newP2
= newProps2
.toList();
253 foreach(QCMakeProperty p
, newP
)
255 this->insertRow(rowCount
);
256 this->setPropertyData(this->index(rowCount
, 0), p
, true);
259 foreach(QCMakeProperty p
, newP2
)
261 this->insertRow(rowCount
);
262 this->setPropertyData(this->index(rowCount
, 0), p
, false);
266 else if(this->View
== GroupView
)
268 QMap
<QString
, QCMakePropertyList
> newPropsTree
;
269 this->breakProperties(newProps
, newPropsTree
);
270 QMap
<QString
, QCMakePropertyList
> newPropsTree2
;
271 this->breakProperties(newProps2
, newPropsTree2
);
273 QStandardItem
* root
= this->invisibleRootItem();
275 foreach(QString key
, newPropsTree
.keys())
277 QCMakePropertyList props
= newPropsTree
[key
];
279 QList
<QStandardItem
*> parentItems
;
281 new QStandardItem(key
.isEmpty() ? tr("Ungrouped Entries") : key
)
283 parentItems
.append(new QStandardItem());
284 parentItems
[0]->setData(QBrush(QColor(255,100,100)), Qt::BackgroundColorRole
);
285 parentItems
[1]->setData(QBrush(QColor(255,100,100)), Qt::BackgroundColorRole
);
286 root
->appendRow(parentItems
);
288 foreach(QCMakeProperty prop
, props
)
290 QList
<QStandardItem
*> items
;
291 items
.append(new QStandardItem());
292 items
.append(new QStandardItem());
293 parentItems
[0]->appendRow(items
);
294 this->setPropertyData(this->indexFromItem(items
[0]), prop
, true);
298 foreach(QString key
, newPropsTree2
.keys())
300 QCMakePropertyList props
= newPropsTree2
[key
];
302 QStandardItem
* parentItem
=
303 new QStandardItem(key
.isEmpty() ? tr("Ungrouped Entries") : key
);
304 root
->appendRow(parentItem
);
306 foreach(QCMakeProperty prop
, props
)
308 QList
<QStandardItem
*> items
;
309 items
.append(new QStandardItem());
310 items
.append(new QStandardItem());
311 parentItem
->appendRow(items
);
312 this->setPropertyData(this->indexFromItem(items
[0]), prop
, false);
317 this->blockSignals(b
);
321 QCMakeCacheModel::ViewType
QCMakeCacheModel::viewType() const
326 void QCMakeCacheModel::setViewType(QCMakeCacheModel::ViewType t
)
330 QCMakePropertyList props
= this->properties();
331 QCMakePropertyList oldProps
;
332 int numNew
= this->NewPropertyCount
;
333 int numTotal
= props
.count();
334 for(int i
=numNew
; i
<numTotal
; i
++)
336 oldProps
.append(props
[i
]);
339 bool b
= this->blockSignals(true);
341 this->setProperties(oldProps
);
342 this->setProperties(props
);
343 this->blockSignals(b
);
347 void QCMakeCacheModel::setPropertyData(const QModelIndex
& idx1
,
348 const QCMakeProperty
& prop
, bool isNew
)
350 QModelIndex idx2
= idx1
.sibling(idx1
.row(), 1);
352 this->setData(idx1
, prop
.Key
, Qt::DisplayRole
);
353 this->setData(idx1
, prop
.Help
, QCMakeCacheModel::HelpRole
);
354 this->setData(idx1
, prop
.Type
, QCMakeCacheModel::TypeRole
);
355 this->setData(idx1
, prop
.Advanced
, QCMakeCacheModel::AdvancedRole
);
357 if(prop
.Type
== QCMakeProperty::BOOL
)
359 int check
= prop
.Value
.toBool() ? Qt::Checked
: Qt::Unchecked
;
360 this->setData(idx2
, check
, Qt::CheckStateRole
);
364 this->setData(idx2
, prop
.Value
, Qt::DisplayRole
);
366 this->setData(idx2
, prop
.Help
, QCMakeCacheModel::HelpRole
);
368 if (!prop
.Strings
.isEmpty())
370 this->setData(idx1
, prop
.Strings
, QCMakeCacheModel::StringsRole
);
375 this->setData(idx1
, QBrush(QColor(255,100,100)), Qt::BackgroundColorRole
);
376 this->setData(idx2
, QBrush(QColor(255,100,100)), Qt::BackgroundColorRole
);
380 void QCMakeCacheModel::getPropertyData(const QModelIndex
& idx1
,
381 QCMakeProperty
& prop
) const
383 QModelIndex idx2
= idx1
.sibling(idx1
.row(), 1);
385 prop
.Key
= this->data(idx1
, Qt::DisplayRole
).toString();
386 prop
.Help
= this->data(idx1
, HelpRole
).toString();
387 prop
.Type
= static_cast<QCMakeProperty::PropertyType
>(this->data(idx1
, TypeRole
).toInt());
388 prop
.Advanced
= this->data(idx1
, AdvancedRole
).toBool();
389 prop
.Strings
= this->data(idx1
, QCMakeCacheModel::StringsRole
).toStringList();
390 if(prop
.Type
== QCMakeProperty::BOOL
)
392 int check
= this->data(idx2
, Qt::CheckStateRole
).toInt();
393 prop
.Value
= check
== Qt::Checked
;
397 prop
.Value
= this->data(idx2
, Qt::DisplayRole
).toString();
401 QString
QCMakeCacheModel::prefix(const QString
& s
)
403 QString prefix
= s
.section('_', 0, 0);
411 void QCMakeCacheModel::breakProperties(const QSet
<QCMakeProperty
>& props
,
412 QMap
<QString
, QCMakePropertyList
>& result
)
414 QMap
<QString
, QCMakePropertyList
> tmp
;
415 // return a map of properties grouped by prefixes, and sorted
416 foreach(QCMakeProperty p
, props
)
418 QString prefix
= QCMakeCacheModel::prefix(p
.Key
);
419 tmp
[prefix
].append(p
);
421 // sort it and re-org any properties with only one sub item
422 QCMakePropertyList reorgProps
;
423 QMap
<QString
, QCMakePropertyList
>::iterator iter
;
424 for(iter
= tmp
.begin(); iter
!= tmp
.end();)
426 if(iter
->count() == 1)
428 reorgProps
.append((*iter
)[0]);
429 iter
= tmp
.erase(iter
);
437 if(reorgProps
.count())
439 tmp
[QString()] += reorgProps
;
444 QCMakePropertyList
QCMakeCacheModel::properties() const
446 QCMakePropertyList props
;
448 if(!this->rowCount())
453 QList
<QModelIndex
> idxs
;
454 idxs
.append(this->index(0,0));
456 // walk the entire model for property entries
457 // this works regardless of a flat view or a tree view
458 while(!idxs
.isEmpty())
460 QModelIndex idx
= idxs
.last();
461 if(this->hasChildren(idx
) && this->rowCount(idx
))
463 idxs
.append(this->index(0,0, idx
));
469 this->getPropertyData(idx
, prop
);
472 // go to the next in the tree
473 while(!idxs
.isEmpty() && !idxs
.last().sibling(idxs
.last().row()+1, 0).isValid())
479 idxs
.last() = idxs
.last().sibling(idxs
.last().row()+1, 0);
487 bool QCMakeCacheModel::insertProperty(QCMakeProperty::PropertyType t
,
488 const QString
& name
, const QString
& description
,
489 const QVariant
& value
, bool advanced
)
494 prop
.Help
= description
;
496 prop
.Advanced
= advanced
;
498 //insert at beginning
500 this->setPropertyData(this->index(0,0), prop
, true);
501 this->NewPropertyCount
++;
505 void QCMakeCacheModel::setEditEnabled(bool e
)
507 this->EditEnabled
= e
;
510 bool QCMakeCacheModel::editEnabled() const
512 return this->EditEnabled
;
515 int QCMakeCacheModel::newPropertyCount() const
517 return this->NewPropertyCount
;
520 Qt::ItemFlags
QCMakeCacheModel::flags (const QModelIndex
& idx
) const
522 Qt::ItemFlags f
= QStandardItemModel::flags(idx
);
523 if(!this->EditEnabled
)
525 f
&= ~Qt::ItemIsEditable
;
528 if(QCMakeProperty::BOOL
== this->data(idx
, TypeRole
).toInt())
530 f
|= Qt::ItemIsUserCheckable
;
535 QModelIndex
QCMakeCacheModel::buddy(const QModelIndex
& idx
) const
537 if(!this->hasChildren(idx
) &&
538 this->data(idx
, TypeRole
).toInt() != QCMakeProperty::BOOL
)
540 return this->index(idx
.row(), 1, idx
.parent());
545 QCMakeCacheModelDelegate::QCMakeCacheModelDelegate(QObject
* p
)
546 : QItemDelegate(p
), FileDialogFlag(false)
550 void QCMakeCacheModelDelegate::setFileDialogFlag(bool f
)
552 this->FileDialogFlag
= f
;
555 QWidget
* QCMakeCacheModelDelegate::createEditor(QWidget
* p
,
556 const QStyleOptionViewItem
&, const QModelIndex
& idx
) const
558 QModelIndex var
= idx
.sibling(idx
.row(), 0);
559 int type
= var
.data(QCMakeCacheModel::TypeRole
).toInt();
560 if(type
== QCMakeProperty::BOOL
)
564 else if(type
== QCMakeProperty::PATH
)
566 QCMakePathEditor
* editor
=
567 new QCMakePathEditor(p
,
568 var
.data(Qt::DisplayRole
).toString());
569 QObject::connect(editor
, SIGNAL(fileDialogExists(bool)), this,
570 SLOT(setFileDialogFlag(bool)));
573 else if(type
== QCMakeProperty::FILEPATH
)
575 QCMakeFilePathEditor
* editor
=
576 new QCMakeFilePathEditor(p
,
577 var
.data(Qt::DisplayRole
).toString());
578 QObject::connect(editor
, SIGNAL(fileDialogExists(bool)), this,
579 SLOT(setFileDialogFlag(bool)));
582 else if(type
== QCMakeProperty::STRING
&&
583 var
.data(QCMakeCacheModel::StringsRole
).isValid())
585 QCMakeComboBox
* editor
=
586 new QCMakeComboBox(p
, var
.data(QCMakeCacheModel::StringsRole
).toStringList());
587 editor
->setFrame(false);
591 QLineEdit
* editor
= new QLineEdit(p
);
592 editor
->setFrame(false);
596 bool QCMakeCacheModelDelegate::editorEvent(QEvent
* e
, QAbstractItemModel
* model
,
597 const QStyleOptionViewItem
& option
, const QModelIndex
& index
)
599 Qt::ItemFlags flags
= model
->flags(index
);
600 if (!(flags
& Qt::ItemIsUserCheckable
) || !(option
.state
& QStyle::State_Enabled
)
601 || !(flags
& Qt::ItemIsEnabled
))
606 QVariant value
= index
.data(Qt::CheckStateRole
);
607 if (!value
.isValid())
612 if ((e
->type() == QEvent::MouseButtonRelease
)
613 || (e
->type() == QEvent::MouseButtonDblClick
))
615 // eat the double click events inside the check rect
616 if (e
->type() == QEvent::MouseButtonDblClick
)
621 else if (e
->type() == QEvent::KeyPress
)
623 if(static_cast<QKeyEvent
*>(e
)->key() != Qt::Key_Space
&&
624 static_cast<QKeyEvent
*>(e
)->key() != Qt::Key_Select
)
634 Qt::CheckState state
= (static_cast<Qt::CheckState
>(value
.toInt()) == Qt::Checked
635 ? Qt::Unchecked
: Qt::Checked
);
636 return model
->setData(index
, state
, Qt::CheckStateRole
);
639 bool QCMakeCacheModelDelegate::eventFilter(QObject
* object
, QEvent
* event
)
641 // workaround for what looks like a bug in Qt on Mac OS X
642 // where it doesn't create a QWidget wrapper for the native file dialog
643 // so the Qt library ends up assuming the focus was lost to something else
644 if(event
->type() == QEvent::FocusOut
&& this->FileDialogFlag
)
648 return QItemDelegate::eventFilter(object
, event
);