PropertyEditor.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <QApplication>
00028 # include <QPainter>
00029 #endif
00030 
00031 #include <Base/Console.h>
00032 #include <App/Application.h>
00033 #include <App/Document.h>
00034 #include "PropertyEditor.h"
00035 #include "PropertyItemDelegate.h"
00036 #include "PropertyModel.h"
00037 
00038 using namespace Gui::PropertyEditor;
00039 
00040 PropertyEditor::PropertyEditor(QWidget *parent)
00041     : QTreeView(parent), autoupdate(false), committing(false), delaybuild(false)
00042 {
00043     propertyModel = new PropertyModel(this);
00044     setModel(propertyModel);
00045 
00046     PropertyItemDelegate* delegate = new PropertyItemDelegate(this);
00047     delegate->setItemEditorFactory(new PropertyItemEditorFactory);
00048     setItemDelegate(delegate);
00049 
00050     setAlternatingRowColors(true);
00051     setRootIsDecorated(true);
00052 }
00053 
00054 PropertyEditor::~PropertyEditor()
00055 {
00056 }
00057 
00058 void PropertyEditor::setAutomaticDocumentUpdate(bool v)
00059 {
00060     autoupdate = v;
00061 }
00062 
00063 bool PropertyEditor::isAutomaticDocumentUpdate(bool) const
00064 {
00065     return autoupdate;
00066 }
00067 
00068 QStyleOptionViewItem PropertyEditor::viewOptions() const
00069 {
00070     QStyleOptionViewItem option = QTreeView::viewOptions();
00071     option.showDecorationSelected = true;
00072     return option;
00073 }
00074 
00075 void PropertyEditor::closeEditor (QWidget * editor, QAbstractItemDelegate::EndEditHint hint)
00076 {
00077     if (autoupdate) {
00078         App::Document* doc = App::GetApplication().getActiveDocument();
00079         if (doc && doc->isTouched())
00080             doc->recompute();
00081     }
00082     QTreeView::closeEditor(editor, hint);
00083 }
00084 
00085 void PropertyEditor::commitData (QWidget * editor)
00086 {
00087     committing = true;
00088     QTreeView::commitData(editor);
00089     committing = false;
00090     if (delaybuild) {
00091         delaybuild = false;
00092         propertyModel->buildUp(std::map<std::string, std::vector<App::Property*> >());
00093     }
00094 }
00095 
00096 void PropertyEditor::editorDestroyed (QObject * editor)
00097 {
00098     QTreeView::editorDestroyed(editor);
00099 }
00100 
00101 void PropertyEditor::currentChanged ( const QModelIndex & current, const QModelIndex & previous )
00102 {
00103     QTreeView::currentChanged(current, previous);
00104     if (previous.isValid())
00105         closePersistentEditor(model()->buddy(previous));
00106     if (current.isValid())
00107         openPersistentEditor(model()->buddy(current));
00108 }
00109 
00110 void PropertyEditor::drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const
00111 {
00112     QTreeView::drawBranches(painter, rect, index);
00113 
00114     QStyleOptionViewItem opt = viewOptions();
00115     PropertyItem *property = static_cast<PropertyItem*>(index.internalPointer());
00116     if (property && property->isSeparator()) {
00117         painter->fillRect(rect, opt.palette.dark());
00118     //} else if (selectionModel()->isSelected(index)) {
00119     //    painter->fillRect(rect, opt.palette.brush(QPalette::Highlight));
00120     }
00121 
00122     //QPen savedPen = painter->pen();
00123     //QColor color = static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &opt));
00124     //painter->setPen(QPen(color));
00125     //painter->drawLine(rect.x(), rect.bottom(), rect.right(), rect.bottom());
00126     //painter->setPen(savedPen);
00127 }
00128 
00129 void PropertyEditor::buildUp(const std::map<std::string, std::vector<App::Property*> >& props)
00130 {
00131     if (committing) {
00132         Base::Console().Warning("While committing the data to the property the selection has changed.\n");
00133         delaybuild = true;
00134         return;
00135     }
00136 
00137     QModelIndex index = this->currentIndex();
00138     QStringList propertyPath = propertyModel->propertyPathFromIndex(index);
00139     propertyModel->buildUp(props);
00140     if (!propertyPath.isEmpty()) {
00141         QModelIndex index = propertyModel->propertyIndexFromPath(propertyPath);
00142         this->setCurrentIndex(index);
00143     }
00144 }
00145 
00146 #include "moc_PropertyEditor.cpp"

Generated on Wed Nov 23 19:00:30 2011 for FreeCAD by  doxygen 1.6.1