CMake/Source/QtDialog/QCMakeCacheView.cxx

288 lines
7.2 KiB
C++
Raw Normal View History

2007-11-02 18:55:57 +03:00
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
2007-11-02 18:50:17 +03:00
#include "QCMakeCacheView.h"
#include <QToolButton>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QEvent>
QCMakeCacheView::QCMakeCacheView(QWidget* p)
: QTableView(p), Init(false)
2007-11-02 18:50:17 +03:00
{
QCMakeCacheModel* m = new QCMakeCacheModel(this);
this->setModel(m);
this->horizontalHeader()->setStretchLastSection(true);
this->verticalHeader()->hide();
QCMakeCacheModelDelegate* delegate = new QCMakeCacheModelDelegate(this);
this->setItemDelegate(delegate);
}
void QCMakeCacheView::showEvent(QShowEvent* e)
2007-11-02 18:50:17 +03:00
{
if(!this->Init)
2007-11-02 18:50:17 +03:00
{
// initialize the table view column size
int colWidth = this->columnWidth(0) + this->columnWidth(1);
this->setColumnWidth(0, colWidth/2);
this->setColumnWidth(1, colWidth/2);
this->Init = true;
2007-11-02 18:50:17 +03:00
}
return QTableView::showEvent(e);
2007-11-02 18:50:17 +03:00
}
QCMakeCacheModel* QCMakeCacheView::cacheModel() const
{
return qobject_cast<QCMakeCacheModel*>(this->model());
}
QModelIndex QCMakeCacheView::moveCursor(CursorAction act,
Qt::KeyboardModifiers mod)
{
// tab through values only (not names)
QModelIndex current = this->currentIndex();
if(act == MoveNext)
{
if(!current.isValid())
{
return this->model()->index(0, 1);
}
else if(current.column() == 0)
{
return this->model()->index(current.row(), 1);
}
else
{
return this->model()->index(current.row()+1, 1);
}
}
else if(act == MovePrevious)
{
if(!current.isValid())
{
return this->model()->index(0, 1);
}
else
{
return this->model()->index(current.row()-1, 1);
}
}
return QTableView::moveCursor(act, mod);
}
2007-11-02 18:50:17 +03:00
QCMakeCacheModel::QCMakeCacheModel(QObject* p)
: QAbstractTableModel(p), IsDirty(false)
2007-11-02 18:50:17 +03:00
{
}
QCMakeCacheModel::~QCMakeCacheModel()
{
}
bool QCMakeCacheModel::isDirty() const
{
return this->IsDirty;
}
2007-11-02 18:50:17 +03:00
void QCMakeCacheModel::setProperties(const QCMakeCachePropertyList& props)
{
this->Properties = props;
this->reset();
this->IsDirty = false;
2007-11-02 18:50:17 +03:00
}
QCMakeCachePropertyList QCMakeCacheModel::properties() const
{
return this->Properties;
}
int QCMakeCacheModel::columnCount (const QModelIndex& /*parent*/ ) const
2007-11-02 18:50:17 +03:00
{
return 2;
}
QVariant QCMakeCacheModel::data (const QModelIndex& index, int role) const
2007-11-02 18:50:17 +03:00
{
if(index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole))
{
2007-11-02 18:50:17 +03:00
return this->Properties[index.row()].Key;
}
else if(index.column() == 0 && role == Qt::ToolTipRole)
{
return this->data(index, Qt::DisplayRole).toString() + "\n" +
this->data(index, QCMakeCacheModel::HelpRole).toString();
}
2007-11-02 18:50:17 +03:00
else if(index.column() == 1 && (role == Qt::DisplayRole || role == Qt::EditRole))
{
if(this->Properties[index.row()].Type != QCMakeCacheProperty::BOOL)
{
return this->Properties[index.row()].Value;
}
}
else if(index.column() == 1 && role == Qt::CheckStateRole)
{
if(this->Properties[index.row()].Type == QCMakeCacheProperty::BOOL)
{
return this->Properties[index.row()].Value.toBool() ? Qt::Checked : Qt::Unchecked;
}
}
2007-11-02 18:50:17 +03:00
else if(role == QCMakeCacheModel::HelpRole)
{
2007-11-02 18:50:17 +03:00
return this->Properties[index.row()].Help;
}
2007-11-02 18:50:17 +03:00
else if(role == QCMakeCacheModel::TypeRole)
{
2007-11-02 18:50:17 +03:00
return this->Properties[index.row()].Type;
}
2007-11-02 18:50:17 +03:00
else if(role == QCMakeCacheModel::AdvancedRole)
{
2007-11-02 18:50:17 +03:00
return this->Properties[index.row()].Advanced;
}
2007-11-02 18:50:17 +03:00
return QVariant();
}
QModelIndex QCMakeCacheModel::parent (const QModelIndex& /*index*/) const
2007-11-02 18:50:17 +03:00
{
return QModelIndex();
}
int QCMakeCacheModel::rowCount (const QModelIndex& parent) const
2007-11-02 18:50:17 +03:00
{
if(parent.isValid())
{
2007-11-02 18:50:17 +03:00
return 0;
}
2007-11-02 18:50:17 +03:00
return this->Properties.count();
}
QVariant QCMakeCacheModel::headerData (int section, Qt::Orientation orient, int role) const
2007-11-02 18:50:17 +03:00
{
// return header labels
2007-11-02 18:50:17 +03:00
if(role == Qt::DisplayRole && orient == Qt::Horizontal)
{
2007-11-02 18:50:17 +03:00
return section == 0 ? "Name" : "Value";
}
2007-11-02 18:50:17 +03:00
return QVariant();
}
Qt::ItemFlags QCMakeCacheModel::flags (const QModelIndex& index) const
2007-11-02 18:50:17 +03:00
{
Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
// all column 1's are editable
2007-11-02 18:50:17 +03:00
if(index.column() == 1)
{
f |= Qt::ItemIsEditable;
// booleans are editable in place
if(this->Properties[index.row()].Type == QCMakeCacheProperty::BOOL)
{
f |= Qt::ItemIsUserCheckable;
}
}
return f;
2007-11-02 18:50:17 +03:00
}
bool QCMakeCacheModel::setData (const QModelIndex& index, const QVariant& value, int role)
2007-11-02 18:50:17 +03:00
{
if(index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole))
{
2007-11-02 18:50:17 +03:00
this->Properties[index.row()].Key = value.toString();
this->IsDirty = true;
emit this->dataChanged(index, index);
}
2007-11-02 18:50:17 +03:00
else if(index.column() == 1 && (role == Qt::DisplayRole || role == Qt::EditRole))
{
2007-11-02 18:50:17 +03:00
this->Properties[index.row()].Value = value.toString();
this->IsDirty = true;
emit this->dataChanged(index, index);
}
else if(index.column() == 1 && (role == Qt::CheckStateRole))
{
this->Properties[index.row()].Value = value.toInt() == Qt::Checked;
this->IsDirty = true;
emit this->dataChanged(index, index);
}
2007-11-02 18:50:17 +03:00
return false;
}
QCMakeCacheModelDelegate::QCMakeCacheModelDelegate(QObject* p)
: QItemDelegate(p)
{
}
QWidget* QCMakeCacheModelDelegate::createEditor(QWidget* parent,
const QStyleOptionViewItem&, const QModelIndex& index) const
{
QVariant type = index.data(QCMakeCacheModel::TypeRole);
if(type == QCMakeCacheProperty::BOOL)
{
return NULL;
}
2007-11-02 18:50:17 +03:00
else if(type == QCMakeCacheProperty::PATH)
{
return new QCMakeCachePathEditor(index.data().toString(), false, parent);
}
2007-11-02 18:50:17 +03:00
else if(type == QCMakeCacheProperty::FILEPATH)
{
return new QCMakeCachePathEditor(index.data().toString(), true, parent);
}
2007-11-02 18:50:17 +03:00
return new QLineEdit(parent);
}
QCMakeCachePathEditor::QCMakeCachePathEditor(const QString& file, bool fp, QWidget* p)
: QWidget(p), LineEdit(this), IsFilePath(fp)
2007-11-02 18:50:17 +03:00
{
QHBoxLayout* l = new QHBoxLayout(this);
l->setMargin(0);
l->setSpacing(0);
l->addWidget(&this->LineEdit);
QToolButton* tb = new QToolButton(this);
tb->setText("...");
l->addWidget(tb);
QObject::connect(tb, SIGNAL(clicked(bool)),
this, SLOT(chooseFile()));
this->LineEdit.setText(file);
tb->setFocusProxy(&this->LineEdit);
this->setFocusProxy(&this->LineEdit);
}
void QCMakeCachePathEditor::chooseFile()
{
QString path;
if(this->IsFilePath)
{
path = QFileDialog::getOpenFileName(this, "TODO");
}
else
{
path = QFileDialog::getExistingDirectory(this, "TODO", this->value());
}
2007-11-02 18:50:17 +03:00
if(!path.isEmpty())
{
2007-11-02 18:50:17 +03:00
this->LineEdit.setText(path);
}
2007-11-02 18:50:17 +03:00
}