ENH: fix uppercase version so defines are not upper as well
[cmake.git] / Source / QtDialog / QCMakeCacheView.cxx
blob1ede0679a0c05003bc52feed9fdff9601791f2aa
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: QCMakeCacheView.cxx,v $
5 Language: C++
6 Date: $Date: 2008-06-30 18:29:08 $
7 Version: $Revision: 1.38 $
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>
22 #include <QEvent>
23 #include <QStyle>
24 #include <QKeyEvent>
25 #include <QSortFilterProxyModel>
27 #include "QCMakeWidgets.h"
29 // filter for searches
30 class QCMakeSearchFilter : public QSortFilterProxyModel
32 public:
33 QCMakeSearchFilter(QObject* o) : QSortFilterProxyModel(o) {}
34 protected:
35 bool filterAcceptsRow(int row, const QModelIndex& p) const
37 QStringList strs;
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());
48 else
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()))
67 return true;
71 return false;
75 // filter for searches
76 class QCMakeAdvancedFilter : public QSortFilterProxyModel
78 public:
79 QCMakeAdvancedFilter(QObject* o)
80 : QSortFilterProxyModel(o), ShowAdvanced(false) {}
82 void setShowAdvanced(bool f)
84 this->ShowAdvanced = f;
85 this->invalidate();
87 bool showAdvanced() const { return this->ShowAdvanced; }
89 protected:
91 bool 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))
104 return true;
106 return false;
109 // check children
110 int num = m->rowCount(idx);
111 for(int i=0; i<num; i++)
113 bool accept = this->filterAcceptsRow(i, idx);
114 if(accept)
116 return true;
119 return false;
123 QCMakeCacheView::QCMakeCacheView(QWidget* p)
124 : QTreeView(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
170 if(act == MoveHome)
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();
186 #endif
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),
203 EditEnabled(true),
204 NewPropertyCount(0),
205 View(FlatView)
207 QStringList labels;
208 labels << tr("Name") << tr("Value");
209 this->setHorizontalHeaderLabels(labels);
212 QCMakeCacheModel::~QCMakeCacheModel()
216 static uint qHash(const QCMakeProperty& p)
218 return qHash(p.Key);
221 void QCMakeCacheModel::clear()
223 this->QStandardItemModel::clear();
224 this->NewPropertyCount = 0;
226 QStringList labels;
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);
243 this->clear();
244 this->NewPropertyCount = newProps.size();
246 if(View == FlatView)
248 QCMakePropertyList newP = newProps.toList();
249 QCMakePropertyList newP2 = newProps2.toList();
250 qSort(newP);
251 qSort(newP2);
252 int rowCount = 0;
253 foreach(QCMakeProperty p, newP)
255 this->insertRow(rowCount);
256 this->setPropertyData(this->index(rowCount, 0), p, true);
257 rowCount++;
259 foreach(QCMakeProperty p, newP2)
261 this->insertRow(rowCount);
262 this->setPropertyData(this->index(rowCount, 0), p, false);
263 rowCount++;
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;
280 parentItems.append(
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);
318 this->reset();
321 QCMakeCacheModel::ViewType QCMakeCacheModel::viewType() const
323 return this->View;
326 void QCMakeCacheModel::setViewType(QCMakeCacheModel::ViewType t)
328 this->View = 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);
340 this->clear();
341 this->setProperties(oldProps);
342 this->setProperties(props);
343 this->blockSignals(b);
344 this->reset();
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);
362 else
364 this->setData(idx2, prop.Value, Qt::DisplayRole);
366 this->setData(idx2, prop.Help, QCMakeCacheModel::HelpRole);
368 if(isNew)
370 this->setData(idx1, QBrush(QColor(255,100,100)), Qt::BackgroundColorRole);
371 this->setData(idx2, QBrush(QColor(255,100,100)), Qt::BackgroundColorRole);
375 void QCMakeCacheModel::getPropertyData(const QModelIndex& idx1,
376 QCMakeProperty& prop) const
378 QModelIndex idx2 = idx1.sibling(idx1.row(), 1);
380 prop.Key = this->data(idx1, Qt::DisplayRole).toString();
381 prop.Help = this->data(idx1, HelpRole).toString();
382 prop.Type = static_cast<QCMakeProperty::PropertyType>(this->data(idx1, TypeRole).toInt());
383 prop.Advanced = this->data(idx1, AdvancedRole).toBool();
384 if(prop.Type == QCMakeProperty::BOOL)
386 int check = this->data(idx2, Qt::CheckStateRole).toInt();
387 prop.Value = check == Qt::Checked;
389 else
391 prop.Value = this->data(idx2, Qt::DisplayRole).toString();
395 QString QCMakeCacheModel::prefix(const QString& s)
397 QString prefix = s.section('_', 0, 0);
398 if(prefix == s)
400 prefix = QString();
402 return prefix;
405 void QCMakeCacheModel::breakProperties(const QSet<QCMakeProperty>& props,
406 QMap<QString, QCMakePropertyList>& result)
408 QMap<QString, QCMakePropertyList> tmp;
409 // return a map of properties grouped by prefixes, and sorted
410 foreach(QCMakeProperty p, props)
412 QString prefix = QCMakeCacheModel::prefix(p.Key);
413 tmp[prefix].append(p);
415 // sort it and re-org any properties with only one sub item
416 QCMakePropertyList reorgProps;
417 QMap<QString, QCMakePropertyList>::iterator iter;
418 for(iter = tmp.begin(); iter != tmp.end();)
420 if(iter->count() == 1)
422 reorgProps.append((*iter)[0]);
423 iter = tmp.erase(iter);
425 else
427 qSort(*iter);
428 ++iter;
431 if(reorgProps.count())
433 tmp[QString()] += reorgProps;
435 result = tmp;
438 QCMakePropertyList QCMakeCacheModel::properties() const
440 QCMakePropertyList props;
442 if(!this->rowCount())
444 return props;
447 QList<QModelIndex> idxs;
448 idxs.append(this->index(0,0));
450 // walk the entire model for property entries
451 // this works regardless of a flat view or a tree view
452 while(!idxs.isEmpty())
454 QModelIndex idx = idxs.last();
455 if(this->hasChildren(idx) && this->rowCount(idx))
457 idxs.append(this->index(0,0, idx));
459 else
461 // get data
462 QCMakeProperty prop;
463 this->getPropertyData(idx, prop);
464 props.append(prop);
466 // go to the next in the tree
467 while(!idxs.isEmpty() && !idxs.last().sibling(idxs.last().row()+1, 0).isValid())
469 idxs.removeLast();
471 if(!idxs.isEmpty())
473 idxs.last() = idxs.last().sibling(idxs.last().row()+1, 0);
478 return props;
481 bool QCMakeCacheModel::insertProperty(QCMakeProperty::PropertyType t,
482 const QString& name, const QString& description,
483 const QVariant& value, bool advanced)
485 QCMakeProperty prop;
486 prop.Key = name;
487 prop.Value = value;
488 prop.Help = description;
489 prop.Type = t;
490 prop.Advanced = advanced;
492 //insert at beginning
493 this->insertRow(0);
494 this->setPropertyData(this->index(0,0), prop, true);
495 this->NewPropertyCount++;
496 return true;
499 void QCMakeCacheModel::setEditEnabled(bool e)
501 this->EditEnabled = e;
504 bool QCMakeCacheModel::editEnabled() const
506 return this->EditEnabled;
509 int QCMakeCacheModel::newPropertyCount() const
511 return this->NewPropertyCount;
514 Qt::ItemFlags QCMakeCacheModel::flags (const QModelIndex& idx) const
516 Qt::ItemFlags f = QStandardItemModel::flags(idx);
517 if(!this->EditEnabled)
519 f &= ~Qt::ItemIsEditable;
521 if(QCMakeProperty::BOOL == this->data(idx, TypeRole).toInt())
523 f |= Qt::ItemIsUserCheckable;
525 return f;
528 QModelIndex QCMakeCacheModel::buddy(const QModelIndex& idx) const
530 if(!this->hasChildren(idx) &&
531 this->data(idx, TypeRole).toInt() != QCMakeProperty::BOOL)
533 return this->index(idx.row(), 1, idx.parent());
535 return idx;
538 QCMakeCacheModelDelegate::QCMakeCacheModelDelegate(QObject* p)
539 : QItemDelegate(p), FileDialogFlag(false)
543 void QCMakeCacheModelDelegate::setFileDialogFlag(bool f)
545 this->FileDialogFlag = f;
548 QWidget* QCMakeCacheModelDelegate::createEditor(QWidget* p,
549 const QStyleOptionViewItem&, const QModelIndex& idx) const
551 QModelIndex var = idx.sibling(idx.row(), 0);
552 int type = var.data(QCMakeCacheModel::TypeRole).toInt();
553 if(type == QCMakeProperty::BOOL)
555 return NULL;
557 else if(type == QCMakeProperty::PATH)
559 QCMakePathEditor* editor =
560 new QCMakePathEditor(p,
561 var.data(Qt::DisplayRole).toString());
562 QObject::connect(editor, SIGNAL(fileDialogExists(bool)), this,
563 SLOT(setFileDialogFlag(bool)));
564 return editor;
566 else if(type == QCMakeProperty::FILEPATH)
568 QCMakeFilePathEditor* editor =
569 new QCMakeFilePathEditor(p,
570 var.data(Qt::DisplayRole).toString());
571 QObject::connect(editor, SIGNAL(fileDialogExists(bool)), this,
572 SLOT(setFileDialogFlag(bool)));
573 return editor;
576 return new QLineEdit(p);
579 bool QCMakeCacheModelDelegate::editorEvent(QEvent* e, QAbstractItemModel* model,
580 const QStyleOptionViewItem& option, const QModelIndex& index)
582 Qt::ItemFlags flags = model->flags(index);
583 if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
584 || !(flags & Qt::ItemIsEnabled))
586 return false;
589 QVariant value = index.data(Qt::CheckStateRole);
590 if (!value.isValid())
592 return false;
595 if ((e->type() == QEvent::MouseButtonRelease)
596 || (e->type() == QEvent::MouseButtonDblClick))
598 // eat the double click events inside the check rect
599 if (e->type() == QEvent::MouseButtonDblClick)
601 return true;
604 else if (e->type() == QEvent::KeyPress)
606 if(static_cast<QKeyEvent*>(e)->key() != Qt::Key_Space &&
607 static_cast<QKeyEvent*>(e)->key() != Qt::Key_Select)
609 return false;
612 else
614 return false;
617 Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
618 ? Qt::Unchecked : Qt::Checked);
619 return model->setData(index, state, Qt::CheckStateRole);
622 bool QCMakeCacheModelDelegate::eventFilter(QObject* object, QEvent* event)
624 // workaround for what looks like a bug in Qt on Mac OS X
625 // where it doesn't create a QWidget wrapper for the native file dialog
626 // so the Qt library ends up assuming the focus was lost to something else
627 if(event->type() == QEvent::FocusOut && this->FileDialogFlag)
629 return false;
631 return QItemDelegate::eventFilter(object, event);