1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
13 #include "QCMakeCacheView.h"
15 #include <QHBoxLayout>
16 #include <QHeaderView>
20 #include <QSortFilterProxyModel>
21 #include <QMetaProperty>
22 #include <QApplication>
24 #include "QCMakeWidgets.h"
26 // filter for searches
27 class QCMakeSearchFilter : public QSortFilterProxyModel
30 QCMakeSearchFilter(QObject* o) : QSortFilterProxyModel(o) {}
32 bool filterAcceptsRow(int row, const QModelIndex& p) const
35 const QAbstractItemModel* m = this->sourceModel();
36 QModelIndex idx = m->index(row, 0, p);
38 // if there are no children, get strings for column 0 and 1
39 if(!m->hasChildren(idx))
41 strs.append(m->data(idx).toString());
42 idx = m->index(row, 1, p);
43 strs.append(m->data(idx).toString());
47 // get strings for children entries to compare with
48 // instead of comparing with the parent
49 int num = m->rowCount(idx);
50 for(int i=0; i<num; i++)
52 QModelIndex tmpidx = m->index(i, 0, idx);
53 strs.append(m->data(tmpidx).toString());
54 tmpidx = m->index(i, 1, idx);
55 strs.append(m->data(tmpidx).toString());
59 // check all strings for a match
60 foreach(QString str, strs)
62 if(str.contains(this->filterRegExp()))
72 // filter for searches
73 class QCMakeAdvancedFilter : public QSortFilterProxyModel
76 QCMakeAdvancedFilter(QObject* o)
77 : QSortFilterProxyModel(o), ShowAdvanced(false) {}
79 void setShowAdvanced(bool f)
81 this->ShowAdvanced = f;
84 bool showAdvanced() const { return this->ShowAdvanced; }
90 bool filterAcceptsRow(int row, const QModelIndex& p) const
92 const QAbstractItemModel* m = this->sourceModel();
93 QModelIndex idx = m->index(row, 0, p);
95 // if there are no children
96 if(!m->hasChildren(idx))
98 bool adv = m->data(idx, QCMakeCacheModel::AdvancedRole).toBool();
99 if(!adv || (adv && this->ShowAdvanced))
107 int num = m->rowCount(idx);
108 for(int i=0; i<num; i++)
110 bool accept = this->filterAcceptsRow(i, idx);
120 QCMakeCacheView::QCMakeCacheView(QWidget* p)
123 // hook up our model and search/filter proxies
124 this->CacheModel = new QCMakeCacheModel(this);
125 this->AdvancedFilter = new QCMakeAdvancedFilter(this);
126 this->AdvancedFilter->setSourceModel(this->CacheModel);
127 this->AdvancedFilter->setDynamicSortFilter(true);
128 this->SearchFilter = new QCMakeSearchFilter(this);
129 this->SearchFilter->setSourceModel(this->AdvancedFilter);
130 this->SearchFilter->setFilterCaseSensitivity(Qt::CaseInsensitive);
131 this->SearchFilter->setDynamicSortFilter(true);
132 this->setModel(this->SearchFilter);
134 // our delegate for creating our editors
135 QCMakeCacheModelDelegate* delegate = new QCMakeCacheModelDelegate(this);
136 this->setItemDelegate(delegate);
138 this->setUniformRowHeights(true);
140 this->setEditTriggers(QAbstractItemView::AllEditTriggers);
142 // tab, backtab doesn't step through items
143 this->setTabKeyNavigation(false);
145 this->setRootIsDecorated(false);
148 bool QCMakeCacheView::event(QEvent* e)
150 if(e->type() == QEvent::Show)
152 this->header()->setDefaultSectionSize(this->viewport()->width()/2);
154 return QTreeView::event(e);
157 QCMakeCacheModel* QCMakeCacheView::cacheModel() const
159 return this->CacheModel;
162 QModelIndex QCMakeCacheView::moveCursor(CursorAction act,
163 Qt::KeyboardModifiers mod)
165 // want home/end to go to begin/end of rows, not columns
168 return this->model()->index(0, 1);
170 else if(act == MoveEnd)
172 return this->model()->index(this->model()->rowCount()-1, 1);
174 return QTreeView::moveCursor(act, mod);
177 void QCMakeCacheView::setShowAdvanced(bool s)
179 #if QT_VERSION >= 040300
180 // new 4.3 API that needs to be called. what about an older Qt?
181 this->SearchFilter->invalidate();
184 this->AdvancedFilter->setShowAdvanced(s);
187 bool QCMakeCacheView::showAdvanced() const
189 return this->AdvancedFilter->showAdvanced();
192 void QCMakeCacheView::setSearchFilter(const QString& s)
194 this->SearchFilter->setFilterFixedString(s);
197 QCMakeCacheModel::QCMakeCacheModel(QObject* p)
198 : QStandardItemModel(p),
203 this->ShowNewProperties = true;
205 labels << tr("Name") << tr("Value");
206 this->setHorizontalHeaderLabels(labels);
209 QCMakeCacheModel::~QCMakeCacheModel()
213 static uint qHash(const QCMakeProperty& p)
218 void QCMakeCacheModel::setShowNewProperties(bool f)
220 this->ShowNewProperties = f;
223 void QCMakeCacheModel::clear()
225 this->QStandardItemModel::clear();
226 this->NewPropertyCount = 0;
229 labels << tr("Name") << tr("Value");
230 this->setHorizontalHeaderLabels(labels);
233 void QCMakeCacheModel::setProperties(const QCMakePropertyList& props)
235 QSet<QCMakeProperty> newProps, newProps2;
237 if(this->ShowNewProperties)
239 newProps = props.toSet();
240 newProps2 = newProps;
241 QSet<QCMakeProperty> oldProps = this->properties().toSet();
242 oldProps.intersect(newProps);
243 newProps.subtract(oldProps);
244 newProps2.subtract(newProps);
248 newProps2 = props.toSet();
251 bool b = this->blockSignals(true);
254 this->NewPropertyCount = newProps.size();
258 QCMakePropertyList newP = newProps.toList();
259 QCMakePropertyList newP2 = newProps2.toList();
263 foreach(QCMakeProperty p, newP)
265 this->insertRow(row_count);
266 this->setPropertyData(this->index(row_count, 0), p, true);
269 foreach(QCMakeProperty p, newP2)
271 this->insertRow(row_count);
272 this->setPropertyData(this->index(row_count, 0), p, false);
276 else if(this->View == GroupView)
278 QMap<QString, QCMakePropertyList> newPropsTree;
279 this->breakProperties(newProps, newPropsTree);
280 QMap<QString, QCMakePropertyList> newPropsTree2;
281 this->breakProperties(newProps2, newPropsTree2);
283 QStandardItem* root = this->invisibleRootItem();
285 foreach(QString key, newPropsTree.keys())
287 QCMakePropertyList props2 = newPropsTree[key];
289 QList<QStandardItem*> parentItems;
291 new QStandardItem(key.isEmpty() ? tr("Ungrouped Entries") : key)
293 parentItems.append(new QStandardItem());
294 parentItems[0]->setData(QBrush(QColor(255,100,100)), Qt::BackgroundColorRole);
295 parentItems[1]->setData(QBrush(QColor(255,100,100)), Qt::BackgroundColorRole);
296 parentItems[0]->setData(1, GroupRole);
297 parentItems[1]->setData(1, GroupRole);
298 root->appendRow(parentItems);
300 int num = props2.size();
301 for(int i=0; i<num; i++)
303 QCMakeProperty prop = props2[i];
304 QList<QStandardItem*> items;
305 items.append(new QStandardItem());
306 items.append(new QStandardItem());
307 parentItems[0]->appendRow(items);
308 this->setPropertyData(this->indexFromItem(items[0]), prop, true);
312 foreach(QString key, newPropsTree2.keys())
314 QCMakePropertyList props2 = newPropsTree2[key];
316 QStandardItem* parentItem =
317 new QStandardItem(key.isEmpty() ? tr("Ungrouped Entries") : key);
318 root->appendRow(parentItem);
319 parentItem->setData(1, GroupRole);
321 int num = props2.size();
322 for(int i=0; i<num; i++)
324 QCMakeProperty prop = props2[i];
325 QList<QStandardItem*> items;
326 items.append(new QStandardItem());
327 items.append(new QStandardItem());
328 parentItem->appendRow(items);
329 this->setPropertyData(this->indexFromItem(items[0]), prop, false);
334 this->blockSignals(b);
338 QCMakeCacheModel::ViewType QCMakeCacheModel::viewType() const
343 void QCMakeCacheModel::setViewType(QCMakeCacheModel::ViewType t)
347 QCMakePropertyList props = this->properties();
348 QCMakePropertyList oldProps;
349 int numNew = this->NewPropertyCount;
350 int numTotal = props.count();
351 for(int i=numNew; i<numTotal; i++)
353 oldProps.append(props[i]);
356 bool b = this->blockSignals(true);
358 this->setProperties(oldProps);
359 this->setProperties(props);
360 this->blockSignals(b);
364 void QCMakeCacheModel::setPropertyData(const QModelIndex& idx1,
365 const QCMakeProperty& prop, bool isNew)
367 QModelIndex idx2 = idx1.sibling(idx1.row(), 1);
369 this->setData(idx1, prop.Key, Qt::DisplayRole);
370 this->setData(idx1, prop.Help, QCMakeCacheModel::HelpRole);
371 this->setData(idx1, prop.Type, QCMakeCacheModel::TypeRole);
372 this->setData(idx1, prop.Advanced, QCMakeCacheModel::AdvancedRole);
374 if(prop.Type == QCMakeProperty::BOOL)
376 int check = prop.Value.toBool() ? Qt::Checked : Qt::Unchecked;
377 this->setData(idx2, check, Qt::CheckStateRole);
381 this->setData(idx2, prop.Value, Qt::DisplayRole);
383 this->setData(idx2, prop.Help, QCMakeCacheModel::HelpRole);
385 if (!prop.Strings.isEmpty())
387 this->setData(idx1, prop.Strings, QCMakeCacheModel::StringsRole);
392 this->setData(idx1, QBrush(QColor(255,100,100)), Qt::BackgroundColorRole);
393 this->setData(idx2, QBrush(QColor(255,100,100)), Qt::BackgroundColorRole);
397 void QCMakeCacheModel::getPropertyData(const QModelIndex& idx1,
398 QCMakeProperty& prop) const
400 QModelIndex idx2 = idx1.sibling(idx1.row(), 1);
402 prop.Key = this->data(idx1, Qt::DisplayRole).toString();
403 prop.Help = this->data(idx1, HelpRole).toString();
404 prop.Type = static_cast<QCMakeProperty::PropertyType>(this->data(idx1, TypeRole).toInt());
405 prop.Advanced = this->data(idx1, AdvancedRole).toBool();
406 prop.Strings = this->data(idx1, QCMakeCacheModel::StringsRole).toStringList();
407 if(prop.Type == QCMakeProperty::BOOL)
409 int check = this->data(idx2, Qt::CheckStateRole).toInt();
410 prop.Value = check == Qt::Checked;
414 prop.Value = this->data(idx2, Qt::DisplayRole).toString();
418 QString QCMakeCacheModel::prefix(const QString& s)
420 QString prefix = s.section('_', 0, 0);
428 void QCMakeCacheModel::breakProperties(const QSet<QCMakeProperty>& props,
429 QMap<QString, QCMakePropertyList>& result)
431 QMap<QString, QCMakePropertyList> tmp;
432 // return a map of properties grouped by prefixes, and sorted
433 foreach(QCMakeProperty p, props)
435 QString prefix = QCMakeCacheModel::prefix(p.Key);
436 tmp[prefix].append(p);
438 // sort it and re-org any properties with only one sub item
439 QCMakePropertyList reorgProps;
440 QMap<QString, QCMakePropertyList>::iterator iter;
441 for(iter = tmp.begin(); iter != tmp.end();)
443 if(iter->count() == 1)
445 reorgProps.append((*iter)[0]);
446 iter = tmp.erase(iter);
454 if(reorgProps.count())
456 tmp[QString()] += reorgProps;
461 QCMakePropertyList QCMakeCacheModel::properties() const
463 QCMakePropertyList props;
465 if(!this->rowCount())
470 QList<QModelIndex> idxs;
471 idxs.append(this->index(0,0));
473 // walk the entire model for property entries
474 // this works regardless of a flat view or a tree view
475 while(!idxs.isEmpty())
477 QModelIndex idx = idxs.last();
478 if(this->hasChildren(idx) && this->rowCount(idx))
480 idxs.append(this->index(0,0, idx));
484 if(!data(idx, GroupRole).toInt())
488 this->getPropertyData(idx, prop);
492 // go to the next in the tree
493 while(!idxs.isEmpty() && (
494 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) && QT_VERSION < QT_VERSION_CHECK(5, 1, 0)
495 (idxs.last().row()+1) >= rowCount(idxs.last().parent()) ||
497 !idxs.last().sibling(idxs.last().row()+1, 0).isValid()))
503 idxs.last() = idxs.last().sibling(idxs.last().row()+1, 0);
511 bool QCMakeCacheModel::insertProperty(QCMakeProperty::PropertyType t,
512 const QString& name, const QString& description,
513 const QVariant& value, bool advanced)
518 prop.Help = description;
520 prop.Advanced = advanced;
522 //insert at beginning
524 this->setPropertyData(this->index(0,0), prop, true);
525 this->NewPropertyCount++;
529 void QCMakeCacheModel::setEditEnabled(bool e)
531 this->EditEnabled = e;
534 bool QCMakeCacheModel::editEnabled() const
536 return this->EditEnabled;
539 int QCMakeCacheModel::newPropertyCount() const
541 return this->NewPropertyCount;
544 Qt::ItemFlags QCMakeCacheModel::flags (const QModelIndex& idx) const
546 Qt::ItemFlags f = QStandardItemModel::flags(idx);
547 if(!this->EditEnabled)
549 f &= ~Qt::ItemIsEditable;
552 if(QCMakeProperty::BOOL == this->data(idx, TypeRole).toInt())
554 f |= Qt::ItemIsUserCheckable;
559 QModelIndex QCMakeCacheModel::buddy(const QModelIndex& idx) const
561 if(!this->hasChildren(idx) &&
562 this->data(idx, TypeRole).toInt() != QCMakeProperty::BOOL)
564 return this->index(idx.row(), 1, idx.parent());
569 QCMakeCacheModelDelegate::QCMakeCacheModelDelegate(QObject* p)
570 : QItemDelegate(p), FileDialogFlag(false)
574 void QCMakeCacheModelDelegate::setFileDialogFlag(bool f)
576 this->FileDialogFlag = f;
579 QWidget* QCMakeCacheModelDelegate::createEditor(QWidget* p,
580 const QStyleOptionViewItem&, const QModelIndex& idx) const
582 QModelIndex var = idx.sibling(idx.row(), 0);
583 int type = var.data(QCMakeCacheModel::TypeRole).toInt();
584 if(type == QCMakeProperty::BOOL)
588 else if(type == QCMakeProperty::PATH)
590 QCMakePathEditor* editor =
591 new QCMakePathEditor(p,
592 var.data(Qt::DisplayRole).toString());
593 QObject::connect(editor, SIGNAL(fileDialogExists(bool)), this,
594 SLOT(setFileDialogFlag(bool)));
597 else if(type == QCMakeProperty::FILEPATH)
599 QCMakeFilePathEditor* editor =
600 new QCMakeFilePathEditor(p,
601 var.data(Qt::DisplayRole).toString());
602 QObject::connect(editor, SIGNAL(fileDialogExists(bool)), this,
603 SLOT(setFileDialogFlag(bool)));
606 else if(type == QCMakeProperty::STRING &&
607 var.data(QCMakeCacheModel::StringsRole).isValid())
609 QCMakeComboBox* editor =
610 new QCMakeComboBox(p, var.data(QCMakeCacheModel::StringsRole).toStringList());
611 editor->setFrame(false);
615 QLineEdit* editor = new QLineEdit(p);
616 editor->setFrame(false);
620 bool QCMakeCacheModelDelegate::editorEvent(QEvent* e, QAbstractItemModel* model,
621 const QStyleOptionViewItem& option, const QModelIndex& index)
623 Qt::ItemFlags flags = model->flags(index);
624 if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
625 || !(flags & Qt::ItemIsEnabled))
630 QVariant value = index.data(Qt::CheckStateRole);
631 if (!value.isValid())
636 if ((e->type() == QEvent::MouseButtonRelease)
637 || (e->type() == QEvent::MouseButtonDblClick))
639 // eat the double click events inside the check rect
640 if (e->type() == QEvent::MouseButtonDblClick)
645 else if (e->type() == QEvent::KeyPress)
647 if(static_cast<QKeyEvent*>(e)->key() != Qt::Key_Space &&
648 static_cast<QKeyEvent*>(e)->key() != Qt::Key_Select)
658 Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
659 ? Qt::Unchecked : Qt::Checked);
660 bool success = model->setData(index, state, Qt::CheckStateRole);
663 this->recordChange(model, index);
668 // Issue 205903 fixed in Qt 4.5.0.
669 // Can remove this function and FileDialogFlag when minimum Qt version is 4.5
670 bool QCMakeCacheModelDelegate::eventFilter(QObject* object, QEvent* evt)
672 // workaround for what looks like a bug in Qt on Mac OS X
673 // where it doesn't create a QWidget wrapper for the native file dialog
674 // so the Qt library ends up assuming the focus was lost to something else
676 if(evt->type() == QEvent::FocusOut && this->FileDialogFlag)
680 return QItemDelegate::eventFilter(object, evt);
683 void QCMakeCacheModelDelegate::setModelData(QWidget* editor,
684 QAbstractItemModel* model, const QModelIndex& index ) const
686 QItemDelegate::setModelData(editor, model, index);
687 const_cast<QCMakeCacheModelDelegate*>(this)->recordChange(model, index);
690 QSize QCMakeCacheModelDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
692 QSize sz = QItemDelegate::sizeHint(option, index);
693 QStyle *style = QApplication::style();
695 // increase to checkbox size
696 QStyleOptionButton opt;
697 opt.QStyleOption::operator=(option);
698 sz = sz.expandedTo(style->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt, NULL).size());
703 QSet<QCMakeProperty> QCMakeCacheModelDelegate::changes() const
708 void QCMakeCacheModelDelegate::clearChanges()
713 void QCMakeCacheModelDelegate::recordChange(QAbstractItemModel* model, const QModelIndex& index)
715 QModelIndex idx = index;
716 QAbstractItemModel* mymodel = model;
717 while(qobject_cast<QAbstractProxyModel*>(mymodel))
719 idx = static_cast<QAbstractProxyModel*>(mymodel)->mapToSource(idx);
720 mymodel = static_cast<QAbstractProxyModel*>(mymodel)->sourceModel();
722 QCMakeCacheModel* cache_model = qobject_cast<QCMakeCacheModel*>(mymodel);
723 if(cache_model && idx.isValid())
726 idx = idx.sibling(idx.row(), 0);
727 cache_model->getPropertyData(idx, prop);
729 // clean out an old one
730 QSet<QCMakeProperty>::iterator iter = mChanges.find(prop);
731 if(iter != mChanges.end())
733 mChanges.erase(iter);
735 // now add the new item
736 mChanges.insert(prop);